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: 2010/03/25 14:37:43 $
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 {"disable-stat",0, 0, 'S'}, //disable detail stat
68 {"always-online",0, 0, 'O'}, //always online
69 {"u0", 0, 0, 500}, //U0
70 {"u1", 0, 0, 501}, //U1
71 {"u2", 0, 0, 502}, //U2
72 {"u3", 0, 0, 503}, //U3
73 {"u4", 0, 0, 504}, //U4
74 {"u5", 0, 0, 505}, //U5
75 {"u6", 0, 0, 506}, //U6
76 {"u7", 0, 0, 507}, //U7
77 {"u8", 0, 0, 508}, //U8
78 {"u9", 0, 0, 509}, //U9
79 {"d0", 0, 0, 600}, //D0
80 {"d1", 0, 0, 601}, //D1
81 {"d2", 0, 0, 602}, //D2
82 {"d3", 0, 0, 603}, //D3
83 {"d4", 0, 0, 604}, //D4
84 {"d5", 0, 0, 605}, //D5
85 {"d6", 0, 0, 606}, //D6
86 {"d7", 0, 0, 607}, //D7
87 {"d8", 0, 0, 608}, //D8
88 {"d9", 0, 0, 609}, //D9
90 {"ud0", 0, 0, 700}, //UserData0
91 {"ud1", 0, 0, 701}, //UserData1
92 {"ud2", 0, 0, 702}, //UserData2
93 {"ud3", 0, 0, 703}, //UserData3
94 {"ud4", 0, 0, 704}, //UserData4
95 {"ud5", 0, 0, 705}, //UserData5
96 {"ud6", 0, 0, 706}, //UserData6
97 {"ud7", 0, 0, 707}, //UserData7
98 {"ud8", 0, 0, 708}, //UserData8
99 {"ud9", 0, 0, 709}, //UserData9
101 {"prepaid", 0, 0, 'e'}, //prepaid traff
102 {"create", 0, 0, 'n'}, //create
103 {"delete", 0, 0, 'l'}, //delete
105 {"note", 0, 0, 'N'}, //Note
106 {"name", 0, 0, 'A'}, //nAme
107 {"address", 0, 0, 'D'}, //aDdress
108 {"email", 0, 0, 'L'}, //emaiL
109 {"phone", 0, 0, 'P'}, //phone
110 {"group", 0, 0, 'G'}, //Group
111 {"ip", 0, 0, 'I'}, //IP-address of user
115 struct option long_options_set[] = {
116 {"server", 1, 0, 's'}, //Server
117 {"port", 1, 0, 'p'}, //Port
118 {"admin", 1, 0, 'a'}, //Admin
119 {"admin_pass", 1, 0, 'w'}, //passWord
120 {"user", 1, 0, 'u'}, //User
121 {"addcash", 1, 0, 'c'}, //Add Cash
122 {"setcash", 1, 0, 'v'}, //Set Cash
123 {"credit", 1, 0, 'r'}, //cRedit
124 {"tariff", 1, 0, 't'}, //Tariff
125 {"message", 1, 0, 'm'}, //message
126 {"password", 1, 0, 'o'}, //password
127 {"down", 1, 0, 'd'}, //down
128 {"passive", 1, 0, 'i'}, //passive
129 {"disable-stat",1, 0, 'S'}, //disable detail stat
130 {"always-online",1, 0, 'O'}, //always online
131 {"u0", 1, 0, 500}, //U0
132 {"u1", 1, 0, 501}, //U1
133 {"u2", 1, 0, 502}, //U2
134 {"u3", 1, 0, 503}, //U3
135 {"u4", 1, 0, 504}, //U4
136 {"u5", 1, 0, 505}, //U5
137 {"u6", 1, 0, 506}, //U6
138 {"u7", 1, 0, 507}, //U7
139 {"u8", 1, 0, 508}, //U8
140 {"u9", 1, 0, 509}, //U9
141 {"d0", 1, 0, 600}, //D0
142 {"d1", 1, 0, 601}, //D1
143 {"d2", 1, 0, 602}, //D2
144 {"d3", 1, 0, 603}, //D3
145 {"d4", 1, 0, 604}, //D4
146 {"d5", 1, 0, 605}, //D5
147 {"d6", 1, 0, 606}, //D6
148 {"d7", 1, 0, 607}, //D7
149 {"d8", 1, 0, 608}, //D8
150 {"d9", 1, 0, 609}, //D9
152 {"ud0", 1, 0, 700}, //UserData
153 {"ud1", 1, 0, 701}, //UserData1
154 {"ud2", 1, 0, 702}, //UserData2
155 {"ud3", 1, 0, 703}, //UserData3
156 {"ud4", 1, 0, 704}, //UserData4
157 {"ud5", 1, 0, 705}, //UserData5
158 {"ud6", 1, 0, 706}, //UserData6
159 {"ud7", 1, 0, 707}, //UserData7
160 {"ud8", 1, 0, 708}, //UserData8
161 {"ud9", 1, 0, 709}, //UserData9
163 {"prepaid", 1, 0, 'e'}, //prepaid traff
164 {"create", 1, 0, 'n'}, //create
165 {"delete", 1, 0, 'l'}, //delete
167 {"note", 1, 0, 'N'}, //Note
168 {"name", 1, 0, 'A'}, //nAme
169 {"address", 1, 0, 'D'}, //aDdress
170 {"email", 1, 0, 'L'}, //emaiL
171 {"phone", 1, 0, 'P'}, //phone
172 {"group", 1, 0, 'G'}, //Group
173 {"ip", 0, 0, 'I'}, //IP-address of user
177 //-----------------------------------------------------------------------------
178 double ParseCash(const char * c, string * message)
180 //-c 123.45:log message
184 str = new char[strlen(c) + 1];
186 strncpy(str, c, strlen(c));
189 msg = strchr(str, ':');
199 if (strtodouble2(str, cash) != 0)
201 printf("Incorrect cash value %s\n", c);
202 exit(PARAMETER_PARSING_ERR_CODE);
208 //-----------------------------------------------------------------------------
209 double ParseCredit(const char * c)
212 if (strtodouble2(c, credit) != 0)
214 printf("Incorrect credit value %s\n", c);
215 exit(PARAMETER_PARSING_ERR_CODE);
220 //-----------------------------------------------------------------------------
221 double ParsePrepaidTraffic(const char * c)
224 if (strtodouble2(c, credit) != 0)
226 printf("Incorrect prepaid traffic value %s\n", c);
227 exit(PARAMETER_PARSING_ERR_CODE);
232 //-----------------------------------------------------------------------------
233 int64_t ParseTraff(const char * c)
236 if (str2x(c, traff) != 0)
238 printf("Incorrect credit value %s\n", c);
239 exit(PARAMETER_PARSING_ERR_CODE);
244 //-----------------------------------------------------------------------------
245 bool ParseDownPassive(const char * dp)
247 if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0')))
249 printf("Incorrect value %s\n", dp);
250 exit(PARAMETER_PARSING_ERR_CODE);
255 //-----------------------------------------------------------------------------
256 string ParseTariff(const char * t, int &chgType)
268 if (strlen(s1) >= TARIFF_NAME_LEN)
270 printf("Tariff name too big %s\n", s1);
271 exit(PARAMETER_PARSING_ERR_CODE);
278 printf("Incorrect tariff value %s\n", t);
279 exit(PARAMETER_PARSING_ERR_CODE);
282 s2 = strtok(NULL, ":");
288 chgType = TARIFF_NOW;
295 if (strcmp(s2, "now") == 0)
296 chgType = TARIFF_NOW;
298 if (strcmp(s2, "delayed") == 0)
299 chgType = TARIFF_DEL;
301 if (strcmp(s2, "recalc") == 0)
302 chgType = TARIFF_REC;
306 printf("Incorrect tariff value %s\n", t);
307 exit(PARAMETER_PARSING_ERR_CODE);
314 //-----------------------------------------------------------------------------
315 void ParseAnyString(const char * c, string * msg, const char * enc)
318 char * ob = new char[strlen(c) + 1];
319 char * ib = new char[strlen(c) + 1];
326 setlocale(LC_ALL, "");
329 strncpy(charsetF, nl_langinfo(CODESET), 255);
331 const char * charsetT = enc;
335 size_t insize = strlen(ib);
336 size_t outsize = strlen(ib);
340 cd = iconv_open(charsetT, charsetF);
341 if (cd == (iconv_t) -1)
345 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
350 printf("error iconv_open\n");
352 exit(ICONV_ERR_CODE);
355 #if defined(FREE_BSD) || defined(FREE_BSD5)
356 nconv = iconv (cd, (const char**)&inbuf, &insize, &outbuf, &outsize);
358 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
360 //printf("nconv=%d outsize=%d\n", nconv, outsize);
361 if (nconv == (size_t) -1)
365 printf("iconv error\n");
366 exit(ICONV_ERR_CODE);
378 //-----------------------------------------------------------------------------
379 void CreateRequestSet(REQUEST * req, char * r)
381 const int strLen = 10024;
383 memset(str, 0, strLen);
387 if (!req->usrMsg.res_empty())
390 Encode12str(msg, req->usrMsg);
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.c_str());
392 //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())
431 Encode12str(msg, req->message);
432 sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg.c_str());
436 if (!req->setCash.res_empty())
439 Encode12str(msg, req->message);
440 sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg.c_str());
444 if (!req->usrPasswd.res_empty())
446 sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
450 if (!req->down.res_empty())
452 sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
456 if (!req->passive.res_empty())
458 sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
462 if (!req->disableDetailStat.res_empty())
464 sprintf(str, "<disableDetailStat value=\"%d\" />\n", req->disableDetailStat.const_data());
468 if (!req->alwaysOnline.res_empty())
470 sprintf(str, "<aonline value=\"%d\" />\n", req->alwaysOnline.const_data());
474 // IP-address of user
475 if (!req->ips.res_empty())
477 sprintf(str, "<ip value=\"%s\" />\n", req->ips.const_data().c_str());
481 int uPresent = false;
482 int dPresent = false;
483 for (int i = 0; i < DIR_NUM; i++)
485 if (!req->u[i].res_empty())
487 if (!uPresent && !dPresent)
489 sprintf(str, "<traff ");
495 ss << req->u[i].const_data();
496 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
497 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
500 if (!req->d[i].res_empty())
502 if (!uPresent && !dPresent)
504 sprintf(str, "<traff ");
510 ss << req->d[i].const_data();
511 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
515 if (uPresent || dPresent)
522 if (!req->tariff.res_empty())
524 switch (req->chgTariff)
527 sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
531 sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
535 sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
542 if (!req->note.res_empty())
545 Encode12str(note, req->note);
546 sprintf(str, "<note value=\"%s\"/>", note.c_str());
550 if (!req->name.res_empty())
553 Encode12str(name, req->name);
554 sprintf(str, "<name value=\"%s\"/>", name.c_str());
558 if (!req->address.res_empty())
561 Encode12str(address, req->address);
562 sprintf(str, "<address value=\"%s\"/>", address.c_str());
566 if (!req->email.res_empty())
569 Encode12str(email, req->email);
570 sprintf(str, "<email value=\"%s\"/>", email.c_str());
574 if (!req->phone.res_empty())
577 Encode12str(phone, req->phone);
578 sprintf(str, "<phone value=\"%s\"/>", phone.c_str());
582 if (!req->group.res_empty())
585 Encode12str(group, req->group);
586 sprintf(str, "<group value=\"%s\"/>", group.c_str());
590 for (int i = 0; i < USERDATA_NUM; i++)
592 if (!req->ud[i].res_empty())
595 Encode12str(ud, req->ud[i]);
596 sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
601 strcat(r, "</SetUser>\n");
603 //-----------------------------------------------------------------------------
604 int CheckParameters(REQUEST * req)
609 int a = !req->admLogin.res_empty()
610 && !req->admPasswd.res_empty()
611 && !req->server.res_empty()
612 && !req->port.res_empty()
613 && !req->login.res_empty();
615 int b = !req->cash.res_empty()
616 || !req->setCash.res_empty()
617 || !req->credit.res_empty()
618 || !req->prepaidTraff.res_empty()
619 || !req->tariff.res_empty()
620 || !req->usrMsg.res_empty()
621 || !req->usrPasswd.res_empty()
623 || !req->note.res_empty()
624 || !req->name.res_empty()
625 || !req->address.res_empty()
626 || !req->email.res_empty()
627 || !req->phone.res_empty()
628 || !req->group.res_empty()
629 || !req->ips.res_empty() // IP-address of user
635 for (int i = 0; i < DIR_NUM; i++)
637 if (req->u[i].res_empty())
644 for (int i = 0; i < DIR_NUM; i++)
646 if (req->d[i].res_empty())
653 for (int i = 0; i < DIR_NUM; i++)
655 if (req->ud[i].res_empty())
663 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
664 return a && (b || u || d || ud);
666 //-----------------------------------------------------------------------------
667 int CheckParametersGet(REQUEST * req)
669 return CheckParameters(req);
671 //-----------------------------------------------------------------------------
672 int CheckParametersSet(REQUEST * req)
674 return CheckParameters(req);
676 //-----------------------------------------------------------------------------
677 int mainGet(int argc, char **argv)
681 RESETABLE<string> t1;
682 int missedOptionArg = false;
684 const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISO";
685 int option_index = -1;
690 c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
701 req.port = ParseServerPort(optarg);
706 req.admLogin = ParseAdminLogin(optarg);
709 case 'w': //admin password
710 req.admPasswd = ParsePassword(optarg);
713 case 'o': //change user password
718 req.login = ParseUser(optarg);
741 case 'e': //Prepaid Traffic
742 req.prepaidTraff = 1;
769 case 'I': //IP-address of user
773 case 'S': //Detail stat status
774 req.disableDetailStat = " ";
777 case 'O': //Always online status
778 req.alwaysOnline = " ";
791 //printf("U%d\n", c - 500);
805 //printf("D%d\n", c - 600);
819 //printf("UD%d\n", c - 700);
820 req.ud[c - 700] = " ";
825 //printf ("Unknown option \n");
826 missedOptionArg = true;
830 printf ("?? getopt returned character code 0%o ??\n", c);
836 printf ("non-option ARGV-elements: ");
837 while (optind < argc)
838 printf ("%s ", argv[optind++]);
840 exit(PARAMETER_PARSING_ERR_CODE);
843 if (missedOptionArg || !CheckParametersGet(&req))
845 //printf("Parameter needed\n");
847 exit(PARAMETER_PARSING_ERR_CODE);
850 return ProcessGetUser(req.server, req.port, req.admLogin, req.admPasswd, req.login, &req);
852 //-----------------------------------------------------------------------------
853 int mainSet(int argc, char **argv)
858 bool isMessage = false;
861 RESETABLE<string> t1;
863 const char * short_options_set = "s:p:a:w:u:c:r:t:m:o:d:i:e:v:nlN:A:D:L:P:G:I:S:O:";
865 int missedOptionArg = false;
869 int option_index = -1;
871 c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
883 req.port = ParseServerPort(optarg);
888 req.admLogin = ParseAdminLogin(optarg);
891 case 'w': //admin password
892 req.admPasswd = ParsePassword(optarg);
895 case 'o': //change user password
896 req.usrPasswd = ParsePassword(optarg);
900 req.login = ParseUser(optarg);
904 req.cash = ParseCash(optarg, &req.message);
908 req.setCash = ParseCash(optarg, &req.message);
912 req.credit = ParseCredit(optarg);
916 req.down = ParseDownPassive(optarg);
920 req.passive = ParseDownPassive(optarg);
924 req.tariff = ParseTariff(optarg, req.chgTariff);
928 ParseAnyString(optarg, &str);
933 case 'e': //Prepaid Traffic
934 req.prepaidTraff = ParsePrepaidTraffic(optarg);
937 case 'n': //Create User
938 req.createUser = true;
941 case 'l': //Delete User
942 req.deleteUser = true;
946 ParseAnyString(optarg, &str);
951 ParseAnyString(optarg, &str, "koi8-ru");
956 ParseAnyString(optarg, &str);
961 ParseAnyString(optarg, &str);
963 //printf("EMAIL=%s\n", optarg);
967 ParseAnyString(optarg, &str);
972 ParseAnyString(optarg, &str);
976 case 'I': //IP-address of user
977 ParseAnyString(optarg, &str);
982 req.disableDetailStat = ParseDownPassive(optarg);
986 req.alwaysOnline = ParseDownPassive(optarg);
999 //printf("U%d\n", c - 500);
1000 req.u[c - 500] = ParseTraff(optarg);
1013 //printf("D%d\n", c - 600);
1014 req.d[c - 600] = ParseTraff(optarg);
1017 case 700: //UserData
1027 ParseAnyString(optarg, &str);
1028 //printf("UD%d\n", c - 700);
1029 req.ud[c - 700] = str;
1033 //printf("Missing option argument\n");
1034 missedOptionArg = true;
1038 //printf("Missing option argument\n");
1039 missedOptionArg = true;
1043 printf("?? getopt returned character code 0%o ??\n", c);
1049 printf ("non-option ARGV-elements: ");
1050 while (optind < argc)
1051 printf ("%s ", argv[optind++]);
1053 exit(PARAMETER_PARSING_ERR_CODE);
1056 if (missedOptionArg || !CheckParametersSet(&req))
1058 //printf("Parameter needed\n");
1060 exit(PARAMETER_PARSING_ERR_CODE);
1063 const int rLen = 20000;
1065 memset(rstr, 0, rLen);
1067 CreateRequestSet(&req, rstr);
1068 return ProcessSetUser(req.server, req.port, req.admLogin, req.admPasswd, rstr, NULL, isMessage);
1070 //-----------------------------------------------------------------------------
1071 int main(int argc, char **argv)
1076 exit(PARAMETER_PARSING_ERR_CODE);
1079 if (strcmp(argv[1], "get") == 0)
1082 return mainGet(argc - 1, argv + 1);
1084 else if (strcmp(argv[1], "set") == 0)
1087 return mainSet(argc - 1, argv + 1);
1092 exit(PARAMETER_PARSING_ERR_CODE);
1094 return UNKNOWN_ERR_CODE;
1096 //-----------------------------------------------------------------------------