comparison src/connection.c @ 10740:94cc67130789

[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. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sat, 26 Mar 2005 23:25:18 +0000
parents 55af3fa46329
children 4228b6d78506
comparison
equal deleted inserted replaced
10739:42dbc4ba1325 10740:94cc67130789
21 * You should have received a copy of the GNU General Public License 21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software 22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */ 24 */
25 #include "internal.h" 25 #include "internal.h"
26 #include "account.h"
26 #include "blist.h" 27 #include "blist.h"
27 #include "connection.h" 28 #include "connection.h"
28 #include "debug.h" 29 #include "debug.h"
29 #include "log.h" 30 #include "log.h"
30 #include "notify.h" 31 #include "notify.h"
38 static GList *connections_connecting = NULL; 39 static GList *connections_connecting = NULL;
39 static GaimConnectionUiOps *connection_ui_ops = NULL; 40 static GaimConnectionUiOps *connection_ui_ops = NULL;
40 41
41 static int connections_handle; 42 static int connections_handle;
42 43
43 GaimConnection * 44 void
44 gaim_connection_new(GaimAccount *account) 45 gaim_connection_new(GaimAccount *account, gboolean regist, const char *password)
45 { 46 {
46 GaimConnection *gc; 47 GaimConnection *gc;
47 48 GaimPlugin *prpl;
48 g_return_val_if_fail(account != NULL, NULL); 49 GaimPluginProtocolInfo *prpl_info;
50
51 g_return_if_fail(account != NULL);
52
53 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account));
54
55 if (prpl != NULL)
56 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl);
57 else {
58 gchar *message;
59
60 message = g_strdup_printf(_("Missing protocol plugin for %s"),
61 gaim_account_get_username(account));
62 gaim_notify_error(NULL, regist ? _("Registration Error") :
63 _("Connection Error"), message, NULL);
64 g_free(message);
65 return;
66 }
67
68 if (regist)
69 {
70 if (prpl_info->register_user == NULL)
71 return;
72 }
73 else
74 {
75 if ((password == NULL) &&
76 !(prpl_info->options & OPT_PROTO_NO_PASSWORD) &&
77 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL))
78 {
79 gaim_debug_error("connection", "Can not connect to account %s without "
80 "a password.\n", gaim_account_get_username(account));
81 return;
82 }
83 }
49 84
50 gc = g_new0(GaimConnection, 1); 85 gc = g_new0(GaimConnection, 1);
51 86 gc->prpl = prpl;
52 gc->prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); 87 gc->password = g_strdup(password);
53
54 gaim_connection_set_account(gc, account); 88 gaim_connection_set_account(gc, account);
89 gaim_connection_set_state(gc, GAIM_CONNECTING);
90 connections = g_list_append(connections, gc);
55 gaim_account_set_connection(account, gc); 91 gaim_account_set_connection(account, gc);
56 92
57 return gc; 93 gaim_signal_emit(gaim_connections_get_handle(), "signing-on", gc);
94
95 if (regist)
96 {
97 gaim_debug_info("connection", "Registering. gc = %p\n", gc);
98
99 /* set this so we don't auto-reconnect after registering */
100 gc->wants_to_die = TRUE;
101
102 prpl_info->register_user(account);
103 }
104 else
105 {
106 gaim_debug_info("connection", "Connecting. gc = %p\n", gc);
107
108 gaim_signal_emit(gaim_accounts_get_handle(), "account-connecting", account);
109 prpl_info->login(account, gaim_account_get_active_status(account));
110 }
58 } 111 }
59 112
60 void 113 void
61 gaim_connection_destroy(GaimConnection *gc) 114 gaim_connection_destroy(GaimConnection *gc)
62 { 115 {
73 gaim_debug_info("connection", "Destroying connection %p\n", gc); 126 gaim_debug_info("connection", "Destroying connection %p\n", gc);
74 127
75 account = gaim_connection_get_account(gc); 128 account = gaim_connection_get_account(gc);
76 gaim_account_set_connection(account, NULL); 129 gaim_account_set_connection(account, NULL);
77 130
131 if (gc->password != NULL)
132 g_free(gc->password);
133
78 if (gc->display_name != NULL) 134 if (gc->display_name != NULL)
79 g_free(gc->display_name); 135 g_free(gc->display_name);
80 136
81 if (gc->disconnect_timeout) 137 if (gc->disconnect_timeout)
82 gaim_timeout_remove(gc->disconnect_timeout); 138 gaim_timeout_remove(gc->disconnect_timeout);
83 139
84 g_free(gc); 140 g_free(gc);
85 }
86
87 static void
88 request_pass_ok_cb(GaimAccount *account, const char *entry)
89 {
90 gaim_account_set_password(account, (*entry != '\0') ? entry : NULL);
91
92 gaim_account_connect(account);
93 }
94
95 void
96 gaim_connection_register(GaimConnection *gc)
97 {
98 GaimAccount *account;
99 GaimConnectionUiOps *ops;
100 GaimPluginProtocolInfo *prpl_info = NULL;
101
102 g_return_if_fail(gc != NULL);
103
104 gaim_debug_info("connection", "Registering. gc = %p\n", gc);
105
106 ops = gaim_connections_get_ui_ops();
107
108 if (gc->prpl != NULL)
109 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
110 else
111 {
112 gchar *message = g_strdup_printf(_("Missing protocol plugin for %s"),
113 gaim_account_get_username(gaim_connection_get_account(gc)));
114
115 gaim_debug_error("connection", "Could not get prpl info for %p\n", gc);
116 gaim_notify_error(NULL, _("Registration Error"),
117 message, NULL);
118 g_free(message);
119 return;
120 }
121
122 if (prpl_info->register_user == NULL)
123 return;
124
125 account = gaim_connection_get_account(gc);
126
127 if (gaim_connection_get_state(gc) != GAIM_DISCONNECTED)
128 return;
129
130 gaim_connection_set_state(gc, GAIM_CONNECTING);
131
132 connections = g_list_append(connections, gc);
133
134 gaim_signal_emit(gaim_connections_get_handle(), "signing-on", gc);
135
136 /* set this so we don't auto-reconnect after registering */
137 gc->wants_to_die = TRUE;
138
139 gaim_debug_info("connection", "Calling register_user\n");
140
141 prpl_info->register_user(account);
142 }
143
144
145 void
146 gaim_connection_connect(GaimConnection *gc)
147 {
148 GaimAccount *account;
149 GaimPluginProtocolInfo *prpl_info = NULL;
150 GaimStatus *status;
151
152 g_return_if_fail(gc != NULL);
153
154 gaim_debug_info("connection", "Connecting. gc = %p\n", gc);
155
156 if (gc->prpl != NULL)
157 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
158 else {
159 gchar *message = g_strdup_printf(_("Missing protocol plugin for %s"),
160 gaim_account_get_username(gaim_connection_get_account(gc)));
161
162 gaim_debug_error("connection", "Could not get prpl info for %p\n", gc);
163 gaim_notify_error(NULL, _("Connection Error"), message, NULL);
164 g_free(message);
165 return;
166 }
167
168 account = gaim_connection_get_account(gc);
169
170 if (gaim_connection_get_state(gc) != GAIM_DISCONNECTED)
171 return;
172
173 if (!(prpl_info->options & OPT_PROTO_NO_PASSWORD) &&
174 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL) &&
175 gaim_account_get_password(account) == NULL) {
176 gchar *primary;
177 gchar *escaped;
178 const gchar *username = gaim_account_get_username(account);
179
180 gaim_debug_info("connection", "Requesting password\n");
181 gaim_connection_destroy(gc);
182 escaped = g_markup_escape_text(username, strlen(username));
183 primary = g_strdup_printf(_("Enter password for %s (%s)"), escaped,
184 gaim_account_get_protocol_name(account));
185 gaim_request_input(gc, _("Enter Password"), primary, NULL, NULL,
186 FALSE, TRUE, NULL,
187 _("OK"), G_CALLBACK(request_pass_ok_cb),
188 _("Cancel"), NULL, account);
189 g_free(primary);
190 g_free(escaped);
191
192 return;
193 }
194
195 gaim_connection_set_state(gc, GAIM_CONNECTING);
196
197 connections = g_list_append(connections, gc);
198
199 gaim_signal_emit(gaim_connections_get_handle(), "signing-on", gc);
200
201 gaim_debug_info("connection", "Calling serv_login\n");
202
203 status = gaim_account_get_active_status(account);
204 serv_login(account, status);
205 } 141 }
206 142
207 void 143 void
208 gaim_connection_disconnect(GaimConnection *gc) 144 gaim_connection_disconnect(GaimConnection *gc)
209 { 145 {
253 } 189 }
254 190
255 gaim_request_close_with_handle(gc); 191 gaim_request_close_with_handle(gc);
256 gaim_notify_close_with_handle(gc); 192 gaim_notify_close_with_handle(gc);
257 } 193 }
258
259 if (!gaim_account_get_remember_password(account))
260 gaim_account_set_password(account, NULL);
261 194
262 gaim_connection_destroy(gc); 195 gaim_connection_destroy(gc);
263 } 196 }
264 197
265 gboolean 198 gboolean
429 362
430 return gc->account; 363 return gc->account;
431 } 364 }
432 365
433 const char * 366 const char *
367 gaim_connection_get_password(const GaimConnection *gc)
368 {
369 g_return_val_if_fail(gc != NULL, NULL);
370
371 return gc->password;
372 }
373
374 const char *
434 gaim_connection_get_display_name(const GaimConnection *gc) 375 gaim_connection_get_display_name(const GaimConnection *gc)
435 { 376 {
436 g_return_val_if_fail(gc != NULL, NULL); 377 g_return_val_if_fail(gc != NULL, NULL);
437 378
438 return gc->display_name; 379 return gc->display_name;