/*
 * 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 <glib/gi18n-lib.h>

#include "purplecommands.h"

#include "purpleconversation.h"
#include "purplemessage.h"

/******************************************************************************
 * Helpers
 *****************************************************************************/
static PurpleMessage *
purple_message_from_command(PurpleConversation *conversation, GStrv params) {
	PurpleAccount *account = NULL;
	PurpleContactInfo *info = NULL;
	PurpleConversationMember *member = NULL;
	PurpleConversationMembers *members = NULL;
	PurpleMessage *message = NULL;
	char *body = NULL;

	account = purple_conversation_get_account(conversation);
	if(!PURPLE_IS_ACCOUNT(account)) {
		return NULL;
	}

	members = purple_conversation_get_members(conversation);

	info = purple_account_get_contact_info(account);
	member = purple_conversation_members_find_member(members, info);

	body = g_strjoinv(" ", params);
	message = purple_message_new(member, body);
	g_free(body);

	return message;
}

/******************************************************************************
 * Action Command
 *****************************************************************************/
static void
purple_action_command_executed(G_GNUC_UNUSED PurpleCommand *command,
                               PurpleConversation *conversation,
                               GStrv params,
                               G_GNUC_UNUSED gpointer data)
{
	PurpleMessage *message = NULL;

	message = purple_message_from_command(conversation, params);
	if(PURPLE_IS_MESSAGE(message)) {
		purple_message_set_action(message, TRUE);
		purple_conversation_send_message_async(conversation, message, NULL,
		                                       NULL, NULL);
	}

	g_clear_object(&message);
}

PurpleCommand *
purple_action_command_new(void) {
	PurpleCommand *command = NULL;

	command = purple_command_new("action", "libpurple", 0);
	purple_command_set_summary(command, _("Sends a message as an action"));
	g_signal_connect(command, "executed",
	                 G_CALLBACK(purple_action_command_executed), NULL);

	return command;
}

/******************************************************************************
 * Say Command
 *****************************************************************************/
static void
purple_say_command_executed(G_GNUC_UNUSED PurpleCommand *command,
                            PurpleConversation *conversation,
                            GStrv params,
                            G_GNUC_UNUSED gpointer data)
{
	PurpleMessage *message = NULL;

	message = purple_message_from_command(conversation, params);
	if(PURPLE_IS_MESSAGE(message)) {
		purple_conversation_send_message_async(conversation, message, NULL,
		                                       NULL, NULL);
	}

	g_clear_object(&message);
}

PurpleCommand *
purple_say_command_new(void) {
	PurpleCommand *command = NULL;

	command = purple_command_new("say", "libpurple", 0);
	purple_command_set_summary(command, _("Sends a message"));
	g_signal_connect(command, "executed",
	                 G_CALLBACK(purple_say_command_executed), NULL);

	return command;
}
