From 329ca8e8f40efdd7838d40435b5f113d2877c13c Mon Sep 17 00:00:00 2001 From: Test_User Date: Fri, 5 May 2023 23:34:55 -0400 Subject: Switch to gnutls, add handling of NICK, add responses to unknown/invalid/etc command, change a few other things --- network.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 98 insertions(+), 26 deletions(-) (limited to 'network.c') diff --git a/network.c b/network.c index 3f4d8fa..70c58fe 100644 --- a/network.c +++ b/network.c @@ -26,7 +26,7 @@ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -#include +#include #include #include #include @@ -74,7 +74,7 @@ int ping_handler(struct string sender, uint64_t argc, struct string *argv) { } uint64_t len = 1 + argv[1].len + 6 + argv[1].len + 1 + sender.len + 1; - uint8_t msg[len]; + char msg[len]; uint64_t offset = 0; msg[0] = ':'; offset++; @@ -90,7 +90,10 @@ int ping_handler(struct string sender, uint64_t argc, struct string *argv) { offset += sender.len; msg[offset] = '\n'; - SSL_write(ssl, msg, len); + struct string m; + m.data = msg; + m.len = len; + SEND(m); return 0; } @@ -230,6 +233,7 @@ int uid_handler(struct string sender, uint64_t argc, struct string *argv) { if (!nick.data) goto uid_handler_free_server; nick.len = argv[2].len; + memcpy(nick.data, argv[2].data, argv[2].len); struct string hostname; hostname.data = malloc(argv[3].len); @@ -381,8 +385,8 @@ int kill_handler(struct string sender, uint64_t argc, struct string *argv) { return 1; } - uint8_t current_time[21]; // C HaxServ will be deprecated long before we reach 20-digit timestamps - snprintf(current_time, 21, "%d", time(NULL)); + char current_time[21]; // C HaxServ will be deprecated long before we reach 20-digit timestamps + snprintf(current_time, 21, "%ld", time(NULL)); SEND(STRING("UID 1HC000000 ")); SEND(((struct string){current_time, strlen(current_time)})); SEND(STRING(" ")); @@ -408,6 +412,45 @@ int kill_handler(struct string sender, uint64_t argc, struct string *argv) { return 0; } +int nick_handler(struct string sender, uint64_t argc, struct string *argv) { + if (argc < 2) { + WRITES(2, STRING("Invalid NICK recieved! (Missing parameters)\n")); + return 1; + } + + struct user_info *info = get_table_index(user_list, sender); + if (!info) { + WRITES(2, STRING("NICK: Unknown user!\n")); + return 1; + } + + void *tmp = malloc(argv[0].len); + if (!tmp) { + WRITES(2, STRING("OOM! (nick_handler)\n")); + return 1; + } + memcpy(tmp, argv[0].data, argv[0].len); + + free(info->nick.data); + info->nick.data = tmp; + info->nick.len = argv[0].len; + + if (argv[1].len > 20) { + WRITES(2, STRING("Invalid NICK recieved! (Timestamp too long)\n")); + return 1; + } + + uint8_t err; + uint64_t ts = str_to_unsigned(argv[1], &err); + if (err) { + WRITES(2, STRING("Invalid NICK recieved! (Invalid timestamp)\n")); + return 1; + } + info->nick_ts = ts; + + return 0; +} + int privmsg_handler(struct string sender, uint64_t argc, struct string *argv) { if (argc < 2) { WRITES(2, STRING("Invalid PRIVMSG recieved (Missing parameters)\n")); @@ -421,21 +464,16 @@ int privmsg_handler(struct string sender, uint64_t argc, struct string *argv) { uint64_t offset; if (argv[0].data[0] == '#') { - if (argv[1].len < command_prefix.len || memcmp(argv[1].data, command_prefix.data, command_prefix.len) != 0) { - WRITES(1, STRING("Message is not a command.\n")); - return 0; // not for us - } + if (argv[1].len < command_prefix.len || memcmp(argv[1].data, command_prefix.data, command_prefix.len) != 0) + return 0; offset = command_prefix.len; } else { offset = 0; } - if (offset >= argv[1].len || argv[1].data[offset] == ' ') { - // TODO: complain about empty command - WRITES(1, STRING("Command is empty?\n")); + if (offset >= argv[1].len || argv[1].data[offset] == ' ') return 0; - } uint64_t command_argc = 0; uint64_t old_offset = offset; @@ -475,21 +513,50 @@ int privmsg_handler(struct string sender, uint64_t argc, struct string *argv) { if (cmd->privs.len != 0 && sender.len != 3) { // servers always count as oper :P struct user_info *user = get_table_index(user_list, sender); if (!user) { - // TODO: complain about unknown user - WRITES(1, STRING("User is unknown!\n")); - return 0; + WRITES(2, STRING("User is unknown!\n")); + + SEND(STRING(":1HC000000 NOTICE ")); + if (argv[0].data[0] == '#') + SEND(argv[0]); + else + SEND(sender); + SEND(STRING(" :You don't seem to exist... and neither do I!\n")); + + return 1; // have already desynced } if (user->opertype.len != cmd->privs.len || memcmp(user->opertype.data, cmd->privs.data, cmd->privs.len)) { // TODO: complain about missing privs - WRITES(1, STRING("User lacks privs for this command!\n")); + SEND(STRING(":1HC000000 NOTICE ")); + if (argv[0].data[0] == '#') + SEND(argv[0]); + else + SEND(sender); + SEND(STRING(" :You are not authorized to execute this command.\n")); + return 0; } } - WRITES(1, STRING("Executing command ")); - WRITES(1, command_argv[0]); - write(1, "\n", 1); + SEND(STRING(":1HC000000 PRIVMSG ")); + SEND(log_channel); + if (sender.len == 3) { + SEND(STRING(" :Server ")); + SEND(sender); + } else { + struct user_info *user = get_table_index(user_list, sender); + if (user) { + SEND(STRING(" :User ")); + SEND(user->nick); + } else { + SEND(STRING(" :An unknown user (something desycned... this shouldn't happen)")); + } + } + + SEND(STRING(" executes `")); + SEND(argv[1]); + SEND(STRING("'\n")); + return cmd->func(sender, argv[1], argv[0], command_argc, command_argv); } else { // TODO: complain about remote access @@ -497,10 +564,14 @@ int privmsg_handler(struct string sender, uint64_t argc, struct string *argv) { return 0; } } else { - // TODO: complain about unknown command - WRITES(1, STRING("Command is unknown!\n")); - WRITES(1, command_argv[0]); - write(1, "\n", 1); + SEND(STRING(":1HC000000 NOTICE ")); + if (argv[0].data[0] == '#') + SEND(argv[0]); + else + SEND(sender); + SEND(STRING(" :Unknown command: " "\x03" "04")); + SEND(argv[1]); + SEND(STRING("\n")); return 0; } } @@ -517,6 +588,7 @@ int initservernetwork(void) { set_table_index(&network_commands, STRING("PRIVMSG"), &privmsg_handler); set_table_index(&network_commands, STRING("QUIT"), &quit_handler); set_table_index(&network_commands, STRING("KILL"), &kill_handler); + set_table_index(&network_commands, STRING("NICK"), &nick_handler); init_user_commands(); @@ -531,8 +603,8 @@ int initservernetwork(void) { SEND(send_password); SEND(STRING(" 0 1HC :HaxServ\n")); SEND(STRING("BURST ")); - uint8_t current_time[21]; // C HaxServ will be deprecated long before we reach 20-digit timestamps - snprintf(current_time, 21, "%d", time(NULL)); + char current_time[21]; // C HaxServ will be deprecated long before we reach 20-digit timestamps + snprintf(current_time, 21, "%ld", time(NULL)); SEND(((struct string){current_time, strlen(current_time)})); SEND(STRING("\nUID 1HC000000 ")); SEND(((struct string){current_time, strlen(current_time)})); -- cgit v1.2.3