Mercurial > pidgin.yaz
annotate src/connection.c @ 5740:6ec7b32ab1df
[gaim-migrate @ 6164]
Hopefully fixed an infinite loop bug on disconnect, but I'm not holding my
breath.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Wed, 04 Jun 2003 07:28:36 +0000 |
parents | 2e9f040f6b66 |
children | 1b5e6e6e80e9 |
rev | line source |
---|---|
5563 | 1 /** |
2 * @file connection.c Connection API | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
7 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org> | |
5631 | 8 * |
5563 | 9 * This program is free software; you can redistribute it and/or modify |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 */ | |
5717 | 23 |
5563 | 24 #include "connection.h" |
5717 | 25 #include "debug.h" |
26 #include "gaim.h" | |
5563 | 27 |
28 static GList *connections = NULL; | |
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
29 static GList *connections_connecting = NULL; |
5563 | 30 static GaimConnectionUiOps *connection_ui_ops = NULL; |
31 | |
32 GaimConnection * | |
33 gaim_connection_new(GaimAccount *account) | |
34 { | |
35 GaimConnection *gc; | |
36 | |
37 g_return_val_if_fail(account != NULL, NULL); | |
38 | |
39 gc = g_new0(GaimConnection, 1); | |
40 | |
41 gc->prpl = gaim_find_prpl(gaim_account_get_protocol(account)); | |
42 | |
43 gaim_connection_set_account(gc, account); | |
44 gaim_account_set_connection(account, gc); | |
45 | |
46 return gc; | |
47 } | |
48 | |
49 void | |
50 gaim_connection_destroy(GaimConnection *gc) | |
51 { | |
52 g_return_if_fail(gc != NULL); | |
53 | |
54 if (gaim_connection_get_state(gc) != GAIM_DISCONNECTED) { | |
55 gaim_connection_disconnect(gc); | |
56 | |
57 return; | |
58 } | |
59 | |
60 if (gc->display_name != NULL) | |
61 g_free(gc->display_name); | |
62 | |
63 if (gc->away != NULL) | |
64 g_free(gc->away); | |
65 | |
66 if (gc->away_state != NULL) | |
67 g_free(gc->away_state); | |
68 | |
69 g_free(gc); | |
70 } | |
71 | |
72 void | |
73 gaim_connection_connect(GaimConnection *gc) | |
74 { | |
75 GaimAccount *account; | |
76 GaimConnectionUiOps *ops; | |
77 GaimPluginProtocolInfo *prpl_info = NULL; | |
78 | |
79 g_return_if_fail(gc != NULL); | |
80 | |
81 ops = gaim_get_connection_ui_ops(); | |
82 | |
83 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
84 | |
85 account = gaim_connection_get_account(gc); | |
86 | |
87 if ((prpl_info->options & OPT_PROTO_NO_PASSWORD) && | |
88 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL) && | |
89 gaim_account_get_password(account) == NULL) { | |
90 | |
5581
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
91 gaim_debug(GAIM_DEBUG_INFO, "connection", "Requestin password\n"); |
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
92 |
5563 | 93 if (ops != NULL && ops->request_pass != NULL) |
94 ops->request_pass(gc); | |
95 | |
96 return; | |
97 } | |
98 | |
99 gaim_connection_set_state(gc, GAIM_CONNECTING); | |
100 | |
5581
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
101 gaim_debug(GAIM_DEBUG_INFO, "connection", "Calling serv_login\n"); |
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
102 |
5623
8c895d80159a
[gaim-migrate @ 6030]
Christian Hammond <chipx86@chipx86.com>
parents:
5622
diff
changeset
|
103 connections = g_list_append(connections, gc); |
8c895d80159a
[gaim-migrate @ 6030]
Christian Hammond <chipx86@chipx86.com>
parents:
5622
diff
changeset
|
104 |
5563 | 105 serv_login(account); |
106 } | |
107 | |
108 void | |
109 gaim_connection_disconnect(GaimConnection *gc) | |
110 { | |
111 GList *wins; | |
112 | |
113 g_return_if_fail(gc != NULL); | |
114 | |
5740
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
115 if (gaim_connection_get_state(gc) != GAIM_DISCONNECTED) { |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
116 if (gaim_connection_get_state(gc) != GAIM_CONNECTING) |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
117 gaim_blist_remove_account(gaim_connection_get_account(gc)); |
5563 | 118 |
5740
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
119 serv_close(gc); |
5563 | 120 |
5740
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
121 gaim_connection_set_state(gc, GAIM_DISCONNECTED); |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
122 |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
123 connections = g_list_remove(connections, gc); |
5563 | 124 |
5740
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
125 gaim_event_broadcast(event_signoff, gc); |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
126 system_log(log_signoff, gc, NULL, |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
127 OPT_LOG_BUDDY_SIGNON | OPT_LOG_MY_SIGNON); |
5615
6500a6c8d679
[gaim-migrate @ 6022]
Christian Hammond <chipx86@chipx86.com>
parents:
5581
diff
changeset
|
128 |
5740
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
129 /* |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
130 * XXX This is a hack! Remove this and replace it with a better event |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
131 * notification system. |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
132 */ |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
133 for (wins = gaim_get_windows(); wins != NULL; wins = wins->next) { |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
134 GaimWindow *win = (GaimWindow *)wins->data; |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
135 gaim_conversation_update(gaim_window_get_conversation_at(win, 0), |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
136 GAIM_CONV_ACCOUNT_OFFLINE); |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
137 } |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
138 |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
139 gaim_request_close_with_handle(gc); |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
140 gaim_notify_close_with_handle(gc); |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
141 |
6ec7b32ab1df
[gaim-migrate @ 6164]
Christian Hammond <chipx86@chipx86.com>
parents:
5727
diff
changeset
|
142 update_privacy_connections(); |
5563 | 143 } |
144 | |
5622
70ae81fc802f
[gaim-migrate @ 6029]
Christian Hammond <chipx86@chipx86.com>
parents:
5615
diff
changeset
|
145 gaim_connection_destroy(gc); |
70ae81fc802f
[gaim-migrate @ 6029]
Christian Hammond <chipx86@chipx86.com>
parents:
5615
diff
changeset
|
146 |
5563 | 147 /* XXX More UI stuff! */ |
148 if (connections != NULL) | |
149 return; | |
150 | |
151 destroy_all_dialogs(); | |
152 gaim_blist_destroy(); | |
153 | |
154 show_login(); | |
155 } | |
156 | |
157 /* | |
158 * d:)->-< | |
159 * | |
160 * d:O-\-< | |
161 * | |
162 * d:D-/-< | |
163 * | |
164 * d8D->-< DANCE! | |
165 */ | |
166 | |
167 void | |
168 gaim_connection_set_state(GaimConnection *gc, GaimConnectionState state) | |
169 { | |
170 g_return_if_fail(gc != NULL); | |
171 | |
172 gc->state = state; | |
173 | |
174 if (gc->state == GAIM_CONNECTED) { | |
175 GaimConnectionUiOps *ops = gaim_get_connection_ui_ops(); | |
176 GaimBlistNode *gnode,*bnode; | |
177 GList *wins; | |
178 GList *add_buds=NULL; | |
179 | |
180 /* Set the time the account came online */ | |
181 time(&gc->login_time); | |
182 | |
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
183 connections_connecting = g_list_append(connections_connecting, gc); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
184 |
5563 | 185 if (ops != NULL && ops->connected != NULL) |
186 ops->connected(gc); | |
187 | |
188 gaim_blist_show(); | |
189 gaim_blist_add_account(gc->account); | |
190 | |
191 /* I hate this. -- ChipX86 */ | |
192 gaim_setup(gc); | |
193 | |
194 /* | |
195 * XXX This is a hack! Remove this and replace it with a better event | |
196 * notification system. | |
197 */ | |
198 for (wins = gaim_get_windows(); wins != NULL; wins = wins->next) { | |
5676
dae79aefac8d
[gaim-migrate @ 6094]
Christian Hammond <chipx86@chipx86.com>
parents:
5631
diff
changeset
|
199 GaimWindow *win = (GaimWindow *)wins->data; |
5563 | 200 gaim_conversation_update(gaim_window_get_conversation_at(win, 0), |
201 GAIM_CONV_ACCOUNT_ONLINE); | |
202 } | |
203 | |
204 gaim_event_broadcast(event_signon, gc); | |
205 system_log(log_signon, gc, NULL, | |
206 OPT_LOG_BUDDY_SIGNON | OPT_LOG_MY_SIGNON); | |
207 | |
208 #if 0 | |
209 /* away option given? */ | |
210 if (opt_away) { | |
211 away_on_login(opt_away_arg); | |
212 /* don't do it again */ | |
213 opt_away = 0; | |
214 } else if (awaymessage) { | |
215 serv_set_away(gc, GAIM_AWAY_CUSTOM, awaymessage->message); | |
216 } | |
217 if (opt_away_arg != NULL) { | |
218 g_free(opt_away_arg); | |
219 opt_away_arg = NULL; | |
220 } | |
221 #endif | |
222 | |
223 /* let the prpl know what buddies we pulled out of the local list */ | |
224 for (gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) { | |
225 if(!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
226 continue; | |
227 for(bnode = gnode->child; bnode; bnode = bnode->next) { | |
228 if(GAIM_BLIST_NODE_IS_BUDDY(bnode)) { | |
229 struct buddy *b = (struct buddy *)bnode; | |
230 if(b->account == gc->account) { | |
231 add_buds = g_list_append(add_buds, b->name); | |
232 } | |
233 } | |
234 } | |
235 } | |
236 | |
237 if(add_buds) { | |
238 serv_add_buddies(gc, add_buds); | |
239 g_list_free(add_buds); | |
240 } | |
241 | |
242 serv_set_permit_deny(gc); | |
243 } | |
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
244 else { |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
245 connections_connecting = g_list_remove(connections_connecting, gc); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
246 } |
5563 | 247 } |
248 | |
249 void | |
250 gaim_connection_set_account(GaimConnection *gc, GaimAccount *account) | |
251 { | |
252 g_return_if_fail(gc != NULL); | |
253 g_return_if_fail(account != NULL); | |
254 | |
255 gc->account = account; | |
256 } | |
257 | |
258 void | |
259 gaim_connection_set_display_name(GaimConnection *gc, const char *name) | |
260 { | |
261 g_return_if_fail(gc != NULL); | |
262 | |
263 if (gc->display_name != NULL) | |
264 g_free(gc->display_name); | |
265 | |
266 gc->display_name = (name == NULL ? NULL : g_strdup(name)); | |
267 } | |
268 | |
269 GaimConnectionState | |
270 gaim_connection_get_state(const GaimConnection *gc) | |
271 { | |
272 g_return_val_if_fail(gc != NULL, GAIM_DISCONNECTED); | |
273 | |
274 return gc->state; | |
275 } | |
276 | |
277 GaimAccount * | |
278 gaim_connection_get_account(const GaimConnection *gc) | |
279 { | |
280 g_return_val_if_fail(gc != NULL, NULL); | |
281 | |
282 return gc->account; | |
283 } | |
284 | |
285 const char * | |
286 gaim_connection_get_display_name(const GaimConnection *gc) | |
287 { | |
288 g_return_val_if_fail(gc != NULL, NULL); | |
289 | |
290 return gc->display_name; | |
291 } | |
292 | |
293 void | |
294 gaim_connection_update_progress(GaimConnection *gc, const char *text, | |
295 size_t step, size_t count) | |
296 { | |
297 GaimConnectionUiOps *ops; | |
298 | |
299 g_return_if_fail(gc != NULL); | |
300 g_return_if_fail(text != NULL); | |
301 g_return_if_fail(step < count); | |
302 g_return_if_fail(count > 1); | |
303 | |
304 ops = gaim_get_connection_ui_ops(); | |
305 | |
306 if (ops != NULL && ops->connect_progress != NULL) | |
307 ops->connect_progress(gc, text, step, count); | |
308 } | |
309 | |
310 void | |
5571
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
311 gaim_connection_notice(GaimConnection *gc, const char *text) |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
312 { |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
313 GaimConnectionUiOps *ops; |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
314 |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
315 g_return_if_fail(gc != NULL); |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
316 g_return_if_fail(text != NULL); |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
317 |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
318 ops = gaim_get_connection_ui_ops(); |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
319 |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
320 if (ops != NULL && ops->notice != NULL) |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
321 ops->notice(gc, text); |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
322 } |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
323 |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
324 void |
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
325 gaim_connection_error(GaimConnection *gc, const char *text) |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
326 { |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
327 GaimConnectionUiOps *ops; |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
328 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
329 g_return_if_fail(gc != NULL); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
330 g_return_if_fail(text != NULL); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
331 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
332 ops = gaim_get_connection_ui_ops(); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
333 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
334 if (ops != NULL && ops->disconnected != NULL) |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
335 ops->disconnected(gc, text); |
5727 | 336 |
337 gaim_connection_disconnect(gc); | |
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
338 } |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
339 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
340 void |
5563 | 341 gaim_connections_disconnect_all(void) |
342 { | |
343 GList *l; | |
344 | |
5624
4cc9e3fa22c4
[gaim-migrate @ 6031]
Christian Hammond <chipx86@chipx86.com>
parents:
5623
diff
changeset
|
345 while ((l = gaim_connections_get_all()) != NULL) |
5563 | 346 gaim_connection_destroy(l->data); |
347 } | |
348 | |
349 GList * | |
350 gaim_connections_get_all(void) | |
351 { | |
352 return connections; | |
353 } | |
354 | |
355 void | |
356 gaim_set_connection_ui_ops(GaimConnectionUiOps *ops) | |
357 { | |
358 connection_ui_ops = ops; | |
359 } | |
360 | |
361 GaimConnectionUiOps * | |
362 gaim_get_connection_ui_ops(void) | |
363 { | |
364 return connection_ui_ops; | |
365 } | |
366 |