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