| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #ifdef WIN32 |
| 20 | #include "windows.h" |
| 21 | #endif |
| 22 | |
| 23 | #include <cstdio> |
| 24 | #include <cstring> |
| 25 | |
| 26 | #if defined(POSIX) |
| 27 | #include <sys/time.h> |
| 28 | #include <unistd.h> |
| 29 | #endif |
| 30 | |
| 31 | #include "chat_api.h" |
| 32 | #include "grdefs.h" |
| 33 | #include "mtstrings.h" |
| 34 | #include "networking.h" |
| 35 | |
| 36 | extern const char *GetString(int d); |
| 37 | |
| 38 | typedef void *(*mem_malloc_fp)(int size); |
| 39 | extern mem_malloc_fp DLLmem_malloc; |
| 40 | |
| 41 | typedef void (*mem_free_fp)(void *memblock); |
| 42 | extern mem_free_fp DLLmem_free; |
| 43 | |
| 44 | typedef int (*nw_Asyncgethostbyname_fp)(uint32_t *ip, int command, const char *hostname); |
| 45 | extern nw_Asyncgethostbyname_fp DLLnw_Asyncgethostbyname; |
| 46 | |
| 47 | typedef int (*PollUI_fp)(); |
| 48 | extern PollUI_fp DLLPollUI; |
| 49 | |
| 50 | #ifndef RELEASE |
| 51 | #define DLLmprintf(...) DLLDebug_ConsolePrintf(__VA_ARGS__) |
| 52 | #else |
| 53 | #define DLLmprintf(...) |
| 54 | #endif |
| 55 | |
| 56 | typedef void (*Debug_ConsolePrintf_fp)(int n, const char *format, ...); |
| 57 | extern Debug_ConsolePrintf_fp DLLDebug_ConsolePrintf; |
| 58 | #define MAXCHATBUFFER 500 |
| 59 | |
| 60 | SOCKET Chatsock; |
| 61 | SOCKADDR_IN Chataddr; |
| 62 | int Socket_connecting = 0; |
| 63 | char Nick_name[33]; |
| 64 | char Orignial_nick_name[33]; |
| 65 | int Nick_variety = 0; |
| 66 | char szChat_channel[33] = ""; |
| 67 | char Input_chat_buffer[MAXCHATBUFFER] = ""; |
| 68 | char Chat_tracker_id[33]; |
| 69 | char Getting_user_channel_info_for[33] = ""; |
| 70 | char Getting_user_tracker_info_for[33] = ""; |
| 71 | int Getting_user_channel_error = 0; |
| 72 | int Getting_user_tracker_error = 0; |
| 73 | char User_req_tracker_id[100] = ""; // These are oversized for saftey |
| 74 | char User_req_channel[100] = ""; |
| 75 | char *User_list = nullptr; |
| 76 | char *Chan_list = nullptr; |
| 77 | int Socket_connected = 0; |
| 78 | int Chat_server_connected = 0; |
| 79 | int Joining_channel = 0; |
| 80 | int Joined_channel = 0; |
| 81 | int GettingChannelList = 0; |
| 82 | int GettingUserTID = 0; |
| 83 | int GettingUserChannel = 0; |
| 84 | |
| 85 | Chat_user *Firstuser, *Curruser; |
| 86 | Chat_command *Firstcommand, *Currcommand; |
| 87 | Chat_channel *Firstchannel, *Currchannel; |
| 88 | |
| 89 | void ChatInit() { |
| 90 | Socket_connecting = 0; |
| 91 | Nick_name[0] = 0; |
| 92 | Orignial_nick_name[0] = 0; |
| 93 | Nick_variety = 0; |
| 94 | szChat_channel[0] = '\0'; |
| 95 | Input_chat_buffer[0] = '\0'; |
| 96 | Chat_tracker_id[0] = 0; |
| 97 | Getting_user_channel_info_for[0] = '\0'; |
| 98 | Getting_user_tracker_info_for[0] = '\0'; |
| 99 | Getting_user_channel_error = 0; |
| 100 | Getting_user_tracker_error = 0; |
| 101 | User_req_tracker_id[0] = '\0'; |
| 102 | User_req_channel[0] = '\0'; |
| 103 | User_list = nullptr; |
| 104 | Chan_list = nullptr; |
| 105 | Socket_connected = 0; |
| 106 | Chat_server_connected = 0; |
| 107 | Joining_channel = 0; |
| 108 | Joined_channel = 0; |
| 109 | GettingChannelList = 0; |
| 110 | GettingUserTID = 0; |
| 111 | GettingUserChannel = 0; |
| 112 | } |
| 113 | |
| 114 | // Return codes: |
| 115 | //-2 Already connected |
| 116 | //-1 Failed to connect |
| 117 | // 0 Connecting |
| 118 | // 1 Connected |
| 119 | // Call it once with the server IP address, and it will return immediately |
| 120 | // with 0. Keep calling it until it returns something other than 0 |
| 121 | // note: the nickname may be changed if someone with that name already |
| 122 | // exists (Scourge1 for instance) |
| 123 | int ConnectToChatServer(const char *serveraddr, int16_t chat_port, char *nickname, char *trackerid) { |
| 124 | char signon_str[100]; |
| 125 | |
| 126 | // if(Socket_connected && ) return -2; |
| 127 | |
| 128 | if (!Socket_connecting) { |
| 129 | strcpy(Nick_name, nickname); |
| 130 | for (uint32_t l = 0; l < strlen(Nick_name); l++) |
| 131 | if (Nick_name[l] == ' ') |
| 132 | Nick_name[l] = '_'; |
| 133 | strcpy(Orignial_nick_name, nickname); |
| 134 | strcpy(Chat_tracker_id, trackerid); |
| 135 | |
| 136 | Firstuser = nullptr; |
| 137 | Firstcommand = nullptr; |
| 138 | Chat_server_connected = 0; |
| 139 | FlushChatCommandQueue(); |
| 140 | |
| 141 | if (chat_port == 0) { |
| 142 | // AfxMessageBox("Invalid chat port, must be host.com:port (ie. irc.dal.net:6667)"); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | Chatsock = socket(AF_INET, SOCK_STREAM, 0); |
| 147 | if (INVALID_SOCKET == Chatsock) { |
| 148 | // AfxMessageBox("Unable to open socket!"); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | memset(&Chataddr, 0, sizeof(SOCKADDR_IN)); |
| 153 | Chataddr.sin_family = AF_INET; |
| 154 | Chataddr.sin_addr.s_addr = INADDR_ANY; |
| 155 | Chataddr.sin_port = 0; |
| 156 | |
| 157 | if (SOCKET_ERROR == bind(Chatsock, (SOCKADDR *)&Chataddr, sizeof(sockaddr))) { |
| 158 | // AfxMessageBox("Unable to bind socket!"); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | #ifdef WIN32 |
| 163 | unsigned long arg = 1; |
| 164 | ioctlsocket(Chatsock, FIONBIO, &arg); |
| 165 | #else // WIN32 |
| 166 | fcntl(Chatsock, F_SETFL, fcntl(Chatsock, F_GETFL, 0) | O_NONBLOCK); |
| 167 | #endif |
| 168 | |
| 169 | /* |
| 170 | HOSTENT *he; |
| 171 | he = gethostbyname(chat_server); |
| 172 | if(!he) |
| 173 | { |
| 174 | //AfxMessageBox("Unable to gethostbyname.\n"); |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | memcpy(&Chataddr.sin_addr.s_addr, he->h_addr_list[0],4);//&iaddr, 4); |
| 179 | |
| 180 | */ |
| 181 | |
| 182 | int rcode; |
| 183 | uint32_t ip; |
| 184 | |
| 185 | DLLnw_Asyncgethostbyname(&ip, NW_AGHBN_LOOKUP, serveraddr); |
| 186 | do { |
| 187 | rcode = DLLnw_Asyncgethostbyname(&ip, NW_AGHBN_READ, serveraddr); |
| 188 | if (99 == DLLPollUI()) { |
| 189 | return 0; |
| 190 | } |
| 191 | } while (rcode == 0); |
| 192 | |
| 193 | if (rcode != 1) { |
| 194 | DLLmprintf(0, "Unable to gethostbyname(\"%s\").\n", serveraddr); |
| 195 | DLLmprintf(0, "WSAGetLastError() returned %d.\n", WSAGetLastError()); |
| 196 | DLLnw_Asyncgethostbyname(nullptr, NW_AGHBN_CANCEL, nullptr); |
| 197 | return 0; |
| 198 | } |
| 199 | memcpy(&Chataddr.sin_addr.s_addr, &ip, 4); |
| 200 | |
| 201 | Chataddr.sin_port = htons(chat_port); |
| 202 | |
| 203 | if (SOCKET_ERROR == connect(Chatsock, (SOCKADDR *)&Chataddr, sizeof(SOCKADDR_IN))) { |
| 204 | int ret = WSAGetLastError(); |
| 205 | #if !defined(POSIX) |
| 206 | if (WSAEWOULDBLOCK == WSAGetLastError()) |
| 207 | #else |
| 208 | if (EINPROGRESS == ret || 0 == ret) |
| 209 | #endif |
| 210 | { |
| 211 | DLLmprintf(0, "Beginning socket connect\n"); |
| 212 | Socket_connecting = 1; |
| 213 | return 0; |
| 214 | } |
| 215 | } else { |
| 216 | // This should never happen, connect should always return WSAEWOULDBLOCK |
| 217 | DLLmprintf(0, "connect returned too soon!\n"); |
| 218 | Socket_connecting = 1; |
| 219 | Socket_connected = 1; |
| 220 | DLLmprintf(0, "Socket connected, sending user and nickname request\n"); |
| 221 | snprintf(signon_str, sizeof(signon_str), "/USER %s %s %s :%s", "user", "user", "user", Chat_tracker_id); |
| 222 | SendChatString(signon_str, 1); |
| 223 | snprintf(signon_str, sizeof(signon_str), "/NICK %s", Nick_name); |
| 224 | SendChatString(signon_str, 1); |
| 225 | return 0; |
| 226 | } |
| 227 | } else { |
| 228 | if (Chat_server_connected) { |
| 229 | return 1; |
| 230 | } |
| 231 | |
| 232 | if (!Socket_connected) { |
| 233 | // Do a few select to check for an error, or to see if we are writeable (connected) |
| 234 | fd_set write_fds, error_fds; |
| 235 | |
| 236 | #ifdef WIN32 |
| 237 | TIMEVAL timeout; |
| 238 | #else |
| 239 | struct timeval timeout; |
| 240 | #endif |
| 241 | |
| 242 | timeout.tv_sec = 0; |
| 243 | timeout.tv_usec = 0; |
| 244 | |
| 245 | FD_ZERO(&write_fds); |
| 246 | FD_SET(Chatsock, &write_fds); |
| 247 | // Writable -- that means it's connected |
| 248 | if (select(Chatsock + 1, nullptr, &write_fds, nullptr, &timeout)) { |
| 249 | Socket_connected = 1; |
| 250 | DLLmprintf(0, "Socket connected, sending user and nickname request\n"); |
| 251 | snprintf(signon_str, sizeof(signon_str), "/USER %s %s %s :%s", "user", "user", "user", Chat_tracker_id); |
| 252 | SendChatString(signon_str, 1); |
| 253 | snprintf(signon_str, sizeof(signon_str), "/NICK %s", Nick_name); |
| 254 | SendChatString(signon_str, 1); |
| 255 | return 0; |
| 256 | // Now we are waiting for Chat_server_connected |
| 257 | } |
| 258 | FD_ZERO(&error_fds); |
| 259 | FD_SET(Chatsock, &error_fds); |
| 260 | // error -- that means it's not going to connect |
| 261 | if (select(Chatsock + 1, nullptr, nullptr, &error_fds, &timeout)) { |
| 262 | DLLmprintf(0, "Select returned an error!\n"); |
| 263 | return -1; |
| 264 | } |
| 265 | return 0; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | // Call it to close the connection. It returns immediately |
| 273 | void DisconnectFromChatServer() { |
| 274 | if (!Socket_connected) |
| 275 | return; |
| 276 | SendChatString("/QUIT", 1); |
| 277 | shutdown(Chatsock, 2); |
| 278 | #ifdef WIN32 |
| 279 | closesocket(Chatsock); |
| 280 | #else |
| 281 | close(Chatsock); |
| 282 | #endif |
| 283 | Socket_connecting = 0; |
| 284 | Socket_connected = 0; |
| 285 | Input_chat_buffer[0] = '\0'; |
| 286 | if (User_list) { |
| 287 | DLLmem_free(User_list); |
| 288 | User_list = nullptr; |
| 289 | } |
| 290 | if (Chan_list) { |
| 291 | DLLmem_free(Chan_list); |
| 292 | Chan_list = nullptr; |
| 293 | } |
| 294 | |
| 295 | Chat_server_connected = 0; |
| 296 | Joining_channel = 0; |
| 297 | Joined_channel = 0; |
| 298 | RemoveAllChatUsers(); |
| 299 | FlushChatCommandQueue(); |
| 300 | FlushChannelList(); |
| 301 | } |
| 302 | |
| 303 | // returns NULL if no line is there to print, otherwise returns a string to |
| 304 | // print (all preformatted of course) |
| 305 | const char *GetChatText() { |
| 306 | |
| 307 | if (!Socket_connected) |
| 308 | return nullptr; |
| 309 | |
| 310 | // ChatGetString will do the formatting |
| 311 | return ChatGetString(); |
| 312 | } |
| 313 | |
| 314 | // Send a string to be sent as chat, or scanned for messages (/msg <user> |
| 315 | // string) |
| 316 | const char *SendChatString(const char *line, int raw) { |
| 317 | char szCmd[200]; |
| 318 | char szTarget[50]; |
| 319 | if (!Socket_connected) |
| 320 | return nullptr; |
| 321 | |
| 322 | if (line[0] == '/') { |
| 323 | |
| 324 | // Start off by getting the command |
| 325 | strcpy(szCmd, GetWordNum(0, line + 1)); |
| 326 | if (stricmp(szCmd, "msg") == 0) { |
| 327 | strcpy(szTarget, GetWordNum(1, line + 1)); |
| 328 | snprintf(szCmd, sizeof(szCmd), "PRIVMSG %s :%s\n\r", szTarget, line + strlen("/msg ") + strlen(szTarget) + 1); |
| 329 | send(Chatsock, szCmd, strlen(szCmd), 0); |
| 330 | szCmd[strlen(szCmd) - 2] = '\0'; |
| 331 | return ParseIRCMessage(szCmd, MSG_LOCAL); |
| 332 | } |
| 333 | if (stricmp(szCmd, "me") == 0) { |
| 334 | snprintf(szCmd, sizeof(szCmd), "PRIVMSG %s :\001ACTION %s\001\n\r", szChat_channel, line + strlen("/me ")); |
| 335 | send(Chatsock, szCmd, strlen(szCmd), 0); |
| 336 | szCmd[strlen(szCmd) - 2] = '\0'; |
| 337 | return ParseIRCMessage(szCmd, MSG_LOCAL); |
| 338 | } |
| 339 | if (stricmp(szCmd, "xyz") == 0) { |
| 340 | // Special command to send raw irc commands |
| 341 | snprintf(szCmd, sizeof(szCmd), "%s\n\r", line + strlen("/xyz ")); |
| 342 | send(Chatsock, szCmd, strlen(szCmd), 0); |
| 343 | return nullptr; |
| 344 | } |
| 345 | if (stricmp(szCmd, "list") == 0) { |
| 346 | snprintf(szCmd, sizeof(szCmd), "%s\n\r", line + 1); |
| 347 | send(Chatsock, szCmd, strlen(szCmd), 0); |
| 348 | return nullptr; |
| 349 | } |
| 350 | if (raw) { |
| 351 | snprintf(szCmd, sizeof(szCmd), "%s\n\r", line + 1); |
| 352 | send(Chatsock, szCmd, strlen(szCmd), 0); |
| 353 | return nullptr; |
| 354 | } |
| 355 | return "Unrecognized command"; |
| 356 | |
| 357 | } else { |
| 358 | if (szChat_channel[0]) { |
| 359 | snprintf(szCmd, sizeof(szCmd), "PRIVMSG %s :%s\n\r", szChat_channel, line); |
| 360 | send(Chatsock, szCmd, strlen(szCmd), 0); |
| 361 | szCmd[strlen(szCmd) - 2] = '\0'; |
| 362 | return ParseIRCMessage(szCmd, MSG_LOCAL); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return nullptr; |
| 367 | } |
| 368 | |
| 369 | // Returns a structure which contains a command and possible some data (like |
| 370 | // a user joining or leaving) if one is waiting |
| 371 | // This tells you if you need to add a user from the userlist, remove a user, |
| 372 | // etc. Also for status messages, like if you get knocked |
| 373 | // off the server for some reason. |
| 374 | Chat_command *GetChatCommand() { |
| 375 | if (!Socket_connected) |
| 376 | return nullptr; |
| 377 | return GetChatCommandFromQueue(); |
| 378 | } |
| 379 | |
| 380 | // This function returns a list of users in the current channel, in one |
| 381 | // string, separated by spaces, terminated by a null |
| 382 | // (Spaces aren't allowed as part of a nickname) |
| 383 | char *GetChatUserList() { |
| 384 | int iuser_list_length = 0; |
| 385 | ; |
| 386 | if (User_list) { |
| 387 | DLLmem_free(User_list); |
| 388 | User_list = nullptr; |
| 389 | } |
| 390 | if (!Socket_connected) |
| 391 | return nullptr; |
| 392 | |
| 393 | Curruser = Firstuser; |
| 394 | while (Curruser) { |
| 395 | iuser_list_length += strlen(Curruser->nick_name) + 1; |
| 396 | Curruser = Curruser->next; |
| 397 | } |
| 398 | Curruser = Firstuser; |
| 399 | User_list = (char *)DLLmem_malloc(iuser_list_length + 1); |
| 400 | User_list[0] = '\0'; |
| 401 | while (Curruser) { |
| 402 | strcat(User_list, Curruser->nick_name); |
| 403 | strcat(User_list, " "); |
| 404 | Curruser = Curruser->next; |
| 405 | } |
| 406 | |
| 407 | return User_list; |
| 408 | } |
| 409 | |
| 410 | // Call this to set/join a channel. Since we can't be sure that we will be |
| 411 | // able to join that channel, check it for completion |
| 412 | // You can't be in more than one channel at a time with this API, so you |
| 413 | // leave the current channel before trying to join |
| 414 | // a new one. Because of this if the join fails, make sure you try to join |
| 415 | // another channel, or the user wont be able to chat |
| 416 | //-1 Failed to join |
| 417 | // 0 joining |
| 418 | // 1 successfully joined |
| 419 | int SetNewChatChannel(const char *channel) { |
| 420 | char partstr[100]; |
| 421 | if (!Socket_connected) |
| 422 | return -1; |
| 423 | if (Joining_channel == 1) { |
| 424 | if (Joined_channel == 1) { |
| 425 | // We made it in! |
| 426 | Joining_channel = 0; |
| 427 | return 1; |
| 428 | } else if (Joined_channel == -1) { |
| 429 | // Error -- we got a message that the channel was invite only, or we were banned or something |
| 430 | Joining_channel = 0; |
| 431 | strcpy(szChat_channel, ""); |
| 432 | return -1; |
| 433 | } |
| 434 | } else { |
| 435 | if (szChat_channel[0]) { |
| 436 | snprintf(partstr, sizeof(partstr), "/PART %s", szChat_channel); |
| 437 | SendChatString(partstr, 1); |
| 438 | } |
| 439 | strcpy(szChat_channel, channel); |
| 440 | snprintf(partstr, sizeof(partstr), "/JOIN %s", szChat_channel); |
| 441 | SendChatString(partstr, 1); |
| 442 | Joining_channel = 1; |
| 443 | Joined_channel = 0; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | const char *ChatGetString() { |
| 450 | char ch[2]; |
| 451 | char *p; |
| 452 | int bytesread = 1; |
| 453 | static char return_string[MAXCHATBUFFER]; |
| 454 | |
| 455 | while ((bytesread != 0) && (bytesread != SOCKET_ERROR)) { |
| 456 | bytesread = recv(Chatsock, ch, 1, 0); |
| 457 | if (bytesread == SOCKET_ERROR) { |
| 458 | uint32_t lerror = WSAGetLastError(); |
| 459 | #if !defined(POSIX) |
| 460 | if (WSAEWOULDBLOCK != lerror) |
| 461 | #else |
| 462 | if (WSAEWOULDBLOCK != lerror && 0 != lerror) |
| 463 | #endif |
| 464 | { |
| 465 | DLLmprintf(0, "recv caused an error: %d\n", lerror); |
| 466 | } |
| 467 | return nullptr; |
| 468 | } |
| 469 | if (bytesread) { |
| 470 | ch[1] = '\0'; |
| 471 | // DLLmprintf(0,ch); |
| 472 | if ((ch[0] == 0x0a) || (ch[0] == 0x0d)) { |
| 473 | if (Input_chat_buffer[0] == '\0') { |
| 474 | // Blank line, ignore it |
| 475 | return nullptr; |
| 476 | } |
| 477 | strcpy(return_string, Input_chat_buffer); |
| 478 | Input_chat_buffer[0] = '\0'; |
| 479 | // DLLmprintf(0,"->|%s\n",return_string); |
| 480 | p = ParseIRCMessage(return_string, MSG_REMOTE); |
| 481 | |
| 482 | return p; |
| 483 | } |
| 484 | strcat(Input_chat_buffer, ch); |
| 485 | } else { |
| 486 | // Select said we had read data, but 0 bytes read means disconnected |
| 487 | DLLmprintf(0, "Disconnected! Doh!"); |
| 488 | AddChatCommandToQueue(CC_DISCONNECTED, nullptr, 0); |
| 489 | return nullptr; |
| 490 | } |
| 491 | } |
| 492 | return nullptr; |
| 493 | } |
| 494 | |
| 495 | const char *GetWordNum(int num, const char *l_String) { |
| 496 | static char strreturn[600]; |
| 497 | static char ptokstr[600]; |
| 498 | char seps[10] = " \n\r\t"; |
| 499 | char *token, *strstart; |
| 500 | |
| 501 | strstart = ptokstr; |
| 502 | |
| 503 | strcpy(ptokstr, l_String); |
| 504 | |
| 505 | token = strtok(ptokstr, seps); |
| 506 | |
| 507 | for (int i = 0; i != num; i++) { |
| 508 | token = strtok(nullptr, seps); |
| 509 | } |
| 510 | if (token) { |
| 511 | strcpy(strreturn, token); |
| 512 | } else { |
| 513 | return ""; |
| 514 | } |
| 515 | // check for the ':' char.... |
| 516 | if (token[0] == ':') { |
| 517 | // Its not pretty, but it works, return the rest of the string |
| 518 | strcpy(strreturn, l_String + ((token - strstart) + 1)); |
| 519 | } |
| 520 | |
| 521 | // return the appropriate response. |
| 522 | return strreturn; |
| 523 | } |
| 524 | |
| 525 | int AddChatUser(const char *nickname) { |
| 526 | Curruser = Firstuser; |
| 527 | while (Curruser) { |
| 528 | if (stricmp(nickname, Curruser->nick_name) == 0) |
| 529 | return 0; |
| 530 | Curruser = Curruser->next; |
| 531 | } |
| 532 | |
| 533 | Curruser = Firstuser; |
| 534 | if (Firstuser == nullptr) { |
| 535 | Firstuser = (Chat_user *)DLLmem_malloc(sizeof(Chat_user)); |
| 536 | // ASSERT(Firstuser); |
| 537 | strcpy(Firstuser->nick_name, nickname); |
| 538 | Firstuser->next = nullptr; |
| 539 | AddChatCommandToQueue(CC_USER_JOINING, nickname, strlen(nickname) + 1); |
| 540 | return 1; |
| 541 | } else { |
| 542 | while (Curruser->next) { |
| 543 | Curruser = Curruser->next; |
| 544 | } |
| 545 | Curruser->next = (Chat_user *)DLLmem_malloc(sizeof(Chat_user)); |
| 546 | Curruser = Curruser->next; |
| 547 | // ASSERT(Curruser); |
| 548 | strcpy(Curruser->nick_name, nickname); |
| 549 | Curruser->next = nullptr; |
| 550 | AddChatCommandToQueue(CC_USER_JOINING, nickname, strlen(nickname) + 1); |
| 551 | return 1; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | int RemoveChatUser(char *nickname) { |
| 556 | Chat_user *prv_user = nullptr; |
| 557 | |
| 558 | Curruser = Firstuser; |
| 559 | while (Curruser) { |
| 560 | if (stricmp(nickname, Curruser->nick_name) == 0) { |
| 561 | if (prv_user) { |
| 562 | prv_user->next = Curruser->next; |
| 563 | |
| 564 | } else { |
| 565 | Firstuser = Curruser->next; |
| 566 | } |
| 567 | AddChatCommandToQueue(CC_USER_LEAVING, Curruser->nick_name, strlen(Curruser->nick_name) + 1); |
| 568 | DLLmem_free(Curruser); |
| 569 | return 1; |
| 570 | } |
| 571 | prv_user = Curruser; |
| 572 | Curruser = Curruser->next; |
| 573 | } |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | void RemoveAllChatUsers() { |
| 578 | Chat_user *tmp_user = nullptr; |
| 579 | Curruser = Firstuser; |
| 580 | while (Curruser) { |
| 581 | tmp_user = Curruser->next; |
| 582 | AddChatCommandToQueue(CC_USER_LEAVING, Curruser->nick_name, strlen(Curruser->nick_name) + 1); |
| 583 | DLLmem_free(Curruser); |
| 584 | Curruser = tmp_user; |
| 585 | } |
| 586 | Firstuser = nullptr; |
| 587 | } |
| 588 | |
| 589 | char *ParseIRCMessage(char *Line, int iMode) { |
| 590 | char szRemLine[MAXLOCALSTRING]; |
| 591 | const char *pszTempStr; |
| 592 | char szPrefix[MAXLOCALSTRING]; |
| 593 | char szHackPrefix[MAXLOCALSTRING]; |
| 594 | char szTarget[MAXLOCALSTRING]; |
| 595 | char szNick[MAXLOCALSTRING]; |
| 596 | char szCmd[MAXLOCALSTRING]; |
| 597 | char szCTCPCmd[MAXLOCALSTRING]; |
| 598 | |
| 599 | static char szResponse[MAXLOCALSTRING]; |
| 600 | |
| 601 | int iNickLen; |
| 602 | int iPrefixLen = 0; |
| 603 | |
| 604 | szPrefix[0] = '\0'; |
| 605 | szNick[0] = '\0'; |
| 606 | |
| 607 | if (strlen(Line) >= MAXLOCALSTRING) { |
| 608 | return nullptr; |
| 609 | } |
| 610 | // Nick included.... |
| 611 | if (iMode == MSG_REMOTE) { |
| 612 | strcpy(szRemLine, Line); |
| 613 | // Start by getting the prefix |
| 614 | szHackPrefix[0] = '\0'; |
| 615 | if (Line[0] == ':') { |
| 616 | // |
| 617 | pszTempStr = GetWordNum(0, Line + 1); |
| 618 | strcpy(szPrefix, pszTempStr); |
| 619 | strcpy(szHackPrefix, pszTempStr); |
| 620 | strcpy(szRemLine, Line + 1 + strlen(szPrefix)); |
| 621 | } |
| 622 | // Next, get the Nick |
| 623 | pszTempStr = strtok(szHackPrefix, "!"); |
| 624 | if (pszTempStr) { |
| 625 | strcpy(szNick, pszTempStr); |
| 626 | } else { |
| 627 | strncpy(szNick, szPrefix, 31); |
| 628 | szNick[31] = 0; |
| 629 | } |
| 630 | // strcpy(NewMsg.Nickname,szNick); |
| 631 | iNickLen = strlen(szNick); |
| 632 | iPrefixLen = strlen(szPrefix); |
| 633 | } else if (iMode == MSG_LOCAL) { |
| 634 | strcpy(szRemLine, Line); |
| 635 | strcpy(szNick, Nick_name); |
| 636 | strcpy(szPrefix, Nick_name); |
| 637 | // strcpy(NewMsg.Nickname,szNick); |
| 638 | iNickLen = -2; |
| 639 | iPrefixLen = -2; |
| 640 | } |
| 641 | // Next is the command |
| 642 | pszTempStr = GetWordNum(0, szRemLine); |
| 643 | if (pszTempStr[0]) { |
| 644 | strcpy(szCmd, pszTempStr); |
| 645 | } else { |
| 646 | // Shouldn't ever happen, but we can't be sure of what the host will send us. |
| 647 | return nullptr; |
| 648 | } |
| 649 | |
| 650 | // Move the szRemLine string up |
| 651 | strcpy(szRemLine, Line + iPrefixLen + strlen(szCmd) + 2); |
| 652 | // Now parse the commands! |
| 653 | // printf("%s",szCmd); |
| 654 | if (stricmp(szCmd, "PRIVMSG") == 0) { |
| 655 | pszTempStr = GetWordNum(0, szRemLine); |
| 656 | strcpy(szTarget, pszTempStr); |
| 657 | strcpy(szRemLine, Line + iPrefixLen + strlen(szCmd) + strlen(szTarget) + 4); |
| 658 | if (szRemLine[0] == ':') { |
| 659 | strcpy(szCTCPCmd, GetWordNum(0, szRemLine + 1)); |
| 660 | if (szCTCPCmd[strlen(szCTCPCmd) - 1] == 0x01) |
| 661 | szCTCPCmd[strlen(szCTCPCmd) - 1] = 0x00; |
| 662 | |
| 663 | } else { |
| 664 | strcpy(szCTCPCmd, GetWordNum(0, szRemLine)); |
| 665 | if (szCTCPCmd[strlen(szCTCPCmd) - 1] == 0x01) |
| 666 | szCTCPCmd[strlen(szCTCPCmd) - 1] = 0x00; |
| 667 | } |
| 668 | if (szCTCPCmd[0] == 0x01) { |
| 669 | // Handle ctcp message |
| 670 | strcpy(szRemLine, Line + iPrefixLen + strlen(szCmd) + strlen(szTarget) + strlen(szCTCPCmd) + 6); |
| 671 | szRemLine[strlen(szRemLine) - 1] = '\0'; // null out the ending 0x01 |
| 672 | if (stricmp(szCTCPCmd + 1, "ACTION") == 0) { |
| 673 | // Posture |
| 674 | snprintf(szResponse, sizeof(szResponse), "\1\xff\x28\x28* %s %s", szNick, szRemLine); |
| 675 | return szResponse; |
| 676 | } |
| 677 | if (iMode == MSG_LOCAL) { |
| 678 | strcpy(szHackPrefix, Line + iPrefixLen + strlen(szCmd) + strlen(szTarget) + 4); |
| 679 | szRemLine[strlen(szRemLine) - 1] = '\0'; |
| 680 | snprintf(szResponse, sizeof(szResponse), "** CTCP %s %s %s", szTarget, szCTCPCmd + 1, szRemLine); |
| 681 | return szResponse; |
| 682 | } |
| 683 | if (stricmp(szCTCPCmd + 1, "PING") == 0) { |
| 684 | snprintf(szResponse, sizeof(szResponse), "/NOTICE %s :\001PING %s\001", szNick, |
| 685 | szRemLine); // Don't need the trailing \001 because szremline has it. |
| 686 | SendChatString(szResponse, 1); |
| 687 | return nullptr; |
| 688 | } |
| 689 | if (stricmp(szCTCPCmd + 1, "VERSION") == 0) { |
| 690 | // reply with a notice version & copyright |
| 691 | // sprintf(szTempLine,"NOTICE %s :\001VERSION Copyright(c)\001\n",szNick); |
| 692 | |
| 693 | return nullptr; |
| 694 | } |
| 695 | strcpy(szRemLine, 1 + GetWordNum(0, Line + iPrefixLen + strlen(szCmd) + strlen(szTarget) + 4)); |
| 696 | szRemLine[strlen(szRemLine) - 1] = '\0'; |
| 697 | snprintf(szResponse, sizeof(szResponse), "** CTCP Message from %s (%s)", szNick, szRemLine); |
| 698 | return nullptr; // szResponse; |
| 699 | } |
| 700 | // differentiate between channel and private |
| 701 | if ((szTarget[0] == '#') || (szTarget[0] == '+')) { |
| 702 | pszTempStr = GetWordNum(0, szRemLine); |
| 703 | snprintf(szResponse, sizeof(szResponse), "\1\xff\xff\xff[%s] %s", szNick, pszTempStr); |
| 704 | return szResponse; |
| 705 | } else { |
| 706 | if (iMode == MSG_LOCAL) { |
| 707 | pszTempStr = GetWordNum(0, szRemLine); |
| 708 | snprintf(szResponse, sizeof(szResponse), TXT_PXO_PRIVMSGTO, szTarget, pszTempStr); |
| 709 | } else { |
| 710 | pszTempStr = GetWordNum(0, szRemLine); |
| 711 | snprintf(szResponse, sizeof(szResponse), TXT_PXO_PRIVMSGFROM, szNick, pszTempStr); |
| 712 | } |
| 713 | return szResponse; |
| 714 | } |
| 715 | } |
| 716 | // don't handle any other messages locally. |
| 717 | if (iMode == MSG_LOCAL) { |
| 718 | return nullptr; |
| 719 | } |
| 720 | |
| 721 | if (stricmp(szCmd, "NOTICE") == 0) { |
| 722 | |
| 723 | pszTempStr = GetWordNum(0, szRemLine); |
| 724 | strcpy(szTarget, pszTempStr); |
| 725 | strcpy(szRemLine, Line + iPrefixLen + strlen(szCmd) + strlen(szTarget) + 4); |
| 726 | if (szRemLine[0] == ':') { |
| 727 | strcpy(szCTCPCmd, GetWordNum(0, szRemLine + 1)); |
| 728 | if (szCTCPCmd[strlen(szCTCPCmd) - 1] == 0x01) |
| 729 | szCTCPCmd[strlen(szCTCPCmd) - 1] = 0x00; |
| 730 | |
| 731 | } else { |
| 732 | strcpy(szCTCPCmd, GetWordNum(0, szRemLine)); |
| 733 | if (szCTCPCmd[strlen(szCTCPCmd) - 1] == 0x01) |
| 734 | szCTCPCmd[strlen(szCTCPCmd) - 1] = 0x00; |
| 735 | } |
| 736 | if (szCTCPCmd[0] == 0x01) { |
| 737 | // Handle ctcp message |
| 738 | strcpy(szRemLine, Line + iPrefixLen + strlen(szCmd) + strlen(szTarget) + strlen(szCTCPCmd) + 6); |
| 739 | szRemLine[strlen(szRemLine) - 1] = '\0'; // null out the ending 0x01 |
| 740 | if (stricmp(szCTCPCmd + 1, "PING") == 0) { |
| 741 | // This is a ping response, figure out time and print |
| 742 | // sprintf(NewMsg.Message,"** Ping Response from %s: %ums",szNick,ulping); |
| 743 | return nullptr; |
| 744 | } |
| 745 | |
| 746 | // Default message |
| 747 | strcpy(szRemLine, 1 + GetWordNum(0, Line + iPrefixLen + strlen(szCmd) + strlen(szTarget) + 4)); |
| 748 | szRemLine[strlen(szRemLine) - 1] = '\0'; |
| 749 | snprintf(szResponse, sizeof(szResponse), "** CTCP Message from %s (%s)", szNick, szRemLine); |
| 750 | return nullptr; // szResponse; |
| 751 | } |
| 752 | strncpy(szResponse, szRemLine, sizeof(szResponse)); |
| 753 | return nullptr; |
| 754 | } |
| 755 | if (stricmp(szCmd, "JOIN") == 0) { |
| 756 | // see if it is me! |
| 757 | if (stricmp(Nick_name, szNick) == 0) { |
| 758 | // Yup, it's me! |
| 759 | // if(stricmp(szChat_channel,GetWordNum(0,szRemLine))==0) |
| 760 | //{ |
| 761 | Joined_channel = 1; |
| 762 | // if(stricmp(szChat_channel,"#autoselect")==0) |
| 763 | //{ |
| 764 | strcpy(szChat_channel, GetWordNum(0, szRemLine)); |
| 765 | AddChatCommandToQueue(CC_YOURCHANNEL, szChat_channel, strlen(szChat_channel) + 1); |
| 766 | |
| 767 | //} |
| 768 | // CC_YOURCHANNEL |
| 769 | //} |
| 770 | } |
| 771 | AddChatUser(szNick); |
| 772 | |
| 773 | pszTempStr = GetWordNum(0, szRemLine); |
| 774 | strcpy(szTarget, pszTempStr); |
| 775 | // strcpy(szRemLine,Line+iPrefixLen+strlen(szCmd)+strlen(szTarget)+3); |
| 776 | |
| 777 | // strcpy(NewMsg.Channel,szTarget); |
| 778 | |
| 779 | AddChatUser(szNick); |
| 780 | snprintf(szResponse, sizeof(szResponse), "** %s has joined %s", szNick, szTarget); |
| 781 | return nullptr; // szResponse; |
| 782 | // Add them to the userlist too! |
| 783 | } |
| 784 | if (stricmp(szCmd, "PART") == 0) { |
| 785 | pszTempStr = GetWordNum(0, szRemLine); |
| 786 | strcpy(szTarget, pszTempStr); |
| 787 | strcpy(szRemLine, Line + iPrefixLen + strlen(szCmd) + strlen(szTarget) + 3); |
| 788 | // see if it is me! |
| 789 | if (stricmp(Nick_name, szNick) == 0) { |
| 790 | // Yup, it's me! |
| 791 | // szChat_channel[0]=NULL; |
| 792 | RemoveAllChatUsers(); |
| 793 | } |
| 794 | |
| 795 | RemoveChatUser(szNick); |
| 796 | return nullptr; |
| 797 | // Remove them to the userlist too! |
| 798 | } |
| 799 | if (stricmp(szCmd, "KICK") == 0) { |
| 800 | pszTempStr = GetWordNum(0, szRemLine); |
| 801 | strcpy(szTarget, pszTempStr); |
| 802 | pszTempStr = GetWordNum(1, szRemLine); |
| 803 | strcpy(szHackPrefix, pszTempStr); |
| 804 | pszTempStr = GetWordNum(2, szRemLine); |
| 805 | // see if it is me! |
| 806 | if (stricmp(Nick_name, GetWordNum(1, szRemLine)) == 0) { |
| 807 | // Yup, it's me! |
| 808 | szChat_channel[0] = '\0'; |
| 809 | // bNewStatus=1; |
| 810 | AddChatCommandToQueue(CC_KICKED, nullptr, 0); |
| 811 | RemoveAllChatUsers(); |
| 812 | } |
| 813 | // sprintf(szResponse,"*** %s has kicked %s from channel %s (%s)",szNick,szHackPrefix,szTarget,pszTempStr); |
| 814 | // Remove them to the userlist too! |
| 815 | RemoveChatUser(szNick); |
| 816 | return nullptr; // szResponse; |
| 817 | } |
| 818 | if (stricmp(szCmd, "NICK") == 0) { |
| 819 | // see if it is me! |
| 820 | if (stricmp(Nick_name, szNick) == 0) { |
| 821 | // Yup, it's me! |
| 822 | strcpy(Nick_name, GetWordNum(0, szRemLine)); |
| 823 | } |
| 824 | char nicks[70]; |
| 825 | snprintf(nicks, sizeof(nicks), "%s %s", szNick, GetWordNum(0, szRemLine)); |
| 826 | AddChatCommandToQueue(CC_NICKCHANGED, nicks, strlen(nicks) + 1); |
| 827 | RemoveChatUser(szNick); |
| 828 | AddChatUser(GetWordNum(0, szRemLine)); |
| 829 | // sprintf(szResponse,"*** %s is now known as %s",szNick,GetWordNum(0,szRemLine)); |
| 830 | return nullptr; // szResponse; |
| 831 | } |
| 832 | if (stricmp(szCmd, "PING") == 0) { |
| 833 | // respond with pong (GetWordNum(0,szRemLine)) |
| 834 | snprintf(szResponse, sizeof(szResponse), "/PONG :%s", GetWordNum(0, szRemLine)); |
| 835 | SendChatString(szResponse, 1); |
| 836 | return nullptr; |
| 837 | } |
| 838 | if (stricmp(szCmd, "MODE") == 0) { |
| 839 | // Channel Mode info |
| 840 | return nullptr; |
| 841 | } |
| 842 | |
| 843 | if (stricmp(szCmd, "401") == 0) { |
| 844 | // This is whois user info, we can get their tracker info from here. -5 |
| 845 | char szWhoisUser[33]; |
| 846 | strcpy(szWhoisUser, GetWordNum(1, szRemLine)); |
| 847 | Getting_user_tracker_error = 1; |
| 848 | Getting_user_channel_error = 1; |
| 849 | |
| 850 | snprintf(szResponse, sizeof(szResponse), TXT_PXO_ERRORNOTONLINE, szWhoisUser); |
| 851 | return szResponse; |
| 852 | } |
| 853 | if (stricmp(szCmd, "311") == 0) { |
| 854 | char szWhoisUser[33]; |
| 855 | strcpy(szWhoisUser, GetWordNum(1, szRemLine)); |
| 856 | // This is whois user info, we can get their tracker info from here. -5 |
| 857 | // if(stricmp(Getting_user_tracker_info_for,szWhoisUser)==0) |
| 858 | //{ |
| 859 | strcpy(User_req_tracker_id, GetWordNum(5, szRemLine)); |
| 860 | //} |
| 861 | return nullptr; |
| 862 | } |
| 863 | if (stricmp(szCmd, "319") == 0) { |
| 864 | char szWhoisUser[33]; |
| 865 | strcpy(szWhoisUser, GetWordNum(1, szRemLine)); |
| 866 | // This is whois channel info -- what channel they are on -2 |
| 867 | // if(stricmp(Getting_user_channel_info_for,szWhoisUser)==0) |
| 868 | //{ |
| 869 | strcpy(User_req_channel, GetWordNum(2, szRemLine)); |
| 870 | //} |
| 871 | return nullptr; |
| 872 | } |
| 873 | |
| 874 | // End of whois and we didn't get a channel means they aren't in a channel. |
| 875 | if (stricmp(szCmd, "318") == 0) { |
| 876 | if (!*User_req_channel) { |
| 877 | User_req_channel[0] = '*'; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | if (stricmp(szCmd, "321") == 0) { |
| 882 | // start of channel list |
| 883 | FlushChannelList(); |
| 884 | GettingChannelList = 1; |
| 885 | return nullptr; |
| 886 | } |
| 887 | if (stricmp(szCmd, "322") == 0) { |
| 888 | // channel list data |
| 889 | if (GettingChannelList == 1) { |
| 890 | char channel_list_name[33]; |
| 891 | char sztopic[200]; |
| 892 | strcpy(sztopic, GetWordNum(3, szRemLine)); |
| 893 | strcpy(channel_list_name, GetWordNum(1, szRemLine)); |
| 894 | AddChannel(channel_list_name, atoi(GetWordNum(2, szRemLine)), sztopic); |
| 895 | } |
| 896 | return nullptr; |
| 897 | } |
| 898 | if (stricmp(szCmd, "323") == 0) { |
| 899 | // end of channel list |
| 900 | GettingChannelList = 2; |
| 901 | return nullptr; |
| 902 | } |
| 903 | if (stricmp(szCmd, "324") == 0) { |
| 904 | // Channel Mode info |
| 905 | return nullptr; |
| 906 | } |
| 907 | |
| 908 | if (stricmp(szCmd, "332") == 0) { |
| 909 | // Channel Topic, update status bar. |
| 910 | if (stricmp(szChat_channel, szTarget) == 0) { |
| 911 | // strncpy(szChanTopic,GetWordNum(2,szRemLine),70); |
| 912 | } |
| 913 | // sprintf(NewMsg.Message,"*** %s has changed the topic to: %s",szNick,GetWordNum(2,szRemLine)); |
| 914 | |
| 915 | return nullptr; |
| 916 | } |
| 917 | if (stricmp(szCmd, "TOPIC") == 0) { |
| 918 | // Channel Topic, update status bar. |
| 919 | if (stricmp(szChat_channel, szTarget) == 0) { |
| 920 | // strncpy(szChanTopic,GetWordNum(1,szRemLine),70); |
| 921 | } |
| 922 | // sprintf(NewMsg.Message,"*** %s has changed the topic to: %s",szNick,GetWordNum(1,szRemLine)); |
| 923 | return nullptr; |
| 924 | } |
| 925 | if (stricmp(szCmd, "QUIT") == 0) { |
| 926 | // Remove the user! |
| 927 | RemoveChatUser(szNick); |
| 928 | return nullptr; |
| 929 | } |
| 930 | if (stricmp(szCmd, "376") == 0) // end of motd, trigger autojoin... |
| 931 | { |
| 932 | if (!Chat_server_connected) { |
| 933 | DLLmprintf(0, "Connected to chat server!\n"); |
| 934 | Chat_server_connected = 1; |
| 935 | // We want to make sure we know our nick. This is somewhat of a hack |
| 936 | strcpy(Nick_name, GetWordNum(0, szRemLine + 1)); |
| 937 | } |
| 938 | return nullptr; |
| 939 | } |
| 940 | if ((stricmp(szCmd, "377") == 0) || (stricmp(szCmd, "372") == 0) || (stricmp(szCmd, "372") == 0) |
| 941 | |
| 942 | ) { |
| 943 | // Stip the message, and display it. |
| 944 | pszTempStr = GetWordNum(3, Line); |
| 945 | snprintf(szResponse, sizeof(szResponse), "\1\xcf\xf8\xb9%s", pszTempStr); |
| 946 | return szResponse; |
| 947 | } |
| 948 | // Ignore these messages |
| 949 | if (((stricmp(szCmd, "366") == 0)) || (stricmp(szCmd, "333") == 0) || // Who set the topic |
| 950 | (stricmp(szCmd, "329") == 0)) // Time Channel created |
| 951 | /* |
| 952 | (stricmp(szCmd,"305")==0) || |
| 953 | (stricmp(szCmd,"306")==0) || |
| 954 | (stricmp(szCmd,"311")==0) || //WHOIS stuff |
| 955 | (stricmp(szCmd,"312")==0) || |
| 956 | (stricmp(szCmd,"313")==0) || |
| 957 | (stricmp(szCmd,"317")==0) || |
| 958 | (stricmp(szCmd,"318")==0) || |
| 959 | (stricmp(szCmd,"319")==0) || |
| 960 | */ |
| 961 | |
| 962 | { |
| 963 | return nullptr; |
| 964 | } |
| 965 | if (stricmp(szCmd, "353") == 0) { |
| 966 | |
| 967 | // Names in the channel. |
| 968 | pszTempStr = GetWordNum(3, Line + iPrefixLen + strlen(szCmd) + 2); |
| 969 | strcpy(szRemLine, pszTempStr); |
| 970 | pszTempStr = strtok(szRemLine, " "); |
| 971 | |
| 972 | while (pszTempStr) { |
| 973 | if (pszTempStr[0] == '@') { |
| 974 | AddChatUser(pszTempStr + 1); |
| 975 | } else if (pszTempStr[0] == '+') { |
| 976 | AddChatUser(pszTempStr + 1); |
| 977 | } else { |
| 978 | AddChatUser(pszTempStr); |
| 979 | } |
| 980 | pszTempStr = strtok(nullptr, " "); |
| 981 | } |
| 982 | return nullptr; |
| 983 | } |
| 984 | // MOTD Codes |
| 985 | if ((stricmp(szCmd, "001") == 0) || (stricmp(szCmd, "002") == 0) || (stricmp(szCmd, "003") == 0) || |
| 986 | (stricmp(szCmd, "004") == 0) || (stricmp(szCmd, "251") == 0) || (stricmp(szCmd, "254") == 0) || |
| 987 | (stricmp(szCmd, "255") == 0) || (stricmp(szCmd, "265") == 0) || (stricmp(szCmd, "375") == 0) || |
| 988 | (stricmp(szCmd, "372") == 0) || (stricmp(szCmd, "375") == 0)) { |
| 989 | // Stip the message, and display it. |
| 990 | pszTempStr = GetWordNum(3, Line); |
| 991 | strcpy(szResponse, pszTempStr); |
| 992 | return nullptr; |
| 993 | // return szResponse; |
| 994 | } |
| 995 | if (stricmp(szCmd, "432") == 0) { |
| 996 | // Channel Mode info |
| 997 | snprintf(szResponse, sizeof(szResponse), "%s", TXT_PXO_BADNICK); |
| 998 | AddChatCommandToQueue(CC_DISCONNECTED, nullptr, 0); |
| 999 | return szResponse; |
| 1000 | } |
| 1001 | if (stricmp(szCmd, "433") == 0) { |
| 1002 | // Channel Mode info |
| 1003 | char new_nick[33]; |
| 1004 | snprintf(new_nick, sizeof(new_nick), "%s%d", Orignial_nick_name, Nick_variety); |
| 1005 | strcpy(Nick_name, new_nick); |
| 1006 | Nick_variety++; |
| 1007 | snprintf(szResponse, sizeof(szResponse), "/NICK %s", new_nick); |
| 1008 | SendChatString(szResponse, 1); |
| 1009 | return nullptr; |
| 1010 | } |
| 1011 | // Default print |
| 1012 | strcpy(szResponse, Line); |
| 1013 | // return szResponse; |
| 1014 | return nullptr; |
| 1015 | } |
| 1016 | |
| 1017 | void AddChatCommandToQueue(int command, const void *data, int len) { |
| 1018 | Currcommand = Firstcommand; |
| 1019 | if (Firstcommand == nullptr) { |
| 1020 | Firstcommand = (Chat_command *)DLLmem_malloc(sizeof(Chat_command)); |
| 1021 | // ASSERT(Firstcommand); |
| 1022 | Firstcommand->next = nullptr; |
| 1023 | Currcommand = Firstcommand; |
| 1024 | } else { |
| 1025 | while (Currcommand->next) { |
| 1026 | Currcommand = Currcommand->next; |
| 1027 | } |
| 1028 | Currcommand->next = (Chat_command *)DLLmem_malloc(sizeof(Chat_command)); |
| 1029 | // ASSERT(Currcommand->next); |
| 1030 | Currcommand = Currcommand->next; |
| 1031 | } |
| 1032 | Currcommand->command = command; |
| 1033 | if (len && data) |
| 1034 | memcpy(&Currcommand->data, data, len); |
| 1035 | Currcommand->next = nullptr; |
| 1036 | } |
| 1037 | |
| 1038 | Chat_command *GetChatCommandFromQueue() { |
| 1039 | static Chat_command response_cmd; |
| 1040 | Chat_command *tmp_cmd; |
| 1041 | if (!Firstcommand) |
| 1042 | return nullptr; |
| 1043 | Currcommand = Firstcommand; |
| 1044 | memcpy(&response_cmd, Currcommand, sizeof(Chat_command)); |
| 1045 | tmp_cmd = Currcommand->next; |
| 1046 | DLLmem_free(Firstcommand); |
| 1047 | Firstcommand = tmp_cmd; |
| 1048 | return &response_cmd; |
| 1049 | } |
| 1050 | |
| 1051 | void FlushChatCommandQueue() { |
| 1052 | Chat_command *tmp_cmd; |
| 1053 | Currcommand = Firstcommand; |
| 1054 | |
| 1055 | while (Currcommand) { |
| 1056 | tmp_cmd = Currcommand->next; |
| 1057 | DLLmem_free(Currcommand); |
| 1058 | Currcommand = tmp_cmd; |
| 1059 | } |
| 1060 | Firstcommand = nullptr; |
| 1061 | } |
| 1062 | |
| 1063 | void FlushChannelList() { |
| 1064 | Chat_channel *tmp_chan; |
| 1065 | Currchannel = Firstchannel; |
| 1066 | |
| 1067 | while (Currchannel) { |
| 1068 | tmp_chan = Currchannel->next; |
| 1069 | DLLmem_free(Currchannel); |
| 1070 | Currchannel = tmp_chan; |
| 1071 | } |
| 1072 | Firstchannel = nullptr; |
| 1073 | } |
| 1074 | char *GetChannelList() { |
| 1075 | int ichan_list_length = 0; |
| 1076 | char sznumusers[10]; |
| 1077 | |
| 1078 | if (GettingChannelList != 2) |
| 1079 | return nullptr; |
| 1080 | if (!Socket_connected) |
| 1081 | return nullptr; |
| 1082 | |
| 1083 | if (Chan_list) { |
| 1084 | DLLmem_free(Chan_list); |
| 1085 | Chan_list = nullptr; |
| 1086 | } |
| 1087 | |
| 1088 | Currchannel = Firstchannel; |
| 1089 | while (Currchannel) { |
| 1090 | ichan_list_length += strlen(Currchannel->topic) + 1 + strlen(Currchannel->channel_name) + 1 + |
| 1091 | 5; // 1 for the space, and 4 for the number of users 0000-9999 + space |
| 1092 | Currchannel = Currchannel->next; |
| 1093 | } |
| 1094 | Currchannel = Firstchannel; |
| 1095 | Chan_list = (char *)DLLmem_malloc(ichan_list_length + 1); |
| 1096 | Chan_list[0] = '\0'; |
| 1097 | while (Currchannel) { |
| 1098 | strcat(Chan_list, "$"); |
| 1099 | strcat(Chan_list, Currchannel->channel_name); |
| 1100 | strcat(Chan_list, " "); |
| 1101 | snprintf(sznumusers, sizeof(sznumusers), "%d ", Currchannel->users); |
| 1102 | strcat(Chan_list, sznumusers); |
| 1103 | strcat(Chan_list, Currchannel->topic); // fgets |
| 1104 | strcat(Chan_list, " "); |
| 1105 | Currchannel = Currchannel->next; |
| 1106 | } |
| 1107 | FlushChannelList(); |
| 1108 | GettingChannelList = 0; |
| 1109 | return Chan_list; |
| 1110 | } |
| 1111 | |
| 1112 | void AddChannel(char *channel, uint16_t numusers, char *topic) { |
| 1113 | Currchannel = Firstchannel; |
| 1114 | if (Firstchannel == nullptr) { |
| 1115 | Firstchannel = (Chat_channel *)DLLmem_malloc(sizeof(Chat_channel)); |
| 1116 | // ASSERT(Firstchannel); |
| 1117 | strcpy(Firstchannel->channel_name, channel); |
| 1118 | strcpy(Firstchannel->topic, topic); |
| 1119 | Firstchannel->users = numusers; |
| 1120 | Firstchannel->next = nullptr; |
| 1121 | Currchannel = Firstchannel; |
| 1122 | } else { |
| 1123 | while (Currchannel->next) { |
| 1124 | Currchannel = Currchannel->next; |
| 1125 | } |
| 1126 | Currchannel->next = (Chat_channel *)DLLmem_malloc(sizeof(Chat_channel)); |
| 1127 | // ASSERT(Currchannel->next); |
| 1128 | Currchannel = Currchannel->next; |
| 1129 | strcpy(Currchannel->channel_name, channel); |
| 1130 | strcpy(Currchannel->topic, topic); |
| 1131 | Currchannel->users = numusers; |
| 1132 | } |
| 1133 | Currchannel->next = nullptr; |
| 1134 | } |
| 1135 | |
| 1136 | char *GetTrackerIdByUser(const char *nickname) { |
| 1137 | char szWhoisCmd[100]; |
| 1138 | |
| 1139 | if (nickname == (char *)-1) { |
| 1140 | GettingUserTID = 0; |
| 1141 | Getting_user_tracker_error = 0; |
| 1142 | return nullptr; |
| 1143 | } else if (GettingUserTID) { |
| 1144 | if (Getting_user_tracker_error) { |
| 1145 | Getting_user_tracker_error = 0; |
| 1146 | GettingUserTID = 0; |
| 1147 | return (char *)-1; |
| 1148 | } |
| 1149 | |
| 1150 | if (*User_req_tracker_id) { |
| 1151 | GettingUserTID = 0; |
| 1152 | return User_req_tracker_id; |
| 1153 | } |
| 1154 | } else { |
| 1155 | strcpy(Getting_user_tracker_info_for, nickname); |
| 1156 | snprintf(szWhoisCmd, sizeof(szWhoisCmd), "/WHOIS %s", nickname); |
| 1157 | User_req_tracker_id[0] = '\0'; |
| 1158 | SendChatString(szWhoisCmd, 1); |
| 1159 | GettingUserTID = 1; |
| 1160 | } |
| 1161 | return nullptr; |
| 1162 | } |
| 1163 | |
| 1164 | char *GetChannelByUser(const char *nickname) { |
| 1165 | char szWhoisCmd[100]; |
| 1166 | |
| 1167 | if (nickname == (char *)-1) { |
| 1168 | GettingUserChannel = 0; |
| 1169 | Getting_user_channel_error = 0; |
| 1170 | return nullptr; |
| 1171 | } else if (GettingUserChannel) { |
| 1172 | if (Getting_user_channel_error) { |
| 1173 | Getting_user_channel_error = 0; |
| 1174 | GettingUserChannel = 0; |
| 1175 | return (char *)-1; |
| 1176 | } |
| 1177 | if (*User_req_channel) { |
| 1178 | GettingUserChannel = 0; |
| 1179 | return User_req_channel; |
| 1180 | } |
| 1181 | } else { |
| 1182 | strcpy(Getting_user_channel_info_for, nickname); |
| 1183 | User_req_channel[0] = '\0'; |
| 1184 | snprintf(szWhoisCmd, sizeof(szWhoisCmd), "/WHOIS %s", nickname); |
| 1185 | SendChatString(szWhoisCmd, 1); |
| 1186 | GettingUserChannel = 1; |
| 1187 | } |
| 1188 | return nullptr; |
| 1189 | } |