summaryrefslogtreecommitdiff
path: root/src/init.c
blob: 29e895f8393fa6976308ca33d40b831354fb9ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/* Initalization and related routines.
 *
 * (C) 2003-2011 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church. 
 * 
 *
 */

#include "services.h"
#include "pseudo.h"
int servernum = 0;

extern void moduleAddMsgs(void);
extern void moduleAddIRCDMsgs(void);

/*************************************************************************/

void introduce_user(const char *user)
{

    /* Watch out for infinite loops... */
#define LTSIZE 20
    static int lasttimes[LTSIZE];
    if (lasttimes[0] >= time(NULL) - 3)
        fatal("introduce_user() loop detected");
    memmove(lasttimes, lasttimes + 1, sizeof(lasttimes) - sizeof(int));
    lasttimes[LTSIZE - 1] = time(NULL);
#undef LTSIZE

    /* Introduce OperServ first because on some IRCd's send
     * we send data from OperServ before introduction completes.
     * Patch fixing ratbox RESV support provided by Jobe. */
    if (!user || stricmp(user, s_OperServ) == 0) {
        anope_cmd_nick(s_OperServ, desc_OperServ, ircd->operservmode);
    }

    /* NickServ */
    if (!user || stricmp(user, s_NickServ) == 0) {
        anope_cmd_nick(s_NickServ, desc_NickServ, ircd->nickservmode);
    }

    /* ChanServ */
    if (!user || stricmp(user, s_ChanServ) == 0) {
        anope_cmd_nick(s_ChanServ, desc_ChanServ, ircd->chanservmode);
    }
    if (s_HostServ && ircd->vhost
        && (!user || stricmp(user, s_HostServ) == 0)) {
        anope_cmd_nick(s_HostServ, desc_HostServ, ircd->hostservmode);
    }

    if (!user || stricmp(user, s_MemoServ) == 0) {
        anope_cmd_nick(s_MemoServ, desc_MemoServ, ircd->memoservmode);
    }

    if (s_BotServ && (!user || stricmp(user, s_BotServ) == 0)) {
        anope_cmd_nick(s_BotServ, desc_BotServ, ircd->botservmode);
    }

    if (!user || stricmp(user, s_HelpServ) == 0) {
        anope_cmd_nick(s_HelpServ, desc_HelpServ, ircd->helpservmode);
    }

    if (s_DevNull && (!user || stricmp(user, s_DevNull) == 0)) {
        anope_cmd_nick(s_DevNull, desc_DevNull, ircd->devnullmode);
    }

    if (!user || stricmp(user, s_GlobalNoticer) == 0) {
        anope_cmd_nick(s_GlobalNoticer, desc_GlobalNoticer,
                       ircd->globalmode);
    }

    /* We make aliases go online */
    if (s_NickServAlias && (!user || stricmp(user, s_NickServAlias) == 0)) {
        anope_cmd_nick(s_NickServAlias, desc_NickServAlias,
                       ircd->nickservaliasmode);
    }

    if (s_ChanServAlias && (!user || stricmp(user, s_ChanServAlias) == 0)) {
        anope_cmd_nick(s_ChanServAlias, desc_ChanServAlias,
                       ircd->chanservaliasmode);
    }

    if (s_MemoServAlias && (!user || stricmp(user, s_MemoServAlias) == 0)) {
        anope_cmd_nick(s_MemoServAlias, desc_MemoServAlias,
                       ircd->memoservaliasmode);
    }

    if (s_BotServAlias && (!user || stricmp(user, s_BotServAlias) == 0)) {
        anope_cmd_nick(s_BotServAlias, desc_BotServAlias,
                       ircd->botservaliasmode);
    }

    if (s_HelpServAlias && (!user || stricmp(user, s_HelpServAlias) == 0)) {
        anope_cmd_nick(s_HelpServAlias, desc_HelpServAlias,
                       ircd->helpservaliasmode);
    }

    if (s_OperServAlias && (!user || stricmp(user, s_OperServAlias) == 0)) {
        anope_cmd_nick(s_OperServAlias, desc_OperServAlias,
                       ircd->operservaliasmode);
    }

    if (s_DevNullAlias && (!user || stricmp(user, s_DevNullAlias) == 0)) {
        anope_cmd_nick(s_DevNullAlias, desc_DevNullAlias,
                       ircd->devnullvaliasmode);
    }
    if (s_HostServAlias && ircd->vhost
        && (!user || stricmp(user, s_HostServAlias) == 0)) {
        anope_cmd_nick(s_HostServAlias, desc_HostServAlias,
                       ircd->hostservaliasmode);
    }
    if (s_GlobalNoticerAlias
        && (!user || stricmp(user, s_GlobalNoticerAlias) == 0)) {
        anope_cmd_nick(s_GlobalNoticerAlias, desc_GlobalNoticerAlias,
                       ircd->globalaliasmode);
    }

    /* We make the bots go online */
    if (s_BotServ) {
        BotInfo *bi;
        int i;

        for (i = 0; i < 256; i++)
            for (bi = botlists[i]; bi; bi = bi->next) {

                if (!user || !stricmp(user, bi->nick))
                    anope_cmd_bot_nick(bi->nick, bi->user, bi->host,
                                       bi->real, ircd->botserv_bot_mode);
            }
    }
}

