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