/[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.9 - (show annotations) (download) (as text)
Mon Nov 15 17:42:33 1999 UTC (19 years, 9 months ago) by fabian
Branch: MAIN
Changes since 1.8: +5 -6 lines
File MIME type: text/x-chdr
Toth's sync-1.5-14Nov1999-1

1 /*
2 * main.c -- handles:
3 * changing nicknames when the desired nick is in use
4 * flood detection
5 * signal handling
6 * command line arguments
7 *
8 * dprintf'ized, 15nov1995
9 */
10 /*
11 * This file is part of the eggdrop source code
12 * copyright (c) 1997 Robey Pointer
13 * and is distributed according to the GNU general public license.
14 *
15 * Parts of this eggdrop source code are copyright (c)1999 Eggheads
16 * and is distributed according to the GNU general public license.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 *
32 * The author (Robey Pointer) can be reached at: robey@netcom.com
33 * NOTE: Robey is no long working on this code, there is a discussion
34 * list avaliable at eggheads@eggheads.org.
35 */
36
37 #include "main.h"
38 #include <time.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <signal.h>
42 #include <netdb.h>
43 #include <setjmp.h>
44 #ifdef STOP_UAC /* osf/1 complains a lot */
45 #include <sys/sysinfo.h>
46 #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
47 #endif
48 /* some systems have a working sys/wait.h even though configure will
49 * decide it's not bsd compatable. oh well. */
50 #include "chan.h"
51 #include "modules.h"
52 #include "tandem.h"
53 #ifdef CYGWIN_HACKS
54 #include <windows.h>
55 BOOL FreeConsole(VOID);
56 #endif
57
58 #ifndef _POSIX_SOURCE
59 /* solaris needs this */
60 #define _POSIX_SOURCE 1
61 #endif
62
63 extern char origbotname[];
64 extern int dcc_total;
65 extern struct dcc_t *dcc;
66 extern int conmask;
67 extern struct userrec *userlist;
68 extern int cache_hit, cache_miss;
69 extern char userfile[];
70 extern struct chanset_t *chanset;
71 extern char botnetnick[];
72 extern log_t *logs;
73 extern Tcl_Interp *interp;
74 extern int max_logs;
75 extern tcl_timer_t *timer, *utimer;
76 extern jmp_buf alarmret;
77 extern int quick_logs; /* dw */
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] = "1.5.0";
87 int egg_numver = 1050000;
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 int keep_all_logs = 0; /* never erase logfiles, no matter how old
103 * they are? */
104 time_t online_since; /* unix-time that the bot loaded up */
105 int make_userfile = 0; /* using bot in make-userfile mode? (first
106 * user to 'hello' becomes master) */
107 char owner[121] = ""; /* permanent owner(s) of the bot */
108 char pid_file[40]; /* name of the file for the pid to be
109 * stored in */
110 int save_users_at = 0; /* how many minutes past the hour to
111 * save the userfile? */
112 int notify_users_at = 0; /* how many minutes past the hour to
113 * notify users of notes? */
114 int switch_logfiles_at = 300; /* when (military time) to switch logfiles */
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 = 0; /* die if bot receives SIGTERM */
122 int resolve_timeout = 15; /* hostname/address lookup timeout */
123 time_t now; /* duh, now :) */
124
125 /* context storage for fatal crashes */
126 char cx_file[16][30];
127 char cx_note[16][256];
128 int cx_line[16];
129 int cx_ptr = 0;
130
131 void fatal(char *s, int recoverable)
132 {
133 int i;
134
135 putlog(LOG_MISC, "*", "* %s", s);
136 flushlogs();
137 for (i = 0; i < dcc_total; i++)
138 killsock(dcc[i].sock);
139 unlink(pid_file);
140 if (!recoverable)
141 exit(1);
142 }
143
144 int expmem_chanprog(), expmem_users(), expmem_misc(), expmem_dccutil(),
145 expmem_botnet(), expmem_tcl(), expmem_tclhash(), expmem_net(),
146 expmem_modules(int), expmem_language(), expmem_tcldcc();
147
148 /* for mem.c : calculate memory we SHOULD be using */
149 int expected_memory()
150 {
151 int tot;
152
153 context;
154 tot = expmem_chanprog() + expmem_users() + expmem_misc() +
155 expmem_dccutil() + expmem_botnet() + expmem_tcl() + expmem_tclhash() +
156 expmem_net() + expmem_modules(0) + expmem_language() + expmem_tcldcc();
157 return tot;
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 int nested_debug = 0;
179
180 void write_debug()
181 {
182 int x;
183 char s[80];
184 int y;
185
186 if (nested_debug) {
187 /* yoicks, if we have this there's serious trouble */
188 /* all of these are pretty reliable, so we'll try these */
189 /* dont try and display context-notes in here, it's _not_ safe <cybah> */
190 x = creat("DEBUG.DEBUG", 0644);
191 setsock(x, SOCK_NONSOCK);
192 if (x >= 0) {
193 strcpy(s, ctime(&now));
194 dprintf(-x, "Debug (%s) written %s", ver, s);
195 dprintf(-x, "Please report problem to eggheads@eggheads.org");
196 dprintf(-x, "after a visit to http://www.eggheads.org/bugs.html");
197 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
198 dprintf(-x, "Context: ");
199 cx_ptr = cx_ptr & 15;
200 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
201 dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
202 dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
203 killsock(x);
204 close(x);
205 }
206 exit(1); /* dont even try & tell people about, that may
207 * have caused the fault last time */
208 } else
209 nested_debug = 1;
210 putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
211 cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
212 putlog(LOG_MISC, "*", "* Please REPORT this BUG!");
213 putlog(LOG_MISC, "*", "* Check doc/BUG-REPORT on how to do so.");
214 x = creat("DEBUG", 0644);
215 setsock(x, SOCK_NONSOCK);
216 if (x < 0) {
217 putlog(LOG_MISC, "*", "* Failed to write DEBUG");
218 } else {
219 strcpy(s, ctime(&now));
220 dprintf(-x, "Debug (%s) written %s", ver, s);
221 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
222 #ifdef STATIC
223 dprintf(-x, "STATICALLY LINKED\n");
224 #endif
225 strcpy(s, "info library");
226 if (interp && (Tcl_Eval(interp, s) == TCL_OK))
227 dprintf(-x, "Using tcl library: %s (header version %s)\n",
228 interp->result, TCL_VERSION);
229 dprintf(-x, "Compile flags: %s\n", CCFLAGS);
230 dprintf(-x, "Link flags : %s\n", LDFLAGS);
231 dprintf(-x, "Strip flags : %s\n", STRIPFLAGS);
232 dprintf(-x, "Context: ");
233 cx_ptr = cx_ptr & 15;
234 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
235 dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
236 (cx_note[y][0]) ? cx_note[y] : "");
237 dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
238 (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
239 tell_dcc(-x);
240 dprintf(-x, "\n");
241 debug_mem_to_dcc(-x);
242 killsock(x);
243 close(x);
244 putlog(LOG_MISC, "*", "* Wrote DEBUG");
245 }
246 }
247
248 void assert_failed (const char *module, const char *file, const int line)
249 {
250 write_debug();
251 if (!module) {
252 putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
253 } else {
254 putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
255 }
256 fatal ("ASSERT FAILED -- CRASHING!", 1);
257 exit (1);
258 }
259
260 static void got_bus(int z)
261 {
262 write_debug();
263 fatal("BUS ERROR -- CRASHING!", 1);
264 #ifdef SA_RESETHAND
265 kill(getpid(), SIGBUS);
266 #else
267 exit(1);
268 #endif
269 }
270
271 static void got_segv(int z)
272 {
273 write_debug();
274 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
275 #ifdef SA_RESETHAND
276 kill(getpid(), SIGSEGV);
277 #else
278 exit(1);
279 #endif
280 }
281
282 static void got_fpe(int z)
283 {
284 write_debug();
285 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
286 }
287
288 static void got_term(int z)
289 {
290 write_userfile(-1);
291 check_tcl_event("sigterm");
292 if (die_on_sigterm) {
293 botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
294 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
295 } else {
296 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
297 }
298 }
299
300 static void got_quit(int z)
301 {
302 check_tcl_event("sigquit");
303 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
304 return;
305 }
306
307 static void got_hup(int z)
308 {
309 write_userfile(-1);
310 check_tcl_event("sighup");
311 if (die_on_sighup) {
312 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
313 } else
314 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
315 do_restart = -2;
316 return;
317 }
318
319 static void got_alarm(int z)
320 {
321 /* a call to resolver (gethostbyname, etc) timed out */
322 longjmp(alarmret, 1);
323 /* STUPID STUPID STUPID */
324 /* return; */
325 }
326
327 /* got ILL signal -- log context and continue */
328 static void got_ill(int z)
329 {
330 check_tcl_event("sigill");
331 write_debug();
332 putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
333 cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
334 fatal ("GOT ILL SIGNAL -- CRASHING!", 1);
335 #ifdef SA_RESETHAND
336 kill(getpid(), SIGILL);
337 #else
338 exit(1);
339 #endif
340 }
341
342 static void do_arg(char *s)
343 {
344 int i;
345
346 if (s[0] == '-')
347 for (i = 1; i < strlen(s); i++) {
348 if (s[i] == 'n')
349 backgrd = 0;
350 if (s[i] == 'c') {
351 con_chan = 1;
352 term_z = 0;
353 }
354 if (s[i] == 't') {
355 con_chan = 0;
356 term_z = 1;
357 }
358 if (s[i] == 'm')
359 make_userfile = 1;
360 if (s[i] == 'v') {
361 char x[256], *z = x;
362
363 strcpy(x, egg_version);
364 newsplit(&z);
365 newsplit(&z);
366 printf("%s\n", version);
367 if (z[0])
368 printf(" (patches: %s)\n", z);
369 exit(0);
370 }
371 if (s[i] == 'h') {
372 printf("\n%s\n\n", version);
373 printf(EGG_USAGE);
374 printf("\n");
375 exit(0);
376 }
377 } else
378 strcpy(configfile, s);
379 }
380
381 void backup_userfile()
382 {
383 char s[150];
384
385 putlog(LOG_MISC, "*", USERF_BACKUP);
386 strcpy(s, userfile);
387 strcat(s, "~bak");
388 copyfile(userfile, s);
389 }
390
391 /* timer info: */
392 static int lastmin = 99;
393 static time_t then;
394 static struct tm *nowtm;
395
396 /* rally BB, this is not QUITE as bad as it seems <G> */
397 /* ONCE A SECOND */
398 static void core_secondly()
399 {
400 static int cnt = 0;
401 int miltime;
402
403 do_check_timers(&utimer); /* secondly timers */
404 cnt++;
405 if (cnt >= 10) { /* every 10 seconds */
406 cnt = 0;
407 check_expired_dcc();
408 if (con_chan && !backgrd) {
409 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
410 tell_verbose_status(DP_STDOUT);
411 do_module_report(DP_STDOUT, 0, "server");
412 do_module_report(DP_STDOUT, 0, "channels");
413 tell_mem_status_dcc(DP_STDOUT);
414 }
415 }
416 context;
417 nowtm = localtime(&now);
418 if (nowtm->tm_min != lastmin) {
419 int i = 0;
420
421 /* once a minute */
422 lastmin = (lastmin + 1) % 60;
423 call_hook(HOOK_MINUTELY);
424 check_expired_ignores();
425 autolink_cycle(NULL); /* attempt autolinks */
426 /* in case for some reason more than 1 min has passed: */
427 while (nowtm->tm_min != lastmin) {
428 /* timer drift, dammit */
429 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm->tm_min);
430 context;
431 i++;
432 lastmin = (lastmin + 1) % 60;
433 call_hook(HOOK_MINUTELY);
434 }
435 if (i > 1)
436 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
437 miltime = (nowtm->tm_hour * 100) + (nowtm->tm_min);
438 context;
439 if (((int) (nowtm->tm_min / 5) * 5) == (nowtm->tm_min)) { /* 5 min */
440 call_hook(HOOK_5MINUTELY);
441 check_botnet_pings();
442 context;
443 if (quick_logs == 0) {
444 flushlogs();
445 check_logsize();
446 }
447 if (miltime == 0) { /* at midnight */
448 char s[128];
449 int j;
450
451 s[my_strcpy(s, ctime(&now)) - 1] = 0;
452 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
453 backup_userfile();
454 for (j = 0; j < max_logs; j++) {
455 if (logs[j].filename != NULL && logs[j].f != NULL) {
456 fclose(logs[j].f);
457 logs[j].f = NULL;
458 }
459 }
460 }
461 }
462 context;
463 if (nowtm->tm_min == notify_users_at)
464 call_hook(HOOK_HOURLY);
465 context; /* these no longer need checking since they are
466 * all check vs minutely settings and we only
467 * get this far on the minute */
468 if (miltime == switch_logfiles_at) {
469 call_hook(HOOK_DAILY);
470 if (!keep_all_logs) {
471 putlog(LOG_MISC, "*", MISC_LOGSWITCH);
472 for (i = 0; i < max_logs; i++)
473 if (logs[i].filename) {
474 char s[1024];
475
476 if (logs[i].f) {
477 fclose(logs[i].f);
478 logs[i].f = NULL;
479 }
480 simple_sprintf(s, "%s.yesterday", logs[i].filename);
481 unlink(s);
482 movefile(logs[i].filename, s);
483 }
484 }
485 }
486 }
487 }
488
489 static void core_minutely()
490 {
491 context;
492 check_tcl_time(nowtm);
493 do_check_timers(&timer);
494 context;
495 if (quick_logs != 0) {
496 flushlogs();
497 check_logsize();
498 }
499 }
500
501 static void core_hourly()
502 {
503 context;
504 write_userfile(-1);
505 }
506
507 static void event_rehash()
508 {
509 context;
510 check_tcl_event("rehash");
511 }
512
513 static void event_prerehash()
514 {
515 context;
516 check_tcl_event("prerehash");
517 }
518
519 static void event_save()
520 {
521 context;
522 check_tcl_event("save");
523 }
524
525 static void event_logfile()
526 {
527 context;
528 check_tcl_event("logfile");
529 }
530
531 void kill_tcl();
532 extern module_entry *module_list;
533 void restart_chons();
534
535 #ifdef STATIC
536 void check_static(char *, char *(*)());
537
538 #include "mod/static.h"
539 #endif
540 int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
541 init_net(), init_modules(), init_tcl(int, char **),
542 init_language(int);
543
544 int main(int argc, char **argv)
545 {
546 int xx, i;
547 char buf[520], s[520];
548 FILE *f;
549 struct sigaction sv;
550 struct chanset_t *chan;
551
552 /* initialise context list */
553 for (i = 0; i < 16; i++) {
554 context;
555 }
556 #include "patch.h"
557 /* version info! */
558 sprintf(ver, "eggdrop v%s", egg_version);
559 sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999 Eggheads", egg_version);
560 /* now add on the patchlevel (for Tcl) */
561 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
562 strcat(egg_version, egg_xtra);
563 context;
564 #ifdef STOP_UAC
565 {
566 int nvpair[2];
567
568 nvpair[0] = SSIN_UACPROC;
569 nvpair[1] = UAC_NOPRINT;
570 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
571 }
572 #endif
573 /* set up error traps: */
574 sv.sa_handler = got_bus;
575 sigemptyset(&sv.sa_mask);
576 #ifdef SA_RESETHAND
577 sv.sa_flags = SA_RESETHAND;
578 #else
579 sv.sa_flags = 0;
580 #endif
581 sigaction(SIGBUS, &sv, NULL);
582 sv.sa_handler = got_segv;
583 sigaction(SIGSEGV, &sv, NULL);
584 #ifdef SA_RESETHAND
585 sv.sa_flags = 0;
586 #endif
587 sv.sa_handler = got_fpe;
588 sigaction(SIGFPE, &sv, NULL);
589 sv.sa_handler = got_term;
590 sigaction(SIGTERM, &sv, NULL);
591 sv.sa_handler = got_hup;
592 sigaction(SIGHUP, &sv, NULL);
593 sv.sa_handler = got_quit;
594 sigaction(SIGQUIT, &sv, NULL);
595 sv.sa_handler = SIG_IGN;
596 sigaction(SIGPIPE, &sv, NULL);
597 sv.sa_handler = got_ill;
598 sigaction(SIGILL, &sv, NULL);
599 sv.sa_handler = got_alarm;
600 sigaction(SIGALRM, &sv, NULL);
601 /* initialize variables and stuff */
602 now = time(NULL);
603 chanset = NULL;
604 nowtm = localtime(&now);
605 lastmin = nowtm->tm_min;
606 srandom(now);
607 init_mem();
608 init_language(1);
609 if (argc > 1)
610 for (i = 1; i < argc; i++)
611 do_arg(argv[i]);
612 printf("\n%s\n", version);
613 init_dcc_max();
614 init_userent();
615 init_misc();
616 init_bots();
617 init_net();
618 init_modules();
619 init_tcl(argc, argv);
620 init_language(0);
621 #ifdef STATIC
622 link_statics();
623 #endif
624 context;
625 strcpy(s, ctime(&now));
626 s[strlen(s) - 1] = 0;
627 strcpy(&s[11], &s[20]);
628 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
629 chanprog();
630 context;
631 if (encrypt_pass == 0) {
632 printf(MOD_NOCRYPT);
633 exit(1);
634 }
635 i = 0;
636 for (chan = chanset; chan; chan = chan->next)
637 i++;
638 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
639 botnetnick, i, count_users(userlist));
640 cache_miss = 0;
641 cache_hit = 0;
642 context;
643 sprintf(pid_file, "pid.%s", botnetnick);
644 context;
645 /* check for pre-existing eggdrop! */
646 f = fopen(pid_file, "r");
647 if (f != NULL) {
648 fgets(s, 10, f);
649 xx = atoi(s);
650 kill(xx, SIGCHLD); /* meaningless kill to determine if pid
651 * is used */
652 if (errno != ESRCH) {
653 printf(EGG_RUNNING1, origbotname);
654 printf(EGG_RUNNING2, pid_file);
655 exit(1);
656 }
657 }
658 context;
659 #ifndef CYGWIN_HACKS
660 /* move into background? */
661 if (backgrd) {
662 xx = fork();
663 if (xx == -1)
664 fatal("CANNOT FORK PROCESS.", 0);
665 if (xx != 0) {
666 FILE *fp;
667
668 /* need to attempt to write pid now, not later */
669 unlink(pid_file);
670 fp = fopen(pid_file, "w");
671 if (fp != NULL) {
672 fprintf(fp, "%u\n", xx);
673 if (fflush(fp)) {
674 /* kill bot incase a botchk is run from crond */
675 printf(EGG_NOWRITE, pid_file);
676 printf(" Try freeing some disk space\n");
677 fclose(fp);
678 unlink(pid_file);
679 exit(1);
680 }
681 fclose(fp);
682 } else
683 printf(EGG_NOWRITE, pid_file);
684 printf("Launched into the background (pid: %d)\n\n", xx);
685 #if HAVE_SETPGID
686 setpgid(xx, xx);
687 #endif
688 exit(0);
689 }
690 }
691 #endif
692 use_stderr = 0; /* stop writing to stderr now */
693 xx = getpid();
694 if ((xx != 0) && (!backgrd)) {
695 FILE *fp;
696
697 /* write pid to file */
698 unlink(pid_file);
699 fp = fopen(pid_file, "w");
700 if (fp != NULL) {
701 fprintf(fp, "%u\n", xx);
702 if (fflush(fp)) {
703 /* let the bot live since this doesn't appear to be a botchk */
704 printf(EGG_NOWRITE, pid_file);
705 fclose(fp);
706 unlink(pid_file);
707 }
708 fclose(fp);
709 } else
710 printf(EGG_NOWRITE, pid_file);
711 }
712 if (backgrd) {
713 /* ok, try to disassociate from controlling terminal */
714 /* (finger cross) */
715 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
716 setpgid(0, 0);
717 #endif
718 /* close out stdin/out/err */
719 freopen("/dev/null", "r", stdin);
720 freopen("/dev/null", "w", stdout);
721 freopen("/dev/null", "w", stderr);
722 #ifdef CYGWIN_HACKS
723 FreeConsole();
724 #endif
725 /* tcl wants those file handles kept open */
726 /* close(0); close(1); close(2); */
727 }
728 /* terminal emulating dcc chat */
729 if ((!backgrd) && (term_z)) {
730 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
731
732 dcc[n].addr = iptolong(getmyip());
733 dcc[n].sock = STDOUT;
734 dcc[n].timeval = now;
735 dcc[n].u.chat->con_flags = conmask;
736 dcc[n].u.chat->strip_flags = STRIP_ALL;
737 dcc[n].status = STAT_ECHO;
738 strcpy(dcc[n].nick, "HQ");
739 strcpy(dcc[n].host, "llama@console");
740 dcc[n].user = get_user_by_handle(userlist, "HQ");
741 /* make sure there's an innocuous HQ user if needed */
742 if (!dcc[n].user) {
743 adduser(userlist, "HQ", "none", "-", USER_PARTY);
744 dcc[n].user = get_user_by_handle(userlist, "HQ");
745 }
746 setsock(STDOUT, 0); /* entry in net table */
747 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
748 dcc_chatter(n);
749 }
750 then = now;
751 online_since = now;
752 autolink_cycle(NULL); /* hurry and connect to tandem bots */
753 add_help_reference("cmds1.help");
754 add_help_reference("cmds2.help");
755 add_help_reference("core.help");
756 add_hook(HOOK_SECONDLY, core_secondly);
757 add_hook(HOOK_MINUTELY, core_minutely);
758 add_hook(HOOK_HOURLY, core_hourly);
759 add_hook(HOOK_REHASH, event_rehash);
760 add_hook(HOOK_PRE_REHASH, event_prerehash);
761 add_hook(HOOK_USERFILE, event_save);
762 add_hook(HOOK_DAILY, event_logfile);
763
764 debug0("main: entering loop");
765 while (1) {
766 int socket_cleanup = 0;
767
768 context;
769 #if !defined(HAVE_PRE7_5_TCL) && !defined(HAVE_TCL_THREADS)
770 /* process a single tcl event */
771 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
772 #endif
773 /* lets move some of this here, reducing the numer of actual
774 * calls to periodic_timers */
775 now = time(NULL);
776 random(); /* woop, lets really jumble things */
777 if (now != then) { /* once a second */
778 call_hook(HOOK_SECONDLY);
779 then = now;
780 }
781 context;
782 /* only do this every so often */
783 if (!socket_cleanup) {
784 /* clean up sockets that were just left for dead */
785 for (i = 0; i < dcc_total; i++) {
786 if (dcc[i].type == &DCC_LOST) {
787 dcc[i].type = (struct dcc_table *) (dcc[i].sock);
788 lostdcc(i);
789 i--;
790 }
791 }
792 /* check for server or dcc activity */
793 dequeue_sockets();
794 socket_cleanup = 5;
795 } else
796 socket_cleanup--;
797 /* new net routines: help me mary! */
798 xx = sockgets(buf, &i);
799 if (xx >= 0) { /* non-error */
800 int idx;
801
802 context;
803 for (idx = 0; idx < dcc_total; idx++)
804 if (dcc[idx].sock == xx) {
805 if (dcc[idx].type && dcc[idx].type->activity)
806 dcc[idx].type->activity(idx, buf, i);
807 else
808 putlog(LOG_MISC, "*",
809 "!!! untrapped dcc activity: type %s, sock %d",
810 dcc[idx].type->name, dcc[idx].sock);
811 break;
812 }
813 } else if (xx == -1) { /* EOF from someone */
814 int idx;
815
816 if ((i == STDOUT) && !backgrd)
817 fatal("END OF FILE ON TERMINAL", 0);
818 context;
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 context;
840 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
841 for (i = 0; i < dcc_total; i++) {
842 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
843 if (dcc[i].type == &DCC_LOST)
844 dcc[i].type = (struct dcc_table *) (dcc[i].sock);
845 putlog(LOG_MISC, "*",
846 "DCC socket %d (type %d, name '%s') expired -- pfft",
847 dcc[i].sock, dcc[i].type, dcc[i].nick);
848 killsock(dcc[i].sock);
849 lostdcc(i);
850 i--;
851 }
852 }
853 } else if (xx == (-3)) {
854 call_hook(HOOK_IDLE);
855 socket_cleanup = 0; /* if we've been idle, cleanup & flush */
856 }
857 if (do_restart) {
858 if (do_restart == -2)
859 rehash();
860 else {
861 /* unload as many modules as possible */
862 int f = 1;
863 module_entry *p;
864 Function x;
865 char xx[256];
866
867 while (f) {
868 f = 0;
869 for (p = module_list; p != NULL; p = p->next) {
870 dependancy *d = dependancy_list;
871 int ok = 1;
872
873 while (ok && d) {
874 if (d->needed == p)
875 ok = 0;
876 d = d->next;
877 }
878 if (ok) {
879 strcpy(xx, p->name);
880 if (module_unload(xx, origbotname) == NULL) {
881 f = 1;
882 break;
883 }
884 }
885 }
886 }
887 p = module_list;
888 if (p && p->next && p->next->next)
889 /* should be only 2 modules now -
890 * blowfish & eggdrop */
891 putlog(LOG_MISC, "*", MOD_STAGNANT);
892 context;
893 flushlogs();
894 context;
895 kill_tcl();
896 init_tcl(argc, argv);
897 init_language(0);
898 x = p->funcs[MODCALL_START];
899 x(0);
900 rehash();
901 restart_chons();
902 }
903 do_restart = 0;
904 }
905 }
906 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23