/[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.117 - (hide annotations) (download) (as text)
Sun May 5 16:40:38 2002 UTC (17 years, 3 months ago) by tothwolf
Branch: MAIN
Changes since 1.116: +9 -6 lines
File MIME type: text/x-chdr
Grafted GPL header onto newer files.
Storing rcsid in binary files now.

1 guppy 1.56 /*
2 tothwolf 1.117 * main.c --
3 guppy 1.56 *
4 tothwolf 1.117 * core event handling
5     * signal handling
6     * command line arguments
7 segfault 1.1 */
8 guppy 1.56 /*
9     * Copyright (C) 1997 Robey Pointer
10 wcc 1.106 * Copyright (C) 1999, 2000, 2001, 2002 Eggheads Development Team
11 guppy 1.56 *
12 fabian 1.14 * 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 guppy 1.56 *
17 segfault 1.1 * 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 guppy 1.56 *
22 segfault 1.1 * 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 fabian 1.14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25     */
26 guppy 1.56 /*
27 segfault 1.1 * The author (Robey Pointer) can be reached at: robey@netcom.com
28 tothwolf 1.117 * NOTE: Robey is no longer working on this code, there is a discussion
29 guppy 1.60 * list available at eggheads@eggheads.org.
30 segfault 1.1 */
31 tothwolf 1.117
32     #ifndef lint
33     static const char rcsid[] = "$Id$";
34     #endif
35 segfault 1.1
36 fabian 1.28 #include "main.h"
37 fabian 1.27 #include <fcntl.h>
38 segfault 1.1 #include <time.h>
39     #include <errno.h>
40     #include <signal.h>
41     #include <netdb.h>
42     #include <setjmp.h>
43 wingman 1.116 #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 ite 1.76 #include <locale.h>
52    
53 fabian 1.27 #ifdef STOP_UAC /* osf/1 complains a lot */
54 segfault 1.1 #include <sys/sysinfo.h>
55     #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
56     #endif
57 fabian 1.27 /* Some systems have a working sys/wait.h even though configure will
58     * decide it's not bsd compatable. Oh well.
59     */
60 guppy 1.49
61 segfault 1.1 #include "chan.h"
62     #include "modules.h"
63     #include "tandem.h"
64 fabian 1.43 #include "bg.h"
65 stdarg 1.82 #include "egg_timer.h"
66 stdarg 1.85 #include "core_binds.h"
67 ite 1.105 #include "logfile.h"
68     #include "misc.h"
69 wingman 1.116 #include "dccutil.h" /* dprintf_eggdrop, new_dcc,
70     dcc_chatter, dcc_remove_lost,
71     lostdcc */
72 fabian 1.38
73 ite 1.96 #include "lib/adns/adns.h"
74 drummer 1.71
75 poptix 1.62 #ifdef CYGWIN_HACKS
76     #include <windows.h>
77     #endif
78    
79 segfault 1.1 #ifndef _POSIX_SOURCE
80 fabian 1.27 /* Solaris needs this */
81 segfault 1.1 #define _POSIX_SOURCE 1
82     #endif
83    
84 ite 1.104 extern char userfile[], botnetnick[];
85     extern int dcc_total, conmask, cache_hit, cache_miss;
86 fabian 1.27 extern struct dcc_t *dcc;
87     extern struct userrec *userlist;
88     extern struct chanset_t *chanset;
89     extern jmp_buf alarmret;
90    
91 wingman 1.116 #ifndef MAKING_MODS
92     extern struct dcc_table DCC_CHAT;
93     #endif /* MAKING_MODS */
94 segfault 1.1
95 guppy 1.56 /*
96 segfault 1.1 * 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 ite 1.76 char egg_version[1024] = VERSION;
103     int egg_numver = VERSION_NUM;
104 segfault 1.1
105 ite 1.109 eggdrop_t *egg = NULL; /* Eggdrop's context */
106    
107 fabian 1.27 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 ite 1.113 static char preload_module[121] = ""; /* Name of the module to preload */
119 fabian 1.27 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 guppy 1.92 char pid_file[120]; /* Name of the file for the pid to be
126 fabian 1.27 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 guppy 1.52 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
138 fabian 1.27 int resolve_timeout = 15; /* hostname/address lookup timeout */
139 guppy 1.72 char quit_msg[1024]; /* quit message */
140 fabian 1.27 time_t now; /* duh, now :) */
141 stdarg 1.108 egg_timeval_t egg_timeval_now; /* Same thing, but seconds and microseconds. */
142 fabian 1.27
143     /* Traffic stats
144     */
145 stdarg 1.115 #include "traffic.h" /* egg_traffic_t */
146     egg_traffic_t traffic;
147 segfault 1.1
148 fabian 1.44 void fatal(const char *s, int recoverable)
149 segfault 1.1 {
150     int i;
151    
152     putlog(LOG_MISC, "*", "* %s", s);
153     flushlogs();
154     for (i = 0; i < dcc_total; i++)
155 fabian 1.19 if (dcc[i].sock >= 0)
156 fabian 1.15 killsock(dcc[i].sock);
157 segfault 1.1 unlink(pid_file);
158 fabian 1.43 if (!recoverable) {
159     bg_send_quit(BG_ABORT);
160 segfault 1.1 exit(1);
161 fabian 1.43 }
162 segfault 1.1 }
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 fabian 1.27 /* Only timeout 1 socket per cycle, too risky for more */
178 segfault 1.1 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 fabian 1.43 bg_send_quit(BG_ABORT);
189 segfault 1.1 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 fabian 1.43 bg_send_quit(BG_ABORT);
200 segfault 1.1 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 stdarg 1.85 check_bind_event("sigterm");
213 segfault 1.1 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 stdarg 1.85 check_bind_event("sigquit");
223 segfault 1.1 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
224     return;
225     }
226    
227     static void got_hup(int z)
228     {
229     write_userfile(-1);
230 stdarg 1.85 check_bind_event("sighup");
231 segfault 1.1 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 fabian 1.27 /* A call to resolver (gethostbyname, etc) timed out
240     */
241 segfault 1.1 static void got_alarm(int z)
242     {
243     longjmp(alarmret, 1);
244 fabian 1.27
245     /* -Never reached- */
246 segfault 1.1 }
247    
248 guppy 1.101 /* Got ILL signal
249 fabian 1.27 */
250 segfault 1.1 static void got_ill(int z)
251     {
252 stdarg 1.85 check_bind_event("sigill");
253 fabian 1.10 }
254    
255 ite 1.114
256     static void print_version (void)
257 segfault 1.1 {
258 guppy 1.49 char x[1024], *z = x;
259 ite 1.114
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 segfault 1.1
286 ite 1.114 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 segfault 1.1 backgrd = 0;
315 ite 1.114 break;
316    
317     case 'c':
318 segfault 1.1 con_chan = 1;
319     term_z = 0;
320 ite 1.114 break;
321    
322     case 't':
323 segfault 1.1 con_chan = 0;
324     term_z = 1;
325 ite 1.114 break;
326    
327     case 'm':
328 segfault 1.1 make_userfile = 1;
329 ite 1.114 break;
330    
331     case 'p':
332     strlcpy(preload_module, optarg, sizeof preload_module);
333     break;
334    
335     case 'v':
336     print_version();
337 fabian 1.43 bg_send_quit(BG_ABORT);
338 segfault 1.1 exit(0);
339 ite 1.114 break; /* this should never be reached */
340    
341     case 'h':
342     print_help();
343 fabian 1.43 bg_send_quit(BG_ABORT);
344 segfault 1.1 exit(0);
345 ite 1.114 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 segfault 1.1 }
363    
364 fabian 1.27 /* Timer info */
365     static int lastmin = 99;
366     static struct tm nowtm;
367 segfault 1.1
368 fabian 1.34 /* Called once a second.
369 fabian 1.27 */
370 segfault 1.1 static void core_secondly()
371     {
372     static int cnt = 0;
373     int miltime;
374    
375 stdarg 1.82 call_hook(HOOK_SECONDLY); /* Will be removed later */
376 segfault 1.1 cnt++;
377 fabian 1.27 if (cnt >= 10) { /* Every 10 seconds */
378 segfault 1.1 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 tothwolf 1.79 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
388 fabian 1.17 if (nowtm.tm_min != lastmin) {
389 segfault 1.1 int i = 0;
390    
391 fabian 1.27 /* Once a minute */
392 segfault 1.1 lastmin = (lastmin + 1) % 60;
393     call_hook(HOOK_MINUTELY);
394     check_expired_ignores();
395 fabian 1.27 autolink_cycle(NULL); /* Attempt autolinks */
396     /* In case for some reason more than 1 min has passed: */
397 fabian 1.17 while (nowtm.tm_min != lastmin) {
398 fabian 1.27 /* Timer drift, dammit */
399 fabian 1.17 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
400 segfault 1.1 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 fabian 1.17 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
407     if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
408 segfault 1.1 call_hook(HOOK_5MINUTELY);
409 guppy 1.49 if (!miltime) { /* At midnight */
410     char s[25];
411 segfault 1.1
412 ite 1.111 strlcpy(s, ctime(&now), sizeof s);
413 fabian 1.4 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
414 guppy 1.69 call_hook(HOOK_BACKUP);
415 stdarg 1.90 call_hook(HOOK_DAILY);
416 segfault 1.1 }
417     }
418 fabian 1.17 if (nowtm.tm_min == notify_users_at)
419 segfault 1.1 call_hook(HOOK_HOURLY);
420     }
421     }
422    
423     static void core_minutely()
424     {
425 stdarg 1.85 check_bind_time(&nowtm);
426 segfault 1.1 }
427    
428     static void core_hourly()
429     {
430     write_userfile(-1);
431     }
432    
433     static void event_rehash()
434     {
435 stdarg 1.85 check_bind_event("rehash");
436 segfault 1.1 }
437    
438     static void event_prerehash()
439     {
440 stdarg 1.85 check_bind_event("prerehash");
441 segfault 1.1 }
442    
443     static void event_save()
444     {
445 stdarg 1.85 check_bind_event("save");
446 segfault 1.1 }
447    
448     static void event_logfile()
449     {
450 stdarg 1.85 check_bind_event("logfile");
451 segfault 1.1 }
452    
453 fabian 1.13 static void event_resettraffic()
454     {
455 stdarg 1.115 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 fabian 1.13 }
472    
473 guppy 1.61 static void event_loaded()
474     {
475 stdarg 1.85 check_bind_event("loaded");
476 guppy 1.61 }
477    
478 segfault 1.1 void kill_tcl();
479     extern module_entry *module_list;
480     void restart_chons();
481    
482 tothwolf 1.97 int init_userent(), init_net(),
483 ite 1.109 init_tcl(int, char **);
484 stdarg 1.112 int user_init();
485 stdarg 1.78 void botnet_init();
486 stdarg 1.88 void dns_init();
487 stdarg 1.85 void binds_init();
488 stdarg 1.94 void dcc_init();
489 fabian 1.11
490 fabian 1.44 void patch(const char *str)
491 fabian 1.11 {
492     char *p = strchr(egg_version, '+');
493    
494     if (!p)
495     p = &egg_version[strlen(egg_version)];
496     sprintf(p, "+%s", str);
497     egg_numver++;
498     sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
499     }
500 segfault 1.1
501 stdarg 1.100 /*
502 fabian 1.45 static inline void garbage_collect(void)
503     {
504     static u_8bit_t run_cnt = 0;
505    
506     if (run_cnt == 3)
507     garbage_collect_tclhash();
508     else
509     run_cnt++;
510     }
511 stdarg 1.100 */
512 fabian 1.45
513 segfault 1.1 int main(int argc, char **argv)
514     {
515     int xx, i;
516 guppy 1.49 char buf[520], s[25];
517 segfault 1.1 FILE *f;
518     struct sigaction sv;
519     struct chanset_t *chan;
520 stdarg 1.82 egg_timeval_t howlong;
521 segfault 1.1
522 tothwolf 1.81 #ifdef DEBUG
523 fabian 1.21 /* Make sure it can write core, if you make debug. Else it's pretty
524     * useless (dw)
525     */
526 fabian 1.15 {
527     #include <sys/resource.h>
528     struct rlimit cdlim;
529 fabian 1.21
530 fabian 1.15 cdlim.rlim_cur = RLIM_INFINITY;
531     cdlim.rlim_max = RLIM_INFINITY;
532 fabian 1.32 setrlimit(RLIMIT_CORE, &cdlim);
533 fabian 1.15 }
534     #endif
535 fabian 1.32
536 ite 1.99 #ifdef ENABLE_NLS
537 ite 1.76 setlocale(LC_MESSAGES, "");
538     bindtextdomain(PACKAGE, LOCALEDIR);
539     textdomain(PACKAGE);
540 ite 1.99 #endif
541 ite 1.76
542 segfault 1.1 #include "patch.h"
543 fabian 1.27 /* Version info! */
544 ite 1.114 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
545 wcc 1.106 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2002 Eggheads",
546 guppy 1.56 egg_version);
547 fabian 1.27 /* Now add on the patchlevel (for Tcl) */
548 fabian 1.2 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
549 segfault 1.1 strcat(egg_version, egg_xtra);
550     #ifdef STOP_UAC
551     {
552     int nvpair[2];
553    
554     nvpair[0] = SSIN_UACPROC;
555     nvpair[1] = UAC_NOPRINT;
556     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
557     }
558     #endif
559 fabian 1.27
560     /* Set up error traps: */
561 segfault 1.1 sv.sa_handler = got_bus;
562     sigemptyset(&sv.sa_mask);
563     #ifdef SA_RESETHAND
564     sv.sa_flags = SA_RESETHAND;
565     #else
566     sv.sa_flags = 0;
567     #endif
568     sigaction(SIGBUS, &sv, NULL);
569     sv.sa_handler = got_segv;
570     sigaction(SIGSEGV, &sv, NULL);
571     #ifdef SA_RESETHAND
572     sv.sa_flags = 0;
573     #endif
574     sv.sa_handler = got_fpe;
575     sigaction(SIGFPE, &sv, NULL);
576     sv.sa_handler = got_term;
577     sigaction(SIGTERM, &sv, NULL);
578     sv.sa_handler = got_hup;
579     sigaction(SIGHUP, &sv, NULL);
580     sv.sa_handler = got_quit;
581     sigaction(SIGQUIT, &sv, NULL);
582     sv.sa_handler = SIG_IGN;
583     sigaction(SIGPIPE, &sv, NULL);
584     sv.sa_handler = got_ill;
585     sigaction(SIGILL, &sv, NULL);
586     sv.sa_handler = got_alarm;
587     sigaction(SIGALRM, &sv, NULL);
588 fabian 1.27
589     /* Initialize variables and stuff */
590 stdarg 1.108 timer_get_time(&egg_timeval_now);
591     now = egg_timeval_now.sec;
592 segfault 1.1 chanset = NULL;
593 tothwolf 1.79 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
594 fabian 1.17 lastmin = nowtm.tm_min;
595 poptix 1.63 srandom(now % (getpid() + getppid()));
596 ite 1.114
597     do_args(argc, argv);
598    
599 segfault 1.1 printf("\n%s\n", version);
600 guppy 1.93 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
601 guppy 1.53
602     /* Don't allow eggdrop to run as root */
603     if (((int) getuid() == 0) || ((int) geteuid() == 0))
604 ite 1.76 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
605 guppy 1.53
606 ite 1.109 egg = eggdrop_new();
607 ite 1.110 script_init();
608 stdarg 1.98 binds_init();
609 ite 1.109 modules_init();
610 stdarg 1.90 logfile_init();
611 stdarg 1.89 timer_init();
612 stdarg 1.88 dns_init();
613 stdarg 1.85 core_binds_init();
614 stdarg 1.94 dcc_init();
615 stdarg 1.112 user_init();
616 segfault 1.1 init_userent();
617 stdarg 1.78 botnet_init();
618 segfault 1.1 init_net();
619 stdarg 1.80
620 fabian 1.43 if (backgrd)
621     bg_prepare_split();
622 stdarg 1.80
623 fabian 1.8 init_tcl(argc, argv);
624 stdarg 1.80
625 ite 1.111 strlcpy(s, ctime(&now), sizeof s);
626 segfault 1.1 strcpy(&s[11], &s[20]);
627     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
628 ite 1.113 if (preload_module[0])
629     module_load(preload_module);
630 segfault 1.1 chanprog();
631 guppy 1.49 if (!encrypt_pass) {
632 ite 1.73 printf(_("You have installed modules but have not selected an encryption\n\
633     module, please consult the default config file for info.\n"));
634 fabian 1.43 bg_send_quit(BG_ABORT);
635 segfault 1.1 exit(1);
636     }
637     i = 0;
638     for (chan = chanset; chan; chan = chan->next)
639     i++;
640 fabian 1.4 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
641 segfault 1.1 botnetnick, i, count_users(userlist));
642     cache_miss = 0;
643     cache_hit = 0;
644 guppy 1.92
645     if (!pid_file[0])
646     snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
647 fabian 1.27
648     /* Check for pre-existing eggdrop! */
649 segfault 1.1 f = fopen(pid_file, "r");
650     if (f != NULL) {
651     fgets(s, 10, f);
652     xx = atoi(s);
653 fabian 1.27 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
654     is used */
655 segfault 1.1 if (errno != ESRCH) {
656 guppy 1.103 printf(_("I detect %s already running from this directory.\n"), botnetnick);
657 ite 1.73 printf(_("If this is incorrect, erase the %s\n"), pid_file);
658 fabian 1.43 bg_send_quit(BG_ABORT);
659 segfault 1.1 exit(1);
660     }
661     }
662 fabian 1.27
663     /* Move into background? */
664 segfault 1.1 if (backgrd) {
665 fabian 1.38 #ifndef CYGWIN_HACKS
666 fabian 1.43 bg_do_split();
667 fabian 1.38 } else { /* !backgrd */
668 fabian 1.5 #endif
669 fabian 1.38 xx = getpid();
670     if (xx != 0) {
671     FILE *fp;
672    
673     /* Write pid to file */
674     unlink(pid_file);
675     fp = fopen(pid_file, "w");
676     if (fp != NULL) {
677     fprintf(fp, "%u\n", xx);
678     if (fflush(fp)) {
679     /* Let the bot live since this doesn't appear to be a botchk */
680 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
681 fabian 1.38 fclose(fp);
682     unlink(pid_file);
683     } else
684     fclose(fp);
685 fabian 1.27 } else
686 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
687 fabian 1.38 #ifdef CYGWIN_HACKS
688 guppy 1.68 printf("Launched into the background (pid: %d)\n\n", xx);
689 fabian 1.38 #endif
690     }
691 segfault 1.1 }
692 fabian 1.38
693     use_stderr = 0; /* Stop writing to stderr now */
694 segfault 1.1 if (backgrd) {
695 fabian 1.27 /* Ok, try to disassociate from controlling terminal (finger cross) */
696 fabian 1.5 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
697 segfault 1.1 setpgid(0, 0);
698     #endif
699 fabian 1.38 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
700 segfault 1.1 freopen("/dev/null", "r", stdin);
701     freopen("/dev/null", "w", stdout);
702     freopen("/dev/null", "w", stderr);
703 poptix 1.62 #ifdef CYGWIN_HACKS
704     FreeConsole();
705     #endif
706 segfault 1.1 }
707 fabian 1.27
708     /* Terminal emulating dcc chat */
709 fabian 1.42 if (!backgrd && term_z) {
710 segfault 1.1 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
711    
712 drummer 1.71 dcc[n].addr[0] = '\0';
713 segfault 1.1 dcc[n].sock = STDOUT;
714     dcc[n].timeval = now;
715     dcc[n].u.chat->con_flags = conmask;
716     dcc[n].u.chat->strip_flags = STRIP_ALL;
717     dcc[n].status = STAT_ECHO;
718     strcpy(dcc[n].nick, "HQ");
719     strcpy(dcc[n].host, "llama@console");
720 ite 1.107 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
721 fabian 1.27 /* Make sure there's an innocuous HQ user if needed */
722 segfault 1.1 if (!dcc[n].user) {
723 ite 1.107 userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
724     dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
725 segfault 1.1 }
726 fabian 1.27 setsock(STDOUT, 0); /* Entry in net table */
727 segfault 1.1 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
728     dcc_chatter(n);
729     }
730 fabian 1.27
731 segfault 1.1 online_since = now;
732 fabian 1.27 autolink_cycle(NULL); /* Hurry and connect to tandem bots */
733 segfault 1.1 add_help_reference("cmds1.help");
734     add_help_reference("cmds2.help");
735     add_help_reference("core.help");
736 stdarg 1.82 howlong.sec = 1;
737     howlong.usec = 0;
738     timer_create_repeater(&howlong, (Function) core_secondly);
739 fabian 1.22 add_hook(HOOK_MINUTELY, (Function) core_minutely);
740     add_hook(HOOK_HOURLY, (Function) core_hourly);
741     add_hook(HOOK_REHASH, (Function) event_rehash);
742     add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
743     add_hook(HOOK_USERFILE, (Function) event_save);
744 guppy 1.69 add_hook(HOOK_BACKUP, (Function) backup_userfile);
745 fabian 1.22 add_hook(HOOK_DAILY, (Function) event_logfile);
746     add_hook(HOOK_DAILY, (Function) event_resettraffic);
747 guppy 1.61 add_hook(HOOK_LOADED, (Function) event_loaded);
748    
749     call_hook(HOOK_LOADED);
750 segfault 1.1
751     debug0("main: entering loop");
752     while (1) {
753     int socket_cleanup = 0;
754    
755 fabian 1.43 #if !defined(HAVE_PRE7_5_TCL)
756 fabian 1.27 /* Process a single tcl event */
757 segfault 1.1 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
758 fabian 1.5 #endif
759 fabian 1.45
760 fabian 1.27 /* Lets move some of this here, reducing the numer of actual
761     * calls to periodic_timers
762     */
763 stdarg 1.108 timer_get_time(&egg_timeval_now);
764     now = egg_timeval_now.sec;
765 fabian 1.27 random(); /* Woop, lets really jumble things */
766 stdarg 1.82 timer_run();
767 fabian 1.45
768     /* Only do this every so often. */
769 segfault 1.1 if (!socket_cleanup) {
770 fabian 1.45 socket_cleanup = 5;
771    
772     /* Remove dead dcc entries. */
773 fabian 1.16 dcc_remove_lost();
774 fabian 1.45
775     /* Check for server or dcc activity. */
776 segfault 1.1 dequeue_sockets();
777     } else
778     socket_cleanup--;
779 fabian 1.45
780     /* Free unused structures. */
781 stdarg 1.100 /* garbage_collect(); */
782 fabian 1.45
783 segfault 1.1 xx = sockgets(buf, &i);
784 stdarg 1.108 timer_get_time(&egg_timeval_now);
785 fabian 1.27 if (xx >= 0) { /* Non-error */
786 segfault 1.1 int idx;
787    
788     for (idx = 0; idx < dcc_total; idx++)
789     if (dcc[idx].sock == xx) {
790 fabian 1.13 if (dcc[idx].type && dcc[idx].type->activity) {
791 fabian 1.27 /* Traffic stats */
792     if (dcc[idx].type->name) {
793     if (!strncmp(dcc[idx].type->name, "BOT", 3))
794 stdarg 1.115 traffic.in_today.bn += strlen(buf) + 1;
795 fabian 1.27 else if (!strcmp(dcc[idx].type->name, "SERVER"))
796 stdarg 1.115 traffic.in_today.irc += strlen(buf) + 1;
797 fabian 1.27 else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
798 stdarg 1.115 traffic.in_today.dcc += strlen(buf) + 1;
799 fabian 1.27 else if (!strncmp(dcc[idx].type->name, "FILES", 5))
800 stdarg 1.115 traffic.in_today.dcc += strlen(buf) + 1;
801 fabian 1.27 else if (!strcmp(dcc[idx].type->name, "SEND"))
802 stdarg 1.115 traffic.in_today.trans += strlen(buf) + 1;
803 fabian 1.27 else if (!strncmp(dcc[idx].type->name, "GET", 3))
804 stdarg 1.115 traffic.in_today.trans += strlen(buf) + 1;
805 fabian 1.27 else
806 stdarg 1.115 traffic.in_today.unknown += strlen(buf) + 1;
807 fabian 1.27 }
808 guppy 1.56 dcc[idx].type->activity(idx, buf, i);
809 fabian 1.13 } else
810 segfault 1.1 putlog(LOG_MISC, "*",
811     "!!! untrapped dcc activity: type %s, sock %d",
812     dcc[idx].type->name, dcc[idx].sock);
813     break;
814     }
815     } else if (xx == -1) { /* EOF from someone */
816     int idx;
817    
818 fabian 1.45 if (i == STDOUT && !backgrd)
819 segfault 1.1 fatal("END OF FILE ON TERMINAL", 0);
820     for (idx = 0; idx < dcc_total; idx++)
821     if (dcc[idx].sock == i) {
822     if (dcc[idx].type && dcc[idx].type->eof)
823     dcc[idx].type->eof(idx);
824     else {
825     putlog(LOG_MISC, "*",
826     "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
827 fabian 1.2 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
828     killsock(i);
829 segfault 1.1 lostdcc(idx);
830     }
831     idx = dcc_total + 1;
832     }
833     if (idx == dcc_total) {
834     putlog(LOG_MISC, "*",
835 fabian 1.2 "(@) EOF socket %d, not a dcc socket, not anything.", i);
836     close(i);
837     killsock(i);
838 segfault 1.1 }
839 fabian 1.45 } else if (xx == -2 && errno != EINTR) { /* select() error */
840 segfault 1.1 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     putlog(LOG_MISC, "*",
844     "DCC socket %d (type %d, name '%s') expired -- pfft",
845     dcc[i].sock, dcc[i].type, dcc[i].nick);
846     killsock(dcc[i].sock);
847     lostdcc(i);
848     i--;
849     }
850     }
851 fabian 1.45 } else if (xx == -3) {
852 segfault 1.1 call_hook(HOOK_IDLE);
853 fabian 1.27 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
854 segfault 1.1 }
855 fabian 1.27
856 segfault 1.1 if (do_restart) {
857     if (do_restart == -2)
858     rehash();
859     else {
860 fabian 1.27 /* Unload as many modules as possible */
861 segfault 1.1 int f = 1;
862     module_entry *p;
863     Function x;
864     char xx[256];
865 guppy 1.59
866     /* oops, I guess we should call this event before tcl is restarted */
867 stdarg 1.85 check_bind_event("prerestart");
868 segfault 1.1
869     while (f) {
870     f = 0;
871     for (p = module_list; p != NULL; p = p->next) {
872     dependancy *d = dependancy_list;
873     int ok = 1;
874    
875     while (ok && d) {
876     if (d->needed == p)
877     ok = 0;
878     d = d->next;
879     }
880     if (ok) {
881     strcpy(xx, p->name);
882 guppy 1.103 if (module_unload(xx, botnetnick) == NULL) {
883 segfault 1.1 f = 1;
884     break;
885     }
886     }
887     }
888     }
889     p = module_list;
890     if (p && p->next && p->next->next)
891 fabian 1.45 /* Should be only 2 modules now - blowfish (or some other
892     encryption module) and eggdrop. */
893 ite 1.73 putlog(LOG_MISC, "*", _("Stagnant module; there WILL be memory leaks!"));
894 segfault 1.1 flushlogs();
895     kill_tcl();
896 stdarg 1.87 timer_destroy_all(); /* Destroy all timers. */
897 fabian 1.8 init_tcl(argc, argv);
898 fabian 1.30 /* We expect the encryption module as the current module pointed
899     * to by `module_list'.
900     */
901 segfault 1.1 x = p->funcs[MODCALL_START];
902 fabian 1.30 /* `NULL' indicates that we just recently restarted. The module
903     * is expected to re-initialise as needed.
904     */
905     x(NULL);
906 segfault 1.1 rehash();
907     restart_chons();
908 guppy 1.61 call_hook(HOOK_LOADED);
909 segfault 1.1 }
910     do_restart = 0;
911     }
912     }
913     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23