/[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.1 - (hide annotations) (download) (as text)
Mon Sep 13 14:36:18 1999 UTC (19 years, 10 months ago) by segfault
Branch: MAIN
File MIME type: text/x-chdr
Initial commit based off of .29 cvs

1 segfault 1.1 /*
2     * main.c -- handles:
3     * changing nicknames when the desired nick is in use
4     * flood detection
5     * signal handling
6     * command line arguments
7     *
8     * dprintf'ized, 15nov1995
9     */
10     /*
11     * This file is part of the eggdrop source code
12     * copyright (c) 1997 Robey Pointer
13     * and is distributed according to the GNU general public license.
14     *
15     * Parts of this eggdrop source code are copyright (c)1999 Eggheads
16     * and is distributed according to the GNU general public license.
17     *
18     * This program is free software; you can redistribute it and/or modify
19     * it under the terms of the GNU General Public License as published by
20     * the Free Software Foundation; either version 2 of the License, or
21     * (at your option) any later version.
22     *
23     * This program is distributed in the hope that it will be useful,
24     * but WITHOUT ANY WARRANTY; without even the implied warranty of
25     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26     * GNU General Public License for more details.
27     *
28     * You should have received a copy of the GNU General Public License
29     * along with this program; if not, write to the Free Software
30     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31     *
32     * The author (Robey Pointer) can be reached at: robey@netcom.com
33     * NOTE: Robey is no long working on this code, there is a discussion
34     * list avaliable at eggheads@eggheads.org.
35     */
36    
37     #include "main.h"
38     #include <time.h>
39     #include <fcntl.h>
40     #include <errno.h>
41     #include <signal.h>
42     #include <netdb.h>
43     #include <setjmp.h>
44     #ifdef STOP_UAC /* osf/1 complains a lot */
45     #include <sys/sysinfo.h>
46     #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
47     #endif
48     /* some systems have a working sys/wait.h even though configure will
49     * decide it's not bsd compatable. oh well. */
50     #include "chan.h"
51     #include "modules.h"
52     #include "tandem.h"
53    
54     #ifndef _POSIX_SOURCE
55     /* solaris needs this */
56     #define _POSIX_SOURCE 1
57     #endif
58    
59     extern char origbotname[];
60     extern int dcc_total;
61     extern struct dcc_t *dcc;
62     extern int conmask;
63     extern struct userrec *userlist;
64     extern int cache_hit, cache_miss;
65     extern char userfile[];
66     extern struct chanset_t *chanset;
67     extern char botnetnick[];
68     extern log_t *logs;
69     extern Tcl_Interp *interp;
70     extern int max_logs;
71     extern tcl_timer_t *timer, *utimer;
72     extern jmp_buf alarmret;
73     extern int quick_logs; /* dw */
74    
75     /*
76     * Please use the PATCH macro instead of directly altering the version
77     * string from now on (it makes it much easier to maintain patches).
78     * Also please read the README file regarding your rights to distribute
79     * modified versions of this bot.
80     */
81    
82     char egg_version[1024] = "1.4.0";
83     int egg_numver = 1040000;
84    
85     char notify_new[121] = ""; /* person to send a note to for new users */
86     int default_flags = 0; /* default user flags and */
87     int default_uflags = 0; /* default userdefinied flags for people */
88     /* who say 'hello' or for .adduser */
89    
90     int backgrd = 1; /* run in the background? */
91     int con_chan = 0; /* foreground: constantly display channel
92     * stats? */
93     int term_z = 0; /* foreground: use the terminal as a party
94     * line? */
95     char configfile[121] = "egg.config"; /* name of the config file */
96     char helpdir[121]; /* directory of help files (if used) */
97     char textdir[121] = ""; /* directory for text files that get dumped */
98     int keep_all_logs = 0; /* never erase logfiles, no matter how old
99     * they are? */
100     time_t online_since; /* unix-time that the bot loaded up */
101     int make_userfile = 0; /* using bot in make-userfile mode? (first
102     * user to 'hello' becomes master) */
103     char owner[121] = ""; /* permanent owner(s) of the bot */
104     char pid_file[40]; /* name of the file for the pid to be
105     * stored in */
106     int save_users_at = 0; /* how many minutes past the hour to
107     * save the userfile? */
108     int notify_users_at = 0; /* how many minutes past the hour to
109     * notify users of notes? */
110     int switch_logfiles_at = 300; /* when (military time) to switch logfiles */
111     char version[81]; /* version info (long form) */
112     char ver[41]; /* version info (short form) */
113     char egg_xtra[2048]; /* patch info */
114     int use_stderr = 1; /* send stuff to stderr instead of logfiles? */
115     int do_restart = 0; /* .restart has been called, restart asap */
116     int die_on_sighup = 0; /* die if bot receives SIGHUP */
117     int die_on_sigterm = 0; /* die if bot receives SIGTERM */
118     int resolve_timeout = 15; /* hostname/address lookup timeout */
119     time_t now; /* duh, now :) */
120    
121     /* context storage for fatal crashes */
122     char cx_file[16][30];
123     char cx_note[16][256];
124     int cx_line[16];
125     int cx_ptr = 0;
126    
127     void fatal(char *s, int recoverable)
128     {
129     int i;
130    
131     putlog(LOG_MISC, "*", "* %s", s);
132     flushlogs();
133     for (i = 0; i < dcc_total; i++)
134     killsock(dcc[i].sock);
135     unlink(pid_file);
136     if (!recoverable)
137     exit(1);
138     }
139    
140     int expmem_chanprog(), expmem_users(), expmem_misc(), expmem_dccutil(),
141     expmem_botnet(), expmem_tcl(), expmem_tclhash(), expmem_net(),
142     expmem_modules(int), expmem_language(), expmem_tcldcc();
143    
144     /* for mem.c : calculate memory we SHOULD be using */
145     int expected_memory()
146     {
147     int tot;
148    
149     context;
150     tot = expmem_chanprog() + expmem_users() + expmem_misc() +
151     expmem_dccutil() + expmem_botnet() + expmem_tcl() + expmem_tclhash() +
152     expmem_net() + expmem_modules(0) + expmem_language() + expmem_tcldcc();
153     return tot;
154     }
155    
156     static void check_expired_dcc()
157     {
158     int i;
159    
160     for (i = 0; i < dcc_total; i++)
161     if (dcc[i].type && dcc[i].type->timeout_val &&
162     ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
163     if (dcc[i].type->timeout)
164     dcc[i].type->timeout(i);
165     else if (dcc[i].type->eof)
166     dcc[i].type->eof(i);
167     else
168     continue;
169     /* only timeout 1 socket per cycle, too risky for more */
170     return;
171     }
172     }
173    
174     static int nested_debug = 0;
175    
176     void write_debug()
177     {
178     int x;
179     char s[80];
180     int y;
181    
182     if (nested_debug) {
183     /* yoicks, if we have this there's serious trouble */
184     /* all of these are pretty reliable, so we'll try these */
185     /* dont try and display context-notes in here, it's _not_ safe <cybah> */
186     x = creat("DEBUG.DEBUG", 0644);
187     setsock(x, SOCK_NONSOCK);
188     if (x >= 0) {
189     strcpy(s, ctime(&now));
190     dprintf(-x, "Debug (%s) written %s", ver, s);
191     dprintf(-x, "Please report problem to eggheads@eggheads.org");
192     dprintf(-x, "after a visit to http://www.eggheads.org/bugs.html");
193     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
194     dprintf(-x, "Context: ");
195     cx_ptr = cx_ptr & 15;
196     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
197     dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
198     dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
199     killsock(x);
200     close(x);
201     }
202     exit(1); /* dont even try & tell people about, that may
203     * have caused the fault last time */
204     } else
205     nested_debug = 1;
206     putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
207     cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
208     x = creat("DEBUG", 0644);
209     setsock(x, SOCK_NONSOCK);
210     if (x < 0) {
211     putlog(LOG_MISC, "*", "* Failed to write DEBUG");
212     } else {
213     strcpy(s, ctime(&now));
214     dprintf(-x, "Debug (%s) written %s", ver, s);
215     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
216     #ifdef STATIC
217     dprintf(-x, "STATICALLY LINKED\n");
218     #endif
219     strcpy(s, "info library");
220     if (interp && (Tcl_Eval(interp, s) == TCL_OK))
221     dprintf(-x, "Using tcl library: %s (header version %s)\n",
222     interp->result, TCL_VERSION);
223     dprintf(-x, "Compile flags: %s\n", CCFLAGS);
224     dprintf(-x, "Link flags : %s\n", LDFLAGS);
225     dprintf(-x, "Strip flags : %s\n", STRIPFLAGS);
226     dprintf(-x, "Context: ");
227     cx_ptr = cx_ptr & 15;
228     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
229     dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
230     (cx_note[y][0]) ? cx_note[y] : "");
231     dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
232     (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
233     tell_dcc(-x);
234     dprintf(-x, "\n");
235     debug_mem_to_dcc(-x);
236     killsock(x);
237     close(x);
238     putlog(LOG_MISC, "*", "* Wrote DEBUG");
239     }
240     }
241    
242     static void got_bus(int z)
243     {
244     write_debug();
245     fatal("BUS ERROR -- CRASHING!", 1);
246     #ifdef SA_RESETHAND
247     kill(getpid(), SIGBUS);
248     #else
249     exit(1);
250     #endif
251     }
252    
253     static void got_segv(int z)
254     {
255     write_debug();
256     fatal("SEGMENT VIOLATION -- CRASHING!", 1);
257     #ifdef SA_RESETHAND
258     kill(getpid(), SIGSEGV);
259     #else
260     exit(1);
261     #endif
262     }
263    
264     static void got_fpe(int z)
265     {
266     write_debug();
267     fatal("FLOATING POINT ERROR -- CRASHING!", 0);
268     }
269    
270     static void got_term(int z)
271     {
272     write_userfile(-1);
273     check_tcl_event("sigterm");
274     if (die_on_sigterm) {
275     botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
276     fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
277     } else {
278     putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
279     }
280     }
281    
282     static void got_quit(int z)
283     {
284     check_tcl_event("sigquit");
285     putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
286     return;
287     }
288    
289     static void got_hup(int z)
290     {
291     write_userfile(-1);
292     check_tcl_event("sighup");
293     if (die_on_sighup) {
294     fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
295     } else
296     putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
297     do_restart = -2;
298     return;
299     }
300    
301     static void got_alarm(int z)
302     {
303     /* a call to resolver (gethostbyname, etc) timed out */
304     longjmp(alarmret, 1);
305     /* STUPID STUPID STUPID */
306     /* return; */
307     }
308    
309     /* got ILL signal -- log context and continue */
310     static void got_ill(int z)
311     {
312     check_tcl_event("sigill");
313     putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
314     cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
315     }
316    
317     static void do_arg(char *s)
318     {
319     int i;
320    
321     if (s[0] == '-')
322     for (i = 1; i < strlen(s); i++) {
323     if (s[i] == 'n')
324     backgrd = 0;
325     if (s[i] == 'c') {
326     con_chan = 1;
327     term_z = 0;
328     }
329     if (s[i] == 't') {
330     con_chan = 0;
331     term_z = 1;
332     }
333     if (s[i] == 'm')
334     make_userfile = 1;
335     if (s[i] == 'v') {
336     char x[256], *z = x;
337    
338     strcpy(x, egg_version);
339     newsplit(&z);
340     newsplit(&z);
341     printf("%s\n", version);
342     if (z[0])
343     printf(" (patches: %s)\n", z);
344     exit(0);
345     }
346     if (s[i] == 'h') {
347     printf("\n%s\n\n", version);
348     printf(EGG_USAGE);
349     printf("\n");
350     exit(0);
351     }
352     } else
353     strcpy(configfile, s);
354     }
355    
356     void backup_userfile()
357     {
358     char s[150];
359    
360     putlog(LOG_MISC, "*", USERF_BACKUP);
361     strcpy(s, userfile);
362     strcat(s, "~bak");
363     copyfile(userfile, s);
364     }
365    
366     /* timer info: */
367     static int lastmin = 99;
368     static time_t then;
369     static struct tm *nowtm;
370    
371     /* rally BB, this is not QUITE as bad as it seems <G> */
372     /* ONCE A SECOND */
373     static void core_secondly()
374     {
375     static int cnt = 0;
376     int miltime;
377    
378     do_check_timers(&utimer); /* secondly timers */
379     cnt++;
380     if (cnt >= 10) { /* every 10 seconds */
381     cnt = 0;
382     check_expired_dcc();
383     if (con_chan && !backgrd) {
384     dprintf(DP_STDOUT, "\033[2J\033[1;1H");
385     tell_verbose_status(DP_STDOUT);
386     do_module_report(DP_STDOUT, 0, "server");
387     do_module_report(DP_STDOUT, 0, "channels");
388     tell_mem_status_dcc(DP_STDOUT);
389     }
390     }
391     context;
392     nowtm = localtime(&now);
393     if (nowtm->tm_min != lastmin) {
394     int i = 0;
395    
396     /* once a minute */
397     lastmin = (lastmin + 1) % 60;
398     call_hook(HOOK_MINUTELY);
399     check_expired_ignores();
400     autolink_cycle(NULL); /* attempt autolinks */
401     /* in case for some reason more than 1 min has passed: */
402     while (nowtm->tm_min != lastmin) {
403     /* timer drift, dammit */
404     debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm->tm_min);
405     context;
406     i++;
407     lastmin = (lastmin + 1) % 60;
408     call_hook(HOOK_MINUTELY);
409     }
410     if (i > 1)
411     putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
412     miltime = (nowtm->tm_hour * 100) + (nowtm->tm_min);
413     context;
414     if (((int) (nowtm->tm_min / 5) * 5) == (nowtm->tm_min)) { /* 5 min */
415     call_hook(HOOK_5MINUTELY);
416     check_botnet_pings();
417     context;
418     if (quick_logs == 0) {
419     flushlogs();
420     check_logsize();
421     }
422     if (miltime == 0) { /* at midnight */
423     char s[128];
424     int j;
425    
426     s[my_strcpy(s, ctime(&now)) - 1] = 0;
427     putlog(LOG_MISC, "*", "--- %.11s%s", s, s + 20);
428     backup_userfile();
429     for (j = 0; j < max_logs; j++) {
430     if (logs[j].filename != NULL && logs[j].f != NULL) {
431     fclose(logs[j].f);
432     logs[j].f = NULL;
433     }
434     }
435     }
436     }
437     context;
438     if (nowtm->tm_min == notify_users_at)
439     call_hook(HOOK_HOURLY);
440     context; /* these no longer need checking since they are
441     * all check vs minutely settings and we only
442     * get this far on the minute */
443     if (miltime == switch_logfiles_at) {
444     call_hook(HOOK_DAILY);
445     if (!keep_all_logs) {
446     putlog(LOG_MISC, "*", MISC_LOGSWITCH);
447     for (i = 0; i < max_logs; i++)
448     if (logs[i].filename) {
449     char s[1024];
450    
451     if (logs[i].f) {
452     fclose(logs[i].f);
453     logs[i].f = NULL;
454     }
455     simple_sprintf(s, "%s.yesterday", logs[i].filename);
456     unlink(s);
457     rename(logs[i].filename, s);
458     }
459     }
460     }
461     }
462     }
463    
464     static void core_minutely()
465     {
466     context;
467     check_tcl_time(nowtm);
468     do_check_timers(&timer);
469     context;
470     if (quick_logs != 0) {
471     flushlogs();
472     check_logsize();
473     }
474     }
475    
476     static void core_hourly()
477     {
478     context;
479     write_userfile(-1);
480     }
481    
482     static void event_rehash()
483     {
484     context;
485     check_tcl_event("rehash");
486     }
487    
488     static void event_prerehash()
489     {
490     context;
491     check_tcl_event("prerehash");
492     }
493    
494     static void event_save()
495     {
496     context;
497     check_tcl_event("save");
498     }
499    
500     static void event_logfile()
501     {
502     context;
503     check_tcl_event("logfile");
504     }
505    
506     void kill_tcl();
507     extern module_entry *module_list;
508     void restart_chons();
509    
510     #ifdef STATIC
511     void check_static(char *, char *(*)());
512    
513     #include "mod/static.h"
514     #endif
515     int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
516     init_net(), init_modules(), init_tcl(), init_language(int);
517    
518     int main(int argc, char **argv)
519     {
520     int xx, i;
521     char buf[520], s[520];
522     FILE *f;
523     struct sigaction sv;
524     struct chanset_t *chan;
525    
526     /* initialise context list */
527     for (i = 0; i < 16; i++) {
528     context;
529     }
530     #include "patch.h"
531     /* version info! */
532     sprintf(ver, "eggdrop v%s", egg_version);
533     sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999 Eggheads", egg_version);
534     /* now add on the patchlevel (for Tcl) */
535     sprintf(&egg_version[strlen(egg_version)], " %08u", egg_numver);
536     strcat(egg_version, egg_xtra);
537     context;
538     #ifdef STOP_UAC
539     {
540     int nvpair[2];
541    
542     nvpair[0] = SSIN_UACPROC;
543     nvpair[1] = UAC_NOPRINT;
544     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
545     }
546     #endif
547     /* set up error traps: */
548     sv.sa_handler = got_bus;
549     sigemptyset(&sv.sa_mask);
550     #ifdef SA_RESETHAND
551     sv.sa_flags = SA_RESETHAND;
552     #else
553     sv.sa_flags = 0;
554     #endif
555     sigaction(SIGBUS, &sv, NULL);
556     sv.sa_handler = got_segv;
557     sigaction(SIGSEGV, &sv, NULL);
558     #ifdef SA_RESETHAND
559     sv.sa_flags = 0;
560     #endif
561     sv.sa_handler = got_fpe;
562     sigaction(SIGFPE, &sv, NULL);
563     sv.sa_handler = got_term;
564     sigaction(SIGTERM, &sv, NULL);
565     sv.sa_handler = got_hup;
566     sigaction(SIGHUP, &sv, NULL);
567     sv.sa_handler = got_quit;
568     sigaction(SIGQUIT, &sv, NULL);
569     sv.sa_handler = SIG_IGN;
570     sigaction(SIGPIPE, &sv, NULL);
571     sv.sa_handler = got_ill;
572     sigaction(SIGILL, &sv, NULL);
573     sv.sa_handler = got_alarm;
574     sigaction(SIGALRM, &sv, NULL);
575     /* initialize variables and stuff */
576     now = time(NULL);
577     chanset = NULL;
578     nowtm = localtime(&now);
579     lastmin = nowtm->tm_min;
580     srandom(now);
581     init_mem();
582     init_language(1);
583     if (argc > 1)
584     for (i = 1; i < argc; i++)
585     do_arg(argv[i]);
586     printf("\n%s\n", version);
587     init_dcc_max();
588     init_userent();
589     init_misc();
590     init_bots();
591     init_net();
592     init_modules();
593     init_tcl();
594     init_language(0);
595     #ifdef STATIC
596     link_statics();
597     #endif
598     context;
599     strcpy(s, ctime(&now));
600     s[strlen(s) - 1] = 0;
601     strcpy(&s[11], &s[20]);
602     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
603     chanprog();
604     context;
605     if (encrypt_pass == 0) {
606     printf(MOD_NOCRYPT);
607     exit(1);
608     }
609     i = 0;
610     for (chan = chanset; chan; chan = chan->next)
611     i++;
612     putlog(LOG_ALL, "*", "=== %s: %d channels, %d users.\n",
613     botnetnick, i, count_users(userlist));
614     cache_miss = 0;
615     cache_hit = 0;
616     context;
617     sprintf(pid_file, "pid.%s", botnetnick);
618     context;
619     /* check for pre-existing eggdrop! */
620     f = fopen(pid_file, "r");
621     if (f != NULL) {
622     fgets(s, 10, f);
623     xx = atoi(s);
624     kill(xx, SIGCHLD); /* meaningless kill to determine if pid
625     * is used */
626     if (errno != ESRCH) {
627     printf(EGG_RUNNING1, origbotname);
628     printf(EGG_RUNNING2, pid_file);
629     exit(1);
630     }
631     }
632     context;
633     /* move into background? */
634     if (backgrd) {
635     xx = fork();
636     if (xx == -1)
637     fatal("CANNOT FORK PROCESS.", 0);
638     if (xx != 0) {
639     FILE *fp;
640    
641     /* need to attempt to write pid now, not later */
642     unlink(pid_file);
643     fp = fopen(pid_file, "w");
644     if (fp != NULL) {
645     fprintf(fp, "%u\n", xx);
646     if (fflush(fp)) {
647     /* kill bot incase a botchk is run from crond */
648     printf(EGG_NOWRITE, pid_file);
649     printf(" Try freeing some disk space\n");
650     fclose(fp);
651     unlink(pid_file);
652     exit(1);
653     }
654     fclose(fp);
655     } else
656     printf(EGG_NOWRITE, pid_file);
657     printf("Launched into the background (pid: %d)\n\n", xx);
658     #if HAVE_SETPGID
659     setpgid(xx, xx);
660     #endif
661     exit(0);
662     }
663     }
664     use_stderr = 0; /* stop writing to stderr now */
665     xx = getpid();
666     if ((xx != 0) && (!backgrd)) {
667     FILE *fp;
668    
669     /* write pid to file */
670     unlink(pid_file);
671     fp = fopen(pid_file, "w");
672     if (fp != NULL) {
673     fprintf(fp, "%u\n", xx);
674     if (fflush(fp)) {
675     /* let the bot live since this doesn't appear to be a botchk */
676     printf(EGG_NOWRITE, pid_file);
677     fclose(fp);
678     unlink(pid_file);
679     }
680     fclose(fp);
681     } else
682     printf(EGG_NOWRITE, pid_file);
683     }
684     if (backgrd) {
685     /* ok, try to disassociate from controlling terminal */
686     /* (finger cross) */
687     #if HAVE_SETPGID
688     setpgid(0, 0);
689     #endif
690     /* close out stdin/out/err */
691     freopen("/dev/null", "r", stdin);
692     freopen("/dev/null", "w", stdout);
693     freopen("/dev/null", "w", stderr);
694     /* tcl wants those file handles kept open */
695     /* close(0); close(1); close(2); */
696     }
697     /* terminal emulating dcc chat */
698     if ((!backgrd) && (term_z)) {
699     int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
700    
701     dcc[n].addr = iptolong(getmyip());
702     dcc[n].sock = STDOUT;
703     dcc[n].timeval = now;
704     dcc[n].u.chat->con_flags = conmask;
705     dcc[n].u.chat->strip_flags = STRIP_ALL;
706     dcc[n].status = STAT_ECHO;
707     strcpy(dcc[n].nick, "HQ");
708     strcpy(dcc[n].host, "llama@console");
709     dcc[n].user = get_user_by_handle(userlist, "HQ");
710     /* make sure there's an innocuous HQ user if needed */
711     if (!dcc[n].user) {
712     adduser(userlist, "HQ", "none", "-", USER_PARTY);
713     dcc[n].user = get_user_by_handle(userlist, "HQ");
714     }
715     setsock(STDOUT, 0); /* entry in net table */
716     dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
717     dcc_chatter(n);
718     }
719     then = now;
720     online_since = now;
721     autolink_cycle(NULL); /* hurry and connect to tandem bots */
722     add_help_reference("cmds1.help");
723     add_help_reference("cmds2.help");
724     add_help_reference("core.help");
725     add_hook(HOOK_SECONDLY, core_secondly);
726     add_hook(HOOK_MINUTELY, core_minutely);
727     add_hook(HOOK_HOURLY, core_hourly);
728     add_hook(HOOK_REHASH, event_rehash);
729     add_hook(HOOK_PRE_REHASH, event_prerehash);
730     add_hook(HOOK_USERFILE, event_save);
731     add_hook(HOOK_DAILY, event_logfile);
732    
733     debug0("main: entering loop");
734     while (1) {
735     int socket_cleanup = 0;
736    
737     context;
738     /* process a single tcl event */
739     Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
740     /* lets move some of this here, reducing the numer of actual
741     * calls to periodic_timers */
742     now = time(NULL);
743     random(); /* woop, lets really jumble things */
744     if (now != then) { /* once a second */
745     call_hook(HOOK_SECONDLY);
746     then = now;
747     }
748     context;
749     /* only do this every so often */
750     if (!socket_cleanup) {
751     /* clean up sockets that were just left for dead */
752     for (i = 0; i < dcc_total; i++) {
753     if (dcc[i].type == &DCC_LOST) {
754     dcc[i].type = (struct dcc_table *) (dcc[i].sock);
755     lostdcc(i);
756     i--;
757     }
758     }
759     /* check for server or dcc activity */
760     dequeue_sockets();
761     socket_cleanup = 5;
762     } else
763     socket_cleanup--;
764     /* new net routines: help me mary! */
765     xx = sockgets(buf, &i);
766     if (xx >= 0) { /* non-error */
767     int idx;
768    
769     context;
770     for (idx = 0; idx < dcc_total; idx++)
771     if (dcc[idx].sock == xx) {
772     if (dcc[idx].type && dcc[idx].type->activity)
773     dcc[idx].type->activity(idx, buf, i);
774     else
775     putlog(LOG_MISC, "*",
776     "!!! untrapped dcc activity: type %s, sock %d",
777     dcc[idx].type->name, dcc[idx].sock);
778     break;
779     }
780     } else if (xx == -1) { /* EOF from someone */
781     int idx;
782    
783     if ((i == STDOUT) && !backgrd)
784     fatal("END OF FILE ON TERMINAL", 0);
785     context;
786     for (idx = 0; idx < dcc_total; idx++)
787     if (dcc[idx].sock == i) {
788     if (dcc[idx].type && dcc[idx].type->eof)
789     dcc[idx].type->eof(idx);
790     else {
791     putlog(LOG_MISC, "*",
792     "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
793     xx, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
794     killsock(xx);
795     lostdcc(idx);
796     }
797     idx = dcc_total + 1;
798     }
799     if (idx == dcc_total) {
800     putlog(LOG_MISC, "*",
801     "(@) EOF socket %d, not a dcc socket, not anything.",
802     xx);
803     close(xx);
804     killsock(xx);
805     }
806     } else if ((xx == -2) && (errno != EINTR)) { /* select() error */
807     context;
808     putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
809     for (i = 0; i < dcc_total; i++) {
810     if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
811     if (dcc[i].type == &DCC_LOST)
812     dcc[i].type = (struct dcc_table *) (dcc[i].sock);
813     putlog(LOG_MISC, "*",
814     "DCC socket %d (type %d, name '%s') expired -- pfft",
815     dcc[i].sock, dcc[i].type, dcc[i].nick);
816     killsock(dcc[i].sock);
817     lostdcc(i);
818     i--;
819     }
820     }
821     } else if (xx == (-3)) {
822     call_hook(HOOK_IDLE);
823     socket_cleanup = 0; /* if we've been idle, cleanup & flush */
824     }
825     if (do_restart) {
826     if (do_restart == -2)
827     rehash();
828     else {
829     /* unload as many modules as possible */
830     int f = 1;
831     module_entry *p;
832     Function x;
833     char xx[256];
834    
835     while (f) {
836     f = 0;
837     for (p = module_list; p != NULL; p = p->next) {
838     dependancy *d = dependancy_list;
839     int ok = 1;
840    
841     while (ok && d) {
842     if (d->needed == p)
843     ok = 0;
844     d = d->next;
845     }
846     if (ok) {
847     strcpy(xx, p->name);
848     if (module_unload(xx, origbotname) == NULL) {
849     f = 1;
850     break;
851     }
852     }
853     }
854     }
855     p = module_list;
856     if (p && p->next && p->next->next)
857     /* should be only 2 modules now -
858     * blowfish & eggdrop */
859     putlog(LOG_MISC, "*", MOD_STAGNANT);
860     context;
861     flushlogs();
862     context;
863     for (i = 0; i < max_logs; i++) {
864     if (logs[i].f != NULL) {
865     fclose(logs[i].f);
866     nfree(logs[i].filename);
867     nfree(logs[i].chname);
868     logs[i].filename = NULL;
869     logs[i].chname = NULL;
870     logs[i].mask = 0;
871     logs[i].f = NULL;
872     }
873     }
874     context;
875     kill_tcl();
876     init_tcl();
877     init_language(0);
878     x = p->funcs[MODCALL_START];
879     x(0);
880     rehash();
881     restart_chons();
882     }
883     do_restart = 0;
884     }
885     }
886     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23