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