From 6003cc7138c4cf1424ba38a533553015bfa84898 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch <p.janouch@gmail.com>
Date: Mon, 27 Apr 2015 01:47:21 +0200
Subject: degesch: log outcoming CTCPs

---
 degesch.c | 45 +++++++++++++++++++++++++++++++++------------
 1 file changed, 33 insertions(+), 12 deletions(-)

diff --git a/degesch.c b/degesch.c
index 05b22ba..ab4c3ed 100644
--- a/degesch.c
+++ b/degesch.c
@@ -2656,6 +2656,33 @@ ctime_now (char buf[26])
 	return buf;
 }
 
+static void irc_send_ctcp_reply (struct app_context *ctx, const char *recipient,
+	const char *format, ...) ATTRIBUTE_PRINTF (3, 4);
+
+static void
+irc_send_ctcp_reply (struct app_context *ctx,
+	const char *recipient, const char *format, ...)
+{
+	struct str m;
+	str_init (&m);
+
+	va_list ap;
+	va_start (ap, format);
+	str_append_vprintf (&m, format, ap);
+	va_end (ap);
+
+	irc_send (ctx, "NOTICE %s :\x01%s\x01", recipient, m.str);
+
+	char *text_utf8 = irc_to_utf8 (ctx, m.str);
+	char *recipient_utf8 = irc_to_utf8 (ctx, recipient);
+	str_free (&m);
+
+	buffer_send_status (ctx, ctx->server_buffer,
+		"CTCP reply to %s: %s", recipient_utf8, text_utf8);
+	free (text_utf8);
+	free (recipient_utf8);
+}
+
 static void
 irc_handle_ctcp_request (struct app_context *ctx,
 	const struct irc_message *msg, struct ctcp_chunk *chunk)
@@ -2672,25 +2699,19 @@ irc_handle_ctcp_request (struct app_context *ctx,
 	if (irc_is_channel (ctx, target))
 		recipient = target;
 
-	// TODO: log a "CTCP reply to <recipient>: <whatever>
-	//   Probably abstract the irc_send call to something like
-	//   irc_send_ctcp_reply (ctx, recipient, fmt, ...)
-
 	if (!strcmp (chunk->tag.str, "CLIENTINFO"))
-		irc_send (ctx, "NOTICE %s :\x01" "CLIENTINFO %s %s %s %s\x01",
-			recipient, "PING", "VERSION", "TIME", "CLIENTINFO");
+		irc_send_ctcp_reply (ctx, recipient, "CLIENTINFO %s %s %s %s",
+			"PING", "VERSION", "TIME", "CLIENTINFO");
 	else if (!strcmp (chunk->tag.str, "PING"))
-		irc_send (ctx, "NOTICE %s :\x01" "PING %s\x01",
-			recipient, chunk->text.str);
+		irc_send_ctcp_reply (ctx, recipient, "PING %s", chunk->text.str);
 	else if (!strcmp (chunk->tag.str, "VERSION"))
 	{
 		struct utsname info;
 		if (uname (&info))
 			LOG_LIBC_FAILURE ("uname");
 		else
-			irc_send (ctx, "NOTICE %s :\x01" "VERSION %s %s on %s %s\x01",
-				recipient, PROGRAM_NAME, PROGRAM_VERSION,
-				info.sysname, info.machine);
+			irc_send_ctcp_reply (ctx, recipient, "VERSION %s %s on %s %s",
+				PROGRAM_NAME, PROGRAM_VERSION, info.sysname, info.machine);
 	}
 	else if (!strcmp (chunk->tag.str, "TIME"))
 	{
@@ -2698,7 +2719,7 @@ irc_handle_ctcp_request (struct app_context *ctx,
 		if (!ctime_now (buf))
 			LOG_LIBC_FAILURE ("asctime_r");
 		else
-			irc_send (ctx, "NOTICE %s :\x01" "TIME %s\x01", recipient, buf);
+			irc_send_ctcp_reply (ctx, recipient, "TIME %s", buf);
 	}
 
 	free (nickname);
-- 
cgit v1.2.3-70-g09d2