| 1 | /* |
| 2 | * Copyright (C) 2007 Pelish ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "common/setup_before.h" |
| 20 | #include "handle_irc_common.h" |
| 21 | |
| 22 | #include <cstring> |
| 23 | #include <cctype> |
| 24 | #include <cstdlib> |
| 25 | #include <string> |
| 26 | |
| 27 | #include "compat/strcasecmp.h" |
| 28 | #include "common/eventlog.h" |
| 29 | #include "common/util.h" |
| 30 | #include "common/irc_protocol.h" |
| 31 | |
| 32 | #include "handle_irc.h" |
| 33 | #include "handle_wol.h" |
| 34 | #include "handle_wserv.h" |
| 35 | |
| 36 | #include "prefs.h" |
| 37 | #include "command.h" |
| 38 | #include "irc.h" |
| 39 | |
| 40 | #include "common/setup_after.h" |
| 41 | |
| 42 | namespace pvpgn |
| 43 | { |
| 44 | |
| 45 | namespace bnetd |
| 46 | { |
| 47 | |
| 48 | static int handle_irc_common_con_command(t_connection * conn, char const * command, int numparams, char ** params, char * text) |
| 49 | { |
| 50 | if (!conn) { |
| 51 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | switch (conn_get_class(conn)) { |
| 56 | case conn_class_irc: |
| 57 | return handle_irc_con_command(conn, command, numparams, params, text); |
| 58 | case conn_class_wserv: |
| 59 | return handle_wserv_con_command(conn, command, numparams, params, text); |
| 60 | case conn_class_wol: |
| 61 | case conn_class_wladder: |
| 62 | case conn_class_wgameres: |
| 63 | return handle_wol_con_command(conn, command, numparams, params, text); |
| 64 | default: |
| 65 | return handle_irc_con_command(conn, command, numparams, params, text); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static int handle_irc_common_log_command(t_connection * conn, char const * command, int numparams, char ** params, char * text) |
| 70 | { |
| 71 | if (!conn) { |
| 72 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | switch (conn_get_class(conn)) { |
| 77 | case conn_class_irc: |
| 78 | return handle_irc_log_command(conn, command, numparams, params, text); |
| 79 | case conn_class_wol: |
| 80 | case conn_class_wgameres: |
| 81 | return handle_wol_log_command(conn, command, numparams, params, text); |
| 82 | default: |
| 83 | return handle_irc_log_command(conn, command, numparams, params, text); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static int handle_irc_common_set_class(t_connection * conn, char const * command, int numparams, char ** params, char * text) |
| 88 | { |
| 89 | if (!conn) { |
| 90 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | if (conn_get_class(conn) != conn_class_ircinit) { |
| 95 | DEBUG0("FIXME: conn_get_class(conn) != conn_class_ircinit"); |
| 96 | return -1; |
| 97 | } |
| 98 | else { |
| 99 | if (strcasecmp(command, "VERCHK") == 0) { |
| 100 | DEBUG0("Got WSERV packet"); |
| 101 | if (std::strcmp(prefs_get_wolv2_addrs(), "") != 0) |
| 102 | conn_set_class(conn, conn_class_wserv); |
| 103 | else |
| 104 | conn_set_state(conn, conn_state_destroy); |
| 105 | return 0; |
| 106 | } |
| 107 | else if (strcasecmp(command, "CVERS") == 0) { |
| 108 | DEBUG0("Got WOL packet"); |
| 109 | /* FIXME: We can check it not by address but check if client is supported by tag_check_in_list() */ |
| 110 | if ((std::strcmp(prefs_get_wolv1_addrs(), "") != 0) || (std::strcmp(prefs_get_wolv2_addrs(), "") != 0)) |
| 111 | conn_set_class(conn, conn_class_wol); |
| 112 | else |
| 113 | conn_set_state(conn, conn_state_destroy); |
| 114 | return 0; |
| 115 | } |
| 116 | else if ((strcasecmp(command, "LISTSEARCH") == 0) || |
| 117 | (strcasecmp(command, "RUNGSEARCH") == 0) || |
| 118 | (strcasecmp(command, "HIGHSCORE") == 0)) { |
| 119 | DEBUG0("Got WOL Ladder packet"); |
| 120 | if (std::strcmp(prefs_get_wolv2_addrs(), "") != 0) |
| 121 | conn_set_class(conn, conn_class_wladder); /* is handled in handle_wol.* now */ |
| 122 | else |
| 123 | conn_set_state(conn, conn_state_destroy); |
| 124 | return 0; |
| 125 | } |
| 126 | else if ((strcasecmp(command, "CRYPT") == 0) || |
| 127 | (strcasecmp(command, "LOGIN") == 0)) { |
| 128 | DEBUG0("Got GameSpy packet"); |
| 129 | if (std::strcmp(prefs_get_irc_addrs(), "") != 0) |
| 130 | conn_set_class(conn, conn_class_irc); |
| 131 | // conn_set_class(conn,conn_class_gspy_peerchat); |
| 132 | else |
| 133 | conn_set_state(conn, conn_state_destroy); |
| 134 | return 0; |
| 135 | } |
| 136 | else { |
| 137 | DEBUG0("Got IRC packet"); |
| 138 | if (std::strcmp(prefs_get_irc_addrs(), "") != 0) |
| 139 | conn_set_class(conn, conn_class_irc); |
| 140 | else |
| 141 | conn_set_state(conn, conn_state_destroy); |
| 142 | return 0; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static int handle_irc_common_line(t_connection * conn, char const * ircline) |
| 148 | { |
| 149 | /* [:prefix] <command> [[param1] [param2] ... [paramN]] [:<text>] */ |
| 150 | char * line; /* copy of ircline */ |
| 151 | char * prefix = NULL; /* optional; mostly NULL */ |
| 152 | char * command; /* mandatory */ |
| 153 | char ** params = NULL; /* optional (array of params) */ |
| 154 | char * text = NULL; /* optional */ |
| 155 | char * bnet_command = NULL; /* amadeo: used for battle.net.commands */ |
| 156 | int unrecognized_before = 0; |
| 157 | int linelen; /* amadeo: counter for stringlenghts */ |
| 158 | int numparams = 0; |
| 159 | char * tempparams; |
| 160 | int i; |
| 161 | |
| 162 | if (!conn) { |
| 163 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 164 | return -1; |
| 165 | } |
| 166 | if (!ircline) { |
| 167 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL ircline"); |
| 168 | return -1; |
| 169 | } |
| 170 | if (ircline[0] == '\0') { |
| 171 | /** |
| 172 | * PELISH: We dont send warning about that because it is client-side problem. |
| 173 | * eventlog(eventlog_level_error,__FUNCTION__,"got empty ircline"); |
| 174 | */ |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | //amadeo: code was sent by some unknown fellow of pvpgn, prevents buffer-overflow for |
| 179 | // too long irc-lines |
| 180 | //PELISH: According to RFC2812 we do truncation on 512 byte |
| 181 | |
| 182 | if (std::strlen(ircline) > MAX_IRC_MESSAGE_LEN) { |
| 183 | char * tmp = (char *)ircline; |
| 184 | eventlog(eventlog_level_warn, __FUNCTION__, "line too long, truncation..."); |
| 185 | tmp[MAX_IRC_MESSAGE_LEN] = '\0'; |
| 186 | } |
| 187 | |
| 188 | line = xstrdup(ircline); |
| 189 | |
| 190 | /* split the message */ |
| 191 | if (line[0] == ':') { |
| 192 | /* The prefix is optional and is rarely provided */ |
| 193 | prefix = line; |
| 194 | if (!(command = std::strchr(line, ' '))) { |
| 195 | eventlog(eventlog_level_warn, __FUNCTION__, "got malformed line (missing command)"); |
| 196 | xfree(line); |
| 197 | return -1; |
| 198 | } |
| 199 | *command++ = '\0'; |
| 200 | } |
| 201 | else { |
| 202 | /* In most cases command is the first thing on the line */ |
| 203 | command = line; |
| 204 | } |
| 205 | |
| 206 | tempparams = std::strchr(command, ' '); |
| 207 | if (tempparams) { |
| 208 | *tempparams++ = '\0'; |
| 209 | if (tempparams[0] == ':') { |
| 210 | text = tempparams + 1; /* theres just text, no params. skip the colon */ |
| 211 | } |
| 212 | else { |
| 213 | for (i = 0; tempparams[i] != '\0'; i++) { |
| 214 | if ((tempparams[i] == ' ') && (tempparams[i + 1] == ':')) { |
| 215 | text = tempparams + i; |
| 216 | *text++ = '\0'; |
| 217 | text++; /* skip the colon */ |
| 218 | break; /* text found, stop search */ |
| 219 | } |
| 220 | } |
| 221 | params = irc_get_paramelems(tempparams); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (params) { |
| 226 | /* count parameters */ |
| 227 | for (numparams = 0; params[numparams]; numparams++); |
| 228 | } |
| 229 | |
| 230 | { |
| 231 | std::string paramtemp; |
| 232 | bool first = true; |
| 233 | for (i = 0; ((numparams > 0) && (params[i])); i++) |
| 234 | { |
| 235 | if (first) |
| 236 | { |
| 237 | first = false; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | paramtemp.append(" "); |
| 242 | } |
| 243 | |
| 244 | paramtemp.append("\"" + std::string(params[i]) + "\""); |
| 245 | } |
| 246 | |
| 247 | eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got \"{}\" \"{}\" [{}] \"{}\"", conn_get_socket(conn), ((prefix) ? (prefix) : ("")), command, paramtemp, ((text) ? (text) : (""))); |
| 248 | } |
| 249 | |
| 250 | if (conn_get_class(conn) == conn_class_ircinit) { |
| 251 | handle_irc_common_set_class(conn, command, numparams, params, text); |
| 252 | } |
| 253 | |
| 254 | if (conn_get_state(conn) == conn_state_connected) { |
| 255 | conn_set_state(conn, conn_state_bot_username); /* PELISH: What is this for? */ |
| 256 | |
| 257 | if ((conn_get_class(conn) != conn_class_wserv) && |
| 258 | (conn_get_class(conn) != conn_class_wladder)) { |
| 259 | |
| 260 | t_timer_data temp; |
| 261 | temp.n = prefs_get_irc_latency(); |
| 262 | conn_test_latency(conn, std::time(NULL), temp); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (handle_irc_common_con_command(conn, command, numparams, params, text) != -1) {} |
| 267 | else if (conn_get_state(conn) != conn_state_loggedin) |
| 268 | { |
| 269 | std::string tmp(":Unrecognized command \"" + std::string(command) + "\" (before login)"); |
| 270 | if (tmp.length() > MAX_IRC_MESSAGE_LEN) |
| 271 | irc_send(conn, ERR_UNKNOWNCOMMAND, tmp.c_str()); |
| 272 | else |
| 273 | irc_send(conn, ERR_UNKNOWNCOMMAND, ":Unrecognized command (before login)"); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | /* command is handled later */ |
| 278 | unrecognized_before = 1; |
| 279 | } |
| 280 | /* --- The following should only be executable after login --- */ |
| 281 | if ((conn_get_state(conn) == conn_state_loggedin) && (unrecognized_before)) { |
| 282 | |
| 283 | if (handle_irc_common_log_command(conn, command, numparams, params, text) != -1) {} |
| 284 | else if ((strstart(command, "LAG") != 0) && (strstart(command, "JOIN") != 0)){ |
| 285 | linelen = std::strlen(ircline); |
| 286 | bnet_command = (char*)xmalloc(linelen + 2); |
| 287 | bnet_command[0] = '/'; |
| 288 | std::strcpy(bnet_command + 1, ircline); |
| 289 | handle_command(conn, bnet_command); |
| 290 | xfree((void*)bnet_command); |
| 291 | } |
| 292 | } /* loggedin */ |
| 293 | if (params) |
| 294 | irc_unget_paramelems(params); |
| 295 | xfree(line); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | extern int handle_irc_common_packet(t_connection * conn, t_packet const * const packet) |
| 301 | { |
| 302 | unsigned int i; |
| 303 | char ircline[MAX_IRC_MESSAGE_LEN]; |
| 304 | char const * data; |
| 305 | |
| 306 | if (!packet) { |
| 307 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet"); |
| 308 | return -1; |
| 309 | } |
| 310 | if ((conn_get_class(conn) != conn_class_ircinit) && |
| 311 | (conn_get_class(conn) != conn_class_irc) && |
| 312 | (conn_get_class(conn) != conn_class_wol) && |
| 313 | (conn_get_class(conn) != conn_class_wserv) && |
| 314 | (conn_get_class(conn) != conn_class_wladder)) { |
| 315 | eventlog(eventlog_level_error, __FUNCTION__, "FIXME: handle_irc_packet without any reason (conn->class != conn_class_irc/ircinit/wol/wserv...)"); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | // eventlog(eventlog_level_debug,__FUNCTION__,"got \"%s\"",packet_get_raw_data_const(packet,0)); |
| 320 | |
| 321 | std::memset(ircline, 0, sizeof(ircline)); |
| 322 | data = conn_get_ircline(conn); /* fetch current status */ |
| 323 | if (data) |
| 324 | std::snprintf(ircline, sizeof ircline, "%s", data); |
| 325 | unsigned ircpos = std::strlen(ircline); |
| 326 | data = (const char *)packet_get_raw_data_const(packet, 0); |
| 327 | |
| 328 | for (i = 0; i < packet_get_size(packet); i++) { |
| 329 | if (data[i] == '\n') { |
| 330 | /* end of line */ |
| 331 | handle_irc_common_line(conn, ircline); |
| 332 | std::memset(ircline, 0, sizeof(ircline)); |
| 333 | ircpos = 0; |
| 334 | } |
| 335 | else { |
| 336 | if (ircpos < MAX_IRC_MESSAGE_LEN - 1) |
| 337 | ircline[ircpos++] = data[i]; |
| 338 | else { |
| 339 | ircpos++; /* for the statistic :) */ |
| 340 | eventlog(eventlog_level_warn, __FUNCTION__, "[{}] client exceeded maximum allowed message length by {} characters", conn_get_socket(conn), ircpos - MAX_IRC_MESSAGE_LEN); |
| 341 | if (ircpos > 100 + MAX_IRC_MESSAGE_LEN) { |
| 342 | /* automatic flood protection */ |
| 343 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] excess flood", conn_get_socket(conn)); |
| 344 | return -1; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | conn_set_ircline(conn, ircline); /* write back current status */ |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | } |
| 354 | |
| 355 | } |