/[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.125 - (show annotations) (download) (as text)
Thu Oct 10 04:41:59 2002 UTC (16 years, 10 months ago) by stdarg
Branch: MAIN
Changes since 1.124: +2 -2 lines
File MIME type: text/x-chdr
* Moved bind tables to libeggdrop

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23