/[cvs]/eggdrop1.9/src/main.c
ViewVC logotype

Annotation of /eggdrop1.9/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.115 - (hide annotations) (download) (as text)
Fri May 3 07:57:12 2002 UTC (17 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.114: +26 -45 lines
File MIME type: text/x-chdr
* Made itraffic_* otraffic_* into a single struct.
* mod-path -> mod_path

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23