Mercurial > pidgin
annotate src/list.c @ 2526:413a81136e3a
[gaim-migrate @ 2539]
hi
committer: Tailor Script <tailor@pidgin.im>
| author | Eric Warmenhoven <eric@warmenhoven.org> |
|---|---|
| date | Wed, 17 Oct 2001 20:24:11 +0000 |
| parents | 569ae9f2bb89 |
| children | 0e0a54e5819a |
| rev | line source |
|---|---|
| 2382 | 1 /* |
| 2 * gaim | |
| 3 * | |
| 4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
| 5 * | |
| 6 * This program is free software; you can redistribute it and/or modify | |
| 7 * it under the terms of the GNU General Public License as published by | |
| 8 * the Free Software Foundation; either version 2 of the License, or | |
| 9 * (at your option) any later version. | |
| 10 * | |
| 11 * This program is distributed in the hope that it will be useful, | |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 * GNU General Public License for more details. | |
| 15 * | |
| 16 * You should have received a copy of the GNU General Public License | |
| 17 * along with this program; if not, write to the Free Software | |
| 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 * | |
| 20 */ | |
| 21 | |
| 22 #ifdef HAVE_CONFIG_H | |
| 23 #include <config.h> | |
| 24 #endif | |
| 25 #include <string.h> | |
| 26 #include <sys/types.h> | |
| 27 #include <sys/stat.h> | |
| 28 #include <unistd.h> | |
| 29 #include "gaim.h" | |
| 30 #include "prpl.h" | |
| 31 | |
| 32 #define PATHSIZE 1024 | |
| 33 | |
| 34 void remove_buddy(struct gaim_connection *gc, struct group *rem_g, struct buddy *rem_b) | |
| 35 { | |
| 36 GSList *grp; | |
| 37 GSList *mem; | |
| 38 | |
| 39 struct group *delg; | |
| 40 struct buddy *delb; | |
| 41 | |
| 42 /* we assume that gc is not NULL and that the buddy exists somewhere within the | |
| 43 * gc's buddy list, therefore we can safely remove it. we need to ensure this | |
| 44 * via the UI | |
| 45 */ | |
| 46 | |
| 47 grp = g_slist_find(gc->groups, rem_g); | |
| 48 delg = (struct group *)grp->data; | |
| 49 mem = delg->members; | |
| 50 | |
| 51 mem = g_slist_find(mem, rem_b); | |
| 52 delb = (struct buddy *)mem->data; | |
| 53 | |
| 54 delg->members = g_slist_remove(delg->members, delb); | |
| 55 | |
| 56 ui_remove_buddy(gc, rem_g, rem_b); | |
| 57 | |
| 58 g_free(rem_b); | |
| 59 | |
| 60 /* we don't flush buddy list to cache because in the case of remove_group that would | |
| 61 * mean writing to the buddy list file once for each buddy, plus one more time */ | |
| 62 } | |
| 63 | |
| 64 void remove_group(struct gaim_connection *gc, struct group *rem_g) | |
| 65 { | |
| 66 GSList *grp; | |
| 67 GSList *mem; | |
| 68 GList *tmp = NULL; | |
| 69 | |
| 70 struct group *delg; | |
| 71 struct buddy *delb; | |
| 72 | |
| 73 /* we assume that the group actually does exist within the gc, and that the gc is not NULL. | |
| 74 * the UI is responsible for this */ | |
| 75 | |
| 76 grp = g_slist_find(gc->groups, rem_g); | |
| 77 delg = (struct group *)grp->data; | |
| 78 mem = delg->members; | |
| 79 | |
| 80 while (delg->members) { | |
| 81 delb = (struct buddy *)delg->members->data; | |
| 82 tmp = g_list_append(tmp, g_strdup(delb->name)); | |
| 83 remove_buddy(gc, delg, delb); /* this should take care of removing | |
| 84 the group_show if necessary */ | |
| 85 } | |
| 86 | |
| 87 gc->groups = g_slist_remove(gc->groups, delg); | |
| 88 | |
| 89 serv_remove_buddies(gc, tmp); | |
| 90 while (tmp) { | |
| 91 g_free(tmp->data); | |
| 92 tmp = g_list_remove(tmp, tmp->data); | |
| 93 } | |
| 94 | |
| 95 ui_remove_group(gc, rem_g); | |
| 96 | |
| 97 g_free(rem_g); | |
| 98 | |
| 99 /* don't flush buddy list to cache in order to be consistent with remove_buddy, | |
| 100 * mostly. remove_group is only called from one place, so we'll let it handle it. */ | |
| 101 } | |
| 102 | |
| 103 struct buddy *add_buddy(struct gaim_connection *gc, char *group, char *buddy, char *show) | |
| 104 { | |
| 105 struct buddy *b; | |
| 106 struct group *g; | |
| 107 char *good; | |
| 108 | |
| 109 if ((b = find_buddy(gc, buddy)) != NULL) | |
| 110 return b; | |
| 111 | |
| 112 g = find_group(gc, group); | |
| 113 | |
| 114 if (g == NULL) | |
| 115 g = add_group(gc, group); | |
| 116 | |
| 117 b = (struct buddy *)g_new0(struct buddy, 1); | |
| 118 | |
| 119 if (!b) | |
| 120 return NULL; | |
| 121 | |
| 122 b->gc = gc; | |
| 123 b->present = 0; | |
| 124 | |
| 125 if (gc->prpl->normalize) | |
| 126 good = (*gc->prpl->normalize)(buddy); | |
| 127 else | |
| 128 good = buddy; | |
| 129 | |
| 130 g_snprintf(b->name, sizeof(b->name), "%s", good); | |
| 131 g_snprintf(b->show, sizeof(b->show), "%s", show ? (show[0] ? show : good) : good); | |
| 132 | |
| 133 g->members = g_slist_append(g->members, b); | |
| 134 | |
| 135 b->idle = 0; | |
| 136 b->caps = 0; | |
| 137 | |
| 138 ui_add_buddy(gc, g, b); | |
| 139 | |
| 140 return b; | |
| 141 } | |
| 142 | |
| 143 struct group *add_group(struct gaim_connection *gc, char *group) | |
| 144 { | |
| 145 struct group *g = find_group(gc, group); | |
| 146 if (g) | |
| 147 return g; | |
| 148 g = (struct group *)g_new0(struct group, 1); | |
| 149 if (!g) | |
| 150 return NULL; | |
| 151 | |
| 152 g->gc = gc; | |
| 153 strncpy(g->name, group, sizeof(g->name)); | |
| 154 gc->groups = g_slist_append(gc->groups, g); | |
| 155 | |
| 156 g->members = NULL; | |
| 157 | |
| 158 ui_add_group(gc, g); | |
| 159 | |
| 160 return g; | |
| 161 } | |
| 162 | |
| 163 struct group *find_group(struct gaim_connection *gc, char *group) | |
| 164 { | |
| 165 struct group *g; | |
| 166 GSList *grp; | |
| 167 GSList *c = connections; | |
| 168 struct gaim_connection *z; | |
| 169 char *grpname = g_malloc(strlen(group) + 1); | |
| 170 | |
| 171 strcpy(grpname, normalize (group)); | |
| 172 if (gc) { | |
| 173 grp = gc->groups; | |
| 174 while (grp) { | |
| 175 g = (struct group *)grp->data; | |
| 176 if (!g_strcasecmp(normalize (g->name), grpname)) { | |
| 177 g_free(grpname); | |
| 178 return g; | |
| 179 } | |
| 180 grp = g_slist_next(grp); | |
| 181 } | |
| 182 | |
| 183 g_free(grpname); | |
| 184 return NULL; | |
| 185 } else { | |
| 186 while (c) { | |
| 187 z = (struct gaim_connection *)c->data; | |
| 188 grp = z->groups; | |
| 189 while (grp) { | |
| 190 g = (struct group *)grp->data; | |
| 191 if (!g_strcasecmp(normalize (g->name), grpname)) { | |
| 192 g_free(grpname); | |
| 193 return g; | |
| 194 } | |
| 195 grp = g_slist_next(grp); | |
| 196 } | |
| 197 | |
| 198 c = c->next; | |
| 199 } | |
| 200 g_free(grpname); | |
| 201 return NULL; | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 struct group *find_group_by_buddy(struct gaim_connection *gc, char *who) | |
| 206 { | |
| 207 struct group *g; | |
| 208 struct buddy *b; | |
| 209 GSList *grp; | |
| 210 GSList *mem; | |
| 211 char *whoname; | |
| 212 char *(*norm)(const char *); | |
| 213 | |
| 214 if (gc) { | |
| 215 if (gc->prpl->normalize) | |
| 216 norm = gc->prpl->normalize; | |
| 217 else | |
| 218 norm = normalize; | |
| 219 whoname = g_strdup((*norm)(who)); | |
| 220 grp = gc->groups; | |
| 221 while (grp) { | |
| 222 g = (struct group *)grp->data; | |
| 223 | |
| 224 mem = g->members; | |
| 225 while (mem) { | |
| 226 b = (struct buddy *)mem->data; | |
| 227 if (!strcmp((*norm)(b->name), whoname)) { | |
| 228 g_free(whoname); | |
| 229 return g; | |
| 230 } | |
| 231 mem = mem->next; | |
| 232 } | |
| 233 grp = g_slist_next(grp); | |
| 234 } | |
| 235 g_free(whoname); | |
| 236 return NULL; | |
| 237 } else { | |
| 238 GSList *c = connections; | |
| 239 struct gaim_connection *z; | |
| 240 while (c) { | |
| 241 z = (struct gaim_connection *)c->data; | |
| 242 if (z->prpl->normalize) | |
| 243 norm = z->prpl->normalize; | |
| 244 else | |
| 245 norm = normalize; | |
| 246 whoname = g_strdup((*norm)(who)); | |
| 247 grp = z->groups; | |
| 248 while (grp) { | |
| 249 g = (struct group *)grp->data; | |
| 250 | |
| 251 mem = g->members; | |
| 252 while (mem) { | |
| 253 b = (struct buddy *)mem->data; | |
| 254 if (!strcmp((*norm)(b->name), whoname)) { | |
| 255 g_free(whoname); | |
| 256 return g; | |
| 257 } | |
| 258 mem = mem->next; | |
| 259 } | |
| 260 grp = g_slist_next(grp); | |
| 261 } | |
| 262 c = c->next; | |
| 263 g_free(whoname); | |
| 264 } | |
| 265 return NULL; | |
| 266 } | |
| 267 } | |
| 268 | |
| 269 struct buddy *find_buddy(struct gaim_connection *gc, char *who) | |
| 270 { | |
| 271 struct group *g; | |
| 272 struct buddy *b; | |
| 273 GSList *grp; | |
| 274 GSList *c; | |
| 275 struct gaim_connection *z; | |
| 276 GSList *mem; | |
| 277 char *whoname; | |
| 278 char *(*norm)(const char *); | |
| 279 | |
| 280 if (gc) { | |
| 281 if (gc->prpl->normalize) | |
| 282 norm = gc->prpl->normalize; | |
| 283 else | |
| 284 norm = normalize; | |
| 285 whoname = g_strdup((*norm)(who)); | |
| 286 grp = gc->groups; | |
| 287 while (grp) { | |
| 288 g = (struct group *)grp->data; | |
| 289 | |
| 290 mem = g->members; | |
| 291 while (mem) { | |
| 292 b = (struct buddy *)mem->data; | |
| 293 if (!strcmp((*norm)(b->name), whoname)) { | |
| 294 g_free(whoname); | |
| 295 return b; | |
| 296 } | |
| 297 mem = mem->next; | |
| 298 } | |
| 299 grp = g_slist_next(grp); | |
| 300 } | |
| 301 g_free(whoname); | |
| 302 return NULL; | |
| 303 } else { | |
| 304 c = connections; | |
| 305 while (c) { | |
| 306 z = (struct gaim_connection *)c->data; | |
| 307 if (z->prpl->normalize) | |
| 308 norm = z->prpl->normalize; | |
| 309 else | |
| 310 norm = normalize; | |
| 311 whoname = g_strdup((*norm)(who)); | |
| 312 grp = z->groups; | |
| 313 while (grp) { | |
| 314 g = (struct group *)grp->data; | |
| 315 | |
| 316 mem = g->members; | |
| 317 while (mem) { | |
| 318 b = (struct buddy *)mem->data; | |
| 319 if (!strcmp((*norm)(b->name), whoname)) { | |
| 320 g_free(whoname); | |
| 321 return b; | |
| 322 } | |
| 323 mem = mem->next; | |
| 324 } | |
| 325 grp = g_slist_next(grp); | |
| 326 } | |
| 327 c = c->next; | |
| 328 g_free(whoname); | |
| 329 } | |
| 330 return NULL; | |
| 331 } | |
| 332 } | |
| 333 | |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
334 void parse_toc_buddy_list(struct gaim_connection *gc, char *config) |
| 2382 | 335 { |
| 336 char *c; | |
| 337 char current[256]; | |
| 338 char *name; | |
| 339 GList *bud; | |
| 340 int how_many = 0; | |
| 341 | |
| 342 bud = NULL; | |
| 343 | |
| 344 if (config != NULL) { | |
| 345 | |
| 346 /* skip "CONFIG:" (if it exists) */ | |
| 347 c = strncmp(config + 6 /* sizeof(struct sflap_hdr) */ , "CONFIG:", strlen("CONFIG:")) ? | |
| 348 strtok(config, "\n") : | |
| 349 strtok(config + 6 /* sizeof(struct sflap_hdr) */ + strlen("CONFIG:"), "\n"); | |
| 350 do { | |
| 351 if (c == NULL) | |
| 352 break; | |
| 353 if (*c == 'g') { | |
| 354 strncpy(current, c + 2, sizeof(current)); | |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
355 if (!find_group(gc, current)) { |
|
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
356 add_group(gc, current); |
|
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
357 how_many++; |
|
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
358 } |
| 2382 | 359 } else if (*c == 'b' && !find_buddy(gc, c + 2)) { |
| 360 char nm[80], sw[80], *tmp = c + 2; | |
| 361 int i = 0; | |
| 362 while (*tmp != ':' && *tmp) | |
| 363 nm[i++] = *tmp++; | |
| 364 if (*tmp == ':') | |
| 365 *tmp++ = '\0'; | |
| 366 nm[i] = '\0'; | |
| 367 i = 0; | |
| 368 while (*tmp) | |
| 369 sw[i++] = *tmp++; | |
| 370 sw[i] = '\0'; | |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
371 if (!find_buddy(gc, nm)) { |
| 2382 | 372 add_buddy(gc, current, nm, sw); |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
373 how_many++; |
|
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
374 bud = g_list_append(bud, c + 2); |
|
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
375 } |
| 2382 | 376 } else if (*c == 'p') { |
| 377 GSList *d = gc->permit; | |
| 378 char *n; | |
| 379 name = g_malloc(strlen(c + 2) + 2); | |
| 380 g_snprintf(name, strlen(c + 2) + 1, "%s", c + 2); | |
| 381 n = g_strdup(normalize (name)); | |
| 382 while (d) { | |
| 383 if (!g_strcasecmp(n, normalize (d->data))) | |
| 384 break; | |
| 385 d = d->next; | |
| 386 } | |
| 387 g_free(n); | |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
388 if (!d) { |
| 2382 | 389 gc->permit = g_slist_append(gc->permit, name); |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
390 how_many++; |
|
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
391 } else |
| 2382 | 392 g_free(name); |
| 393 } else if (*c == 'd') { | |
| 394 GSList *d = gc->deny; | |
| 395 char *n; | |
| 396 name = g_malloc(strlen(c + 2) + 2); | |
| 397 g_snprintf(name, strlen(c + 2) + 1, "%s", c + 2); | |
| 398 n = g_strdup(normalize (name)); | |
| 399 while (d) { | |
| 400 if (!g_strcasecmp(n, normalize (d->data))) | |
| 401 break; | |
| 402 d = d->next; | |
| 403 } | |
| 404 g_free(n); | |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
405 if (!d) { |
| 2382 | 406 gc->deny = g_slist_append(gc->deny, name); |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
407 how_many++; |
|
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
408 } else |
| 2382 | 409 g_free(name); |
| 410 } else if (!strncmp("toc", c, 3)) { | |
| 411 sscanf(c + strlen(c) - 1, "%d", &gc->permdeny); | |
| 412 debug_printf("permdeny: %d\n", gc->permdeny); | |
| 413 if (gc->permdeny == 0) | |
| 414 gc->permdeny = 1; | |
| 415 } else if (*c == 'm') { | |
| 416 sscanf(c + 2, "%d", &gc->permdeny); | |
| 417 debug_printf("permdeny: %d\n", gc->permdeny); | |
| 418 if (gc->permdeny == 0) | |
| 419 gc->permdeny = 1; | |
| 420 } | |
| 421 } while ((c = strtok(NULL, "\n"))); | |
| 422 | |
| 423 if (bud != NULL) { | |
| 424 serv_add_buddies(gc, bud); | |
| 425 g_list_free(bud); | |
| 426 } | |
| 427 serv_set_permit_deny(gc); | |
| 428 } | |
| 429 | |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
430 if (how_many != 0) |
| 2382 | 431 do_export(gc); |
| 432 } | |
| 433 | |
| 434 void toc_build_config(struct gaim_connection *gc, char *s, int len, gboolean show) | |
| 435 { | |
| 436 GSList *grp = gc->groups; | |
| 437 GSList *mem; | |
| 438 struct group *g; | |
| 439 struct buddy *b; | |
| 440 GSList *plist = gc->permit; | |
| 441 GSList *dlist = gc->deny; | |
| 442 | |
| 443 int pos = 0; | |
| 444 | |
| 445 if (!gc->permdeny) | |
| 446 gc->permdeny = 1; | |
| 447 | |
| 448 pos += g_snprintf(&s[pos], len - pos, "m %d\n", gc->permdeny); | |
| 449 while (len > pos && grp) { | |
| 450 g = (struct group *)grp->data; | |
| 451 pos += g_snprintf(&s[pos], len - pos, "g %s\n", g->name); | |
| 452 mem = g->members; | |
| 453 while (len > pos && mem) { | |
| 454 b = (struct buddy *)mem->data; | |
| 455 pos += g_snprintf(&s[pos], len - pos, "b %s%s%s\n", b->name, | |
| 456 (show && strcmp(b->name, b->show)) ? ":" : "", | |
| 457 (show && strcmp(b->name, b->show)) ? b->show : ""); | |
| 458 mem = mem->next; | |
| 459 } | |
| 460 grp = g_slist_next(grp); | |
| 461 } | |
| 462 | |
| 463 while (len > pos && plist) { | |
| 464 pos += g_snprintf(&s[pos], len - pos, "p %s\n", (char *)plist->data); | |
| 465 plist = plist->next; | |
| 466 } | |
| 467 | |
| 468 while (len > pos && dlist) { | |
| 469 pos += g_snprintf(&s[pos], len - pos, "d %s\n", (char *)dlist->data); | |
| 470 dlist = dlist->next; | |
| 471 } | |
| 472 } | |
| 473 | |
| 474 /* remove leading whitespace from a string */ | |
| 475 static char *remove_spaces(char *str) | |
| 476 { | |
| 477 int i; | |
| 478 char *new; | |
| 479 | |
| 480 if (str == NULL) | |
| 481 return NULL; | |
| 482 | |
| 483 i = strspn(str, " \t\n\r\f"); | |
| 484 new = &str[i]; | |
| 485 | |
| 486 return new; | |
| 487 } | |
| 488 | |
| 489 | |
| 490 /* translate an AIM 3 buddylist (*.lst) to a GAIM buddylist */ | |
| 491 static void translate_lst(FILE *src_fp, char *dest) | |
| 492 { | |
| 493 char line[BUF_LEN], *line2; | |
| 494 char *name; | |
| 495 int i; | |
| 496 | |
| 497 sprintf(dest, "m 1\n"); | |
| 498 | |
| 499 while (fgets(line, BUF_LEN, src_fp)) { | |
| 500 line2 = remove_spaces(line); | |
| 501 if (strstr(line2, "group") == line2) { | |
| 502 name = strpbrk(line2, " \t\n\r\f") + 1; | |
| 503 strcat(dest, "g "); | |
| 504 for (i = 0; i < strcspn(name, "\n\r"); i++) | |
| 505 if (name[i] != '\"') | |
| 506 strncat(dest, &name[i], 1); | |
| 507 strcat(dest, "\n"); | |
| 508 } | |
| 509 if (strstr(line2, "buddy") == line2) { | |
| 510 name = strpbrk(line2, " \t\n\r\f") + 1; | |
| 511 strcat(dest, "b "); | |
| 512 for (i = 0; i < strcspn(name, "\n\r"); i++) | |
| 513 if (name[i] != '\"') | |
| 514 strncat(dest, &name[i], 1); | |
| 515 strcat(dest, "\n"); | |
| 516 } | |
| 517 } | |
| 518 | |
| 519 return; | |
| 520 } | |
| 521 | |
| 522 | |
| 523 /* translate an AIM 4 buddylist (*.blt) to GAIM format */ | |
| 524 static void translate_blt(FILE *src_fp, char *dest) | |
| 525 { | |
| 526 int i; | |
| 527 char line[BUF_LEN]; | |
| 528 char *buddy; | |
| 529 | |
| 530 sprintf(dest, "m 1\n"); | |
| 531 | |
| 532 while (strstr(fgets(line, BUF_LEN, src_fp), "Buddy") == NULL); | |
| 533 while (strstr(fgets(line, BUF_LEN, src_fp), "list") == NULL); | |
| 534 | |
| 535 while (1) { | |
| 536 fgets(line, BUF_LEN, src_fp); | |
| 537 if (strchr(line, '}') != NULL) | |
| 538 break; | |
| 539 | |
| 540 /* Syntax starting with "<group> {" */ | |
| 541 if (strchr(line, '{') != NULL) { | |
| 542 strcat(dest, "g "); | |
| 543 buddy = remove_spaces(strtok(line, "{")); | |
| 544 for (i = 0; i < strlen(buddy); i++) { | |
| 545 if (buddy[i] != '\"') | |
| 546 strncat(dest, &buddy[i], 1); | |
| 547 } | |
| 548 strcat(dest, "\n"); | |
| 549 while (strchr(fgets(line, BUF_LEN, src_fp), '}') == NULL) { | |
| 550 buddy = remove_spaces(line); | |
| 551 strcat(dest, "b "); | |
| 552 if (strchr(buddy, '\"') != NULL) { | |
| 553 buddy++; | |
| 554 strncat(dest, buddy, strchr(buddy, '\"') - buddy); | |
| 555 strcat(dest, "\n"); | |
| 556 } else | |
| 557 strcat(dest, buddy); | |
| 558 } | |
| 559 } | |
| 560 /* Syntax "group buddy buddy ..." */ | |
| 561 else { | |
| 562 buddy = remove_spaces(strtok(line, " \n")); | |
| 563 strcat(dest, "g "); | |
| 564 if (strchr(buddy, '\"') != NULL) { | |
| 565 strcat(dest, &buddy[1]); | |
| 566 strcat(dest, " "); | |
| 567 buddy = remove_spaces(strtok(NULL, " \n")); | |
| 568 while (strchr(buddy, '\"') == NULL) { | |
| 569 strcat(dest, buddy); | |
| 570 strcat(dest, " "); | |
| 571 buddy = remove_spaces(strtok(NULL, " \n")); | |
| 572 } | |
| 573 strncat(dest, buddy, strlen(buddy) - 1); | |
| 574 } else { | |
| 575 strcat(dest, buddy); | |
| 576 } | |
| 577 strcat(dest, "\n"); | |
| 578 while ((buddy = remove_spaces(strtok(NULL, " \n"))) != NULL) { | |
| 579 strcat(dest, "b "); | |
| 580 if (strchr(buddy, '\"') != NULL) { | |
| 581 strcat(dest, &buddy[1]); | |
| 582 strcat(dest, " "); | |
| 583 buddy = remove_spaces(strtok(NULL, " \n")); | |
| 584 while (strchr(buddy, '\"') == NULL) { | |
| 585 strcat(dest, buddy); | |
| 586 strcat(dest, " "); | |
| 587 buddy = remove_spaces(strtok(NULL, " \n")); | |
| 588 } | |
| 589 strncat(dest, buddy, strlen(buddy) - 1); | |
| 590 } else { | |
| 591 strcat(dest, buddy); | |
| 592 } | |
| 593 strcat(dest, "\n"); | |
| 594 } | |
| 595 } | |
| 596 } | |
| 597 | |
| 598 return; | |
| 599 } | |
| 600 | |
| 601 static gchar *get_screenname_filename(const char *name) | |
| 602 { | |
| 603 gchar **split; | |
| 604 gchar *good; | |
| 605 | |
| 606 split = g_strsplit(name, G_DIR_SEPARATOR_S, -1); | |
| 607 good = g_strjoinv(NULL, split); | |
| 608 g_strfreev(split); | |
| 609 | |
| 610 g_strup(good); | |
| 611 | |
| 612 return good; | |
| 613 } | |
| 614 | |
| 615 /* see if a buddy list cache file for this user exists */ | |
| 616 | |
| 617 gboolean bud_list_cache_exists(struct gaim_connection *gc) | |
| 618 { | |
| 619 gboolean ret = FALSE; | |
| 620 char path[PATHSIZE]; | |
| 621 char *file; | |
| 622 struct stat sbuf; | |
| 623 char *g_screenname; | |
| 624 | |
| 625 g_screenname = get_screenname_filename(gc->username); | |
| 626 | |
| 627 file = gaim_user_dir(); | |
| 628 if (file != (char *)NULL) { | |
| 629 g_snprintf(path, sizeof path, "%s/%s.%d.blist", file, g_screenname, | |
| 630 (gc->protocol == PROTO_OSCAR) ? PROTO_TOC : gc->protocol); | |
| 631 if (!stat(path, &sbuf)) { | |
| 632 debug_printf("%s exists.\n", path); | |
| 633 ret = TRUE; | |
| 634 } else { | |
| 635 char path2[PATHSIZE]; | |
| 636 debug_printf("%s does not exist.\n", path); | |
| 637 g_snprintf(path2, sizeof path2, "%s/%s.blist", file, g_screenname); | |
| 638 if (!stat(path2, &sbuf)) { | |
| 639 debug_printf("%s exists, moving to %s\n", path2, path); | |
| 640 if (rename(path2, path)) | |
| 641 debug_printf("rename didn't work!\n"); | |
| 642 else | |
| 643 ret = TRUE; | |
| 644 } | |
| 645 } | |
| 646 g_free(file); | |
| 647 } | |
| 648 g_free(g_screenname); | |
| 649 return ret; | |
| 650 } | |
| 651 | |
| 652 void do_import(struct gaim_connection *gc, char *filename) | |
| 653 { | |
| 654 char *buf = g_malloc(BUF_LONG * 2); | |
| 655 char *buf2; | |
| 656 char *first = g_malloc(64); | |
| 657 char *file; | |
| 658 char path[PATHSIZE]; | |
| 659 char *g_screenname; | |
| 660 int len; | |
| 661 FILE *f; | |
| 662 | |
| 663 if (filename) { | |
| 664 g_snprintf(path, sizeof(path), "%s", filename); | |
| 665 } else { | |
| 666 g_screenname = get_screenname_filename(gc->username); | |
| 667 | |
| 668 file = gaim_user_dir(); | |
| 669 if (file != (char *)NULL) { | |
| 670 sprintf(path, "%s/%s.%d.blist", file, g_screenname, | |
| 671 (gc->protocol == PROTO_OSCAR) ? PROTO_TOC : gc->protocol); | |
| 672 g_free(file); | |
| 673 g_free(g_screenname); | |
| 674 } else { | |
| 675 g_free(g_screenname); | |
| 676 g_free(buf); | |
| 677 g_free(first); | |
| 678 return; | |
| 679 } | |
| 680 } | |
| 681 | |
| 682 if (!(f = fopen(path, "r"))) { | |
| 683 debug_printf("Unable to open %s.\n", path); | |
| 684 g_free(buf); | |
| 685 g_free(first); | |
| 686 return; | |
| 687 } | |
| 688 | |
| 689 fgets(first, 64, f); | |
| 690 | |
| 691 /* AIM 4 buddy list */ | |
| 692 if (!g_strncasecmp(first, "Config {", strlen("Config {"))) { | |
| 693 debug_printf("aim 4\n"); | |
| 694 rewind(f); | |
| 695 translate_blt(f, buf); | |
| 696 debug_printf("%s\n", buf); | |
| 697 buf2 = buf; | |
| 698 buf = g_malloc(8193); | |
| 699 g_snprintf(buf, 8192, "toc_set_config {%s}\n", buf2); | |
| 700 g_free(buf2); | |
| 701 /* AIM 3 buddy list */ | |
| 702 } else if (strstr(first, "group") != NULL) { | |
| 703 debug_printf("aim 3\n"); | |
| 704 rewind(f); | |
| 705 translate_lst(f, buf); | |
| 706 debug_printf("%s\n", buf); | |
| 707 buf2 = buf; | |
| 708 buf = g_malloc(8193); | |
| 709 g_snprintf(buf, 8192, "toc_set_config {%s}\n", buf2); | |
| 710 g_free(buf2); | |
| 711 /* GAIM buddy list - no translation */ | |
| 712 } else if (first[0] == 'm') { | |
| 713 rewind(f); | |
| 714 len = fread(buf, 1, BUF_LONG * 2, f); | |
| 715 buf[len] = '\0'; | |
| 716 buf2 = buf; | |
| 717 buf = g_malloc(8193); | |
| 718 g_snprintf(buf, 8192, "toc_set_config {%s}\n", buf2); | |
| 719 g_free(buf2); | |
| 720 /* Something else */ | |
| 721 } else { | |
| 722 g_free(buf); | |
| 723 g_free(first); | |
| 724 fclose(f); | |
| 725 return; | |
| 726 } | |
| 727 | |
|
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
728 parse_toc_buddy_list(gc, buf); |
| 2382 | 729 |
| 730 fclose(f); | |
| 731 | |
| 732 g_free(buf); | |
| 733 g_free(first); | |
| 734 } | |
| 735 | |
| 736 void do_export(struct gaim_connection *g) | |
| 737 { | |
| 738 FILE *dir; | |
| 739 FILE *f; | |
| 740 char buf[32 * 1024]; | |
| 741 char *file; | |
| 742 char path[PATHSIZE]; | |
| 743 char *g_screenname; | |
| 744 | |
| 745 file = gaim_user_dir(); | |
| 746 if (!file) | |
| 747 return; | |
| 748 | |
| 749 strcpy(buf, file); | |
| 750 dir = fopen(buf, "r"); | |
| 751 if (!dir) | |
| 752 mkdir(buf, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 753 else | |
| 754 fclose(dir); | |
| 755 | |
| 756 g_screenname = get_screenname_filename(g->username); | |
| 757 | |
| 758 sprintf(path, "%s/%s.%d.blist", file, g_screenname, | |
| 759 (g->protocol == PROTO_OSCAR) ? PROTO_TOC : g->protocol); | |
| 760 if ((f = fopen(path, "w"))) { | |
| 761 debug_printf("writing %s\n", path); | |
| 762 toc_build_config(g, buf, 8192 - 1, TRUE); | |
| 763 fprintf(f, "%s\n", buf); | |
| 764 fclose(f); | |
| 765 chmod(buf, S_IRUSR | S_IWUSR); | |
| 766 } else { | |
| 767 debug_printf("unable to write %s\n", path); | |
| 768 } | |
| 769 | |
| 770 g_free(g_screenname); | |
| 771 g_free(file); | |
| 772 } |
