-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyak.c
140 lines (117 loc) · 2.8 KB
/
yak.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
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "auth.h"
#include "callback.h"
#include "chan.h"
#include "diagnostic.h"
#include "ircproto.h"
#include "list.h"
#include "plg.h"
#include "pref.h"
#include "privmsg.h"
#include "socket.h"
#include "thread.h"
#include "usr.h"
/* necessary for restarting process with same params */
char **argv;
char *host;
char *port;
char *ssl;
char *bot_nick;
char *bot_user;
char *bot_real;
char *nickservnick;
char *bot_identify;
char *bot_oper_name;
char *bot_oper_pw;
char **bot_channels;
char **bot_owners;
static void free_nick_owners_channels()
{
free(bot_nick);
free(bot_owners);
free(bot_channels);
}
static void load_whoiam()
{
char *owners_str, *chan_str, *tok;
int i;
host = pref_get("host");
port = pref_get("port");
ssl = pref_get("secure");
bot_nick = strdup(pref_get("nick"));
bot_user = pref_get("user");
bot_real = pref_get("real");
nickservnick = pref_get("nickserv-nick");
bot_identify = pref_get("identify-pw");
bot_oper_name = pref_get("oper-name");
bot_oper_pw = pref_get("oper-pw");
owners_str = pref_get("owners");
chan_str = pref_get("channels");
bot_owners = NULL;
for (i = 0, tok = strtok(owners_str, " "); tok; i++, tok = strtok(NULL, " ")) {
bot_owners = realloc(bot_owners, sizeof(char*) * (i + 2));
bot_owners[i] = tok;
}
bot_owners[i] = NULL;
/* move to plugin */
bot_channels = NULL;
for (i = 0, tok = strtok(chan_str, " "); tok; i++, tok = strtok(NULL, " ")) {
bot_channels = realloc(bot_channels, sizeof(char*) * (i + 2));
bot_channels[i] = tok;
}
bot_channels[i] = NULL;
add_shutdown_fn(free_nick_owners_channels);
}
int main(int argc, char **_argv)
{
char *prefix, *cmd, *params;
char **chans;
int ncmd, i;
argv = _argv;
/* lock the global mutex during program startup */
nthreads++;
mutex_on();
pref_init();
load_whoiam();
callback_init();
sock_init();
usr_init();
chan_init();
auth_init();
privmsg_init();
info("loading plugins from plugins.txt");
plg_load_plgs_from("plugins.txt");
mutex_off();
nthreads--;
/* maybe something drastic happened during startup */
if (want_quit)
goto shutdown;
establish_connection(host, port, ssl);
ircproto_nick(bot_nick);
ircproto_user(bot_user, bot_real);
if (bot_identify)
ircproto_identify(nickservnick, bot_identify);
if (bot_oper_name && bot_oper_pw)
ircproto_oper(bot_oper_name, bot_oper_pw);
while (!want_quit) {
ircproto_read_message(&prefix, &cmd, &ncmd, ¶ms);
mutex_on();
if (ncmd)
callback_emit_numeric(prefix, ncmd, params);
else
callback_emit_str(prefix, cmd, params);
if (ncmd == RPL_WELCOME) {
/* distributed on successful user registration */
for (chans = bot_channels; *chans; chans++)
ircproto_join(*chans);
}
mutex_off();
}
shutdown:
mutex_on();
safe_shutdown();
return 0;
}