/[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.121 - (hide annotations) (download) (as text)
Fri Sep 20 02:06:25 2002 UTC (16 years, 11 months ago) by stdarg
Branch: MAIN
Changes since 1.120: +5 -4 lines
File MIME type: text/x-chdr
* Moved several things from the core into libegg
* Server module uses new network code
* Lots of stuff removed from the server module, like flood stuff, which should go into a new module
* Moved some things from irc module to server module (mostly bind stuff, like pub)
* Not everything works right now, but I wanted to get it into cvs in case my computer dies a horrible death. I'm going to combine irc/server (mostly) and make a new one for all the default protection stuff.
* Right now server messages are not queued. I thought the code was pretty much beyond repair. The new queueing code will hopefully be more maintainable.
* I'm not sure I added/removed all the files necessary! Let me know if it doesn't compile for you.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23