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"
31 #include "stg/user_conf.h"
32 #include "stg/user_stat.h"
33 #include "stg/common.h"
54 typedef typename T::value_type type;
58 struct ARRAY_TYPE<T[]>
63 template <typename T, size_t N>
64 struct ARRAY_TYPE<T[N]>
70 struct nullary_function
72 typedef T result_type;
76 class binder0 : public nullary_function<typename F::result_type>
79 binder0(const F & func, const typename F::argument_type & arg)
80 : m_func(func), m_arg(arg) {}
81 typename F::result_type operator()() const { return m_func(m_arg); }
84 typename F::argument_type m_arg;
89 binder0<F> bind0(const F & func, const typename F::argument_type & arg)
91 return binder0<F>(func, arg);
94 template <typename C, typename A, typename R>
95 class METHOD1_ADAPTER : public std::unary_function<A, R>
98 METHOD1_ADAPTER(R (C::* func)(A), C & obj) : m_func(func), m_obj(obj) {}
99 R operator()(A arg) { return (m_obj.*m_func)(arg); }
105 template <typename C, typename A, typename R>
106 class CONST_METHOD1_ADAPTER : public std::unary_function<A, R>
109 CONST_METHOD1_ADAPTER(R (C::* func)(A) const, C & obj) : m_func(func), m_obj(obj) {}
110 R operator()(A arg) const { return (m_obj.*m_func)(arg); }
112 R (C::* m_func)(A) const;
116 template <typename C, typename A, typename R>
117 METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A), C & obj)
119 return METHOD1_ADAPTER<C, A, R>(func, obj);
122 template <typename C, typename A, typename R>
123 CONST_METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A) const, C & obj)
125 return CONST_METHOD1_ADAPTER<C, A, R>(func, obj);
128 template <typename T>
129 bool SetArrayItem(T & array, const char * index, const typename ARRAY_TYPE<T>::type & value)
132 if (str2x(index, pos))
138 void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * data)
142 std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl;
150 void UsageImpl(bool full);
151 void UsageConnection();
152 void UsageAdmins(bool full);
153 void UsageTariffs(bool full);
154 void UsageUsers(bool full);
155 void UsageServices(bool full);
156 void UsageCorporations(bool full);
160 void ReadUserConfigFile(SGCONF::OPTION_BLOCK & block)
162 std::vector<std::string> paths;
163 const char * configHome = getenv("XDG_CONFIG_HOME");
164 if (configHome == NULL)
166 const char * home = getenv("HOME");
169 paths.push_back(std::string(home) + "/.config/sgconf/sgconf.conf");
170 paths.push_back(std::string(home) + "/.sgconf/sgconf.conf");
173 paths.push_back(std::string(configHome) + "/sgconf/sgconf.conf");
174 for (std::vector<std::string>::const_iterator it = paths.begin(); it != paths.end(); ++it)
175 if (access(it->c_str(), R_OK) == 0)
177 block.ParseFile(*it);
182 } // namespace anonymous
187 class CONFIG_ACTION : public ACTION
190 CONFIG_ACTION(CONFIG & config,
191 const std::string & paramDescription)
193 m_description(paramDescription)
196 virtual ACTION * Clone() const { return new CONFIG_ACTION(*this); }
198 virtual std::string ParamDescription() const { return m_description; }
199 virtual std::string DefaultDescription() const { return ""; }
200 virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
201 virtual PARSER_STATE Parse(int argc, char ** argv);
205 std::string m_description;
206 OPTION_BLOCK m_suboptions;
208 void ParseCredentials(const std::string & credentials);
209 void ParseHostAndPort(const std::string & hostAndPort);
212 PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv)
217 throw ERROR("Missing argument.");
218 char * pos = strchr(*argv, '@');
221 ParseCredentials(std::string(*argv, pos));
222 ParseHostAndPort(std::string(pos + 1));
226 ParseHostAndPort(std::string(*argv));
228 return PARSER_STATE(false, --argc, ++argv);
231 void CONFIG_ACTION::ParseCredentials(const std::string & credentials)
233 std::string::size_type pos = credentials.find_first_of(':');
234 if (pos != std::string::npos)
236 m_config.userName = credentials.substr(0, pos);
237 m_config.userPass = credentials.substr(pos + 1);
241 m_config.userName = credentials;
245 void CONFIG_ACTION::ParseHostAndPort(const std::string & hostAndPort)
247 std::string::size_type pos = hostAndPort.find_first_of(':');
248 if (pos != std::string::npos)
250 m_config.server = hostAndPort.substr(0, pos);
252 if (str2x(hostAndPort.substr(pos + 1), port))
253 throw ERROR("Invalid port value: '" + hostAndPort.substr(pos + 1) + "'");
254 m_config.port = port;
258 m_config.server = hostAndPort;
263 CONFIG_ACTION * MakeParamAction(CONFIG & config,
264 const std::string & paramDescription)
266 return new CONFIG_ACTION(config, paramDescription);
269 } // namespace SGCONF
273 struct option long_options_get[] = {
274 {"server", 1, 0, 's'}, //Server
275 {"port", 1, 0, 'p'}, //Port
276 {"admin", 1, 0, 'a'}, //Admin
277 {"admin_pass", 1, 0, 'w'}, //passWord
278 {"user", 1, 0, 'u'}, //User
279 {"addcash", 0, 0, 'c'}, //Add Cash
280 //{"setcash", 0, 0, 'v'}, //Set Cash
281 {"credit", 0, 0, 'r'}, //cRedit
282 {"tariff", 0, 0, 't'}, //Tariff
283 {"message", 0, 0, 'm'}, //message
284 {"password", 0, 0, 'o'}, //password
285 {"down", 0, 0, 'd'}, //down
286 {"passive", 0, 0, 'i'}, //passive
287 {"disable-stat",0, 0, 'S'}, //disable detail stat
288 {"always-online",0, 0, 'O'}, //always online
289 {"session-upload", 1, 0, 500}, //SU0
290 {"session-download", 1, 0, 501}, //SD0
291 {"month-upload", 1, 0, 502}, //MU0
292 {"month-download", 1, 0, 503}, //MD0
294 {"user-data", 1, 0, 700}, //UserData0
296 {"prepaid", 0, 0, 'e'}, //prepaid traff
297 {"create", 0, 0, 'n'}, //create
298 {"delete", 0, 0, 'l'}, //delete
300 {"note", 0, 0, 'N'}, //Note
301 {"name", 0, 0, 'A'}, //nAme
302 {"address", 0, 0, 'D'}, //aDdress
303 {"email", 0, 0, 'L'}, //emaiL
304 {"phone", 0, 0, 'P'}, //phone
305 {"group", 0, 0, 'G'}, //Group
306 {"ip", 0, 0, 'I'}, //IP-address of user
307 {"authorized-by",0, 0, 800}, //always online
311 struct option long_options_set[] = {
312 {"server", 1, 0, 's'}, //Server
313 {"port", 1, 0, 'p'}, //Port
314 {"admin", 1, 0, 'a'}, //Admin
315 {"admin_pass", 1, 0, 'w'}, //passWord
316 {"user", 1, 0, 'u'}, //User
317 {"addcash", 1, 0, 'c'}, //Add Cash
318 {"setcash", 1, 0, 'v'}, //Set Cash
319 {"credit", 1, 0, 'r'}, //cRedit
320 {"tariff", 1, 0, 't'}, //Tariff
321 {"message", 1, 0, 'm'}, //message
322 {"password", 1, 0, 'o'}, //password
323 {"down", 1, 0, 'd'}, //down
324 {"passive", 1, 0, 'i'}, //passive
325 {"disable-stat",1, 0, 'S'}, //disable detail stat
326 {"always-online",1, 0, 'O'}, //always online
327 {"session-upload", 1, 0, 500}, //U0
328 {"session-download", 1, 0, 501}, //U1
329 {"month-upload", 1, 0, 502}, //U2
330 {"month-download", 1, 0, 503}, //U3
332 {"user-data", 1, 0, 700}, //UserData
334 {"prepaid", 1, 0, 'e'}, //prepaid traff
335 {"create", 1, 0, 'n'}, //create
336 {"delete", 1, 0, 'l'}, //delete
338 {"note", 1, 0, 'N'}, //Note
339 {"name", 1, 0, 'A'}, //nAme
340 {"address", 1, 0, 'D'}, //aDdress
341 {"email", 1, 0, 'L'}, //emaiL
342 {"phone", 1, 0, 'P'}, //phone
343 {"group", 1, 0, 'G'}, //Group
344 {"ip", 0, 0, 'I'}, //IP-address of user
348 //-----------------------------------------------------------------------------
349 CASH_INFO ParseCash(const char * str)
351 //-c 123.45:log message
352 std::string cashString;
354 const char * pos = strchr(str, ':');
357 cashString.append(str, pos);
358 message.append(pos + 1);
364 if (strtodouble2(cashString, cash) != 0)
366 printf("Incorrect cash value %s\n", str);
367 exit(PARAMETER_PARSING_ERR_CODE);
370 return CASH_INFO(cash, message);
372 //-----------------------------------------------------------------------------
373 double ParseCredit(const char * c)
376 if (strtodouble2(c, credit) != 0)
378 printf("Incorrect credit value %s\n", c);
379 exit(PARAMETER_PARSING_ERR_CODE);
384 //-----------------------------------------------------------------------------
385 double ParsePrepaidTraffic(const char * c)
388 if (strtodouble2(c, credit) != 0)
390 printf("Incorrect prepaid traffic value %s\n", c);
391 exit(PARAMETER_PARSING_ERR_CODE);
396 //-----------------------------------------------------------------------------
397 int64_t ParseTraff(const char * c)
400 if (str2x(c, traff) != 0)
402 printf("Incorrect credit value %s\n", c);
403 exit(PARAMETER_PARSING_ERR_CODE);
408 //-----------------------------------------------------------------------------
409 bool ParseDownPassive(const char * dp)
411 if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0')))
413 printf("Incorrect value %s\n", dp);
414 exit(PARAMETER_PARSING_ERR_CODE);
419 //-----------------------------------------------------------------------------
420 void ParseTariff(const char * str, RESETABLE<std::string> & tariffName, RESETABLE<std::string> & nextTariff)
422 const char * pos = strchr(str, ':');
425 std::string tariff(str, pos);
426 if (strcmp(pos + 1, "now") == 0)
428 else if (strcmp(pos + 1, "delayed") == 0)
432 printf("Incorrect tariff value '%s'. Should be '<tariff>', '<tariff>:now' or '<tariff>:delayed'.\n", str);
433 exit(PARAMETER_PARSING_ERR_CODE);
439 //-----------------------------------------------------------------------------
440 time_t ParseCreditExpire(const char * str)
442 struct tm brokenTime;
444 brokenTime.tm_wday = 0;
445 brokenTime.tm_yday = 0;
446 brokenTime.tm_isdst = 0;
447 brokenTime.tm_hour = 0;
448 brokenTime.tm_min = 0;
449 brokenTime.tm_sec = 0;
451 stg_strptime(str, "%Y-%m-%d", &brokenTime);
453 return stg_timegm(&brokenTime);
455 //-----------------------------------------------------------------------------
456 void ParseAnyString(const char * c, string * msg, const char * enc)
459 char * ob = new char[strlen(c) + 1];
460 char * ib = new char[strlen(c) + 1];
467 setlocale(LC_ALL, "");
470 strncpy(charsetF, nl_langinfo(CODESET), 255);
472 const char * charsetT = enc;
476 size_t insize = strlen(ib);
477 size_t outsize = strlen(ib);
481 cd = iconv_open(charsetT, charsetF);
482 if (cd == (iconv_t) -1)
486 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
491 printf("error iconv_open\n");
493 exit(ICONV_ERR_CODE);
496 #if defined(FREE_BSD) || defined(FREE_BSD5)
497 nconv = iconv (cd, (const char**)&inbuf, &insize, &outbuf, &outsize);
499 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
501 //printf("nconv=%d outsize=%d\n", nconv, outsize);
502 if (nconv == (size_t) -1)
506 printf("iconv error\n");
507 exit(ICONV_ERR_CODE);
519 //-----------------------------------------------------------------------------
520 void CreateRequestSet(REQUEST * req, char * r)
522 const int strLen = 10024;
524 memset(str, 0, strLen);
528 if (!req->usrMsg.empty())
531 Encode12str(msg, req->usrMsg.data());
532 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());
533 //sprintf(str, "<message login=\"%s\" priority=\"0\" text=\"%s\"/>\n", req->login, msg);
540 sprintf(str, "<DelUser login=\"%s\"/>", req->login.const_data().c_str());
548 sprintf(str, "<AddUser> <login value=\"%s\"/> </AddUser>", req->login.const_data().c_str());
554 strcat(r, "<SetUser>\n");
555 sprintf(str, "<login value=\"%s\"/>\n", req->login.const_data().c_str());
557 if (!req->credit.empty())
559 sprintf(str, "<credit value=\"%f\"/>\n", req->credit.const_data());
563 if (!req->creditExpire.empty())
565 sprintf(str, "<creditExpire value=\"%ld\"/>\n", req->creditExpire.const_data());
569 if (!req->prepaidTraff.empty())
571 sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
575 if (!req->cash.empty())
578 Encode12str(msg, req->message);
579 sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg.c_str());
583 if (!req->setCash.empty())
586 Encode12str(msg, req->message);
587 sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg.c_str());
591 if (!req->usrPasswd.empty())
593 sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
597 if (!req->down.empty())
599 sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
603 if (!req->passive.empty())
605 sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
609 if (!req->disableDetailStat.empty())
611 sprintf(str, "<disableDetailStat value=\"%d\" />\n", req->disableDetailStat.const_data());
615 if (!req->alwaysOnline.empty())
617 sprintf(str, "<aonline value=\"%d\" />\n", req->alwaysOnline.const_data());
621 // IP-address of user
622 if (!req->ips.empty())
624 sprintf(str, "<ip value=\"%s\" />\n", req->ips.const_data().c_str());
628 int uPresent = false;
629 int dPresent = false;
630 for (int i = 0; i < DIR_NUM; i++)
632 if (!req->monthUpload[i].empty())
634 if (!uPresent && !dPresent)
636 sprintf(str, "<traff ");
642 ss << req->monthUpload[i].const_data();
643 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
644 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
647 if (!req->monthDownload[i].empty())
649 if (!uPresent && !dPresent)
651 sprintf(str, "<traff ");
657 ss << req->monthDownload[i].const_data();
658 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
661 if (!req->sessionUpload[i].empty())
663 if (!uPresent && !dPresent)
665 sprintf(str, "<traff ");
671 ss << req->sessionUpload[i].const_data();
672 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
673 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
676 if (!req->sessionDownload[i].empty())
678 if (!uPresent && !dPresent)
680 sprintf(str, "<traff ");
686 ss << req->sessionDownload[i].const_data();
687 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
691 if (uPresent || dPresent)
698 if (!req->tariff.empty())
700 switch (req->chgTariff)
703 sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
707 sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
711 sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
718 if (!req->note.empty())
721 Encode12str(note, req->note.data());
722 sprintf(str, "<note value=\"%s\"/>", note.c_str());
726 if (!req->name.empty())
729 Encode12str(name, req->name.data());
730 sprintf(str, "<name value=\"%s\"/>", name.c_str());
734 if (!req->address.empty())
737 Encode12str(address, req->address.data());
738 sprintf(str, "<address value=\"%s\"/>", address.c_str());
742 if (!req->email.empty())
745 Encode12str(email, req->email.data());
746 sprintf(str, "<email value=\"%s\"/>", email.c_str());
750 if (!req->phone.empty())
753 Encode12str(phone, req->phone.data());
754 sprintf(str, "<phone value=\"%s\"/>", phone.c_str());
758 if (!req->group.empty())
761 Encode12str(group, req->group.data());
762 sprintf(str, "<group value=\"%s\"/>", group.c_str());
766 for (int i = 0; i < USERDATA_NUM; i++)
768 if (!req->userData[i].empty())
771 Encode12str(ud, req->userData[i].data());
772 sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
777 strcat(r, "</SetUser>\n");
779 //-----------------------------------------------------------------------------
780 int CheckParameters(REQUEST * req)
787 bool a = !req->admLogin.empty()
788 && !req->admPasswd.empty()
789 && !req->server.empty()
790 && !req->port.empty()
791 && !req->login.empty();
793 bool b = !req->cash.empty()
794 || !req->setCash.empty()
795 || !req->credit.empty()
796 || !req->prepaidTraff.empty()
797 || !req->tariff.empty()
798 || !req->usrMsg.empty()
799 || !req->usrPasswd.empty()
801 || !req->note.empty()
802 || !req->name.empty()
803 || !req->address.empty()
804 || !req->email.empty()
805 || !req->phone.empty()
806 || !req->group.empty()
807 || !req->ips.empty() // IP-address of user
813 for (int i = 0; i < DIR_NUM; i++)
815 if (req->sessionUpload[i].empty())
822 for (int i = 0; i < DIR_NUM; i++)
824 if (req->sessionDownload[i].empty())
831 for (int i = 0; i < DIR_NUM; i++)
833 if (req->monthUpload[i].empty())
840 for (int i = 0; i < DIR_NUM; i++)
842 if (req->monthDownload[i].empty())
849 for (int i = 0; i < DIR_NUM; i++)
851 if (req->userData[i].empty())
859 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
860 return a && (b || su || sd || mu || md || ud);
862 //-----------------------------------------------------------------------------
863 int CheckParametersGet(REQUEST * req)
865 return CheckParameters(req);
867 //-----------------------------------------------------------------------------
868 int CheckParametersSet(REQUEST * req)
870 return CheckParameters(req);
872 //-----------------------------------------------------------------------------
873 bool mainGet(int argc, char **argv)
877 RESETABLE<string> t1;
878 int missedOptionArg = false;
880 const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISOE";
881 int option_index = -1;
886 c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
897 req.port = ParseServerPort(optarg);
902 req.admLogin = ParseAdminLogin(optarg);
905 case 'w': //admin password
906 req.admPasswd = ParsePassword(optarg);
909 case 'o': //change user password
914 req.login = ParseUser(optarg);
925 case 'E': //credit expire
926 req.creditExpire = 1;
941 case 'e': //Prepaid Traffic
942 req.prepaidTraff = 1;
969 case 'I': //IP-address of user
973 case 'S': //Detail stat status
974 req.disableDetailStat = " ";
977 case 'O': //Always online status
978 req.alwaysOnline = " ";
982 SetArrayItem(req.sessionUpload, optarg, 1);
983 //req.sessionUpload[optarg] = 1;
986 SetArrayItem(req.sessionDownload, optarg, 1);
987 //req.sessionDownload[optarg] = 1;
990 SetArrayItem(req.monthUpload, optarg, 1);
991 //req.monthUpload[optarg] = 1;
994 SetArrayItem(req.monthDownload, optarg, 1);
995 //req.monthDownload[optarg] = 1;
999 SetArrayItem(req.userData, optarg, std::string(" "));
1000 //req.userData[optarg] = " ";
1009 missedOptionArg = true;
1013 printf ("?? getopt returned character code 0%o ??\n", c);
1019 printf ("non-option ARGV-elements: ");
1020 while (optind < argc)
1021 printf ("%s ", argv[optind++]);
1023 exit(PARAMETER_PARSING_ERR_CODE);
1026 if (missedOptionArg || !CheckParametersGet(&req))
1028 //printf("Parameter needed\n");
1030 exit(PARAMETER_PARSING_ERR_CODE);
1034 return ProcessAuthBy(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data());
1036 return ProcessGetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req);
1038 //-----------------------------------------------------------------------------
1039 bool mainSet(int argc, char **argv)
1044 bool isMessage = false;
1047 RESETABLE<string> t1;
1049 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:";
1051 int missedOptionArg = false;
1057 int option_index = -1;
1059 c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
1067 req.server = optarg;
1071 req.port = ParseServerPort(optarg);
1076 req.admLogin = ParseAdminLogin(optarg);
1079 case 'w': //admin password
1080 req.admPasswd = ParsePassword(optarg);
1083 case 'o': //change user password
1084 conf.password = ParsePassword(optarg);
1088 req.login = ParseUser(optarg);
1091 case 'c': //add cash
1092 stat.cashAdd = ParseCash(optarg);
1095 case 'v': //set cash
1096 stat.cashSet = ParseCash(optarg);
1100 conf.credit = ParseCredit(optarg);
1103 case 'E': //credit expire
1104 conf.creditExpire = ParseCreditExpire(optarg);
1108 conf.disabled = ParseDownPassive(optarg);
1112 conf.passive = ParseDownPassive(optarg);
1116 ParseTariff(optarg, conf.tariffName, conf.nextTariff);
1120 ParseAnyString(optarg, &str);
1125 case 'e': //Prepaid Traffic
1126 stat.freeMb = ParsePrepaidTraffic(optarg);
1129 case 'n': //Create User
1130 req.createUser = true;
1133 case 'l': //Delete User
1134 req.deleteUser = true;
1138 ParseAnyString(optarg, &str, "koi8-ru");
1143 ParseAnyString(optarg, &str, "koi8-ru");
1144 conf.realName = str;
1148 ParseAnyString(optarg, &str, "koi8-ru");
1153 ParseAnyString(optarg, &str, "koi8-ru");
1158 ParseAnyString(optarg, &str);
1163 ParseAnyString(optarg, &str, "koi8-ru");
1167 case 'I': //IP-address of user
1168 ParseAnyString(optarg, &str);
1169 conf.ips = StrToIPS(str);
1173 conf.disabledDetailStat = ParseDownPassive(optarg);
1177 conf.alwaysOnline = ParseDownPassive(optarg);
1181 SetArrayItem(stat.sessionUp, optarg, ParseTraff(argv[optind++]));
1184 SetArrayItem(stat.sessionDown, optarg, ParseTraff(argv[optind++]));
1187 SetArrayItem(stat.monthUp, optarg, ParseTraff(argv[optind++]));
1190 SetArrayItem(stat.monthDown, optarg, ParseTraff(argv[optind++]));
1193 case 700: //UserData
1194 ParseAnyString(argv[optind++], &str);
1195 SetArrayItem(conf.userdata, optarg, str);
1199 missedOptionArg = true;
1203 missedOptionArg = true;
1207 printf("?? getopt returned character code 0%o ??\n", c);
1213 printf ("non-option ARGV-elements: ");
1214 while (optind < argc)
1215 printf ("%s ", argv[optind++]);
1217 exit(PARAMETER_PARSING_ERR_CODE);
1220 if (missedOptionArg || !CheckParametersSet(&req))
1222 //printf("Parameter needed\n");
1224 exit(PARAMETER_PARSING_ERR_CODE);
1227 const int rLen = 20000;
1229 memset(rstr, 0, rLen);
1232 return ProcessSendMessage(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req.usrMsg.data());
1234 return ProcessSetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), conf, stat);
1236 //-----------------------------------------------------------------------------
1237 int main(int argc, char **argv)
1239 SGCONF::CONFIG config;
1241 SGCONF::OPTION_BLOCKS blocks;
1242 blocks.Add("General options")
1243 .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), "<config file>"), "override default config file")
1244 .Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit")
1245 .Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit")
1246 .Add("v", "version", SGCONF::MakeFunc0Action(Version), "\t\tshow version information and exit");
1247 SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options")
1248 .Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "<address>"), "\t\thost to connect")
1249 .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "\t\tport to connect")
1250 .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
1251 .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "\tpassword for the administrative login")
1252 .Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
1253 blocks.Add("Raw XML")
1254 .Add("r", "raw", SGCONF::MakeFunc1Action(), "\t\tmake raw XML request")
1255 /*blocks.Add("Admins management options")
1256 .Add("get-admins", SGCONF::MakeConfAction())
1257 .Add("get-admin", SGCONF::MakeConfAction())
1258 .Add("add-admin", SGCONF::MakeConfAction())
1259 .Add("del-admin", SGCONF::MakeConfAction())
1260 .Add("chg-admin", SGCONF::MakeConfAction());*/
1263 SGCONF::PARSER_STATE state(false, argc, argv);
1267 state = blocks.Parse(--argc, ++argv); // Skipping self name
1269 catch (const SGCONF::OPTION::ERROR& ex)
1271 std::cerr << ex.what() << "\n";
1280 std::cerr << "Unknown option: '" << *state.argv << "'\n";
1286 SGCONF::CONFIG configOverride(config);
1288 if (config.configFile.empty())
1290 const char * mainConfigFile = "/etc/sgconf/sgconf.conf";
1291 if (access(mainConfigFile, R_OK) == 0)
1292 block.ParseFile(mainConfigFile);
1293 ReadUserConfigFile(block);
1297 block.ParseFile(config.configFile.data());
1300 config = configOverride;
1302 catch (const std::exception& ex)
1304 std::cerr << ex.what() << "\n";
1308 std::cerr << "Config: " << config.Serialize() << std::endl;
1321 exit(PARAMETER_PARSING_ERR_CODE);
1324 if (strcmp(argv[1], "get") == 0)
1327 return mainGet(argc - 1, argv + 1);
1329 else if (strcmp(argv[1], "set") == 0)
1332 if (mainSet(argc - 1, argv + 1) )
1339 exit(PARAMETER_PARSING_ERR_CODE);
1341 return UNKNOWN_ERR_CODE;
1343 //-----------------------------------------------------------------------------
1358 void UsageImpl(bool full)
1360 std::cout << "sgconf is the Stargazer management utility.\n\n"
1362 << "\tsgconf [options]\n\n"
1363 << "General options:\n"
1364 << "\t-c, --config <config file>\t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n"
1365 << "\t-h, --help\t\t\t\tshow this help and exit\n"
1366 << "\t--help-all\t\t\t\tshow full help and exit\n"
1367 << "\t-v, --version\t\t\t\tshow version information and exit\n\n";
1372 UsageServices(full);
1373 UsageCorporations(full);
1375 //-----------------------------------------------------------------------------
1376 void UsageConnection()
1378 std::cout << "Connection options:\n"
1379 << "\t-s, --server <address>\t\t\thost to connect (ip or domain name, default: \"localhost\")\n"
1380 << "\t-p, --port <port>\t\t\tport to connect (default: \"5555\")\n"
1381 << "\t-u, --username <username>\t\tadministrative login (default: \"admin\")\n"
1382 << "\t-w, --userpass <password>\t\tpassword for administrative login\n"
1383 << "\t-a, --address <connection string>\tconnection params as a single string in format: <login>:<password>@<host>:<port>\n\n";
1385 //-----------------------------------------------------------------------------
1386 void UsageAdmins(bool full)
1388 std::cout << "Admins management options:\n"
1389 << "\t--get-admins\t\t\t\tget a list of admins (subsequent options will define what to show)\n";
1391 std::cout << "\t\t--login\t\t\t\tshow admin's login\n"
1392 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1393 std::cout << "\t--get-admin\t\t\t\tget the information about admin\n";
1395 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to show\n"
1396 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1397 std::cout << "\t--add-admin\t\t\t\tadd a new admin\n";
1399 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to add\n"
1400 << "\t\t--password <password>\t\tpassword of the admin to add\n"
1401 << "\t\t--priv <priv number>\t\tpriviledges of the admin to add\n\n";
1402 std::cout << "\t--del-admin\t\t\t\tdelete an existing admin\n";
1404 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to delete\n\n";
1405 std::cout << "\t--chg-admin\t\t\t\tchange an existing admin\n";
1407 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to change\n"
1408 << "\t\t--priv <priv number>\t\tnew priviledges\n\n";
1410 //-----------------------------------------------------------------------------
1411 void UsageTariffs(bool full)
1413 std::cout << "Tariffs management options:\n"
1414 << "\t--get-tariffs\t\t\t\tget a list of tariffs (subsequent options will define what to show)\n";
1416 std::cout << "\t\t--name\t\t\t\tshow tariff's name\n"
1417 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1418 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1419 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1420 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1421 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1422 std::cout << "\t--get-tariff\t\t\t\tget the information about tariff\n";
1424 std::cout << "\t\t--name <name>\t\t\tname of the tariff to show\n"
1425 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1426 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1427 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1428 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1429 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1430 std::cout << "\t--add-tariff\t\t\t\tadd a new tariff\n";
1432 std::cout << "\t\t--name <name>\t\t\tname of the tariff to add\n"
1433 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1434 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1435 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1436 << "\t\t--traff-type <type>\t\twhat type of traffi will be accounted by the tariff\n"
1437 << "\t\t--times <times>\t\t\tslash-separated list of \"day\" time-spans (in form \"hh:mm-hh:mm\") for each direction\n"
1438 << "\t\t--prices-day-a <prices>\t\tslash-separated list of prices for \"day\" traffic before threshold for each direction\n"
1439 << "\t\t--prices-night-a <prices>\tslash-separated list of prices for \"night\" traffic before threshold for each direction\n"
1440 << "\t\t--prices-day-b <prices>\t\tslash-separated list of prices for \"day\" traffic after threshold for each direction\n"
1441 << "\t\t--prices-night-b <prices>\tslash-separated list of prices for \"night\" traffic after threshold for each direction\n"
1442 << "\t\t--single-prices <yes|no>\tslash-separated list of \"single price\" flags for each direction\n"
1443 << "\t\t--no-discounts <yes|no>\t\tslash-separated list of \"no discount\" flags for each direction\n"
1444 << "\t\t--thresholds <thresholds>\tslash-separated list of thresholds (in Mb) for each direction\n\n";
1445 std::cout << "\t--del-tariff\t\t\t\tdelete an existing tariff\n";
1447 std::cout << "\t\t--name <name>\t\t\tname of the tariff to delete\n\n";
1448 std::cout << "\t--chg-tariff\t\t\t\tchange an existing tariff\n";
1450 std::cout << "\t\t--name <name>\t\t\tname of the tariff to change\n"
1451 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1452 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1453 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1454 << "\t\t--traff-type <type>\t\twhat type of traffix will be accounted by the tariff\n"
1455 << "\t\t--dir <N>\t\t\tnumber of direction data to change\n"
1456 << "\t\t\t--time <time>\t\t\"day\" time-span (in form \"hh:mm-hh:mm\")\n"
1457 << "\t\t\t--price-day-a <price>\tprice for \"day\" traffic before threshold\n"
1458 << "\t\t\t--price-night-a <price>\tprice for \"night\" traffic before threshold\n"
1459 << "\t\t\t--price-day-b <price>\tprice for \"day\" traffic after threshold\n"
1460 << "\t\t\t--price-night-b <price>\tprice for \"night\" traffic after threshold\n"
1461 << "\t\t\t--single-price <yes|no>\t\"single price\" flag\n"
1462 << "\t\t\t--no-discount <yes|no>\t\"no discount\" flag\n"
1463 << "\t\t\t--threshold <threshold>\tthreshold (in Mb)\n\n";
1465 //-----------------------------------------------------------------------------
1466 void UsageUsers(bool full)
1468 std::cout << "Users management options:\n"
1469 << "\t--get-users\t\t\t\tget a list of users (subsequent options will define what to show)\n";
1471 std::cout << "\n\n";
1472 std::cout << "\t--get-user\t\t\t\tget the information about user\n";
1474 std::cout << "\n\n";
1475 std::cout << "\t--add-user\t\t\t\tadd a new user\n";
1477 std::cout << "\n\n";
1478 std::cout << "\t--del-user\t\t\t\tdelete an existing user\n";
1480 std::cout << "\n\n";
1481 std::cout << "\t--chg-user\t\t\t\tchange an existing user\n";
1483 std::cout << "\n\n";
1484 std::cout << "\t--check-user\t\t\t\tcheck credentials is valid\n";
1486 std::cout << "\n\n";
1487 std::cout << "\t--send-message\t\t\t\tsend a message to a user\n";
1489 std::cout << "\n\n";
1491 //-----------------------------------------------------------------------------
1492 void UsageServices(bool full)
1494 std::cout << "Services management options:\n"
1495 << "\t--get-services\t\t\t\tget a list of services (subsequent options will define what to show)\n";
1497 std::cout << "\t\t--name\t\t\t\tshow service's name\n"
1498 << "\t\t--comment\t\t\tshow a comment to the service\n"
1499 << "\t\t--cost\t\t\t\tshow service's cost\n"
1500 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1501 std::cout << "\t--get-service\t\t\t\tget the information about service\n";
1503 std::cout << "\t\t--name <name>\t\t\tname of the service to show\n"
1504 << "\t\t--comment\t\t\tshow a comment to the service\n"
1505 << "\t\t--cost\t\t\t\tshow service's cost\n"
1506 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1507 std::cout << "\t--add-service\t\t\t\tadd a new service\n";
1509 std::cout << "\t\t--name <name>\t\t\tname of the service to add\n"
1510 << "\t\t--comment <comment>\t\ta comment to the service\n"
1511 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1512 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1513 std::cout << "\t--del-service\t\t\t\tdelete an existing service\n";
1515 std::cout << "\t\t--name <name>\t\t\tname of the service to delete\n\n";
1516 std::cout << "\t--chg-service\t\t\t\tchange an existing service\n";
1518 std::cout << "\t\t--name <name>\t\t\tname of the service to change\n"
1519 << "\t\t--comment <comment>\t\ta comment to the service\n"
1520 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1521 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1523 //-----------------------------------------------------------------------------
1524 void UsageCorporations(bool full)
1526 std::cout << "Corporations management options:\n"
1527 << "\t--get-corporations\t\t\tget a list of corporations (subsequent options will define what to show)\n";
1529 std::cout << "\t\t--name\t\t\t\tshow corporation's name\n"
1530 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1531 std::cout << "\t--get-corp\t\t\t\tget the information about corporation\n";
1533 std::cout << "\t\t--name <name>\t\t\tname of the corporation to show\n"
1534 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1535 std::cout << "\t--add-corp\t\t\t\tadd a new corporation\n";
1537 std::cout << "\t\t--name <name>\t\t\tname of the corporation to add\n"
1538 << "\t\t--cash <cash>\t\t\tinitial corporation's cash (default: \"0\")\n\n";
1539 std::cout << "\t--del-corp\t\t\t\tdelete an existing corporation\n";
1541 std::cout << "\t\t--name <name>\t\t\tname of the corporation to delete\n\n";
1542 std::cout << "\t--chg-corp\t\t\t\tchange an existing corporation\n";
1544 std::cout << "\t\t--name <name>\t\t\tname of the corporation to change\n"
1545 << "\t\t--add-cash <amount>[:<message>]\tadd cash to the corporation's account and optional comment message\n"
1546 << "\t\t--set-cash <cash>[:<message>]\tnew corporation's cash and optional comment message\n\n";
1551 std::cout << "sgconf, version: 2.0.0-alpha.\n";
1554 } // namespace anonymous