]> git.stg.codes - stg.git/blob - projects/sgconf/main.cpp
Implemented some operations with tariffs.
[stg.git] / projects / sgconf / main.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "request.h"
23 #include "common_sg.h"
24 #include "sg_error_codes.h"
25
26 #include "xml.h"
27 #include "admins.h"
28 #include "tariffs.h"
29 #include "options.h"
30 #include "actions.h"
31 #include "config.h"
32
33 #include "stg/user_conf.h"
34 #include "stg/user_stat.h"
35 #include "stg/common.h"
36
37 #include <cerrno>
38 #include <clocale>
39 #include <cstdio>
40 #include <cstdlib>
41 #include <cstring>
42 #include <string>
43 #include <sstream>
44
45 #include <unistd.h>
46 #include <getopt.h>
47 #include <iconv.h>
48 #include <langinfo.h>
49
50 namespace
51 {
52
53 template <typename T>
54 struct ARRAY_TYPE
55 {
56 typedef typename T::value_type type;
57 };
58
59 template <typename T>
60 struct ARRAY_TYPE<T[]>
61 {
62 typedef T type;
63 };
64
65 template <typename T, size_t N>
66 struct ARRAY_TYPE<T[N]>
67 {
68 typedef T type;
69 };
70
71 template <typename T>
72 struct nullary_function
73 {
74 typedef T result_type;
75 };
76
77 template <typename F>
78 class binder0 : public nullary_function<typename F::result_type>
79 {
80     public:
81         binder0(const F & func, const typename F::argument_type & arg)
82             : m_func(func), m_arg(arg) {}
83         typename F::result_type operator()() const { return m_func(m_arg); }
84     private:
85         F m_func;
86         typename F::argument_type m_arg;
87 };
88
89 template <typename F>
90 inline
91 binder0<F> bind0(const F & func, const typename F::argument_type & arg)
92 {
93 return binder0<F>(func, arg);
94 }
95
96 template <typename C, typename A, typename R>
97 class METHOD1_ADAPTER : public std::unary_function<A, R>
98 {
99     public:
100         METHOD1_ADAPTER(R (C::* func)(A), C & obj) : m_func(func), m_obj(obj) {}
101         R operator()(A arg) { return (m_obj.*m_func)(arg); }
102     private:
103         R (C::* m_func)(A);
104         C & m_obj;
105 };
106
107 template <typename C, typename A, typename R>
108 class CONST_METHOD1_ADAPTER : public std::unary_function<A, R>
109 {
110     public:
111         CONST_METHOD1_ADAPTER(R (C::* func)(A) const, C & obj) : m_func(func), m_obj(obj) {}
112         R operator()(A arg) const { return (m_obj.*m_func)(arg); }
113     private:
114         R (C::* m_func)(A) const;
115         C & m_obj;
116 };
117
118 template <typename C, typename A, typename R>
119 METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A), C & obj)
120 {
121 return METHOD1_ADAPTER<C, A, R>(func, obj);
122 }
123
124 template <typename C, typename A, typename R>
125 CONST_METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A) const, C & obj)
126 {
127 return CONST_METHOD1_ADAPTER<C, A, R>(func, obj);
128 }
129
130 template <typename T>
131 bool SetArrayItem(T & array, const char * index, const typename ARRAY_TYPE<T>::type & value)
132 {
133 size_t pos = 0;
134 if (str2x(index, pos))
135     return false;
136 array[pos] = value;
137 return true;
138 }
139
140 void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * /*data*/)
141 {
142 if (!result)
143     {
144     std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl;
145     return;
146     }
147 SGCONF::PrintXML(response);
148 }
149
150 void Usage();
151 void UsageAll();
152 void UsageImpl(bool full);
153 void UsageConnection();
154 void UsageAdmins(bool full);
155 void UsageTariffs(bool full);
156 void UsageUsers(bool full);
157 void UsageServices(bool full);
158 void UsageCorporations(bool full);
159
160 void Version();
161
162 void ReadUserConfigFile(SGCONF::OPTION_BLOCK & block)
163 {
164 std::vector<std::string> paths;
165 const char * configHome = getenv("XDG_CONFIG_HOME");
166 if (configHome == NULL)
167     {
168     const char * home = getenv("HOME");
169     if (home == NULL)
170         return;
171     paths.push_back(std::string(home) + "/.config/sgconf/sgconf.conf");
172     paths.push_back(std::string(home) + "/.sgconf/sgconf.conf");
173     }
174 else
175     paths.push_back(std::string(configHome) + "/sgconf/sgconf.conf");
176 for (std::vector<std::string>::const_iterator it = paths.begin(); it != paths.end(); ++it)
177     if (access(it->c_str(), R_OK) == 0)
178         {
179         block.ParseFile(*it);
180         return;
181         }
182 }
183
184 } // namespace anonymous
185
186 namespace SGCONF
187 {
188
189 class CONFIG_ACTION : public ACTION
190 {
191     public:
192         CONFIG_ACTION(SGCONF::CONFIG & config,
193                       const std::string & paramDescription)
194             : m_config(config),
195               m_description(paramDescription)
196         {}
197
198         virtual ACTION * Clone() const { return new CONFIG_ACTION(*this); }
199
200         virtual std::string ParamDescription() const { return m_description; }
201         virtual std::string DefaultDescription() const { return ""; }
202         virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
203         virtual PARSER_STATE Parse(int argc, char ** argv);
204
205     private:
206         SGCONF::CONFIG & m_config;
207         std::string m_description;
208         OPTION_BLOCK m_suboptions;
209
210         void ParseCredentials(const std::string & credentials);
211         void ParseHostAndPort(const std::string & hostAndPort);
212 };
213
214 typedef bool (* API_FUNCTION) (const SGCONF::CONFIG &,
215                                const std::string &,
216                                const std::map<std::string, std::string> &);
217
218 class COMMAND
219 {
220     public:
221         COMMAND(API_FUNCTION funPtr,
222                 const std::string & arg,
223                 const std::map<std::string, std::string> & options)
224             : m_funPtr(funPtr),
225               m_arg(arg),
226               m_options(options)
227         {}
228         bool Execute(const SGCONF::CONFIG & config) const
229         {
230             return m_funPtr(config, m_arg, m_options);
231         }
232
233     private:
234         API_FUNCTION m_funPtr;
235         std::string m_arg;
236         std::map<std::string, std::string> m_options;
237 };
238
239 class COMMANDS
240 {
241     public:
242         void Add(API_FUNCTION funPtr,
243                  const std::string & arg,
244                  const std::map<std::string, std::string> & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); }
245         bool Execute(const SGCONF::CONFIG & config) const
246         {
247             std::list<COMMAND>::const_iterator it(m_commands.begin());
248             bool res = true;
249             while (it != m_commands.end() && res)
250             {
251                 res = res && it->Execute(config);
252                 ++it;
253             }
254             return res;
255         }
256     private:
257         std::list<COMMAND> m_commands;
258 };
259
260 class API_ACTION : public ACTION
261 {
262     public:
263         API_ACTION(COMMANDS & commands,
264                    const std::string & paramDescription,
265                    bool needArgument,
266                    const OPTION_BLOCK& suboptions,
267                    API_FUNCTION funPtr)
268             : m_commands(commands),
269               m_description(paramDescription),
270               m_argument(needArgument ? "1" : ""), // Hack
271               m_suboptions(suboptions),
272               m_funPtr(funPtr)
273         {}
274         API_ACTION(COMMANDS & commands,
275                    const std::string & paramDescription,
276                    bool needArgument,
277                    API_FUNCTION funPtr)
278             : m_commands(commands),
279               m_description(paramDescription),
280               m_argument(needArgument ? "1" : ""), // Hack
281               m_funPtr(funPtr)
282         {}
283
284         virtual ACTION * Clone() const { return new API_ACTION(*this); }
285
286         virtual std::string ParamDescription() const { return m_description; }
287         virtual std::string DefaultDescription() const { return ""; }
288         virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
289         virtual PARSER_STATE Parse(int argc, char ** argv)
290         {
291         PARSER_STATE state(false, argc, argv);
292         if (!m_argument.empty())
293             {
294             if (argc == 0 ||
295                 argv == NULL ||
296                 *argv == NULL)
297                 throw ERROR("Missing argument.");
298             m_argument = *argv;
299             --state.argc;
300             ++state.argv;
301             }
302         m_suboptions.Parse(state.argc, state.argv);
303         m_commands.Add(m_funPtr, m_argument, m_params);
304         return state;
305         }
306
307     private:
308         COMMANDS & m_commands;
309         std::string m_description;
310         std::string m_argument;
311         OPTION_BLOCK m_suboptions;
312         std::map<std::string, std::string> m_params;
313         API_FUNCTION m_funPtr;
314 };
315
316 PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv)
317 {
318 if (argc == 0 ||
319     argv == NULL ||
320     *argv == NULL)
321     throw ERROR("Missing argument.");
322 char * pos = strchr(*argv, '@');
323 if (pos != NULL)
324     {
325     ParseCredentials(std::string(*argv, pos));
326     ParseHostAndPort(std::string(pos + 1));
327     }
328 else
329     {
330     ParseHostAndPort(std::string(*argv));
331     }
332 return PARSER_STATE(false, --argc, ++argv);
333 }
334
335 void CONFIG_ACTION::ParseCredentials(const std::string & credentials)
336 {
337 std::string::size_type pos = credentials.find_first_of(':');
338 if (pos != std::string::npos)
339     {
340     m_config.userName = credentials.substr(0, pos);
341     m_config.userPass = credentials.substr(pos + 1);
342     }
343 else
344     {
345     m_config.userName = credentials;
346     }
347 }
348
349 void CONFIG_ACTION::ParseHostAndPort(const std::string & hostAndPort)
350 {
351 std::string::size_type pos = hostAndPort.find_first_of(':');
352 if (pos != std::string::npos)
353     {
354     m_config.server = hostAndPort.substr(0, pos);
355     uint16_t port = 0;
356     if (str2x(hostAndPort.substr(pos + 1), port))
357         throw ERROR("Invalid port value: '" + hostAndPort.substr(pos + 1) + "'");
358     m_config.port = port;
359     }
360 else
361     {
362     m_config.server = hostAndPort;
363     }
364 }
365
366 inline
367 CONFIG_ACTION * MakeParamAction(SGCONF::CONFIG & config,
368                                 const std::string & paramDescription)
369 {
370 return new CONFIG_ACTION(config, paramDescription);
371 }
372
373 inline
374 ACTION * MakeAPIAction(COMMANDS & commands,
375                        const std::string & paramDescription,
376                        bool needArgument,
377                        API_FUNCTION funPtr)
378 {
379 return new API_ACTION(commands, paramDescription, needArgument, funPtr);
380 }
381
382 inline
383 ACTION * MakeAPIAction(COMMANDS & commands,
384                        API_FUNCTION funPtr)
385 {
386 return new API_ACTION(commands, "", false, funPtr);
387 }
388
389 bool RawXMLFunction(const SGCONF::CONFIG & config,
390                     const std::string & arg,
391                     const std::map<std::string, std::string> & /*options*/)
392 {
393     STG::SERVCONF proto(config.server.data(),
394                         config.port.data(),
395                         config.userName.data(),
396                         config.userPass.data());
397     return proto.RawXML(arg, RawXMLCallback, NULL) == STG::st_ok;
398 }
399
400 } // namespace SGCONF
401
402 time_t stgTime;
403
404 struct option long_options_get[] = {
405 {"server",      1, 0, 's'},  //Server
406 {"port",        1, 0, 'p'},  //Port
407 {"admin",       1, 0, 'a'},  //Admin
408 {"admin_pass",  1, 0, 'w'},  //passWord
409 {"user",        1, 0, 'u'},  //User
410 {"addcash",     0, 0, 'c'},  //Add Cash
411 //{"setcash",     0, 0, 'v'},  //Set Cash
412 {"credit",      0, 0, 'r'},  //cRedit
413 {"tariff",      0, 0, 't'},  //Tariff
414 {"message",     0, 0, 'm'},  //message
415 {"password",    0, 0, 'o'},  //password
416 {"down",        0, 0, 'd'},  //down
417 {"passive",     0, 0, 'i'},  //passive
418 {"disable-stat",0, 0, 'S'},  //disable detail stat
419 {"always-online",0, 0, 'O'}, //always online
420 {"session-upload",   1, 0, 500},  //SU0
421 {"session-download", 1, 0, 501},  //SD0
422 {"month-upload",     1, 0, 502},  //MU0
423 {"month-download",   1, 0, 503},  //MD0
424
425 {"user-data",   1, 0, 700},  //UserData0
426
427 {"prepaid",     0, 0, 'e'},  //prepaid traff
428 {"create",      0, 0, 'n'},  //create
429 {"delete",      0, 0, 'l'},  //delete
430
431 {"note",        0, 0, 'N'},  //Note
432 {"name",        0, 0, 'A'},  //nAme
433 {"address",     0, 0, 'D'},  //aDdress
434 {"email",       0, 0, 'L'},  //emaiL
435 {"phone",       0, 0, 'P'},  //phone
436 {"group",       0, 0, 'G'},  //Group
437 {"ip",          0, 0, 'I'},  //IP-address of user
438 {"authorized-by",0, 0, 800}, //always online
439
440 {0, 0, 0, 0}};
441
442 struct option long_options_set[] = {
443 {"server",      1, 0, 's'},  //Server
444 {"port",        1, 0, 'p'},  //Port
445 {"admin",       1, 0, 'a'},  //Admin
446 {"admin_pass",  1, 0, 'w'},  //passWord
447 {"user",        1, 0, 'u'},  //User
448 {"addcash",     1, 0, 'c'},  //Add Cash
449 {"setcash",     1, 0, 'v'},  //Set Cash
450 {"credit",      1, 0, 'r'},  //cRedit
451 {"tariff",      1, 0, 't'},  //Tariff
452 {"message",     1, 0, 'm'},  //message
453 {"password",    1, 0, 'o'},  //password
454 {"down",        1, 0, 'd'},  //down
455 {"passive",     1, 0, 'i'},  //passive
456 {"disable-stat",1, 0, 'S'},  //disable detail stat
457 {"always-online",1, 0, 'O'},  //always online
458 {"session-upload",   1, 0, 500},  //U0
459 {"session-download", 1, 0, 501},  //U1
460 {"month-upload",     1, 0, 502},  //U2
461 {"month-download",   1, 0, 503},  //U3
462
463 {"user-data",        1, 0, 700},  //UserData
464
465 {"prepaid",     1, 0, 'e'},  //prepaid traff
466 {"create",      1, 0, 'n'},  //create
467 {"delete",      1, 0, 'l'},  //delete
468
469 {"note",        1, 0, 'N'},  //Note
470 {"name",        1, 0, 'A'},  //nAme
471 {"address",     1, 0, 'D'},  //aDdress
472 {"email",       1, 0, 'L'},  //emaiL
473 {"phone",       1, 0, 'P'},  //phone
474 {"group",       1, 0, 'G'},  //Group
475 {"ip",          0, 0, 'I'},  //IP-address of user
476
477 {0, 0, 0, 0}};
478
479 //-----------------------------------------------------------------------------
480 CASH_INFO ParseCash(const char * str)
481 {
482 //-c 123.45:log message
483 std::string cashString;
484 std::string message;
485 const char * pos = strchr(str, ':');
486 if (pos != NULL)
487     {
488     cashString.append(str, pos);
489     message.append(pos + 1);
490     }
491 else
492     cashString = str;
493
494 double cash = 0;
495 if (strtodouble2(cashString, cash) != 0)
496     {
497     printf("Incorrect cash value %s\n", str);
498     exit(PARAMETER_PARSING_ERR_CODE);
499     }
500
501 return CASH_INFO(cash, message);
502 }
503 //-----------------------------------------------------------------------------
504 double ParseCredit(const char * c)
505 {
506 double credit;
507 if (strtodouble2(c, credit) != 0)
508     {
509     printf("Incorrect credit value %s\n", c);
510     exit(PARAMETER_PARSING_ERR_CODE);
511     }
512
513 return credit;
514 }
515 //-----------------------------------------------------------------------------
516 double ParsePrepaidTraffic(const char * c)
517 {
518 double credit;
519 if (strtodouble2(c, credit) != 0)
520     {
521     printf("Incorrect prepaid traffic value %s\n", c);
522     exit(PARAMETER_PARSING_ERR_CODE);
523     }
524
525 return credit;
526 }
527 //-----------------------------------------------------------------------------
528 int64_t ParseTraff(const char * c)
529 {
530 int64_t traff;
531 if (str2x(c, traff) != 0)
532     {
533     printf("Incorrect credit value %s\n", c);
534     exit(PARAMETER_PARSING_ERR_CODE);
535     }
536
537 return traff;
538 }
539 //-----------------------------------------------------------------------------
540 bool ParseDownPassive(const char * dp)
541 {
542 if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0')))
543     {
544     printf("Incorrect value %s\n", dp);
545     exit(PARAMETER_PARSING_ERR_CODE);
546     }
547
548 return dp[0] - '0';
549 }
550 //-----------------------------------------------------------------------------
551 void ParseTariff(const char * str, RESETABLE<std::string> & tariffName, RESETABLE<std::string> & nextTariff)
552 {
553 const char * pos = strchr(str, ':');
554 if (pos != NULL)
555     {
556     std::string tariff(str, pos);
557     if (strcmp(pos + 1, "now") == 0)
558         tariffName = tariff;
559     else if (strcmp(pos + 1, "delayed") == 0)
560         nextTariff = tariff;
561     else
562         {
563         printf("Incorrect tariff value '%s'. Should be '<tariff>', '<tariff>:now' or '<tariff>:delayed'.\n", str);
564         exit(PARAMETER_PARSING_ERR_CODE);
565         }
566     }
567 else
568     tariffName = str;
569 }
570 //-----------------------------------------------------------------------------
571 time_t ParseCreditExpire(const char * str)
572 {
573 struct tm brokenTime;
574
575 brokenTime.tm_wday = 0;
576 brokenTime.tm_yday = 0;
577 brokenTime.tm_isdst = 0;
578 brokenTime.tm_hour = 0;
579 brokenTime.tm_min = 0;
580 brokenTime.tm_sec = 0;
581
582 stg_strptime(str, "%Y-%m-%d", &brokenTime);
583
584 return stg_timegm(&brokenTime);
585 }
586 //-----------------------------------------------------------------------------
587 void ParseAnyString(const char * c, string * msg, const char * enc)
588 {
589 iconv_t cd;
590 char * ob = new char[strlen(c) + 1];
591 char * ib = new char[strlen(c) + 1];
592
593 strcpy(ib, c);
594
595 char * outbuf = ob;
596 char * inbuf = ib;
597
598 setlocale(LC_ALL, "");
599
600 char charsetF[255];
601 strncpy(charsetF, nl_langinfo(CODESET), 255);
602
603 const char * charsetT = enc;
604
605 size_t nconv = 1;
606
607 size_t insize = strlen(ib);
608 size_t outsize = strlen(ib);
609
610 insize = strlen(c);
611
612 cd = iconv_open(charsetT, charsetF);
613 if (cd == (iconv_t) -1)
614     {
615     if (errno == EINVAL)
616         {
617         printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
618         *msg = c;
619         return;
620         }
621     else
622         printf("error iconv_open\n");
623
624     exit(ICONV_ERR_CODE);
625     }
626
627 #if defined(FREE_BSD) || defined(FREE_BSD5)
628 nconv = iconv (cd, (const char**)&inbuf, &insize, &outbuf, &outsize);
629 #else
630 nconv = iconv (cd, &inbuf, &insize, &outbuf, &outsize);
631 #endif
632 //printf("nconv=%d outsize=%d\n", nconv, outsize);
633 if (nconv == (size_t) -1)
634     {
635     if (errno != EINVAL)
636         {
637         printf("iconv error\n");
638         exit(ICONV_ERR_CODE);
639         }
640     }
641
642 *outbuf = L'\0';
643
644 iconv_close(cd);
645 *msg = ob;
646
647 delete[] ob;
648 delete[] ib;
649 }
650 //-----------------------------------------------------------------------------
651 void CreateRequestSet(REQUEST * req, char * r)
652 {
653 const int strLen = 10024;
654 char str[strLen];
655 memset(str, 0, strLen);
656
657 r[0] = 0;
658
659 if (!req->usrMsg.empty())
660     {
661     string msg;
662     Encode12str(msg, req->usrMsg.data());
663     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());
664     //sprintf(str, "<message login=\"%s\" priority=\"0\" text=\"%s\"/>\n", req->login, msg);
665     strcat(r, str);
666     return;
667     }
668
669 if (req->deleteUser)
670     {
671     sprintf(str, "<DelUser login=\"%s\"/>", req->login.const_data().c_str());
672     strcat(r, str);
673     //printf("%s\n", r);
674     return;
675     }
676
677 if (req->createUser)
678     {
679     sprintf(str, "<AddUser> <login value=\"%s\"/> </AddUser>", req->login.const_data().c_str());
680     strcat(r, str);
681     //printf("%s\n", r);
682     return;
683     }
684
685 strcat(r, "<SetUser>\n");
686 sprintf(str, "<login value=\"%s\"/>\n", req->login.const_data().c_str());
687 strcat(r, str);
688 if (!req->credit.empty())
689     {
690     sprintf(str, "<credit value=\"%f\"/>\n", req->credit.const_data());
691     strcat(r, str);
692     }
693
694 if (!req->creditExpire.empty())
695     {
696     sprintf(str, "<creditExpire value=\"%ld\"/>\n", req->creditExpire.const_data());
697     strcat(r, str);
698     }
699
700 if (!req->prepaidTraff.empty())
701     {
702     sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
703     strcat(r, str);
704     }
705
706 if (!req->cash.empty())
707     {
708     string msg;
709     Encode12str(msg, req->message);
710     sprintf(str, "<cash add=\"%f\" msg=\"%s\"/>\n", req->cash.const_data(), msg.c_str());
711     strcat(r, str);
712     }
713
714 if (!req->setCash.empty())
715     {
716     string msg;
717     Encode12str(msg, req->message);
718     sprintf(str, "<cash set=\"%f\" msg=\"%s\"/>\n", req->setCash.const_data(), msg.c_str());
719     strcat(r, str);
720     }
721
722 if (!req->usrPasswd.empty())
723     {
724     sprintf(str, "<password value=\"%s\" />\n", req->usrPasswd.const_data().c_str());
725     strcat(r, str);
726     }
727
728 if (!req->down.empty())
729     {
730     sprintf(str, "<down value=\"%d\" />\n", req->down.const_data());
731     strcat(r, str);
732     }
733
734 if (!req->passive.empty())
735     {
736     sprintf(str, "<passive value=\"%d\" />\n", req->passive.const_data());
737     strcat(r, str);
738     }
739
740 if (!req->disableDetailStat.empty())
741     {
742     sprintf(str, "<disableDetailStat value=\"%d\" />\n", req->disableDetailStat.const_data());
743     strcat(r, str);
744     }
745
746 if (!req->alwaysOnline.empty())
747     {
748     sprintf(str, "<aonline value=\"%d\" />\n", req->alwaysOnline.const_data());
749     strcat(r, str);
750     }
751
752 // IP-address of user
753 if (!req->ips.empty())
754     {
755     sprintf(str, "<ip value=\"%s\" />\n", req->ips.const_data().c_str());
756     strcat(r, str);
757     }
758
759 int uPresent = false;
760 int dPresent = false;
761 for (int i = 0; i < DIR_NUM; i++)
762     {
763     if (!req->monthUpload[i].empty())
764         {
765         if (!uPresent && !dPresent)
766             {
767             sprintf(str, "<traff ");
768             strcat(r, str);
769             uPresent = true;
770             }
771
772         stringstream ss;
773         ss << req->monthUpload[i].const_data();
774         //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
775         sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
776         strcat(r, str);
777         }
778     if (!req->monthDownload[i].empty())
779         {
780         if (!uPresent && !dPresent)
781             {
782             sprintf(str, "<traff ");
783             strcat(r, str);
784             dPresent = true;
785             }
786
787         stringstream ss;
788         ss << req->monthDownload[i].const_data();
789         sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
790         strcat(r, str);
791         }
792     if (!req->sessionUpload[i].empty())
793         {
794         if (!uPresent && !dPresent)
795             {
796             sprintf(str, "<traff ");
797             strcat(r, str);
798             uPresent = true;
799             }
800
801         stringstream ss;
802         ss << req->sessionUpload[i].const_data();
803         //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
804         sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
805         strcat(r, str);
806         }
807     if (!req->sessionDownload[i].empty())
808         {
809         if (!uPresent && !dPresent)
810             {
811             sprintf(str, "<traff ");
812             strcat(r, str);
813             dPresent = true;
814             }
815
816         stringstream ss;
817         ss << req->sessionDownload[i].const_data();
818         sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
819         strcat(r, str);
820         }
821     }
822 if (uPresent || dPresent)
823     {
824     strcat(r, "/>");
825     }
826
827 //printf("%s\n", r);
828
829 if (!req->tariff.empty())
830     {
831     switch (req->chgTariff)
832         {
833         case TARIFF_NOW:
834             sprintf(str, "<tariff now=\"%s\"/>\n", req->tariff.const_data().c_str());
835             strcat(r, str);
836             break;
837         case TARIFF_REC:
838             sprintf(str, "<tariff recalc=\"%s\"/>\n", req->tariff.const_data().c_str());
839             strcat(r, str);
840             break;
841         case TARIFF_DEL:
842             sprintf(str, "<tariff delayed=\"%s\"/>\n", req->tariff.const_data().c_str());
843             strcat(r, str);
844             break;
845         }
846
847     }
848
849 if (!req->note.empty())
850     {
851     string note;
852     Encode12str(note, req->note.data());
853     sprintf(str, "<note value=\"%s\"/>", note.c_str());
854     strcat(r, str);
855     }
856
857 if (!req->name.empty())
858     {
859     string name;
860     Encode12str(name, req->name.data());
861     sprintf(str, "<name value=\"%s\"/>", name.c_str());
862     strcat(r, str);
863     }
864
865 if (!req->address.empty())
866     {
867     string address;
868     Encode12str(address, req->address.data());
869     sprintf(str, "<address value=\"%s\"/>", address.c_str());
870     strcat(r, str);
871     }
872
873 if (!req->email.empty())
874     {
875     string email;
876     Encode12str(email, req->email.data());
877     sprintf(str, "<email value=\"%s\"/>", email.c_str());
878     strcat(r, str);
879     }
880
881 if (!req->phone.empty())
882     {
883     string phone;
884     Encode12str(phone, req->phone.data());
885     sprintf(str, "<phone value=\"%s\"/>", phone.c_str());
886     strcat(r, str);
887     }
888
889 if (!req->group.empty())
890     {
891     string group;
892     Encode12str(group, req->group.data());
893     sprintf(str, "<group value=\"%s\"/>", group.c_str());
894     strcat(r, str);
895     }
896
897 for (int i = 0; i < USERDATA_NUM; i++)
898     {
899     if (!req->userData[i].empty())
900         {
901         string ud;
902         Encode12str(ud, req->userData[i].data());
903         sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
904         strcat(r, str);
905         }
906     }
907
908 strcat(r, "</SetUser>\n");
909 }
910 //-----------------------------------------------------------------------------
911 int CheckParameters(REQUEST * req)
912 {
913 bool su = false;
914 bool sd = false;
915 bool mu = false;
916 bool md = false;
917 bool ud = false;
918 bool a = !req->admLogin.empty()
919     && !req->admPasswd.empty()
920     && !req->server.empty()
921     && !req->port.empty()
922     && !req->login.empty();
923
924 bool b = !req->cash.empty()
925     || !req->setCash.empty()
926     || !req->credit.empty()
927     || !req->prepaidTraff.empty()
928     || !req->tariff.empty()
929     || !req->usrMsg.empty()
930     || !req->usrPasswd.empty()
931
932     || !req->note.empty()
933     || !req->name.empty()
934     || !req->address.empty()
935     || !req->email.empty()
936     || !req->phone.empty()
937     || !req->group.empty()
938     || !req->ips.empty() // IP-address of user
939
940     || !req->createUser
941     || !req->deleteUser;
942
943
944 for (int i = 0; i < DIR_NUM; i++)
945     {
946     if (req->sessionUpload[i].empty())
947         {
948         su = true;
949         break;
950         }
951     }
952
953 for (int i = 0; i < DIR_NUM; i++)
954     {
955     if (req->sessionDownload[i].empty())
956         {
957         sd = true;
958         break;
959         }
960     }
961
962 for (int i = 0; i < DIR_NUM; i++)
963     {
964     if (req->monthUpload[i].empty())
965         {
966         mu = true;
967         break;
968         }
969     }
970
971 for (int i = 0; i < DIR_NUM; i++)
972     {
973     if (req->monthDownload[i].empty())
974         {
975         md = true;
976         break;
977         }
978     }
979
980 for (int i = 0; i < DIR_NUM; i++)
981     {
982     if (req->userData[i].empty())
983         {
984         ud = true;
985         break;
986         }
987     }
988
989
990 //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
991 return a && (b || su || sd || mu || md || ud);
992 }
993 //-----------------------------------------------------------------------------
994 int CheckParametersGet(REQUEST * req)
995 {
996 return CheckParameters(req);
997 }
998 //-----------------------------------------------------------------------------
999 int CheckParametersSet(REQUEST * req)
1000 {
1001 return CheckParameters(req);
1002 }
1003 //-----------------------------------------------------------------------------
1004 bool mainGet(int argc, char **argv)
1005 {
1006 int c;
1007 REQUEST req;
1008 RESETABLE<string>   t1;
1009 int missedOptionArg = false;
1010
1011 const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISOE";
1012 int option_index = -1;
1013
1014 while (1)
1015     {
1016     option_index = -1;
1017     c = getopt_long(argc, argv, short_options_get, long_options_get, &option_index);
1018     if (c == -1)
1019         break;
1020
1021     switch (c)
1022         {
1023         case 's': //server
1024             req.server = optarg;
1025             break;
1026
1027         case 'p': //port
1028             req.port = ParseServerPort(optarg);
1029             //req.portReq = 1;
1030             break;
1031
1032         case 'a': //admin
1033             req.admLogin = ParseAdminLogin(optarg);
1034             break;
1035
1036         case 'w': //admin password
1037             req.admPasswd = ParsePassword(optarg);
1038             break;
1039
1040         case 'o': //change user password
1041             req.usrPasswd = " ";
1042             break;
1043
1044         case 'u': //user
1045             req.login = ParseUser(optarg);
1046             break;
1047
1048         case 'c': //get cash
1049             req.cash = 1;
1050             break;
1051
1052         case 'r': //credit
1053             req.credit = 1;
1054             break;
1055
1056         case 'E': //credit expire
1057             req.creditExpire = 1;
1058             break;
1059
1060         case 'd': //down
1061             req.down = 1;
1062             break;
1063
1064         case 'i': //passive
1065             req.passive = 1;
1066             break;
1067
1068         case 't': //tariff
1069             req.tariff = " ";
1070             break;
1071
1072         case 'e': //Prepaid Traffic
1073             req.prepaidTraff = 1;
1074             break;
1075
1076         case 'N': //Note
1077             req.note = " ";
1078             break;
1079
1080         case 'A': //nAme
1081             req.name = " ";
1082             break;
1083
1084         case 'D': //aDdress
1085             req.address =" ";
1086             break;
1087
1088         case 'L': //emaiL
1089             req.email = " ";
1090             break;
1091
1092         case 'P': //phone
1093             req.phone = " ";
1094             break;
1095
1096         case 'G': //Group
1097             req.group = " ";
1098             break;
1099
1100         case 'I': //IP-address of user
1101             req.ips = " ";
1102             break;
1103
1104         case 'S': //Detail stat status
1105             req.disableDetailStat = " ";
1106             break;
1107
1108         case 'O': //Always online status
1109             req.alwaysOnline = " ";
1110             break;
1111
1112         case 500: //U
1113             SetArrayItem(req.sessionUpload, optarg, 1);
1114             //req.sessionUpload[optarg] = 1;
1115             break;
1116         case 501:
1117             SetArrayItem(req.sessionDownload, optarg, 1);
1118             //req.sessionDownload[optarg] = 1;
1119             break;
1120         case 502:
1121             SetArrayItem(req.monthUpload, optarg, 1);
1122             //req.monthUpload[optarg] = 1;
1123             break;
1124         case 503:
1125             SetArrayItem(req.monthDownload, optarg, 1);
1126             //req.monthDownload[optarg] = 1;
1127             break;
1128
1129         case 700: //UserData
1130             SetArrayItem(req.userData, optarg, std::string(" "));
1131             //req.userData[optarg] = " ";
1132             break;
1133
1134         case 800:
1135             req.authBy = true;
1136             break;
1137
1138         case '?':
1139         case ':':
1140             missedOptionArg = true;
1141             break;
1142
1143         default:
1144             printf ("?? getopt returned character code 0%o ??\n", c);
1145         }
1146     }
1147
1148 if (optind < argc)
1149     {
1150     printf ("non-option ARGV-elements: ");
1151     while (optind < argc)
1152         printf ("%s ", argv[optind++]);
1153     UsageInfo();
1154     exit(PARAMETER_PARSING_ERR_CODE);
1155     }
1156
1157 if (missedOptionArg || !CheckParametersGet(&req))
1158     {
1159     //printf("Parameter needed\n");
1160     UsageInfo();
1161     exit(PARAMETER_PARSING_ERR_CODE);
1162     }
1163
1164 if (req.authBy)
1165     return ProcessAuthBy(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data());
1166 else
1167     return ProcessGetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req);
1168 }
1169 //-----------------------------------------------------------------------------
1170 bool mainSet(int argc, char **argv)
1171 {
1172 string str;
1173
1174 int c;
1175 bool isMessage = false;
1176 REQUEST req;
1177
1178 RESETABLE<string>   t1;
1179
1180 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:";
1181
1182 int missedOptionArg = false;
1183
1184 USER_CONF_RES conf;
1185 USER_STAT_RES stat;
1186 while (1)
1187     {
1188     int option_index = -1;
1189
1190     c = getopt_long(argc, argv, short_options_set, long_options_set, &option_index);
1191
1192     if (c == -1)
1193         break;
1194
1195     switch (c)
1196         {
1197         case 's': //server
1198             req.server = optarg;
1199             break;
1200
1201         case 'p': //port
1202             req.port = ParseServerPort(optarg);
1203             //req.portReq = 1;
1204             break;
1205
1206         case 'a': //admin
1207             req.admLogin = ParseAdminLogin(optarg);
1208             break;
1209
1210         case 'w': //admin password
1211             req.admPasswd = ParsePassword(optarg);
1212             break;
1213
1214         case 'o': //change user password
1215             conf.password = ParsePassword(optarg);
1216             break;
1217
1218         case 'u': //user
1219             req.login = ParseUser(optarg);
1220             break;
1221
1222         case 'c': //add cash
1223             stat.cashAdd = ParseCash(optarg);
1224             break;
1225
1226         case 'v': //set cash
1227             stat.cashSet = ParseCash(optarg);
1228             break;
1229
1230         case 'r': //credit
1231             conf.credit = ParseCredit(optarg);
1232             break;
1233
1234         case 'E': //credit expire
1235             conf.creditExpire = ParseCreditExpire(optarg);
1236             break;
1237
1238         case 'd': //down
1239             conf.disabled = ParseDownPassive(optarg);
1240             break;
1241
1242         case 'i': //passive
1243             conf.passive = ParseDownPassive(optarg);
1244             break;
1245
1246         case 't': //tariff
1247             ParseTariff(optarg, conf.tariffName, conf.nextTariff);
1248             break;
1249
1250         case 'm': //message
1251             ParseAnyString(optarg, &str);
1252             req.usrMsg = str;
1253             isMessage = true;
1254             break;
1255
1256         case 'e': //Prepaid Traffic
1257             stat.freeMb = ParsePrepaidTraffic(optarg);
1258             break;
1259
1260         case 'n': //Create User
1261             req.createUser = true;
1262             break;
1263
1264         case 'l': //Delete User
1265             req.deleteUser = true;
1266             break;
1267
1268         case 'N': //Note
1269             ParseAnyString(optarg, &str, "koi8-ru");
1270             conf.note = str;
1271             break;
1272
1273         case 'A': //nAme
1274             ParseAnyString(optarg, &str, "koi8-ru");
1275             conf.realName = str;
1276             break;
1277
1278         case 'D': //aDdress
1279             ParseAnyString(optarg, &str, "koi8-ru");
1280             conf.address = str;
1281             break;
1282
1283         case 'L': //emaiL
1284             ParseAnyString(optarg, &str, "koi8-ru");
1285             conf.email = str;
1286             break;
1287
1288         case 'P': //phone
1289             ParseAnyString(optarg, &str);
1290             conf.phone = str;
1291             break;
1292
1293         case 'G': //Group
1294             ParseAnyString(optarg, &str, "koi8-ru");
1295             conf.group = str;
1296             break;
1297
1298         case 'I': //IP-address of user
1299             ParseAnyString(optarg, &str);
1300             conf.ips = StrToIPS(str);
1301             break;
1302
1303         case 'S':
1304             conf.disabledDetailStat = ParseDownPassive(optarg);
1305             break;
1306
1307         case 'O':
1308             conf.alwaysOnline = ParseDownPassive(optarg);
1309             break;
1310
1311         case 500: //U
1312             SetArrayItem(stat.sessionUp, optarg, ParseTraff(argv[optind++]));
1313             break;
1314         case 501:
1315             SetArrayItem(stat.sessionDown, optarg, ParseTraff(argv[optind++]));
1316             break;
1317         case 502:
1318             SetArrayItem(stat.monthUp, optarg, ParseTraff(argv[optind++]));
1319             break;
1320         case 503:
1321             SetArrayItem(stat.monthDown, optarg, ParseTraff(argv[optind++]));
1322             break;
1323
1324         case 700: //UserData
1325             ParseAnyString(argv[optind++], &str);
1326             SetArrayItem(conf.userdata, optarg, str);
1327             break;
1328
1329         case '?':
1330             missedOptionArg = true;
1331             break;
1332
1333         case ':':
1334             missedOptionArg = true;
1335             break;
1336
1337         default:
1338             printf("?? getopt returned character code 0%o ??\n", c);
1339         }
1340     }
1341
1342 if (optind < argc)
1343     {
1344     printf ("non-option ARGV-elements: ");
1345     while (optind < argc)
1346         printf ("%s ", argv[optind++]);
1347     UsageConf();
1348     exit(PARAMETER_PARSING_ERR_CODE);
1349     }
1350
1351 if (missedOptionArg || !CheckParametersSet(&req))
1352     {
1353     //printf("Parameter needed\n");
1354     UsageConf();
1355     exit(PARAMETER_PARSING_ERR_CODE);
1356     }
1357
1358 const int rLen = 20000;
1359 char rstr[rLen];
1360 memset(rstr, 0, rLen);
1361
1362 if (isMessage)
1363     return ProcessSendMessage(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req.usrMsg.data());
1364
1365 return ProcessSetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), conf, stat);
1366 }
1367 //-----------------------------------------------------------------------------
1368 int main(int argc, char **argv)
1369 {
1370 SGCONF::CONFIG config;
1371 SGCONF::COMMANDS commands;
1372
1373 SGCONF::OPTION_BLOCKS blocks;
1374 blocks.Add("General options")
1375       .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), "<config file>"), "override default config file")
1376       .Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit")
1377       .Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit")
1378       .Add("v", "version", SGCONF::MakeFunc0Action(Version), "\t\tshow version information and exit");
1379 SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options")
1380       .Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "<address>"), "\t\thost to connect")
1381       .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "\t\tport to connect")
1382       .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
1383       .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "\tpassword for the administrative login")
1384       .Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
1385 blocks.Add("Raw XML")
1386       .Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", true, SGCONF::RawXMLFunction), "\tmake raw XML request");
1387 blocks.Add("Admin management options")
1388       .Add("get-admins", SGCONF::MakeAPIAction(commands, SGCONF::GetAdminsFunction), "\tget admin list")
1389       .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::GetAdminFunction), "\tget admin")
1390       .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::AddAdminFunction), "\tadd admin")
1391       .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::DelAdminFunction), "\tdel admin")
1392       .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::ChgAdminFunction), "\tchange admin");
1393 blocks.Add("Tariff management options")
1394       .Add("get-tariffs", SGCONF::MakeAPIAction(commands, SGCONF::GetTariffsFunction), "\tget tariff list")
1395       .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::GetTariffFunction), "\tget tariff")
1396       .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddTariffFunction), "\tadd tariff")
1397       .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelTariffFunction), "\tdel tariff")
1398       .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgTariffFunction), "\tchange tariff");
1399
1400
1401 SGCONF::PARSER_STATE state(false, argc, argv);
1402
1403 try
1404 {
1405 state = blocks.Parse(--argc, ++argv); // Skipping self name
1406 }
1407 catch (const SGCONF::OPTION::ERROR& ex)
1408 {
1409 std::cerr << ex.what() << "\n";
1410 return -1;
1411 }
1412
1413 if (state.stop)
1414     return 0;
1415
1416 if (state.argc > 0)
1417     {
1418     std::cerr << "Unknown option: '" << *state.argv << "'\n";
1419     return -1;
1420     }
1421
1422 try
1423 {
1424 SGCONF::CONFIG configOverride(config);
1425
1426 if (config.configFile.empty())
1427     {
1428     const char * mainConfigFile = "/etc/sgconf/sgconf.conf";
1429     if (access(mainConfigFile, R_OK) == 0)
1430         block.ParseFile(mainConfigFile);
1431     ReadUserConfigFile(block);
1432     }
1433 else
1434     {
1435     block.ParseFile(config.configFile.data());
1436     }
1437
1438 config = configOverride;
1439 }
1440 catch (const std::exception& ex)
1441 {
1442 std::cerr << ex.what() << "\n";
1443 return -1;
1444 }
1445
1446 std::cerr << "Config: " << config.Serialize() << std::endl;
1447 return commands.Execute(config) ? 0 : -1;
1448
1449 /*return 0;
1450
1451 if (argc < 2)
1452     {
1453     Usage();
1454     return 1;
1455     }
1456
1457 if (argc <= 2)
1458     {
1459     UsageConf();
1460     exit(PARAMETER_PARSING_ERR_CODE);
1461     }
1462
1463 if (strcmp(argv[1], "get") == 0)
1464     {
1465     //printf("get\n");
1466     return mainGet(argc - 1, argv + 1);
1467     }
1468 else if (strcmp(argv[1], "set") == 0)
1469     {
1470     //printf("set\n");
1471     if (mainSet(argc - 1, argv + 1) )
1472         return 0;
1473     return -1;
1474     }
1475 else
1476     {
1477     UsageConf();
1478     exit(PARAMETER_PARSING_ERR_CODE);
1479     }
1480 return UNKNOWN_ERR_CODE;*/
1481 }
1482 //-----------------------------------------------------------------------------
1483
1484 namespace
1485 {
1486
1487 void Usage()
1488 {
1489 UsageImpl(false);
1490 }
1491
1492 void UsageAll()
1493 {
1494 UsageImpl(true);
1495 }
1496
1497 void UsageImpl(bool full)
1498 {
1499 std::cout << "sgconf is the Stargazer management utility.\n\n"
1500           << "Usage:\n"
1501           << "\tsgconf [options]\n\n"
1502           << "General options:\n"
1503           << "\t-c, --config <config file>\t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n"
1504           << "\t-h, --help\t\t\t\tshow this help and exit\n"
1505           << "\t--help-all\t\t\t\tshow full help and exit\n"
1506           << "\t-v, --version\t\t\t\tshow version information and exit\n\n";
1507 UsageConnection();
1508 UsageAdmins(full);
1509 UsageTariffs(full);
1510 UsageUsers(full);
1511 UsageServices(full);
1512 UsageCorporations(full);
1513 }
1514 //-----------------------------------------------------------------------------
1515 void UsageConnection()
1516 {
1517 std::cout << "Connection options:\n"
1518           << "\t-s, --server <address>\t\t\thost to connect (ip or domain name, default: \"localhost\")\n"
1519           << "\t-p, --port <port>\t\t\tport to connect (default: \"5555\")\n"
1520           << "\t-u, --username <username>\t\tadministrative login (default: \"admin\")\n"
1521           << "\t-w, --userpass <password>\t\tpassword for administrative login\n"
1522           << "\t-a, --address <connection string>\tconnection params as a single string in format: <login>:<password>@<host>:<port>\n\n";
1523 }
1524 //-----------------------------------------------------------------------------
1525 void UsageAdmins(bool full)
1526 {
1527 std::cout << "Admins management options:\n"
1528           << "\t--get-admins\t\t\t\tget a list of admins (subsequent options will define what to show)\n";
1529 if (full)
1530     std::cout << "\t\t--login\t\t\t\tshow admin's login\n"
1531               << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1532 std::cout << "\t--get-admin\t\t\t\tget the information about admin\n";
1533 if (full)
1534     std::cout << "\t\t--login <login>\t\t\tlogin of the admin to show\n"
1535               << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n";
1536 std::cout << "\t--add-admin\t\t\t\tadd a new admin\n";
1537 if (full)
1538     std::cout << "\t\t--login <login>\t\t\tlogin of the admin to add\n"
1539               << "\t\t--password <password>\t\tpassword of the admin to add\n"
1540               << "\t\t--priv <priv number>\t\tpriviledges of the admin to add\n\n";
1541 std::cout << "\t--del-admin\t\t\t\tdelete an existing admin\n";
1542 if (full)
1543     std::cout << "\t\t--login <login>\t\t\tlogin of the admin to delete\n\n";
1544 std::cout << "\t--chg-admin\t\t\t\tchange an existing admin\n";
1545 if (full)
1546     std::cout << "\t\t--login <login>\t\t\tlogin of the admin to change\n"
1547               << "\t\t--priv <priv number>\t\tnew priviledges\n\n";
1548 }
1549 //-----------------------------------------------------------------------------
1550 void UsageTariffs(bool full)
1551 {
1552 std::cout << "Tariffs management options:\n"
1553           << "\t--get-tariffs\t\t\t\tget a list of tariffs (subsequent options will define what to show)\n";
1554 if (full)
1555     std::cout << "\t\t--name\t\t\t\tshow tariff's name\n"
1556               << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1557               << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1558               << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1559               << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1560               << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1561 std::cout << "\t--get-tariff\t\t\t\tget the information about tariff\n";
1562 if (full)
1563     std::cout << "\t\t--name <name>\t\t\tname of the tariff to show\n"
1564               << "\t\t--fee\t\t\t\tshow tariff's fee\n"
1565               << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
1566               << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
1567               << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
1568               << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
1569 std::cout << "\t--add-tariff\t\t\t\tadd a new tariff\n";
1570 if (full)
1571     std::cout << "\t\t--name <name>\t\t\tname of the tariff to add\n"
1572               << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1573               << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1574               << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1575               << "\t\t--traff-type <type>\t\twhat type of traffi will be accounted by the tariff\n"
1576               << "\t\t--times <times>\t\t\tslash-separated list of \"day\" time-spans (in form \"hh:mm-hh:mm\") for each direction\n"
1577               << "\t\t--prices-day-a <prices>\t\tslash-separated list of prices for \"day\" traffic before threshold for each direction\n"
1578               << "\t\t--prices-night-a <prices>\tslash-separated list of prices for \"night\" traffic before threshold for each direction\n"
1579               << "\t\t--prices-day-b <prices>\t\tslash-separated list of prices for \"day\" traffic after threshold for each direction\n"
1580               << "\t\t--prices-night-b <prices>\tslash-separated list of prices for \"night\" traffic after threshold for each direction\n"
1581               << "\t\t--single-prices <yes|no>\tslash-separated list of \"single price\" flags for each direction\n"
1582               << "\t\t--no-discounts <yes|no>\t\tslash-separated list of \"no discount\" flags for each direction\n"
1583               << "\t\t--thresholds <thresholds>\tslash-separated list of thresholds (in Mb) for each direction\n\n";
1584 std::cout << "\t--del-tariff\t\t\t\tdelete an existing tariff\n";
1585 if (full)
1586     std::cout << "\t\t--name <name>\t\t\tname of the tariff to delete\n\n";
1587 std::cout << "\t--chg-tariff\t\t\t\tchange an existing tariff\n";
1588 if (full)
1589     std::cout << "\t\t--name <name>\t\t\tname of the tariff to change\n"
1590               << "\t\t--fee <fee>\t\t\tstariff's fee\n"
1591               << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
1592               << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
1593               << "\t\t--traff-type <type>\t\twhat type of traffix will be accounted by the tariff\n"
1594               << "\t\t--dir <N>\t\t\tnumber of direction data to change\n"
1595               << "\t\t\t--time <time>\t\t\"day\" time-span (in form \"hh:mm-hh:mm\")\n"
1596               << "\t\t\t--price-day-a <price>\tprice for \"day\" traffic before threshold\n"
1597               << "\t\t\t--price-night-a <price>\tprice for \"night\" traffic before threshold\n"
1598               << "\t\t\t--price-day-b <price>\tprice for \"day\" traffic after threshold\n"
1599               << "\t\t\t--price-night-b <price>\tprice for \"night\" traffic after threshold\n"
1600               << "\t\t\t--single-price <yes|no>\t\"single price\" flag\n"
1601               << "\t\t\t--no-discount <yes|no>\t\"no discount\" flag\n"
1602               << "\t\t\t--threshold <threshold>\tthreshold (in Mb)\n\n";
1603 }
1604 //-----------------------------------------------------------------------------
1605 void UsageUsers(bool full)
1606 {
1607 std::cout << "Users management options:\n"
1608           << "\t--get-users\t\t\t\tget a list of users (subsequent options will define what to show)\n";
1609 if (full)
1610     std::cout << "\n\n";
1611 std::cout << "\t--get-user\t\t\t\tget the information about user\n";
1612 if (full)
1613     std::cout << "\n\n";
1614 std::cout << "\t--add-user\t\t\t\tadd a new user\n";
1615 if (full)
1616     std::cout << "\n\n";
1617 std::cout << "\t--del-user\t\t\t\tdelete an existing user\n";
1618 if (full)
1619     std::cout << "\n\n";
1620 std::cout << "\t--chg-user\t\t\t\tchange an existing user\n";
1621 if (full)
1622     std::cout << "\n\n";
1623 std::cout << "\t--check-user\t\t\t\tcheck credentials is valid\n";
1624 if (full)
1625     std::cout << "\n\n";
1626 std::cout << "\t--send-message\t\t\t\tsend a message to a user\n";
1627 if (full)
1628     std::cout << "\n\n";
1629 }
1630 //-----------------------------------------------------------------------------
1631 void UsageServices(bool full)
1632 {
1633 std::cout << "Services management options:\n"
1634           << "\t--get-services\t\t\t\tget a list of services (subsequent options will define what to show)\n";
1635 if (full)
1636     std::cout << "\t\t--name\t\t\t\tshow service's name\n"
1637               << "\t\t--comment\t\t\tshow a comment to the service\n"
1638               << "\t\t--cost\t\t\t\tshow service's cost\n"
1639               << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1640 std::cout << "\t--get-service\t\t\t\tget the information about service\n";
1641 if (full)
1642     std::cout << "\t\t--name <name>\t\t\tname of the service to show\n"
1643               << "\t\t--comment\t\t\tshow a comment to the service\n"
1644               << "\t\t--cost\t\t\t\tshow service's cost\n"
1645               << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
1646 std::cout << "\t--add-service\t\t\t\tadd a new service\n";
1647 if (full)
1648     std::cout << "\t\t--name <name>\t\t\tname of the service to add\n"
1649               << "\t\t--comment <comment>\t\ta comment to the service\n"
1650               << "\t\t--cost <cost>\t\t\tservice's cost\n"
1651               << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1652 std::cout << "\t--del-service\t\t\t\tdelete an existing service\n";
1653 if (full)
1654     std::cout << "\t\t--name <name>\t\t\tname of the service to delete\n\n";
1655 std::cout << "\t--chg-service\t\t\t\tchange an existing service\n";
1656 if (full)
1657     std::cout << "\t\t--name <name>\t\t\tname of the service to change\n"
1658               << "\t\t--comment <comment>\t\ta comment to the service\n"
1659               << "\t\t--cost <cost>\t\t\tservice's cost\n"
1660               << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
1661 }
1662 //-----------------------------------------------------------------------------
1663 void UsageCorporations(bool full)
1664 {
1665 std::cout << "Corporations management options:\n"
1666           << "\t--get-corporations\t\t\tget a list of corporations (subsequent options will define what to show)\n";
1667 if (full)
1668     std::cout << "\t\t--name\t\t\t\tshow corporation's name\n"
1669               << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1670 std::cout << "\t--get-corp\t\t\t\tget the information about corporation\n";
1671 if (full)
1672     std::cout << "\t\t--name <name>\t\t\tname of the corporation to show\n"
1673               << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
1674 std::cout << "\t--add-corp\t\t\t\tadd a new corporation\n";
1675 if (full)
1676     std::cout << "\t\t--name <name>\t\t\tname of the corporation to add\n"
1677               << "\t\t--cash <cash>\t\t\tinitial corporation's cash (default: \"0\")\n\n";
1678 std::cout << "\t--del-corp\t\t\t\tdelete an existing corporation\n";
1679 if (full)
1680     std::cout << "\t\t--name <name>\t\t\tname of the corporation to delete\n\n";
1681 std::cout << "\t--chg-corp\t\t\t\tchange an existing corporation\n";
1682 if (full)
1683     std::cout << "\t\t--name <name>\t\t\tname of the corporation to change\n"
1684               << "\t\t--add-cash <amount>[:<message>]\tadd cash to the corporation's account and optional comment message\n"
1685               << "\t\t--set-cash <cash>[:<message>]\tnew corporation's cash and optional comment message\n\n";
1686 }
1687
1688 void Version()
1689 {
1690 std::cout << "sgconf, version: 2.0.0-alpha.\n";
1691 }
1692
1693 } // namespace anonymous