diff options
author | Johnny Richard <johnny@johnnyrichard.com> | 2022-04-22 00:43:59 +0200 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2022-04-22 00:59:27 +0200 |
commit | 2269e8904b56ae0dd80461cc7a0507862b33e34e (patch) | |
tree | 1d93f82ca561d65005b13a4ccdc846c995d66185 | |
parent | c4797d9dfcf82edf0212925190d5157055fdd187 (diff) |
client: Implement client_seng_msg
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | client.c | 33 | ||||
-rw-r--r-- | client.h | 6 | ||||
-rw-r--r-- | server.c | 34 |
4 files changed, 47 insertions, 30 deletions
@@ -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); +} @@ -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 */ @@ -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); } |