Sat, 26 Mar 2005 23:25:18 +0000
[gaim-migrate @ 12342]
More big changes, yay.
I combined gaim_connection_new and gaim_connection_connect. Earlier
today I realized that it's dumb to have a GaimConnection that isn't
connected.
I'm about to combine gaim_connection_disconnect and
gaim_connection_destroy, as well.
I added a "password" field to GaimConnection.
It holds the password used to login a specific GaimConnection. Now,
when "remember password" is false, account->password is NEVER set.
When the user tries to sign on and Gaim prompts for the password,
it goes directly into the GaimConnection.
--- a/plugins/tcl/tcl_cmds.c Sat Mar 26 21:22:53 2005 +0000 +++ b/plugins/tcl/tcl_cmds.c Sat Mar 26 23:25:18 2005 +0000 @@ -119,10 +119,9 @@ error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account); if (error || !tcl_validate_account(account, interp)) return TCL_ERROR; - if (gaim_account_is_connected(account)) - Tcl_SetIntObj(result, (int)gaim_account_get_connection(account)); - else - Tcl_SetIntObj(result, (int)gaim_account_connect(account)); + if (!gaim_account_is_connected(account)) + gaim_account_connect(account); + Tcl_SetIntObj(result, (int)gaim_account_get_connection(account)); break; case CMD_ACCOUNT_CONNECTION: if (objc != 3) {
--- a/src/account.c Sat Mar 26 21:22:53 2005 +0000 +++ b/src/account.c Sat Mar 26 23:25:18 2005 +0000 @@ -741,44 +741,83 @@ g_free(account); } -GaimConnection * +void gaim_account_register(GaimAccount *account) { - GaimConnection *gc; + g_return_if_fail(account != NULL); - g_return_val_if_fail(account != NULL, NULL); + gaim_debug_info("account", "Registering account %s\n", + gaim_account_get_username(account)); - if (gaim_account_get_connection(account) != NULL) - return NULL; + gaim_connection_new(account, TRUE, NULL); +} - gc = gaim_connection_new(account); +static void +request_password_ok_cb(GaimAccount *account, const char *entry) +{ + if (!entry || !*entry) + { + gaim_notify_error(account, NULL, _("Password is required to sign on."), NULL); + return; + } - gaim_debug_info("account", "Registering account %p. gc = %p\n", - account, gc); + if (gaim_account_get_remember_password(account)) + gaim_account_set_password(account, entry); - gaim_connection_register(gc); - - return gc; + gaim_connection_new(account, FALSE, entry); } -GaimConnection * +static void +request_password(GaimAccount *account) +{ + gchar *primary; + gchar *escaped; + const gchar *username; + + username = gaim_account_get_username(account); + escaped = g_markup_escape_text(username, strlen(username)); + primary = g_strdup_printf(_("Enter password for %s (%s)"), escaped, + gaim_account_get_protocol_name(account)); + gaim_request_input(account, _("Enter Password"), primary, NULL, NULL, + FALSE, TRUE, NULL, + _("OK"), G_CALLBACK(request_password_ok_cb), + _("Cancel"), NULL, account); + g_free(primary); + g_free(escaped); +} + +void gaim_account_connect(GaimAccount *account) { - GaimConnection *gc; + GaimPlugin *prpl; + GaimPluginProtocolInfo *prpl_info; + const char *password; + + g_return_if_fail(account != NULL); - g_return_val_if_fail(account != NULL, NULL); + gaim_debug_info("account", "Connecting to account %s\n", + gaim_account_get_username(account)); - if (gaim_account_get_connection(account) != NULL) - return NULL; + prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); + if (prpl == NULL) + { + gchar *message; - gc = gaim_connection_new(account); + message = g_strdup_printf(_("Missing protocol plugin for %s"), + gaim_account_get_username(account)); + gaim_notify_error(NULL, _("Connection Error"), message, NULL); + g_free(message); + return; + } - gaim_debug_info("account", "Connecting to account %p. gc = %p\n", - account, gc); - - gaim_connection_connect(gc); - - return gc; + prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); + password = gaim_account_get_password(account); + if ((password == NULL) && + !(prpl_info->options & OPT_PROTO_NO_PASSWORD) && + !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL)) + request_password(account); + else + gaim_connection_new(account, FALSE, password); } void @@ -940,9 +979,7 @@ { g_return_if_fail(account != NULL); - if (account->username != NULL) - g_free(account->username); - + g_free(account->username); account->username = (username == NULL ? NULL : g_strdup(username)); schedule_accounts_save(); @@ -953,8 +990,11 @@ { g_return_if_fail(account != NULL); - if (account->password != NULL) - g_free(account->password); + g_free(account->password); + account->password = NULL; + + if (!gaim_account_get_remember_password(account)) + return; account->password = (password == NULL ? NULL : g_strdup(password)); @@ -966,9 +1006,7 @@ { g_return_if_fail(account != NULL); - if (account->alias != NULL) - g_free(account->alias); - + g_free(account->alias); account->alias = (alias == NULL ? NULL : g_strdup(alias)); schedule_accounts_save(); @@ -979,9 +1017,7 @@ { g_return_if_fail(account != NULL); - if (account->user_info != NULL) - g_free(account->user_info); - + g_free(account->user_info); account->user_info = (user_info == NULL ? NULL : g_strdup(user_info)); schedule_accounts_save(); @@ -992,11 +1028,9 @@ { g_return_if_fail(account != NULL); - if (account->buddy_icon != NULL) - g_free(account->buddy_icon); - + g_free(account->buddy_icon); account->buddy_icon = (icon == NULL ? NULL : g_strdup(icon)); - if (account->gc) + if (gaim_account_is_connected(account)) serv_set_buddyicon(account->gc, icon); schedule_accounts_save(); @@ -1008,9 +1042,7 @@ g_return_if_fail(account != NULL); g_return_if_fail(protocol_id != NULL); - if (account->protocol_id != NULL) - g_free(account->protocol_id); - + g_free(account->protocol_id); account->protocol_id = g_strdup(protocol_id); schedule_accounts_save();
--- a/src/account.h Sat Mar 26 21:22:53 2005 +0000 +++ b/src/account.h Sat Mar 26 23:25:18 2005 +0000 @@ -113,20 +113,15 @@ * Connects to an account. * * @param account The account to connect to. - * @param status The status the account should use when logging in. - * - * @return The gaim connection. */ -GaimConnection *gaim_account_connect(GaimAccount *account); +void gaim_account_connect(GaimAccount *account); /** * Registers an account. * * @param account The account to register. - * - * @return The gaim connection. */ -GaimConnection *gaim_account_register(GaimAccount *account); +void gaim_account_register(GaimAccount *account); /** * Disconnects from an account.
--- a/src/connection.c Sat Mar 26 21:22:53 2005 +0000 +++ b/src/connection.c Sat Mar 26 23:25:18 2005 +0000 @@ -23,6 +23,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "internal.h" +#include "account.h" #include "blist.h" #include "connection.h" #include "debug.h" @@ -40,21 +41,73 @@ static int connections_handle; -GaimConnection * -gaim_connection_new(GaimAccount *account) +void +gaim_connection_new(GaimAccount *account, gboolean regist, const char *password) { GaimConnection *gc; + GaimPlugin *prpl; + GaimPluginProtocolInfo *prpl_info; - g_return_val_if_fail(account != NULL, NULL); + g_return_if_fail(account != NULL); + + prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); + + if (prpl != NULL) + prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); + else { + gchar *message; + + message = g_strdup_printf(_("Missing protocol plugin for %s"), + gaim_account_get_username(account)); + gaim_notify_error(NULL, regist ? _("Registration Error") : + _("Connection Error"), message, NULL); + g_free(message); + return; + } + + if (regist) + { + if (prpl_info->register_user == NULL) + return; + } + else + { + if ((password == NULL) && + !(prpl_info->options & OPT_PROTO_NO_PASSWORD) && + !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL)) + { + gaim_debug_error("connection", "Can not connect to account %s without " + "a password.\n", gaim_account_get_username(account)); + return; + } + } gc = g_new0(GaimConnection, 1); - - gc->prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); - + gc->prpl = prpl; + gc->password = g_strdup(password); gaim_connection_set_account(gc, account); + gaim_connection_set_state(gc, GAIM_CONNECTING); + connections = g_list_append(connections, gc); gaim_account_set_connection(account, gc); - return gc; + gaim_signal_emit(gaim_connections_get_handle(), "signing-on", gc); + + if (regist) + { + gaim_debug_info("connection", "Registering. gc = %p\n", gc); + + /* set this so we don't auto-reconnect after registering */ + gc->wants_to_die = TRUE; + + prpl_info->register_user(account); + } + else + { + gaim_debug_info("connection", "Connecting. gc = %p\n", gc); + + gaim_signal_emit(gaim_accounts_get_handle(), "account-connecting", account); + prpl_info->login(account, gaim_account_get_active_status(account)); + } } void @@ -75,6 +128,9 @@ account = gaim_connection_get_account(gc); gaim_account_set_connection(account, NULL); + if (gc->password != NULL) + g_free(gc->password); + if (gc->display_name != NULL) g_free(gc->display_name); @@ -84,126 +140,6 @@ g_free(gc); } -static void -request_pass_ok_cb(GaimAccount *account, const char *entry) -{ - gaim_account_set_password(account, (*entry != '\0') ? entry : NULL); - - gaim_account_connect(account); -} - -void -gaim_connection_register(GaimConnection *gc) -{ - GaimAccount *account; - GaimConnectionUiOps *ops; - GaimPluginProtocolInfo *prpl_info = NULL; - - g_return_if_fail(gc != NULL); - - gaim_debug_info("connection", "Registering. gc = %p\n", gc); - - ops = gaim_connections_get_ui_ops(); - - if (gc->prpl != NULL) - prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); - else - { - gchar *message = g_strdup_printf(_("Missing protocol plugin for %s"), - gaim_account_get_username(gaim_connection_get_account(gc))); - - gaim_debug_error("connection", "Could not get prpl info for %p\n", gc); - gaim_notify_error(NULL, _("Registration Error"), - message, NULL); - g_free(message); - return; - } - - if (prpl_info->register_user == NULL) - return; - - account = gaim_connection_get_account(gc); - - if (gaim_connection_get_state(gc) != GAIM_DISCONNECTED) - return; - - gaim_connection_set_state(gc, GAIM_CONNECTING); - - connections = g_list_append(connections, gc); - - gaim_signal_emit(gaim_connections_get_handle(), "signing-on", gc); - - /* set this so we don't auto-reconnect after registering */ - gc->wants_to_die = TRUE; - - gaim_debug_info("connection", "Calling register_user\n"); - - prpl_info->register_user(account); -} - - -void -gaim_connection_connect(GaimConnection *gc) -{ - GaimAccount *account; - GaimPluginProtocolInfo *prpl_info = NULL; - GaimStatus *status; - - g_return_if_fail(gc != NULL); - - gaim_debug_info("connection", "Connecting. gc = %p\n", gc); - - if (gc->prpl != NULL) - prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); - else { - gchar *message = g_strdup_printf(_("Missing protocol plugin for %s"), - gaim_account_get_username(gaim_connection_get_account(gc))); - - gaim_debug_error("connection", "Could not get prpl info for %p\n", gc); - gaim_notify_error(NULL, _("Connection Error"), message, NULL); - g_free(message); - return; - } - - account = gaim_connection_get_account(gc); - - if (gaim_connection_get_state(gc) != GAIM_DISCONNECTED) - return; - - if (!(prpl_info->options & OPT_PROTO_NO_PASSWORD) && - !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL) && - gaim_account_get_password(account) == NULL) { - gchar *primary; - gchar *escaped; - const gchar *username = gaim_account_get_username(account); - - gaim_debug_info("connection", "Requesting password\n"); - gaim_connection_destroy(gc); - escaped = g_markup_escape_text(username, strlen(username)); - primary = g_strdup_printf(_("Enter password for %s (%s)"), escaped, - gaim_account_get_protocol_name(account)); - gaim_request_input(gc, _("Enter Password"), primary, NULL, NULL, - FALSE, TRUE, NULL, - _("OK"), G_CALLBACK(request_pass_ok_cb), - _("Cancel"), NULL, account); - g_free(primary); - g_free(escaped); - - return; - } - - gaim_connection_set_state(gc, GAIM_CONNECTING); - - connections = g_list_append(connections, gc); - - gaim_signal_emit(gaim_connections_get_handle(), "signing-on", gc); - - gaim_debug_info("connection", "Calling serv_login\n"); - - status = gaim_account_get_active_status(account); - serv_login(account, status); -} - void gaim_connection_disconnect(GaimConnection *gc) { @@ -256,9 +192,6 @@ gaim_notify_close_with_handle(gc); } - if (!gaim_account_get_remember_password(account)) - gaim_account_set_password(account, NULL); - gaim_connection_destroy(gc); } @@ -431,6 +364,14 @@ } const char * +gaim_connection_get_password(const GaimConnection *gc) +{ + g_return_val_if_fail(gc != NULL, NULL); + + return gc->password; +} + +const char * gaim_connection_get_display_name(const GaimConnection *gc) { g_return_val_if_fail(gc != NULL, NULL);
--- a/src/connection.h Sat Mar 26 21:22:53 2005 +0000 +++ b/src/connection.h Sat Mar 26 23:25:18 2005 +0000 @@ -79,6 +79,7 @@ GaimConnectionState state; /**< The connection state. */ GaimAccount *account; /**< The account being connected to. */ + char *password; /**< The password used. */ int inpa; /**< The input watcher. */ GSList *buddy_chats; /**< A list of active chats. */ @@ -113,44 +114,35 @@ /*@{*/ /** - * Creates a connection to the specified account. + * This function should only be called by gaim_connection_connect() + * in account.c. If you're trying to sign on an account, use that + * function instead. + * + * Creates a connection to the specified account and either connects + * or attempts to register a new account. If you are logging in, + * the connection uses the current active status for this account. + * So if you want to sign on as "away," for example, you need to + * have called gaim_account_set_status(account, "away"). + * (And this will call gaim_account_connect() automatically). * * @param account The account the connection should be connecting to. - * - * @return The gaim connection. + * @param register Whether we are registering a new account or just + * trying to do a normal signon. + * @param password The password to use. */ -GaimConnection *gaim_connection_new(GaimAccount *account); - -/** - * Destroys and closes a gaim connection. - * - * @param gc The gaim connection to destroy. - */ -void gaim_connection_destroy(GaimConnection *gc); +void gaim_connection_new(GaimAccount *account, gboolean regist, + const char *password); /** * This function should only be called by gaim_connection_connect() * in account.c. If you're trying to sign on an account, use that * function instead. * - * Logs in to this connection. The connection uses the current - * active status in the account as the initial status. So if - * you want to sign on as "away," for example, you need to - * have called gaim_account_set_status(account, "away"). - * (And generally this has the effect of also signin on). - * - * @param gc The connection to log in. + * Disconnects and destroys a GaimConnection. * - * @see gaim_connection_disconnect() + * @param gc The gaim connection to destroy. */ -void gaim_connection_connect(GaimConnection *gc); - -/** - * Registers a connection. - * - * @param gc The connection to register. - */ -void gaim_connection_register(GaimConnection *gc); +void gaim_connection_destroy(GaimConnection *gc); /** * This function should only be called by gaim_connection_disconnect() @@ -216,6 +208,15 @@ GaimAccount *gaim_connection_get_account(const GaimConnection *gc); /** + * Returns the connection's password.