/*************************************************************************/

/* Set GID if necessary.  Return 0 if successful (or if RUNGROUP not
 * defined), else print an error message to logfile and return -1.
 */

static int set_group(void)
{
#if defined(RUNGROUP) && defined(HAVE_SETGRENT)
    struct group *gr;

    setgrent();
    while ((gr = getgrent()) != NULL) {
        if (strcmp(gr->gr_name, RUNGROUP) == 0)
            break;
    }
    endgrent();
    if (gr) {
        setgid(gr->gr_gid);
        return 0;
    } else {
        alog("Unknown group `%s'\n", RUNGROUP);
        return -1;
    }
#else
    return 0;
#endif
}

/*************************************************************************/

/* Parse command-line options for the "-dir" option only.  Return 0 if all
 * went well or -1 for a syntax error.
 */

/* XXX this could fail if we have "-some-option-taking-an-argument -dir" */

static int parse_dir_options(int ac, char **av)
{
    int i;
    char *s;

    for (i = 1; i < ac; i++) {
        s = av[i];
        if (*s == '-') {
            s++;
            if (strcmp(s, "dir") == 0) {
                if (++i >= ac) {
                    fprintf(stderr, "-dir requires a parameter\n");
                    return -1;
                }
                services_dir = av[i];
            } else if (strcmp(s, "log") == 0) {
                if (++i >= ac) {
                    fprintf(stderr, "-log requires a parameter\n");
                    return -1;
                }
                log_filename = av[i];
            } else if (strcmp(s, "version") == 0) {
                fprintf(stdout, "Anope-%s %s -- %s\n", version_number,
                        version_flags, version_build);
                exit(EXIT_SUCCESS);
            }
        }
    }
    return 0;
}

/*************************************************************************/

/* Parse command-line options.  Return 0 if all went well, -1 for an error
 * with an option, or 1 for -help.
 */

