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