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