purplesatoriprotocolconversation.c

Sun, 10 Aug 2025 23:53:22 +0800

author
Gong Zhile <gongzl@stu.hebust.edu.cn>
date
Sun, 10 Aug 2025 23:53:22 +0800
changeset 3
33a7b189a2c6
parent 1
98bcf06036b8
permissions
-rw-r--r--

Various improvement, Support configuration from UI

/*
 * Purple Satori Plugin - Satori Protocol Plugin for Purple3
 * Copyright (C) 2025 Gong Zhile
 *
 * 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 "purplesatoriprotocolconversation.h"

#include "glib.h"
#include "purplesatoriconnection.h"
#include "purplesatoriplugin.h"
#include "purplesatoriprotocol.h"
#include "satoriapi.h"
#include "satoritypes.h"

/******************************************************************************
 * PurpleProtocolConversation Implementation
 *****************************************************************************/
static PurpleCreateConversationDetails *
purple_satori_protocol_get_create_conversation_details(G_GNUC_UNUSED PurpleProtocolConversation *protocol,
						       G_GNUC_UNUSED PurpleAccount *account)
{
	return purple_create_conversation_details_new(2);
}

static void
purple_satori_protocol_create_conversation_async(PurpleProtocolConversation *protocol,
						 PurpleAccount *account,
						 PurpleCreateConversationDetails *details,
						 GCancellable *cancellable,
						 GAsyncReadyCallback callback,
						 gpointer data)
{
	GListModel		*participants	= NULL;
	GTask			*task		= NULL;
	guint			 n_participants = 0;

	task = g_task_new(protocol, cancellable, callback, data);
	g_task_set_source_tag(task,
	                      purple_satori_protocol_create_conversation_async);

	participants = purple_create_conversation_details_get_participants(details);
	n_participants = g_list_model_get_n_items(participants);
	if (n_participants == 0) {
		g_task_return_new_error_literal(task, PURPLE_SATORI_DOMAIN, 0,
		                                _("no participants were provided"));
		g_clear_object(&task);
		return;
	}

	if (n_participants > 1) {
		g_task_return_new_error_literal(task, PURPLE_SATORI_DOMAIN, 0,
		                                _("only dm conversation supported"));
		g_clear_object(&task);
		return;		/* not implimented */
	}

	for(guint i = 0; i < g_list_model_get_n_items(participants); i++) {
		PurpleContactInfo *info = NULL;
		SatoriUser user = { 0 };

		info = g_list_model_get_item(participants, i);
		satori_user_from_contactinfo(info, &user);

		satori_create_dm_channel(
			PURPLE_SATORI_CONNECTION(
				purple_account_get_connection(account)),
			&user, task);

		g_clear_object(&info);
		g_clear_object(&details);
		return;
	}
}

static PurpleConversation *
purple_satori_protocol_create_conversation_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol,
                                                GAsyncResult *result,
                                                GError **error)
{
	GTask *task = G_TASK(result);

	g_return_val_if_fail(g_task_get_source_tag(task) ==
	                     purple_satori_protocol_create_conversation_async,
	                     NULL);

	return g_task_propagate_pointer(task, error);
}

static void
purple_satori_protocol_send_message_async(PurpleProtocolConversation *protocol,
					  PurpleConversation *conversation,
					  PurpleMessage *message,
					  GCancellable *cancellable,
					  GAsyncReadyCallback callback,
					  gpointer data)
{
	PurpleSatoriConnection *con = \
		PURPLE_SATORI_CONNECTION(purple_conversation_get_connection(conversation));

	purple_conversation_write_message(conversation, message);

	GTask *task = g_task_new(protocol, cancellable, callback, data);
	g_task_set_source_tag(task,
			      purple_satori_protocol_send_message_async);

	gchar *content = \
		g_markup_escape_text(purple_message_get_contents(message), -1);
	satori_send_message(con, conversation, message, content, task);
	g_free(content);

	return;
}

static gboolean
purple_satori_protocol_send_message_finish(G_GNUC_UNUSED PurpleProtocolConversation *protocol,
                                         GAsyncResult *result,
                                         GError **error)
{
	g_return_val_if_fail(G_IS_TASK(result), FALSE);

	return g_task_propagate_boolean(G_TASK(result), error);
}

void
purple_satori_protocol_conversation_init(PurpleProtocolConversationInterface *iface) {
	iface->get_create_conversation_details =
		purple_satori_protocol_get_create_conversation_details;
	iface->create_conversation_async =
		purple_satori_protocol_create_conversation_async;
	iface->create_conversation_finish =
		purple_satori_protocol_create_conversation_finish;

	iface->send_message_async = purple_satori_protocol_send_message_async;
	iface->send_message_finish = purple_satori_protocol_send_message_finish;
}

mercurial