Mercurial > pidgin
annotate src/list.c @ 2572:ad0d7c423669
[gaim-migrate @ 2585]
people who make commits should ./configure with --enable-debug.
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Mon, 22 Oct 2001 21:05:27 +0000 |
parents | 20418e5702eb |
children | 0f5390176949 |
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 | |
2536
0e0a54e5819a
[gaim-migrate @ 2549]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2526
diff
changeset
|
474 /* translate an AIM 3 buddylist (*.lst) to a Gaim buddylist */ |
2382 | 475 static void translate_lst(FILE *src_fp, char *dest) |
476 { | |
477 char line[BUF_LEN], *line2; | |
478 char *name; | |
479 int i; | |
480 | |
481 sprintf(dest, "m 1\n"); | |
482 | |
483 while (fgets(line, BUF_LEN, src_fp)) { | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
484 line2 = g_strchug(line); |
2382 | 485 if (strstr(line2, "group") == line2) { |
486 name = strpbrk(line2, " \t\n\r\f") + 1; | |
487 strcat(dest, "g "); | |
488 for (i = 0; i < strcspn(name, "\n\r"); i++) | |
489 if (name[i] != '\"') | |
490 strncat(dest, &name[i], 1); | |
491 strcat(dest, "\n"); | |
492 } | |
493 if (strstr(line2, "buddy") == line2) { | |
494 name = strpbrk(line2, " \t\n\r\f") + 1; | |
495 strcat(dest, "b "); | |
496 for (i = 0; i < strcspn(name, "\n\r"); i++) | |
497 if (name[i] != '\"') | |
498 strncat(dest, &name[i], 1); | |
499 strcat(dest, "\n"); | |
500 } | |
501 } | |
502 | |
503 return; | |
504 } | |
505 | |
506 | |
2536
0e0a54e5819a
[gaim-migrate @ 2549]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2526
diff
changeset
|
507 /* translate an AIM 4 buddylist (*.blt) to Gaim format */ |
2382 | 508 static void translate_blt(FILE *src_fp, char *dest) |
509 { | |
510 int i; | |
511 char line[BUF_LEN]; | |
512 char *buddy; | |
513 | |
514 sprintf(dest, "m 1\n"); | |
515 | |
516 while (strstr(fgets(line, BUF_LEN, src_fp), "Buddy") == NULL); | |
517 while (strstr(fgets(line, BUF_LEN, src_fp), "list") == NULL); | |
518 | |
519 while (1) { | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
520 fgets(line, BUF_LEN, src_fp); g_strchomp(line); |
2382 | 521 if (strchr(line, '}') != NULL) |
522 break; | |
523 | |
524 if (strchr(line, '{') != NULL) { | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
525 /* Syntax starting with "<group> {" */ |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
526 |
2382 | 527 strcat(dest, "g "); |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
528 buddy = g_strchug(strtok(line, "{")); |
2382 | 529 for (i = 0; i < strlen(buddy); i++) { |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
530 if (buddy[i] != '\"') { |
2382 | 531 strncat(dest, &buddy[i], 1); |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
532 } |
2382 | 533 } |
534 strcat(dest, "\n"); | |
535 while (strchr(fgets(line, BUF_LEN, src_fp), '}') == NULL) { | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
536 gboolean pounce = FALSE; |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
537 g_strchomp(line); |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
538 buddy = g_strchug(line); |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
539 debug_printf("\nbuddy: \"%s\"\n\n", buddy); |
2382 | 540 strcat(dest, "b "); |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
541 if (strchr(buddy, '{') != NULL) { |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
542 /* buddy pounce, etc */ |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
543 char *pos = strchr(buddy, '{') - 1; |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
544 *pos = 0; |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
545 pounce = TRUE; |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
546 } |
2382 | 547 if (strchr(buddy, '\"') != NULL) { |
548 buddy++; | |
549 strncat(dest, buddy, strchr(buddy, '\"') - buddy); | |
550 } else | |
551 strcat(dest, buddy); | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
552 strcat(dest, "\n"); |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
553 if (pounce) |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
554 do |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
555 fgets(line, BUF_LEN, src_fp); |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
556 while (!strchr(line, '}')); |
2382 | 557 } |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
558 } else { |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
559 |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
560 /* Syntax "group buddy buddy ..." */ |
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
561 buddy = g_strchug(strtok(line, " \n")); |
2382 | 562 strcat(dest, "g "); |
563 if (strchr(buddy, '\"') != NULL) { | |
564 strcat(dest, &buddy[1]); | |
565 strcat(dest, " "); | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
566 buddy = g_strchug(strtok(NULL, " \n")); |
2382 | 567 while (strchr(buddy, '\"') == NULL) { |
568 strcat(dest, buddy); | |
569 strcat(dest, " "); | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
570 buddy = g_strchug(strtok(NULL, " \n")); |
2382 | 571 } |
572 strncat(dest, buddy, strlen(buddy) - 1); | |
573 } else { | |
574 strcat(dest, buddy); | |
575 } | |
576 strcat(dest, "\n"); | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
577 while ((buddy = g_strchug(strtok(NULL, " \n"))) != NULL) { |
2382 | 578 strcat(dest, "b "); |
579 if (strchr(buddy, '\"') != NULL) { | |
580 strcat(dest, &buddy[1]); | |
581 strcat(dest, " "); | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
582 buddy = g_strchug(strtok(NULL, " \n")); |
2382 | 583 while (strchr(buddy, '\"') == NULL) { |
584 strcat(dest, buddy); | |
585 strcat(dest, " "); | |
2548
20418e5702eb
[gaim-migrate @ 2561]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2536
diff
changeset
|
586 buddy = g_strchug(strtok(NULL, " \n")); |
2382 | 587 } |
588 strncat(dest, buddy, strlen(buddy) - 1); | |
589 } else { | |
590 strcat(dest, buddy); | |
591 } | |
592 strcat(dest, "\n"); | |
593 } | |
594 } | |
595 } | |
596 | |
597 return; | |
598 } | |
599 | |
600 static gchar *get_screenname_filename(const char *name) | |
601 { | |
602 gchar **split; | |
603 gchar *good; | |
604 | |
605 split = g_strsplit(name, G_DIR_SEPARATOR_S, -1); | |
606 good = g_strjoinv(NULL, split); | |
607 g_strfreev(split); | |
608 | |
609 g_strup(good); | |
610 | |
611 return good; | |
612 } | |
613 | |
614 /* see if a buddy list cache file for this user exists */ | |
615 | |
616 gboolean bud_list_cache_exists(struct gaim_connection *gc) | |
617 { | |
618 gboolean ret = FALSE; | |
619 char path[PATHSIZE]; | |
620 char *file; | |
621 struct stat sbuf; | |
622 char *g_screenname; | |
623 | |
624 g_screenname = get_screenname_filename(gc->username); | |
625 | |
626 file = gaim_user_dir(); | |
627 if (file != (char *)NULL) { | |
628 g_snprintf(path, sizeof path, "%s/%s.%d.blist", file, g_screenname, | |
629 (gc->protocol == PROTO_OSCAR) ? PROTO_TOC : gc->protocol); | |
630 if (!stat(path, &sbuf)) { | |
631 debug_printf("%s exists.\n", path); | |
632 ret = TRUE; | |
633 } else { | |
634 char path2[PATHSIZE]; | |
635 debug_printf("%s does not exist.\n", path); | |
636 g_snprintf(path2, sizeof path2, "%s/%s.blist", file, g_screenname); | |
637 if (!stat(path2, &sbuf)) { | |
638 debug_printf("%s exists, moving to %s\n", path2, path); | |
639 if (rename(path2, path)) | |
640 debug_printf("rename didn't work!\n"); | |
641 else | |
642 ret = TRUE; | |
643 } | |
644 } | |
645 g_free(file); | |
646 } | |
647 g_free(g_screenname); | |
648 return ret; | |
649 } | |
650 | |
651 void do_import(struct gaim_connection *gc, char *filename) | |
652 { | |
653 char *buf = g_malloc(BUF_LONG * 2); | |
654 char *buf2; | |
655 char *first = g_malloc(64); | |
656 char *file; | |
657 char path[PATHSIZE]; | |
658 char *g_screenname; | |
659 int len; | |
660 FILE *f; | |
661 | |
662 if (filename) { | |
663 g_snprintf(path, sizeof(path), "%s", filename); | |
664 } else { | |
665 g_screenname = get_screenname_filename(gc->username); | |
666 | |
667 file = gaim_user_dir(); | |
668 if (file != (char *)NULL) { | |
669 sprintf(path, "%s/%s.%d.blist", file, g_screenname, | |
670 (gc->protocol == PROTO_OSCAR) ? PROTO_TOC : gc->protocol); | |
671 g_free(file); | |
672 g_free(g_screenname); | |
673 } else { | |
674 g_free(g_screenname); | |
675 g_free(buf); | |
676 g_free(first); | |
677 return; | |
678 } | |
679 } | |
680 | |
681 if (!(f = fopen(path, "r"))) { | |
682 debug_printf("Unable to open %s.\n", path); | |
683 g_free(buf); | |
684 g_free(first); | |
685 return; | |
686 } | |
687 | |
688 fgets(first, 64, f); | |
689 | |
690 /* AIM 4 buddy list */ | |
691 if (!g_strncasecmp(first, "Config {", strlen("Config {"))) { | |
692 debug_printf("aim 4\n"); | |
693 rewind(f); | |
694 translate_blt(f, buf); | |
695 debug_printf("%s\n", buf); | |
696 buf2 = buf; | |
697 buf = g_malloc(8193); | |
698 g_snprintf(buf, 8192, "toc_set_config {%s}\n", buf2); | |
699 g_free(buf2); | |
700 /* AIM 3 buddy list */ | |
701 } else if (strstr(first, "group") != NULL) { | |
702 debug_printf("aim 3\n"); | |
703 rewind(f); | |
704 translate_lst(f, buf); | |
705 debug_printf("%s\n", buf); | |
706 buf2 = buf; | |
707 buf = g_malloc(8193); | |
708 g_snprintf(buf, 8192, "toc_set_config {%s}\n", buf2); | |
709 g_free(buf2); | |
2536
0e0a54e5819a
[gaim-migrate @ 2549]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2526
diff
changeset
|
710 /* Gaim buddy list - no translation */ |
2382 | 711 } else if (first[0] == 'm') { |
712 rewind(f); | |
713 len = fread(buf, 1, BUF_LONG * 2, f); | |
714 buf[len] = '\0'; | |
715 buf2 = buf; | |
716 buf = g_malloc(8193); | |
717 g_snprintf(buf, 8192, "toc_set_config {%s}\n", buf2); | |
718 g_free(buf2); | |
719 /* Something else */ | |
720 } else { | |
721 g_free(buf); | |
722 g_free(first); | |
723 fclose(f); | |
724 return; | |
725 } | |
726 | |
2526
413a81136e3a
[gaim-migrate @ 2539]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
727 parse_toc_buddy_list(gc, buf); |
2382 | 728 |
729 fclose(f); | |
730 | |
731 g_free(buf); | |
732 g_free(first); | |
733 } | |
734 | |
735 void do_export(struct gaim_connection *g) | |
736 { | |
737 FILE *dir; | |
738 FILE *f; | |
739 char buf[32 * 1024]; | |
740 char *file; | |
741 char path[PATHSIZE]; | |
742 char *g_screenname; | |
743 | |
744 file = gaim_user_dir(); | |
745 if (!file) | |
746 return; | |
747 | |
748 strcpy(buf, file); | |
749 dir = fopen(buf, "r"); | |
750 if (!dir) | |
751 mkdir(buf, S_IRUSR | S_IWUSR | S_IXUSR); | |
752 else | |
753 fclose(dir); | |
754 | |
755 g_screenname = get_screenname_filename(g->username); | |
756 | |
757 sprintf(path, "%s/%s.%d.blist", file, g_screenname, | |
758 (g->protocol == PROTO_OSCAR) ? PROTO_TOC : g->protocol); | |
759 if ((f = fopen(path, "w"))) { | |
760 debug_printf("writing %s\n", path); | |
761 toc_build_config(g, buf, 8192 - 1, TRUE); | |
762 fprintf(f, "%s\n", buf); | |
763 fclose(f); | |
764 chmod(buf, S_IRUSR | S_IWUSR); | |
765 } else { | |
766 debug_printf("unable to write %s\n", path); | |
767 } | |
768 | |
769 g_free(g_screenname); | |
770 g_free(file); | |
771 } |