about summary refs log tree commit diff
path: root/reply.c
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2026-01-07 05:03:32 +0300
committerNakidai <nakidai@disroot.org>2026-01-07 05:03:32 +0300
commit5cbbb8342feea527bfe3a5650d7c6a461254964a (patch)
treea790a6fed11fcf3e442839038ea495e0a5fa092d /reply.c
parent62f4f17989622a91aa39834b73d4e527a96ce211 (diff)
downloadlibreircd-5cbbb8342feea527bfe3a5650d7c6a461254964a.tar.gz
libreircd-5cbbb8342feea527bfe3a5650d7c6a461254964a.zip
Use function with switch() in reply.c
Overall, this solution looks better. Also, now macro is better
Diffstat (limited to 'reply.c')
-rw-r--r--reply.c168
1 files changed, 47 insertions, 121 deletions
diff --git a/reply.c b/reply.c
index b73fbea..06d9f92 100644
--- a/reply.c
+++ b/reply.c
@@ -21,188 +21,114 @@
 #include <err.h>
 
 
-typedef int Reply(const struct Peer *peer, va_list ap);
-
-#define REPLY(n) static int r##n (const struct Peer *peer, va_list ap)
 #define ARG(n) const char *n = va_arg(ap, const char *)
-
-REPLY(1)
+#define M1(x, ...)
+#define M2(x, ...) ARG(x); M1(__VA_ARGS__)
+#define M3(x, ...) ARG(x); M2(__VA_ARGS__)
+#define M4(x, ...) ARG(x); M3(__VA_ARGS__)
+#define M5(x, ...) ARG(x); M4(__VA_ARGS__)
+#define M6(x, ...) ARG(x); M5(__VA_ARGS__)
+#define M7(x, ...) ARG(x); M6(__VA_ARGS__)
+#define GET_M(_0,_1,_2,_3,_4,_5,_6,_7,NAME,...) NAME
+#define ARGS(_0, ...) GET_M(_0,__VA_ARGS__,M7,M6,M5,M4,M3,M2,M1,M0,)(__VA_ARGS__,)
+
+#define REPLY(n, action, ...) case n: { ARGS(,__VA_ARGS__); return action; }
+#define WRITE(fmt, ...) writef(peer->fd, fmt, __VA_ARGS__)
+
+
+static int
+vreply(const struct Peer *peer, int number, va_list ap)
 {
-	return writef(
-		peer->fd,
+	switch (number)
+	{
+	REPLY(1, WRITE(
 		":%s 001 %s Welcome to the Internet Relay Network %s!%s@%s",
 		hostname,
 		getnick(peer),
 		getnick(peer),
 		peer->user,
 		peer->host
-	);
-}
-
-REPLY(401)
-{
-	ARG(name);
-	return writef(
-		peer->fd,
+	), _);
+	REPLY(401, WRITE(
 		":%s 401 %s %s :No such nick/channel",
 		hostname,
 		getnick(peer),
 		name
-	);
-}
-
-REPLY(403)
-{
-	ARG(channel);
-	return writef(
-		peer->fd,
+	), name, _);
+	REPLY(403, WRITE(
 		":%s 403 %s %s :No such channel",
 		hostname,
 		getnick(peer),
 		channel
-	);
-}
-
-REPLY(405)
-{
-	ARG(channel);
-	return writef(
-		peer->fd,
+	), channel, _);
+	REPLY(405, WRITE(
 		":%s 405 %s %s :You have joined too many channels",
 		hostname,
 		getnick(peer),
 		channel
-	);
-}
-
-REPLY(411)
-{
-	return writef(
-		peer->fd,
+	), channel, _);
+	REPLY(411, WRITE(
 		":%s 411 %s :No recipient given (PRIVMSG)",
 		hostname,
 		getnick(peer)
-	);
-}
-
-REPLY(412)
-{
-	return writef(
-		peer->fd,
+	), _);
+	REPLY(412, WRITE(
 		":%s 412 %s :No text to send",
 		hostname,
 		getnick(peer)
-	);
-}
-
-REPLY(421)
-{
-	ARG(command);
-	return writef(
-		peer->fd,
+	), _);
+	REPLY(421, WRITE(
 		":%s 421 %s %s :Unknown command",
 		hostname,
 		getnick(peer),
 		command
-	);
-}
-
-REPLY(431)
-{
-	return writef(
-		peer->fd,
+	), command, _);
+	REPLY(431, WRITE(
 		":%s 431 %s :No nickname given",
 		hostname,
 		getnick(peer)
-	);
-}
-
-REPLY(442)
-{
-	ARG(channel);
-	return writef(
-		peer->fd,
+	), _);
+	REPLY(442, WRITE(
 		":%s 442 %s %s :You're not on that channel",
 		hostname,
 		getnick(peer),
 		channel
-	);
-}
-
-REPLY(451)
-{
-	return writef(
-		peer->fd,
+	), channel, _);
+	REPLY(451, WRITE(
 		":%s 451 %s :You have not registered",
 		hostname,
 		getnick(peer)
-	);
-}
-
-REPLY(461)
-{
-	ARG(command);
-	return writef(
-		peer->fd,
+	), _);
+	REPLY(461, WRITE(
 		":%s 461 %s %s :Not enough parameters",
 		hostname,
 		getnick(peer),
 		command
-	);
-}
-
-REPLY(462)
-{
-	return writef(
-		peer->fd,
+	), command, _);
+	REPLY(462, WRITE(
 		":%s 462 %s :Unauthorized command (already registered)",
 		hostname,
 		getnick(peer)
-	);
-}
-
-REPLY(471)
-{
-	ARG(channel);
-	return writef(
+	), _);
+	REPLY(471, WRITE(
 		peer->fd,
 		":%s 471 %s %s :Cannot join channel (+l)",
 		hostname,
 		getnick(peer),
 		channel
-	);
+	), channel, _);
+	default: warnx("YOU FUCKING STUPID SHIT IMPLEMENT REPLY #%d NOW!!!", number); return -1;
+	}
 }
 
-#undef REPLY
-#define REPLY(n) [n] = r##n
-static Reply *replies[] = {
-	REPLY(  1),
-	REPLY(401),
-	REPLY(403),
-	REPLY(405),
-	REPLY(411),
-	REPLY(412),
-	REPLY(421),
-	REPLY(431),
-	REPLY(442),
-	REPLY(451),
-	REPLY(461),
-	REPLY(462),
-	REPLY(471),
-};
-
 int
 reply(const struct Peer *peer, int number, ...)
 {
-	Reply *choice;
 	va_list args;
 	int ret;
 
-	choice = replies[number];
-	if (!choice)
-		warnx("YOU FUCKING STUPID SHIT IMPLEMENT REPLY #%d NOW!!!", number);
 	va_start(args, number);
-	ret = choice(peer, args);
+	ret = vreply(peer, number, args);
 	va_end(args);
 
 	return ret;