# HG changeset patch
# Node ID 25ba309901b160a51446adb19ce625ddde3d75ac
# Parent  ac5311a8f11ebadd2282dffbe6141a79ad6e4163
diff --git a/libpurple/core.c b/libpurple/core.c
--- a/libpurple/core.c
+++ b/libpurple/core.c
@@ -133,6 +133,9 @@
 
 	purple_credential_manager_startup(core); /* before accounts */
 
+	/* Before plugins as they will add commands. */
+	purple_command_manager_startup();
+
 	/* Since plugins get probed so early we should probably initialize their
 	 * subsystem right away too.
 	 */
@@ -143,7 +146,6 @@
 	purple_contact_manager_startup();
 	purple_presence_manager_startup();
 	purple_conversation_manager_startup();
-	purple_command_manager_startup();
 	purple_whiteboard_manager_startup();
 
 	/* Setup the history adapter. */
@@ -209,7 +211,6 @@
 
 	/* Save .xml files, remove signals, etc. */
 	purple_idle_manager_shutdown();
-	purple_command_manager_shutdown();
 	purple_whiteboard_manager_shutdown();
 	purple_conversation_manager_shutdown();
 	purple_presence_manager_shutdown();
@@ -229,6 +230,7 @@
 	purple_account_manager_shutdown();
 	purple_credential_manager_shutdown(core);
 	purple_protocol_manager_shutdown();
+	purple_command_manager_shutdown();
 
 	purple_notification_manager_shutdown();
 	purple_history_manager_shutdown();
diff --git a/protocols/demo/meson.build b/protocols/demo/meson.build
--- a/protocols/demo/meson.build
+++ b/protocols/demo/meson.build
@@ -1,4 +1,6 @@
 DEMO_SOURCES = [
+	'purpledemocommands.c',
+	'purpledemocommands.h',
 	'purpledemoconnection.c',
 	'purpledemoconnection.h',
 	'purpledemocontacts.c',
diff --git a/protocols/demo/purpledemocommands.c b/protocols/demo/purpledemocommands.c
new file mode 100644
--- /dev/null
+++ b/protocols/demo/purpledemocommands.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 "purpledemocommands.h"
+
+#define PURPLE_DEMO_COMMAND_SOURCE N_("Demo Protocol")
+
+/******************************************************************************
+ * Command Implementations
+ *****************************************************************************/
+static void
+purple_demo_command_edited_executed(G_GNUC_UNUSED PurpleCommand *command,
+                                    PurpleConversation *conversation,
+                                    GStrv params,
+                                    G_GNUC_UNUSED gpointer data)
+{
+	PurpleAccount *account = NULL;
+	PurpleContactInfo *info = NULL;
+	PurpleMessage *message = NULL;
+	char *contents = NULL;
+
+	account = purple_conversation_get_account(conversation);
+	info = purple_account_get_contact_info(account);
+
+	contents = g_strjoinv(" ", params);
+
+	message = purple_message_new(info, contents);
+	purple_message_set_edited(message, TRUE);
+
+	g_clear_pointer(&contents, g_free);
+
+	purple_conversation_send_message_async(conversation, message, NULL, NULL,
+	                                       NULL);
+	g_clear_object(&message);
+}
+
+/******************************************************************************
+ * Public API
+ *****************************************************************************/
+void
+purple_demo_commands_add(void) {
+	PurpleCommand *command = NULL;
+	PurpleCommandManager *manager = NULL;
+	PurpleTags *tags = NULL;
+
+	manager = purple_command_manager_get_default();
+
+	command = purple_command_new("edited", PURPLE_DEMO_COMMAND_SOURCE, 1000);
+	purple_command_set_summary(command,
+	                           N_("Sends the contents as an edited message"));
+	g_signal_connect(command, "executed",
+	                 G_CALLBACK(purple_demo_command_edited_executed), NULL);
+	tags = purple_command_get_tags(command);
+	purple_tags_add(tags, "protocol-id:prpl-demo");
+	purple_command_manager_add(manager, command);
+}
+
+void
+purple_demo_commands_remove(void) {
+	PurpleCommandManager *manager = purple_command_manager_get_default();
+
+	purple_command_manager_remove_all_with_source(manager,
+	                                              PURPLE_DEMO_COMMAND_SOURCE);
+}
\ No newline at end of file
diff --git a/protocols/demo/purpledemocommands.h b/protocols/demo/purpledemocommands.h
new file mode 100644
--- /dev/null
+++ b/protocols/demo/purpledemocommands.h
@@ -0,0 +1,33 @@
+/*
+ * 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/>.
+ */
+
+#ifndef PURPLE_DEMO_COMMANDS_H
+#define PURPLE_DEMO_COMMANDS_H
+
+#include <glib.h>
+
+#include <purple.h>
+
+G_BEGIN_DECLS
+
+G_GNUC_INTERNAL void purple_demo_commands_add(void);
+G_GNUC_INTERNAL void purple_demo_commands_remove(void);
+
+G_END_DECLS
+
+#endif /* PURPLE_DEMO_COMMANDS_H */
diff --git a/protocols/demo/purpledemoplugin.c b/protocols/demo/purpledemoplugin.c
--- a/protocols/demo/purpledemoplugin.c
+++ b/protocols/demo/purpledemoplugin.c
@@ -26,6 +26,7 @@
 
 #include "purpledemoplugin.h"
 
+#include "purpledemocommands.h"
 #include "purpledemoconnection.h"
 #include "purpledemoprotocol.h"
 
@@ -84,6 +85,8 @@
 		return FALSE;
 	}
 
+	purple_demo_commands_add();
+
 	return TRUE;
 }
 
@@ -107,6 +110,8 @@
 
 	g_clear_object(&demo_protocol);
 
+	purple_demo_commands_remove();
+
 	return TRUE;
 }
 
diff --git a/protocols/demo/purpledemoprotocolconversation.c b/protocols/demo/purpledemoprotocolconversation.c
--- a/protocols/demo/purpledemoprotocolconversation.c
+++ b/protocols/demo/purpledemoprotocolconversation.c
@@ -251,6 +251,8 @@
 			info = g_new(PurpleDemoProtocolIMInfo, 1);
 			info->conversation = g_object_ref(conversation);
 			info->message = purple_message_new(contact_info, contents);
+			purple_message_set_edited(info->message,
+			                          purple_message_get_edited(message));
 
 			g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
 			                purple_demo_protocol_echo_im_cb, info,
