]> jfr.im git - irc/evilnet/x3.git/blob - src/main-common.c
Fix typos and copy errors in chanserv help
[irc/evilnet/x3.git] / src / main-common.c
1 extern FILE *replay_file;
2
3
4 time_t boot_time;
5 time_t burst_begin;
6 time_t now;
7 time_t burst_length;
8 struct log_type *MAIN_LOG;
9
10 int quit_services;
11 int max_cycles;
12
13 char *services_config = "x3.conf";
14
15 char **services_argv;
16 int services_argc;
17
18 struct cManagerNode cManager;
19
20 struct policer_params *oper_policer_params, *luser_policer_params, *god_policer_params;
21
22 static const struct message_entry msgtab[] = {
23 { "MSG_NONE", "None" },
24 { "MSG_ON", "On" },
25 { "MSG_OFF", "Off" },
26 { "MSG_NEVER", "Never" },
27 { "MSG_SERVICE_IMMUNE", "$b%s$b may not be kicked, killed, banned, or deopped." },
28 { "MSG_SERVICE_PRIVILEGED", "$b%s$b is a privileged service." },
29 { "MSG_NOT_A_SERVICE", "$b%s$b is not a service bot." },
30 { "MSG_COMMAND_UNKNOWN", "$b%s$b is an unknown command." },
31 { "MSG_COMMAND_PRIVILEGED", "$b%s$b is a privileged command." },
32 { "MSG_COMMAND_DISABLED", "$b%s$b is a disabled command." },
33 { "MSG_SETTING_PRIVILEGED", "$b%s$b is a privileged setting." },
34 { "MSG_AUTHENTICATE", "You must first authenticate with $b$N$b." },
35 { "MSG_USER_AUTHENTICATE", "%s must first authenticate with $b$N$b." },
36 { "MSG_SET_EMAIL_ADDR", "You must first set your account's email address. (Contact network staff if you cannot auth to your account.)" },
37 { "MSG_HANDLE_UNKNOWN", "Account $b%s$b has not been registered." },
38 { "MSG_NICK_UNKNOWN", "User with nick $b%s$b does not exist." },
39 { "MSG_CHANNEL_UNKNOWN", "Channel with name $b%s$b does not exist." },
40 { "MSG_SERVER_UNKNOWN", "Server with name $b%s$b does not exist or is not linked." },
41 { "MSG_MODULE_UNKNOWN", "No module has been registered with name $b%s$b." },
42 { "MSG_INVALID_MODES", "$b%s$b is an invalid set of channel modes." },
43 { "MSG_INVALID_GLINE", "Invalid G-line '%s'." },
44 { "MSG_INVALID_DURATION", "Invalid time span '%s'." },
45 { "MSG_NOT_TARGET_NAME", "You must provide the name of a channel or user." },
46 { "MSG_NOT_CHANNEL_NAME", "The channel name you specified is not a valid channel name." },
47 { "MSG_INVALID_CHANNEL", "The channel name you specified does not exist." },
48 { "MSG_CHANNEL_ABSENT", "You aren't currently in $b%s$b." },
49 { "MSG_CHANNEL_USER_ABSENT", "$b%s$b isn't currently in $b%s$b." },
50 { "MSG_MISSING_PARAMS", "$b%s$b requires more parameters." },
51 { "MSG_DEPRECATED_COMMAND", "The $b%s$b command has been deprecated, and will be removed in the future; please use $b%s$b instead." },
52 { "MSG_OPER_SUSPENDED", "Your $b$O$b access has been suspended." },
53 { "MSG_USER_OUTRANKED", "$b%s$b outranks you (command has no effect)." },
54 { "MSG_STUPID_ACCESS_CHANGE", "Please ask someone $belse$b to demote you." },
55 { "MSG_NO_SEARCH_ACCESS", "You do not have enough access to search based on $b%s$b." },
56 { "MSG_INVALID_CRITERIA", "$b%s$b is an invalid search criteria." },
57 { "MSG_MATCH_COUNT", "Found $b%u$b matches." },
58 { "MSG_NO_MATCHES", "Nothing matched the criteria of your search." },
59 { "MSG_TOPIC_UNKNOWN", "No help on that topic." },
60 { "MSG_INVALID_BINARY", "$b%s$b is an invalid binary value." },
61 { "MSG_INTERNAL_FAILURE", "Your command could not be processed due to an internal failure." },
62 { "MSG_DB_UNKNOWN", "I do not know of a database named %s." },
63 { "MSG_DB_IS_MONDO", "Database %s is in the \"mondo\" database and cannot be written separately." },
64 { "MSG_DB_WRITE_ERROR", "Error while writing database %s." },
65 { "MSG_DB_WROTE_DB", "Wrote database %s (in %lu.%06lu seconds)." },
66 { "MSG_DB_WROTE_ALL", "Wrote all databases (in %lu.%06lu seconds)." },
67 { "MSG_AND", "," },
68 { "MSG_0_SECONDS", "0 seconds" },
69 { "MSG_YEAR", "y" },
70 { "MSG_YEARS", "y" },
71 { "MSG_WEEK", "w" },
72 { "MSG_WEEKS", "w" },
73 { "MSG_DAY", "d" },
74 { "MSG_DAYS", "d" },
75 { "MSG_HOUR", "h" },
76 { "MSG_HOURS", "h" },
77 { "MSG_MINUTE", "m" },
78 { "MSG_MINUTES", "m" },
79 { "MSG_SECOND", "s" },
80 { "MSG_SECONDS", "s" },
81 { "MSG_BAR", "----------------------------------------" },
82 { "MSG_INVALID_SHUN", "Invalid Shun '%s'." },
83 { "MSG_MATCH_COUNT", "-----------Found $b%3u$b Matches------------" },
84 { NULL, NULL }
85 };
86
87 void uplink_select(char *name);
88
89 static int
90 uplink_insert(const char *key, void *data, UNUSED_ARG(void *extra))
91 {
92 struct uplinkNode *uplink = malloc(sizeof(struct uplinkNode));
93 struct record_data *rd = data;
94 struct addrinfo hints, *ai;
95 int enabled = 1;
96 char *str;
97
98 if(!uplink)
99 {
100 return 0;
101 }
102
103 uplink->name = (char *)key;
104 uplink->host = database_get_data(rd->d.object, "address", RECDB_QSTRING);
105
106 str = database_get_data(rd->d.object, "port", RECDB_QSTRING);
107 uplink->port = str ? atoi(str) : 6667;
108 uplink->password = database_get_data(rd->d.object, "password", RECDB_QSTRING);
109 uplink->their_password = database_get_data(rd->d.object, "uplink_password", RECDB_QSTRING);
110
111 str = database_get_data(rd->d.object, "enabled", RECDB_QSTRING);
112 if(str)
113 {
114 enabled = atoi(str) ? 1 : 0;
115 }
116
117 cManager.enabled += enabled;
118
119 str = database_get_data(rd->d.object, "max_tries", RECDB_QSTRING);
120 uplink->max_tries = str ? atoi(str) : 3;
121 uplink->flags = enabled ? 0 : UPLINK_UNAVAILABLE;
122 uplink->state = DISCONNECTED;
123 uplink->tries = 0;
124
125 str = database_get_data(rd->d.object, "bind_address", RECDB_QSTRING);
126 memset(&hints, 0, sizeof(hints));
127 hints.ai_flags = AI_PASSIVE;
128 hints.ai_socktype = SOCK_STREAM;
129 if (!getaddrinfo(str, NULL, &hints, &ai))
130 {
131 uplink->bind_addr_len = ai->ai_addrlen;
132 uplink->bind_addr = calloc(1, ai->ai_addrlen);
133 memcpy(uplink->bind_addr, ai->ai_addr, ai->ai_addrlen);
134 freeaddrinfo(ai);
135 }
136 else
137 {
138 uplink->bind_addr = NULL;
139 uplink->bind_addr_len = 0;
140 }
141
142 uplink->next = cManager.uplinks;
143 uplink->prev = NULL;
144
145 if(cManager.uplinks)
146 {
147 cManager.uplinks->prev = uplink;
148 }
149
150 cManager.uplinks = uplink;
151
152 /* If the configuration is being reloaded, set the current uplink
153 to the reloaded equivalent, if possible. */
154 if(cManager.uplink
155 && enabled
156 && !irccasecmp(uplink->host, cManager.uplink->host)
157 && uplink->port == cManager.uplink->port)
158 {
159 uplink->state = cManager.uplink->state;
160 uplink->tries = cManager.uplink->tries;
161 cManager.uplink = uplink;
162 }
163
164 return 0;
165 }
166
167 void
168 uplink_compile(void)
169 {
170 const char *cycles;
171 dict_t conf_node;
172 struct uplinkNode *oldUplinks = NULL, *oldUplink = NULL;
173
174 /* Save the old uplinks, we'll remove them later. */
175 oldUplink = cManager.uplink;
176 oldUplinks = cManager.uplinks;
177
178 cycles = conf_get_data("server/max_cycles", RECDB_QSTRING);
179 max_cycles = cycles ? atoi(cycles) : 30;
180 if(!(conf_node = conf_get_data("uplinks", RECDB_OBJECT)))
181 {
182 log_module(MAIN_LOG, LOG_FATAL, "No uplinks configured; giving up.");
183 exit(1);
184 }
185
186 cManager.enabled = 0;
187 dict_foreach(conf_node, uplink_insert, NULL);
188
189 /* Remove the old uplinks, if any. It doesn't matter if oldUplink (below)
190 is a reference to one of these, because it won't get dereferenced. */
191 if(oldUplinks)
192 {
193 struct uplinkNode *uplink, *next;
194
195 oldUplinks->prev->next = NULL;
196
197 for(uplink = oldUplinks; uplink; uplink = next)
198 {
199 next = uplink->next;
200 free(uplink->bind_addr);
201 free(uplink);
202 }
203 }
204
205 /* If the uplink hasn't changed, it's either NULL or pointing at
206 an uplink that was just deleted, select a new one. */
207 if(cManager.uplink == oldUplink)
208 {
209 if(oldUplink)
210 {
211 irc_squit(self, "Uplinks updated; selecting new uplink.", NULL);
212 }
213
214 cManager.uplink = NULL;
215 uplink_select(NULL);
216 }
217 }
218
219 struct uplinkNode *
220 uplink_find(char *name)
221 {
222 struct uplinkNode *uplink;
223
224 if(!cManager.enabled || !cManager.uplinks)
225 {
226 return NULL;
227 }
228
229 for(uplink = cManager.uplinks; uplink; uplink = uplink->next)
230 {
231 if(!strcasecmp(uplink->name, name))
232 {
233 return uplink;
234 }
235 }
236
237 return NULL;
238 }
239
240 void
241 uplink_select(char *name)
242 {
243 struct uplinkNode *start, *uplink, *next;
244 int stop;
245
246 if(!cManager.enabled || !cManager.uplinks)
247 {
248 log_module(MAIN_LOG, LOG_FATAL, "No uplinks enabled; giving up.");
249 exit(1);
250 }
251
252 if(!cManager.uplink)
253 {
254 start = cManager.uplinks;
255 }
256 else
257 {
258 start = cManager.uplink->next;
259 if(!start)
260 {
261 start = cManager.uplinks;
262 }
263 }
264
265 stop = 0;
266 for(uplink = start; uplink; uplink = next)
267 {
268 next = uplink->next ? uplink->next : cManager.uplinks;
269
270 if(stop)
271 {
272 uplink = NULL;
273 break;
274 }
275
276 /* We've wrapped around the list. */
277 if(next == start)
278 {
279 sleep((cManager.cycles >> 1) * 5);
280 cManager.cycles++;
281
282 if(max_cycles && (cManager.cycles >= max_cycles))
283 {
284 log_module(MAIN_LOG, LOG_ERROR, "Maximum uplink list cycles exceeded; giving up.");
285 exit(1);
286 }
287
288 /* Give the uplink currently in 'uplink' consideration,
289 and if not selected, break on the next iteration. */
290 stop = 1;
291 }
292
293 /* Skip bad uplinks. */
294 if(uplink->flags & UPLINK_UNAVAILABLE)
295 {
296 continue;
297 }
298
299 if(name && irccasecmp(uplink->name, name))
300 {
301 /* If we were told to connect to a specific uplink, don't stop
302 until we find it.
303 */
304 continue;
305 }
306
307 /* It would be possible to track uplink health through a variety
308 of statistics and only break on the best uplink. For now, break
309 on the first available one.
310 */
311
312 break;
313 }
314
315 if(!uplink)
316 {
317 /* We are shit outta luck if every single uplink has been passed
318 over. Use the current uplink if possible. */
319 if(!cManager.uplink || cManager.uplink->flags & UPLINK_UNAVAILABLE)
320 {
321 log_module(MAIN_LOG, LOG_ERROR, "All available uplinks exhausted; giving up.");
322 exit(1);
323 }
324
325 return;
326 }
327
328 cManager.uplink = uplink;
329 }
330
331 int
332 uplink_connect(void)
333 {
334 struct uplinkNode *uplink = cManager.uplink;
335
336 if(uplink->state != DISCONNECTED)
337 {
338 return 0;
339 }
340
341 if(uplink->flags & UPLINK_UNAVAILABLE)
342 {
343 uplink_select(NULL);
344 uplink = cManager.uplink;
345 }
346
347 if(uplink->tries)
348 {
349 /* This delay could scale with the number of tries. */
350 sleep(2);
351 }
352
353 if(!create_socket_client(uplink))
354 {
355 if(uplink->max_tries && (uplink->tries >= uplink->max_tries))
356 {
357 /* This is a bad uplink, move on. */
358 uplink->flags |= UPLINK_UNAVAILABLE;
359 uplink_select(NULL);
360 }
361
362 return 0;
363 }
364 else
365 {
366 uplink->state = AUTHENTICATING;
367 irc_introduce(uplink->password);
368 }
369
370 return 1;
371 }
372
373 void
374 received_ping(void)
375 {
376 /* This function is called when a ping is received. Take it as
377 a sign of link health and reset the connection manager
378 information. */
379
380 cManager.cycles = 0;
381 }
382
383 static exit_func_t *ef_list;
384 static void **ef_list_extra;
385 static unsigned int ef_size = 0, ef_used = 0;
386
387 void reg_exit_func(exit_func_t handler, void *extra)
388 {
389 if (ef_used == ef_size) {
390 if (ef_size) {
391 ef_size <<= 1;
392 ef_list = realloc(ef_list, ef_size*sizeof(exit_func_t));
393 ef_list_extra = realloc(ef_list_extra, ef_size*sizeof(void*));
394 } else {
395 ef_size = 8;
396 ef_list = malloc(ef_size*sizeof(exit_func_t));
397 ef_list_extra = malloc(ef_size*sizeof(void*));
398 }
399 }
400 ef_list[ef_used] = handler;
401 ef_list_extra[ef_used++] = extra;
402 }
403
404 void call_exit_funcs(void)
405 {
406 unsigned int n = ef_used;
407
408 /* Call them in reverse order because we initialize logs, then
409 * nickserv, then chanserv, etc., and they register their exit
410 * funcs in that order, and there are some dependencies (for
411 * example, ChanServ requires NickServ to not have cleaned up).
412 */
413
414 while (n > 0) {
415 --n;
416 ef_list[n](ef_list_extra[n]);
417 }
418 free(ef_list);
419 free(ef_list_extra);
420 ef_used = ef_size = 0;
421 }
422
423 int
424 set_policer_param(const char *param, void *data, void *extra)
425 {
426 struct record_data *rd = data;
427 const char *str = GET_RECORD_QSTRING(rd);
428 if (str) {
429 policer_params_set(extra, param, str);
430 }
431 return 0;
432 }
433
434 static void
435 conf_globals(void)
436 {
437 const char *info;
438 dict_t dict;
439
440 info = conf_get_data("services/opserv/nick", RECDB_QSTRING);
441 if (info && (info[0] == '.'))
442 info = NULL;
443 init_opserv(info);
444
445 info = conf_get_data("services/global/nick", RECDB_QSTRING);
446 if (info && (info[0] == '.'))
447 info = NULL;
448 init_global(info);
449
450 info = conf_get_data("services/nickserv/nick", RECDB_QSTRING);
451 if (info && (info[0] == '.'))
452 info = NULL;
453 init_nickserv(info);
454
455 info = conf_get_data("services/chanserv/nick", RECDB_QSTRING);
456 if (info && (info[0] == '.'))
457 info = NULL;
458 init_chanserv(info);
459
460 god_policer_params = policer_params_new();
461 if ((dict = conf_get_data("policers/commands-god", RECDB_OBJECT))) {
462 dict_foreach(dict, set_policer_param, god_policer_params);
463 } else {
464 policer_params_set(god_policer_params, "size", "30");
465 policer_params_set(god_policer_params, "drain-rate", "1");
466 }
467 oper_policer_params = policer_params_new();
468 if ((dict = conf_get_data("policers/commands-oper", RECDB_OBJECT))) {
469 dict_foreach(dict, set_policer_param, oper_policer_params);
470 } else {
471 policer_params_set(oper_policer_params, "size", "10");
472 policer_params_set(oper_policer_params, "drain-rate", "1");
473 }
474 luser_policer_params = policer_params_new();
475 if ((dict = conf_get_data("policers/commands-luser", RECDB_OBJECT))) {
476 dict_foreach(dict, set_policer_param, luser_policer_params);
477 } else {
478 policer_params_set(luser_policer_params, "size", "5");
479 policer_params_set(luser_policer_params, "drain-rate", "0.50");
480 }
481
482 info = conf_get_data("services/spamserv/nick", RECDB_QSTRING);
483 if (info && (info[0] == '.'))
484 info = NULL;
485 init_spamserv(info);
486 }
487
488 #ifdef HAVE_SYS_RESOURCE_H
489
490 static int
491 set_item_rlimit(const char *name, void *data, void *extra)
492 {
493 long rsrc;
494 int found;
495 struct record_data *rd = data;
496 struct rlimit rlim;
497 const char *str;
498
499 rsrc = (long)dict_find(extra, name, &found);
500 if (!found) {
501 log_module(MAIN_LOG, LOG_ERROR, "Invalid rlimit \"%s\" in rlimits section.", name);
502 return 0;
503 }
504 if (!(str = GET_RECORD_QSTRING(rd))) {
505 log_module(MAIN_LOG, LOG_ERROR, "Missing or invalid parameter type for rlimit \"%s\".", name);
506 return 0;
507 }
508 if (getrlimit(rsrc, &rlim) < 0) {
509 log_module(MAIN_LOG, LOG_ERROR, "Couldn't get rlimit \"%s\": errno %d: %s", name, errno, strerror(errno));
510 return 0;
511 }
512 rlim.rlim_cur = ParseVolume(str);
513 if (setrlimit(rsrc, &rlim) < 0) {
514 log_module(MAIN_LOG, LOG_ERROR, "Couldn't set rlimit \"%s\": errno %d: %s", name, errno, strerror(errno));
515 }
516 return 0;
517 }
518
519 static void
520 conf_rlimits(void)
521 {
522 dict_t dict, values;
523
524 values = dict_new();
525 dict_insert(values, "data", (void*)RLIMIT_DATA);
526 dict_insert(values, "stack", (void*)RLIMIT_STACK);
527 #ifdef RLIMIT_VMEM
528 dict_insert(values, "vmem", (void*)RLIMIT_VMEM);
529 #else
530 #ifdef RLIMIT_AS
531 dict_insert(values, "vmem", (void*)RLIMIT_AS);
532 #endif
533 #endif
534 if ((dict = conf_get_data("rlimits", RECDB_OBJECT))) {
535 dict_foreach(dict, set_item_rlimit, values);
536 }
537 dict_delete(values);
538 }
539
540 #else
541
542 static void
543 conf_rlimits(void)
544 {
545 }
546
547 #endif
548
549 static void
550 usage(char *exe_name)
551 {
552 /* We can assume we have getopt_long(). */
553 printf("Usage: %s [-c config] [-r log] [-d] [-f] [-v|-h]\n"
554 " -c, --config selects a different configuration file.\n"
555 " -d, --debug enables debug mode.\n"
556 " -f, --foreground run X3 in the foreground.\n"
557 " -h, --help prints this usage message.\n"
558 " -k, --check checks the configuration file's syntax.\n"
559 " -r, --replay replay a log file (for debugging).\n"
560 " -v, --version prints this program's version.\n"
561 , exe_name);
562 }
563
564 static void
565 version()
566 {
567 printf(" --------------------------------------------------\n"
568 " - "PACKAGE_STRING", Built: " __DATE__ ", " __TIME__".\n"
569 " - Copyright (C) 2000 - 2005, srvx Development Team\n"
570 " - Copyright (C) 2004 - 2005, X3 Development Team\n"
571 " --------------------------------------------------\n");
572 }
573
574 static void
575 license()
576 {
577 printf("\n"
578 "This program is free software; you can redistribute it and/or modify\n"
579 "it under the terms of the GNU General Public License as published by\n"
580 "the Free Software Foundation; either version 3 of the License, or\n"
581 "(at your option) any later version.\n"
582 "\n"
583 "This program is distributed in the hope that it will be useful,\n"
584 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
585 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
586 "GNU General Public License for more details.\n"
587 "\n"
588 "You should have received a copy of the GNU General Public License\n"
589 "along with this program; if not, write to the Free Software\n"
590 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");
591 }
592
593 void main_shutdown(UNUSED_ARG(void *extra))
594 {
595 struct uplinkNode *ul, *ul_next;
596 ioset_cleanup();
597 for (ul = cManager.uplinks; ul; ul = ul_next) {
598 ul_next = ul->next;
599 free(ul->bind_addr);
600 free(ul);
601 }
602 tools_cleanup();
603 conf_close();
604 #if defined(PID_FILE)
605 remove(PID_FILE);
606 #endif
607 policer_params_delete(god_policer_params);
608 policer_params_delete(oper_policer_params);
609 policer_params_delete(luser_policer_params);
610 if (replay_file)
611 fclose(replay_file);
612 }