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 struct nullary_function
71 typedef T result_type;
75 class binder0 : public nullary_function<typename F::result_type>
78 binder0(const F & func, const typename F::argument_type & arg)
79 : m_func(func), m_arg(arg) {}
80 typename F::result_type operator()() const { return m_func(m_arg); }
83 typename F::argument_type m_arg;
88 binder0<F> bind0(const F & func, const typename F::argument_type & arg)
90 return binder0<F>(func, arg);
93 template <typename C, typename A, typename R>
94 class METHOD1_ADAPTER : public std::unary_function<A, R>
97 METHOD1_ADAPTER(R (C::* func)(A), C & obj) : m_func(func), m_obj(obj) {}
98 R operator()(A arg) { return (m_obj.*m_func)(arg); }
104 template <typename C, typename A, typename R>
105 class CONST_METHOD1_ADAPTER : public std::unary_function<A, R>
108 CONST_METHOD1_ADAPTER(R (C::* func)(A) const, C & obj) : m_func(func), m_obj(obj) {}
109 R operator()(A arg) const { return (m_obj.*m_func)(arg); }
111 R (C::* m_func)(A) const;
115 template <typename C, typename A, typename R>
116 METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A), C & obj)
118 return METHOD1_ADAPTER<C, A, R>(func, obj);
121 template <typename C, typename A, typename R>
122 CONST_METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A) const, C & obj)
124 return CONST_METHOD1_ADAPTER<C, A, R>(func, obj);
127 template <typename T>
128 bool SetArrayItem(T & array, const char * index, const typename ARRAY_TYPE<T>::type & value)
131 if (str2x(index, pos))
139 void UsageImpl(bool full);
140 void UsageConnection();
141 void UsageAdmins(bool full);
142 void UsageTariffs(bool full);
143 void UsageUsers(bool full);
144 void UsageServices(bool full);
145 void UsageCorporations(bool full);
149 } // namespace anonymous
154 class CONFIG_ACTION : public ACTION
157 CONFIG_ACTION(CONFIG & config,
158 const std::string & paramDescription)
160 m_description(paramDescription)
163 virtual ACTION * Clone() const { return new CONFIG_ACTION(*this); }
165 virtual std::string ParamDescription() const { return m_description; }
166 virtual std::string DefaultDescription() const { return ""; }
167 virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
168 virtual PARSER_STATE Parse(int argc, char ** argv);
172 std::string m_description;
173 OPTION_BLOCK m_suboptions;
175 void ParseCredentials(const std::string & credentials);
176 void ParseHostAndPort(const std::string & hostAndPort);
179 PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv)
181 char * pos = strchr(*argv, '@');
184 ParseCredentials(std::string(*argv, pos));
185 ParseHostAndPort(std::string(pos + 1));
189 ParseHostAndPort(std::string(*argv));
191 return PARSER_STATE(false, --argc, ++argv);
194 void CONFIG_ACTION::ParseCredentials(const std::string & credentials)
196 std::string::size_type pos = credentials.find_first_of(':');
197 if (pos != std::string::npos)
199 m_config.userName = credentials.substr(0, pos);
200 m_config.userPass = credentials.substr(pos + 1);
204 m_config.userName = credentials;
208 void CONFIG_ACTION::ParseHostAndPort(const std::string & hostAndPort)
210 std::string::size_type pos = hostAndPort.find_first_of(':');
211 if (pos != std::string::npos)
213 m_config.server = hostAndPort.substr(0, pos);
215 if (str2x(hostAndPort.substr(pos + 1), port))
216 throw ERROR("Invalid port value: '" + hostAndPort.substr(pos + 1) + "'");
217 m_config.port = port;
221 m_config.server = hostAndPort;
226 CONFIG_ACTION * MakeParamAction(CONFIG & config,
227 const std::string & paramDescription)
229 return new CONFIG_ACTION(config, paramDescription);
232 } // namespace SGCONF
236 struct option long_options_get[] = {
237 {"server", 1, 0, 's'}, //Server
238 {"port", 1, 0, 'p'}, //Port
239 {"admin", 1, 0, 'a'}, //Admin
240 {"admin_pass", 1, 0, 'w'}, //passWord
241 {"user", 1, 0, 'u'}, //User
242 {"addcash", 0, 0, 'c'}, //Add Cash
243 //{"setcash", 0, 0, 'v'}, //Set Cash
244 {"credit", 0, 0, 'r'}, //cRedit
245 {"tariff", 0, 0, 't'}, //Tariff
246 {"message", 0, 0, 'm'}, //message
247 {"password", 0, 0, 'o'}, //password
248 {"down", 0, 0, 'd'}, //down
249 {"passive", 0, 0, 'i'}, //passive
250 {"disable-stat",0, 0, 'S'}, //disable detail stat
251 {"always-online",0, 0, 'O'}, //always online
252 {"session-upload", 1, 0, 500}, //SU0
253 {"session-download", 1, 0, 501}, //SD0
254 {"month-upload", 1, 0, 502}, //MU0
255 {"month-download", 1, 0, 503}, //MD0
257 {"user-data", 1, 0, 700}, //UserData0
259 {"prepaid", 0, 0, 'e'}, //prepaid traff
260 {"create", 0, 0, 'n'}, //create
261 {"delete", 0, 0, 'l'}, //delete
263 {"note", 0, 0, 'N'}, //Note
264 {"name", 0, 0, 'A'}, //nAme
265 {"address", 0, 0, 'D'}, //aDdress
266 {"email", 0, 0, 'L'}, //emaiL
267 {"phone", 0, 0, 'P'}, //phone
268 {"group", 0, 0, 'G'}, //Group
269 {"ip", 0, 0, 'I'}, //IP-address of user
270 {"authorized-by",0, 0, 800}, //always online
274 struct option long_options_set[] = {
275 {"server", 1, 0, 's'}, //Server
276 {"port", 1, 0, 'p'}, //Port
277 {"admin", 1, 0, 'a'}, //Admin
278 {"admin_pass", 1, 0, 'w'}, //passWord
279 {"user", 1, 0, 'u'}, //User
280 {"addcash", 1, 0, 'c'}, //Add Cash
281 {"setcash", 1, 0, 'v'}, //Set Cash
282 {"credit", 1, 0, 'r'}, //cRedit
283 {"tariff", 1, 0, 't'}, //Tariff
284 {"message", 1, 0, 'm'}, //message
285 {"password", 1, 0, 'o'}, //password
286 {"down", 1, 0, 'd'}, //down
287 {"passive", 1, 0, 'i'}, //passive
288 {"disable-stat",1, 0, 'S'}, //disable detail stat
289 {"always-online",1, 0, 'O'}, //always online
290 {"session-upload", 1, 0, 500}, //U0
291 {"session-download", 1, 0, 501}, //U1
292 {"month-upload", 1, 0, 502}, //U2
293 {"month-download", 1, 0, 503}, //U3
295 {"user-data", 1, 0, 700}, //UserData
297 {"prepaid", 1, 0, 'e'}, //prepaid traff
298 {"create", 1, 0, 'n'}, //create
299 {"delete", 1, 0, 'l'}, //delete
301 {"note", 1, 0, 'N'}, //Note
302 {"name", 1, 0, 'A'}, //nAme
303 {"address", 1, 0, 'D'}, //aDdress
304 {"email", 1, 0, 'L'}, //emaiL
305 {"phone", 1, 0, 'P'}, //phone
306 {"group", 1, 0, 'G'}, //Group
307 {"ip", 0, 0, 'I'}, //IP-address of user
311 //-----------------------------------------------------------------------------
312 CASH_INFO ParseCash(const char * str)
314 //-c 123.45:log message
315 std::string cashString;
317 const char * pos = strchr(str, ':');
320 cashString.append(str, pos);
321 message.append(pos + 1);
327 if (strtodouble2(cashString, cash) != 0)
329 printf("Incorrect cash value %s\n", str);
330 exit(PARAMETER_PARSING_ERR_CODE);
333 return CASH_INFO(cash, message);
335 //-----------------------------------------------------------------------------
336 double ParseCredit(const char * c)
339 if (strtodouble2(c, credit) != 0)
341 printf("Incorrect credit value %s\n", c);
342 exit(PARAMETER_PARSING_ERR_CODE);
347 //-----------------------------------------------------------------------------
348 double ParsePrepaidTraffic(const char * c)
351 if (strtodouble2(c, credit) != 0)
353 printf("Incorrect prepaid traffic value %s\n", c);
354 exit(PARAMETER_PARSING_ERR_CODE);
359 //-----------------------------------------------------------------------------
360 int64_t ParseTraff(const char * c)
363 if (str2x(c, traff) != 0)
365 printf("Incorrect credit value %s\n", c);
366 exit(PARAMETER_PARSING_ERR_CODE);
371 //-----------------------------------------------------------------------------
372 bool ParseDownPassive(const char * dp)
374 if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0')))
376 printf("Incorrect value %s\n", dp);
377 exit(PARAMETER_PARSING_ERR_CODE);
382 //-----------------------------------------------------------------------------
383 void ParseTariff(const char * str, RESETABLE<std::string> & tariffName, RESETABLE<std::string> & nextTariff)
385 const char * pos = strchr(str, ':');
388 std::string tariff(str, pos);
389 if (strcmp(pos + 1, "now") == 0)
391 else if (strcmp(pos + 1, "delayed") == 0)
395 printf("Incorrect tariff value '%s'. Should be '<tariff>', '<tariff>:now' or '<tariff>:delayed'.\n", str);
396 exit(PARAMETER_PARSING_ERR_CODE);
402 //-----------------------------------------------------------------------------
403 time_t ParseCreditExpire(const char * str)
405 struct tm brokenTime;
407 brokenTime.tm_wday = 0;
408 brokenTime.tm_yday = 0;
409 brokenTime.tm_isdst = 0;
410 brokenTime.tm_hour = 0;
411 brokenTime.tm_min = 0;
412 brokenTime.tm_sec = 0;
414 stg_strptime(str, "%Y-%m-%d", &brokenTime);
416 return stg_timegm(&brokenTime);
418 //-----------------------------------------------------------------------------
419 void ParseAnyString(const char * c, string * msg, const char * enc)
422 char * ob = new char[strlen(c) + 1];
423 char * ib = new char[strlen(c) + 1];
430 setlocale(LC_ALL, "");
433 strncpy(charsetF, nl_langinfo(CODESET), 255);
435 const char * charsetT = enc;
439 size_t insize = strlen(ib);
440 size_t outsize = strlen(ib);
444 cd = iconv_open(charsetT, charsetF);
445 if (cd == (iconv_t) -1)
449 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
454 printf("error iconv_open\n");
456 exit(ICONV_ERR_CODE);
459 #if defined(FREE_BSD) || defined(FREE_BSD5)
460 nconv = iconv (cd, (const char**)&inbuf, &insize, &outbuf, &outsize);
462 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
464 //printf("nconv=%d outsize=%d\n", nconv, outsize);
465 if (nconv == (size_t) -1)
469 printf("iconv error\n");
470 exit(ICONV_ERR_CODE);
482 //-----------------------------------------------------------------------------
483 void CreateRequestSet(REQUEST * req, char * r)
485 const int strLen = 10024;
487 memset(str, 0, strLen);
491 if (!req->usrMsg.empty())
494 Encode12str(msg, req->usrMsg.data());
495 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());
496 //sprintf(str, "<message login=\"%s\" priority=\"0\" text=\"%s\"/>\n", req->login, msg);
503 sprintf(str, "<DelUser login=\"%s\"/>", req->login.const_data().c_str());
511 sprintf(str, "<AddUser> <login value=\"%s\"/> </AddUser>", req->login.const_data().c_str());
517 strcat(r, "<SetUser>\n");
518 sprintf(str, "<login value=\"%s\"/>\n", req->login.const_data().c_str());
520 if (!req->credit.empty())
522 sprintf(str, "<credit value=\"%f\"/>\n", req->credit.const_data());
526 if (!req->creditExpire.empty())
528 sprintf(str, "<creditExpire value=\"%ld\"/>\n", req->creditExpire.const_data());
532 if (!req->prepaidTraff.empty())
534 sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
538 if (!req->cash.empty())
541 Encode12str(msg, req->message);
542 sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg.c_str());
546 if (!req->setCash.empty())
549 Encode12str(msg, req->message);
550 sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg.c_str());
554 if (!req->usrPasswd.empty())
556 sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
560 if (!req->down.empty())
562 sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
566 if (!req->passive.empty())
568 sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
572 if (!req->disableDetailStat.empty())
574 sprintf(str, "<disableDetailStat value=\"%d\" />\n", req->disableDetailStat.const_data());
578 if (!req->alwaysOnline.empty())
580 sprintf(str, "<aonline value=\"%d\" />\n", req->alwaysOnline.const_data());
584 // IP-address of user
585 if (!req->ips.empty())
587 sprintf(str, "<ip value=\"%s\" />\n", req->ips.const_data().c_str());
591 int uPresent = false;
592 int dPresent = false;
593 for (int i = 0; i < DIR_NUM; i++)
595 if (!req->monthUpload[i].empty())
597 if (!uPresent && !dPresent)
599 sprintf(str, "<traff ");
605 ss << req->monthUpload[i].const_data();
606 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
607 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
610 if (!req->monthDownload[i].empty())
612 if (!uPresent && !dPresent)
614 sprintf(str, "<traff ");
620 ss << req->monthDownload[i].const_data();
621 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
624 if (!req->sessionUpload[i].empty())
626 if (!uPresent && !dPresent)
628 sprintf(str, "<traff ");
634 ss << req->sessionUpload[i].const_data();
635 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
636 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
639 if (!req->sessionDownload[i].empty())
641 if (!uPresent && !dPresent)
643 sprintf(str, "<traff ");
649 ss << req->sessionDownload[i].const_data();
650 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
654 if (uPresent || dPresent)
661 if (!req->tariff.empty())
663 switch (req->chgTariff)
666 sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
670 sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
674 sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
681 if (!req->note.empty())
684 Encode12str(note, req->note.data());
685 sprintf(str, "<note value=\"%s\"/>", note.c_str());
689 if (!req->name.empty())
692 Encode12str(name, req->name.data());
693 sprintf(str, "<name value=\"%s\"/>", name.c_str());
697 if (!req->address.empty())
700 Encode12str(address, req->address.data());
701 sprintf(str, "<address value=\"%s\"/>", address.c_str());
705 if (!req->email.empty())
708 Encode12str(email, req->email.data());
709 sprintf(str, "<email value=\"%s\"/>", email.c_str());
713 if (!req->phone.empty())
716 Encode12str(phone, req->phone.data());
717 sprintf(str, "<phone value=\"%s\"/>", phone.c_str());
721 if (!req->group.empty())
724 Encode12str(group, req->group.data());
725 sprintf(str, "<group value=\"%s\"/>", group.c_str());
729 for (int i = 0; i < USERDATA_NUM; i++)
731 if (!req->userData[i].empty())
734 Encode12str(ud, req->userData[i].data());
735 sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
740 strcat(r, "</SetUser>\n");
742 //-----------------------------------------------------------------------------
743 int CheckParameters(REQUEST * req)
750 bool a = !req->admLogin.empty()
751 && !req->admPasswd.empty()
752 && !req->server.empty()
753 && !req->port.empty()
754 && !req->login.empty();
756 bool b = !req->cash.empty()
757 || !req->setCash.empty()
758 || !req->credit.empty()
759 || !req->prepaidTraff.empty()
760 || !req->tariff.empty()
761 || !req->usrMsg.empty()
762 || !req->usrPasswd.empty()
764 || !req->note.empty()
765 || !req->name.empty()
766 || !req->address.empty()
767 || !req->email.empty()
768 || !req->phone.empty()
769 || !req->group.empty()
770 || !req->ips.empty() // IP-address of user
776 for (int i = 0; i < DIR_NUM; i++)
778 if (req->sessionUpload[i].empty())
785 for (int i = 0; i < DIR_NUM; i++)
787 if (req->sessionDownload[i].empty())
794 for (int i = 0; i < DIR_NUM; i++)
796 if (req->monthUpload[i].empty())
803 for (int i = 0; i < DIR_NUM; i++)
805 if (req->monthDownload[i].empty())
812 for (int i = 0; i < DIR_NUM; i++)
814 if (req->userData[i].empty())
822 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
823 return a && (b || su || sd || mu || md || ud);
825 //-----------------------------------------------------------------------------
826 int CheckParametersGet(REQUEST * req)
828 return CheckParameters(req);
830 //-----------------------------------------------------------------------------
831 int CheckParametersSet(REQUEST * req)
833 return CheckParameters(req);
835 //-----------------------------------------------------------------------------
836 bool mainGet(int argc, char **argv)
840 RESETABLE<string> t1;
841 int missedOptionArg = false;
843 const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISOE";
844 int option_index = -1;
849 c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
860 req.port = ParseServerPort(optarg);
865 req.admLogin = ParseAdminLogin(optarg);
868 case 'w': //admin password
869 req.admPasswd = ParsePassword(optarg);
872 case 'o': //change user password
877 req.login = ParseUser(optarg);
888 case 'E': //credit expire
889 req.creditExpire = 1;
904 case 'e': //Prepaid Traffic
905 req.prepaidTraff = 1;
932 case 'I': //IP-address of user
936 case 'S': //Detail stat status
937 req.disableDetailStat = " ";
940 case 'O': //Always online status
941 req.alwaysOnline = " ";
945 SetArrayItem(req.sessionUpload, optarg, 1);
946 //req.sessionUpload[optarg] = 1;
949 SetArrayItem(req.sessionDownload, optarg, 1);
950 //req.sessionDownload[optarg] = 1;
953 SetArrayItem(req.monthUpload, optarg, 1);
954 //req.monthUpload[optarg] = 1;
957 SetArrayItem(req.monthDownload, optarg, 1);
958 //req.monthDownload[optarg] = 1;
962 SetArrayItem(req.userData, optarg, std::string(" "));
963 //req.userData[optarg] = " ";
972 missedOptionArg = true;
976 printf ("?? getopt returned character code 0%o ??\n", c);
982 printf ("non-option ARGV-elements: ");
983 while (optind < argc)
984 printf ("%s ", argv[optind++]);
986 exit(PARAMETER_PARSING_ERR_CODE);
989 if (missedOptionArg || !CheckParametersGet(&req))
991 //printf("Parameter needed\n");
993 exit(PARAMETER_PARSING_ERR_CODE);
997 return ProcessAuthBy(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data());
999 return ProcessGetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req);
1001 //-----------------------------------------------------------------------------
1002 bool mainSet(int argc, char **argv)
1007 bool isMessage = false;
1010 RESETABLE<string> t1;
1012 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:";
1014 int missedOptionArg = false;
1020 int option_index = -1;
1022 c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
1030 req.server = optarg;
1034 req.port = ParseServerPort(optarg);
1039 req.admLogin = ParseAdminLogin(optarg);
1042 case 'w': //admin password
1043 req.admPasswd = ParsePassword(optarg);
1046 case 'o': //change user password
1047 conf.password = ParsePassword(optarg);
1051 req.login = ParseUser(optarg);
1054 case 'c': //add cash
1055 stat.cashAdd = ParseCash(optarg);
1058 case 'v': //set cash
1059 stat.cashSet = ParseCash(optarg);
1063 conf.credit = ParseCredit(optarg);
1066 case 'E': //credit expire
1067 conf.creditExpire = ParseCreditExpire(optarg);
1071 conf.disabled = ParseDownPassive(optarg);
1075 conf.passive = ParseDownPassive(optarg);
1079 ParseTariff(optarg, conf.tariffName, conf.nextTariff);
1083 ParseAnyString(optarg, &str);
1088 case 'e': //Prepaid Traffic
1089 stat.freeMb = ParsePrepaidTraffic(optarg);
1092 case 'n': //Create User
1093 req.createUser = true;
1096 case 'l': //Delete User
1097 req.deleteUser = true;
1101 ParseAnyString(optarg, &str, "koi8-ru");
1106 ParseAnyString(optarg, &str, "koi8-ru");
1107 conf.realName = str;
1111 ParseAnyString(optarg, &str, "koi8-ru");
1116 ParseAnyString(optarg, &str, "koi8-ru");
1121 ParseAnyString(optarg, &str);
1126 ParseAnyString(optarg, &str, "koi8-ru");
1130 case 'I': //IP-address of user
1131 ParseAnyString(optarg, &str);
1132 conf.ips = StrToIPS(str);
1136 conf.disabledDetailStat = ParseDownPassive(optarg);
1140 conf.alwaysOnline = ParseDownPassive(optarg);
1144 SetArrayItem(stat.sessionUp, optarg, ParseTraff(argv[optind++]));
1147 SetArrayItem(stat.sessionDown, optarg, ParseTraff(argv[optind++]));
1150 SetArrayItem(stat.monthUp, optarg, ParseTraff(argv[optind++]));
1153 SetArrayItem(stat.monthDown, optarg, ParseTraff(argv[optind++]));
1156 case 700: //UserData
1157 ParseAnyString(argv[optind++], &str);
1158 SetArrayItem(conf.userdata, optarg, str);
1162 missedOptionArg = true;
1166 missedOptionArg = true;
1170 printf("?? getopt returned character code 0%o ??\n", c);
1176 printf ("non-option ARGV-elements: ");
1177 while (optind < argc)
1178 printf ("%s ", argv[optind++]);
1180 exit(PARAMETER_PARSING_ERR_CODE);
1183 if (missedOptionArg || !CheckParametersSet(&req))
1185 //printf("Parameter needed\n");
1187 exit(PARAMETER_PARSING_ERR_CODE);
1190 const int rLen = 20000;
1192 memset(rstr, 0, rLen);
1195 return ProcessSendMessage(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req.usrMsg.data());
1197 return ProcessSetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), conf, stat);
1199 //-----------------------------------------------------------------------------
1200 int main(int argc, char **argv)
1202 SGCONF::CONFIG config;
1204 SGCONF::OPTION_BLOCKS blocks;
1205 blocks.Add("General options")
1206 .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), "<config file>"), "override default config file")
1207 .Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit")
1208 .Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit")
1209 .Add("v", "version", SGCONF::MakeFunc0Action(Version), "\t\tshow version information and exit");
1210 blocks.Add("Connection options")
1211 .Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "<address>"), "\t\thost to connect")
1212 .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "\t\tport to connect")
1213 .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
1214 .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "\tpassword for the administrative login")
1215 .Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
1217 SGCONF::PARSER_STATE state(blocks.Parse(--argc, ++argv)); // Skipping self name
1224 std::cerr << "Unknown option: '" << *state.argv << "'\n";
1239 exit(PARAMETER_PARSING_ERR_CODE);
1242 if (strcmp(argv[1], "get") == 0)
1245 return mainGet(argc - 1, argv + 1);
1247 else if (strcmp(argv[1], "set") == 0)
1250 if (mainSet(argc - 1, argv + 1) )
1257 exit(PARAMETER_PARSING_ERR_CODE);
1259 return UNKNOWN_ERR_CODE;
1261 //-----------------------------------------------------------------------------
1276 void UsageImpl(bool full)
1278 std::cout << "sgconf is the Stargazer management utility.\n\n"
1280 << "\tsgconf [options]\n\n"
1281 << "General options:\n"
1282 << "\t-c, --config <config file>\t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n"
1283 << "\t-h, --help\t\t\t\tshow this help and exit\n"
1284 << "\t--help-all\t\t\t\tshow full help and exit\n"
1285 << "\t-v, --version\t\t\t\tshow version information and exit\n\n";
1290 UsageServices(full);
1291 UsageCorporations(full);
1293 //-----------------------------------------------------------------------------
1294 void UsageConnection()
1296 std::cout << "Connection options:\n"
1297 << "\t-s, --server <address>\t\t\thost to connect (ip or domain name, default: \"localhost\")\n"
1298 << "\t-p, --port <port>\t\t\tport to connect (default: \"5555\")\n"
1299 << "\t-u, --username <username>\t\tadministrative login (default: \"admin\")\n"
1300 << "\t-w, --userpass <password>\t\tpassword for administrative login\n"
1301 << "\t-a, --address <connection string>\tconnection params as a single string in format: <login>:<password>@<host>:<port>\n\n";
1303 //-----------------------------------------------------------------------------
1304 void UsageAdmins(bool full)
1306 std::cout << "Admins management options:\n"
1307 << "\t--get-admins\t\t\t\tget a list of admins (subsequent options will define what to show)\n";
1309 std::cout << "\t\t--login\t\t\t\tshow admin's login\n"
1310 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1311 std::cout << "\t--get-admin\t\t\t\tget the information about admin\n";
1313 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to show\n"
1314 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1315 std::cout << "\t--add-admin\t\t\t\tadd a new admin\n";
1317 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to add\n"
1318 << "\t\t--password <password>\t\tpassword of the admin to add\n"
1319 << "\t\t--priv <priv number>\t\tpriviledges of the admin to add\n\n";
1320 std::cout << "\t--del-admin\t\t\t\tdelete an existing admin\n";
1322 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to delete\n\n";
1323 std::cout << "\t--chg-admin\t\t\t\tchange an existing admin\n";
1325 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to change\n"
1326 << "\t\t--priv <priv number>\t\tnew priviledges\n\n";
1328 //-----------------------------------------------------------------------------
1329 void UsageTariffs(bool full)
1331 std::cout << "Tariffs management options:\n"
1332 << "\t--get-tariffs\t\t\t\tget a list of tariffs (subsequent options will define what to show)\n";
1334 std::cout << "\t\t--name\t\t\t\tshow tariff's name\n"
1335 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1336 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1337 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1338 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1339 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1340 std::cout << "\t--get-tariff\t\t\t\tget the information about tariff\n";
1342 std::cout << "\t\t--name <name>\t\t\tname of the tariff to show\n"
1343 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1344 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1345 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1346 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1347 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1348 std::cout << "\t--add-tariff\t\t\t\tadd a new tariff\n";
1350 std::cout << "\t\t--name <name>\t\t\tname of the tariff to add\n"
1351 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1352 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1353 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1354 << "\t\t--traff-type <type>\t\twhat type of traffi will be accounted by the tariff\n"
1355 << "\t\t--times <times>\t\t\tslash-separated list of \"day\" time-spans (in form \"hh:mm-hh:mm\") for each direction\n"
1356 << "\t\t--prices-day-a <prices>\t\tslash-separated list of prices for \"day\" traffic before threshold for each direction\n"
1357 << "\t\t--prices-night-a <prices>\tslash-separated list of prices for \"night\" traffic before threshold for each direction\n"
1358 << "\t\t--prices-day-b <prices>\t\tslash-separated list of prices for \"day\" traffic after threshold for each direction\n"
1359 << "\t\t--prices-night-b <prices>\tslash-separated list of prices for \"night\" traffic after threshold for each direction\n"
1360 << "\t\t--single-prices <yes|no>\tslash-separated list of \"single price\" flags for each direction\n"
1361 << "\t\t--no-discounts <yes|no>\t\tslash-separated list of \"no discount\" flags for each direction\n"
1362 << "\t\t--thresholds <thresholds>\tslash-separated list of thresholds (in Mb) for each direction\n\n";
1363 std::cout << "\t--del-tariff\t\t\t\tdelete an existing tariff\n";
1365 std::cout << "\t\t--name <name>\t\t\tname of the tariff to delete\n\n";
1366 std::cout << "\t--chg-tariff\t\t\t\tchange an existing tariff\n";
1368 std::cout << "\t\t--name <name>\t\t\tname of the tariff to change\n"
1369 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1370 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1371 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1372 << "\t\t--traff-type <type>\t\twhat type of traffix will be accounted by the tariff\n"
1373 << "\t\t--dir <N>\t\t\tnumber of direction data to change\n"
1374 << "\t\t\t--time <time>\t\t\"day\" time-span (in form \"hh:mm-hh:mm\")\n"
1375 << "\t\t\t--price-day-a <price>\tprice for \"day\" traffic before threshold\n"
1376 << "\t\t\t--price-night-a <price>\tprice for \"night\" traffic before threshold\n"
1377 << "\t\t\t--price-day-b <price>\tprice for \"day\" traffic after threshold\n"
1378 << "\t\t\t--price-night-b <price>\tprice for \"night\" traffic after threshold\n"
1379 << "\t\t\t--single-price <yes|no>\t\"single price\" flag\n"
1380 << "\t\t\t--no-discount <yes|no>\t\"no discount\" flag\n"
1381 << "\t\t\t--threshold <threshold>\tthreshold (in Mb)\n\n";
1383 //-----------------------------------------------------------------------------
1384 void UsageUsers(bool full)
1386 std::cout << "Users management options:\n"
1387 << "\t--get-users\t\t\t\tget a list of users (subsequent options will define what to show)\n";
1389 std::cout << "\n\n";
1390 std::cout << "\t--get-user\t\t\t\tget the information about user\n";
1392 std::cout << "\n\n";
1393 std::cout << "\t--add-user\t\t\t\tadd a new user\n";
1395 std::cout << "\n\n";
1396 std::cout << "\t--del-user\t\t\t\tdelete an existing user\n";
1398 std::cout << "\n\n";
1399 std::cout << "\t--chg-user\t\t\t\tchange an existing user\n";
1401 std::cout << "\n\n";
1402 std::cout << "\t--check-user\t\t\t\tcheck credentials is valid\n";
1404 std::cout << "\n\n";
1405 std::cout << "\t--send-message\t\t\t\tsend a message to a user\n";
1407 std::cout << "\n\n";
1409 //-----------------------------------------------------------------------------
1410 void UsageServices(bool full)
1412 std::cout << "Services management options:\n"
1413 << "\t--get-services\t\t\t\tget a list of services (subsequent options will define what to show)\n";
1415 std::cout << "\t\t--name\t\t\t\tshow service's name\n"
1416 << "\t\t--comment\t\t\tshow a comment to the service\n"
1417 << "\t\t--cost\t\t\t\tshow service's cost\n"
1418 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1419 std::cout << "\t--get-service\t\t\t\tget the information about service\n";
1421 std::cout << "\t\t--name <name>\t\t\tname of the service to show\n"
1422 << "\t\t--comment\t\t\tshow a comment to the service\n"
1423 << "\t\t--cost\t\t\t\tshow service's cost\n"
1424 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1425 std::cout << "\t--add-service\t\t\t\tadd a new service\n";
1427 std::cout << "\t\t--name <name>\t\t\tname of the service to add\n"
1428 << "\t\t--comment <comment>\t\ta comment to the service\n"
1429 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1430 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1431 std::cout << "\t--del-service\t\t\t\tdelete an existing service\n";
1433 std::cout << "\t\t--name <name>\t\t\tname of the service to delete\n\n";
1434 std::cout << "\t--chg-service\t\t\t\tchange an existing service\n";
1436 std::cout << "\t\t--name <name>\t\t\tname of the service to change\n"
1437 << "\t\t--comment <comment>\t\ta comment to the service\n"
1438 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1439 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1441 //-----------------------------------------------------------------------------
1442 void UsageCorporations(bool full)
1444 std::cout << "Corporations management options:\n"
1445 << "\t--get-corporations\t\t\tget a list of corporations (subsequent options will define what to show)\n";
1447 std::cout << "\t\t--name\t\t\t\tshow corporation's name\n"
1448 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1449 std::cout << "\t--get-corp\t\t\t\tget the information about corporation\n";
1451 std::cout << "\t\t--name <name>\t\t\tname of the corporation to show\n"
1452 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1453 std::cout << "\t--add-corp\t\t\t\tadd a new corporation\n";
1455 std::cout << "\t\t--name <name>\t\t\tname of the corporation to add\n"
1456 << "\t\t--cash <cash>\t\t\tinitial corporation's cash (default: \"0\")\n\n";
1457 std::cout << "\t--del-corp\t\t\t\tdelete an existing corporation\n";
1459 std::cout << "\t\t--name <name>\t\t\tname of the corporation to delete\n\n";
1460 std::cout << "\t--chg-corp\t\t\t\tchange an existing corporation\n";
1462 std::cout << "\t\t--name <name>\t\t\tname of the corporation to change\n"
1463 << "\t\t--add-cash <amount>[:<message>]\tadd cash to the corporation's account and optional comment message\n"
1464 << "\t\t--set-cash <cash>[:<message>]\tnew corporation's cash and optional comment message\n\n";
1469 std::cout << "sgconf, version: 2.0.0-alpha.\n";
1472 } // namespace anonymous