3 #include "stg/tariffs.h"
 
   6 #include "stg/user_ips.h"
 
   7 #include "stg/common.h"
 
   8 #include "stg/user_property.h"
 
   9 #include "user_helper.h"
 
  11 //------------------------------------------------------------------------------
 
  13 void USER_HELPER::GetUserInfo(xmlrpc_c::value * info,
 
  16 std::map<std::string, xmlrpc_c::value> structVal;
 
  18 structVal["result"] = xmlrpc_c::value_boolean(true);
 
  19 structVal["login"] = xmlrpc_c::value_string(ptr->GetLogin());
 
  23     structVal["password"] = xmlrpc_c::value_string(ptr->GetProperty().password.Get());
 
  27     structVal["password"] = xmlrpc_c::value_string("++++++++");
 
  30 structVal["cash"] = xmlrpc_c::value_double(ptr->GetProperty().cash.Get());
 
  31 structVal["freemb"] = xmlrpc_c::value_double(ptr->GetProperty().freeMb.Get());
 
  32 structVal["credit"] = xmlrpc_c::value_double(ptr->GetProperty().credit.Get());
 
  34 if (ptr->GetProperty().nextTariff.Get() != "")
 
  36     structVal["tariff"] = xmlrpc_c::value_string(
 
  37             ptr->GetProperty().tariffName.Get() +
 
  39             ptr->GetProperty().nextTariff.Get()
 
  44     structVal["tariff"] = xmlrpc_c::value_string(ptr->GetProperty().tariffName.Get());
 
  47 structVal["note"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().note, "KOI8-RU", "UTF-8"));
 
  49 structVal["phone"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().phone, "KOI8-RU", "UTF-8"));
 
  51 structVal["address"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().address, "KOI8-RU", "UTF-8"));
 
  53 structVal["email"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().email, "KOI8-RU", "UTF-8"));
 
  55 std::vector<xmlrpc_c::value> userdata;
 
  57 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata0.Get(), "KOI8-RU", "UTF-8")));
 
  58 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata1.Get(), "KOI8-RU", "UTF-8")));
 
  59 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata2.Get(), "KOI8-RU", "UTF-8")));
 
  60 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata3.Get(), "KOI8-RU", "UTF-8")));
 
  61 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata4.Get(), "KOI8-RU", "UTF-8")));
 
  62 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata5.Get(), "KOI8-RU", "UTF-8")));
 
  63 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata6.Get(), "KOI8-RU", "UTF-8")));
 
  64 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata7.Get(), "KOI8-RU", "UTF-8")));
 
  65 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata8.Get(), "KOI8-RU", "UTF-8")));
 
  66 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata9.Get(), "KOI8-RU", "UTF-8")));
 
  68 structVal["userdata"] = xmlrpc_c::value_array(userdata);
 
  70 structVal["name"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().realName, "KOI8-RU", "UTF-8"));
 
  72 structVal["group"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().group, "KOI8-RU", "UTF-8"));
 
  74 structVal["status"] = xmlrpc_c::value_boolean(ptr->GetConnected());
 
  75 structVal["aonline"] = xmlrpc_c::value_boolean(ptr->GetProperty().alwaysOnline.Get());
 
  76 structVal["currip"] = xmlrpc_c::value_string(inet_ntostring(ptr->GetCurrIP()));
 
  77 structVal["pingtime"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetPingTime()));
 
  78 structVal["ips"] = xmlrpc_c::value_string(ptr->GetProperty().ips.Get().GetIpStr());
 
  80 std::map<std::string, xmlrpc_c::value> traffInfo;
 
  81 std::vector<xmlrpc_c::value> mu(DIR_NUM);
 
  82 std::vector<xmlrpc_c::value> md(DIR_NUM);
 
  83 std::vector<xmlrpc_c::value> su(DIR_NUM);
 
  84 std::vector<xmlrpc_c::value> sd(DIR_NUM);
 
  90 download = ptr->GetProperty().down.Get();
 
  91 upload = ptr->GetProperty().up.Get();
 
  92 sdownload = ptr->GetSessionUpload();
 
  93 supload = ptr->GetSessionDownload();
 
  95 for (int j = 0; j < DIR_NUM; j++)
 
  98     x2str(upload[j], value);
 
  99     mu[j] = xmlrpc_c::value_string(value);
 
 100     x2str(download[j], value);
 
 101     md[j] = xmlrpc_c::value_string(value);
 
 102     x2str(supload[j], value);
 
 103     su[j] = xmlrpc_c::value_string(value);
 
 104     x2str(sdownload[j], value);
 
 105     sd[j] = xmlrpc_c::value_string(value);
 
 108 traffInfo["mu"] = xmlrpc_c::value_array(mu);
 
 109 traffInfo["md"] = xmlrpc_c::value_array(md);
 
 110 traffInfo["su"] = xmlrpc_c::value_array(su);
 
 111 traffInfo["sd"] = xmlrpc_c::value_array(sd);
 
 113 structVal["traff"] = xmlrpc_c::value_struct(traffInfo);
 
 115 structVal["down"] = xmlrpc_c::value_boolean(ptr->GetProperty().disabled.Get());
 
 116 structVal["disableddetailstat"] = xmlrpc_c::value_boolean(ptr->GetProperty().disabledDetailStat.Get());
 
 117 structVal["passive"] = xmlrpc_c::value_boolean(ptr->GetProperty().passive.Get());
 
 118 structVal["lastcash"] = xmlrpc_c::value_double(ptr->GetProperty().lastCashAdd.Get());
 
 119 structVal["lasttimecash"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetProperty().lastCashAddTime.Get()));
 
 120 structVal["lastactivitytime"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetProperty().lastActivityTime.Get()));
 
 121 structVal["creditexpire"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetProperty().creditExpire.Get()));
 
 123 *info = xmlrpc_c::value_struct(structVal);
 
 126 //------------------------------------------------------------------------------
 
 128 bool USER_HELPER::SetUserInfo(const xmlrpc_c::value & info,
 
 130                               const std::string & login,
 
 134 std::map<std::string, xmlrpc_c::value> structVal(
 
 135     static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(info))
 
 138 std::map<std::string, xmlrpc_c::value>::iterator it;
 
 141 bool alwaysOnline = ptr->GetProperty().alwaysOnline;
 
 142 if ((it = structVal.find("aonline")) != structVal.end())
 
 145     alwaysOnline = xmlrpc_c::value_boolean(it->second);
 
 147 bool onlyOneIP = ptr->GetProperty().ips.ConstData().OnlyOneIP();
 
 148 if ((it = structVal.find("ips")) != structVal.end())
 
 151     onlyOneIP = StrToIPS(xmlrpc_c::value_string(it->second)).OnlyOneIP();
 
 154 if (check && alwaysOnline && !onlyOneIP)
 
 156     printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
 
 160 if ((it = structVal.find("ips")) != structVal.end())
 
 163     ips = StrToIPS(xmlrpc_c::value_string(it->second));
 
 165     for (size_t i = 0; i < ips.Count(); ++i)
 
 168         uint32_t ip = ips[i].ip;
 
 169         if (users.IsIPInUse(ip, login, &user))
 
 171             printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str());
 
 176     if (!ptr->GetProperty().ips.Set(ips,
 
 183 if ((it = structVal.find("aonline")) != structVal.end())
 
 185     bool value(xmlrpc_c::value_boolean(it->second));
 
 186     if (ptr->GetProperty().alwaysOnline.Get() != value)
 
 187         if (!ptr->GetProperty().alwaysOnline.Set(value,
 
 194 if ((it = structVal.find("password")) != structVal.end())
 
 196     std::string value(xmlrpc_c::value_string(it->second));
 
 197     if (ptr->GetProperty().password.Get() != value)
 
 198         if (!ptr->GetProperty().password.Set(value,
 
 205 if ((it = structVal.find("address")) != structVal.end())
 
 207     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
 
 208     if (ptr->GetProperty().address.Get() != value)
 
 209         if (!ptr->GetProperty().address.Set(value,
 
 216 if ((it = structVal.find("phone")) != structVal.end())
 
 218     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
 
 219     if (ptr->GetProperty().phone.Get() != value)
 
 220         if (!ptr->GetProperty().phone.Set(value,
 
 227 if ((it = structVal.find("email")) != structVal.end())
 
 229     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
 
 230     if (ptr->GetProperty().email.Get() != value)
 
 231         if (!ptr->GetProperty().email.Set(value,
 
 238 if ((it = structVal.find("cash")) != structVal.end())
 
 240     double value(xmlrpc_c::value_double(it->second));
 
 241     if (std::fabs(ptr->GetProperty().cash.Get() - value) > 1.0e-3)
 
 242         if (!ptr->GetProperty().cash.Set(value,
 
 249 if ((it = structVal.find("creditexpire")) != structVal.end())
 
 251     time_t value(xmlrpc_c::value_int(it->second));
 
 252     if (ptr->GetProperty().creditExpire.Get() != value)
 
 253         if (!ptr->GetProperty().creditExpire.Set(value,
 
 260 if ((it = structVal.find("credit")) != structVal.end())
 
 262     double value(xmlrpc_c::value_double(it->second));
 
 263     if (std::fabs(ptr->GetProperty().credit.Get() - value) > 1.0e-3)
 
 264         if (!ptr->GetProperty().credit.Set(value,
 
 271 if ((it = structVal.find("freemb")) != structVal.end())
 
 273     double value(xmlrpc_c::value_double(it->second));
 
 274     if (std::fabs(ptr->GetProperty().freeMb.Get() - value) > 1.0e-3)
 
 275         if (!ptr->GetProperty().freeMb.Set(value,
 
 282 if ((it = structVal.find("down")) != structVal.end())
 
 284     bool value(xmlrpc_c::value_boolean(it->second));
 
 285     if (ptr->GetProperty().disabled.Get() != value)
 
 286         if (!ptr->GetProperty().disabled.Set(value,
 
 293 if ((it = structVal.find("passive")) != structVal.end())
 
 295     bool value(xmlrpc_c::value_boolean(it->second));
 
 296     if (ptr->GetProperty().passive.Get() != value)
 
 297         if (!ptr->GetProperty().passive.Set(value,
 
 304 if ((it = structVal.find("disableddetailstat")) != structVal.end())
 
 306     bool value(xmlrpc_c::value_boolean(it->second));
 
 307     if (ptr->GetProperty().disabledDetailStat.Get() != value)
 
 308         if (!ptr->GetProperty().disabledDetailStat.Set(value,
 
 315 if ((it = structVal.find("name")) != structVal.end())
 
 317     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
 
 318     if (ptr->GetProperty().realName.Get() != value)
 
 319         if (!ptr->GetProperty().realName.Set(value,
 
 326 if ((it = structVal.find("group")) != structVal.end())
 
 328     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
 
 329     if (ptr->GetProperty().group.Get() != value)
 
 330         if (!ptr->GetProperty().group.Set(value,
 
 337 if ((it = structVal.find("note")) != structVal.end())
 
 339     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
 
 340     if (ptr->GetProperty().note.Get() != value)
 
 341         if (!ptr->GetProperty().note.Set(value,
 
 348 if ((it = structVal.find("userdata")) != structVal.end())
 
 350     std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
 
 351     userdata.push_back(ptr->GetProperty().userdata0.GetPointer());
 
 352     userdata.push_back(ptr->GetProperty().userdata1.GetPointer());
 
 353     userdata.push_back(ptr->GetProperty().userdata2.GetPointer());
 
 354     userdata.push_back(ptr->GetProperty().userdata3.GetPointer());
 
 355     userdata.push_back(ptr->GetProperty().userdata4.GetPointer());
 
 356     userdata.push_back(ptr->GetProperty().userdata5.GetPointer());
 
 357     userdata.push_back(ptr->GetProperty().userdata6.GetPointer());
 
 358     userdata.push_back(ptr->GetProperty().userdata7.GetPointer());
 
 359     userdata.push_back(ptr->GetProperty().userdata8.GetPointer());
 
 360     userdata.push_back(ptr->GetProperty().userdata9.GetPointer());
 
 362     std::vector<xmlrpc_c::value> udata(
 
 363         xmlrpc_c::value_array(it->second).vectorValueValue()
 
 366     for (unsigned i = 0; i < userdata.size(); ++i)
 
 368         std::string value(IconvString(xmlrpc_c::value_string(udata[i]), "UTF-8", "KOI8-RU"));
 
 369         if (userdata[i]->Get() != value)
 
 370             if (!userdata[i]->Set(value,
 
 378 if ((it = structVal.find("traff")) != structVal.end())
 
 380     std::map<std::string, xmlrpc_c::value> traff(
 
 381         static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(it->second))
 
 385     dtData = ptr->GetProperty().up.Get();
 
 386     if ((it = traff.find("mu")) != traff.end())
 
 388         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
 
 390         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
 
 393             if (str2x(xmlrpc_c::value_string(data[i]), value))
 
 394                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month upload value'\n");
 
 398         if (!ptr->GetProperty().up.Set(dtData,
 
 404     dtData = ptr->GetProperty().down.Get();
 
 405     if ((it = traff.find("md")) != traff.end())
 
 407         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
 
 409         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
 
 412             if (str2x(xmlrpc_c::value_string(data[i]), value))
 
 413                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month download value'\n");
 
 417         if (!ptr->GetProperty().down.Set(dtData,
 
 425 if ((it = structVal.find("tariff")) != structVal.end())
 
 427     std::string tariff(xmlrpc_c::value_string(it->second));
 
 428     size_t pos = tariff.find('/');
 
 429     std::string nextTariff;
 
 430     if (pos != std::string::npos)
 
 432         nextTariff = tariff.substr(pos + 1);
 
 433         tariff = tariff.substr(0, pos);
 
 436     const TARIFF * newTariff = tariffs->FindByName(tariff);
 
 439         const TARIFF * currentTariff = ptr->GetTariff();
 
 440         std::string message = currentTariff->TariffChangeIsAllowed(*newTariff, stgTime);
 
 443             if (ptr->GetProperty().tariffName.Get() != tariff)
 
 445                 if (!ptr->GetProperty().tariffName.Set(tariff,
 
 454             GetStgLogger()("Tariff change is prohibited for user %s. %s", ptr->GetLogin().c_str(), message.c_str());
 
 458     if (nextTariff != "" &&
 
 459         tariffs->FindByName(nextTariff))
 
 460         if (ptr->GetProperty().nextTariff.Get() != nextTariff)
 
 461             if (!ptr->GetProperty().nextTariff.Set(tariff,