besser82 / rpms / pidgin

Forked from rpms/pidgin 6 years ago
Clone
e4aaff4
/*
e4aaff4
 * One Time Password support plugin for libpurple
e4aaff4
 *
e4aaff4
 * Copyright (C) 2009, Daniel Atallah <datallah@pidgin.im>
e4aaff4
 *
e4aaff4
 * This program is free software; you can redistribute it and/or
e4aaff4
 * modify it under the terms of the GNU General Public License as
e4aaff4
 * published by the Free Software Foundation; either version 2 of the
e4aaff4
 * License, or (at your option) any later version.
e4aaff4
 *
e4aaff4
 * This program is distributed in the hope that it will be useful, but
e4aaff4
 * WITHOUT ANY WARRANTY; without even the implied warranty of
e4aaff4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e4aaff4
 * General Public License for more details.
e4aaff4
 *
e4aaff4
 * You should have received a copy of the GNU General Public License
e4aaff4
 * along with this program; if not, write to the Free Software
e4aaff4
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
e4aaff4
 * 02111-1301, USA.
e4aaff4
 */
e4aaff4
#include "internal.h"
e4aaff4
#include "debug.h"
e4aaff4
#include "plugin.h"
e4aaff4
#include "version.h"
e4aaff4
#include "account.h"
e4aaff4
#include "accountopt.h"
e4aaff4
e4aaff4
#define PLUGIN_ID "core-one_time_password"
e4aaff4
#define PREF_NAME PLUGIN_ID "_enabled"
e4aaff4
e4aaff4
static void
e4aaff4
signed_on_cb(PurpleConnection *conn, void *data)
e4aaff4
{
e4aaff4
	PurpleAccount *account = purple_connection_get_account(conn);
e4aaff4
e4aaff4
	if (purple_account_get_bool(account, PREF_NAME, FALSE)) {
e4aaff4
		if(purple_account_get_remember_password(account))
e4aaff4
			purple_debug_error("One Time Password",
e4aaff4
					   "Unable to enforce one time password for account %s (%s).\n"
e4aaff4
					   "Account is set to remember the password.\n",
e4aaff4
					   purple_account_get_username(account),
e4aaff4
					   purple_account_get_protocol_name(account));
e4aaff4
		else {
e4aaff4
e4aaff4
			purple_debug_info("One Time Password", "Clearing password for account %s (%s).\n",
e4aaff4
					  purple_account_get_username(account),
e4aaff4
					  purple_account_get_protocol_name(account));
e4aaff4
e4aaff4
			purple_account_set_password(account, NULL);
e4aaff4
			/* TODO: Do we need to somehow clear conn->password ? */
e4aaff4
		}
e4aaff4
	}
e4aaff4
}
e4aaff4
e4aaff4
static gboolean
e4aaff4
plugin_load(PurplePlugin *plugin)
e4aaff4
{
e4aaff4
	PurplePlugin *prpl;
e4aaff4
	PurplePluginProtocolInfo *prpl_info;
e4aaff4
	PurpleAccountOption *option;
e4aaff4
	GList *l;
e4aaff4
e4aaff4
	/* Register protocol preference. */
e4aaff4
	for (l = purple_plugins_get_protocols(); l != NULL; l = l->next) {
e4aaff4
		prpl = (PurplePlugin *)l->data;
e4aaff4
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
e4aaff4
		if (prpl_info != NULL && !(prpl_info->options & OPT_PROTO_NO_PASSWORD)) {
e4aaff4
			option = purple_account_option_bool_new(_("One Time Password"),
e4aaff4
								PREF_NAME, FALSE);
e4aaff4
			prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, option);
e4aaff4
		}
e4aaff4
	}
e4aaff4
e4aaff4
	/* Register callback. */
e4aaff4
	purple_signal_connect(purple_connections_get_handle(), "signed-on",
e4aaff4
			      plugin, PURPLE_CALLBACK(signed_on_cb), NULL);
