DaZeus  2.0
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
config.h
Go to the documentation of this file.
1 
6 #ifndef _DAZEUS_CONFIG_H
7 #define _DAZEUS_CONFIG_H
8 
9 #include "network.h"
10 #include "database.h"
11 #include <stdexcept>
12 #include <stdint.h>
13 #include <string>
14 #include <sstream>
15 #include <assert.h>
16 
17 namespace dazeus {
18 
19 struct ConfigReaderState;
20 
21 struct GlobalConfig {
23  : default_nickname("DaZeus")
24  , default_username("DaZeus")
25  , default_fullname("DaZeus IRC bot")
26  , plugindirectory("plugins")
27  , highlight("}") {}
28 
29  std::string default_nickname;
30  std::string default_username;
31  std::string default_fullname;
32  std::string plugindirectory;
33  std::string highlight;
34 };
35 
36 struct PluginConfig {
37  PluginConfig(std::string n) : name(n), per_network(false) {}
38  std::string name;
39  std::string path;
40  std::string executable;
42  std::string parameters;
43  std::map<std::string, std::string> config;
44 };
45 
46 struct SocketConfig {
47  SocketConfig() : port(0) {}
48  std::string toString() {
49  if(type == "unix") {
50  return "unix:" + path;
51  } else if(type == "tcp") {
52  std::stringstream ss;
53  ss << "tcp:" << host << ":" << port;
54  return ss.str();
55  }
56  assert(!"Unknown type in SocketConfig::toString()");
57  return "";
58  }
59  std::string type;
60  std::string host;
61  uint16_t port;
62  std::string path;
63 };
64 
65 class ConfigReader {
66  std::vector<NetworkConfig*> networks;
67  std::vector<PluginConfig*> plugins;
68  std::vector<SocketConfig*> sockets;
69  GlobalConfig *global;
70  DatabaseConfig *database;
71  std::string file;
72  ConfigReaderState *state;
73  bool is_read;
74 
75 public:
76  struct exception : public std::runtime_error {
77  exception(std::string e) : std::runtime_error(e) {}
78  };
79 
80  ConfigReader(std::string file);
81  ConfigReader(ConfigReader const&);
83  ~ConfigReader();
84 
85  bool isRead() { return is_read; }
86  void read();
87  std::string error();
88  ConfigReaderState *_state() { return state; }
89 
90  const std::vector<NetworkConfig*> &getNetworks() { return networks; }
91  const std::vector<PluginConfig*> &getPlugins() { return plugins; }
92  const std::vector<SocketConfig*> &getSockets() { return sockets; }
94  if(sockets.size() == 0) {
95  throw std::runtime_error("No sockets defined, cannot run plugins");
96  }
97  return sockets.at(0);
98  }
99  GlobalConfig *getGlobalConfig() { return global; }
100  DatabaseConfig *getDatabaseConfig() { return database; }
101  std::string &getFile() { return file; }
102 };
103 
104 }
105 
106 #endif