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