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