Debian Codegraph @

src/bnetd/handle_irc.cpp

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