/[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.115 - (show annotations) (download) (as text)
Fri May 3 07:57:12 2002 UTC (17 years, 3 months ago) by stdarg
Branch: MAIN
Changes since 1.114: +26 -45 lines
File MIME type: text/x-chdr
* Made itraffic_* otraffic_* into a single struct.
* mod-path -> mod_path

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23