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