static int parse_options(int ac, char **av)
{
    int i;
    char *s, *t;

    for (i = 1; i < ac; i++) {
        s = av[i];
        if (*s == '-') {
            s++;
            if (strcmp(s, "remote") == 0) {
                if (++i >= ac) {
                    fprintf(stderr, "-remote requires hostname[:port]\n");
                    return -1;
                }
                s = av[i];
                t = strchr(s, ':');
                if (t) {
                    int portnum;
                    *t++ = 0;
                    portnum = atoi(t);
                    if ((portnum > 0) && (portnum < 65535))
                        RemotePort = portnum;
                    else {
                        fprintf(stderr,
                                "-remote: Port numbers must be in the range 1..65535. Using default.\n");
                        return -1;
                    }
                }
                RemoteServer = s;
            } else if (strcmp(s, "local") == 0) {
                if (++i >= ac) {
                    fprintf(stderr,
                            "-local requires hostname or [hostname]:[port]\n");
                    return -1;
                }
                s = av[i];
                t = strchr(s, ':');
                if (t) {
                    int portnum;
                    *t++ = 0;
                    portnum = atoi(t);
                    if ((portnum >= 0) && (portnum < 65535))
                        LocalPort = portnum;
                    else {
                        fprintf(stderr,
                                "-local: Port numbers must be in the range 1..65535 or 0. Using default.\n");
                        return -1;
                    }
                }
                LocalHost = s;
            } else if (strcmp(s, "name") == 0) {
                if (++i >= ac) {
                    fprintf(stderr, "-name requires a parameter\n");
                    return -1;
                }
                ServerName = av[i];
            } else if (strcmp(s, "desc") == 0) {
                if (++i >= ac) {
                    fprintf(stderr, "-desc requires a parameter\n");
                    return -1;
                }
                ServerDesc = av[i];
            } else if (strcmp(s, "user") == 0) {
                if (++i >= ac) {
                    fprintf(stderr, "-user requires a parameter\n");
                    return -1;
                }
                ServiceUser = av[i];
            } else if (strcmp(s, "host") == 0) {
                if (++i >= ac) {
                    fprintf(stderr, "-host requires a parameter\n");
                    return -1;
                }
                ServiceHost = av[i];
            } else if (strcmp(s, "dir") == 0) {
                /* Handled by parse_dir_options() */
                i++;            /* Skip parameter */
            } else if (strcmp(s, "log") == 0) {
                /* Handled by parse_dir_options(), too */
                i++;            /* Skip parameter */
            } else if (strcmp(s, "update") == 0) {
                if (++i >= ac) {
                    fprintf(stderr, "-update requires a parameter\n");
                    return -1;
                }
                s = av[i];
                if (atoi(s) <= 0) {
                    fprintf(stderr,
                            "-update: number of seconds must be positive");
                    return -1;
                } else
                    UpdateTimeout = atol(s);
            } else if (strcmp(s, "expire") == 0) {
                if (++i >= ac) {
                    fprintf(stderr, "-expire requires a parameter\n");
                    return -1;
                }
                s = av[i];
                if (atoi(s) <= 0) {
                    fprintf(stderr,
                            "-expire: number of seconds must be positive\n");
                    return -1;
                } else
                    ExpireTimeout = atol(s);
            } else if (strcmp(s, "debug") == 0) {
                debug++;
            } else if (strcmp(s, "readonly") == 0) {
                readonly = 1;
                skeleton = 0;
            } else if (strcmp(s, "skeleton") == 0) {
                readonly = 0;
                skeleton = 1;
            } else if (strcmp(s, "nofork") == 0) {
                nofork = 1;
            } else if (strcmp(s, "logchan") == 0) {
                if (!LogChannel) {
                    fprintf(stderr,
                            "-logchan: LogChannel must be defined in services.conf\n");
                } else {        /* LogChannel */

                    logchan = 1;
                }
            } else if (strcmp(s, "forceload") == 0) {
                forceload = 1;
            } else if (strcmp(s, "nothird") == 0) {
                nothird = 1;
            } else if (strcmp(s, "protocoldebug") == 0) {
                protocoldebug = 1;
            } else if (strcmp(s, "support") == 0) {
                nofork = 1;
                debug++;
                nothird = 1;
            } else if (!strcmp(s, "noexpire")) {
                noexpire = 1;
            } else if (!strcmp(s, "help")) {
                fprintf(stdout, "Anope-%s %s -- %s\n", version_number,
                        version_flags, version_build);
                fprintf(stdout,
                        "Anope IRC Services (http://www.anope.org)\n");
                fprintf(stdout, "Usage ./services [options] ...\n");
                fprintf(stdout,
                        "-remote        -remote hostname[:port]\n");
                fprintf(stdout, "-local         -local hostname[:port]\n");
                fprintf(stdout, "-name          -name servername\n");
                fprintf(stdout, "-desc          -desc serverdesc\n");
                fprintf(stdout, "-user          -user serviceuser\n");
                fprintf(stdout, "-host          -host servicehost\n");
                fprintf(stdout,
                        "-update        -update updatetime(secs)\n");
                fprintf(stdout,
                        "-expire        -expire expiretime(secs)\n");
                fprintf(stdout, "-debug         -debug\n");
                fprintf(stdout, "-protocoldebug -protocoldebug\n");
                fprintf(stdout, "-nofork        -nofork\n");
                fprintf(stdout, "-logchan       -logchan channelname\n");
                fprintf(stdout, "-skeleton      -skeleton\n");
                fprintf(stdout, "-forceload     -forceload\n");
                fprintf(stdout, "-nothird       -nothird\n");
                fprintf(stdout, "-support       -support\n");
                fprintf(stdout, "-readonly      -readonly\n");
                fprintf(stdout, "-noexpire      -noexpire\n");
                fprintf(stdout, "-version       -version\n");
                fprintf(stdout, "-help          -help\n");
                fprintf(stdout, "-log           -log logfilename\n");
                fprintf(stdout,
                        "-dir           -dir servicesdirectory\n\n");
                fprintf(stdout,
                        "Further support is available from http://www.anope.org\n");
                fprintf(stdout,
                        "Or visit US on IRC at irc.anope.org #anope\n");
                exit(EXIT_SUCCESS);
            } else {
                fprintf(stderr, "Unknown option -%s\n", s);
                return -1;
            }
        } else {
            fprintf(stderr, "Non-option arguments not allowed\n");
            return -1;
        }
    }
    return 0;
}

