Small cosmetic changes
[bpdyndnsd] / src / config.cpp
1 //
2 // C++ Implementation: config
3 //
4 // Description: 
5 //
6 //
7 // Author: Bjoern Sikora <bjoern.sikora@intra2net.com>, (C) 2009
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12 #include "config.h"
13
14
15 // Define allowed command line and config file options
16 Config::Config()
17     : daemon_mode(true)
18     , loglevel(0)
19     , syslog(true)
20 {
21     // Define valid command line parameters
22     opt_desc_cmd = new po::options_description("Command line options");
23     opt_desc_cmd->add_options()
24         ("help,?","Show help.")
25         ("version,v","Show version.")
26         ("protocol,q",po::value<string>(),"Set the service protocol type.")
27         ("host,h",po::value<string>(),"Set the hostname to update.")
28         ("login,l",po::value<string>(),"Set the login.")
29         ("password,p",po::value<string>(),"Set the password.")
30     ;
31
32     // Define valid config file main section parameters
33     opt_desc_conf_main = new po::options_description("Config file main section options");
34     opt_desc_conf_main->add_options()
35         ("main.daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
36         ("main.logfile",po::value<string>()->default_value("/var/log/bpdyndns.log"),"Where to log.")
37         ("main.loglevel",po::value<int>()->default_value(0),"Loglevel.")
38         ("main.syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
39     ;
40
41     // Define valid config file main section parameters
42     opt_desc_conf_service = new po::options_description("Config file service section options");
43     opt_desc_conf_service->add_options()
44         ("service.protocol",po::value<string>(),"The service protocol.")
45         ("service.host",po::value<string>(),"The hostname to update.")
46         ("service.login",po::value<string>(),"Login name.")
47         ("service.password",po::value<string>(),"Corresponding password.")
48     ;
49 }
50
51
52 Config::~Config()
53 {
54     delete opt_desc_cmd;
55     delete opt_desc_conf_main;
56     delete opt_desc_conf_service;
57 }
58
59
60 int Config::parse_cmd_line(int argc, char *argv[])
61 {
62     try
63     {
64         po::variables_map vm;
65         po::store(po::parse_command_line(argc, argv, *this->opt_desc_cmd), vm);
66         po::notify(vm);
67
68         if(vm.count("help"))
69             return 1;
70         else if(vm.count("version"))
71             return 2;
72
73         // Are all needed options set to create a Service Object
74         if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
75         {
76             // Get the cmd parameter values for protocol host login and password
77             string protocol = vm["protocol"].as<string>();
78             string host = vm["host"].as<string>();
79             string login = vm["login"].as<string>();
80             string password = vm["password"].as<string>();
81
82             //TODO: convert protocol option to lowercase
83
84             // Create corresponding Service object TODO: swap to extra method
85             if(protocol == "dhs")
86             {
87                 Service * service = new DHS(host,login,password);
88                 this->services.push_back(service);
89             }
90             else if(protocol == "ods")
91             {
92                 Service * service = new ODS(host,login,password);
93                 this->services.push_back(service);
94             }
95         }
96         else
97         {
98             cout << "Not all needed options set!\n" << endl;
99             return 1;
100         }
101     }
102     catch(po::unknown_option e)
103     {
104         cout << "Unknown option set." << endl;
105         return 1;
106     }
107     return 0;
108 }
109
110
111 int Config::load_config_from_files(string config_path)
112 {
113     // first load the main config file bpdyndnsd.conf
114     string main_conf_file = config_path.append("/bpdyndnsd.conf");
115     ifstream config_file (main_conf_file.c_str(),ifstream::in);
116     if(config_file.is_open())
117     {
118         try
119         {
120             // parse the config file
121             po::variables_map vm;
122             po::parsed_options parsed = po::parse_config_file(config_file,*this->opt_desc_conf_main,true);
123             po::store(parsed,vm);
124             po::notify(vm);
125
126             if(vm.count("daemon_mode") && vm.count("logfile") && vm.count("loglevel") && vm.count("syslog"))
127             {
128                 // Get the options from main section
129                 this->daemon_mode = vm["main.daemon_mode"].as<bool>();
130                 this->logfile = vm["main.logfile"].as<string>();
131                 this->loglevel = vm["main.loglevel"].as<int>();
132                 this->syslog = vm["main.syslog"].as<bool>();
133             }
134         }
135         catch(po::unknown_option e)
136         {
137             // unknown option in config file detected
138             config_file.close();
139             cout << "Unknown option in config file detected!" << endl;
140             return 3;
141         }
142         config_file.close();
143     }
144     else
145     {
146         cout << "Can't open main config file for reading: " << main_conf_file << endl;
147         return 4;
148     }
149
150     // then load all service definition files in config path
151
152     // TODO: code to load service definition files in config path
153     return -1;
154 }
155
156
157 list<Service*> Config::get_services()
158 {
159     return this->services;
160 }
161
162
163 void Config::print_usage()
164 {
165     cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
166     cout << *opt_desc_cmd << endl;
167 }
168
169
170 void Config::print_version(string version)
171 {
172     cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl;
173 }