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)
184 throw ERROR("Missing argument.");
185 char * pos = strchr(*argv, '@');
188 ParseCredentials(std::string(*argv, pos));
189 ParseHostAndPort(std::string(pos + 1));
193 ParseHostAndPort(std::string(*argv));
195 return PARSER_STATE(false, --argc, ++argv);
198 void CONFIG_ACTION::ParseCredentials(const std::string & credentials)
200 std::string::size_type pos = credentials.find_first_of(':');
201 if (pos != std::string::npos)
203 m_config.userName = credentials.substr(0, pos);
204 m_config.userPass = credentials.substr(pos + 1);
208 m_config.userName = credentials;
212 void CONFIG_ACTION::ParseHostAndPort(const std::string & hostAndPort)
214 std::string::size_type pos = hostAndPort.find_first_of(':');
215 if (pos != std::string::npos)
217 m_config.server = hostAndPort.substr(0, pos);
219 if (str2x(hostAndPort.substr(pos + 1), port))
220 throw ERROR("Invalid port value: '" + hostAndPort.substr(pos + 1) + "'");
221 m_config.port = port;
225 m_config.server = hostAndPort;
230 CONFIG_ACTION * MakeParamAction(CONFIG & config,
231 const std::string & paramDescription)
233 return new CONFIG_ACTION(config, paramDescription);
236 } // namespace SGCONF
240 struct option long_options_get[] = {
241 {"server", 1, 0, 's'}, //Server
242 {"port", 1, 0, 'p'}, //Port
243 {"admin", 1, 0, 'a'}, //Admin
244 {"admin_pass", 1, 0, 'w'}, //passWord
245 {"user", 1, 0, 'u'}, //User
246 {"addcash", 0, 0, 'c'}, //Add Cash
247 //{"setcash", 0, 0, 'v'}, //Set Cash
248 {"credit", 0, 0, 'r'}, //cRedit
249 {"tariff", 0, 0, 't'}, //Tariff
250 {"message", 0, 0, 'm'}, //message
251 {"password", 0, 0, 'o'}, //password
252 {"down", 0, 0, 'd'}, //down
253 {"passive", 0, 0, 'i'}, //passive
254 {"disable-stat",0, 0, 'S'}, //disable detail stat
255 {"always-online",0, 0, 'O'}, //always online
256 {"session-upload", 1, 0, 500}, //SU0
257 {"session-download", 1, 0, 501}, //SD0
258 {"month-upload", 1, 0, 502}, //MU0
259 {"month-download", 1, 0, 503}, //MD0
261 {"user-data", 1, 0, 700}, //UserData0
263 {"prepaid", 0, 0, 'e'}, //prepaid traff
264 {"create", 0, 0, 'n'}, //create
265 {"delete", 0, 0, 'l'}, //delete
267 {"note", 0, 0, 'N'}, //Note
268 {"name", 0, 0, 'A'}, //nAme
269 {"address", 0, 0, 'D'}, //aDdress
270 {"email", 0, 0, 'L'}, //emaiL
271 {"phone", 0, 0, 'P'}, //phone
272 {"group", 0, 0, 'G'}, //Group
273 {"ip", 0, 0, 'I'}, //IP-address of user
274 {"authorized-by",0, 0, 800}, //always online
278 struct option long_options_set[] = {
279 {"server", 1, 0, 's'}, //Server
280 {"port", 1, 0, 'p'}, //Port
281 {"admin", 1, 0, 'a'}, //Admin
282 {"admin_pass", 1, 0, 'w'}, //passWord
283 {"user", 1, 0, 'u'}, //User
284 {"addcash", 1, 0, 'c'}, //Add Cash
285 {"setcash", 1, 0, 'v'}, //Set Cash
286 {"credit", 1, 0, 'r'}, //cRedit
287 {"tariff", 1, 0, 't'}, //Tariff
288 {"message", 1, 0, 'm'}, //message
289 {"password", 1, 0, 'o'}, //password
290 {"down", 1, 0, 'd'}, //down
291 {"passive", 1, 0, 'i'}, //passive
292 {"disable-stat",1, 0, 'S'}, //disable detail stat
293 {"always-online",1, 0, 'O'}, //always online
294 {"session-upload", 1, 0, 500}, //U0
295 {"session-download", 1, 0, 501}, //U1
296 {"month-upload", 1, 0, 502}, //U2
297 {"month-download", 1, 0, 503}, //U3
299 {"user-data", 1, 0, 700}, //UserData
301 {"prepaid", 1, 0, 'e'}, //prepaid traff
302 {"create", 1, 0, 'n'}, //create
303 {"delete", 1, 0, 'l'}, //delete
305 {"note", 1, 0, 'N'}, //Note
306 {"name", 1, 0, 'A'}, //nAme
307 {"address", 1, 0, 'D'}, //aDdress
308 {"email", 1, 0, 'L'}, //emaiL
309 {"phone", 1, 0, 'P'}, //phone
310 {"group", 1, 0, 'G'}, //Group
311 {"ip", 0, 0, 'I'}, //IP-address of user
315 //-----------------------------------------------------------------------------
316 CASH_INFO ParseCash(const char * str)
318 //-c 123.45:log message
319 std::string cashString;
321 const char * pos = strchr(str, ':');
324 cashString.append(str, pos);
325 message.append(pos + 1);
331 if (strtodouble2(cashString, cash) != 0)
333 printf("Incorrect cash value %s\n", str);
334 exit(PARAMETER_PARSING_ERR_CODE);
337 return CASH_INFO(cash, message);
339 //-----------------------------------------------------------------------------
340 double ParseCredit(const char * c)
343 if (strtodouble2(c, credit) != 0)
345 printf("Incorrect credit value %s\n", c);
346 exit(PARAMETER_PARSING_ERR_CODE);
351 //-----------------------------------------------------------------------------
352 double ParsePrepaidTraffic(const char * c)
355 if (strtodouble2(c, credit) != 0)
357 printf("Incorrect prepaid traffic value %s\n", c);
358 exit(PARAMETER_PARSING_ERR_CODE);
363 //-----------------------------------------------------------------------------
364 int64_t ParseTraff(const char * c)
367 if (str2x(c, traff) != 0)
369 printf("Incorrect credit value %s\n", c);
370 exit(PARAMETER_PARSING_ERR_CODE);
375 //-----------------------------------------------------------------------------
376 bool ParseDownPassive(const char * dp)
378 if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0')))
380 printf("Incorrect value %s\n", dp);
381 exit(PARAMETER_PARSING_ERR_CODE);
386 //-----------------------------------------------------------------------------
387 void ParseTariff(const char * str, RESETABLE<std::string> & tariffName, RESETABLE<std::string> & nextTariff)
389 const char * pos = strchr(str, ':');
392 std::string tariff(str, pos);
393 if (strcmp(pos + 1, "now") == 0)
395 else if (strcmp(pos + 1, "delayed") == 0)
399 printf("Incorrect tariff value '%s'. Should be '<tariff>', '<tariff>:now' or '<tariff>:delayed'.\n", str);
400 exit(PARAMETER_PARSING_ERR_CODE);
406 //-----------------------------------------------------------------------------
407 time_t ParseCreditExpire(const char * str)
409 struct tm brokenTime;
411 brokenTime.tm_wday = 0;
412 brokenTime.tm_yday = 0;
413 brokenTime.tm_isdst = 0;
414 brokenTime.tm_hour = 0;
415 brokenTime.tm_min = 0;
416 brokenTime.tm_sec = 0;
418 stg_strptime(str, "%Y-%m-%d", &brokenTime);
420 return stg_timegm(&brokenTime);
422 //-----------------------------------------------------------------------------
423 void ParseAnyString(const char * c, string * msg, const char * enc)
426 char * ob = new char[strlen(c) + 1];
427 char * ib = new char[strlen(c) + 1];
434 setlocale(LC_ALL, "");
437 strncpy(charsetF, nl_langinfo(CODESET), 255);
439 const char * charsetT = enc;
443 size_t insize = strlen(ib);
444 size_t outsize = strlen(ib);
448 cd = iconv_open(charsetT, charsetF);
449 if (cd == (iconv_t) -1)
453 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
458 printf("error iconv_open\n");
460 exit(ICONV_ERR_CODE);
463 #if defined(FREE_BSD) || defined(FREE_BSD5)
464 nconv = iconv (cd, (const char**)&inbuf, &insize, &outbuf, &outsize);
466 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
468 //printf("nconv=%d outsize=%d\n", nconv, outsize);
469 if (nconv == (size_t) -1)
473 printf("iconv error\n");
474 exit(ICONV_ERR_CODE);
486 //-----------------------------------------------------------------------------
487 void CreateRequestSet(REQUEST * req, char * r)
489 const int strLen = 10024;
491 memset(str, 0, strLen);
495 if (!req->usrMsg.empty())
498 Encode12str(msg, req->usrMsg.data());
499 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());
500 //sprintf(str, "<message login=\"%s\" priority=\"0\" text=\"%s\"/>\n", req->login, msg);
507 sprintf(str, "<DelUser login=\"%s\"/>", req->login.const_data().c_str());
515 sprintf(str, "<AddUser> <login value=\"%s\"/> </AddUser>", req->login.const_data().c_str());
521 strcat(r, "<SetUser>\n");
522 sprintf(str, "<login value=\"%s\"/>\n", req->login.const_data().c_str());
524 if (!req->credit.empty())
526 sprintf(str, "<credit value=\"%f\"/>\n", req->credit.const_data());
530 if (!req->creditExpire.empty())
532 sprintf(str, "<creditExpire value=\"%ld\"/>\n", req->creditExpire.const_data());
536 if (!req->prepaidTraff.empty())
538 sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
542 if (!req->cash.empty())
545 Encode12str(msg, req->message);
546 sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg.c_str());
550 if (!req->setCash.empty())
553 Encode12str(msg, req->message);
554 sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg.c_str());
558 if (!req->usrPasswd.empty())
560 sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
564 if (!req->down.empty())
566 sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
570 if (!req->passive.empty())
572 sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
576 if (!req->disableDetailStat.empty())
578 sprintf(str, "<disableDetailStat value=\"%d\" />\n", req->disableDetailStat.const_data());
582 if (!req->alwaysOnline.empty())
584 sprintf(str, "<aonline value=\"%d\" />\n", req->alwaysOnline.const_data());
588 // IP-address of user
589 if (!req->ips.empty())
591 sprintf(str, "<ip value=\"%s\" />\n", req->ips.const_data().c_str());
595 int uPresent = false;
596 int dPresent = false;
597 for (int i = 0; i < DIR_NUM; i++)
599 if (!req->monthUpload[i].empty())
601 if (!uPresent && !dPresent)
603 sprintf(str, "<traff ");
609 ss << req->monthUpload[i].const_data();
610 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
611 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
614 if (!req->monthDownload[i].empty())
616 if (!uPresent && !dPresent)
618 sprintf(str, "<traff ");
624 ss << req->monthDownload[i].const_data();
625 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
628 if (!req->sessionUpload[i].empty())
630 if (!uPresent && !dPresent)
632 sprintf(str, "<traff ");
638 ss << req->sessionUpload[i].const_data();
639 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
640 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
643 if (!req->sessionDownload[i].empty())
645 if (!uPresent && !dPresent)
647 sprintf(str, "<traff ");
653 ss << req->sessionDownload[i].const_data();
654 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
658 if (uPresent || dPresent)
665 if (!req->tariff.empty())
667 switch (req->chgTariff)
670 sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
674 sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
678 sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
685 if (!req->note.empty())
688 Encode12str(note, req->note.data());
689 sprintf(str, "<note value=\"%s\"/>", note.c_str());
693 if (!req->name.empty())
696 Encode12str(name, req->name.data());
697 sprintf(str, "<name value=\"%s\"/>", name.c_str());
701 if (!req->address.empty())
704 Encode12str(address, req->address.data());
705 sprintf(str, "<address value=\"%s\"/>", address.c_str());
709 if (!req->email.empty())
712 Encode12str(email, req->email.data());
713 sprintf(str, "<email value=\"%s\"/>", email.c_str());
717 if (!req->phone.empty())
720 Encode12str(phone, req->phone.data());
721 sprintf(str, "<phone value=\"%s\"/>", phone.c_str());
725 if (!req->group.empty())
728 Encode12str(group, req->group.data());
729 sprintf(str, "<group value=\"%s\"/>", group.c_str());
733 for (int i = 0; i < USERDATA_NUM; i++)
735 if (!req->userData[i].empty())
738 Encode12str(ud, req->userData[i].data());
739 sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
744 strcat(r, "</SetUser>\n");
746 //-----------------------------------------------------------------------------
747 int CheckParameters(REQUEST * req)
754 bool a = !req->admLogin.empty()
755 && !req->admPasswd.empty()
756 && !req->server.empty()
757 && !req->port.empty()
758 && !req->login.empty();
760 bool b = !req->cash.empty()
761 || !req->setCash.empty()
762 || !req->credit.empty()
763 || !req->prepaidTraff.empty()
764 || !req->tariff.empty()
765 || !req->usrMsg.empty()
766 || !req->usrPasswd.empty()
768 || !req->note.empty()
769 || !req->name.empty()
770 || !req->address.empty()
771 || !req->email.empty()
772 || !req->phone.empty()
773 || !req->group.empty()
774 || !req->ips.empty() // IP-address of user
780 for (int i = 0; i < DIR_NUM; i++)
782 if (req->sessionUpload[i].empty())
789 for (int i = 0; i < DIR_NUM; i++)
791 if (req->sessionDownload[i].empty())
798 for (int i = 0; i < DIR_NUM; i++)
800 if (req->monthUpload[i].empty())
807 for (int i = 0; i < DIR_NUM; i++)
809 if (req->monthDownload[i].empty())
816 for (int i = 0; i < DIR_NUM; i++)
818 if (req->userData[i].empty())
826 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
827 return a && (b || su || sd || mu || md || ud);
829 //-----------------------------------------------------------------------------
830 int CheckParametersGet(REQUEST * req)
832 return CheckParameters(req);
834 //-----------------------------------------------------------------------------
835 int CheckParametersSet(REQUEST * req)
837 return CheckParameters(req);
839 //-----------------------------------------------------------------------------
840 bool mainGet(int argc, char **argv)
844 RESETABLE<string> t1;
845 int missedOptionArg = false;
847 const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISOE";
848 int option_index = -1;
853 c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
864 req.port = ParseServerPort(optarg);
869 req.admLogin = ParseAdminLogin(optarg);
872 case 'w': //admin password
873 req.admPasswd = ParsePassword(optarg);
876 case 'o': //change user password
881 req.login = ParseUser(optarg);
892 case 'E': //credit expire
893 req.creditExpire = 1;
908 case 'e': //Prepaid Traffic
909 req.prepaidTraff = 1;
936 case 'I': //IP-address of user
940 case 'S': //Detail stat status
941 req.disableDetailStat = " ";
944 case 'O': //Always online status
945 req.alwaysOnline = " ";
949 SetArrayItem(req.sessionUpload, optarg, 1);
950 //req.sessionUpload[optarg] = 1;
953 SetArrayItem(req.sessionDownload, optarg, 1);
954 //req.sessionDownload[optarg] = 1;
957 SetArrayItem(req.monthUpload, optarg, 1);
958 //req.monthUpload[optarg] = 1;
961 SetArrayItem(req.monthDownload, optarg, 1);
962 //req.monthDownload[optarg] = 1;
966 SetArrayItem(req.userData, optarg, std::string(" "));
967 //req.userData[optarg] = " ";
976 missedOptionArg = true;
980 printf ("?? getopt returned character code 0%o ??\n", c);
986 printf ("non-option ARGV-elements: ");
987 while (optind < argc)
988 printf ("%s ", argv[optind++]);
990 exit(PARAMETER_PARSING_ERR_CODE);
993 if (missedOptionArg || !CheckParametersGet(&req))
995 //printf("Parameter needed\n");
997 exit(PARAMETER_PARSING_ERR_CODE);
1001 return ProcessAuthBy(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data());
1003 return ProcessGetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req);
1005 //-----------------------------------------------------------------------------
1006 bool mainSet(int argc, char **argv)
1011 bool isMessage = false;
1014 RESETABLE<string> t1;
1016 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:";
1018 int missedOptionArg = false;
1024 int option_index = -1;
1026 c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
1034 req.server = optarg;
1038 req.port = ParseServerPort(optarg);
1043 req.admLogin = ParseAdminLogin(optarg);
1046 case 'w': //admin password
1047 req.admPasswd = ParsePassword(optarg);
1050 case 'o': //change user password
1051 conf.password = ParsePassword(optarg);
1055 req.login = ParseUser(optarg);
1058 case 'c': //add cash
1059 stat.cashAdd = ParseCash(optarg);
1062 case 'v': //set cash
1063 stat.cashSet = ParseCash(optarg);
1067 conf.credit = ParseCredit(optarg);
1070 case 'E': //credit expire
1071 conf.creditExpire = ParseCreditExpire(optarg);
1075 conf.disabled = ParseDownPassive(optarg);
1079 conf.passive = ParseDownPassive(optarg);
1083 ParseTariff(optarg, conf.tariffName, conf.nextTariff);
1087 ParseAnyString(optarg, &str);
1092 case 'e': //Prepaid Traffic
1093 stat.freeMb = ParsePrepaidTraffic(optarg);
1096 case 'n': //Create User
1097 req.createUser = true;
1100 case 'l': //Delete User
1101 req.deleteUser = true;
1105 ParseAnyString(optarg, &str, "koi8-ru");
1110 ParseAnyString(optarg, &str, "koi8-ru");
1111 conf.realName = str;
1115 ParseAnyString(optarg, &str, "koi8-ru");
1120 ParseAnyString(optarg, &str, "koi8-ru");
1125 ParseAnyString(optarg, &str);
1130 ParseAnyString(optarg, &str, "koi8-ru");
1134 case 'I': //IP-address of user
1135 ParseAnyString(optarg, &str);
1136 conf.ips = StrToIPS(str);
1140 conf.disabledDetailStat = ParseDownPassive(optarg);
1144 conf.alwaysOnline = ParseDownPassive(optarg);
1148 SetArrayItem(stat.sessionUp, optarg, ParseTraff(argv[optind++]));
1151 SetArrayItem(stat.sessionDown, optarg, ParseTraff(argv[optind++]));
1154 SetArrayItem(stat.monthUp, optarg, ParseTraff(argv[optind++]));
1157 SetArrayItem(stat.monthDown, optarg, ParseTraff(argv[optind++]));
1160 case 700: //UserData
1161 ParseAnyString(argv[optind++], &str);
1162 SetArrayItem(conf.userdata, optarg, str);
1166 missedOptionArg = true;
1170 missedOptionArg = true;
1174 printf("?? getopt returned character code 0%o ??\n", c);
1180 printf ("non-option ARGV-elements: ");
1181 while (optind < argc)
1182 printf ("%s ", argv[optind++]);
1184 exit(PARAMETER_PARSING_ERR_CODE);
1187 if (missedOptionArg || !CheckParametersSet(&req))
1189 //printf("Parameter needed\n");
1191 exit(PARAMETER_PARSING_ERR_CODE);
1194 const int rLen = 20000;
1196 memset(rstr, 0, rLen);
1199 return ProcessSendMessage(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req.usrMsg.data());
1201 return ProcessSetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), conf, stat);
1203 //-----------------------------------------------------------------------------
1204 int main(int argc, char **argv)
1206 SGCONF::CONFIG config;
1208 SGCONF::OPTION_BLOCKS blocks;
1209 blocks.Add("General options")
1210 .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), "<config file>"), "override default config file")
1211 .Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit")
1212 .Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit")
1213 .Add("v", "version", SGCONF::MakeFunc0Action(Version), "\t\tshow version information and exit");
1214 blocks.Add("Connection options")
1215 .Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "<address>"), "\t\thost to connect")
1216 .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "\t\tport to connect")
1217 .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
1218 .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "\tpassword for the administrative login")
1219 .Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
1222 SGCONF::PARSER_STATE state(false, argc, argv);
1226 state = blocks.Parse(--argc, ++argv); // Skipping self name
1228 catch (const SGCONF::OPTION::ERROR& ex)
1230 std::cerr << ex.what() << "\n";
1239 std::cerr << "Unknown option: '" << *state.argv << "'\n";
1254 exit(PARAMETER_PARSING_ERR_CODE);
1257 if (strcmp(argv[1], "get") == 0)
1260 return mainGet(argc - 1, argv + 1);
1262 else if (strcmp(argv[1], "set") == 0)
1265 if (mainSet(argc - 1, argv + 1) )
1272 exit(PARAMETER_PARSING_ERR_CODE);
1274 return UNKNOWN_ERR_CODE;
1276 //-----------------------------------------------------------------------------
1291 void UsageImpl(bool full)
1293 std::cout << "sgconf is the Stargazer management utility.\n\n"
1295 << "\tsgconf [options]\n\n"
1296 << "General options:\n"
1297 << "\t-c, --config <config file>\t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n"
1298 << "\t-h, --help\t\t\t\tshow this help and exit\n"
1299 << "\t--help-all\t\t\t\tshow full help and exit\n"
1300 << "\t-v, --version\t\t\t\tshow version information and exit\n\n";
1305 UsageServices(full);
1306 UsageCorporations(full);
1308 //-----------------------------------------------------------------------------
1309 void UsageConnection()
1311 std::cout << "Connection options:\n"
1312 << "\t-s, --server <address>\t\t\thost to connect (ip or domain name, default: \"localhost\")\n"
1313 << "\t-p, --port <port>\t\t\tport to connect (default: \"5555\")\n"
1314 << "\t-u, --username <username>\t\tadministrative login (default: \"admin\")\n"
1315 << "\t-w, --userpass <password>\t\tpassword for administrative login\n"
1316 << "\t-a, --address <connection string>\tconnection params as a single string in format: <login>:<password>@<host>:<port>\n\n";
1318 //-----------------------------------------------------------------------------
1319 void UsageAdmins(bool full)
1321 std::cout << "Admins management options:\n"
1322 << "\t--get-admins\t\t\t\tget a list of admins (subsequent options will define what to show)\n";
1324 std::cout << "\t\t--login\t\t\t\tshow admin's login\n"
1325 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1326 std::cout << "\t--get-admin\t\t\t\tget the information about admin\n";
1328 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to show\n"
1329 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1330 std::cout << "\t--add-admin\t\t\t\tadd a new admin\n";
1332 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to add\n"
1333 << "\t\t--password <password>\t\tpassword of the admin to add\n"
1334 << "\t\t--priv <priv number>\t\tpriviledges of the admin to add\n\n";
1335 std::cout << "\t--del-admin\t\t\t\tdelete an existing admin\n";
1337 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to delete\n\n";
1338 std::cout << "\t--chg-admin\t\t\t\tchange an existing admin\n";
1340 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to change\n"
1341 << "\t\t--priv <priv number>\t\tnew priviledges\n\n";
1343 //-----------------------------------------------------------------------------
1344 void UsageTariffs(bool full)
1346 std::cout << "Tariffs management options:\n"
1347 << "\t--get-tariffs\t\t\t\tget a list of tariffs (subsequent options will define what to show)\n";
1349 std::cout << "\t\t--name\t\t\t\tshow tariff's name\n"
1350 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1351 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1352 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1353 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1354 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1355 std::cout << "\t--get-tariff\t\t\t\tget the information about tariff\n";
1357 std::cout << "\t\t--name <name>\t\t\tname of the tariff to show\n"
1358 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1359 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1360 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1361 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1362 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1363 std::cout << "\t--add-tariff\t\t\t\tadd a new tariff\n";
1365 std::cout << "\t\t--name <name>\t\t\tname of the tariff to add\n"
1366 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1367 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1368 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1369 << "\t\t--traff-type <type>\t\twhat type of traffi will be accounted by the tariff\n"
1370 << "\t\t--times <times>\t\t\tslash-separated list of \"day\" time-spans (in form \"hh:mm-hh:mm\") for each direction\n"
1371 << "\t\t--prices-day-a <prices>\t\tslash-separated list of prices for \"day\" traffic before threshold for each direction\n"
1372 << "\t\t--prices-night-a <prices>\tslash-separated list of prices for \"night\" traffic before threshold for each direction\n"
1373 << "\t\t--prices-day-b <prices>\t\tslash-separated list of prices for \"day\" traffic after threshold for each direction\n"
1374 << "\t\t--prices-night-b <prices>\tslash-separated list of prices for \"night\" traffic after threshold for each direction\n"
1375 << "\t\t--single-prices <yes|no>\tslash-separated list of \"single price\" flags for each direction\n"
1376 << "\t\t--no-discounts <yes|no>\t\tslash-separated list of \"no discount\" flags for each direction\n"
1377 << "\t\t--thresholds <thresholds>\tslash-separated list of thresholds (in Mb) for each direction\n\n";
1378 std::cout << "\t--del-tariff\t\t\t\tdelete an existing tariff\n";
1380 std::cout << "\t\t--name <name>\t\t\tname of the tariff to delete\n\n";
1381 std::cout << "\t--chg-tariff\t\t\t\tchange an existing tariff\n";
1383 std::cout << "\t\t--name <name>\t\t\tname of the tariff to change\n"
1384 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1385 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1386 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1387 << "\t\t--traff-type <type>\t\twhat type of traffix will be accounted by the tariff\n"
1388 << "\t\t--dir <N>\t\t\tnumber of direction data to change\n"
1389 << "\t\t\t--time <time>\t\t\"day\" time-span (in form \"hh:mm-hh:mm\")\n"
1390 << "\t\t\t--price-day-a <price>\tprice for \"day\" traffic before threshold\n"
1391 << "\t\t\t--price-night-a <price>\tprice for \"night\" traffic before threshold\n"
1392 << "\t\t\t--price-day-b <price>\tprice for \"day\" traffic after threshold\n"
1393 << "\t\t\t--price-night-b <price>\tprice for \"night\" traffic after threshold\n"
1394 << "\t\t\t--single-price <yes|no>\t\"single price\" flag\n"
1395 << "\t\t\t--no-discount <yes|no>\t\"no discount\" flag\n"
1396 << "\t\t\t--threshold <threshold>\tthreshold (in Mb)\n\n";
1398 //-----------------------------------------------------------------------------
1399 void UsageUsers(bool full)
1401 std::cout << "Users management options:\n"
1402 << "\t--get-users\t\t\t\tget a list of users (subsequent options will define what to show)\n";
1404 std::cout << "\n\n";
1405 std::cout << "\t--get-user\t\t\t\tget the information about user\n";
1407 std::cout << "\n\n";
1408 std::cout << "\t--add-user\t\t\t\tadd a new user\n";
1410 std::cout << "\n\n";
1411 std::cout << "\t--del-user\t\t\t\tdelete an existing user\n";
1413 std::cout << "\n\n";
1414 std::cout << "\t--chg-user\t\t\t\tchange an existing user\n";
1416 std::cout << "\n\n";
1417 std::cout << "\t--check-user\t\t\t\tcheck credentials is valid\n";
1419 std::cout << "\n\n";
1420 std::cout << "\t--send-message\t\t\t\tsend a message to a user\n";
1422 std::cout << "\n\n";
1424 //-----------------------------------------------------------------------------
1425 void UsageServices(bool full)
1427 std::cout << "Services management options:\n"
1428 << "\t--get-services\t\t\t\tget a list of services (subsequent options will define what to show)\n";
1430 std::cout << "\t\t--name\t\t\t\tshow service's name\n"
1431 << "\t\t--comment\t\t\tshow a comment to the service\n"
1432 << "\t\t--cost\t\t\t\tshow service's cost\n"
1433 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1434 std::cout << "\t--get-service\t\t\t\tget the information about service\n";
1436 std::cout << "\t\t--name <name>\t\t\tname of the service to show\n"
1437 << "\t\t--comment\t\t\tshow a comment to the service\n"
1438 << "\t\t--cost\t\t\t\tshow service's cost\n"
1439 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1440 std::cout << "\t--add-service\t\t\t\tadd a new service\n";
1442 std::cout << "\t\t--name <name>\t\t\tname of the service to add\n"
1443 << "\t\t--comment <comment>\t\ta comment to the service\n"
1444 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1445 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1446 std::cout << "\t--del-service\t\t\t\tdelete an existing service\n";
1448 std::cout << "\t\t--name <name>\t\t\tname of the service to delete\n\n";
1449 std::cout << "\t--chg-service\t\t\t\tchange an existing service\n";
1451 std::cout << "\t\t--name <name>\t\t\tname of the service to change\n"
1452 << "\t\t--comment <comment>\t\ta comment to the service\n"
1453 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1454 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1456 //-----------------------------------------------------------------------------
1457 void UsageCorporations(bool full)
1459 std::cout << "Corporations management options:\n"
1460 << "\t--get-corporations\t\t\tget a list of corporations (subsequent options will define what to show)\n";
1462 std::cout << "\t\t--name\t\t\t\tshow corporation's name\n"
1463 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1464 std::cout << "\t--get-corp\t\t\t\tget the information about corporation\n";
1466 std::cout << "\t\t--name <name>\t\t\tname of the corporation to show\n"
1467 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1468 std::cout << "\t--add-corp\t\t\t\tadd a new corporation\n";
1470 std::cout << "\t\t--name <name>\t\t\tname of the corporation to add\n"
1471 << "\t\t--cash <cash>\t\t\tinitial corporation's cash (default: \"0\")\n\n";
1472 std::cout << "\t--del-corp\t\t\t\tdelete an existing corporation\n";
1474 std::cout << "\t\t--name <name>\t\t\tname of the corporation to delete\n\n";
1475 std::cout << "\t--chg-corp\t\t\t\tchange an existing corporation\n";
1477 std::cout << "\t\t--name <name>\t\t\tname of the corporation to change\n"
1478 << "\t\t--add-cash <amount>[:<message>]\tadd cash to the corporation's account and optional comment message\n"
1479 << "\t\t--set-cash <cash>[:<message>]\tnew corporation's cash and optional comment message\n\n";
1484 std::cout << "sgconf, version: 2.0.0-alpha.\n";
1487 } // namespace anonymous