# HG changeset patch
# Node ID f01688c6999ad268b70e823319e211ad7b5786c0
# Parent  0253971eeb18dd103477b65bdfe8cbe889b8c40b
diff --git a/protocols/ircv3/meson.build b/protocols/ircv3/meson.build
--- a/protocols/ircv3/meson.build
+++ b/protocols/ircv3/meson.build
@@ -1,6 +1,7 @@
 IRCV3_SOURCES = [
 	'purpleircv3connection.c',
 	'purpleircv3core.c',
+	'purpleircv3ctcphandlers.c',
 	'purpleircv3messagehandlers.c',
 	'purpleircv3protocol.c',
 	'purpleircv3protocolconversation.c',
@@ -9,6 +10,7 @@
 IRCV3_HEADERS = [
 	'purpleircv3connection.h',
 	'purpleircv3core.h',
+	'purpleircv3ctcphandlers.h',
 	'purpleircv3messagehandlers.h',
 	'purpleircv3protocol.h',
 	'purpleircv3protocolconversation.h',
@@ -18,7 +20,7 @@
 	subdir_done()
 endif
 
-ibis_dep = dependency('ibis', version : '>= 0.8.0')
+ibis_dep = dependency('ibis', version : '>= 0.8.1')
 
 ircv3_filebase = f'purple-@purple_major_version@-ircv3'
 
diff --git a/protocols/ircv3/purpleircv3ctcphandlers.c b/protocols/ircv3/purpleircv3ctcphandlers.c
new file mode 100644
--- /dev/null
+++ b/protocols/ircv3/purpleircv3ctcphandlers.c
@@ -0,0 +1,76 @@
+/*
+ * Purple - Internet Messaging Library
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * Purple is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "purpleircv3ctcphandlers.h"
+
+/******************************************************************************
+ * Handlers
+ *****************************************************************************/
+static void
+purple_ircv3_ctcp_handle_version(G_GNUC_UNUSED PurpleIRCv3Connection *connection,
+                                 IbisClient *client, IbisMessage *message,
+                                 G_GNUC_UNUSED IbisCTCPMessage *ctcp_message)
+{
+	PurpleUi *ui = NULL;
+	IbisMessage *response = NULL;
+	IbisCTCPMessage *ctcp_response = NULL;
+	char *nick = NULL;
+	const char *id = NULL;
+	const char *source = NULL;
+
+	source = ibis_message_get_source(message);
+	ibis_source_parse(source, &nick, NULL, NULL);
+
+	ui = purple_core_get_ui();
+	id = purple_ui_get_id(ui);
+
+	ctcp_response = ibis_ctcp_message_new(IBIS_CTCP_VERSION);
+	ibis_ctcp_message_set_params(ctcp_response, id, NULL);
+
+	response = ibis_message_new(IBIS_MSG_NOTICE);
+	ibis_message_set_params(response, nick, NULL);
+	g_free(nick);
+
+	ibis_message_set_ctcp_message(response, ctcp_response);
+	g_clear_object(&ctcp_response);
+
+	ibis_client_write(client, response);
+}
+
+/******************************************************************************
+ * Internal API
+ *****************************************************************************/
+void
+purple_ircv3_ctcp_handler(PurpleIRCv3Connection *connection,
+                          IbisClient *client, IbisMessage *message)
+{
+	IbisCTCPMessage *ctcp_message = NULL;
+	const char *command = NULL;
+
+	ctcp_message = ibis_message_get_ctcp_message(message);
+	command = ibis_ctcp_message_get_command(ctcp_message);
+
+	if(purple_strequal(command, IBIS_CTCP_VERSION)) {
+		purple_ircv3_ctcp_handle_version(connection, client, message,
+		                                 ctcp_message);
+	}
+}
diff --git a/protocols/ircv3/purpleircv3ctcphandlers.h b/protocols/ircv3/purpleircv3ctcphandlers.h
new file mode 100644
--- /dev/null
+++ b/protocols/ircv3/purpleircv3ctcphandlers.h
@@ -0,0 +1,45 @@
+/*
+ * Purple - Internet Messaging Library
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * Purple is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#if !defined(PURPLE_IRCV3_GLOBAL_HEADER_INSIDE) && \
+    !defined(PURPLE_IRCV3_COMPILATION)
+# error "only <libpurple/protocols/ircv3.h> may be included directly"
+#endif
+
+#ifndef PURPLE_IRCV3_CTCP_HANDLERS_H
+#define PURPLE_IRCV3_CTCP_HANDLERS_H
+
+#include <glib.h>
+
+#include <ibis.h>
+
+#include <purple.h>
+
+#include "purpleircv3connection.h"
+
+G_BEGIN_DECLS
+
+G_GNUC_INTERNAL void purple_ircv3_ctcp_handler(PurpleIRCv3Connection *connection, IbisClient *client, IbisMessage *message);
+
+G_END_DECLS
+
+#endif /* PURPLE_IRCV3_CTCP_HANDLERS_H */
diff --git a/protocols/ircv3/purpleircv3messagehandlers.c b/protocols/ircv3/purpleircv3messagehandlers.c
--- a/protocols/ircv3/purpleircv3messagehandlers.c
+++ b/protocols/ircv3/purpleircv3messagehandlers.c
@@ -26,6 +26,7 @@
 
 #include "purpleircv3connection.h"
 #include "purpleircv3core.h"
+#include "purpleircv3ctcphandlers.h"
 
 /******************************************************************************
  * Helpers
@@ -421,15 +422,19 @@
 		purple_message_set_timestamp_now(message);
 	}
 
-	/* Check if this is a CTCP message. */
-	if(ibis_message_get_ctcp(ibis_message)) {
-		/* TODO: later... */
-	}
-
 	purple_conversation_write_message(conversation, message);
 
 	g_clear_object(&message);
 
+	/* If the message contained a CTCP message and was a PRIVMSG, we then need
+	 * to attempt to handle it.
+	 */
+	if(IBIS_IS_CTCP_MESSAGE(ctcp_message) &&
+	   purple_strequal(command, IBIS_MSG_PRIVMSG))
+	{
+		purple_ircv3_ctcp_handler(connection, client, ibis_message);
+	}
+
 	return TRUE;
 }
 
diff --git a/subprojects/ibis.wrap b/subprojects/ibis.wrap
--- a/subprojects/ibis.wrap
+++ b/subprojects/ibis.wrap
@@ -1,4 +1,4 @@
 [wrap-hg]
 directory = ibis
 url = https://keep.imfreedom.org/ibis/ibis
-revision = v0.8.0
+revision = v0.8.1
