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>
37 //#include "srvconf_common.h"
38 #include "stg_const.h"
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 PARSER_GET_USERS::PARSER_GET_USERS()
47 : RecvUserDataCb(NULL),
53 //-----------------------------------------------------------------------------
54 int PARSER_GET_USERS::ParseStart(const char *el, const char **attr)
69 ParseUserParams(el, attr);
73 //-----------------------------------------------------------------------------
74 void PARSER_GET_USERS::ParseEnd(const char *)
81 RecvUserDataCb(&user, userDataCb);
85 //-----------------------------------------------------------------------------
86 void PARSER_GET_USERS::ParseUsers(const char * el, const char ** attr)
88 if (strcasecmp(el, "users") == 0)
95 //-----------------------------------------------------------------------------
96 void PARSER_GET_USERS::ParseUser(const char * el, const char ** attr)
100 if (strcasecmp(el, "user") != 0)
105 if (strcasecmp(attr[0], "login") != 0)
109 user.login = attr[1];
112 //-----------------------------------------------------------------------------
113 void PARSER_GET_USERS::ParseUserParams(const char * el, const char ** attr)
115 if (strcasecmp(el, "cash") == 0)
117 if (strtodouble2(attr[1], user.cash) < 0)
123 /*if (strcasecmp(el, "LastCash") == 0)
125 if (strtodouble2(attr[1], user.lastCash) < 0)
127 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
132 /*if (strcasecmp(el, "LastActivityTime") == 0)
134 if (strtol(attr[1], user.lastActivityTime) < 0)
136 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
142 /*if (strcasecmp(el, "LastTimeCash") == 0)
144 if (strtol(attr[1], user.lastTimeCash) < 0)
146 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
151 /*if (strcasecmp(el, "CashExpire") == 0)
153 if (strtol(attr[1], user.cashExpire) < 0)
155 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
160 if (strcasecmp(el, "credit") == 0)
162 if (strtodouble2(attr[1], user.credit) < 0)
168 /*if (strcasecmp(el, "freemb") == 0)
170 if (strtodouble2(attr[1], user.freeMb) < 0)
172 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
177 if (strcasecmp(el, "down") == 0)
179 if (str2x(attr[1], user.down) < 0)
185 if (strcasecmp(el, "passive") == 0)
187 if (str2x(attr[1], user.passive) < 0)
193 if (strcasecmp(el, "disableDetailStat") == 0)
195 if (str2x(attr[1], user.disableDetailStat) < 0)
202 if (strcasecmp(el, "status") == 0)
204 if (str2x(attr[1], user.connected) < 0)
210 if (strcasecmp(el, "aonline") == 0)
212 if (str2x(attr[1], user.alwaysOnline) < 0)
218 if (strcasecmp(el, "currip") == 0)
220 user.ip = inet_addr(attr[1]);
223 if (strcasecmp(el, "ip") == 0)
229 if (strcasecmp(el, "tariff") == 0)
231 //KOIToWin(user.tariff, *(attr+1), TARIFF_LEN);
232 user.tariff = attr[1];
236 if (strcasecmp(el, "password") == 0)
238 user.password = *(attr+1);
242 if (strcasecmp(el, "iface") == 0)
244 user.iface = attr[1];
248 /*if (strcasecmp(el, "name") == 0)
250 / *char nameEnc[REALNM_LEN * 2 + 1];
251 char name[REALNM_LEN];
252 strncpy(nameEnc, attr[1], REALNM_LEN * 2 + 1);
253 Decode21(name, nameEnc);
254 KOIToWin(user.realName, name, REALNM_LEN);* /
255 Decode21str(user.realName, attr[1]);
259 if (strcasecmp(el, "address") == 0)
261 /*char addressEnc[ADDR_LEN * 2 + 1];
262 char address[ADDR_LEN];
263 strncpy(addressEnc, attr[1], ADDR_LEN * 2 + 1);
264 Decode21(address, addressEnc);
265 KOIToWin(user.address, address, ADDR_LEN);*/
266 Decode21str(user.address, attr[1]);
270 if (strcasecmp(el, "phone") == 0)
272 /*char phoneEnc[PHONE_LEN * 2 + 1];
273 char phone[PHONE_LEN];
274 strncpy(phoneEnc, attr[1], PHONE_LEN * 2 + 1);
275 Decode21(phone, phoneEnc);
276 KOIToWin(user.phone, phone, PHONE_LEN);*/
277 Decode21str(user.phone, attr[1]);
281 if (strcasecmp(el, "note") == 0)
283 /*char noteEnc[NOTE_LEN * 2 + 1];
285 strncpy(noteEnc, attr[1], NOTE_LEN * 2 + 1);*/
286 //KOIToWin(user.note, note, NOTE_LEN);
288 Decode21str(user.note, attr[1]);
292 if (strcasecmp(el, "email") == 0)
294 /*char emailEnc[EMAIL_LEN * 2 + 1];
295 char email[EMAIL_LEN];
296 strncpy(emailEnc, attr[1], EMAIL_LEN * 2 + 1);
297 Decode21(email, emailEnc);
298 //KOIToWin(user.email, email, EMAIL_LEN);
299 user.email = email;*/
300 Decode21str(user.email, attr[1]);
304 if (strcasecmp(el, "group") == 0)
306 /*char groupEnc[GROUP_LEN * 2 + 1];
307 char group[GROUP_LEN];
308 strncpy(groupEnc, attr[1], GROUP_LEN * 2 + 1);
309 Decode21(group, groupEnc);
310 //KOIToWin(user.group, group, GROUP_LEN);
311 user.group = group;*/
312 Decode21str(user.group, attr[1]);
316 if (strcasecmp(el, "traff") == 0)
318 ParseUserLoadStat(el, attr);
323 //-----------------------------------------------------------------------------
324 void PARSER_GET_USERS::ParseUserLoadStat(const char *, const char ** attr)
330 for (int j = 0; j < DIR_NUM; j++)
332 sprintf(dir, "MU%d", j);
333 if (strcasecmp(dir, attr[i]) == 0)
335 str2x(attr[i+1], user.stat.mu[j]);
339 for (int j = 0; j < DIR_NUM; j++)
341 sprintf(dir, "MD%d", j);
342 if (strcasecmp(dir, attr[i]) == 0)
344 str2x(attr[i+1], user.stat.md[j]);
348 for (int j = 0; j < DIR_NUM; j++)
350 sprintf(dir, "SU%d", j);
351 if (strcasecmp(dir, attr[i]) == 0)
353 str2x(attr[i+1], user.stat.su[j]);
357 for (int j = 0; j < DIR_NUM; j++)
359 sprintf(dir, "SD%d", j);
360 if (strcasecmp(dir, attr[i]) == 0)
362 str2x(attr[i+1], user.stat.sd[j]);
370 //-----------------------------------------------------------------------------
371 void PARSER_GET_USERS::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
376 //-----------------------------------------------------------------------------
377 //-----------------------------------------------------------------------------
378 //-----------------------------------------------------------------------------
379 PARSER_GET_USER::PARSER_GET_USER()
380 : RecvUserDataCb(NULL),
386 //-----------------------------------------------------------------------------
387 int PARSER_GET_USER::ParseStart(const char *el, const char **attr)
397 ParseUserParams(el, attr);
401 //-----------------------------------------------------------------------------
402 void PARSER_GET_USER::ParseEnd(const char *)
409 RecvUserDataCb(&user, userDataCb);
413 //-----------------------------------------------------------------------------
414 void PARSER_GET_USER::ParseUser(const char * el, const char ** attr)
416 if (strcasecmp(el, "user") == 0)
418 if (strcasecmp(attr[1], "error") == 0)
423 //-----------------------------------------------------------------------------
424 struct ParsedStringParams
429 //-----------------------------------------------------------------------------
430 struct ParsedDoubleParams
435 //-----------------------------------------------------------------------------
436 void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr)
438 //printf("PARSER_GET_USER::ParseUserParams el=%s attr[1]=%s\n", el, attr[1]);
440 if (strcasecmp(el, "login") == 0)
442 user.login = attr[1];
446 /*if (strcasecmp(el, "LastActivityTime") == 0)
448 if (strtol(attr[1], user.lastActivityTime) < 0)
450 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
456 if (strcasecmp(el, "LastTimeCash") == 0)
458 if (strtol(attr[1], user.lastTimeCash) < 0)
460 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
465 if (strcasecmp(el, "CashExpire") == 0)
467 if (strtol(attr[1], user.cashExpire) < 0)
469 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
475 if (strcasecmp(el, "down") == 0)
477 if (str2x(attr[1], user.down) < 0)
483 if (strcasecmp(el, "passive") == 0)
485 if (str2x(attr[1], user.passive) < 0)
491 if (strcasecmp(el, "disableDetailStat") == 0)
493 if (str2x(attr[1], user.disableDetailStat) < 0)
500 if (strcasecmp(el, "status") == 0)
502 if (str2x(attr[1], user.connected) < 0)
508 if (strcasecmp(el, "aonline") == 0)
510 if (str2x(attr[1], user.alwaysOnline) < 0)
516 if (strcasecmp(el, "currip") == 0)
518 user.ip = inet_addr(attr[1]);
521 for (int i = 0; i < USERDATA_NUM; i++)
525 string udName = "UserData" + num;
526 if (strcasecmp(el, udName.c_str()) == 0)
528 Decode21str(user.userData[i], attr[1]);
533 ParsedStringParams psp[] =
536 {&user.tariff, "tariff"},
537 {&user.password, "password"},
538 {&user.iface, "iface"},
541 for (unsigned i = 0; i < sizeof(psp)/sizeof(ParsedStringParams); i++)
543 if (strcasecmp(el, psp[i].paramName.c_str()) == 0)
545 *psp[i].param = attr[1];
550 ParsedStringParams pspEnc[] =
552 {&user.note, "note"},
553 {&user.email, "email"},
554 {&user.group, "group"},
555 {&user.name, "name"},
556 {&user.address, "address"},
557 {&user.phone, "phone"}
560 for (unsigned i = 0; i < sizeof(pspEnc)/sizeof(ParsedStringParams); i++)
562 if (strcasecmp(el, pspEnc[i].paramName.c_str()) == 0)
564 Decode21str(*pspEnc[i].param, attr[1]);
569 ParsedDoubleParams pdp[] =
571 {&user.cash, "cash"},
572 {&user.credit, "credit"},
573 {&user.lastCash, "lastCash"},
574 {&user.prepaidTraff, "freemb"},
577 for (unsigned i = 0; i < sizeof(pdp)/sizeof(ParsedDoubleParams); i++)
579 if (strcasecmp(el, pdp[i].paramName.c_str()) == 0)
581 strtodouble2(attr[1], *pdp[i].param);
586 if (strcasecmp(el, "traff") == 0)
588 ParseUserLoadStat(el, attr);
592 //-----------------------------------------------------------------------------
593 void PARSER_GET_USER::ParseUserLoadStat(const char *, const char ** attr)
599 for (int j = 0; j < DIR_NUM; j++)
601 sprintf(dir, "MU%d", j);
602 if (strcasecmp(dir, attr[i]) == 0)
604 str2x(attr[i+1], user.stat.mu[j]);
608 for (int j = 0; j < DIR_NUM; j++)
610 sprintf(dir, "MD%d", j);
611 if (strcasecmp(dir, attr[i]) == 0)
613 str2x(attr[i+1], user.stat.md[j]);
617 for (int j = 0; j < DIR_NUM; j++)
619 sprintf(dir, "SU%d", j);
620 if (strcasecmp(dir, attr[i]) == 0)
622 str2x(attr[i+1], user.stat.su[j]);
626 for (int j = 0; j < DIR_NUM; j++)
628 sprintf(dir, "SD%d", j);
629 if (strcasecmp(dir, attr[i]) == 0)
631 str2x(attr[i+1], user.stat.sd[j]);
639 //-----------------------------------------------------------------------------
640 void PARSER_GET_USER::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
645 //-----------------------------------------------------------------------------
646 //-----------------------------------------------------------------------------
647 //-----------------------------------------------------------------------------
648 PARSER_GET_SERVER_INFO::PARSER_GET_SERVER_INFO()
649 : RecvServerInfoDataCb(NULL),
650 serverInfoDataCb(NULL),
655 //-----------------------------------------------------------------------------
656 int PARSER_GET_SERVER_INFO::ParseStart(const char *el, const char **attr)
661 if (strcasecmp(el, "ServerInfo") != 0)
663 //printf("%s\n", el);
670 if (strcasecmp(el, "uname") == 0)
675 if (strcasecmp(el, "version") == 0)
677 ParseServerVersion(attr);
680 if (strcasecmp(el, "tariff") == 0)
682 ParseTariffType(attr);
685 if (strcasecmp(el, "dir_num") == 0)
690 if (strcasecmp(el, "users_num") == 0)
695 if (strcasecmp(el, "tariff_num") == 0)
697 ParseTariffsNum(attr);
701 for (int j = 0; j < DIR_NUM; j++)
704 sprintf(str, "dir_name_%d", j);
705 if (strcasecmp(el, str) == 0)
707 ParseDirName(attr, j);
715 //-----------------------------------------------------------------------------
716 void PARSER_GET_SERVER_INFO::ParseEnd(const char *)
721 RecvServerInfoDataCb(&serverInfo, serverInfoDataCb);
724 //-----------------------------------------------------------------------------
725 /*void PARSER_GET_SERVER_INFO::ParseServerInfo(const char * el, const char ** attr)
728 //-----------------------------------------------------------------------------
729 void PARSER_GET_SERVER_INFO::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)
731 RecvServerInfoDataCb = f;
732 serverInfoDataCb = data;
734 //-----------------------------------------------------------------------------
735 void PARSER_GET_SERVER_INFO::ParseUname(const char ** attr)
737 if (strcmp(*attr, "value") == 0)
738 serverInfo.uname = attr[1];
740 //-----------------------------------------------------------------------------
741 void PARSER_GET_SERVER_INFO::ParseServerVersion(const char ** attr)
743 if (strcmp(*attr, "value") == 0)
744 serverInfo.version = attr[1];
746 //-----------------------------------------------------------------------------
747 void PARSER_GET_SERVER_INFO::ParseUsersNum(const char ** attr)
749 if (strcmp(*attr, "value") == 0)
751 if (str2x(attr[1], serverInfo.usersNum) < 0)
753 serverInfo.usersNum = -1;
758 //-----------------------------------------------------------------------------
759 void PARSER_GET_SERVER_INFO::ParseTariffsNum(const char ** attr)
761 if (strcmp(*attr, "value") == 0)
763 if (str2x(attr[1], serverInfo.tariffNum) < 0)
765 serverInfo.tariffNum = -1;
770 //-----------------------------------------------------------------------------
771 void PARSER_GET_SERVER_INFO::ParseTariffType(const char ** attr)
773 if (strcmp(*attr, "value") == 0)
775 if (str2x(attr[1], serverInfo.tariffType) < 0)
777 serverInfo.tariffType = -1;
782 //-----------------------------------------------------------------------------
783 void PARSER_GET_SERVER_INFO::ParseDirNum(const char **attr)
785 if (strcasecmp(*attr, "value") == 0)
787 if (str2x(attr[1], serverInfo.dirNum) < 0)
789 serverInfo.dirNum = -1;
794 //-----------------------------------------------------------------------------
795 void PARSER_GET_SERVER_INFO::ParseDirName(const char **attr, int d)
797 if (strcmp(attr[0], "value") == 0)
799 char str[2*DIRNAME_LEN + 1];
800 Decode21(str, attr[1]);
801 serverInfo.dirName[d] = str;
804 //-----------------------------------------------------------------------------
805 //-----------------------------------------------------------------------------
806 //-----------------------------------------------------------------------------
807 PARSER_CHG_USER::PARSER_CHG_USER()
808 : RecvChgUserCb(NULL),
814 //-----------------------------------------------------------------------------
815 int PARSER_CHG_USER::ParseStart(const char *el, const char **attr)
820 if (strcasecmp(el, "SetUser") == 0)
822 ParseAnswer(el, attr);
824 else if (strcasecmp(el, "DelUser") == 0)
826 ParseAnswer(el, attr);
828 else if (strcasecmp(el, "AddUser") == 0)
830 ParseAnswer(el, attr);
835 //-----------------------------------------------------------------------------
836 void PARSER_CHG_USER::ParseEnd(const char *)
840 //-----------------------------------------------------------------------------
841 void PARSER_CHG_USER::ParseAnswer(const char *, const char **attr)
845 RecvChgUserCb(attr[1], chgUserCbData);
848 //-----------------------------------------------------------------------------
849 void PARSER_CHG_USER::SetChgUserRecvCb(RecvChgUserCb_t f, void * data)
852 chgUserCbData = data;
854 //-----------------------------------------------------------------------------
855 //-----------------------------------------------------------------------------
856 //-----------------------------------------------------------------------------
857 PARSER_CHECK_USER::PARSER_CHECK_USER()
858 : RecvCheckUserCb(NULL),
859 checkUserCbData(NULL),
864 //-----------------------------------------------------------------------------
865 int PARSER_CHECK_USER::ParseStart(const char *el, const char **attr)
870 if (strcasecmp(el, "CheckUser") == 0)
872 //printf("el=%s attr[0]=%s attr[1]=%s\n", el, attr[0], attr[1]);
873 ParseAnswer(el, attr);
878 //-----------------------------------------------------------------------------
879 void PARSER_CHECK_USER::ParseEnd(const char *)
883 //-----------------------------------------------------------------------------
884 void PARSER_CHECK_USER::ParseAnswer(const char *, const char **attr)
888 RecvCheckUserCb(attr[1], checkUserCbData);
891 //-----------------------------------------------------------------------------
892 void PARSER_CHECK_USER::SetCheckUserRecvCb(RecvCheckUserCb_t f, void * data)
895 checkUserCbData = data;
897 //-----------------------------------------------------------------------------
898 //-----------------------------------------------------------------------------
899 //-----------------------------------------------------------------------------
900 PARSER_SEND_MESSAGE::PARSER_SEND_MESSAGE()
901 : RecvSendMessageCb(NULL),
902 sendMessageCbData(NULL),
907 //-----------------------------------------------------------------------------
908 int PARSER_SEND_MESSAGE::ParseStart(const char *el, const char **attr)
913 if (strcasecmp(el, "SendMessageResult") == 0)
915 ParseAnswer(el, attr);
920 //-----------------------------------------------------------------------------
921 void PARSER_SEND_MESSAGE::ParseEnd(const char *)
925 //-----------------------------------------------------------------------------
926 void PARSER_SEND_MESSAGE::ParseAnswer(const char *, const char **attr)
928 if (RecvSendMessageCb)
929 RecvSendMessageCb(attr[1], sendMessageCbData);
931 //-----------------------------------------------------------------------------
932 void PARSER_SEND_MESSAGE::SetSendMessageRecvCb(RecvSendMessageCb_t f, void * data)
934 RecvSendMessageCb = f;
935 sendMessageCbData = data;
937 //-----------------------------------------------------------------------------