e4aaff4
e4aaff4
	return TRUE;
e4aaff4
}
e4aaff4
e4aaff4
static gboolean
e4aaff4
plugin_unload(PurplePlugin *plugin)
e4aaff4
{
e4aaff4
	PurplePlugin *prpl;
e4aaff4
	PurplePluginProtocolInfo *prpl_info;
e4aaff4
	PurpleAccountOption *option;
e4aaff4
	GList *l, *options;
e4aaff4
e4aaff4
	/* Remove protocol preference. */
e4aaff4
	for (l = purple_plugins_get_protocols(); l != NULL; l = l->next) {
e4aaff4
		prpl = (PurplePlugin *)l->data;
e4aaff4
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
e4aaff4
		if (prpl_info != NULL && !(prpl_info->options & OPT_PROTO_NO_PASSWORD)) {
e4aaff4
			options = prpl_info->protocol_options;
e4aaff4
			while (options != NULL) {
e4aaff4
				option = (PurpleAccountOption *) options->data;
e4aaff4
				if (strcmp(PREF_NAME, purple_account_option_get_setting(option)) == 0) {
e4aaff4
					prpl_info->protocol_options = g_list_delete_link(prpl_info->protocol_options, options);
e4aaff4
					purple_account_option_destroy(option);
e4aaff4
					break;
e4aaff4
				}
e4aaff4
				options = options->next;
e4aaff4
			}
e4aaff4
		}
e4aaff4
	}
e4aaff4
e4aaff4
	/* Callback will be automagically unregistered */
e4aaff4
e4aaff4
	return TRUE;
e4aaff4
}
e4aaff4
e4aaff4
static PurplePluginInfo info =
e4aaff4
{
e4aaff4
	PURPLE_PLUGIN_MAGIC,
e4aaff4
	PURPLE_MAJOR_VERSION,
e4aaff4
	PURPLE_MINOR_VERSION,
e4aaff4
	PURPLE_PLUGIN_STANDARD,				/**< type           */
e4aaff4
	NULL,						/**< ui_requirement */
e4aaff4
	0,						/**< flags          */
e4aaff4
	NULL,						/**< dependencies   */
e4aaff4
	PURPLE_PRIORITY_DEFAULT,			/**< priority       */
e4aaff4
	PLUGIN_ID,					/**< id             */
e4aaff4
	N_("One Time Password Support"),		/**< name           */
e4aaff4
	DISPLAY_VERSION,				/**< version        */
e4aaff4
							/**  summary        */
e4aaff4
	N_("Enforce that passwords are used only once."),
e4aaff4
							/**  description    */
e4aaff4
	N_("Allows you to enforce on a per-account basis that passwords not "
e4aaff4
	   "being saved are only used in a single successful connection.\n"
e4aaff4
	   "Note: The account password must not be saved for this to work."),
e4aaff4
	"Daniel Atallah <datallah@pidgin.im>",		/**< author         */
e4aaff4
	PURPLE_WEBSITE,					/**< homepage       */
e4aaff4
	plugin_load,					/**< load           */
e4aaff4
	plugin_unload,					/**< unload         */
e4aaff4
	NULL,						/**< destroy        */
e4aaff4
	NULL,						/**< ui_info        */
e4aaff4
	NULL,						/**< extra_info     */
e4aaff4
	NULL,						/**< prefs_info     */
e4aaff4
	NULL,						/**< actions        */
e4aaff4
	NULL,						/**< reserved 1     */
e4aaff4
	NULL,						/**< reserved 2     */
e4aaff4
	NULL,						/**< reserved 3     */
e4aaff4
	NULL						/**< reserved 4     */
e4aaff4
};
e4aaff4
e4aaff4
static void
e4aaff4
init_plugin(PurplePlugin *plugin)
e4aaff4
{
e4aaff4
}
e4aaff4
e4aaff4
PURPLE_INIT_PLUGIN(one_time_password, init_plugin, info)