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