/[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.133 - (hide annotations) (download) (as text)
Tue Feb 11 02:32:05 2003 UTC (16 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.132: +5 -2 lines
File MIME type: text/x-chdr
* Some initial config code to play around with.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23