Mercurial > pidgin
annotate src/blist.c @ 5357:2a1c92df7024
[gaim-migrate @ 5733]
This should make event broadcasting on loader plugins (like perl) work
again.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sun, 11 May 2003 21:47:29 +0000 |
parents | c1e7c3f72128 |
children | c4e7a079cc04 |
rev | line source |
---|---|
5228 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 2003, Sean Egan <sean.egan@binghamton.edu> | |
5 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 * | |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 #include <string.h> | |
27 #include <stdlib.h> | |
28 #include <sys/types.h> | |
29 #include <sys/stat.h> | |
30 #ifndef _WIN32 | |
31 #include <unistd.h> | |
32 #else | |
33 #include <direct.h> | |
34 #endif | |
35 #include <ctype.h> | |
36 #include "gaim.h" | |
37 #include "prpl.h" | |
38 #include "blist.h" | |
39 | |
40 #ifdef _WIN32 | |
41 #include "win32dep.h" | |
42 #endif | |
43 | |
44 #define PATHSIZE 1024 | |
45 | |
46 struct gaim_buddy_list *gaimbuddylist = NULL; | |
47 static struct gaim_blist_ui_ops *blist_ui_ops = NULL; | |
48 | |
49 /***************************************************************************** | |
50 * Private Utility functions * | |
51 *****************************************************************************/ | |
52 static GaimBlistNode *gaim_blist_get_last_sibling(GaimBlistNode *node) | |
53 { | |
54 GaimBlistNode *n = node; | |
55 if (!n) | |
56 return NULL; | |
57 while (n->next) | |
58 n = n->next; | |
59 return n; | |
60 } | |
61 static GaimBlistNode *gaim_blist_get_last_child(GaimBlistNode *node) | |
62 { | |
63 if (!node) | |
64 return NULL; | |
65 return gaim_blist_get_last_sibling(node->child); | |
66 } | |
67 | |
5247 | 68 struct _gaim_hbuddy { |
69 char *name; | |
70 struct gaim_account *account; | |
71 }; | |
72 | |
73 static guint _gaim_blist_hbuddy_hash (struct _gaim_hbuddy *hb) | |
74 { | |
75 return g_str_hash(hb->name); | |
76 } | |
77 | |
78 static guint _gaim_blist_hbuddy_equal (struct _gaim_hbuddy *hb1, struct _gaim_hbuddy *hb2) | |
79 { | |
80 return ((!strcmp(hb1->name, hb2->name)) && hb1->account == hb2->account); | |
81 } | |
82 | |
5228 | 83 /***************************************************************************** |
84 * Public API functions * | |
85 *****************************************************************************/ | |
86 | |
87 struct gaim_buddy_list *gaim_blist_new() | |
88 { | |
89 struct gaim_buddy_list *gbl = g_new0(struct gaim_buddy_list, 1); | |
90 | |
91 gbl->ui_ops = gaim_get_blist_ui_ops(); | |
92 | |
5247 | 93 gbl->buddies = g_hash_table_new ((GHashFunc)_gaim_blist_hbuddy_hash, |
94 (GEqualFunc)_gaim_blist_hbuddy_equal); | |
95 | |
5228 | 96 if (gbl->ui_ops != NULL && gbl->ui_ops->new_list != NULL) |
97 gbl->ui_ops->new_list(gbl); | |
98 | |
99 return gbl; | |
100 } | |
101 | |
102 void | |
103 gaim_set_blist(struct gaim_buddy_list *list) | |
104 { | |
105 gaimbuddylist = list; | |
106 } | |
107 | |
108 struct gaim_buddy_list * | |
109 gaim_get_blist(void) | |
110 { | |
111 return gaimbuddylist; | |
112 } | |
113 | |
114 void gaim_blist_show () | |
115 { | |
116 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
117 if (ops) | |
118 ops->show(gaimbuddylist); | |
119 } | |
120 | |
121 void gaim_blist_destroy() | |
122 { | |
123 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
124 if (ops) | |
125 ops->destroy(gaimbuddylist); | |
126 } | |
127 | |
128 void gaim_blist_set_visible (gboolean show) | |
129 { | |
130 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
131 if (ops) | |
132 ops->set_visible(gaimbuddylist, show); | |
133 } | |
134 | |
135 void gaim_blist_update_buddy_status (struct buddy *buddy, int status) | |
136 { | |
5266
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
137 struct gaim_blist_ui_ops *ops; |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
138 |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
139 if (buddy->uc == status) |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
140 return; |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
141 |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
142 ops = gaimbuddylist->ui_ops; |
5228 | 143 |
5305 | 144 if((status & UC_UNAVAILABLE) != (buddy->uc & UC_UNAVAILABLE)) { |
145 if(status & UC_UNAVAILABLE) | |
146 gaim_event_broadcast(event_buddy_away, buddy->account->gc, buddy->name); | |
147 else | |
148 gaim_event_broadcast(event_buddy_back, buddy->account->gc, buddy->name); | |
149 } | |
5228 | 150 |
5305 | 151 buddy->uc = status; |
5228 | 152 if (ops) |
153 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
154 } | |
155 | |
156 static gboolean presence_update_timeout_cb(struct buddy *buddy) { | |
157 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
158 | |
159 if(buddy->present == GAIM_BUDDY_SIGNING_ON) { | |
160 buddy->present = GAIM_BUDDY_ONLINE; | |
161 gaim_event_broadcast(event_buddy_signon, buddy->account->gc, buddy->name); | |
162 } else if(buddy->present == GAIM_BUDDY_SIGNING_OFF) { | |
163 buddy->present = GAIM_BUDDY_OFFLINE; | |
164 } | |
165 | |
166 buddy->timer = 0; | |
167 | |
168 if (ops) | |
169 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
170 | |
171 return FALSE; | |
172 } | |
173 | |
174 void gaim_blist_update_buddy_presence(struct buddy *buddy, int presence) { | |
175 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
176 gboolean do_timer = FALSE; | |
177 | |
178 if (!GAIM_BUDDY_IS_ONLINE(buddy) && presence) { | |
179 buddy->present = GAIM_BUDDY_SIGNING_ON; | |
180 gaim_event_broadcast(event_buddy_signon, buddy->account->gc, buddy->name); | |
181 do_timer = TRUE; | |
5277 | 182 ((struct group *)((GaimBlistNode *)buddy)->parent)->online++; |
5228 | 183 } else if(GAIM_BUDDY_IS_ONLINE(buddy) && !presence) { |
184 buddy->present = GAIM_BUDDY_SIGNING_OFF; | |
185 gaim_event_broadcast(event_buddy_signoff, buddy->account->gc, buddy->name); | |
186 do_timer = TRUE; | |
5277 | 187 ((struct group *)((GaimBlistNode *)buddy)->parent)->online--; |
5228 | 188 } |
189 | |
190 if(do_timer) { | |
191 if(buddy->timer > 0) | |
192 g_source_remove(buddy->timer); | |
193 buddy->timer = g_timeout_add(10000, (GSourceFunc)presence_update_timeout_cb, buddy); | |
194 } | |
195 | |
196 if (ops) | |
197 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
198 } | |
199 | |
200 | |
201 void gaim_blist_update_buddy_idle (struct buddy *buddy, int idle) | |
202 { | |
203 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
204 buddy->idle = idle; | |
205 if (ops) | |
206 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
207 } | |
208 void gaim_blist_update_buddy_evil (struct buddy *buddy, int warning) | |
209 { | |
210 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
211 buddy->evil = warning; | |
212 if (ops) | |
213 ops->update(gaimbuddylist,(GaimBlistNode*)buddy); | |
214 } | |
215 void gaim_blist_update_buddy_icon(struct buddy *buddy) { | |
216 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
217 if(ops) | |
218 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
219 } | |
220 void gaim_blist_rename_buddy (struct buddy *buddy, const char *name) | |
221 { | |
222 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
223 g_free(buddy->name); | |
224 buddy->name = g_strdup(name); | |
225 if (ops) | |
226 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
227 } | |
5234 | 228 |
229 void gaim_blist_alias_chat(struct chat *chat, const char *alias) | |
230 { | |
231 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
232 | |
5237 | 233 g_free(chat->alias); |
5234 | 234 |
5237 | 235 if(alias && strlen(alias)) |
236 chat->alias = g_strdup(alias); | |
237 else | |
238 chat->alias = NULL; | |
239 | |
5234 | 240 if(ops) |
241 ops->update(gaimbuddylist, (GaimBlistNode*)chat); | |
242 } | |
243 | |
5228 | 244 void gaim_blist_alias_buddy (struct buddy *buddy, const char *alias) |
245 { | |
246 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
247 struct gaim_conversation *conv; | |
248 | |
249 g_free(buddy->alias); | |
250 | |
251 if(alias && strlen(alias)) | |
252 buddy->alias = g_strdup(alias); | |
253 else | |
254 buddy->alias = NULL; | |
255 | |
256 if (ops) | |
257 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
258 | |
259 conv = gaim_find_conversation_with_account(buddy->name, buddy->account); | |
260 | |
261 if (conv) | |
262 gaim_conversation_autoset_title(conv); | |
263 } | |
264 | |
265 void gaim_blist_rename_group(struct group *group, const char *name) | |
266 { | |
267 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
5346 | 268 struct group *dest_group; |
269 GaimBlistNode *prev, *child, *next; | |
270 GSList *accts; | |
271 | |
272 if(!name || !strlen(name) || !strcmp(name, group->name)) { | |
273 /* nothing to do here */ | |
274 return; | |
275 } else if((dest_group = gaim_find_group(name))) { | |
276 /* here we're merging two groups */ | |
277 prev = gaim_blist_get_last_child((GaimBlistNode*)dest_group); | |
278 child = ((GaimBlistNode*)group)->child; | |
279 | |
280 while(child) | |
281 { | |
282 next = child->next; | |
283 if(GAIM_BLIST_NODE_IS_BUDDY(child)) { | |
284 gaim_blist_add_buddy((struct buddy *)child, dest_group, prev); | |
285 prev = child; | |
286 } else if(GAIM_BLIST_NODE_IS_CHAT(child)) { | |
287 gaim_blist_add_chat((struct chat *)child, dest_group, prev); | |
288 prev = child; | |
289 } else { | |
290 gaim_debug(GAIM_DEBUG_ERROR, "blist", | |
291 "Unknown child type in group %s\n", group->name); | |
292 } | |
293 child = next; | |
294 } | |
295 for (accts = gaim_group_get_accounts(group); accts; accts = g_slist_remove(accts, accts->data)) { | |
296 struct gaim_account *account = accts->data; | |
297 serv_rename_group(account->gc, group, name); | |
298 } | |
299 gaim_blist_remove_group(group); | |
300 } else { | |
301 /* a simple rename */ | |
302 for (accts = gaim_group_get_accounts(group); accts; accts = g_slist_remove(accts, accts->data)) { | |
303 struct gaim_account *account = accts->data; | |
304 serv_rename_group(account->gc, group, name); | |
305 } | |
306 g_free(group->name); | |
307 group->name = g_strdup(name); | |
308 if (ops) | |
309 ops->update(gaimbuddylist, (GaimBlistNode*)group); | |
310 } | |
5228 | 311 } |
5234 | 312 |
313 struct chat *gaim_chat_new(struct gaim_account *account, const char *alias, GHashTable *components) | |
314 { | |
315 struct chat *chat; | |
316 struct gaim_blist_ui_ops *ops; | |
317 | |
5237 | 318 if(!components) |
5234 | 319 return NULL; |
320 | |
321 chat = g_new0(struct chat, 1); | |
322 chat->account = account; | |
5237 | 323 if(alias && strlen(alias)) |
324 chat->alias = g_strdup(alias); | |
5234 | 325 chat->components = components; |
326 | |
327 ((GaimBlistNode*)chat)->type = GAIM_BLIST_CHAT_NODE; | |
328 | |
329 ops = gaim_get_blist_ui_ops(); | |
330 | |
331 if (ops != NULL && ops->new_node != NULL) | |
332 ops->new_node((GaimBlistNode *)chat); | |
333 | |
334 return chat; | |
335 } | |
336 | |
5228 | 337 struct buddy *gaim_buddy_new(struct gaim_account *account, const char *screenname, const char *alias) |
338 { | |
339 struct buddy *b; | |
340 struct gaim_blist_ui_ops *ops; | |
341 | |
342 b = g_new0(struct buddy, 1); | |
343 b->account = account; | |
344 b->name = g_strdup(screenname); | |
345 b->alias = g_strdup(alias); | |
346 b->settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
347 ((GaimBlistNode*)b)->type = GAIM_BLIST_BUDDY_NODE; | |
348 | |
349 ops = gaim_get_blist_ui_ops(); | |
350 | |
351 if (ops != NULL && ops->new_node != NULL) | |
352 ops->new_node((GaimBlistNode *)b); | |
353 | |
354 return b; | |
355 } | |
5234 | 356 void gaim_blist_add_chat(struct chat *chat, struct group *group, GaimBlistNode *node) |
357 { | |
358 GaimBlistNode *n = node, *cnode = (GaimBlistNode*)chat; | |
359 struct group *g = group; | |
360 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
361 gboolean save = FALSE; | |
362 | |
363 if (!n) { | |
364 if (!g) { | |
365 g = gaim_group_new(_("Chats")); | |
366 gaim_blist_add_group(g, NULL); | |
367 } | |
368 n = gaim_blist_get_last_child((GaimBlistNode*)g); | |
369 } else { | |
370 g = (struct group*)n->parent; | |
371 } | |
372 | |
373 /* if we're moving to overtop of ourselves, do nothing */ | |
374 if(cnode == n) | |
375 return; | |
376 | |
377 if (cnode->parent) { | |
378 /* This chat was already in the list and is | |
379 * being moved. | |
380 */ | |
5277 | 381 ((struct group *)cnode->parent)->totalsize--; |
5287 | 382 if (chat->account->gc) { |
383 ((struct group *)cnode->parent)->online--; | |
5277 | 384 ((struct group *)cnode->parent)->currentsize--; |
5287 | 385 } |
5234 | 386 if(cnode->next) |
387 cnode->next->prev = cnode->prev; | |
388 if(cnode->prev) | |
389 cnode->prev->next = cnode->next; | |
390 if(cnode->parent->child == cnode) | |
391 cnode->parent->child = cnode->next; | |
392 | |
393 ops->remove(gaimbuddylist, cnode); | |
394 | |
395 save = TRUE; | |
396 } | |
397 | |
398 if (n) { | |
399 if(n->next) | |
400 n->next->prev = cnode; | |
401 cnode->next = n->next; | |
402 cnode->prev = n; | |
403 cnode->parent = n->parent; | |
404 n->next = cnode; | |
5277 | 405 ((struct group *)n->parent)->totalsize++; |
5287 | 406 if (chat->account->gc) { |
407 ((struct group *)n->parent)->online++; | |
5277 | 408 ((struct group *)n->parent)->currentsize++; |
5287 | 409 } |
5234 | 410 } else { |
411 ((GaimBlistNode*)g)->child = cnode; | |
412 cnode->next = cnode->prev = NULL; | |
413 cnode->parent = (GaimBlistNode*)g; | |
5277 | 414 g->totalsize++; |
5287 | 415 if (chat->account->gc) { |
416 g->online++; | |
5277 | 417 g->currentsize++; |
5287 | 418 } |
5234 | 419 } |
420 | |
421 if (ops) | |
422 ops->update(gaimbuddylist, (GaimBlistNode*)cnode); | |
423 if (save) | |
424 gaim_blist_save(); | |
425 } | |
426 | |
5228 | 427 void gaim_blist_add_buddy (struct buddy *buddy, struct group *group, GaimBlistNode *node) |
428 { | |
429 GaimBlistNode *n = node, *bnode = (GaimBlistNode*)buddy; | |
430 struct group *g = group; | |
431 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
5247 | 432 struct _gaim_hbuddy *hb; |
5228 | 433 gboolean save = FALSE; |
434 | |
435 if (!n) { | |
436 if (!g) { | |
437 g = gaim_group_new(_("Buddies")); | |
438 gaim_blist_add_group(g, NULL); | |
439 } | |
440 n = gaim_blist_get_last_child((GaimBlistNode*)g); | |
441 } else { | |
442 g = (struct group*)n->parent; | |
443 } | |
444 | |
445 /* if we're moving to overtop of ourselves, do nothing */ | |
446 if(bnode == n) | |
447 return; | |
448 | |
449 if (bnode->parent) { | |
450 /* This buddy was already in the list and is | |
451 * being moved. | |
452 */ | |
5277 | 453 ((struct group *)bnode->parent)->totalsize--; |
454 if (buddy->account->gc) | |
455 ((struct group *)bnode->parent)->currentsize--; | |
5313 | 456 if (buddy->present) |
457 ((struct group *)bnode->parent)->online--; | |
5277 | 458 |
5228 | 459 if(bnode->next) |
460 bnode->next->prev = bnode->prev; | |
461 if(bnode->prev) | |
462 bnode->prev->next = bnode->next; | |
463 if(bnode->parent->child == bnode) | |
464 bnode->parent->child = bnode->next; | |
465 | |
466 ops->remove(gaimbuddylist, bnode); | |
467 | |
5277 | 468 if (bnode->parent != ((GaimBlistNode*)g)) { |
5228 | 469 serv_move_buddy(buddy, (struct group*)bnode->parent, g); |
5277 | 470 } |
5228 | 471 save = TRUE; |
472 } | |
473 | |
474 if (n) { | |
475 if(n->next) | |
476 n->next->prev = (GaimBlistNode*)buddy; | |
477 ((GaimBlistNode*)buddy)->next = n->next; | |
478 ((GaimBlistNode*)buddy)->prev = n; | |
479 ((GaimBlistNode*)buddy)->parent = n->parent; | |
480 n->next = (GaimBlistNode*)buddy; | |
5277 | 481 ((struct group *)n->parent)->totalsize++; |
482 if (buddy->account->gc) | |
483 ((struct group *)n->parent)->currentsize++; | |
5313 | 484 if (buddy->present) |
485 ((struct group *)n->parent)->online++; | |
5228 | 486 } else { |
487 ((GaimBlistNode*)g)->child = (GaimBlistNode*)buddy; | |
488 ((GaimBlistNode*)buddy)->next = NULL; | |
489 ((GaimBlistNode*)buddy)->prev = NULL; | |
490 ((GaimBlistNode*)buddy)->parent = (GaimBlistNode*)g; | |
5277 | 491 g->totalsize++; |
492 if (buddy->account->gc) | |
493 g->currentsize++; | |
5313 | 494 if (buddy->present) |
495 g->online++; | |
5228 | 496 } |
497 | |
5247 | 498 hb = g_malloc(sizeof(struct _gaim_hbuddy)); |
499 hb->name = g_strdup(normalize(buddy->name)); | |
500 hb->account = buddy->account; | |
501 | |
502 if (g_hash_table_lookup(gaimbuddylist->buddies, (gpointer)hb)) { | |
503 /* This guy already exists */ | |
504 g_free(hb->name); | |
505 g_free(hb); | |
506 } else { | |
507 g_hash_table_insert(gaimbuddylist->buddies, (gpointer)hb, (gpointer)buddy); | |
508 } | |
509 | |
5228 | 510 if (ops) |
511 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
512 if (save) | |
513 gaim_blist_save(); | |
514 } | |
515 | |
516 struct group *gaim_group_new(const char *name) | |
517 { | |
518 struct group *g = gaim_find_group(name); | |
519 | |
520 if (!g) { | |
521 struct gaim_blist_ui_ops *ops; | |
522 g= g_new0(struct group, 1); | |
523 g->name = g_strdup(name); | |
5277 | 524 g->totalsize = 0; |
525 g->currentsize = 0; | |
526 g->online = 0; | |
5228 | 527 g->settings = g_hash_table_new_full(g_str_hash, g_str_equal, |
528 g_free, g_free); | |
529 ((GaimBlistNode*)g)->type = GAIM_BLIST_GROUP_NODE; | |
530 | |
531 ops = gaim_get_blist_ui_ops(); | |
532 | |
533 if (ops != NULL && ops->new_node != NULL) | |
534 ops->new_node((GaimBlistNode *)g); | |
535 | |
536 } | |
537 return g; | |
538 } | |
539 | |
540 void gaim_blist_add_group (struct group *group, GaimBlistNode *node) | |
541 { | |
542 struct gaim_blist_ui_ops *ops; | |
543 GaimBlistNode *gnode = (GaimBlistNode*)group; | |
544 gboolean save = FALSE; | |
545 | |
546 if (!gaimbuddylist) | |
547 gaimbuddylist = gaim_blist_new(); | |
548 ops = gaimbuddylist->ui_ops; | |
549 | |
550 if (!gaimbuddylist->root) { | |
551 gaimbuddylist->root = gnode; | |
552 return; | |
553 } | |
554 | |
555 if (!node) | |
556 node = gaim_blist_get_last_sibling(gaimbuddylist->root); | |
557 | |
558 /* if we're moving to overtop of ourselves, do nothing */ | |
559 if(gnode == node) | |
560 return; | |
561 | |
562 if (gaim_find_group(group->name)) { | |
563 /* This is just being moved */ | |
564 | |
565 ops->remove(gaimbuddylist, (GaimBlistNode*)group); | |
566 | |
567 if(gnode == gaimbuddylist->root) | |
568 gaimbuddylist->root = gnode->next; | |
569 if(gnode->prev) | |
570 gnode->prev->next = gnode->next; | |
571 if(gnode->next) | |
572 gnode->next->prev = gnode->prev; | |
573 | |
574 save = TRUE; | |
575 } | |
576 | |
577 gnode->next = node->next; | |
578 gnode->prev = node; | |
579 if(node->next) | |
580 node->next->prev = gnode; | |
581 node->next = gnode; | |
582 | |
583 if (ops) { | |
584 ops->update(gaimbuddylist, gnode); | |
585 for(node = gnode->child; node; node = node->next) | |
586 ops->update(gaimbuddylist, node); | |
587 } | |
588 if (save) | |
589 gaim_blist_save(); | |
590 } | |
591 | |
592 void gaim_blist_remove_buddy (struct buddy *buddy) | |
593 { | |
594 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
595 | |
596 GaimBlistNode *gnode, *node = (GaimBlistNode*)buddy; | |
597 struct group *group; | |
5247 | 598 struct _gaim_hbuddy hb, *key; |
599 struct buddy *val; | |
5228 | 600 |
601 gnode = node->parent; | |
602 group = (struct group *)gnode; | |
603 | |
604 if(gnode->child == node) | |
605 gnode->child = node->next; | |
606 if (node->prev) | |
607 node->prev->next = node->next; | |
608 if (node->next) | |
609 node->next->prev = node->prev; | |
5277 | 610 group->totalsize--; |
611 if (buddy->account->gc) | |
612 group->currentsize--; | |
5228 | 613 |
5247 | 614 hb.name = normalize(buddy->name); |
615 hb.account = buddy->account; | |
616 if (g_hash_table_lookup_extended(gaimbuddylist->buddies, &hb, (gpointer *)&key, (gpointer *)&val)) { | |
617 g_hash_table_remove(gaimbuddylist->buddies, &hb); | |
618 g_free(key->name); | |
619 g_free(key); | |
620 } | |
621 | |
5292 | 622 if(buddy->timer > 0) |
623 g_source_remove(buddy->timer); | |
624 | |
5228 | 625 ops->remove(gaimbuddylist, node); |
626 g_hash_table_destroy(buddy->settings); | |
627 g_free(buddy->name); | |
628 g_free(buddy->alias); | |
629 g_free(buddy); | |
630 } | |
631 | |
5234 | 632 void gaim_blist_remove_chat (struct chat *chat) |
633 { | |
634 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
635 | |
636 GaimBlistNode *gnode, *node = (GaimBlistNode*)chat; | |
637 struct group *group; | |
638 | |
639 gnode = node->parent; | |
640 group = (struct group *)gnode; | |
641 | |
642 if(gnode->child == node) | |
643 gnode->child = node->next; | |
644 if (node->prev) | |
645 node->prev->next = node->next; | |
646 if (node->next) | |
647 node->next->prev = node->prev; | |
5277 | 648 group->totalsize--; |
649 if (chat->account->gc) | |
650 group->currentsize--; | |
5234 | 651 |
652 ops->remove(gaimbuddylist, node); | |
653 g_hash_table_destroy(chat->components); | |
654 g_free(chat->alias); | |
655 g_free(chat); | |
656 } | |
657 | |
5228 | 658 void gaim_blist_remove_group (struct group *group) |
659 { | |
660 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
661 GaimBlistNode *node = (GaimBlistNode*)group; | |
662 | |
663 if(node->child) { | |
664 char *buf; | |
665 int count = 0; | |
666 GaimBlistNode *child = node->child; | |
667 | |
668 while(child) { | |
669 count++; | |
670 child = child->next; | |
671 } | |
672 | |
673 buf = g_strdup_printf(_("%d buddies from group %s were not " | |
674 "removed because their accounts were not logged in. These " | |
675 "buddies, and the group were not removed.\n"), | |
676 count, group->name); | |
677 do_error_dialog(_("Group Not Removed"), buf, GAIM_ERROR); | |
678 g_free(buf); | |
679 return; | |
680 } | |
681 | |
682 if(gaimbuddylist->root == node) | |
683 gaimbuddylist->root = node->next; | |
684 if (node->prev) | |
685 node->prev->next = node->next; | |
686 if (node->next) | |
687 node->next->prev = node->prev; | |
688 | |
689 ops->remove(gaimbuddylist, node); | |
690 g_free(group->name); | |
691 g_free(group); | |
692 } | |
693 | |
694 char *gaim_get_buddy_alias_only(struct buddy *b) { | |
695 if(!b) | |
696 return NULL; | |
697 if(b->alias && b->alias[0]) | |
698 return b->alias; | |
699 else if((misc_options & OPT_MISC_USE_SERVER_ALIAS) && b->server_alias) | |
700 return b->server_alias; | |
701 return NULL; | |
702 } | |
703 | |
704 char * gaim_get_buddy_alias (struct buddy *buddy) | |
705 { | |
706 char *ret = gaim_get_buddy_alias_only(buddy); | |
707 if(!ret) | |
708 return buddy ? buddy->name : _("Unknown"); | |
709 return ret; | |
710 | |
711 } | |
712 | |
713 struct buddy *gaim_find_buddy(struct gaim_account *account, const char *name) | |
714 { | |
5247 | 715 struct buddy *buddy; |
716 struct _gaim_hbuddy hb; | |
5228 | 717 |
718 if (!gaimbuddylist) | |
719 return NULL; | |
720 | |
5247 | 721 hb.name = normalize(name); |
722 hb.account = account; | |
723 buddy = g_hash_table_lookup(gaimbuddylist->buddies, &hb); | |
724 | |
725 return buddy; | |
5228 | 726 } |
727 | |
728 struct group *gaim_find_group(const char *name) | |
729 { | |
730 GaimBlistNode *node; | |
731 if (!gaimbuddylist) | |
732 return NULL; | |
733 node = gaimbuddylist->root; | |
734 while(node) { | |
735 if (!strcmp(((struct group*)node)->name, name)) | |
736 return (struct group*)node; | |
737 node = node->next; | |
738 } | |
739 return NULL; | |
740 } | |
741 struct group *gaim_find_buddys_group(struct buddy *buddy) | |
742 { | |
743 if (!buddy) | |
744 return NULL; | |
745 return (struct group*)(((GaimBlistNode*)buddy)->parent); | |
746 } | |
747 | |
748 GSList *gaim_group_get_accounts(struct group *g) | |
749 { | |
750 GSList *l = NULL; | |
751 GaimBlistNode *child = ((GaimBlistNode *)g)->child; | |
752 | |
753 while (child) { | |
754 if (!g_slist_find(l, ((struct buddy*)child)->account)) | |
755 l = g_slist_append(l, ((struct buddy*)child)->account); | |
756 child = child->next; | |
757 } | |
758 return l; | |
759 } | |
760 | |
5234 | 761 void gaim_blist_add_account(struct gaim_account *account) |
762 { | |
763 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
764 GaimBlistNode *group, *buddy; | |
765 | |
766 if(!gaimbuddylist) | |
767 return; | |
768 | |
769 for(group = gaimbuddylist->root; group; group = group->next) { | |
770 if(!GAIM_BLIST_NODE_IS_GROUP(group)) | |
771 continue; | |
772 for(buddy = group->child; buddy; buddy = buddy->next) { | |
773 if(GAIM_BLIST_NODE_IS_BUDDY(buddy)) { | |
774 if (account == ((struct buddy*)buddy)->account) { | |
5277 | 775 ((struct group *)group)->currentsize++; |
5234 | 776 if(ops) |
777 ops->update(gaimbuddylist, buddy); | |
778 } | |
779 } else if(GAIM_BLIST_NODE_IS_CHAT(buddy)) { | |
780 if (account == ((struct chat*)buddy)->account) { | |
5287 | 781 ((struct group *)group)->online++; |
5277 | 782 ((struct group *)group)->currentsize++; |
5234 | 783 if(ops) |
784 ops->update(gaimbuddylist, buddy); | |
785 } | |
786 } | |
787 } | |
788 } | |
789 } | |
790 | |
5228 | 791 void gaim_blist_remove_account(struct gaim_account *account) |
792 { | |
793 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
5234 | 794 GaimBlistNode *group, *buddy; |
795 | |
5228 | 796 if (!gaimbuddylist) |
797 return; | |
5234 | 798 |
799 for(group = gaimbuddylist->root; group; group = group->next) { | |
800 if(!GAIM_BLIST_NODE_IS_GROUP(group)) | |
801 continue; | |
802 for(buddy = group->child; buddy; buddy = buddy->next) { | |
803 if(GAIM_BLIST_NODE_IS_BUDDY(buddy)) { | |
804 if (account == ((struct buddy*)buddy)->account) { | |
5277 | 805 if (((struct buddy *)buddy)->present) |
806 ((struct group *)group)->online--; | |
5234 | 807 ((struct buddy*)buddy)->present = GAIM_BUDDY_OFFLINE; |
5277 | 808 ((struct group *)group)->currentsize--; |
5234 | 809 if(ops) |
810 ops->remove(gaimbuddylist, buddy); | |
811 } | |
812 } else if(GAIM_BLIST_NODE_IS_CHAT(buddy)) { | |
813 if (account == ((struct chat*)buddy)->account) { | |
5277 | 814 ((struct group *)group)->online--; |
815 ((struct group *)group)->currentsize--; | |
5234 | 816 if(ops) |
817 ops->remove(gaimbuddylist, buddy); | |
818 } | |
5228 | 819 } |
820 } | |
821 } | |
822 } | |
823 | |
824 void parse_toc_buddy_list(struct gaim_account *account, char *config) | |
825 { | |
826 char *c; | |
827 char current[256]; | |
828 GList *bud = NULL; | |
829 | |
830 | |
831 if (config != NULL) { | |
832 | |
833 /* skip "CONFIG:" (if it exists) */ | |
834 c = strncmp(config + 6 /* sizeof(struct sflap_hdr) */ , "CONFIG:", strlen("CONFIG:")) ? | |
835 strtok(config, "\n") : | |
836 strtok(config + 6 /* sizeof(struct sflap_hdr) */ + strlen("CONFIG:"), "\n"); | |
837 do { | |
838 if (c == NULL) | |
839 break; | |
840 if (*c == 'g') { | |
841 char *utf8 = NULL; | |
842 utf8 = gaim_try_conv_to_utf8(c + 2); | |
843 if (utf8 == NULL) { | |
844 g_strlcpy(current, _("Invalid Groupname"), sizeof(current)); | |
845 } else { | |
846 g_strlcpy(current, utf8, sizeof(current)); | |
847 g_free(utf8); | |
848 } | |
849 if (!gaim_find_group(current)) { | |
850 struct group *g = gaim_group_new(current); | |
851 gaim_blist_add_group(g, NULL); | |
852 } | |
853 } else if (*c == 'b') { /*&& !gaim_find_buddy(user, c + 2)) {*/ | |
854 char nm[80], sw[388], *a, *utf8 = NULL; | |
855 | |
856 if ((a = strchr(c + 2, ':')) != NULL) { | |
857 *a++ = '\0'; /* nul the : */ | |
858 } | |
859 | |
860 g_strlcpy(nm, c + 2, sizeof(nm)); | |
861 if (a) { | |
862 utf8 = gaim_try_conv_to_utf8(a); | |
863 if (utf8 == NULL) { | |
864 gaim_debug(GAIM_DEBUG_ERROR, "toc blist", | |
865 "Failed to convert alias for " | |
866 "'%s' to UTF-8\n", nm); | |
867 } | |
868 } | |
869 if (utf8 == NULL) { | |
870 sw[0] = '\0'; | |
871 } else { | |
872 /* This can leave a partial sequence at the end, | |
873 * but who cares? */ | |
874 g_strlcpy(sw, utf8, sizeof(sw)); | |
875 g_free(utf8); | |
876 } | |
877 | |
878 if (!gaim_find_buddy(account, nm)) { | |
879 struct buddy *b = gaim_buddy_new(account, nm, sw); | |
880 struct group *g = gaim_find_group(current); | |
881 gaim_blist_add_buddy(b, g, NULL); | |
882 bud = g_list_append(bud, g_strdup(nm)); | |
883 } | |
884 } else if (*c == 'p') { | |
885 gaim_privacy_permit_add(account, c + 2); | |
886 } else if (*c == 'd') { | |
887 gaim_privacy_deny_add(account, c + 2); | |
888 } else if (!strncmp("toc", c, 3)) { | |
889 sscanf(c + strlen(c) - 1, "%d", &account->permdeny); | |
890 gaim_debug(GAIM_DEBUG_MISC, "toc blist", | |
891 "permdeny: %d\n", account->permdeny); | |
892 if (account->permdeny == 0) | |
893 account->permdeny = 1; | |
894 } else if (*c == 'm') { | |
895 sscanf(c + 2, "%d", &account->permdeny); | |
896 gaim_debug(GAIM_DEBUG_MISC, "toc blist", | |
897 "permdeny: %d\n", account->permdeny); | |
898 if (account->permdeny == 0) | |
899 account->permdeny = 1; | |
900 } | |
901 } while ((c = strtok(NULL, "\n"))); | |
902 | |
903 if(account->gc) { | |
904 if(bud) { | |
905 GList *node = bud; | |
906 serv_add_buddies(account->gc, bud); | |
907 while(node) { | |
908 g_free(node->data); | |
909 node = node->next; | |
910 } | |
911 } | |
912 serv_set_permit_deny(account->gc); | |
913 } | |
914 g_list_free(bud); | |
915 } | |
916 } | |
917 | |
918 #if 0 | |
919 /* translate an AIM 3 buddylist (*.lst) to a Gaim buddylist */ | |
920 static GString *translate_lst(FILE *src_fp) | |
921 { | |
922 char line[BUF_LEN], *line2; | |
923 char *name; | |
924 int i; | |
925 | |
926 GString *dest = g_string_new("m 1\n"); | |
927 | |
928 while (fgets(line, BUF_LEN, src_fp)) { | |
929 line2 = g_strchug(line); | |
930 if (strstr(line2, "group") == line2) { | |
931 name = strpbrk(line2, " \t\n\r\f") + 1; | |
932 dest = g_string_append(dest, "g "); | |
933 for (i = 0; i < strcspn(name, "\n\r"); i++) | |
934 if (name[i] != '\"') | |
935 dest = g_string_append_c(dest, name[i]); | |
936 dest = g_string_append_c(dest, '\n'); | |
937 } | |
938 if (strstr(line2, "buddy") == line2) { | |
939 name = strpbrk(line2, " \t\n\r\f") + 1; | |
940 dest = g_string_append(dest, "b "); | |
941 for (i = 0; i < strcspn(name, "\n\r"); i++) | |
942 if (name[i] != '\"') | |
943 dest = g_string_append_c(dest, name[i]); | |
944 dest = g_string_append_c(dest, '\n'); | |
945 } | |
946 } | |
947 | |
948 return dest; | |
949 } | |
950 | |
951 | |
952 /* translate an AIM 4 buddylist (*.blt) to Gaim format */ | |
953 static GString *translate_blt(FILE *src_fp) | |
954 { | |
955 int i; | |
956 char line[BUF_LEN]; | |
957 char *buddy; | |
958 | |
959 GString *dest = g_string_new("m 1\n"); | |
960 | |
961 while (strstr(fgets(line, BUF_LEN, src_fp), "Buddy") == NULL); | |
962 while (strstr(fgets(line, BUF_LEN, src_fp), "list") == NULL); | |
963 | |
964 while (1) { | |
965 fgets(line, BUF_LEN, src_fp); g_strchomp(line); | |
966 if (strchr(line, '}') != NULL) | |
967 break; | |
968 | |
969 if (strchr(line, '{') != NULL) { | |
970 /* Syntax starting with "<group> {" */ | |
971 | |
972 dest = g_string_append(dest, "g "); | |
973 buddy = g_strchug(strtok(line, "{")); | |
974 for (i = 0; i < strlen(buddy); i++) | |
975 if (buddy[i] != '\"') | |
976 dest = g_string_append_c(dest, buddy[i]); | |
977 dest = g_string_append_c(dest, '\n'); | |
978 while (strchr(fgets(line, BUF_LEN, src_fp), '}') == NULL) { | |
979 gboolean pounce = FALSE; | |
980 char *e; | |
981 g_strchomp(line); | |
982 buddy = g_strchug(line); | |
983 gaim_debug(GAIM_DEBUG_MISC, "AIM 4 blt import", | |
984 "buddy: \"%s\"\n", buddy); | |
985 dest = g_string_append(dest, "b "); | |
986 if (strchr(buddy, '{') != NULL) { | |
987 /* buddy pounce, etc */ | |
988 char *pos = strchr(buddy, '{') - 1; | |
989 *pos = 0; | |
990 pounce = TRUE; | |
991 } | |
992 if ((e = strchr(buddy, '\"')) != NULL) { | |
993 *e = '\0'; | |
994 buddy++; | |
995 } | |
996 dest = g_string_append(dest, buddy); | |
997 dest = g_string_append_c(dest, '\n'); | |
998 if (pounce) | |
999 do | |
1000 fgets(line, BUF_LEN, src_fp); | |
1001 while (!strchr(line, '}')); | |
1002 } | |
1003 } else { | |
1004 | |
1005 /* Syntax "group buddy buddy ..." */ | |
1006 buddy = g_strchug(strtok(line, " \n")); | |
1007 dest = g_string_append(dest, "g "); | |
1008 if (strchr(buddy, '\"') != NULL) { | |
1009 dest = g_string_append(dest, &buddy[1]); | |
1010 dest = g_string_append_c(dest, ' '); | |
1011 buddy = g_strchug(strtok(NULL, " \n")); | |
1012 while (strchr(buddy, '\"') == NULL) { | |
1013 dest = g_string_append(dest, buddy); | |
1014 dest = g_string_append_c(dest, ' '); | |
1015 buddy = g_strchug(strtok(NULL, " \n")); | |
1016 } | |
1017 buddy[strlen(buddy) - 1] = '\0'; | |
1018 dest = g_string_append(dest, buddy); | |
1019 } else { | |
1020 dest = g_string_append(dest, buddy); | |
1021 } | |
1022 dest = g_string_append_c(dest, '\n'); | |
1023 while ((buddy = g_strchug(strtok(NULL, " \n"))) != NULL) { | |
1024 dest = g_string_append(dest, "b "); | |
1025 if (strchr(buddy, '\"') != NULL) { | |
1026 dest = g_string_append(dest, &buddy[1]); | |
1027 dest = g_string_append_c(dest, ' '); | |
1028 buddy = g_strchug(strtok(NULL, " \n")); | |
1029 while (strchr(buddy, '\"') == NULL) { | |
1030 dest = g_string_append(dest, buddy); | |
1031 dest = g_string_append_c(dest, ' '); | |
1032 buddy = g_strchug(strtok(NULL, " \n")); | |
1033 } | |
1034 buddy[strlen(buddy) - 1] = '\0'; | |
1035 dest = g_string_append(dest, buddy); | |
1036 } else { | |
1037 dest = g_string_append(dest, buddy); | |
1038 } | |
1039 dest = g_string_append_c(dest, '\n'); | |
1040 } | |
1041 } | |
1042 } | |
1043 | |
1044 return dest; | |
1045 } | |
1046 | |
1047 static GString *translate_gnomeicu(FILE *src_fp) | |
1048 { | |
1049 char line[BUF_LEN]; | |
1050 GString *dest = g_string_new("m 1\ng Buddies\n"); | |
1051 | |
1052 while (strstr(fgets(line, BUF_LEN, src_fp), "NewContacts") == NULL); | |
1053 | |
1054 while (fgets(line, BUF_LEN, src_fp)) { | |
1055 char *eq; | |
1056 g_strchomp(line); | |
1057 if (line[0] == '\n' || line[0] == '[') | |
1058 break; | |
1059 eq = strchr(line, '='); | |
1060 if (!eq) | |
1061 break; | |
1062 *eq = ':'; | |
1063 eq = strchr(eq, ','); | |
1064 if (eq) | |
1065 *eq = '\0'; | |
1066 dest = g_string_append(dest, "b "); | |
1067 dest = g_string_append(dest, line); | |
1068 dest = g_string_append_c(dest, '\n'); | |
1069 } | |
1070 | |
1071 return dest; | |
1072 } | |
1073 #endif | |
1074 | |
1075 static gchar *get_screenname_filename(const char *name) | |
1076 { | |
1077 gchar **split; | |
1078 gchar *good; | |
1079 gchar *ret; | |
1080 | |
1081 split = g_strsplit(name, G_DIR_SEPARATOR_S, -1); | |
1082 good = g_strjoinv(NULL, split); | |
1083 g_strfreev(split); | |
1084 | |
1085 ret = g_utf8_strup(good, -1); | |
1086 | |
1087 g_free(good); | |
1088 | |
1089 return ret; | |
1090 } | |
1091 | |
1092 static gboolean gaim_blist_read(const char *filename); | |
1093 | |
1094 | |
1095 static void do_import(struct gaim_account *account, const char *filename) | |
1096 { | |
1097 GString *buf = NULL; | |
1098 char first[64]; | |
1099 char path[PATHSIZE]; | |
1100 int len; | |
1101 FILE *f; | |
1102 struct stat st; | |
1103 | |
1104 if (filename) { | |
1105 g_snprintf(path, sizeof(path), "%s", filename); | |
1106 } else { | |
1107 char *g_screenname = get_screenname_filename(account->username); | |
1108 char *file = gaim_user_dir(); | |
1109 int protocol = (account->protocol == GAIM_PROTO_OSCAR) ? (isalpha(account->username[0]) ? GAIM_PROTO_TOC : GAIM_PROTO_ICQ): account->protocol; | |
1110 | |
1111 if (file != (char *)NULL) { | |
1112 sprintf(path, "%s" G_DIR_SEPARATOR_S "%s.%d.blist", file, g_screenname, protocol); | |
1113 g_free(g_screenname); | |
1114 } else { | |
1115 g_free(g_screenname); | |
1116 return; | |
1117 } | |
1118 } | |
1119 | |
1120 if (stat(path, &st)) { | |
1121 gaim_debug(GAIM_DEBUG_ERROR, "blist import", "Unable to stat %s.\n", | |
1122 path); | |
1123 return; | |
1124 } | |
1125 | |
1126 if (!(f = fopen(path, "r"))) { | |
1127 gaim_debug(GAIM_DEBUG_ERROR, "blist import", "Unable to open %s.\n", | |
1128 path); | |
1129 return; | |
1130 } | |
1131 | |
1132 fgets(first, 64, f); | |
1133 | |
1134 if ((first[0] == '\n') || (first[0] == '\r' && first[1] == '\n')) | |
1135 fgets(first, 64, f); | |
1136 | |
1137 #if 0 | |
1138 if (!g_strncasecmp(first, "<xml", strlen("<xml"))) { | |
1139 /* new gaim XML buddy list */ | |
1140 gaim_blist_read(path); | |
1141 | |
1142 /* We really don't need to bother doing stuf like translating AIM 3 buddy lists anymore */ | |
1143 | |
1144 } else if (!g_strncasecmp(first, "Config {", strlen("Config {"))) { | |
1145 /* AIM 4 buddy list */ | |
1146 gaim_debug(GAIM_DEBUG_MISC, "blist import", "aim 4\n"); | |
1147 rewind(f); | |
1148 buf = translate_blt(f); | |
1149 } else if (strstr(first, "group") != NULL) { | |
1150 /* AIM 3 buddy list */ | |
1151 gaim_debug(GAIM_DEBUG_MISC, "blist import", "aim 3\n"); | |
1152 rewind(f); | |
1153 buf = translate_lst(f); | |
1154 } else if (!g_strncasecmp(first, "[User]", strlen("[User]"))) { | |
1155 /* GnomeICU (hopefully) */ | |
1156 gaim_debug(GAIM_DEBUG_MISC, "blist import", "gnomeicu\n"); | |
1157 rewind(f); | |
1158 buf = translate_gnomeicu(f); | |
1159 | |
1160 } else | |
1161 #endif | |
1162 if (first[0] == 'm') { | |
1163 /* Gaim buddy list - no translation */ | |
1164 char buf2[BUF_LONG * 2]; | |
1165 buf = g_string_new(""); | |
1166 rewind(f); | |
1167 while (1) { | |
1168 len = fread(buf2, 1, BUF_LONG * 2 - 1, f); | |
1169 if (len <= 0) | |
1170 break; | |
1171 buf2[len] = '\0'; | |
1172 buf = g_string_append(buf, buf2); | |
1173 if (len != BUF_LONG * 2 - 1) | |
1174 break; | |
1175 } | |
1176 } | |
1177 | |
1178 fclose(f); | |
1179 | |
1180 if (buf) { | |
1181 buf = g_string_prepend(buf, "toc_set_config {"); | |
1182 buf = g_string_append(buf, "}\n"); | |
1183 parse_toc_buddy_list(account, buf->str); | |
1184 g_string_free(buf, TRUE); | |
1185 } | |
1186 } | |
1187 | |
1188 gboolean gaim_group_on_account(struct group *g, struct gaim_account *account) { | |
1189 GaimBlistNode *bnode; | |
1190 for(bnode = g->node.child; bnode; bnode = bnode->next) { | |
1191 struct buddy *b = (struct buddy *)bnode; | |
1192 if(!GAIM_BLIST_NODE_IS_BUDDY(bnode)) | |
1193 continue; | |
1194 if((!account && b->account->gc) || b->account == account) | |
1195 return TRUE; | |
1196 } | |
1197 return FALSE; | |
1198 } | |
1199 | |
1200 static gboolean blist_safe_to_write = FALSE; | |
1201 | |
1202 static char *blist_parser_group_name = NULL; | |
1203 static char *blist_parser_person_name = NULL; | |
1204 static char *blist_parser_account_name = NULL; | |
1205 static int blist_parser_account_protocol = 0; | |
5234 | 1206 static char *blist_parser_chat_alias = NULL; |
1207 static char *blist_parser_component_name = NULL; | |
1208 static char *blist_parser_component_value = NULL; | |
5228 | 1209 static char *blist_parser_buddy_name = NULL; |
1210 static char *blist_parser_buddy_alias = NULL; | |
1211 static char *blist_parser_setting_name = NULL; | |
1212 static char *blist_parser_setting_value = NULL; | |
1213 static GHashTable *blist_parser_buddy_settings = NULL; | |
1214 static GHashTable *blist_parser_group_settings = NULL; | |
5234 | 1215 static GHashTable *blist_parser_chat_components = NULL; |
5228 | 1216 static int blist_parser_privacy_mode = 0; |
1217 static GList *tag_stack = NULL; | |
1218 enum { | |
1219 BLIST_TAG_GAIM, | |
1220 BLIST_TAG_BLIST, | |
1221 BLIST_TAG_GROUP, | |
5234 | 1222 BLIST_TAG_CHAT, |
1223 BLIST_TAG_COMPONENT, | |
5228 | 1224 BLIST_TAG_PERSON, |
1225 BLIST_TAG_BUDDY, | |
1226 BLIST_TAG_NAME, | |
1227 BLIST_TAG_ALIAS, | |
1228 BLIST_TAG_SETTING, | |
1229 BLIST_TAG_PRIVACY, | |
1230 BLIST_TAG_ACCOUNT, | |
1231 BLIST_TAG_PERMIT, | |
1232 BLIST_TAG_BLOCK, | |
1233 BLIST_TAG_IGNORE | |
1234 }; | |
1235 static gboolean blist_parser_error_occurred = FALSE; | |
1236 | |
1237 static void blist_start_element_handler (GMarkupParseContext *context, | |
1238 const gchar *element_name, | |
1239 const gchar **attribute_names, | |
1240 const gchar **attribute_values, | |
1241 gpointer user_data, | |
1242 GError **error) { | |
1243 int i; | |
1244 | |
1245 if(!strcmp(element_name, "gaim")) { | |
1246 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_GAIM)); | |
1247 } else if(!strcmp(element_name, "blist")) { | |
1248 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_BLIST)); | |
1249 } else if(!strcmp(element_name, "group")) { | |
1250 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_GROUP)); | |
1251 for(i=0; attribute_names[i]; i++) { | |
1252 if(!strcmp(attribute_names[i], "name")) { | |
1253 g_free(blist_parser_group_name); | |
1254 blist_parser_group_name = g_strdup(attribute_values[i]); | |
1255 } | |
1256 } | |
1257 if(blist_parser_group_name) { | |
1258 struct group *g = gaim_group_new(blist_parser_group_name); | |
1259 gaim_blist_add_group(g,NULL); | |
1260 } | |
5234 | 1261 } else if(!strcmp(element_name, "chat")) { |
1262 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_CHAT)); | |
1263 for(i=0; attribute_names[i]; i++) { | |
1264 if(!strcmp(attribute_names[i], "account")) { | |
1265 g_free(blist_parser_account_name); | |
1266 blist_parser_account_name = g_strdup(attribute_values[i]); | |
1267 } else if(!strcmp(attribute_names[i], "protocol")) { | |
1268 blist_parser_account_protocol = atoi(attribute_values[i]); | |
1269 } | |
1270 } | |
5228 | 1271 } else if(!strcmp(element_name, "person")) { |
1272 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_PERSON)); | |
1273 for(i=0; attribute_names[i]; i++) { | |
1274 if(!strcmp(attribute_names[i], "name")) { | |
1275 g_free(blist_parser_person_name); | |
1276 blist_parser_person_name = g_strdup(attribute_values[i]); | |
1277 } | |
1278 } | |
1279 } else if(!strcmp(element_name, "buddy")) { | |
1280 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_BUDDY)); | |
1281 for(i=0; attribute_names[i]; i++) { | |
1282 if(!strcmp(attribute_names[i], "account")) { | |
1283 g_free(blist_parser_account_name); | |
1284 blist_parser_account_name = g_strdup(attribute_values[i]); | |
1285 } else if(!strcmp(attribute_names[i], "protocol")) { | |
1286 blist_parser_account_protocol = atoi(attribute_values[i]); | |
1287 } | |
1288 } | |
1289 } else if(!strcmp(element_name, "name")) { | |
1290 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_NAME)); | |
1291 } else if(!strcmp(element_name, "alias")) { | |
1292 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_ALIAS)); | |
1293 } else if(!strcmp(element_name, "setting")) { | |
1294 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_SETTING)); | |
1295 for(i=0; attribute_names[i]; i++) { | |
1296 if(!strcmp(attribute_names[i], "name")) { | |
1297 g_free(blist_parser_setting_name); | |
1298 blist_parser_setting_name = g_strdup(attribute_values[i]); | |
1299 } | |
1300 } | |
5234 | 1301 } else if(!strcmp(element_name, "component")) { |
1302 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_COMPONENT)); | |
1303 for(i=0; attribute_names[i]; i++) { | |
1304 if(!strcmp(attribute_names[i], "name")) { | |
1305 g_free(blist_parser_component_name); | |
1306 blist_parser_component_name = g_strdup(attribute_values[i]); | |
1307 } | |
1308 } | |
1309 | |
5228 | 1310 } else if(!strcmp(element_name, "privacy")) { |
1311 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_PRIVACY)); | |
1312 } else if(!strcmp(element_name, "account")) { | |
1313 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_ACCOUNT)); | |
1314 for(i=0; attribute_names[i]; i++) { | |
1315 if(!strcmp(attribute_names[i], "protocol")) | |
1316 blist_parser_account_protocol = atoi(attribute_values[i]); | |
1317 else if(!strcmp(attribute_names[i], "mode")) | |
1318 blist_parser_privacy_mode = atoi(attribute_values[i]); | |
1319 else if(!strcmp(attribute_names[i], "name")) { | |
1320 g_free(blist_parser_account_name); | |
1321 blist_parser_account_name = g_strdup(attribute_values[i]); | |
1322 } | |
1323 } | |
1324 } else if(!strcmp(element_name, "permit")) { | |
1325 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_PERMIT)); | |
1326 } else if(!strcmp(element_name, "block")) { | |
1327 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_BLOCK)); | |
1328 } else if(!strcmp(element_name, "ignore")) { | |
1329 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_IGNORE)); | |
1330 } | |
1331 } | |
1332 | |
1333 static void blist_end_element_handler(GMarkupParseContext *context, | |
1334 const gchar *element_name, gpointer user_data, GError **error) { | |
1335 if(!strcmp(element_name, "gaim")) { | |
1336 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1337 } else if(!strcmp(element_name, "blist")) { | |
1338 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1339 } else if(!strcmp(element_name, "group")) { | |
1340 if(blist_parser_group_settings) { | |
1341 struct group *g = gaim_find_group(blist_parser_group_name); | |
1342 g_hash_table_destroy(g->settings); | |
1343 g->settings = blist_parser_group_settings; | |
1344 } | |
1345 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1346 blist_parser_group_settings = NULL; | |
5234 | 1347 } else if(!strcmp(element_name, "chat")) { |
1348 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1349 blist_parser_account_protocol); | |
5237 | 1350 if(account) { |
5234 | 1351 struct chat *chat = gaim_chat_new(account, blist_parser_chat_alias, blist_parser_chat_components); |
1352 struct group *g = gaim_find_group(blist_parser_group_name); | |
1353 gaim_blist_add_chat(chat,g,NULL); | |
1354 } | |
1355 g_free(blist_parser_chat_alias); | |
1356 blist_parser_chat_alias = NULL; | |
1357 g_free(blist_parser_account_name); | |
1358 blist_parser_account_name = NULL; | |
1359 blist_parser_chat_components = NULL; | |
1360 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
5228 | 1361 } else if(!strcmp(element_name, "person")) { |
1362 g_free(blist_parser_person_name); | |
1363 blist_parser_person_name = NULL; | |
1364 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1365 } else if(!strcmp(element_name, "buddy")) { | |
1366 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1367 blist_parser_account_protocol); | |
1368 if(account) { | |
1369 struct buddy *b = gaim_buddy_new(account, blist_parser_buddy_name, blist_parser_buddy_alias); | |
1370 struct group *g = gaim_find_group(blist_parser_group_name); | |
1371 gaim_blist_add_buddy(b,g,NULL); | |
1372 if(blist_parser_buddy_settings) { | |
1373 g_hash_table_destroy(b->settings); | |
1374 b->settings = blist_parser_buddy_settings; | |
1375 } | |
1376 } | |
1377 g_free(blist_parser_buddy_name); | |
1378 blist_parser_buddy_name = NULL; | |
1379 g_free(blist_parser_buddy_alias); | |
1380 blist_parser_buddy_alias = NULL; | |
1381 g_free(blist_parser_account_name); | |
1382 blist_parser_account_name = NULL; | |
1383 blist_parser_buddy_settings = NULL; | |
1384 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1385 } else if(!strcmp(element_name, "name")) { | |
1386 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1387 } else if(!strcmp(element_name, "alias")) { | |
1388 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
5234 | 1389 } else if(!strcmp(element_name, "component")) { |
1390 if(!blist_parser_chat_components) | |
1391 blist_parser_chat_components = g_hash_table_new_full(g_str_hash, | |
1392 g_str_equal, g_free, g_free); | |
1393 if(blist_parser_component_name && blist_parser_component_value) { | |
1394 g_hash_table_replace(blist_parser_chat_components, | |
1395 g_strdup(blist_parser_component_name), | |
1396 g_strdup(blist_parser_component_value)); | |
1397 } | |
1398 g_free(blist_parser_component_name); | |
1399 g_free(blist_parser_component_value); | |
1400 blist_parser_component_name = NULL; | |
1401 blist_parser_component_value = NULL; | |
1402 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
5228 | 1403 } else if(!strcmp(element_name, "setting")) { |
1404 if(GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_BUDDY) { | |
1405 if(!blist_parser_buddy_settings) | |
1406 blist_parser_buddy_settings = g_hash_table_new_full(g_str_hash, | |
1407 g_str_equal, g_free, g_free); | |
1408 if(blist_parser_setting_name && blist_parser_setting_value) { | |
1409 g_hash_table_replace(blist_parser_buddy_settings, | |
1410 g_strdup(blist_parser_setting_name), | |
1411 g_strdup(blist_parser_setting_value)); | |
1412 } | |
1413 } else if(GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_GROUP) { | |
1414 if(!blist_parser_group_settings) | |
1415 blist_parser_group_settings = g_hash_table_new_full(g_str_hash, | |
1416 g_str_equal, g_free, g_free); | |
1417 if(blist_parser_setting_name && blist_parser_setting_value) { | |
1418 g_hash_table_replace(blist_parser_group_settings, | |
1419 g_strdup(blist_parser_setting_name), | |
1420 g_strdup(blist_parser_setting_value)); | |
1421 } | |
1422 } | |
1423 g_free(blist_parser_setting_name); | |
1424 g_free(blist_parser_setting_value); | |
1425 blist_parser_setting_name = NULL; | |
1426 blist_parser_setting_value = NULL; | |
1427 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1428 } else if(!strcmp(element_name, "privacy")) { | |
1429 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1430 } else if(!strcmp(element_name, "account")) { | |
1431 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1432 blist_parser_account_protocol); | |
1433 if(account) { | |
1434 account->permdeny = blist_parser_privacy_mode; | |
1435 } | |
1436 g_free(blist_parser_account_name); | |
1437 blist_parser_account_name = NULL; | |
1438 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1439 } else if(!strcmp(element_name, "permit")) { | |
1440 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1441 blist_parser_account_protocol); | |
1442 if(account) { | |
1443 gaim_privacy_permit_add(account, blist_parser_buddy_name); | |
1444 } | |
1445 g_free(blist_parser_buddy_name); | |
1446 blist_parser_buddy_name = NULL; | |
1447 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1448 } else if(!strcmp(element_name, "block")) { | |
1449 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1450 blist_parser_account_protocol); | |
1451 if(account) { | |
1452 gaim_privacy_deny_add(account, blist_parser_buddy_name); | |
1453 } | |
1454 g_free(blist_parser_buddy_name); | |
1455 blist_parser_buddy_name = NULL; | |
1456 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1457 } else if(!strcmp(element_name, "ignore")) { | |
1458 /* we'll apparently do something with this later */ | |
1459 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1460 } | |
1461 } | |
1462 | |
1463 static void blist_text_handler(GMarkupParseContext *context, const gchar *text, | |
1464 gsize text_len, gpointer user_data, GError **error) { | |
1465 switch(GPOINTER_TO_INT(tag_stack->data)) { | |
1466 case BLIST_TAG_NAME: | |
1467 blist_parser_buddy_name = g_strndup(text, text_len); | |
1468 break; | |
1469 case BLIST_TAG_ALIAS: | |
5234 | 1470 if(tag_stack->next && |
1471 GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_BUDDY) | |
1472 blist_parser_buddy_alias = g_strndup(text, text_len); | |
1473 else if(tag_stack->next && | |
1474 GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_CHAT) | |
1475 blist_parser_chat_alias = g_strndup(text, text_len); | |
5228 | 1476 break; |
1477 case BLIST_TAG_PERMIT: | |
1478 case BLIST_TAG_BLOCK: | |
1479 case BLIST_TAG_IGNORE: | |
1480 blist_parser_buddy_name = g_strndup(text, text_len); | |
1481 break; | |
5234 | 1482 case BLIST_TAG_COMPONENT: |
1483 blist_parser_component_value = g_strndup(text, text_len); | |
1484 break; | |
5228 | 1485 case BLIST_TAG_SETTING: |
1486 blist_parser_setting_value = g_strndup(text, text_len); | |
1487 break; | |
1488 default: | |
1489 break; | |
1490 } | |
1491 } | |
1492 | |
1493 static void blist_error_handler(GMarkupParseContext *context, GError *error, | |
1494 gpointer user_data) { | |
1495 blist_parser_error_occurred = TRUE; | |
1496 gaim_debug(GAIM_DEBUG_ERROR, "blist import", | |
1497 "Error parsing blist.xml: %s\n", error->message); | |
1498 } | |
1499 | |
1500 static GMarkupParser blist_parser = { | |
1501 blist_start_element_handler, | |
1502 blist_end_element_handler, | |
1503 blist_text_handler, | |
1504 NULL, | |
1505 blist_error_handler | |
1506 }; | |
1507 | |
1508 static gboolean gaim_blist_read(const char *filename) { | |
1509 gchar *contents = NULL; | |
1510 gsize length; | |
1511 GMarkupParseContext *context; | |
1512 GError *error = NULL; | |
1513 | |
1514 gaim_debug(GAIM_DEBUG_INFO, "blist import", | |
1515 "Reading %s\n", filename); | |
1516 if(!g_file_get_contents(filename, &contents, &length, &error)) { | |
1517 gaim_debug(GAIM_DEBUG_ERROR, "blist import", | |
1518 "Error reading blist: %s\n", error->message); | |
1519 g_error_free(error); | |
1520 return FALSE; | |
1521 } | |
1522 | |
1523 context = g_markup_parse_context_new(&blist_parser, 0, NULL, NULL); | |
1524 | |
1525 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
1526 g_markup_parse_context_free(context); | |
1527 g_free(contents); | |
1528 return FALSE; | |
1529 } | |
1530 | |
1531 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
1532 gaim_debug(GAIM_DEBUG_ERROR, "blist import", | |
1533 "Error parsing %s\n", filename); | |
1534 g_markup_parse_context_free(context); | |
1535 g_free(contents); | |
1536 return FALSE; | |
1537 } | |
1538 | |
1539 g_markup_parse_context_free(context); | |
1540 g_free(contents); | |
1541 | |
1542 if(blist_parser_error_occurred) | |
1543 return FALSE; | |
1544 | |
1545 gaim_debug(GAIM_DEBUG_INFO, "blist import", "Finished reading %s\n", | |
1546 filename); | |
1547 | |
1548 return TRUE; | |
1549 } | |
1550 | |
1551 void gaim_blist_load() { | |
1552 GSList *accts; | |
1553 char *user_dir = gaim_user_dir(); | |
1554 char *filename; | |
1555 char *msg; | |
1556 | |
1557 blist_safe_to_write = TRUE; | |
1558 | |
1559 if(!user_dir) | |
1560 return; | |
1561 | |
1562 filename = g_build_filename(user_dir, "blist.xml", NULL); | |
1563 | |
1564 if(g_file_test(filename, G_FILE_TEST_EXISTS)) { | |
1565 if(!gaim_blist_read(filename)) { | |
1566 msg = g_strdup_printf(_("An error was encountered parsing your " | |
1567 "buddy list. It has not been loaded.")); | |
1568 do_error_dialog(_("Buddy List Error"), msg, GAIM_ERROR); | |
1569 g_free(msg); | |
1570 } | |
1571 } else if(g_slist_length(gaim_accounts)) { | |
1572 /* rob wants to inform the user that their buddy lists are | |
1573 * being converted */ | |
1574 msg = g_strdup_printf(_("Gaim is converting your old buddy lists " | |
1575 "to a new format, which will now be located at %s"), | |
1576 filename); | |
1577 do_error_dialog(_("Converting Buddy List"), msg, GAIM_INFO); | |
1578 g_free(msg); | |
1579 | |
1580 /* now, let gtk actually display the dialog before we start anything */ | |
1581 while(gtk_events_pending()) | |
1582 gtk_main_iteration(); | |
1583 | |
1584 /* read in the old lists, then save to the new format */ | |
1585 for(accts = gaim_accounts; accts; accts = accts->next) { | |
1586 do_import(accts->data, NULL); | |
1587 } | |
1588 gaim_blist_save(); | |
1589 } | |
1590 | |
1591 g_free(filename); | |
1592 } | |
1593 | |
1594 static void blist_print_group_settings(gpointer key, gpointer data, | |
1595 gpointer user_data) { | |
1596 char *key_val; | |
1597 char *data_val; | |
1598 FILE *file = user_data; | |
1599 | |
1600 if(!key || !data) | |
1601 return; | |
1602 | |
1603 key_val = g_markup_escape_text(key, -1); | |
1604 data_val = g_markup_escape_text(data, -1); | |
1605 | |
1606 fprintf(file, "\t\t\t<setting name=\"%s\">%s</setting>\n", key_val, | |
1607 data_val); | |
1608 g_free(key_val); | |
1609 g_free(data_val); | |
1610 } | |
1611 | |
1612 static void blist_print_buddy_settings(gpointer key, gpointer data, | |
1613 gpointer user_data) { | |
1614 char *key_val; | |
1615 char *data_val; | |
1616 FILE *file = user_data; | |
1617 | |
1618 if(!key || !data) | |
1619 return; | |
1620 | |
1621 key_val = g_markup_escape_text(key, -1); | |
1622 data_val = g_markup_escape_text(data, -1); | |
1623 | |
1624 fprintf(file, "\t\t\t\t\t<setting name=\"%s\">%s</setting>\n", key_val, | |
1625 data_val); | |
1626 g_free(key_val); | |
1627 g_free(data_val); | |
1628 } | |
1629 | |
5234 | 1630 static void blist_print_chat_components(gpointer key, gpointer data, |
1631 gpointer user_data) { | |
1632 char *key_val; | |
1633 char *data_val; | |
1634 FILE *file = user_data; | |
1635 | |
1636 if(!key || !data) | |
1637 return; | |
1638 | |
1639 key_val = g_markup_escape_text(key, -1); | |
1640 data_val = g_markup_escape_text(data, -1); | |
1641 | |
1642 fprintf(file, "\t\t\t\t<component name=\"%s\">%s</component>\n", key_val, | |
1643 data_val); | |
1644 g_free(key_val); | |
1645 g_free(data_val); | |
1646 } | |
1647 | |
5228 | 1648 static void gaim_blist_write(FILE *file, struct gaim_account *exp_acct) { |
1649 GSList *accounts, *buds; | |
1650 GaimBlistNode *gnode,*bnode; | |
1651 struct group *group; | |
1652 struct buddy *bud; | |
1653 fprintf(file, "<?xml version='1.0' encoding='UTF-8' ?>\n"); | |
1654 fprintf(file, "<gaim version=\"1\">\n"); | |
1655 fprintf(file, "\t<blist>\n"); | |
1656 | |
1657 for(gnode = gaimbuddylist->root; gnode; gnode = gnode->next) { | |
1658 if(!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
1659 continue; | |
1660 group = (struct group *)gnode; | |
1661 if(!exp_acct || gaim_group_on_account(group, exp_acct)) { | |
1662 char *group_name = g_markup_escape_text(group->name, -1); | |
1663 fprintf(file, "\t\t<group name=\"%s\">\n", group_name); | |
1664 g_hash_table_foreach(group->settings, blist_print_group_settings, file); | |
1665 for(bnode = gnode->child; bnode; bnode = bnode->next) { | |
5234 | 1666 if(GAIM_BLIST_NODE_IS_BUDDY(bnode)) { |
1667 bud = (struct buddy *)bnode; | |
1668 if(!exp_acct || bud->account == exp_acct) { | |
1669 char *bud_name = g_markup_escape_text(bud->name, -1); | |
1670 char *bud_alias = NULL; | |
1671 char *acct_name = g_markup_escape_text(bud->account->username, -1); | |
1672 if(bud->alias) | |
1673 bud_alias= g_markup_escape_text(bud->alias, -1); | |
1674 fprintf(file, "\t\t\t<person name=\"%s\">\n", | |
1675 bud_alias ? bud_alias : bud_name); | |
1676 fprintf(file, "\t\t\t\t<buddy protocol=\"%d\" " | |
1677 "account=\"%s\">\n", bud->account->protocol, | |
1678 acct_name); | |
1679 fprintf(file, "\t\t\t\t\t<name>%s</name>\n", bud_name); | |
1680 if(bud_alias) { | |
1681 fprintf(file, "\t\t\t\t\t<alias>%s</alias>\n", | |
1682 bud_alias); | |
1683 } | |
1684 g_hash_table_foreach(bud->settings, | |
1685 blist_print_buddy_settings, file); | |
1686 fprintf(file, "\t\t\t\t</buddy>\n"); | |
1687 fprintf(file, "\t\t\t</person>\n"); | |
1688 g_free(bud_name); | |
1689 g_free(bud_alias); | |
1690 g_free(acct_name); | |
5228 | 1691 } |
5234 | 1692 } else if(GAIM_BLIST_NODE_IS_CHAT(bnode)) { |
1693 struct chat *chat = (struct chat *)bnode; | |
1694 if(!exp_acct || chat->account == exp_acct) { | |
1695 char *acct_name = g_markup_escape_text(chat->account->username, -1); | |
1696 fprintf(file, "\t\t\t<chat protocol=\"%d\" account=\"%s\">\n", chat->account->protocol, acct_name); | |
5237 | 1697 if(chat->alias) { |
1698 char *chat_alias = g_markup_escape_text(chat->alias, -1); | |
1699 fprintf(file, "\t\t\t\t<alias>%s</alias>\n", chat_alias); | |
1700 g_free(chat_alias); | |
1701 } | |
5234 | 1702 g_hash_table_foreach(chat->components, |
1703 blist_print_chat_components, file); | |
1704 fprintf(file, "\t\t\t</chat>\n"); | |
5237 | 1705 g_free(acct_name); |
5234 | 1706 } |
5228 | 1707 } |
1708 } | |
1709 fprintf(file, "\t\t</group>\n"); | |
1710 g_free(group_name); | |
1711 } | |
1712 } | |
1713 | |
1714 fprintf(file, "\t</blist>\n"); | |
1715 fprintf(file, "\t<privacy>\n"); | |
1716 | |
1717 for(accounts = gaim_accounts; accounts; accounts = accounts->next) { | |
1718 struct gaim_account *account = accounts->data; | |
1719 char *acct_name = g_markup_escape_text(account->username, -1); | |
1720 if(!exp_acct || account == exp_acct) { | |
1721 fprintf(file, "\t\t<account protocol=\"%d\" name=\"%s\" " | |
1722 "mode=\"%d\">\n", account->protocol, acct_name, account->permdeny); | |
1723 for(buds = account->permit; buds; buds = buds->next) { | |
1724 char *bud_name = g_markup_escape_text(buds->data, -1); | |
1725 fprintf(file, "\t\t\t<permit>%s</permit>\n", bud_name); | |
1726 g_free(bud_name); | |
1727 } | |
1728 for(buds = account->deny; buds; buds = buds->next) { | |
1729 char *bud_name = g_markup_escape_text(buds->data, -1); | |
1730 fprintf(file, "\t\t\t<block>%s</block>\n", bud_name); | |
1731 g_free(bud_name); | |
1732 } | |
1733 fprintf(file, "\t\t</account>\n"); | |
1734 } | |
1735 g_free(acct_name); | |
1736 } | |
1737 | |
1738 fprintf(file, "\t</privacy>\n"); | |
1739 fprintf(file, "</gaim>\n"); | |
1740 } | |
1741 | |
1742 void gaim_blist_save() { | |
1743 FILE *file; | |
1744 char *user_dir = gaim_user_dir(); | |
1745 char *filename; | |
1746 char *filename_real; | |
1747 | |
1748 if(!user_dir) | |
1749 return; | |
1750 if(!blist_safe_to_write) { | |
1751 gaim_debug(GAIM_DEBUG_WARNING, "blist save", | |
1752 "AHH!! Tried to write the blist before we read it!\n"); | |
1753 return; | |
1754 } | |
1755 | |
1756 file = fopen(user_dir, "r"); | |
1757 if(!file) | |
1758 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
1759 else | |
1760 fclose(file); | |
1761 | |
1762 filename = g_build_filename(user_dir, "blist.xml.save", NULL); | |
1763 | |
1764 if((file = fopen(filename, "w"))) { | |
1765 gaim_blist_write(file, NULL); | |
1766 fclose(file); | |
1767 chmod(filename, S_IRUSR | S_IWUSR); | |
1768 } else { | |
1769 gaim_debug(GAIM_DEBUG_ERROR, "blist save", "Unable to write %s\n", | |
1770 filename); | |
1771 } | |
1772 | |
1773 filename_real = g_build_filename(user_dir, "blist.xml", NULL); | |
1774 | |
1775 if(rename(filename, filename_real) < 0) | |
1776 gaim_debug(GAIM_DEBUG_ERROR, "blist save", | |
1777 "Error renaming %s to %s\n", filename, filename_real); | |
1778 | |
1779 | |
1780 g_free(filename); | |
1781 g_free(filename_real); | |
1782 } | |
1783 | |
1784 gboolean gaim_privacy_permit_add(struct gaim_account *account, const char *who) { | |
1785 GSList *d = account->permit; | |
1786 char *n = g_strdup(normalize(who)); | |
1787 while(d) { | |
1788 if(!gaim_utf8_strcasecmp(n, normalize(d->data))) | |
1789 break; | |
1790 d = d->next; | |
1791 } | |
1792 g_free(n); | |
1793 if(!d) { | |
1794 account->permit = g_slist_append(account->permit, g_strdup(who)); | |
1795 return TRUE; | |
1796 } | |
1797 | |
1798 return FALSE; | |
1799 } | |
1800 | |
1801 gboolean gaim_privacy_permit_remove(struct gaim_account *account, const char *who) { | |
1802 GSList *d = account->permit; | |
1803 char *n = g_strdup(normalize(who)); | |
1804 while(d) { | |
1805 if(!gaim_utf8_strcasecmp(n, normalize(d->data))) | |
1806 break; | |
1807 d = d->next; | |
1808 } | |
1809 g_free(n); | |
1810 if(d) { | |
1811 account->permit = g_slist_remove(account->permit, d->data); | |
1812 g_free(d->data); | |
1813 return TRUE; | |
1814 } | |
1815 return FALSE; | |
1816 } | |
1817 | |
1818 gboolean gaim_privacy_deny_add(struct gaim_account *account, const char *who) { | |
1819 GSList *d = account->deny; | |
1820 char *n = g_strdup(normalize(who)); | |
1821 while(d) { | |
1822 if(!gaim_utf8_strcasecmp(n, normalize(d->data))) | |
1823 break; | |
1824 d = d->next; | |
1825 } | |
1826 g_free(n); | |
1827 if(!d) { | |
1828 account->deny = g_slist_append(account->deny, g_strdup(who)); | |
1829 return TRUE; | |
1830 } | |
1831 | |
1832 return FALSE; | |
1833 } | |
1834 | |
1835 gboolean gaim_privacy_deny_remove(struct gaim_account *account, const char *who) { | |
1836 GSList *d = account->deny; | |
1837 char *n = g_strdup(normalize(who)); | |
1838 while(d) { | |
1839 if(!gaim_utf8_strcasecmp(n, normalize(d->data))) | |
1840 break; | |
1841 d = d->next; | |
1842 } | |
1843 g_free(n); | |
1844 if(d) { | |
1845 account->deny = g_slist_remove(account->deny, d->data); | |
1846 g_free(d->data); | |
1847 return TRUE; | |
1848 } | |
1849 return FALSE; | |
1850 } | |
1851 | |
1852 void gaim_group_set_setting(struct group *g, const char *key, | |
1853 const char *value) { | |
1854 if(!g) | |
1855 return; | |
1856 g_hash_table_replace(g->settings, g_strdup(key), g_strdup(value)); | |
1857 } | |
1858 | |
1859 char *gaim_group_get_setting(struct group *g, const char *key) { | |
1860 if(!g) | |
1861 return NULL; | |
1862 return g_strdup(g_hash_table_lookup(g->settings, key)); | |
1863 } | |
1864 | |
1865 void gaim_buddy_set_setting(struct buddy *b, const char *key, | |
1866 const char *value) { | |
1867 if(!b) | |
1868 return; | |
1869 g_hash_table_replace(b->settings, g_strdup(key), g_strdup(value)); | |
1870 } | |
1871 | |
1872 char *gaim_buddy_get_setting(struct buddy *b, const char *key) { | |
1873 if(!b) | |
1874 return NULL; | |
1875 return g_strdup(g_hash_table_lookup(b->settings, key)); | |
1876 } | |
1877 | |
1878 void gaim_set_blist_ui_ops(struct gaim_blist_ui_ops *ops) | |
1879 { | |
1880 blist_ui_ops = ops; | |
1881 } | |
1882 | |
1883 struct gaim_blist_ui_ops * | |
1884 gaim_get_blist_ui_ops(void) | |
1885 { | |
1886 return blist_ui_ops; | |
1887 } | |
1888 | |
1889 int gaim_blist_get_group_size(struct group *group, gboolean offline) { | |
1890 if(!group) | |
1891 return 0; | |
1892 | |
5277 | 1893 return offline ? group->totalsize : group->currentsize; |
5228 | 1894 } |
1895 | |
1896 int gaim_blist_get_group_online_count(struct group *group) { | |
1897 if(!group) | |
1898 return 0; | |
1899 | |
5277 | 1900 return group->online; |
5228 | 1901 } |
1902 | |
1903 |