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