/*************************************************************************/

/* Remove our PID file.  Done at exit. */

static void remove_pidfile(void)
{
    remove(PIDFilename);
}

/*************************************************************************/

/* Create our PID file and write the PID to it. */

static void write_pidfile(void)
{
    FILE *pidfile;

    pidfile = fopen(PIDFilename, "w");
    if (pidfile) {
        fprintf(pidfile, "%d\n", (int) getpid());
        fclose(pidfile);
        atexit(remove_pidfile);
    } else {
        log_perror("Warning: cannot write to PID file %s", PIDFilename);
    }
}

/*************************************************************************/

/* Overall initialization routines.  Return 0 on success, -1 on failure. */

int openlog_failed = 0, openlog_errno = 0;

int init_primary(int ac, char **av)
{
    int started_from_term = isatty(0) && isatty(1) && isatty(2);

    /* Set file creation mask and group ID. */
#if defined(DEFUMASK) && HAVE_UMASK
    umask(DEFUMASK);
#endif
    if (set_group() < 0)
        return -1;

    /* Parse command line for -dir and -version options. */
    parse_dir_options(ac, av);

    /* Chdir to Services data directory. */
    if (chdir(services_dir) < 0) {
        fprintf(stderr, "chdir(%s): %s\n", services_dir, strerror(errno));
        return -1;
    }

    /* Open logfile, and complain if we didn't. */
    if (open_log() < 0) {
        openlog_errno = errno;
        if (started_from_term) {
            fprintf(stderr, "Warning: unable to open log file %s: %s\n",
                    log_filename, strerror(errno));
        } else {
            openlog_failed = 1;
        }
    }

    /* Read configuration file; exit if there are problems. */
    if (!read_config(0)) {
        return -1;
    }

    /* Add IRCD Protocol Module; exit if there are errors */
    if (protocol_module_init()) {
        return -1;
    }

    /* Add Encryption Module; exit if there are errors */
    if (encryption_module_init()) {
        return -1;
    }
    return 0;
}

