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>
24 $Date: 2009/06/22 15:57:49 $
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
43 #include "common_sg.h"
44 #include "sg_error_codes.h"
50 int ParseReplyGet(void * data, list<string> * ans);
51 int ParseReplySet(void * data, list<string> * ans);
53 struct option long_options_get[] = {
54 {"server", 1, 0, 's'}, //Server
55 {"port", 1, 0, 'p'}, //Port
56 {"admin", 1, 0, 'a'}, //Admin
57 {"admin_pass", 1, 0, 'w'}, //passWord
58 {"user", 1, 0, 'u'}, //User
59 {"addcash", 0, 0, 'c'}, //Add Cash
60 //{"setcash", 0, 0, 'v'}, //Set Cash
61 {"credit", 0, 0, 'r'}, //cRedit
62 {"tariff", 0, 0, 't'}, //Tariff
63 {"message", 0, 0, 'm'}, //message
64 {"password", 0, 0, 'o'}, //password
65 {"down", 0, 0, 'd'}, //down
66 {"passive", 0, 0, 'i'}, //passive
67 {"u0", 0, 0, 500}, //U0
68 {"u1", 0, 0, 501}, //U1
69 {"u2", 0, 0, 502}, //U2
70 {"u3", 0, 0, 503}, //U3
71 {"u4", 0, 0, 504}, //U4
72 {"u5", 0, 0, 505}, //U5
73 {"u6", 0, 0, 506}, //U6
74 {"u7", 0, 0, 507}, //U7
75 {"u8", 0, 0, 508}, //U8
76 {"u9", 0, 0, 509}, //U9
77 {"d0", 0, 0, 600}, //D0
78 {"d1", 0, 0, 601}, //D1
79 {"d2", 0, 0, 602}, //D2
80 {"d3", 0, 0, 603}, //D3
81 {"d4", 0, 0, 604}, //D4
82 {"d5", 0, 0, 605}, //D5
83 {"d6", 0, 0, 606}, //D6
84 {"d7", 0, 0, 607}, //D7
85 {"d8", 0, 0, 608}, //D8
86 {"d9", 0, 0, 609}, //D9
88 {"ud0", 0, 0, 700}, //UserData0
89 {"ud1", 0, 0, 701}, //UserData1
90 {"ud2", 0, 0, 702}, //UserData2
91 {"ud3", 0, 0, 703}, //UserData3
92 {"ud4", 0, 0, 704}, //UserData4
93 {"ud5", 0, 0, 705}, //UserData5
94 {"ud6", 0, 0, 706}, //UserData6
95 {"ud7", 0, 0, 707}, //UserData7
96 {"ud8", 0, 0, 708}, //UserData8
97 {"ud9", 0, 0, 709}, //UserData9
99 {"prepaid", 0, 0, 'e'}, //prepaid traff
100 {"create", 0, 0, 'n'}, //create
101 {"delete", 0, 0, 'l'}, //delete
103 {"note", 0, 0, 'N'}, //Note
104 {"name", 0, 0, 'A'}, //nAme
105 {"address", 0, 0, 'D'}, //aDdress
106 {"email", 0, 0, 'L'}, //emaiL
107 {"phone", 0, 0, 'P'}, //phone
108 {"group", 0, 0, 'G'}, //Group
112 struct option long_options_set[] = {
113 {"server", 1, 0, 's'}, //Server
114 {"port", 1, 0, 'p'}, //Port
115 {"admin", 1, 0, 'a'}, //Admin
116 {"admin_pass", 1, 0, 'w'}, //passWord
117 {"user", 1, 0, 'u'}, //User
118 {"addcash", 1, 0, 'c'}, //Add Cash
119 {"setcash", 1, 0, 'v'}, //Set Cash
120 {"credit", 1, 0, 'r'}, //cRedit
121 {"tariff", 1, 0, 't'}, //Tariff
122 {"message", 1, 0, 'm'}, //message
123 {"password", 1, 0, 'o'}, //password
124 {"down", 1, 0, 'd'}, //down
125 {"passive", 1, 0, 'i'}, //passive
126 {"u0", 1, 0, 500}, //U0
127 {"u1", 1, 0, 501}, //U1
128 {"u2", 1, 0, 502}, //U2
129 {"u3", 1, 0, 503}, //U3
130 {"u4", 1, 0, 504}, //U4
131 {"u5", 1, 0, 505}, //U5
132 {"u6", 1, 0, 506}, //U6
133 {"u7", 1, 0, 507}, //U7
134 {"u8", 1, 0, 508}, //U8
135 {"u9", 1, 0, 509}, //U9
136 {"d0", 1, 0, 600}, //D0
137 {"d1", 1, 0, 601}, //D1
138 {"d2", 1, 0, 602}, //D2
139 {"d3", 1, 0, 603}, //D3
140 {"d4", 1, 0, 604}, //D4
141 {"d5", 1, 0, 605}, //D5
142 {"d6", 1, 0, 606}, //D6
143 {"d7", 1, 0, 607}, //D7
144 {"d8", 1, 0, 608}, //D8
145 {"d9", 1, 0, 609}, //D9
147 {"ud0", 1, 0, 700}, //UserData
148 {"ud1", 1, 0, 701}, //UserData1
149 {"ud2", 1, 0, 702}, //UserData2
150 {"ud3", 1, 0, 703}, //UserData3
151 {"ud4", 1, 0, 704}, //UserData4
152 {"ud5", 1, 0, 705}, //UserData5
153 {"ud6", 1, 0, 706}, //UserData6
154 {"ud7", 1, 0, 707}, //UserData7
155 {"ud8", 1, 0, 708}, //UserData8
156 {"ud9", 1, 0, 709}, //UserData9
158 {"prepaid", 1, 0, 'e'}, //prepaid traff
159 {"create", 1, 0, 'n'}, //create
160 {"delete", 1, 0, 'l'}, //delete
162 {"note", 1, 0, 'N'}, //Note
163 {"name", 1, 0, 'A'}, //nAme
164 {"address", 1, 0, 'D'}, //aDdress
165 {"email", 1, 0, 'L'}, //emaiL
166 {"phone", 1, 0, 'P'}, //phone
167 {"group", 1, 0, 'G'}, //Group
171 //-----------------------------------------------------------------------------
172 void CreateRequestGet(REQUEST * req, char * r)
175 r1 = "<GetUser login=\"" + req->login.const_data() + "\"/>\n";
176 strcpy(r, r1.c_str());
178 //-----------------------------------------------------------------------------
179 double ParseCash(const char * c, string * message)
181 //-c 123.45:log message
185 str = new char[strlen(c) + 1];
187 strncpy(str, c, strlen(c));
188 msg = strchr(str, ':');
198 if (strtodouble2(str, cash) != 0)
200 printf("Incorrect cash value %s\n", c);
201 exit(PARAMETER_PARSING_ERR_CODE);
207 //-----------------------------------------------------------------------------
208 double ParseCredit(const char * c)
211 if (strtodouble2(c, credit) != 0)
213 printf("Incorrect credit value %s\n", c);
214 exit(PARAMETER_PARSING_ERR_CODE);
219 //-----------------------------------------------------------------------------
220 double ParsePrepaidTraffic(const char * c)
223 if (strtodouble2(c, credit) != 0)
225 printf("Incorrect prepaid traffic value %s\n", c);
226 exit(PARAMETER_PARSING_ERR_CODE);
231 //-----------------------------------------------------------------------------
232 int64_t ParseTraff(const char * c)
235 if (str2x(c, traff) != 0)
237 printf("Incorrect credit value %s\n", c);
238 exit(PARAMETER_PARSING_ERR_CODE);
243 //-----------------------------------------------------------------------------
244 bool ParseDownPassive(const char * dp)
246 if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0')))
248 printf("Incorrect value %s\n", dp);
249 exit(PARAMETER_PARSING_ERR_CODE);
254 //-----------------------------------------------------------------------------
255 string ParseTariff(const char * t, int &chgType)
267 if (strlen(s1) >= TARIFF_NAME_LEN)
269 printf("Tariff name too big %s\n", s1);
270 exit(PARAMETER_PARSING_ERR_CODE);
277 printf("Incorrect tariff value %s\n", t);
278 exit(PARAMETER_PARSING_ERR_CODE);
281 s2 = strtok(NULL, ":");
287 chgType = TARIFF_NOW;
294 if (strcmp(s2, "now") == 0)
295 chgType = TARIFF_NOW;
297 if (strcmp(s2, "delayed") == 0)
298 chgType = TARIFF_DEL;
300 if (strcmp(s2, "recalc") == 0)
301 chgType = TARIFF_REC;
305 printf("Incorrect tariff value %s\n", t);
306 exit(PARAMETER_PARSING_ERR_CODE);
313 //-----------------------------------------------------------------------------
314 void ParseAnyString(const char * c, string * msg)
317 char * ob = new char[strlen(c) + 1];
318 char * ib = new char[strlen(c) + 1];
325 setlocale(LC_ALL, "");
328 strncpy(charsetF, nl_langinfo(CODESET), 255);
330 char * charsetT = "koi8-r";
334 size_t insize = strlen(ib);
335 size_t outsize = strlen(ib);
339 cd = iconv_open(charsetT, charsetF);
340 if (cd == (iconv_t) -1)
344 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
349 printf("error iconv_open\n");
351 exit(ICONV_ERR_CODE);
354 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
355 //printf("nconv=%d outsize=%d\n", nconv, outsize);
356 if (nconv == (size_t) -1)
360 printf("iconv error\n");
361 exit(ICONV_ERR_CODE);
373 //-----------------------------------------------------------------------------
374 void CreateRequestSet(REQUEST * req, char * r)
376 const int strLen = 10024;
378 memset(str, 0, strLen);
382 if (!req->usrMsg.res_empty())
384 int len = req->usrMsg.const_data().length() * 2 + 1;
385 char * msg = new char[len];
387 Encode12(msg, req->usrMsg.const_data().c_str(), req->usrMsg.const_data().length());
389 sprintf(str, "<Message login=\"%s\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"%s\"/>", req->login.const_data().c_str(), msg);
390 //sprintf(str, "<message login=\"%s\" priority=\"0\" text=\"%s\"/>\n", req->login, msg);
399 sprintf(str, "<DelUser login=\"%s\"/>", req->login.const_data().c_str());
407 sprintf(str, "<AddUser> <login value=\"%s\"/> </AddUser>", req->login.const_data().c_str());
413 strcat(r, "<SetUser>\n");
414 sprintf(str, "<login value=\"%s\"/>\n", req->login.const_data().c_str());
416 if (!req->credit.res_empty())
418 sprintf(str, "<credit value=\"%f\"/>\n", req->credit.const_data());
422 if (!req->prepaidTraff.res_empty())
424 sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
428 if (!req->cash.res_empty())
430 int len = req->message.length() * 2 + 1;
431 char * msg = new char[len];
434 Encode12(msg, req->message.c_str(), req->message.length());
435 sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg);
440 if (!req->setCash.res_empty())
442 int len = req->message.length() * 2 + 1;
443 char * msg = new char[len];
445 Encode12(msg, req->message.c_str(), req->message.length());
446 sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg);
451 if (!req->usrPasswd.res_empty())
453 sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
457 if (!req->down.res_empty())
459 sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
463 if (!req->passive.res_empty())
465 sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
469 int uPresent = false;
470 int dPresent = false;
471 for (int i = 0; i < DIR_NUM; i++)
473 if (!req->u[i].res_empty())
475 if (!uPresent && !dPresent)
477 sprintf(str, "<traff ");
483 ss << req->u[i].const_data();
484 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
485 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
488 if (!req->d[i].res_empty())
490 if (!uPresent && !dPresent)
492 sprintf(str, "<traff ");
498 ss << req->d[i].const_data();
499 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
503 if (uPresent || dPresent)
510 if (!req->tariff.res_empty())
512 switch (req->chgTariff)
515 sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
519 sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
523 sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
530 if (!req->note.res_empty())
532 int len = req->note.const_data().length() * 2 + 1;
533 char * note = new char[len];
534 memset(note, 0, len);
536 Encode12(note, req->note.const_data().c_str(), req->note.const_data().length());
538 sprintf(str, "<note value=\"%s\"/>", note);
543 if (!req->name.res_empty())
545 int len = req->note.const_data().length() * 2 + 1;
546 char * name = new char[len];
547 memset(name, 0, len);
549 Encode12(name, req->name.const_data().c_str(), req->name.const_data().length());
551 sprintf(str, "<name value=\"%s\"/>", name);
556 if (!req->address.res_empty())
558 int len = req->note.const_data().length() * 2 + 1;
559 char * address = new char[len];
560 memset(address, 0, len);
562 Encode12(address, req->address.const_data().c_str(), req->address.const_data().length());
564 sprintf(str, "<address value=\"%s\"/>", address);
569 if (!req->email.res_empty())
571 int len = req->note.const_data().length() * 2 + 1;
572 char * email = new char[len];
573 memset(email, 0, len);
575 Encode12(email, req->email.const_data().c_str(), req->email.const_data().length());
577 sprintf(str, "<email value=\"%s\"/>", email);
582 if (!req->phone.res_empty())
584 int len = req->note.const_data().length() * 2 + 1;
585 char * phone = new char[len];
586 memset(phone, 0, len);
588 Encode12(phone, req->phone.const_data().c_str(), req->phone.const_data().length());
590 sprintf(str, "<phone value=\"%s\"/>", phone);
595 if (!req->group.res_empty())
597 int len = req->note.const_data().length() * 2 + 1;
598 char * group = new char[len];
599 memset(group, 0, len);
601 Encode12(group, req->group.const_data().c_str(), req->group.const_data().length());
603 sprintf(str, "<group value=\"%s\"/>", group);
608 for (int i = 0; i < USERDATA_NUM; i++)
610 if (!req->ud[i].res_empty())
612 int len = req->ud[i].const_data().length() * 2 + 1;
613 char * ud = new char[len];
616 Encode12(ud, req->ud[i].const_data().c_str(), req->ud[i].const_data().length());
618 sprintf(str, "<userdata%d value=\"%s\"/>", i, ud);
624 strcat(r, "</SetUser>\n");
626 //-----------------------------------------------------------------------------
627 int CheckParameters(REQUEST * req)
632 int a = !req->admLogin.res_empty()
633 && !req->admPasswd.res_empty()
634 && !req->server.res_empty()
635 && !req->port.res_empty()
636 && !req->login.res_empty();
638 int b = !req->cash.res_empty()
639 || !req->setCash.res_empty()
640 || !req->credit.res_empty()
641 || !req->prepaidTraff.res_empty()
642 || !req->tariff.res_empty()
643 || !req->usrMsg.res_empty()
644 || !req->usrPasswd.res_empty()
646 || !req->note.res_empty()
647 || !req->name.res_empty()
648 || !req->address.res_empty()
649 || !req->email.res_empty()
650 || !req->phone.res_empty()
651 || !req->group.res_empty();
653 for (int i = 0; i < DIR_NUM; i++)
655 if (req->u[i].res_empty())
662 for (int i = 0; i < DIR_NUM; i++)
664 if (req->d[i].res_empty())
671 for (int i = 0; i < DIR_NUM; i++)
673 if (req->ud[i].res_empty())
681 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
682 return a && (b || u || d || ud);
684 //-----------------------------------------------------------------------------
685 int CheckParametersGet(REQUEST * req)
687 return CheckParameters(req);
689 //-----------------------------------------------------------------------------
690 int CheckParametersSet(REQUEST * req)
692 return CheckParameters(req);
694 //-----------------------------------------------------------------------------
695 int mainGet(int argc, char **argv)
699 RESETABLE<string> t1;
701 char * short_options_get = "s:p:a:w:u:crtmodieNADLPG";
702 int option_index = -1;
707 c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
718 req.port = ParseServerPort(optarg);
723 req.admLogin = ParseAdminLogin(optarg);
726 case 'w': //admin password
727 req.admPasswd = ParsePassword(optarg);
730 case 'o': //change user password
731 req.usrPasswd = ParsePassword(optarg);
735 req.login = ParseUser(optarg);
758 case 'e': //Prepaid Traffic
759 req.prepaidTraff = 1;
796 //printf("U%d\n", c - 500);
810 //printf("D%d\n", c - 600);
824 //printf("UD%d\n", c - 700);
825 req.ud[c - 700] = " ";
829 //printf ("Unknown option \n");
833 printf ("?? getopt returned character code 0%o ??\n", c);
839 printf ("non-option ARGV-elements: ");
840 while (optind < argc)
841 printf ("%s ", argv[optind++]);
843 exit(PARAMETER_PARSING_ERR_CODE);
846 if (CheckParametersGet(&req) == 0)
848 //printf("Parameter needed\n");
850 exit(PARAMETER_PARSING_ERR_CODE);
854 const int rLen = 20000;
856 memset(rstr, 0, rLen);
858 CreateRequestGet(&req, rstr);
859 Process(req.server, req.port, req.admLogin, req.admPasswd, rstr, ParseReplyGet);
863 //-----------------------------------------------------------------------------
864 int mainSet(int argc, char **argv)
871 RESETABLE<string> t1;
873 char * short_options_set = "s:p:a:w:u:c:r:t:m:o:d:i:e:v:nlN:A:D:L:P:G:";
877 int option_index = -1;
879 c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
890 req.port = ParseServerPort(optarg);
895 req.admLogin = ParseAdminLogin(optarg);
898 case 'w': //admin password
899 req.admPasswd = ParsePassword(optarg);
902 case 'o': //change user password
903 req.usrPasswd = ParsePassword(optarg);
907 req.login = ParseUser(optarg);
911 req.cash = ParseCash(optarg, &req.message);
915 req.setCash = ParseCash(optarg, &req.message);
919 req.credit = ParseCredit(optarg);
923 req.down = ParseDownPassive(optarg);
927 req.passive = ParseDownPassive(optarg);
931 req.tariff = ParseTariff(optarg, req.chgTariff);
935 //ParseMessage(optarg, &req.usrMsg);
939 case 'e': //Prepaid Traffic
940 req.prepaidTraff = ParsePrepaidTraffic(optarg);
943 case 'n': //Create User
944 req.createUser = true;
947 case 'l': //Delete User
948 req.deleteUser = true;
952 ParseAnyString(optarg, &str);
957 ParseAnyString(optarg, &str);
962 ParseAnyString(optarg, &str);
967 ParseAnyString(optarg, &str);
972 ParseAnyString(optarg, &str);
977 ParseAnyString(optarg, &str);
991 //printf("U%d\n", c - 500);
992 req.u[c - 500] = ParseTraff(optarg);
1005 //printf("D%d\n", c - 600);
1006 req.d[c - 600] = ParseTraff(optarg);
1009 case 700: //UserData
1019 ParseAnyString(optarg, &str);
1020 //printf("UD%d\n", c - 700);
1021 req.ud[c - 700] = str;
1025 //printf ("Unknown option \n");
1029 printf ("?? getopt returned character code 0%o ??\n", c);
1035 printf ("non-option ARGV-elements: ");
1036 while (optind < argc)
1037 printf ("%s ", argv[optind++]);
1039 exit(PARAMETER_PARSING_ERR_CODE);
1042 if (CheckParametersSet(&req) == 0)
1044 //printf("Parameter needed\n");
1046 exit(PARAMETER_PARSING_ERR_CODE);
1049 const int rLen = 20000;
1051 memset(rstr, 0, rLen);
1053 CreateRequestGet(&req, rstr);
1054 Process(req.server, req.port, req.admLogin, req.admPasswd, rstr, ParseReplySet);
1059 //-----------------------------------------------------------------------------
1060 int main(int argc, char **argv)
1065 exit(PARAMETER_PARSING_ERR_CODE);
1068 if (strcmp(argv[1], "get"))
1070 return mainGet(argc - 1, argv + 1);
1072 else if (strcmp(argv[1], "set"))
1074 return mainGet(argc - 1, argv + 1);
1079 exit(PARAMETER_PARSING_ERR_CODE);
1081 return UNKNOWN_ERR_CODE;
1083 //-----------------------------------------------------------------------------