/[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.116 - (hide annotations) (download) (as text)
Sun May 5 15:19:11 2002 UTC (17 years, 3 months ago) by wingman
Branch: MAIN
Changes since 1.115: +15 -11 lines
File MIME type: text/x-chdr
Changes:

 * removed patch.h
 * added proper <filename>.h to (nearly) all <filename.h>
 * moved fixcolon to server.mod and irc.mod
 * moved ip2long to net.h
 * removed some unused function (forgot the name :-P)

Todo:

 * move config.h #include's away from main.h to files which
   needs them
 * clean up lib/eggdrop/module.h
   (now not just main.h is included but nearly all .h files from
    the core, but i guess this fixes itself when the global func
    table is abandon - then the modules must be revised too and
    we can clean up module.h)

And i'll bet i forgot something... ;-)

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23