int init_secondary(int ac, char **av)
{
    int i;
    int started_from_term = isatty(0) && isatty(1) && isatty(2);

    /* Add Core MSG handles */
    moduleAddMsgs();

    /* Parse all remaining command-line options. */
    parse_options(ac, av);

    /* Parse the defcon mode string if needed */
    if (DefConLevel && !ircd->delay_cl_intro) {
        if (!defconParseModeString(DefConChanModes)) {
            fprintf(stderr,
                    "services.conf: The given DefConChanModes mode string was incorrect (see log for exact errors)\n");
            return -1;
        }
    }
#ifndef _WIN32
    if (!nofork) {
        if ((i = fork()) < 0) {
            perror("fork()");
            return -1;
        } else if (i != 0) {
            exit(0);
        }
        if (started_from_term) {
            close(0);
            close(1);
            close(2);
        }
        if (setpgid(0, 0) < 0) {
            perror("setpgid()");
            return -1;
        }
    }
#else
    /* Initialize winsocks -- codemastr */
    {
        WSADATA wsa;
        if (WSAStartup(MAKEWORD(1, 1), &wsa)) {
            alog("Failed to initialized WinSock library");
            return -1;
        }
    }
    if (!SupportedWindowsVersion()) {

        char *winver = GetWindowsVersion();

        alog("%s is not a supported version of Windows", winver);

        free(winver);

        return -1;

    }
    if (!nofork) {
        alog("Launching Anope into the background");
        FreeConsole();
    }
#endif

    /* Write our PID to the PID file. */
    write_pidfile();

    /* Announce ourselves to the logfile. */
    if (debug || readonly || skeleton) {
        alog("Anope %s (ircd protocol: %s) starting up (options:%s%s%s)",
             version_number, version_protocol,
             debug ? " debug" : "", readonly ? " readonly" : "",
             skeleton ? " skeleton" : "");
    } else {
        alog("Anope %s (ircd protocol: %s) starting up",
             version_number, version_protocol);
    }
    start_time = time(NULL);



    /* If in read-only mode, close the logfile again. */
    if (readonly)
        close_log();

    /* Set signal handlers.  Catch certain signals to let us do things or
     * panic as necessary, and ignore all others.
     */

#ifndef _WIN32
#if defined(NSIG)
    for (i = 1; i <= NSIG - 1; i++) {
#else
    for (i = 1; i <= 31; i++) {
#endif
        signal(i, SIG_IGN);
    }
#else
    /* work around to bug #527 */
    signal(SIGILL, SIG_IGN);
    signal(SIGBREAK, SIG_IGN);
    signal(SIGABRT, SIG_IGN);
#endif

    signal(SIGINT, sighandler);
    signal(SIGTERM, sighandler);
#ifndef _WIN32
    signal(SIGQUIT, sighandler);
#endif
    if (!DumpCore) {
        signal(SIGSEGV, sighandler);
#ifndef _WIN32
        signal(SIGBUS, sighandler);
        signal(SIGTRAP, sighandler);
#endif
    } else {
        signal(SIGSEGV, SIG_DFL);
#ifndef _WIN32
        signal(SIGBUS, sighandler);
        signal(SIGTRAP, sighandler);
#endif
    }
#ifndef _WIN32
    signal(SIGQUIT, sighandler);
    signal(SIGHUP, sighandler);
    signal(SIGUSR2, sighandler);
#endif

#ifdef SIGIOT
    signal(SIGIOT, sighandler);
#endif
    signal(SIGFPE, sighandler);

#ifndef _WIN32
    signal(SIGUSR1, sighandler);        /* This is our "out-of-memory" panic switch */
#endif

    /* Initialize multi-language support */
    lang_init();
    if (debug)
        alog("debug: Loaded languages");


    /* Initialize subservices */
    ns_init();
    cs_init();
    ms_init();
    bs_init();
    os_init();
    hostserv_init();
    helpserv_init();

#ifdef USE_RDB
    if (!rdb_init()) {
        if (UseRDB) {
            UseRDB = 0;
            alog("Error: Disabling UseRDB due to errors with SQL");
        }
    } else {
        if (MysqlSecure && UseRDB) {
            UseRDB = 0;
            alog("Error: MySQL password are encrypted using method in MysqlSecure disabling UseRDB");
        }
    }
#endif

    /* load any custom modules */
    modules_init();

    /* Initialize random number generator */
    rand_init();
    add_entropy_userkeys();

    /* Load up databases */
#ifdef USE_RDB
    if (UseRDB)
        rdb_load_dbases();
    /* Need a better way to handle this -dane */
    if (!UseRDB) {
#endif
        if (!skeleton) {
            load_ns_dbase();
            if (debug)
                alog("debug: Loaded %s database (1/%d)", s_NickServ,
                     (PreNickDBName ? 8 : 7));
            if (s_HostServ) {
                load_hs_dbase();
                if (debug)
                    alog("debug: Loaded %s database (2/%d)", s_HostServ,
                         (PreNickDBName ? 8 : 7));
            } else if (debug) {
                alog("debug: HostServ database (2/%d) not loaded because HostServ is disabled", (PreNickDBName ? 8 : 7));
            }
            if (s_BotServ) {
                load_bs_dbase();
                if (debug)
                    alog("debug: Loaded %s database (3/%d)", s_BotServ,
                         (PreNickDBName ? 8 : 7));
            } else if (debug) {
                alog("debug: BotServ database (3/%d) not loaded because BotServ is disabled", (PreNickDBName ? 8 : 7));
            }
            load_cs_dbase();
            if (debug)
                alog("debug: Loaded %s database (4/%d)", s_ChanServ,
                     (PreNickDBName ? 8 : 7));
        }
        load_os_dbase();
        if (debug)
            alog("debug: Loaded %s database (5/%d)", s_OperServ,
                 (PreNickDBName ? 8 : 7));
        load_news();
        if (debug)
            alog("debug: Loaded news database (6/%d)",
                 (PreNickDBName ? 8 : 7));
        load_exceptions();
        if (debug)
            alog("debug: Loaded exception database (7/%d)",
                 (PreNickDBName ? 8 : 7));
        if (PreNickDBName) {
            load_ns_req_db();
            if (debug)
                alog("debug: Loaded PreNick database (8/8)");
        }
#ifdef USE_RDB
    }
#endif
    alog("Databases loaded");

    /* Save the databases back to file/mysql to reflect any changes */
#ifdef USE_RDB
    if (!UseRDB) {              /* Only save if we are not using remote databases
                                 * to avoid floods. As a side effects our nice
                                 * FFF databases won't get overwritten if the
                                 * mysql db is broken (empty etc.) */
#endif
        alog("Info: Reflecting database records.");
        save_databases();
#ifdef USE_RDB
    } else {
        alog("Info: Not reflecting database records.");
    }
#endif
    send_event(EVENT_CONNECT, 1, EVENT_START);

    /* Connect to the remote server */
    servsock = conn(RemoteServer, RemotePort, LocalHost, LocalPort);
    if (servsock < 0 && RemoteServer2) {
        servsock = conn(RemoteServer2, RemotePort2, LocalHost, LocalPort);
        if (servsock < 0 && RemoteServer3) {
            servsock =
                conn(RemoteServer3, RemotePort3, LocalHost, LocalPort);
            if (servsock < 0) {
                fatal_perror("Can't connect to server");
            } else {
                servernum = 3;
                alog("Connected to Server %d (%s:%d)", servernum,
                     RemoteServer3, RemotePort3);
            }
        } else {
            if (servsock < 0) {
                fatal_perror("Can't connect to server");
            }
            servernum = 2;
            alog("Connected to Server %d (%s:%d)", servernum,
                 RemoteServer2, RemotePort2);
        }
    } else {
        if (servsock < 0) {
            fatal_perror("Can't connect to server");
        }
        servernum = 1;
        alog("Connected to Server %d (%s:%d)", servernum, RemoteServer,
             RemotePort);
    }

    anope_cmd_connect(servernum);
    send_event(EVENT_CONNECT, 1, EVENT_STOP);

    sgets2(inbuf, sizeof(inbuf), servsock);
    if (strnicmp(inbuf, "ERROR", 5) == 0) {
        /* Close server socket first to stop wallops, since the other
         * server doesn't want to listen to us anyway */
        disconn(servsock);
        servsock = -1;
        fatal("Remote server returned: %s", inbuf);
    }

    /* Announce a logfile error if there was one */
    if (openlog_failed) {
        anope_cmd_global(NULL, "Warning: couldn't open logfile: %s",
                         strerror(openlog_errno));
    }

    if (!ircd->delay_cl_intro)
        init_tertiary();

    /* Success! */
    return 0;
}

/*************************************************************************/

void init_tertiary()
{
    /* Begin our burst..*/
    anope_cmd_bob();

    /* Bring in our pseudo-clients */
    introduce_user(NULL);

    /* And hybrid needs Global joined in the logchan */
    if (logchan && ircd->join2msg) {
        /* XXX might desync */
        anope_cmd_join(s_GlobalNoticer, LogChannel, time(NULL));
    }

    /* End our burst.. */
    anope_cmd_eob();

    /**
     * Load our delayed modeles - modules that are planing on making clients need to wait till now
     * where as modules wanting to modify our ircd connection messages need to load eariler :|
     **/
    modules_delayed_init();
}

/*************************************************************************/