protocols/ircv3/purpleircv3commands.c

Thu, 24 Apr 2025 22:19:39 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Thu, 24 Apr 2025 22:19:39 -0500
changeset 43242
a9bc19e23c36
parent 43126
5b5a883528e0
child 43290
01edcfbfebaa
permissions
-rw-r--r--

IRCv3: check if a member is in a conversation before adding them

Awhile ago we update the NAMREPLY handler to build a separate
Purple.ConversationMembers and then splice that onto the existing one to help
with sorting and other performance issues. However, we didn't check if the
users already existed in the existing list, so we would get duplicates. This
address that.

Also fixed a reference leak.

Testing Done:
Joined a channel and sent `/quote names #channel` multiple times and verified that the member list did not grow to include a bunch of duplicates.

Reviewed at https://reviews.imfreedom.org/r/3987/

/*
 * 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;
}

mercurial