/[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.108 - (show annotations) (download) (as text)
Mon Feb 25 02:59:50 2002 UTC (17 years, 6 months ago) by stdarg
Branch: MAIN
Changes since 1.107: +7 -9 lines
File MIME type: text/x-chdr
* Added 'timers' and 'timer_info' commands
* Removed some unnecessary usage of time(NULL) in logfile.c
* New global variable egg_timeval_now (like 'now' but with microseconds)
* Fixed some bugs, cleaned up some code, in egg_timer.c

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23