/[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.13 - (show annotations) (download) (as text)
Sun Dec 19 16:06:34 1999 UTC (19 years, 9 months ago) by fabian
Branch: MAIN
Changes since 1.12: +63 -3 lines
File MIME type: text/x-chdr
G`Quann's traffic patch

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23