aboutsummaryrefslogblamecommitdiffstats
path: root/mail/importers/netscape-importer.c
blob: 281d03353638b5d555445b380855d125aaa8c4b6 (plain) (tree)
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
















































































































































































































































































































































































































































































                                                                                                             
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* netscape-importer.c
 * 
 * Authors: 
 *    Iain Holmes  <iain@ximian.com>
 *
 * Copyright 2001 Ximian, Inc. (http://www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>

#include <glib.h>
#include <gnome.h>

#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-generic-factory.h>
#include <camel/camel-mime-message.h>
#include <camel/camel-mime-part.h>
#include <camel/camel-exception.h>

#include <importer/evolution-intelligent-importer.h>
#include <importer/GNOME_Evolution_Importer.h>
#include "mail-importer.h"
#include "mail-tools.h"

static char *nsmail_dir = NULL;

extern char *evolution_dir;

#define NETSCAPE_INTELLIGENT_IMPORTER_IID "OAFIID:GNOME_Evolution_Mail_Netscape_Intelligent_Importer_Factory"
#define KEY "netscape-mail-imported"

/*#define SUPER_IMPORTER_DEBUG*/
#ifdef SUPER_IMPORTER_DEBUG
#define d(x) x
#else
#define d(x)
#endif

typedef struct {
    MailImporter importer;
    GList *dir_list;

    int num;
    CamelMimeParser *mp;
    BonoboListener *listener;
} NetscapeImporter;

static gboolean
netscape_import_mbox (CamelFolder *folder,
              const char *filename)
{
    gboolean done = FALSE;
    CamelException *ex;
    CamelMimeParser *mp;
    int fd, n = 0;

    fd = open (filename, O_RDONLY);
    if (fd == -1) {
        g_warning ("Cannot open %s", filename);
        return FALSE;
    }

    camel_object_ref (CAMEL_OBJECT (folder));
    camel_folder_freeze (folder);

    ex = camel_exception_new ();
    mp = camel_mime_parser_new ();
    camel_mime_parser_scan_from (mp, TRUE);
    if (camel_mime_parser_init_with_fd (mp, fd) == -1) {
        g_warning ("Unable to process file %s", filename);
        camel_object_unref (CAMEL_OBJECT (mp));
        camel_folder_thaw (folder);
        camel_object_unref (CAMEL_OBJECT (folder));
        return FALSE;
    }

    while (camel_mime_parser_step (mp, 0, 0) == HSCAN_FROM) {
        /* Import the next message */
        CamelMimeMessage *msg;
        CamelMessageInfo *info;

        msg = camel_mime_message_new ();
        if (camel_mime_part_construct_from_parser (CAMEL_MIME_PART (msg),
                               mp) == -1) {
            g_warning ("Failed message %d", n);
            camel_object_unref (CAMEL_OBJECT (msg));
            done = TRUE;
        }

        info = g_new0 (CamelMessageInfo, 1);
        camel_folder_append_message (folder, msg, info, ex);
        g_free (info);
        camel_object_unref (CAMEL_OBJECT (msg));
        if (camel_exception_is_set (ex)) {
            g_warning ("Failed message %d", n);
            done = TRUE;
        }

        if (!done) {
            n++;
            camel_mime_parser_step (mp, 0, 0);
        }
    }

    camel_folder_sync (folder, FALSE, ex);
    camel_folder_thaw (folder);
    camel_object_unref (CAMEL_OBJECT (folder));
    done = TRUE;

    camel_exception_free (ex);
    return done;
}

static void
netscape_clean_up (void)
{
    g_free (nsmail_dir);
    nsmail_dir = NULL;
}

static gboolean
netscape_can_import (EvolutionIntelligentImporter *ii,
             void *closure)
{
    NetscapeImporter *importer = closure;
    char *nsprefs;
    FILE *prefs_handle;
    char *key;

    /* Already imported */
    key = g_strdup_printf ("=%s/config/Mail=/importers/", evolution_dir);
    gnome_config_push_prefix (key);
    g_free (key);

    if (gnome_config_get_bool (KEY) == TRUE) {
        gnome_config_pop_prefix ();
        return FALSE;
    }
    gnome_config_pop_prefix ();

    nsprefs = gnome_util_prepend_user_home (".netscape/preferences.js");
    prefs_handle = fopen (nsprefs, "r");
    g_free (nsprefs);

    if (prefs_handle == NULL) {
        d(g_warning ("No .netscape/preferences.js"));
        return FALSE;
    }

    /* Find the user mail dir */
    while (1) {
        char line[4096];

        fgets (line, 4096, prefs_handle);
        if (line == NULL) {
            g_warning ("No mail.directory entry");
            fclose (prefs_handle);
            return FALSE;
        }

        if (strstr (line, "mail.directory") != NULL) {
            char *sep, *start, *end;
            /* Found the line */
            
            sep = strchr (line, ',');
            if (sep == NULL) {
                g_warning ("Bad line %s", line);
                fclose (prefs_handle);
                return FALSE;
            }

            start = strchr (sep, '\"') + 1;
            if (start == NULL) {
                g_warning ("Bad line %s", line);
                fclose (prefs_handle);
                return FALSE;
            }

            end = strrchr (sep, '\"');
            if (end == NULL) {
                g_warning ("Bad line %s", line);
                fclose (prefs_handle);
                return FALSE;
            }
            
            nsmail_dir = g_strndup (start, end - start);
            d(g_warning ("Got nsmail_dir: %s", nsmail_dir));
            fclose (prefs_handle);
            return TRUE;
        }
    }
}

static void
netscape_import_file (NetscapeImporter *importer,
              const char *parent,
              const char *dirname,
              const char *filename,
              const char *fullpath)
{
    char *summary, *summarypath, *foldername, *path;
    char *protocol;
    CamelException *ex;
    CamelFolder *folder;
    
    /* Check that the file is a netscape mbox. 
       It should have an associated .summary file */
    summary = g_strdup_printf (".%s.summary", filename);
    summarypath = g_concat_dir_and_file (dirname, summary);
    if (!g_file_exists (summarypath)) {
        d(g_warning ("%s does not exist.\nIgnoring %s", summary,
                 filename));
        g_free (summary);
        g_free (summarypath);
        return;
    }

    g_free (summary);
    g_free (summarypath);

    /* Do import */
    foldername = g_concat_dir_and_file (parent, filename);

    d(g_warning ("Importing %s as %s\n", filename, fullpath));

    ex = camel_exception_new ();
    protocol = g_strconcat ("file://", fullpath, NULL);
    folder = mail_tool_uri_to_folder (protocol, ex);
    if (camel_exception_is_set (ex)) {
        g_free (protocol);
        camel_exception_free (ex);
        return;
    }

    if (folder == NULL) {
        g_warning ("Folder for %s is NULL", fullpath);
        camel_exception_free (ex);
        return;
    }

    camel_exception_free (ex);

    path = g_concat_dir_and_file (dirname, filename);
    netscape_import_mbox (folder, path);
    g_free (path);
}

typedef struct {
    NetscapeImporter *importer;
    char *parent;
    char *dirname;
    char *filename;
} NetscapeCreateDirectoryData;

static void
netscape_dir_created (BonoboListener *listener,
              const char *event_name,
              const BonoboArg *event_data,
              CORBA_Environment *ev,
              NetscapeImporter *importer)
{
    NetscapeCreateDirectoryData *data;
    GList *l;
    GNOME_Evolution_Storage_FolderResult *result;
    char *fullpath;
    
    if (strcmp (event_name, "evolution-shell:folder_created") != 0) {
        return; /* Unknown event notification */
    }

    result = event_data->_value;
    fullpath = result->path;

    l = importer->dir_list;
    importer->dir_list = g_list_remove_link (importer->dir_list, l);
    data = l->data;
    g_list_free_1 (l);

    /* Import the file */
    /* We got the folder, so try to import the file into it. */
    netscape_import_file (data->importer, data->parent, 
                  data->dirname, data->filename, fullpath);

    g_free (data->parent);
    g_free (data->dirname);
    g_free (data->filename);
    g_free (data);

    if (importer->dir_list) {
        /* Do the next in the list */
        data = importer->dir_list->data;
        mail_importer_create_folder (data->parent, data->filename, NULL, 
                         importer->listener);
    }
}

/* This function basically flattens the tree structure.
   It makes a list of all the directories that are to be imported. */
static void
scan_dir (NetscapeImporter *importer,
      const char *orig_parent,
      const char *dirname)
{
    DIR *nsmail;
    struct stat buf;
    struct dirent *current;

    nsmail = opendir (dirname);
    if (nsmail == NULL) {
        d(g_warning ("Could not open %s\nopendir returned: %s", 
                 dirname, g_strerror (errno)));
        return;
    }

    current = readdir (nsmail);
    while (current) {
        char *fullname;

        /* Ignore things which start with . 
           which should be ., .., and the summaries. */
        if (current->d_name[0] =='.') {
            current = readdir (nsmail);
            continue;
        }

        fullname = g_concat_dir_and_file (dirname, current->d_name);
        if (stat (fullname, &buf) == -1) {
            d(g_warning ("Could not stat %s\nstat returned:%s",
                     fullname, g_strerror (errno)));
            current = readdir (nsmail);
            g_free (fullname);
            continue;
        }

        if (S_ISREG (buf.st_mode)) {
            char *sbd, *dir, *parent;
            NetscapeCreateDirectoryData *data;

            d(g_print ("File: %s\n", fullname));

            data = g_new0 (NetscapeCreateDirectoryData, 1);
            data->importer = importer;
            data->parent = g_strdup (orig_parent);
            data->dirname = g_strdup (dirname);
            data->filename = g_strdup (current->d_name);

            importer->dir_list = g_list_append (importer->dir_list,
                                data);

    
            parent = g_concat_dir_and_file (orig_parent, 
                            data->filename);
            
            /* Check if a .sbd folder exists */
            dir = g_concat_dir_and_file (data->dirname, data->filename);
            sbd = g_strconcat (dir, ".sbd", NULL);
            g_free (dir);
            if (g_file_exists (sbd)) {
                scan_dir (importer, parent, sbd);
            }
            
            g_free (parent);
            g_free (sbd);
        } 
        
        g_free (fullname);
        current = readdir (nsmail);
    }
}

static void
netscape_create_structure (EvolutionIntelligentImporter *ii,
               void *closure)
{
    NetscapeImporter *importer = closure;
    NetscapeCreateDirectoryData *data;
    char *key;

    g_return_if_fail (nsmail_dir != NULL);

    scan_dir (importer, "/", nsmail_dir);

    importer->listener = bonobo_listener_new (NULL, NULL);
    gtk_signal_connect (GTK_OBJECT (importer->listener), "event_notify",
                GTK_SIGNAL_FUNC (netscape_dir_created), importer);
    data = importer->dir_list->data;
    mail_importer_create_folder (data->parent, data->filename, NULL, 
                     importer->listener);

    key = g_strdup_printf ("=%s/config/Mail=/importers/", evolution_dir);
    gnome_config_push_prefix (key);
    g_free (key);

    gnome_config_set_bool (KEY, TRUE);
    gnome_config_pop_prefix ();

    gnome_config_sync ();
    gnome_config_drop_all ();
}

static BonoboObject *
netscape_factory_fn (BonoboGenericFactory *_factory,
         void *closure)
{
    EvolutionIntelligentImporter *importer;
    NetscapeImporter *netscape;
    char *message = N_("Evolution has found Netscape mail files.\n"
               "Would you like them to be imported into Evolution?");

    netscape = g_new0 (NetscapeImporter, 1);
    importer = evolution_intelligent_importer_new (netscape_can_import,
                               netscape_create_structure,
                               "Netscape mail", 
                               message, netscape);
    return BONOBO_OBJECT (importer);
}

/* Entry Point */
void
mail_importer_module_init (void)
{
    static gboolean initialised = FALSE;
    BonoboGenericFactory *factory;

    if (initialised == TRUE)
        return;

    factory = bonobo_generic_factory_new (NETSCAPE_INTELLIGENT_IMPORTER_IID,
                          netscape_factory_fn, NULL);
    if (factory == NULL)
        g_warning ("Could not initialise Netscape Intelligent Mail Importer.");
    initialised = TRUE;
}

/* GModule g_module_unload routine */
void 
g_module_unload (void)
{
    netscape_clean_up ();
}