Debian Codegraph @

src/bnetd/handle_irc.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2001 Marco Ziech ([email protected])
3 * Copyright (C) 2005 Bryan Biedenkapp ([email protected])
4 * Copyright (C) 2006,2007 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 "handle_irc.h"
23
24 #include <cstring>
25 #include <cctype>
26 #include <cstdlib>
27
28 #include "compat/strcasecmp.h"
29 #include "common/irc_protocol.h"
30 #include "common/eventlog.h"
31 #include "common/bnethash.h"
32 #include "common/tag.h"
33 #include "common/util.h"
34 #include "common/list.h"
35 #include "common/addr.h"
36 #include "common/xstring.h"
37
38 #include "prefs.h"
39 #include "command.h"
40 #include "irc.h"
41 #include "account.h"
42 #include "account_wrap.h"
43 #include "command_groups.h"
44 #include "channel.h"
45 #include "message.h"
46 #include "tick.h"
47 #include "topic.h"
48 #include "server.h"
49 #include "common/setup_after.h"
•50 #include "common/trans.h"
51
52 namespace pvpgn
53 {
54
55 namespace bnetd
56 {
57
58 typedef int(*t_irc_command)(t_connection * conn, int numparams, char ** params, char * text);
59
60 typedef struct {
61 const char * irc_command_string;
62 t_irc_command irc_command_handler;
63 } t_irc_command_table_row;
64
65 static int _handle_user_command(t_connection * conn, int numparams, char ** params, char * text);
66 static int _handle_pass_command(t_connection * conn, int numparams, char ** params, char * text);
67 static int _handle_privmsg_command(t_connection * conn, int numparams, char ** params, char * text);
68 static int _handle_notice_command(t_connection * conn, int numparams, char ** params, char * text);
69 static int _handle_quit_command(t_connection * conn, int numparams, char ** params, char * text);
70
71 static int _handle_who_command(t_connection * conn, int numparams, char ** params, char * text);
72 static int _handle_list_command(t_connection * conn, int numparams, char ** params, char * text);
73 static int _handle_names_command(t_connection * conn, int numparams, char ** params, char * text);
74 static int _handle_userhost_command(t_connection * conn, int numparams, char ** params, char * text);
75 static int _handle_ison_command(t_connection * conn, int numparams, char ** params, char * text);
76 static int _handle_whois_command(t_connection * conn, int numparams, char ** params, char * text);
77 static int _handle_part_command(t_connection * conn, int numparams, char ** params, char * text);
78
79
80 /* state "connected" handlers */
81 static const t_irc_command_table_row irc_con_command_table[] =
82 {
83 { "NICK", _handle_nick_command },
84 { "USER", _handle_user_command },
85 { "PING", _handle_ping_command },
86 { "PONG", _handle_pong_command },
87 { "PASS", _handle_pass_command },
88 { "PRIVMSG", _handle_privmsg_command },
89 { "NOTICE", _handle_notice_command },
90 { "QUIT", _handle_quit_command },
91
92 { NULL, NULL }
93 };
94
95 /* state "logged in" handlers */
96 static const t_irc_command_table_row irc_log_command_table[] =
97 {
98 { "WHO", _handle_who_command },
99 { "LIST", _handle_list_command },
100 { "TOPIC", _handle_topic_command },
101 { "JOIN", _handle_join_command },
102 { "NAMES", _handle_names_command },
103 { "MODE", _handle_mode_command },
104 { "USERHOST", _handle_userhost_command },
105 { "ISON", _handle_ison_command },
106 { "WHOIS", _handle_whois_command },
107 { "PART", _handle_part_command },
108 { "KICK", _handle_kick_command },
109 { "TIME", _handle_time_command },
110
111 { NULL, NULL }
112 };
113
114
115 extern int handle_irc_con_command(t_connection * conn, char const * command, int numparams, char ** params, char * text)
116 {
117 t_irc_command_table_row const *p;
118
119 for (p = irc_con_command_table; p->irc_command_string != NULL; p++) {
120 if (strcasecmp(command, p->irc_command_string) == 0) {
121 if (p->irc_command_handler != NULL)
122 return ((p->irc_command_handler)(conn, numparams, params, text));
123 }
124 }
125 return -1;
126 }
127
128 extern int handle_irc_log_command(t_connection * conn, char const * command, int numparams, char ** params, char * text)
129 {
130 t_irc_command_table_row const *p;
131
132 for (p = irc_log_command_table; p->irc_command_string != NULL; p++) {
133 if (strcasecmp(command, p->irc_command_string) == 0) {
134 if (p->irc_command_handler != NULL)
135 return ((p->irc_command_handler)(conn, numparams, params, text));
136 }
137 }
138 return -1;
139 }
140
141 extern int handle_irc_welcome(t_connection * conn)
142 {
143 char temp[MAX_IRC_MESSAGE_LEN];
144 std::time_t temptime;
145 char const * tempname;
146 char const * temptimestr;
147
148 if (!conn) {
149 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
150 return -1;
151 }
152
153 tempname = conn_get_loggeduser(conn);
154
155 if ((34 + std::strlen(tempname) + 1) <= MAX_IRC_MESSAGE_LEN)
156 std::sprintf(temp, ":Welcome to the %s IRC Network %s", prefs_get_irc_network_name(), tempname);
157 else
158 std::sprintf(temp, ":Maximum length exceeded");
159 irc_send(conn, RPL_WELCOME, temp);
160
161 if ((14 + std::strlen(server_get_hostname()) + 10 + std::strlen(PVPGN_SOFTWARE " " PVPGN_VERSION) + 1) <= MAX_IRC_MESSAGE_LEN)
162 std::sprintf(temp, ":Your host is %s, running " PVPGN_SOFTWARE " " PVPGN_VERSION, server_get_hostname());
163 else
164 std::sprintf(temp, ":Maximum length exceeded");
165 irc_send(conn, RPL_YOURHOST, temp);
166
167 temptime = server_get_starttime(); /* FIXME: This should be build time */
168 temptimestr = std::ctime(&temptime);
169 if ((25 + std::strlen(temptimestr) + 1) <= MAX_IRC_MESSAGE_LEN)
170 std::sprintf(temp, ":This server was created %s", temptimestr); /* FIXME: is ctime() portable? */
171 else
172 std::sprintf(temp, ":Maximum length exceeded");
173 irc_send(conn, RPL_CREATED, temp);
174
175 /* we don't give mode information on MYINFO we give it on ISUPPORT */
176 if ((std::strlen(server_get_hostname()) + 7 + std::strlen(PVPGN_SOFTWARE " " PVPGN_VERSION) + 9 + 1) <= MAX_IRC_MESSAGE_LEN)
177 std::sprintf(temp, "%s " PVPGN_SOFTWARE " " PVPGN_VERSION " - -", server_get_hostname());
178 else
179 std::sprintf(temp, ":Maximum length exceeded");
180 irc_send(conn, RPL_MYINFO, temp);
181
182 std::sprintf(temp, "NICKLEN=%d TOPICLEN=%d CHANNELLEN=%d PREFIX=%s CHANTYPES=" CHANNEL_TYPE " NETWORK=%s IRCD=" PVPGN_SOFTWARE,
183 MAX_CHARNAME_LEN, MAX_TOPIC_LEN, MAX_CHANNELNAME_LEN, CHANNEL_PREFIX, prefs_get_irc_network_name());
184
185 if ((std::strlen(temp)) <= MAX_IRC_MESSAGE_LEN)
186 irc_send(conn, RPL_ISUPPORT, temp);
187 else {
188 std::sprintf(temp, ":Maximum length exceeded");
189 irc_send(conn, RPL_ISUPPORT, temp);
190 }
191
192 irc_send_motd(conn);
193
194 message_send_text(conn, message_type_notice, NULL, "This is an experimental service");
195
196 conn_set_state(conn, conn_state_bot_password);
197
198 if (conn_get_ircpass(conn)) {
199 message_send_text(conn, message_type_notice, NULL, "Trying to authenticate with PASS ...");
200 irc_authenticate(conn, conn_get_ircpass(conn));
201 }
202 else {
203 message_send_text(conn, message_type_notice, NULL, "No PASS command received. Please identify yourself by /msg NICKSERV identify <password>.");
204 }
205 return 0;
206 }
207
208 static int _handle_user_command(t_connection * conn, int numparams, char ** params, char * text)
209 {
210 /* RFC 2812 says: */
211 /* <user> <mode> <unused> :<realname>*/
212 /* ircII and X-Chat say: */
213 /* mz SHODAN localhost :Marco Ziech */
214 /* BitchX says: */
215 /* mz +iws mz :Marco Ziech */
216 /* Don't bother with, params 1 and 2 anymore they don't contain what they should. */
217 char * user = NULL;
218 char * realname = NULL;
219
220 if ((numparams >= 3) && (params[0]) && (text)) {
221 user = params[0];
222 realname = text;
223
224 if (conn_get_user(conn)) {
225 irc_send(conn, ERR_ALREADYREGISTRED, ":You are already registred");
226 }
227 else {
228 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got USER: user=\"{}\" realname=\"{}\"", conn_get_socket(conn), user, realname);
229 conn_set_user(conn, user);
230 conn_set_owner(conn, realname);
231 if (conn_get_loggeduser(conn))
232 handle_irc_welcome(conn); /* only send the welcome if we have USER and NICK */
233 }
234 }
235 else {
236 irc_send(conn, ERR_NEEDMOREPARAMS, "USER :Not enough parameters");
237 }
238 return 0;
239 }
240
241 static int _handle_pass_command(t_connection * conn, int numparams, char ** params, char * text)
242 {
243 if ((!conn_get_ircpass(conn)) && (conn_get_state(conn) == conn_state_bot_username)) {
244 t_hash h;
245
246 if (numparams >= 1) {
247 bnet_hash(&h, std::strlen(params[0]), params[0]);
248 conn_set_ircpass(conn, hash_get_str(h));
249 }
250 else
251 irc_send(conn, ERR_NEEDMOREPARAMS, "PASS :Not enough parameters");
252 }
253 else {
254 irc_send(conn, ERR_ALREADYREGISTRED, ":Unauthorized command (already registered)");
255 }
256 return 0;
257 }
258
259 static int _handle_privmsg_command(t_connection * conn, int numparams, char ** params, char * text)
260 {
261 if ((numparams >= 1) && (text))
262 {
263 int i;
264 char ** e;
265
266 e = irc_get_listelems(params[0]);
267 /* FIXME: support wildcards! */
268
269 /* start amadeo: code was sent by some unkown fellow of pvpgn (maybe u wanna give us your name
270 for any credits), it adds nick-registration, i changed some things here and there... */
271 for (i = 0; ((e) && (e[i])); i++) {
272 if (strcasecmp(e[i], "NICKSERV") == 0) {
273 char * pass;
274
275 pass = std::strchr(text, ' ');
276 if (pass)
277 *pass++ = '\0';
278
279 if (strcasecmp(text, "identify") == 0) {
280 switch (conn_get_state(conn)) {
281 case conn_state_bot_password:
282 {
283 if (pass) {
284 t_hash h;
285
286 strtolower(pass);
287 bnet_hash(&h, std::strlen(pass), pass);
288 irc_authenticate(conn, hash_get_str(h));
289 }
290 else {
291 message_send_text(conn, message_type_notice, NULL, "Syntax: IDENTIFY <password> (max 16 characters)");
292 }
293 break;
294 }
295 case conn_state_loggedin:
296 {
297 message_send_text(conn, message_type_notice, NULL, "You don't need to IDENTIFY");
298 break;
299 }
300 default:;
301 eventlog(eventlog_level_trace, __FUNCTION__, "got /msg in unexpected connection state ({})", conn_state_get_str(conn_get_state(conn)));
302 }
303 }
304 else if (strcasecmp(text, "register") == 0) {
305 t_hash passhash;
306 t_account * temp;
307 char * username = (char *)conn_get_loggeduser(conn);
308
309 if (account_check_name(username)<0) {
310 message_send_text(conn, message_type_error, conn, "Account name contains invalid symbol!");
311 break;
312 }
313
314 if (!prefs_get_allow_new_accounts()){
315 message_send_text(conn, message_type_error, conn, "Account creation is not allowed");
316 break;
317 }
318
319 if (!pass || pass[0] == '\0' || (std::strlen(pass)>16)) {
320 message_send_text(conn, message_type_error, conn, "Syntax: REGISTER <password> (max 16 characters)");
321 break;
322 }
323
324 strtolower(pass);
325
326 bnet_hash(&passhash, std::strlen(pass), pass);
327
328 message_send_text(conn, message_type_info, conn, std::string("Trying to create account \"" + std::string(username) + "\" with password \"" + std::string(pass) + "\"").c_str());
329
330 temp = accountlist_create_account(username, hash_get_str(passhash));
331 if (!temp) {
332 message_send_text(conn, message_type_error, conn, "Failed to create account!");
333 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account \"{}\" not created (failed)", conn_get_socket(conn), username);
334 conn_unget_chatname(conn, username);
335 break;
336 }
337
338 message_send_text(conn, message_type_info, conn, std::string("Account #" + std::to_string(account_get_uid(temp)) + " created."));
339 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account \"{}\" created", conn_get_socket(conn), username);
340 conn_unget_chatname(conn, username);
341 }
342 else {
343 message_send_text(conn, message_type_notice, nullptr, "Invalid arguments for NICKSERV");
344 message_send_text(conn, message_type_notice, nullptr, std::string(":Unrecognized command \"" + std::string(text) + "\"").c_str());
345 }
346 }
347 else if (conn_get_state(conn) == conn_state_loggedin) {
348 if (e[i][0] == '#') {
349 /* channel message */
350 t_channel * channel;
351
352 if ((channel = channellist_find_channel_by_name(irc_convert_ircname(e[i]), NULL, NULL))) {
353 if ((std::strlen(text) >= 9) && (std::strncmp(text, "\001ACTION ", 8) == 0) && (text[std::strlen(text) - 1] == '\001')) {
354 /* at least "\001ACTION \001" */
355 /* it's a CTCP ACTION message */
356 text = text + 8;
357 text[std::strlen(text) - 1] = '\0';
358 channel_message_send(channel, message_type_emote, conn, text);
359 }
360 else {
361 channel_message_log(channel, conn, 1, text);
362 channel_message_send(channel, message_type_talk, conn, text);
363 }
364 }
365 else {
366 irc_send(conn, ERR_NOSUCHCHANNEL, ":No such channel");
367 }
368 }
369 else {
370 /* whisper */
371 t_connection * user;
372
373 if ((user = connlist_find_connection_by_accountname(e[i])))
374 {
375 message_send_text(user, message_type_whisper, conn, text);
376 }
377 else
378 {
379 irc_send(conn, ERR_NOSUCHNICK, ":No such user");
380 }
381 }
382 }
383 }
384 if (e)
385 irc_unget_listelems(e);
386 }
387 else
388 irc_send(conn, ERR_NEEDMOREPARAMS, "PRIVMSG :Not enough parameters");
389 return 0;
390 }
391
392 static int _handle_notice_command(t_connection * conn, int numparams, char ** params, char * text)
393 {
394 if ((numparams >= 1) && (text)) {
395 int i;
396 char ** e;
397
398 e = irc_get_listelems(params[0]);
399 /* FIXME: support wildcards! */
400
401 for (i = 0; ((e) && (e[i])); i++) {
402 if (conn_get_state(conn) == conn_state_loggedin) {
403 t_connection * user;
404
405 if ((user = connlist_find_connection_by_accountname(e[i]))) {
406 message_send_text(user, message_type_notice, conn, text);
407 }
408 else {
409 irc_send(conn, ERR_NOSUCHNICK, ":No such user");
410 }
411 }
412 }
413 if (e)
414 irc_unget_listelems(e);
415 }
416 else
417 irc_send(conn, ERR_NEEDMOREPARAMS, "NOTICE :Not enough parameters");
418 return 0;
419 }
420
421 static int _handle_who_command(t_connection * conn, int numparams, char ** params, char * text)
422 {
423 if (numparams >= 1) {
424 int i;
425 char ** e;
426
427 e = irc_get_listelems(params[0]);
428 for (i = 0; ((e) && (e[i])); i++) {
429 irc_who(conn, e[i]);
430 }
431 irc_send(conn, RPL_ENDOFWHO, ":End of WHO list"); /* RFC2812 only requires this to be sent if a list of names was given. Undernet seems to always send it, so do we :) */
432 if (e)
433 irc_unget_listelems(e);
434 }
435 else
436 irc_send(conn, ERR_NEEDMOREPARAMS, "WHO :Not enough parameters");
437 return 0;
438 }
439
440 static int _handle_list_command(t_connection * conn, int numparams, char ** params, char * text)
441 {
442 std::string tmp;
443 irc_send(conn, RPL_LISTSTART, "Channel :Users Names"); /* backward compatibility */
444
445 if (numparams == 0)
446 {
447 t_elem const * curr;
448 class_topic Topic;
449 LIST_TRAVERSE_CONST(channellist(), curr)
450 {
451 t_channel const * channel = (const t_channel*)elem_get_data(curr);
452 char const * tempname = irc_convert_channel(channel, conn);
453 std::string topicstr = Topic.get(channel_get_name(channel));
454
455 /* FIXME: AARON: only list channels like in /channels command */
456 tmp = std::string(tempname) + " " + std::to_string(channel_get_length(channel)) + " :" + topicstr;
457
458 if (tmp.length() > MAX_IRC_MESSAGE_LEN)
459 eventlog(eventlog_level_warn, __FUNCTION__, "LISTREPLY length exceeded");
460
461 irc_send(conn, RPL_LIST, tmp.c_str());
462 }
463 }
464 else if (numparams >= 1)
465 {
466 int i;
467 char ** e;
468 class_topic Topic;
469
470 e = irc_get_listelems(params[0]);
471 /* FIXME: support wildcards! */
472
473 for (i = 0; ((e) && (e[i])); i++)
474 {
475 char const * verytemp = irc_convert_ircname(e[i]);
476 if (!verytemp)
477 continue; /* something is wrong with the name ... */
478
479 t_channel const * channel = channellist_find_channel_by_name(verytemp, NULL, NULL);
480 if (!channel)
481 continue; /* channel doesn't exist */
482
483 std::string topicstr = Topic.get(channel_get_name(channel));
484 char const * tempname = irc_convert_channel(channel, conn);
485
486 tmp = std::string(tempname) + " " + std::to_string(channel_get_length(channel)) + " :" + topicstr;
487
488 if (tmp.length() > MAX_IRC_MESSAGE_LEN)
489 eventlog(eventlog_level_warn, __FUNCTION__, "LISTREPLY length exceeded");
490
491 irc_send(conn, RPL_LIST, tmp.c_str());
492 }
493
494 if (e)
495 irc_unget_listelems(e);
496 }
497
498 irc_send(conn, RPL_LISTEND, ":End of LIST command");
499
500 return 0;
501 }
502
503 static int _handle_names_command(t_connection * conn, int numparams, char ** params, char * text)
504 {
505 t_channel * channel;
506
507 if (numparams >= 1) {
508 char ** e;
509 char const * verytemp;
510 int i;
511
512 e = irc_get_listelems(params[0]);
513 for (i = 0; ((e) && (e[i])); i++) {
514 verytemp = irc_convert_ircname(e[i]);
515
516 if (!verytemp)
517 continue; /* something is wrong with the name ... */
518 channel = channellist_find_channel_by_name(verytemp, NULL, NULL);
519 if (!channel)
520 continue; /* channel doesn't exist */
521 irc_send_rpl_namreply(conn, channel);
522 }
523 if (e)
524 irc_unget_listelems(e);
525 }
526 else if (numparams == 0) {
527 irc_send_rpl_namreply(conn, NULL);
528 }
529 return 0;
530 }
531
532 static int _handle_userhost_command(t_connection * conn, int numparams, char ** params, char * text)
533 {
534 /* FIXME: Send RPL_USERHOST */
535 return 0;
536 }
537
538 static int _handle_quit_command(t_connection * conn, int numparams, char ** params, char * text)
539 {
540 conn_quit_channel(conn, text);
541 conn_set_state(conn, conn_state_destroy);
542 return 0;
543 }
544
545 static int _handle_ison_command(t_connection * conn, int numparams, char ** params, char * text)
546 {
547 char temp[MAX_IRC_MESSAGE_LEN];
548 char first = 1;
549
550 if (numparams >= 1)
551 {
552 int i;
553
554 temp[0] = '\0';
555 for (i = 0; (i < numparams && (params) && (params[i])); i++)
556 {
557 if (connlist_find_connection_by_accountname(params[i]))
558 {
559 std::snprintf(temp, sizeof temp, "%s%s", first ? ":" : " ", params[i]);
560 first = 0;
561 }
562 }
563 irc_send(conn, RPL_ISON, temp);
564 }
565 else
566 irc_send(conn, ERR_NEEDMOREPARAMS, "ISON :Not enough parameters");
567 return 0;
568 }
569
570 static int _handle_whois_command(t_connection * conn, int numparams, char ** params, char * text)
571 {
572 std::string tmp;
573
574 if (numparams >= 1)
575 {
576 int i;
577 char ** e = irc_get_listelems(params[0]);
578 t_connection * c;
579 t_channel * chan;
580
581 for (i = 0; ((e) && (e[i])); i++)
582 {
583 if ((c = connlist_find_connection_by_accountname(e[i])))
584 {
585 if (prefs_get_hide_addr() && !(account_get_command_groups(conn_get_account(conn)) & command_get_group("/admin-addr")))
586 tmp = std::string(e[i]) + " " + std::string(clienttag_uint_to_str(conn_get_clienttag(c))) + " hidden * :PvPGN user";
587 else
588 tmp = std::string(e[i]) + " " + std::string(clienttag_uint_to_str(conn_get_clienttag(c))) + " " + std::string(addr_num_to_ip_str(conn_get_addr(c))) + " * :PvPGN user";
589 irc_send(conn, RPL_WHOISUSER, tmp.c_str());
590
591 if ((chan = conn_get_channel(conn)))
592 {
593 std::string flg;
594 auto flags = conn_get_flags(c);
595
596 if (flags & MF_BLIZZARD)
597 flg = '@';
598 else if ((flags & MF_BNET) || (flags & MF_GAVEL))
599 flg = '%';
600 else if (flags & MF_VOICE)
601 flg = '+';
602 else
603 flg = ' ';
604
605 tmp = std::string(e[i]) + " :" + flg + std::string(irc_convert_channel(chan, conn));
606 irc_send(conn, RPL_WHOISCHANNELS, tmp.c_str());
607 }
608
609 }
610 else
611 irc_send(conn, ERR_NOSUCHNICK, ":No such nick/channel");
612
613 }
614 irc_send(conn, RPL_ENDOFWHOIS, ":End of /WHOIS list");
615 if (e)
616 irc_unget_listelems(e);
617 }
618 else
619 irc_send(conn, ERR_NEEDMOREPARAMS, "WHOIS :Not enough parameters");
620 return 0;
621 }
622
623 static int _handle_part_command(t_connection * conn, int numparams, char ** params, char * text)
624 {
625 conn_part_channel(conn);
626 return 0;
627 }
628
629 }
630
631 }