summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--client.c33
-rw-r--r--client.h6
-rw-r--r--server.c34
4 files changed, 47 insertions, 30 deletions
diff --git a/Makefile b/Makefile
index dc195c2..33702dd 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
- $(CC) -ggdb -o papo main.c server.c log.o hash_table.o string_view.o
+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
.PHONY: test
test: all
diff --git a/client.c b/client.c
new file mode 100644
index 0000000..8a3a57b
--- /dev/null
+++ b/client.c
@@ -0,0 +1,33 @@
+/*
+ * Papo IRC Server
+ * Copyright (C) 2021 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 "client.h"
+#include "log.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+
+void
+client_send_msg(client_t *client, const char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ if (vdprintf(client->fd, fmt, args) < 0) {
+ log_error("could not send data to client: %s", client->nick);
+ }
+ va_end(args);
+}
diff --git a/client.h b/client.h
index 9bd6a58..4d0dc3d 100644
--- a/client.h
+++ b/client.h
@@ -15,6 +15,8 @@
* 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 CLIENT_H
+#define CLIENT_H
#define BUFFER_SIZE 4096
@@ -25,3 +27,7 @@ typedef struct client {
char msg_buf[BUFFER_SIZE];
int msg_buf_i;
} client_t;
+
+void client_send_msg(client_t *client, const char *fmt, ...);
+
+#endif /* CLIENT_H */
diff --git a/server.c b/server.c
index 3b26d2d..b7c70cb 100644
--- a/server.c
+++ b/server.c
@@ -238,13 +238,7 @@ server_on_user_msg(server_t *server,
client_t *client,
string_view_t msg)
{
- char rbuf[BUFFER_SIZE];
- memset(rbuf, 0, BUFFER_SIZE);
-
- sprintf(rbuf, "001 %s :Welcome!\n", client->nick);
- if (send(client->fd, rbuf, strlen(rbuf), 0) == -1) {
- log_error("could not send data to client: %s", strerror(errno));
- }
+ client_send_msg(client, "001 %s :Welcome!\n", client->nick);
}
static void
@@ -252,13 +246,7 @@ server_on_ping_msg(server_t *server,
client_t *client,
string_view_t msg)
{
- char rbuf[BUFFER_SIZE];
- memset(rbuf, 0, BUFFER_SIZE);
-
- sprintf(rbuf, "PONG %.*s\n", msg.size, msg.data);
- if (send(client->fd, rbuf, strlen(rbuf), 0) == -1) {
- log_error("could not send data to client: %s", strerror(errno));
- }
+ client_send_msg(client, "PONG %.*s\n", msg.size, msg.data);
}
static void
@@ -270,23 +258,13 @@ server_on_privmsg_msg(server_t *server,
char nick[sv_nick.size + 1];
string_view_to_cstr(&sv_nick, nick);
- client_t *client_recv = hash_table_get(server->client_table, nick);
+ client_t *client_receiver = hash_table_get(server->client_table, nick);
- char rbuf[BUFFER_SIZE];
- memset(rbuf, 0, BUFFER_SIZE);
-
- if (client_recv == NULL) {
- sprintf(rbuf, ":localhost 401 %s %s :No such nick/channel\n", client->nick, nick);
- if (send(client->fd, rbuf, strlen(rbuf), 0) == -1) {
- log_error("could not send data to client: %s", strerror(errno));
- }
+ if (client_receiver == NULL) {
+ client_send_msg(client, ":localhost 401 %s %s :No such nick/channel\n", client->nick, nick);
return;
}
string_view_chop_by_delim(&msg, ':');
-
- sprintf(rbuf, ":%s PRIVMSG %s :%.*s\n", client->nick, client_recv->nick, msg.size, msg.data);
- if (send(client_recv->fd, rbuf, strlen(rbuf), 0) == -1) {
- log_error("could not send data to client: %s", strerror(errno));
- }
+ client_send_msg(client_receiver, ":%s PRIVMSG %s :%.*s\n", client->nick, client_receiver->nick, msg.size, msg.data);
}