]> git.stg.codes - stg.git/blobdiff - projects/stargazer/user_impl.cpp
Merge branch 'stg-2.409'
[stg.git] / projects / stargazer / user_impl.cpp
index f8335525a00acb3020df41ab84649a8e5e4b9ff7..09e71a35bc2051a006f755eff573c967a3097335 100644 (file)
@@ -80,7 +80,7 @@ USER_IMPL::USER_IMPL(const SETTINGS * s,
            const USERS * u,
            const SERVICES & svcs)
     : users(u),
-      property(s->GetScriptsDir()),
+      property(*s),
       WriteServLog(GetStgLogger()),
       lastScanMessages(0),
       id(0),
@@ -134,6 +134,8 @@ USER_IMPL::USER_IMPL(const SETTINGS * s,
       userdata7(property.userdata7),
       userdata8(property.userdata8),
       userdata9(property.userdata9),
+      sessionUploadModTime(stgTime),
+      sessionDownloadModTime(stgTime),
       passiveNotifier(this),
       disabledNotifier(this),
       tariffNotifier(this),
@@ -150,7 +152,7 @@ USER_IMPL::USER_IMPL(const SETTINGS_IMPL * s,
                      const USERS * u,
                      const SERVICES & svcs)
     : users(u),
-      property(s->GetScriptsDir()),
+      property(*s),
       WriteServLog(GetStgLogger()),
       lastScanMessages(0),
       id(0),
@@ -204,6 +206,8 @@ USER_IMPL::USER_IMPL(const SETTINGS_IMPL * s,
       userdata7(property.userdata7),
       userdata8(property.userdata8),
       userdata9(property.userdata9),
+      sessionUploadModTime(stgTime),
+      sessionDownloadModTime(stgTime),
       passiveNotifier(this),
       disabledNotifier(this),
       tariffNotifier(this),
@@ -218,6 +222,7 @@ void USER_IMPL::Init()
 {
 password = "*_EMPTY_PASSWORD_*";
 tariffName = NO_TARIFF_NAME;
+tariff = tariffs->FindByName(tariffName);
 ips = StrToIPS("*");
 lastWriteStat = stgTime + random() % settings->GetStatWritePeriod();
 lastWriteDetailedStat = stgTime;
@@ -237,7 +242,7 @@ pthread_mutex_init(&mutex, &attr);
 USER_IMPL::USER_IMPL(const USER_IMPL & u)
     : USER(),
       users(u.users),
-      property(u.settings->GetScriptsDir()),
+      property(*u.settings),
       WriteServLog(GetStgLogger()),
       lastScanMessages(0),
       login(u.login),
@@ -298,6 +303,8 @@ USER_IMPL::USER_IMPL(const USER_IMPL & u)
       userdata9(property.userdata9),
       sessionUpload(),
       sessionDownload(),
+      sessionUploadModTime(stgTime),
+      sessionDownloadModTime(stgTime),
       passiveNotifier(this),
       disabledNotifier(this),
       tariffNotifier(this),
@@ -621,7 +628,7 @@ if (!fakeConnect)
     connected = true;
     }
 
-if (store->WriteUserConnect(login, currIP))
+if (!settings->GetDisableSessionLog() && store->WriteUserConnect(login, currIP))
     {
     WriteServLog("Cannot write connect for user %s.", login.c_str());
     WriteServLog("%s", store->GetStrError().c_str());
@@ -685,8 +692,8 @@ std::string reasonMessage(reason);
 if (!lastDisconnectReason.empty())
     reasonMessage += ": " + lastDisconnectReason;
 
-if (store->WriteUserDisconnect(login, up, down, sessionUpload, sessionDownload,
-                               cash, freeMb, reasonMessage))
+if (!settings->GetDisableSessionLog() && store->WriteUserDisconnect(login, up, down, sessionUpload, sessionDownload,
+                                                                    cash, freeMb, reasonMessage))
     {
     WriteServLog("Cannot write disconnect for user %s.", login.c_str());
     WriteServLog("%s", store->GetStrError().c_str());
@@ -1184,7 +1191,7 @@ if (nextTariff.ConstData() != "")
         }
     else
         {
-        std::string message = tariff->TariffChangeIsAllowed(*nt);
+        std::string message = tariff->TariffChangeIsAllowed(*nt, stgTime);
         if (message.empty())
             {
             property.tariffName.Set(nextTariff, sysAdmin, login, store);
@@ -1342,6 +1349,75 @@ switch (settings->GetFeeChargeType())
 ResetPassiveTime();
 }
 //-----------------------------------------------------------------------------
+void USER_IMPL::ProcessServices()
+{
+struct tm tms;
+time_t t = stgTime;
+localtime_r(&t, &tms);
+
+double passiveTimePart = 1.0;
+if (!settings->GetFullFee())
+    {
+    passiveTimePart = GetPassiveTimePart();
+    }
+else
+    {
+    if (passive.ConstData())
+        {
+        printfd(__FILE__, "Don't charge fee `cause we are passive\n");
+        return;
+        }
+    }
+
+for (size_t i = 0; i < property.Conf().services.size(); ++i)
+    {
+    SERVICE_CONF conf;
+    if (m_services.Find(property.Conf().services[i], &conf))
+        continue;
+    if (conf.payDay == tms.tm_mday ||
+        (conf.payDay == 0 && tms.tm_mday == DaysInCurrentMonth()))
+        {
+        double c = cash;
+        double fee = conf.cost * passiveTimePart;
+        printfd(__FILE__, "Service fee. login: %8s Cash=%f Credit=%f  Fee=%f PassiveTimePart=%f fee=%f\n",
+                login.c_str(),
+                cash.ConstData(),
+                credit.ConstData(),
+                tariff->GetFee(),
+                passiveTimePart,
+                fee);
+        switch (settings->GetFeeChargeType())
+            {
+            case 0:
+                property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge");
+                SetPrepaidTraff();
+                break;
+            case 1:
+                if (c + credit >= 0)
+                    {
+                    property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge");
+                    SetPrepaidTraff();
+                    }
+                break;
+            case 2:
+                if (c + credit >= fee)
+                    {
+                    property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge");
+                    SetPrepaidTraff();
+                    }
+                break;
+            case 3:
+                if (c >= 0)
+                    {
+                    property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge");
+                    SetPrepaidTraff();
+                    }
+                break;
+            }
+        }
+    }
+}
+//-----------------------------------------------------------------------------
 void USER_IMPL::SetPrepaidTraff()
 {
 if (tariff != NULL)
@@ -1508,13 +1584,18 @@ else if (!oldValue && newValue && user->IsInetable())
 //-----------------------------------------------------------------------------
 void CHG_TARIFF_NOTIFIER::Notify(const std::string &, const std::string & newTariff)
 {
+STG_LOCKER lock(&user->mutex);
 if (user->settings->GetReconnectOnTariffChange() && user->connected)
     user->Disconnect(false, "Change tariff");
 user->tariff = user->tariffs->FindByName(newTariff);
 if (user->settings->GetReconnectOnTariffChange() &&
     !user->authorizedBy.empty() &&
     user->IsInetable())
+    {
+    // This notifier gets called *before* changing the tariff, and in Connect we want to see new tariff name.
+    user->property.Conf().tariffName = newTariff;
     user->Connect(false);
+    }
 }
 //-----------------------------------------------------------------------------
 void CHG_CASH_NOTIFIER::Notify(const double & oldCash, const double & newCash)