]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/user_helper.cpp
Delete debug output scriptParams
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / user_helper.cpp
1 #include <cmath>
2
3 #include "stg/tariffs.h"
4 #include "stg/admin.h"
5 #include "stg/store.h"
6 #include "stg/user_ips.h"
7 #include "stg/common.h"
8 #include "stg/user_property.h"
9 #include "user_helper.h"
10
11 //------------------------------------------------------------------------------
12
13 void USER_HELPER::GetUserInfo(xmlrpc_c::value * info,
14                               bool hidePassword)
15 {
16 std::map<std::string, xmlrpc_c::value> structVal;
17
18 structVal["result"] = xmlrpc_c::value_boolean(true);
19 structVal["login"] = xmlrpc_c::value_string(ptr->GetLogin());
20
21 if (!hidePassword)
22     {
23     structVal["password"] = xmlrpc_c::value_string(ptr->GetProperty().password.Get());
24     }
25 else
26     {
27     structVal["password"] = xmlrpc_c::value_string("++++++++");
28     }
29
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());
33
34 if (ptr->GetProperty().nextTariff.Get() != "")
35     {
36     structVal["tariff"] = xmlrpc_c::value_string(
37             ptr->GetProperty().tariffName.Get() +
38             "/" +
39             ptr->GetProperty().nextTariff.Get()
40             );
41     }
42 else
43     {
44     structVal["tariff"] = xmlrpc_c::value_string(ptr->GetProperty().tariffName.Get());
45     }
46
47 structVal["note"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().note, "KOI8-RU", "UTF-8"));
48
49 structVal["phone"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().phone, "KOI8-RU", "UTF-8"));
50
51 structVal["address"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().address, "KOI8-RU", "UTF-8"));
52
53 structVal["email"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().email, "KOI8-RU", "UTF-8"));
54
55 std::vector<xmlrpc_c::value> userdata;
56
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")));
67
68 structVal["userdata"] = xmlrpc_c::value_array(userdata);
69
70 structVal["name"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().realName, "KOI8-RU", "UTF-8"));
71
72 structVal["group"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().group, "KOI8-RU", "UTF-8"));
73
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());
79
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);
85
86 DIR_TRAFF upload;
87 DIR_TRAFF download;
88 DIR_TRAFF supload;
89 DIR_TRAFF sdownload;
90 download = ptr->GetProperty().down.Get();
91 upload = ptr->GetProperty().up.Get();
92 sdownload = ptr->GetSessionUpload();
93 supload = ptr->GetSessionDownload();
94
95 for (int j = 0; j < DIR_NUM; j++)
96     {
97     std::string value;
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);
106     }
107
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);
112
113 structVal["traff"] = xmlrpc_c::value_struct(traffInfo);
114
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()));
122
123 *info = xmlrpc_c::value_struct(structVal);
124 }
125
126 //------------------------------------------------------------------------------
127
128 bool USER_HELPER::SetUserInfo(const xmlrpc_c::value & info,
129                               const ADMIN * admin,
130                               const std::string & login,
131                               const STORE & store,
132                               TARIFFS * tariffs)
133 {
134 std::map<std::string, xmlrpc_c::value> structVal(
135     static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(info))
136     );
137
138 std::map<std::string, xmlrpc_c::value>::iterator it;
139
140 bool check = false;
141 bool alwaysOnline = ptr->GetProperty().alwaysOnline;
142 if ((it = structVal.find("aonline")) != structVal.end())
143     {
144     check = true;
145     alwaysOnline = xmlrpc_c::value_boolean(it->second);
146     }
147 bool onlyOneIP = ptr->GetProperty().ips.ConstData().OnlyOneIP();
148 if ((it = structVal.find("ips")) != structVal.end())
149     {
150     check = true;
151     onlyOneIP = StrToIPS(xmlrpc_c::value_string(it->second)).OnlyOneIP();
152     }
153
154 if (check && alwaysOnline && !onlyOneIP)
155     {
156     printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
157     return true;
158     }
159
160 if ((it = structVal.find("ips")) != structVal.end())
161     {
162     USER_IPS ips;
163     ips = StrToIPS(xmlrpc_c::value_string(it->second));
164     if (!ptr->GetProperty().ips.Set(ips,
165                                 admin,
166                                 login,
167                                 &store))
168         return true;
169     }
170
171 if ((it = structVal.find("aonline")) != structVal.end())
172     {
173     bool value(xmlrpc_c::value_boolean(it->second));
174     if (ptr->GetProperty().alwaysOnline.Get() != value)
175         if (!ptr->GetProperty().alwaysOnline.Set(value,
176                                              admin,
177                                              login,
178                                              &store))
179             return true;
180     }
181
182 if ((it = structVal.find("password")) != structVal.end())
183     {
184     std::string value(xmlrpc_c::value_string(it->second));
185     if (ptr->GetProperty().password.Get() != value)
186         if (!ptr->GetProperty().password.Set(value,
187                                          admin,
188                                          login,
189                                          &store))
190             return true;
191     }
192
193 if ((it = structVal.find("address")) != structVal.end())
194     {
195     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
196     if (ptr->GetProperty().address.Get() != value)
197         if (!ptr->GetProperty().address.Set(value,
198                                         admin,
199                                         login,
200                                         &store))
201             return true;
202     }
203
204 if ((it = structVal.find("phone")) != structVal.end())
205     {
206     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
207     if (ptr->GetProperty().phone.Get() != value)
208         if (!ptr->GetProperty().phone.Set(value,
209                                       admin,
210                                       login,
211                                       &store))
212             return true;
213     }
214
215 if ((it = structVal.find("email")) != structVal.end())
216     {
217     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
218     if (ptr->GetProperty().email.Get() != value)
219         if (!ptr->GetProperty().email.Set(value,
220                                       admin,
221                                       login,
222                                       &store))
223             return true;
224     }
225
226 if ((it = structVal.find("cash")) != structVal.end())
227     {
228     double value(xmlrpc_c::value_double(it->second));
229     if (std::fabs(ptr->GetProperty().cash.Get() - value) > 1.0e-3)
230         if (!ptr->GetProperty().cash.Set(value,
231                                      admin,
232                                      login,
233                                      &store))
234             return true;
235     }
236
237 if ((it = structVal.find("creditexpire")) != structVal.end())
238     {
239     time_t value(xmlrpc_c::value_int(it->second));
240     if (ptr->GetProperty().creditExpire.Get() != value)
241         if (!ptr->GetProperty().creditExpire.Set(value,
242                                              admin,
243                                              login,
244                                              &store))
245             return true;
246     }
247
248 if ((it = structVal.find("credit")) != structVal.end())
249     {
250     double value(xmlrpc_c::value_double(it->second));
251     if (std::fabs(ptr->GetProperty().credit.Get() - value) > 1.0e-3)
252         if (!ptr->GetProperty().credit.Set(value,
253                                        admin,
254                                        login,
255                                        &store))
256             return true;
257     }
258
259 if ((it = structVal.find("freemb")) != structVal.end())
260     {
261     double value(xmlrpc_c::value_double(it->second));
262     if (std::fabs(ptr->GetProperty().freeMb.Get() - value) > 1.0e-3)
263         if (!ptr->GetProperty().freeMb.Set(value,
264                                        admin,
265                                        login,
266                                        &store))
267             return true;
268     }
269
270 if ((it = structVal.find("down")) != structVal.end())
271     {
272     bool value(xmlrpc_c::value_boolean(it->second));
273     if (ptr->GetProperty().disabled.Get() != value)
274         if (!ptr->GetProperty().disabled.Set(value,
275                                          admin,
276                                          login,
277                                          &store))
278             return true;
279     }
280
281 if ((it = structVal.find("passive")) != structVal.end())
282     {
283     bool value(xmlrpc_c::value_boolean(it->second));
284     if (ptr->GetProperty().passive.Get() != value)
285         if (!ptr->GetProperty().passive.Set(value,
286                                         admin,
287                                         login,
288                                         &store))
289             return true;
290     }
291
292 if ((it = structVal.find("disableddetailstat")) != structVal.end())
293     {
294     bool value(xmlrpc_c::value_boolean(it->second));
295     if (ptr->GetProperty().disabledDetailStat.Get() != value)
296         if (!ptr->GetProperty().disabledDetailStat.Set(value,
297                                                    admin,
298                                                    login,
299                                                    &store))
300             return true;
301     }
302
303 if ((it = structVal.find("name")) != structVal.end())
304     {
305     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
306     if (ptr->GetProperty().realName.Get() != value)
307         if (!ptr->GetProperty().realName.Set(value,
308                                          admin,
309                                          login,
310                                          &store))
311             return true;
312     }
313
314 if ((it = structVal.find("group")) != structVal.end())
315     {
316     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
317     if (ptr->GetProperty().group.Get() != value)
318         if (!ptr->GetProperty().group.Set(value,
319                                       admin,
320                                       login,
321                                       &store))
322             return true;
323     }
324
325 if ((it = structVal.find("note")) != structVal.end())
326     {
327     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
328     if (ptr->GetProperty().note.Get() != value)
329         if (!ptr->GetProperty().note.Set(value,
330                                      admin,
331                                      login,
332                                      &store))
333             return true;
334     }
335
336 if ((it = structVal.find("userdata")) != structVal.end())
337     {
338     std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
339     userdata.push_back(ptr->GetProperty().userdata0.GetPointer());
340     userdata.push_back(ptr->GetProperty().userdata1.GetPointer());
341     userdata.push_back(ptr->GetProperty().userdata2.GetPointer());
342     userdata.push_back(ptr->GetProperty().userdata3.GetPointer());
343     userdata.push_back(ptr->GetProperty().userdata4.GetPointer());
344     userdata.push_back(ptr->GetProperty().userdata5.GetPointer());
345     userdata.push_back(ptr->GetProperty().userdata6.GetPointer());
346     userdata.push_back(ptr->GetProperty().userdata7.GetPointer());
347     userdata.push_back(ptr->GetProperty().userdata8.GetPointer());
348     userdata.push_back(ptr->GetProperty().userdata9.GetPointer());
349
350     std::vector<xmlrpc_c::value> udata(
351         xmlrpc_c::value_array(it->second).vectorValueValue()
352         );
353
354     for (unsigned i = 0; i < userdata.size(); ++i)
355         {
356         std::string value(IconvString(xmlrpc_c::value_string(udata[i]), "UTF-8", "KOI8-RU"));
357         if (userdata[i]->Get() != value)
358             if (!userdata[i]->Set(value,
359                                   admin,
360                                   login,
361                                   &store))
362                 return true;
363         }
364     }
365
366 if ((it = structVal.find("traff")) != structVal.end())
367     {
368     std::map<std::string, xmlrpc_c::value> traff(
369         static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(it->second))
370         );
371
372     DIR_TRAFF dtData;
373     dtData = ptr->GetProperty().up.Get();
374     if ((it = traff.find("mu")) != traff.end())
375         {
376         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
377
378         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
379             {
380             int64_t value;
381             if (str2x(xmlrpc_c::value_string(data[i]), value))
382                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month upload value'\n");
383             else
384                 dtData[i] = value;
385             }
386         if (!ptr->GetProperty().up.Set(dtData,
387                                    admin,
388                                    login,
389                                    &store))
390             return true;
391         }
392     dtData = ptr->GetProperty().down.Get();
393     if ((it = traff.find("md")) != traff.end())
394         {
395         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
396
397         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
398             {
399             int64_t value;
400             if (str2x(xmlrpc_c::value_string(data[i]), value))
401                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month download value'\n");
402             else
403                 dtData[i] = value;
404             }
405         if (!ptr->GetProperty().down.Set(dtData,
406                                      admin,
407                                      login,
408                                      &store))
409             return true;
410         }
411     }
412
413 if ((it = structVal.find("tariff")) != structVal.end())
414     {
415     std::string tariff(xmlrpc_c::value_string(it->second));
416     size_t pos = tariff.find('/');
417     std::string nextTariff;
418     if (pos != std::string::npos)
419         {
420         nextTariff = tariff.substr(pos + 1);
421         tariff = tariff.substr(0, pos);
422         }
423
424     if (tariffs->FindByName(tariff))
425         if (ptr->GetProperty().tariffName.Get() != tariff)
426             if (!ptr->GetProperty().tariffName.Set(tariff,
427                                                admin,
428                                                login,
429                                                &store))
430                 return true;
431
432     if (nextTariff != "" &&
433         tariffs->FindByName(nextTariff))
434         if (ptr->GetProperty().nextTariff.Get() != nextTariff)
435             if (!ptr->GetProperty().nextTariff.Set(tariff,
436                                                admin,
437                                                login,
438                                                &store))
439                 return true;
440     }
441
442 return false;
443 }