/[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.129 - (show annotations) (download) (as text)
Wed Jan 29 07:42:50 2003 UTC (16 years, 5 months ago) by wcc
Branch: MAIN
Changes since 1.128: +12 -3 lines
File MIME type: text/x-chdr
* Synch 1.6: save_ignores, save_ignores2, killtransfer-segv,
             bugfixes (whatever applied), header-fixes, hello.

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.128 2003/01/02 21:33:16 wcc Exp $";
34 #endif
35
36 #include <eggdrop/eggdrop.h>
37 #include "main.h"
38 #include <fcntl.h>
39 #ifdef TIME_WITH_SYS_TIME
40 # include <sys/time.h>
41 # include <time.h>
42 #else
43 # ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
45 # else
46 # include <time.h>
47 # endif
48 #endif
49 #include <errno.h>
50 #include <signal.h>
51 #include <netdb.h>
52 #include <setjmp.h>
53 #include "users.h" /* check_expired_ignores,
54 get_user_by_handle */
55 #include "chanprog.h" /* tell_verbose_status,
56 chanprog, rehash */
57 #include "net.h" /* killsock, setsock,
58 dequeue_sockets, sockgets */
59 #include "userrec.h" /* write_userfile, count_users */
60 #include <locale.h>
61
62 #ifdef STOP_UAC /* osf/1 complains a lot */
63 #include <sys/sysinfo.h>
64 #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
65 #endif
66 /* Some systems have a working sys/wait.h even though configure will
67 * decide it's not bsd compatable. Oh well.
68 */
69
70 #include "chan.h"
71 #include "modules.h"
72 #include "bg.h"
73 #include "core_binds.h"
74 #include "logfile.h"
75 #include "misc.h"
76 #include "traffic.h"
77 #include "dccutil.h" /* dprintf_eggdrop, new_dcc,
78 dcc_chatter, dcc_remove_lost,
79 lostdcc */
80
81 #include "lib/adns/adns.h"
82
83 #ifdef CYGWIN_HACKS
84 #include <windows.h>
85 #endif
86
87 #ifndef _POSIX_SOURCE
88 /* Solaris needs this */
89 #define _POSIX_SOURCE 1
90 #endif
91
92 extern char userfile[], botnetnick[];
93 extern int dcc_total, conmask, cache_hit, cache_miss;
94 extern struct dcc_t *dcc;
95 extern struct userrec *userlist;
96 extern struct chanset_t *chanset;
97 extern jmp_buf alarmret;
98
99 #ifndef MAKING_MODS
100 extern struct dcc_table DCC_CHAT;
101 #endif /* MAKING_MODS */
102
103 /*
104 * Please use the PATCH macro instead of directly altering the version
105 * string from now on (it makes it much easier to maintain patches).
106 * Also please read the README file regarding your rights to distribute
107 * modified versions of this bot.
108 */
109
110 char egg_version[1024] = VERSION;
111 int egg_numver = VERSION_NUM;
112
113 eggdrop_t *egg = NULL; /* Eggdrop's context */
114
115 char notify_new[121] = ""; /* Person to send a note to for new users */
116 int default_flags = 0; /* Default user flags and */
117 int default_uflags = 0; /* Default userdefinied flags for people
118 who say 'hello' or for .adduser */
119
120 int backgrd = 1; /* Run in the background? */
121 int con_chan = 0; /* Foreground: constantly display channel
122 stats? */
123 int term_z = 0; /* Foreground: use the terminal as a party
124 line? */
125 char configfile[121] = "eggdrop.conf"; /* Name of the config file */
126 static char preload_module[121] = ""; /* Name of the module to preload */
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 'p':
335 strlcpy(preload_module, optarg, sizeof preload_module);
336 break;
337
338 case 'v':
339 print_version();
340 bg_send_quit(BG_ABORT);
341 exit(0);
342 break; /* this should never be reached */
343
344 case 'h':
345 print_help();
346 bg_send_quit(BG_ABORT);
347 exit(0);
348 break; /* this should never be reached */
349
350 case 0: /* Long option with no short option */
351 case '?': /* Invalid option. */
352 /* getopt_long() already printed an error message. */
353 bg_send_quit(BG_ABORT);
354 exit(1);
355
356 default: /* bug: option not considered. */
357 fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
358 bg_send_quit(BG_ABORT);
359 exit(1);
360 break; /* this should never be reached */
361 }
362 }
363 if (optind < argc)
364 strlcpy(configfile, argv[optind], sizeof configfile);
365 }
366
367 /* Timer info */
368 static int lastmin = 99;
369 static struct tm nowtm;
370
371 /* Called once a second.
372 */
373 static void core_secondly()
374 {
375 static int cnt = 0;
376 int miltime;
377
378 call_hook(HOOK_SECONDLY); /* Will be removed later */
379 cnt++;
380 if (cnt >= 10) { /* Every 10 seconds */
381 cnt = 0;
382 check_expired_dcc();
383 if (con_chan && !backgrd) {
384 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
385 //tell_verbose_status(DP_STDOUT);
386 do_module_report(DP_STDOUT, 0, "server");
387 do_module_report(DP_STDOUT, 0, "channels");
388 }
389 }
390 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
391 if (nowtm.tm_min != lastmin) {
392 int i = 0;
393
394 /* Once a minute */
395 lastmin = (lastmin + 1) % 60;
396 call_hook(HOOK_MINUTELY);
397 check_expired_ignores();
398 /* In case for some reason more than 1 min has passed: */
399 while (nowtm.tm_min != lastmin) {
400 /* Timer drift, dammit */
401 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
402 i++;
403 lastmin = (lastmin + 1) % 60;
404 call_hook(HOOK_MINUTELY);
405 }
406 if (i > 1)
407 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
408 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
409 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
410 call_hook(HOOK_5MINUTELY);
411 if (!miltime) { /* At midnight */
412 char s[25];
413
414 strlcpy(s, ctime(&now), sizeof s);
415 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
416 call_hook(HOOK_BACKUP);
417 call_hook(HOOK_DAILY);
418 }
419 }
420 if (nowtm.tm_min == notify_users_at)
421 call_hook(HOOK_HOURLY);
422 }
423 }
424
425 static void core_minutely()
426 {
427 check_bind_time(&nowtm);
428 }
429
430 static void core_hourly()
431 {
432 write_userfile(-1);
433 }
434
435 static void event_rehash()
436 {
437 check_bind_event("rehash");
438 }
439
440 static void event_prerehash()
441 {
442 check_bind_event("prerehash");
443 }
444
445 static void event_save()
446 {
447 check_bind_event("save");
448 }
449
450 static void event_logfile()
451 {
452 check_bind_event("logfile");
453 }
454
455 static void event_loaded()
456 {
457 check_bind_event("loaded");
458 }
459
460 void kill_tcl();
461 extern module_entry *module_list;
462
463 int init_userent(), init_net(),
464 init_tcl(int, char **);
465 void dns_init();
466 void binds_init();
467 void dcc_init();
468
469 void patch(const char *str)
470 {
471 char *p = strchr(egg_version, '+');
472
473 if (!p)
474 p = &egg_version[strlen(egg_version)];
475 sprintf(p, "+%s", str);
476 egg_numver++;
477 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
478 }
479
480 /*
481 static inline void garbage_collect(void)
482 {
483 static u_8bit_t run_cnt = 0;
484
485 if (run_cnt == 3)
486 garbage_collect_tclhash();
487 else
488 run_cnt++;
489 }
490 */
491
492 int main(int argc, char **argv)
493 {
494 int xx, i;
495 char buf[520], s[25];
496 FILE *f;
497 struct sigaction sv;
498 struct chanset_t *chan;
499 egg_timeval_t howlong;
500
501 #ifdef DEBUG
502 /* Make sure it can write core, if you make debug. Else it's pretty
503 * useless (dw)
504 */
505 {
506 #include <sys/resource.h>
507 struct rlimit cdlim;
508
509 cdlim.rlim_cur = RLIM_INFINITY;
510 cdlim.rlim_max = RLIM_INFINITY;
511 setrlimit(RLIMIT_CORE, &cdlim);
512 }
513 #endif
514
515 #ifdef ENABLE_NLS
516 setlocale(LC_MESSAGES, "");
517 bindtextdomain(PACKAGE, LOCALEDIR);
518 textdomain(PACKAGE);
519 #endif
520
521 #include "patch.h"
522 /* Version info! */
523 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
524 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2003 Eggheads",
525 egg_version);
526 /* Now add on the patchlevel (for Tcl) */
527 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
528 strcat(egg_version, egg_xtra);
529 #ifdef STOP_UAC
530 {
531 int nvpair[2];
532
533 nvpair[0] = SSIN_UACPROC;
534 nvpair[1] = UAC_NOPRINT;
535 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
536 }
537 #endif
538
539 /* Set up error traps: */
540 sv.sa_handler = got_bus;
541 sigemptyset(&sv.sa_mask);
542 #ifdef SA_RESETHAND
543 sv.sa_flags = SA_RESETHAND;
544 #else
545 sv.sa_flags = 0;
546 #endif
547 sigaction(SIGBUS, &sv, NULL);
548 sv.sa_handler = got_segv;
549 sigaction(SIGSEGV, &sv, NULL);
550 #ifdef SA_RESETHAND
551 sv.sa_flags = 0;
552 #endif
553 sv.sa_handler = got_fpe;
554 sigaction(SIGFPE, &sv, NULL);
555 sv.sa_handler = got_term;
556 sigaction(SIGTERM, &sv, NULL);
557 sv.sa_handler = got_hup;
558 sigaction(SIGHUP, &sv, NULL);
559 sv.sa_handler = got_quit;
560 sigaction(SIGQUIT, &sv, NULL);
561 sv.sa_handler = SIG_IGN;
562 sigaction(SIGPIPE, &sv, NULL);
563 sv.sa_handler = got_ill;
564 sigaction(SIGILL, &sv, NULL);
565 sv.sa_handler = got_alarm;
566 sigaction(SIGALRM, &sv, NULL);
567
568 /* Initialize variables and stuff */
569 timer_update_now(&egg_timeval_now);
570 now = egg_timeval_now.sec;
571 chanset = NULL;
572 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
573 lastmin = nowtm.tm_min;
574 srandom(now % (getpid() + getppid()));
575
576 do_args(argc, argv);
577
578 printf("\n%s\n", version);
579 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
580
581 /* Don't allow eggdrop to run as root */
582 if (((int) getuid() == 0) || ((int) geteuid() == 0))
583 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
584
585 egg = eggdrop_new();
586 script_init();
587 binds_init();
588 user_init();
589 user_load("users.xml");
590 partyline_init();
591 telnet_init();
592 modules_init();
593 logfile_init();
594 dns_init();
595 egg_dns_init();
596 core_binds_init();
597 dcc_init();
598 old_user_init();
599 init_userent();
600 init_net();
601 traffic_init();
602
603 if (backgrd)
604 bg_prepare_split();
605
606 init_tcl(argc, argv);
607
608 strlcpy(s, ctime(&now), sizeof s);
609 strcpy(&s[11], &s[20]);
610 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
611 if (preload_module[0])
612 module_load(preload_module);
613 chanprog();
614 if (!encrypt_pass) {
615 printf(_("You have installed modules but have not selected an encryption\n\
616 module, please consult the default config file for info.\n"));
617 bg_send_quit(BG_ABORT);
618 exit(1);
619 }
620 i = 0;
621 for (chan = chanset; chan; chan = chan->next)
622 i++;
623 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
624 botnetnick, i, count_users(userlist));
625 cache_miss = 0;
626 cache_hit = 0;
627
628 if (!pid_file[0])
629 snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
630
631 /* Check for pre-existing eggdrop! */
632 f = fopen(pid_file, "r");
633 if (f != NULL) {
634 fgets(s, 10, f);
635 xx = atoi(s);
636 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
637 is used */
638 if (errno != ESRCH) {
639 printf(_("I detect %s already running from this directory.\n"), botnetnick);
640 printf(_("If this is incorrect, erase the %s\n"), pid_file);
641 bg_send_quit(BG_ABORT);
642 exit(1);
643 }
644 }
645
646 /* Move into background? */
647 if (backgrd) {
648 #ifndef CYGWIN_HACKS
649 bg_do_split();
650 } else { /* !backgrd */
651 #endif
652 xx = getpid();
653 if (xx != 0) {
654 FILE *fp;
655
656 /* Write pid to file */
657 unlink(pid_file);
658 fp = fopen(pid_file, "w");
659 if (fp != NULL) {
660 fprintf(fp, "%u\n", xx);
661 if (fflush(fp)) {
662 /* Let the bot live since this doesn't appear to be a botchk */
663 printf(_("* Warning! Could not write %s file!\n"), pid_file);
664 fclose(fp);
665 unlink(pid_file);
666 } else
667 fclose(fp);
668 } else
669 printf(_("* Warning! Could not write %s file!\n"), pid_file);
670 #ifdef CYGWIN_HACKS
671 printf("Launched into the background (pid: %d)\n\n", xx);
672 #endif
673 }
674 }
675
676 use_stderr = 0; /* Stop writing to stderr now */
677 if (backgrd) {
678 /* Ok, try to disassociate from controlling terminal (finger cross) */
679 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
680 setpgid(0, 0);
681 #endif
682 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
683 freopen("/dev/null", "r", stdin);
684 freopen("/dev/null", "w", stdout);
685 freopen("/dev/null", "w", stderr);
686 #ifdef CYGWIN_HACKS
687 FreeConsole();
688 #endif
689 }
690
691 /* Terminal emulating dcc chat */
692 if (!backgrd && term_z) {
693 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
694
695 dcc[n].addr[0] = '\0';
696 dcc[n].sock = STDOUT;
697 dcc[n].timeval = now;
698 dcc[n].u.chat->con_flags = conmask;
699 dcc[n].status = STAT_ECHO;
700 strcpy(dcc[n].nick, "HQ");
701 strcpy(dcc[n].host, "llama@console");
702 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
703 /* Make sure there's an innocuous HQ user if needed */
704 if (!dcc[n].user) {
705 userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
706 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
707 }
708 setsock(STDOUT, 0); /* Entry in net table */
709 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
710 dcc_chatter(n);
711 }
712
713 online_since = now;
714 add_help_reference("cmds1.help");
715 add_help_reference("cmds2.help");
716 add_help_reference("core.help");
717 howlong.sec = 1;
718 howlong.usec = 0;
719 timer_create_repeater(&howlong, (Function) core_secondly);
720
721 /* init time'd hooks */
722 add_hook(HOOK_MINUTELY, (Function) core_minutely);
723 add_hook(HOOK_HOURLY, (Function) core_hourly);
724 add_hook(HOOK_REHASH, (Function) event_rehash);
725 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
726 add_hook(HOOK_USERFILE, (Function) event_save);
727 add_hook(HOOK_BACKUP, (Function) backup_userfile);
728 add_hook(HOOK_DAILY, (Function) event_logfile);
729 add_hook(HOOK_LOADED, (Function) event_loaded);
730
731 call_hook(HOOK_LOADED);
732
733 debug0("main: entering loop");
734 while (1) {
735 int socket_cleanup = 0;
736
737 #if !defined(HAVE_PRE7_5_TCL)
738 /* Process a single tcl event */
739 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
740 #endif
741
742 /* Lets move some of this here, reducing the numer of actual
743 * calls to periodic_timers
744 */
745 timer_get_time(&egg_timeval_now);
746 now = egg_timeval_now.sec;
747 random(); /* Woop, lets really jumble things */
748 timer_run();
749
750 /* Only do this every so often. */
751 if (!socket_cleanup) {
752 socket_cleanup = 5;
753
754 /* Remove dead dcc entries. */
755 dcc_remove_lost();
756
757 /* Check for server or dcc activity. */
758 dequeue_sockets();
759 } else
760 socket_cleanup--;
761
762 /* Free unused structures. */
763 /* garbage_collect(); */
764
765 sockbuf_update_all(0);
766 xx = sockgets(buf, &i);
767 timer_update_now(&egg_timeval_now);
768 if (xx >= 0) { /* Non-error */
769 int idx;
770
771 for (idx = 0; idx < dcc_total; idx++)
772 if (dcc[idx].sock == xx) {
773 if (dcc[idx].type && dcc[idx].type->activity) {
774
775 /* update traffic stats */
776 traffic_update_in(dcc[idx].type, (strlen(buf) + 1));
777
778 dcc[idx].type->activity(idx, buf, i);
779 } else
780 putlog(LOG_MISC, "*",
781 "!!! untrapped dcc activity: type %s, sock %d",
782 dcc[idx].type->name, dcc[idx].sock);
783 break;
784 }
785 } else if (xx == -1) { /* EOF from someone */
786 int idx;
787
788 if (i == STDOUT && !backgrd)
789 fatal("END OF FILE ON TERMINAL", 0);
790 for (idx = 0; idx < dcc_total; idx++)
791 if (dcc[idx].sock == i) {
792 if (dcc[idx].type && dcc[idx].type->eof)
793 dcc[idx].type->eof(idx);
794 else {
795 putlog(LOG_MISC, "*",
796 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
797 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
798 killsock(i);
799 lostdcc(idx);
800 }
801 idx = dcc_total + 1;
802 }
803 if (idx == dcc_total) {
804 putlog(LOG_MISC, "*",
805 "(@) EOF socket %d, not a dcc socket, not anything.", i);
806 close(i);
807 killsock(i);
808 }
809 } else if (xx == -2 && errno != EINTR) { /* select() error */
810 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
811 for (i = 0; i < dcc_total; i++) {
812 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno == EBADF)) {
813 putlog(LOG_MISC, "*",
814 "DCC socket %d (type %d, name '%s') expired -- pfft",
815 dcc[i].sock, dcc[i].type, dcc[i].nick);
816 killsock(dcc[i].sock);
817 lostdcc(i);
818 i--;
819 }
820 }
821 } else if (xx == -3) {
822 call_hook(HOOK_IDLE);
823 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
824 }
825
826 if (do_restart) {
827 if (do_restart == -2)
828 rehash();
829 else {
830 /* Unload as many modules as possible */
831 int f = 1;
832 module_entry *p;
833 Function x;
834 char xx[256];
835
836 /* oops, I guess we should call this event before tcl is restarted */
837 check_bind_event("prerestart");
838
839 while (f) {
840 f = 0;
841 for (p = module_list; p != NULL; p = p->next) {
842 dependancy *d = dependancy_list;
843 int ok = 1;
844
845 while (ok && d) {
846 if (d->needed == p)
847 ok = 0;
848 d = d->next;
849 }
850 if (ok) {
851 strcpy(xx, p->name);
852 if (module_unload(xx, botnetnick) == NULL) {
853 f = 1;
854 break;
855 }
856 }
857 }
858 }
859 p = module_list;
860 if (p && p->next && p->next->next)
861 /* Should be only 2 modules now - blowfish (or some other
862 encryption module) and eggdrop. */
863 putlog(LOG_MISC, "*", _("Stagnant module; there WILL be memory leaks!"));
864 flushlogs();
865 kill_tcl();
866 timer_destroy_all(); /* Destroy all timers. */
867 init_tcl(argc, argv);
868 /* We expect the encryption module as the current module pointed
869 * to by `module_list'.
870 */
871 x = p->funcs[MODCALL_START];
872 /* `NULL' indicates that we just recently restarted. The module
873 * is expected to re-initialise as needed.
874 */
875 x(NULL);
876 rehash();
877 call_hook(HOOK_LOADED);
878 }
879 do_restart = 0;
880 }
881 }
882 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23