/[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.128 - (show annotations) (download) (as text)
Thu Jan 2 21:33:16 2003 UTC (16 years, 9 months ago) by wcc
Branch: MAIN
Changes since 1.127: +3 -3 lines
File MIME type: text/x-chdr
* Updated copyright info for 2003.

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.127 2002/12/12 04:28:22 wcc 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] = "help/"; /* Directory of help files (if used) */
119 char textdir[121] = "text/"; /* 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 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) 2003 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 binds_init();
579 user_init();
580 user_load("users.xml");
581 partyline_init();
582 telnet_init();
583 modules_init();
584 logfile_init();
585 dns_init();
586 egg_dns_init();
587 core_binds_init();
588 dcc_init();
589 old_user_init();
590 init_userent();
591 init_net();
592 traffic_init();
593
594 if (backgrd)
595 bg_prepare_split();
596
597 init_tcl(argc, argv);
598
599 strlcpy(s, ctime(&now), sizeof s);
600 strcpy(&s[11], &s[20]);
601 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
602 if (preload_module[0])
603 module_load(preload_module);
604 chanprog();
605 if (!encrypt_pass) {
606 printf(_("You have installed modules but have not selected an encryption\n\
607 module, please consult the default config file for info.\n"));
608 bg_send_quit(BG_ABORT);
609 exit(1);
610 }
611 i = 0;
612 for (chan = chanset; chan; chan = chan->next)
613 i++;
614 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
615 botnetnick, i, count_users(userlist));
616 cache_miss = 0;
617 cache_hit = 0;
618
619 if (!pid_file[0])
620 snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
621
622 /* Check for pre-existing eggdrop! */
623 f = fopen(pid_file, "r");
624 if (f != NULL) {
625 fgets(s, 10, f);
626 xx = atoi(s);
627 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
628 is used */
629 if (errno != ESRCH) {
630 printf(_("I detect %s already running from this directory.\n"), botnetnick);
631 printf(_("If this is incorrect, erase the %s\n"), pid_file);
632 bg_send_quit(BG_ABORT);
633 exit(1);
634 }
635 }
636
637 /* Move into background? */
638 if (backgrd) {
639 #ifndef CYGWIN_HACKS
640 bg_do_split();
641 } else { /* !backgrd */
642 #endif
643 xx = getpid();
644 if (xx != 0) {
645 FILE *fp;
646
647 /* Write pid to file */
648 unlink(pid_file);
649 fp = fopen(pid_file, "w");
650 if (fp != NULL) {
651 fprintf(fp, "%u\n", xx);
652 if (fflush(fp)) {
653 /* Let the bot live since this doesn't appear to be a botchk */
654 printf(_("* Warning! Could not write %s file!\n"), pid_file);
655 fclose(fp);
656 unlink(pid_file);
657 } else
658 fclose(fp);
659 } else
660 printf(_("* Warning! Could not write %s file!\n"), pid_file);
661 #ifdef CYGWIN_HACKS
662 printf("Launched into the background (pid: %d)\n\n", xx);
663 #endif
664 }
665 }
666
667 use_stderr = 0; /* Stop writing to stderr now */
668 if (backgrd) {
669 /* Ok, try to disassociate from controlling terminal (finger cross) */
670 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
671 setpgid(0, 0);
672 #endif
673 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
674 freopen("/dev/null", "r", stdin);
675 freopen("/dev/null", "w", stdout);
676 freopen("/dev/null", "w", stderr);
677 #ifdef CYGWIN_HACKS
678 FreeConsole();
679 #endif
680 }
681
682 /* Terminal emulating dcc chat */
683 if (!backgrd && term_z) {
684 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
685
686 dcc[n].addr[0] = '\0';
687 dcc[n].sock = STDOUT;
688 dcc[n].timeval = now;
689 dcc[n].u.chat->con_flags = conmask;
690 dcc[n].status = STAT_ECHO;
691 strcpy(dcc[n].nick, "HQ");
692 strcpy(dcc[n].host, "llama@console");
693 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
694 /* Make sure there's an innocuous HQ user if needed */
695 if (!dcc[n].user) {
696 userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
697 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
698 }
699 setsock(STDOUT, 0); /* Entry in net table */
700 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
701 dcc_chatter(n);
702 }
703
704 online_since = now;
705 add_help_reference("cmds1.help");
706 add_help_reference("cmds2.help");
707 add_help_reference("core.help");
708 howlong.sec = 1;
709 howlong.usec = 0;
710 timer_create_repeater(&howlong, (Function) core_secondly);
711
712 /* init time'd hooks */
713 add_hook(HOOK_MINUTELY, (Function) core_minutely);
714 add_hook(HOOK_HOURLY, (Function) core_hourly);
715 add_hook(HOOK_REHASH, (Function) event_rehash);
716 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
717 add_hook(HOOK_USERFILE, (Function) event_save);
718 add_hook(HOOK_BACKUP, (Function) backup_userfile);
719 add_hook(HOOK_DAILY, (Function) event_logfile);
720 add_hook(HOOK_LOADED, (Function) event_loaded);
721
722 call_hook(HOOK_LOADED);
723
724 debug0("main: entering loop");
725 while (1) {
726 int socket_cleanup = 0;
727
728 #if !defined(HAVE_PRE7_5_TCL)
729 /* Process a single tcl event */
730 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
731 #endif
732
733 /* Lets move some of this here, reducing the numer of actual
734 * calls to periodic_timers
735 */
736 timer_get_time(&egg_timeval_now);
737 now = egg_timeval_now.sec;
738 random(); /* Woop, lets really jumble things */
739 timer_run();
740
741 /* Only do this every so often. */
742 if (!socket_cleanup) {
743 socket_cleanup = 5;
744
745 /* Remove dead dcc entries. */
746 dcc_remove_lost();
747
748 /* Check for server or dcc activity. */
749 dequeue_sockets();
750 } else
751 socket_cleanup--;
752
753 /* Free unused structures. */
754 /* garbage_collect(); */
755
756 sockbuf_update_all(0);
757 xx = sockgets(buf, &i);
758 timer_update_now(&egg_timeval_now);
759 if (xx >= 0) { /* Non-error */
760 int idx;
761
762 for (idx = 0; idx < dcc_total; idx++)
763 if (dcc[idx].sock == xx) {
764 if (dcc[idx].type && dcc[idx].type->activity) {
765
766 /* update traffic stats */
767 traffic_update_in(dcc[idx].type, (strlen(buf) + 1));
768
769 dcc[idx].type->activity(idx, buf, i);
770 } else
771 putlog(LOG_MISC, "*",
772 "!!! untrapped dcc activity: type %s, sock %d",
773 dcc[idx].type->name, dcc[idx].sock);
774 break;
775 }
776 } else if (xx == -1) { /* EOF from someone */
777 int idx;
778
779 if (i == STDOUT && !backgrd)
780 fatal("END OF FILE ON TERMINAL", 0);
781 for (idx = 0; idx < dcc_total; idx++)
782 if (dcc[idx].sock == i) {
783 if (dcc[idx].type && dcc[idx].type->eof)
784 dcc[idx].type->eof(idx);
785 else {
786 putlog(LOG_MISC, "*",
787 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
788 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
789 killsock(i);
790 lostdcc(idx);
791 }
792 idx = dcc_total + 1;
793 }
794 if (idx == dcc_total) {
795 putlog(LOG_MISC, "*",
796 "(@) EOF socket %d, not a dcc socket, not anything.", i);
797 close(i);
798 killsock(i);
799 }
800 } else if (xx == -2 && errno != EINTR) { /* select() error */
801 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
802 for (i = 0; i < dcc_total; i++) {
803 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
804 putlog(LOG_MISC, "*",
805 "DCC socket %d (type %d, name '%s') expired -- pfft",
806 dcc[i].sock, dcc[i].type, dcc[i].nick);
807 killsock(dcc[i].sock);
808 lostdcc(i);
809 i--;
810 }
811 }
812 } else if (xx == -3) {
813 call_hook(HOOK_IDLE);
814 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
815 }
816
817 if (do_restart) {
818 if (do_restart == -2)
819 rehash();
820 else {
821 /* Unload as many modules as possible */
822 int f = 1;
823 module_entry *p;
824 Function x;
825 char xx[256];
826
827 /* oops, I guess we should call this event before tcl is restarted */
828 check_bind_event("prerestart");
829
830 while (f) {
831 f = 0;
832 for (p = module_list; p != NULL; p = p->next) {
833 dependancy *d = dependancy_list;
834 int ok = 1;
835
836 while (ok && d) {
837 if (d->needed == p)
838 ok = 0;
839 d = d->next;
840 }
841 if (ok) {
842 strcpy(xx, p->name);
843 if (module_unload(xx, botnetnick) == NULL) {
844 f = 1;
845 break;
846 }
847 }
848 }
849 }
850 p = module_list;
851 if (p && p->next && p->next->next)
852 /* Should be only 2 modules now - blowfish (or some other
853 encryption module) and eggdrop. */
854 putlog(LOG_MISC, "*", _("Stagnant module; there WILL be memory leaks!"));
855 flushlogs();
856 kill_tcl();
857 timer_destroy_all(); /* Destroy all timers. */
858 init_tcl(argc, argv);
859 /* We expect the encryption module as the current module pointed
860 * to by `module_list'.
861 */
862 x = p->funcs[MODCALL_START];
863 /* `NULL' indicates that we just recently restarted. The module
864 * is expected to re-initialise as needed.
865 */
866 x(NULL);
867 rehash();
868 call_hook(HOOK_LOADED);
869 }
870 do_restart = 0;
871 }
872 }
873 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23