1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/*
* Routines to copy information from a Gaim buddy list into an
* Evolution addressbook.
*
* I currently copy IM account names and buddy icons, provided you
* don't already have a buddy icon defined for a person.
*
* This works today (25 October 2004), but is pretty sure to break
* later on as the Gaim buddylist file format shifts.
*
* Nat Friedman <nat@novell.com>
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <glib.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <e-util/e-xml-utils.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <string.h>
#include <libebook/e-book.h>
#include <libedataserverui/e-source-combo-box.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <e-util/e-config.h>
#include "bbdb.h"
typedef struct {
char *account_name;
char *proto;
char *alias;
char *icon;
} GaimBuddy;
/* Defined in bbdb.c */
EBook *bbdb_open_addressbook (int type);
/* Forward declarations for this file. */
void bbdb_sync_buddy_list (void);
static gboolean bbdb_merge_buddy_to_contact (EBook *book, GaimBuddy *b, EContact *c);
static GList *bbdb_get_gaim_buddy_list (void);
static char *get_node_text (xmlNodePtr node);
static char *get_buddy_icon_from_setting (xmlNodePtr setting);
static void free_buddy_list (GList *blist);
static void parse_buddy_group (xmlNodePtr group, GList **buddies, GSList *blocked);
static EContactField proto_to_contact_field (const char *proto);
void
bbdb_sync_buddy_list_check (void)
{
GConfClient *gconf;
struct stat statbuf;
time_t last_sync;
char *blist_path;
char *last_sync_str;
gconf = gconf_client_get_default ();
blist_path = g_build_path ("/", getenv ("HOME"), ".purple/blist.xml", NULL);
if (stat (blist_path, &statbuf) < 0) {
g_free (blist_path);
g_object_unref (G_OBJECT (gconf));
return;
}
g_free (blist_path);
/* Reprocess the buddy list if it's been updated. */
last_sync_str = gconf_client_get_string (gconf, GCONF_KEY_GAIM_LAST_SYNC, NULL);
if (last_sync_str == NULL || ! strcmp ((const char *)last_sync_str, ""))
last_sync = (time_t) 0;
else
last_sync = (time_t) g_ascii_strtoull (last_sync_str, NULL, 10);
g_free (last_sync_str);
g_object_unref (G_OBJECT (gconf));
if (statbuf.st_mtime > last_sync) {
fprintf (stderr, "bbdb: Buddy list has changed since last sync.\n");
bbdb_sync_buddy_list ();
}
}
void
bbdb_sync_buddy_list (void)
{
GList *blist, *l;
EBook *book = NULL;
/* Get the Gaim buddy list */
blist = bbdb_get_gaim_buddy_list ();
if (blist == NULL)
return;
/* Open the addressbook */
book = bbdb_open_addressbook (GAIM_ADDRESSBOOK);
if (book == NULL) {
free_buddy_list (blist);
return;
}
printf ("bbdb: Synchronizing buddy list to contacts...\n");
/* Walk the buddy list */
for (l = blist; l != NULL; l = l->next) {
GaimBuddy *b = l->data;
EBookQuery *query;
GList *contacts;
GError *error = NULL;
EContact *c;
if (b->alias == NULL || strlen (b->alias) == 0)
b->alias = b->account_name;
/* Look for an exact match full name == buddy alias */
query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_IS, b->alias);
e_book_get_contacts (book, query, &contacts, NULL);
e_book_query_unref (query);
if (contacts != NULL) {
/* FIXME: If there's more than one contact with this
name, just give up; we're not smart enough for
this. */
if (contacts->next != NULL)
continue;
c = E_CONTACT (contacts->data);
if (! bbdb_merge_buddy_to_contact (book, b, c))
continue;
/* Write it out to the addressbook */
if (! e_book_commit_contact (book, c, &error)) {
g_warning ("bbdb: Could not modify contact: %s\n", error->message);
g_error_free (error);
}
continue;
}
/* Otherwise, create a new contact. */
c = e_contact_new ();
e_contact_set (c, E_CONTACT_FULL_NAME, (gpointer) b->alias);
if (! bbdb_merge_buddy_to_contact (book, b, c)) {
g_object_unref (G_OBJECT (c));
continue;
}
if (! e_book_add_contact (book, c, &error)) {
g_warning ("bbdb: Failed to add new contact: %s\n", error->message);
g_error_free (error);
return;
}
g_object_unref (G_OBJECT (c));
}
/* Update the last-sync'd time */
{
GConfClient *gconf;
time_t last_sync;
char *last_sync_str;
gconf = gconf_client_get_default ();
time (&last_sync);
last_sync_str = g_strdup_printf ("%ld", (glong) last_sync);
gconf_client_set_string (gconf, GCONF_KEY_GAIM_LAST_SYNC, last_sync_str, NULL);
g_free (last_sync_str);
g_object_unref (G_OBJECT (gconf));
}
printf ("bbdb: Done syncing buddy list to contacts.\n");
}
static gboolean
im_list_contains_buddy (GList *ims, GaimBuddy *b)
{
GList *l;
for (l = ims; l != NULL; l = l->next) {
char *im = (char *) l->data;
if (! strcmp (im, b->account_name))
return TRUE;
}
return FALSE;
}
static gboolean
bbdb_merge_buddy_to_contact (EBook *book, GaimBuddy *b, EContact *c)
{
EContactField field;
GList *ims, *l;
gboolean dirty = FALSE;
EContactPhoto *photo = NULL;
GError *error = NULL;
/* Set the IM account */
field = proto_to_contact_field (b->proto);
ims = e_contact_get (c, field);
if (! im_list_contains_buddy (ims, b)) {
ims = g_list_append (ims, (gpointer) b->account_name);
e_contact_set (c, field, (gpointer) ims);
dirty = TRUE;
}
/* Set the photo if it's not set */
if (b->icon != NULL) {
photo = e_contact_get (c, E_CONTACT_PHOTO);
if (photo == NULL) {
gchar *contents = NULL;
photo = g_new0 (EContactPhoto, 1);
photo->type = E_CONTACT_PHOTO_TYPE_INLINED;
if (! g_file_get_contents (b->icon, &contents, &photo->data.inlined.length, &error)) {
g_warning ("bbdb: Could not read buddy icon: %s\n", error->message);
g_error_free (error);
for (l = ims; l != NULL; l = l->next)
g_free ((char *) l->data);
g_list_free (ims);
return dirty;
}
photo->data.inlined.data = (unsigned char *)contents;
e_contact_set (c, E_CONTACT_PHOTO, (gpointer) photo);
dirty = TRUE;
}
}
/* Clean up */
if (photo != NULL)
e_contact_photo_free (photo);
for (l = ims; l != NULL; l = l->next)
g_free ((char *) l->data);
g_list_free (ims);
return dirty;
}
static EContactField
proto_to_contact_field (const char *proto)
{
if (! strcmp (proto, "prpl-oscar"))
return E_CONTACT_IM_AIM;
if (! strcmp (proto, "prpl-novell"))
return E_CONTACT_IM_GROUPWISE;
if (! strcmp (proto, "prpl-msn"))
return E_CONTACT_IM_MSN;
if (! strcmp (proto, "prpl-icq"))
return E_CONTACT_IM_ICQ;
if (! strcmp (proto, "prpl-yahoo"))
return E_CONTACT_IM_YAHOO;
if (! strcmp (proto, "prpl-jabber"))
return E_CONTACT_IM_JABBER;
if (! strcmp (proto, "prpl-gg"))
return E_CONTACT_IM_GADUGADU;
return E_CONTACT_IM_AIM;
}
static void
get_all_blocked (xmlNodePtr node, GSList **blocked)
{
xmlNodePtr child;
if (!node || !blocked)
return;
for (child = node->children; child; child = child->next) {
if (child->children)
get_all_blocked (child, blocked);
if (!strcmp ((const char *)child->name, "block")) {
char *name = get_node_text (child);
if (name)
*blocked = g_slist_prepend (*blocked, name);
}
}
}
static GList *
bbdb_get_gaim_buddy_list (void)
{
char *blist_path;
xmlDocPtr buddy_xml;
xmlNodePtr root, child, blist;
GList *buddies = NULL;
GSList *blocked = NULL;
blist_path = g_build_path ("/", getenv ("HOME"), ".purple/blist.xml", NULL);
buddy_xml = xmlParseFile (blist_path);
g_free (blist_path);
if (! buddy_xml) {
fprintf (stderr, "bbdb: Could not open Pidgin buddy list.\n");
return NULL;
}
root = xmlDocGetRootElement (buddy_xml);
if (strcmp ((const char *)root->name, "purple")) {
fprintf (stderr, "bbdb: Could not parse Pidgin buddy list.\n");
xmlFreeDoc (buddy_xml);
return NULL;
}
for (child = root->children; child != NULL; child = child->next) {
if (! strcmp ((const char *)child->name, "privacy")) {
get_all_blocked (child, &blocked);
break;
}
}
blist = NULL;
for (child = root->children; child != NULL; child = child->next) {
if (! strcmp ((const char *)child->name, "blist")) {
blist = child;
break;
}
}
if (blist == NULL) {
fprintf (stderr, "bbdb: Could not find 'blist' element in Pidgin buddy list.\n");
xmlFreeDoc (buddy_xml);
return NULL;
}
for (child = blist->children; child != NULL; child = child->next) {
if (! strcmp ((const char *)child->name, "group"))
parse_buddy_group (child, &buddies, blocked);
}
xmlFreeDoc (buddy_xml);
g_slist_foreach (blocked, (GFunc)g_free, NULL);
g_slist_free (blocked);
return buddies;
}
static void
free_gaim_body (GaimBuddy *gb)
{
if (!gb)
return;
g_free (gb->icon);
g_free (gb->alias);
g_free (gb->account_name);
g_free (gb->proto);
g_free (gb);
}
static void
free_buddy_list (GList *blist)
{
g_list_foreach (blist, (GFunc)free_gaim_body, NULL);
g_list_free (blist);
}
static char *
get_node_text (xmlNodePtr node)
{
if (node->children == NULL || node->children->content == NULL ||
strcmp ((char *)node->children->name, "text"))
return NULL;
return g_strdup ((char *)node->children->content);
}
static char *
get_buddy_icon_from_setting (xmlNodePtr setting)
{
char *icon = NULL;
icon = get_node_text (setting);
if (icon [0] != '/') {
char *path;
path = g_build_path ("/", getenv ("HOME"), ".purple/icons", icon, NULL);
g_free (icon);
icon = path;
}
return icon;
}
static void
parse_contact (xmlNodePtr contact, GList **buddies, GSList *blocked)
{
xmlNodePtr child;
xmlNodePtr buddy = NULL;
GaimBuddy *gb;
gboolean is_blocked = FALSE;
for (child = contact->children; child != NULL; child = child->next) {
if (! strcmp ((const char *)child->name, "buddy")) {
buddy = child;
break;
}
}
if (buddy == NULL) {
fprintf (stderr, "bbdb: Could not find buddy in contact. Malformed Pidgin buddy list file.\n");
return;
}
gb = g_new0 (GaimBuddy, 1);
gb->proto = e_xml_get_string_prop_by_name (buddy, (const unsigned char *)"proto");
for (child = buddy->children; child != NULL && !is_blocked; child = child->next) {
if (! strcmp ((const char *)child->name, "setting")) {
char *setting_type;
setting_type = e_xml_get_string_prop_by_name (child, (const unsigned char *)"name");
if (! strcmp ((const char *)setting_type, "buddy_icon"))
gb->icon = get_buddy_icon_from_setting (child);
g_free (setting_type);
} else if (! strcmp ((const char *)child->name, "name")) {
gb->account_name = get_node_text (child);
is_blocked = g_slist_find_custom (blocked, gb->account_name, (GCompareFunc)strcmp) != NULL;
} else if (! strcmp ((const char *)child->name, "alias"))
gb->alias = get_node_text (child);
}
if (is_blocked)
free_gaim_body (gb);
else
*buddies = g_list_prepend (*buddies, gb);
}
static void
parse_buddy_group (xmlNodePtr group, GList **buddies, GSList *blocked)
{
xmlNodePtr child;
for (child = group->children; child != NULL; child = child->next) {
if (strcmp ((const char *)child->name, "contact"))
continue;
parse_contact (child, buddies, blocked);
}
}
|