]> git.stg.codes - stg.git/blob - sgconf/main.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / 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 "xml.h"
23 #include "admins.h"
24 #include "tariffs.h"
25 #include "users.h"
26 #include "services.h"
27 #include "corps.h"
28 #include "info.h"
29
30 #include "api_action.h"
31 #include "options.h"
32 #include "actions.h"
33 #include "config.h"
34
35 #include <string>
36 #include <iostream>
37
38 #include <cstdlib> // getenv
39 #include <cstring> // str*
40
41 #include <unistd.h> // access
42 #include <libgen.h> // basename
43
44 namespace
45 {
46
47 template <typename T>
48 struct nullary_function
49 {
50 typedef T result_type;
51 };
52
53 template <typename F>
54 class binder0 : public nullary_function<typename F::result_type>
55 {
56     public:
57         binder0(const F & func, const typename F::argument_type & arg)
58             : m_func(func), m_arg(arg) {}
59         typename F::result_type operator()() const { return m_func(m_arg); }
60     private:
61         F m_func;
62         typename F::argument_type m_arg;
63 };
64
65 template <typename F>
66 inline
67 binder0<F> bind0(const F & func, const typename F::argument_type & arg)
68 {
69 return binder0<F>(func, arg);
70 }
71
72 template <typename A, typename R>
73 class FUNC1_ADAPTER : public std::unary_function<A, R>
74 {
75     public:
76         FUNC1_ADAPTER(R (*func)(A)) : m_func(func) {}
77         const R operator()(A arg) const { return (m_func)(arg); }
78     private:
79         R (*m_func)(A);
80 };
81
82 template <typename C, typename A, typename R>
83 class METHOD1_ADAPTER : public std::unary_function<A, R>
84 {
85     public:
86         METHOD1_ADAPTER(R (C::* func)(A), C & obj) : m_func(func), m_obj(obj) {}
87         R operator()(A arg) { return (m_obj.*m_func)(arg); }
88     private:
89         R (C::* m_func)(A);
90         C & m_obj;
91 };
92
93 template <typename C, typename A, typename R>
94 class CONST_METHOD1_ADAPTER : public std::unary_function<A, R>
95 {
96     public:
97         CONST_METHOD1_ADAPTER(R (C::* func)(A) const, C & obj) : m_func(func), m_obj(obj) {}
98         R operator()(A arg) const { return (m_obj.*m_func)(arg); }
99     private:
100         R (C::* m_func)(A) const;
101         C & m_obj;
102 };
103
104 template <typename A, typename R>
105 FUNC1_ADAPTER<A, R> Func1Adapt(R (func)(A))
106 {
107 return FUNC1_ADAPTER<A, R>(func);
108 }
109
110 template <typename C, typename A, typename R>
111 METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A), C & obj)
112 {
113 return METHOD1_ADAPTER<C, A, R>(func, obj);
114 }
115
116 template <typename C, typename A, typename R>
117 CONST_METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A) const, C & obj)
118 {
119 return CONST_METHOD1_ADAPTER<C, A, R>(func, obj);
120 }
121
122 void Version(const std::string & self)
123 {
124 std::cout << self << ", version: 2.0.0.\n";
125 }
126
127 void ReadUserConfigFile(SGCONF::OPTION_BLOCK & block)
128 {
129 std::vector<std::string> paths;
130 const char * configHome = getenv("XDG_CONFIG_HOME");
131 if (configHome == NULL)
132     {
133     const char * home = getenv("HOME");
134     if (home == NULL)
135         return;
136     paths.push_back(std::string(home) + "/.config/sgconf/sgconf.conf");
137     paths.push_back(std::string(home) + "/.sgconf/sgconf.conf");
138     }
139 else
140     paths.push_back(std::string(configHome) + "/sgconf/sgconf.conf");
141 for (std::vector<std::string>::const_iterator it = paths.begin(); it != paths.end(); ++it)
142     if (access(it->c_str(), R_OK) == 0)
143         {
144         block.ParseFile(*it);
145         return;
146         }
147 }
148
149 } // namespace anonymous
150
151 namespace SGCONF
152 {
153
154 class CONFIG_ACTION : public ACTION
155 {
156     public:
157         CONFIG_ACTION(SGCONF::CONFIG & config,
158                       const std::string & paramDescription)
159             : m_config(config),
160               m_description(paramDescription)
161         {}
162
163         virtual ACTION * Clone() const { return new CONFIG_ACTION(*this); }
164
165         virtual std::string ParamDescription() const { return m_description; }
166         virtual std::string DefaultDescription() const { return ""; }
167         virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
168         virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/);
169
170     private:
171         SGCONF::CONFIG & m_config;
172         std::string m_description;
173         OPTION_BLOCK m_suboptions;
174
175         void ParseCredentials(const std::string & credentials);
176         void ParseHostAndPort(const std::string & hostAndPort);
177 };
178
179
180 PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv, void * /*data*/)
181 {
182 if (argc == 0 ||
183     argv == NULL ||
184     *argv == NULL)
185     throw ERROR("Missing argument.");
186 char * pos = strchr(*argv, '@');
187 if (pos != NULL)
188     {
189     ParseCredentials(std::string(*argv, pos));
190     ParseHostAndPort(std::string(pos + 1));
191     }
192 else
193     {
194     ParseHostAndPort(std::string(*argv));
195     }
196 return PARSER_STATE(false, --argc, ++argv);
197 }
198
199 void CONFIG_ACTION::ParseCredentials(const std::string & credentials)
200 {
201 std::string::size_type pos = credentials.find_first_of(':');
202 if (pos != std::string::npos)
203     {
204     m_config.userName = credentials.substr(0, pos);
205     m_config.userPass = credentials.substr(pos + 1);
206     }
207 else
208     {
209     m_config.userName = credentials;
210     }
211 }
212
213 void CONFIG_ACTION::ParseHostAndPort(const std::string & hostAndPort)
214 {
215 std::string::size_type pos = hostAndPort.find_first_of(':');
216 if (pos != std::string::npos)
217     {
218     m_config.server = hostAndPort.substr(0, pos);
219     uint16_t port = 0;
220     if (str2x(hostAndPort.substr(pos + 1), port))
221         throw ERROR("Invalid port value: '" + hostAndPort.substr(pos + 1) + "'");
222     m_config.port = port;
223     }
224 else
225     {
226     m_config.server = hostAndPort;
227     }
228 }
229
230 inline
231 CONFIG_ACTION * MakeParamAction(SGCONF::CONFIG & config,
232                                 const std::string & paramDescription)
233 {
234 return new CONFIG_ACTION(config, paramDescription);
235 }
236
237 } // namespace SGCONF
238
239 //-----------------------------------------------------------------------------
240 int main(int argc, char **argv)
241 {
242 std::string self(basename(argv[0]));
243 SGCONF::CONFIG config;
244 SGCONF::COMMANDS commands;
245
246 SGCONF::OPTION_BLOCKS blocks;
247 blocks.Add("General options")
248       .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), "<config file>"), "override default config file")
249       .Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit")
250       //.Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit")
251       .Add("v", "version", SGCONF::MakeFunc0Action(bind0(Func1Adapt(Version), self)), "\t\tshow version information and exit");
252 SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options")
253       .Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "<address>"), "\t\thost to connect")
254       .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "\t\tport to connect")
255       .Add("local-address", SGCONF::MakeParamAction(config.localAddress, std::string(""), "<address>"), "\tlocal address to bind")
256       .Add("local-port", SGCONF::MakeParamAction(config.localPort, uint16_t(0), "<port>"), "\t\tlocal port to bind")
257       .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
258       .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "\tpassword for the administrative login")
259       .Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
260 blocks.Add("Debug options")
261       .Add("show-config", SGCONF::MakeParamAction(config.showConfig), "\tshow config and exit");
262 SGCONF::AppendXMLOptionBlock(commands, blocks);
263 SGCONF::AppendServerInfoBlock(commands, blocks);
264 SGCONF::AppendAdminsOptionBlock(commands, blocks);
265 SGCONF::AppendTariffsOptionBlock(commands, blocks);
266 SGCONF::AppendUsersOptionBlock(commands, blocks);
267 SGCONF::AppendServicesOptionBlock(commands, blocks);
268 SGCONF::AppendCorpsOptionBlock(commands, blocks);
269
270 SGCONF::PARSER_STATE state(false, argc, argv);
271
272 try
273 {
274 state = blocks.Parse(--argc, ++argv); // Skipping self name
275 }
276 catch (const SGCONF::OPTION::ERROR& ex)
277 {
278 std::cerr << ex.what() << "\n";
279 return -1;
280 }
281
282 if (state.stop)
283     return 0;
284
285 if (state.argc > 0)
286     {
287     std::cerr << "Unknown option: '" << *state.argv << "'\n";
288     return -1;
289     }
290
291 try
292 {
293 SGCONF::CONFIG configOverride(config);
294
295 if (config.configFile.empty())
296     {
297     const char * mainConfigFile = "/etc/sgconf/sgconf.conf";
298     if (access(mainConfigFile, R_OK) == 0)
299         block.ParseFile(mainConfigFile);
300     ReadUserConfigFile(block);
301     }
302 else
303     {
304     block.ParseFile(config.configFile.data());
305     }
306
307 config = configOverride;
308
309 if (!config.showConfig.empty() && config.showConfig.data())
310     {
311     std::cout << config.Serialize() << std::endl;
312     return 0;
313     }
314 return commands.Execute(config) ? 0 : -1;
315 }
316 catch (const std::exception& ex)
317 {
318 std::cerr << ex.what() << "\n";
319 return -1;
320 }
321 }
322 //-----------------------------------------------------------------------------
323
324 namespace
325 {
326
327 /*void UsageTariffs(bool full)
328 {
329 std::cout << "Tariffs management options:\n"
330           << "\t--get-tariffs\t\t\t\tget a list of tariffs (subsequent options will define what to show)\n";
331 if (full)
332     std::cout << "\t\t--name\t\t\t\tshow tariff's name\n"
333               << "\t\t--fee\t\t\t\tshow tariff's fee\n"
334               << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
335               << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
336               << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
337               << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
338 std::cout << "\t--get-tariff\t\t\t\tget the information about tariff\n";
339 if (full)
340     std::cout << "\t\t--name <name>\t\t\tname of the tariff to show\n"
341               << "\t\t--fee\t\t\t\tshow tariff's fee\n"
342               << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n"
343               << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n"
344               << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n"
345               << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n";
346 std::cout << "\t--add-tariff\t\t\t\tadd a new tariff\n";
347 if (full)
348     std::cout << "\t\t--name <name>\t\t\tname of the tariff to add\n"
349               << "\t\t--fee <fee>\t\t\tstariff's fee\n"
350               << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
351               << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
352               << "\t\t--traff-type <type>\t\twhat type of traffi will be accounted by the tariff\n"
353               << "\t\t--times <times>\t\t\tslash-separated list of \"day\" time-spans (in form \"hh:mm-hh:mm\") for each direction\n"
354               << "\t\t--prices-day-a <prices>\t\tslash-separated list of prices for \"day\" traffic before threshold for each direction\n"
355               << "\t\t--prices-night-a <prices>\tslash-separated list of prices for \"night\" traffic before threshold for each direction\n"
356               << "\t\t--prices-day-b <prices>\t\tslash-separated list of prices for \"day\" traffic after threshold for each direction\n"
357               << "\t\t--prices-night-b <prices>\tslash-separated list of prices for \"night\" traffic after threshold for each direction\n"
358               << "\t\t--single-prices <yes|no>\tslash-separated list of \"single price\" flags for each direction\n"
359               << "\t\t--no-discounts <yes|no>\t\tslash-separated list of \"no discount\" flags for each direction\n"
360               << "\t\t--thresholds <thresholds>\tslash-separated list of thresholds (in Mb) for each direction\n\n";
361 std::cout << "\t--del-tariff\t\t\t\tdelete an existing tariff\n";
362 if (full)
363     std::cout << "\t\t--name <name>\t\t\tname of the tariff to delete\n\n";
364 std::cout << "\t--chg-tariff\t\t\t\tchange an existing tariff\n";
365 if (full)
366     std::cout << "\t\t--name <name>\t\t\tname of the tariff to change\n"
367               << "\t\t--fee <fee>\t\t\tstariff's fee\n"
368               << "\t\t--free <free>\t\t\ttariff's prepaid traffic in terms of cost\n"
369               << "\t\t--passive-cost <cost>\t\ttariff's cost of \"freeze\"\n"
370               << "\t\t--traff-type <type>\t\twhat type of traffix will be accounted by the tariff\n"
371               << "\t\t--dir <N>\t\t\tnumber of direction data to change\n"
372               << "\t\t\t--time <time>\t\t\"day\" time-span (in form \"hh:mm-hh:mm\")\n"
373               << "\t\t\t--price-day-a <price>\tprice for \"day\" traffic before threshold\n"
374               << "\t\t\t--price-night-a <price>\tprice for \"night\" traffic before threshold\n"
375               << "\t\t\t--price-day-b <price>\tprice for \"day\" traffic after threshold\n"
376               << "\t\t\t--price-night-b <price>\tprice for \"night\" traffic after threshold\n"
377               << "\t\t\t--single-price <yes|no>\t\"single price\" flag\n"
378               << "\t\t\t--no-discount <yes|no>\t\"no discount\" flag\n"
379               << "\t\t\t--threshold <threshold>\tthreshold (in Mb)\n\n";
380 }
381 //-----------------------------------------------------------------------------
382 void UsageUsers(bool full)
383 {
384 std::cout << "Users management options:\n"
385           << "\t--get-users\t\t\t\tget a list of users (subsequent options will define what to show)\n";
386 if (full)
387     std::cout << "\n\n";
388 std::cout << "\t--get-user\t\t\t\tget the information about user\n";
389 if (full)
390     std::cout << "\n\n";
391 std::cout << "\t--add-user\t\t\t\tadd a new user\n";
392 if (full)
393     std::cout << "\n\n";
394 std::cout << "\t--del-user\t\t\t\tdelete an existing user\n";
395 if (full)
396     std::cout << "\n\n";
397 std::cout << "\t--chg-user\t\t\t\tchange an existing user\n";
398 if (full)
399     std::cout << "\n\n";
400 std::cout << "\t--check-user\t\t\t\tcheck credentials is valid\n";
401 if (full)
402     std::cout << "\n\n";
403 std::cout << "\t--send-message\t\t\t\tsend a message to a user\n";
404 if (full)
405     std::cout << "\n\n";
406 }
407 //-----------------------------------------------------------------------------
408 void UsageServices(bool full)
409 {
410 std::cout << "Services management options:\n"
411           << "\t--get-services\t\t\t\tget a list of services (subsequent options will define what to show)\n";
412 if (full)
413     std::cout << "\t\t--name\t\t\t\tshow service's name\n"
414               << "\t\t--comment\t\t\tshow a comment to the service\n"
415               << "\t\t--cost\t\t\t\tshow service's cost\n"
416               << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
417 std::cout << "\t--get-service\t\t\t\tget the information about service\n";
418 if (full)
419     std::cout << "\t\t--name <name>\t\t\tname of the service to show\n"
420               << "\t\t--comment\t\t\tshow a comment to the service\n"
421               << "\t\t--cost\t\t\t\tshow service's cost\n"
422               << "\t\t--pay-day\t\t\tshow service's pay day\n\n";
423 std::cout << "\t--add-service\t\t\t\tadd a new service\n";
424 if (full)
425     std::cout << "\t\t--name <name>\t\t\tname of the service to add\n"
426               << "\t\t--comment <comment>\t\ta comment to the service\n"
427               << "\t\t--cost <cost>\t\t\tservice's cost\n"
428               << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
429 std::cout << "\t--del-service\t\t\t\tdelete an existing service\n";
430 if (full)
431     std::cout << "\t\t--name <name>\t\t\tname of the service to delete\n\n";
432 std::cout << "\t--chg-service\t\t\t\tchange an existing service\n";
433 if (full)
434     std::cout << "\t\t--name <name>\t\t\tname of the service to change\n"
435               << "\t\t--comment <comment>\t\ta comment to the service\n"
436               << "\t\t--cost <cost>\t\t\tservice's cost\n"
437               << "\t\t--pay-day <day>\t\t\tservice's pay day\n\n";
438 }
439 //-----------------------------------------------------------------------------
440 void UsageCorporations(bool full)
441 {
442 std::cout << "Corporations management options:\n"
443           << "\t--get-corporations\t\t\tget a list of corporations (subsequent options will define what to show)\n";
444 if (full)
445     std::cout << "\t\t--name\t\t\t\tshow corporation's name\n"
446               << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
447 std::cout << "\t--get-corp\t\t\t\tget the information about corporation\n";
448 if (full)
449     std::cout << "\t\t--name <name>\t\t\tname of the corporation to show\n"
450               << "\t\t--cash\t\t\t\tshow corporation's cash\n\n";
451 std::cout << "\t--add-corp\t\t\t\tadd a new corporation\n";
452 if (full)
453     std::cout << "\t\t--name <name>\t\t\tname of the corporation to add\n"
454               << "\t\t--cash <cash>\t\t\tinitial corporation's cash (default: \"0\")\n\n";
455 std::cout << "\t--del-corp\t\t\t\tdelete an existing corporation\n";
456 if (full)
457     std::cout << "\t\t--name <name>\t\t\tname of the corporation to delete\n\n";
458 std::cout << "\t--chg-corp\t\t\t\tchange an existing corporation\n";
459 if (full)
460     std::cout << "\t\t--name <name>\t\t\tname of the corporation to change\n"
461               << "\t\t--add-cash <amount>[:<message>]\tadd cash to the corporation's account and optional comment message\n"
462               << "\t\t--set-cash <cash>[:<message>]\tnew corporation's cash and optional comment message\n\n";
463 }*/
464
465 } // namespace anonymous