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