about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-10-24 00:26:53 -0400
committerJune McEnroe <june@causal.agency>2019-10-24 00:26:53 -0400
commitd5aedc3d84eea0d970f90ddc04f9be331e9c20e9 (patch)
tree473c357825d0e8515baed6709d2e3f7672d69df0
parentMake serverFormat public (diff)
downloadpounce-d5aedc3d84eea0d970f90ddc04f9be331e9c20e9.tar.gz
pounce-d5aedc3d84eea0d970f90ddc04f9be331e9c20e9.zip
Make clientFormat public
Diffstat (limited to '')
-rw-r--r--bounce.h2
-rw-r--r--client.c4
-rw-r--r--state.c52
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 <assert.h>
 #include <err.h>
-#include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -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]);
 	}
 }