# HG changeset patch
# Node ID d6d811ba8493a5da0283855414fbe2333ab4be1e
# Parent  4ff60371673fb56fb0de96de16052ca9f37a3243
diff --git a/protocols/ircv3/meson.build b/protocols/ircv3/meson.build
--- a/protocols/ircv3/meson.build
+++ b/protocols/ircv3/meson.build
@@ -1,4 +1,5 @@
 IRCV3_SOURCES = [
+	'purpleircv3commands.c',
 	'purpleircv3connection.c',
 	'purpleircv3core.c',
 	'purpleircv3ctcphandlers.c',
@@ -8,6 +9,7 @@
 ]
 
 IRCV3_HEADERS = [
+	'purpleircv3commands.h',
 	'purpleircv3connection.h',
 	'purpleircv3core.h',
 	'purpleircv3ctcphandlers.h',
diff --git a/protocols/ircv3/purpleircv3commands.c b/protocols/ircv3/purpleircv3commands.c
new file mode 100644
--- /dev/null
+++ b/protocols/ircv3/purpleircv3commands.c
@@ -0,0 +1,72 @@
+/*
+ * 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 "purpleircv3commands.h"
+
+#include "purpleircv3connection.h"
+
+/******************************************************************************
+ * Internal Callbacks
+ *****************************************************************************/
+gboolean
+purple_ircv3_command_query_cb(G_GNUC_UNUSED PurpleCommand *command,
+                              PurpleConversation *conversation,
+                              GStrv params,
+                              G_GNUC_UNUSED gpointer data)
+{
+    PurpleIRCv3Connection *v3_connection = NULL;
+    PurpleAccount *account = NULL;
+    PurpleConnection *connection = NULL;
+    PurpleConversation *new_conversation = NULL;
+    guint n_params = 0;
+
+    n_params = g_strv_length(params);
+    if(n_params < 1) {
+        return FALSE;
+    }
+
+    account = purple_conversation_get_account(conversation);
+    connection = purple_account_get_connection(account);
+    v3_connection = PURPLE_IRCV3_CONNECTION(connection);
+
+    new_conversation = purple_ircv3_connection_find_or_create_conversation(v3_connection,
+                                                                           params[0]);
+
+    if(n_params > 1) {
+        PurpleMessage *message = NULL;
+        PurpleContactInfo *author = NULL;
+        char *contents = NULL;
+
+        author = purple_account_get_contact_info(account);
+        contents = g_strjoinv(" ", params + 1);
+
+        message = purple_message_new(author, contents);
+        g_free(contents);
+
+        purple_conversation_send_message_async(new_conversation, message, NULL,
+                                               NULL, NULL);
+
+        g_clear_object(&message);
+    }
+
+    return TRUE;
+}
diff --git a/protocols/ircv3/purpleircv3commands.h b/protocols/ircv3/purpleircv3commands.h
new file mode 100644
--- /dev/null
+++ b/protocols/ircv3/purpleircv3commands.h
@@ -0,0 +1,41 @@
+/*
+ * 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_COMMANDS_H
+#define PURPLE_IRCV3_COMMANDS_H
+
+#include <glib.h>
+
+#include <purple.h>
+
+G_BEGIN_DECLS
+
+G_GNUC_INTERNAL gboolean purple_ircv3_command_query_cb(PurpleCommand *command, PurpleConversation *conversation, GStrv params, gpointer data);
+
+G_END_DECLS
+
+#endif /* PURPLE_IRCV3_COMMANDS_H */
diff --git a/protocols/ircv3/purpleircv3core.h b/protocols/ircv3/purpleircv3core.h
--- a/protocols/ircv3/purpleircv3core.h
+++ b/protocols/ircv3/purpleircv3core.h
@@ -35,6 +35,7 @@
 #define PURPLE_IRCV3_DEFAULT_TLS_PORT 6697
 
 #define PURPLE_IRCV3_ICON_NAME "im-ircv3"
+#define PURPLE_IRCV3_PROTOCOL_ID "prpl-ircv3"
 
 #define PURPLE_IRCV3_DOMAIN (g_quark_from_static_string("ircv3-plugin"))
 
diff --git a/protocols/ircv3/purpleircv3protocol.c b/protocols/ircv3/purpleircv3protocol.c
--- a/protocols/ircv3/purpleircv3protocol.c
+++ b/protocols/ircv3/purpleircv3protocol.c
@@ -24,6 +24,7 @@
 
 #include "purpleircv3protocol.h"
 
+#include "purpleircv3commands.h"
 #include "purpleircv3connection.h"
 #include "purpleircv3core.h"
 #include "purpleircv3protocolconversation.h"
@@ -215,7 +216,33 @@
 	                              purple_ircv3_protocol_conversation_init))
 
 static void
-purple_ircv3_protocol_init(G_GNUC_UNUSED PurpleIRCv3Protocol *protocol) {
+purple_ircv3_protocol_finalize(GObject *obj) {
+	PurpleCommandManager *manager = NULL;
+
+	manager = purple_command_manager_get_default();
+	purple_command_manager_remove_all_with_source(manager,
+	                                              PURPLE_IRCV3_PROTOCOL_ID);
+
+	G_OBJECT_CLASS(purple_ircv3_protocol_parent_class)->finalize(obj);
+}
+
+static void
+purple_ircv3_protocol_init(PurpleIRCv3Protocol *protocol) {
+	PurpleCommand *command = NULL;
+	PurpleCommandManager *manager = NULL;
+	PurpleTags *tags = NULL;
+
+	manager = purple_command_manager_get_default();
+
+	command = purple_command_new("query", "IRCv3", 1000);
+	purple_command_set_summary(command,
+	                           _("Open a conversation with the given nick"));
+	tags = purple_command_get_tags(command);
+	purple_tags_add(tags, "protocol-id:" PURPLE_IRCV3_PROTOCOL_ID);
+	g_signal_connect_object(command, "executed",
+	                        G_CALLBACK(purple_ircv3_command_query_cb),
+	                        protocol, G_CONNECT_DEFAULT);
+	purple_command_manager_add(manager, command);
 }
 
 static void
@@ -224,8 +251,11 @@
 
 static void
 purple_ircv3_protocol_class_init(PurpleIRCv3ProtocolClass *klass) {
+	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
 	PurpleProtocolClass *protocol_class = PURPLE_PROTOCOL_CLASS(klass);
 
+	obj_class->finalize = purple_ircv3_protocol_finalize;
+
 	protocol_class->get_user_splits = purple_ircv3_protocol_get_user_splits;
 	protocol_class->get_account_options =
 		purple_ircv3_protocol_get_account_options;
@@ -249,7 +279,7 @@
 purple_ircv3_protocol_new(void) {
 	return g_object_new(
 		PURPLE_IRCV3_TYPE_PROTOCOL,
-		"id", "prpl-ircv3",
+		"id", PURPLE_IRCV3_PROTOCOL_ID,
 		"name", "IRCv3",
 		"description", _("Version 3 of Internet Relay Chat (IRC)."),
 		"icon-name", PURPLE_IRCV3_ICON_NAME,
