From 9a1fdcc9d24fe8dd20cacee0e25a57c6219651a2 Mon Sep 17 00:00:00 2001 From: Test_User Date: Sat, 2 Sep 2023 21:17:20 -0400 Subject: clear command, some other old stufff I forgot to commit --- client_network.c | 23 +++++++++++++++++++++++ commands.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ server_network.c | 7 +++++++ table.c | 8 ++++++++ table.h | 1 + 5 files changed, 92 insertions(+) diff --git a/client_network.c b/client_network.c index a043338..a0c25be 100644 --- a/client_network.c +++ b/client_network.c @@ -195,6 +195,18 @@ int client_nick_handler(uint64_t argc, struct string *argv) { void *tmp = malloc(argv[0].len); if (!tmp) return 1; + + void *name_for_tables; + if (client_connected) { + name_for_tables = malloc(argv[0].len); + if (!name_for_tables) { + free(tmp); + return 1; + } + + memcpy(name_for_tables, argv[0].data, argv[0].len); + } + memcpy(tmp, argv[0].data, argv[0].len); if (client_connected) { @@ -211,6 +223,17 @@ int client_nick_handler(uint64_t argc, struct string *argv) { client_nick.len = argv[0].len; if (client_connected) { + struct user_info *client = get_table_index(user_list, STRING("1HC000001")); + if (client) { + free(client->nick.data); + client->nick.data = name_for_tables; // Will not be used uninitialized, ignore the compiler's complaint here + client->nick.len = argv[0].len; + } else { + free(name_for_tables); + WRITES(2, STRING("Client connected but client data missing!\n")); + return 1; + } + SEND(STRING(":1HC000001 NICK ")); SEND(client_nick); SEND(STRING(" ")); diff --git a/commands.c b/commands.c index fdae58b..33ffd21 100644 --- a/commands.c +++ b/commands.c @@ -182,6 +182,58 @@ static struct command_def spam_command_def = { .summary = STRING("Repeats a message to a target times"), }; +int clear_command(struct string sender, struct string original_message, struct string to, uint64_t argc, struct string *argv, char is_local) { + if (argc < 2) { + privmsg(STRING("1HC000000"), to, 1, (struct string[]){STRING("Missing args!")}); + return 0; + } + + struct channel_info *channel = get_table_index(channel_list, argv[1]); + if (!channel) { + privmsg(STRING("1HC000000"), to, 1, (struct string[]){STRING("That channel doesn't seem to exist, so is thereby already cleared.")}); + return 0; + } + + for (uint64_t i = 0; i < channel->user_list.len; i++) { + // TODO: Proper kick function, will prob come as a part of my next HaxServ rewrite + + SEND(STRING(":1HC000000 KICK ")); + SEND(argv[1]); + SEND(STRING(" ")); + SEND(channel->user_list.array[i].name); + SEND(STRING(" :\n")); + + if (client_connected) { + SENDCLIENT(STRING(":")); + SENDCLIENT(nick); + SENDCLIENT(STRING("!")); + SENDCLIENT(nick); + SENDCLIENT(STRING("@")); + SENDCLIENT(hostmask); + SENDCLIENT(STRING(" KICK ")); + SENDCLIENT(argv[1]); + SENDCLIENT(STRING(" ")); + + struct user_info *user = get_table_index(user_list, channel->user_list.array[i].name); + if (user) + SENDCLIENT(user->nick); + else + SENDCLIENT(user_list.array[i].name); + + SENDCLIENT(STRING(" :\r\n")); + } + } + clear_table(&(channel->user_list)); + + return 0; +} +static struct command_def clear_command_def = { + .func = clear_command, + .privs = STRING("NetAdmin"), + .local_only = 0, + .summary = STRING("Clears a channel"), +}; + int init_user_commands(void) { srandom(time(NULL)); @@ -192,6 +244,7 @@ int init_user_commands(void) { set_table_index(&user_commands, STRING("cr"), &cr_command_def); set_table_index(&user_commands, STRING("help"), &help_command_def); set_table_index(&user_commands, STRING("spam"), &spam_command_def); + set_table_index(&user_commands, STRING("clear"), &clear_command_def); return 0; } diff --git a/server_network.c b/server_network.c index 9d0d7d6..27c43a6 100644 --- a/server_network.c +++ b/server_network.c @@ -916,6 +916,13 @@ int initservernetwork(void) { return 1; } + struct timeval timeout = { + .tv_sec = 60, + .tv_usec = 0, + }; + + setsockopt(server_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + // probably inefficient to be calling SSL_write this frequently, but also less effort SEND(STRING("SERVER ")); SEND(server_name); diff --git a/table.c b/table.c index a06e034..a4adf61 100644 --- a/table.c +++ b/table.c @@ -171,3 +171,11 @@ void * remove_table_index(struct table *tbl, struct string name) { return ptr; } + +void clear_table(struct table *tbl) { + for (uint64_t i = 0; i < tbl->len; i++) + free(tbl->array[i].name.data); + + tbl->array = realloc(tbl->array, 0); + tbl->len = 0; +} diff --git a/table.h b/table.h index 2683cb2..7bcd6f2 100644 --- a/table.h +++ b/table.h @@ -44,3 +44,4 @@ extern int set_table_index(struct table *tbl, struct string name, void *ptr); extern void * get_table_index(struct table tbl, struct string name); uint8_t has_table_index(struct table tbl, struct string name); extern void * remove_table_index(struct table *tbl, struct string name); // returns same as get_table_index +extern void clear_table(struct table *tbl); -- cgit v1.2.3