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