-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.c
421 lines (352 loc) · 7.4 KB
/
irc.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include "colour.h"
#include "commands.h"
#include "config.h"
#include "hook.h"
#include "mcc.h"
#include "network.h"
#include "network_worker.h"
#include "socket.h"
#include "timer.h"
struct irc_packet_t
{
char message[500];
struct irc_packet_t *next;
};
struct irc_t
{
struct {
char *hostname;
int port;
char *name;
char *channel;
char *pass;
} settings;
int fd;
struct timer_t *timer;
int irc_stage;
struct irc_packet_t *queue;
struct irc_packet_t **queue_end;
char read_buf[2048];
char *read_pos;
};
static void irc_queue(struct irc_t *s, const char *message)
{
*s->queue_end = calloc(1, sizeof **s->queue_end);
if (*s->queue_end == NULL) {
LOG("[irc] couldn't allocate %zu bytes\n", sizeof **s->queue_end);
return;
}
strncpy((*s->queue_end)->message, message, sizeof (*s->queue_end)->message);
/* First message, flag for writing */
if (s->queue_end == &s->queue) socket_flag_write(s->fd);
s->queue_end = &(*s->queue_end)->next;
}
void irc_command(char *src, char *command)
{
/* char *bufp = command;
char *param[10];
int params = 0;
memset(param, 0, sizeof param);
for (;;)
{
size_t l = strcspn(bufp, " ,");
bool end = false;
if (bufp[l] == '\0') end = true;
bufp[l] = '\0';
param[params++] = bufp;
bufp += l + 1;
if (end) break;
}
if (!command_process(NULL, params, (const char **)param))
{
//client_notify(c, "Unknown command");
return;
}*/
}
static void irc_process(struct irc_t *s, char *message)
{
char buf[512];
char *src = NULL;
char *cmd = NULL;
char *arg = NULL;
if (message[0] == ':')
{
src = message + 1;
cmd = strchr(message, ' ');
if (cmd == NULL) return;
*cmd++ = '\0';
/* Crop source to just the nick */
char *tmp = strchr(src, '!');
if (tmp != NULL) *tmp = '\0';
}
else
{
cmd = message;
}
arg = strchr(cmd, ' ');
if (arg != NULL) *arg++ = '\0';
if (strcmp(cmd, "PING") == 0)
{
snprintf(buf, sizeof buf, "PONG %s\r\n", arg);
irc_queue(s, buf);
}
else if (strncmp(cmd, "255", 3) == 0)
{
if (s->irc_stage == 1)
{
s->irc_stage = 2;
}
}
else if (strcmp(cmd, "PRIVMSG") == 0)
{
char *notify = strchr(arg, ':');
if (notify == NULL) return;
notify++;
if (arg[0] != '#')
{
/* Not a channel message, must be a private message */
irc_command(src, notify);
}
else
{
if (strncmp(notify, "\001ACTION", 7) == 0)
{
/* Trim \001 character from end */
notify[strlen(notify) - 1] = '\0';
snprintf(buf, sizeof buf, "! " TAG_NAVY "* %s%s", src, notify + 7);
}
else
{
snprintf(buf, sizeof buf, "! " TAG_NAVY "%s:" TAG_WHITE " %s", src, notify);
}
/* Strip non-ascii characters */
int i;
for (i = 0; i < strlen(buf); i++)
{
if (buf[i] < 32 || buf[i] > 127) buf[i] = '?';
}
net_notify_all(buf);
}
}
}
static int irc_convert_colour(int colour)
{
switch (colour)
{
case '0': return 1;
case '1': return 2;
case '2': return 3;
case '3': return 10;
case '4': return 5;
case '5': return 6;
case '6': return 7;
case '7': return 15;
case '8': return 14;
case '9': return 12;
case 'a': return 9;
case 'b': return 11;
case 'c': return 4;
case 'd': return 13;
case 'e': return 8;
case 'f': return 0;
default: return colour;
}
}
static void irc_message(int hook, void *data, void *arg)
{
struct irc_t *s = arg;
char *d = data;
if (d[0] != '!') return;
char buf[512];
snprintf(buf, sizeof buf, "PRIVMSG %s :%s\r\n", s->settings.channel, (char *)data);
unsigned i;
size_t len = strlen(buf);
for (i = 0; i < len - 1; i++)
{
if (buf[i] == '&' && buf[i + 1] != ' ')
{
int col = irc_convert_colour(buf[i + 1]);
if (col == 0)
{
buf[i] = 3;
buf[i + 1] = ' ';
}
else if (col >= 10)
{
memmove(&buf[i + 2], &buf[i + 1], len - i);
buf[i] = 3;
buf[i + 1] = (col / 10) + '0';
buf[i + 2] = (col % 10) + '0';
i++;
len++;
}
else
{
buf[i] = 3;
buf[i + 1] = col + '0';
}
i++;
}
}
irc_queue(s, buf);
}
static void irc_end(struct irc_t *s)
{
LOG("[irc] Closed connection\n");
close(s->fd);
deregister_hook(&irc_message, s);
deregister_socket(s->fd);
/* Free remaining packets */
while (s->queue != NULL)
{
struct irc_packet_t *ircp = s->queue;
s->queue = s->queue->next;
free(ircp);
}
s->fd = -1;
}
static void irc_run(int fd, bool can_write, bool can_read, void *arg)
{
struct irc_t *s = arg;
char buf[513];
switch (s->irc_stage)
{
case 0:
if (s->settings.pass != NULL)
{
snprintf(buf, sizeof buf, "PASS %s\r\n", s->settings.pass);
irc_queue(s, buf);
}
snprintf(buf, sizeof buf, "NICK %s\r\n", s->settings.name);
irc_queue(s, buf);
snprintf(buf, sizeof buf, "USER %s 8 * :%s\r\n", s->settings.name, s->settings.name);
irc_queue(s, buf);
s->irc_stage = 1;
break;
case 1:
break;
case 2:
snprintf(buf, sizeof buf, "JOIN %s\r\n", s->settings.channel);
irc_queue(s, buf);
s->irc_stage = 3;
break;
}
if (can_read)
{
int res = recv(fd, s->read_pos, sizeof s->read_buf - (s->read_pos - s->read_buf) - 1, 0);
if (res == -1)
{
if (errno != EWOULDBLOCK && errno != EAGAIN)
{
LOG("[irc] recv: %s\n", strerror(errno));
irc_end(s);
}
return;
}
else if (res == 0)
{
irc_end(s);
return;
}
s->read_pos[res] = '\0';
/* Strip off new lines/carriage returns */
char *c, *bufp = s->read_buf;
while (bufp < s->read_pos + res)
{
c = strstr(bufp, "\r\n");
if (c == NULL) break;
*c = '\0';
irc_process(s, bufp);
bufp = c + 2;
}
if (bufp < s->read_pos + res)
{
memmove(s->read_buf, bufp, strlen(bufp));
s->read_pos = s->read_buf + strlen(bufp);
}
else
{
s->read_pos = s->read_buf;
}
}
if (can_write)
{
while (s->queue != NULL)
{
struct irc_packet_t *ircp = s->queue;
int res = send(fd, s->queue->message, strlen(s->queue->message), MSG_NOSIGNAL);
if (res == -1)
{
//if (errno != EWOULDBLOCK)
//{
LOG("[irc] send: %s\n", strerror(errno));
//}
return;
}
s->queue = s->queue->next;
free(ircp);
}
socket_clear_write(s->fd);
s->queue_end = &s->queue;
}
}
static void irc_connected(int fd, void *arg)
{
struct irc_t *s = arg;
if (fd != -1)
{
register_socket(fd, &irc_run, s);
s->fd = fd;
s->irc_stage = 0;
s->queue = NULL;
s->queue_end = &s->queue;
s->read_pos = s->read_buf;
socket_flag_write(s->fd);
register_hook(HOOK_CHAT, &irc_message, s);
}
}
static void irc_start(void *arg)
{
struct irc_t *s = arg;
/* Already connected? */
if (s->fd != -1) return;
network_connect(s->settings.hostname, s->settings.port, &irc_connected, s);
}
void module_init(void **arg)
{
struct irc_t *s = malloc(sizeof *s);
if (s == NULL)
{
LOG("[irc] couldn't allocate %zu bytes\n", sizeof *s);
return;
}
memset(s, 0, sizeof *s);
*arg = s;
if (!config_get_string("irc.hostname", &s->settings.hostname) ||
!config_get_int("irc.port", &s->settings.port) ||
!config_get_string("irc.name", &s->settings.name) ||
!config_get_string("irc.channel", &s->settings.channel))
{
free(s);
*arg = NULL;
return;
}
config_get_string("irc.pass", &s->settings.pass);
s->fd = -1;
s->timer = register_timer("irc", 60000, &irc_start, s, false);
}
void module_deinit(void *arg)
{
struct irc_t *s = arg;
if (s == NULL) return;
irc_end(s);
deregister_timer(s->timer);
free(s);
}