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++)
97 mu[j] = xmlrpc_c::value_string(std::to_string(upload[j]));
98 md[j] = xmlrpc_c::value_string(std::to_string(download[j]));
99 su[j] = xmlrpc_c::value_string(std::to_string(supload[j]));
100 sd[j] = xmlrpc_c::value_string(std::to_string(sdownload[j]));
103 traffInfo["mu"] = xmlrpc_c::value_array(mu);
104 traffInfo["md"] = xmlrpc_c::value_array(md);
105 traffInfo["su"] = xmlrpc_c::value_array(su);
106 traffInfo["sd"] = xmlrpc_c::value_array(sd);
108 structVal["traff"] = xmlrpc_c::value_struct(traffInfo);
110 structVal["down"] = xmlrpc_c::value_boolean(ptr->GetProperty().disabled.Get());
111 structVal["disableddetailstat"] = xmlrpc_c::value_boolean(ptr->GetProperty().disabledDetailStat.Get());
112 structVal["passive"] = xmlrpc_c::value_boolean(ptr->GetProperty().passive.Get());
113 structVal["lastcash"] = xmlrpc_c::value_double(ptr->GetProperty().lastCashAdd.Get());
114 structVal["lasttimecash"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetProperty().lastCashAddTime.Get()));
115 structVal["lastactivitytime"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetProperty().lastActivityTime.Get()));
116 structVal["creditexpire"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetProperty().creditExpire.Get()));
118 *info = xmlrpc_c::value_struct(structVal);
121 //------------------------------------------------------------------------------
123 bool USER_HELPER::SetUserInfo(const xmlrpc_c::value & info,
125 const std::string & login,
129 std::map<std::string, xmlrpc_c::value> structVal(
130 static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(info))
133 std::map<std::string, xmlrpc_c::value>::iterator it;
136 bool alwaysOnline = ptr->GetProperty().alwaysOnline;
137 if ((it = structVal.find("aonline")) != structVal.end())
140 alwaysOnline = xmlrpc_c::value_boolean(it->second);
142 bool onlyOneIP = ptr->GetProperty().ips.ConstData().OnlyOneIP();
143 if ((it = structVal.find("ips")) != structVal.end())
146 onlyOneIP = StrToIPS(xmlrpc_c::value_string(it->second)).OnlyOneIP();
149 if (check && alwaysOnline && !onlyOneIP)
151 printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
155 if ((it = structVal.find("ips")) != structVal.end())
158 ips = StrToIPS(xmlrpc_c::value_string(it->second));
160 for (size_t i = 0; i < ips.Count(); ++i)
163 uint32_t ip = ips[i].ip;
164 if (users.IsIPInUse(ip, login, &user))
166 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());
171 if (!ptr->GetProperty().ips.Set(ips,
178 if ((it = structVal.find("aonline")) != structVal.end())
180 bool value(xmlrpc_c::value_boolean(it->second));
181 if (ptr->GetProperty().alwaysOnline.Get() != value)
182 if (!ptr->GetProperty().alwaysOnline.Set(value,
189 if ((it = structVal.find("password")) != structVal.end())
191 std::string value(xmlrpc_c::value_string(it->second));
192 if (ptr->GetProperty().password.Get() != value)
193 if (!ptr->GetProperty().password.Set(value,
200 if ((it = structVal.find("address")) != structVal.end())
202 std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
203 if (ptr->GetProperty().address.Get() != value)
204 if (!ptr->GetProperty().address.Set(value,
211 if ((it = structVal.find("phone")) != structVal.end())
213 std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
214 if (ptr->GetProperty().phone.Get() != value)
215 if (!ptr->GetProperty().phone.Set(value,
222 if ((it = structVal.find("email")) != structVal.end())
224 std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
225 if (ptr->GetProperty().email.Get() != value)
226 if (!ptr->GetProperty().email.Set(value,
233 if ((it = structVal.find("cash")) != structVal.end())
235 double value(xmlrpc_c::value_double(it->second));
236 if (std::fabs(ptr->GetProperty().cash.Get() - value) > 1.0e-3)
237 if (!ptr->GetProperty().cash.Set(value,
244 if ((it = structVal.find("creditexpire")) != structVal.end())
246 time_t value(xmlrpc_c::value_int(it->second));
247 if (ptr->GetProperty().creditExpire.Get() != value)
248 if (!ptr->GetProperty().creditExpire.Set(value,
255 if ((it = structVal.find("credit")) != structVal.end())
257 double value(xmlrpc_c::value_double(it->second));
258 if (std::fabs(ptr->GetProperty().credit.Get() - value) > 1.0e-3)
259 if (!ptr->GetProperty().credit.Set(value,
266 if ((it = structVal.find("freemb")) != structVal.end())
268 double value(xmlrpc_c::value_double(it->second));
269 if (std::fabs(ptr->GetProperty().freeMb.Get() - value) > 1.0e-3)
270 if (!ptr->GetProperty().freeMb.Set(value,
277 if ((it = structVal.find("down")) != structVal.end())
279 bool value(xmlrpc_c::value_boolean(it->second));
280 if (ptr->GetProperty().disabled.Get() != value)
281 if (!ptr->GetProperty().disabled.Set(value,
288 if ((it = structVal.find("passive")) != structVal.end())
290 bool value(xmlrpc_c::value_boolean(it->second));
291 if (ptr->GetProperty().passive.Get() != value)
292 if (!ptr->GetProperty().passive.Set(value,
299 if ((it = structVal.find("disableddetailstat")) != structVal.end())
301 bool value(xmlrpc_c::value_boolean(it->second));
302 if (ptr->GetProperty().disabledDetailStat.Get() != value)
303 if (!ptr->GetProperty().disabledDetailStat.Set(value,
310 if ((it = structVal.find("name")) != structVal.end())
312 std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
313 if (ptr->GetProperty().realName.Get() != value)
314 if (!ptr->GetProperty().realName.Set(value,
321 if ((it = structVal.find("group")) != structVal.end())
323 std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
324 if (ptr->GetProperty().group.Get() != value)
325 if (!ptr->GetProperty().group.Set(value,
332 if ((it = structVal.find("note")) != structVal.end())
334 std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
335 if (ptr->GetProperty().note.Get() != value)
336 if (!ptr->GetProperty().note.Set(value,
343 if ((it = structVal.find("userdata")) != structVal.end())
345 std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
346 userdata.push_back(ptr->GetProperty().userdata0.GetPointer());
347 userdata.push_back(ptr->GetProperty().userdata1.GetPointer());
348 userdata.push_back(ptr->GetProperty().userdata2.GetPointer());
349 userdata.push_back(ptr->GetProperty().userdata3.GetPointer());
350 userdata.push_back(ptr->GetProperty().userdata4.GetPointer());
351 userdata.push_back(ptr->GetProperty().userdata5.GetPointer());
352 userdata.push_back(ptr->GetProperty().userdata6.GetPointer());
353 userdata.push_back(ptr->GetProperty().userdata7.GetPointer());
354 userdata.push_back(ptr->GetProperty().userdata8.GetPointer());
355 userdata.push_back(ptr->GetProperty().userdata9.GetPointer());
357 std::vector<xmlrpc_c::value> udata(
358 xmlrpc_c::value_array(it->second).vectorValueValue()
361 for (unsigned i = 0; i < userdata.size(); ++i)
363 std::string value(IconvString(xmlrpc_c::value_string(udata[i]), "UTF-8", "KOI8-RU"));
364 if (userdata[i]->Get() != value)
365 if (!userdata[i]->Set(value,
373 if ((it = structVal.find("traff")) != structVal.end())
375 std::map<std::string, xmlrpc_c::value> traff(
376 static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(it->second))
380 dtData = ptr->GetProperty().up.Get();
381 if ((it = traff.find("mu")) != traff.end())
383 std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
385 for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
388 if (str2x(xmlrpc_c::value_string(data[i]), value))
389 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month upload value'\n");
393 if (!ptr->GetProperty().up.Set(dtData,
399 dtData = ptr->GetProperty().down.Get();
400 if ((it = traff.find("md")) != traff.end())
402 std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
404 for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
407 if (str2x(xmlrpc_c::value_string(data[i]), value))
408 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month download value'\n");
412 if (!ptr->GetProperty().down.Set(dtData,
420 if ((it = structVal.find("tariff")) != structVal.end())
422 std::string tariff(xmlrpc_c::value_string(it->second));
423 size_t pos = tariff.find('/');
424 std::string nextTariff;
425 if (pos != std::string::npos)
427 nextTariff = tariff.substr(pos + 1);
428 tariff = tariff.substr(0, pos);
431 const TARIFF * newTariff = tariffs->FindByName(tariff);
434 const TARIFF * currentTariff = ptr->GetTariff();
435 std::string message = currentTariff->TariffChangeIsAllowed(*newTariff, stgTime);
438 if (ptr->GetProperty().tariffName.Get() != tariff)
440 if (!ptr->GetProperty().tariffName.Set(tariff,
449 GetStgLogger()("Tariff change is prohibited for user %s. %s", ptr->GetLogin().c_str(), message.c_str());
453 if (nextTariff != "" &&
454 tariffs->FindByName(nextTariff))
455 if (ptr->GetProperty().nextTariff.Get() != nextTariff)
456 if (!ptr->GetProperty().nextTariff.Set(tariff,