2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2010/08/04 00:40:00 $
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
36 #include "stg/common.h"
37 #include "stg/const.h"
38 #include "stg/servconf.h"
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 PARSER_GET_USERS::PARSER_GET_USERS()
46 : RecvUserDataCb(NULL),
51 //-----------------------------------------------------------------------------
52 int PARSER_GET_USERS::ParseStart(const char *el, const char **attr)
67 ParseUserParams(el, attr);
71 //-----------------------------------------------------------------------------
72 void PARSER_GET_USERS::ParseEnd(const char *)
79 RecvUserDataCb(&user, userDataCb);
83 //-----------------------------------------------------------------------------
84 void PARSER_GET_USERS::ParseUsers(const char * el, const char ** attr)
86 if (strcasecmp(el, "users") == 0)
93 //-----------------------------------------------------------------------------
94 void PARSER_GET_USERS::ParseUser(const char * el, const char ** attr)
98 if (strcasecmp(el, "user") != 0)
103 if (strcasecmp(attr[0], "login") != 0)
107 user.login = attr[1];
110 //-----------------------------------------------------------------------------
111 void PARSER_GET_USERS::ParseUserParams(const char * el, const char ** attr)
113 if (strcasecmp(el, "cash") == 0)
115 if (strtodouble2(attr[1], user.cash) < 0)
121 /*if (strcasecmp(el, "LastCash") == 0)
123 if (strtodouble2(attr[1], user.lastCash) < 0)
125 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
130 /*if (strcasecmp(el, "LastActivityTime") == 0)
132 if (strtol(attr[1], user.lastActivityTime) < 0)
134 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
140 /*if (strcasecmp(el, "LastTimeCash") == 0)
142 if (strtol(attr[1], user.lastTimeCash) < 0)
144 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
149 /*if (strcasecmp(el, "CashExpire") == 0)
151 if (strtol(attr[1], user.cashExpire) < 0)
153 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
158 if (strcasecmp(el, "credit") == 0)
160 if (strtodouble2(attr[1], user.credit) < 0)
166 if (strcasecmp(el, "creditExpire") == 0)
168 if (str2x(attr[1], user.creditExpire) < 0)
174 /*if (strcasecmp(el, "freemb") == 0)
176 if (strtodouble2(attr[1], user.freeMb) < 0)
178 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
183 if (strcasecmp(el, "down") == 0)
185 if (str2x(attr[1], user.down) < 0)
191 if (strcasecmp(el, "passive") == 0)
193 if (str2x(attr[1], user.passive) < 0)
199 if (strcasecmp(el, "disableDetailStat") == 0)
201 if (str2x(attr[1], user.disableDetailStat) < 0)
208 if (strcasecmp(el, "status") == 0)
210 if (str2x(attr[1], user.connected) < 0)
216 if (strcasecmp(el, "aonline") == 0)
218 if (str2x(attr[1], user.alwaysOnline) < 0)
224 if (strcasecmp(el, "currip") == 0)
226 user.ip = inet_addr(attr[1]);
229 if (strcasecmp(el, "ip") == 0)
235 if (strcasecmp(el, "tariff") == 0)
237 //KOIToWin(user.tariff, *(attr+1), TARIFF_LEN);
238 user.tariff = attr[1];
242 if (strcasecmp(el, "password") == 0)
244 user.password = *(attr+1);
248 if (strcasecmp(el, "iface") == 0)
250 user.iface = attr[1];
254 /*if (strcasecmp(el, "name") == 0)
256 / *char nameEnc[REALNM_LEN * 2 + 1];
257 char name[REALNM_LEN];
258 strncpy(nameEnc, attr[1], REALNM_LEN * 2 + 1);
259 Decode21(name, nameEnc);
260 KOIToWin(user.realName, name, REALNM_LEN);* /
261 Decode21str(user.realName, attr[1]);
265 if (strcasecmp(el, "address") == 0)
267 /*char addressEnc[ADDR_LEN * 2 + 1];
268 char address[ADDR_LEN];
269 strncpy(addressEnc, attr[1], ADDR_LEN * 2 + 1);
270 Decode21(address, addressEnc);
271 KOIToWin(user.address, address, ADDR_LEN);*/
272 Decode21str(user.address, attr[1]);
276 if (strcasecmp(el, "phone") == 0)
278 /*char phoneEnc[PHONE_LEN * 2 + 1];
279 char phone[PHONE_LEN];
280 strncpy(phoneEnc, attr[1], PHONE_LEN * 2 + 1);
281 Decode21(phone, phoneEnc);
282 KOIToWin(user.phone, phone, PHONE_LEN);*/
283 Decode21str(user.phone, attr[1]);
287 if (strcasecmp(el, "note") == 0)
289 /*char noteEnc[NOTE_LEN * 2 + 1];
291 strncpy(noteEnc, attr[1], NOTE_LEN * 2 + 1);*/
292 //KOIToWin(user.note, note, NOTE_LEN);
294 Decode21str(user.note, attr[1]);
298 if (strcasecmp(el, "email") == 0)
300 /*char emailEnc[EMAIL_LEN * 2 + 1];
301 char email[EMAIL_LEN];
302 strncpy(emailEnc, attr[1], EMAIL_LEN * 2 + 1);
303 Decode21(email, emailEnc);
304 //KOIToWin(user.email, email, EMAIL_LEN);
305 user.email = email;*/
306 Decode21str(user.email, attr[1]);
310 if (strcasecmp(el, "group") == 0)
312 /*char groupEnc[GROUP_LEN * 2 + 1];
313 char group[GROUP_LEN];
314 strncpy(groupEnc, attr[1], GROUP_LEN * 2 + 1);
315 Decode21(group, groupEnc);
316 //KOIToWin(user.group, group, GROUP_LEN);
317 user.group = group;*/
318 Decode21str(user.group, attr[1]);
322 if (strcasecmp(el, "traff") == 0)
324 ParseUserLoadStat(el, attr);
329 //-----------------------------------------------------------------------------
330 void PARSER_GET_USERS::ParseUserLoadStat(const char *, const char ** attr)
336 for (int j = 0; j < DIR_NUM; j++)
338 sprintf(dir, "MU%d", j);
339 if (strcasecmp(dir, attr[i]) == 0)
341 str2x(attr[i+1], user.stat.mu[j]);
345 for (int j = 0; j < DIR_NUM; j++)
347 sprintf(dir, "MD%d", j);
348 if (strcasecmp(dir, attr[i]) == 0)
350 str2x(attr[i+1], user.stat.md[j]);
354 for (int j = 0; j < DIR_NUM; j++)
356 sprintf(dir, "SU%d", j);
357 if (strcasecmp(dir, attr[i]) == 0)
359 str2x(attr[i+1], user.stat.su[j]);
363 for (int j = 0; j < DIR_NUM; j++)
365 sprintf(dir, "SD%d", j);
366 if (strcasecmp(dir, attr[i]) == 0)
368 str2x(attr[i+1], user.stat.sd[j]);
376 //-----------------------------------------------------------------------------
377 void PARSER_GET_USERS::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
382 //-----------------------------------------------------------------------------
383 //-----------------------------------------------------------------------------
384 //-----------------------------------------------------------------------------
385 PARSER_GET_USER::PARSER_GET_USER()
386 : RecvUserDataCb(NULL),
391 //-----------------------------------------------------------------------------
392 int PARSER_GET_USER::ParseStart(const char *el, const char **attr)
402 ParseUserParams(el, attr);
406 //-----------------------------------------------------------------------------
407 void PARSER_GET_USER::ParseEnd(const char *)
414 RecvUserDataCb(&user, userDataCb);
418 //-----------------------------------------------------------------------------
419 void PARSER_GET_USER::ParseUser(const char * el, const char ** attr)
421 if (strcasecmp(el, "user") == 0)
423 if (strcasecmp(attr[1], "error") == 0)
428 //-----------------------------------------------------------------------------
429 struct ParsedStringParams
434 //-----------------------------------------------------------------------------
435 struct ParsedDoubleParams
440 //-----------------------------------------------------------------------------
441 void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr)
443 //printf("PARSER_GET_USER::ParseUserParams el=%s attr[1]=%s\n", el, attr[1]);
445 if (strcasecmp(el, "login") == 0)
447 user.login = attr[1];
451 /*if (strcasecmp(el, "LastActivityTime") == 0)
453 if (strtol(attr[1], user.lastActivityTime) < 0)
455 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
461 if (strcasecmp(el, "LastTimeCash") == 0)
463 if (strtol(attr[1], user.lastTimeCash) < 0)
465 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
470 if (strcasecmp(el, "CashExpire") == 0)
472 if (strtol(attr[1], user.cashExpire) < 0)
474 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
480 if (strcasecmp(el, "down") == 0)
482 if (str2x(attr[1], user.down) < 0)
488 if (strcasecmp(el, "passive") == 0)
490 if (str2x(attr[1], user.passive) < 0)
496 if (strcasecmp(el, "disableDetailStat") == 0)
498 if (str2x(attr[1], user.disableDetailStat) < 0)
505 if (strcasecmp(el, "status") == 0)
507 if (str2x(attr[1], user.connected) < 0)
513 if (strcasecmp(el, "aonline") == 0)
515 if (str2x(attr[1], user.alwaysOnline) < 0)
521 if (strcasecmp(el, "currip") == 0)
523 user.ip = inet_addr(attr[1]);
526 if (strcasecmp(el, "creditExpire") == 0)
528 if (str2x(attr[1], user.creditExpire) < 0)
534 for (int i = 0; i < USERDATA_NUM; i++)
538 string udName = "UserData" + num;
539 if (strcasecmp(el, udName.c_str()) == 0)
541 Decode21str(user.userData[i], attr[1]);
546 ParsedStringParams psp[] =
549 {&user.tariff, "tariff"},
550 {&user.password, "password"},
551 {&user.iface, "iface"},
554 for (unsigned i = 0; i < sizeof(psp)/sizeof(ParsedStringParams); i++)
556 if (strcasecmp(el, psp[i].paramName.c_str()) == 0)
558 *psp[i].param = attr[1];
563 ParsedStringParams pspEnc[] =
565 {&user.note, "note"},
566 {&user.email, "email"},
567 {&user.group, "group"},
568 {&user.name, "name"},
569 {&user.address, "address"},
570 {&user.phone, "phone"}
573 for (unsigned i = 0; i < sizeof(pspEnc)/sizeof(ParsedStringParams); i++)
575 if (strcasecmp(el, pspEnc[i].paramName.c_str()) == 0)
577 Decode21str(*pspEnc[i].param, attr[1]);
582 ParsedDoubleParams pdp[] =
584 {&user.cash, "cash"},
585 {&user.credit, "credit"},
586 {&user.lastCash, "lastCash"},
587 {&user.prepaidTraff, "freemb"},
590 for (unsigned i = 0; i < sizeof(pdp)/sizeof(ParsedDoubleParams); i++)
592 if (strcasecmp(el, pdp[i].paramName.c_str()) == 0)
594 strtodouble2(attr[1], *pdp[i].param);
599 if (strcasecmp(el, "traff") == 0)
601 ParseUserLoadStat(el, attr);
605 //-----------------------------------------------------------------------------
606 void PARSER_GET_USER::ParseUserLoadStat(const char *, const char ** attr)
612 for (int j = 0; j < DIR_NUM; j++)
614 sprintf(dir, "MU%d", j);
615 if (strcasecmp(dir, attr[i]) == 0)
617 str2x(attr[i+1], user.stat.mu[j]);
621 for (int j = 0; j < DIR_NUM; j++)
623 sprintf(dir, "MD%d", j);
624 if (strcasecmp(dir, attr[i]) == 0)
626 str2x(attr[i+1], user.stat.md[j]);
630 for (int j = 0; j < DIR_NUM; j++)
632 sprintf(dir, "SU%d", j);
633 if (strcasecmp(dir, attr[i]) == 0)
635 str2x(attr[i+1], user.stat.su[j]);
639 for (int j = 0; j < DIR_NUM; j++)
641 sprintf(dir, "SD%d", j);
642 if (strcasecmp(dir, attr[i]) == 0)
644 str2x(attr[i+1], user.stat.sd[j]);
652 //-----------------------------------------------------------------------------
653 void PARSER_GET_USER::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
658 //-----------------------------------------------------------------------------
659 //-----------------------------------------------------------------------------
660 //-----------------------------------------------------------------------------
661 PARSER_GET_SERVER_INFO::PARSER_GET_SERVER_INFO()
662 : RecvServerInfoDataCb(NULL),
663 serverInfoDataCb(NULL),
667 //-----------------------------------------------------------------------------
668 int PARSER_GET_SERVER_INFO::ParseStart(const char *el, const char **attr)
673 if (strcasecmp(el, "ServerInfo") != 0)
675 //printf("%s\n", el);
682 if (strcasecmp(el, "uname") == 0)
687 if (strcasecmp(el, "version") == 0)
689 ParseServerVersion(attr);
692 if (strcasecmp(el, "tariff") == 0)
694 ParseTariffType(attr);
697 if (strcasecmp(el, "dir_num") == 0)
702 if (strcasecmp(el, "users_num") == 0)
707 if (strcasecmp(el, "tariff_num") == 0)
709 ParseTariffsNum(attr);
713 for (int j = 0; j < DIR_NUM; j++)
716 sprintf(str, "dir_name_%d", j);
717 if (strcasecmp(el, str) == 0)
719 ParseDirName(attr, j);
727 //-----------------------------------------------------------------------------
728 void PARSER_GET_SERVER_INFO::ParseEnd(const char *)
733 RecvServerInfoDataCb(&serverInfo, serverInfoDataCb);
736 //-----------------------------------------------------------------------------
737 /*void PARSER_GET_SERVER_INFO::ParseServerInfo(const char * el, const char ** attr)
740 //-----------------------------------------------------------------------------
741 void PARSER_GET_SERVER_INFO::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)
743 RecvServerInfoDataCb = f;
744 serverInfoDataCb = data;
746 //-----------------------------------------------------------------------------
747 void PARSER_GET_SERVER_INFO::ParseUname(const char ** attr)
749 if (strcmp(*attr, "value") == 0)
750 serverInfo.uname = attr[1];
752 //-----------------------------------------------------------------------------
753 void PARSER_GET_SERVER_INFO::ParseServerVersion(const char ** attr)
755 if (strcmp(*attr, "value") == 0)
756 serverInfo.version = attr[1];
758 //-----------------------------------------------------------------------------
759 void PARSER_GET_SERVER_INFO::ParseUsersNum(const char ** attr)
761 if (strcmp(*attr, "value") == 0)
763 if (str2x(attr[1], serverInfo.usersNum) < 0)
765 serverInfo.usersNum = -1;
770 //-----------------------------------------------------------------------------
771 void PARSER_GET_SERVER_INFO::ParseTariffsNum(const char ** attr)
773 if (strcmp(*attr, "value") == 0)
775 if (str2x(attr[1], serverInfo.tariffNum) < 0)
777 serverInfo.tariffNum = -1;
782 //-----------------------------------------------------------------------------
783 void PARSER_GET_SERVER_INFO::ParseTariffType(const char ** attr)
785 if (strcmp(*attr, "value") == 0)
787 if (str2x(attr[1], serverInfo.tariffType) < 0)
789 serverInfo.tariffType = -1;
794 //-----------------------------------------------------------------------------
795 void PARSER_GET_SERVER_INFO::ParseDirNum(const char **attr)
797 if (strcasecmp(*attr, "value") == 0)
799 if (str2x(attr[1], serverInfo.dirNum) < 0)
801 serverInfo.dirNum = -1;
806 //-----------------------------------------------------------------------------
807 void PARSER_GET_SERVER_INFO::ParseDirName(const char **attr, int d)
809 if (strcmp(attr[0], "value") == 0)
811 char str[2*DIRNAME_LEN + 1];
812 Decode21(str, attr[1]);
813 serverInfo.dirName[d] = str;
816 //-----------------------------------------------------------------------------
817 //-----------------------------------------------------------------------------
818 //-----------------------------------------------------------------------------
819 PARSER_AUTH_BY::PARSER_AUTH_BY()
820 : RecvAuthByDataCb(NULL),
825 //-----------------------------------------------------------------------------
826 int PARSER_AUTH_BY::ParseStart(const char *el, const char **attr)
831 if (strcasecmp(el, "AuthorizedBy") != 0)
833 list.erase(list.begin(), list.end());
834 //printf("%s\n", el);
841 if (strcasecmp(el, "Auth") == 0)
843 if (attr && attr[0] && attr[1])
844 list.push_back(attr[1]);
851 //-----------------------------------------------------------------------------
852 void PARSER_AUTH_BY::ParseEnd(const char *)
857 RecvAuthByDataCb(list, authByDataCb);
860 //-----------------------------------------------------------------------------
861 void PARSER_AUTH_BY::SetRecvCb(RecvAuthByDataCb_t f, void * data)
863 RecvAuthByDataCb = f;
866 //-----------------------------------------------------------------------------
867 //-----------------------------------------------------------------------------
868 //-----------------------------------------------------------------------------
869 PARSER_CHG_USER::PARSER_CHG_USER()
870 : RecvChgUserCb(NULL),
875 //-----------------------------------------------------------------------------
876 int PARSER_CHG_USER::ParseStart(const char *el, const char **attr)
881 if (strcasecmp(el, "SetUser") == 0)
883 ParseAnswer(el, attr);
885 else if (strcasecmp(el, "DelUser") == 0)
887 ParseAnswer(el, attr);
889 else if (strcasecmp(el, "AddUser") == 0)
891 ParseAnswer(el, attr);
896 //-----------------------------------------------------------------------------
897 void PARSER_CHG_USER::ParseEnd(const char *)
901 //-----------------------------------------------------------------------------
902 void PARSER_CHG_USER::ParseAnswer(const char *, const char **attr)
906 RecvChgUserCb(attr[1], chgUserCbData);
909 //-----------------------------------------------------------------------------
910 void PARSER_CHG_USER::SetChgUserRecvCb(RecvChgUserCb_t f, void * data)
913 chgUserCbData = data;
915 //-----------------------------------------------------------------------------
916 //-----------------------------------------------------------------------------
917 //-----------------------------------------------------------------------------
918 PARSER_CHECK_USER::PARSER_CHECK_USER()
919 : RecvCheckUserCb(NULL),
920 checkUserCbData(NULL),
924 //-----------------------------------------------------------------------------
925 int PARSER_CHECK_USER::ParseStart(const char *el, const char **attr)
930 if (strcasecmp(el, "CheckUser") == 0)
932 //printf("el=%s attr[0]=%s attr[1]=%s\n", el, attr[0], attr[1]);
933 ParseAnswer(el, attr);
938 //-----------------------------------------------------------------------------
939 void PARSER_CHECK_USER::ParseEnd(const char *)
943 //-----------------------------------------------------------------------------
944 void PARSER_CHECK_USER::ParseAnswer(const char *, const char **attr)
948 RecvCheckUserCb(attr[1], checkUserCbData);
951 //-----------------------------------------------------------------------------
952 void PARSER_CHECK_USER::SetCheckUserRecvCb(RecvCheckUserCb_t f, void * data)
955 checkUserCbData = data;
957 //-----------------------------------------------------------------------------
958 //-----------------------------------------------------------------------------
959 //-----------------------------------------------------------------------------
960 PARSER_SEND_MESSAGE::PARSER_SEND_MESSAGE()
961 : RecvSendMessageCb(NULL),
962 sendMessageCbData(NULL),
966 //-----------------------------------------------------------------------------
967 int PARSER_SEND_MESSAGE::ParseStart(const char *el, const char **attr)
972 if (strcasecmp(el, "SendMessageResult") == 0)
974 ParseAnswer(el, attr);
979 //-----------------------------------------------------------------------------
980 void PARSER_SEND_MESSAGE::ParseEnd(const char *)
984 //-----------------------------------------------------------------------------
985 void PARSER_SEND_MESSAGE::ParseAnswer(const char *, const char **attr)
987 if (RecvSendMessageCb)
988 RecvSendMessageCb(attr[1], sendMessageCbData);
990 //-----------------------------------------------------------------------------
991 void PARSER_SEND_MESSAGE::SetSendMessageRecvCb(RecvSendMessageCb_t f, void * data)
993 RecvSendMessageCb = f;
994 sendMessageCbData = data;
996 //-----------------------------------------------------------------------------