/*
 * 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 *info = NULL;
		PurpleConversationMember *member = NULL;
		PurpleConversationMembers *members = NULL;
		char *contents = NULL;

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

		contents = g_strjoinv(" ", params + 1);

		message = purple_message_new(member, contents);
		g_free(contents);

		purple_conversation_send_message_async(new_conversation, message, NULL,
		                                       NULL, NULL);

		g_clear_object(&message);
	}

	return TRUE;
}

gboolean
purple_ircv3_command_quote_cb(G_GNUC_UNUSED PurpleCommand *command,
                              PurpleConversation *conversation,
                              GStrv params,
                              G_GNUC_UNUSED gpointer data)
{
	IbisMessage *message = NULL;
	char *raw = NULL;

	raw = g_strjoinv(" ", params);
	message = ibis_message_parse(raw, NULL);
	g_free(raw);

	if(IBIS_IS_MESSAGE(message)) {
		PurpleIRCv3Connection *v3_connection = NULL;
		PurpleAccount *account = NULL;
		PurpleConnection *connection = NULL;
		IbisClient *client = NULL;

		account = purple_conversation_get_account(conversation);
		connection = purple_account_get_connection(account);
		v3_connection = PURPLE_IRCV3_CONNECTION(connection);
		client = purple_ircv3_connection_get_client(v3_connection);

		ibis_client_write(client, message);

		return TRUE;
	}

	return FALSE;
}
