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>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
23 #include "common_sg.h"
24 #include "sg_error_codes.h"
30 #include "stg/user_conf.h"
31 #include "stg/user_stat.h"
32 #include "stg/common.h"
53 typedef typename T::value_type type;
57 struct ARRAY_TYPE<T[]>
62 template <typename T, size_t N>
63 struct ARRAY_TYPE<T[N]>
69 bool SetArrayItem(T & array, const char * index, const typename ARRAY_TYPE<T>::type & value)
72 if (str2x(index, pos))
80 void UsageImpl(bool full);
81 void UsageConnection();
82 void UsageAdmins(bool full);
83 void UsageTariffs(bool full);
84 void UsageUsers(bool full);
85 void UsageServices(bool full);
86 void UsageCorporations(bool full);
90 } // namespace anonymous
95 class CONFIG_ACTION: public ACTION
98 CONFIG_ACTION(CONFIG & config,
99 const std::string & paramDescription)
101 m_description(paramDescription)
104 virtual std::string ParamDescription() const { return m_description; }
105 virtual std::string DefaultDescription() const { return ""; }
106 virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
107 virtual PARSER_STATE Parse(int argc, char ** argv);
111 std::string m_description;
112 OPTION_BLOCK m_suboptions;
114 void ParseCredentials(const std::string & credentials);
115 void ParseHostAndPort(const std::string & hostAndPort);
118 PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv)
120 char * pos = strchr(*argv, '@');
123 ParseCredentials(std::string(*argv, pos));
124 ParseHostAndPort(std::string(pos + 1));
128 ParseHostAndPort(std::string(*argv));
130 return PARSER_STATE(false, --argc, ++argv);
133 void CONFIG_ACTION::ParseCredentials(const std::string & credentials)
135 std::string::size_type pos = credentials.find_first_of(':');
136 if (pos != std::string::npos)
138 m_config.userName = credentials.substr(0, pos);
139 m_config.userPass = credentials.substr(pos + 1);
143 m_config.userName = credentials;
147 void CONFIG_ACTION::ParseHostAndPort(const std::string & hostAndPort)
149 std::string::size_type pos = hostAndPort.find_first_of(':');
150 if (pos != std::string::npos)
152 m_config.server = hostAndPort.substr(0, pos);
154 if (str2x(hostAndPort.substr(pos + 1), port))
155 throw ERROR("Invalid port value: '" + hostAndPort.substr(pos + 1) + "'");
156 m_config.port = port;
160 m_config.server = hostAndPort;
165 CONFIG_ACTION * MakeParamAction(CONFIG & config,
166 const std::string & paramDescription)
168 return new CONFIG_ACTION(config, paramDescription);
171 } // namespace SGCONF
175 struct option long_options_get[] = {
176 {"server", 1, 0, 's'}, //Server
177 {"port", 1, 0, 'p'}, //Port
178 {"admin", 1, 0, 'a'}, //Admin
179 {"admin_pass", 1, 0, 'w'}, //passWord
180 {"user", 1, 0, 'u'}, //User
181 {"addcash", 0, 0, 'c'}, //Add Cash
182 //{"setcash", 0, 0, 'v'}, //Set Cash
183 {"credit", 0, 0, 'r'}, //cRedit
184 {"tariff", 0, 0, 't'}, //Tariff
185 {"message", 0, 0, 'm'}, //message
186 {"password", 0, 0, 'o'}, //password
187 {"down", 0, 0, 'd'}, //down
188 {"passive", 0, 0, 'i'}, //passive
189 {"disable-stat",0, 0, 'S'}, //disable detail stat
190 {"always-online",0, 0, 'O'}, //always online
191 {"session-upload", 1, 0, 500}, //SU0
192 {"session-download", 1, 0, 501}, //SD0
193 {"month-upload", 1, 0, 502}, //MU0
194 {"month-download", 1, 0, 503}, //MD0
196 {"user-data", 1, 0, 700}, //UserData0
198 {"prepaid", 0, 0, 'e'}, //prepaid traff
199 {"create", 0, 0, 'n'}, //create
200 {"delete", 0, 0, 'l'}, //delete
202 {"note", 0, 0, 'N'}, //Note
203 {"name", 0, 0, 'A'}, //nAme
204 {"address", 0, 0, 'D'}, //aDdress
205 {"email", 0, 0, 'L'}, //emaiL
206 {"phone", 0, 0, 'P'}, //phone
207 {"group", 0, 0, 'G'}, //Group
208 {"ip", 0, 0, 'I'}, //IP-address of user
209 {"authorized-by",0, 0, 800}, //always online
213 struct option long_options_set[] = {
214 {"server", 1, 0, 's'}, //Server
215 {"port", 1, 0, 'p'}, //Port
216 {"admin", 1, 0, 'a'}, //Admin
217 {"admin_pass", 1, 0, 'w'}, //passWord
218 {"user", 1, 0, 'u'}, //User
219 {"addcash", 1, 0, 'c'}, //Add Cash
220 {"setcash", 1, 0, 'v'}, //Set Cash
221 {"credit", 1, 0, 'r'}, //cRedit
222 {"tariff", 1, 0, 't'}, //Tariff
223 {"message", 1, 0, 'm'}, //message
224 {"password", 1, 0, 'o'}, //password
225 {"down", 1, 0, 'd'}, //down
226 {"passive", 1, 0, 'i'}, //passive
227 {"disable-stat",1, 0, 'S'}, //disable detail stat
228 {"always-online",1, 0, 'O'}, //always online
229 {"session-upload", 1, 0, 500}, //U0
230 {"session-download", 1, 0, 501}, //U1
231 {"month-upload", 1, 0, 502}, //U2
232 {"month-download", 1, 0, 503}, //U3
234 {"user-data", 1, 0, 700}, //UserData
236 {"prepaid", 1, 0, 'e'}, //prepaid traff
237 {"create", 1, 0, 'n'}, //create
238 {"delete", 1, 0, 'l'}, //delete
240 {"note", 1, 0, 'N'}, //Note
241 {"name", 1, 0, 'A'}, //nAme
242 {"address", 1, 0, 'D'}, //aDdress
243 {"email", 1, 0, 'L'}, //emaiL
244 {"phone", 1, 0, 'P'}, //phone
245 {"group", 1, 0, 'G'}, //Group
246 {"ip", 0, 0, 'I'}, //IP-address of user
250 //-----------------------------------------------------------------------------
251 CASH_INFO ParseCash(const char * str)
253 //-c 123.45:log message
254 std::string cashString;
256 const char * pos = strchr(str, ':');
259 cashString.append(str, pos);
260 message.append(pos + 1);
266 if (strtodouble2(cashString, cash) != 0)
268 printf("Incorrect cash value %s\n", str);
269 exit(PARAMETER_PARSING_ERR_CODE);
272 return CASH_INFO(cash, message);
274 //-----------------------------------------------------------------------------
275 double ParseCredit(const char * c)
278 if (strtodouble2(c, credit) != 0)
280 printf("Incorrect credit value %s\n", c);
281 exit(PARAMETER_PARSING_ERR_CODE);
286 //-----------------------------------------------------------------------------
287 double ParsePrepaidTraffic(const char * c)
290 if (strtodouble2(c, credit) != 0)
292 printf("Incorrect prepaid traffic value %s\n", c);
293 exit(PARAMETER_PARSING_ERR_CODE);
298 //-----------------------------------------------------------------------------
299 int64_t ParseTraff(const char * c)
302 if (str2x(c, traff) != 0)
304 printf("Incorrect credit value %s\n", c);
305 exit(PARAMETER_PARSING_ERR_CODE);
310 //-----------------------------------------------------------------------------
311 bool ParseDownPassive(const char * dp)
313 if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0')))
315 printf("Incorrect value %s\n", dp);
316 exit(PARAMETER_PARSING_ERR_CODE);
321 //-----------------------------------------------------------------------------
322 void ParseTariff(const char * str, RESETABLE<std::string> & tariffName, RESETABLE<std::string> & nextTariff)
324 const char * pos = strchr(str, ':');
327 std::string tariff(str, pos);
328 if (strcmp(pos + 1, "now") == 0)
330 else if (strcmp(pos + 1, "delayed") == 0)
334 printf("Incorrect tariff value '%s'. Should be '<tariff>', '<tariff>:now' or '<tariff>:delayed'.\n", str);
335 exit(PARAMETER_PARSING_ERR_CODE);
341 //-----------------------------------------------------------------------------
342 time_t ParseCreditExpire(const char * str)
344 struct tm brokenTime;
346 brokenTime.tm_wday = 0;
347 brokenTime.tm_yday = 0;
348 brokenTime.tm_isdst = 0;
349 brokenTime.tm_hour = 0;
350 brokenTime.tm_min = 0;
351 brokenTime.tm_sec = 0;
353 stg_strptime(str, "%Y-%m-%d", &brokenTime);
355 return stg_timegm(&brokenTime);
357 //-----------------------------------------------------------------------------
358 void ParseAnyString(const char * c, string * msg, const char * enc)
361 char * ob = new char[strlen(c) + 1];
362 char * ib = new char[strlen(c) + 1];
369 setlocale(LC_ALL, "");
372 strncpy(charsetF, nl_langinfo(CODESET), 255);
374 const char * charsetT = enc;
378 size_t insize = strlen(ib);
379 size_t outsize = strlen(ib);
383 cd = iconv_open(charsetT, charsetF);
384 if (cd == (iconv_t) -1)
388 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
393 printf("error iconv_open\n");
395 exit(ICONV_ERR_CODE);
398 #if defined(FREE_BSD) || defined(FREE_BSD5)
399 nconv = iconv (cd, (const char**)&inbuf, &insize, &outbuf, &outsize);
401 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
403 //printf("nconv=%d outsize=%d\n", nconv, outsize);
404 if (nconv == (size_t) -1)
408 printf("iconv error\n");
409 exit(ICONV_ERR_CODE);
421 //-----------------------------------------------------------------------------
422 void CreateRequestSet(REQUEST * req, char * r)
424 const int strLen = 10024;
426 memset(str, 0, strLen);
430 if (!req->usrMsg.empty())
433 Encode12str(msg, req->usrMsg.data());
434 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());
435 //sprintf(str, "<message login=\"%s\" priority=\"0\" text=\"%s\"/>\n", req->login, msg);
442 sprintf(str, "<DelUser login=\"%s\"/>", req->login.const_data().c_str());
450 sprintf(str, "<AddUser> <login value=\"%s\"/> </AddUser>", req->login.const_data().c_str());
456 strcat(r, "<SetUser>\n");
457 sprintf(str, "<login value=\"%s\"/>\n", req->login.const_data().c_str());
459 if (!req->credit.empty())
461 sprintf(str, "<credit value=\"%f\"/>\n", req->credit.const_data());
465 if (!req->creditExpire.empty())
467 sprintf(str, "<creditExpire value=\"%ld\"/>\n", req->creditExpire.const_data());
471 if (!req->prepaidTraff.empty())
473 sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
477 if (!req->cash.empty())
480 Encode12str(msg, req->message);
481 sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg.c_str());
485 if (!req->setCash.empty())
488 Encode12str(msg, req->message);
489 sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg.c_str());
493 if (!req->usrPasswd.empty())
495 sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
499 if (!req->down.empty())
501 sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
505 if (!req->passive.empty())
507 sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
511 if (!req->disableDetailStat.empty())
513 sprintf(str, "<disableDetailStat value=\"%d\" />\n", req->disableDetailStat.const_data());
517 if (!req->alwaysOnline.empty())
519 sprintf(str, "<aonline value=\"%d\" />\n", req->alwaysOnline.const_data());
523 // IP-address of user
524 if (!req->ips.empty())
526 sprintf(str, "<ip value=\"%s\" />\n", req->ips.const_data().c_str());
530 int uPresent = false;
531 int dPresent = false;
532 for (int i = 0; i < DIR_NUM; i++)
534 if (!req->monthUpload[i].empty())
536 if (!uPresent && !dPresent)
538 sprintf(str, "<traff ");
544 ss << req->monthUpload[i].const_data();
545 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
546 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
549 if (!req->monthDownload[i].empty())
551 if (!uPresent && !dPresent)
553 sprintf(str, "<traff ");
559 ss << req->monthDownload[i].const_data();
560 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
563 if (!req->sessionUpload[i].empty())
565 if (!uPresent && !dPresent)
567 sprintf(str, "<traff ");
573 ss << req->sessionUpload[i].const_data();
574 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
575 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
578 if (!req->sessionDownload[i].empty())
580 if (!uPresent && !dPresent)
582 sprintf(str, "<traff ");
588 ss << req->sessionDownload[i].const_data();
589 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
593 if (uPresent || dPresent)
600 if (!req->tariff.empty())
602 switch (req->chgTariff)
605 sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
609 sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
613 sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
620 if (!req->note.empty())
623 Encode12str(note, req->note.data());
624 sprintf(str, "<note value=\"%s\"/>", note.c_str());
628 if (!req->name.empty())
631 Encode12str(name, req->name.data());
632 sprintf(str, "<name value=\"%s\"/>", name.c_str());
636 if (!req->address.empty())
639 Encode12str(address, req->address.data());
640 sprintf(str, "<address value=\"%s\"/>", address.c_str());
644 if (!req->email.empty())
647 Encode12str(email, req->email.data());
648 sprintf(str, "<email value=\"%s\"/>", email.c_str());
652 if (!req->phone.empty())
655 Encode12str(phone, req->phone.data());
656 sprintf(str, "<phone value=\"%s\"/>", phone.c_str());
660 if (!req->group.empty())
663 Encode12str(group, req->group.data());
664 sprintf(str, "<group value=\"%s\"/>", group.c_str());
668 for (int i = 0; i < USERDATA_NUM; i++)
670 if (!req->userData[i].empty())
673 Encode12str(ud, req->userData[i].data());
674 sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
679 strcat(r, "</SetUser>\n");
681 //-----------------------------------------------------------------------------
682 int CheckParameters(REQUEST * req)
689 bool a = !req->admLogin.empty()
690 && !req->admPasswd.empty()
691 && !req->server.empty()
692 && !req->port.empty()
693 && !req->login.empty();
695 bool b = !req->cash.empty()
696 || !req->setCash.empty()
697 || !req->credit.empty()
698 || !req->prepaidTraff.empty()
699 || !req->tariff.empty()
700 || !req->usrMsg.empty()
701 || !req->usrPasswd.empty()
703 || !req->note.empty()
704 || !req->name.empty()
705 || !req->address.empty()
706 || !req->email.empty()
707 || !req->phone.empty()
708 || !req->group.empty()
709 || !req->ips.empty() // IP-address of user
715 for (int i = 0; i < DIR_NUM; i++)
717 if (req->sessionUpload[i].empty())
724 for (int i = 0; i < DIR_NUM; i++)
726 if (req->sessionDownload[i].empty())
733 for (int i = 0; i < DIR_NUM; i++)
735 if (req->monthUpload[i].empty())
742 for (int i = 0; i < DIR_NUM; i++)
744 if (req->monthDownload[i].empty())
751 for (int i = 0; i < DIR_NUM; i++)
753 if (req->userData[i].empty())
761 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
762 return a && (b || su || sd || mu || md || ud);
764 //-----------------------------------------------------------------------------
765 int CheckParametersGet(REQUEST * req)
767 return CheckParameters(req);
769 //-----------------------------------------------------------------------------
770 int CheckParametersSet(REQUEST * req)
772 return CheckParameters(req);
774 //-----------------------------------------------------------------------------
775 bool mainGet(int argc, char **argv)
779 RESETABLE<string> t1;
780 int missedOptionArg = false;
782 const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISOE";
783 int option_index = -1;
788 c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
799 req.port = ParseServerPort(optarg);
804 req.admLogin = ParseAdminLogin(optarg);
807 case 'w': //admin password
808 req.admPasswd = ParsePassword(optarg);
811 case 'o': //change user password
816 req.login = ParseUser(optarg);
827 case 'E': //credit expire
828 req.creditExpire = 1;
843 case 'e': //Prepaid Traffic
844 req.prepaidTraff = 1;
871 case 'I': //IP-address of user
875 case 'S': //Detail stat status
876 req.disableDetailStat = " ";
879 case 'O': //Always online status
880 req.alwaysOnline = " ";
884 SetArrayItem(req.sessionUpload, optarg, 1);
885 //req.sessionUpload[optarg] = 1;
888 SetArrayItem(req.sessionDownload, optarg, 1);
889 //req.sessionDownload[optarg] = 1;
892 SetArrayItem(req.monthUpload, optarg, 1);
893 //req.monthUpload[optarg] = 1;
896 SetArrayItem(req.monthDownload, optarg, 1);
897 //req.monthDownload[optarg] = 1;
901 SetArrayItem(req.userData, optarg, std::string(" "));
902 //req.userData[optarg] = " ";
911 missedOptionArg = true;
915 printf ("?? getopt returned character code 0%o ??\n", c);
921 printf ("non-option ARGV-elements: ");
922 while (optind < argc)
923 printf ("%s ", argv[optind++]);
925 exit(PARAMETER_PARSING_ERR_CODE);
928 if (missedOptionArg || !CheckParametersGet(&req))
930 //printf("Parameter needed\n");
932 exit(PARAMETER_PARSING_ERR_CODE);
936 return ProcessAuthBy(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data());
938 return ProcessGetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req);
940 //-----------------------------------------------------------------------------
941 bool mainSet(int argc, char **argv)
946 bool isMessage = false;
949 RESETABLE<string> t1;
951 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:";
953 int missedOptionArg = false;
959 int option_index = -1;
961 c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
973 req.port = ParseServerPort(optarg);
978 req.admLogin = ParseAdminLogin(optarg);
981 case 'w': //admin password
982 req.admPasswd = ParsePassword(optarg);
985 case 'o': //change user password
986 conf.password = ParsePassword(optarg);
990 req.login = ParseUser(optarg);
994 stat.cashAdd = ParseCash(optarg);
998 stat.cashSet = ParseCash(optarg);
1002 conf.credit = ParseCredit(optarg);
1005 case 'E': //credit expire
1006 conf.creditExpire = ParseCreditExpire(optarg);
1010 conf.disabled = ParseDownPassive(optarg);
1014 conf.passive = ParseDownPassive(optarg);
1018 ParseTariff(optarg, conf.tariffName, conf.nextTariff);
1022 ParseAnyString(optarg, &str);
1027 case 'e': //Prepaid Traffic
1028 stat.freeMb = ParsePrepaidTraffic(optarg);
1031 case 'n': //Create User
1032 req.createUser = true;
1035 case 'l': //Delete User
1036 req.deleteUser = true;
1040 ParseAnyString(optarg, &str, "koi8-ru");
1045 ParseAnyString(optarg, &str, "koi8-ru");
1046 conf.realName = str;
1050 ParseAnyString(optarg, &str, "koi8-ru");
1055 ParseAnyString(optarg, &str, "koi8-ru");
1060 ParseAnyString(optarg, &str);
1065 ParseAnyString(optarg, &str, "koi8-ru");
1069 case 'I': //IP-address of user
1070 ParseAnyString(optarg, &str);
1071 conf.ips = StrToIPS(str);
1075 conf.disabledDetailStat = ParseDownPassive(optarg);
1079 conf.alwaysOnline = ParseDownPassive(optarg);
1083 SetArrayItem(stat.sessionUp, optarg, ParseTraff(argv[optind++]));
1086 SetArrayItem(stat.sessionDown, optarg, ParseTraff(argv[optind++]));
1089 SetArrayItem(stat.monthUp, optarg, ParseTraff(argv[optind++]));
1092 SetArrayItem(stat.monthDown, optarg, ParseTraff(argv[optind++]));
1095 case 700: //UserData
1096 ParseAnyString(argv[optind++], &str);
1097 SetArrayItem(conf.userdata, optarg, str);
1101 missedOptionArg = true;
1105 missedOptionArg = true;
1109 printf("?? getopt returned character code 0%o ??\n", c);
1115 printf ("non-option ARGV-elements: ");
1116 while (optind < argc)
1117 printf ("%s ", argv[optind++]);
1119 exit(PARAMETER_PARSING_ERR_CODE);
1122 if (missedOptionArg || !CheckParametersSet(&req))
1124 //printf("Parameter needed\n");
1126 exit(PARAMETER_PARSING_ERR_CODE);
1129 const int rLen = 20000;
1131 memset(rstr, 0, rLen);
1134 return ProcessSendMessage(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req.usrMsg.data());
1136 return ProcessSetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), conf, stat);
1138 //-----------------------------------------------------------------------------
1139 int main(int argc, char **argv)
1144 SGCONF::CONFIG config;
1146 SGCONF::OPTION_BLOCK generalOptions;
1147 generalOptions.Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), "<config file>"), "override default config file");
1148 generalOptions.Add("h", "help", SGCONF::MakeFunc0Action(Usage), "show this help and exit");
1149 generalOptions.Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "show full help and exit");
1150 generalOptions.Add("v", "version", SGCONF::MakeFunc0Action(Version), "show version information and exit");
1152 SGCONF::OPTION_BLOCK connOptions;
1153 connOptions.Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "<address>"), "host to connect");
1154 connOptions.Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "port to connect");
1155 connOptions.Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "administrative login");
1156 connOptions.Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "password for the administrative login");
1157 connOptions.Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
1168 exit(PARAMETER_PARSING_ERR_CODE);
1171 if (strcmp(argv[1], "get") == 0)
1174 return mainGet(argc - 1, argv + 1);
1176 else if (strcmp(argv[1], "set") == 0)
1179 if (mainSet(argc - 1, argv + 1) )
1186 exit(PARAMETER_PARSING_ERR_CODE);
1188 return UNKNOWN_ERR_CODE;
1190 //-----------------------------------------------------------------------------
1205 void UsageImpl(bool full)
1207 std::cout << "sgconf is the Stargazer management utility.\n\n"
1209 << "\tsgconf [options]\n\n"
1210 << "General options:\n"
1211 << "\t-c, --config <config file>\t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n"
1212 << "\t-h, --help\t\t\t\tshow this help and exit\n"
1213 << "\t--help-all\t\t\t\tshow full help and exit\n"
1214 << "\t-v, --version\t\t\t\tshow version information and exit\n\n";
1219 UsageServices(full);
1220 UsageCorporations(full);
1222 //-----------------------------------------------------------------------------
1223 void UsageConnection()
1225 std::cout << "Connection options:\n"
1226 << "\t-s, --server <address>\t\t\thost to connect (ip or domain name, default: \"localhost\")\n"
1227 << "\t-p, --port <port>\t\t\tport to connect (default: \"5555\")\n"
1228 << "\t-u, --username <username>\t\tadministrative login (default: \"admin\")\n"
1229 << "\t-w, --userpass <password>\t\tpassword for administrative login\n"
1230 << "\t-a, --address <connection string>\tconnection params as a single string in format: <login>:<password>@<host>:<port>\n\n";
1232 //-----------------------------------------------------------------------------
1233 void UsageAdmins(bool full)
1235 std::cout << "Admins management options:\n"
1236 << "\t--get-admins\t\t\t\tget a list of admins (subsequent options will define what to show)\n";
1238 std::cout << "\t\t--login\t\t\t\tshow admin's login\n"
1239 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1240 std::cout << "\t--get-admin\t\t\t\tget the information about admin\n";
1242 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to show\n"
1243 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1244 std::cout << "\t--add-admin\t\t\t\tadd a new admin\n";
1246 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to add\n"
1247 << "\t\t--password <password>\t\tpassword of the admin to add\n"
1248 << "\t\t--priv <priv number>\t\tpriviledges of the admin to add\n\n";
1249 std::cout << "\t--del-admin\t\t\t\tdelete an existing admin\n";
1251 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to delete\n\n";
1252 std::cout << "\t--chg-admin\t\t\t\tchange an existing admin\n";
1254 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to change\n"
1255 << "\t\t--priv <priv number>\t\tnew priviledges\n\n";
1257 //-----------------------------------------------------------------------------
1258 void UsageTariffs(bool full)
1260 std::cout << "Tariffs management options:\n"
1261 << "\t--get-tariffs\t\t\t\tget a list of tariffs (subsequent options will define what to show)\n";
1263 std::cout << "\t\t--name\t\t\t\tshow tariff's name\n"
1264 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1265 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1266 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1267 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1268 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1269 std::cout << "\t--get-tariff\t\t\t\tget the information about tariff\n";
1271 std::cout << "\t\t--name <name>\t\t\tname of the tariff to show\n"
1272 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1273 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1274 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1275 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1276 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1277 std::cout << "\t--add-tariff\t\t\t\tadd a new tariff\n";
1279 std::cout << "\t\t--name <name>\t\t\tname of the tariff to add\n"
1280 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1281 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1282 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1283 << "\t\t--traff-type <type>\t\twhat type of traffi will be accounted by the tariff\n"
1284 << "\t\t--times <times>\t\t\tslash-separated list of \"day\" time-spans (in form \"hh:mm-hh:mm\") for each direction\n"
1285 << "\t\t--prices-day-a <prices>\t\tslash-separated list of prices for \"day\" traffic before threshold for each direction\n"
1286 << "\t\t--prices-night-a <prices>\tslash-separated list of prices for \"night\" traffic before threshold for each direction\n"
1287 << "\t\t--prices-day-b <prices>\t\tslash-separated list of prices for \"day\" traffic after threshold for each direction\n"
1288 << "\t\t--prices-night-b <prices>\tslash-separated list of prices for \"night\" traffic after threshold for each direction\n"
1289 << "\t\t--single-prices <yes|no>\tslash-separated list of \"single price\" flags for each direction\n"
1290 << "\t\t--no-discounts <yes|no>\t\tslash-separated list of \"no discount\" flags for each direction\n"
1291 << "\t\t--thresholds <thresholds>\tslash-separated list of thresholds (in Mb) for each direction\n\n";
1292 std::cout << "\t--del-tariff\t\t\t\tdelete an existing tariff\n";
1294 std::cout << "\t\t--name <name>\t\t\tname of the tariff to delete\n\n";
1295 std::cout << "\t--chg-tariff\t\t\t\tchange an existing tariff\n";
1297 std::cout << "\t\t--name <name>\t\t\tname of the tariff to change\n"
1298 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1299 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1300 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1301 << "\t\t--traff-type <type>\t\twhat type of traffix will be accounted by the tariff\n"
1302 << "\t\t--dir <N>\t\t\tnumber of direction data to change\n"
1303 << "\t\t\t--time <time>\t\t\"day\" time-span (in form \"hh:mm-hh:mm\")\n"
1304 << "\t\t\t--price-day-a <price>\tprice for \"day\" traffic before threshold\n"
1305 << "\t\t\t--price-night-a <price>\tprice for \"night\" traffic before threshold\n"
1306 << "\t\t\t--price-day-b <price>\tprice for \"day\" traffic after threshold\n"
1307 << "\t\t\t--price-night-b <price>\tprice for \"night\" traffic after threshold\n"
1308 << "\t\t\t--single-price <yes|no>\t\"single price\" flag\n"
1309 << "\t\t\t--no-discount <yes|no>\t\"no discount\" flag\n"
1310 << "\t\t\t--threshold <threshold>\tthreshold (in Mb)\n\n";
1312 //-----------------------------------------------------------------------------
1313 void UsageUsers(bool full)
1315 std::cout << "Users management options:\n"
1316 << "\t--get-users\t\t\t\tget a list of users (subsequent options will define what to show)\n";
1318 std::cout << "\n\n";
1319 std::cout << "\t--get-user\t\t\t\tget the information about user\n";
1321 std::cout << "\n\n";
1322 std::cout << "\t--add-user\t\t\t\tadd a new user\n";
1324 std::cout << "\n\n";
1325 std::cout << "\t--del-user\t\t\t\tdelete an existing user\n";
1327 std::cout << "\n\n";
1328 std::cout << "\t--chg-user\t\t\t\tchange an existing user\n";
1330 std::cout << "\n\n";
1331 std::cout << "\t--check-user\t\t\t\tcheck credentials is valid\n";
1333 std::cout << "\n\n";
1334 std::cout << "\t--send-message\t\t\t\tsend a message to a user\n";
1336 std::cout << "\n\n";
1338 //-----------------------------------------------------------------------------
1339 void UsageServices(bool full)
1341 std::cout << "Services management options:\n"
1342 << "\t--get-services\t\t\t\tget a list of services (subsequent options will define what to show)\n";
1344 std::cout << "\t\t--name\t\t\t\tshow service's name\n"
1345 << "\t\t--comment\t\t\tshow a comment to the service\n"
1346 << "\t\t--cost\t\t\t\tshow service's cost\n"
1347 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1348 std::cout << "\t--get-service\t\t\t\tget the information about service\n";
1350 std::cout << "\t\t--name <name>\t\t\tname of the service to show\n"
1351 << "\t\t--comment\t\t\tshow a comment to the service\n"
1352 << "\t\t--cost\t\t\t\tshow service's cost\n"
1353 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1354 std::cout << "\t--add-service\t\t\t\tadd a new service\n";
1356 std::cout << "\t\t--name <name>\t\t\tname of the service to add\n"
1357 << "\t\t--comment <comment>\t\ta comment to the service\n"
1358 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1359 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1360 std::cout << "\t--del-service\t\t\t\tdelete an existing service\n";
1362 std::cout << "\t\t--name <name>\t\t\tname of the service to delete\n\n";
1363 std::cout << "\t--chg-service\t\t\t\tchange an existing service\n";
1365 std::cout << "\t\t--name <name>\t\t\tname of the service to change\n"
1366 << "\t\t--comment <comment>\t\ta comment to the service\n"
1367 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1368 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1370 //-----------------------------------------------------------------------------
1371 void UsageCorporations(bool full)
1373 std::cout << "Corporations management options:\n"
1374 << "\t--get-corporations\t\t\tget a list of corporations (subsequent options will define what to show)\n";
1376 std::cout << "\t\t--name\t\t\t\tshow corporation's name\n"
1377 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1378 std::cout << "\t--get-corp\t\t\t\tget the information about corporation\n";
1380 std::cout << "\t\t--name <name>\t\t\tname of the corporation to show\n"
1381 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1382 std::cout << "\t--add-corp\t\t\t\tadd a new corporation\n";
1384 std::cout << "\t\t--name <name>\t\t\tname of the corporation to add\n"
1385 << "\t\t--cash <cash>\t\t\tinitial corporation's cash (default: \"0\")\n\n";
1386 std::cout << "\t--del-corp\t\t\t\tdelete an existing corporation\n";
1388 std::cout << "\t\t--name <name>\t\t\tname of the corporation to delete\n\n";
1389 std::cout << "\t--chg-corp\t\t\t\tchange an existing corporation\n";
1391 std::cout << "\t\t--name <name>\t\t\tname of the corporation to change\n"
1392 << "\t\t--add-cash <amount>[:<message>]\tadd cash to the corporation's account and optional comment message\n"
1393 << "\t\t--set-cash <cash>[:<message>]\tnew corporation's cash and optional comment message\n\n";
1398 std::cout << "sgconf, version: 2.0.0-alpha.\n";
1401 } // namespace anonymous