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