comparison src/blist.c @ 5228:1a53330dfd34

[gaim-migrate @ 5598] People have been talking about doing it, but nobody has done it. Since I want to clean up some header files today: list.[ch] -> blist.[ch] gtklist.h -> gtkblist.h buddy.c -> gtkblist.c committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Sat, 26 Apr 2003 17:56:23 +0000
parents
children 890b29f00b68
comparison
equal deleted inserted replaced
5227:6d1707dc8c3d 5228:1a53330dfd34
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
68 /*****************************************************************************
69 * Public API functions *
70 *****************************************************************************/
71
72 struct gaim_buddy_list *gaim_blist_new()
73 {
74 struct gaim_buddy_list *gbl = g_new0(struct gaim_buddy_list, 1);
75
76 gbl->ui_ops = gaim_get_blist_ui_ops();
77
78 if (gbl->ui_ops != NULL && gbl->ui_ops->new_list != NULL)
79 gbl->ui_ops->new_list(gbl);
80
81 return gbl;
82 }
83
84 void
85 gaim_set_blist(struct gaim_buddy_list *list)
86 {
87 gaimbuddylist = list;
88 }
89
90 struct gaim_buddy_list *
91 gaim_get_blist(void)
92 {
93 return gaimbuddylist;
94 }
95
96 void gaim_blist_show ()
97 {
98 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
99 if (ops)
100 ops->show(gaimbuddylist);
101 }
102
103 void gaim_blist_destroy()
104 {
105 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
106 if (ops)
107 ops->destroy(gaimbuddylist);
108 }
109
110 void gaim_blist_set_visible (gboolean show)
111 {
112 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
113 if (ops)
114 ops->set_visible(gaimbuddylist, show);
115 }
116
117 void gaim_blist_update_buddy_status (struct buddy *buddy, int status)
118 {
119 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
120 buddy->uc = status;
121
122 if(!(status & UC_UNAVAILABLE))
123 gaim_event_broadcast(event_buddy_back, buddy->account->gc, buddy->name);
124 else
125 gaim_event_broadcast(event_buddy_away, buddy->account->gc, buddy->name);
126
127 if (ops)
128 ops->update(gaimbuddylist, (GaimBlistNode*)buddy);
129 }
130
131 static gboolean presence_update_timeout_cb(struct buddy *buddy) {
132 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
133
134 if(buddy->present == GAIM_BUDDY_SIGNING_ON) {
135 buddy->present = GAIM_BUDDY_ONLINE;
136 gaim_event_broadcast(event_buddy_signon, buddy->account->gc, buddy->name);
137 } else if(buddy->present == GAIM_BUDDY_SIGNING_OFF) {
138 buddy->present = GAIM_BUDDY_OFFLINE;
139 }
140
141 buddy->timer = 0;
142
143 if (ops)
144 ops->update(gaimbuddylist, (GaimBlistNode*)buddy);
145
146 return FALSE;
147 }
148
149 void gaim_blist_update_buddy_presence(struct buddy *buddy, int presence) {
150 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
151 gboolean do_timer = FALSE;
152
153 if (!GAIM_BUDDY_IS_ONLINE(buddy) && presence) {
154 buddy->present = GAIM_BUDDY_SIGNING_ON;
155 gaim_event_broadcast(event_buddy_signon, buddy->account->gc, buddy->name);
156 do_timer = TRUE;
157 } else if(GAIM_BUDDY_IS_ONLINE(buddy) && !presence) {
158 buddy->present = GAIM_BUDDY_SIGNING_OFF;
159 gaim_event_broadcast(event_buddy_signoff, buddy->account->gc, buddy->name);
160 do_timer = TRUE;
161 }
162
163 if(do_timer) {
164 if(buddy->timer > 0)
165 g_source_remove(buddy->timer);
166 buddy->timer = g_timeout_add(10000, (GSourceFunc)presence_update_timeout_cb, buddy);
167 }
168
169 if (ops)
170 ops->update(gaimbuddylist, (GaimBlistNode*)buddy);
171 }
172
173
174 void gaim_blist_update_buddy_idle (struct buddy *buddy, int idle)
175 {
176 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
177 buddy->idle = idle;
178 if (ops)
179 ops->update(gaimbuddylist, (GaimBlistNode*)buddy);
180 }
181 void gaim_blist_update_buddy_evil (struct buddy *buddy, int warning)
182 {
183 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
184 buddy->evil = warning;
185 if (ops)
186 ops->update(gaimbuddylist,(GaimBlistNode*)buddy);
187 }
188 void gaim_blist_update_buddy_icon(struct buddy *buddy) {
189 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
190 if(ops)
191 ops->update(gaimbuddylist, (GaimBlistNode*)buddy);
192 }
193 void gaim_blist_rename_buddy (struct buddy *buddy, const char *name)
194 {
195 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
196 g_free(buddy->name);
197 buddy->name = g_strdup(name);
198 if (ops)
199 ops->update(gaimbuddylist, (GaimBlistNode*)buddy);
200 }
201 void gaim_blist_alias_buddy (struct buddy *buddy, const char *alias)
202 {
203 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
204 struct gaim_conversation *conv;
205
206 g_free(buddy->alias);
207
208 if(alias && strlen(alias))
209 buddy->alias = g_strdup(alias);
210 else
211 buddy->alias = NULL;
212
213 if (ops)
214 ops->update(gaimbuddylist, (GaimBlistNode*)buddy);
215
216 conv = gaim_find_conversation_with_account(buddy->name, buddy->account);
217
218 if (conv)
219 gaim_conversation_autoset_title(conv);
220 }
221
222 void gaim_blist_rename_group(struct group *group, const char *name)
223 {
224 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
225 g_free(group->name);
226 group->name = g_strdup(name);
227 if (ops)
228 ops->update(gaimbuddylist, (GaimBlistNode*)group);
229 }
230 struct buddy *gaim_buddy_new(struct gaim_account *account, const char *screenname, const char *alias)
231 {
232 struct buddy *b;
233 struct gaim_blist_ui_ops *ops;
234
235 b = g_new0(struct buddy, 1);
236 b->account = account;
237 b->name = g_strdup(screenname);
238 b->alias = g_strdup(alias);
239 b->settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
240 ((GaimBlistNode*)b)->type = GAIM_BLIST_BUDDY_NODE;
241
242 ops = gaim_get_blist_ui_ops();
243
244 if (ops != NULL && ops->new_node != NULL)
245 ops->new_node((GaimBlistNode *)b);
246
247 return b;
248 }
249 void gaim_blist_add_buddy (struct buddy *buddy, struct group *group, GaimBlistNode *node)
250 {
251 GaimBlistNode *n = node, *bnode = (GaimBlistNode*)buddy;
252 struct group *g = group;
253 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
254 gboolean save = FALSE;
255
256 if (!n) {
257 if (!g) {
258 g = gaim_group_new(_("Buddies"));
259 gaim_blist_add_group(g, NULL);
260 }
261 n = gaim_blist_get_last_child((GaimBlistNode*)g);
262 } else {
263 g = (struct group*)n->parent;
264 }
265
266 /* if we're moving to overtop of ourselves, do nothing */
267 if(bnode == n)
268 return;
269
270 if (bnode->parent) {
271 /* This buddy was already in the list and is
272 * being moved.
273 */
274 if(bnode->next)
275 bnode->next->prev = bnode->prev;
276 if(bnode->prev)
277 bnode->prev->next = bnode->next;
278 if(bnode->parent->child == bnode)
279 bnode->parent->child = bnode->next;
280
281 ops->remove(gaimbuddylist, bnode);
282
283 if (bnode->parent != ((GaimBlistNode*)g))
284 serv_move_buddy(buddy, (struct group*)bnode->parent, g);
285 save = TRUE;
286 }
287
288 if (n) {
289 if(n->next)
290 n->next->prev = (GaimBlistNode*)buddy;
291 ((GaimBlistNode*)buddy)->next = n->next;
292 ((GaimBlistNode*)buddy)->prev = n;
293 ((GaimBlistNode*)buddy)->parent = n->parent;
294 n->next = (GaimBlistNode*)buddy;
295 } else {
296 ((GaimBlistNode*)g)->child = (GaimBlistNode*)buddy;
297 ((GaimBlistNode*)buddy)->next = NULL;
298 ((GaimBlistNode*)buddy)->prev = NULL;
299 ((GaimBlistNode*)buddy)->parent = (GaimBlistNode*)g;
300 }
301
302 if (ops)
303 ops->update(gaimbuddylist, (GaimBlistNode*)buddy);
304 if (save)
305 gaim_blist_save();
306 }
307
308 struct group *gaim_group_new(const char *name)
309 {
310 struct group *g = gaim_find_group(name);
311
312 if (!g) {
313 struct gaim_blist_ui_ops *ops;
314 g= g_new0(struct group, 1);
315 g->name = g_strdup(name);
316 g->settings = g_hash_table_new_full(g_str_hash, g_str_equal,
317 g_free, g_free);
318 ((GaimBlistNode*)g)->type = GAIM_BLIST_GROUP_NODE;
319
320 ops = gaim_get_blist_ui_ops();
321
322 if (ops != NULL && ops->new_node != NULL)
323 ops->new_node((GaimBlistNode *)g);
324
325 }
326 return g;
327 }
328
329 void gaim_blist_add_group (struct group *group, GaimBlistNode *node)
330 {
331 struct gaim_blist_ui_ops *ops;
332 GaimBlistNode *gnode = (GaimBlistNode*)group;
333 gboolean save = FALSE;
334
335 if (!gaimbuddylist)
336 gaimbuddylist = gaim_blist_new();
337 ops = gaimbuddylist->ui_ops;
338
339 if (!gaimbuddylist->root) {
340 gaimbuddylist->root = gnode;
341 return;
342 }
343
344 if (!node)
345 node = gaim_blist_get_last_sibling(gaimbuddylist->root);
346
347 /* if we're moving to overtop of ourselves, do nothing */
348 if(gnode == node)
349 return;
350
351 if (gaim_find_group(group->name)) {
352 /* This is just being moved */
353
354 ops->remove(gaimbuddylist, (GaimBlistNode*)group);
355
356 if(gnode == gaimbuddylist->root)
357 gaimbuddylist->root = gnode->next;
358 if(gnode->prev)
359 gnode->prev->next = gnode->next;
360 if(gnode->next)
361 gnode->next->prev = gnode->prev;
362
363 save = TRUE;
364 }
365
366 gnode->next = node->next;
367 gnode->prev = node;
368 if(node->next)
369 node->next->prev = gnode;
370 node->next = gnode;
371
372 if (ops) {
373 ops->update(gaimbuddylist, gnode);
374 for(node = gnode->child; node; node = node->next)
375 ops->update(gaimbuddylist, node);
376 }
377 if (save)
378 gaim_blist_save();
379 }
380
381 void gaim_blist_remove_buddy (struct buddy *buddy)
382 {
383 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
384
385 GaimBlistNode *gnode, *node = (GaimBlistNode*)buddy;
386 struct group *group;
387
388 gnode = node->parent;
389 group = (struct group *)gnode;
390
391 if(gnode->child == node)
392 gnode->child = node->next;
393 if (node->prev)
394 node->prev->next = node->next;
395 if (node->next)
396 node->next->prev = node->prev;
397
398 ops->remove(gaimbuddylist, node);
399 g_hash_table_destroy(buddy->settings);
400 g_free(buddy->name);
401 g_free(buddy->alias);
402 g_free(buddy);
403 }
404
405 void gaim_blist_remove_group (struct group *group)
406 {
407 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
408 GaimBlistNode *node = (GaimBlistNode*)group;
409
410 if(node->child) {
411 char *buf;
412 int count = 0;
413 GaimBlistNode *child = node->child;
414
415 while(child) {
416 count++;
417 child = child->next;
418 }
419
420 buf = g_strdup_printf(_("%d buddies from group %s were not "
421 "removed because their accounts were not logged in. These "
422 "buddies, and the group were not removed.\n"),
423 count, group->name);
424 do_error_dialog(_("Group Not Removed"), buf, GAIM_ERROR);
425 g_free(buf);
426 return;
427 }
428
429 if(gaimbuddylist->root == node)
430 gaimbuddylist->root = node->next;
431 if (node->prev)
432 node->prev->next = node->next;
433 if (node->next)
434 node->next->prev = node->prev;
435
436 ops->remove(gaimbuddylist, node);
437 g_free(group->name);
438 g_free(group);
439 }
440
441 char *gaim_get_buddy_alias_only(struct buddy *b) {
442 if(!b)
443 return NULL;
444 if(b->alias && b->alias[0])
445 return b->alias;
446 else if((misc_options & OPT_MISC_USE_SERVER_ALIAS) && b->server_alias)
447 return b->server_alias;
448 return NULL;
449 }
450
451 char * gaim_get_buddy_alias (struct buddy *buddy)
452 {
453 char *ret = gaim_get_buddy_alias_only(buddy);
454 if(!ret)
455 return buddy ? buddy->name : _("Unknown");
456 return ret;
457
458 }
459
460 struct buddy *gaim_find_buddy(struct gaim_account *account, const char *name)
461 {
462 GaimBlistNode *group;
463 GaimBlistNode *buddy;
464 char *norm_name = g_strdup(normalize(name));
465
466 if (!gaimbuddylist)
467 return NULL;
468
469 group = gaimbuddylist->root;
470 while (group) {
471 buddy = group->child;
472 while (buddy) {
473 if (!gaim_utf8_strcasecmp(normalize(((struct buddy*)buddy)->name), norm_name) && account == ((struct buddy*)buddy)->account) {
474 g_free(norm_name);
475 return (struct buddy*)buddy;
476 }
477 buddy = buddy->next;
478 }
479 group = group->next;
480 }
481 g_free(norm_name);
482 return NULL;
483 }
484
485 struct group *gaim_find_group(const char *name)
486 {
487 GaimBlistNode *node;
488 if (!gaimbuddylist)
489 return NULL;
490 node = gaimbuddylist->root;
491 while(node) {
492 if (!strcmp(((struct group*)node)->name, name))
493 return (struct group*)node;
494 node = node->next;
495 }
496 return NULL;
497 }
498 struct group *gaim_find_buddys_group(struct buddy *buddy)
499 {
500 if (!buddy)
501 return NULL;
502 return (struct group*)(((GaimBlistNode*)buddy)->parent);
503 }
504
505 GSList *gaim_group_get_accounts(struct group *g)
506 {
507 GSList *l = NULL;
508 GaimBlistNode *child = ((GaimBlistNode *)g)->child;
509
510 while (child) {
511 if (!g_slist_find(l, ((struct buddy*)child)->account))
512 l = g_slist_append(l, ((struct buddy*)child)->account);
513 child = child->next;
514 }
515 return l;
516 }
517
518 void gaim_blist_remove_account(struct gaim_account *account)
519 {
520 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops;
521 GaimBlistNode *group = gaimbuddylist->root;
522 GaimBlistNode *buddy;
523 if (!gaimbuddylist)
524 return;
525 while (group) {
526 buddy = group->child;
527 while (buddy) {
528 if (account == ((struct buddy*)buddy)->account) {
529 ((struct buddy*)buddy)->present = GAIM_BUDDY_OFFLINE;
530 if(ops)
531 ops->remove(gaimbuddylist, buddy);
532 }
533 buddy = buddy->next;
534 }
535 group = group->next;
536 }
537 }
538
539 void parse_toc_buddy_list(struct gaim_account *account, char *config)
540 {
541 char *c;
542 char current[256];
543 GList *bud = NULL;
544
545
546 if (config != NULL) {
547
548 /* skip "CONFIG:" (if it exists) */
549 c = strncmp(config + 6 /* sizeof(struct sflap_hdr) */ , "CONFIG:", strlen("CONFIG:")) ?
550 strtok(config, "\n") :
551 strtok(config + 6 /* sizeof(struct sflap_hdr) */ + strlen("CONFIG:"), "\n");
552 do {
553 if (c == NULL)
554 break;
555 if (*c == 'g') {
556 char *utf8 = NULL;
557 utf8 = gaim_try_conv_to_utf8(c + 2);
558 if (utf8 == NULL) {
559 g_strlcpy(current, _("Invalid Groupname"), sizeof(current));
560 } else {
561 g_strlcpy(current, utf8, sizeof(current));
562 g_free(utf8);
563 }
564 if (!gaim_find_group(current)) {
565 struct group *g = gaim_group_new(current);
566 gaim_blist_add_group(g, NULL);
567 }
568 } else if (*c == 'b') { /*&& !gaim_find_buddy(user, c + 2)) {*/
569 char nm[80], sw[388], *a, *utf8 = NULL;
570
571 if ((a = strchr(c + 2, ':')) != NULL) {
572 *a++ = '\0'; /* nul the : */
573 }
574
575 g_strlcpy(nm, c + 2, sizeof(nm));
576 if (a) {
577 utf8 = gaim_try_conv_to_utf8(a);
578 if (utf8 == NULL) {
579 gaim_debug(GAIM_DEBUG_ERROR, "toc blist",
580 "Failed to convert alias for "
581 "'%s' to UTF-8\n", nm);
582 }
583 }
584 if (utf8 == NULL) {
585 sw[0] = '\0';
586 } else {
587 /* This can leave a partial sequence at the end,
588 * but who cares? */
589 g_strlcpy(sw, utf8, sizeof(sw));
590 g_free(utf8);
591 }
592
593 if (!gaim_find_buddy(account, nm)) {
594 struct buddy *b = gaim_buddy_new(account, nm, sw);
595 struct group *g = gaim_find_group(current);
596 gaim_blist_add_buddy(b, g, NULL);
597 bud = g_list_append(bud, g_strdup(nm));
598 }
599 } else if (*c == 'p') {
600 gaim_privacy_permit_add(account, c + 2);
601 } else if (*c == 'd') {
602 gaim_privacy_deny_add(account, c + 2);
603 } else if (!strncmp("toc", c, 3)) {
604 sscanf(c + strlen(c) - 1, "%d", &account->permdeny);
605 gaim_debug(GAIM_DEBUG_MISC, "toc blist",
606 "permdeny: %d\n", account->permdeny);
607 if (account->permdeny == 0)
608 account->permdeny = 1;
609 } else if (*c == 'm') {
610 sscanf(c + 2, "%d", &account->permdeny);
611 gaim_debug(GAIM_DEBUG_MISC, "toc blist",
612 "permdeny: %d\n", account->permdeny);
613 if (account->permdeny == 0)
614 account->permdeny = 1;
615 }
616 } while ((c = strtok(NULL, "\n")));
617
618 if(account->gc) {
619 if(bud) {
620 GList *node = bud;
621 serv_add_buddies(account->gc, bud);
622 while(node) {
623 g_free(node->data);
624 node = node->next;
625 }
626 }
627 serv_set_permit_deny(account->gc);
628 }
629 g_list_free(bud);
630 }
631 }
632
633 #if 0
634 /* translate an AIM 3 buddylist (*.lst) to a Gaim buddylist */
635 static GString *translate_lst(FILE *src_fp)
636 {
637 char line[BUF_LEN], *line2;
638 char *name;
639 int i;
640
641 GString *dest = g_string_new("m 1\n");
642
643 while (fgets(line, BUF_LEN, src_fp)) {
644 line2 = g_strchug(line);
645 if (strstr(line2, "group") == line2) {
646 name = strpbrk(line2, " \t\n\r\f") + 1;
647 dest = g_string_append(dest, "g ");
648 for (i = 0; i < strcspn(name, "\n\r"); i++)
649 if (name[i] != '\"')
650 dest = g_string_append_c(dest, name[i]);
651 dest = g_string_append_c(dest, '\n');
652 }
653 if (strstr(line2, "buddy") == line2) {
654 name = strpbrk(line2, " \t\n\r\f") + 1;
655 dest = g_string_append(dest, "b ");
656 for (i = 0; i < strcspn(name, "\n\r"); i++)
657 if (name[i] != '\"')
658 dest = g_string_append_c(dest, name[i]);
659 dest = g_string_append_c(dest, '\n');
660 }
661 }
662
663 return dest;
664 }
665
666
667 /* translate an AIM 4 buddylist (*.blt) to Gaim format */
668 static GString *translate_blt(FILE *src_fp)
669 {
670 int i;
671 char line[BUF_LEN];
672 char *buddy;
673
674 GString *dest = g_string_new("m 1\n");
675
676 while (strstr(fgets(line, BUF_LEN, src_fp), "Buddy") == NULL);
677 while (strstr(fgets(line, BUF_LEN, src_fp), "list") == NULL);
678
679 while (1) {
680 fgets(line, BUF_LEN, src_fp); g_strchomp(line);
681 if (strchr(line, '}') != NULL)
682 break;
683
684 if (strchr(line, '{') != NULL) {
685 /* Syntax starting with "<group> {" */
686
687 dest = g_string_append(dest, "g ");
688 buddy = g_strchug(strtok(line, "{"));
689 for (i = 0; i < strlen(buddy); i++)
690 if (buddy[i] != '\"')
691 dest = g_string_append_c(dest, buddy[i]);
692 dest = g_string_append_c(dest, '\n');
693 while (strchr(fgets(line, BUF_LEN, src_fp), '}') == NULL) {
694 gboolean pounce = FALSE;
695 char *e;
696 g_strchomp(line);
697 buddy = g_strchug(line);
698 gaim_debug(GAIM_DEBUG_MISC, "AIM 4 blt import",
699 "buddy: \"%s\"\n", buddy);
700 dest = g_string_append(dest, "b ");
701 if (strchr(buddy, '{') != NULL) {
702 /* buddy pounce, etc */
703 char *pos = strchr(buddy, '{') - 1;
704 *pos = 0;
705 pounce = TRUE;
706 }
707 if ((e = strchr(buddy, '\"')) != NULL) {
708 *e = '\0';
709 buddy++;
710 }
711 dest = g_string_append(dest, buddy);
712 dest = g_string_append_c(dest, '\n');
713 if (pounce)
714 do
715 fgets(line, BUF_LEN, src_fp);
716 while (!strchr(line, '}'));
717 }
718 } else {
719
720 /* Syntax "group buddy buddy ..." */
721 buddy = g_strchug(strtok(line, " \n"));
722 dest = g_string_append(dest, "g ");
723 if (strchr(buddy, '\"') != NULL) {
724 dest = g_string_append(dest, &buddy[1]);
725 dest = g_string_append_c(dest, ' ');
726 buddy = g_strchug(strtok(NULL, " \n"));
727 while (strchr(buddy, '\"') == NULL) {
728 dest = g_string_append(dest, buddy);
729 dest = g_string_append_c(dest, ' ');
730 buddy = g_strchug(strtok(NULL, " \n"));
731 }
732 buddy[strlen(buddy) - 1] = '\0';
733 dest = g_string_append(dest, buddy);
734 } else {
735 dest = g_string_append(dest, buddy);
736 }
737 dest = g_string_append_c(dest, '\n');
738 while ((buddy = g_strchug(strtok(NULL, " \n"))) != NULL) {
739 dest = g_string_append(dest, "b ");
740 if (strchr(buddy, '\"') != NULL) {
741 dest = g_string_append(dest, &buddy[1]);
742 dest = g_string_append_c(dest, ' ');
743 buddy = g_strchug(strtok(NULL, " \n"));
744 while (strchr(buddy, '\"') == NULL) {
745 dest = g_string_append(dest, buddy);
746 dest = g_string_append_c(dest, ' ');
747 buddy = g_strchug(strtok(NULL, " \n"));
748 }
749 buddy[strlen(buddy) - 1] = '\0';
750 dest = g_string_append(dest, buddy);
751 } else {
752 dest = g_string_append(dest, buddy);
753 }
754 dest = g_string_append_c(dest, '\n');
755 }
756 }
757 }
758
759 return dest;
760 }
761
762 static GString *translate_gnomeicu(FILE *src_fp)
763 {
764 char line[BUF_LEN];
765 GString *dest = g_string_new("m 1\ng Buddies\n");
766
767 while (strstr(fgets(line, BUF_LEN, src_fp), "NewContacts") == NULL);
768
769 while (fgets(line, BUF_LEN, src_fp)) {
770 char *eq;
771 g_strchomp(line);
772 if (line[0] == '\n' || line[0] == '[')
773 break;
774 eq = strchr(line, '=');
775 if (!eq)
776 break;
777 *eq = ':';
778 eq = strchr(eq, ',');
779 if (eq)
780 *eq = '\0';
781 dest = g_string_append(dest, "b ");
782 dest = g_string_append(dest, line);
783 dest = g_string_append_c(dest, '\n');
784 }
785
786 return dest;
787 }
788 #endif
789
790 static gchar *get_screenname_filename(const char *name)
791 {
792 gchar **split;
793 gchar *good;
794 gchar *ret;
795
796 split = g_strsplit(name, G_DIR_SEPARATOR_S, -1);
797 good = g_strjoinv(NULL, split);
798 g_strfreev(split);
799
800 ret = g_utf8_strup(good, -1);
801
802 g_free(good);
803
804 return ret;
805 }
806
807 static gboolean gaim_blist_read(const char *filename);
808
809
810 static void do_import(struct gaim_account *account, const char *filename)
811 {
812 GString *buf = NULL;
813 char first[64];
814 char path[PATHSIZE];
815 int len;
816 FILE *f;
817 struct stat st;
818
819 if (filename) {
820 g_snprintf(path, sizeof(path), "%s", filename);
821 } else {
822 char *g_screenname = get_screenname_filename(account->username);
823 char *file = gaim_user_dir();
824 int protocol = (account->protocol == GAIM_PROTO_OSCAR) ? (isalpha(account->username[0]) ? GAIM_PROTO_TOC : GAIM_PROTO_ICQ): account->protocol;
825
826 if (file != (char *)NULL) {
827 sprintf(path, "%s" G_DIR_SEPARATOR_S "%s.%d.blist", file, g_screenname, protocol);
828 g_free(g_screenname);
829 } else {
830 g_free(g_screenname);
831 return;
832 }
833 }
834
835 if (stat(path, &st)) {
836 gaim_debug(GAIM_DEBUG_ERROR, "blist import", "Unable to stat %s.\n",
837 path);
838 return;
839 }
840
841 if (!(f = fopen(path, "r"))) {
842 gaim_debug(GAIM_DEBUG_ERROR, "blist import", "Unable to open %s.\n",
843 path);
844 return;
845 }
846
847 fgets(first, 64, f);
848
849 if ((first[0] == '\n') || (first[0] == '\r' && first[1] == '\n'))
850 fgets(first, 64, f);
851
852 #if 0
853 if (!g_strncasecmp(first, "<xml", strlen("<xml"))) {
854 /* new gaim XML buddy list */
855 gaim_blist_read(path);
856
857 /* We really don't need to bother doing stuf like translating AIM 3 buddy lists anymore */
858
859 } else if (!g_strncasecmp(first, "Config {", strlen("Config {"))) {
860 /* AIM 4 buddy list */
861 gaim_debug(GAIM_DEBUG_MISC, "blist import", "aim 4\n");
862 rewind(f);
863 buf = translate_blt(f);
864 } else if (strstr(first, "group") != NULL) {
865 /* AIM 3 buddy list */
866 gaim_debug(GAIM_DEBUG_MISC, "blist import", "aim 3\n");
867 rewind(f);
868 buf = translate_lst(f);
869 } else if (!g_strncasecmp(first, "[User]", strlen("[User]"))) {
870 /* GnomeICU (hopefully) */
871 gaim_debug(GAIM_DEBUG_MISC, "blist import", "gnomeicu\n");
872 rewind(f);
873 buf = translate_gnomeicu(f);
874
875 } else
876 #endif
877 if (first[0] == 'm') {
878 /* Gaim buddy list - no translation */
879 char buf2[BUF_LONG * 2];
880 buf = g_string_new("");
881 rewind(f);
882 while (1) {
883 len = fread(buf2, 1, BUF_LONG * 2 - 1, f);
884 if (len <= 0)
885 break;
886 buf2[len] = '\0';
887 buf = g_string_append(buf, buf2);
888 if (len != BUF_LONG * 2 - 1)
889 break;
890 }
891 }
892
893 fclose(f);
894
895 if (buf) {
896 buf = g_string_prepend(buf, "toc_set_config {");
897 buf = g_string_append(buf, "}\n");
898 parse_toc_buddy_list(account, buf->str);
899 g_string_free(buf, TRUE);
900 }
901 }
902
903 gboolean gaim_group_on_account(struct group *g, struct gaim_account *account) {
904 GaimBlistNode *bnode;
905 for(bnode = g->node.child; bnode; bnode = bnode->next) {
906 struct buddy *b = (struct buddy *)bnode;
907 if(!GAIM_BLIST_NODE_IS_BUDDY(bnode))
908 continue;
909 if((!account && b->account->gc) || b->account == account)
910 return TRUE;
911 }
912 return FALSE;
913 }
914
915 static gboolean blist_safe_to_write = FALSE;
916
917 static char *blist_parser_group_name = NULL;
918 static char *blist_parser_person_name = NULL;
919 static char *blist_parser_account_name = NULL;
920 static int blist_parser_account_protocol = 0;
921 static char *blist_parser_buddy_name = NULL;
922 static char *blist_parser_buddy_alias = NULL;
923 static char *blist_parser_setting_name = NULL;
924 static char *blist_parser_setting_value = NULL;
925 static GHashTable *blist_parser_buddy_settings = NULL;
926 static GHashTable *blist_parser_group_settings = NULL;
927 static int blist_parser_privacy_mode = 0;
928 static GList *tag_stack = NULL;
929 enum {
930 BLIST_TAG_GAIM,
931 BLIST_TAG_BLIST,
932 BLIST_TAG_GROUP,
933 BLIST_TAG_PERSON,
934 BLIST_TAG_BUDDY,
935 BLIST_TAG_NAME,
936 BLIST_TAG_ALIAS,
937 BLIST_TAG_SETTING,
938 BLIST_TAG_PRIVACY,
939 BLIST_TAG_ACCOUNT,
940 BLIST_TAG_PERMIT,
941 BLIST_TAG_BLOCK,
942 BLIST_TAG_IGNORE
943 };
944 static gboolean blist_parser_error_occurred = FALSE;
945
946 static void blist_start_element_handler (GMarkupParseContext *context,
947 const gchar *element_name,
948 const gchar **attribute_names,
949 const gchar **attribute_values,
950 gpointer user_data,
951 GError **error) {
952 int i;
953
954 if(!strcmp(element_name, "gaim")) {
955 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_GAIM));
956 } else if(!strcmp(element_name, "blist")) {
957 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_BLIST));
958 } else if(!strcmp(element_name, "group")) {
959 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_GROUP));
960 for(i=0; attribute_names[i]; i++) {
961 if(!strcmp(attribute_names[i], "name")) {
962 g_free(blist_parser_group_name);
963 blist_parser_group_name = g_strdup(attribute_values[i]);
964 }
965 }
966 if(blist_parser_group_name) {
967 struct group *g = gaim_group_new(blist_parser_group_name);
968 gaim_blist_add_group(g,NULL);
969 }
970 } else if(!strcmp(element_name, "person")) {
971 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_PERSON));
972 for(i=0; attribute_names[i]; i++) {
973 if(!strcmp(attribute_names[i], "name")) {
974 g_free(blist_parser_person_name);
975 blist_parser_person_name = g_strdup(attribute_values[i]);
976 }
977 }
978 } else if(!strcmp(element_name, "buddy")) {
979 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_BUDDY));
980 for(i=0; attribute_names[i]; i++) {
981 if(!strcmp(attribute_names[i], "account")) {
982 g_free(blist_parser_account_name);
983 blist_parser_account_name = g_strdup(attribute_values[i]);
984 } else if(!strcmp(attribute_names[i], "protocol")) {
985 blist_parser_account_protocol = atoi(attribute_values[i]);
986 }
987 }
988 } else if(!strcmp(element_name, "name")) {
989 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_NAME));
990 } else if(!strcmp(element_name, "alias")) {
991 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_ALIAS));
992 } else if(!strcmp(element_name, "setting")) {
993 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_SETTING));
994 for(i=0; attribute_names[i]; i++) {
995 if(!strcmp(attribute_names[i], "name")) {
996 g_free(blist_parser_setting_name);
997 blist_parser_setting_name = g_strdup(attribute_values[i]);
998 }
999 }
1000 } else if(!strcmp(element_name, "privacy")) {
1001 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_PRIVACY));
1002 } else if(!strcmp(element_name, "account")) {
1003 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_ACCOUNT));
1004 for(i=0; attribute_names[i]; i++) {
1005 if(!strcmp(attribute_names[i], "protocol"))
1006 blist_parser_account_protocol = atoi(attribute_values[i]);
1007 else if(!strcmp(attribute_names[i], "mode"))
1008 blist_parser_privacy_mode = atoi(attribute_values[i]);
1009 else if(!strcmp(attribute_names[i], "name")) {
1010 g_free(blist_parser_account_name);
1011 blist_parser_account_name = g_strdup(attribute_values[i]);
1012 }
1013 }
1014 } else if(!strcmp(element_name, "permit")) {
1015 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_PERMIT));
1016 } else if(!strcmp(element_name, "block")) {
1017 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_BLOCK));
1018 } else if(!strcmp(element_name, "ignore")) {
1019 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_IGNORE));
1020 }
1021 }
1022
1023 static void blist_end_element_handler(GMarkupParseContext *context,
1024 const gchar *element_name, gpointer user_data, GError **error) {
1025 if(!strcmp(element_name, "gaim")) {
1026 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1027 } else if(!strcmp(element_name, "blist")) {
1028 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1029 } else if(!strcmp(element_name, "group")) {
1030 if(blist_parser_group_settings) {
1031 struct group *g = gaim_find_group(blist_parser_group_name);
1032 g_hash_table_destroy(g->settings);
1033 g->settings = blist_parser_group_settings;
1034 }
1035 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1036 blist_parser_group_settings = NULL;
1037 } else if(!strcmp(element_name, "person")) {
1038 g_free(blist_parser_person_name);
1039 blist_parser_person_name = NULL;
1040 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1041 } else if(!strcmp(element_name, "buddy")) {
1042 struct gaim_account *account = gaim_account_find(blist_parser_account_name,
1043 blist_parser_account_protocol);
1044 if(account) {
1045 struct buddy *b = gaim_buddy_new(account, blist_parser_buddy_name, blist_parser_buddy_alias);
1046 struct group *g = gaim_find_group(blist_parser_group_name);
1047 gaim_blist_add_buddy(b,g,NULL);
1048 if(blist_parser_buddy_settings) {
1049 g_hash_table_destroy(b->settings);
1050 b->settings = blist_parser_buddy_settings;
1051 }
1052 }
1053 g_free(blist_parser_buddy_name);
1054 blist_parser_buddy_name = NULL;
1055 g_free(blist_parser_buddy_alias);
1056 blist_parser_buddy_alias = NULL;
1057 g_free(blist_parser_account_name);
1058 blist_parser_account_name = NULL;
1059 blist_parser_buddy_settings = NULL;
1060 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1061 } else if(!strcmp(element_name, "name")) {
1062 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1063 } else if(!strcmp(element_name, "alias")) {
1064 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1065 } else if(!strcmp(element_name, "setting")) {
1066 if(GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_BUDDY) {
1067 if(!blist_parser_buddy_settings)
1068 blist_parser_buddy_settings = g_hash_table_new_full(g_str_hash,
1069 g_str_equal, g_free, g_free);
1070 if(blist_parser_setting_name && blist_parser_setting_value) {
1071 g_hash_table_replace(blist_parser_buddy_settings,
1072 g_strdup(blist_parser_setting_name),
1073 g_strdup(blist_parser_setting_value));
1074 }
1075 } else if(GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_GROUP) {
1076 if(!blist_parser_group_settings)
1077 blist_parser_group_settings = g_hash_table_new_full(g_str_hash,
1078 g_str_equal, g_free, g_free);
1079 if(blist_parser_setting_name && blist_parser_setting_value) {
1080 g_hash_table_replace(blist_parser_group_settings,
1081 g_strdup(blist_parser_setting_name),
1082 g_strdup(blist_parser_setting_value));
1083 }
1084 }
1085 g_free(blist_parser_setting_name);
1086 g_free(blist_parser_setting_value);
1087 blist_parser_setting_name = NULL;
1088 blist_parser_setting_value = NULL;
1089 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1090 } else if(!strcmp(element_name, "privacy")) {
1091 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1092 } else if(!strcmp(element_name, "account")) {
1093 struct gaim_account *account = gaim_account_find(blist_parser_account_name,
1094 blist_parser_account_protocol);
1095 if(account) {
1096 account->permdeny = blist_parser_privacy_mode;
1097 }
1098 g_free(blist_parser_account_name);
1099 blist_parser_account_name = NULL;
1100 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1101 } else if(!strcmp(element_name, "permit")) {
1102 struct gaim_account *account = gaim_account_find(blist_parser_account_name,
1103 blist_parser_account_protocol);
1104 if(account) {
1105 gaim_privacy_permit_add(account, blist_parser_buddy_name);
1106 }
1107 g_free(blist_parser_buddy_name);
1108 blist_parser_buddy_name = NULL;
1109 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1110 } else if(!strcmp(element_name, "block")) {
1111 struct gaim_account *account = gaim_account_find(blist_parser_account_name,
1112 blist_parser_account_protocol);
1113 if(account) {
1114 gaim_privacy_deny_add(account, blist_parser_buddy_name);
1115 }
1116 g_free(blist_parser_buddy_name);
1117 blist_parser_buddy_name = NULL;
1118 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1119 } else if(!strcmp(element_name, "ignore")) {
1120 /* we'll apparently do something with this later */
1121 tag_stack = g_list_delete_link(tag_stack, tag_stack);
1122 }
1123 }
1124
1125 static void blist_text_handler(GMarkupParseContext *context, const gchar *text,
1126 gsize text_len, gpointer user_data, GError **error) {
1127 switch(GPOINTER_TO_INT(tag_stack->data)) {
1128 case BLIST_TAG_NAME:
1129 blist_parser_buddy_name = g_strndup(text, text_len);
1130 break;
1131 case BLIST_TAG_ALIAS:
1132 blist_parser_buddy_alias = g_strndup(text, text_len);
1133 break;
1134 case BLIST_TAG_PERMIT:
1135 case BLIST_TAG_BLOCK:
1136 case BLIST_TAG_IGNORE:
1137 blist_parser_buddy_name = g_strndup(text, text_len);
1138 break;
1139 case BLIST_TAG_SETTING:
1140 blist_parser_setting_value = g_strndup(text, text_len);
1141 break;
1142 default:
1143 break;
1144 }
1145 }
1146
1147 static void blist_error_handler(GMarkupParseContext *context, GError *error,
1148 gpointer user_data) {
1149 blist_parser_error_occurred = TRUE;
1150 gaim_debug(GAIM_DEBUG_ERROR, "blist import",
1151 "Error parsing blist.xml: %s\n", error->message);
1152 }
1153
1154 static GMarkupParser blist_parser = {
1155 blist_start_element_handler,
1156 blist_end_element_handler,
1157 blist_text_handler,
1158 NULL,
1159 blist_error_handler
1160 };
1161
1162 static gboolean gaim_blist_read(const char *filename) {
1163 gchar *contents = NULL;
1164 gsize length;
1165 GMarkupParseContext *context;
1166 GError *error = NULL;
1167
1168 gaim_debug(GAIM_DEBUG_INFO, "blist import",
1169 "Reading %s\n", filename);
1170 if(!g_file_get_contents(filename, &contents, &length, &error)) {
1171 gaim_debug(GAIM_DEBUG_ERROR, "blist import",
1172 "Error reading blist: %s\n", error->message);
1173 g_error_free(error);
1174 return FALSE;
1175 }
1176
1177 context = g_markup_parse_context_new(&blist_parser, 0, NULL, NULL);
1178
1179 if(!g_markup_parse_context_parse(context, contents, length, NULL)) {
1180 g_markup_parse_context_free(context);
1181 g_free(contents);
1182 return FALSE;
1183 }
1184
1185 if(!g_markup_parse_context_end_parse(context, NULL)) {
1186 gaim_debug(GAIM_DEBUG_ERROR, "blist import",
1187 "Error parsing %s\n", filename);
1188 g_markup_parse_context_free(context);
1189 g_free(contents);
1190 return FALSE;
1191 }
1192
1193 g_markup_parse_context_free(context);
1194 g_free(contents);
1195
1196 if(blist_parser_error_occurred)
1197 return FALSE;
1198
1199 gaim_debug(GAIM_DEBUG_INFO, "blist import", "Finished reading %s\n",
1200 filename);
1201
1202 return TRUE;
1203 }
1204
1205 void gaim_blist_load() {
1206 GSList *accts;
1207 char *user_dir = gaim_user_dir();
1208 char *filename;
1209 char *msg;
1210
1211 blist_safe_to_write = TRUE;
1212
1213 if(!user_dir)
1214 return;
1215
1216 filename = g_build_filename(user_dir, "blist.xml", NULL);
1217
1218 if(g_file_test(filename, G_FILE_TEST_EXISTS)) {
1219 if(!gaim_blist_read(filename)) {
1220 msg = g_strdup_printf(_("An error was encountered parsing your "
1221 "buddy list. It has not been loaded."));
1222 do_error_dialog(_("Buddy List Error"), msg, GAIM_ERROR);
1223 g_free(msg);
1224 }
1225 } else if(g_slist_length(gaim_accounts)) {
1226 /* rob wants to inform the user that their buddy lists are
1227 * being converted */
1228 msg = g_strdup_printf(_("Gaim is converting your old buddy lists "
1229 "to a new format, which will now be located at %s"),
1230 filename);
1231 do_error_dialog(_("Converting Buddy List"), msg, GAIM_INFO);
1232 g_free(msg);
1233
1234 /* now, let gtk actually display the dialog before we start anything */
1235 while(gtk_events_pending())
1236 gtk_main_iteration();
1237
1238 /* read in the old lists, then save to the new format */
1239 for(accts = gaim_accounts; accts; accts = accts->next) {
1240 do_import(accts->data, NULL);
1241 }
1242 gaim_blist_save();
1243 }
1244
1245 g_free(filename);
1246 }
1247
1248 static void blist_print_group_settings(gpointer key, gpointer data,
1249 gpointer user_data) {
1250 char *key_val;
1251 char *data_val;
1252 FILE *file = user_data;
1253
1254 if(!key || !data)
1255 return;
1256
1257 key_val = g_markup_escape_text(key, -1);
1258 data_val = g_markup_escape_text(data, -1);
1259
1260 fprintf(file, "\t\t\t<setting name=\"%s\">%s</setting>\n", key_val,
1261 data_val);
1262 g_free(key_val);
1263 g_free(data_val);
1264 }
1265
1266 static void blist_print_buddy_settings(gpointer key, gpointer data,
1267 gpointer user_data) {
1268 char *key_val;
1269 char *data_val;
1270 FILE *file = user_data;
1271
1272 if(!key || !data)
1273 return;
1274
1275 key_val = g_markup_escape_text(key, -1);
1276 data_val = g_markup_escape_text(data, -1);
1277
1278 fprintf(file, "\t\t\t\t\t<setting name=\"%s\">%s</setting>\n", key_val,
1279 data_val);
1280 g_free(key_val);
1281 g_free(data_val);
1282 }
1283
1284 static void gaim_blist_write(FILE *file, struct gaim_account *exp_acct) {
1285 GSList *accounts, *buds;
1286 GaimBlistNode *gnode,*bnode;
1287 struct group *group;
1288 struct buddy *bud;
1289 fprintf(file, "<?xml version='1.0' encoding='UTF-8' ?>\n");
1290 fprintf(file, "<gaim version=\"1\">\n");
1291 fprintf(file, "\t<blist>\n");
1292
1293 for(gnode = gaimbuddylist->root; gnode; gnode = gnode->next) {
1294 if(!GAIM_BLIST_NODE_IS_GROUP(gnode))
1295 continue;
1296 group = (struct group *)gnode;
1297 if(!exp_acct || gaim_group_on_account(group, exp_acct)) {
1298 char *group_name = g_markup_escape_text(group->name, -1);
1299 fprintf(file, "\t\t<group name=\"%s\">\n", group_name);
1300 g_hash_table_foreach(group->settings, blist_print_group_settings, file);
1301 for(bnode = gnode->child; bnode; bnode = bnode->next) {
1302 if(!GAIM_BLIST_NODE_IS_BUDDY(bnode))
1303 continue;
1304 bud = (struct buddy *)bnode;
1305 if(!exp_acct || bud->account == exp_acct) {
1306 char *bud_name = g_markup_escape_text(bud->name, -1);
1307 char *bud_alias = NULL;
1308 char *acct_name = g_markup_escape_text(bud->account->username, -1);
1309 if(bud->alias)
1310 bud_alias= g_markup_escape_text(bud->alias, -1);
1311 fprintf(file, "\t\t\t<person name=\"%s\">\n",
1312 bud_alias ? bud_alias : bud_name);
1313 fprintf(file, "\t\t\t\t<buddy protocol=\"%d\" "
1314 "account=\"%s\">\n", bud->account->protocol,
1315 acct_name);
1316 fprintf(file, "\t\t\t\t\t<name>%s</name>\n", bud_name);
1317 if(bud_alias) {
1318 fprintf(file, "\t\t\t\t\t<alias>%s</alias>\n",
1319 bud_alias);
1320 }
1321 g_hash_table_foreach(bud->settings,
1322 blist_print_buddy_settings, file);
1323 fprintf(file, "\t\t\t\t</buddy>\n");
1324 fprintf(file, "\t\t\t</person>\n");
1325 g_free(bud_name);
1326 g_free(bud_alias);
1327 g_free(acct_name);
1328 }
1329 }
1330 fprintf(file, "\t\t</group>\n");
1331 g_free(group_name);
1332 }
1333 }
1334
1335 fprintf(file, "\t</blist>\n");
1336 fprintf(file, "\t<privacy>\n");
1337
1338 for(accounts = gaim_accounts; accounts; accounts = accounts->next) {
1339 struct gaim_account *account = accounts->data;
1340 char *acct_name = g_markup_escape_text(account->username, -1);
1341 if(!exp_acct || account == exp_acct) {
1342 fprintf(file, "\t\t<account protocol=\"%d\" name=\"%s\" "
1343 "mode=\"%d\">\n", account->protocol, acct_name, account->permdeny);
1344 for(buds = account->permit; buds; buds = buds->next) {
1345 char *bud_name = g_markup_escape_text(buds->data, -1);
1346 fprintf(file, "\t\t\t<permit>%s</permit>\n", bud_name);
1347 g_free(bud_name);
1348 }
1349 for(buds = account->deny; buds; buds = buds->next) {
1350 char *bud_name = g_markup_escape_text(buds->data, -1);
1351 fprintf(file, "\t\t\t<block>%s</block>\n", bud_name);
1352 g_free(bud_name);
1353 }
1354 fprintf(file, "\t\t</account>\n");
1355 }
1356 g_free(acct_name);
1357 }
1358
1359 fprintf(file, "\t</privacy>\n");
1360 fprintf(file, "</gaim>\n");
1361 }
1362
1363 void gaim_blist_save() {
1364 FILE *file;
1365 char *user_dir = gaim_user_dir();
1366 char *filename;
1367 char *filename_real;
1368
1369 if(!user_dir)
1370 return;
1371 if(!blist_safe_to_write) {
1372 gaim_debug(GAIM_DEBUG_WARNING, "blist save",
1373 "AHH!! Tried to write the blist before we read it!\n");
1374 return;
1375 }
1376
1377 file = fopen(user_dir, "r");
1378 if(!file)
1379 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR);
1380 else
1381 fclose(file);
1382
1383 filename = g_build_filename(user_dir, "blist.xml.save", NULL);
1384
1385 if((file = fopen(filename, "w"))) {
1386 gaim_blist_write(file, NULL);
1387 fclose(file);
1388 chmod(filename, S_IRUSR | S_IWUSR);
1389 } else {
1390 gaim_debug(GAIM_DEBUG_ERROR, "blist save", "Unable to write %s\n",
1391 filename);
1392 }
1393
1394 filename_real = g_build_filename(user_dir, "blist.xml", NULL);
1395
1396 if(rename(filename, filename_real) < 0)
1397 gaim_debug(GAIM_DEBUG_ERROR, "blist save",
1398 "Error renaming %s to %s\n", filename, filename_real);
1399
1400
1401 g_free(filename);
1402 g_free(filename_real);
1403 }
1404
1405 gboolean gaim_privacy_permit_add(struct gaim_account *account, const char *who) {
1406 GSList *d = account->permit;
1407 char *n = g_strdup(normalize(who));
1408 while(d) {
1409 if(!gaim_utf8_strcasecmp(n, normalize(d->data)))
1410 break;
1411 d = d->next;
1412 }
1413 g_free(n);
1414 if(!d) {
1415 account->permit = g_slist_append(account->permit, g_strdup(who));
1416 return TRUE;
1417 }
1418
1419 return FALSE;
1420 }
1421
1422 gboolean gaim_privacy_permit_remove(struct gaim_account *account, const char *who) {
1423 GSList *d = account->permit;
1424 char *n = g_strdup(normalize(who));
1425 while(d) {
1426 if(!gaim_utf8_strcasecmp(n, normalize(d->data)))
1427 break;
1428 d = d->next;
1429 }
1430 g_free(n);
1431 if(d) {
1432 account->permit = g_slist_remove(account->permit, d->data);
1433 g_free(d->data);
1434 return TRUE;
1435 }
1436 return FALSE;
1437 }
1438
1439 gboolean gaim_privacy_deny_add(struct gaim_account *account, const char *who) {
1440 GSList *d = account->deny;
1441 char *n = g_strdup(normalize(who));
1442 while(d) {
1443 if(!gaim_utf8_strcasecmp(n, normalize(d->data)))
1444 break;
1445 d = d->next;
1446 }
1447 g_free(n);
1448 if(!d) {
1449 account->deny = g_slist_append(account->deny, g_strdup(who));
1450 return TRUE;
1451 }
1452
1453 return FALSE;
1454 }
1455
1456 gboolean gaim_privacy_deny_remove(struct gaim_account *account, const char *who) {
1457 GSList *d = account->deny;
1458 char *n = g_strdup(normalize(who));
1459 while(d) {
1460 if(!gaim_utf8_strcasecmp(n, normalize(d->data)))
1461 break;
1462 d = d->next;
1463 }
1464 g_free(n);
1465 if(d) {
1466 account->deny = g_slist_remove(account->deny, d->data);
1467 g_free(d->data);
1468 return TRUE;
1469 }
1470 return FALSE;
1471 }
1472
1473 void gaim_group_set_setting(struct group *g, const char *key,
1474 const char *value) {
1475 if(!g)
1476 return;
1477 g_hash_table_replace(g->settings, g_strdup(key), g_strdup(value));
1478 }
1479
1480 char *gaim_group_get_setting(struct group *g, const char *key) {
1481 if(!g)
1482 return NULL;
1483 return g_strdup(g_hash_table_lookup(g->settings, key));
1484 }
1485
1486 void gaim_buddy_set_setting(struct buddy *b, const char *key,
1487 const char *value) {
1488 if(!b)
1489 return;
1490 g_hash_table_replace(b->settings, g_strdup(key), g_strdup(value));
1491 }
1492
1493 char *gaim_buddy_get_setting(struct buddy *b, const char *key) {
1494 if(!b)
1495 return NULL;
1496 return g_strdup(g_hash_table_lookup(b->settings, key));
1497 }
1498
1499 void gaim_set_blist_ui_ops(struct gaim_blist_ui_ops *ops)
1500 {
1501 blist_ui_ops = ops;
1502 }
1503
1504 struct gaim_blist_ui_ops *
1505 gaim_get_blist_ui_ops(void)
1506 {
1507 return blist_ui_ops;
1508 }
1509
1510 int gaim_blist_get_group_size(struct group *group, gboolean offline) {
1511 GaimBlistNode *node;
1512 int count = 0;
1513
1514 if(!group)
1515 return 0;
1516
1517 for(node = group->node.child; node; node = node->next) {
1518 if(GAIM_BLIST_NODE_IS_BUDDY(node)) {
1519 struct buddy *b = (struct buddy *)node;
1520 if(b->account->gc || offline)
1521 count++;
1522 }
1523 }
1524
1525 return count;
1526 }
1527
1528 int gaim_blist_get_group_online_count(struct group *group) {
1529 GaimBlistNode *node;
1530 int count = 0;
1531
1532 if(!group)
1533 return 0;
1534
1535 for(node = group->node.child; node; node = node->next) {
1536 if(GAIM_BLIST_NODE_IS_BUDDY(node)) {
1537 struct buddy *b = (struct buddy *)node;
1538 if(GAIM_BUDDY_IS_ONLINE(b))
1539 count++;
1540 }
1541 }
1542
1543 return count;
1544 }
1545
1546