]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser.cpp
Fixed merge issues.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser.cpp
1 #include <stdio.h>
2 #include <sys/utsname.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 #include <arpa/inet.h>
7
8 #include <cstring>
9 #include <cerrno>
10 #include <sstream>
11
12 #include "stg/version.h"
13 #include "stg/tariffs.h"
14 #include "stg/user_property.h"
15 #include "stg/settings.h"
16 #include "stg/logger.h"
17 #include "parser.h"
18
19 #define  UNAME_LEN      (256)
20 //-----------------------------------------------------------------------------
21 //  GET SERVER INFO
22 //-----------------------------------------------------------------------------
23 int PARSER_GET_SERVER_INFO::ParseStart(void *, const char *el, const char **)
24 {
25 answerList->erase(answerList->begin(), answerList->end());
26 if (strcasecmp(el, "GetServerInfo") == 0)
27     {
28     return 0;
29     }
30 return -1;
31 }
32 //-----------------------------------------------------------------------------
33 int PARSER_GET_SERVER_INFO::ParseEnd(void *, const char *el)
34 {
35 if (strcasecmp(el, "GetServerInfo") == 0)
36     {
37     CreateAnswer();
38     return 0;
39     }
40 return -1;
41 }
42 //-----------------------------------------------------------------------------
43 void PARSER_GET_SERVER_INFO::CreateAnswer()
44 {
45 char s[UNAME_LEN + 128];
46 char un[UNAME_LEN];
47 struct utsname utsn;
48
49 uname(&utsn);
50 un[0] = 0;
51
52 strcat(un, utsn.sysname);
53 strcat(un, " ");
54 strcat(un, utsn.release);
55 strcat(un, " ");
56 strcat(un, utsn.machine);
57 strcat(un, " ");
58 strcat(un, utsn.nodename);
59
60 //answerList->clear();
61 answerList->erase(answerList->begin(), answerList->end());
62 answerList->push_back("<ServerInfo>");
63
64 sprintf(s, "<version value=\"%s\"/>", SERVER_VERSION);
65 answerList->push_back(s);
66
67 sprintf(s, "<tariff_num value=\"%llu\"/>", static_cast<unsigned long long>(tariffs->Count()));
68 answerList->push_back(s);
69
70 sprintf(s, "<tariff value=\"%d\"/>", 2);
71 answerList->push_back(s);
72
73 sprintf(s, "<users_num value=\"%llu\"/>", static_cast<unsigned long long>(users->Count()));
74 answerList->push_back(s);
75
76 sprintf(s, "<uname value=\"%s\"/>", un);
77 answerList->push_back(s);
78
79 sprintf(s, "<dir_num value=\"%d\"/>", DIR_NUM);
80 answerList->push_back(s);
81
82 sprintf(s, "<day_fee value=\"%d\"/>", settings->GetDayFee());
83 answerList->push_back(s);
84
85 for (int i = 0; i< DIR_NUM; i++)
86     {
87     std::string dn2e;
88     Encode12str(dn2e, settings->GetDirName(i));
89     sprintf(s, "<dir_name_%d value=\"%s\"/>", i, dn2e.c_str());
90     answerList->push_back(s);
91     }
92
93 answerList->push_back("</ServerInfo>");
94 }
95 //-----------------------------------------------------------------------------
96 //  GET USER
97 //-----------------------------------------------------------------------------
98 int PARSER_GET_USER::ParseStart(void *, const char *el, const char **attr)
99 {
100 if (strcasecmp(el, "GetUser") == 0)
101     {
102     if (attr[0] && attr[1])
103         login = attr[1];
104     else
105         {
106         //login.clear();
107         login.erase(login.begin(), login.end());
108         return -1;
109         }
110     return 0;
111     }
112 return -1;
113 }
114 //-----------------------------------------------------------------------------
115 int PARSER_GET_USER::ParseEnd(void *, const char *el)
116 {
117 if (strcasecmp(el, "GetUser") == 0)
118     {
119     CreateAnswer();
120     return 0;
121     }
122 return -1;
123 }
124 //-----------------------------------------------------------------------------
125 void PARSER_GET_USER::CreateAnswer()
126 {
127 std::string s;
128 std::string enc;
129
130 USER_PTR u;
131
132 answerList->erase(answerList->begin(), answerList->end());
133
134 if (users->FindByName(login, &u))
135     {
136     s = "<user result=\"error\"/>";
137     answerList->push_back(s);
138     return;
139     }
140
141 s = "<user result=\"ok\">";
142 answerList->push_back(s);
143
144 s = "<login value=\"" + u->GetLogin() + "\"/>";
145 answerList->push_back(s);
146
147 if (currAdmin->GetPriv()->userConf || currAdmin->GetPriv()->userPasswd)
148     s = "<password value=\"" + u->GetProperty().password.Get() + "\" />";
149 else
150     s = "<password value=\"++++++\"/>";
151 answerList->push_back(s);
152
153 strprintf(&s, "<cash value=\"%f\" />", u->GetProperty().cash.Get());
154 answerList->push_back(s);
155
156 strprintf(&s, "<freemb value=\"%f\" />", u->GetProperty().freeMb.Get());
157 answerList->push_back(s);
158
159 strprintf(&s, "<credit value=\"%f\" />", u->GetProperty().credit.Get());
160 answerList->push_back(s);
161
162 if (u->GetProperty().nextTariff.Get() != "")
163     {
164     strprintf(&s, "<tariff value=\"%s/%s\" />",
165               u->GetProperty().tariffName.Get().c_str(),
166               u->GetProperty().nextTariff.Get().c_str());
167     }
168 else
169     {
170     strprintf(&s, "<tariff value=\"%s\" />",
171               u->GetProperty().tariffName.Get().c_str());
172     }
173
174 answerList->push_back(s);
175
176 Encode12str(enc, u->GetProperty().note);
177 s = "<note value=\"" + enc + "\" />";
178 answerList->push_back(s);
179
180 Encode12str(enc, u->GetProperty().phone);
181 s = "<phone value=\"" + enc + "\" />";
182 answerList->push_back(s);
183
184 Encode12str(enc, u->GetProperty().address);
185 s = "<address value=\"" + enc + "\" />";
186 answerList->push_back(s);
187
188 Encode12str(enc, u->GetProperty().email);
189 s = "<email value=\"" + enc + "\" />";
190 answerList->push_back(s);
191
192
193 std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
194 userdata.push_back(u->GetProperty().userdata0.GetPointer());
195 userdata.push_back(u->GetProperty().userdata1.GetPointer());
196 userdata.push_back(u->GetProperty().userdata2.GetPointer());
197 userdata.push_back(u->GetProperty().userdata3.GetPointer());
198 userdata.push_back(u->GetProperty().userdata4.GetPointer());
199 userdata.push_back(u->GetProperty().userdata5.GetPointer());
200 userdata.push_back(u->GetProperty().userdata6.GetPointer());
201 userdata.push_back(u->GetProperty().userdata7.GetPointer());
202 userdata.push_back(u->GetProperty().userdata8.GetPointer());
203 userdata.push_back(u->GetProperty().userdata9.GetPointer());
204
205 std::string tmpI;
206 for (unsigned i = 0; i < userdata.size(); i++)
207     {
208     Encode12str(enc, userdata[i]->Get());
209     s = "<UserData" + x2str(i, tmpI) + " value=\"" + enc + "\" />";
210     answerList->push_back(s);
211     }
212
213 Encode12str(enc, u->GetProperty().realName);
214 s = "<name value=\"" + enc + "\" />";
215 answerList->push_back(s);
216
217 Encode12str(enc, u->GetProperty().group);
218 s = "<GROUP value=\"" + enc + "\" />";
219 answerList->push_back(s);
220
221 strprintf(&s, "<status value=\"%d\" />", u->GetConnected());
222 answerList->push_back(s);
223
224 strprintf(&s, "<aonline value=\"%d\" />", u->GetProperty().alwaysOnline.Get());
225 answerList->push_back(s);
226
227 strprintf(&s, "<currip value=\"%s\" />", inet_ntostring(u->GetCurrIP()).c_str());
228 answerList->push_back(s);
229
230 strprintf(&s, "<PingTime value=\"%lu\" />", u->GetPingTime());
231 answerList->push_back(s);
232
233 std::ostringstream sstr;
234 sstr << u->GetProperty().ips.Get();
235 strprintf(&s, "<ip value=\"%s\" />", sstr.str().c_str());
236 answerList->push_back(s);
237
238 char * ss;
239 ss = new char[DIR_NUM*25*4 + 50];
240 char st[50];
241 sprintf(ss, "<traff");
242 DIR_TRAFF upload;
243 DIR_TRAFF download;
244 download = u->GetProperty().down.Get();
245 upload = u->GetProperty().up.Get();
246
247 for (int j = 0; j < DIR_NUM; j++)
248     {
249     std::string s;
250     x2str(upload[j], s);
251     sprintf(st, " MU%d=\"%s\"", j, s.c_str());
252     strcat(ss, st);
253
254     x2str(download[j], s);
255     sprintf(st, " MD%d=\"%s\"", j, s.c_str());
256     strcat(ss, st);
257
258     sprintf(st, " SU%d=\"0\"", j);
259     strcat(ss, st);
260
261     sprintf(st, " SD%d=\"0\"", j);
262     strcat(ss, st);
263     }
264 strcat(ss, " />");
265 answerList->push_back(ss);
266 delete[] ss;
267
268 strprintf(&s, "<down value=\"%d\" />", u->GetProperty().disabled.Get());
269 answerList->push_back(s);
270
271 strprintf(&s, "<DisableDetailStat value=\"%d\" />", u->GetProperty().disabledDetailStat.Get());
272 answerList->push_back(s);
273
274 strprintf(&s, "<passive value=\"%d\" />", u->GetProperty().passive.Get());
275 answerList->push_back(s);
276
277 strprintf(&s, "<LastCash value=\"%f\" />", u->GetProperty().lastCashAdd.Get());
278 answerList->push_back(s);
279
280 strprintf(&s, "<LastTimeCash value=\"%ld\" />", u->GetProperty().lastCashAddTime.Get());
281 answerList->push_back(s);
282
283 strprintf(&s, "<LastActivityTime value=\"%ld\" />", u->GetProperty().lastActivityTime.Get());
284 answerList->push_back(s);
285
286 strprintf(&s, "<CreditExpire value=\"%ld\" />", u->GetProperty().creditExpire.Get());
287 answerList->push_back(s);
288
289 s = "<AuthorizedBy>";
290 std::vector<std::string> list(u->GetAuthorizers());
291 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
292     s += "<Auth name=\"" + *it + "\">";
293 s += "</AuthorizedBy>";
294 answerList->push_back(s);
295
296 strprintf(&s, "</user>");
297 answerList->push_back(s);
298 }
299 //-----------------------------------------------------------------------------
300 //  GET USERS
301 //-----------------------------------------------------------------------------
302 int PARSER_GET_USERS::ParseStart(void *, const char *el, const char ** attr)
303 {
304 /*if (attr && *attr && *(attr+1))
305     {
306     printfd(__FILE__, "attr=%s %s\n", *attr, *(attr+1));
307     }
308 else
309     {
310     printfd(__FILE__, "attr = NULL\n");
311     }*/
312
313 lastUpdateFound = false;
314 if (strcasecmp(el, "GetUsers") == 0)
315     {
316     while (attr && *attr && *(attr+1))
317         {
318         if (strcasecmp(*attr, "LastUpdate") == 0)
319             {
320             if (str2x(*(attr+1), lastUserUpdateTime) == 0)
321                 {
322                 //printfd(__FILE__, "lastUserUpdateTime=%d\n", lastUserUpdateTime);
323                 lastUpdateFound = true;
324                 }
325             else
326                 {
327                 //printfd(__FILE__, "NO lastUserUpdateTime\n");
328                 }
329             }
330         ++attr;
331         }
332
333     return 0;
334     }
335 return -1;
336 }
337 //-----------------------------------------------------------------------------
338 int PARSER_GET_USERS::ParseEnd(void *, const char *el)
339 {
340 if (strcasecmp(el, "GetUsers") == 0)
341     {
342     CreateAnswer();
343     return 0;
344     }
345 return -1;
346 }
347 //-----------------------------------------------------------------------------
348 void PARSER_GET_USERS::CreateAnswer()
349 {
350 answerList->erase(answerList->begin(), answerList->end());
351
352 std::string s;
353 std::string userStart;
354 std::string traffStart;
355 std::string traffMiddle;
356 std::string traffFinish;
357 std::string middle;
358 std::string userFinish;
359
360
361 std::string enc;
362
363 USER_PTR u;
364
365 int h = users->OpenSearch();
366 if (!h)
367     {
368     printfd(__FILE__, "users->OpenSearch() error\n");
369     users->CloseSearch(h);
370     return;
371     }
372 std::string updateTime;
373 x2str(time(NULL), updateTime);
374
375 if (lastUpdateFound)
376     answerList->push_back("<Users LastUpdate=\"" + updateTime + "\">");
377 else
378     answerList->push_back("<Users>");
379
380 while (1)
381     {
382     if (users->SearchNext(h, &u))
383         {
384         break;
385         }
386     userStart = "<user login=\"" + u->GetLogin() + "\">";
387     middle = "";
388
389     if (u->GetProperty().password.ModificationTime() > lastUserUpdateTime)
390         {
391         if (currAdmin->GetPriv()->userConf || currAdmin->GetPriv()->userPasswd)
392             s = "<password value=\"" + u->GetProperty().password.Get() + "\" />";
393         else
394             s = "<password value=\"++++++\"/>";
395         middle += s;
396         }
397
398
399     if (u->GetProperty().cash.ModificationTime() > lastUserUpdateTime)
400         {
401         strprintf(&s, "<cash value=\"%f\" />", u->GetProperty().cash.Get());
402         middle += s;
403         //printfd(__FILE__, "cash value=\"%f\"\n", u->GetProperty().cash.Get());
404         }
405
406
407     if (u->GetProperty().freeMb.ModificationTime() > lastUserUpdateTime)
408         {
409         strprintf(&s, "<freemb value=\"%f\" />", u->GetProperty().freeMb.Get());
410         middle += s;
411         }
412
413     if (u->GetProperty().credit.ModificationTime() > lastUserUpdateTime)
414         {
415         strprintf(&s, "<credit value=\"%f\" />", u->GetProperty().credit.Get());
416         middle += s;
417         }
418
419     if (u->GetProperty().nextTariff.Get() != "")
420         {
421         if (u->GetProperty().tariffName.ModificationTime() > lastUserUpdateTime
422             || u->GetProperty().nextTariff.ModificationTime() > lastUserUpdateTime)
423             {
424             strprintf(&s, "<tariff value=\"%s/%s\" />",
425                       u->GetProperty().tariffName.Get().c_str(),
426                       u->GetProperty().nextTariff.Get().c_str());
427             middle += s;
428             }
429         }
430     else
431         {
432         if (u->GetProperty().tariffName.ModificationTime() > lastUserUpdateTime)
433             {
434             strprintf(&s, "<tariff value=\"%s\" />",
435                       u->GetProperty().tariffName.Get().c_str());
436             middle += s;
437             }
438         }
439
440     if (u->GetProperty().note.ModificationTime() > lastUserUpdateTime)
441         {
442         Encode12str(enc, u->GetProperty().note);
443         strprintf(&s, "<note value=\"%s\" />", enc.c_str());
444         middle += s;
445         }
446
447     if (u->GetProperty().phone.ModificationTime() > lastUserUpdateTime)
448         {
449         Encode12str(enc, u->GetProperty().phone);
450         strprintf(&s, "<phone value=\"%s\" />", enc.c_str());
451         middle += s;
452         }
453
454     if (u->GetProperty().address.ModificationTime() > lastUserUpdateTime)
455         {
456         Encode12str(enc, u->GetProperty().address);
457         strprintf(&s, "<address value=\"%s\" />", enc.c_str());
458         middle += s;
459         }
460
461     if (u->GetProperty().email.ModificationTime() > lastUserUpdateTime)
462         {
463         Encode12str(enc, u->GetProperty().email);
464         strprintf(&s, "<email value=\"%s\" />", enc.c_str());
465         middle += s;
466         }
467
468     std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
469     userdata.push_back(u->GetProperty().userdata0.GetPointer());
470     userdata.push_back(u->GetProperty().userdata1.GetPointer());
471     userdata.push_back(u->GetProperty().userdata2.GetPointer());
472     userdata.push_back(u->GetProperty().userdata3.GetPointer());
473     userdata.push_back(u->GetProperty().userdata4.GetPointer());
474     userdata.push_back(u->GetProperty().userdata5.GetPointer());
475     userdata.push_back(u->GetProperty().userdata6.GetPointer());
476     userdata.push_back(u->GetProperty().userdata7.GetPointer());
477     userdata.push_back(u->GetProperty().userdata8.GetPointer());
478     userdata.push_back(u->GetProperty().userdata9.GetPointer());
479
480     std::string tmpI;
481     for (unsigned i = 0; i < userdata.size(); i++)
482         {
483         if (userdata[i]->ModificationTime() > lastUserUpdateTime)
484             {
485             Encode12str(enc, userdata[i]->Get());
486             s = "<UserData" + x2str(i, tmpI) + " value=\"" + enc + "\" />";
487             middle += s;
488             }
489         }
490
491     if (u->GetProperty().realName.ModificationTime() > lastUserUpdateTime)
492         {
493         Encode12str(enc, u->GetProperty().realName);
494         strprintf(&s, "<name value=\"%s\" />", enc.c_str());
495         middle += s;
496         }
497
498     if (u->GetProperty().group.ModificationTime() > lastUserUpdateTime)
499         {
500         Encode12str(enc, u->GetProperty().group);
501         strprintf(&s, "<GROUP value=\"%s\" />", enc.c_str());
502         middle += s;
503         }
504
505     if (u->GetProperty().alwaysOnline.ModificationTime() > lastUserUpdateTime)
506         {
507         strprintf(&s, "<aonline value=\"%d\" />", u->GetProperty().alwaysOnline.Get());
508         middle += s;
509         }
510
511     if (u->GetCurrIPModificationTime() > lastUserUpdateTime)
512         {
513         strprintf(&s, "<currip value=\"%s\" />", inet_ntostring(u->GetCurrIP()).c_str());
514         middle += s;
515         }
516
517
518     if (u->GetConnectedModificationTime() > lastUserUpdateTime)
519         {
520         strprintf(&s, "<status value=\"%d\" />", u->GetConnected());
521         middle += s;
522         }
523
524     if (u->GetPingTime() > lastUserUpdateTime)
525         {
526         strprintf(&s, "<PingTime value=\"%lu\" />", u->GetPingTime());
527         middle += s;
528         }
529
530     if (u->GetProperty().ips.ModificationTime() > lastUserUpdateTime)
531         {
532         std::ostringstream sstr;
533         sstr << u->GetProperty().ips.Get();
534         strprintf(&s, "<ip value=\"%s\" />", sstr.str().c_str());
535         middle += s;
536         }
537
538     char st[50];
539     traffStart = "<traff";
540     DIR_TRAFF upload;
541     DIR_TRAFF download;
542     download = u->GetProperty().down.Get();
543     upload = u->GetProperty().up.Get();
544     traffMiddle = "";
545
546     if (u->GetProperty().up.ModificationTime() > lastUserUpdateTime)
547         {
548         for (int j = 0; j < DIR_NUM; j++)
549             {
550             std::string s;
551             x2str(upload[j], s);
552             sprintf(st, " MU%d=\"%s\" ", j, s.c_str());
553             traffMiddle += st;
554             }
555         }
556
557     if (u->GetProperty().down.ModificationTime() > lastUserUpdateTime)
558         {
559         for (int j = 0; j < DIR_NUM; j++)
560             {
561             x2str(download[j], s);
562             sprintf(st, " MD%d=\"%s\" ", j, s.c_str());
563             traffMiddle += st;
564             }
565         }
566
567     traffFinish = " />";
568     if (traffMiddle.length() > 0)
569         {
570         middle += traffStart;
571         middle += traffMiddle;
572         middle += traffFinish;
573         }
574
575     if (u->GetProperty().disabled.ModificationTime() > lastUserUpdateTime)
576         {
577         strprintf(&s, "<down value=\"%d\" />", u->GetProperty().disabled.Get());
578         middle += s;
579         }
580
581     if (u->GetProperty().disabledDetailStat.ModificationTime() > lastUserUpdateTime)
582         {
583         strprintf(&s, "<DisableDetailStat value=\"%d\" />", u->GetProperty().disabledDetailStat.Get());
584         middle += s;
585         }
586
587     //printfd(__FILE__, ">>>>> %s\n", s.c_str());
588
589     if (u->GetProperty().passive.ModificationTime() > lastUserUpdateTime)
590         {
591         strprintf(&s, "<passive value=\"%d\" />", u->GetProperty().passive.Get());
592         middle += s;
593         }
594
595     if (u->GetProperty().lastCashAdd.ModificationTime() > lastUserUpdateTime)
596         {
597         strprintf(&s, "<LastCash value=\"%f\" />", u->GetProperty().lastCashAdd.Get());
598         middle += s;
599         }
600
601     if (u->GetProperty().lastCashAddTime.ModificationTime() > lastUserUpdateTime)
602         {
603         strprintf(&s, "<LastTimeCash value=\"%ld\" />", u->GetProperty().lastCashAddTime.Get());
604         middle += s;
605         }
606
607
608     if (u->GetProperty().lastActivityTime.ModificationTime() > lastUserUpdateTime)
609         {
610         strprintf(&s, "<LastActivityTime value=\"%ld\" />", u->GetProperty().lastActivityTime.Get());
611         middle += s;
612         }
613
614     if (u->GetProperty().creditExpire.ModificationTime() > lastUserUpdateTime)
615         {
616         strprintf(&s, "<CreditExpire value=\"%ld\" />", u->GetProperty().creditExpire.Get());
617         middle += s;
618         }
619
620
621     userFinish = "</user>";
622
623     if (middle.length() > 0)
624         {
625         /*printfd(__FILE__, "login: %s\n", u->GetLogin().c_str());
626         printfd(__FILE__, "middle: %s\n", middle.c_str());*/
627
628         answerList->push_back(userStart);
629         answerList->push_back(middle);
630         answerList->push_back(userFinish);
631         }
632     }
633
634 users->CloseSearch(h);
635
636 //answerList->push_back("</Users>");
637
638 answerList->push_back("</Users>");
639 }
640 //-----------------------------------------------------------------------------
641 //  ADD USER
642 //-----------------------------------------------------------------------------
643 int PARSER_ADD_USER::ParseStart(void *, const char *el, const char **attr)
644 {
645 depth++;
646
647 if (depth == 1)
648     {
649     if (strcasecmp(el, "AddUser") == 0)
650         {
651         return 0;
652         }
653     }
654 else
655     {
656     if (strcasecmp(el, "login") == 0)
657         {
658         login = attr[1];
659         return 0;
660         }
661     }
662 return -1;
663 }
664 //-----------------------------------------------------------------------------
665 int PARSER_ADD_USER::ParseEnd(void *, const char *el)
666 {
667 if (depth == 1)
668     {
669     if (strcasecmp(el, "AddUser") == 0)
670         {
671         CreateAnswer();
672         depth--;
673         return 0;
674         }
675     }
676
677 depth--;
678 return -1;
679 }
680 //-----------------------------------------------------------------------------
681 void PARSER_ADD_USER::Reset()
682 {
683 BASE_PARSER::Reset();
684 depth = 0;
685 }
686 //-----------------------------------------------------------------------------
687 void PARSER_ADD_USER::CreateAnswer()
688 {
689 //answerList->clear();
690 answerList->erase(answerList->begin(), answerList->end());
691
692 if (CheckUserData() == 0)
693     {
694     answerList->push_back("<AddUser result=\"ok\"/>");
695     }
696 else
697     {
698     answerList->push_back("<AddUser result=\"error\" reason=\"Access denied\"/>");
699     }
700 }
701 //-----------------------------------------------------------------------------
702 int PARSER_ADD_USER::CheckUserData()
703 {
704 USER_PTR u;
705 if (users->FindByName(login, &u))
706     {
707     return users->Add(login, currAdmin);
708     }
709 return -1;
710 }
711 //-----------------------------------------------------------------------------
712 //  PARSER CHG USER
713 //-----------------------------------------------------------------------------
714 PARSER_CHG_USER::PARSER_CHG_USER()
715     : BASE_PARSER(),
716       usr(NULL),
717       ucr(NULL),
718       upr(NULL),
719       downr(NULL),
720       cashMsg(),
721       login(),
722       cashMustBeAdded(false),
723       res(0)
724 {
725 Reset();
726 }
727 //-----------------------------------------------------------------------------
728 PARSER_CHG_USER::~PARSER_CHG_USER()
729 {
730 delete usr;
731 delete ucr;
732 delete[] upr;
733 delete[] downr;
734 }
735 //-----------------------------------------------------------------------------
736 void PARSER_CHG_USER::Reset()
737 {
738 depth = 0;
739 delete usr;
740
741 delete ucr;
742
743 delete[] upr;
744
745 delete[] downr;
746
747 usr = new USER_STAT_RES;
748 ucr = new USER_CONF_RES;
749
750 upr = new RESETABLE<uint64_t>[DIR_NUM];
751 downr = new RESETABLE<uint64_t>[DIR_NUM];
752 }
753 //-----------------------------------------------------------------------------
754 std::string PARSER_CHG_USER::EncChar2String(const char * strEnc)
755 {
756 std::string str;
757 Decode21str(str, strEnc);
758 return str;
759 }
760 //-----------------------------------------------------------------------------
761 int PARSER_CHG_USER::ParseStart(void *, const char *el, const char **attr)
762 {
763 depth++;
764
765 if (depth == 1)
766     {
767     if (strcasecmp(el, "SetUser") == 0)
768         {
769         return 0;
770         }
771     }
772 else
773     {
774     //printfd(__FILE__, "el=%s\n", el);
775     if (strcasecmp(el, "login") == 0)
776         {
777         login = attr[1];
778         return 0;
779         }
780
781     if (strcasecmp(el, "ip") == 0)
782         {
783         try
784             {
785             ucr->ips = StrToIPS(attr[1]);
786             }
787         catch (...)
788             {
789             printfd(__FILE__, "StrToIPS Error!\n");
790             }
791         }
792
793     if (strcasecmp(el, "password") == 0)
794         {
795         ucr->password = attr[1];
796         return 0;
797         }
798
799     if (strcasecmp(el, "address") == 0)
800         {
801         ucr->address = EncChar2String(attr[1]);
802         return 0;
803         }
804
805     if (strcasecmp(el, "aonline") == 0)
806         {
807         ucr->alwaysOnline = (*(attr[1]) != '0');
808         return 0;
809         }
810
811     if (strcasecmp(el, "cash") == 0)
812         {
813         if (attr[2] && (strcasecmp(attr[2], "msg") == 0))
814             {
815             cashMsg = EncChar2String(attr[3]);
816             }
817
818         double cash;
819         if (strtodouble2(attr[1], cash) == 0)
820             usr->cash = cash;
821
822         if (strcasecmp(attr[0], "set") == 0)
823             cashMustBeAdded = false;
824
825         if (strcasecmp(attr[0], "add") == 0)
826             cashMustBeAdded = true;
827
828         return 0;
829         }
830
831     if (strcasecmp(el, "CreditExpire") == 0)
832         {
833         long int creditExpire = 0;
834         if (str2x(attr[1], creditExpire) == 0)
835             ucr->creditExpire = (time_t)creditExpire;
836
837         return 0;
838         }
839
840     if (strcasecmp(el, "credit") == 0)
841         {
842         double credit;
843         if (strtodouble2(attr[1], credit) == 0)
844             ucr->credit = credit;
845         return 0;
846         }
847
848     if (strcasecmp(el, "freemb") == 0)
849         {
850         double freeMb;
851         if (strtodouble2(attr[1], freeMb) == 0)
852             usr->freeMb = freeMb;
853         return 0;
854         }
855
856     if (strcasecmp(el, "down") == 0)
857         {
858         int down = 0;
859         if (str2x(attr[1], down) == 0)
860             ucr->disabled = down;
861         return 0;
862         }
863
864     if (strcasecmp(el, "DisableDetailStat") == 0)
865         {
866         int disabledDetailStat = 0;
867         if (str2x(attr[1], disabledDetailStat) == 0)
868             ucr->disabledDetailStat = disabledDetailStat;
869         return 0;
870         }
871
872     if (strcasecmp(el, "email") == 0)
873         {
874         ucr->email = EncChar2String(attr[1]);
875         return 0;
876         }
877
878     for (int i = 0; i < USERDATA_NUM; i++)
879         {
880         char name[15];
881         sprintf(name, "userdata%d", i);
882         if (strcasecmp(el, name) == 0)
883             {
884             ucr->userdata[i] = EncChar2String(attr[1]);
885             return 0;
886             }
887         }
888
889     if (strcasecmp(el, "group") == 0)
890         {
891         ucr->group = EncChar2String(attr[1]);
892         return 0;
893         }
894
895     if (strcasecmp(el, "note") == 0)
896         {
897         ucr->note = EncChar2String(attr[1]);
898         return 0;
899         }
900
901     if (strcasecmp(el, "passive") == 0)
902         {
903         int passive = 0;
904         if (str2x(attr[1], passive) == 0)
905             ucr->passive = passive;
906         return 0;
907         }
908
909     if (strcasecmp(el, "phone") == 0)
910         {
911         ucr->phone = EncChar2String(attr[1]);
912         return 0;
913         }
914
915     if (strcasecmp(el, "Name") == 0)
916         {
917         ucr->realName = EncChar2String(attr[1]);
918         return 0;
919         }
920
921     if (strcasecmp(el, "traff") == 0)
922         {
923         int j = 0;
924         int dir;
925         DIR_TRAFF dtu;
926         DIR_TRAFF dtd;
927         uint64_t t = 0;
928         while (attr[j])
929             {
930             dir = attr[j][2] - '0';
931
932             if (strncasecmp(attr[j], "md", 2) == 0)
933                 {
934                 str2x(attr[j+1], t);
935                 dtd[dir] = t;
936                 downr[dir] = t;
937                 }
938             if (strncasecmp(attr[j], "mu", 2) == 0)
939                 {
940                 str2x(attr[j+1], t);
941                 dtu[dir] = t;
942                 upr[dir] = t;
943                 }
944             j+=2;
945             }
946         return 0;
947         }
948
949     if (strcasecmp(el, "tariff") == 0)
950         {
951         if (strcasecmp(attr[0], "now") == 0)
952             ucr->tariffName = attr[1];
953
954         if (strcasecmp(attr[0], "delayed") == 0)
955             ucr->nextTariff = attr[1];
956
957         return 0;
958         }
959     }
960 return -1;
961 }
962 //-----------------------------------------------------------------------------
963 int PARSER_CHG_USER::ParseEnd(void *, const char *el)
964 {
965 if (depth == 1)
966     {
967     if (strcasecmp(el, "SetUser") == 0)
968         {
969         AplayChanges();
970         CreateAnswer();
971         depth--;
972         return 0;
973         }
974     }
975
976 depth--;
977 return -1;
978 }
979 //-----------------------------------------------------------------------------
980 void PARSER_CHG_USER::CreateAnswer()
981 {
982 //answerList->clear();
983 answerList->erase(answerList->begin(), answerList->end());
984
985 switch (res)
986     {
987     case 0:
988         answerList->push_back("<SetUser result=\"ok\"/>");
989         break;
990     case -1:
991         answerList->push_back("<SetUser result=\"error\"/>");
992         break;
993     case -2:
994         answerList->push_back("<SetUser result=\"error\"/>");
995         break;
996     default:
997         answerList->push_back("<SetUser result=\"error\"/>");
998         break;
999     }
1000
1001 }
1002 //-----------------------------------------------------------------------------
1003 int PARSER_CHG_USER::AplayChanges()
1004 {
1005 printfd(__FILE__, "PARSER_CHG_USER::AplayChanges()\n");
1006 USER_PTR u;
1007
1008 res = 0;
1009 if (users->FindByName(login, &u))
1010     {
1011     res = -1;
1012     return -1;
1013     }
1014
1015 bool check = false;
1016 bool alwaysOnline = u->GetProperty().alwaysOnline;
1017 if (!ucr->alwaysOnline.empty())
1018     {
1019     check = true;
1020     alwaysOnline = ucr->alwaysOnline.const_data();
1021     }
1022 bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP();
1023 if (!ucr->ips.empty())
1024     {
1025     check = true;
1026     onlyOneIP = ucr->ips.const_data().OnlyOneIP();
1027     }
1028
1029 if (check && alwaysOnline && !onlyOneIP)
1030     {
1031     printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
1032     GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", currAdmin->GetLogStr().c_str());
1033     res = -1;
1034     return -1;
1035     }
1036
1037 for (size_t i = 0; i < ucr->ips.const_data().Count(); ++i)
1038     {
1039     CONST_USER_PTR user;
1040     uint32_t ip = ucr->ips.const_data().operator[](i).ip;
1041     if (users->IsIPInUse(ip, login, &user))
1042         {
1043         printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str());
1044         GetStgLogger()("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", currAdmin->GetLogStr().c_str(), inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str());
1045         res = -1;
1046         return -1;
1047         }
1048     }
1049
1050 if (!ucr->ips.empty())
1051     if (!u->GetProperty().ips.Set(ucr->ips.const_data(), currAdmin, login, store))
1052         res = -1;
1053
1054 if (!ucr->alwaysOnline.empty())
1055     if (!u->GetProperty().alwaysOnline.Set(ucr->alwaysOnline.const_data(),
1056                                       currAdmin, login, store))
1057         res = -1;
1058
1059 if (!ucr->address.empty())
1060     if (!u->GetProperty().address.Set(ucr->address.const_data(), currAdmin, login, store))
1061         res = -1;
1062
1063 if (!ucr->creditExpire.empty())
1064     if (!u->GetProperty().creditExpire.Set(ucr->creditExpire.const_data(),
1065                                       currAdmin, login, store))
1066         res = -1;
1067
1068 if (!ucr->credit.empty())
1069     if (!u->GetProperty().credit.Set(ucr->credit.const_data(), currAdmin, login, store))
1070         res = -1;
1071
1072 if (!usr->freeMb.empty())
1073     if (!u->GetProperty().freeMb.Set(usr->freeMb.const_data(), currAdmin, login, store))
1074         res = -1;
1075
1076 if (!ucr->disabled.empty())
1077     if (!u->GetProperty().disabled.Set(ucr->disabled.const_data(), currAdmin, login, store))
1078         res = -1;
1079
1080 if (!ucr->disabledDetailStat.empty())
1081     if (!u->GetProperty().disabledDetailStat.Set(ucr->disabledDetailStat.const_data(), currAdmin, login, store))
1082         res = -1;
1083
1084 if (!ucr->email.empty())
1085     if (!u->GetProperty().email.Set(ucr->email.const_data(), currAdmin, login, store))
1086         res = -1;
1087
1088 if (!ucr->group.empty())
1089     if (!u->GetProperty().group.Set(ucr->group.const_data(), currAdmin, login, store))
1090         res = -1;
1091
1092 if (!ucr->note.empty())
1093     if (!u->GetProperty().note.Set(ucr->note.const_data(), currAdmin, login, store))
1094         res = -1;
1095
1096 std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
1097 userdata.push_back(u->GetProperty().userdata0.GetPointer());
1098 userdata.push_back(u->GetProperty().userdata1.GetPointer());
1099 userdata.push_back(u->GetProperty().userdata2.GetPointer());
1100 userdata.push_back(u->GetProperty().userdata3.GetPointer());
1101 userdata.push_back(u->GetProperty().userdata4.GetPointer());
1102 userdata.push_back(u->GetProperty().userdata5.GetPointer());
1103 userdata.push_back(u->GetProperty().userdata6.GetPointer());
1104 userdata.push_back(u->GetProperty().userdata7.GetPointer());
1105 userdata.push_back(u->GetProperty().userdata8.GetPointer());
1106 userdata.push_back(u->GetProperty().userdata9.GetPointer());
1107
1108 for (int i = 0; i < (int)userdata.size(); i++)
1109     {
1110     if (!ucr->userdata[i].empty())
1111         {
1112         if(!userdata[i]->Set(ucr->userdata[i].const_data(), currAdmin, login, store))
1113             res = -1;
1114         }
1115     }
1116
1117 if (!ucr->passive.empty())
1118     if (!u->GetProperty().passive.Set(ucr->passive.const_data(), currAdmin, login, store))
1119         res = -1;
1120
1121 if (!ucr->password.empty())
1122     if (!u->GetProperty().password.Set(ucr->password.const_data(), currAdmin, login, store))
1123         res = -1;
1124
1125 if (!ucr->phone.empty())
1126     if (!u->GetProperty().phone.Set(ucr->phone.const_data(), currAdmin, login, store))
1127         res = -1;
1128
1129 if (!ucr->realName.empty())
1130     if (!u->GetProperty().realName.Set(ucr->realName.const_data(), currAdmin, login, store))
1131         res = -1;
1132
1133
1134 if (!usr->cash.empty())
1135     {
1136     //if (*currAdmin->GetPriv()->userCash)
1137         {
1138         if (cashMustBeAdded)
1139             {
1140             if (!u->GetProperty().cash.Set(usr->cash.const_data() + u->GetProperty().cash,
1141                                            currAdmin,
1142                                            login,
1143                                            store,
1144                                            cashMsg))
1145                 res = -1;
1146             }
1147         else
1148             {
1149             if (!u->GetProperty().cash.Set(usr->cash.const_data(), currAdmin, login, store, cashMsg))
1150                 res = -1;
1151             }
1152         }
1153     }
1154
1155
1156 if (!ucr->tariffName.empty())
1157     {
1158     if (tariffs->FindByName(ucr->tariffName.const_data()))
1159         {
1160         if (!u->GetProperty().tariffName.Set(ucr->tariffName.const_data(), currAdmin, login, store))
1161             res = -1;
1162         u->ResetNextTariff();
1163         }
1164     else
1165         {
1166         //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
1167         res = -1;
1168         }
1169     }
1170
1171 if (!ucr->nextTariff.empty())
1172     {
1173     if (tariffs->FindByName(ucr->nextTariff.const_data()))
1174         {
1175         if (!u->GetProperty().nextTariff.Set(ucr->nextTariff.const_data(), currAdmin, login, store))
1176             res = -1;
1177         }
1178     else
1179         {
1180         //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
1181         res = -1;
1182         }
1183     }
1184
1185 DIR_TRAFF up = u->GetProperty().up;
1186 DIR_TRAFF down = u->GetProperty().down;
1187 int upCount = 0;
1188 int downCount = 0;
1189 for (int i = 0; i < DIR_NUM; i++)
1190     {
1191     if (!upr[i].empty())
1192         {
1193         up[i] = upr[i].data();
1194         upCount++;
1195         }
1196     if (!downr[i].empty())
1197         {
1198         down[i] = downr[i].data();
1199         downCount++;
1200         }
1201     }
1202
1203 if (upCount)
1204     if (!u->GetProperty().up.Set(up, currAdmin, login, store))
1205         res = -1;
1206
1207 if (downCount)
1208     if (!u->GetProperty().down.Set(down, currAdmin, login, store))
1209         res = -1;
1210
1211 u->WriteConf();
1212 u->WriteStat();
1213
1214 return 0;
1215 }
1216 //-----------------------------------------------------------------------------
1217 //      SEND MESSAGE
1218 //-----------------------------------------------------------------------------
1219 int PARSER_SEND_MESSAGE::ParseStart(void *, const char *el, const char **attr)
1220 {
1221 if (strcasecmp(el, "Message") == 0)
1222     {
1223     for (int i = 0; i < 14; i++)
1224         {
1225         if (attr[i] == NULL)
1226             {
1227             result = res_params_error;
1228             CreateAnswer();
1229             printfd(__FILE__, "To few parameters\n");
1230             return 0;
1231             }
1232         }
1233
1234     for (int i = 0; i < 14; i+=2)
1235         {
1236         if (strcasecmp(attr[i], "login") == 0)
1237             {
1238             ParseLogins(attr[i+1]);
1239             /*if (users->FindByName(login, &u))
1240                 {
1241                 result = res_unknown;
1242                 break;
1243                 }*/
1244             }
1245
1246         if (strcasecmp(attr[i], "MsgVer") == 0)
1247             {
1248             str2x(attr[i+1], msg.header.ver);
1249             if (msg.header.ver != 1)
1250                 result = res_params_error;
1251             }
1252
1253         if (strcasecmp(attr[i], "MsgType") == 0)
1254             {
1255             str2x(attr[i+1], msg.header.type);
1256             if (msg.header.type != 1)
1257                 result = res_params_error;
1258             }
1259
1260         if (strcasecmp(attr[i], "Repeat") == 0)
1261             {
1262             str2x(attr[i+1], msg.header.repeat);
1263             if (msg.header.repeat < 0)
1264                 result = res_params_error;
1265             }
1266
1267         if (strcasecmp(attr[i], "RepeatPeriod") == 0)
1268             {
1269             str2x(attr[i+1], msg.header.repeatPeriod);
1270             }
1271
1272         if (strcasecmp(attr[i], "ShowTime") == 0)
1273             {
1274             str2x(attr[i+1], msg.header.showTime);
1275             }
1276
1277         if (strcasecmp(attr[i], "Text") == 0)
1278             {
1279             Decode21str(msg.text, attr[i+1]);
1280             result = res_ok;
1281             }
1282         }
1283     return 0;
1284     }
1285 return -1;
1286 }
1287 //-----------------------------------------------------------------------------
1288 int PARSER_SEND_MESSAGE::ParseEnd(void *, const char *el)
1289 {
1290 //MSG msg;
1291 if (strcasecmp(el, "Message") == 0)
1292     {
1293     result = res_unknown;
1294     for (unsigned i = 0; i < logins.size(); i++)
1295         {
1296         if (users->FindByName(logins[i], &u))
1297             {
1298             printfd(__FILE__, "User not found. %s\n", logins[i].c_str());
1299             continue;
1300             }
1301         msg.header.creationTime = static_cast<unsigned int>(stgTime);
1302         u->AddMessage(&msg);
1303         result = res_ok;
1304         }
1305     /*if (result == res_ok)
1306         {
1307         if (strcmp(login, "*") == 0)
1308             {
1309             msg.text = text;
1310             msg.prio = pri;
1311             printfd(__FILE__, "SendMsg text: %s\n", text);
1312             users->GetAllUsers(SendMessageAllUsers, &msg);
1313             }
1314         else
1315             {
1316             u->AddMessage(pri, text);
1317             }
1318         }*/
1319     CreateAnswer();
1320     return 0;
1321     }
1322 return -1;
1323 }
1324 //-----------------------------------------------------------------------------
1325 int PARSER_SEND_MESSAGE::ParseLogins(const char * login)
1326 {
1327 char * p;
1328 char * l = new char[strlen(login) + 1];
1329 strcpy(l, login);
1330 p = strtok(l, ":");
1331 logins.clear();
1332 while(p)
1333     {
1334     logins.push_back(p);
1335     p = strtok(NULL, ":");
1336     }
1337
1338 delete[] l;
1339 return 0;
1340 }
1341 //-----------------------------------------------------------------------------
1342 void PARSER_SEND_MESSAGE::CreateAnswer()
1343 {
1344 //answerList->clear();
1345 answerList->erase(answerList->begin(), answerList->end());
1346 //answerList->push_back("<SendMessageResult value=\"ok\"/>");
1347 //
1348 switch (result)
1349     {
1350     case res_ok:
1351         answerList->push_back("<SendMessageResult value=\"ok\"/>");
1352         break;
1353     case res_params_error:
1354         printfd(__FILE__, "res_params_error\n");
1355         answerList->push_back("<SendMessageResult value=\"Parameters error\"/>");
1356         break;
1357     case res_unknown:
1358         printfd(__FILE__, "res_unknown\n");
1359         answerList->push_back("<SendMessageResult value=\"Unknown user\"/>");
1360         break;
1361     default:
1362         printfd(__FILE__, "res_default\n");
1363     }
1364
1365 }
1366 //-----------------------------------------------------------------------------
1367 //      DEL USER
1368 //-----------------------------------------------------------------------------
1369 int PARSER_DEL_USER::ParseStart(void *, const char *el, const char **attr)
1370 {
1371 res = 0;
1372 if (strcasecmp(el, "DelUser") == 0)
1373     {
1374     if (attr[0] == NULL || attr[1] == NULL)
1375         {
1376         //CreateAnswer("Parameters error!");
1377         CreateAnswer();
1378         return 0;
1379         }
1380
1381     if (users->FindByName(attr[1], &u))
1382         {
1383         res = 1;
1384         CreateAnswer();
1385         return 0;
1386         }
1387     CreateAnswer();
1388     return 0;
1389     }
1390 return -1;
1391 }
1392 //-----------------------------------------------------------------------------
1393 int PARSER_DEL_USER::ParseEnd(void *, const char *el)
1394 {
1395 if (strcasecmp(el, "DelUser") == 0)
1396     {
1397     if (!res)
1398         users->Del(u->GetLogin(), currAdmin);
1399
1400     return 0;
1401     }
1402 return -1;
1403 }
1404 //-----------------------------------------------------------------------------
1405 void PARSER_DEL_USER::CreateAnswer()
1406 {
1407 if (res)
1408     answerList->push_back("<DelUser value=\"error\" reason=\"User not found\"/>");
1409 else
1410     answerList->push_back("<DelUser value=\"ok\"/>");
1411 }
1412 //-----------------------------------------------------------------------------
1413 /*void PARSERDELUSER::CreateAnswer(char * mes)
1414 {
1415 //answerList->clear();
1416 answerList->erase(answerList->begin(), answerList->end());
1417
1418 char str[255];
1419 sprintf(str, "<DelUser value=\"%s\"/>", mes);
1420 answerList->push_back(str);
1421 }*/
1422 //-----------------------------------------------------------------------------
1423 //  CHECK USER
1424 // <checkuser login="vasya" password=\"123456\"/>
1425 //-----------------------------------------------------------------------------
1426 int PARSER_CHECK_USER::ParseStart(void *, const char *el, const char **attr)
1427 {
1428 result = false;
1429
1430 if (strcasecmp(el, "CheckUser") == 0)
1431     {
1432     if (attr[0] == NULL || attr[1] == NULL
1433      || attr[2] == NULL || attr[3] == NULL)
1434         {
1435         result = false;
1436         CreateAnswer();
1437         printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
1438         return 0;
1439         }
1440
1441     USER_PTR user;
1442     if (users->FindByName(attr[1], &user))
1443         {
1444         result = false;
1445         CreateAnswer();
1446         printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
1447         return 0;
1448         }
1449
1450     if (strcmp(user->GetProperty().password.Get().c_str(), attr[3]))
1451         {
1452         result = false;
1453         CreateAnswer();
1454         printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
1455         return 0;
1456         }
1457
1458     result = true;
1459     CreateAnswer();
1460     return 0;
1461     }
1462 return -1;
1463 }
1464 //-----------------------------------------------------------------------------
1465 int PARSER_CHECK_USER::ParseEnd(void *, const char *el)
1466 {
1467 if (strcasecmp(el, "CheckUser") == 0)
1468     {
1469     return 0;
1470     }
1471 return -1;
1472 }
1473 //-----------------------------------------------------------------------------
1474 void PARSER_CHECK_USER::CreateAnswer()
1475 {
1476 if (result)
1477     answerList->push_back("<CheckUser value=\"Ok\"/>");
1478 else
1479     answerList->push_back("<CheckUser value=\"Err\"/>");
1480 }
1481 //-----------------------------------------------------------------------------
1482 //-----------------------------------------------------------------------------
1483 //-----------------------------------------------------------------------------