]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser.cpp
Модификация библиотеки conffiles
[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::CheckUserData()
1007 {
1008 return true;
1009 }
1010 //-----------------------------------------------------------------------------
1011 int PARSER_CHG_USER::AplayChanges()
1012 {
1013 user_iter u;
1014
1015 res = 0;
1016 if (users->FindByName(login, &u))
1017     {
1018     res = -1;
1019     return -1;
1020     }
1021
1022 if (!ucr->ips.res_empty())
1023     if (!u->property.ips.Set(ucr->ips.const_data(), currAdmin, login, store))
1024         res = -1;
1025
1026 if (!ucr->address.res_empty())
1027     if (!u->property.address.Set(ucr->address.const_data(), currAdmin, login, store))
1028         res = -1;
1029
1030 if (!ucr->alwaysOnline.res_empty())
1031     if (!u->property.alwaysOnline.Set(ucr->alwaysOnline.const_data(),
1032                                       currAdmin, login, store))
1033         res = -1;
1034
1035 if (!ucr->creditExpire.res_empty())
1036     if (!u->property.creditExpire.Set(ucr->creditExpire.const_data(),
1037                                       currAdmin, login, store))
1038         res = -1;
1039
1040 if (!ucr->credit.res_empty())
1041     if (!u->property.credit.Set(ucr->credit.const_data(), currAdmin, login, store))
1042         res = -1;
1043
1044 if (!usr->freeMb.res_empty())
1045     if (!u->property.freeMb.Set(usr->freeMb.const_data(), currAdmin, login, store))
1046         res = -1;
1047
1048 if (!ucr->disabled.res_empty())
1049     if (!u->property.disabled.Set(ucr->disabled.const_data(), currAdmin, login, store))
1050         res = -1;
1051
1052 if (!ucr->disabledDetailStat.res_empty())
1053     if (!u->property.disabledDetailStat.Set(ucr->disabledDetailStat.const_data(), currAdmin, login, store))
1054         res = -1;
1055
1056 if (!ucr->email.res_empty())
1057     if (!u->property.email.Set(ucr->email.const_data(), currAdmin, login, store))
1058         res = -1;
1059
1060 if (!ucr->group.res_empty())
1061     if (!u->property.group.Set(ucr->group.const_data(), currAdmin, login, store))
1062         res = -1;
1063
1064 if (!ucr->note.res_empty())
1065     if (!u->property.note.Set(ucr->note.const_data(), currAdmin, login, store))
1066         res = -1;
1067
1068 vector<USER_PROPERTY_LOGGED<string> *> userdata;
1069 userdata.push_back(u->property.userdata0.GetPointer());
1070 userdata.push_back(u->property.userdata1.GetPointer());
1071 userdata.push_back(u->property.userdata2.GetPointer());
1072 userdata.push_back(u->property.userdata3.GetPointer());
1073 userdata.push_back(u->property.userdata4.GetPointer());
1074 userdata.push_back(u->property.userdata5.GetPointer());
1075 userdata.push_back(u->property.userdata6.GetPointer());
1076 userdata.push_back(u->property.userdata7.GetPointer());
1077 userdata.push_back(u->property.userdata8.GetPointer());
1078 userdata.push_back(u->property.userdata9.GetPointer());
1079
1080 for (int i = 0; i < (int)userdata.size(); i++)
1081     {
1082     if (!ucr->userdata[i].res_empty())
1083         {
1084         if(!userdata[i]->Set(ucr->userdata[i].const_data(), currAdmin, login, store))
1085             res = -1;
1086         }
1087     }
1088
1089 if (!ucr->passive.res_empty())
1090     if (!u->property.passive.Set(ucr->passive.const_data(), currAdmin, login, store))
1091         res = -1;
1092
1093 if (!ucr->password.res_empty())
1094     if (!u->property.password.Set(ucr->password.const_data(), currAdmin, login, store))
1095         res = -1;
1096
1097 if (!ucr->phone.res_empty())
1098     if (!u->property.phone.Set(ucr->phone.const_data(), currAdmin, login, store))
1099         res = -1;
1100
1101 if (!ucr->realName.res_empty())
1102     if (!u->property.realName.Set(ucr->realName.const_data(), currAdmin, login, store))
1103         res = -1;
1104
1105
1106 if (!usr->cash.res_empty())
1107     {
1108     //if (currAdmin->GetPriv()->userCash)
1109         {
1110         if (cashMustBeAdded)
1111             {
1112             if (!u->property.cash.Set(usr->cash.const_data() + u->property.cash,
1113                                       currAdmin,
1114                                       login,
1115                                       store,
1116                                       cashMsg))
1117                 res = -1;
1118             }
1119         else
1120             {
1121             if (!u->property.cash.Set(usr->cash.const_data(), currAdmin, login, store, cashMsg))
1122                 res = -1;
1123             }
1124         }
1125     }
1126
1127
1128 if (!ucr->tariffName.res_empty())
1129     {
1130     if (tariffs->FindByName(ucr->tariffName.const_data()))
1131         {
1132         if (!u->property.tariffName.Set(ucr->tariffName.const_data(), currAdmin, login, store))
1133             res = -1;
1134         u->ResetNextTariff();
1135         }
1136     else
1137         {
1138         //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
1139         res = -1;
1140         }
1141     }
1142
1143 if (!ucr->nextTariff.res_empty())
1144     {
1145     if (tariffs->FindByName(ucr->nextTariff.const_data()))
1146         {
1147         if (!u->property.nextTariff.Set(ucr->nextTariff.const_data(), currAdmin, login, store))
1148             res = -1;
1149         }
1150     else
1151         {
1152         //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
1153         res = -1;
1154         }
1155     }
1156
1157 DIR_TRAFF up = u->property.up;
1158 DIR_TRAFF down = u->property.down;
1159 int upCount = 0;
1160 int downCount = 0;
1161 for (int i = 0; i < DIR_NUM; i++)
1162     {
1163     if (!upr[i].res_empty())
1164         {
1165         up[i] = upr[i];
1166         upCount++;
1167         }
1168     if (!downr[i].res_empty())
1169         {
1170         down[i] = downr[i];
1171         downCount++;
1172         }
1173     }
1174
1175 if (upCount)
1176     if (!u->property.up.Set(up, currAdmin, login, store))
1177         res = -1;
1178
1179 if (downCount)
1180     if (!u->property.down.Set(down, currAdmin, login, store))
1181         res = -1;
1182
1183 /*if (!usr->down.res_empty())
1184     {
1185     u->property.down.Set(usr->down.const_data(), currAdmin, login, store);
1186     }
1187 if (!usr->up.res_empty())
1188     {
1189     u->property.up.Set(usr->up.const_data(), currAdmin, login, store);
1190     }*/
1191
1192 u->WriteConf();
1193 u->WriteStat();
1194
1195 return 0;
1196 }
1197 //-----------------------------------------------------------------------------
1198 //      SEND MESSAGE
1199 //-----------------------------------------------------------------------------
1200 int PARSER_SEND_MESSAGE::ParseStart(void *, const char *el, const char **attr)
1201 {
1202 if (strcasecmp(el, "Message") == 0)
1203     {
1204     for (int i = 0; i < 14; i++)
1205         {
1206         if (attr[i] == NULL)
1207             {
1208             result = res_params_error;
1209             CreateAnswer();
1210             printfd(__FILE__, "To few parameters\n");
1211             return 0;
1212             }
1213         }
1214
1215     for (int i = 0; i < 14; i+=2)
1216         {
1217         if (strcasecmp(attr[i], "login") == 0)
1218             {
1219             ParseLogins(attr[i+1]);
1220             /*if (users->FindByName(login, &u))
1221                 {
1222                 result = res_unknown;
1223                 break;
1224                 }*/
1225             }
1226
1227         if (strcasecmp(attr[i], "MsgVer") == 0)
1228             {
1229             str2x(attr[i+1], msg.header.ver);
1230             if (msg.header.ver != 1)
1231                 result = res_params_error;
1232             }
1233
1234         if (strcasecmp(attr[i], "MsgType") == 0)
1235             {
1236             str2x(attr[i+1], msg.header.type);
1237             if (msg.header.type != 1)
1238                 result = res_params_error;
1239             }
1240
1241         if (strcasecmp(attr[i], "Repeat") == 0)
1242             {
1243             str2x(attr[i+1], msg.header.repeat);
1244             if (msg.header.repeat < 0)
1245                 result = res_params_error;
1246             }
1247
1248         if (strcasecmp(attr[i], "RepeatPeriod") == 0)
1249             {
1250             str2x(attr[i+1], msg.header.repeatPeriod);
1251             }
1252
1253         if (strcasecmp(attr[i], "ShowTime") == 0)
1254             {
1255             str2x(attr[i+1], msg.header.showTime);
1256             }
1257
1258         if (strcasecmp(attr[i], "Text") == 0)
1259             {
1260             Decode21str(msg.text, attr[i+1]);
1261             result = res_ok;
1262             }
1263         }
1264     return 0;
1265     }
1266 return -1;
1267 }
1268 //-----------------------------------------------------------------------------
1269 int PARSER_SEND_MESSAGE::ParseEnd(void *, const char *el)
1270 {
1271 //MSG msg;
1272 if (strcasecmp(el, "Message") == 0)
1273     {
1274     result = res_unknown;
1275     for (unsigned i = 0; i < logins.size(); i++)
1276         {
1277         if (users->FindByName(logins[i], &u))
1278             {
1279             printfd(__FILE__, "User not found. %s\n", logins[i].c_str());
1280             continue;
1281             }
1282         msg.header.creationTime = stgTime;
1283         u->AddMessage(&msg);
1284         result = res_ok;
1285         }
1286     /*if (result == res_ok)
1287         {
1288         if (strcmp(login, "*") == 0)
1289             {
1290             msg.text = text;
1291             msg.prio = pri;
1292             printfd(__FILE__, "SendMsg text: %s\n", text);
1293             users->GetAllUsers(SendMessageAllUsers, &msg);
1294             }
1295         else
1296             {
1297             u->AddMessage(pri, text);
1298             }
1299         }*/
1300     CreateAnswer();
1301     return 0;
1302     }
1303 return -1;
1304 }
1305 //-----------------------------------------------------------------------------
1306 int PARSER_SEND_MESSAGE::ParseLogins(const char * login)
1307 {
1308 char * p;
1309 char * l = new char[strlen(login) + 1];
1310 strcpy(l, login);
1311 p = strtok(l, ":");
1312 logins.clear();
1313 while(p)
1314     {
1315     logins.push_back(p);
1316     p = strtok(NULL, ":");
1317     }
1318
1319 delete[] l;
1320 return 0;
1321 }
1322 //-----------------------------------------------------------------------------
1323 void PARSER_SEND_MESSAGE::CreateAnswer()
1324 {
1325 //answerList->clear();
1326 answerList->erase(answerList->begin(), answerList->end());
1327 //answerList->push_back("<SendMessageResult value=\"ok\"/>");
1328 //
1329 switch (result)
1330     {
1331     case res_ok:
1332         answerList->push_back("<SendMessageResult value=\"ok\"/>");
1333         break;
1334     case res_params_error:
1335         printfd(__FILE__, "res_params_error\n");
1336         answerList->push_back("<SendMessageResult value=\"Parameters error\"/>");
1337         break;
1338     case res_unknown:
1339         printfd(__FILE__, "res_unknown\n");
1340         answerList->push_back("<SendMessageResult value=\"Unknown user\"/>");
1341         break;
1342     default:
1343         printfd(__FILE__, "res_default\n");
1344     }
1345
1346 }
1347 //-----------------------------------------------------------------------------
1348 //      DEL USER
1349 //-----------------------------------------------------------------------------
1350 int PARSER_DEL_USER::ParseStart(void *, const char *el, const char **attr)
1351 {
1352 res = 0;
1353 if (strcasecmp(el, "DelUser") == 0)
1354     {
1355     if (attr[0] == NULL || attr[1] == NULL)
1356         {
1357         //CreateAnswer("Parameters error!");
1358         CreateAnswer();
1359         return 0;
1360         }
1361
1362     if (users->FindByName(attr[1], &u))
1363         {
1364         res = 1;
1365         CreateAnswer();
1366         return 0;
1367         }
1368     CreateAnswer();
1369     return 0;
1370     }
1371 return -1;
1372 }
1373 //-----------------------------------------------------------------------------
1374 int PARSER_DEL_USER::ParseEnd(void *, const char *el)
1375 {
1376 if (strcasecmp(el, "DelUser") == 0)
1377     {
1378     if (!res)
1379         users->Del(u->GetLogin(), currAdmin);
1380
1381     return 0;
1382     }
1383 return -1;
1384 }
1385 //-----------------------------------------------------------------------------
1386 void PARSER_DEL_USER::CreateAnswer()
1387 {
1388 if (res)
1389     answerList->push_back("<DelUser value=\"error\" reason=\"User not found\"/>");
1390 else
1391     answerList->push_back("<DelUser value=\"ok\"/>");
1392 }
1393 //-----------------------------------------------------------------------------
1394 /*void PARSERDELUSER::CreateAnswer(char * mes)
1395 {
1396 //answerList->clear();
1397 answerList->erase(answerList->begin(), answerList->end());
1398
1399 char str[255];
1400 sprintf(str, "<DelUser value=\"%s\"/>", mes);
1401 answerList->push_back(str);
1402 }*/
1403 //-----------------------------------------------------------------------------
1404 //  CHECK USER
1405 // <checkuser login="vasya" password=\"123456\"/>
1406 //-----------------------------------------------------------------------------
1407 int PARSER_CHECK_USER::ParseStart(void *, const char *el, const char **attr)
1408 {
1409 result = false;
1410
1411 if (strcasecmp(el, "CheckUser") == 0)
1412     {
1413     if (attr[0] == NULL || attr[1] == NULL
1414      || attr[2] == NULL || attr[3] == NULL)
1415         {
1416         result = false;
1417         CreateAnswer();
1418         printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
1419         return 0;
1420         }
1421
1422     user_iter user;
1423     if (users->FindByName(attr[1], &user))
1424         {
1425         result = false;
1426         CreateAnswer();
1427         printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
1428         return 0;
1429         }
1430
1431     if (strcmp(user->property.password.Get().c_str(), attr[3]))
1432         {
1433         result = false;
1434         CreateAnswer();
1435         printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
1436         return 0;
1437         }
1438
1439     result = true;
1440     CreateAnswer();
1441     return 0;
1442     }
1443 return -1;
1444 }
1445 //-----------------------------------------------------------------------------
1446 int PARSER_CHECK_USER::ParseEnd(void *, const char *el)
1447 {
1448 if (strcasecmp(el, "CheckUser") == 0)
1449     {
1450     return 0;
1451     }
1452 return -1;
1453 }
1454 //-----------------------------------------------------------------------------
1455 void PARSER_CHECK_USER::CreateAnswer()
1456 {
1457 if (result)
1458     answerList->push_back("<CheckUser value=\"Ok\"/>");
1459 else
1460     answerList->push_back("<CheckUser value=\"Err\"/>");
1461 }
1462 //-----------------------------------------------------------------------------
1463 //-----------------------------------------------------------------------------
1464 //-----------------------------------------------------------------------------