/[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.119 - (show annotations) (download) (as text)
Sat Jun 1 13:27:46 2002 UTC (17 years, 4 months ago) by wingman
Branch: MAIN
Changes since 1.118: +9 -44 lines
File MIME type: text/x-chdr
	* moved traffic commands to traffic.[c|h] and cleaned
	  up net.c/main.c

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23