summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2022-05-10 03:09:47 +0200
committerJohnny Richard <johnny@johnnyrichard.com>2022-05-10 03:09:47 +0200
commit6c5276eb486f88b73c9309ffec713f1732922a02 (patch)
tree2997ced24529b8fe2814284a91753435a36311ea
parent31586df150671904bbd116534e38ecb1d4748962 (diff)
channel: Create channel and implement join msgHEADmaster
-rw-r--r--Makefile4
-rw-r--r--channel.c64
-rw-r--r--channel.h37
-rw-r--r--server.c29
-rw-r--r--server.h1
5 files changed, 131 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index 33702dd..164a3ad 100644
--- a/Makefile
+++ b/Makefile
@@ -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 */
diff --git a/server.c b/server.c
index 5be310b..b2a9a77 100644
--- a/server.c
+++ b/server.c
@@ -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",
diff --git a/server.h b/server.h
index 1b2f564..b5088cc 100644
--- a/server.h
+++ b/server.h
@@ -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;