Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / account.c
blob6969066acee709acb4cfc0e70e3ecda471e46498
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "glibcompat.h"
24 #include "accounts.h"
25 #include "core.h"
26 #include "debug.h"
27 #include "network.h"
28 #include "notify.h"
29 #include "pounce.h"
30 #include "prefs.h"
31 #include "request.h"
32 #include "server.h"
33 #include "signals.h"
34 #include "util.h"
36 /**
37 * PurpleAccount:
39 * Structure representing an account.
41 struct _PurpleAccount
43 GObject gparent;
46 typedef struct
48 char *username; /* The username. */
49 char *alias; /* How you appear to yourself. */
50 char *password; /* The account password. */
51 char *user_info; /* User information. */
53 char *buddy_icon_path; /* The buddy icon's non-cached path. */
55 gboolean remember_pass; /* Remember the password. */
58 * TODO: After a GObject representing a protocol is ready, use it
59 * here instead of the protocol ID.
61 char *protocol_id; /* The ID of the protocol. */
63 PurpleConnection *gc; /* The connection handle. */
64 gboolean disconnecting; /* The account is currently disconnecting */
66 GHashTable *settings; /* Protocol-specific settings. */
67 GHashTable *ui_settings; /* UI-specific settings. */
69 PurpleProxyInfo *proxy_info; /* Proxy information. This will be set */
70 /* to NULL when the account inherits */
71 /* proxy settings from global prefs. */
74 * TODO: Instead of linked lists for permit and deny, use a data
75 * structure that allows fast lookups AND decent performance when
76 * iterating through all items. Fast lookups should help performance
77 * for protocols like MSN, where all your buddies exist in your permit
78 * list therefore the permit list is large. Possibly GTree or
79 * GHashTable.
81 GSList *permit; /* Permit list. */
82 GSList *deny; /* Deny list. */
83 PurpleAccountPrivacyType privacy_type; /* The permit/deny setting. */
85 GList *status_types; /* Status types. */
87 PurplePresence *presence; /* Presence. */
88 PurpleLog *system_log; /* The system log */
90 PurpleAccountRegistrationCb registration_cb;
91 void *registration_cb_user_data;
93 PurpleConnectionErrorInfo *current_error; /* Errors */
94 } PurpleAccountPrivate;
96 typedef struct
98 char *ui;
99 GValue value;
101 } PurpleAccountSetting;
103 typedef struct
105 PurpleAccountRequestType type;
106 PurpleAccount *account;
107 void *ui_handle;
108 char *user;
109 gpointer userdata;
110 PurpleAccountRequestAuthorizationCb auth_cb;
111 PurpleAccountRequestAuthorizationCb deny_cb;
112 guint ref;
113 } PurpleAccountRequestInfo;
115 typedef struct
117 PurpleCallback cb;
118 gpointer data;
119 } PurpleCallbackBundle;
121 /* GObject Property enums */
122 enum
124 PROP_0,
125 PROP_USERNAME,
126 PROP_PRIVATE_ALIAS,
127 PROP_ENABLED,
128 PROP_CONNECTION,
129 PROP_PROTOCOL_ID,
130 PROP_USER_INFO,
131 PROP_BUDDY_ICON_PATH,
132 PROP_REMEMBER_PASSWORD,
133 PROP_CHECK_MAIL,
134 PROP_LAST
137 static GParamSpec *properties[PROP_LAST];
138 static GList *handles = NULL;
140 G_DEFINE_TYPE_WITH_PRIVATE(PurpleAccount, purple_account, G_TYPE_OBJECT);
142 /******************************************************************************
143 * Helpers
144 *****************************************************************************/
145 static void
146 purple_account_register_got_password_cb(PurpleAccount *account,
147 const gchar *password, GError *error, gpointer data)
149 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
151 _purple_connection_new(account, TRUE, password);
154 void
155 purple_account_register(PurpleAccount *account)
157 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
159 purple_debug_info("account", "Registering account %s\n",
160 purple_account_get_username(account));
162 purple_keyring_get_password(account,
163 purple_account_register_got_password_cb, NULL);
166 static void
167 purple_account_unregister_got_password_cb(PurpleAccount *account,
168 const gchar *password, GError *error, gpointer data)
170 PurpleCallbackBundle *cbb = data;
171 PurpleAccountUnregistrationCb cb;
173 cb = (PurpleAccountUnregistrationCb)cbb->cb;
174 _purple_connection_new_unregister(account, password, cb, cbb->data);
176 g_free(cbb);
179 struct register_completed_closure
181 PurpleAccount *account;
182 gboolean succeeded;
185 static gboolean
186 purple_account_register_completed_cb(gpointer data)
188 struct register_completed_closure *closure = data;
189 PurpleAccountPrivate *priv;
191 priv = purple_account_get_instance_private(closure->account);
193 if (priv->registration_cb)
194 (priv->registration_cb)(closure->account, closure->succeeded,
195 priv->registration_cb_user_data);
197 g_object_unref(closure->account);
198 g_free(closure);
200 return FALSE;
203 static void
204 request_password_ok_cb(PurpleAccount *account, PurpleRequestFields *fields)
206 const char *entry;
207 gboolean remember;
209 entry = purple_request_fields_get_string(fields, "password");
210 remember = purple_request_fields_get_bool(fields, "remember");
212 if (!entry || !*entry)
214 purple_notify_error(account, NULL,
215 _("Password is required to sign on."), NULL,
216 purple_request_cpar_from_account(account));
217 return;
220 purple_account_set_remember_password(account, remember);
222 purple_account_set_password(account, entry, NULL, NULL);
223 _purple_connection_new(account, FALSE, entry);
226 static void
227 request_password_cancel_cb(PurpleAccount *account, PurpleRequestFields *fields)
229 /* Disable the account as the user has cancelled connecting */
230 purple_account_set_enabled(account, purple_core_get_ui(), FALSE);
234 static void
235 purple_account_connect_got_password_cb(PurpleAccount *account,
236 const gchar *password, GError *error, gpointer data)
238 PurpleProtocol *protocol = data;
240 if ((password == NULL || *password == '\0') &&
241 !(purple_protocol_get_options(protocol) & OPT_PROTO_NO_PASSWORD) &&
242 !(purple_protocol_get_options(protocol) & OPT_PROTO_PASSWORD_OPTIONAL))
243 purple_account_request_password(account,
244 G_CALLBACK(request_password_ok_cb),
245 G_CALLBACK(request_password_cancel_cb), account);
246 else
247 _purple_connection_new(account, FALSE, password);
250 static PurpleAccountRequestInfo *
251 purple_account_request_info_unref(PurpleAccountRequestInfo *info)
253 if (--info->ref)
254 return info;
256 /* TODO: This will leak info->user_data, but there is no callback to just clean that up */
257 g_free(info->user);
258 g_free(info);
259 return NULL;
262 static void
263 purple_account_request_close_info(PurpleAccountRequestInfo *info)
265 PurpleAccountUiOps *ops;
267 ops = purple_accounts_get_ui_ops();
269 if (ops != NULL && ops->close_account_request != NULL)
270 ops->close_account_request(info->ui_handle);
272 purple_account_request_info_unref(info);
275 static void
276 request_auth_cb(const char *message, void *data)
278 PurpleAccountRequestInfo *info = data;
280 handles = g_list_remove(handles, info);
282 if (info->auth_cb != NULL)
283 info->auth_cb(message, info->userdata);
285 purple_signal_emit(purple_accounts_get_handle(),
286 "account-authorization-granted", info->account, info->user, message);
288 purple_account_request_info_unref(info);
291 static void
292 request_deny_cb(const char *message, void *data)
294 PurpleAccountRequestInfo *info = data;
296 handles = g_list_remove(handles, info);
298 if (info->deny_cb != NULL)
299 info->deny_cb(message, info->userdata);
301 purple_signal_emit(purple_accounts_get_handle(),
302 "account-authorization-denied", info->account, info->user, message);
304 purple_account_request_info_unref(info);
307 static void
308 change_password_cb(PurpleAccount *account, PurpleRequestFields *fields)
310 const char *orig_pass, *new_pass_1, *new_pass_2;
312 orig_pass = purple_request_fields_get_string(fields, "password");
313 new_pass_1 = purple_request_fields_get_string(fields, "new_password_1");
314 new_pass_2 = purple_request_fields_get_string(fields, "new_password_2");
316 if (g_utf8_collate(new_pass_1, new_pass_2))
318 purple_notify_error(account, NULL,
319 _("New passwords do not match."), NULL,
320 purple_request_cpar_from_account(account));
322 return;
325 if ((purple_request_fields_is_field_required(fields, "password") &&
326 (orig_pass == NULL || *orig_pass == '\0')) ||
327 (purple_request_fields_is_field_required(fields, "new_password_1") &&
328 (new_pass_1 == NULL || *new_pass_1 == '\0')) ||
329 (purple_request_fields_is_field_required(fields, "new_password_2") &&
330 (new_pass_2 == NULL || *new_pass_2 == '\0')))
332 purple_notify_error(account, NULL,
333 _("Fill out all fields completely."), NULL,
334 purple_request_cpar_from_account(account));
335 return;
338 purple_account_change_password(account, orig_pass, new_pass_1);
341 static void
342 set_user_info_cb(PurpleAccount *account, const char *user_info)
344 PurpleConnection *gc;
346 purple_account_set_user_info(account, user_info);
347 gc = purple_account_get_connection(account);
348 purple_serv_set_info(gc, user_info);
351 struct public_alias_closure
353 PurpleAccount *account;
354 gpointer failure_cb;
357 static gboolean
358 set_public_alias_unsupported(gpointer data)
360 struct public_alias_closure *closure = data;
361 PurpleSetPublicAliasFailureCallback failure_cb = closure->failure_cb;
363 failure_cb(closure->account,
364 _("This protocol does not support setting a public alias."));
366 g_object_unref(closure->account);
367 g_free(closure);
369 return FALSE;
372 static gboolean
373 get_public_alias_unsupported(gpointer data)
375 struct public_alias_closure *closure = data;
376 PurpleGetPublicAliasFailureCallback failure_cb = closure->failure_cb;
378 failure_cb(closure->account,
379 _("This protocol does not support fetching the public alias."));
381 g_object_unref(closure->account);
382 g_free(closure);
384 return FALSE;
387 static void
388 delete_setting(void *data)
390 PurpleAccountSetting *setting = (PurpleAccountSetting *)data;
392 g_free(setting->ui);
393 g_value_unset(&setting->value);
395 g_free(setting);
398 static GHashTable *
399 get_ui_settings_table(PurpleAccount *account, const char *ui)
401 GHashTable *table;
402 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
404 table = g_hash_table_lookup(priv->ui_settings, ui);
406 if (table == NULL) {
407 table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
408 delete_setting);
409 g_hash_table_insert(priv->ui_settings, g_strdup(ui), table);
412 return table;
415 static PurpleConnectionState
416 purple_account_get_state(PurpleAccount *account)
418 PurpleConnection *gc;
420 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), PURPLE_CONNECTION_DISCONNECTED);
422 gc = purple_account_get_connection(account);
423 if (!gc)
424 return PURPLE_CONNECTION_DISCONNECTED;
426 return purple_connection_get_state(gc);
429 static void
430 purple_account_get_password_got(PurpleAccount *account,
431 const gchar *password, GError *error, gpointer data)
433 PurpleCallbackBundle *cbb = data;
434 PurpleKeyringReadCallback cb;
435 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
437 purple_debug_info("account",
438 "Read password for account %s from async keyring.\n",
439 purple_account_get_username(account));
441 purple_str_wipe(priv->password);
442 priv->password = g_strdup(password);
444 cb = (PurpleKeyringReadCallback)cbb->cb;
445 if (cb != NULL)
446 cb(account, password, error, cbb->data);
448 g_free(cbb);
452 * This makes sure your permit list contains all buddies from your
453 * buddy list and ONLY buddies from your buddy list.
455 static void
456 add_all_buddies_to_permit_list(PurpleAccount *account, gboolean local)
458 GSList *list;
459 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
461 /* Remove anyone in the permit list who is not in the buddylist */
462 for (list = priv->permit; list != NULL; ) {
463 char *person = list->data;
464 list = list->next;
465 if (!purple_blist_find_buddy(account, person))
466 purple_account_privacy_permit_remove(account, person, local);
469 /* Now make sure everyone in the buddylist is in the permit list */
470 list = purple_blist_find_buddies(account, NULL);
471 while (list != NULL)
473 PurpleBuddy *buddy = list->data;
474 const gchar *name = purple_buddy_get_name(buddy);
476 if (!g_slist_find_custom(priv->permit, name, (GCompareFunc)g_utf8_collate))
477 purple_account_privacy_permit_add(account, name, local);
478 list = g_slist_delete_link(list, list);
482 void
483 _purple_account_set_current_error(PurpleAccount *account,
484 PurpleConnectionErrorInfo *new_err)
486 PurpleConnectionErrorInfo *old_err;
487 PurpleAccountPrivate *priv;
489 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
490 priv = purple_account_get_instance_private(account);
492 old_err = priv->current_error;
494 if(new_err == old_err)
495 return;
497 priv->current_error = new_err;
499 purple_signal_emit(purple_accounts_get_handle(),
500 "account-error-changed",
501 account, old_err, new_err);
502 purple_accounts_schedule_save();
504 if(old_err)
505 g_free(old_err->description);
507 g_free(old_err);
510 /******************************************************************************
511 * XmlNode Helpers
512 *****************************************************************************/
513 static PurpleXmlNode *
514 status_attribute_to_xmlnode(PurpleStatus *status, PurpleStatusType *type,
515 PurpleStatusAttribute *attr)
517 PurpleXmlNode *node;
518 const char *id;
519 char *value = NULL;
520 PurpleStatusAttribute *default_attr;
521 GValue *default_value;
522 GType attr_type;
523 GValue *attr_value;
525 id = purple_status_attribute_get_id(attr);
526 g_return_val_if_fail(id, NULL);
528 attr_value = purple_status_get_attr_value(status, id);
529 g_return_val_if_fail(attr_value, NULL);
530 attr_type = G_VALUE_TYPE(attr_value);
533 * If attr_value is a different type than it should be
534 * then don't write it to the file.
536 default_attr = purple_status_type_get_attr(type, id);
537 default_value = purple_status_attribute_get_value(default_attr);
538 if (attr_type != G_VALUE_TYPE(default_value))
539 return NULL;
542 * If attr_value is the same as the default for this status
543 * then there is no need to write it to the file.
545 if (attr_type == G_TYPE_STRING)
547 const char *string_value = g_value_get_string(attr_value);
548 const char *default_string_value = g_value_get_string(default_value);
549 if (purple_strequal(string_value, default_string_value))
550 return NULL;
551 value = g_value_dup_string(attr_value);
553 else if (attr_type == G_TYPE_INT)
555 int int_value = g_value_get_int(attr_value);
556 if (int_value == g_value_get_int(default_value))
557 return NULL;
558 value = g_strdup_printf("%d", int_value);
560 else if (attr_type == G_TYPE_BOOLEAN)
562 gboolean boolean_value = g_value_get_boolean(attr_value);
563 if (boolean_value == g_value_get_boolean(default_value))
564 return NULL;
565 value = g_strdup(boolean_value ?
566 "true" : "false");
568 else
570 return NULL;
573 g_return_val_if_fail(value, NULL);
575 node = purple_xmlnode_new("attribute");
577 purple_xmlnode_set_attrib(node, "id", id);
578 purple_xmlnode_set_attrib(node, "value", value);
580 g_free(value);
582 return node;
585 static PurpleXmlNode *
586 status_attrs_to_xmlnode(PurpleStatus *status)
588 PurpleStatusType *type = purple_status_get_status_type(status);
589 PurpleXmlNode *node, *child;
590 GList *attrs, *attr;
592 node = purple_xmlnode_new("attributes");
594 attrs = purple_status_type_get_attrs(type);
595 for (attr = attrs; attr != NULL; attr = attr->next)
597 child = status_attribute_to_xmlnode(status, type, (PurpleStatusAttribute *)attr->data);
598 if (child)
599 purple_xmlnode_insert_child(node, child);
602 return node;
605 static PurpleXmlNode *
606 status_to_xmlnode(PurpleStatus *status)
608 PurpleXmlNode *node, *child;
610 node = purple_xmlnode_new("status");
611 purple_xmlnode_set_attrib(node, "type", purple_status_get_id(status));
612 if (purple_status_get_name(status) != NULL)
613 purple_xmlnode_set_attrib(node, "name", purple_status_get_name(status));
614 purple_xmlnode_set_attrib(node, "active", purple_status_is_active(status) ? "true" : "false");
616 child = status_attrs_to_xmlnode(status);
617 purple_xmlnode_insert_child(node, child);
619 return node;
622 static PurpleXmlNode *
623 statuses_to_xmlnode(PurplePresence *presence)
625 PurpleXmlNode *node, *child;
626 GList *statuses;
627 PurpleStatus *status;
629 node = purple_xmlnode_new("statuses");
631 statuses = purple_presence_get_statuses(presence);
632 for (; statuses != NULL; statuses = statuses->next)
634 status = statuses->data;
635 if (purple_status_type_is_saveable(purple_status_get_status_type(status)))
637 child = status_to_xmlnode(status);
638 purple_xmlnode_insert_child(node, child);
642 return node;
645 static PurpleXmlNode *
646 proxy_settings_to_xmlnode(const PurpleProxyInfo *proxy_info)
648 PurpleXmlNode *node, *child;
649 PurpleProxyType proxy_type;
650 const char *value;
651 int int_value;
652 char buf[21];
654 proxy_type = purple_proxy_info_get_proxy_type(proxy_info);
656 node = purple_xmlnode_new("proxy");
658 child = purple_xmlnode_new_child(node, "type");
659 purple_xmlnode_insert_data(child,
660 (proxy_type == PURPLE_PROXY_USE_GLOBAL ? "global" :
661 proxy_type == PURPLE_PROXY_NONE ? "none" :
662 proxy_type == PURPLE_PROXY_HTTP ? "http" :
663 proxy_type == PURPLE_PROXY_SOCKS4 ? "socks4" :
664 proxy_type == PURPLE_PROXY_SOCKS5 ? "socks5" :
665 proxy_type == PURPLE_PROXY_TOR ? "tor" :
666 proxy_type == PURPLE_PROXY_USE_ENVVAR ? "envvar" : "unknown"), -1);
668 if ((value = purple_proxy_info_get_host(proxy_info)) != NULL)
670 child = purple_xmlnode_new_child(node, "host");
671 purple_xmlnode_insert_data(child, value, -1);
674 if ((int_value = purple_proxy_info_get_port(proxy_info)) != 0)
676 g_snprintf(buf, sizeof(buf), "%d", int_value);
677 child = purple_xmlnode_new_child(node, "port");
678 purple_xmlnode_insert_data(child, buf, -1);
681 if ((value = purple_proxy_info_get_username(proxy_info)) != NULL)
683 child = purple_xmlnode_new_child(node, "username");
684 purple_xmlnode_insert_data(child, value, -1);
687 if ((value = purple_proxy_info_get_password(proxy_info)) != NULL)
689 child = purple_xmlnode_new_child(node, "password");
690 purple_xmlnode_insert_data(child, value, -1);
693 return node;
696 static PurpleXmlNode *
697 current_error_to_xmlnode(PurpleConnectionErrorInfo *err)
699 PurpleXmlNode *node, *child;
700 char type_str[3];
702 node = purple_xmlnode_new("current_error");
704 if(err == NULL)
705 return node;
707 /* It doesn't make sense to have transient errors persist across a
708 * restart.
710 if(!purple_connection_error_is_fatal (err->type))
711 return node;
713 child = purple_xmlnode_new_child(node, "type");
714 g_snprintf(type_str, sizeof(type_str), "%u", err->type);
715 purple_xmlnode_insert_data(child, type_str, -1);
717 child = purple_xmlnode_new_child(node, "description");
718 if(err->description) {
719 char *utf8ized = purple_utf8_try_convert(err->description);
720 if(utf8ized == NULL)
721 utf8ized = purple_utf8_salvage(err->description);
722 purple_xmlnode_insert_data(child, utf8ized, -1);
723 g_free(utf8ized);
726 return node;
729 static void
730 setting_to_xmlnode(gpointer key, gpointer value, gpointer user_data)
732 const char *name;
733 PurpleAccountSetting *setting;
734 PurpleXmlNode *node, *child;
735 char buf[21];
737 name = (const char *)key;
738 setting = (PurpleAccountSetting *)value;
739 node = (PurpleXmlNode *)user_data;
741 child = purple_xmlnode_new_child(node, "setting");
742 purple_xmlnode_set_attrib(child, "name", name);
744 if (G_VALUE_HOLDS_INT(&setting->value)) {
745 purple_xmlnode_set_attrib(child, "type", "int");
746 g_snprintf(buf, sizeof(buf), "%d", g_value_get_int(&setting->value));
747 purple_xmlnode_insert_data(child, buf, -1);
749 else if (G_VALUE_HOLDS_STRING(&setting->value) && g_value_get_string(&setting->value) != NULL) {
750 purple_xmlnode_set_attrib(child, "type", "string");
751 purple_xmlnode_insert_data(child, g_value_get_string(&setting->value), -1);
753 else if (G_VALUE_HOLDS_BOOLEAN(&setting->value)) {
754 purple_xmlnode_set_attrib(child, "type", "bool");
755 g_snprintf(buf, sizeof(buf), "%d", g_value_get_boolean(&setting->value));
756 purple_xmlnode_insert_data(child, buf, -1);
760 static void
761 ui_setting_to_xmlnode(gpointer key, gpointer value, gpointer user_data)
763 const char *ui;
764 GHashTable *table;
765 PurpleXmlNode *node, *child;
767 ui = (const char *)key;
768 table = (GHashTable *)value;
769 node = (PurpleXmlNode *)user_data;
771 if (g_hash_table_size(table) > 0)
773 child = purple_xmlnode_new_child(node, "settings");
774 purple_xmlnode_set_attrib(child, "ui", ui);
775 g_hash_table_foreach(table, setting_to_xmlnode, child);
779 PurpleXmlNode *
780 _purple_account_to_xmlnode(PurpleAccount *account)
782 PurpleXmlNode *node, *child;
783 const char *tmp;
784 PurplePresence *presence;
785 const PurpleProxyInfo *proxy_info;
786 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
788 node = purple_xmlnode_new("account");
790 child = purple_xmlnode_new_child(node, "protocol");
791 purple_xmlnode_insert_data(child, purple_account_get_protocol_id(account), -1);
793 child = purple_xmlnode_new_child(node, "name");
794 purple_xmlnode_insert_data(child, purple_account_get_username(account), -1);
796 if (purple_account_get_remember_password(account))
798 const char *keyring_id = NULL;
799 const char *mode = NULL;
800 char *data = NULL;
801 GError *error = NULL;
802 GDestroyNotify destroy = NULL;
803 gboolean exported = purple_keyring_export_password(account,
804 &keyring_id, &mode, &data, &error, &destroy);
806 if (error != NULL) {
807 purple_debug_error("account",
808 "Failed to export password for account %s: %s.\n",
809 purple_account_get_username(account),
810 error->message);
811 } else if (exported) {
812 child = purple_xmlnode_new_child(node, "password");
813 if (keyring_id != NULL)
814 purple_xmlnode_set_attrib(child, "keyring_id", keyring_id);
815 if (mode != NULL)
816 purple_xmlnode_set_attrib(child, "mode", mode);
817 if (data != NULL)
818 purple_xmlnode_insert_data(child, data, -1);
820 if (destroy != NULL)
821 destroy(data);
825 if ((tmp = purple_account_get_private_alias(account)) != NULL)
827 child = purple_xmlnode_new_child(node, "alias");
828 purple_xmlnode_insert_data(child, tmp, -1);
831 if ((presence = purple_account_get_presence(account)) != NULL)
833 child = statuses_to_xmlnode(presence);
834 purple_xmlnode_insert_child(node, child);
837 if ((tmp = purple_account_get_user_info(account)) != NULL)
839 /* TODO: Do we need to call purple_str_strip_char(tmp, '\r') here? */
840 child = purple_xmlnode_new_child(node, "user-info");
841 purple_xmlnode_insert_data(child, tmp, -1);
844 if (g_hash_table_size(priv->settings) > 0)
846 child = purple_xmlnode_new_child(node, "settings");
847 g_hash_table_foreach(priv->settings, setting_to_xmlnode, child);
850 if (g_hash_table_size(priv->ui_settings) > 0)
852 g_hash_table_foreach(priv->ui_settings, ui_setting_to_xmlnode, node);
855 if ((proxy_info = purple_account_get_proxy_info(account)) != NULL)
857 child = proxy_settings_to_xmlnode(proxy_info);
858 purple_xmlnode_insert_child(node, child);
861 child = current_error_to_xmlnode(priv->current_error);
862 purple_xmlnode_insert_child(node, child);
864 return node;
867 /******************************************************************************
868 * GObject Implementation
869 *****************************************************************************/
870 static void
871 purple_account_set_property(GObject *obj, guint param_id, const GValue *value,
872 GParamSpec *pspec)
874 PurpleAccount *account = PURPLE_ACCOUNT(obj);
876 switch (param_id) {
877 case PROP_USERNAME:
878 purple_account_set_username(account, g_value_get_string(value));
879 break;
880 case PROP_PRIVATE_ALIAS:
881 purple_account_set_private_alias(account, g_value_get_string(value));
882 break;
883 case PROP_ENABLED:
884 purple_account_set_enabled(account, purple_core_get_ui(),
885 g_value_get_boolean(value));
886 break;
887 case PROP_CONNECTION:
888 purple_account_set_connection(account, g_value_get_object(value));
889 break;
890 case PROP_PROTOCOL_ID:
891 purple_account_set_protocol_id(account, g_value_get_string(value));
892 break;
893 case PROP_USER_INFO:
894 purple_account_set_user_info(account, g_value_get_string(value));
895 break;
896 case PROP_BUDDY_ICON_PATH:
897 purple_account_set_buddy_icon_path(account,
898 g_value_get_string(value));
899 break;
900 case PROP_REMEMBER_PASSWORD:
901 purple_account_set_remember_password(account,
902 g_value_get_boolean(value));
903 break;
904 case PROP_CHECK_MAIL:
905 purple_account_set_check_mail(account, g_value_get_boolean(value));
906 break;
907 default:
908 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
909 break;
913 static void
914 purple_account_get_property(GObject *obj, guint param_id, GValue *value,
915 GParamSpec *pspec)
917 PurpleAccount *account = PURPLE_ACCOUNT(obj);
919 switch (param_id) {
920 case PROP_USERNAME:
921 g_value_set_string(value, purple_account_get_username(account));
922 break;
923 case PROP_PRIVATE_ALIAS:
924 g_value_set_string(value, purple_account_get_private_alias(account));
925 break;
926 case PROP_ENABLED:
927 g_value_set_boolean(value, purple_account_get_enabled(account,
928 purple_core_get_ui()));
929 break;
930 case PROP_CONNECTION:
931 g_value_set_object(value, purple_account_get_connection(account));
932 break;
933 case PROP_PROTOCOL_ID:
934 g_value_set_string(value, purple_account_get_protocol_id(account));
935 break;
936 case PROP_USER_INFO:
937 g_value_set_string(value, purple_account_get_user_info(account));
938 break;
939 case PROP_BUDDY_ICON_PATH:
940 g_value_set_string(value,
941 purple_account_get_buddy_icon_path(account));
942 break;
943 case PROP_REMEMBER_PASSWORD:
944 g_value_set_boolean(value,
945 purple_account_get_remember_password(account));
946 break;
947 case PROP_CHECK_MAIL:
948 g_value_set_boolean(value, purple_account_get_check_mail(account));
949 break;
950 default:
951 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
952 break;
956 static void
957 purple_account_init(PurpleAccount *account)
959 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
961 priv->settings = g_hash_table_new_full(g_str_hash, g_str_equal,
962 g_free, delete_setting);
963 priv->ui_settings = g_hash_table_new_full(g_str_hash, g_str_equal,
964 g_free, (GDestroyNotify)g_hash_table_destroy);
965 priv->system_log = NULL;
967 priv->privacy_type = PURPLE_ACCOUNT_PRIVACY_ALLOW_ALL;
970 static void
971 purple_account_constructed(GObject *object)
973 PurpleAccount *account = PURPLE_ACCOUNT(object);
974 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
975 gchar *username, *protocol_id;
976 PurpleProtocol *protocol = NULL;
977 PurpleStatusType *status_type;
979 G_OBJECT_CLASS(purple_account_parent_class)->constructed(object);
981 g_object_get(object,
982 "username", &username,
983 "protocol-id", &protocol_id,
984 NULL);
986 purple_signal_emit(purple_accounts_get_handle(), "account-created",
987 account);
989 protocol = purple_protocols_find(protocol_id);
990 if (protocol == NULL) {
991 g_free(username);
992 g_free(protocol_id);
993 return;
996 purple_account_set_status_types(account,
997 purple_protocol_class_status_types(protocol, account));
999 priv->presence = PURPLE_PRESENCE(purple_account_presence_new(account));
1001 status_type = purple_account_get_status_type_with_primitive(account,
1002 PURPLE_STATUS_AVAILABLE);
1003 if (status_type != NULL)
1004 purple_presence_set_status_active(priv->presence,
1005 purple_status_type_get_id(status_type),
1006 TRUE);
1007 else
1008 purple_presence_set_status_active(priv->presence,
1009 "offline",
1010 TRUE);
1012 g_free(username);
1013 g_free(protocol_id);
1016 static void
1017 purple_account_dispose(GObject *object)
1019 PurpleAccount *account = PURPLE_ACCOUNT(object);
1020 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
1022 if (!purple_account_is_disconnected(account))
1023 purple_account_disconnect(account);
1025 if (priv->presence) {
1026 g_object_unref(priv->presence);
1027 priv->presence = NULL;
1030 G_OBJECT_CLASS(purple_account_parent_class)->dispose(object);
1033 static void
1034 purple_account_finalize(GObject *object)
1036 GList *l;
1037 PurpleAccount *account = PURPLE_ACCOUNT(object);
1038 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
1040 purple_debug_info("account", "Destroying account %p\n", account);
1041 purple_signal_emit(purple_accounts_get_handle(), "account-destroying",
1042 account);
1044 for (l = purple_conversations_get_all(); l != NULL; l = l->next)
1046 PurpleConversation *conv = (PurpleConversation *)l->data;
1048 if (purple_conversation_get_account(conv) == account)
1049 purple_conversation_set_account(conv, NULL);
1052 purple_account_set_status_types(account, NULL);
1054 if (priv->proxy_info)
1055 purple_proxy_info_destroy(priv->proxy_info);
1057 if(priv->system_log)
1058 purple_log_free(priv->system_log);
1060 if (priv->current_error) {
1061 g_free(priv->current_error->description);
1062 g_free(priv->current_error);
1065 g_free(priv->username);
1066 g_free(priv->alias);
1067 purple_str_wipe(priv->password);
1068 g_free(priv->user_info);
1069 g_free(priv->buddy_icon_path);
1070 g_free(priv->protocol_id);
1072 g_hash_table_destroy(priv->settings);
1073 g_hash_table_destroy(priv->ui_settings);
1075 g_slist_free_full(priv->deny, g_free);
1076 g_slist_free_full(priv->permit, g_free);
1078 G_OBJECT_CLASS(purple_account_parent_class)->finalize(object);
1081 static void
1082 purple_account_class_init(PurpleAccountClass *klass)
1084 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
1086 obj_class->dispose = purple_account_dispose;
1087 obj_class->finalize = purple_account_finalize;
1088 obj_class->constructed = purple_account_constructed;
1090 /* Setup properties */
1091 obj_class->get_property = purple_account_get_property;
1092 obj_class->set_property = purple_account_set_property;
1094 properties[PROP_USERNAME] = g_param_spec_string("username", "Username",
1095 "The username for the account.", NULL,
1096 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
1098 properties[PROP_PRIVATE_ALIAS] = g_param_spec_string("private-alias",
1099 "Private Alias",
1100 "The private alias for the account.", NULL,
1101 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1103 properties[PROP_USER_INFO] = g_param_spec_string("user-info",
1104 "User information",
1105 "Detailed user information for the account.", NULL,
1106 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1108 properties[PROP_BUDDY_ICON_PATH] = g_param_spec_string("buddy-icon-path",
1109 "Buddy icon path",
1110 "Path to the buddyicon for the account.", NULL,
1111 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1113 properties[PROP_ENABLED] = g_param_spec_boolean("enabled", "Enabled",
1114 "Whether the account is enabled or not.", FALSE,
1115 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1117 properties[PROP_REMEMBER_PASSWORD] = g_param_spec_boolean(
1118 "remember-password", "Remember password",
1119 "Whether to remember and store the password for this account.",
1120 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1122 properties[PROP_CHECK_MAIL] = g_param_spec_boolean("check-mail",
1123 "Check mail",
1124 "Whether to check mails for this account.", FALSE,
1125 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1127 properties[PROP_CONNECTION] = g_param_spec_object("connection",
1128 "Connection",
1129 "The connection for the account.", PURPLE_TYPE_CONNECTION,
1130 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1132 properties[PROP_PROTOCOL_ID] = g_param_spec_string("protocol-id",
1133 "Protocol ID",
1134 "ID of the protocol that is responsible for the account.", NULL,
1135 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
1136 G_PARAM_STATIC_STRINGS);
1138 g_object_class_install_properties(obj_class, PROP_LAST, properties);
1141 /******************************************************************************
1142 * Public API
1143 *****************************************************************************/
1144 PurpleAccount *
1145 purple_account_new(const char *username, const char *protocol_id)
1147 PurpleAccount *account;
1149 g_return_val_if_fail(username != NULL, NULL);
1150 g_return_val_if_fail(protocol_id != NULL, NULL);
1152 account = purple_accounts_find(username, protocol_id);
1154 if (account != NULL)
1155 return account;
1157 account = g_object_new(PURPLE_TYPE_ACCOUNT,
1158 "username", username,
1159 "protocol-id", protocol_id,
1160 NULL);
1162 return account;
1165 void
1166 purple_account_connect(PurpleAccount *account)
1168 const char *username;
1169 PurpleProtocol *protocol;
1170 PurpleAccountPrivate *priv;
1172 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1174 username = purple_account_get_username(account);
1176 if (!purple_account_get_enabled(account, purple_core_get_ui())) {
1177 purple_debug_info("account",
1178 "Account %s not enabled, not connecting.\n",
1179 username);
1180 return;
1183 protocol = purple_protocols_find(purple_account_get_protocol_id(account));
1184 if (protocol == NULL) {
1185 gchar *message;
1187 message = g_strdup_printf(_("Missing protocol for %s"), username);
1188 purple_notify_error(account, _("Connection Error"), message,
1189 NULL, purple_request_cpar_from_account(account));
1190 g_free(message);
1191 return;
1194 priv = purple_account_get_instance_private(account);
1196 purple_debug_info("account", "Connecting to account %s.\n", username);
1198 if (priv->password != NULL) {
1199 purple_account_connect_got_password_cb(account,
1200 priv->password, NULL, protocol);
1201 } else {
1202 purple_keyring_get_password(account,
1203 purple_account_connect_got_password_cb, protocol);
1207 void
1208 purple_account_set_register_callback(PurpleAccount *account, PurpleAccountRegistrationCb cb, void *user_data)
1210 PurpleAccountPrivate *priv;
1212 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1214 priv = purple_account_get_instance_private(account);
1216 priv->registration_cb = cb;
1217 priv->registration_cb_user_data = user_data;
1220 void
1221 purple_account_register_completed(PurpleAccount *account, gboolean succeeded)
1223 struct register_completed_closure *closure;
1225 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1227 closure = g_new0(struct register_completed_closure, 1);
1228 closure->account = g_object_ref(account);
1229 closure->succeeded = succeeded;
1231 g_timeout_add(0, purple_account_register_completed_cb, closure);
1234 void
1235 purple_account_unregister(PurpleAccount *account, PurpleAccountUnregistrationCb cb, void *user_data)
1237 PurpleCallbackBundle *cbb;
1239 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1241 purple_debug_info("account", "Unregistering account %s\n",
1242 purple_account_get_username(account));
1244 cbb = g_new0(PurpleCallbackBundle, 1);
1245 cbb->cb = PURPLE_CALLBACK(cb);
1246 cbb->data = user_data;
1248 purple_keyring_get_password(account,
1249 purple_account_unregister_got_password_cb, cbb);
1252 void
1253 purple_account_disconnect(PurpleAccount *account)
1255 PurpleConnection *gc;
1256 PurpleAccountPrivate *priv;
1257 const char *username;
1259 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1260 g_return_if_fail(!purple_account_is_disconnecting(account));
1261 g_return_if_fail(!purple_account_is_disconnected(account));
1263 priv = purple_account_get_instance_private(account);
1265 username = purple_account_get_username(account);
1266 purple_debug_info("account", "Disconnecting account %s (%p)\n",
1267 username ? username : "(null)", account);
1269 priv->disconnecting = TRUE;
1271 gc = purple_account_get_connection(account);
1272 g_object_unref(gc);
1273 purple_account_set_connection(account, NULL);
1275 priv->disconnecting = FALSE;
1278 gboolean
1279 purple_account_is_disconnecting(PurpleAccount *account)
1281 PurpleAccountPrivate *priv;
1283 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), TRUE);
1285 priv = purple_account_get_instance_private(account);
1286 return priv->disconnecting;
1289 void
1290 purple_account_notify_added(PurpleAccount *account, const char *remote_user,
1291 const char *id, const char *alias,
1292 const char *message)
1294 PurpleAccountUiOps *ui_ops;
1296 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1297 g_return_if_fail(remote_user != NULL);
1299 ui_ops = purple_accounts_get_ui_ops();
1301 if (ui_ops != NULL && ui_ops->notify_added != NULL)
1302 ui_ops->notify_added(account, remote_user, id, alias, message);
1305 void
1306 purple_account_request_add(PurpleAccount *account, const char *remote_user,
1307 const char *id, const char *alias,
1308 const char *message)
1310 PurpleAccountUiOps *ui_ops;
1312 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1313 g_return_if_fail(remote_user != NULL);
1315 ui_ops = purple_accounts_get_ui_ops();
1317 if (ui_ops != NULL && ui_ops->request_add != NULL)
1318 ui_ops->request_add(account, remote_user, id, alias, message);
1321 void *
1322 purple_account_request_authorization(PurpleAccount *account, const char *remote_user,
1323 const char *id, const char *alias, const char *message, gboolean on_list,
1324 PurpleAccountRequestAuthorizationCb auth_cb, PurpleAccountRequestAuthorizationCb deny_cb, void *user_data)
1326 PurpleAccountUiOps *ui_ops;
1327 PurpleAccountRequestInfo *info;
1328 int plugin_return;
1329 char *response = NULL;
1331 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
1332 g_return_val_if_fail(remote_user != NULL, NULL);
1334 ui_ops = purple_accounts_get_ui_ops();
1336 plugin_return = GPOINTER_TO_INT(
1337 purple_signal_emit_return_1(
1338 purple_accounts_get_handle(),
1339 "account-authorization-requested",
1340 account, remote_user, message, &response
1343 switch (plugin_return)
1345 case PURPLE_ACCOUNT_RESPONSE_IGNORE:
1346 g_free(response);
1347 return NULL;
1348 case PURPLE_ACCOUNT_RESPONSE_ACCEPT:
1349 if (auth_cb != NULL)
1350 auth_cb(response, user_data);
1351 g_free(response);
1352 return NULL;
1353 case PURPLE_ACCOUNT_RESPONSE_DENY:
1354 if (deny_cb != NULL)
1355 deny_cb(response, user_data);
1356 g_free(response);
1357 return NULL;
1360 g_free(response);
1362 if (ui_ops != NULL && ui_ops->request_authorize != NULL) {
1363 info = g_new0(PurpleAccountRequestInfo, 1);
1364 info->type = PURPLE_ACCOUNT_REQUEST_AUTHORIZATION;
1365 info->account = account;
1366 info->auth_cb = auth_cb;
1367 info->deny_cb = deny_cb;
1368 info->userdata = user_data;
1369 info->user = g_strdup(remote_user);
1370 info->ref = 2; /* We hold an extra ref to make sure info remains valid
1371 if any of the callbacks are called synchronously. We
1372 unref it after the function call */
1374 info->ui_handle = ui_ops->request_authorize(account, remote_user, id, alias, message,
1375 on_list, request_auth_cb, request_deny_cb, info);
1377 info = purple_account_request_info_unref(info);
1378 if (info) {
1379 handles = g_list_append(handles, info);
1380 return info->ui_handle;
1384 return NULL;
1387 void
1388 purple_account_request_close_with_account(PurpleAccount *account)
1390 GList *l, *l_next;
1392 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1394 for (l = handles; l != NULL; l = l_next) {
1395 PurpleAccountRequestInfo *info = l->data;
1397 l_next = l->next;
1399 if (info->account == account) {
1400 handles = g_list_remove(handles, info);
1401 purple_account_request_close_info(info);
1406 void
1407 purple_account_request_close(void *ui_handle)
1409 GList *l, *l_next;
1411 g_return_if_fail(ui_handle != NULL);
1413 for (l = handles; l != NULL; l = l_next) {
1414 PurpleAccountRequestInfo *info = l->data;
1416 l_next = l->next;
1418 if (info->ui_handle == ui_handle) {
1419 handles = g_list_remove(handles, info);
1420 purple_account_request_close_info(info);
1425 void
1426 purple_account_request_password(PurpleAccount *account, GCallback ok_cb,
1427 GCallback cancel_cb, void *user_data)
1429 gchar *primary;
1430 const gchar *username;
1431 PurpleRequestFieldGroup *group;
1432 PurpleRequestField *field;
1433 PurpleRequestFields *fields;
1435 /* Close any previous password request windows */
1436 purple_request_close_with_handle(account);
1438 username = purple_account_get_username(account);
1439 primary = g_strdup_printf(_("Enter password for %s (%s)"), username,
1440 purple_account_get_protocol_name(account));
1442 fields = purple_request_fields_new();
1443 group = purple_request_field_group_new(NULL);
1444 purple_request_fields_add_group(fields, group);
1446 field = purple_request_field_string_new("password", _("Enter Password"), NULL, FALSE);
1447 purple_request_field_string_set_masked(field, TRUE);
1448 purple_request_field_set_required(field, TRUE);
1449 purple_request_field_group_add_field(group, field);
1451 field = purple_request_field_bool_new("remember", _("Save password"), FALSE);
1452 purple_request_field_group_add_field(group, field);
1454 purple_request_fields(account, NULL, primary, NULL, fields, _("OK"),
1455 ok_cb, _("Cancel"), cancel_cb,
1456 purple_request_cpar_from_account(account), user_data);
1457 g_free(primary);
1460 void
1461 purple_account_request_change_password(PurpleAccount *account)
1463 PurpleRequestFields *fields;
1464 PurpleRequestFieldGroup *group;
1465 PurpleRequestField *field;
1466 PurpleConnection *gc;
1467 PurpleProtocol *protocol = NULL;
1468 char primary[256];
1470 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1471 g_return_if_fail(purple_account_is_connected(account));
1473 gc = purple_account_get_connection(account);
1474 if (gc != NULL)
1475 protocol = purple_connection_get_protocol(gc);
1477 fields = purple_request_fields_new();
1479 group = purple_request_field_group_new(NULL);
1480 purple_request_fields_add_group(fields, group);
1482 field = purple_request_field_string_new("password", _("Original password"),
1483 NULL, FALSE);
1484 purple_request_field_string_set_masked(field, TRUE);
1485 if (!protocol || !(purple_protocol_get_options(protocol) & OPT_PROTO_PASSWORD_OPTIONAL))
1486 purple_request_field_set_required(field, TRUE);
1487 purple_request_field_group_add_field(group, field);
1489 field = purple_request_field_string_new("new_password_1",
1490 _("New password"),
1491 NULL, FALSE);
1492 purple_request_field_string_set_masked(field, TRUE);
1493 if (!protocol || !(purple_protocol_get_options(protocol) & OPT_PROTO_PASSWORD_OPTIONAL))
1494 purple_request_field_set_required(field, TRUE);
1495 purple_request_field_group_add_field(group, field);
1497 field = purple_request_field_string_new("new_password_2",
1498 _("New password (again)"),
1499 NULL, FALSE);
1500 purple_request_field_string_set_masked(field, TRUE);
1501 if (!protocol || !(purple_protocol_get_options(protocol) & OPT_PROTO_PASSWORD_OPTIONAL))
1502 purple_request_field_set_required(field, TRUE);
1503 purple_request_field_group_add_field(group, field);
1505 g_snprintf(primary, sizeof(primary), _("Change password for %s"),
1506 purple_account_get_username(account));
1508 /* I'm sticking this somewhere in the code: bologna */
1510 purple_request_fields(purple_account_get_connection(account), NULL,
1511 primary, _("Please enter your current password and your new "
1512 "password."), fields, _("OK"), G_CALLBACK(change_password_cb),
1513 _("Cancel"), NULL, purple_request_cpar_from_account(account),
1514 account);
1517 void
1518 purple_account_request_change_user_info(PurpleAccount *account)
1520 PurpleConnection *gc;
1521 char primary[256];
1523 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1524 g_return_if_fail(purple_account_is_connected(account));
1526 gc = purple_account_get_connection(account);
1528 g_snprintf(primary, sizeof(primary),
1529 _("Change user information for %s"),
1530 purple_account_get_username(account));
1532 purple_request_input(gc, _("Set User Info"), primary, NULL,
1533 purple_account_get_user_info(account),
1534 TRUE, FALSE, ((gc != NULL) &&
1535 (purple_connection_get_flags(gc) & PURPLE_CONNECTION_FLAG_HTML) ? "html" : NULL),
1536 _("Save"), G_CALLBACK(set_user_info_cb),
1537 _("Cancel"), NULL,
1538 purple_request_cpar_from_account(account),
1539 account);
1542 void
1543 purple_account_set_username(PurpleAccount *account, const char *username)
1545 PurpleAccountPrivate *priv;
1547 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1549 priv = purple_account_get_instance_private(account);
1551 g_free(priv->username);
1552 priv->username = g_strdup(username);
1554 g_object_notify_by_pspec(G_OBJECT(account), properties[PROP_USERNAME]);
1556 purple_accounts_schedule_save();
1558 /* if the name changes, we should re-write the buddy list
1559 * to disk with the new name */
1560 purple_blist_save_account(purple_blist_get_default(), account);
1563 void
1564 purple_account_set_password(PurpleAccount *account, const gchar *password,
1565 PurpleKeyringSaveCallback cb, gpointer data)
1567 PurpleAccountPrivate *priv;
1569 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1571 priv = purple_account_get_instance_private(account);
1573 purple_str_wipe(priv->password);
1574 priv->password = g_strdup(password);
1576 purple_accounts_schedule_save();
1578 if (!purple_account_get_remember_password(account)) {
1579 purple_debug_info("account",
1580 "Password for %s set, not sent to keyring.\n",
1581 purple_account_get_username(account));
1583 if (cb != NULL)
1584 cb(account, NULL, data);
1585 } else {
1586 purple_keyring_set_password(account, password, cb, data);
1590 void
1591 purple_account_set_private_alias(PurpleAccount *account, const char *alias)
1593 PurpleAccountPrivate *priv;
1595 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1597 priv = purple_account_get_instance_private(account);
1600 * Do nothing if alias and priv->alias are both NULL. Or if
1601 * they're the exact same string.
1603 if (alias == priv->alias)
1604 return;
1606 if ((!alias && priv->alias) || (alias && !priv->alias) ||
1607 g_utf8_collate(priv->alias, alias))
1609 char *old = priv->alias;
1611 priv->alias = g_strdup(alias);
1612 g_object_notify_by_pspec(G_OBJECT(account),
1613 properties[PROP_PRIVATE_ALIAS]);
1614 purple_signal_emit(purple_accounts_get_handle(), "account-alias-changed",
1615 account, old);
1616 g_free(old);
1618 purple_accounts_schedule_save();
1622 void
1623 purple_account_set_user_info(PurpleAccount *account, const char *user_info)
1625 PurpleAccountPrivate *priv;
1627 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1629 priv = purple_account_get_instance_private(account);
1631 g_free(priv->user_info);
1632 priv->user_info = g_strdup(user_info);
1634 g_object_notify_by_pspec(G_OBJECT(account), properties[PROP_USER_INFO]);
1636 purple_accounts_schedule_save();
1639 void purple_account_set_buddy_icon_path(PurpleAccount *account, const char *path)
1641 PurpleAccountPrivate *priv;
1643 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1645 priv = purple_account_get_instance_private(account);
1647 g_free(priv->buddy_icon_path);
1648 priv->buddy_icon_path = g_strdup(path);
1650 g_object_notify_by_pspec(G_OBJECT(account),
1651 properties[PROP_BUDDY_ICON_PATH]);
1653 purple_accounts_schedule_save();
1656 void
1657 purple_account_set_protocol_id(PurpleAccount *account, const char *protocol_id)
1659 PurpleAccountPrivate *priv;
1661 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1662 g_return_if_fail(protocol_id != NULL);
1664 priv = purple_account_get_instance_private(account);
1666 g_free(priv->protocol_id);
1667 priv->protocol_id = g_strdup(protocol_id);
1669 g_object_notify_by_pspec(G_OBJECT(account), properties[PROP_PROTOCOL_ID]);
1671 purple_accounts_schedule_save();
1674 void
1675 purple_account_set_connection(PurpleAccount *account, PurpleConnection *gc)
1677 PurpleAccountPrivate *priv;
1679 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1681 priv = purple_account_get_instance_private(account);
1682 priv->gc = gc;
1684 g_object_notify_by_pspec(G_OBJECT(account), properties[PROP_CONNECTION]);
1687 void
1688 purple_account_set_remember_password(PurpleAccount *account, gboolean value)
1690 PurpleAccountPrivate *priv;
1692 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1694 priv = purple_account_get_instance_private(account);
1695 priv->remember_pass = value;
1697 g_object_notify_by_pspec(G_OBJECT(account),
1698 properties[PROP_REMEMBER_PASSWORD]);
1700 purple_accounts_schedule_save();
1703 void
1704 purple_account_set_check_mail(PurpleAccount *account, gboolean value)
1706 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1708 purple_account_set_bool(account, "check-mail", value);
1710 g_object_notify_by_pspec(G_OBJECT(account), properties[PROP_CHECK_MAIL]);
1713 void
1714 purple_account_set_enabled(PurpleAccount *account, const char *ui,
1715 gboolean value)
1717 PurpleConnection *gc;
1718 PurpleAccountPrivate *priv;
1719 gboolean was_enabled = FALSE;
1721 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1722 g_return_if_fail(ui != NULL);
1724 was_enabled = purple_account_get_enabled(account, ui);
1726 purple_account_set_ui_bool(account, ui, "auto-login", value);
1727 gc = purple_account_get_connection(account);
1729 if(was_enabled && !value)
1730 purple_signal_emit(purple_accounts_get_handle(), "account-disabled", account);
1731 else if(!was_enabled && value)
1732 purple_signal_emit(purple_accounts_get_handle(), "account-enabled", account);
1734 g_object_notify_by_pspec(G_OBJECT(account), properties[PROP_ENABLED]);
1736 if ((gc != NULL) && (_purple_connection_wants_to_die(gc)))
1737 return;
1739 priv = purple_account_get_instance_private(account);
1741 if (value && purple_presence_is_online(priv->presence))
1742 purple_account_connect(account);
1743 else if (!value && !purple_account_is_disconnected(account))
1744 purple_account_disconnect(account);
1747 void
1748 purple_account_set_proxy_info(PurpleAccount *account, PurpleProxyInfo *info)
1750 PurpleAccountPrivate *priv;
1752 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1754 priv = purple_account_get_instance_private(account);
1756 if (priv->proxy_info != NULL)
1757 purple_proxy_info_destroy(priv->proxy_info);
1759 priv->proxy_info = info;
1761 purple_accounts_schedule_save();
1764 void
1765 purple_account_set_privacy_type(PurpleAccount *account, PurpleAccountPrivacyType privacy_type)
1767 PurpleAccountPrivate *priv;
1769 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1771 priv = purple_account_get_instance_private(account);
1772 priv->privacy_type = privacy_type;
1775 void
1776 purple_account_set_status_types(PurpleAccount *account, GList *status_types)
1778 PurpleAccountPrivate *priv;
1780 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1782 priv = purple_account_get_instance_private(account);
1784 /* Out with the old... */
1785 g_list_free_full(priv->status_types,
1786 (GDestroyNotify)purple_status_type_destroy);
1788 /* In with the new... */
1789 priv->status_types = status_types;
1792 void
1793 purple_account_set_status(PurpleAccount *account, const char *status_id,
1794 gboolean active, ...)
1796 GList *attrs = NULL;
1797 const gchar *id;
1798 gpointer data;
1799 va_list args;
1801 va_start(args, active);
1802 while ((id = va_arg(args, const char *)) != NULL)
1804 attrs = g_list_append(attrs, (char *)id);
1805 data = va_arg(args, void *);
1806 attrs = g_list_append(attrs, data);
1808 purple_account_set_status_list(account, status_id, active, attrs);
1809 g_list_free(attrs);
1810 va_end(args);
1813 void
1814 purple_account_set_status_list(PurpleAccount *account, const char *status_id,
1815 gboolean active, GList *attrs)
1817 PurpleStatus *status;
1819 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1820 g_return_if_fail(status_id != NULL);
1822 status = purple_account_get_status(account, status_id);
1823 if (status == NULL)
1825 purple_debug_error("account",
1826 "Invalid status ID '%s' for account %s (%s)\n",
1827 status_id, purple_account_get_username(account),
1828 purple_account_get_protocol_id(account));
1829 return;
1832 if (active || purple_status_is_independent(status))
1833 purple_status_set_active_with_attrs_list(status, active, attrs);
1836 * Our current statuses are saved to accounts.xml (so that when we
1837 * reconnect, we go back to the previous status).
1839 purple_accounts_schedule_save();
1842 void
1843 purple_account_set_public_alias(PurpleAccount *account,
1844 const char *alias, PurpleSetPublicAliasSuccessCallback success_cb,
1845 PurpleSetPublicAliasFailureCallback failure_cb)
1847 PurpleConnection *gc;
1848 PurpleProtocol *protocol = NULL;
1850 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1851 g_return_if_fail(purple_account_is_connected(account));
1853 gc = purple_account_get_connection(account);
1854 protocol = purple_connection_get_protocol(gc);
1856 if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, set_public_alias))
1857 purple_protocol_server_iface_set_public_alias(protocol, gc, alias, success_cb, failure_cb);
1858 else if (failure_cb) {
1859 struct public_alias_closure *closure =
1860 g_new0(struct public_alias_closure, 1);
1861 closure->account = g_object_ref(account);
1862 closure->failure_cb = failure_cb;
1863 g_timeout_add(0, set_public_alias_unsupported, closure);
1867 void
1868 purple_account_get_public_alias(PurpleAccount *account,
1869 PurpleGetPublicAliasSuccessCallback success_cb,
1870 PurpleGetPublicAliasFailureCallback failure_cb)
1872 PurpleConnection *gc;
1873 PurpleProtocol *protocol = NULL;
1875 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1876 g_return_if_fail(purple_account_is_connected(account));
1878 gc = purple_account_get_connection(account);
1879 protocol = purple_connection_get_protocol(gc);
1881 if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, get_public_alias))
1882 purple_protocol_server_iface_get_public_alias(protocol, gc, success_cb, failure_cb);
1883 else if (failure_cb) {
1884 struct public_alias_closure *closure =
1885 g_new0(struct public_alias_closure, 1);
1886 closure->account = g_object_ref(account);
1887 closure->failure_cb = failure_cb;
1888 g_timeout_add(0, get_public_alias_unsupported, closure);
1892 gboolean
1893 purple_account_get_silence_suppression(PurpleAccount *account)
1895 return purple_account_get_bool(account, "silence-suppression", FALSE);
1898 void
1899 purple_account_set_silence_suppression(PurpleAccount *account, gboolean value)
1901 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1903 purple_account_set_bool(account, "silence-suppression", value);
1906 void
1907 purple_account_clear_settings(PurpleAccount *account)
1909 PurpleAccountPrivate *priv;
1911 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1913 priv = purple_account_get_instance_private(account);
1914 g_hash_table_destroy(priv->settings);
1916 priv->settings = g_hash_table_new_full(g_str_hash, g_str_equal,
1917 g_free, delete_setting);
1920 void
1921 purple_account_remove_setting(PurpleAccount *account, const char *setting)
1923 PurpleAccountPrivate *priv;
1925 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1926 g_return_if_fail(setting != NULL);
1928 priv = purple_account_get_instance_private(account);
1930 g_hash_table_remove(priv->settings, setting);
1933 void
1934 purple_account_set_int(PurpleAccount *account, const char *name, int value)
1936 PurpleAccountSetting *setting;
1937 PurpleAccountPrivate *priv;
1939 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1940 g_return_if_fail(name != NULL);
1942 priv = purple_account_get_instance_private(account);
1944 setting = g_new0(PurpleAccountSetting, 1);
1946 g_value_init(&setting->value, G_TYPE_INT);
1947 g_value_set_int(&setting->value, value);
1949 g_hash_table_insert(priv->settings, g_strdup(name), setting);
1951 purple_accounts_schedule_save();
1954 void
1955 purple_account_set_string(PurpleAccount *account, const char *name,
1956 const char *value)
1958 PurpleAccountSetting *setting;
1959 PurpleAccountPrivate *priv;
1961 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1962 g_return_if_fail(name != NULL);
1964 priv = purple_account_get_instance_private(account);
1966 setting = g_new0(PurpleAccountSetting, 1);
1968 g_value_init(&setting->value, G_TYPE_STRING);
1969 g_value_set_string(&setting->value, value);
1971 g_hash_table_insert(priv->settings, g_strdup(name), setting);
1973 purple_accounts_schedule_save();
1976 void
1977 purple_account_set_bool(PurpleAccount *account, const char *name, gboolean value)
1979 PurpleAccountSetting *setting;
1980 PurpleAccountPrivate *priv;
1982 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
1983 g_return_if_fail(name != NULL);
1985 priv = purple_account_get_instance_private(account);
1987 setting = g_new0(PurpleAccountSetting, 1);
1989 g_value_init(&setting->value, G_TYPE_BOOLEAN);
1990 g_value_set_boolean(&setting->value, value);
1992 g_hash_table_insert(priv->settings, g_strdup(name), setting);
1994 purple_accounts_schedule_save();
1997 void
1998 purple_account_set_ui_int(PurpleAccount *account, const char *ui,
1999 const char *name, int value)
2001 PurpleAccountSetting *setting;
2002 GHashTable *table;
2004 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
2005 g_return_if_fail(ui != NULL);
2006 g_return_if_fail(name != NULL);
2008 setting = g_new0(PurpleAccountSetting, 1);
2010 setting->ui = g_strdup(ui);
2011 g_value_init(&setting->value, G_TYPE_INT);
2012 g_value_set_int(&setting->value, value);
2014 table = get_ui_settings_table(account, ui);
2016 g_hash_table_insert(table, g_strdup(name), setting);
2018 purple_accounts_schedule_save();
2021 void
2022 purple_account_set_ui_string(PurpleAccount *account, const char *ui,
2023 const char *name, const char *value)
2025 PurpleAccountSetting *setting;
2026 GHashTable *table;
2028 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
2029 g_return_if_fail(ui != NULL);
2030 g_return_if_fail(name != NULL);
2032 setting = g_new0(PurpleAccountSetting, 1);
2034 setting->ui = g_strdup(ui);
2035 g_value_init(&setting->value, G_TYPE_STRING);
2036 g_value_set_string(&setting->value, value);
2038 table = get_ui_settings_table(account, ui);
2040 g_hash_table_insert(table, g_strdup(name), setting);
2042 purple_accounts_schedule_save();
2045 void
2046 purple_account_set_ui_bool(PurpleAccount *account, const char *ui,
2047 const char *name, gboolean value)
2049 PurpleAccountSetting *setting;
2050 GHashTable *table;
2052 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
2053 g_return_if_fail(ui != NULL);
2054 g_return_if_fail(name != NULL);
2056 setting = g_new0(PurpleAccountSetting, 1);
2058 setting->ui = g_strdup(ui);
2059 g_value_init(&setting->value, G_TYPE_BOOLEAN);
2060 g_value_set_boolean(&setting->value, value);
2062 table = get_ui_settings_table(account, ui);
2064 g_hash_table_insert(table, g_strdup(name), setting);
2066 purple_accounts_schedule_save();
2069 gboolean
2070 purple_account_is_connected(PurpleAccount *account)
2072 return (purple_account_get_state(account) == PURPLE_CONNECTION_CONNECTED);
2075 gboolean
2076 purple_account_is_connecting(PurpleAccount *account)
2078 return (purple_account_get_state(account) == PURPLE_CONNECTION_CONNECTING);
2081 gboolean
2082 purple_account_is_disconnected(PurpleAccount *account)
2084 return (purple_account_get_state(account) == PURPLE_CONNECTION_DISCONNECTED);
2087 const char *
2088 purple_account_get_username(PurpleAccount *account)
2090 PurpleAccountPrivate *priv;
2092 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2094 priv = purple_account_get_instance_private(account);
2095 return priv->username;
2098 void
2099 purple_account_get_password(PurpleAccount *account,
2100 PurpleKeyringReadCallback cb, gpointer data)
2102 PurpleAccountPrivate *priv;
2104 if (account == NULL) {
2105 cb(NULL, NULL, NULL, data);
2106 return;
2109 priv = purple_account_get_instance_private(account);
2111 if (priv->password != NULL) {
2112 purple_debug_info("account",
2113 "Reading password for account %s from cache.\n",
2114 purple_account_get_username(account));
2115 cb(account, priv->password, NULL, data);
2116 } else {
2117 PurpleCallbackBundle *cbb = g_new0(PurpleCallbackBundle, 1);
2118 cbb->cb = PURPLE_CALLBACK(cb);
2119 cbb->data = data;
2121 purple_debug_info("account",
2122 "Reading password for account %s from async keyring.\n",
2123 purple_account_get_username(account));
2124 purple_keyring_get_password(account,
2125 purple_account_get_password_got, cbb);
2129 const char *
2130 purple_account_get_private_alias(PurpleAccount *account)
2132 PurpleAccountPrivate *priv;
2134 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2136 priv = purple_account_get_instance_private(account);
2137 return priv->alias;
2140 const char *
2141 purple_account_get_user_info(PurpleAccount *account)
2143 PurpleAccountPrivate *priv;
2145 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2147 priv = purple_account_get_instance_private(account);
2148 return priv->user_info;
2151 const char *
2152 purple_account_get_buddy_icon_path(PurpleAccount *account)
2154 PurpleAccountPrivate *priv;
2156 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2158 priv = purple_account_get_instance_private(account);
2159 return priv->buddy_icon_path;
2162 const char *
2163 purple_account_get_protocol_id(PurpleAccount *account)
2165 PurpleAccountPrivate *priv;
2167 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2169 priv = purple_account_get_instance_private(account);
2170 return priv->protocol_id;
2173 const char *
2174 purple_account_get_protocol_name(PurpleAccount *account)
2176 PurpleProtocol *p;
2178 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2180 p = purple_protocols_find(purple_account_get_protocol_id(account));
2182 return (p && purple_protocol_get_name(p) ?
2183 _(purple_protocol_get_name(p)) : _("Unknown"));
2186 PurpleConnection *
2187 purple_account_get_connection(PurpleAccount *account)
2189 PurpleAccountPrivate *priv;
2191 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2193 priv = purple_account_get_instance_private(account);
2194 return priv->gc;
2197 const gchar *
2198 purple_account_get_name_for_display(PurpleAccount *account)
2200 PurpleBuddy *self = NULL;
2201 PurpleConnection *gc = NULL;
2202 const gchar *name = NULL, *username = NULL, *displayname = NULL;
2204 name = purple_account_get_private_alias(account);
2206 if (name) {
2207 return name;
2210 username = purple_account_get_username(account);
2211 self = purple_blist_find_buddy((PurpleAccount *)account, username);
2213 if (self) {
2214 const gchar *calias= purple_buddy_get_contact_alias(self);
2216 /* We don't want to return the buddy name if the buddy/contact
2217 * doesn't have an alias set. */
2218 if (!purple_strequal(username, calias)) {
2219 return calias;
2223 gc = purple_account_get_connection(account);
2224 displayname = purple_connection_get_display_name(gc);
2226 if (displayname) {
2227 return displayname;
2230 return username;
2233 gboolean
2234 purple_account_get_remember_password(PurpleAccount *account)
2236 PurpleAccountPrivate *priv;
2238 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), FALSE);
2240 priv = purple_account_get_instance_private(account);
2241 return priv->remember_pass;
2244 gboolean
2245 purple_account_get_check_mail(PurpleAccount *account)
2247 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), FALSE);
2249 return purple_account_get_bool(account, "check-mail", FALSE);
2252 gboolean
2253 purple_account_get_enabled(PurpleAccount *account, const char *ui)
2255 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), FALSE);
2256 g_return_val_if_fail(ui != NULL, FALSE);
2258 return purple_account_get_ui_bool(account, ui, "auto-login", FALSE);
2261 PurpleProxyInfo *
2262 purple_account_get_proxy_info(PurpleAccount *account)
2264 PurpleAccountPrivate *priv;
2266 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2268 priv = purple_account_get_instance_private(account);
2269 return priv->proxy_info;
2272 PurpleAccountPrivacyType
2273 purple_account_get_privacy_type(PurpleAccount *account)
2275 PurpleAccountPrivate *priv;
2277 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), PURPLE_ACCOUNT_PRIVACY_ALLOW_ALL);
2279 priv = purple_account_get_instance_private(account);
2280 return priv->privacy_type;
2283 gboolean
2284 purple_account_privacy_permit_add(PurpleAccount *account, const char *who,
2285 gboolean local_only)
2287 char *name;
2288 PurpleBuddy *buddy;
2289 PurpleAccountPrivate *priv;
2290 PurpleAccountUiOps *ui_ops = purple_accounts_get_ui_ops();
2292 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), FALSE);
2293 g_return_val_if_fail(who != NULL, FALSE);
2295 priv = purple_account_get_instance_private(account);
2296 name = g_strdup(purple_normalize(account, who));
2298 if (g_slist_find_custom(priv->permit, name, (GCompareFunc)g_strcmp0) != NULL) {
2299 /* This buddy already exists, so bail out */
2300 g_free(name);
2301 return FALSE;
2304 priv->permit = g_slist_append(priv->permit, name);
2306 if (!local_only && purple_account_is_connected(account))
2307 purple_serv_add_permit(purple_account_get_connection(account), who);
2309 if (ui_ops != NULL && ui_ops->permit_added != NULL)
2310 ui_ops->permit_added(account, who);
2312 purple_blist_save_account(purple_blist_get_default(), account);
2314 /* This lets the UI know a buddy has had its privacy setting changed */
2315 buddy = purple_blist_find_buddy(account, name);
2316 if (buddy != NULL) {
2317 purple_signal_emit(purple_blist_get_handle(),
2318 "buddy-privacy-changed", buddy);
2320 return TRUE;
2323 gboolean
2324 purple_account_privacy_permit_remove(PurpleAccount *account, const char *who,
2325 gboolean local_only)
2327 GSList *l;
2328 const char *name;
2329 PurpleBuddy *buddy;
2330 char *del;
2331 PurpleAccountPrivate *priv;
2332 PurpleAccountUiOps *ui_ops = purple_accounts_get_ui_ops();
2334 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), FALSE);
2335 g_return_val_if_fail(who != NULL, FALSE);
2337 priv = purple_account_get_instance_private(account);
2338 name = purple_normalize(account, who);
2340 l = g_slist_find_custom(priv->permit, name, (GCompareFunc)g_strcmp0);
2341 if (l == NULL)
2342 /* We didn't find the buddy we were looking for, so bail out */
2343 return FALSE;
2345 /* We should not free l->data just yet. There can be occasions where
2346 * l->data == who. In such cases, freeing l->data here can cause crashes
2347 * later when who is used. */
2348 del = l->data;
2349 priv->permit = g_slist_delete_link(priv->permit, l);
2351 if (!local_only && purple_account_is_connected(account))
2352 purple_serv_rem_permit(purple_account_get_connection(account), who);
2354 if (ui_ops != NULL && ui_ops->permit_removed != NULL)
2355 ui_ops->permit_removed(account, who);
2357 purple_blist_save_account(purple_blist_get_default(), account);
2359 buddy = purple_blist_find_buddy(account, name);
2360 if (buddy != NULL) {
2361 purple_signal_emit(purple_blist_get_handle(),
2362 "buddy-privacy-changed", buddy);
2364 g_free(del);
2365 return TRUE;
2368 gboolean
2369 purple_account_privacy_deny_add(PurpleAccount *account, const char *who,
2370 gboolean local_only)
2372 char *name;
2373 PurpleBuddy *buddy;
2374 PurpleAccountPrivate *priv;
2375 PurpleAccountUiOps *ui_ops = purple_accounts_get_ui_ops();
2377 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), FALSE);
2378 g_return_val_if_fail(who != NULL, FALSE);
2380 priv = purple_account_get_instance_private(account);
2381 name = g_strdup(purple_normalize(account, who));
2383 if (g_slist_find_custom(priv->deny, name, (GCompareFunc)g_strcmp0) != NULL) {
2384 /* This buddy already exists, so bail out */
2385 g_free(name);
2386 return FALSE;
2389 priv->deny = g_slist_append(priv->deny, name);
2391 if (!local_only && purple_account_is_connected(account))
2392 purple_serv_add_deny(purple_account_get_connection(account), who);
2394 if (ui_ops != NULL && ui_ops->deny_added != NULL)
2395 ui_ops->deny_added(account, who);
2397 purple_blist_save_account(purple_blist_get_default(), account);
2399 buddy = purple_blist_find_buddy(account, name);
2400 if (buddy != NULL) {
2401 purple_signal_emit(purple_blist_get_handle(),
2402 "buddy-privacy-changed", buddy);
2404 return TRUE;
2407 gboolean
2408 purple_account_privacy_deny_remove(PurpleAccount *account, const char *who,
2409 gboolean local_only)
2411 GSList *l;
2412 const char *normalized;
2413 char *name;
2414 PurpleBuddy *buddy;
2415 PurpleAccountPrivate *priv;
2416 PurpleAccountUiOps *ui_ops = purple_accounts_get_ui_ops();
2418 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), FALSE);
2419 g_return_val_if_fail(who != NULL, FALSE);
2421 priv = purple_account_get_instance_private(account);
2422 normalized = purple_normalize(account, who);
2424 l = g_slist_find_custom(priv->deny, normalized, (GCompareFunc)g_strcmp0);
2425 if (l == NULL)
2426 /* We didn't find the buddy we were looking for, so bail out */
2427 return FALSE;
2429 buddy = purple_blist_find_buddy(account, normalized);
2431 name = l->data;
2432 priv->deny = g_slist_delete_link(priv->deny, l);
2434 if (!local_only && purple_account_is_connected(account))
2435 purple_serv_rem_deny(purple_account_get_connection(account), name);
2437 if (ui_ops != NULL && ui_ops->deny_removed != NULL)
2438 ui_ops->deny_removed(account, who);
2440 if (buddy != NULL) {
2441 purple_signal_emit(purple_blist_get_handle(),
2442 "buddy-privacy-changed", buddy);
2445 g_free(name);
2447 purple_blist_save_account(purple_blist_get_default(), account);
2449 return TRUE;
2452 void
2453 purple_account_privacy_allow(PurpleAccount *account, const char *who)
2455 GSList *list;
2456 PurpleAccountPrivacyType type = purple_account_get_privacy_type(account);
2457 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
2459 switch (type) {
2460 case PURPLE_ACCOUNT_PRIVACY_ALLOW_ALL:
2461 return;
2462 case PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS:
2463 purple_account_privacy_permit_add(account, who, FALSE);
2464 break;
2465 case PURPLE_ACCOUNT_PRIVACY_DENY_USERS:
2466 purple_account_privacy_deny_remove(account, who, FALSE);
2467 break;
2468 case PURPLE_ACCOUNT_PRIVACY_DENY_ALL:
2470 /* Empty the allow-list. */
2471 const char *norm = purple_normalize(account, who);
2472 for (list = priv->permit; list != NULL;) {
2473 char *person = list->data;
2474 list = list->next;
2475 if (!purple_strequal(norm, person))
2476 purple_account_privacy_permit_remove(account, person, FALSE);
2478 purple_account_privacy_permit_add(account, who, FALSE);
2479 purple_account_set_privacy_type(account, PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS);
2481 break;
2482 case PURPLE_ACCOUNT_PRIVACY_ALLOW_BUDDYLIST:
2483 if (!purple_blist_find_buddy(account, who)) {
2484 add_all_buddies_to_permit_list(account, FALSE);
2485 purple_account_privacy_permit_add(account, who, FALSE);
2486 purple_account_set_privacy_type(account, PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS);
2488 break;
2489 default:
2490 g_return_if_reached();
2493 /* Notify the server if the privacy setting was changed */
2494 if (type != purple_account_get_privacy_type(account) && purple_account_is_connected(account))
2495 purple_serv_set_permit_deny(purple_account_get_connection(account));
2498 void
2499 purple_account_privacy_deny(PurpleAccount *account, const char *who)
2501 GSList *list;
2502 PurpleAccountPrivacyType type = purple_account_get_privacy_type(account);
2503 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
2505 switch (type) {
2506 case PURPLE_ACCOUNT_PRIVACY_ALLOW_ALL:
2508 /* Empty the deny-list. */
2509 const char *norm = purple_normalize(account, who);
2510 for (list = priv->deny; list != NULL; ) {
2511 char *person = list->data;
2512 list = list->next;
2513 if (!purple_strequal(norm, person))
2514 purple_account_privacy_deny_remove(account, person, FALSE);
2516 purple_account_privacy_deny_add(account, who, FALSE);
2517 purple_account_set_privacy_type(account, PURPLE_ACCOUNT_PRIVACY_DENY_USERS);
2519 break;
2520 case PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS:
2521 purple_account_privacy_permit_remove(account, who, FALSE);
2522 break;
2523 case PURPLE_ACCOUNT_PRIVACY_DENY_USERS:
2524 purple_account_privacy_deny_add(account, who, FALSE);
2525 break;
2526 case PURPLE_ACCOUNT_PRIVACY_DENY_ALL:
2527 break;
2528 case PURPLE_ACCOUNT_PRIVACY_ALLOW_BUDDYLIST:
2529 if (purple_blist_find_buddy(account, who)) {
2530 add_all_buddies_to_permit_list(account, FALSE);
2531 purple_account_privacy_permit_remove(account, who, FALSE);
2532 purple_account_set_privacy_type(account, PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS);
2534 break;
2535 default:
2536 g_return_if_reached();
2539 /* Notify the server if the privacy setting was changed */
2540 if (type != purple_account_get_privacy_type(account) && purple_account_is_connected(account))
2541 purple_serv_set_permit_deny(purple_account_get_connection(account));
2544 GSList *
2545 purple_account_privacy_get_permitted(PurpleAccount *account)
2547 PurpleAccountPrivate *priv = NULL;
2549 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2551 priv = purple_account_get_instance_private(account);
2552 return priv->permit;
2555 GSList *
2556 purple_account_privacy_get_denied(PurpleAccount *account)
2558 PurpleAccountPrivate *priv = NULL;
2560 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2562 priv = purple_account_get_instance_private(account);
2563 return priv->deny;
2566 gboolean
2567 purple_account_privacy_check(PurpleAccount *account, const char *who)
2569 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
2571 switch (purple_account_get_privacy_type(account)) {
2572 case PURPLE_ACCOUNT_PRIVACY_ALLOW_ALL:
2573 return TRUE;
2575 case PURPLE_ACCOUNT_PRIVACY_DENY_ALL:
2576 return FALSE;
2578 case PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS:
2579 who = purple_normalize(account, who);
2580 return (g_slist_find_custom(priv->permit, who, (GCompareFunc)g_strcmp0) != NULL);
2582 case PURPLE_ACCOUNT_PRIVACY_DENY_USERS:
2583 who = purple_normalize(account, who);
2584 return (g_slist_find_custom(priv->deny, who, (GCompareFunc)g_strcmp0) == NULL);
2586 case PURPLE_ACCOUNT_PRIVACY_ALLOW_BUDDYLIST:
2587 return (purple_blist_find_buddy(account, who) != NULL);
2589 default:
2590 g_return_val_if_reached(TRUE);
2594 PurpleStatus *
2595 purple_account_get_active_status(PurpleAccount *account)
2597 PurpleAccountPrivate *priv;
2599 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2601 priv = purple_account_get_instance_private(account);
2602 return purple_presence_get_active_status(priv->presence);
2605 PurpleStatus *
2606 purple_account_get_status(PurpleAccount *account, const char *status_id)
2608 PurpleAccountPrivate *priv;
2610 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2611 g_return_val_if_fail(status_id != NULL, NULL);
2613 priv = purple_account_get_instance_private(account);
2615 return purple_presence_get_status(priv->presence, status_id);
2618 PurpleStatusType *
2619 purple_account_get_status_type(PurpleAccount *account, const char *id)
2621 GList *l;
2623 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2624 g_return_val_if_fail(id != NULL, NULL);
2626 for (l = purple_account_get_status_types(account); l != NULL; l = l->next)
2628 PurpleStatusType *status_type = (PurpleStatusType *)l->data;
2630 if (purple_strequal(purple_status_type_get_id(status_type), id))
2631 return status_type;
2634 return NULL;
2637 PurpleStatusType *
2638 purple_account_get_status_type_with_primitive(PurpleAccount *account, PurpleStatusPrimitive primitive)
2640 GList *l;
2642 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2644 for (l = purple_account_get_status_types(account); l != NULL; l = l->next)
2646 PurpleStatusType *status_type = (PurpleStatusType *)l->data;
2648 if (purple_status_type_get_primitive(status_type) == primitive)
2649 return status_type;
2652 return NULL;
2655 PurplePresence *
2656 purple_account_get_presence(PurpleAccount *account)
2658 PurpleAccountPrivate *priv;
2660 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2662 priv = purple_account_get_instance_private(account);
2663 return priv->presence;
2666 gboolean
2667 purple_account_is_status_active(PurpleAccount *account,
2668 const char *status_id)
2670 PurpleAccountPrivate *priv;
2672 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), FALSE);
2673 g_return_val_if_fail(status_id != NULL, FALSE);
2675 priv = purple_account_get_instance_private(account);
2677 return purple_presence_is_status_active(priv->presence, status_id);
2680 GList *
2681 purple_account_get_status_types(PurpleAccount *account)
2683 PurpleAccountPrivate *priv;
2685 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2687 priv = purple_account_get_instance_private(account);
2688 return priv->status_types;
2692 purple_account_get_int(PurpleAccount *account, const char *name,
2693 int default_value)
2695 PurpleAccountSetting *setting;
2696 PurpleAccountPrivate *priv;
2698 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), default_value);
2699 g_return_val_if_fail(name != NULL, default_value);
2701 priv = purple_account_get_instance_private(account);
2703 setting = g_hash_table_lookup(priv->settings, name);
2705 if (setting == NULL)
2706 return default_value;
2708 g_return_val_if_fail(G_VALUE_HOLDS_INT(&setting->value), default_value);
2710 return g_value_get_int(&setting->value);
2713 const char *
2714 purple_account_get_string(PurpleAccount *account, const char *name,
2715 const char *default_value)
2717 PurpleAccountSetting *setting;
2718 PurpleAccountPrivate *priv;
2720 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), default_value);
2721 g_return_val_if_fail(name != NULL, default_value);
2723 priv = purple_account_get_instance_private(account);
2725 setting = g_hash_table_lookup(priv->settings, name);
2727 if (setting == NULL)
2728 return default_value;
2730 g_return_val_if_fail(G_VALUE_HOLDS_STRING(&setting->value), default_value);
2732 return g_value_get_string(&setting->value);
2735 gboolean
2736 purple_account_get_bool(PurpleAccount *account, const char *name,
2737 gboolean default_value)
2739 PurpleAccountSetting *setting;
2740 PurpleAccountPrivate *priv;
2742 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), default_value);
2743 g_return_val_if_fail(name != NULL, default_value);
2745 priv = purple_account_get_instance_private(account);
2747 setting = g_hash_table_lookup(priv->settings, name);
2749 if (setting == NULL)
2750 return default_value;
2752 g_return_val_if_fail(G_VALUE_HOLDS_BOOLEAN(&setting->value), default_value);
2754 return g_value_get_boolean(&setting->value);
2758 purple_account_get_ui_int(PurpleAccount *account, const char *ui,
2759 const char *name, int default_value)
2761 PurpleAccountSetting *setting;
2762 PurpleAccountPrivate *priv;
2763 GHashTable *table;
2765 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), default_value);
2766 g_return_val_if_fail(ui != NULL, default_value);
2767 g_return_val_if_fail(name != NULL, default_value);
2769 priv = purple_account_get_instance_private(account);
2771 if ((table = g_hash_table_lookup(priv->ui_settings, ui)) == NULL)
2772 return default_value;
2774 if ((setting = g_hash_table_lookup(table, name)) == NULL)
2775 return default_value;
2777 g_return_val_if_fail(G_VALUE_HOLDS_INT(&setting->value), default_value);
2779 return g_value_get_int(&setting->value);
2782 const char *
2783 purple_account_get_ui_string(PurpleAccount *account, const char *ui,
2784 const char *name, const char *default_value)
2786 PurpleAccountSetting *setting;
2787 PurpleAccountPrivate *priv;
2788 GHashTable *table;
2790 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), default_value);
2791 g_return_val_if_fail(ui != NULL, default_value);
2792 g_return_val_if_fail(name != NULL, default_value);
2794 priv = purple_account_get_instance_private(account);
2796 if ((table = g_hash_table_lookup(priv->ui_settings, ui)) == NULL)
2797 return default_value;
2799 if ((setting = g_hash_table_lookup(table, name)) == NULL)
2800 return default_value;
2802 g_return_val_if_fail(G_VALUE_HOLDS_STRING(&setting->value), default_value);
2804 return g_value_get_string(&setting->value);
2807 gboolean
2808 purple_account_get_ui_bool(PurpleAccount *account, const char *ui,
2809 const char *name, gboolean default_value)
2811 PurpleAccountSetting *setting;
2812 PurpleAccountPrivate *priv;
2813 GHashTable *table;
2815 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), default_value);
2816 g_return_val_if_fail(ui != NULL, default_value);
2817 g_return_val_if_fail(name != NULL, default_value);
2819 priv = purple_account_get_instance_private(account);
2821 if ((table = g_hash_table_lookup(priv->ui_settings, ui)) == NULL)
2822 return default_value;
2824 if ((setting = g_hash_table_lookup(table, name)) == NULL)
2825 return default_value;
2827 g_return_val_if_fail(G_VALUE_HOLDS_BOOLEAN(&setting->value), default_value);
2829 return g_value_get_boolean(&setting->value);
2832 PurpleLog *
2833 purple_account_get_log(PurpleAccount *account, gboolean create)
2835 PurpleAccountPrivate *priv;
2837 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
2839 priv = purple_account_get_instance_private(account);
2841 if(!priv->system_log && create){
2842 PurplePresence *presence;
2843 int login_time;
2844 GDateTime *dt;
2846 presence = purple_account_get_presence(account);
2847 login_time = purple_presence_get_login_time(presence);
2848 if (login_time != 0) {
2849 dt = g_date_time_new_from_unix_local(login_time);
2850 } else {
2851 dt = g_date_time_new_now_local();
2854 priv->system_log = purple_log_new(PURPLE_LOG_SYSTEM,
2855 purple_account_get_username(account),
2856 account, NULL, dt);
2857 g_date_time_unref(dt);
2860 return priv->system_log;
2863 void
2864 purple_account_destroy_log(PurpleAccount *account)
2866 PurpleAccountPrivate *priv;
2868 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
2870 priv = purple_account_get_instance_private(account);
2872 if(priv->system_log){
2873 purple_log_free(priv->system_log);
2874 priv->system_log = NULL;
2878 void
2879 purple_account_add_buddy(PurpleAccount *account, PurpleBuddy *buddy, const char *message)
2881 PurpleProtocol *protocol = NULL;
2882 PurpleConnection *gc;
2884 g_return_if_fail(PURPLE_IS_ACCOUNT(account));
2885 g_return_if_fail(PURPLE_IS_BUDDY(buddy));
2887 gc = purple_account_get_connection(account);
2888 if (gc != NULL)
2889 protocol = purple_connection_get_protocol(gc);
2891 if (protocol != NULL)
2892 purple_protocol_server_iface_add_buddy(protocol, gc, buddy,
2893 purple_buddy_get_group(buddy), message);
2896 void
2897 purple_account_add_buddies(PurpleAccount *account, GList *buddies, const char *message)
2899 PurpleProtocol *protocol = NULL;
2900 PurpleConnection *gc = purple_account_get_connection(account);
2902 if (gc != NULL)
2903 protocol = purple_connection_get_protocol(gc);
2905 if (protocol) {
2906 GList *cur, *groups = NULL;
2908 /* Make a list of what group each buddy is in */
2909 for (cur = buddies; cur != NULL; cur = cur->next) {
2910 PurpleBuddy *buddy = cur->data;
2911 groups = g_list_append(groups, purple_buddy_get_group(buddy));
2914 if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, add_buddies))
2915 purple_protocol_server_iface_add_buddies(protocol, gc, buddies, groups, message);
2916 else if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, add_buddy)) {
2917 GList *curb = buddies, *curg = groups;
2919 while ((curb != NULL) && (curg != NULL)) {
2920 purple_protocol_server_iface_add_buddy(protocol, gc, curb->data, curg->data, message);
2921 curb = curb->next;
2922 curg = curg->next;
2926 g_list_free(groups);
2930 void
2931 purple_account_remove_buddy(PurpleAccount *account, PurpleBuddy *buddy,
2932 PurpleGroup *group)
2934 PurpleProtocol *protocol = NULL;
2935 PurpleConnection *gc = purple_account_get_connection(account);
2937 if (gc != NULL)
2938 protocol = purple_connection_get_protocol(gc);
2940 if (protocol)
2941 purple_protocol_server_iface_remove_buddy(protocol, gc, buddy, group);
2944 void
2945 purple_account_remove_buddies(PurpleAccount *account, GList *buddies, GList *groups)
2947 PurpleProtocol *protocol = NULL;
2948 PurpleConnection *gc = purple_account_get_connection(account);
2950 if (gc != NULL)
2951 protocol = purple_connection_get_protocol(gc);
2953 if (protocol) {
2954 if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, remove_buddies))
2955 purple_protocol_server_iface_remove_buddies(protocol, gc, buddies, groups);
2956 else {
2957 GList *curb = buddies;
2958 GList *curg = groups;
2959 while ((curb != NULL) && (curg != NULL)) {
2960 purple_account_remove_buddy(account, curb->data, curg->data);
2961 curb = curb->next;
2962 curg = curg->next;
2968 void
2969 purple_account_remove_group(PurpleAccount *account, PurpleGroup *group)
2971 PurpleProtocol *protocol = NULL;
2972 PurpleConnection *gc = purple_account_get_connection(account);
2974 if (gc != NULL)
2975 protocol = purple_connection_get_protocol(gc);
2977 if (protocol)
2978 purple_protocol_server_iface_remove_group(protocol, gc, group);
2981 void
2982 purple_account_change_password(PurpleAccount *account, const char *orig_pw,
2983 const char *new_pw)
2985 PurpleProtocol *protocol = NULL;
2986 PurpleConnection *gc = purple_account_get_connection(account);
2988 purple_account_set_password(account, new_pw, NULL, NULL);
2990 if (gc != NULL)
2991 protocol = purple_connection_get_protocol(gc);
2993 if (protocol)
2994 purple_protocol_server_iface_change_passwd(protocol, gc, orig_pw, new_pw);
2997 gboolean purple_account_supports_offline_message(PurpleAccount *account, PurpleBuddy *buddy)
2999 PurpleConnection *gc;
3000 PurpleProtocol *protocol = NULL;
3002 g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), FALSE);
3003 g_return_val_if_fail(PURPLE_IS_BUDDY(buddy), FALSE);
3005 gc = purple_account_get_connection(account);
3006 if (gc == NULL)
3007 return FALSE;
3009 protocol = purple_connection_get_protocol(gc);
3011 if (!protocol)
3012 return FALSE;
3013 return purple_protocol_client_iface_offline_message(protocol, buddy);
3016 const PurpleConnectionErrorInfo *
3017 purple_account_get_current_error(PurpleAccount *account)
3019 PurpleAccountPrivate *priv = purple_account_get_instance_private(account);
3021 return priv->current_error;
3024 void
3025 purple_account_clear_current_error(PurpleAccount *account)
3027 _purple_account_set_current_error(account, NULL);