Adapt migration for files
[pidgin-git.git] / libpurple / plugins / one_time_password.c
blob6cffafc13028cd6ecbcfc4dbc602d63d8955eeda
1 /*
2 * One Time Password support plugin for libpurple
4 * Copyright (C) 2009, Daniel Atallah <datallah@pidgin.im>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02111-1301, USA.
21 #include "internal.h"
22 #include "debug.h"
23 #include "plugins.h"
24 #include "version.h"
25 #include "account.h"
26 #include "accountopt.h"
28 #define PLUGIN_ID "core-one_time_password"
29 #define PREF_NAME PLUGIN_ID "_enabled"
31 static void
32 signed_on_cb(PurpleConnection *conn, void *data)
34 PurpleAccount *account = purple_connection_get_account(conn);
36 if (purple_account_get_bool(account, PREF_NAME, FALSE)) {
37 if(purple_account_get_remember_password(account))
38 purple_debug_error("One Time Password",
39 "Unable to enforce one time password for account %s (%s).\n"
40 "Account is set to remember the password.\n",
41 purple_account_get_username(account),
42 purple_account_get_protocol_name(account));
43 else {
45 purple_debug_info("One Time Password", "Clearing password for account %s (%s).\n",
46 purple_account_get_username(account),
47 purple_account_get_protocol_name(account));
49 purple_account_set_password(account, NULL, NULL, NULL);
50 /* TODO: Do we need to somehow clear conn->password ? */
55 static PurplePluginInfo *
56 plugin_query(GError **error)
58 const gchar * const authors[] = {
59 "Daniel Atallah <datallah@pidgin.im>",
60 NULL
63 return purple_plugin_info_new(
64 "id", PLUGIN_ID,
65 "name", N_("One Time Password Support"),
66 "version", DISPLAY_VERSION,
67 "category", N_("Security"),
68 "summary", N_("Enforce that passwords are used only once."),
69 "description", N_("Allows you to enforce on a per-account basis that "
70 "passwords not being saved are only used in a "
71 "single successful connection.\n"
72 "Note: The account password must not be saved for "
73 "this to work."),
74 "authors", authors,
75 "website", PURPLE_WEBSITE,
76 "abi-version", PURPLE_ABI_VERSION,
77 NULL
81 static gboolean
82 plugin_load(PurplePlugin *plugin, GError **error)
84 PurpleProtocol *protocol;
85 PurpleAccountOption *option;
86 GList *list, *l;
88 list = purple_protocols_get_all();
90 /* Register protocol preference. */
91 for (l = list; l != NULL; l = l->next) {
92 protocol = PURPLE_PROTOCOL(l->data);
93 if (protocol != NULL && !(purple_protocol_get_options(protocol) & OPT_PROTO_NO_PASSWORD)) {
94 option = purple_account_option_bool_new(_("One Time Password"),
95 PREF_NAME, FALSE);
96 protocol->account_options = g_list_append(protocol->account_options, option);
99 g_list_free(list);
101 /* Register callback. */
102 purple_signal_connect(purple_connections_get_handle(), "signed-on",
103 plugin, PURPLE_CALLBACK(signed_on_cb), NULL);
105 return TRUE;
108 static gboolean
109 plugin_unload(PurplePlugin *plugin, GError **error)
111 PurpleProtocol *protocol;
112 PurpleAccountOption *option;
113 GList *list, *l, *options;
115 list = purple_protocols_get_all();
117 /* Remove protocol preference. */
118 for (l = list; l != NULL; l = l->next) {
119 protocol = PURPLE_PROTOCOL(l->data);
120 if (protocol != NULL && !(purple_protocol_get_options(protocol) & OPT_PROTO_NO_PASSWORD)) {
121 options = purple_protocol_get_account_options(protocol);
122 while (options != NULL) {
123 option = (PurpleAccountOption *) options->data;
124 if (strcmp(PREF_NAME, purple_account_option_get_setting(option)) == 0) {
125 protocol->account_options = g_list_delete_link(protocol->account_options, options);
126 purple_account_option_destroy(option);
127 break;
129 options = options->next;
133 g_list_free(list);
135 /* Callback will be automagically unregistered */
137 return TRUE;
140 PURPLE_PLUGIN_INIT(one_time_password, plugin_query, plugin_load, plugin_unload);