]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/user_helper.cpp
Minor ChangeLog fix
[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 if ((it = structVal.find("password")) != structVal.end())
140     {
141     std::string value(xmlrpc_c::value_string(it->second));
142     if (ptr->GetProperty().password.Get() != value)
143         if (!ptr->GetProperty().password.Set(value,
144                                          admin,
145                                          login,
146                                          &store))
147             return true;
148     }
149
150 if ((it = structVal.find("ips")) != structVal.end())
151     {
152     USER_IPS ips;
153     ips = StrToIPS(xmlrpc_c::value_string(it->second));
154     if (!ptr->GetProperty().ips.Set(ips,
155                                 admin,
156                                 login,
157                                 &store))
158         return true;
159     }
160
161 if ((it = structVal.find("address")) != structVal.end())
162     {
163     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
164     if (ptr->GetProperty().address.Get() != value)
165         if (!ptr->GetProperty().address.Set(value,
166                                         admin,
167                                         login,
168                                         &store))
169             return true;
170     }
171
172 if ((it = structVal.find("phone")) != structVal.end())
173     {
174     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
175     if (ptr->GetProperty().phone.Get() != value)
176         if (!ptr->GetProperty().phone.Set(value,
177                                       admin,
178                                       login,
179                                       &store))
180             return true;
181     }
182
183 if ((it = structVal.find("email")) != structVal.end())
184     {
185     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
186     if (ptr->GetProperty().email.Get() != value)
187         if (!ptr->GetProperty().email.Set(value,
188                                       admin,
189                                       login,
190                                       &store))
191             return true;
192     }
193
194 if ((it = structVal.find("cash")) != structVal.end())
195     {
196     double value(xmlrpc_c::value_double(it->second));
197     if (ptr->GetProperty().cash.Get() != value)
198         if (!ptr->GetProperty().cash.Set(value,
199                                      admin,
200                                      login,
201                                      &store))
202             return true;
203     }
204
205 if ((it = structVal.find("creditexpire")) != structVal.end())
206     {
207     time_t value(xmlrpc_c::value_int(it->second));
208     if (ptr->GetProperty().creditExpire.Get() != value)
209         if (!ptr->GetProperty().creditExpire.Set(value,
210                                              admin,
211                                              login,
212                                              &store))
213             return true;
214     }
215
216 if ((it = structVal.find("credit")) != structVal.end())
217     {
218     double value(xmlrpc_c::value_double(it->second));
219     if (ptr->GetProperty().credit.Get() != value)
220         if (!ptr->GetProperty().credit.Set(value,
221                                        admin,
222                                        login,
223                                        &store))
224             return true;
225     }
226
227 if ((it = structVal.find("freemb")) != structVal.end())
228     {
229     double value(xmlrpc_c::value_double(it->second));
230     if (ptr->GetProperty().freeMb.Get() != value)
231         if (!ptr->GetProperty().freeMb.Set(value,
232                                        admin,
233                                        login,
234                                        &store))
235             return true;
236     }
237
238 if ((it = structVal.find("down")) != structVal.end())
239     {
240     bool value(xmlrpc_c::value_boolean(it->second));
241     if (ptr->GetProperty().disabled.Get() != value)
242         if (!ptr->GetProperty().disabled.Set(value,
243                                          admin,
244                                          login,
245                                          &store))
246             return true;
247     }
248
249 if ((it = structVal.find("passive")) != structVal.end())
250     {
251     bool value(xmlrpc_c::value_boolean(it->second));
252     if (ptr->GetProperty().passive.Get() != value)
253         if (!ptr->GetProperty().passive.Set(value,
254                                         admin,
255                                         login,
256                                         &store))
257             return true;
258     }
259
260 if ((it = structVal.find("aonline")) != structVal.end())
261     {
262     bool value(xmlrpc_c::value_boolean(it->second));
263     if (ptr->GetProperty().alwaysOnline.Get() != value)
264         if (!ptr->GetProperty().alwaysOnline.Set(value,
265                                              admin,
266                                              login,
267                                              &store))
268             return true;
269     }
270
271 if ((it = structVal.find("disableddetailstat")) != structVal.end())
272     {
273     bool value(xmlrpc_c::value_boolean(it->second));
274     if (ptr->GetProperty().disabledDetailStat.Get() != value)
275         if (!ptr->GetProperty().disabledDetailStat.Set(value,
276                                                    admin,
277                                                    login,
278                                                    &store))
279             return true;
280     }
281
282 if ((it = structVal.find("name")) != structVal.end())
283     {
284     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
285     if (ptr->GetProperty().realName.Get() != value)
286         if (!ptr->GetProperty().realName.Set(value,
287                                          admin,
288                                          login,
289                                          &store))
290             return true;
291     }
292
293 if ((it = structVal.find("group")) != structVal.end())
294     {
295     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
296     if (ptr->GetProperty().group.Get() != value)
297         if (!ptr->GetProperty().group.Set(value,
298                                       admin,
299                                       login,
300                                       &store))
301             return true;
302     }
303
304 if ((it = structVal.find("note")) != structVal.end())
305     {
306     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
307     if (ptr->GetProperty().note.Get() != value)
308         if (!ptr->GetProperty().note.Set(value,
309                                      admin,
310                                      login,
311                                      &store))
312             return true;
313     }
314
315 if ((it = structVal.find("userdata")) != structVal.end())
316     {
317     std::vector<USER_PROPERTY_LOGGED<string> *> userdata;
318     userdata.push_back(ptr->GetProperty().userdata0.GetPointer());
319     userdata.push_back(ptr->GetProperty().userdata1.GetPointer());
320     userdata.push_back(ptr->GetProperty().userdata2.GetPointer());
321     userdata.push_back(ptr->GetProperty().userdata3.GetPointer());
322     userdata.push_back(ptr->GetProperty().userdata4.GetPointer());
323     userdata.push_back(ptr->GetProperty().userdata5.GetPointer());
324     userdata.push_back(ptr->GetProperty().userdata6.GetPointer());
325     userdata.push_back(ptr->GetProperty().userdata7.GetPointer());
326     userdata.push_back(ptr->GetProperty().userdata8.GetPointer());
327     userdata.push_back(ptr->GetProperty().userdata9.GetPointer());
328
329     std::vector<xmlrpc_c::value> udata(
330         xmlrpc_c::value_array(it->second).vectorValueValue()
331         );
332
333     for (unsigned i = 0; i < userdata.size(); ++i)
334         {
335         std::string value(IconvString(xmlrpc_c::value_string(udata[i]), "UTF-8", "KOI8-RU"));
336         if (userdata[i]->Get() != value)
337             if (!userdata[i]->Set(value,
338                                   admin,
339                                   login,
340                                   &store))
341                 return true;
342         }
343     }
344
345 if ((it = structVal.find("traff")) != structVal.end())
346     {
347     std::map<std::string, xmlrpc_c::value> traff(
348         static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(it->second))
349         );
350
351     DIR_TRAFF dtData;
352     dtData = ptr->GetProperty().up.Get();
353     if ((it = traff.find("mu")) != traff.end())
354         {
355         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
356
357         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
358             {
359             int64_t value;
360             if (str2x(xmlrpc_c::value_string(data[i]), value))
361                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month upload value'\n");
362             else
363                 dtData[i] = value;
364             }
365         if (!ptr->GetProperty().up.Set(dtData,
366                                    admin,
367                                    login,
368                                    &store))
369             return true;
370         }
371     dtData = ptr->GetProperty().down.Get();
372     if ((it = traff.find("md")) != traff.end())
373         {
374         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
375
376         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
377             {
378             int64_t value;
379             if (str2x(xmlrpc_c::value_string(data[i]), value))
380                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month download value'\n");
381             else
382                 dtData[i] = value;
383             }
384         if (!ptr->GetProperty().down.Set(dtData,
385                                      admin,
386                                      login,
387                                      &store))
388             return true;
389         }
390     }
391
392 if ((it = structVal.find("tariff")) != structVal.end())
393     {
394     std::string tariff(xmlrpc_c::value_string(it->second));
395     size_t pos = tariff.find('/');
396     std::string nextTariff;
397     if (pos != std::string::npos)
398         {
399         nextTariff = tariff.substr(pos + 1);
400         tariff = tariff.substr(0, pos);
401         }
402
403     if (tariffs->FindByName(tariff))
404         if (ptr->GetProperty().tariffName.Get() != tariff)
405             if (!ptr->GetProperty().tariffName.Set(tariff,
406                                                admin,
407                                                login,
408                                                &store))
409                 return true;
410
411     if (nextTariff != "" &&
412         tariffs->FindByName(nextTariff))
413         if (ptr->GetProperty().nextTariff.Get() != nextTariff)
414             if (!ptr->GetProperty().nextTariff.Set(tariff,
415                                                admin,
416                                                login,
417                                                &store))
418                 return true;
419     }
420
421 return false;
422 }