about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ircd.h4
-rw-r--r--reply.c32
2 files changed, 33 insertions, 3 deletions
diff --git a/ircd.h b/ircd.h
index 07bdd0f..6035b79 100644
--- a/ircd.h
+++ b/ircd.h
@@ -30,6 +30,10 @@
 
 #define BIT(x) x##_BIT, x = 1 << x##_BIT, x##_BIT_ = x##_BIT
 #define lengthof(X) (sizeof(X) / sizeof(*(X)))
+
+/* when `ensure' is used, it's better to think of it like it's the condition under which code
+ * below "exists" (so, ensure that `cond' evaluates to true), and not like it's a condition when
+ * to stop, trying to invert cond */
 #define ensure(cond, iffalse, doexit) if (!(cond)) { iffalse; if (doexit >= 0) return doexit; }
 
 struct Message
diff --git a/reply.c b/reply.c
index 06d9f92..eaa56f4 100644
--- a/reply.c
+++ b/reply.c
@@ -32,8 +32,29 @@
 #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__,)
 
+/*
+ * REPLY macro allows to write less boilerplate for each IRC reply
+ * For example,
+ * | REPLY(401, WRITE(
+ * | 	":%s 401 %s %s :No such nick/channel",
+ * | 	hostname,
+ * | 	getnick(peer),
+ * | 	name
+ * | ), name, _);
+ * is unrolled into
+ * | {
+ * | 	const char *name = va_arg(ap, const char *);
+ * | 	return WRITE(
+ * | 		":%s 401 %s %s :No such nick/channel",
+ * | 		hostname,
+ * | 		getnick(peer),
+ * | 		name
+ * | 	);
+ * | }
+ *
+ * Note the `_' argument, it's present as C doesn't support empty __VA_ARGS__
+ */
 #define REPLY(n, action, ...) case n: { ARGS(,__VA_ARGS__); return action; }
-#define WRITE(fmt, ...) writef(peer->fd, fmt, __VA_ARGS__)
 
 
 static int
@@ -41,6 +62,7 @@ vreply(const struct Peer *peer, int number, va_list ap)
 {
 	switch (number)
 	{
+#define WRITE(...) writef(peer->fd, __VA_ARGS__)
 	REPLY(1, WRITE(
 		":%s 001 %s Welcome to the Internet Relay Network %s!%s@%s",
 		hostname,
@@ -111,13 +133,17 @@ vreply(const struct Peer *peer, int number, va_list ap)
 		getnick(peer)
 	), _);
 	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;
+	default: warn("unknown reply: %d", number); WRITE(
+		":%s 421 %s err :Reply %d is not implemented yet",
+		hostname,
+		getnick(peer),
+		number
+	);
 	}
 }