Mercurial > pidgin
annotate libgaim/connection.c @ 14489:de8aea7e9f07
[gaim-migrate @ 17208]
I copied parts of libgaim/plugins/perl/Makefile.am
I have no idea if these changes are going to screw up the gtk perl
stuff, but at least it doesn't break "make dist"
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sat, 09 Sep 2006 21:02:31 +0000 (2006-09-09) |
parents | af856551902b |
children | f283ebe419e4 |
rev | line source |
---|---|
14192 | 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 { | |
14483
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
64 GaimPluginProtocolInfo *prpl_info = NULL; |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
65 |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
66 if (gc != NULL && gc->prpl != NULL) |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
67 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
68 |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
69 if (!prpl_info || !prpl_info->keepalive) |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
70 return; |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
71 |
14192 | 72 if (on && !gc->keepalive) |
73 { | |
74 gaim_debug_info("connection", "Activating keepalive.\n"); | |
75 gc->keepalive = gaim_timeout_add(30000, send_keepalive, gc); | |
76 } | |
77 else if (!on && gc->keepalive > 0) | |
78 { | |
79 gaim_debug_info("connection", "Deactivating keepalive.\n"); | |
80 gaim_timeout_remove(gc->keepalive); | |
81 gc->keepalive = 0; | |
82 } | |
83 } | |
84 | |
85 void | |
86 gaim_connection_new(GaimAccount *account, gboolean regist, const char *password) | |
87 { | |
88 GaimConnection *gc; | |
89 GaimPlugin *prpl; | |
90 GaimPluginProtocolInfo *prpl_info; | |
91 | |
92 g_return_if_fail(account != NULL); | |
93 | |
94 if (!gaim_account_is_disconnected(account)) | |
95 return; | |
96 | |
97 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); | |
98 | |
99 if (prpl != NULL) | |
100 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
101 else { | |
102 gchar *message; | |
103 | |
104 message = g_strdup_printf(_("Missing protocol plugin for %s"), | |
105 gaim_account_get_username(account)); | |
106 gaim_notify_error(NULL, regist ? _("Registration Error") : | |
107 _("Connection Error"), message, NULL); | |
108 g_free(message); | |
109 return; | |
110 } | |
111 | |
112 if (regist) | |
113 { | |
114 if (prpl_info->register_user == NULL) | |
115 return; | |
116 } | |
117 else | |
118 { | |
119 if (((password == NULL) || (*password == '\0')) && | |
120 !(prpl_info->options & OPT_PROTO_NO_PASSWORD) && | |
121 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL)) | |
122 { | |
123 gaim_debug_error("connection", "Can not connect to account %s without " | |
124 "a password.\n", gaim_account_get_username(account)); | |
125 return; | |
126 } | |
127 } | |
128 | |
129 gc = g_new0(GaimConnection, 1); | |
130 GAIM_DBUS_REGISTER_POINTER(gc, GaimConnection); | |
131 | |
132 gc->prpl = prpl; | |
133 if ((password != NULL) && (*password != '\0')) | |
134 gc->password = g_strdup(password); | |
135 gaim_connection_set_account(gc, account); | |
136 gaim_connection_set_state(gc, GAIM_CONNECTING); | |
137 connections = g_list_append(connections, gc); | |
138 gaim_account_set_connection(account, gc); | |
139 | |
140 gaim_signal_emit(gaim_connections_get_handle(), "signing-on", gc); | |
141 | |
142 if (regist) | |
143 { | |
144 gaim_debug_info("connection", "Registering. gc = %p\n", gc); | |
145 | |
146 /* set this so we don't auto-reconnect after registering */ | |
147 gc->wants_to_die = TRUE; | |
148 | |
149 prpl_info->register_user(account); | |
150 } | |
151 else | |
152 { | |
153 gaim_debug_info("connection", "Connecting. gc = %p\n", gc); | |
154 | |
155 gaim_signal_emit(gaim_accounts_get_handle(), "account-connecting", account); | |
156 prpl_info->login(account); | |
157 } | |
158 } | |
159 | |
160 void | |
161 gaim_connection_destroy(GaimConnection *gc) | |
162 { | |
163 GaimAccount *account; | |
164 #if 0 | |
165 GList *wins; | |
166 #endif | |
167 GaimPluginProtocolInfo *prpl_info = NULL; | |
168 gboolean remove = FALSE; | |
169 | |
170 g_return_if_fail(gc != NULL); | |
171 | |
172 account = gaim_connection_get_account(gc); | |
173 | |
174 gaim_debug_info("connection", "Disconnecting connection %p\n", gc); | |
175 | |
176 if (gaim_connection_get_state(gc) != GAIM_CONNECTING) | |
177 remove = TRUE; | |
178 | |
179 gaim_signal_emit(gaim_connections_get_handle(), "signing-off", gc); | |
180 | |
181 while (gc->buddy_chats) | |
182 { | |
183 GaimConversation *b = gc->buddy_chats->data; | |
184 | |
185 gc->buddy_chats = g_slist_remove(gc->buddy_chats, b); | |
186 gaim_conv_chat_left(GAIM_CONV_CHAT(b)); | |
187 } | |
188 | |
189 update_keepalive(gc, FALSE); | |
190 | |
191 if (gc->prpl != NULL) | |
192 { | |
193 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
194 | |
195 if (prpl_info->close) | |
196 (prpl_info->close)(gc); | |
197 } | |
198 | |
199 connections = g_list_remove(connections, gc); | |
200 | |
201 gaim_connection_set_state(gc, GAIM_DISCONNECTED); | |
202 | |
203 if (remove) | |
204 gaim_blist_remove_account(account); | |
205 | |
206 gaim_signal_emit(gaim_connections_get_handle(), "signed-off", gc); | |
207 | |
208 #if 0 | |
209 /* see comment later in file on if 0'd same code */ | |
210 /* | |
211 * XXX This is a hack! Remove this and replace it with a better event | |
212 * notification system. | |
213 */ | |
214 for (wins = gaim_get_windows(); wins != NULL; wins = wins->next) { | |
215 GaimConvWindow *win = (GaimConvWindow *)wins->data; | |
216 gaim_conversation_update(gaim_conv_window_get_conversation_at(win, 0), | |
217 GAIM_CONV_ACCOUNT_OFFLINE); | |
218 } | |
219 #endif | |
220 | |
221 gaim_request_close_with_handle(gc); | |
222 gaim_notify_close_with_handle(gc); | |
223 | |
224 gaim_debug_info("connection", "Destroying connection %p\n", gc); | |
225 | |
226 gaim_account_set_connection(account, NULL); | |
227 | |
228 g_free(gc->password); | |
229 g_free(gc->display_name); | |
230 | |
231 if (gc->disconnect_timeout) | |
232 gaim_timeout_remove(gc->disconnect_timeout); | |
233 | |
234 GAIM_DBUS_UNREGISTER_POINTER(gc); | |
235 g_free(gc); | |
236 } | |
237 | |
238 /* | |
239 * d:)->-< | |
240 * | |
241 * d:O-\-< | |
242 * | |
243 * d:D-/-< | |
244 * | |
245 * d8D->-< DANCE! | |
246 */ | |
247 | |
248 void | |
249 gaim_connection_set_state(GaimConnection *gc, GaimConnectionState state) | |
250 { | |
251 GaimConnectionUiOps *ops; | |
252 | |
253 g_return_if_fail(gc != NULL); | |
254 | |
255 if (gc->state == state) | |
256 return; | |
257 | |
258 gc->state = state; | |
259 | |
260 ops = gaim_connections_get_ui_ops(); | |
261 | |
262 if (gc->state == GAIM_CONNECTING) { | |
263 connections_connecting = g_list_append(connections_connecting, gc); | |
264 } | |
265 else { | |
266 connections_connecting = g_list_remove(connections_connecting, gc); | |
267 } | |
268 | |
269 if (gc->state == GAIM_CONNECTED) { | |
270 GaimAccount *account; | |
271 GaimPresence *presence; | |
272 | |
273 account = gaim_connection_get_account(gc); | |
274 presence = gaim_account_get_presence(account); | |
275 | |
276 /* Set the time the account came online */ | |
277 gaim_presence_set_login_time(presence, time(NULL)); | |
278 | |
279 if (gaim_prefs_get_bool("/core/logging/log_system")) | |
280 { | |
281 GaimLog *log = gaim_account_get_log(account, TRUE); | |
282 | |
283 if (log != NULL) | |
284 { | |
285 char *msg = g_strdup_printf(_("+++ %s signed on"), | |
286 gaim_account_get_username(account)); | |
287 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
288 gaim_account_get_username(account), | |
289 gaim_presence_get_login_time(presence), | |
290 msg); | |
291 g_free(msg); | |
292 } | |
293 } | |
294 | |
295 if (ops != NULL && ops->connected != NULL) | |
296 ops->connected(gc); | |
297 | |
298 gaim_blist_add_account(account); | |
299 | |
300 gaim_signal_emit(gaim_connections_get_handle(), "signed-on", gc); | |
301 | |
302 serv_set_permit_deny(gc); | |
303 | |
304 update_keepalive(gc, TRUE); | |
305 | |
306 if (gaim_account_get_user_info(account) != NULL) | |
307 serv_set_info(gc, gaim_account_get_user_info(account)); | |
308 } | |
309 else if (gc->state == GAIM_DISCONNECTED) { | |
310 GaimAccount *account = gaim_connection_get_account(gc); | |
311 | |
312 if (gaim_prefs_get_bool("/core/logging/log_system")) | |
313 { | |
314 GaimLog *log = gaim_account_get_log(account, FALSE); | |
315 | |
316 if (log != NULL) | |
317 { | |
318 char *msg = g_strdup_printf(_("+++ %s signed off"), | |
319 gaim_account_get_username(account)); | |
320 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
321 gaim_account_get_username(account), time(NULL), | |
322 msg); | |
323 g_free(msg); | |
324 } | |
325 } | |
326 | |
327 gaim_account_destroy_log(account); | |
328 | |
329 if (ops != NULL && ops->disconnected != NULL) | |
330 ops->disconnected(gc); | |
331 } | |
332 } | |
333 | |
334 void | |
335 gaim_connection_set_account(GaimConnection *gc, GaimAccount *account) | |
336 { | |
337 g_return_if_fail(gc != NULL); | |
338 g_return_if_fail(account != NULL); | |
339 | |
340 gc->account = account; | |
341 } | |
342 | |
343 void | |
344 gaim_connection_set_display_name(GaimConnection *gc, const char *name) | |
345 { | |
346 g_return_if_fail(gc != NULL); | |
347 | |
348 g_free(gc->display_name); | |
349 gc->display_name = g_strdup(name); | |
350 } | |
351 | |
352 GaimConnectionState | |
353 gaim_connection_get_state(const GaimConnection *gc) | |
354 { | |
355 g_return_val_if_fail(gc != NULL, GAIM_DISCONNECTED); | |
356 | |
357 return gc->state; | |
358 } | |
359 | |
360 GaimAccount * | |
361 gaim_connection_get_account(const GaimConnection *gc) | |
362 { | |
363 g_return_val_if_fail(gc != NULL, NULL); | |
364 | |
365 return gc->account; | |
366 } | |
367 | |
368 const char * | |
369 gaim_connection_get_password(const GaimConnection *gc) | |
370 { | |
371 g_return_val_if_fail(gc != NULL, NULL); | |
372 | |
373 return gc->password; | |
374 } | |
375 | |
376 const char * | |
377 gaim_connection_get_display_name(const GaimConnection *gc) | |
378 { | |
379 g_return_val_if_fail(gc != NULL, NULL); | |
380 | |
381 return gc->display_name; | |
382 } | |
383 | |
384 void | |
385 gaim_connection_update_progress(GaimConnection *gc, const char *text, | |
386 size_t step, size_t count) | |
387 { | |
388 GaimConnectionUiOps *ops; | |
389 | |
390 g_return_if_fail(gc != NULL); | |
391 g_return_if_fail(text != NULL); | |
392 g_return_if_fail(step < count); | |
393 g_return_if_fail(count > 1); | |
394 | |
395 ops = gaim_connections_get_ui_ops(); | |
396 | |
397 if (ops != NULL && ops->connect_progress != NULL) | |
398 ops->connect_progress(gc, text, step, count); | |
399 } | |
400 | |
401 void | |
402 gaim_connection_notice(GaimConnection *gc, const char *text) | |
403 { | |
404 GaimConnectionUiOps *ops; | |
405 | |
406 g_return_if_fail(gc != NULL); | |
407 g_return_if_fail(text != NULL); | |
408 | |
409 ops = gaim_connections_get_ui_ops(); | |
410 | |
411 if (ops != NULL && ops->notice != NULL) | |
412 ops->notice(gc, text); | |
413 } | |
414 | |
415 static gboolean | |
416 gaim_connection_disconnect_cb(gpointer data) | |
417 { | |
418 GaimAccount *account = data; | |
419 char *password = g_strdup(gaim_account_get_password(account)); | |
420 gaim_account_disconnect(account); | |
421 gaim_account_set_password(account, password); | |
422 g_free(password); | |
423 return FALSE; | |
424 } | |
425 | |
426 void | |
427 gaim_connection_error(GaimConnection *gc, const char *text) | |
428 { | |
429 GaimConnectionUiOps *ops; | |
430 | |
431 g_return_if_fail(gc != NULL); | |
432 g_return_if_fail(GAIM_CONNECTION_IS_VALID(gc)); | |
433 g_return_if_fail(text != NULL); | |
434 | |
435 /* If we've already got one error, we don't need any more */ | |
436 if (gc->disconnect_timeout) | |
437 return; | |
438 | |
439 ops = gaim_connections_get_ui_ops(); | |
440 | |
441 if (ops != NULL) { | |
442 if (ops->report_disconnect != NULL) | |
443 ops->report_disconnect(gc, text); | |
444 } | |
445 | |
446 gc->disconnect_timeout = gaim_timeout_add(0, gaim_connection_disconnect_cb, | |
447 gaim_connection_get_account(gc)); | |
448 } | |
449 | |
450 void | |
451 gaim_connections_disconnect_all(void) | |
452 { | |
453 GList *l; | |
454 GaimConnection *gc; | |
455 | |
456 while ((l = gaim_connections_get_all()) != NULL) { | |
457 gc = l->data; | |
458 gc->wants_to_die = TRUE; | |
459 gaim_account_disconnect(gc->account); | |
460 } | |
461 } | |
462 | |
463 GList * | |
464 gaim_connections_get_all(void) | |
465 { | |
466 return connections; | |
467 } | |
468 | |
469 GList * | |
470 gaim_connections_get_connecting(void) | |
471 { | |
472 return connections_connecting; | |
473 } | |
474 | |
475 void | |
476 gaim_connections_set_ui_ops(GaimConnectionUiOps *ops) | |
477 { | |
478 connection_ui_ops = ops; | |
479 } | |
480 | |
481 GaimConnectionUiOps * | |
482 gaim_connections_get_ui_ops(void) | |
483 { | |
484 return connection_ui_ops; | |
485 } | |
486 | |
487 void | |
488 gaim_connections_init(void) | |
489 { | |
490 void *handle = gaim_connections_get_handle(); | |
491 | |
492 gaim_signal_register(handle, "signing-on", | |
493 gaim_marshal_VOID__POINTER, NULL, 1, | |
494 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
495 GAIM_SUBTYPE_CONNECTION)); | |
496 | |
497 gaim_signal_register(handle, "signed-on", | |
498 gaim_marshal_VOID__POINTER, NULL, 1, | |
499 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
500 GAIM_SUBTYPE_CONNECTION)); | |
501 | |
502 gaim_signal_register(handle, "signing-off", | |
503 gaim_marshal_VOID__POINTER, NULL, 1, | |
504 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
505 GAIM_SUBTYPE_CONNECTION)); | |
506 | |
507 gaim_signal_register(handle, "signed-off", | |
508 gaim_marshal_VOID__POINTER, NULL, 1, | |
509 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
510 GAIM_SUBTYPE_CONNECTION)); | |
511 } | |
512 | |
513 void | |
514 gaim_connections_uninit(void) | |
515 { | |
516 gaim_signals_unregister_by_instance(gaim_connections_get_handle()); | |
517 } | |
518 | |
519 void * | |
520 gaim_connections_get_handle(void) | |
521 { | |
522 return &connections_handle; | |
523 } |