summary refs log tree commit diff
diff options
context:
space:
mode:
-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]);
 	}
 }
> 2019-02-22Rename global tags with angle bracketsJune McEnroe 2019-02-22Show status window while connectingJune McEnroe 2019-02-22Reorganize UI code for the umpteenth timeJune McEnroe It's actually in a good state now, I think. 2019-02-21Replace "view" with "window"June McEnroe I think originally I didn't want to use the same word as curses WINDOW but it's really much clearer for the user if they're just called windows. UI code probably needs yet another rewrite though. Still feels messy. 2019-02-21Remove ROT13June McEnroe It's just not convenient when it can only do the whole line... 2019-02-21Clean up man pageJune McEnroe 2019-01-26Draw UI before connectingJune McEnroe Otherwise the "Traveling" message isn't visible while connecting. 2019-01-25Avoid unused variable warnings with getyxJune McEnroe 2019-01-25Add GNU/Linux build instructionsJune McEnroe