comparison lib/irccd/config.cpp @ 111:1ed760f6e0c6

Irccd: new brace styles, #487
author David Demelier <markand@malikania.fr>
date Wed, 27 Apr 2016 21:37:09 +0200
parents 04d672ab41a4
children 6a99814c2317
comparison
equal deleted inserted replaced
110:5d71d270a2dd 111:1ed760f6e0c6
72 bool daemonize = m_options.count("-f") == 0 && m_options.count("--foreground") == 0; 72 bool daemonize = m_options.count("-f") == 0 && m_options.count("--foreground") == 0;
73 73
74 if (daemonize && sc != config.end()) { 74 if (daemonize && sc != config.end()) {
75 it = sc->find("foreground"); 75 it = sc->find("foreground");
76 76
77 if (it != sc->end()) 77 if (it != sc->end()) {
78 daemonize = !util::isBoolean(it->value()); 78 daemonize = !util::isBoolean(it->value());
79 } 79 }
80 80 }
81 if (daemonize) 81
82 if (daemonize) {
82 daemon(1, 0); 83 daemon(1, 0);
84 }
83 #endif 85 #endif
84 86
85 if (sc != config.end()) { 87 if (sc != config.end()) {
86 try { 88 try {
87 #if defined(HAVE_SETGID) 89 #if defined(HAVE_SETGID)
88 if ((it = sc->find("gid")) != sc->end()) 90 if ((it = sc->find("gid")) != sc->end()) {
89 sys::setGid(it->value()); 91 sys::setGid(it->value());
92 }
90 #endif 93 #endif
91 #if defined(HAVE_SETUID) 94 #if defined(HAVE_SETUID)
92 if ((it = sc->find("uid")) != sc->end()) 95 if ((it = sc->find("uid")) != sc->end()) {
93 sys::setUid(it->value()); 96 sys::setUid(it->value());
97 }
94 #endif 98 #endif
95 } catch (const std::exception &ex) { 99 } catch (const std::exception &ex) {
96 log::warning() << "irccd: could not set " << it->key() << ": " << ex.what() << std::endl; 100 log::warning() << "irccd: could not set " << it->key() << ": " << ex.what() << std::endl;
97 } 101 }
98 } 102 }
111 string errors = "/var/log/irccd/errors.txt"; 115 string errors = "/var/log/irccd/errors.txt";
112 #endif 116 #endif
113 117
114 ini::Section::const_iterator it; 118 ini::Section::const_iterator it;
115 119
116 if ((it = sc.find("path-logs")) != sc.end()) 120 if ((it = sc.find("path-logs")) != sc.end()) {
117 normal = it->value(); 121 normal = it->value();
118 if ((it = sc.find("path-errors")) != sc.end()) 122 }
123 if ((it = sc.find("path-errors")) != sc.end()) {
119 errors = it->value(); 124 errors = it->value();
125 }
120 126
121 log::setInterface(make_unique<log::File>(move(normal), move(errors))); 127 log::setInterface(make_unique<log::File>(move(normal), move(errors)));
122 } 128 }
123 129
124 void Config::loadLogSyslog() const 130 void Config::loadLogSyslog() const
132 138
133 void Config::loadLogs(const ini::Document &config) const 139 void Config::loadLogs(const ini::Document &config) const
134 { 140 {
135 ini::Document::const_iterator sc = config.find("logs"); 141 ini::Document::const_iterator sc = config.find("logs");
136 142
137 if (sc == config.end()) 143 if (sc == config.end()) {
138 return; 144 return;
145 }
139 146
140 ini::Section::const_iterator it; 147 ini::Section::const_iterator it;
141 148
142 if ((it = sc->find("verbose")) != sc->end() && m_options.count("-v") == 0 && m_options.count("--verbose")) 149 if ((it = sc->find("verbose")) != sc->end() && m_options.count("-v") == 0 && m_options.count("--verbose")) {
143 log::setVerbose(util::isBoolean(it->value())); 150 log::setVerbose(util::isBoolean(it->value()));
151 }
144 if ((it = sc->find("type")) != sc->end()) { 152 if ((it = sc->find("type")) != sc->end()) {
145 /* Console is the default, no test case */ 153 /* Console is the default, no test case */
146 if (it->value() == "file") 154 if (it->value() == "file") {
147 loadLogFile(*sc); 155 loadLogFile(*sc);
148 else if (it->value() == "syslog") 156 } else if (it->value() == "syslog") {
149 loadLogSyslog(); 157 loadLogSyslog();
150 else 158 } else {
151 log::warning() << "irccd: unknown log type: " << it->value() << std::endl; 159 log::warning() << "irccd: unknown log type: " << it->value() << std::endl;
160 }
152 } 161 }
153 } 162 }
154 163
155 void Config::loadPlugins(Irccd &irccd, const ini::Section &sc) const 164 void Config::loadPlugins(Irccd &irccd, const ini::Section &sc) const
156 { 165 {
157 #if defined(WITH_JS) 166 #if defined(WITH_JS)
158 for (const ini::Option &option : sc) { 167 for (const ini::Option &option : sc) {
159 try { 168 try {
160 if (option.value().empty()) 169 if (option.value().empty()) {
161 irccd.loadPlugin(option.key(), option.key(), true); 170 irccd.loadPlugin(option.key(), option.key(), true);
162 else 171 } else {
163 irccd.loadPlugin(option.key(), option.value(), false); 172 irccd.loadPlugin(option.key(), option.value(), false);
173 }
164 } catch (const std::exception &ex) { 174 } catch (const std::exception &ex) {
165 log::warning() << "plugin " << option.key() << ": " << ex.what() << std::endl; 175 log::warning() << "plugin " << option.key() << ": " << ex.what() << std::endl;
166 } 176 }
167 } 177 }
168 #else 178 #else
174 void Config::loadPluginConfig(Irccd &irccd, const ini::Section &sc, string name) const 184 void Config::loadPluginConfig(Irccd &irccd, const ini::Section &sc, string name) const
175 { 185 {
176 #if defined(WITH_JS) 186 #if defined(WITH_JS)
177 PluginConfig config; 187 PluginConfig config;
178 188
179 for (const ini::Option &option : sc) 189 for (const ini::Option &option : sc) {
180 config.emplace(option.key(), option.value()); 190 config.emplace(option.key(), option.value());
191 }
181 192
182 irccd.addPluginConfig(std::move(name), std::move(config)); 193 irccd.addPluginConfig(std::move(name), std::move(config));
183 #else 194 #else
184 (void)irccd; 195 (void)irccd;
185 (void)sc; 196 (void)sc;
195 206
196 /* 207 /*
197 * Load plugin configurations before we load plugins since we use them 208 * Load plugin configurations before we load plugins since we use them
198 * when we load the plugin itself. 209 * when we load the plugin itself.
199 */ 210 */
200 for (const ini::Section &section : config) 211 for (const ini::Section &section : config) {
201 if (regex_match(section.key(), match, regex)) 212 if (regex_match(section.key(), match, regex)) {
202 loadPluginConfig(irccd, section, match[1]); 213 loadPluginConfig(irccd, section, match[1]);
214 }
215 }
203 216
204 ini::Document::const_iterator it = config.find("plugins"); 217 ini::Document::const_iterator it = config.find("plugins");
205 218
206 if (it != config.end()) 219 if (it != config.end()) {
207 loadPlugins(irccd, *it); 220 loadPlugins(irccd, *it);
221 }
208 #else 222 #else
209 (void)irccd; 223 (void)irccd;
210 (void)config; 224 (void)config;
211 225
212 log::warning() << "irccd: JavaScript disabled, ignoring plugins" << std::endl; 226 log::warning() << "irccd: JavaScript disabled, ignoring plugins" << std::endl;
220 ServerSettings settings; 234 ServerSettings settings;
221 235
222 /* Name */ 236 /* Name */
223 ini::Section::const_iterator it; 237 ini::Section::const_iterator it;
224 238
225 if ((it = sc.find("name")) == sc.end()) 239 if ((it = sc.find("name")) == sc.end()) {
226 throw std::invalid_argument("server: missing name"); 240 throw std::invalid_argument("server: missing name");
227 else if (!util::isIdentifierValid(it->value())) 241 } else if (!util::isIdentifierValid(it->value())) {
228 throw std::invalid_argument("server " + it->value() + ": name is not valid"); 242 throw std::invalid_argument("server " + it->value() + ": name is not valid");
229 else if (irccd.hasServer(it->value())) 243 } else if (irccd.hasServer(it->value())) {
230 throw std::invalid_argument("server " + it->value() + ": already exists"); 244 throw std::invalid_argument("server " + it->value() + ": already exists");
245 }
231 246
232 info.name = it->value(); 247 info.name = it->value();
233 248
234 /* Host */ 249 /* Host */
235 if ((it = sc.find("host")) == sc.end()) 250 if ((it = sc.find("host")) == sc.end()) {
236 throw std::invalid_argument("server " + info.name + ": missing host"); 251 throw std::invalid_argument("server " + info.name + ": missing host");
252 }
237 253
238 info.host = it->value(); 254 info.host = it->value();
239 255
240 /* Optional identity */ 256 /* Optional identity */
241 if ((it = sc.find("identity")) != sc.end()) 257 if ((it = sc.find("identity")) != sc.end()) {
242 identity = irccd.findIdentity(it->value()); 258 identity = irccd.findIdentity(it->value());
259 }
243 260
244 /* Optional port */ 261 /* Optional port */
245 if ((it = sc.find("port")) != sc.end()) { 262 if ((it = sc.find("port")) != sc.end()) {
246 try { 263 try {
247 info.port = std::stoi(it->value()); 264 info.port = std::stoi(it->value());
249 throw std::invalid_argument("server " + info.name + ": invalid port number: " + it->value()); 266 throw std::invalid_argument("server " + info.name + ": invalid port number: " + it->value());
250 } 267 }
251 } 268 }
252 269
253 /* Optional password */ 270 /* Optional password */
254 if ((it = sc.find("password")) != sc.end()) 271 if ((it = sc.find("password")) != sc.end()) {
255 info.password = it->value(); 272 info.password = it->value();
273 }
256 274
257 /* Optional flags */ 275 /* Optional flags */
258 if ((it = sc.find("ipv6")) != sc.end() && util::isBoolean(it->value())) 276 if ((it = sc.find("ipv6")) != sc.end() && util::isBoolean(it->value())) {
259 info.flags |= ServerInfo::Ipv6; 277 info.flags |= ServerInfo::Ipv6;
260 if ((it = sc.find("ssl")) != sc.end()) 278 }
261 if (util::isBoolean(it->value())) 279 if ((it = sc.find("ssl")) != sc.end()) {
280 if (util::isBoolean(it->value())) {
262 #if defined(WITH_SSL) 281 #if defined(WITH_SSL)
263 info.flags |= ServerInfo::Ssl; 282 info.flags |= ServerInfo::Ssl;
264 #else 283 #else
265 throw std::invalid_argument("server " + info.name + ": ssl is disabled"); 284 throw std::invalid_argument("server " + info.name + ": ssl is disabled");
266 #endif 285 #endif
267 286 }
268 if ((it = sc.find("ssl-verify")) != sc.end()) 287 }
269 if (util::isBoolean(it->value())) 288 if ((it = sc.find("ssl-verify")) != sc.end()) {
289 if (util::isBoolean(it->value())) {
270 #if defined(WITH_SSL) 290 #if defined(WITH_SSL)
271 info.flags |= ServerInfo::SslVerify; 291 info.flags |= ServerInfo::SslVerify;
272 #else 292 #else
273 throw std::invalid_argument("server " + info.name + ": ssl is disabled"); 293 throw std::invalid_argument("server " + info.name + ": ssl is disabled");
274 #endif 294 #endif
275 295 }
296 }
276 /* Options */ 297 /* Options */
277 if ((it = sc.find("auto-rejoin")) != sc.end() && util::isBoolean(it->value())) 298 if ((it = sc.find("auto-rejoin")) != sc.end() && util::isBoolean(it->value())) {
278 settings.flags |= ServerSettings::AutoRejoin; 299 settings.flags |= ServerSettings::AutoRejoin;
279 if ((it = sc.find("join-invite")) != sc.end() && util::isBoolean(it->value())) 300 }
301 if ((it = sc.find("join-invite")) != sc.end() && util::isBoolean(it->value())) {
280 settings.flags |= ServerSettings::JoinInvite; 302 settings.flags |= ServerSettings::JoinInvite;
303 }
281 304
282 /* Channels */ 305 /* Channels */
283 if ((it = sc.find("channels")) != sc.end()) { 306 if ((it = sc.find("channels")) != sc.end()) {
284 for (const std::string &s : *it) { 307 for (const std::string &s : *it) {
285 ServerChannel channel; 308 ServerChannel channel;
292 } 315 }
293 316
294 settings.channels.push_back(std::move(channel)); 317 settings.channels.push_back(std::move(channel));
295 } 318 }
296 } 319 }
297 if ((it = sc.find("command-char")) != sc.end()) 320 if ((it = sc.find("command-char")) != sc.end()) {
298 settings.command = it->value(); 321 settings.command = it->value();
322 }
299 323
300 /* Reconnect */ 324 /* Reconnect */
301 try { 325 try {
302 if ((it = sc.find("reconnect-tries")) != sc.end()) 326 if ((it = sc.find("reconnect-tries")) != sc.end()) {
303 settings.reconnect_tries = std::stoi(it->value()); 327 settings.reconnect_tries = std::stoi(it->value());
304 if ((it = sc.find("reconnect-timeout")) != sc.end()) 328 }
329 if ((it = sc.find("reconnect-timeout")) != sc.end()) {
305 settings.reconnect_timeout = std::stoi(it->value()); 330 settings.reconnect_timeout = std::stoi(it->value());
306 if ((it = sc.find("ping-timeout")) != sc.end()) 331 }
332 if ((it = sc.find("ping-timeout")) != sc.end()) {
307 settings.ping_timeout = std::stoi(it->value()); 333 settings.ping_timeout = std::stoi(it->value());
334 }
308 } catch (const std::exception &) { 335 } catch (const std::exception &) {
309 throw std::invalid_argument("server " + info.name + ": invalid number for " + it->key() + ": " + it->value()); 336 throw std::invalid_argument("server " + info.name + ": invalid number for " + it->key() + ": " + it->value());
310 } 337 }
311 338
312 irccd.addServer(std::make_shared<Server>(std::move(info), std::move(identity), std::move(settings))); 339 irccd.addServer(std::make_shared<Server>(std::move(info), std::move(identity), std::move(settings)));
337 } 364 }
338 365
339 identity.name = it->value(); 366 identity.name = it->value();
340 367
341 /* Optional stuff */ 368 /* Optional stuff */
342 if ((it = sc.find("username")) != sc.end()) 369 if ((it = sc.find("username")) != sc.end()) {
343 identity.username = it->value(); 370 identity.username = it->value();
344 if ((it = sc.find("realname")) != sc.end()) 371 }
372 if ((it = sc.find("realname")) != sc.end()) {
345 identity.realname = it->value(); 373 identity.realname = it->value();
346 if ((it = sc.find("nickname")) != sc.end()) 374 }
375 if ((it = sc.find("nickname")) != sc.end()) {
347 identity.nickname = it->value(); 376 identity.nickname = it->value();
348 if ((it = sc.find("ctcp-version")) != sc.end()) 377 }
378 if ((it = sc.find("ctcp-version")) != sc.end()) {
349 identity.ctcpversion = it->value(); 379 identity.ctcpversion = it->value();
380 }
350 381
351 log::debug() << "identity " << identity.name << ": "; 382 log::debug() << "identity " << identity.name << ": ";
352 log::debug() << "nickname=" << identity.nickname << ", username=" << identity.username << ", "; 383 log::debug() << "nickname=" << identity.nickname << ", username=" << identity.username << ", ";
353 log::debug() << "realname=" << identity.realname << ", ctcp-version=" << identity.ctcpversion << endl; 384 log::debug() << "realname=" << identity.realname << ", ctcp-version=" << identity.ctcpversion << endl;
354 385
379 RuleAction action = RuleAction::Accept; 410 RuleAction action = RuleAction::Accept;
380 411
381 /* Get the sets */ 412 /* Get the sets */
382 ini::Section::const_iterator it; 413 ini::Section::const_iterator it;
383 414
384 if ((it = sc.find("servers")) != sc.end()) 415 if ((it = sc.find("servers")) != sc.end()) {
385 servers = toSet(*it); 416 servers = toSet(*it);
386 if ((it = sc.find("channels")) != sc.end()) 417 }
418 if ((it = sc.find("channels")) != sc.end()) {
387 channels = toSet(*it); 419 channels = toSet(*it);
388 if ((it = sc.find("origins")) != sc.end()) 420 }
421 if ((it = sc.find("origins")) != sc.end()) {
389 origins = toSet(*it); 422 origins = toSet(*it);
390 if ((it = sc.find("plugins")) != sc.end()) 423 }
424 if ((it = sc.find("plugins")) != sc.end()) {
391 plugins = toSet(*it); 425 plugins = toSet(*it);
392 if ((it = sc.find("channels")) != sc.end()) 426 }
427 if ((it = sc.find("channels")) != sc.end()) {
393 channels = toSet(*it); 428 channels = toSet(*it);
429 }
394 430
395 /* Get the action */ 431 /* Get the action */
396 if ((it = sc.find("action")) == sc.end()) 432 if ((it = sc.find("action")) == sc.end()) {
397 throw std::invalid_argument("missing action parameter"); 433 throw std::invalid_argument("missing action parameter");
398 if (it->value() == "drop") 434 }
435 if (it->value() == "drop") {
399 action = RuleAction::Drop; 436 action = RuleAction::Drop;
400 else if (it->value() == "accept") 437 } else if (it->value() == "accept") {
401 action = RuleAction::Accept; 438 action = RuleAction::Accept;
402 else 439 } else {
403 throw std::invalid_argument("invalid action given: " + it->value()); 440 throw std::invalid_argument("invalid action given: " + it->value());
441 }
404 442
405 irccd.addRule(Rule(move(servers), move(channels), move(origins), move(plugins), move(events), action)); 443 irccd.addRule(Rule(move(servers), move(channels), move(origins), move(plugins), move(events), action));
406 } 444 }
407 445
408 void Config::loadRules(Irccd &irccd, const ini::Document &config) const 446 void Config::loadRules(Irccd &irccd, const ini::Document &config) const
426 ini::Section::const_iterator it; 464 ini::Section::const_iterator it;
427 465
428 /* Port */ 466 /* Port */
429 int port; 467 int port;
430 468
431 if ((it = sc.find("port")) == sc.end()) 469 if ((it = sc.find("port")) == sc.end()) {
432 throw invalid_argument("missing port"); 470 throw invalid_argument("missing port");
471 }
433 472
434 try { 473 try {
435 port = stoi(it->value()); 474 port = stoi(it->value());
436 } catch (const std::exception &) { 475 } catch (const std::exception &) {
437 throw std::invalid_argument("invalid port number: " + it->value()); 476 throw std::invalid_argument("invalid port number: " + it->value());
438 } 477 }
439 478
440 /* Address*/ 479 /* Address*/
441 std::string address = "*"; 480 std::string address = "*";
442 481
443 if ((it = sc.find("address")) != sc.end()) 482 if ((it = sc.find("address")) != sc.end()) {
444 address = it->value(); 483 address = it->value();
484 }
445 485
446 /* Domain */ 486 /* Domain */
447 if ((it = sc.find("domain")) != sc.end()) { 487 if ((it = sc.find("domain")) != sc.end()) {
448 ipv6 = false; 488 ipv6 = false;
449 ipv4 = false; 489 ipv4 = false;
450 490
451 for (const string &v : *it) { 491 for (const string &v : *it) {
452 if (v == "ipv4") 492 if (v == "ipv4") {
453 ipv4 = true; 493 ipv4 = true;
454 if (v == "ipv6") 494 }
495 if (v == "ipv6") {
455 ipv6 = true; 496 ipv6 = true;
456 } 497 }
457 } 498 }
458 499 }
459 if (ipv6) 500
501 if (ipv6) {
460 irccd.addTransport(std::make_shared<TransportServerIp>(AF_INET6, move(address), port, !ipv4)); 502 irccd.addTransport(std::make_shared<TransportServerIp>(AF_INET6, move(address), port, !ipv4));
461 else if (ipv4) 503 } else if (ipv4) {
462 irccd.addTransport(std::make_shared<TransportServerIp>(AF_INET, move(address), port)); 504 irccd.addTransport(std::make_shared<TransportServerIp>(AF_INET, move(address), port));
463 else 505 } else {
464 throw std::invalid_argument("domain must at least have ipv4 or ipv6"); 506 throw std::invalid_argument("domain must at least have ipv4 or ipv6");
507 }
465 } 508 }
466 509
467 void Config::loadTransportUnix(Irccd &irccd, const ini::Section &sc) const 510 void Config::loadTransportUnix(Irccd &irccd, const ini::Section &sc) const
468 { 511 {
469 #if !defined(IRCCD_SYSTEM_WINDOWS) 512 #if !defined(IRCCD_SYSTEM_WINDOWS)
490 for (const ini::Section &sc : config) { 533 for (const ini::Section &sc : config) {
491 if (sc.key() == "transport") { 534 if (sc.key() == "transport") {
492 try { 535 try {
493 ini::Section::const_iterator it = sc.find("type"); 536 ini::Section::const_iterator it = sc.find("type");
494 537
495 if (it == sc.end()) 538 if (it == sc.end()) {
496 log::warning() << "transport: missing type parameter" << std::endl; 539 log::warning() << "transport: missing type parameter" << std::endl;
497 else if (it->value() == "ip") 540 } else if (it->value() == "ip") {
498 loadTransportIp(irccd, sc); 541 loadTransportIp(irccd, sc);
499 else if (it->value() == "unix") 542 } else if (it->value() == "unix") {
500 loadTransportUnix(irccd, sc); 543 loadTransportUnix(irccd, sc);
501 else 544 } else {
502 log::warning() << "transport: invalid type given: " << std::endl; 545 log::warning() << "transport: invalid type given: " << std::endl;
546 }
503 } catch (const net::Error &error) { 547 } catch (const net::Error &error) {
504 log::warning() << "transport: " << error.function() << ": " << error.what() << std::endl; 548 log::warning() << "transport: " << error.function() << ": " << error.what() << std::endl;
505 } catch (const exception &ex) { 549 } catch (const exception &ex) {
506 log::warning() << "transport: error: " << ex.what() << endl; 550 log::warning() << "transport: error: " << ex.what() << endl;
507 } 551 }
543 void Config::load(Irccd &irccd) 587 void Config::load(Irccd &irccd)
544 { 588 {
545 auto it = m_options.find("-c"); 589 auto it = m_options.find("-c");
546 auto found = false; 590 auto found = false;
547 591
548 if (it != m_options.end()) 592 if (it != m_options.end()) {
549 found = openConfig(irccd, it->second); 593 found = openConfig(irccd, it->second);
550 else if ((it = m_options.find("--config")) != m_options.end()) 594 } else if ((it = m_options.find("--config")) != m_options.end()) {
551 found = openConfig(irccd, it->second); 595 found = openConfig(irccd, it->second);
552 else { 596 } else {
553 /* Search for a configuration file */ 597 /* Search for a configuration file */
554 for (const string &path : path::list(path::PathConfig)) { 598 for (const string &path : path::list(path::PathConfig)) {
555 string fullpath = path + "irccd.conf"; 599 string fullpath = path + "irccd.conf";
556 600
557 log::info() << "irccd: trying " << fullpath << endl; 601 log::info() << "irccd: trying " << fullpath << endl;