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 "stg_const.h"
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 PARSER_GET_USERS::PARSER_GET_USERS()
46 : 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, "creditExpire") == 0)
170 if (str2x(attr[1], user.creditExpire) < 0)
176 /*if (strcasecmp(el, "freemb") == 0)
178 if (strtodouble2(attr[1], user.freeMb) < 0)
180 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
185 if (strcasecmp(el, "down") == 0)
187 if (str2x(attr[1], user.down) < 0)
193 if (strcasecmp(el, "passive") == 0)
195 if (str2x(attr[1], user.passive) < 0)
201 if (strcasecmp(el, "disableDetailStat") == 0)
203 if (str2x(attr[1], user.disableDetailStat) < 0)
210 if (strcasecmp(el, "status") == 0)
212 if (str2x(attr[1], user.connected) < 0)
218 if (strcasecmp(el, "aonline") == 0)
220 if (str2x(attr[1], user.alwaysOnline) < 0)
226 if (strcasecmp(el, "currip") == 0)
228 user.ip = inet_addr(attr[1]);
231 if (strcasecmp(el, "ip") == 0)
237 if (strcasecmp(el, "tariff") == 0)
239 //KOIToWin(user.tariff, *(attr+1), TARIFF_LEN);
240 user.tariff = attr[1];
244 if (strcasecmp(el, "password") == 0)
246 user.password = *(attr+1);
250 if (strcasecmp(el, "iface") == 0)
252 user.iface = attr[1];
256 /*if (strcasecmp(el, "name") == 0)
258 / *char nameEnc[REALNM_LEN * 2 + 1];
259 char name[REALNM_LEN];
260 strncpy(nameEnc, attr[1], REALNM_LEN * 2 + 1);
261 Decode21(name, nameEnc);
262 KOIToWin(user.realName, name, REALNM_LEN);* /
263 Decode21str(user.realName, attr[1]);
267 if (strcasecmp(el, "address") == 0)
269 /*char addressEnc[ADDR_LEN * 2 + 1];
270 char address[ADDR_LEN];
271 strncpy(addressEnc, attr[1], ADDR_LEN * 2 + 1);
272 Decode21(address, addressEnc);
273 KOIToWin(user.address, address, ADDR_LEN);*/
274 Decode21str(user.address, attr[1]);
278 if (strcasecmp(el, "phone") == 0)
280 /*char phoneEnc[PHONE_LEN * 2 + 1];
281 char phone[PHONE_LEN];
282 strncpy(phoneEnc, attr[1], PHONE_LEN * 2 + 1);
283 Decode21(phone, phoneEnc);
284 KOIToWin(user.phone, phone, PHONE_LEN);*/
285 Decode21str(user.phone, attr[1]);
289 if (strcasecmp(el, "note") == 0)
291 /*char noteEnc[NOTE_LEN * 2 + 1];
293 strncpy(noteEnc, attr[1], NOTE_LEN * 2 + 1);*/
294 //KOIToWin(user.note, note, NOTE_LEN);
296 Decode21str(user.note, attr[1]);
300 if (strcasecmp(el, "email") == 0)
302 /*char emailEnc[EMAIL_LEN * 2 + 1];
303 char email[EMAIL_LEN];
304 strncpy(emailEnc, attr[1], EMAIL_LEN * 2 + 1);
305 Decode21(email, emailEnc);
306 //KOIToWin(user.email, email, EMAIL_LEN);
307 user.email = email;*/
308 Decode21str(user.email, attr[1]);
312 if (strcasecmp(el, "group") == 0)
314 /*char groupEnc[GROUP_LEN * 2 + 1];
315 char group[GROUP_LEN];
316 strncpy(groupEnc, attr[1], GROUP_LEN * 2 + 1);
317 Decode21(group, groupEnc);
318 //KOIToWin(user.group, group, GROUP_LEN);
319 user.group = group;*/
320 Decode21str(user.group, attr[1]);
324 if (strcasecmp(el, "traff") == 0)
326 ParseUserLoadStat(el, attr);
331 //-----------------------------------------------------------------------------
332 void PARSER_GET_USERS::ParseUserLoadStat(const char *, const char ** attr)
338 for (int j = 0; j < DIR_NUM; j++)
340 sprintf(dir, "MU%d", j);
341 if (strcasecmp(dir, attr[i]) == 0)
343 str2x(attr[i+1], user.stat.mu[j]);
347 for (int j = 0; j < DIR_NUM; j++)
349 sprintf(dir, "MD%d", j);
350 if (strcasecmp(dir, attr[i]) == 0)
352 str2x(attr[i+1], user.stat.md[j]);
356 for (int j = 0; j < DIR_NUM; j++)
358 sprintf(dir, "SU%d", j);
359 if (strcasecmp(dir, attr[i]) == 0)
361 str2x(attr[i+1], user.stat.su[j]);
365 for (int j = 0; j < DIR_NUM; j++)
367 sprintf(dir, "SD%d", j);
368 if (strcasecmp(dir, attr[i]) == 0)
370 str2x(attr[i+1], user.stat.sd[j]);
378 //-----------------------------------------------------------------------------
379 void PARSER_GET_USERS::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
384 //-----------------------------------------------------------------------------
385 //-----------------------------------------------------------------------------
386 //-----------------------------------------------------------------------------
387 PARSER_GET_USER::PARSER_GET_USER()
388 : RecvUserDataCb(NULL),
395 //-----------------------------------------------------------------------------
396 int PARSER_GET_USER::ParseStart(const char *el, const char **attr)
406 ParseUserParams(el, attr);
410 //-----------------------------------------------------------------------------
411 void PARSER_GET_USER::ParseEnd(const char *)
418 RecvUserDataCb(&user, userDataCb);
422 //-----------------------------------------------------------------------------
423 void PARSER_GET_USER::ParseUser(const char * el, const char ** attr)
425 if (strcasecmp(el, "user") == 0)
427 if (strcasecmp(attr[1], "error") == 0)
432 //-----------------------------------------------------------------------------
433 struct ParsedStringParams
438 //-----------------------------------------------------------------------------
439 struct ParsedDoubleParams
444 //-----------------------------------------------------------------------------
445 void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr)
447 //printf("PARSER_GET_USER::ParseUserParams el=%s attr[1]=%s\n", el, attr[1]);
449 if (strcasecmp(el, "login") == 0)
451 user.login = attr[1];
455 /*if (strcasecmp(el, "LastActivityTime") == 0)
457 if (strtol(attr[1], user.lastActivityTime) < 0)
459 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
465 if (strcasecmp(el, "LastTimeCash") == 0)
467 if (strtol(attr[1], user.lastTimeCash) < 0)
469 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
474 if (strcasecmp(el, "CashExpire") == 0)
476 if (strtol(attr[1], user.cashExpire) < 0)
478 MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0);
484 if (strcasecmp(el, "down") == 0)
486 if (str2x(attr[1], user.down) < 0)
492 if (strcasecmp(el, "passive") == 0)
494 if (str2x(attr[1], user.passive) < 0)
500 if (strcasecmp(el, "disableDetailStat") == 0)
502 if (str2x(attr[1], user.disableDetailStat) < 0)
509 if (strcasecmp(el, "status") == 0)
511 if (str2x(attr[1], user.connected) < 0)
517 if (strcasecmp(el, "aonline") == 0)
519 if (str2x(attr[1], user.alwaysOnline) < 0)
525 if (strcasecmp(el, "currip") == 0)
527 user.ip = inet_addr(attr[1]);
530 if (strcasecmp(el, "creditExpire") == 0)
532 if (str2x(attr[1], user.creditExpire) < 0)
538 for (int i = 0; i < USERDATA_NUM; i++)
542 string udName = "UserData" + num;
543 if (strcasecmp(el, udName.c_str()) == 0)
545 Decode21str(user.userData[i], attr[1]);
550 ParsedStringParams psp[] =
553 {&user.tariff, "tariff"},
554 {&user.password, "password"},
555 {&user.iface, "iface"},
558 for (unsigned i = 0; i < sizeof(psp)/sizeof(ParsedStringParams); i++)
560 if (strcasecmp(el, psp[i].paramName.c_str()) == 0)
562 *psp[i].param = attr[1];
567 ParsedStringParams pspEnc[] =
569 {&user.note, "note"},
570 {&user.email, "email"},
571 {&user.group, "group"},
572 {&user.name, "name"},
573 {&user.address, "address"},
574 {&user.phone, "phone"}
577 for (unsigned i = 0; i < sizeof(pspEnc)/sizeof(ParsedStringParams); i++)
579 if (strcasecmp(el, pspEnc[i].paramName.c_str()) == 0)
581 Decode21str(*pspEnc[i].param, attr[1]);
586 ParsedDoubleParams pdp[] =
588 {&user.cash, "cash"},
589 {&user.credit, "credit"},
590 {&user.lastCash, "lastCash"},
591 {&user.prepaidTraff, "freemb"},
594 for (unsigned i = 0; i < sizeof(pdp)/sizeof(ParsedDoubleParams); i++)
596 if (strcasecmp(el, pdp[i].paramName.c_str()) == 0)
598 strtodouble2(attr[1], *pdp[i].param);
603 if (strcasecmp(el, "traff") == 0)
605 ParseUserLoadStat(el, attr);
609 //-----------------------------------------------------------------------------
610 void PARSER_GET_USER::ParseUserLoadStat(const char *, const char ** attr)
616 for (int j = 0; j < DIR_NUM; j++)
618 sprintf(dir, "MU%d", j);
619 if (strcasecmp(dir, attr[i]) == 0)
621 str2x(attr[i+1], user.stat.mu[j]);
625 for (int j = 0; j < DIR_NUM; j++)
627 sprintf(dir, "MD%d", j);
628 if (strcasecmp(dir, attr[i]) == 0)
630 str2x(attr[i+1], user.stat.md[j]);
634 for (int j = 0; j < DIR_NUM; j++)
636 sprintf(dir, "SU%d", j);
637 if (strcasecmp(dir, attr[i]) == 0)
639 str2x(attr[i+1], user.stat.su[j]);
643 for (int j = 0; j < DIR_NUM; j++)
645 sprintf(dir, "SD%d", j);
646 if (strcasecmp(dir, attr[i]) == 0)
648 str2x(attr[i+1], user.stat.sd[j]);
656 //-----------------------------------------------------------------------------
657 void PARSER_GET_USER::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
662 //-----------------------------------------------------------------------------
663 //-----------------------------------------------------------------------------
664 //-----------------------------------------------------------------------------
665 PARSER_GET_SERVER_INFO::PARSER_GET_SERVER_INFO()
666 : RecvServerInfoDataCb(NULL),
667 serverInfoDataCb(NULL),
673 //-----------------------------------------------------------------------------
674 int PARSER_GET_SERVER_INFO::ParseStart(const char *el, const char **attr)
679 if (strcasecmp(el, "ServerInfo") != 0)
681 //printf("%s\n", el);
688 if (strcasecmp(el, "uname") == 0)
693 if (strcasecmp(el, "version") == 0)
695 ParseServerVersion(attr);
698 if (strcasecmp(el, "tariff") == 0)
700 ParseTariffType(attr);
703 if (strcasecmp(el, "dir_num") == 0)
708 if (strcasecmp(el, "users_num") == 0)
713 if (strcasecmp(el, "tariff_num") == 0)
715 ParseTariffsNum(attr);
719 for (int j = 0; j < DIR_NUM; j++)
722 sprintf(str, "dir_name_%d", j);
723 if (strcasecmp(el, str) == 0)
725 ParseDirName(attr, j);
733 //-----------------------------------------------------------------------------
734 void PARSER_GET_SERVER_INFO::ParseEnd(const char *)
739 RecvServerInfoDataCb(&serverInfo, serverInfoDataCb);
742 //-----------------------------------------------------------------------------
743 /*void PARSER_GET_SERVER_INFO::ParseServerInfo(const char * el, const char ** attr)
746 //-----------------------------------------------------------------------------
747 void PARSER_GET_SERVER_INFO::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)
749 RecvServerInfoDataCb = f;
750 serverInfoDataCb = data;
752 //-----------------------------------------------------------------------------
753 void PARSER_GET_SERVER_INFO::ParseUname(const char ** attr)
755 if (strcmp(*attr, "value") == 0)
756 serverInfo.uname = attr[1];
758 //-----------------------------------------------------------------------------
759 void PARSER_GET_SERVER_INFO::ParseServerVersion(const char ** attr)
761 if (strcmp(*attr, "value") == 0)
762 serverInfo.version = attr[1];
764 //-----------------------------------------------------------------------------
765 void PARSER_GET_SERVER_INFO::ParseUsersNum(const char ** attr)
767 if (strcmp(*attr, "value") == 0)
769 if (str2x(attr[1], serverInfo.usersNum) < 0)
771 serverInfo.usersNum = -1;
776 //-----------------------------------------------------------------------------
777 void PARSER_GET_SERVER_INFO::ParseTariffsNum(const char ** attr)
779 if (strcmp(*attr, "value") == 0)
781 if (str2x(attr[1], serverInfo.tariffNum) < 0)
783 serverInfo.tariffNum = -1;
788 //-----------------------------------------------------------------------------
789 void PARSER_GET_SERVER_INFO::ParseTariffType(const char ** attr)
791 if (strcmp(*attr, "value") == 0)
793 if (str2x(attr[1], serverInfo.tariffType) < 0)
795 serverInfo.tariffType = -1;
800 //-----------------------------------------------------------------------------
801 void PARSER_GET_SERVER_INFO::ParseDirNum(const char **attr)
803 if (strcasecmp(*attr, "value") == 0)
805 if (str2x(attr[1], serverInfo.dirNum) < 0)
807 serverInfo.dirNum = -1;
812 //-----------------------------------------------------------------------------
813 void PARSER_GET_SERVER_INFO::ParseDirName(const char **attr, int d)
815 if (strcmp(attr[0], "value") == 0)
817 char str[2*DIRNAME_LEN + 1];
818 Decode21(str, attr[1]);
819 serverInfo.dirName[d] = str;
822 //-----------------------------------------------------------------------------
823 //-----------------------------------------------------------------------------
824 //-----------------------------------------------------------------------------
825 PARSER_CHG_USER::PARSER_CHG_USER()
826 : RecvChgUserCb(NULL),
832 //-----------------------------------------------------------------------------
833 int PARSER_CHG_USER::ParseStart(const char *el, const char **attr)
838 if (strcasecmp(el, "SetUser") == 0)
840 ParseAnswer(el, attr);
842 else if (strcasecmp(el, "DelUser") == 0)
844 ParseAnswer(el, attr);
846 else if (strcasecmp(el, "AddUser") == 0)
848 ParseAnswer(el, attr);
853 //-----------------------------------------------------------------------------
854 void PARSER_CHG_USER::ParseEnd(const char *)
858 //-----------------------------------------------------------------------------
859 void PARSER_CHG_USER::ParseAnswer(const char *, const char **attr)
863 RecvChgUserCb(attr[1], chgUserCbData);
866 //-----------------------------------------------------------------------------
867 void PARSER_CHG_USER::SetChgUserRecvCb(RecvChgUserCb_t f, void * data)
870 chgUserCbData = data;
872 //-----------------------------------------------------------------------------
873 //-----------------------------------------------------------------------------
874 //-----------------------------------------------------------------------------
875 PARSER_CHECK_USER::PARSER_CHECK_USER()
876 : RecvCheckUserCb(NULL),
877 checkUserCbData(NULL),
882 //-----------------------------------------------------------------------------
883 int PARSER_CHECK_USER::ParseStart(const char *el, const char **attr)
888 if (strcasecmp(el, "CheckUser") == 0)
890 //printf("el=%s attr[0]=%s attr[1]=%s\n", el, attr[0], attr[1]);
891 ParseAnswer(el, attr);
896 //-----------------------------------------------------------------------------
897 void PARSER_CHECK_USER::ParseEnd(const char *)
901 //-----------------------------------------------------------------------------
902 void PARSER_CHECK_USER::ParseAnswer(const char *, const char **attr)
906 RecvCheckUserCb(attr[1], checkUserCbData);
909 //-----------------------------------------------------------------------------
910 void PARSER_CHECK_USER::SetCheckUserRecvCb(RecvCheckUserCb_t f, void * data)
913 checkUserCbData = data;
915 //-----------------------------------------------------------------------------
916 //-----------------------------------------------------------------------------
917 //-----------------------------------------------------------------------------
918 PARSER_SEND_MESSAGE::PARSER_SEND_MESSAGE()
919 : RecvSendMessageCb(NULL),
920 sendMessageCbData(NULL),
925 //-----------------------------------------------------------------------------
926 int PARSER_SEND_MESSAGE::ParseStart(const char *el, const char **attr)
931 if (strcasecmp(el, "SendMessageResult") == 0)
933 ParseAnswer(el, attr);
938 //-----------------------------------------------------------------------------
939 void PARSER_SEND_MESSAGE::ParseEnd(const char *)
943 //-----------------------------------------------------------------------------
944 void PARSER_SEND_MESSAGE::ParseAnswer(const char *, const char **attr)
946 if (RecvSendMessageCb)
947 RecvSendMessageCb(attr[1], sendMessageCbData);
949 //-----------------------------------------------------------------------------
950 void PARSER_SEND_MESSAGE::SetSendMessageRecvCb(RecvSendMessageCb_t f, void * data)
952 RecvSendMessageCb = f;
953 sendMessageCbData = data;
955 //-----------------------------------------------------------------------------