From d5aedc3d84eea0d970f90ddc04f9be331e9c20e9 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Thu, 24 Oct 2019 00:26:53 -0400 Subject: Make clientFormat public --- bounce.h | 2 ++ client.c | 4 ++-- state.c | 52 +++++++++++++++++++++++++++++----------------------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/bounce.h b/bounce.h index aac414f..f63d884 100644 --- a/bounce.h +++ b/bounce.h @@ -73,6 +73,8 @@ void clientFree(struct Client *client); bool clientError(const struct Client *client); void clientRecv(struct Client *client); void clientSend(struct Client *client, const char *ptr, size_t len); +void clientFormat(struct Client *client, const char *format, ...) + __attribute__((format(printf, 2, 3))); bool stateReady(void); void stateParse(char *line); diff --git a/client.c b/client.c index 3e3684f..9bc8a4a 100644 --- a/client.c +++ b/client.c @@ -79,7 +79,7 @@ void clientSend(struct Client *client, const char *ptr, size_t len) { } } -static void format(struct Client *client, const char *format, ...) { +void clientFormat(struct Client *client, const char *format, ...) { char buf[513]; va_list ap; va_start(ap, format); @@ -90,7 +90,7 @@ static void format(struct Client *client, const char *format, ...) { } static void passRequired(struct Client *client) { - format( + clientFormat( client, ":invalid 464 * :Password incorrect\r\n" "ERROR :Password incorrect\r\n" diff --git a/state.c b/state.c index 32c5903..5149c17 100644 --- a/state.c +++ b/state.c @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -199,30 +198,27 @@ void stateParse(char *line) { } } -// FIXME: Deduplicate this. -static void format(struct Client *client, const char *format, ...) { - char buf[513]; - va_list ap; - va_start(ap, format); - int len = vsnprintf(buf, sizeof(buf), format, ap); - va_end(ap); - assert(len > 0 && (size_t)len < sizeof(buf)); - clientSend(client, buf, len); -} - void stateSync(struct Client *client) { - format(client, ":%s 001 %s :%s\r\n", intro.origin, self.nick, intro.welcome); - format(client, ":%s 002 %s :%s\r\n", intro.origin, self.nick, intro.yourHost); - format(client, ":%s 003 %s :%s\r\n", intro.origin, self.nick, intro.created); - format( - client, ":%s 004 %s %s %s %s\r\n", + assert(stateReady()); + + clientFormat( + client, ":%s 001 %s :%s\r\n", intro.origin, self.nick, intro.welcome + ); + clientFormat( + client, ":%s 002 %s :%s\r\n", intro.origin, self.nick, intro.yourHost + ); + clientFormat( + client, ":%s 003 %s :%s\r\n", intro.origin, self.nick, intro.created + ); + clientFormat( + client, ":%s 004 %s %s %s %s %s\r\n", intro.origin, self.nick, intro.myInfo[0], intro.myInfo[1], intro.myInfo[2], intro.myInfo[3] ); size_t i; for (i = 0; support.len - i >= 13; i += 13) { - format( + clientFormat( client, ":%s 005 %s" " %s %s %s %s %s %s %s %s %s %s %s %s %s" @@ -237,16 +233,26 @@ void stateSync(struct Client *client) { support.tokens[i + 12] ); } - // FIXME: Do something about this? if (i < support.len) { - format(client, ":%s 005 %s", intro.origin, self.nick); + char buf[513]; + size_t len = 0; + len += snprintf( + buf, sizeof(buf), ":%s 005 %s", intro.origin, self.nick + ); for (; i < support.len; ++i) { - format(client, " %s", support.tokens[i]); + len += snprintf( + &buf[len], sizeof(buf) - len, " %s", support.tokens[i] + ); } - format(client, " :are supported by this server\r\n"); + len += snprintf( + &buf[len], sizeof(buf) - len, " :are supported by this server\r\n" + ); + assert(len < sizeof(buf)); + clientSend(client, buf, len); } + if (chan.len) assert(self.origin); for (size_t i = 0; i < chan.len; ++i) { - format(client, ":%s JOIN %s\r\n", self.origin, chan.names[i]); + clientFormat(client, ":%s JOIN %s\r\n", self.origin, chan.names[i]); } } -- cgit 1.4.1