]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser.cpp
Rearranged some headers in sgconfig.
[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         DIR_TRAFF dtu;
925         DIR_TRAFF dtd;
926         uint64_t t = 0;
927         while (attr[j])
928             {
929             int 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 /*void PARSERDELUSER::CreateAnswer(char * mes)
1413 {
1414 //answerList->clear();
1415 answerList->erase(answerList->begin(), answerList->end());
1416
1417 char str[255];
1418 sprintf(str, "<DelUser value=\"%s\"/>", mes);
1419 answerList->push_back(str);
1420 }*/
1421 //-----------------------------------------------------------------------------
1422 //  CHECK USER
1423 // <checkuser login="vasya" password=\"123456\"/>
1424 //-----------------------------------------------------------------------------
1425 int PARSER_CHECK_USER::ParseStart(void *, const char *el, const char **attr)
1426 {
1427 result = false;
1428
1429 if (strcasecmp(el, "CheckUser") == 0)
1430     {
1431     if (attr[0] == NULL || attr[1] == NULL
1432      || attr[2] == NULL || attr[3] == NULL)
1433         {
1434         result = false;
1435         CreateAnswer();
1436         printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
1437         return 0;
1438         }
1439
1440     USER_PTR user;
1441     if (users->FindByName(attr[1], &user))
1442         {
1443         result = false;
1444         CreateAnswer();
1445         printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
1446         return 0;
1447         }
1448
1449     if (strcmp(user->GetProperty().password.Get().c_str(), attr[3]))
1450         {
1451         result = false;
1452         CreateAnswer();
1453         printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
1454         return 0;
1455         }
1456
1457     result = true;
1458     CreateAnswer();
1459     return 0;
1460     }
1461 return -1;
1462 }
1463 //-----------------------------------------------------------------------------
1464 int PARSER_CHECK_USER::ParseEnd(void *, const char *el)
1465 {
1466 if (strcasecmp(el, "CheckUser") == 0)
1467     {
1468     return 0;
1469     }
1470 return -1;
1471 }
1472 //-----------------------------------------------------------------------------
1473 void PARSER_CHECK_USER::CreateAnswer()
1474 {
1475 if (result)
1476     answerList->push_back("<CheckUser value=\"Ok\"/>");
1477 else
1478     answerList->push_back("<CheckUser value=\"Err\"/>");
1479 }
1480 //-----------------------------------------------------------------------------
1481 //-----------------------------------------------------------------------------
1482 //-----------------------------------------------------------------------------