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