From b557bac23688b304fbab0a766a2e3e93db78f429 Mon Sep 17 00:00:00 2001 From: Test_User Date: Wed, 9 Aug 2023 00:26:14 -0400 Subject: Various improvements, a few more bugs added to fix later --- client_network.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 139 insertions(+), 19 deletions(-) (limited to 'client_network.c') diff --git a/client_network.c b/client_network.c index ca82379..e2a7c24 100644 --- a/client_network.c +++ b/client_network.c @@ -47,6 +47,141 @@ struct table client_network_commands = {0}; struct string client_nick = {0}; uint8_t client_connected; +int add_local_client(struct string uid, struct string nick_arg, struct string vhost_arg, struct string ident_arg, struct string realname_arg, time_t timestamp) { + if (has_table_index(user_list, uid)) + return 1; + + struct user_info *user = malloc(sizeof(*user)); + if (!user) + goto add_local_client_fail_user; + + struct string server = { + .data = malloc(3), + .len = 3, + }; + if (!server.data) + goto add_local_client_fail_server; + + memcpy(server.data, "1HC", 3); + + struct string hostname = { + .data = malloc(vhost_arg.len), + .len = vhost_arg.len, + }; + if (!hostname.data) + goto add_local_client_fail_hostname; + + memcpy(hostname.data, vhost_arg.data, hostname.len); + + struct string vhost = { + .data = malloc(vhost_arg.len), + .len = vhost_arg.len, + }; + if (!vhost.data) + goto add_local_client_fail_vhost; + + memcpy(vhost.data, vhost_arg.data, vhost.len); + + struct string ident = { + .data = malloc(ident_arg.len), + .len = ident_arg.len, + }; + if (!ident.data) + goto add_local_client_fail_ident; + + memcpy(ident.data, ident_arg.data, ident.len); + + struct string ip = { + .data = malloc(11), + .len = 11, + }; + if (!ip.data) + goto add_local_client_fail_ip; + + memcpy(ip.data, "192.168.1.1", 11); + + struct string realname = { + .data = malloc(realname_arg.len), + .len = realname_arg.len, + }; + if (!realname.data) + goto add_local_client_fail_realname; + + memcpy(realname.data, realname_arg.data, realname.len); + + struct string nick = { + .data = malloc(nick_arg.len), + .len = nick_arg.len, + }; + if (!nick.data) + goto add_local_client_fail_nick; + + memcpy(nick.data, nick_arg.data, nick.len); + + *user = (struct user_info){ + .nick_ts = (uint64_t)timestamp, + .user_ts = (uint64_t)timestamp, + .server = server, + .nick = nick, + .hostname = hostname, + .vhost = vhost, + .ident = ident, + .ip = ip, + .realname = realname, + .metadata = {.array = malloc(0), .len = 0}, + }; + + set_table_index(&user_list, uid, user); + + char string_time[21]; + snprintf(string_time, 21, "%ld", timestamp); + SEND(STRING("UID ")); + SEND(uid); + SEND(STRING(" ")); + SEND(NULSTR(string_time)); + SEND(STRING(" ")); + SEND(nick); + SEND(STRING(" ")); + SEND(vhost); + SEND(STRING(" ")); + SEND(vhost); + SEND(STRING(" ")); + SEND(ident); + SEND(STRING(" ")); + SEND(ip); + SEND(STRING(" ")); + SEND(NULSTR(string_time)); + SEND(STRING(" +k :")); + SEND(realname); + SEND(STRING("\n:")); + SEND(uid); + SEND(STRING(" OPERTYPE ")); + SEND(opertype); + SEND(STRING("\n")); + + return 0; + + add_local_client_fail_nick: + free(realname.data); + add_local_client_fail_realname: + free(ip.data); + add_local_client_fail_ip: + free(ident.data); + add_local_client_fail_ident: + free(vhost.data); + add_local_client_fail_vhost: + free(hostname.data); + add_local_client_fail_hostname: + free(server.data); + add_local_client_fail_server: + free(user); + add_local_client_fail_user: + + WRITES(2, STRING("OOM! (add_local_client)\n")); + + return 1; +} + int client_nick_handler(uint64_t argc, struct string *argv) { if (argc < 1) return 1; @@ -75,7 +210,7 @@ int client_nick_handler(uint64_t argc, struct string *argv) { SEND(STRING(" ")); char current_time[22]; snprintf(current_time, 22, "%ld", time(NULL)); - SEND(((struct string){current_time, strlen(current_time)})); + SEND(NULSTR(current_time)); SEND(STRING("\n")); } @@ -89,23 +224,8 @@ int client_user_handler(uint64_t argc, struct string *argv) { if (client_nick.len == 0) return 1; - char current_time[22]; - snprintf(current_time, 22, "%ld", time(NULL)); - SEND(STRING("UID 1HC000001 ")); - SEND(((struct string){current_time, strlen(current_time)})); - SEND(STRING(" ")); - SEND(client_nick); - SEND(STRING(" ")); - SEND(client_hostmask); - SEND(STRING(" ")); - SEND(client_hostmask); - SEND(STRING(" ")); - SEND(argv[0]); - SEND(STRING(" 192.168.1.1 ")); - SEND(((struct string){current_time, strlen(current_time)})); - SEND(STRING(" +k :")); - SEND(argv[3]); - SEND(STRING("\n")); + if (add_local_client(STRING("1HC000001"), client_nick, client_hostmask, argv[0], argv[3], time(NULL)) != 0) + return 1; SENDCLIENT(STRING(":hax.irc.andrewyu.org 001 ")); SENDCLIENT(client_nick); @@ -220,7 +340,7 @@ int client_join_handler(uint64_t argc, struct string *argv) { SEND(STRING(":1HC FJOIN ")); SEND(channels); SEND(STRING(" ")); - SEND(((struct string){.data = current_time_nulstr, .len = strlen(current_time_nulstr)})); + SEND(NULSTR(current_time_nulstr)); SEND(STRING(" + :,1HC000001\n")); set_table_index(&(channel_info->user_list), STRING("1HC000001"), get_table_index(user_list, STRING("1HC000001"))); // TODO: Actually add local users to user_list -- cgit v1.2.3