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>
40 #include "stg/common.h"
41 #include "stg/netunit.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));
190 msg = strchr(str, ':');
200 if (strtodouble2(str, cash) != 0)
202 printf("Incorrect cash value %s\n", c);
203 exit(PARAMETER_PARSING_ERR_CODE);
209 //-----------------------------------------------------------------------------
210 double ParseCredit(const char * c)
213 if (strtodouble2(c, credit) != 0)
215 printf("Incorrect credit value %s\n", c);
216 exit(PARAMETER_PARSING_ERR_CODE);
221 //-----------------------------------------------------------------------------
222 double ParsePrepaidTraffic(const char * c)
225 if (strtodouble2(c, credit) != 0)
227 printf("Incorrect prepaid traffic value %s\n", c);
228 exit(PARAMETER_PARSING_ERR_CODE);
233 //-----------------------------------------------------------------------------
234 int64_t ParseTraff(const char * c)
237 if (str2x(c, traff) != 0)
239 printf("Incorrect credit value %s\n", c);
240 exit(PARAMETER_PARSING_ERR_CODE);
245 //-----------------------------------------------------------------------------
246 bool ParseDownPassive(const char * dp)
248 if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0')))
250 printf("Incorrect value %s\n", dp);
251 exit(PARAMETER_PARSING_ERR_CODE);
256 //-----------------------------------------------------------------------------
257 string ParseTariff(const char * t, int &chgType)
269 if (strlen(s1) >= TARIFF_NAME_LEN)
271 printf("Tariff name too big %s\n", s1);
272 exit(PARAMETER_PARSING_ERR_CODE);
279 printf("Incorrect tariff value %s\n", t);
280 exit(PARAMETER_PARSING_ERR_CODE);
283 s2 = strtok(NULL, ":");
289 chgType = TARIFF_NOW;
296 if (strcmp(s2, "now") == 0)
297 chgType = TARIFF_NOW;
299 if (strcmp(s2, "delayed") == 0)
300 chgType = TARIFF_DEL;
302 if (strcmp(s2, "recalc") == 0)
303 chgType = TARIFF_REC;
307 printf("Incorrect tariff value %s\n", t);
308 exit(PARAMETER_PARSING_ERR_CODE);
315 //-----------------------------------------------------------------------------
316 void ParseAnyString(const char * c, string * msg)
319 char * ob = new char[strlen(c) + 1];
320 char * ib = new char[strlen(c) + 1];
327 setlocale(LC_ALL, "");
330 strncpy(charsetF, nl_langinfo(CODESET), 255);
332 char * charsetT = "koi8-ru";
336 size_t insize = strlen(ib);
337 size_t outsize = strlen(ib);
341 cd = iconv_open(charsetT, charsetF);
342 if (cd == (iconv_t) -1)
346 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
351 printf("error iconv_open\n");
353 exit(ICONV_ERR_CODE);
356 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
357 //printf("nconv=%d outsize=%d\n", nconv, outsize);
358 if (nconv == (size_t) -1)
362 printf("iconv error\n");
363 exit(ICONV_ERR_CODE);
375 //-----------------------------------------------------------------------------
376 void CreateRequestSet(REQUEST * req, char * r)
378 const int strLen = 10024;
380 memset(str, 0, strLen);
384 if (!req->usrMsg.res_empty())
386 int len = req->usrMsg.const_data().length() * 2 + 1;
387 char * msg = new char[len];
389 Encode12(msg, req->usrMsg.const_data().c_str(), req->usrMsg.const_data().length());
391 sprintf(str, "<Message login=\"%s\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"%s\"/>", req->login.const_data().c_str(), msg);
392 //sprintf(str, "<message login=\"%s\" priority=\"0\" text=\"%s\"/>\n", req->login, msg);
401 sprintf(str, "<DelUser login=\"%s\"/>", req->login.const_data().c_str());
409 sprintf(str, "<AddUser> <login value=\"%s\"/> </AddUser>", req->login.const_data().c_str());
415 strcat(r, "<SetUser>\n");
416 sprintf(str, "<login value=\"%s\"/>\n", req->login.const_data().c_str());
418 if (!req->credit.res_empty())
420 sprintf(str, "<credit value=\"%f\"/>\n", req->credit.const_data());
424 if (!req->prepaidTraff.res_empty())
426 sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
430 if (!req->cash.res_empty())
432 int len = req->message.length() * 2 + 1;
433 char * msg = new char[len];
436 Encode12(msg, req->message.c_str(), req->message.length());
437 sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg);
442 if (!req->setCash.res_empty())
444 int len = req->message.length() * 2 + 1;
445 char * msg = new char[len];
447 Encode12(msg, req->message.c_str(), req->message.length());
448 sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg);
453 if (!req->usrPasswd.res_empty())
455 sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
459 if (!req->down.res_empty())
461 sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
465 if (!req->passive.res_empty())
467 sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
471 int uPresent = false;
472 int dPresent = false;
473 for (int i = 0; i < DIR_NUM; i++)
475 if (!req->u[i].res_empty())
477 if (!uPresent && !dPresent)
479 sprintf(str, "<traff ");
485 ss << req->u[i].const_data();
486 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
487 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
490 if (!req->d[i].res_empty())
492 if (!uPresent && !dPresent)
494 sprintf(str, "<traff ");
500 ss << req->d[i].const_data();
501 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
505 if (uPresent || dPresent)
512 if (!req->tariff.res_empty())
514 switch (req->chgTariff)
517 sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
521 sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
525 sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
532 if (!req->note.res_empty())
534 int len = req->note.const_data().length() * 2 + 1;
535 char * note = new char[len];
536 memset(note, 0, len);
538 Encode12(note, req->note.const_data().c_str(), req->note.const_data().length());
540 sprintf(str, "<note value=\"%s\"/>", note);
545 if (!req->name.res_empty())
547 int len = req->note.const_data().length() * 2 + 1;
548 char * name = new char[len];
549 memset(name, 0, len);
551 Encode12(name, req->name.const_data().c_str(), req->name.const_data().length());
553 sprintf(str, "<name value=\"%s\"/>", name);
558 if (!req->address.res_empty())
560 int len = req->note.const_data().length() * 2 + 1;
561 char * address = new char[len];
562 memset(address, 0, len);
564 Encode12(address, req->address.const_data().c_str(), req->address.const_data().length());
566 sprintf(str, "<address value=\"%s\"/>", address);
571 if (!req->email.res_empty())
573 int len = req->note.const_data().length() * 2 + 1;
574 char * email = new char[len];
575 memset(email, 0, len);
577 Encode12(email, req->email.const_data().c_str(), req->email.const_data().length());
579 sprintf(str, "<email value=\"%s\"/>", email);
584 if (!req->phone.res_empty())
586 int len = req->note.const_data().length() * 2 + 1;
587 char * phone = new char[len];
588 memset(phone, 0, len);
590 Encode12(phone, req->phone.const_data().c_str(), req->phone.const_data().length());
592 sprintf(str, "<phone value=\"%s\"/>", phone);
597 if (!req->group.res_empty())
599 int len = req->note.const_data().length() * 2 + 1;
600 char * group = new char[len];
601 memset(group, 0, len);
603 Encode12(group, req->group.const_data().c_str(), req->group.const_data().length());
605 sprintf(str, "<group value=\"%s\"/>", group);
610 for (int i = 0; i < USERDATA_NUM; i++)
612 if (!req->ud[i].res_empty())
614 int len = req->ud[i].const_data().length() * 2 + 1;
615 char * ud = new char[len];
618 Encode12(ud, req->ud[i].const_data().c_str(), req->ud[i].const_data().length());
620 sprintf(str, "<userdata%d value=\"%s\"/>", i, ud);
626 strcat(r, "</SetUser>\n");
628 //-----------------------------------------------------------------------------
629 int CheckParameters(REQUEST * req)
634 int a = !req->admLogin.res_empty()
635 && !req->admPasswd.res_empty()
636 && !req->server.res_empty()
637 && !req->port.res_empty()
638 && !req->login.res_empty();
640 int b = !req->cash.res_empty()
641 || !req->setCash.res_empty()
642 || !req->credit.res_empty()
643 || !req->prepaidTraff.res_empty()
644 || !req->tariff.res_empty()
645 || !req->usrMsg.res_empty()
646 || !req->usrPasswd.res_empty()
648 || !req->note.res_empty()
649 || !req->name.res_empty()
650 || !req->address.res_empty()
651 || !req->email.res_empty()
652 || !req->phone.res_empty()
653 || !req->group.res_empty();
655 for (int i = 0; i < DIR_NUM; i++)
657 if (req->u[i].res_empty())
664 for (int i = 0; i < DIR_NUM; i++)
666 if (req->d[i].res_empty())
673 for (int i = 0; i < DIR_NUM; i++)
675 if (req->ud[i].res_empty())
683 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
684 return a && (b || u || d || ud);
686 //-----------------------------------------------------------------------------
687 int CheckParametersGet(REQUEST * req)
689 return CheckParameters(req);
691 //-----------------------------------------------------------------------------
692 int CheckParametersSet(REQUEST * req)
694 return CheckParameters(req);
696 //-----------------------------------------------------------------------------
697 int mainGet(int argc, char **argv)
701 RESETABLE<string> t1;
703 char * short_options_get = "s:p:a:w:u:crtmodieNADLPG";
704 int option_index = -1;
709 c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
720 req.port = ParseServerPort(optarg);
725 req.admLogin = ParseAdminLogin(optarg);
728 case 'w': //admin password
729 req.admPasswd = ParsePassword(optarg);
732 case 'o': //change user password
733 req.usrPasswd = ParsePassword(optarg);
737 req.login = ParseUser(optarg);
760 case 'e': //Prepaid Traffic
761 req.prepaidTraff = 1;
798 //printf("U%d\n", c - 500);
812 //printf("D%d\n", c - 600);
826 //printf("UD%d\n", c - 700);
827 req.ud[c - 700] = " ";
831 //printf ("Unknown option \n");
835 printf ("?? getopt returned character code 0%o ??\n", c);
841 printf ("non-option ARGV-elements: ");
842 while (optind < argc)
843 printf ("%s ", argv[optind++]);
845 exit(PARAMETER_PARSING_ERR_CODE);
848 if (CheckParametersGet(&req) == 0)
850 //printf("Parameter needed\n");
852 exit(PARAMETER_PARSING_ERR_CODE);
856 const int rLen = 20000;
858 memset(rstr, 0, rLen);
860 CreateRequestGet(&req, rstr);
861 Process(req.server, req.port, req.admLogin, req.admPasswd, rstr, ParseReplyGet);
865 //-----------------------------------------------------------------------------
866 int mainSet(int argc, char **argv)
873 RESETABLE<string> t1;
875 char * short_options_set = "s:p:a:w:u:c:r:t:m:o:d:i:e:v:nlN:A:D:L:P:G:";
879 int option_index = -1;
881 c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
892 req.port = ParseServerPort(optarg);
897 req.admLogin = ParseAdminLogin(optarg);
900 case 'w': //admin password
901 req.admPasswd = ParsePassword(optarg);
904 case 'o': //change user password
905 req.usrPasswd = ParsePassword(optarg);
909 req.login = ParseUser(optarg);
913 req.cash = ParseCash(optarg, &req.message);
917 req.setCash = ParseCash(optarg, &req.message);
921 req.credit = ParseCredit(optarg);
925 req.down = ParseDownPassive(optarg);
929 req.passive = ParseDownPassive(optarg);
933 req.tariff = ParseTariff(optarg, req.chgTariff);
937 //ParseMessage(optarg, &req.usrMsg);
941 case 'e': //Prepaid Traffic
942 req.prepaidTraff = ParsePrepaidTraffic(optarg);
945 case 'n': //Create User
946 req.createUser = true;
949 case 'l': //Delete User
950 req.deleteUser = true;
954 ParseAnyString(optarg, &str);
959 ParseAnyString(optarg, &str);
964 ParseAnyString(optarg, &str);
969 ParseAnyString(optarg, &str);
974 ParseAnyString(optarg, &str);
979 ParseAnyString(optarg, &str);
993 //printf("U%d\n", c - 500);
994 req.u[c - 500] = ParseTraff(optarg);
1007 //printf("D%d\n", c - 600);
1008 req.d[c - 600] = ParseTraff(optarg);
1011 case 700: //UserData
1021 ParseAnyString(optarg, &str);
1022 //printf("UD%d\n", c - 700);
1023 req.ud[c - 700] = str;
1027 //printf ("Unknown option \n");
1031 printf ("?? getopt returned character code 0%o ??\n", c);
1037 printf ("non-option ARGV-elements: ");
1038 while (optind < argc)
1039 printf ("%s ", argv[optind++]);
1041 exit(PARAMETER_PARSING_ERR_CODE);
1044 if (CheckParametersSet(&req) == 0)
1046 //printf("Parameter needed\n");
1048 exit(PARAMETER_PARSING_ERR_CODE);
1051 const int rLen = 20000;
1053 memset(rstr, 0, rLen);
1055 CreateRequestGet(&req, rstr);
1056 Process(req.server, req.port, req.admLogin, req.admPasswd, rstr, ParseReplySet);
1061 //-----------------------------------------------------------------------------
1062 int main(int argc, char **argv)
1067 exit(PARAMETER_PARSING_ERR_CODE);
1070 if (strcmp(argv[1], "get"))
1072 return mainGet(argc - 1, argv + 1);
1074 else if (strcmp(argv[1], "set"))
1076 return mainGet(argc - 1, argv + 1);
1081 exit(PARAMETER_PARSING_ERR_CODE);
1083 return UNKNOWN_ERR_CODE;
1085 //-----------------------------------------------------------------------------