/[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.123 - (show annotations) (download) (as text)
Sat Sep 21 07:40:16 2002 UTC (16 years, 10 months ago) by stdarg
Branch: MAIN
Changes since 1.122: +3 -1 lines
File MIME type: text/x-chdr
* Added script support for network stuff

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23