/[cvs]/eggdrop1.9/src/main.c
ViewVC logotype

Contents of /eggdrop1.9/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.134 - (show annotations) (download) (as text)
Fri Feb 14 20:55:02 2003 UTC (16 years, 6 months ago) by stdarg
Branch: MAIN
Changes since 1.133: +21 -18 lines
File MIME type: text/x-chdr
* Added autoload section to xml config file and got rid of the -p option (preload).
* To load your old config file, rename it to blah.tcl and put <script>blah.tcl</script> in the autoload section.
* Added sample config.xml and config.tcl files.
* Moved some more script functions to the script module.

1 /*
2 * main.c --
3 *
4 * core event handling
5 * signal handling
6 * command line arguments
7 */
8 /*
9 * Copyright (C) 1997 Robey Pointer
10 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Eggheads Development Team
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26 /*
27 * The author (Robey Pointer) can be reached at: robey@netcom.com
28 * NOTE: Robey is no longer working on this code, there is a discussion
29 * list available at eggheads@eggheads.org.
30 */
31
32 #ifndef lint
33 static const char rcsid[] = "$Id: main.c,v 1.133 2003/02/11 02:32:05 stdarg Exp $";
34 #endif
35
36 #include <eggdrop/eggdrop.h>
37 #include "main.h"
38 #include "core_config.h"
39 #include <fcntl.h>
40 #ifdef TIME_WITH_SYS_TIME
41 # include <sys/time.h>
42 # include <time.h>
43 #else
44 # ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
46 # else
47 # include <time.h>
48 # endif
49 #endif
50 #include <errno.h>
51 #include <signal.h>
52 #include <netdb.h>
53 #include <setjmp.h>
54 #include "users.h" /* check_expired_ignores,
55 get_user_by_handle */
56 #include "chanprog.h" /* tell_verbose_status,
57 chanprog, rehash */
58 #include "net.h" /* killsock, setsock,
59 dequeue_sockets, sockgets */
60 #include "userrec.h" /* write_userfile, count_users */
61 #include <locale.h>
62
63 #ifdef STOP_UAC /* osf/1 complains a lot */
64 #include <sys/sysinfo.h>
65 #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
66 #endif
67 /* Some systems have a working sys/wait.h even though configure will
68 * decide it's not bsd compatable. Oh well.
69 */
70
71 #include "chan.h"
72 #include "modules.h"
73 #include "bg.h"
74 #include "core_binds.h"
75 #include "logfile.h"
76 #include "misc.h"
77 #include "traffic.h"
78 #include "dccutil.h" /* dprintf_eggdrop, new_dcc,
79 dcc_chatter, dcc_remove_lost,
80 lostdcc */
81
82 #include "lib/adns/adns.h"
83
84 #ifdef CYGWIN_HACKS
85 #include <windows.h>
86 #endif
87
88 #ifndef _POSIX_SOURCE
89 /* Solaris needs this */
90 #define _POSIX_SOURCE 1
91 #endif
92
93 extern char userfile[], myname[];
94 extern int dcc_total, conmask, cache_hit, cache_miss;
95 extern struct dcc_t *dcc;
96 extern struct userrec *userlist;
97 extern struct chanset_t *chanset;
98 extern jmp_buf alarmret;
99
100 #ifndef MAKING_MODS
101 extern struct dcc_table DCC_CHAT;
102 #endif /* MAKING_MODS */
103
104 /*
105 * Please use the PATCH macro instead of directly altering the version
106 * string from now on (it makes it much easier to maintain patches).
107 * Also please read the README file regarding your rights to distribute
108 * modified versions of this bot.
109 */
110
111 char egg_version[1024] = VERSION;
112 int egg_numver = VERSION_NUM;
113
114 eggdrop_t *egg = NULL; /* Eggdrop's context */
115
116 char notify_new[121] = ""; /* Person to send a note to for new users */
117 int default_flags = 0; /* Default user flags and */
118 int default_uflags = 0; /* Default userdefinied flags for people
119 who say 'hello' or for .adduser */
120
121 int backgrd = 1; /* Run in the background? */
122 int con_chan = 0; /* Foreground: constantly display channel
123 stats? */
124 int term_z = 0; /* Foreground: use the terminal as a party
125 line? */
126 char configfile[121] = "config.xml"; /* Name of the config file */
127 char helpdir[121] = "help/"; /* Directory of help files (if used) */
128 char textdir[121] = "text/"; /* Directory for text files that get dumped */
129 time_t online_since; /* Unix-time that the bot loaded up */
130 int make_userfile = 0; /* Using bot in make-userfile mode? (first
131 user to 'hello' becomes master) */
132 char owner[121] = ""; /* Permanent owner(s) of the bot */
133 char pid_file[120]; /* Name of the file for the pid to be
134 stored in */
135 int save_users_at = 0; /* How many minutes past the hour to
136 save the userfile? */
137 int notify_users_at = 0; /* How many minutes past the hour to
138 notify users of notes? */
139 char version[81]; /* Version info (long form) */
140 char ver[41]; /* Version info (short form) */
141 char egg_xtra[2048]; /* Patch info */
142 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
143 int do_restart = 0; /* .restart has been called, restart asap */
144 int die_on_sighup = 0; /* die if bot receives SIGHUP */
145 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
146 int resolve_timeout = 15; /* hostname/address lookup timeout */
147 char quit_msg[1024]; /* quit message */
148 time_t now; /* duh, now :) */
149 egg_timeval_t egg_timeval_now; /* Same thing, but seconds and microseconds. */
150
151 void fatal(const char *s, int recoverable)
152 {
153 int i;
154
155 putlog(LOG_MISC, "*", "* %s", s);
156 flushlogs();
157 for (i = 0; i < dcc_total; i++)
158 if (dcc[i].sock >= 0)
159 killsock(dcc[i].sock);
160 unlink(pid_file);
161 if (!recoverable) {
162 bg_send_quit(BG_ABORT);
163 exit(1);
164 }
165 }
166
167 static void check_expired_dcc()
168 {
169 int i;
170
171 for (i = 0; i < dcc_total; i++)
172 if (dcc[i].type && dcc[i].type->timeout_val &&
173 ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
174 if (dcc[i].type->timeout)
175 dcc[i].type->timeout(i);
176 else if (dcc[i].type->eof)
177 dcc[i].type->eof(i);
178 else
179 continue;
180 /* Only timeout 1 socket per cycle, too risky for more */
181 return;
182 }
183 }
184
185 static void got_bus(int z)
186 {
187 fatal("BUS ERROR -- CRASHING!", 1);
188 #ifdef SA_RESETHAND
189 kill(getpid(), SIGBUS);
190 #else
191 bg_send_quit(BG_ABORT);
192 exit(1);
193 #endif
194 }
195
196 static void got_segv(int z)
197 {
198 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
199 #ifdef SA_RESETHAND
200 kill(getpid(), SIGSEGV);
201 #else
202 bg_send_quit(BG_ABORT);
203 exit(1);
204 #endif
205 }
206
207 static void got_fpe(int z)
208 {
209 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
210 }
211
212 static void got_term(int z)
213 {
214 write_userfile(-1);
215 check_bind_event("sigterm");
216 if (die_on_sigterm) {
217 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
218 } else {
219 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
220 }
221 }
222
223 static void got_quit(int z)
224 {
225 check_bind_event("sigquit");
226 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
227 return;
228 }
229
230 static void got_hup(int z)
231 {
232 write_userfile(-1);
233 check_bind_event("sighup");
234 if (die_on_sighup) {
235 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
236 } else
237 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
238 do_restart = -2;
239 return;
240 }
241
242 /* A call to resolver (gethostbyname, etc) timed out
243 */
244 static void got_alarm(int z)
245 {
246 longjmp(alarmret, 1);
247
248 /* -Never reached- */
249 }
250
251 /* Got ILL signal
252 */
253 static void got_ill(int z)
254 {
255 check_bind_event("sigill");
256 }
257
258
259 static void print_version (void)
260 {
261 char x[1024], *z = x;
262
263 strlcpy(x, egg_version, sizeof x);
264 newsplit(&z);
265 newsplit(&z);
266 printf("%s\n", version);
267 if (z[0])
268 printf(" (patches: %s)\n", z);
269 }
270
271 static void print_help (void)
272 {
273 print_version ();
274 printf("\n\
275 Usage: %s [OPTIONS]... [FILE]\n", PACKAGE);
276 printf(_("\n\
277 -h, --help Print help and exit\n\
278 -v, --version Print version and exit\n\
279 -n, --foreground Don't go into the background (default=off)\n\
280 -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
281 -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
282 -m --make-userfile Userfile creation mode\n\
283 -p --load-module=STRING Modules to load before parsing any configuration\n\
284 file\n\
285 FILE optional config filename (default 'eggdrop.conf')\n"));
286 printf("\n");
287 }
288
289 static void do_args(int argc, char * const *argv)
290 {
291 int c;
292
293 optarg = 0;
294 optind = 1;
295 opterr = 1;
296 optopt = '?';
297
298 while (1) {
299 int option_index = 0;
300 static struct option long_options[] = {
301 { "help", 0, NULL, 'h' },
302 { "version", 0, NULL, 'v' },
303 { "load-module", 1, NULL, 'p' },
304 { "foreground", 0, NULL, 'n' },
305 { "channel-stats", 0, NULL, 'c' },
306 { "terminal", 0, NULL, 't' },
307 { "make-userfile", 0, NULL, 'm' },
308 { NULL, 0, NULL, 0 }
309 };
310
311 c = getopt_long (argc, argv, "hvp:nctm", long_options, &option_index);
312
313 if (c == -1) break;
314
315 switch (c) {
316 case 'n':
317 backgrd = 0;
318 break;
319
320 case 'c':
321 con_chan = 1;
322 term_z = 0;
323 break;
324
325 case 't':
326 con_chan = 0;
327 term_z = 1;
328 break;
329
330 case 'm':
331 make_userfile = 1;
332 break;
333
334 case 'v':
335 print_version();
336 bg_send_quit(BG_ABORT);
337 exit(0);
338 break; /* this should never be reached */
339
340 case 'h':
341 print_help();
342 bg_send_quit(BG_ABORT);
343 exit(0);
344 break; /* this should never be reached */
345
346 case 0: /* Long option with no short option */
347 case '?': /* Invalid option. */
348 /* getopt_long() already printed an error message. */
349 bg_send_quit(BG_ABORT);
350 exit(1);
351
352 default: /* bug: option not considered. */
353 fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
354 bg_send_quit(BG_ABORT);
355 exit(1);
356 break; /* this should never be reached */
357 }
358 }
359 if (optind < argc)
360 strlcpy(configfile, argv[optind], sizeof configfile);
361 }
362
363 /* Timer info */
364 static int lastmin = 99;
365 static struct tm nowtm;
366
367 /* Called once a second.
368 */
369 static void core_secondly()
370 {
371 static int cnt = 0;
372 int miltime;
373
374 call_hook(HOOK_SECONDLY); /* Will be removed later */
375 cnt++;
376 if (cnt >= 10) { /* Every 10 seconds */
377 cnt = 0;
378 check_expired_dcc();
379 if (con_chan && !backgrd) {
380 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
381 //tell_verbose_status(DP_STDOUT);
382 do_module_report(DP_STDOUT, 0, "server");
383 do_module_report(DP_STDOUT, 0, "channels");
384 }
385 }
386 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
387 if (nowtm.tm_min != lastmin) {
388 int i = 0;
389
390 /* Once a minute */
391 lastmin = (lastmin + 1) % 60;
392 call_hook(HOOK_MINUTELY);
393 check_expired_ignores();
394 /* In case for some reason more than 1 min has passed: */
395 while (nowtm.tm_min != lastmin) {
396 /* Timer drift, dammit */
397 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
398 i++;
399 lastmin = (lastmin + 1) % 60;
400 call_hook(HOOK_MINUTELY);
401 }
402 if (i > 1)
403 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
404 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
405 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
406 call_hook(HOOK_5MINUTELY);
407 if (!miltime) { /* At midnight */
408 char s[25];
409
410 strlcpy(s, ctime(&now), sizeof s);
411 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
412 call_hook(HOOK_BACKUP);
413 call_hook(HOOK_DAILY);
414 }
415 }
416 if (nowtm.tm_min == notify_users_at)
417 call_hook(HOOK_HOURLY);
418 }
419 }
420
421 static void core_minutely()
422 {
423 check_bind_time(&nowtm);
424 }
425
426 static void core_hourly()
427 {
428 write_userfile(-1);
429 }
430
431 static void event_rehash()
432 {
433 check_bind_event("rehash");
434 }
435
436 static void event_prerehash()
437 {
438 check_bind_event("prerehash");
439 }
440
441 static void event_save()
442 {
443 check_bind_event("save");
444 }
445
446 static void event_logfile()
447 {
448 check_bind_event("logfile");
449 }
450
451 static void event_loaded()
452 {
453 check_bind_event("loaded");
454 }
455
456 void kill_tcl();
457 extern module_entry *module_list;
458
459 int init_userent(), init_net(), init_tcl(int, char **);
460 void core_party_init();
461 void core_config_init();
462 void telnet_init();
463 void dns_init();
464 void binds_init();
465 void dcc_init();
466
467 void patch(const char *str)
468 {
469 char *p = strchr(egg_version, '+');
470
471 if (!p)
472 p = &egg_version[strlen(egg_version)];
473 sprintf(p, "+%s", str);
474 egg_numver++;
475 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
476 }
477
478 /*
479 static inline void garbage_collect(void)
480 {
481 static u_8bit_t run_cnt = 0;
482
483 if (run_cnt == 3)
484 garbage_collect_tclhash();
485 else
486 run_cnt++;
487 }
488 */
489
490 int main(int argc, char **argv)
491 {
492 int xx, i;
493 char buf[520], s[25];
494 FILE *f;
495 struct sigaction sv;
496 struct chanset_t *chan;
497 egg_timeval_t howlong;
498 int timeout;
499 void *config_root;
500 char *modname, *scriptname;
501
502 #ifdef DEBUG
503 /* Make sure it can write core, if you make debug. Else it's pretty
504 * useless (dw)
505 */
506 {
507 #include <sys/resource.h>
508 struct rlimit cdlim;
509
510 cdlim.rlim_cur = RLIM_INFINITY;
511 cdlim.rlim_max = RLIM_INFINITY;
512 setrlimit(RLIMIT_CORE, &cdlim);
513 }
514 #endif
515
516 #ifdef ENABLE_NLS
517 setlocale(LC_MESSAGES, "");
518 bindtextdomain(PACKAGE, LOCALEDIR);
519 textdomain(PACKAGE);
520 #endif
521
522 #include "patch.h"
523 /* Version info! */
524 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
525 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2003 Eggheads",
526 egg_version);
527 /* Now add on the patchlevel (for Tcl) */
528 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
529 strcat(egg_version, egg_xtra);
530 #ifdef STOP_UAC
531 {
532 int nvpair[2];
533
534 nvpair[0] = SSIN_UACPROC;
535 nvpair[1] = UAC_NOPRINT;
536 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
537 }
538 #endif
539
540 /* Set up error traps: */
541 sv.sa_handler = got_bus;
542 sigemptyset(&sv.sa_mask);
543 #ifdef SA_RESETHAND
544 sv.sa_flags = SA_RESETHAND;
545 #else
546 sv.sa_flags = 0;
547 #endif
548 sigaction(SIGBUS, &sv, NULL);
549 sv.sa_handler = got_segv;
550 sigaction(SIGSEGV, &sv, NULL);
551 #ifdef SA_RESETHAND
552 sv.sa_flags = 0;
553 #endif
554 sv.sa_handler = got_fpe;
555 sigaction(SIGFPE, &sv, NULL);
556 sv.sa_handler = got_term;
557 sigaction(SIGTERM, &sv, NULL);
558 sv.sa_handler = got_hup;
559 sigaction(SIGHUP, &sv, NULL);
560 sv.sa_handler = got_quit;
561 sigaction(SIGQUIT, &sv, NULL);
562 sv.sa_handler = SIG_IGN;
563 sigaction(SIGPIPE, &sv, NULL);
564 sv.sa_handler = got_ill;
565 sigaction(SIGILL, &sv, NULL);
566 sv.sa_handler = got_alarm;
567 sigaction(SIGALRM, &sv, NULL);
568
569 /* Initialize variables and stuff */
570 timer_update_now(&egg_timeval_now);
571 now = egg_timeval_now.sec;
572 chanset = NULL;
573 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
574 lastmin = nowtm.tm_min;
575 srandom(now % (getpid() + getppid()));
576
577 do_args(argc, argv);
578
579 printf("\n%s\n", version);
580 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
581
582 /* Don't allow eggdrop to run as root */
583 if (((int) getuid() == 0) || ((int) geteuid() == 0))
584 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
585
586 egg = eggdrop_new();
587 core_config_init(configfile);
588 egglog_init();
589 logfile_init();
590 script_init();
591 binds_init();
592 user_init();
593 if (core_config.userfile) user_load(core_config.userfile);
594 partyline_init();
595 core_party_init();
596 telnet_init();
597 modules_init();
598 logfile_init();
599 dns_init();
600 egg_dns_init();
601 core_binds_init();
602 dcc_init();
603 init_userent();
604 init_net();
605 traffic_init();
606
607 if (backgrd)
608 bg_prepare_split();
609
610 init_tcl(argc, argv);
611
612 strlcpy(s, ctime(&now), sizeof s);
613 strcpy(&s[11], &s[20]);
614 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
615
616 /* Scan the autoload section of config. */
617 config_root = config_get_root("eggdrop");
618 for (i = 0; config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL); i++) {
619 modname = NULL;
620 config_get_str(&modname, config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL);
621 module_load(modname);
622 }
623 for (i = 0; config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL); i++) {
624 scriptname = NULL;
625 config_get_str(&scriptname, config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL);
626 script_load(scriptname);
627 }
628
629
630 i = 0;
631 for (chan = chanset; chan; chan = chan->next)
632 i++;
633 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.", core_config.botname, i,
634 count_users(userlist));
635 cache_miss = 0;
636 cache_hit = 0;
637
638 if (!pid_file[0])
639 snprintf(pid_file, sizeof pid_file, "pid.%s", myname);
640
641 /* Check for pre-existing eggdrop! */
642 f = fopen(pid_file, "r");
643 if (f != NULL) {
644 fgets(s, 10, f);
645 xx = atoi(s);
646 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
647 is used */
648 if (errno != ESRCH) {
649 printf(_("I detect %s already running from this directory.\n"), myname);
650 printf(_("If this is incorrect, erase the %s\n"), pid_file);
651 bg_send_quit(BG_ABORT);
652 exit(1);
653 }
654 }
655
656 /* Move into background? */
657 if (backgrd) {
658 #ifndef CYGWIN_HACKS
659 bg_do_split();
660 } else { /* !backgrd */
661 #endif
662 xx = getpid();
663 if (xx != 0) {
664 FILE *fp;
665
666 /* Write pid to file */
667 unlink(pid_file);
668 fp = fopen(pid_file, "w");
669 if (fp != NULL) {
670 fprintf(fp, "%u\n", xx);
671 if (fflush(fp)) {
672 /* Let the bot live since this doesn't appear to be a botchk */
673 printf(_("* Warning! Could not write %s file!\n"), pid_file);
674 fclose(fp);
675 unlink(pid_file);
676 } else
677 fclose(fp);
678 } else
679 printf(_("* Warning! Could not write %s file!\n"), pid_file);
680 #ifdef CYGWIN_HACKS
681 printf("Launched into the background (pid: %d)\n\n", xx);
682 #endif
683 }
684 }
685
686 use_stderr = 0; /* Stop writing to stderr now */
687 if (backgrd) {
688 /* Ok, try to disassociate from controlling terminal (finger cross) */
689 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
690 setpgid(0, 0);
691 #endif
692 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
693 freopen("/dev/null", "r", stdin);
694 freopen("/dev/null", "w", stdout);
695 freopen("/dev/null", "w", stderr);
696 #ifdef CYGWIN_HACKS
697 FreeConsole();
698 #endif
699 }
700
701 /* Terminal emulating dcc chat */
702 if (!backgrd && term_z) {
703 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
704
705 dcc[n].addr[0] = '\0';
706 dcc[n].sock = STDOUT;
707 dcc[n].timeval = now;
708 dcc[n].u.chat->con_flags = conmask;
709 dcc[n].status = STAT_ECHO;
710 strcpy(dcc[n].nick, "HQ");
711 strcpy(dcc[n].host, "llama@console");
712 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
713 /* Make sure there's an innocuous HQ user if needed */
714 if (!dcc[n].user) {
715 userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
716 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
717 }
718 setsock(STDOUT, 0); /* Entry in net table */
719 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
720 dcc_chatter(n);
721 }
722
723 online_since = now;
724 add_help_reference("cmds1.help");
725 add_help_reference("cmds2.help");
726 add_help_reference("core.help");
727 howlong.sec = 1;
728 howlong.usec = 0;
729 timer_create_repeater(&howlong, (Function) core_secondly);
730
731 /* init time'd hooks */
732 add_hook(HOOK_MINUTELY, (Function) core_minutely);
733 add_hook(HOOK_HOURLY, (Function) core_hourly);
734 add_hook(HOOK_REHASH, (Function) event_rehash);
735 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
736 add_hook(HOOK_USERFILE, (Function) event_save);
737 add_hook(HOOK_BACKUP, (Function) backup_userfile);
738 add_hook(HOOK_DAILY, (Function) event_logfile);
739 add_hook(HOOK_LOADED, (Function) event_loaded);
740
741 call_hook(HOOK_LOADED);
742
743 debug0("main: entering loop");
744 while (1) {
745 int socket_cleanup = 0;
746
747 #if !defined(HAVE_PRE7_5_TCL)
748 /* Process a single tcl event */
749 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
750 #endif
751
752 /* Lets move some of this here, reducing the numer of actual
753 * calls to periodic_timers
754 */
755 timer_get_time(&egg_timeval_now);
756 now = egg_timeval_now.sec;
757 random(); /* Woop, lets really jumble things */
758 timer_run();
759
760 /* Only do this every so often. */
761 if (!socket_cleanup) {
762 socket_cleanup = 5;
763
764 /* Remove dead dcc entries. */
765 dcc_remove_lost();
766
767 /* Check for server or dcc activity. */
768 dequeue_sockets();
769 } else
770 socket_cleanup--;
771
772 /* Free unused structures. */
773 /* garbage_collect(); */
774
775 if (timer_get_shortest(&howlong)) {
776 timeout = 1000;
777 }
778 else {
779 timeout = howlong.sec * 1000 + howlong.usec / 1000;
780 }
781 sockbuf_update_all(timeout);
782
783 xx = sockgets(buf, &i);
784 timer_update_now(&egg_timeval_now);
785 if (xx >= 0) { /* Non-error */
786 int idx;
787
788 for (idx = 0; idx < dcc_total; idx++)
789 if (dcc[idx].sock == xx) {
790 if (dcc[idx].type && dcc[idx].type->activity) {
791
792 /* update traffic stats */
793 traffic_update_in(dcc[idx].type, (strlen(buf) + 1));
794
795 dcc[idx].type->activity(idx, buf, i);
796 } else
797 putlog(LOG_MISC, "*",
798 "!!! untrapped dcc activity: type %s, sock %d",
799 dcc[idx].type->name, dcc[idx].sock);
800 break;
801 }
802 } else if (xx == -1) { /* EOF from someone */
803 int idx;
804
805 if (i == STDOUT && !backgrd)
806 fatal("END OF FILE ON TERMINAL", 0);
807 for (idx = 0; idx < dcc_total; idx++)
808 if (dcc[idx].sock == i) {
809 if (dcc[idx].type && dcc[idx].type->eof)
810 dcc[idx].type->eof(idx);
811 else {
812 putlog(LOG_MISC, "*",
813 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
814 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
815 killsock(i);
816 lostdcc(idx);
817 }
818 idx = dcc_total + 1;
819 }
820 if (idx == dcc_total) {
821 putlog(LOG_MISC, "*",
822 "(@) EOF socket %d, not a dcc socket, not anything.", i);
823 close(i);
824 killsock(i);
825 }
826 } else if (xx == -2 && errno != EINTR) { /* select() error */
827 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
828 for (i = 0; i < dcc_total; i++) {
829 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno == EBADF)) {
830 putlog(LOG_MISC, "*",
831 "DCC socket %d (type %d, name '%s') expired -- pfft",
832 dcc[i].sock, dcc[i].type, dcc[i].nick);
833 killsock(dcc[i].sock);
834 lostdcc(i);
835 i--;
836 }
837 }
838 } else if (xx == -3) {
839 call_hook(HOOK_IDLE);
840 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
841 }
842
843 if (do_restart) {
844 if (do_restart == -2)
845 rehash();
846 else {
847 /* Unload as many modules as possible */
848 int f = 1;
849 module_entry *p;
850 Function x;
851 char xx[256];
852
853 /* oops, I guess we should call this event before tcl is restarted */
854 check_bind_event("prerestart");
855
856 while (f) {
857 f = 0;
858 for (p = module_list; p != NULL; p = p->next) {
859 dependancy *d = dependancy_list;
860 int ok = 1;
861
862 while (ok && d) {
863 if (d->needed == p)
864 ok = 0;
865 d = d->next;
866 }
867 if (ok) {
868 strcpy(xx, p->name);
869 if (module_unload(xx, myname) == NULL) {
870 f = 1;
871 break;
872 }
873 }
874 }
875 }
876 p = module_list;
877 if (p && p->next && p->next->next)
878 /* Should be only 2 modules now - blowfish (or some other
879 encryption module) and eggdrop. */
880 putlog(LOG_MISC, "*", _("Stagnant module; there WILL be memory leaks!"));
881 flushlogs();
882 kill_tcl();
883 timer_destroy_all(); /* Destroy all timers. */
884 init_tcl(argc, argv);
885 /* We expect the encryption module as the current module pointed
886 * to by `module_list'.
887 */
888 x = p->funcs[MODCALL_START];
889 /* `NULL' indicates that we just recently restarted. The module
890 * is expected to re-initialise as needed.
891 */
892 x(NULL);
893 rehash();
894 call_hook(HOOK_LOADED);
895 }
896 do_restart = 0;
897 }
898 }
899 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23