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