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