-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopts.h
209 lines (172 loc) · 5.57 KB
/
opts.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
struct Opts : public std::map<std::string,std::string>
{
Opts(): std::map<std::string,std::string>
{
// Client info
{"nick", "" },
{"user", "nobody" },
{"gecos", "noone" },
// Nickserv identification
{"ns-acct", "" },
{"ns-pass", "" },
// Server info
{"host", "chat.freenode.net" },
{"port", "6667" },
{"pass", "" },
{"proxy", "" /* host:port format */ },
// Misc configuration
{"locale", "" },
{"dbdir", "db" },
{"prefix", "!" },
{"invite-throttle", "300" },
{"owner", "" },
{"throttle-msg", "666" /* milliseconds */ },
{"throttle-join", "333" /* milliseconds */ },
{"as-a-service", "false" },
{"quit-msg", "Quit" },
{"umode", "" },
{"timeout", "300000" /* milliseconds */ },
{"threads", "1" /* for BACKGROUND mode */ },
{"invite", "false" },
{"database", "false" },
{"caps", "true" },
{"registration", "true" },
{"services", "true" },
{"chan-fetch-mode", "true" },
{"chan-fetch-who", "true" },
{"chan-fetch-info", "true" },
{"chan-fetch-lists", "true" },
{"quit", "true" },
{"reconnect", "true" },
}
{
if(at("locale").empty())
{
const char *const loc = secure_getenv("LANG");
if(loc)
at("locale") = loc;
}
}
// Channels to join on connect
std::list<std::string> autojoin;
bool has(const std::string &key) const;
const std::string &operator[](const std::string &key) const;
std::string &operator[](const std::string &key);
template<class T> using non_num_t = typename std::enable_if<!std::is_arithmetic<T>(),T>::type;
template<class T> using num_t = typename std::enable_if<std::is_arithmetic<T>::value,T>::type;
template<class T> non_num_t<T> get(const std::string &key) const;
template<class T> num_t<T> get(const std::string &key) const;
uint parse(const std::vector<std::string> &argv);
// Output this info
friend std::ostream &operator<<(std::ostream &s, const Opts &id);
};
inline
uint Opts::parse(const std::vector<std::string> &strs)
{
uint ret = 0;
parse_args(strs.begin(),strs.end(),"--","=",[&]
(const auto &kv)
{
ret++;
if(kv.second.empty())
(*this)[kv.first] = "true";
else if(kv.first == "join")
autojoin.emplace_back(kv.second);
else
(*this)[kv.first] = kv.second;
});
return ret;
}
template<> inline
bool Opts::get<bool>(const std::string &key)
const try
{
const auto it(this->find(key));
if(it == end())
return false;
const auto &val(it->second);
switch(hash(tolower(val)))
{
case hash("enable"):
case hash("true"):
case hash("one"):
case hash("1"):
return true;
default:
return false;
}
}
catch(const boost::bad_lexical_cast &e)
{
std::cerr << "Bad boolean value for configuration key: "
<< "[" << key << "] "
<< "(" << e.what() << ")"
<< std::endl;
return false;
}
template<class T>
Opts::non_num_t<T> Opts::get(const std::string &key)
const
{
return lex_cast<T>(this->at(key));
}
template<class T>
Opts::num_t<T> Opts::get(const std::string &key)
const try
{
return lex_cast<T>(this->at(key));
}
catch(const boost::bad_lexical_cast &e)
{
std::cerr << "Bad numerical value for configuration key: "
<< "[" << key << "] "
<< "(" << e.what() << ")"
<< std::endl;
return T(0);
}
catch(const std::out_of_range &e)
{
return T(0);
}
inline
std::string &Opts::operator[](const std::string &key)
{
const auto it(find(key));
return it == end()? emplace(key,std::string{}).first->second:
it->second;
}
inline
const std::string &Opts::operator[](const std::string &key)
const
{
const auto it(find(key));
if(it == end())
throw std::out_of_range("Key not found");
return it->second;
}
inline
bool Opts::has(const std::string &key)
const
{
if(count(key) == 0)
return false;
return !at(key).empty();
}
inline
std::ostream &operator<<(std::ostream &s,
const Opts &id)
{
for(const auto &pair : id)
s << std::setw(16) << std::left << pair.first << " => " << pair.second << std::endl;
s << std::endl;
s << "Autojoin channels: " << std::endl;
for(const auto &chan : id.autojoin)
s << "\t" << chan << std::endl;
return s;
}