diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | channel.c | 64 | ||||
-rw-r--r-- | channel.h | 37 | ||||
-rw-r--r-- | server.c | 29 | ||||
-rw-r--r-- | server.h | 1 |
5 files changed, 131 insertions, 4 deletions
@@ -1,8 +1,8 @@ CC=gcc .PHONY: all -all: main.c server.c log.o hash_table.o string_view.o client.o - $(CC) -ggdb -o papo main.c server.c log.o hash_table.o string_view.o client.o +all: main.c server.c log.o hash_table.o string_view.o client.o channel.o + $(CC) -ggdb -o papo main.c server.c log.o hash_table.o string_view.o client.o channel.o .PHONY: test test: all diff --git a/channel.c b/channel.c new file mode 100644 index 0000000..a9cd15e --- /dev/null +++ b/channel.c @@ -0,0 +1,64 @@ +/* + * Papo IRC Server + * Copyright (C) 2022 Johnny Richard + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +#include "channel.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +channel_t* +channel_new(char *name) +{ + if (name == NULL) { + fprintf(stderr, "Could not create channel: name is required\n"); + return NULL; + } + + channel_t* channel = (channel_t*) malloc(sizeof(channel_t)); + if (channel == NULL) { + fprintf(stderr, "Could not create channel: Out or memory\n"); + return NULL; + } + + strcpy(channel->name, name); + channel->client_table = hash_table_new(); + if (channel->client_table == NULL) { + fprintf(stderr, "Could not create channel->client_table\n"); + return NULL; + } + + return channel; +} + +void +channel_add_client(channel_t* channel, client_t* client) +{ + if (client == NULL) { + fprintf(stderr, "client_add_client: client is required\n"); + return; + } + hash_table_insert(channel->client_table, client->nick, client); +} + +void +channel_destroy(channel_t *channel) +{ + hash_table_destroy(channel->client_table); + free(channel); +} + diff --git a/channel.h b/channel.h new file mode 100644 index 0000000..bb0b2ab --- /dev/null +++ b/channel.h @@ -0,0 +1,37 @@ +/* + * Papo IRC Server + * Copyright (C) 2022 Johnny Richard + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +#ifndef CHANNEL_H +#define CHANNEL_H + +#include "client.h" +#include "hash_table.h" + +#include <stdint.h> +#include <stdbool.h> + +typedef struct channel { + char name[128]; + hash_table_t *client_table; +} channel_t; + +// TODO: Broadcast message to connected clients +channel_t* channel_new(char *name); +void channel_add_client(channel_t* channel, client_t* client); +void channel_destroy(channel_t *channel); + +#endif /* CHANNEL_H */ @@ -15,6 +15,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#include "channel.h" #include "client.h" #include "log.h" #include "server.h" @@ -76,7 +77,13 @@ server_init(server_t *server, uint32_t port) server->client_table = hash_table_new(); if (server->client_table == NULL) { - log_fatal("could not create server->hash_table"); + log_fatal("could not create server->client_table"); + exit(EXIT_FAILURE); + } + + server->channel_table = hash_table_new(); + if (server->client_table == NULL) { + log_fatal("could not create server->channel_table"); exit(EXIT_FAILURE); } @@ -303,7 +310,25 @@ server_on_join_msg(server_t *server, client_t *client, string_view_t msg) { - // FIXME: Create server channel hash_table + channel_t* channel = NULL; + + channel = (channel_t*) hash_table_get(server->channel_table, msg.data); + if (channel == NULL) { + string_view_chop_by_delim(&msg, '#'); + + char channel_name[msg.size + 1]; + string_view_to_cstr(&msg, channel_name); + + // FIXME: Destroy channel when everyone leave channel or server get destroyed + channel = channel_new(channel_name); + if (channel == NULL) { + fprintf(stderr, "server_on_join_msg: could not create new channel\n"); + return; + } + channel_add_client(channel, client); + hash_table_insert(server->channel_table, channel->name, channel); + } + client_send_msg( client, ":%s!~%s@localhost JOIN "SVFMT"\n", @@ -31,6 +31,7 @@ typedef struct server { int epoll_fd; struct epoll_event events[MAXEVENTS]; hash_table_t *client_table; + hash_table_t *channel_table; bool running; } server_t; |