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 ++++++++++++++++++++++++++++++++++++++++++++++++------- network.h | 1 + server_network.c | 98 ++++++++++++++++++---------------- types.h | 1 + 4 files changed, 194 insertions(+), 64 deletions(-) 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 diff --git a/network.h b/network.h index 7682ed4..bdaa2a8 100644 --- a/network.h +++ b/network.h @@ -109,3 +109,4 @@ extern char channel_mode_types[UCHAR_MAX]; #define SENDCLIENT(x) write(client_fd, x.data, x.len) extern int PRIVMSG(struct string source, struct string target, struct string message); +extern 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); diff --git a/server_network.c b/server_network.c index eb9dbee..6fc84d7 100644 --- a/server_network.c +++ b/server_network.c @@ -405,36 +405,53 @@ int kill_handler(struct string sender, uint64_t argc, struct string *argv) { // TODO: Get accurate list of what got killed, what to rejoin, etc - struct string id = STRING("1HC000000"); - if (argv[0].len != id.len || memcmp(argv[0].data, id.data, id.len) != 0) { - WRITES(2, STRING("Invalid KILL recieved! (Unknown user)\n")); - return 1; - } - - 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(" ")); - SEND(nick); - SEND(STRING(" ")); - SEND(hostmask); - SEND(STRING(" ")); - SEND(hostmask); - SEND(STRING(" HaxServ 192.168.1.1 ")); - SEND(((struct string){current_time, strlen(current_time)})); - SEND(STRING(" +k :HaxServ\n:1HC000000 OPERTYPE ")); - SEND(opertype); - SEND(STRING("\n")); - - for (uint64_t i = 0; i < num_prejoin_channels; i++) { - SEND(STRING("FJOIN ")); - SEND(prejoin_channels[i]); + struct user_info *user = get_table_index(user_list, argv[0]); + if (!user) + return 0; // TODO: Currently not all local users are considered; fix that, then make this give an error + + if (user->server.len == 3 && memcmp(user->server.data, "1HC", 3) == 0) { + char user_time[21]; + snprintf(user_time, 21, "%ld", user->user_ts); + char nick_time[21]; + snprintf(nick_time, 21, "%ld", user->nick_ts); + SEND(STRING("UID ")); + SEND(argv[0]); SEND(STRING(" ")); - SEND(((struct string){current_time, strlen(current_time)})); - SEND(STRING(" + :,1HC000000\nMODE ")); - SEND(prejoin_channels[i]); - SEND(STRING(" +o 1HC000000\n")); + SEND(NULSTR(user_time)); + SEND(STRING(" ")); + SEND(user->nick); + SEND(STRING(" ")); + SEND(user->hostname); + SEND(STRING(" ")); + SEND(user->vhost); + SEND(STRING(" ")); + SEND(user->ident); + SEND(STRING(" ")); + SEND(user->ip); + SEND(STRING(" ")); + SEND(NULSTR(nick_time)); + SEND(STRING(" +k :")); + SEND(user->realname); + SEND(STRING("\n:")); + SEND(argv[0]); + SEND(STRING(" OPERTYPE ")); + SEND(opertype); + SEND(STRING("\n")); + + for (uint64_t i = 0; i < channel_list.len; i++) { + struct channel_info *channel = channel_list.array[i].ptr; + if (has_table_index(channel->user_list, argv[0])) { + char timestamp[21]; + SEND(STRING("FJOIN ")); + SEND(channel_list.array[i].name); + SEND(STRING(" ")); + snprintf(timestamp, 21, "%ld", channel->ts); + SEND(NULSTR(timestamp)); + SEND(STRING(" + :,")); + SEND(argv[0]); + SEND(STRING("\n")); + } + } } return 0; @@ -875,28 +892,19 @@ int initservernetwork(void) { SEND(send_password); SEND(STRING(" 0 1HC :HaxServ\n")); SEND(STRING("BURST ")); - 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)})); - SEND(STRING(" ")); - SEND(nick); - SEND(STRING(" ")); - SEND(hostmask); - SEND(STRING(" ")); - SEND(hostmask); - SEND(STRING(" HaxServ 192.168.1.1 ")); - SEND(((struct string){current_time, strlen(current_time)})); - SEND(STRING(" +k :HaxServ\n:1HC000000 OPERTYPE ")); - SEND(opertype); + time_t current_time = time(NULL); + char current_time_str[21]; // C HaxServ will be deprecated long before we reach 20-digit timestamps + snprintf(current_time_str, 21, "%ld", current_time); + SEND(NULSTR(current_time_str)); SEND(STRING("\n")); + if (add_local_client(STRING("1HC000000"), nick, hostmask, nick, nick, current_time) != 0) + return 1; for (uint64_t i = 0; i < num_prejoin_channels; i++) { SEND(STRING("FJOIN ")); SEND(prejoin_channels[i]); SEND(STRING(" ")); - SEND(((struct string){current_time, strlen(current_time)})); + SEND(NULSTR(current_time_str)); SEND(STRING(" + :,1HC000000\nMODE ")); SEND(prejoin_channels[i]); SEND(STRING(" +o 1HC000000\n")); diff --git a/types.h b/types.h index af89dd2..2251bcc 100644 --- a/types.h +++ b/types.h @@ -37,4 +37,5 @@ struct string { }; #define STRING(x) (struct string){x, sizeof(x)-1} +#define NULSTR(x) (struct string){x, strlen(x)} #define WRITES(x, y) write(x, y.data, y.len) -- cgit v1.2.3