From c660db6c36ce50e2aa1c3ec99cb62d9aaf3c0102 Mon Sep 17 00:00:00 2001 From: Test_User Date: Tue, 8 Aug 2023 03:25:57 -0400 Subject: User lists for channels and such --- server_network.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 181 insertions(+), 30 deletions(-) (limited to 'server_network.c') diff --git a/server_network.c b/server_network.c index cf7c435..3b58e70 100644 --- a/server_network.c +++ b/server_network.c @@ -117,7 +117,7 @@ int server_handler(struct string sender, uint64_t argc, struct string *argv) { } if (sender.len != 0) { - struct remote_server *from = get_table_index(server_list, sender); + struct server_info *from = get_table_index(server_list, sender); if (!from) { puts("Invalid SERVER recieved! (Unknown source)"); return 1; @@ -147,7 +147,7 @@ int server_handler(struct string sender, uint64_t argc, struct string *argv) { name.len = argv[4].len; memcpy(name.data, argv[4].data, argv[4].len); - struct remote_server *server = malloc(sizeof(*server)); + struct server_info *server = malloc(sizeof(*server)); if (server == 0) goto server_handler_free_name; @@ -163,7 +163,7 @@ int server_handler(struct string sender, uint64_t argc, struct string *argv) { via = (struct string){0}; } - *server = (struct remote_server){ + *server = (struct server_info){ .name = name, .address = address, .distance = distance, @@ -360,6 +360,25 @@ int quit_handler(struct string sender, uint64_t argc, struct string *argv) { return 1; } + for (uint64_t i = 0; i < channel_list.len; i++) { // TODO: Use channel list attached to the user (doesn't exist yet) + struct channel_info *chan_info = channel_list.array[i].ptr; + if (has_table_index(chan_info->user_list, STRING("1HC000001"))) { + SENDCLIENT(STRING(":")); + SENDCLIENT(info->nick); + SENDCLIENT(STRING("!")); + SENDCLIENT(info->ident); + SENDCLIENT(STRING("@")); + SENDCLIENT(info->vhost); + if (argc >= 1) { + SENDCLIENT(STRING(" QUIT :")); + SENDCLIENT(argv[0]); + SENDCLIENT(STRING("\r\n")); + } else { + SENDCLIENT(STRING(" QUIT\r\n")); + } + } + } + free(info->server.data); free(info->nick.data); if (info->opertype.len) @@ -431,6 +450,23 @@ int nick_handler(struct string sender, uint64_t argc, struct string *argv) { return 1; } + for (uint64_t i = 0; i < channel_list.len; i++) { // TODO: More efficient way of doing this + struct channel_info *channel = channel_list.array[i].ptr; + if (has_table_index(channel->user_list, sender) && has_table_index(channel->user_list, STRING("1HC000001"))) { + SENDCLIENT(STRING(":")); + SENDCLIENT(info->nick); + SENDCLIENT(STRING("!")); + SENDCLIENT(info->ident); + SENDCLIENT(STRING("@")); + SENDCLIENT(info->vhost); + SENDCLIENT(STRING(" NICK :")); + SENDCLIENT(argv[0]); + SENDCLIENT(STRING("\r\n")); + + break; + } + } + void *tmp = malloc(argv[0].len); if (!tmp) { WRITES(2, STRING("OOM! (nick_handler)\n")); @@ -464,38 +500,101 @@ int fjoin_handler(struct string sender, uint64_t argc, struct string *argv) { return 1; } -// uint8_t err; -// uint64_t timestamp = str_to_unsigned(argv[1], &err); -// if (err) { -// WRITES(2, STRING("Invalid FJOIN recieved! (Invalid timestamp given)\n")); -// return 1; -// } -// -// // TODO: Parse modes, then make the rest of this work -// -// -// struct channel_info *channel = get_table_index(channel_list, argv[0]); -// if (!channel) { -// channel = malloc(sizeof(*channel)); -// if (!channel) { -// WRITES(2, STRING("OOM! (fjoin_handler)\n")); -// return 1; -// } -// *channel = (struct channel_info){ -// .ts = timestamp, -// -// -// set_table_index(&channel_list, argv[0], channel); -// -// -// } + uint8_t err; + uint64_t timestamp = str_to_unsigned(argv[1], &err); + if (err) { + WRITES(2, STRING("Invalid FJOIN recieved! (Invalid timestamp given)\n")); + return 1; + } + + // TODO: Parse modes, then make the rest of this work + uint64_t userlist_offset = 3; + { + char dir = '?'; + for (uint64_t offset = 0; offset < argv[2].len; offset++) { + if (argv[2].data[offset] == '+') { + dir = '+'; + } else if (argv[2].data[offset] == '-') { + dir = '-'; + } else if (dir == '?') { + WRITES(2, STRING("Invalid FJOIN recieved! (No direction set for modes)\n")); + return 1; + } else if (channel_mode_types[(unsigned char)argv[2].data[offset]] == MODE_TYPE_UNKNOWN) { + WRITES(2, STRING("Invalid FJOIN recieved! (Unknown mode set on the channel)\n")); + return 1; + } else if (channel_mode_types[(unsigned char)argv[2].data[offset]] != MODE_TYPE_NOARGS && dir == '+') { + userlist_offset++; + } + } + } + + if (argc < userlist_offset + 1) { + WRITES(2, STRING("Invalid FJOIN recieved! (Missing mode parameters or user list)\n")); + return 1; + } + + struct channel_info *channel = get_table_index(channel_list, argv[0]); + if (!channel) { + channel = malloc(sizeof(*channel)); + if (!channel) { + WRITES(2, STRING("OOM! (fjoin_handler)\n")); + return 1; + } + *channel = (struct channel_info){ + .ts = timestamp, + .topic = {.data = malloc(0), .len = 0}, + .topic_ts = 0, + .modes = {.array = malloc(0), .len = 0}, + .user_list = {.array = malloc(0), .len = 0}, + .metadata = {.array = malloc(0), .len = 0}, + }; + + set_table_index(&channel_list, argv[0], channel); + } + + struct string userlist = argv[userlist_offset]; + + if (userlist.len < 10) // Not enough for any users + return 0; + + uint8_t sendclient = has_table_index(channel->user_list, STRING("1HC000001")); + + uint64_t offset = 0; + while (1) { + while (offset < userlist.len && userlist.data[offset] != ',') + offset++; + + if (offset > (userlist.len - 1) - 9) + break; + + offset++; + + struct string user = {.data = &(userlist.data[offset]), .len = 9}; + struct user_info *user_info = get_table_index(user_list, user); + + set_table_index(&(channel->user_list), user, user_info); + + if (user_info && sendclient) { + SENDCLIENT(STRING(":")); + SENDCLIENT(user_info->nick); + SENDCLIENT(STRING("!")); + SENDCLIENT(user_info->ident); + SENDCLIENT(STRING("@")); + SENDCLIENT(user_info->vhost); + SENDCLIENT(STRING(" JOIN :")); + SENDCLIENT(argv[0]); + SENDCLIENT(STRING("\r\n")); + } + + offset += 10; + } return 0; } int squit_handler(struct string sender, uint64_t argc, struct string *argv) { if (argc < 1) { - WRITES(2, STRING("Invalid SQUIT recieved! (Missing parameters)")); + WRITES(2, STRING("Invalid SQUIT recieved! (Missing parameters)\n")); return 1; } @@ -521,7 +620,7 @@ int squit_handler(struct string sender, uint64_t argc, struct string *argv) { } } - struct remote_server *server = remove_table_index(&server_list, argv[0]); + struct server_info *server = remove_table_index(&server_list, argv[0]); free(server->name.data); free(server->address.data); if (server->via.data != 0) @@ -572,6 +671,30 @@ int privmsg_handler(struct string sender, uint64_t argc, struct string *argv) { SENDCLIENT(argv[1]); SENDCLIENT(STRING("\r\n")); } + } else { + struct server_info *server = get_table_index(server_list, sender); + if (server) { + if (argv[0].data[0] == '#') { + struct channel_info *channel = get_table_index(channel_list, argv[0]); + if (channel && has_table_index(channel->user_list, STRING("1HC000001"))) { + SENDCLIENT(STRING(":")); + SENDCLIENT(server->address); + SENDCLIENT(STRING(" PRIVMSG ")); + SENDCLIENT(argv[0]); + SENDCLIENT(STRING(" :")); + SENDCLIENT(argv[1]); + SENDCLIENT(STRING("\r\n")); + } + } else if (argv[0].len == 9 && memcmp(argv[0].data, "1HC000001", 9) == 0) { + SENDCLIENT(STRING(":")); + SENDCLIENT(server->address); + SENDCLIENT(STRING(" PRIVMSG ")); + SENDCLIENT(client_nick); + SENDCLIENT(STRING(" :")); + SENDCLIENT(argv[1]); + SENDCLIENT(STRING("\r\n")); + } + } } uint64_t offset; @@ -689,6 +812,33 @@ int privmsg_handler(struct string sender, uint64_t argc, struct string *argv) { } } +int part_handler(struct string sender, uint64_t argc, struct string *argv) { + if (argc < 1) { + WRITES(2, STRING("Invalid PART received! (Missing parameters)\n")); + return 1; + } + + struct channel_info *channel = get_table_index(channel_list, argv[0]); + struct user_info *user = get_table_index(user_list, sender); + if (user && channel && has_table_index(channel->user_list, STRING("1HC000001"))) { + SENDCLIENT(STRING(":")); + SENDCLIENT(user->nick); + SENDCLIENT(STRING("!")); + SENDCLIENT(user->ident); + SENDCLIENT(STRING("@")); + SENDCLIENT(user->vhost); + SENDCLIENT(STRING(" PART ")); + SENDCLIENT(argv[0]); + if (argc >= 2) { + SENDCLIENT(STRING(" :")); + SENDCLIENT(argv[1]); + } + SENDCLIENT(STRING("\r\n")); + } + + return 0; +} + int initservernetwork(void) { server_network_commands.array = malloc(0); server_list.array = malloc(0); @@ -705,6 +855,7 @@ int initservernetwork(void) { set_table_index(&server_network_commands, STRING("NICK"), &nick_handler); set_table_index(&server_network_commands, STRING("FJOIN"), &fjoin_handler); set_table_index(&server_network_commands, STRING("SQUIT"), &squit_handler); + set_table_index(&server_network_commands, STRING("PART"), &part_handler); init_user_commands(); -- cgit v1.2.3