Initial commit on project bpdyndnsd.
[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 Config::Config()
15 {
16     // define valid command line parameters
17     opt_desc_cmd = new po::options_description("Command line options");
18
19     opt_desc_cmd->add_options()
20         ("help,?","Show help.")
21         ("version,v","Show version.")
22         ("protocol,q",po::value<string>(),"Set the service protocol type.")
23         ("host,h",po::value<string>(),"Set the hostname to update.")
24         ("login,l",po::value<string>(),"Set the login.")
25         ("password,p",po::value<string>(),"Set the password.")
26     ;
27
28     // define valid config file parameters
29     opt_desc_conf = new po::options_description("Config file options");
30
31     opt_desc_conf->add_options()
32         ("help,?","Show help.")
33         ("version,v","Show version.")
34     ;
35 }
36
37 Config::~Config()
38 {
39     delete opt_desc_cmd;
40     delete opt_desc_conf;
41 }
42
43 int Config::parse_cmd_line(int argc, char *argv[])
44 {
45     try
46     {
47         po::variables_map vm;
48         po::store(po::parse_command_line(argc, argv, *opt_desc_cmd), vm);
49         po::notify(vm);
50
51         if(vm.count("help"))
52             return 1;
53         else if(vm.count("version"))
54             return 2;
55
56         if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
57         {
58             // get the cmd parameter values for protocol host login and password
59             string protocol = vm["protocol"].as<string>();
60             string host = vm["host"].as<string>();
61             string login = vm["login"].as<string>();
62             string password = vm["password"].as<string>();
63
64             //TODO: convert all options to lowercase
65
66             // create Service object
67             if(protocol == "dhs")
68             {
69                 Service * service = new DHS(host,login,password);
70                 this->services.push_back(service);
71             }
72             else if(protocol == "ods")
73             {
74                 Service * service = new ODS(host,login,password);
75                 this->services.push_back(service);
76             }
77         }
78         else
79         {
80             cout << "Not all needed options set!\n" << endl;
81             return 1;
82         }
83     }
84     catch(boost::program_options::unknown_option e)
85     {
86         cout << "Unknown option set." << endl;
87         return 1;
88     }
89     return 0;
90 }
91
92 void Config::print_usage()
93 {
94     cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
95     cout << *opt_desc_cmd << endl;
96 }
97
98 void Config::print_version(string version)
99 {
100     cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl;
101 }
102
103 void Config::load_config_file(string filename)
104 {
105
106 }
107
108 list<Service*> Config::get_services()
109 {
110     return this->services;
111 }