# HG changeset patch
# Node ID bee1a3724954
# Parent  b5832535a17b
diff --git a/libpurple/protocols/ircv3/meson.build b/libpurple/protocols/ircv3/meson.build
--- a/libpurple/protocols/ircv3/meson.build
+++ b/libpurple/protocols/ircv3/meson.build
@@ -5,7 +5,7 @@
 	'purpleircv3messagehandlers.c',
 	'purpleircv3parser.c',
 	'purpleircv3protocol.c',
-	'purpleircv3protocolim.c',
+	'purpleircv3protocolconversation.c',
 	'purpleircv3sasl.c',
 ]
 
@@ -16,7 +16,7 @@
 	'purpleircv3messagehandlers.h',
 	'purpleircv3parser.h',
 	'purpleircv3protocol.h',
-	'purpleircv3protocolim.h',
+	'purpleircv3protocolconversation.h',
 	'purpleircv3sasl.h',
 ]
 
diff --git a/libpurple/protocols/ircv3/purpleircv3messagehandlers.c b/libpurple/protocols/ircv3/purpleircv3messagehandlers.c
--- a/libpurple/protocols/ircv3/purpleircv3messagehandlers.c
+++ b/libpurple/protocols/ircv3/purpleircv3messagehandlers.c
@@ -159,12 +159,25 @@
 	conversation = purple_conversation_manager_find(conversation_manager,
 	                                                account, target);
 	if(!PURPLE_IS_CONVERSATION(conversation)) {
+		PurpleConversationType type = PurpleConversationTypeUnset;
+		const char *id = NULL;
+
 		if(target[0] == '#') {
-			conversation = purple_chat_conversation_new(account, target);
+			type = PurpleConversationTypeChannel;
+			id = target;
 		} else {
-			conversation = purple_im_conversation_new(account, source);
+			type = PurpleConversationTypeDM;
+			id = source;
 		}
 
+		conversation = g_object_new(
+			PURPLE_TYPE_CONVERSATION,
+			"account", account,
+			"id", id,
+			"name", id,
+			"type", type,
+			NULL);
+
 		purple_conversation_manager_register(conversation_manager,
 		                                     conversation);
 
diff --git a/libpurple/protocols/ircv3/purpleircv3protocol.c b/libpurple/protocols/ircv3/purpleircv3protocol.c
--- a/libpurple/protocols/ircv3/purpleircv3protocol.c
+++ b/libpurple/protocols/ircv3/purpleircv3protocol.c
@@ -22,7 +22,7 @@
 
 #include "purpleircv3connection.h"
 #include "purpleircv3core.h"
-#include "purpleircv3protocolim.h"
+#include "purpleircv3protocolconversation.h"
 
 /******************************************************************************
  * PurpleProtocol Implementation
@@ -151,8 +151,8 @@
 	purple_ircv3_protocol,
 	PURPLE_TYPE_PROTOCOL,
 	0,
-	G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_IM,
-	                              purple_ircv3_protocol_im_init))
+	G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_CONVERSATION,
+	                              purple_ircv3_protocol_conversation_init))
 
 static void
 purple_ircv3_protocol_init(G_GNUC_UNUSED PurpleIRCv3Protocol *protocol) {
diff --git a/libpurple/protocols/ircv3/purpleircv3protocolconversation.h b/libpurple/protocols/ircv3/purpleircv3protocolconversation.h
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/ircv3/purpleircv3protocolconversation.h
@@ -0,0 +1,36 @@
+/*
+ * Purple - Internet Messaging Library
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * This program 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 program 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 program; 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_PROTOCOL_CONVERSATION_H
+#define PURPLE_IRCV3_PROTOCOL_CONVERSATION_H
+
+#include <glib.h>
+
+#include <purple.h>
+
+G_BEGIN_DECLS
+
+G_GNUC_INTERNAL void purple_ircv3_protocol_conversation_init(PurpleProtocolConversationInterface *iface);
+
+G_END_DECLS
+
+#endif /* PURPLE_IRCV3_PROTOCOL_CONVERSATION_H */
diff --git a/libpurple/protocols/ircv3/purpleircv3protocolconversation.c b/libpurple/protocols/ircv3/purpleircv3protocolconversation.c
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/ircv3/purpleircv3protocolconversation.c
@@ -0,0 +1,81 @@
+/*
+ * Purple - Internet Messaging Library
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n-lib.h>
+
+#include "purpleircv3protocolconversation.h"
+
+#include "purpleircv3connection.h"
+
+/******************************************************************************
+ * PurpleProtocolConversation Implementation
+ *****************************************************************************/
+static void
+purple_ircv3_protocol_conversation_send_message_async(PurpleProtocolConversation *protocol,
+                                                      PurpleConversation *conversation,
+                                                      PurpleMessage *message,
+                                                      GCancellable *cancellable,
+                                                      GAsyncReadyCallback callback,
+                                                      gpointer data)
+{
+	PurpleIRCv3Connection *v3_connection = NULL;
+	PurpleAccount *account = NULL;
+	PurpleConnection *connection = NULL;
+	GTask *task = NULL;
+	const char *id = NULL;
+
+	account = purple_conversation_get_account(conversation);
+	connection = purple_account_get_connection(account);
+	v3_connection = PURPLE_IRCV3_CONNECTION(connection);
+
+	id = purple_conversation_get_id(conversation);
+	/* TODO: the new message dialog sets the name but not the id and we want to
+	 * use the id only, so for now if id is NULL we grab the name.
+	 */
+	if(purple_strempty(id)) {
+		id = purple_conversation_get_name(conversation);
+	}
+
+	purple_ircv3_connection_writef(v3_connection, "PRIVMSG %s :%s", id,
+	                               purple_message_get_contents(message));
+
+	task = g_task_new(protocol, cancellable, callback, data);
+	g_task_return_boolean(task, TRUE);
+	g_clear_object(&task);
+
+	/* This will be made conditional when we add echo-message support.
+	 * https://ircv3.net/specs/extensions/echo-message
+	 */
+	purple_conversation_write_message(conversation, message);
+}
+
+static gboolean
+purple_ircv3_protocol_conversation_send_message_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol,
+                                                       GAsyncResult *result,
+                                                       GError **error)
+{
+	return g_task_propagate_boolean(G_TASK(result), error);
+}
+
+void
+purple_ircv3_protocol_conversation_init(PurpleProtocolConversationInterface *iface) {
+	iface->send_message_async =
+		purple_ircv3_protocol_conversation_send_message_async;
+	iface->send_message_finish =
+		purple_ircv3_protocol_conversation_send_message_finish;
+}
diff --git a/libpurple/protocols/ircv3/purpleircv3protocolim.h b/libpurple/protocols/ircv3/purpleircv3protocolim.h
deleted file mode 100644
--- a/libpurple/protocols/ircv3/purpleircv3protocolim.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Purple - Internet Messaging Library
- * Copyright (C) Pidgin Developers <devel@pidgin.im>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser 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_PROTOCOL_IM_H
-#define PURPLE_IRCV3_PROTOCOL_IM_H
-
-#include <glib.h>
-
-#include <purple.h>
-
-G_GNUC_INTERNAL void purple_ircv3_protocol_im_init(PurpleProtocolIMInterface *iface);
-
-#endif /* PURPLE_IRCV3_PROTOCOL_IM_H */
diff --git a/libpurple/protocols/ircv3/purpleircv3protocolim.c b/libpurple/protocols/ircv3/purpleircv3protocolim.c
deleted file mode 100644
--- a/libpurple/protocols/ircv3/purpleircv3protocolim.c
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Purple - Internet Messaging Library
- * Copyright (C) Pidgin Developers <devel@pidgin.im>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, see <https://www.gnu.org/licenses/>.
- */
-
-#include <glib/gi18n-lib.h>
-
-#include "purpleircv3protocolim.h"
-
-#include "purpleircv3connection.h"
-
-/******************************************************************************
- * PurpleProtocolIM Implementation
- *****************************************************************************/
-static gint
-purple_ircv3_protocol_send_im(G_GNUC_UNUSED PurpleProtocolIM *im,
-                              PurpleConnection *conn,
-                              G_GNUC_UNUSED PurpleConversation *conversation,
-                              PurpleMessage *message)
-{
-	PurpleIRCv3Connection *connection = PURPLE_IRCV3_CONNECTION(conn);
-	const char *contents = NULL;
-	const char *recipient = NULL;
-
-	contents = purple_message_get_contents(message);
-	recipient = purple_message_get_recipient(message);
-
-	purple_ircv3_connection_writef(connection, "PRIVMSG %s :%s", recipient,
-	                               contents);
-
-	return 1;
-}
-
-void
-purple_ircv3_protocol_im_init(PurpleProtocolIMInterface *iface) {
-	iface->send = purple_ircv3_protocol_send_im;
-}
