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 void ReadUserConfigFile(SGCONF::OPTION_BLOCK & block)
151 std::vector<std::string> paths;
152 const char * configHome = getenv("XDG_CONFIG_HOME");
153 if (configHome == NULL)
155 const char * home = getenv("HOME");
158 paths.push_back(std::string(home) + "/.config/sgconf/sgconf.conf");
159 paths.push_back(std::string(home) + "/.sgconf/sgconf.conf");
162 paths.push_back(std::string(configHome) + "/sgconf/sgconf.conf");
163 for (std::vector<std::string>::const_iterator it = paths.begin(); it != paths.end(); ++it)
164 if (access(it->c_str(), R_OK) == 0)
166 block.ParseFile(*it);
171 } // namespace anonymous
176 class CONFIG_ACTION : public ACTION
179 CONFIG_ACTION(CONFIG & config,
180 const std::string & paramDescription)
182 m_description(paramDescription)
185 virtual ACTION * Clone() const { return new CONFIG_ACTION(*this); }
187 virtual std::string ParamDescription() const { return m_description; }
188 virtual std::string DefaultDescription() const { return ""; }
189 virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
190 virtual PARSER_STATE Parse(int argc, char ** argv);
194 std::string m_description;
195 OPTION_BLOCK m_suboptions;
197 void ParseCredentials(const std::string & credentials);
198 void ParseHostAndPort(const std::string & hostAndPort);
201 PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv)
206 throw ERROR("Missing argument.");
207 char * pos = strchr(*argv, '@');
210 ParseCredentials(std::string(*argv, pos));
211 ParseHostAndPort(std::string(pos + 1));
215 ParseHostAndPort(std::string(*argv));
217 return PARSER_STATE(false, --argc, ++argv);
220 void CONFIG_ACTION::ParseCredentials(const std::string & credentials)
222 std::string::size_type pos = credentials.find_first_of(':');
223 if (pos != std::string::npos)
225 m_config.userName = credentials.substr(0, pos);
226 m_config.userPass = credentials.substr(pos + 1);
230 m_config.userName = credentials;
234 void CONFIG_ACTION::ParseHostAndPort(const std::string & hostAndPort)
236 std::string::size_type pos = hostAndPort.find_first_of(':');
237 if (pos != std::string::npos)
239 m_config.server = hostAndPort.substr(0, pos);
241 if (str2x(hostAndPort.substr(pos + 1), port))
242 throw ERROR("Invalid port value: '" + hostAndPort.substr(pos + 1) + "'");
243 m_config.port = port;
247 m_config.server = hostAndPort;
252 CONFIG_ACTION * MakeParamAction(CONFIG & config,
253 const std::string & paramDescription)
255 return new CONFIG_ACTION(config, paramDescription);
258 } // namespace SGCONF
262 struct option long_options_get[] = {
263 {"server", 1, 0, 's'}, //Server
264 {"port", 1, 0, 'p'}, //Port
265 {"admin", 1, 0, 'a'}, //Admin
266 {"admin_pass", 1, 0, 'w'}, //passWord
267 {"user", 1, 0, 'u'}, //User
268 {"addcash", 0, 0, 'c'}, //Add Cash
269 //{"setcash", 0, 0, 'v'}, //Set Cash
270 {"credit", 0, 0, 'r'}, //cRedit
271 {"tariff", 0, 0, 't'}, //Tariff
272 {"message", 0, 0, 'm'}, //message
273 {"password", 0, 0, 'o'}, //password
274 {"down", 0, 0, 'd'}, //down
275 {"passive", 0, 0, 'i'}, //passive
276 {"disable-stat",0, 0, 'S'}, //disable detail stat
277 {"always-online",0, 0, 'O'}, //always online
278 {"session-upload", 1, 0, 500}, //SU0
279 {"session-download", 1, 0, 501}, //SD0
280 {"month-upload", 1, 0, 502}, //MU0
281 {"month-download", 1, 0, 503}, //MD0
283 {"user-data", 1, 0, 700}, //UserData0
285 {"prepaid", 0, 0, 'e'}, //prepaid traff
286 {"create", 0, 0, 'n'}, //create
287 {"delete", 0, 0, 'l'}, //delete
289 {"note", 0, 0, 'N'}, //Note
290 {"name", 0, 0, 'A'}, //nAme
291 {"address", 0, 0, 'D'}, //aDdress
292 {"email", 0, 0, 'L'}, //emaiL
293 {"phone", 0, 0, 'P'}, //phone
294 {"group", 0, 0, 'G'}, //Group
295 {"ip", 0, 0, 'I'}, //IP-address of user
296 {"authorized-by",0, 0, 800}, //always online
300 struct option long_options_set[] = {
301 {"server", 1, 0, 's'}, //Server
302 {"port", 1, 0, 'p'}, //Port
303 {"admin", 1, 0, 'a'}, //Admin
304 {"admin_pass", 1, 0, 'w'}, //passWord
305 {"user", 1, 0, 'u'}, //User
306 {"addcash", 1, 0, 'c'}, //Add Cash
307 {"setcash", 1, 0, 'v'}, //Set Cash
308 {"credit", 1, 0, 'r'}, //cRedit
309 {"tariff", 1, 0, 't'}, //Tariff
310 {"message", 1, 0, 'm'}, //message
311 {"password", 1, 0, 'o'}, //password
312 {"down", 1, 0, 'd'}, //down
313 {"passive", 1, 0, 'i'}, //passive
314 {"disable-stat",1, 0, 'S'}, //disable detail stat
315 {"always-online",1, 0, 'O'}, //always online
316 {"session-upload", 1, 0, 500}, //U0
317 {"session-download", 1, 0, 501}, //U1
318 {"month-upload", 1, 0, 502}, //U2
319 {"month-download", 1, 0, 503}, //U3
321 {"user-data", 1, 0, 700}, //UserData
323 {"prepaid", 1, 0, 'e'}, //prepaid traff
324 {"create", 1, 0, 'n'}, //create
325 {"delete", 1, 0, 'l'}, //delete
327 {"note", 1, 0, 'N'}, //Note
328 {"name", 1, 0, 'A'}, //nAme
329 {"address", 1, 0, 'D'}, //aDdress
330 {"email", 1, 0, 'L'}, //emaiL
331 {"phone", 1, 0, 'P'}, //phone
332 {"group", 1, 0, 'G'}, //Group
333 {"ip", 0, 0, 'I'}, //IP-address of user
337 //-----------------------------------------------------------------------------
338 CASH_INFO ParseCash(const char * str)
340 //-c 123.45:log message
341 std::string cashString;
343 const char * pos = strchr(str, ':');
346 cashString.append(str, pos);
347 message.append(pos + 1);
353 if (strtodouble2(cashString, cash) != 0)
355 printf("Incorrect cash value %s\n", str);
356 exit(PARAMETER_PARSING_ERR_CODE);
359 return CASH_INFO(cash, message);
361 //-----------------------------------------------------------------------------
362 double ParseCredit(const char * c)
365 if (strtodouble2(c, credit) != 0)
367 printf("Incorrect credit value %s\n", c);
368 exit(PARAMETER_PARSING_ERR_CODE);
373 //-----------------------------------------------------------------------------
374 double ParsePrepaidTraffic(const char * c)
377 if (strtodouble2(c, credit) != 0)
379 printf("Incorrect prepaid traffic value %s\n", c);
380 exit(PARAMETER_PARSING_ERR_CODE);
385 //-----------------------------------------------------------------------------
386 int64_t ParseTraff(const char * c)
389 if (str2x(c, traff) != 0)
391 printf("Incorrect credit value %s\n", c);
392 exit(PARAMETER_PARSING_ERR_CODE);
397 //-----------------------------------------------------------------------------
398 bool ParseDownPassive(const char * dp)
400 if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0')))
402 printf("Incorrect value %s\n", dp);
403 exit(PARAMETER_PARSING_ERR_CODE);
408 //-----------------------------------------------------------------------------
409 void ParseTariff(const char * str, RESETABLE<std::string> & tariffName, RESETABLE<std::string> & nextTariff)
411 const char * pos = strchr(str, ':');
414 std::string tariff(str, pos);
415 if (strcmp(pos + 1, "now") == 0)
417 else if (strcmp(pos + 1, "delayed") == 0)
421 printf("Incorrect tariff value '%s'. Should be '<tariff>', '<tariff>:now' or '<tariff>:delayed'.\n", str);
422 exit(PARAMETER_PARSING_ERR_CODE);
428 //-----------------------------------------------------------------------------
429 time_t ParseCreditExpire(const char * str)
431 struct tm brokenTime;
433 brokenTime.tm_wday = 0;
434 brokenTime.tm_yday = 0;
435 brokenTime.tm_isdst = 0;
436 brokenTime.tm_hour = 0;
437 brokenTime.tm_min = 0;
438 brokenTime.tm_sec = 0;
440 stg_strptime(str, "%Y-%m-%d", &brokenTime);
442 return stg_timegm(&brokenTime);
444 //-----------------------------------------------------------------------------
445 void ParseAnyString(const char * c, string * msg, const char * enc)
448 char * ob = new char[strlen(c) + 1];
449 char * ib = new char[strlen(c) + 1];
456 setlocale(LC_ALL, "");
459 strncpy(charsetF, nl_langinfo(CODESET), 255);
461 const char * charsetT = enc;
465 size_t insize = strlen(ib);
466 size_t outsize = strlen(ib);
470 cd = iconv_open(charsetT, charsetF);
471 if (cd == (iconv_t) -1)
475 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
480 printf("error iconv_open\n");
482 exit(ICONV_ERR_CODE);
485 #if defined(FREE_BSD) || defined(FREE_BSD5)
486 nconv = iconv (cd, (const char**)&inbuf, &insize, &outbuf, &outsize);
488 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
490 //printf("nconv=%d outsize=%d\n", nconv, outsize);
491 if (nconv == (size_t) -1)
495 printf("iconv error\n");
496 exit(ICONV_ERR_CODE);
508 //-----------------------------------------------------------------------------
509 void CreateRequestSet(REQUEST * req, char * r)
511 const int strLen = 10024;
513 memset(str, 0, strLen);
517 if (!req->usrMsg.empty())
520 Encode12str(msg, req->usrMsg.data());
521 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());
522 //sprintf(str, "<message login=\"%s\" priority=\"0\" text=\"%s\"/>\n", req->login, msg);
529 sprintf(str, "<DelUser login=\"%s\"/>", req->login.const_data().c_str());
537 sprintf(str, "<AddUser> <login value=\"%s\"/> </AddUser>", req->login.const_data().c_str());
543 strcat(r, "<SetUser>\n");
544 sprintf(str, "<login value=\"%s\"/>\n", req->login.const_data().c_str());
546 if (!req->credit.empty())
548 sprintf(str, "<credit value=\"%f\"/>\n", req->credit.const_data());
552 if (!req->creditExpire.empty())
554 sprintf(str, "<creditExpire value=\"%ld\"/>\n", req->creditExpire.const_data());
558 if (!req->prepaidTraff.empty())
560 sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
564 if (!req->cash.empty())
567 Encode12str(msg, req->message);
568 sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg.c_str());
572 if (!req->setCash.empty())
575 Encode12str(msg, req->message);
576 sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg.c_str());
580 if (!req->usrPasswd.empty())
582 sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
586 if (!req->down.empty())
588 sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
592 if (!req->passive.empty())
594 sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
598 if (!req->disableDetailStat.empty())
600 sprintf(str, "<disableDetailStat value=\"%d\" />\n", req->disableDetailStat.const_data());
604 if (!req->alwaysOnline.empty())
606 sprintf(str, "<aonline value=\"%d\" />\n", req->alwaysOnline.const_data());
610 // IP-address of user
611 if (!req->ips.empty())
613 sprintf(str, "<ip value=\"%s\" />\n", req->ips.const_data().c_str());
617 int uPresent = false;
618 int dPresent = false;
619 for (int i = 0; i < DIR_NUM; i++)
621 if (!req->monthUpload[i].empty())
623 if (!uPresent && !dPresent)
625 sprintf(str, "<traff ");
631 ss << req->monthUpload[i].const_data();
632 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
633 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
636 if (!req->monthDownload[i].empty())
638 if (!uPresent && !dPresent)
640 sprintf(str, "<traff ");
646 ss << req->monthDownload[i].const_data();
647 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
650 if (!req->sessionUpload[i].empty())
652 if (!uPresent && !dPresent)
654 sprintf(str, "<traff ");
660 ss << req->sessionUpload[i].const_data();
661 //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
662 sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
665 if (!req->sessionDownload[i].empty())
667 if (!uPresent && !dPresent)
669 sprintf(str, "<traff ");
675 ss << req->sessionDownload[i].const_data();
676 sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
680 if (uPresent || dPresent)
687 if (!req->tariff.empty())
689 switch (req->chgTariff)
692 sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
696 sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
700 sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
707 if (!req->note.empty())
710 Encode12str(note, req->note.data());
711 sprintf(str, "<note value=\"%s\"/>", note.c_str());
715 if (!req->name.empty())
718 Encode12str(name, req->name.data());
719 sprintf(str, "<name value=\"%s\"/>", name.c_str());
723 if (!req->address.empty())
726 Encode12str(address, req->address.data());
727 sprintf(str, "<address value=\"%s\"/>", address.c_str());
731 if (!req->email.empty())
734 Encode12str(email, req->email.data());
735 sprintf(str, "<email value=\"%s\"/>", email.c_str());
739 if (!req->phone.empty())
742 Encode12str(phone, req->phone.data());
743 sprintf(str, "<phone value=\"%s\"/>", phone.c_str());
747 if (!req->group.empty())
750 Encode12str(group, req->group.data());
751 sprintf(str, "<group value=\"%s\"/>", group.c_str());
755 for (int i = 0; i < USERDATA_NUM; i++)
757 if (!req->userData[i].empty())
760 Encode12str(ud, req->userData[i].data());
761 sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
766 strcat(r, "</SetUser>\n");
768 //-----------------------------------------------------------------------------
769 int CheckParameters(REQUEST * req)
776 bool a = !req->admLogin.empty()
777 && !req->admPasswd.empty()
778 && !req->server.empty()
779 && !req->port.empty()
780 && !req->login.empty();
782 bool b = !req->cash.empty()
783 || !req->setCash.empty()
784 || !req->credit.empty()
785 || !req->prepaidTraff.empty()
786 || !req->tariff.empty()
787 || !req->usrMsg.empty()
788 || !req->usrPasswd.empty()
790 || !req->note.empty()
791 || !req->name.empty()
792 || !req->address.empty()
793 || !req->email.empty()
794 || !req->phone.empty()
795 || !req->group.empty()
796 || !req->ips.empty() // IP-address of user
802 for (int i = 0; i < DIR_NUM; i++)
804 if (req->sessionUpload[i].empty())
811 for (int i = 0; i < DIR_NUM; i++)
813 if (req->sessionDownload[i].empty())
820 for (int i = 0; i < DIR_NUM; i++)
822 if (req->monthUpload[i].empty())
829 for (int i = 0; i < DIR_NUM; i++)
831 if (req->monthDownload[i].empty())
838 for (int i = 0; i < DIR_NUM; i++)
840 if (req->userData[i].empty())
848 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
849 return a && (b || su || sd || mu || md || ud);
851 //-----------------------------------------------------------------------------
852 int CheckParametersGet(REQUEST * req)
854 return CheckParameters(req);
856 //-----------------------------------------------------------------------------
857 int CheckParametersSet(REQUEST * req)
859 return CheckParameters(req);
861 //-----------------------------------------------------------------------------
862 bool mainGet(int argc, char **argv)
866 RESETABLE<string> t1;
867 int missedOptionArg = false;
869 const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISOE";
870 int option_index = -1;
875 c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
886 req.port = ParseServerPort(optarg);
891 req.admLogin = ParseAdminLogin(optarg);
894 case 'w': //admin password
895 req.admPasswd = ParsePassword(optarg);
898 case 'o': //change user password
903 req.login = ParseUser(optarg);
914 case 'E': //credit expire
915 req.creditExpire = 1;
930 case 'e': //Prepaid Traffic
931 req.prepaidTraff = 1;
958 case 'I': //IP-address of user
962 case 'S': //Detail stat status
963 req.disableDetailStat = " ";
966 case 'O': //Always online status
967 req.alwaysOnline = " ";
971 SetArrayItem(req.sessionUpload, optarg, 1);
972 //req.sessionUpload[optarg] = 1;
975 SetArrayItem(req.sessionDownload, optarg, 1);
976 //req.sessionDownload[optarg] = 1;
979 SetArrayItem(req.monthUpload, optarg, 1);
980 //req.monthUpload[optarg] = 1;
983 SetArrayItem(req.monthDownload, optarg, 1);
984 //req.monthDownload[optarg] = 1;
988 SetArrayItem(req.userData, optarg, std::string(" "));
989 //req.userData[optarg] = " ";
998 missedOptionArg = true;
1002 printf ("?? getopt returned character code 0%o ??\n", c);
1008 printf ("non-option ARGV-elements: ");
1009 while (optind < argc)
1010 printf ("%s ", argv[optind++]);
1012 exit(PARAMETER_PARSING_ERR_CODE);
1015 if (missedOptionArg || !CheckParametersGet(&req))
1017 //printf("Parameter needed\n");
1019 exit(PARAMETER_PARSING_ERR_CODE);
1023 return ProcessAuthBy(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data());
1025 return ProcessGetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req);
1027 //-----------------------------------------------------------------------------
1028 bool mainSet(int argc, char **argv)
1033 bool isMessage = false;
1036 RESETABLE<string> t1;
1038 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:";
1040 int missedOptionArg = false;
1046 int option_index = -1;
1048 c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
1056 req.server = optarg;
1060 req.port = ParseServerPort(optarg);
1065 req.admLogin = ParseAdminLogin(optarg);
1068 case 'w': //admin password
1069 req.admPasswd = ParsePassword(optarg);
1072 case 'o': //change user password
1073 conf.password = ParsePassword(optarg);
1077 req.login = ParseUser(optarg);
1080 case 'c': //add cash
1081 stat.cashAdd = ParseCash(optarg);
1084 case 'v': //set cash
1085 stat.cashSet = ParseCash(optarg);
1089 conf.credit = ParseCredit(optarg);
1092 case 'E': //credit expire
1093 conf.creditExpire = ParseCreditExpire(optarg);
1097 conf.disabled = ParseDownPassive(optarg);
1101 conf.passive = ParseDownPassive(optarg);
1105 ParseTariff(optarg, conf.tariffName, conf.nextTariff);
1109 ParseAnyString(optarg, &str);
1114 case 'e': //Prepaid Traffic
1115 stat.freeMb = ParsePrepaidTraffic(optarg);
1118 case 'n': //Create User
1119 req.createUser = true;
1122 case 'l': //Delete User
1123 req.deleteUser = true;
1127 ParseAnyString(optarg, &str, "koi8-ru");
1132 ParseAnyString(optarg, &str, "koi8-ru");
1133 conf.realName = str;
1137 ParseAnyString(optarg, &str, "koi8-ru");
1142 ParseAnyString(optarg, &str, "koi8-ru");
1147 ParseAnyString(optarg, &str);
1152 ParseAnyString(optarg, &str, "koi8-ru");
1156 case 'I': //IP-address of user
1157 ParseAnyString(optarg, &str);
1158 conf.ips = StrToIPS(str);
1162 conf.disabledDetailStat = ParseDownPassive(optarg);
1166 conf.alwaysOnline = ParseDownPassive(optarg);
1170 SetArrayItem(stat.sessionUp, optarg, ParseTraff(argv[optind++]));
1173 SetArrayItem(stat.sessionDown, optarg, ParseTraff(argv[optind++]));
1176 SetArrayItem(stat.monthUp, optarg, ParseTraff(argv[optind++]));
1179 SetArrayItem(stat.monthDown, optarg, ParseTraff(argv[optind++]));
1182 case 700: //UserData
1183 ParseAnyString(argv[optind++], &str);
1184 SetArrayItem(conf.userdata, optarg, str);
1188 missedOptionArg = true;
1192 missedOptionArg = true;
1196 printf("?? getopt returned character code 0%o ??\n", c);
1202 printf ("non-option ARGV-elements: ");
1203 while (optind < argc)
1204 printf ("%s ", argv[optind++]);
1206 exit(PARAMETER_PARSING_ERR_CODE);
1209 if (missedOptionArg || !CheckParametersSet(&req))
1211 //printf("Parameter needed\n");
1213 exit(PARAMETER_PARSING_ERR_CODE);
1216 const int rLen = 20000;
1218 memset(rstr, 0, rLen);
1221 return ProcessSendMessage(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req.usrMsg.data());
1223 return ProcessSetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), conf, stat);
1225 //-----------------------------------------------------------------------------
1226 int main(int argc, char **argv)
1228 SGCONF::CONFIG config;
1230 SGCONF::OPTION_BLOCKS blocks;
1231 blocks.Add("General options")
1232 .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), "<config file>"), "override default config file")
1233 .Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit")
1234 .Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit")
1235 .Add("v", "version", SGCONF::MakeFunc0Action(Version), "\t\tshow version information and exit");
1236 SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options")
1237 .Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "<address>"), "\t\thost to connect")
1238 .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "\t\tport to connect")
1239 .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
1240 .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "\tpassword for the administrative login")
1241 .Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
1244 SGCONF::PARSER_STATE state(false, argc, argv);
1248 state = blocks.Parse(--argc, ++argv); // Skipping self name
1250 catch (const SGCONF::OPTION::ERROR& ex)
1252 std::cerr << ex.what() << "\n";
1261 std::cerr << "Unknown option: '" << *state.argv << "'\n";
1267 SGCONF::CONFIG configOverride(config);
1269 if (config.configFile.empty())
1271 const char * mainConfigFile = "/etc/sgconf/sgconf.conf";
1272 if (access(mainConfigFile, R_OK) == 0)
1273 block.ParseFile(mainConfigFile);
1274 ReadUserConfigFile(block);
1278 block.ParseFile(config.configFile.data());
1281 config = configOverride;
1283 catch (const std::exception& ex)
1285 std::cerr << ex.what() << "\n";
1289 std::cerr << "Config: " << config.Serialize() << std::endl;
1302 exit(PARAMETER_PARSING_ERR_CODE);
1305 if (strcmp(argv[1], "get") == 0)
1308 return mainGet(argc - 1, argv + 1);
1310 else if (strcmp(argv[1], "set") == 0)
1313 if (mainSet(argc - 1, argv + 1) )
1320 exit(PARAMETER_PARSING_ERR_CODE);
1322 return UNKNOWN_ERR_CODE;
1324 //-----------------------------------------------------------------------------
1339 void UsageImpl(bool full)
1341 std::cout << "sgconf is the Stargazer management utility.\n\n"
1343 << "\tsgconf [options]\n\n"
1344 << "General options:\n"
1345 << "\t-c, --config <config file>\t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n"
1346 << "\t-h, --help\t\t\t\tshow this help and exit\n"
1347 << "\t--help-all\t\t\t\tshow full help and exit\n"
1348 << "\t-v, --version\t\t\t\tshow version information and exit\n\n";
1353 UsageServices(full);
1354 UsageCorporations(full);
1356 //-----------------------------------------------------------------------------
1357 void UsageConnection()
1359 std::cout << "Connection options:\n"
1360 << "\t-s, --server <address>\t\t\thost to connect (ip or domain name, default: \"localhost\")\n"
1361 << "\t-p, --port <port>\t\t\tport to connect (default: \"5555\")\n"
1362 << "\t-u, --username <username>\t\tadministrative login (default: \"admin\")\n"
1363 << "\t-w, --userpass <password>\t\tpassword for administrative login\n"
1364 << "\t-a, --address <connection string>\tconnection params as a single string in format: <login>:<password>@<host>:<port>\n\n";
1366 //-----------------------------------------------------------------------------
1367 void UsageAdmins(bool full)
1369 std::cout << "Admins management options:\n"
1370 << "\t--get-admins\t\t\t\tget a list of admins (subsequent options will define what to show)\n";
1372 std::cout << "\t\t--login\t\t\t\tshow admin's login\n"
1373 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1374 std::cout << "\t--get-admin\t\t\t\tget the information about admin\n";
1376 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to show\n"
1377 << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1378 std::cout << "\t--add-admin\t\t\t\tadd a new admin\n";
1380 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to add\n"
1381 << "\t\t--password <password>\t\tpassword of the admin to add\n"
1382 << "\t\t--priv <priv number>\t\tpriviledges of the admin to add\n\n";
1383 std::cout << "\t--del-admin\t\t\t\tdelete an existing admin\n";
1385 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to delete\n\n";
1386 std::cout << "\t--chg-admin\t\t\t\tchange an existing admin\n";
1388 std::cout << "\t\t--login <login>\t\t\tlogin of the admin to change\n"
1389 << "\t\t--priv <priv number>\t\tnew priviledges\n\n";
1391 //-----------------------------------------------------------------------------
1392 void UsageTariffs(bool full)
1394 std::cout << "Tariffs management options:\n"
1395 << "\t--get-tariffs\t\t\t\tget a list of tariffs (subsequent options will define what to show)\n";
1397 std::cout << "\t\t--name\t\t\t\tshow tariff's name\n"
1398 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1399 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1400 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1401 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1402 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1403 std::cout << "\t--get-tariff\t\t\t\tget the information about tariff\n";
1405 std::cout << "\t\t--name <name>\t\t\tname of the tariff to show\n"
1406 << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1407 << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1408 << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1409 << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1410 << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1411 std::cout << "\t--add-tariff\t\t\t\tadd a new tariff\n";
1413 std::cout << "\t\t--name <name>\t\t\tname of the tariff to add\n"
1414 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1415 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1416 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1417 << "\t\t--traff-type <type>\t\twhat type of traffi will be accounted by the tariff\n"
1418 << "\t\t--times <times>\t\t\tslash-separated list of \"day\" time-spans (in form \"hh:mm-hh:mm\") for each direction\n"
1419 << "\t\t--prices-day-a <prices>\t\tslash-separated list of prices for \"day\" traffic before threshold for each direction\n"
1420 << "\t\t--prices-night-a <prices>\tslash-separated list of prices for \"night\" traffic before threshold for each direction\n"
1421 << "\t\t--prices-day-b <prices>\t\tslash-separated list of prices for \"day\" traffic after threshold for each direction\n"
1422 << "\t\t--prices-night-b <prices>\tslash-separated list of prices for \"night\" traffic after threshold for each direction\n"
1423 << "\t\t--single-prices <yes|no>\tslash-separated list of \"single price\" flags for each direction\n"
1424 << "\t\t--no-discounts <yes|no>\t\tslash-separated list of \"no discount\" flags for each direction\n"
1425 << "\t\t--thresholds <thresholds>\tslash-separated list of thresholds (in Mb) for each direction\n\n";
1426 std::cout << "\t--del-tariff\t\t\t\tdelete an existing tariff\n";
1428 std::cout << "\t\t--name <name>\t\t\tname of the tariff to delete\n\n";
1429 std::cout << "\t--chg-tariff\t\t\t\tchange an existing tariff\n";
1431 std::cout << "\t\t--name <name>\t\t\tname of the tariff to change\n"
1432 << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1433 << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1434 << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1435 << "\t\t--traff-type <type>\t\twhat type of traffix will be accounted by the tariff\n"
1436 << "\t\t--dir <N>\t\t\tnumber of direction data to change\n"
1437 << "\t\t\t--time <time>\t\t\"day\" time-span (in form \"hh:mm-hh:mm\")\n"
1438 << "\t\t\t--price-day-a <price>\tprice for \"day\" traffic before threshold\n"
1439 << "\t\t\t--price-night-a <price>\tprice for \"night\" traffic before threshold\n"
1440 << "\t\t\t--price-day-b <price>\tprice for \"day\" traffic after threshold\n"
1441 << "\t\t\t--price-night-b <price>\tprice for \"night\" traffic after threshold\n"
1442 << "\t\t\t--single-price <yes|no>\t\"single price\" flag\n"
1443 << "\t\t\t--no-discount <yes|no>\t\"no discount\" flag\n"
1444 << "\t\t\t--threshold <threshold>\tthreshold (in Mb)\n\n";
1446 //-----------------------------------------------------------------------------
1447 void UsageUsers(bool full)
1449 std::cout << "Users management options:\n"
1450 << "\t--get-users\t\t\t\tget a list of users (subsequent options will define what to show)\n";
1452 std::cout << "\n\n";
1453 std::cout << "\t--get-user\t\t\t\tget the information about user\n";
1455 std::cout << "\n\n";
1456 std::cout << "\t--add-user\t\t\t\tadd a new user\n";
1458 std::cout << "\n\n";
1459 std::cout << "\t--del-user\t\t\t\tdelete an existing user\n";
1461 std::cout << "\n\n";
1462 std::cout << "\t--chg-user\t\t\t\tchange an existing user\n";
1464 std::cout << "\n\n";
1465 std::cout << "\t--check-user\t\t\t\tcheck credentials is valid\n";
1467 std::cout << "\n\n";
1468 std::cout << "\t--send-message\t\t\t\tsend a message to a user\n";
1470 std::cout << "\n\n";
1472 //-----------------------------------------------------------------------------
1473 void UsageServices(bool full)
1475 std::cout << "Services management options:\n"
1476 << "\t--get-services\t\t\t\tget a list of services (subsequent options will define what to show)\n";
1478 std::cout << "\t\t--name\t\t\t\tshow service's name\n"
1479 << "\t\t--comment\t\t\tshow a comment to the service\n"
1480 << "\t\t--cost\t\t\t\tshow service's cost\n"
1481 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1482 std::cout << "\t--get-service\t\t\t\tget the information about service\n";
1484 std::cout << "\t\t--name <name>\t\t\tname of the service to show\n"
1485 << "\t\t--comment\t\t\tshow a comment to the service\n"
1486 << "\t\t--cost\t\t\t\tshow service's cost\n"
1487 << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1488 std::cout << "\t--add-service\t\t\t\tadd a new service\n";
1490 std::cout << "\t\t--name <name>\t\t\tname of the service to add\n"
1491 << "\t\t--comment <comment>\t\ta comment to the service\n"
1492 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1493 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1494 std::cout << "\t--del-service\t\t\t\tdelete an existing service\n";
1496 std::cout << "\t\t--name <name>\t\t\tname of the service to delete\n\n";
1497 std::cout << "\t--chg-service\t\t\t\tchange an existing service\n";
1499 std::cout << "\t\t--name <name>\t\t\tname of the service to change\n"
1500 << "\t\t--comment <comment>\t\ta comment to the service\n"
1501 << "\t\t--cost <cost>\t\t\tservice's cost\n"
1502 << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1504 //-----------------------------------------------------------------------------
1505 void UsageCorporations(bool full)
1507 std::cout << "Corporations management options:\n"
1508 << "\t--get-corporations\t\t\tget a list of corporations (subsequent options will define what to show)\n";
1510 std::cout << "\t\t--name\t\t\t\tshow corporation's name\n"
1511 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1512 std::cout << "\t--get-corp\t\t\t\tget the information about corporation\n";
1514 std::cout << "\t\t--name <name>\t\t\tname of the corporation to show\n"
1515 << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1516 std::cout << "\t--add-corp\t\t\t\tadd a new corporation\n";
1518 std::cout << "\t\t--name <name>\t\t\tname of the corporation to add\n"
1519 << "\t\t--cash <cash>\t\t\tinitial corporation's cash (default: \"0\")\n\n";
1520 std::cout << "\t--del-corp\t\t\t\tdelete an existing corporation\n";
1522 std::cout << "\t\t--name <name>\t\t\tname of the corporation to delete\n\n";
1523 std::cout << "\t--chg-corp\t\t\t\tchange an existing corporation\n";
1525 std::cout << "\t\t--name <name>\t\t\tname of the corporation to change\n"
1526 << "\t\t--add-cash <amount>[:<message>]\tadd cash to the corporation's account and optional comment message\n"
1527 << "\t\t--set-cash <cash>[:<message>]\tnew corporation's cash and optional comment message\n\n";
1532 std::cout << "sgconf, version: 2.0.0-alpha.\n";
1535 } // namespace anonymous