Debian Codegraph @

src/bnetd/irc.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2001 Marco Ziech ([email protected])
3 * Copyright (C) 2005 Bryan Biedenkapp ([email protected])
4 * Copyright (C) 2006,2007,2008 Pelish ([email protected])
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "common/setup_before.h"
22 #include "irc.h"
23
24 #include <cinttypes>
25 #include <cstdint>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 #include <ctime>
30
31 #include "compat/strcasecmp.h"
32
33 #include "common/irc_protocol.h"
34 #include "common/packet.h"
35 #include "common/eventlog.h"
36 #include "common/field_sizes.h"
37 #include "common/bnethash.h"
38 #include "common/xalloc.h"
39 #include "common/addr.h"
40 #include "common/tag.h"
41 #include "common/list.h"
42 #include "common/util.h"
43
44 #include "message.h"
45 #include "channel.h"
46 #include "game.h"
47 #include "connection.h"
48 #include "server.h"
49 #include "account.h"
50 #include "account_wrap.h"
51 #include "prefs.h"
52 #include "tick.h"
53 #include "handle_irc.h"
54 #include "handle_wol.h"
55 #include "command_groups.h"
56 #include "topic.h"
57 #include "clan.h"
58 #include "command.h"
59 #include "anongame_wol.h"
60 #include "common/setup_after.h"
61
62 namespace pvpgn
63 {
64
65 namespace bnetd
66 {
67
68 typedef struct {
69 char const * nick;
70 char const * user;
71 char const * host;
72 } t_irc_message_from;
73
74
75 static char ** irc_split_elems(char * list, int separator, int ignoreblank);
76 static int irc_unget_elems(char ** elems);
77 static char * irc_message_preformat(t_irc_message_from const * from, char const * command, char const * dest, char const * text);
78
79 extern int irc_send_cmd(t_connection * conn, char const * command, char const * params)
80 {
81 t_packet * p;
82 char data[MAX_IRC_MESSAGE_LEN + 1];
83 char const * ircname = server_get_hostname();
84 char const * nick;
85
86 if (!conn) {
87 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
88 return -1;
89 }
90 if (!command) {
91 eventlog(eventlog_level_error, __FUNCTION__, "got NULL command");
92 return -1;
93 }
94 if (!(p = packet_create(packet_class_raw))) {
95 eventlog(eventlog_level_error, __FUNCTION__, "could not create packet");
96 return -1;
97 }
98
99 nick = conn_get_loggeduser(conn);
100 if (!nick)
101 nick = "UserName";
102
103 if (params)
104 std::snprintf(data, sizeof(data), ":%s %s %s %s", ircname, command, nick, params);
105 else
106 std::snprintf(data, sizeof(data), ":%s %s %s", ircname, command, nick);
107
108 DEBUG2("[{}] sent \"{}\"", conn_get_socket(conn), data);
109 std::strcat(data, "\r\n");
110 packet_set_size(p, 0);
111 packet_append_data(p, data, std::strlen(data));
112 conn_push_outqueue(conn, p);
113 packet_del_ref(p);
114 return 0;
115 }
116
117 extern int irc_send(t_connection * conn, int code, char const * params)
118 {
119 char temp[4]; /* '000\0' */
120
121 if (!conn) {
122 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
123 return -1;
124 }
125 if ((code > 999) || (code < 0)) { /* more than 3 digits or negative */
126 eventlog(eventlog_level_error, __FUNCTION__, "invalid message code ({})", code);
127 return -1;
128 }
129 std::sprintf(temp, "%03u", code);
130 return irc_send_cmd(conn, temp, params);
131 }
132
133 extern int irc_send_ping(t_connection * conn)
134 {
135 t_packet * p;
136 char data[MAX_IRC_MESSAGE_LEN];
137
138 if (!conn) {
139 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
140 return -1;
141 }
142
143 if ((conn_get_class(conn) == conn_class_wserv) ||
144 (conn_get_class(conn) == conn_class_wgameres) ||
145 (conn_get_class(conn) == conn_class_wladder))
146 return 0;
147
148 if (!(p = packet_create(packet_class_raw))) {
149 eventlog(eventlog_level_error, __FUNCTION__, "could not create packet");
150 return -1;
151 }
152
153 conn_set_ircping(conn, get_ticks());
154 if (conn_get_state(conn) == conn_state_bot_username)
155 std::sprintf(data, "PING :%u", conn_get_ircping(conn)); /* Undernet doesn't reveal the servername yet ... neither do we */
156 else if ((6 + std::strlen(server_get_hostname()) + 2 + 1) <= MAX_IRC_MESSAGE_LEN)
157 std::sprintf(data, "PING :%s", server_get_hostname());
158 else
159 eventlog(eventlog_level_error, __FUNCTION__, "maximum message length exceeded");
160 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] sent \"{}\"", conn_get_socket(conn), data);
161 std::strcat(data, "\r\n");
162 packet_set_size(p, 0);
163 packet_append_data(p, data, std::strlen(data));
164 conn_push_outqueue(conn, p);
165 packet_del_ref(p);
166 return 0;
167 }
168
169 extern int irc_send_pong(t_connection * conn, char const * params)
170 {
171 t_packet * p;
172 char data[MAX_IRC_MESSAGE_LEN];
173
174 if (!conn) {
175 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
176 return -1;
177 }
178 if ((1 + std::strlen(server_get_hostname()) + 1 + 4 + 1 + std::strlen(server_get_hostname()) + ((params) ? (2 + std::strlen(params)) : (0)) + 2 + 1) > MAX_IRC_MESSAGE_LEN) {
179 eventlog(eventlog_level_error, __FUNCTION__, "max message length exceeded");
180 return -1;
181 }
182 if (!(p = packet_create(packet_class_raw))) {
183 eventlog(eventlog_level_error, __FUNCTION__, "could not create packet");
184 return -1;
185 }
186
187 if (params)
188 std::sprintf(data, ":%s PONG %s :%s", server_get_hostname(), server_get_hostname(), params);
189 else
190 std::sprintf(data, ":%s PONG %s", server_get_hostname(), server_get_hostname());
191 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] sent \"{}\"", conn_get_socket(conn), data);
192 std::strcat(data, "\r\n");
193 packet_set_size(p, 0);
194 packet_append_data(p, data, std::strlen(data));
195 conn_push_outqueue(conn, p);
196 packet_del_ref(p);
197 return 0;
198 }
199
200 extern int irc_authenticate(t_connection * conn, char const * passhash)
201 {
202 /* FIXME: Move this function to handle_irc.* file! */
203 char temp[MAX_IRC_MESSAGE_LEN];
204 t_hash h1;
205 t_hash h2;
206 t_account * a;
207 char const * temphash;
208 char const * username;
209
210 if (!conn) {
211 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
212 return 0;
213 }
214 if (!passhash) {
215 eventlog(eventlog_level_error, __FUNCTION__, "got NULL passhash");
216 return 0;
217 }
218 username = conn_get_loggeduser(conn);
219 if (!username) {
220 /* redundant sanity check */
221 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn->protocol.loggeduser");
222 return 0;
223 }
224 a = accountlist_find_account(username);
225 if (!a) {
226 message_send_text(conn, message_type_notice, NULL, "Authentication failed.");
227 return 0;
228 }
229
230 if (connlist_find_connection_by_account(a) && prefs_get_kick_old_login() == 0) {
231 std::snprintf(temp, sizeof(temp), "%s :Account is already in use!", conn_get_loggeduser(conn));
232 irc_send(conn, ERR_NICKNAMEINUSE, temp);
233 }
234 else if (account_get_auth_lock(a) == 1) {
235 message_send_text(conn, message_type_notice, NULL, "Authentication rejected (account is locked) ");
236 }
237 else
238 {
239 hash_set_str(&h1, passhash);
240 temphash = account_get_pass(a);
241 hash_set_str(&h2, temphash);
242 if (hash_eq(h1, h2)) {
243 conn_login(conn, a, username);
244 conn_set_state(conn, conn_state_loggedin);
245 conn_set_clienttag(conn, CLIENTTAG_IIRC_UINT); /* IIRC hope here is ok */
246 message_send_text(conn, message_type_notice, NULL, "Authentication successful. You are now logged in.");
247 return 1;
248 }
249 else {
250 message_send_text(conn, message_type_notice, NULL, "Authentication failed."); /* wrong password */
251 conn_increment_passfail_count(conn);
252 }
253 }
254 return 0;
255 }
256
257 /* Channel name conversion rules: */
258 /* Not allowed in IRC (RFC2812): NUL, BELL, CR, LF, ' ', ':' and ','*/
259 /* ' ' -> '_' */
260 /* '_' -> '%_' */
261 /* '%' -> '%%' */
262 /* '\b' -> '%b' */
263 /* '\n' -> '%n' */
264 /* '\r' -> '%r' */
265 /* ':' -> '%=' */
266 /* ',' -> '%-' */
267 /* In IRC a channel can be specified by '#'+channelname or '!'+channelid */
268 extern char const * irc_convert_channel(t_channel const * channel, t_connection * c)
269 {
270 char const * bname;
271 static char out[MAX_CHANNELNAME_LEN];
272 unsigned int outpos;
273 int i;
274
275 if (!channel)
276 return "*";
277
278 std::memset(out, 0, sizeof(out));
279 out[0] = '#';
280 outpos = 1;
281
282 if ((conn_get_wol(c) == 1) && (channel_get_clienttag(channel) != 0 && (conn_get_clienttag(c) == channel_get_clienttag(channel))))
283 bname = channel_get_shortname(channel); /* We converting unreadable "lob 18 0" names to human redable ones */
284 else
285 bname = channel_get_name(channel);
286
287 for (i = 0; bname[i] != '\0'; i++) {
288 if (bname[i] == ' ') {
289 out[outpos++] = '_';
290 }
291 else if (bname[i] == '_') {
292 out[outpos++] = '%';
293 out[outpos++] = '_';
294 }
295 else if (bname[i] == '%') {
296 out[outpos++] = '%';
297 out[outpos++] = '%';
298 }
299 else if (bname[i] == '\b') {
300 out[outpos++] = '%';
301 out[outpos++] = 'b';
302 }
303 else if (bname[i] == '\n') {
304 out[outpos++] = '%';
305 out[outpos++] = 'n';
306 }
307 else if (bname[i] == '\r') {
308 out[outpos++] = '%';
309 out[outpos++] = 'r';
310 }
311 else if (bname[i] == ':') {
312 out[outpos++] = '%';
313 out[outpos++] = '=';
314 }
315 else if (bname[i] == ',') {
316 out[outpos++] = '%';
317 out[outpos++] = '-';
318 }
319 else {
320 out[outpos++] = bname[i];
321 }
322 if ((outpos + 2) >= (sizeof(out))) {
323 std::sprintf(out, "!%u", channel_get_channelid(channel));
324 return out;
325 }
326 }
327 return out;
328 }
329
330 extern char const * irc_convert_ircname(char const * pircname)
331 {
332 static char out[MAX_CHANNELNAME_LEN];
333 unsigned int outpos;
334 int special;
335 int i;
336 char const * ircname = pircname + 1;
337
338 if (!ircname) {
339 eventlog(eventlog_level_error, __FUNCTION__, "got NULL ircname");
340 return NULL;
341 }
342
343 outpos = 0;
344 std::memset(out, 0, sizeof(out));
345 special = 0;
346 if (pircname[0] == '!') {
347 t_channel * channel;
348
349 channel = channellist_find_channel_bychannelid(std::atoi(ircname));
350 if (channel)
351 return channel_get_name(channel);
352 else
353 return NULL;
354 }
355 else if (pircname[0] != '#') {
356 return NULL;
357 }
358 for (i = 0; ircname[i] != '\0'; i++) {
359 if (ircname[i] == '_') {
360 out[outpos++] = ' ';
361 }
362 else if (ircname[i] == '%') {
363 if (special) {
364 out[outpos++] = '%';
365 special = 0;
366 }
367 else {
368 special = 1;
369 }
370 }
371 else if (special) {
372 if (ircname[i] == '_') {
373 out[outpos++] = '_';
374 }
375 else if (ircname[i] == 'b') {
376 out[outpos++] = '\b';
377 }
378 else if (ircname[i] == 'n') {
379 out[outpos++] = '\n';
380 }
381 else if (ircname[i] == 'r') {
382 out[outpos++] = '\r';
383 }
384 else if (ircname[i] == '=') {
385 out[outpos++] = ':';
386 }
387 else if (ircname[i] == '-') {
388 out[outpos++] = ',';
389 }
390 else {
391 /* maybe it's just a typo :) */
392 out[outpos++] = '%';
393 out[outpos++] = ircname[i];
394 }
395 }
396 else {
397 out[outpos++] = ircname[i];
398 }
399 if ((outpos + 2) >= (sizeof(out))) {
400 return NULL;
401 }
402 }
403 return out;
404 }
405
406 /* splits an string list into its elements */
407 /* (list will be modified) */
408 static char ** irc_split_elems(char * list, int separator, int ignoreblank)
409 {
410 int i;
411 int count;
412 char ** out;
413
414 if (!list) {
415 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
416 return NULL;
417 }
418
419 for (count = 0, i = 0; list[i] != '\0'; i++) {
420 if (list[i] == separator) {
421 count++;
422 if (ignoreblank) {
423 /* ignore more than one separators "in a row" */
424 while ((list[i + 1] != '\0') && (list[i] == separator)) i++;
425 }
426 }
427 }
428 count++; /* count separators -> we have one more element ... */
429 /* we also need a terminating element */
430 out = (char**)xmalloc((count + 1)*sizeof(char *));
431
432 out[0] = list;
433 if (count > 1) {
434 for (i = 1; i < count; i++) {
435 out[i] = std::strchr(out[i - 1], separator);
436 if (!out[i]) {
437 eventlog(eventlog_level_error, __FUNCTION__, "BUG: wrong number of separators");
438 xfree(out);
439 return NULL;
440 }
441 if (ignoreblank)
442 while ((*out[i] + 1) == separator) out[i]++;
443 *out[i]++ = '\0';
444 }
445 if ((ignoreblank) && (out[count - 1]) && (*out[count - 1] == '\0')) {
446 out[count - 1] = NULL; /* last element is blank */
447 }
448 }
449 else if ((ignoreblank) && (*out[0] == '\0')) {
450 out[0] = NULL; /* now we have 2 terminators ... never mind */
451 }
452 out[count] = NULL; /* terminating element */
453 return out;
454 }
455
456 static int irc_unget_elems(char ** elems)
457 {
458 if (!elems) {
459 eventlog(eventlog_level_error, __FUNCTION__, "got NULL elems");
460 return -1;
461 }
462 xfree(elems);
463 return 0;
464 }
465
466 extern char ** irc_get_listelems(char * list)
467 {
468 return irc_split_elems(list, ',', 0);
469 }
470
471 extern int irc_unget_listelems(char ** elems)
472 {
473 return irc_unget_elems(elems);
474 }
475
476 extern char ** irc_get_paramelems(char * list)
477 {
478 return irc_split_elems(list, ' ', 1);
479 }
480
481 extern int irc_unget_paramelems(char ** elems)
482 {
483 return irc_unget_elems(elems);
484 }
485
486 extern char ** irc_get_ladderelems(char * list)
487 {
488 return irc_split_elems(list, ':', 1);
489 }
490
491 extern int irc_unget_ladderelems(char ** elems)
492 {
493 return irc_unget_elems(elems);
494 }
495
496 static char * irc_message_preformat(t_irc_message_from const * from, char const * command, char const * dest, char const * text)
497 {
498 char * myfrom;
499 char const * mydest = "";
500 char const * mytext = "";
501 int len;
502 char * msg;
503
504 if (!command) {
505 eventlog(eventlog_level_error, __FUNCTION__, "got NULL command");
506 return NULL;
507 }
508 if (from) {
509 if ((!from->nick) || (!from->user) || (!from->host)) {
510 eventlog(eventlog_level_error, __FUNCTION__, "got malformed from");
511 return NULL;
512 }
513 myfrom = (char*)xmalloc(std::strlen(from->nick) + 1 + std::strlen(from->user) + 1 + std::strlen(from->host) + 1); /* nick + "!" + user + "@" + host + "\0" */
514 std::sprintf(myfrom, "%s!%s@%s", from->nick, from->user, from->host);
515 }
516 else
517 myfrom = xstrdup(server_get_hostname());
518 if (dest)
519 mydest = dest;
520
521 if (text)
522 mytext = text;
523
524 len = 1 + std::strlen(myfrom) + 1 +
525 std::strlen(command) + 1 +
526 std::strlen(mydest) + 1 +
527 1 + std::strlen(mytext) + 1;
528
529
530 msg = (char*)xmalloc(len);
531
532 std::sprintf(msg, ":%s\n%s\n%s\n%s", myfrom, command, mydest, mytext);
533 xfree(myfrom);
534 return msg;
535 }
536
537 extern int irc_message_postformat(t_packet * packet, t_connection const * dest)
538 {
539 /* the four elements */
540 char * e1;
541 char * e1_2;
542 char * e2;
543 char * e3;
544 char * e4;
545 char const * tname = NULL;
546 char const * toname = "AUTH"; /* fallback name */
547 const char * temp;
548
549 if (!packet) {
550 eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet");
551 return -1;
552 }
553 if (!dest) {
554 eventlog(eventlog_level_error, __FUNCTION__, "got NULL dest");
555 return -1;
556 }
557
558 e1 = (char*)packet_get_raw_data(packet, 0);
559 e2 = std::strchr(e1, '\n');
560 if (!e2) {
561 eventlog(eventlog_level_warn, __FUNCTION__, "malformed message (e2 missing)");
562 return -1;
563 }
564 *e2++ = '\0';
565 e3 = std::strchr(e2, '\n');
566 if (!e3) {
567 eventlog(eventlog_level_warn, __FUNCTION__, "malformed message (e3 missing)");
568 return -1;
569 }
570 *e3++ = '\0';
571 e4 = std::strchr(e3, '\n');
572 if (!e4) {
573 eventlog(eventlog_level_warn, __FUNCTION__, "malformed message (e4 missing)");
574 return -1;
575 }
576 *e4++ = '\0';
577
578 if (prefs_get_hide_addr() && !(account_get_command_groups(conn_get_account(dest)) & command_get_group("/admin-addr")))
579 {
580 e1_2 = std::strchr(e1, '@');
581 if (e1_2)
582 {
583 *e1_2++ = '\0';
584 }
585 }
586 else
587 e1_2 = NULL;
588
589 if (e3[0] == '\0') { /* fill in recipient */
590 if ((tname = conn_get_loggeduser(dest)))
591 toname = tname;
592 }
593 else
594 toname = e3;
595
596 if (std::strcmp(toname, "\r") == 0) {
597 toname = ""; /* HACK: the target field is really empty */
598 temp = "";
599 }
600 else
601 temp = " ";
602
603 if (std::strlen(e1) + 1 + std::strlen(e2) + 1 + std::strlen(toname) + std::strlen(temp) + std::strlen(e4) + 2 + 1 <= MAX_IRC_MESSAGE_LEN) {
604 char msg[MAX_IRC_MESSAGE_LEN + 1];
605
606 if (e1_2)
607 std::sprintf(msg, "%s@hidden %s %s%s%s", e1, e2, toname, temp, e4);
608 else
609 std::sprintf(msg, "%s %s %s%s%s", e1, e2, toname, temp, e4);
610
611 DEBUG2("[{}] sent \"{}\"", conn_get_socket(dest), msg);
612 std::strcat(msg, "\r\n");
613
614 packet_set_size(packet, 0);
615 packet_append_data(packet, msg, std::strlen(msg));
616 if (tname)
617 conn_unget_chatname(dest, tname);
618 return 0;
619 }
620 else {
621 /* FIXME: split up message? */
622 eventlog(eventlog_level_warn, __FUNCTION__, "maximum IRC message length exceeded");
623 if (tname)
624 conn_unget_chatname(dest, tname);
625 return -1;
626 }
627 }
628
629 extern int irc_message_format(t_packet * packet, t_message_type type, t_connection * me, t_connection * dst, char const * text, unsigned int dstflags)
630 {
631 char * msg;
632 char const * ctag;
633 t_irc_message_from from;
634
635 if (!packet)
636 {
637 eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet");
638 return -1;
639 }
640
641 msg = NULL;
642 if (me)
643 ctag = clienttag_uint_to_str(conn_get_clienttag(me));
644 else
645 ctag = clienttag_uint_to_str(CLIENTTAG_IIRC_UINT);
646
647 switch (type)
648 {
649 /* case message_type_adduser: this is sent manually in handle_irc */
650 case message_type_adduser:
651 /* when we do it somewhere else, then we can also make sure to not get our logs spammed */
652 break;
653 case message_type_join:
654 {
655 t_channel * channel;
656 from.nick = conn_get_chatname(me);
657 from.user = ctag;
658 from.host = addr_num_to_ip_str(conn_get_addr(me));
659
660 channel = conn_get_channel(me);
661
662 if (tag_check_wolv2(conn_get_clienttag(dst))) {
663 char temp[MAX_IRC_MESSAGE_LEN];
664 t_clan * clan;
665 unsigned int clanid = 0;
666 std::memset(temp, 0, sizeof(temp));
667
668 /**
669 * For WOLv2 the channel JOIN output must be like the following:
670 * user!WWOL@hostname JOIN :clanID,longIP channelName
671 */
672
673 clan = account_get_clan(conn_get_account(me));
674
675 if (clan)
676 clanid = clan_get_clanid(clan);
677
678 std::sprintf(temp, ":%u,%u", clanid, conn_get_addr(me));
679 msg = irc_message_preformat(&from, "JOIN", temp, irc_convert_channel(channel, dst));
680 }
681 else {
682 msg = irc_message_preformat(&from, "JOIN", "\r", irc_convert_channel(channel, dst));
683 }
684 conn_unget_chatname(me, from.nick);
685 }
686 break;
687 case message_type_part:
688 from.nick = conn_get_chatname(me);
689 from.user = ctag;
690 from.host = addr_num_to_ip_str(conn_get_addr(me));
691 msg = irc_message_preformat(&from, "PART", "\r", irc_convert_channel(conn_get_channel(me), dst));
692 conn_unget_chatname(me, from.nick);
693 break;
694 case message_type_talk:
695 case message_type_whisper:
696 {
697 char const * dest;
698 char temp[MAX_IRC_MESSAGE_LEN];
699
700 if (me) {
701 from.nick = conn_get_chatname(me);
702 from.host = addr_num_to_ip_str(conn_get_addr(me));
703 }
704 else {
705 from.nick = server_get_hostname();
706 from.host = server_get_hostname();
707 }
708 from.user = ctag;
709
710 if (type == message_type_talk)
711 dest = irc_convert_channel(conn_get_channel(me), dst); /* FIXME: support more channels and choose right one! */
712 else
713 dest = ""; /* will be replaced with username in postformat */
714
715 std::sprintf(temp, ":%s", text);
716
717 if ((conn_get_wol(me) != 1) && (conn_get_wol(dst) == 1) && (conn_wol_get_pageme(dst))
718 && (type == message_type_whisper) && (conn_get_channel(me) != (conn_get_channel(dst))))
719 msg = irc_message_preformat(&from, "PAGE", NULL, temp);
720 else
721 msg = irc_message_preformat(&from, "PRIVMSG", dest, temp);
722
723 if (me)
724 conn_unget_chatname(me, from.nick);
725 }
726 break;
727 case message_type_emote:
728 {
729 char const * dest;
730 char temp[MAX_IRC_MESSAGE_LEN];
731
732 /* "\001ACTION " + text + "\001" + \0 */
733
734 /* PELISH: WOLv1, DUNE2000 and RENEGADE shows emotes automaticaly to self */
735 if ((me == dst) && ((tag_check_wolv1(conn_get_clienttag(dst))) ||
736 (conn_get_clienttag(dst) == CLIENTTAG_DUNE2000_UINT) ||
737 (conn_get_clienttag(dst) == CLIENTTAG_RENEGADE_UINT) ||
738 (conn_get_clienttag(dst) == CLIENTTAG_RENGDFDS_UINT)))
739 break;
740
741 if ((8 + std::strlen(text) + 1 + 1) <= MAX_IRC_MESSAGE_LEN) {
742 std::sprintf(temp, ":\001ACTION %s\001", text);
743 }
744 else {
745 std::sprintf(temp, ":\001ACTION (maximum message length exceeded)\001");
746 }
747 from.nick = conn_get_chatname(me);
748 from.user = ctag;
749 from.host = addr_num_to_ip_str(conn_get_addr(me));
750 /* FIXME: also supports whisper emotes? */
751 dest = irc_convert_channel(conn_get_channel(me), dst); /* FIXME: support more channels and choose right one! */
752 msg = irc_message_preformat(&from, "PRIVMSG", dest, temp);
753 conn_unget_chatname(me, from.nick);
754 }
755 break;
756 case message_type_broadcast:
757 case message_type_info:
758 case message_type_error:
759 {
760 char temp[MAX_IRC_MESSAGE_LEN];
761 std::sprintf(temp, ":%s", text);
762 if (conn_get_wol(dst) == 1) {
763 if ((type == message_type_info) || (type == message_type_error))
764 msg = irc_message_preformat(NULL, "PAGE", NULL, temp);
765 else
766 msg = irc_message_preformat(NULL, "NOTICE", NULL, temp);
767 }
768 else
769 msg = irc_message_preformat(NULL, "NOTICE", NULL, temp);
770 }
771 break;
772 case message_type_channel:
773 /* ignore it */
774 break;
775 case message_type_userflags:
776 /* ignore it but maybe will be used for set MODE +o if user is operator
777 * but at this time is this command sended only once when user join
778 * first channel */
779 break;
780 case message_type_mode:
781 from.nick = conn_get_chatname(me);
782 from.user = ctag;
783 from.host = addr_num_to_ip_str(conn_get_addr(me));
784 msg = irc_message_preformat(&from, "MODE", "\r", text);
785 conn_unget_chatname(me, from.nick);
786 break;
787 case message_type_nick:
788 {
789 from.nick = conn_get_loggeduser(me);
790 from.host = addr_num_to_ip_str(conn_get_addr(me));
791 from.user = ctag;
792 msg = irc_message_preformat(&from, "NICK", "\r", text);
793 }
794 break;
795 case message_type_notice:
796 {
797 char temp[MAX_IRC_MESSAGE_LEN];
798 std::sprintf(temp, ":%s", text);
799
800 if (me && conn_get_chatname(me))
801 {
802 from.nick = conn_get_chatname(me);
803 from.host = addr_num_to_ip_str(conn_get_addr(me));
804 from.user = ctag;
805 msg = irc_message_preformat(&from, "NOTICE", "", temp);
806 }
807 else
808 {
809 msg = irc_message_preformat(NULL, "NOTICE", "", temp);
810 }
811 }
812 break;
813 case message_type_namreply:
814 {
815 t_channel * channel;
816
817 channel = conn_get_channel(me);
818
819 irc_send_rpl_namreply(dst, channel);
820 }
821 break;
822 case message_type_topic:
823 {
824 t_channel * channel;
825 channel = conn_get_channel(me);
826 irc_send_topic(dst, channel);
827 }
828 break;
829 case message_type_kick:
830 {
831 char temp[MAX_IRC_MESSAGE_LEN];
832 from.nick = conn_get_chatname(me);
833 from.user = ctag;
834 from.host = addr_num_to_ip_str(conn_get_addr(me));
835 if (text)
836 std::sprintf(temp, "%s :%s", conn_get_chatname(me), text);
837 else
838 std::sprintf(temp, "%s :", conn_get_chatname(me));
839 msg = irc_message_preformat(&from, "KICK", irc_convert_channel(conn_get_channel(me), dst), temp);
840 conn_unget_chatname(me, from.nick);
841 }
842 break;
843 case message_type_quit:
844 if (conn_get_clienttag(dst) == CLIENTTAG_IIRC_UINT) {
845 char temp[MAX_IRC_MESSAGE_LEN];
846 if (text)
847 sprintf(temp, ":%s", text);
848 else
849 sprintf(temp, ":");
850
851 from.nick = conn_get_chatname(me);
852 from.host = addr_num_to_ip_str(conn_get_addr(me));
853 from.user = ctag;
854 msg = irc_message_preformat(&from, "QUIT", "\r", temp);
855 }
856 else {
857 from.nick = conn_get_chatname(me);
858 from.user = ctag;
859 from.host = addr_num_to_ip_str(conn_get_addr(me));
860 msg = irc_message_preformat(&from, "PART", "\r", irc_convert_channel(conn_get_channel(me), dst));
861 conn_unget_chatname(me, from.nick);
862 }
863 break;
864
865 /**
866 * Westwood Online Extensions
867 */
868 case message_type_host:
869 from.nick = conn_get_chatname(me);
870 from.user = ctag;
871 from.host = addr_num_to_ip_str(conn_get_addr(me));
872 msg = irc_message_preformat(&from, "HOST", "\r", text);
873 conn_unget_chatname(me, from.nick);
874 break;
875 case message_type_invmsg:
876 from.nick = conn_get_chatname(me);
877 from.user = ctag;
878 from.host = addr_num_to_ip_str(conn_get_addr(me));
879 msg = irc_message_preformat(&from, "INVMSG", NULL, text);
880 conn_unget_chatname(me, from.nick);
881 break;
882 case message_type_page:
883 {
884 char temp[MAX_IRC_MESSAGE_LEN];
885 from.nick = conn_get_chatname(me);
886 from.user = ctag;
887 from.host = addr_num_to_ip_str(conn_get_addr(me));
888 std::sprintf(temp, ":%s", text);
889 msg = irc_message_preformat(&from, "PAGE", NULL, temp);
890 conn_unget_chatname(me, from.nick);
891 }
892 break;
893 case message_wol_joingame:
894 from.nick = conn_get_chatname(me);
895 from.user = ctag;
896 from.host = addr_num_to_ip_str(conn_get_addr(me));
897 msg = irc_message_preformat(&from, "JOINGAME", "\r", text);
898 conn_unget_chatname(me, from.nick);
899 break;
900 case message_type_gameopt_talk:
901 case message_type_gameopt_whisper:
902 {
903 char const * dest;
904 char temp[MAX_IRC_MESSAGE_LEN];
905
906 if (me) {
907 from.nick = conn_get_chatname(me);
908 from.host = addr_num_to_ip_str(conn_get_addr(me));
909 }
910 else {
911 from.nick = server_get_hostname();
912 from.host = server_get_hostname();
913 }
914 from.user = ctag;
915
916 if (type == message_type_gameopt_talk)
917 dest = irc_convert_channel(conn_get_channel(me), dst); /* FIXME: support more channels and choose right one! */
918 else
919 dest = ""; /* will be replaced with username in postformat */
920
921 std::sprintf(temp, ":%s", text);
922
923 msg = irc_message_preformat(&from, "GAMEOPT", dest, temp);
924
925 if (me)
926 conn_unget_chatname(me, from.nick);
927 }
928 break;
929 case message_wol_start_game:
930 from.nick = conn_get_chatname(me);
931 from.user = ctag;
932 from.host = addr_num_to_ip_str(conn_get_addr(me));
933 msg = irc_message_preformat(&from, "STARTG", NULL, text);
934 conn_unget_chatname(me, from.nick);
935 break;
936 case message_wol_advertr:
937 msg = irc_message_preformat(NULL, "ADVERTR", "\r", text);
938 break;
939 case message_wol_chanchk:
940 msg = irc_message_preformat(NULL, "CHANCHK", "\r", text);
941 break;
942 case message_wol_userip:
943 from.nick = conn_get_chatname(me);
944 from.user = ctag;
945 from.host = addr_num_to_ip_str(conn_get_addr(me));
946 msg = irc_message_preformat(&from, "USERIP", "\r", text);
947 conn_unget_chatname(me, from.nick);
948 break;
949 default:
950 eventlog(eventlog_level_warn, __FUNCTION__, "{} not yet implemented", type);
951 return -1;
952 }
953
954 if (msg) {
955 packet_append_string(packet, msg);
956 xfree(msg);
957 return 0;
958 }
959 return -1;
960 }
961
962 int irc_send_rpl_namreply_internal(t_connection * c, t_channel const * channel){
963 char temp[MAX_IRC_MESSAGE_LEN];
964 char const * ircname;
965 int first = 1;
966 t_connection * m;
967
968 if (!channel) {
969 eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel");
970 return -1;
971 }
972
973 std::memset(temp, 0, sizeof(temp));
974 ircname = irc_convert_channel(channel, c);
975 if (!ircname) {
976 eventlog(eventlog_level_error, __FUNCTION__, "channel has NULL ircname");
977 return -1;
978 }
979 /* '@' = secret; '*' = private; '=' = public */
980 if ((1 + 1 + std::strlen(ircname) + 2 + 1) <= MAX_IRC_MESSAGE_LEN) {
981 std::sprintf(temp, "%c %s :", ((channel_get_permanent(channel)) ? ('=') : ('*')), ircname);
982 }
983 else {
984 eventlog(eventlog_level_warn, __FUNCTION__, "maximum message length exceeded");
985 return -1;
986 }
987
988 if (!(channel_get_flags(channel) & channel_flags_thevoid))
989 for (m = channel_get_first(channel); m; m = channel_get_next()) {
990 char const * name = conn_get_chatname(m);
991 char flg[5] = "";
992 unsigned int flags;
993
994 if (!name)
995 continue;
996 flags = conn_get_flags(m);
997 if (flags & MF_BLIZZARD)
998 std::strcat(flg, "@");
999 else if ((flags & MF_BNET) || (flags & MF_GAVEL))
1000 std::strcat(flg, "%");
1001 else if (flags & MF_VOICE)
1002 std::strcat(flg, "+");
1003 if ((std::strlen(temp) + ((!first) ? (1) : (0)) + std::strlen(flg) + std::strlen(name) + 1) <= sizeof(temp)) {
1004 if (!first) std::strcat(temp, " ");
1005 if (conn_get_wol(c) == 1) {
1006 char _temp[MAX_IRC_MESSAGE_LEN];
1007 t_game * game = conn_get_game(m);
1008 if ((first) && ((std::strcmp(ircname, "#Lob_38_0") == 0) ||
1009 (std::strcmp(ircname, "#Lob_39_0") == 0) ||
1010 (std::strcmp(ircname, "#Lob_40_0") == 0))) {
1011
1012 std::sprintf(_temp, "@matchbot,0,0 ");
1013 std::strcat(temp, _temp);
1014 first = 0;
1015
1016 }
1017
1018 if ((game) && (game_get_channel(game) == channel)) {
1019 if (game_get_owner(game) == m) {
1020 /* PELISH: Only game owners will have OP flag (this prevent official OP to be normal player) */
1021 std::strcat(temp, "@");
1022 }
1023 }
1024 else {
1025 if ((flags & MF_BNET) || (flags & MF_GAVEL))
1026 std::sprintf(flg, "@"); /* PELISH: WOL does not understand to '%' char so we using '@' for TempOP too */
1027 std::strcat(temp, flg);
1028 }
1029 if (tag_check_wolv2(conn_get_clienttag(c))) {
1030 /* BATTLECLAN Support */
1031 std::memset(_temp, 0, sizeof(_temp));
1032 t_clan * clan = account_get_clan(conn_get_account(m));
1033 unsigned int clanid = 0;
1034
1035 if (clan)
1036 clanid = clan_get_clanid(clan);
1037
1038 std::sprintf(_temp, "%s,%u,%u", name, clanid, conn_get_addr(m));
1039 std::strcat(temp, _temp);
1040 }
1041 else {
1042 std::strcat(temp, name);
1043 }
1044 }
1045 else {
1046 std::strcat(temp, flg);
1047 std::strcat(temp, name);
1048 }
1049
1050 first = 0;
1051 }
1052 conn_unget_chatname(m, name);
1053 }
1054 irc_send(c, RPL_NAMREPLY, temp);
1055
1056 return 0;
1057 }
1058
1059 extern int irc_send_rpl_namreply(t_connection * c, t_channel const * channel)
1060 {
1061 char temp[MAX_IRC_MESSAGE_LEN];
1062 char const * ircname;
1063
1064 if (!c) {
1065 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1066 return -1;
1067 }
1068
1069 if (channel) {
1070 ircname = irc_convert_channel(channel, c);
1071 if (!ircname) {
1072 eventlog(eventlog_level_error, __FUNCTION__, "channel has NULL ircname");
1073 return -1;
1074 }
1075 irc_send_rpl_namreply_internal(c, channel);
1076 std::sprintf(temp, "%.32s :End of NAMES list", ircname);
1077 }
1078 else {
1079 t_elem const * curr;
1080 LIST_TRAVERSE_CONST(channellist(), curr)
1081 {
1082 channel = (t_channel*)elem_get_data(curr);
1083 irc_send_rpl_namreply_internal(c, channel);
1084 }
1085 std::sprintf(temp, "* :End of NAMES list");
1086 }
1087
1088 irc_send(c, RPL_ENDOFNAMES, temp);
1089 return 0;
1090 }
1091
1092 static int irc_who_connection(t_connection * dest, t_connection * c)
1093 {
1094 t_account * a;
1095 char const * tempuser;
1096 char const * tempowner;
1097 char const * tempname;
1098 char const * tempip;
1099 char const * tempflags = "@"; /* FIXME: that's dumb */
1100 char temp[MAX_IRC_MESSAGE_LEN];
1101 char const * tempchannel;
1102
1103 if (!dest) {
1104 eventlog(eventlog_level_error, __FUNCTION__, "got NULL destination");
1105 return -1;
1106 }
1107 if (!c) {
1108 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1109 return -1;
1110 }
1111 a = conn_get_account(c);
1112 if (!(tempuser = clienttag_uint_to_str(conn_get_clienttag(c))))
1113 {
1114 eventlog(eventlog_level_error, __FUNCTION__, "got NULL clienttag (tempuser)");
1115 return -1;
1116 }
1117 if (!(tempowner = account_get_ll_owner(a)))
1118 {
1119 eventlog(eventlog_level_error, __FUNCTION__, "got NULL ll_owner (tempowner)");
1120 return -1;
1121 }
1122 if (!(tempname = conn_get_username(c)))
1123 {
1124 eventlog(eventlog_level_error, __FUNCTION__, "got NULL username (tempname)");
1125 return -1;
1126 }
1127 if (!(tempip = addr_num_to_ip_str(conn_get_addr(c))))
1128 {
1129 eventlog(eventlog_level_error, __FUNCTION__, "got NULL addr (tempip)");
1130 return -1;
1131 }
1132 if (!(tempchannel = irc_convert_channel(conn_get_channel(c), c)))
1133 {
1134 eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel (tempchannel)");
1135 return -1;
1136 }
1137 if ((std::strlen(tempchannel) + 1 + std::strlen(tempuser) + 1 + std::strlen(tempip) + 1 + std::strlen(server_get_hostname()) + 1 + std::strlen(tempname) + 1 + 1 + std::strlen(tempflags) + 4 + std::strlen(tempowner) + 1) > MAX_IRC_MESSAGE_LEN) {
1138 eventlog(eventlog_level_info, __FUNCTION__, "WHO reply too long - skip");
1139 return -1;
1140 }
1141 else
1142 std::sprintf(temp, "%s %s %s %s %s %c%s :0 %s", tempchannel, tempuser, tempip, server_get_hostname(), tempname, 'H', tempflags, tempowner);
1143 irc_send(dest, RPL_WHOREPLY, temp);
1144 return 0;
1145 }
1146
1147 extern int irc_who(t_connection * c, char const * name)
1148 {
1149 /* FIXME: support wildcards! */
1150
1151 if (!c) {
1152 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1153 return -1;
1154 }
1155 if (!name) {
1156 eventlog(eventlog_level_error, __FUNCTION__, "got NULL name");
1157 return -1;
1158 }
1159 if ((name[0] == '#') || (name[0] == '&') || (name[0] == '!')) {
1160 /* it's a channel */
1161 t_connection * info;
1162 t_channel * channel;
1163 char const * ircname;
1164
1165 ircname = irc_convert_ircname(name);
1166 channel = channellist_find_channel_by_name(ircname, NULL, NULL);
1167 if (!channel) {
1168 char temp[MAX_IRC_MESSAGE_LEN];
1169
1170 if ((std::strlen(":No such channel") + 1 + std::strlen(name) + 1) <= MAX_IRC_MESSAGE_LEN) {
1171 std::sprintf(temp, ":No such channel %s", name);
1172 irc_send(c, ERR_NOSUCHCHANNEL, temp);
1173 }
1174 else {
1175 irc_send(c, ERR_NOSUCHCHANNEL, ":No such channel");
1176 }
1177 return 0;
1178 }
1179 for (info = channel_get_first(channel); info; info = channel_get_next()) {
1180 irc_who_connection(c, info);
1181 }
1182 }
1183 else {
1184 /* it's just one user */
1185 t_connection * info;
1186
1187 if ((info = connlist_find_connection_by_accountname(name)))
1188 return irc_who_connection(c, info);
1189 }
1190 return 0;
1191 }
1192
1193 extern int irc_send_motd(t_connection * conn)
1194 {
1195 char const * tempname;
1196 char const * filename;
1197 std::FILE *fp;
1198 char * line, *formatted_line;
1199 char send_line[MAX_IRC_MESSAGE_LEN];
1200 char motd_failed = 0;
1201 bool first = true;
1202
1203 if (!conn) {
1204 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1205 return -1;
1206 }
1207
1208 tempname = conn_get_loggeduser(conn);
1209
1210 if ((filename = prefs_get_motdfile())) {
1211 if ((fp = std::fopen(filename, "r"))) {
1212 while ((line = file_get_line(fp))) {
1213 if ((formatted_line = message_format_line(conn, line))) {
1214 formatted_line[0] = ' ';
1215 std::sprintf(send_line, ":-%s", formatted_line);
1216 if (first) {
1217 irc_send(conn, RPL_MOTDSTART, send_line);
1218 first = false;
1219 }
1220 else
1221 irc_send(conn, RPL_MOTD, send_line);
1222 xfree(formatted_line);
1223 }
1224 }
1225
1226 file_get_line(NULL); // clear file_get_line buffer
1227 std::fclose(fp);
1228 }
1229 else
1230 motd_failed = 1;
1231 }
1232 else
1233 motd_failed = 1;
1234
1235 if (motd_failed) {
1236 irc_send(conn, RPL_MOTDSTART, ":- Failed to load motd, sending default motd ");
1237 irc_send(conn, RPL_MOTD, ":- ====================================================== ");
1238 irc_send(conn, RPL_MOTD, ":- http://www.pvpgn.pro ");
1239 irc_send(conn, RPL_MOTD, ":- ====================================================== ");
1240 }
1241 irc_send(conn, RPL_ENDOFMOTD, ":End of /MOTD command");
1242 return 0;
1243 }
1244
1245 int irc_welcome(t_connection * conn){
1246 if (conn_get_wol(conn))
1247 handle_wol_welcome(conn);
1248 else
1249 handle_irc_welcome(conn);
1250
1251 return 0;
1252 }
1253
1254 extern int _handle_nick_command(t_connection * conn, int numparams, char ** params, char * text)
1255 {
1256 char temp[MAX_IRC_MESSAGE_LEN];
1257 /* FIXME: more strict param checking */
1258
1259 if ((conn_get_loggeduser(conn)) &&
1260 (conn_get_state(conn) != conn_state_bot_password &&
1261 conn_get_state(conn) != conn_state_bot_username)) {
1262 irc_send(conn, ERR_RESTRICTED, ":You can't change your nick after login");
1263 }
1264 else {
1265 if ((params) && (params[0])) {
1266 if (conn_get_loggeduser(conn)){
1267 std::snprintf(temp, sizeof(temp), ":%s", params[0]);
1268 message_send_text(conn, message_type_nick, conn, temp);
1269 }
1270 conn_set_loggeduser(conn, params[0]);
1271 }
1272 else if (text) {
1273 if (conn_get_loggeduser(conn)){
1274 std::snprintf(temp, sizeof(temp), ":%s", text);
1275 message_send_text(conn, message_type_nick, conn, temp);
1276 }
1277 conn_set_loggeduser(conn, text);
1278 }
1279 else
1280 irc_send(conn, ERR_NEEDMOREPARAMS, "NICK :Not enough parameters");
1281
1282 if ((conn_get_user(conn)) && (conn_get_loggeduser(conn)))
1283 irc_welcome(conn); /* only send the welcome if we have USER and NICK */
1284 }
1285 return 0;
1286 }
1287
1288 extern int _handle_ping_command(t_connection * conn, int numparams, char ** params, char * text)
1289 {
1290 /* Dizzy: just ignore this because RFC says we should not reply client PINGs
1291 * NOTE: RFC2812 doesn't seem to be very expressive about this ... */
1292 if (numparams)
1293 irc_send_pong(conn, params[0]);
1294 else
1295 irc_send_pong(conn, text);
1296 return 0;
1297 }
1298
1299 extern int _handle_pong_command(t_connection * conn, int numparams, char ** params, char * text)
1300 {
1301 /* NOTE: RFC2812 doesn't seem to be very expressive about this ... */
1302 if (conn_get_ircping(conn) == 0) {
1303 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] PONG without PING", conn_get_socket(conn));
1304 }
1305 else {
1306 unsigned int val = 0;
1307 char * sname;
1308
1309 if (numparams >= 1) {
1310 val = std::strtoul(params[0], NULL, 10);
1311 sname = params[0];
1312 }
1313 else if (text) {
1314 val = std::strtoul(text, NULL, 10);
1315 sname = text;
1316 }
1317 else {
1318 val = 0;
1319 sname = 0;
1320 }
1321
1322 if (conn_get_ircping(conn) != val) {
1323 if ((!(sname)) || (std::strcmp(sname, server_get_hostname()) != 0)) {
1324 /* Actually the servername should not be always accepted but we aren't that pedantic :) */
1325 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] got bad PONG ({}!={} && {}!={})", conn_get_socket(conn), val, conn_get_ircping(conn), sname, server_get_hostname());
1326 return -1;
1327 }
1328 }
1329 conn_set_latency(conn, get_ticks() - conn_get_ircping(conn));
1330 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] latency is now {} ({}-{})", conn_get_socket(conn), get_ticks() - conn_get_ircping(conn), get_ticks(), conn_get_ircping(conn));
1331 conn_set_ircping(conn, 0);
1332 }
1333 return 0;
1334 }
1335
1336 extern int _handle_join_command(t_connection * conn, int numparams, char ** params, char * text)
1337 {
1338 if (numparams >= 1) {
1339 char ** e;
1340
1341 /* According to RFC2812 - if channelname is "0" we need to PART all channels */
1342 if (std::strcmp(params[0], "0") == 0) {
1343 conn_part_channel(conn);
1344 return 0;
1345 }
1346
1347 e = irc_get_listelems(params[0]);
1348 if ((e) && (e[0])) {
1349 char temp[MAX_IRC_MESSAGE_LEN];
1350 char const * ircname = irc_convert_ircname(e[0]);
1351 t_channel * old_channel = conn_get_channel(conn);
1352 t_channel * channel;
1353 t_account * acc = conn_get_account(conn);
1354
1355 if ((ircname) && (channel = channellist_find_channel_by_name(ircname, NULL, NULL))) {
1356 if (channel_check_banning(channel, conn)) {
1357 std::snprintf(temp, sizeof(temp), "%s :You are banned from that channel.", e[0]);
1358 irc_send(conn, ERR_BANNEDFROMCHAN, temp);
1359 if (e)
1360 irc_unget_listelems(e);
1361 return 0;
1362 }
1363
1364 if ((account_get_auth_admin(acc, NULL) != 1) && (account_get_auth_admin(acc, ircname) != 1) &&
1365 (account_get_auth_operator(acc, NULL) != 1) && (account_get_auth_operator(acc, ircname) != 1) &&
1366 (channel_get_max(channel) != -1) && (channel_get_curr(channel) >= channel_get_max(channel))) {
1367
1368 std::snprintf(temp, sizeof(temp), "%s :The channel is currently full.", e[0]);
1369 irc_send(conn, ERR_CHANNELISFULL, temp);
1370 if (e)
1371 irc_unget_listelems(e);
1372 return 0;
1373 }
1374 }
1375
1376 if ((!(ircname)) || (conn_set_channel(conn, ircname) < 0)) {
1377 std::snprintf(temp, sizeof(temp), "%s :JOIN failed", e[0]);
1378 irc_send(conn, ERR_NOSUCHCHANNEL, temp); /* Anything is still bad */
1379 }
1380 }
1381 if (e)
1382 irc_unget_listelems(e);
1383 }
1384 else
1385 irc_send(conn, ERR_NEEDMOREPARAMS, "JOIN :Not enough parameters");
1386 return 0;
1387 }
1388
1389 extern int irc_send_topic(t_connection * c, t_channel const * channel)
1390 {
1391 class_topic Topic;
1392 std::string topicstr = Topic.get(channel_get_name(channel));
1393 char temp[MAX_IRC_MESSAGE_LEN];
1394
1395 if (topicstr.empty() == false) {
1396 std::snprintf(temp, sizeof(temp), "%s :%s", irc_convert_channel(channel, c), topicstr.c_str());
1397 irc_send(c, RPL_TOPIC, temp);
1398 }
1399 else {
1400 std::snprintf(temp, sizeof(temp), "%s :", irc_convert_channel(channel, c));
1401 irc_send(c, RPL_TOPIC, temp);
1402 /* PELISH: This is according to RFC2812 but brakes WOLv1/WOLv2 */
1403 //snprintf(temp, sizeof(temp), "%s :No topic is set", irc_convert_channel(channel,c));
1404 //irc_send(c, RPL_NOTOPIC, temp);
1405 }
1406 return 0;
1407 }
1408
1409 extern int _handle_topic_command(t_connection * conn, int numparams, char ** params, char * text)
1410 {
1411 char ** e = NULL;
1412 char temp[MAX_IRC_MESSAGE_LEN];
1413 class_topic Topic;
1414
1415 if (params && params[0])
1416 {
1417 if (conn_get_wol(conn) == 1) {
1418 t_channel * channel = conn_get_channel(conn);
1419 if (channel)
1420 Topic.set(std::string(channel_get_name(channel)), std::string(text), false);
1421 else {
1422 std::snprintf(temp, sizeof(temp), "%s :You're not on that channel", params[0]);
1423 irc_send(conn, ERR_NOTONCHANNEL, temp);
1424 }
1425 }
1426 e = irc_get_listelems(params[0]);
1427 }
1428 if ((e) && (e[0]))
1429 {
1430 t_channel *channel = conn_get_channel(conn);
1431
1432 if (channel) {
1433 char const * ircname = irc_convert_ircname(e[0]);
1434
1435 if ((ircname) && (strcasecmp(channel_get_name(channel), ircname) == 0)) {
1436 irc_send_topic(conn, channel);
1437 }
1438 else {
1439 std::snprintf(temp, sizeof(temp), "%s :You're not on that channel", e[0]);
1440 irc_send(conn, ERR_NOTONCHANNEL, temp);
1441 }
1442 }
1443 else {
1444 std::snprintf(temp, sizeof(temp), "%s :You're not on that channel", e[0]);
1445 irc_send(conn, ERR_NOTONCHANNEL, temp);
1446 }
1447 }
1448 else
1449 irc_send(conn, ERR_NEEDMOREPARAMS, "TOPIC :Not enough parameters");
1450
1451 if (e)
1452 {
1453 irc_unget_listelems(e);
1454 }
1455
1456 return 0;
1457 }
1458
1459 extern int _handle_kick_command(t_connection * conn, int numparams, char ** params, char * text)
1460 {
1461 char temp[MAX_IRC_MESSAGE_LEN];
1462 char ** e;
1463 /**
1464 * Heres the imput expected
1465 * KICK [channel] [kicked_user],[kicked_user2]
1466 *
1467 * Heres the output expected
1468 * :user!WWOL@hostname KICK [channel] [kicked_user] :[text]
1469 *
1470 * WOL in [text] sends Admin name
1471 */
1472
1473 if ((numparams != 2) || !(params[1])) {
1474 irc_send(conn, ERR_NEEDMOREPARAMS, "KICK :Not enough parameters");
1475 return 0;
1476 }
1477
1478 if (e = irc_get_listelems(params[1]))
1479 {
1480 /* Make standart PvPGN KICK from RFC2812 KICK */
1481 if (text)
1482 std::snprintf(temp, sizeof(temp), "/kick %s %s", e[0], text);
1483 else
1484 std::snprintf(temp, sizeof(temp), "/kick %s", e[0]);
1485
1486 handle_command(conn, temp);
1487
1488 irc_unget_listelems(e);
1489 }
1490 return 0;
1491 }
1492
1493 static int irc_send_banlist(t_connection * conn, t_channel * channel)
1494 {
1495 t_elem const * curr;
1496 char const * banned;
1497 char const * ircname = server_get_hostname();
1498 char temp[MAX_IRC_MESSAGE_LEN];
1499
1500 if (!conn) {
1501 ERROR0("got NULL conn");
1502 return -1;
1503 }
1504
1505 if (!channel) {
1506 ERROR0("got NULL channel");
1507 return -1;
1508 }
1509
1510 LIST_TRAVERSE_CONST(channel_get_banlist(channel), curr) {
1511 banned = (char*)elem_get_data(curr);
1512
1513 //FIXME: right now we lie about who have gives ban and also about bantime
1514 std::snprintf(temp, sizeof(temp), "%s %s!*@* %s 1208297879", irc_convert_channel(channel, conn), banned, ircname);
1515 irc_send(conn, RPL_BANLIST, temp);
1516 }
1517 return 0;
1518 }
1519
1520 extern int _handle_mode_command(t_connection * conn, int numparams, char ** params, char * text)
1521 {
1522 char temp[MAX_IRC_MESSAGE_LEN];
1523 t_account * acc = conn_get_account(conn);
1524
1525 std::memset(temp, 0, sizeof(temp));
1526
1527 if (numparams < 1) {
1528 irc_send(conn, ERR_NEEDMOREPARAMS, "MODE :Not enough parameters");
1529 return 0;
1530 }
1531
1532 if (params[0][0] == '#') {
1533 /* Channel mode */
1534 t_channel * channel;
1535 char const * ircname = irc_convert_ircname(params[0]);
1536
1537 /* FIXME: Supports more than one channel in MODE command */
1538 if (!(channel = conn_get_channel(conn))) {
1539 std::snprintf(temp, sizeof(temp), "%s :No such channel", params[0]);
1540 irc_send(conn, ERR_NOSUCHCHANNEL, temp);
1541 return 0;
1542 }
1543
1544 if (numparams == 1) {
1545 /* FIXME: Send real CHANELMODE flags! */
1546 std::snprintf(temp, sizeof(temp), "%s +tns", params[0]);
1547 irc_send(conn, RPL_CHANNELMODEIS, temp);
1548 return 0;
1549 }
1550
1551 if (numparams == 2) {
1552 if (std::strcmp(params[1], "b") == 0) {
1553 irc_send_banlist(conn, channel);
1554 std::snprintf(temp, sizeof(temp), "%s :End of channel ban list", params[0]);
1555 irc_send(conn, RPL_ENDOFBANLIST, temp);
1556 return 0;
1557 }
1558 else {
1559 std::snprintf(temp, sizeof(temp), ":%s is unknown mode char to me for %s", params[1], params[0]);
1560 irc_send(conn, ERR_UNKNOWNMODE, temp);
1561 return 0;
1562 }
1563 }
1564
1565 /* PELISH: Also tmpOP have setting modes alowed because all new channels have only tmpOP */
1566 if ((channel_conn_is_tmpOP(channel, conn) != 1) &&
1567 (account_get_auth_admin(acc, NULL) != 1) && (account_get_auth_admin(acc, ircname) != 1) &&
1568 (account_get_auth_operator(acc, NULL) != 1) && (account_get_auth_operator(acc, ircname) != 1)) {
1569 std::snprintf(temp, sizeof(temp), "%s :You're not channel operator", params[0]);
1570 irc_send(conn, ERR_CHANOPRIVSNEEDED, temp);
1571 return 0;
1572 }
1573
1574 if (std::strcmp(params[1], "+b") == 0) {
1575 channel_ban_user(channel, params[2]);
1576 std::snprintf(temp, sizeof(temp), "%s %s %s!*@*", params[0], params[1], params[2]);
1577 channel_message_send(channel, message_type_mode, conn, temp);
1578 }
1579 else if (std::strcmp(params[1], "-b") == 0) {
1580 channel_unban_user(channel, params[2]);
1581 std::snprintf(temp, sizeof(temp), "%s %s %s!*@*", params[0], params[1], params[2]);
1582 channel_message_send(channel, message_type_mode, conn, temp);
1583 }
1584 else if (std::strcmp(params[1], "+o") == 0) {
1585 std::snprintf(temp, sizeof(temp), "/op %s", params[2]);
1586 handle_command(conn, temp);
1587 std::snprintf(temp, sizeof(temp), "%s %s %s", params[0], params[1], params[2]);
1588 channel_message_send(channel, message_type_mode, conn, temp);
1589 }
1590 else if (std::strcmp(params[1], "-o") == 0) {
1591 std::snprintf(temp, sizeof(temp), "/deop %s", params[2]);
1592 handle_command(conn, temp);
1593 std::snprintf(temp, sizeof(temp), "%s %s %s", params[0], params[1], params[2]);
1594 channel_message_send(channel, message_type_mode, conn, temp);
1595 }
1596 else if (std::strcmp(params[1], "+v") == 0) {
1597 std::snprintf(temp, sizeof(temp), "/voice %s", params[2]);
1598 handle_command(conn, temp);
1599 std::snprintf(temp, sizeof(temp), "%s %s %s", params[0], params[1], params[2]);
1600 channel_message_send(channel, message_type_mode, conn, temp);
1601 }
1602 else if (std::strcmp(params[1], "-v") == 0) {
1603 std::snprintf(temp, sizeof(temp), "/devoice %s", params[2]);
1604 handle_command(conn, temp);
1605
1606 std::snprintf(temp, sizeof(temp), "%s %s %s", params[0], params[1], params[2]);
1607 channel_message_send(channel, message_type_mode, conn, temp);
1608 }
1609 else if (std::strcmp(params[1], "+i") == 0) {
1610 /* FIXME: channel will be only for invited */
1611 }
1612 else if (std::strcmp(params[1], "+l") == 0) {
1613 channel_set_max(channel, std::atoi(params[2]));
1614 std::snprintf(temp, sizeof(temp), "%s %s %s", params[0], params[1], params[2]);
1615 channel_message_send(channel, message_type_mode, conn, temp);
1616 }
1617 else if (std::strcmp(params[1], "-l") == 0) {
1618 channel_set_max(channel, -1);
1619 std::snprintf(temp, sizeof(temp), "%s %s", params[0], params[1]);
1620 channel_message_send(channel, message_type_mode, conn, temp);
1621 }
1622 else {
1623 std::snprintf(temp, sizeof(temp), ":%s is unknown mode char to me for %s", params[1], params[0]);
1624 irc_send(conn, ERR_UNKNOWNMODE, temp);
1625 }
1626 }
1627 else {
1628 /* User modes */
1629 /* FIXME: Support user modes (away, invisible...) */
1630 irc_send(conn, ERR_UMODEUNKNOWNFLAG, ":Unknown MODE flag");
1631 }
1632 return 0;
1633 }
1634
1635 extern int _handle_time_command(t_connection * conn, int numparams, char ** params, char * text)
1636 {
1637 char temp[MAX_IRC_MESSAGE_LEN] = {}; // PELISH: According to RFC2812
1638 std::time_t now;
1639 char const * ircname = server_get_hostname();
1640
1641 if ((numparams >= 1) && (params[0]))
1642 {
1643 if (std::strcmp(params[0], ircname) == 0)
1644 {
1645 now = std::time(NULL);
1646 std::snprintf(temp, sizeof(temp), "%s :%" PRId64, ircname, static_cast<std::int64_t>(now));
1647 irc_send(conn, RPL_TIME, temp);
1648 }
1649 else {
1650 std::snprintf(temp, sizeof(temp), "%s :No such server", params[0]);
1651 irc_send(conn, ERR_NOSUCHSERVER, temp);
1652 }
1653 }
1654 else {
1655 /* RPL_TIME contains time and name of this server */
1656 now = std::time(NULL);
1657 std::snprintf(temp, sizeof(temp), "%s :%" PRId64, ircname, static_cast<std::int64_t>(now));
1658 irc_send(conn, RPL_TIME, temp);
1659 }
1660 return 0;
1661 }
1662
1663 }
1664
1665 }