1 | //irc.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2012 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include <roard/include/roard.h> |
---|
27 | #include <ctype.h> |
---|
28 | |
---|
29 | #define MAX_CHANNELS 8 |
---|
30 | |
---|
31 | static int on_nick(int client, const char * cmd, char * args, char * text); |
---|
32 | static int on_user(int client, const char * cmd, char * args, char * text); |
---|
33 | static int on_quit(int client, const char * cmd, char * args, char * text); |
---|
34 | static int on_ping(int client, const char * cmd, char * args, char * text); |
---|
35 | static int on_lusers(int client, const char * cmd, char * args, char * text); |
---|
36 | static int on_whois(int client, const char * cmd, char * args, char * text); |
---|
37 | static int on_privmsg(int client, const char * cmd, char * args, char * text); |
---|
38 | |
---|
39 | static int on_list(int client, const char * cmd, char * args, char * text); |
---|
40 | static int on_join(int client, const char * cmd, char * args, char * text); |
---|
41 | static int on_part(int client, const char * cmd, char * args, char * text); |
---|
42 | static int on_names(int client, const char * cmd, char * args, char * text); |
---|
43 | |
---|
44 | static int on_topic(int client, const char * cmd, char * args, char * text); |
---|
45 | |
---|
46 | static int do_join(int client, const char * channel); |
---|
47 | static int do_part(int client, const char * channel); |
---|
48 | static int do_names(int client, const char * channel); |
---|
49 | |
---|
50 | static void cb_client_delete(struct roar_notify_core * core, struct roar_event * event, void * userdata); |
---|
51 | |
---|
52 | static struct roar_subscriber * subscription_client_delete = NULL; |
---|
53 | |
---|
54 | static char * server_name = "irc.roard"; |
---|
55 | static char * server_fullname = "RoarAudio roard IRC Server plugin"; |
---|
56 | |
---|
57 | static char * quit_msg = NULL; |
---|
58 | |
---|
59 | static struct channel { |
---|
60 | char * name; |
---|
61 | struct { |
---|
62 | char * text; |
---|
63 | char * user; |
---|
64 | time_t ts; |
---|
65 | } topic; |
---|
66 | size_t client_count; |
---|
67 | int clients[ROAR_CLIENTS_MAX]; |
---|
68 | } g_channels[MAX_CHANNELS]; |
---|
69 | |
---|
70 | static struct command { |
---|
71 | const char * name; |
---|
72 | int (*func)(int client, const char * cmd, char * args, char * text); |
---|
73 | } g_commands[] = { |
---|
74 | {"NICK", on_nick}, |
---|
75 | {"USER", on_user}, |
---|
76 | {"QUIT", on_quit}, |
---|
77 | {"PING", on_ping}, |
---|
78 | {"LUSERS", on_lusers}, |
---|
79 | {"WHOIS", on_whois}, |
---|
80 | {"PRIVMSG", on_privmsg}, |
---|
81 | {"NOTICE", on_privmsg}, |
---|
82 | {"LIST", on_list}, |
---|
83 | {"JOIN", on_join}, |
---|
84 | {"PART", on_part}, |
---|
85 | {"NAMES", on_names}, |
---|
86 | {"TOPIC", on_topic}, |
---|
87 | {NULL, NULL} |
---|
88 | }; |
---|
89 | |
---|
90 | static int is_valid_name (const char * name) { |
---|
91 | register char c; |
---|
92 | |
---|
93 | for (; (c = *name) != 0; name++) { |
---|
94 | if ( !(isalnum(c) || c == '-' || c == '_') ) |
---|
95 | return 0; |
---|
96 | } |
---|
97 | |
---|
98 | return 1; |
---|
99 | } |
---|
100 | |
---|
101 | static void strip_nl(char * str) { |
---|
102 | register char c; |
---|
103 | |
---|
104 | for (; (c = *str) != 0; str++) { |
---|
105 | if ( c == '\r' || c == '\n' ) { |
---|
106 | *str = 0; |
---|
107 | return; |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | static char * get_nick(int client) { |
---|
113 | struct roar_client * c; |
---|
114 | clients_get(client, &c); |
---|
115 | return c->name; |
---|
116 | } |
---|
117 | |
---|
118 | static const char * get_ident(int client) { |
---|
119 | static char buf[80]; |
---|
120 | struct roar_client * c; |
---|
121 | clients_get(client, &c); |
---|
122 | |
---|
123 | if ( c->uid == -1 ) { |
---|
124 | buf[0] = '~'; |
---|
125 | buf[1] = 0; |
---|
126 | } else { |
---|
127 | snprintf(buf, sizeof(buf)-1, "uid%i~", c->uid); |
---|
128 | buf[sizeof(buf)-1] = 0; |
---|
129 | } |
---|
130 | |
---|
131 | return buf; |
---|
132 | } |
---|
133 | |
---|
134 | static const char * get_node(int client) { |
---|
135 | struct roar_client * c; |
---|
136 | static char buf_nnode[80]; |
---|
137 | char * nnode; |
---|
138 | |
---|
139 | clients_get(client, &c); |
---|
140 | |
---|
141 | roar_nnode_to_str(&(c->nnode), buf_nnode, sizeof(buf_nnode)); |
---|
142 | |
---|
143 | nnode = strstr(buf_nnode, ": "); |
---|
144 | if ( nnode == NULL ) { |
---|
145 | nnode = "unknown~"; |
---|
146 | } else { |
---|
147 | nnode += 2; |
---|
148 | } |
---|
149 | |
---|
150 | return nnode; |
---|
151 | } |
---|
152 | |
---|
153 | static const char * get_ufull(int client) { |
---|
154 | struct roar_client * c; |
---|
155 | static char buf[80]; |
---|
156 | const char * ident = get_ident(client); |
---|
157 | const char * nnode = get_node(client); |
---|
158 | |
---|
159 | clients_get(client, &c); |
---|
160 | |
---|
161 | snprintf(buf, sizeof(buf)-1, "%s!%s@%s", c->name, ident, nnode); |
---|
162 | |
---|
163 | buf[sizeof(buf)-1] = 0; |
---|
164 | |
---|
165 | return buf; |
---|
166 | } |
---|
167 | |
---|
168 | static const char * get_realname(int client) { |
---|
169 | (void)client; |
---|
170 | return "(no realname)"; |
---|
171 | } |
---|
172 | |
---|
173 | static int get_client_by_nick(const char * nick) { |
---|
174 | struct roar_client * c; |
---|
175 | int i; |
---|
176 | |
---|
177 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
178 | if ( clients_get(i, &c) != 0 ) |
---|
179 | continue; |
---|
180 | |
---|
181 | if ( !!strcasecmp(c->name, nick) ) |
---|
182 | continue; |
---|
183 | |
---|
184 | return i; |
---|
185 | } |
---|
186 | |
---|
187 | return -1; |
---|
188 | } |
---|
189 | |
---|
190 | static struct channel * get_channel(const char * name) { |
---|
191 | struct channel * c; |
---|
192 | size_t i; |
---|
193 | |
---|
194 | for (i = 0; i < MAX_CHANNELS; i++) { |
---|
195 | c = &(g_channels[i]); |
---|
196 | |
---|
197 | if ( !c->client_count ) |
---|
198 | continue; |
---|
199 | |
---|
200 | if ( !!strcasecmp(c->name, name) ) |
---|
201 | continue; |
---|
202 | |
---|
203 | return c; |
---|
204 | } |
---|
205 | |
---|
206 | return NULL; |
---|
207 | } |
---|
208 | |
---|
209 | static size_t get_listener_list(int client, const char * channel, int ** listener) { |
---|
210 | struct channel * c; |
---|
211 | static int clients[ROAR_CLIENTS_MAX]; |
---|
212 | size_t ret = 0; |
---|
213 | size_t i, k; |
---|
214 | int j; |
---|
215 | int found; |
---|
216 | |
---|
217 | for (i = 0; i < MAX_CHANNELS; i++) { |
---|
218 | c = &(g_channels[i]); |
---|
219 | |
---|
220 | if ( !c->client_count ) |
---|
221 | continue; |
---|
222 | |
---|
223 | if ( c->clients[client] < 1 ) |
---|
224 | continue; |
---|
225 | |
---|
226 | if ( channel != NULL && !!strcasecmp(c->name, channel) ) |
---|
227 | continue; |
---|
228 | |
---|
229 | for (j = 0; j < ROAR_CLIENTS_MAX; j++) { |
---|
230 | if ( c->clients[j] > 0 ) { |
---|
231 | found = 0; |
---|
232 | for (k = 0; k < ret; k++) { |
---|
233 | if ( clients[k] == j ) { |
---|
234 | found = 1; |
---|
235 | } |
---|
236 | } |
---|
237 | if ( !found ) { |
---|
238 | clients[ret] = j; |
---|
239 | ret++; |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | *listener = clients; |
---|
246 | |
---|
247 | return ret; |
---|
248 | } |
---|
249 | |
---|
250 | static void put_printf(int client, const char *format, ...) { |
---|
251 | va_list ap; |
---|
252 | struct roar_buffer * buf; |
---|
253 | size_t len = 2048; |
---|
254 | void * data; |
---|
255 | int ret; |
---|
256 | |
---|
257 | if ( roar_buffer_new_data(&buf, len, &data) == -1 ) |
---|
258 | return; |
---|
259 | |
---|
260 | va_start(ap, format); |
---|
261 | ret = vsnprintf(data, len-1, format, ap); |
---|
262 | va_end(ap); |
---|
263 | |
---|
264 | if ( roar_buffer_set_len(buf, strlen(data)) == -1 ) { |
---|
265 | roar_buffer_free(buf); |
---|
266 | return; |
---|
267 | } |
---|
268 | |
---|
269 | clients_add_output(client, &buf); |
---|
270 | } |
---|
271 | |
---|
272 | static int do_join(int client, const char * channel) { |
---|
273 | struct channel * c = NULL; |
---|
274 | size_t i; |
---|
275 | |
---|
276 | if ( channel[0] != '#' ) |
---|
277 | return -1; |
---|
278 | |
---|
279 | if ( !is_valid_name(channel+1) ) |
---|
280 | return -1; |
---|
281 | |
---|
282 | for (i = 0; i < MAX_CHANNELS; i++) { |
---|
283 | if ( !g_channels[i].client_count ) |
---|
284 | continue; |
---|
285 | |
---|
286 | if ( !!strcasecmp(g_channels[i].name, channel) ) |
---|
287 | continue; |
---|
288 | |
---|
289 | c = &(g_channels[i]); |
---|
290 | break; |
---|
291 | } |
---|
292 | |
---|
293 | if ( c == NULL ) { |
---|
294 | for (i = 0; i < MAX_CHANNELS; i++) { |
---|
295 | if ( g_channels[i].client_count ) |
---|
296 | continue; |
---|
297 | |
---|
298 | c = &(g_channels[i]); |
---|
299 | break; |
---|
300 | } |
---|
301 | |
---|
302 | if ( c == NULL ) |
---|
303 | return -1; |
---|
304 | |
---|
305 | memset(c, 0, sizeof(*c)); |
---|
306 | |
---|
307 | c->name = roar_mm_strdup(channel); |
---|
308 | } |
---|
309 | |
---|
310 | if ( c->clients[client] ) |
---|
311 | return -1; |
---|
312 | |
---|
313 | c->clients[client] = 1; |
---|
314 | c->client_count++; |
---|
315 | |
---|
316 | return 0; |
---|
317 | } |
---|
318 | |
---|
319 | static int do_part(int client, const char * channel) { |
---|
320 | struct channel * c = NULL; |
---|
321 | size_t i; |
---|
322 | |
---|
323 | for (i = 0; i < MAX_CHANNELS; i++) { |
---|
324 | if ( !g_channels[i].client_count ) |
---|
325 | continue; |
---|
326 | |
---|
327 | if ( !!strcasecmp(g_channels[i].name, channel) ) |
---|
328 | continue; |
---|
329 | |
---|
330 | if ( !g_channels[i].clients[client] ) |
---|
331 | return -1; |
---|
332 | |
---|
333 | c = &(g_channels[i]); |
---|
334 | break; |
---|
335 | } |
---|
336 | |
---|
337 | c->clients[client] = 0; |
---|
338 | c->client_count--; |
---|
339 | |
---|
340 | if ( !c->client_count ) { |
---|
341 | roar_mm_free(c->name); |
---|
342 | |
---|
343 | if ( c->topic.text != NULL ) |
---|
344 | roar_mm_free(c->topic.text); |
---|
345 | if ( c->topic.user != NULL ) |
---|
346 | roar_mm_free(c->topic.user); |
---|
347 | } |
---|
348 | |
---|
349 | return 0; |
---|
350 | } |
---|
351 | |
---|
352 | static int do_names(int client, const char * channel) { |
---|
353 | const char * nick = get_nick(client); |
---|
354 | size_t i; |
---|
355 | char buf[256]; |
---|
356 | size_t len, offset; |
---|
357 | char * c_nick; |
---|
358 | int j; |
---|
359 | |
---|
360 | for (i = 0; i < MAX_CHANNELS; i++) { |
---|
361 | if ( !g_channels[i].client_count ) |
---|
362 | continue; |
---|
363 | |
---|
364 | if ( !!strcasecmp(g_channels[i].name, channel) ) |
---|
365 | continue; |
---|
366 | |
---|
367 | offset = 0; |
---|
368 | |
---|
369 | for (j = 0; j < ROAR_CLIENTS_MAX; j++) { |
---|
370 | if ( g_channels[i].clients[j] == 0 ) |
---|
371 | continue; |
---|
372 | |
---|
373 | c_nick = get_nick(j); |
---|
374 | len = strlen(c_nick); |
---|
375 | if ( (offset + len + 3) > sizeof(buf) ) { |
---|
376 | buf[offset] = 0; |
---|
377 | put_printf(client, ":%s 353 %s = %s :%s\n", server_name, nick, channel, buf); |
---|
378 | offset = 0; |
---|
379 | } |
---|
380 | |
---|
381 | memcpy(buf + offset, c_nick, len); |
---|
382 | offset += len; |
---|
383 | buf[offset] = ' '; |
---|
384 | offset++; |
---|
385 | buf[offset] = 0; |
---|
386 | } |
---|
387 | |
---|
388 | if ( offset ) { |
---|
389 | buf[offset] = 0; |
---|
390 | put_printf(client, ":%s 353 %s = %s :%s\n", server_name, nick, channel, buf); |
---|
391 | } |
---|
392 | put_printf(client, ":%s 366 %s %s :End of /NAMES list.\n", server_name, nick, channel); |
---|
393 | |
---|
394 | return 0; |
---|
395 | } |
---|
396 | |
---|
397 | return -1; |
---|
398 | } |
---|
399 | |
---|
400 | static void cb_client_delete(struct roar_notify_core * core, struct roar_event * event, void * userdata) { |
---|
401 | int * listener; |
---|
402 | size_t count; |
---|
403 | struct channel * c; |
---|
404 | char * text = quit_msg; |
---|
405 | int client = event->target; |
---|
406 | const char * ufull = get_ufull(client); |
---|
407 | size_t i; |
---|
408 | |
---|
409 | (void)core, (void)userdata; |
---|
410 | |
---|
411 | if ( text == NULL ) { |
---|
412 | text = "Client deleted. Died, kicked or internal error."; |
---|
413 | } |
---|
414 | |
---|
415 | put_printf(client, "ERROR :Closing Link: %s (Quit: %s)\n", ufull, text); |
---|
416 | |
---|
417 | count = get_listener_list(client, NULL, &listener); |
---|
418 | for (; count; count--, listener++) |
---|
419 | put_printf(*listener, ":%s QUIT :Quit: %s\n", ufull, text); |
---|
420 | |
---|
421 | for (i = 0; i < MAX_CHANNELS; i++) { |
---|
422 | c = &(g_channels[i]); |
---|
423 | |
---|
424 | if ( !c->client_count ) |
---|
425 | continue; |
---|
426 | |
---|
427 | if ( !c->clients[client] ) |
---|
428 | continue; |
---|
429 | |
---|
430 | c->clients[client] = 0; |
---|
431 | c->client_count--; |
---|
432 | |
---|
433 | if ( !c->client_count ) { |
---|
434 | roar_mm_free(c->name); |
---|
435 | |
---|
436 | if ( c->topic.text != NULL ) |
---|
437 | roar_mm_free(c->topic.text); |
---|
438 | if ( c->topic.user != NULL ) |
---|
439 | roar_mm_free(c->topic.user); |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | clients_flush(client); |
---|
444 | } |
---|
445 | |
---|
446 | static int new_client(int client, struct roar_vio_calls * vio, struct roard_listen * lsock) { |
---|
447 | struct roar_client_server * cs; |
---|
448 | char * name; |
---|
449 | |
---|
450 | (void)client, (void)vio, (void)lsock; |
---|
451 | |
---|
452 | clients_get_server(client, &cs); |
---|
453 | |
---|
454 | name = ROAR_CLIENT(cs)->name; |
---|
455 | snprintf(name, sizeof(ROAR_CLIENT(cs)->name), "Client%i~", client); |
---|
456 | |
---|
457 | /* |
---|
458 | :ph7.ph.sft NOTICE AUTH :*** Looking up your hostname... |
---|
459 | :ph7.ph.sft NOTICE AUTH :*** Couldn't resolve your hostname; using your IP address instead |
---|
460 | NICK nick |
---|
461 | USER A B C D |
---|
462 | :ph7.ph.sft NOTICE AUTH :*** Couldn't resolve your hostname; using your IP address instead |
---|
463 | :ph7.ph.sft 002 nick :Your host is ph7.ph.sft, running version Unreal3.2.7 |
---|
464 | :ph7.ph.sft 003 nick :This server was created Tue Aug 28 2007 at 10:02:00 CEST |
---|
465 | :ph7.ph.sft 004 nick ph7.ph.sft Unreal3.2.7 iowghraAsORTVSxNCWqBzvdHtGp lvhopsmntikrRcaqOALQbSeIKVfMCuzNTGj |
---|
466 | :ph7.ph.sft 005 nick NAMESX SAFELIST HCN MAXCHANNELS=10 CHANLIMIT=#:10 MAXLIST=b:60,e:60,I:60 NICKLEN=30 CHANNELLEN=32 TOPICLEN=307 KICKLEN=307 AWAYLEN=307 MAXTARGETS=20 WALLCHOPS :are supported by this server |
---|
467 | :ph7.ph.sft 005 nick WATCH=128 SILENCE=15 MODES=12 CHANTYPES=# PREFIX=(ohv)@%+ CHANMODES=beIqa,kfL,lj,psmntirRcOAQKVCuzNSMTG NETWORK=PH2 CASEMAPPING=ascii EXTBAN=~,cqnr ELIST=MNUCT STATUSMSG=@%+ EXCEPTS INVEX :are supported by this server |
---|
468 | :ph7.ph.sft 005 nick CMDS=KNOCK,MAP,DCCALLOW,USERIP :are supported by this server |
---|
469 | */ |
---|
470 | |
---|
471 | put_printf(client, |
---|
472 | ":%s 001 %s :Welcome to the roard based IRC server.\n" |
---|
473 | ":%s 375 %s :- %s Message of the Day -\n" |
---|
474 | ":%s 372 %s :- MotD goes here...\n" |
---|
475 | ":%s 376 %s :End of /MOTD command.\n", |
---|
476 | server_name, name, |
---|
477 | server_name, name, server_name, |
---|
478 | server_name, name, |
---|
479 | server_name, name |
---|
480 | ); |
---|
481 | |
---|
482 | on_lusers(client, NULL, NULL, NULL); |
---|
483 | |
---|
484 | return 0; |
---|
485 | } |
---|
486 | |
---|
487 | static int check_client(int client, struct roar_vio_calls * vio) { |
---|
488 | struct roar_vio_calls rvio; |
---|
489 | char cmd[1024*2]; |
---|
490 | char * args; |
---|
491 | char * text; |
---|
492 | ssize_t len; |
---|
493 | size_t i; |
---|
494 | int found = 0; |
---|
495 | |
---|
496 | if ( vio == NULL ) { |
---|
497 | vio = &rvio; |
---|
498 | roar_vio_open_fh_socket(vio, clients_get_fh(client)); |
---|
499 | } |
---|
500 | |
---|
501 | len = roar_vio_read(vio, cmd, sizeof(cmd)-1); |
---|
502 | if ( len < 1 ) { |
---|
503 | clients_delete(client); |
---|
504 | return -1; |
---|
505 | } |
---|
506 | |
---|
507 | cmd[len] = 0; |
---|
508 | |
---|
509 | strip_nl(cmd); |
---|
510 | |
---|
511 | ROAR_DBG("check_client(client=%i, vio=?): cmd=\"%s\"", client, cmd); |
---|
512 | |
---|
513 | if ( cmd[0] == 0 ) |
---|
514 | return 0; |
---|
515 | |
---|
516 | args = strstr(cmd, " "); |
---|
517 | |
---|
518 | if ( args != NULL ) { |
---|
519 | *args = 0; |
---|
520 | args++; |
---|
521 | if ( *args == ':' ) { |
---|
522 | text = args + 1; |
---|
523 | args = NULL; |
---|
524 | } else { |
---|
525 | text = strstr(args, " :"); |
---|
526 | if ( text != NULL ) { |
---|
527 | *text = 0; |
---|
528 | text += 2; |
---|
529 | } |
---|
530 | } |
---|
531 | } else { |
---|
532 | text = NULL; |
---|
533 | } |
---|
534 | |
---|
535 | for (i = 0; g_commands[i].name != NULL; i++) { |
---|
536 | if ( !strcasecmp(g_commands[i].name, cmd) ) { |
---|
537 | found = 1; |
---|
538 | g_commands[i].func(client, cmd, args, text); |
---|
539 | } |
---|
540 | } |
---|
541 | |
---|
542 | if ( !found ) { |
---|
543 | put_printf(client, ":%s 421 %s %s :Unknown command\n", server_name, get_nick(client), cmd); |
---|
544 | } |
---|
545 | |
---|
546 | return 0; |
---|
547 | } |
---|
548 | |
---|
549 | // commands: |
---|
550 | static int on_nick(int client, const char * cmd, char * args, char * text) { |
---|
551 | char * nick = get_nick(client); |
---|
552 | int * listener; |
---|
553 | size_t count; |
---|
554 | const char * ufull; |
---|
555 | |
---|
556 | (void)cmd, (void)text; |
---|
557 | |
---|
558 | if ( args != NULL && args[0] != 0 && is_valid_name(args) && strlen(args) < ROAR_BUFFER_NAME ) { |
---|
559 | if ( get_client_by_nick(args) != -1 ) { |
---|
560 | put_printf(client, ":%s 433 %s %s :Nickname is already in use.\n", server_name, nick, args); |
---|
561 | } else { |
---|
562 | ufull = get_ufull(client); |
---|
563 | put_printf(client, ":%s NICK :%s\n", ufull, args); |
---|
564 | count = get_listener_list(client, NULL, &listener); |
---|
565 | for (; count; count--, listener++) |
---|
566 | if ( *listener != client ) |
---|
567 | put_printf(*listener, ":%s NICK :%s\n", ufull, args); |
---|
568 | |
---|
569 | strcpy(nick, args); |
---|
570 | } |
---|
571 | } else { |
---|
572 | put_printf(client, ":%s 432 %s %s :Erroneous Nickname: Illegal characters\n", server_name, nick, args); |
---|
573 | } |
---|
574 | |
---|
575 | return 0; |
---|
576 | } |
---|
577 | |
---|
578 | static int on_user(int client, const char * cmd, char * args, char * text) { |
---|
579 | (void)client, (void)cmd, (void)args, (void)text; |
---|
580 | return 0; |
---|
581 | } |
---|
582 | |
---|
583 | static int on_quit(int client, const char * cmd, char * args, char * text) { |
---|
584 | int ret; |
---|
585 | |
---|
586 | (void)cmd, (void)args; |
---|
587 | |
---|
588 | if ( text == NULL ) |
---|
589 | text = "... have a cuddle ..."; |
---|
590 | |
---|
591 | quit_msg = text; |
---|
592 | ret = clients_delete(client); |
---|
593 | quit_msg = NULL; |
---|
594 | |
---|
595 | return ret; |
---|
596 | } |
---|
597 | |
---|
598 | static int on_ping(int client, const char * cmd, char * args, char * text) { |
---|
599 | (void)cmd, (void)args; |
---|
600 | |
---|
601 | put_printf(client, "PONG :%s\n", text); |
---|
602 | |
---|
603 | return 0; |
---|
604 | } |
---|
605 | |
---|
606 | static int on_lusers(int client, const char * cmd, char * args, char * text) { |
---|
607 | const char * clientnick = get_nick(client); |
---|
608 | size_t i; |
---|
609 | size_t numchans = 0; |
---|
610 | |
---|
611 | (void)cmd, (void)args, (void)text; |
---|
612 | |
---|
613 | /* |
---|
614 | :v0.fellig.org 251 nick :There are 9 users and 11 invisible on 1 servers |
---|
615 | |
---|
616 | :v0.fellig.org 252 nick 1 :operator(s) online |
---|
617 | |
---|
618 | :v0.fellig.org 254 nick 16 :channels formed |
---|
619 | :v0.fellig.org 255 nick :I have 20 clients and 0 servers |
---|
620 | :v0.fellig.org 265 nick :Current Local Users: 20 Max: 52 |
---|
621 | :v0.fellig.org 266 nick :Current Global Users: 20 Max: 22 |
---|
622 | */ |
---|
623 | |
---|
624 | for (i = 0; i < MAX_CHANNELS; i++) { |
---|
625 | if ( !g_channels[i].client_count ) |
---|
626 | continue; |
---|
627 | numchans++; |
---|
628 | } |
---|
629 | |
---|
630 | put_printf(client, ":%s 251 %s :There are %zu users and 0 invisible on 1 servers\n", |
---|
631 | server_name, clientnick, counters_get(cur, clients)); |
---|
632 | put_printf(client, ":%s 254 %s %zu :channels formed\n", |
---|
633 | server_name, clientnick, numchans); |
---|
634 | put_printf(client, ":%s 255 %s :I have %zu clients and 0 servers\n", |
---|
635 | server_name, clientnick, counters_get(cur, clients)); |
---|
636 | put_printf(client, ":%s 265 %s :Current Local Users: %zu Max: <unknown>\n", |
---|
637 | server_name, clientnick, counters_get(cur, clients)); |
---|
638 | put_printf(client, ":%s 266 %s :Current Global Users: %zu Max: <unknown>\n", |
---|
639 | server_name, clientnick, counters_get(cur, clients)); |
---|
640 | |
---|
641 | return 0; |
---|
642 | } |
---|
643 | |
---|
644 | static int on_whois(int client, const char * cmd, char * args, char * text) { |
---|
645 | const char * clientnick = get_nick(client); |
---|
646 | const char * tnick = args; |
---|
647 | int tclient; |
---|
648 | |
---|
649 | (void)cmd, (void)text; |
---|
650 | |
---|
651 | /* |
---|
652 | :ph7.ph.sft 317 nick phi 131 1322456381 :seconds idle, signon time |
---|
653 | */ |
---|
654 | |
---|
655 | if ( (tclient = get_client_by_nick(tnick)) == -1 ) { |
---|
656 | put_printf(client, ":%s 401 %s %s :No such nick/channel\n", server_name, clientnick, tnick); |
---|
657 | } else { |
---|
658 | put_printf(client, ":%s 311 %s %s %s %s * :%s\n", server_name, clientnick, tnick, |
---|
659 | get_ident(tclient), get_node(tclient), get_realname(tclient)); |
---|
660 | put_printf(client, ":%s 312 %s %s %s :%s\n", server_name, clientnick, tnick, server_name, server_fullname); |
---|
661 | } |
---|
662 | put_printf(client, ":%s 318 %s %s :End of /WHOIS list.\n", server_name, clientnick, tnick); |
---|
663 | |
---|
664 | return 0; |
---|
665 | } |
---|
666 | |
---|
667 | static int on_privmsg(int client, const char * cmd, char * args, char * text) { |
---|
668 | const char * ufull = get_ufull(client); |
---|
669 | int * listener; |
---|
670 | size_t count; |
---|
671 | char * next; |
---|
672 | int tmp; |
---|
673 | |
---|
674 | if ( args == NULL || text == NULL ) |
---|
675 | return -1; |
---|
676 | |
---|
677 | if ( text[0] == 0 ) |
---|
678 | return 0; |
---|
679 | |
---|
680 | while (args != NULL) { |
---|
681 | next = strstr(args, ","); |
---|
682 | if ( next != NULL ) { |
---|
683 | *next = 0; |
---|
684 | next++; |
---|
685 | } |
---|
686 | |
---|
687 | if ( args[0] == '#' ) { |
---|
688 | count = get_listener_list(client, args, &listener); |
---|
689 | for (; count; count--, listener++) |
---|
690 | if ( *listener != client ) |
---|
691 | put_printf(*listener, ":%s %s %s :%s\n", ufull, cmd, args, text); |
---|
692 | } else { |
---|
693 | if ( (tmp = get_client_by_nick(args)) == -1 ) { |
---|
694 | put_printf(client, ":%s 401 %s %s :No such nick/channel\n", server_name, get_nick(client), args); |
---|
695 | } else { |
---|
696 | put_printf(tmp, ":%s %s %s :%s\n", ufull, cmd, args, text); |
---|
697 | } |
---|
698 | } |
---|
699 | |
---|
700 | args = next; |
---|
701 | } |
---|
702 | |
---|
703 | return 0; |
---|
704 | } |
---|
705 | |
---|
706 | |
---|
707 | static int on_list(int client, const char * cmd, char * args, char * text) { |
---|
708 | const char * clientnick = get_nick(client); |
---|
709 | struct channel * c; |
---|
710 | size_t i; |
---|
711 | |
---|
712 | (void)cmd, (void)args, (void)text; |
---|
713 | |
---|
714 | put_printf(client, ":%s 321 %s Channel :Users Name\n", server_name, clientnick); |
---|
715 | |
---|
716 | for (i = 0; i < MAX_CHANNELS; i++) { |
---|
717 | c = &(g_channels[i]); |
---|
718 | if ( !c->client_count ) |
---|
719 | continue; |
---|
720 | |
---|
721 | put_printf(client, ":%s 322 %s %s %zu :[+] %s\n", |
---|
722 | server_name, clientnick, c->name, c->client_count, c->topic.text == NULL ? "" : c->topic.text); |
---|
723 | } |
---|
724 | |
---|
725 | put_printf(client, ":%s 323 %s :End of /LIST\n", server_name, clientnick); |
---|
726 | |
---|
727 | return 0; |
---|
728 | } |
---|
729 | |
---|
730 | static int on_join(int client, const char * cmd, char * args, char * text) { |
---|
731 | struct channel * c; |
---|
732 | const char * ufull = get_ufull(client); |
---|
733 | int * listener; |
---|
734 | size_t count; |
---|
735 | const char * nick; |
---|
736 | |
---|
737 | (void)cmd, (void)text; |
---|
738 | |
---|
739 | if ( args == NULL ) |
---|
740 | return -1; |
---|
741 | |
---|
742 | if ( do_join(client, args) != 0 ) { |
---|
743 | return -1; |
---|
744 | } |
---|
745 | |
---|
746 | count = get_listener_list(client, args, &listener); |
---|
747 | for (; count; count--, listener++) |
---|
748 | put_printf(*listener, ":%s JOIN :%s\n", ufull, args); |
---|
749 | |
---|
750 | c = get_channel(args); |
---|
751 | |
---|
752 | if ( c->topic.text != NULL ) { |
---|
753 | nick = get_nick(client); |
---|
754 | put_printf(client, ":%s 332 %s %s :%s\n" |
---|
755 | ":%s 333 %s %s %s %li\n", |
---|
756 | server_name, nick, c->name, c->topic.text, |
---|
757 | server_name, nick, c->name, c->topic.user, (long int)c->topic.ts |
---|
758 | ); |
---|
759 | } |
---|
760 | |
---|
761 | do_names(client, args); |
---|
762 | |
---|
763 | return 0; |
---|
764 | } |
---|
765 | |
---|
766 | static int on_part(int client, const char * cmd, char * args, char * text) { |
---|
767 | const char * ufull = get_ufull(client); |
---|
768 | int * listener; |
---|
769 | size_t count; |
---|
770 | |
---|
771 | (void)cmd; |
---|
772 | |
---|
773 | if ( args == NULL ) |
---|
774 | return -1; |
---|
775 | |
---|
776 | if ( text == NULL ) |
---|
777 | text = "Dejoined."; |
---|
778 | |
---|
779 | count = get_listener_list(client, args, &listener); |
---|
780 | for (; count; count--, listener++) |
---|
781 | put_printf(*listener, ":%s PART %s :%s\n", ufull, args, text); |
---|
782 | |
---|
783 | do_part(client, args); |
---|
784 | |
---|
785 | return 0; |
---|
786 | } |
---|
787 | |
---|
788 | static int on_names(int client, const char * cmd, char * args, char * text) { |
---|
789 | (void)cmd, (void)text; |
---|
790 | |
---|
791 | if ( args == NULL ) |
---|
792 | return -1; |
---|
793 | |
---|
794 | return do_names(client, args); |
---|
795 | } |
---|
796 | |
---|
797 | static int on_topic(int client, const char * cmd, char * args, char * text) { |
---|
798 | struct channel * c; |
---|
799 | const char * ufull = get_ufull(client); |
---|
800 | int * listener; |
---|
801 | size_t count; |
---|
802 | char * nick = get_nick(client); |
---|
803 | |
---|
804 | (void)cmd; |
---|
805 | |
---|
806 | if ( args == NULL ) |
---|
807 | return -1; |
---|
808 | |
---|
809 | c = get_channel(args); |
---|
810 | |
---|
811 | if ( c == NULL ) |
---|
812 | return -1; |
---|
813 | |
---|
814 | if ( !c->clients[client] ) { |
---|
815 | return -1; |
---|
816 | } |
---|
817 | |
---|
818 | if ( c->topic.text != NULL ) |
---|
819 | roar_mm_free(c->topic.text); |
---|
820 | if ( c->topic.user != NULL ) |
---|
821 | roar_mm_free(c->topic.user); |
---|
822 | |
---|
823 | c->topic.text = NULL; |
---|
824 | c->topic.user = roar_mm_strdup(nick); |
---|
825 | c->topic.ts = time(NULL); |
---|
826 | |
---|
827 | if ( text != NULL ) |
---|
828 | c->topic.text = roar_mm_strdup(text); |
---|
829 | |
---|
830 | if ( text == NULL ) |
---|
831 | text = ""; |
---|
832 | |
---|
833 | count = get_listener_list(client, c->name, &listener); |
---|
834 | for (; count; count--, listener++) |
---|
835 | put_printf(*listener, ":%s TOPIC %s :%s\n", ufull, c->name, text); |
---|
836 | |
---|
837 | return 0; |
---|
838 | } |
---|
839 | |
---|
840 | // plugin handling suff: |
---|
841 | static int init(struct roar_dl_librarypara * para, struct roar_dl_libraryinst * lib) { |
---|
842 | struct roar_event event; |
---|
843 | |
---|
844 | (void)para, (void)lib; |
---|
845 | |
---|
846 | memset(&event, 0, sizeof(event)); |
---|
847 | |
---|
848 | event.event = ROAR_OE_BASICS_DELETE; |
---|
849 | |
---|
850 | event.emitter = -1; |
---|
851 | event.target = -1; |
---|
852 | event.target_type = ROAR_OT_CLIENT; |
---|
853 | |
---|
854 | memset(g_channels, 0, sizeof(g_channels)); |
---|
855 | |
---|
856 | subscription_client_delete = roar_notify_core_subscribe(NULL, &event, cb_client_delete, NULL); |
---|
857 | |
---|
858 | return 0; |
---|
859 | } |
---|
860 | |
---|
861 | static int unload(struct roar_dl_librarypara * para, struct roar_dl_libraryinst * lib) { |
---|
862 | (void)para, (void)lib; |
---|
863 | |
---|
864 | roar_notify_core_unsubscribe(NULL, subscription_client_delete); |
---|
865 | |
---|
866 | return 0; |
---|
867 | } |
---|
868 | |
---|
869 | static struct roard_proto proto[1] = { |
---|
870 | {ROAR_PROTO_IRC, ROAR_SUBSYS_NONE, "Internet Relay Chat", NULL, new_client, NULL, check_client, NULL, NULL} |
---|
871 | }; |
---|
872 | |
---|
873 | ROARD_DL_REG_PROTO(proto) |
---|
874 | |
---|
875 | ROAR_DL_PLUGIN_START(protocol_irc) { |
---|
876 | ROARD_DL_CHECK_VERSIONS(); |
---|
877 | |
---|
878 | ROAR_DL_PLUGIN_META_PRODUCT_NIV("protocol-irc", ROAR_VID_ROARAUDIO, ROAR_VNAME_ROARAUDIO); |
---|
879 | ROAR_DL_PLUGIN_META_VERSION(ROAR_VERSION_STRING); |
---|
880 | ROAR_DL_PLUGIN_META_LICENSE_TAG(GPLv3_0); |
---|
881 | ROAR_DL_PLUGIN_META_CONTACT_FLNE("Philipp", "Schafft", "ph3-der-loewe", "lion@lion.leolix.org"); |
---|
882 | ROAR_DL_PLUGIN_META_DESC("Implementation of the Internet Relay Chat (IRC)"); |
---|
883 | |
---|
884 | ROAR_DL_PLUGIN_REG(ROAR_DL_FN_INIT, init); |
---|
885 | ROAR_DL_PLUGIN_REG_UNLOAD(unload); |
---|
886 | ROARD_DL_REGFN_PROTO(); |
---|
887 | } ROAR_DL_PLUGIN_END |
---|
888 | |
---|
889 | //ll |
---|