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 $
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 {"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
112 {"authorized-by",0, 0, 800}, //always online
116 struct option long_options_set[] = {
117 {"server", 1, 0, 's'}, //Server
118 {"port", 1, 0, 'p'}, //Port
119 {"admin", 1, 0, 'a'}, //Admin
120 {"admin_pass", 1, 0, 'w'}, //passWord
121 {"user", 1, 0, 'u'}, //User
122 {"addcash", 1, 0, 'c'}, //Add Cash
123 {"setcash", 1, 0, 'v'}, //Set Cash
124 {"credit", 1, 0, 'r'}, //cRedit
125 {"tariff", 1, 0, 't'}, //Tariff
126 {"message", 1, 0, 'm'}, //message
127 {"password", 1, 0, 'o'}, //password
128 {"down", 1, 0, 'd'}, //down
129 {"passive", 1, 0, 'i'}, //passive
130 {"disable-stat",1, 0, 'S'}, //disable detail stat
131 {"always-online",1, 0, 'O'}, //always online
132 {"u0", 1, 0, 500}, //U0
133 {"u1", 1, 0, 501}, //U1
134 {"u2", 1, 0, 502}, //U2
135 {"u3", 1, 0, 503}, //U3
136 {"u4", 1, 0, 504}, //U4
137 {"u5", 1, 0, 505}, //U5
138 {"u6", 1, 0, 506}, //U6
139 {"u7", 1, 0, 507}, //U7
140 {"u8", 1, 0, 508}, //U8
141 {"u9", 1, 0, 509}, //U9
142 {"d0", 1, 0, 600}, //D0
143 {"d1", 1, 0, 601}, //D1
144 {"d2", 1, 0, 602}, //D2
145 {"d3", 1, 0, 603}, //D3
146 {"d4", 1, 0, 604}, //D4
147 {"d5", 1, 0, 605}, //D5
148 {"d6", 1, 0, 606}, //D6
149 {"d7", 1, 0, 607}, //D7
150 {"d8", 1, 0, 608}, //D8
151 {"d9", 1, 0, 609}, //D9
153 {"ud0", 1, 0, 700}, //UserData
154 {"ud1", 1, 0, 701}, //UserData1
155 {"ud2", 1, 0, 702}, //UserData2
156 {"ud3", 1, 0, 703}, //UserData3
157 {"ud4", 1, 0, 704}, //UserData4
158 {"ud5", 1, 0, 705}, //UserData5
159 {"ud6", 1, 0, 706}, //UserData6
160 {"ud7", 1, 0, 707}, //UserData7
161 {"ud8", 1, 0, 708}, //UserData8
162 {"ud9", 1, 0, 709}, //UserData9
164 {"prepaid", 1, 0, 'e'}, //prepaid traff
165 {"create", 1, 0, 'n'}, //create
166 {"delete", 1, 0, 'l'}, //delete
168 {"note", 1, 0, 'N'}, //Note
169 {"name", 1, 0, 'A'}, //nAme
170 {"address", 1, 0, 'D'}, //aDdress
171 {"email", 1, 0, 'L'}, //emaiL
172 {"phone", 1, 0, 'P'}, //phone
173 {"group", 1, 0, 'G'}, //Group
174 {"ip", 0, 0, 'I'}, //IP-address of user
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 time_t ParseCreditExpire(const char * str)
318 struct tm brokenTime;
320 brokenTime.tm_wday = 0;
321 brokenTime.tm_yday = 0;
322 brokenTime.tm_isdst = 0;
323 brokenTime.tm_hour = 0;
324 brokenTime.tm_min = 0;
325 brokenTime.tm_sec = 0;
327 stg_strptime(str, "%Y-%m-%d", &brokenTime);
329 return stg_timegm(&brokenTime);
331 //-----------------------------------------------------------------------------
332 void ParseAnyString(const char * c, string * msg, const char * enc)
335 char * ob = new char[strlen(c) + 1];
336 char * ib = new char[strlen(c) + 1];
343 setlocale(LC_ALL, "");
346 strncpy(charsetF, nl_langinfo(CODESET), 255);
348 const char * charsetT = enc;
352 size_t insize = strlen(ib);
353 size_t outsize = strlen(ib);
357 cd = iconv_open(charsetT, charsetF);
358 if (cd == (iconv_t) -1)
362 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
367 printf("error iconv_open\n");
369 exit(ICONV_ERR_CODE);
372 #if defined(FREE_BSD) || defined(FREE_BSD5)
373 nconv = iconv (cd, (const char**)&inbuf, &insize, &outbuf, &outsize);
375 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
377 //printf("nconv=%d outsize=%d\n", nconv, outsize);
378 if (nconv == (size_t) -1)
382 printf("iconv error\n");
383 exit(ICONV_ERR_CODE);
395 //-----------------------------------------------------------------------------
396 void CreateRequestSet(REQUEST * req, char * r)
398 const int strLen = 10024;
400 memset(str, 0, strLen);
404 if (!req->usrMsg.empty())
407 Encode12str(msg, req->usrMsg.const_data());
408 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());
409 //sprintf(str, "<message login=\"%s\" priority=\"0\" text=\"%s\"/>\n", req->login, msg);
416 sprintf(str, "<DelUser login=\"%s\"/>", req->login.const_data().c_str());
424 sprintf(str, "<AddUser> <login value=\"%s\"/> </AddUser>", req->login.const_data().c_str());
430 strcat(r, "<SetUser>\n");
431 sprintf(str, "<login value=\"%s\"/>\n", req->login.const_data().c_str());
433 if (!req->credit.empty())
435 sprintf(str, "<credit value=\"%f\"/>\n", req->credit.const_data());
439 if (!req->creditExpire.empty())
441 sprintf(str, "<creditExpire value=\"%ld\"/>\n", req->creditExpire.const_data());
445 if (!req->prepaidTraff.empty())
447 sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
451 if (!req->cash.empty())
454 Encode12str(msg, req->message.c_str());
455 sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg.c_str());
459 if (!req->setCash.empty())
462 Encode12str(msg, req->message.c_str());
463 sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg.c_str());
467 if (!req->usrPasswd.empty())
469 sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
473 if (!req->down.empty())
475 sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
479 if (!req->passive.empty())
481 sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
485 if (!req->disableDetailStat.empty())
487 sprintf(str, "<disableDetailStat value=\"%d\" />\n", req->disableDetailStat.const_data());
491 if (!req->alwaysOnline.empty())
493 sprintf(str, "<aonline value=\"%d\" />\n", req->alwaysOnline.const_data());
497 // IP-address of user
498 if (!req->ips.empty())
500 sprintf(str, "<ip value=\"%s\" />\n", req->ips.const_data().c_str());
504 int uPresent = false;
505 int dPresent = false;
506 for (int i = 0; i < DIR_NUM; i++)
508 if (!req->u[i].empty())
510 if (!uPresent && !dPresent)
512 sprintf(str, "<traff ");
518 ss << req->u[i].const_data();
519 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
520 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
523 if (!req->d[i].empty())
525 if (!uPresent && !dPresent)
527 sprintf(str, "<traff ");
533 ss << req->d[i].const_data();
534 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
538 if (uPresent || dPresent)
545 if (!req->tariff.empty())
547 switch (req->chgTariff)
550 sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
554 sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
558 sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
565 if (!req->note.empty())
568 Encode12str(note, req->note.const_data());
569 sprintf(str, "<note value=\"%s\"/>", note.c_str());
573 if (!req->name.empty())
576 Encode12str(name, req->name.const_data());
577 sprintf(str, "<name value=\"%s\"/>", name.c_str());
581 if (!req->address.empty())
584 Encode12str(address, req->address.const_data());
585 sprintf(str, "<address value=\"%s\"/>", address.c_str());
589 if (!req->email.empty())
592 Encode12str(email, req->email.const_data());
593 sprintf(str, "<email value=\"%s\"/>", email.c_str());
597 if (!req->phone.empty())
600 Encode12str(phone, req->phone.const_data());
601 sprintf(str, "<phone value=\"%s\"/>", phone.c_str());
605 if (!req->group.empty())
608 Encode12str(group, req->group.const_data());
609 sprintf(str, "<group value=\"%s\"/>", group.c_str());
613 for (int i = 0; i < USERDATA_NUM; i++)
615 if (!req->ud[i].empty())
618 Encode12str(ud, req->ud[i].const_data());
619 sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
624 strcat(r, "</SetUser>\n");
626 //-----------------------------------------------------------------------------
627 int CheckParameters(REQUEST * req)
632 int a = !req->admLogin.empty()
633 && !req->admPasswd.empty()
634 && !req->server.empty()
635 && !req->port.empty()
636 && !req->login.empty();
638 int b = !req->cash.empty()
639 || !req->setCash.empty()
640 || !req->credit.empty()
641 || !req->prepaidTraff.empty()
642 || !req->tariff.empty()
643 || !req->usrMsg.empty()
644 || !req->usrPasswd.empty()
646 || !req->note.empty()
647 || !req->name.empty()
648 || !req->address.empty()
649 || !req->email.empty()
650 || !req->phone.empty()
651 || !req->group.empty()
652 || !req->ips.empty() // IP-address of user
658 for (int i = 0; i < DIR_NUM; i++)
660 if (req->u[i].empty())
667 for (int i = 0; i < DIR_NUM; i++)
669 if (req->d[i].empty())
676 for (int i = 0; i < DIR_NUM; i++)
678 if (req->ud[i].empty())
686 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
687 return a && (b || u || d || ud);
689 //-----------------------------------------------------------------------------
690 int CheckParametersGet(REQUEST * req)
692 return CheckParameters(req);
694 //-----------------------------------------------------------------------------
695 int CheckParametersSet(REQUEST * req)
697 return CheckParameters(req);
699 //-----------------------------------------------------------------------------
700 int mainGet(int argc, char **argv)
704 RESETABLE<string> t1;
705 int missedOptionArg = false;
707 const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISOE";
708 int option_index = -1;
713 c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
724 req.port = ParseServerPort(optarg);
729 req.admLogin = ParseAdminLogin(optarg);
732 case 'w': //admin password
733 req.admPasswd = ParsePassword(optarg);
736 case 'o': //change user password
741 req.login = ParseUser(optarg);
752 case 'E': //credit expire
753 req.creditExpire = 1;
768 case 'e': //Prepaid Traffic
769 req.prepaidTraff = 1;
796 case 'I': //IP-address of user
800 case 'S': //Detail stat status
801 req.disableDetailStat = " ";
804 case 'O': //Always online status
805 req.alwaysOnline = " ";
818 //printf("U%d\n", c - 500);
832 //printf("D%d\n", c - 600);
846 //printf("UD%d\n", c - 700);
847 req.ud[c - 700] = " ";
856 //printf ("Unknown option \n");
857 missedOptionArg = true;
861 printf ("?? getopt returned character code 0%o ??\n", c);
867 printf ("non-option ARGV-elements: ");
868 while (optind < argc)
869 printf ("%s ", argv[optind++]);
871 exit(PARAMETER_PARSING_ERR_CODE);
874 if (missedOptionArg || !CheckParametersGet(&req))
876 //printf("Parameter needed\n");
878 exit(PARAMETER_PARSING_ERR_CODE);
882 return ProcessAuthBy(req.server.const_data(), req.port.const_data(), req.admLogin.const_data(), req.admPasswd.const_data(), req.login.const_data(), &req);
884 return ProcessGetUser(req.server.const_data(), req.port.const_data(), req.admLogin.const_data(), req.admPasswd.const_data(), req.login.const_data(), &req);
886 //-----------------------------------------------------------------------------
887 int mainSet(int argc, char **argv)
892 bool isMessage = false;
895 RESETABLE<string> t1;
897 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:E:";
899 int missedOptionArg = false;
903 int option_index = -1;
905 c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
917 req.port = ParseServerPort(optarg);
922 req.admLogin = ParseAdminLogin(optarg);
925 case 'w': //admin password
926 req.admPasswd = ParsePassword(optarg);
929 case 'o': //change user password
930 req.usrPasswd = ParsePassword(optarg);
934 req.login = ParseUser(optarg);
938 req.cash = ParseCash(optarg, &req.message);
942 req.setCash = ParseCash(optarg, &req.message);
946 req.credit = ParseCredit(optarg);
949 case 'E': //credit expire
950 req.creditExpire = ParseCreditExpire(optarg);
954 req.down = ParseDownPassive(optarg);
958 req.passive = ParseDownPassive(optarg);
962 req.tariff = ParseTariff(optarg, req.chgTariff);
966 ParseAnyString(optarg, &str);
971 case 'e': //Prepaid Traffic
972 req.prepaidTraff = ParsePrepaidTraffic(optarg);
975 case 'n': //Create User
976 req.createUser = true;
979 case 'l': //Delete User
980 req.deleteUser = true;
984 ParseAnyString(optarg, &str, "koi8-ru");
989 ParseAnyString(optarg, &str, "koi8-ru");
994 ParseAnyString(optarg, &str, "koi8-ru");
999 ParseAnyString(optarg, &str, "koi8-ru");
1001 //printf("EMAIL=%s\n", optarg);
1005 ParseAnyString(optarg, &str);
1010 ParseAnyString(optarg, &str, "koi8-ru");
1014 case 'I': //IP-address of user
1015 ParseAnyString(optarg, &str);
1020 req.disableDetailStat = ParseDownPassive(optarg);
1024 req.alwaysOnline = ParseDownPassive(optarg);
1037 //printf("U%d\n", c - 500);
1038 req.u[c - 500] = ParseTraff(optarg);
1051 //printf("D%d\n", c - 600);
1052 req.d[c - 600] = ParseTraff(optarg);
1055 case 700: //UserData
1065 ParseAnyString(optarg, &str);
1066 //printf("UD%d\n", c - 700);
1067 req.ud[c - 700] = str;
1071 //printf("Missing option argument\n");
1072 missedOptionArg = true;
1076 //printf("Missing option argument\n");
1077 missedOptionArg = true;
1081 printf("?? getopt returned character code 0%o ??\n", c);
1087 printf ("non-option ARGV-elements: ");
1088 while (optind < argc)
1089 printf ("%s ", argv[optind++]);
1091 exit(PARAMETER_PARSING_ERR_CODE);
1094 if (missedOptionArg || !CheckParametersSet(&req))
1096 //printf("Parameter needed\n");
1098 exit(PARAMETER_PARSING_ERR_CODE);
1101 const int rLen = 20000;
1103 memset(rstr, 0, rLen);
1105 CreateRequestSet(&req, rstr);
1106 return ProcessSetUser(req.server.const_data(), req.port.const_data(), req.admLogin.const_data(), req.admPasswd.const_data(), rstr, NULL, isMessage);
1108 //-----------------------------------------------------------------------------
1109 int main(int argc, char **argv)
1114 exit(PARAMETER_PARSING_ERR_CODE);
1117 if (strcmp(argv[1], "get") == 0)
1120 return mainGet(argc - 1, argv + 1);
1122 else if (strcmp(argv[1], "set") == 0)
1125 return mainSet(argc - 1, argv + 1);
1130 exit(PARAMETER_PARSING_ERR_CODE);
1132 return UNKNOWN_ERR_CODE;
1134 //-----------------------------------------------------------------------------