/[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.118 - (show annotations) (download) (as text)
Sun May 12 15:35:44 2002 UTC (17 years, 3 months ago) by ite
Branch: MAIN
Changes since 1.117: +1 -2 lines
File MIME type: text/x-chdr
* Now always preforking
* More minor configure macros cleanup
* Let's try to detect JS_NewObject() instead of JS_Now() for javascript library detection.
* Js headers to be corectly detected when --with-jsinc isn't passed to ./configure

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23