-
Notifications
You must be signed in to change notification settings - Fork 0
/
eiwic.c
1571 lines (1310 loc) · 32.4 KB
/
eiwic.c
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* eiwic - Extensible Ircbot Written In C
* Copyright (C) Hannes Gr�uler <lordi@styleliga.org>
*
* eiwic.c: Main eiwic source file - IRC, connection and plugin handling
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <netdb.h>
#include <time.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include "eiwic.h"
#include "settings.h"
#include "numerics.h"
#include "connections.h"
#include "plugins.h"
#define ARGS "l:v:t:dhn"
extern char *optarg;
extern int optind;
extern int opterr;
extern int optopt;
GLOBAL *e_global;
PLUGIN *plugin;
void sigint();
void sigsegv();
void enter_vvd_section(u_char *);
void irc_login();
void banner(void);
void syntax(char *);
int init(char *);
int launch(void);
int main(int argc, char *argv[])
{
int c,
verbose = 2,
logging = 0,
daemon = 0,
noconnect = 0;
char logfile[128], testtrigger[300];
optarg = NULL;
plugin = NULL;
*logfile = 0;
*testtrigger = '\0';
banner();
while ((c = getopt(argc, argv, ARGS)) != -1)
{
switch (c)
{
case 'h':
syntax(argv[0]);
exit(1);
break;
case 'n':
noconnect = 1;
break;
case 't':
noconnect = 1;
strncpy(testtrigger, optarg, 299);
break;
case 'd':
daemon = 1;
break;
case 'l':
logging = 1;
strncpy(logfile, optarg, 127);
break;
case 'v':
verbose = atoi(optarg);
break;
default:
syntax(argv[0]);
exit(1);
break;
}
}
if (argv[optind] == NULL)
{
syntax(argv[0]);
exit(1);
}
e_global = malloc(sizeof(struct eiwic_glob));
bzero(e_global, sizeof(struct eiwic_glob));
strcpy(e_global->var_logfilename, logfile);
strcpy(e_global->var_testtrigger, testtrigger);
e_global->var_daemon = daemon;
e_global->var_log = logging;
e_global->var_verbose = verbose;
e_global->var_noconnect = noconnect;
if (init(argv[optind]) >= 0)
{
log(LOG_STATUS, "Eiwic is launching.. (%i modules)", e_global->plugins.size);
launch();
}
return 0;
}
#ifdef VV_DEBUG
void enter_vvd_section(u_char *sec)
{
if (sec)
{
strncpy(e_global->vvd_section, sec, 40);
}
else
{
*e_global->vvd_section = 0;
e_global->vvd_param = NULL;
}
}
#endif
int sigrised()
{
e_global->jump_back = 1;
return sigsetjmp(e_global->jmp_past, 1);
}
void sigfall()
{
e_global->jump_back = 0;
}
void test_send_ping(void *t)
{
plug_sendf("PING :%d\r\n", time(NULL));
plug_timer_reg(time(NULL) + 10*60, test_send_ping, NULL);
}
/* from http://www.codeproject.com */
int wildcmp(char *wild, char *string) {
char *cp, *mp;
while ((*string) && (*wild != '*')) {
if ((*wild != *string) && (*wild != '?')) {
return 0;
}
wild++;
string++;
}
while (*string) {
if (*wild == '*') {
if (!*++wild) {
return 1;
}
mp = wild;
cp = string+1;
} else if ((*wild == *string) || (*wild == '?')) {
wild++;
string++;
} else {
wild = mp;
string = cp++;
}
}
while (*wild == '*') {
wild++;
}
return !*wild;
}
void eiwic_trigger_parse(char *line, OUTPUT *out, MSG *ircmsg)
{
char *trigger_str, *trigger_arg;
PLUGIN *plugin, *p;
TRIGGER *trigger;
DListElmt *el;
OUTPUT *put = out;
trigger_str = strtok(line, " ");
if (trigger_str != NULL)
{
trigger_arg = strtok(NULL, "");
FOR_EACH(&e_global->triggers, el)
{
/* log(LOG_VERBOSE_DEBUG, "Bla %s", trigger_arg); */
trigger = el->data;
if (strcasecmp(trigger->trigger, trigger_str) == 0
&& trigger->func != NULL)
{
log(LOG_DEBUG, "Recognized trigger %s.", trigger->trigger);
if (trigger->flags == TRIG_ADMIN && ircmsg != NULL)
{
char smask[600];
sprintf(smask, "%s!%s@%s", ircmsg->nick, ircmsg->username, ircmsg->host);
log(LOG_DEBUG,
"%s: is an ADMIN trigger, checking %s against %s.", trigger->trigger,
set_get_string("admin_mask"), smask);
if (!wildcmp(set_get_string("admin_mask"), smask)) continue;
}
if (put == NULL && ircmsg != NULL)
{
if (ircmsg->channel)
{
log(LOG_DEBUG, "output will be CHANNEL '%s'", ircmsg->channel->name);
put = ircmsg->channel->output;
}
else if (ircmsg->user)
{
if (strcmp(ircmsg->cmd, "notice") == 0) {
log(LOG_DEBUG, "output will be USER '%s' [notice]", ircmsg->user->nick);
if (ircmsg->user->output_notice == NULL)
{
ircmsg->user->output_notice = output_open(OP_NOTICE, ircmsg->user);
}
put = ircmsg->user->output_notice;
}
else {
log(LOG_DEBUG, "output will be USER '%s' [query]", ircmsg->user->nick);
if (ircmsg->user->output == NULL)
{
ircmsg->user->output = output_open(OP_QUERY, ircmsg->user);
}
put = ircmsg->user->output;
}
}
}
else if (put != NULL)
{
log(LOG_DEBUG, "output will be %d", put->op_id);
}
log(LOG_DEBUG, "%s: calling "
"ep_triggers and "
"function from module %s",
trigger->trigger,
(trigger->plugin?
trigger->plugin->name:
NULL));
FOR_EACH_DATA(&e_global->plugins, p)
if (p->ep_trigger != NULL)
{
if (sigrised() == 0)
{
plugin = p;
p->ep_trigger(trigger, put, ircmsg);
plugin = NULL;
}
else
{
sigfall();
SEGWARN(p, "ep_trigger");
}
sigfall();
}
END_DATA
if (sigrised() == 0)
{
plugin = trigger->plugin;
trigger->func(trigger_arg, put, ircmsg);
plugin = NULL;
}
else
{
sigfall();
SEGWARN(trigger->plugin, "e_trigger_f");
}
sigfall();
#ifdef VV_DEBUG
log(LOG_VERBOSE_DEBUG, "ep_trigger and module func finished.");
#endif
}
}
}
/* log(LOG_VERBOSE_DEBUG, "Bla2"); */
}
void eiwic_trigger_parse_this(char *t) {
eiwic_trigger_parse(t, e_global->plugin_output, NULL);
}
void irc_parse(struct eiwic_ircmsg *ircmsg)
{
volatile DListElmt *el;
PLUGIN *plug, *p;
TRIGGER *trigger;
MSG m;
char *trigger_str, *trigger_arg;
memcpy(&m, ircmsg, sizeof(MSG));
#ifdef VV_DEBUG
enter_vvd_section("irc_parse");
if (ircmsg->is_server)
{
if (ircmsg->cmd_num > 0)
log(LOG_DEBUG, "[IRCMSG] (server) %s sends %i to %s: %s",
ircmsg->server, ircmsg->cmd_num, ircmsg->to, ircmsg->args);
else
log(LOG_DEBUG, "[IRCMSG] (server) %s sends %s to %s: %s",
ircmsg->server, ircmsg->cmd, ircmsg->to, ircmsg->args);
}
else
{
if (ircmsg->cmd_num > 0)
log(LOG_DEBUG, "[IRCMSG] %s (%s@%s) sends %i to %s: %s",
ircmsg->nick, ircmsg->username, ircmsg->host, ircmsg->cmd_num, ircmsg->to, ircmsg->args);
else
log(LOG_DEBUG, "[IRCMSG] %s (%s@%s) sends %s to %s: %s",
ircmsg->nick, ircmsg->username, ircmsg->host, ircmsg->cmd, ircmsg->to, ircmsg->args);
}
#endif
if (ircmsg->cmd_num == 0)
{
/* TODO: "nick" -> user_nickset */
if (strcmp(ircmsg->cmd, "ping") == 0 && ircmsg->is_server) {
plug_sendf("PONG :%s\r\n", ircmsg->args);
}
else
if (strcmp(ircmsg->cmd, "error") == 0) {
log(LOG_WARNING, "Error from server: %s", ircmsg->args);
}
else
if (strcmp(ircmsg->cmd, "join") == 0) {
if (strcmp(ircmsg->nick, e_global->botnick) == 0)
{
CHANNEL *chan = channel_add(ircmsg->args);
chan->joined = 1;
}
user_add(ircmsg->nick);
}
else
if (strcmp(ircmsg->cmd, "kick") == 0) {
if (strcasecmp(strtok(ircmsg->args, " "), e_global->botnick) == 0)
{
log(LOG_DEBUG, "I was kicked from %s by %s! I hate this mofo!",
ircmsg->to, ircmsg->nick, ircmsg->args);
plug_timer_reg(time(NULL) + set_get_int("rejoin_sleep"), channel_join,
channel_find(ircmsg->to));
}
}
else
if (strcmp(ircmsg->cmd, "privmsg") == 0) {
memcpy(&m, ircmsg, sizeof(struct eiwic_ircmsg));
eiwic_trigger_parse((char *)m.args, NULL, &m);
}
else
if (strcmp(ircmsg->cmd, "notice") == 0) {
memcpy(&m, ircmsg, sizeof(struct eiwic_ircmsg));
eiwic_trigger_parse((char *)m.args, NULL, &m);
}
}
else
switch (ircmsg->cmd_num)
{
case RPL_WELCOME:
strcpy(e_global->botnick, ircmsg->to);
log(LOG_STATUS, "Successfully logged into IRC server");
#ifdef VV_DEBUG
log(LOG_VERBOSE_DEBUG, "----> Server=%s; Nickname=%s", ircmsg->server, e_global->botnick);
#endif
e_global->logged_in = 1;
plug_timer_reg(time(NULL) + 100, test_send_ping, NULL);
/* Join all channels */
channel_update();
break;
case RPL_NAMREPLY:
{
char *chan, *users, *nick;
if ((chan = strtok(ircmsg->args, ":")) == NULL)
break;
if (*chan == '@' || *chan == '+')
chan += 2;
if ((users = strtok(NULL, "")) == NULL)
break;
nick = strtok(users, " ");
if (nick) do
{
if (*nick == '@' ||
*nick == '+' ||
*nick == '%' ||
*nick == '&')
nick++;
user_add(nick);
} while (nick = strtok(NULL, " "));
}
break;
case ERR_NICKNAMEINUSE:
if (e_global->logged_in == 0)
{
/* this sucks but anyway */
char buf[17];
strncpy(buf, set_get_string("nick"), 15);
sprintf(&buf[strlen(buf)>7?7:strlen(buf)], "%i", rand() % 99);
plug_sendf("NICK %s\r\n", buf);
}
break;
}
init_modules();
FOR_EACH(&e_global->plugins, el)
{
plug = el->data;
if (plug->ep_parse == NULL)
continue;
if (sigrised() == 0)
{
plugin = plug;
plugin->ep_parse(ircmsg);
plugin = NULL;
}
else
{
sigfall();
SEGWARN(plugin, "ep_parse");
}
sigfall();
}
/* Post-module-processing parsing... */
if (ircmsg->cmd_num == 0)
{
if (strcmp(ircmsg->cmd, "quit") == 0) {
user_remove(ircmsg->user);
}
else
if (strcmp(ircmsg->cmd, "nick") == 0) {
if (strcmp(ircmsg->user->nick, e_global->botnick) == 0)
{
strcpy(e_global->botnick, ircmsg->args);
}
}
else
if (strcmp(ircmsg->cmd, "part") == 0) {
if (strcmp(ircmsg->nick, e_global->botnick) == 0
&& ircmsg->channel != NULL)
{
ircmsg->channel->joined = 0;
}
}
}
}
void irc_resolve(char *buf, struct eiwic_ircmsg *ircmsg)
{
char *loc,
*from = NULL,
*to =NULL,
*command=NULL,
*args=NULL,
from_nick[40],
from_user[20],
from_host[255],
*edoff;
#ifdef VV_DEBUG
enter_vvd_section("irc_resolve");
e_global->vvd_param = buf;
#endif
bzero(ircmsg, sizeof(MSG));
bzero(from_nick, 40);
bzero(from_user, 20);
bzero(from_host, 255);
loc = (char *)malloc(strlen(buf) + 1);
strcpy(loc, buf);
if (loc[0] == ':')
{
#ifdef VV_DEBUG
enter_vvd_section("irc_resolve:chk1");
#endif
from = strtok(loc, " ") + 1;
command = strtok(NULL, " ");
to = strtok(NULL, "\r\n");
if (*to == ':')
{
args = to + 1;
to = NULL;
}
else
{
if ((args = strchr(to, ' ')) != NULL)
{
*args = 0;
args++;
if (*args == ':') args++;
}
else args = "";
}
#ifdef VV_DEBUG
enter_vvd_section("irc_resolve:chk2");
#endif
strncpy(from_nick, from, sizeof(from_nick)-1);
edoff = strchr(from_nick, '!');
if (edoff)
{
*edoff=0;
strncpy(from_user, strchr(from, '!') + 1,
sizeof(from_user)-1);
edoff = strchr(from_user, '@');
*edoff=0;
strcpy(from_host, strchr(from, '@') + 1);
}
}
else
{
args = strchr(loc, ' ') + 1;
if (args)
{
if (args[0] == ':') args++;
command = strtok(loc, " ");
strtok(args, "\r\n");
}
else
{
command = loc;
}
}
#ifdef VV_DEBUG
enter_vvd_section("irc_resolve:chk3");
#endif
if (command) ircmsg->cmd_num = atoi(command);
if (command) strcpy(ircmsg->cmd, command);
if (to)
{
CHANNEL *c;
strcpy(ircmsg->to, to);
if (c = channel_find(ircmsg->to))
{
ircmsg->channel = c;
}
}
if (args) strcpy(ircmsg->args, args);
if (strlen(from_user) == 0)
{
ircmsg->is_server = 1;
strcpy(ircmsg->server, from_nick);
}
else
{
ircmsg->is_server = 0;
strcpy(ircmsg->nick, from_nick);
strcpy(ircmsg->username, from_user);
strcpy(ircmsg->host, from_host);
ircmsg->user = user_add(ircmsg->nick);
}
strtolower(ircmsg->cmd);
free(loc);
}
void eiwic_test_trigger()
{
}
int proxy_login()
{
char buf[2048], *s;
int len, x;
sprintf(buf, "CONNECT %s:%i HTTP/1.0\r\n\r\n",
e_global->server->host, e_global->server->port);
send(e_global->var_socket, buf, strlen(buf), 0);
*buf = 0;
x = 0;
while ((len = recv(e_global->var_socket, &buf[x], 512, 0)) > 0)
{
buf[x + len] = 0;
if (strstr(buf, "\r\n\r\n"))
{
#ifdef VV_DEBUG
log(LOG_DEBUG, "%s", buf);
#endif
if (strtok(buf, " "))
{
s = strtok(NULL, " ");
if (s)
if (atoi(s) == 200)
{
return 1;
}
}
close(e_global->var_socket);
return -1;
}
x = strlen(buf);
}
close(e_global->var_socket);
return -1;
}
void irc_login()
{
plug_sendf("USER %s eiwic eiwic :%s\r\n",
set_get_string("username"),
set_get_string("realname"));
plug_sendf("NICK %s\r\n", set_get_string("nick"));
}
void eiwic_exit(int i) /* exit() wrapper */
{
CONN *conn;
log(LOG_STATUS, "Shutting down sockets and closing connections..", i);
del_conn:
FOR_EACH_DATA(&e_global->connections, conn)
{
conn_close(conn);
goto del_conn;
}
END_DATA;
log(LOG_STATUS, "Eiwic quitting (%d)..", i);
exit(i);
}
void sigsegv()
{
if (e_global->jump_back == 1)
{
plugin = NULL;
siglongjmp(e_global->jmp_past, 0);
}
log(LOG_ERROR, "Segmentation fault. Quitting.");
#ifdef VV_DEBUG
log(LOG_DEBUG, "Segfault in vvd_section: %s", e_global->vvd_section);
if (e_global->vvd_param)
log(LOG_DEBUG, "Segfault w/ vvv_param: %s", e_global->vvd_param);
#endif
eiwic_exit(1);
}
void sigint()
{
/* By pressing Ctrl-C one time, the bot will try
to disconnect from the server and quit. If the
disconnect doesn't succeed, another Ctrl-C will
make the program exit directly.
*/
if (e_global->quit == 1)
{
eiwic_exit(1);
}
else
{
e_global->quit = 1;
plug_send("QUIT :SIGINT\r\n");
plug_send(NULL);
}
}
void banner(void)
{
}
void syntax(char *argv0)
{
fprintf(stdout, "usage: %s [options] <configfile>\n", argv0);
fprintf(stdout, "options:\n");
fprintf(stdout, " -h show help\n");
fprintf(stdout, " -t '!trig' use this for testing triggers offline\n");
fprintf(stdout, " -d daemon mode, fork into background\n");
fprintf(stdout, " -l file message log into a file (if file is \"-\", it will be\n");
fprintf(stdout, " printed to stdout)\n");
fprintf(stdout, " -v level verbosity level to log messages, default = 1\n");
fprintf(stdout, " 0 = only log errors\n");
fprintf(stdout, " 1 = also log warnings\n");
fprintf(stdout, " 2 = also log normal status messages\n");
fprintf(stdout, " 3 = also log debug messages\n");
fprintf(stdout, " 4 = also log debug messages for initialization\n");
fprintf(stdout, " 5 = also log very verbose debug messages\n");
fprintf(stdout, " ('./configure --vv-debug' required)\n\n");
fprintf(stdout, "examples:\n");
fprintf(stdout, "1) Interesting output to stdout:\n");
fprintf(stdout, " %s -l - -v 2 bot.conf\n", argv0);
fprintf(stdout, "2) Background mode with logfile:\n");
fprintf(stdout, " %s -l eiwic_error.log -d bot.conf\n", argv0);
fprintf(stdout, "3) Debug bot:\n");
fprintf(stdout, " %s -l - -v 10 bot.conf\n", argv0);
fprintf(stdout, "4) Debug your trigger without connecting to a server:\n");
fprintf(stdout, " %s -l - -v 3 bot.conf -t '!say rockn roll'\n", argv0);
fprintf(stdout, "5) Spit out nothing else then the trigger output:\n");
fprintf(stdout, " %s -l - -v 0 bot.conf -t '!say rockn roll'\n", argv0);
fflush(stdout);
}
int init_modules()
{
int err = 0;
PLUGIN *p;
DListElmt *el;
if (e_global->mod_need_init == 1)
{
log(LOG_STATUS, "Initializing modules..");
FOR_EACH(&e_global->plugins, el)
{
p = el->data;
if (p->ep_init && p->inited == 0)
{
e_global->jump_back = 1;
if (sigsetjmp(e_global->jmp_past, 1) == 0)
{
#ifdef VV_DEBUG
log(LOG_VERBOSE_DEBUG, "module(%s) %i,%i", p->name, p, p->ep_init);
#endif
plugin = p;
if (p->ep_init(e_global, p, p->init_out) == 0)
{
log(LOG_ERROR, "Initialization of %s failed.", p->name);
err++;
plugin = NULL;
/* todo: unload modul */
break;
}
p->inited = 1;
plugin = NULL;
}
else
{
e_global->jump_back = 0;
SEGWARN(p, "ep_init");
/* todo: unload modul */
}
e_global->jump_back = 0;
}
}
}
e_global->mod_need_init = 0;
}
int init(char *conf)
{
int err = 0;
PLUGIN *p;
DListElmt *el;
MODLINK *link;
#ifdef VV_DEBUG
enter_vvd_section("init");
#endif
srand(time(NULL));
signal(SIGHUP,SIG_IGN);
signal(SIGPIPE,SIG_IGN);
signal(SIGINT, sigint);
signal(SIGSEGV, sigsegv);
if (e_global->var_log)
{
if (strcmp(e_global->var_logfilename, "-") == 0)
{
strcpy(e_global->var_logfilename, "stdout");
e_global->var_logfile = stdout;
}
else
{
e_global->var_logfile = fopen(e_global->var_logfilename, "a");
}
if (e_global->var_logfile == NULL)
{
err++;
perror("Unable to open log file");
goto abort;
}
}
log(LOG_STATUS, "Initializing eiwic..");
dlist_init(&e_global->plugins, plug_destroy);
dlist_init(&e_global->triggers, plug_trigger_destroy);
dlist_init(&e_global->timers, plug_timer_destroy);
dlist_init(&e_global->connections, conn_destroy);
dlist_init(&e_global->channels, channel_destroy);
dlist_init(&e_global->servers, server_destroy);
dlist_init(&e_global->settings, set_destroy);
dlist_init(&e_global->users, user_destroy);
dlist_init(&e_global->outputs, output_destroy);
e_global->log_output = output_open(OP_LOG, LOG_STATUS);
link = malloc(sizeof(MODLINK));
link->set_add = set_add;
link->set_find = set_find;
link->set_set = set_set;
link->set_is = set_is;
link->set_get_int = set_get_int;
link->set_get_float = set_get_float;
link->set_get_string = set_get_string;
link->set_remove = set_remove;
link->plug_load = plug_load;
link->plug_add = plug_add;
link->plug_unload = plug_unload;
link->plug_remove = plug_remove;
link->plug_destroy = plug_destroy;
link->plug_trigger_reg = plug_trigger_reg;
link->plug_trigger_destroy = plug_trigger_destroy;
link->plug_trigger_unreg = plug_trigger_unreg;
link->plug_timer_reg = plug_timer_reg;
link->plug_timer_destroy = plug_timer_destroy;
link->plug_timer_unreg = plug_timer_unreg;
link->plug_timers = plug_timers;
link->conn_http_get = conn_http_get;
link->conn_unreg = conn_unreg;
link->conn_connect = conn_connect;
link->conn_destroy = conn_destroy;
link->conn_close = conn_close;
link->conn_listen = conn_listen;
link->conn_accept = conn_accept;
link->server_add = server_add;
link->server_destroy = server_destroy;
link->server_remove = server_remove;
link->channel_add = channel_add;
link->channel_remove = channel_remove;
link->channel_destroy = channel_destroy;
link->channel_join = channel_join;
link->channel_find = channel_find;
link->user_add =user_add;
link->user_nickset =user_nickset;
link->user_remove =user_remove;
link->user_destroy =user_destroy;
link->user_find =user_find;
link->output_open = output_open;
link->output_print = output_print;
link->output_tostring = output_tostring;
link->output_printf = output_printf;
link->output_close = output_close;
link->output_destroy = output_destroy;
link->plug_send = plug_send;
link->plug_sendf = plug_sendf;
link->log = log;
link->plog = plog;
link->findip = findip;
#ifdef IPV6
link->findip6 = findip6;
#endif
e_global->modlink = (void *)link;
set_add(SET_STRING, "config_file", conf);
set_add(SET_INT, "connect_sleep", 10);
set_add(SET_INT, "exhaused_sleep", 40);
set_add(SET_INT, "rejoin_sleep", 7);
set_add(SET_STRING, "nick", "");
set_add(SET_STRING, "username", "eiwic");
set_add(SET_STRING, "realname", "the splitrider's bot");
set_add(SET_STRING, "admin_mask", ""); /* no admin by default */
set_add(SET_STRING, "vhost", "");
set_add(SET_STRING, "proxy_host", "");
set_add(SET_INT, "proxy_port", 3128);
set_add(SET_STRING, "module_path", "./");
if (conf_loadfile() == -1)
{
err++;
goto abort;
}
init_modules();
if (set_is("proxy_host"))
{
e_global->proxy = malloc(sizeof(SERVER));
bzero(e_global->proxy, sizeof(SERVER));
strncpy(e_global->proxy->host, set_get_string("proxy_host"), 127);
e_global->proxy->port = set_get_int("proxy_port");
}
else
{
e_global->proxy = NULL;
}
e_global->server_input = output_open(OP_SERVER_INPUT);
if (e_global->plugins.size <= 0)
{
log(LOG_WARNING, "No modules has been added!");
}
if (e_global->channels.size <= 0)
{
log(LOG_WARNING, "No channel has been added!");
}
if (e_global->servers.size <= 0)
{
err++;
log(LOG_ERROR, "No server has been added!");
}