comparison libgaim/connection.c @ 14192:60b1bc8dbf37

[gaim-migrate @ 16863] Renamed 'core' to 'libgaim' committer: Tailor Script <tailor@pidgin.im>
author Evan Schoenberg <evan.s@dreskin.net>
date Sat, 19 Aug 2006 01:50:10 +0000
parents
children af856551902b
comparison
equal deleted inserted replaced
14191:009db0b357b5 14192:60b1bc8dbf37
1 /**
2 * @file connection.c Connection API
3 * @ingroup core
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
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
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 #include "internal.h"
26 #include "account.h"
27 #include "blist.h"
28 #include "connection.h"
29 #include "dbus-maybe.h"
30 #include "debug.h"
31 #include "gaim.h"
32 #include "log.h"
33 #include "notify.h"
34 #include "prefs.h"
35 #include "request.h"
36 #include "server.h"
37 #include "signals.h"
38 #include "util.h"
39
40 static GList *connections = NULL;
41 static GList *connections_connecting = NULL;
42 static GaimConnectionUiOps *connection_ui_ops = NULL;
43
44 static int connections_handle;
45
46 static gboolean
47 send_keepalive(gpointer data)
48 {
49 GaimConnection *gc = data;
50 GaimPluginProtocolInfo *prpl_info = NULL;
51
52 if (gc != NULL && gc->prpl != NULL)
53 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
54
55 if (prpl_info && prpl_info->keepalive)
56 prpl_info->keepalive(gc);
57
58 return TRUE;
59 }
60
61 static void
62 update_keepalive(GaimConnection *gc, gboolean on)
63 {
64 if (on && !gc->keepalive)
65 {
66 gaim_debug_info("connection", "Activating keepalive.\n");
67 gc->keepalive = gaim_timeout_add(30000, send_keepalive, gc);
68 }
69 else if (!on && gc->keepalive > 0)
70 {
71 gaim_debug_info("connection", "Deactivating keepalive.\n");
72 gaim_timeout_remove(gc->keepalive);
73 gc->keepalive = 0;
74 }
75 }
76
77 void
78 gaim_connection_new(GaimAccount *account, gboolean regist, const char *password)
79 {
80 GaimConnection *gc;
81 GaimPlugin *prpl;
82 GaimPluginProtocolInfo *prpl_info;
83
84 g_return_if_fail(account != NULL);
85
86 if (!gaim_account_is_disconnected(account))
87 return;
88
89 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account));
90
91 if (prpl != NULL)
92 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl);
93 else {
94 gchar *message;
95
96 message = g_strdup_printf(_("Missing protocol plugin for %s"),
97 gaim_account_get_username(account));
98 gaim_notify_error(NULL, regist ? _("Registration Error") :
99 _("Connection Error"), message, NULL);
100 g_free(message);
101 return;
102 }
103
104 if (regist)
105 {
106 if (prpl_info->register_user == NULL)
107 return;
108 }
109 else
110 {
111 if (((password == NULL) || (*password == '\0')) &&
112 !(prpl_info->options & OPT_PROTO_NO_PASSWORD) &&
113 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL))
114 {
115 gaim_debug_error("connection", "Can not connect to account %s without "
116 "a password.\n", gaim_account_get_username(account));
117 return;
118 }
119 }
120
121 gc = g_new0(GaimConnection, 1);
122 GAIM_DBUS_REGISTER_POINTER(gc, GaimConnection);
123
124 gc->prpl = prpl;
125 if ((password != NULL) && (*password != '\0'))
126 gc->password = g_strdup(password);
127 gaim_connection_set_account(gc, account);
128 gaim_connection_set_state(gc, GAIM_CONNECTING);
129 connections = g_list_append(connections, gc);
130 gaim_account_set_connection(account, gc);
131
132 gaim_signal_emit(gaim_connections_get_handle(), "signing-on", gc);
133
134 if (regist)
135 {
136 gaim_debug_info("connection", "Registering. gc = %p\n", gc);
137
138 /* set this so we don't auto-reconnect after registering */
139 gc->wants_to_die = TRUE;
140
141 prpl_info->register_user(account);
142 }
143 else
144 {
145 gaim_debug_info("connection", "Connecting. gc = %p\n", gc);
146
147 gaim_signal_emit(gaim_accounts_get_handle(), "account-connecting", account);
148 prpl_info->login(account);
149 }
150 }
151
152 void
153 gaim_connection_destroy(GaimConnection *gc)
154 {
155 GaimAccount *account;
156 #if 0
157 GList *wins;
158 #endif
159 GaimPluginProtocolInfo *prpl_info = NULL;
160 gboolean remove = FALSE;
161
162 g_return_if_fail(gc != NULL);
163
164 account = gaim_connection_get_account(gc);
165
166 gaim_debug_info("connection", "Disconnecting connection %p\n", gc);
167
168 if (gaim_connection_get_state(gc) != GAIM_CONNECTING)
169 remove = TRUE;
170
171 gaim_signal_emit(gaim_connections_get_handle(), "signing-off", gc);
172
173 while (gc->buddy_chats)
174 {
175 GaimConversation *b = gc->buddy_chats->data;
176
177 gc->buddy_chats = g_slist_remove(gc->buddy_chats, b);
178 gaim_conv_chat_left(GAIM_CONV_CHAT(b));
179 }
180
181 update_keepalive(gc, FALSE);
182
183 if (gc->prpl != NULL)
184 {
185 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
186
187 if (prpl_info->close)
188 (prpl_info->close)(gc);
189 }
190
191 connections = g_list_remove(connections, gc);
192
193 gaim_connection_set_state(gc, GAIM_DISCONNECTED);
194
195 if (remove)
196 gaim_blist_remove_account(account);
197
198 gaim_signal_emit(gaim_connections_get_handle(), "signed-off", gc);
199
200 #if 0
201 /* see comment later in file on if 0'd same code */
202 /*
203 * XXX This is a hack! Remove this and replace it with a better event
204 * notification system.
205 */
206 for (wins = gaim_get_windows(); wins != NULL; wins = wins->next) {
207 GaimConvWindow *win = (GaimConvWindow *)wins->data;
208 gaim_conversation_update(gaim_conv_window_get_conversation_at(win, 0),
209 GAIM_CONV_ACCOUNT_OFFLINE);
210 }
211 #endif
212
213 gaim_request_close_with_handle(gc);
214 gaim_notify_close_with_handle(gc);
215
216 gaim_debug_info("connection", "Destroying connection %p\n", gc);
217
218 gaim_account_set_connection(account, NULL);
219
220 g_free(gc->password);
221 g_free(gc->display_name);
222
223 if (gc->disconnect_timeout)
224 gaim_timeout_remove(gc->disconnect_timeout);
225
226 GAIM_DBUS_UNREGISTER_POINTER(gc);
227 g_free(gc);
228 }
229
230 /*
231 * d:)->-<
232 *
233 * d:O-\-<
234 *
235 * d:D-/-<
236 *
237 * d8D->-< DANCE!
238 */
239
240 void
241 gaim_connection_set_state(GaimConnection *gc, GaimConnectionState state)
242 {
243 GaimConnectionUiOps *ops;
244
245 g_return_if_fail(gc != NULL);
246
247 if (gc->state == state)
248 return;
249
250 gc->state = state;
251
252 ops = gaim_connections_get_ui_ops();
253
254 if (gc->state == GAIM_CONNECTING) {
255 connections_connecting = g_list_append(connections_connecting, gc);
256 }
257 else {
258 connections_connecting = g_list_remove(connections_connecting, gc);
259 }
260
261 if (gc->state == GAIM_CONNECTED) {
262 GaimAccount *account;
263 GaimPresence *presence;
264
265 account = gaim_connection_get_account(gc);
266 presence = gaim_account_get_presence(account);
267
268 /* Set the time the account came online */
269 gaim_presence_set_login_time(presence, time(NULL));
270
271 if (gaim_prefs_get_bool("/core/logging/log_system"))
272 {
273 GaimLog *log = gaim_account_get_log(account, TRUE);
274
275 if (log != NULL)
276 {
277 char *msg = g_strdup_printf(_("+++ %s signed on"),
278 gaim_account_get_username(account));
279 gaim_log_write(log, GAIM_MESSAGE_SYSTEM,
280 gaim_account_get_username(account),
281 gaim_presence_get_login_time(presence),
282 msg);
283 g_free(msg);
284 }
285 }
286
287 if (ops != NULL && ops->connected != NULL)
288 ops->connected(gc);
289
290 gaim_blist_add_account(account);
291
292 gaim_signal_emit(gaim_connections_get_handle(), "signed-on", gc);
293
294 serv_set_permit_deny(gc);
295
296 update_keepalive(gc, TRUE);
297
298 if (gaim_account_get_user_info(account) != NULL)
299 serv_set_info(gc, gaim_account_get_user_info(account));
300 }
301 else if (gc->state == GAIM_DISCONNECTED) {
302 GaimAccount *account = gaim_connection_get_account(gc);
303
304 if (gaim_prefs_get_bool("/core/logging/log_system"))
305 {
306 GaimLog *log = gaim_account_get_log(account, FALSE);
307
308 if (log != NULL)
309 {
310 char *msg = g_strdup_printf(_("+++ %s signed off"),
311 gaim_account_get_username(account));
312 gaim_log_write(log, GAIM_MESSAGE_SYSTEM,
313 gaim_account_get_username(account), time(NULL),
314 msg);
315 g_free(msg);
316 }
317 }
318
319 gaim_account_destroy_log(account);
320
321 if (ops != NULL && ops->disconnected != NULL)
322 ops->disconnected(gc);
323 }
324 }
325
326 void
327 gaim_connection_set_account(GaimConnection *gc, GaimAccount *account)
328 {
329 g_return_if_fail(gc != NULL);
330 g_return_if_fail(account != NULL);
331
332 gc->account = account;
333 }
334
335 void
336 gaim_connection_set_display_name(GaimConnection *gc, const char *name)
337 {
338 g_return_if_fail(gc != NULL);
339
340 g_free(gc->display_name);
341 gc->display_name = g_strdup(name);
342 }
343
344 GaimConnectionState
345 gaim_connection_get_state(const GaimConnection *gc)
346 {
347 g_return_val_if_fail(gc != NULL, GAIM_DISCONNECTED);
348
349 return gc->state;
350 }
351
352 GaimAccount *
353 gaim_connection_get_account(const GaimConnection *gc)
354 {
355 g_return_val_if_fail(gc != NULL, NULL);
356
357 return gc->account;
358 }
359
360 const char *
361 gaim_connection_get_password(const GaimConnection *gc)
362 {
363 g_return_val_if_fail(gc != NULL, NULL);
364
365 return gc->password;
366 }
367
368 const char *
369 gaim_connection_get_display_name(const GaimConnection *gc)
370 {
371 g_return_val_if_fail(gc != NULL, NULL);
372
373 return gc->display_name;
374 }
375
376 void
377 gaim_connection_update_progress(GaimConnection *gc, const char *text,
378 size_t step, size_t count)
379 {
380 GaimConnectionUiOps *ops;
381
382 g_return_if_fail(gc != NULL);
383 g_return_if_fail(text != NULL);
384 g_return_if_fail(step < count);
385 g_return_if_fail(count > 1);
386
387 ops = gaim_connections_get_ui_ops();
388
389 if (ops != NULL && ops->connect_progress != NULL)
390 ops->connect_progress(gc, text, step, count);
391 }
392
393 void
394 gaim_connection_notice(GaimConnection *gc, const char *text)
395 {
396 GaimConnectionUiOps *ops;
397
398 g_return_if_fail(gc != NULL);
399 g_return_if_fail(text != NULL);
400
401 ops = gaim_connections_get_ui_ops();
402
403 if (ops != NULL && ops->notice != NULL)
404 ops->notice(gc, text);
405 }
406
407 static gboolean
408 gaim_connection_disconnect_cb(gpointer data)
409 {
410 GaimAccount *account = data;
411 char *password = g_strdup(gaim_account_get_password(account));
412 gaim_account_disconnect(account);
413 gaim_account_set_password(account, password);
414 g_free(password);
415 return FALSE;
416 }
417
418 void
419 gaim_connection_error(GaimConnection *gc, const char *text)
420 {
421 GaimConnectionUiOps *ops;
422
423 g_return_if_fail(gc != NULL);
424 g_return_if_fail(GAIM_CONNECTION_IS_VALID(gc));
425 g_return_if_fail(text != NULL);
426
427 /* If we've already got one error, we don't need any more */
428 if (gc->disconnect_timeout)
429 return;
430
431 ops = gaim_connections_get_ui_ops();
432
433 if (ops != NULL) {
434 if (ops->report_disconnect != NULL)
435 ops->report_disconnect(gc, text);
436 }
437
438 gc->disconnect_timeout = gaim_timeout_add(0, gaim_connection_disconnect_cb,
439 gaim_connection_get_account(gc));
440 }
441
442 void
443 gaim_connections_disconnect_all(void)
444 {
445 GList *l;
446 GaimConnection *gc;
447
448 while ((l = gaim_connections_get_all()) != NULL) {
449 gc = l->data;
450 gc->wants_to_die = TRUE;
451 gaim_account_disconnect(gc->account);
452 }
453 }
454
455 GList *
456 gaim_connections_get_all(void)
457 {
458 return connections;
459 }
460
461 GList *
462 gaim_connections_get_connecting(void)
463 {
464 return connections_connecting;
465 }
466
467 void
468 gaim_connections_set_ui_ops(GaimConnectionUiOps *ops)
469 {
470 connection_ui_ops = ops;
471 }
472
473 GaimConnectionUiOps *
474 gaim_connections_get_ui_ops(void)
475 {
476 return connection_ui_ops;
477 }
478
479 void
480 gaim_connections_init(void)
481 {
482 void *handle = gaim_connections_get_handle();
483
484 gaim_signal_register(handle, "signing-on",
485 gaim_marshal_VOID__POINTER, NULL, 1,
486 gaim_value_new(GAIM_TYPE_SUBTYPE,
487 GAIM_SUBTYPE_CONNECTION));
488
489 gaim_signal_register(handle, "signed-on",
490 gaim_marshal_VOID__POINTER, NULL, 1,
491 gaim_value_new(GAIM_TYPE_SUBTYPE,
492 GAIM_SUBTYPE_CONNECTION));
493
494 gaim_signal_register(handle, "signing-off",
495 gaim_marshal_VOID__POINTER, NULL, 1,
496 gaim_value_new(GAIM_TYPE_SUBTYPE,
497 GAIM_SUBTYPE_CONNECTION));
498
499 gaim_signal_register(handle, "signed-off",
500 gaim_marshal_VOID__POINTER, NULL, 1,
501 gaim_value_new(GAIM_TYPE_SUBTYPE,
502 GAIM_SUBTYPE_CONNECTION));
503 }
504
505 void
506 gaim_connections_uninit(void)
507 {
508 gaim_signals_unregister_by_instance(gaim_connections_get_handle());
509 }
510
511 void *
512 gaim_connections_get_handle(void)
513 {
514 return &connections_handle;
515 }