aboutsummaryrefslogtreecommitdiffstats
path: root/mail/importers/evolution-mbox-importer.c
blob: 1dfeb93b40526118672cbcd45e12805f998c6b36 (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* evolution-mbox-importer.c
 *
 * Authors: Iain Holmes <iain@ximian.com>
 *
 * Copyright (C) 2001  Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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 <ctype.h>

#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-generic-factory.h>

#include <camel/camel-exception.h>
#include <camel/camel-mime-message.h>
#include <camel/camel-mime-parser.h>
#include <camel/camel-mime-part.h>

#include <importer/evolution-importer.h>
#include <importer/GNOME_Evolution_Importer.h>

#include "mozilla-status-headers.h"

#include "mail/mail-importer.h"
#include "mail-tools.h"

#include "e-util/e-path.h"

/*  #define IMPORTER_DEBUG */
#ifdef IMPORTER_DEBUG
#define IN g_print ("=====> %s (%d)\n", __FUNCTION__, __LINE__)
#define OUT g_print ("<==== %s (%d)\n", __FUNCTION__, __LINE__)
#else
#define IN
#define OUT
#endif

#define MBOX_FACTORY_IID "OAFIID:GNOME_Evolution_Mail_Mbox_ImporterFactory"

typedef struct {
    MailImporter importer; /* Parent */

    char *filename;
    int num;
    CamelMimeParser *mp;
    gboolean is_folder;
} MboxImporter;

void mail_importer_module_init (void);

/* EvolutionImporter methods */

static int
string_to_int (const char *str)
{
    int result = 0;
    char *s;

    for (s = (char *) str; *s; s++) {
        char c = toupper (*s);

        result *= 16;

        if (c >= '0' && c <= '9') {
            result += (c - '0');
        } else if (c >= 'A' && c <= 'F') {
            result += (c - 'A');
        }
    }

    g_print ("%s became %d\n", str, result);

    return result;
}

static CamelMessageInfo *
get_info_from_mozilla (const char *mozilla_status,
               gboolean *deleted)
{
    int status;
    CamelMessageInfo *info;

    info = g_new0 (CamelMessageInfo, 1);

    *deleted = FALSE;

    status = string_to_int (mozilla_status);
    if (status == 0) {
        return info;
    }

    if (status & MSG_FLAG_EXPUNGED) {
        *deleted = TRUE;
        g_free (info);

        return NULL;
    }

    if (status & MSG_FLAG_READ) {
        info->flags |= CAMEL_MESSAGE_SEEN;
    }

    if (status & MSG_FLAG_MARKED) {
        info->flags |= CAMEL_MESSAGE_FLAGGED;
    }

    if (status & MSG_FLAG_REPLIED) {
        info->flags |= CAMEL_MESSAGE_ANSWERED;
    }

    return info;
}

static void
process_item_fn (EvolutionImporter *eimporter,
         CORBA_Object listener,
         void *closure,
         CORBA_Environment *ev)
{
    MboxImporter *mbi = (MboxImporter *) closure;
    MailImporter *importer = (MailImporter *) mbi;
    gboolean done = FALSE;
    CamelException *ex;
    char *mozilla_status;

    if (importer->folder == NULL) {
        GNOME_Evolution_ImporterListener_notifyResult (listener,
                                   GNOME_Evolution_ImporterListener_NOT_READY,
                                   TRUE, ev);
        return;
    }

    if (mbi->is_folder == TRUE) {
        GNOME_Evolution_ImporterListener_notifyResult (listener,
                                   GNOME_Evolution_ImporterListener_OK,
                                   FALSE, ev);
        return;
    }
        
    ex = camel_exception_new ();
    if (camel_mime_parser_step (mbi->mp, 0, 0) == HSCAN_FROM) {
        /* Import the next message */
        CamelMimeMessage *msg;
        CamelMessageInfo *info;
        gboolean deleted;

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

        mozilla_status = camel_medium_get_header (CAMEL_MEDIUM (msg), "X-Mozilla-Status");
        if (mozilla_status != NULL) {
            g_print ("Got Mozilla status header: %s\n", mozilla_status);
            info = get_info_from_mozilla (mozilla_status, &deleted);
        } else {
            info = g_new0 (CamelMessageInfo, 1);
        }

        if (deleted == FALSE) {
        /* write the mesg */
            camel_folder_append_message (importer->folder, msg, info, ex);
            g_free (info);
        }

        camel_object_unref (CAMEL_OBJECT (msg));
        if (camel_exception_is_set (ex)) {
            g_warning ("Failed message %d", mbi->num);
            done = TRUE;
        }
        OUT;
    } else {
        IN;
        /* all messages have now been imported */
        camel_folder_sync (importer->folder, FALSE, ex);
        camel_folder_thaw (importer->folder);
        importer->frozen = FALSE;
        done = TRUE;
        OUT;
    }

    if (!done) {
        camel_mime_parser_step (mbi->mp, 0, 0);
    }

    camel_exception_free (ex);
    GNOME_Evolution_ImporterListener_notifyResult (listener,
                               GNOME_Evolution_ImporterListener_OK,
                               !done, ev);
    return;
}

static gboolean
support_format_fn (EvolutionImporter *importer,
           const char *filename,
           void *closure)
{
    char signature[6];
    gboolean ret = FALSE;
    int fd, n;

    fd = open (filename, O_RDONLY);
    if (fd == -1)
        return FALSE;

    n = read (fd, signature, 5);
    if (n > 0) {
        signature[n] = '\0';
        if (!g_strncasecmp (signature, "From ", 5))
            ret = TRUE;
    }

    close (fd);

    return ret; 
}

static void
importer_destroy_cb (GtkObject *object,
             MboxImporter *mbi)
{
    MailImporter *importer;

    importer = (MailImporter *) mbi;
    if (importer->frozen) {
        camel_folder_sync (importer->folder, FALSE, NULL);
        camel_folder_thaw (importer->folder);
    }

    if (importer->folder)
        camel_object_unref (CAMEL_OBJECT (importer->folder));

    g_free (mbi->filename);
    if (mbi->mp)
        camel_object_unref (CAMEL_OBJECT (mbi->mp));

    g_free (mbi);
}

static void
folder_created_cb (BonoboListener *listener,
           const char *event_name,
           const BonoboArg *event_data,
           CORBA_Environment *ev,
           MailImporter *importer)
{
    char *fullpath;
    GNOME_Evolution_Storage_FolderResult *result;
    CamelException *ex;

    if (strcmp (event_name, "evolution-shell:folder_created") != 0) {
        return; /* Unknown event */
    }

    result = event_data->_value;
    fullpath = g_strconcat ("file://", result->path, NULL);

    ex = camel_exception_new ();
    importer->folder = mail_tool_uri_to_folder (fullpath, CAMEL_STORE_FOLDER_CREATE, ex);
    if (camel_exception_is_set (ex)) {
        g_warning ("Error opening %s", fullpath);
        camel_exception_free (ex);

        g_free (fullpath);
        return;
    }

    camel_folder_freeze (importer->folder);
    importer->frozen = TRUE;

    g_free (fullpath);
    bonobo_object_unref (BONOBO_OBJECT (listener));
}

static gboolean
load_file_fn (EvolutionImporter *eimporter,
          const char *filename,
          const char *folderpath,
          void *closure)
{
    MboxImporter *mbi;
    MailImporter *importer;
    gboolean delayed = FALSE;
    struct stat buf;
    int fd;

    mbi = (MboxImporter *) closure;
    importer = (MailImporter *) mbi;

    mbi->filename = g_strdup (filename);

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

    fstat (fd, &buf);
    if (S_ISREG (buf.st_mode)) {
        mbi->mp = camel_mime_parser_new ();
        camel_mime_parser_scan_from (mbi->mp, TRUE);
        if (camel_mime_parser_init_with_fd (mbi->mp, fd) == -1) {
            g_warning ("Unable to process spool folder");
            goto fail;
        }
        mbi->is_folder = FALSE;
    } else {
        mbi->is_folder = TRUE;
    }

    importer->mstream = NULL;
    if (folderpath == NULL || *folderpath == '\0') {
        importer->folder = mail_tool_get_local_inbox (NULL);
    } else {
        char *parent, *tmp, *fullpath, *homedir;
        const char *name;
        BonoboListener *listener;
        CamelException *ex;
        
        tmp = gnome_util_prepend_user_home ("evolution/local");
        homedir = g_strconcat ("file://", tmp, NULL);
        g_free (tmp);

        fullpath = e_path_to_physical (homedir, folderpath);
        ex = camel_exception_new ();
        importer->folder = mail_tool_uri_to_folder (fullpath, 0, ex);
        g_free (homedir);
    
        if (camel_exception_is_set (ex) || importer->folder == NULL) {
            /* Make a new directory */
            name = strrchr (folderpath, '/');
            if (name == NULL) {
                parent = g_strdup ("/");
                name = folderpath;
            } else {
                name += 1;
                parent = g_dirname (folderpath);
            }
            
            listener = bonobo_listener_new (NULL, NULL);
            gtk_signal_connect (GTK_OBJECT (listener), "event-notify",
                        GTK_SIGNAL_FUNC (folder_created_cb),
                        importer);
            
            mail_importer_create_folder (parent, name, NULL, listener);
            camel_exception_free (ex);
            ex = camel_exception_new ();
            importer->folder = NULL;
            g_print ("No folder yet\n");
            delayed = TRUE;
            g_free (parent);
        }
        camel_exception_free (ex);
        g_free (fullpath);
    }

    if (importer->folder == NULL && delayed == FALSE){
        g_warning ("Bad folder\n");
        goto fail;
    }

    if (importer->folder != NULL) { 
        camel_folder_freeze (importer->folder);
        importer->frozen = TRUE;
    }

    return TRUE;

 fail:
    camel_object_unref (CAMEL_OBJECT (mbi->mp));
    mbi->mp = NULL;

    return FALSE;
}

static BonoboObject *
mbox_factory_fn (BonoboGenericFactory *_factory,
         void *closure)
{
    EvolutionImporter *importer;
    MboxImporter *mbox;
    
    mbox = g_new0 (MboxImporter, 1);
    importer = evolution_importer_new (support_format_fn, load_file_fn,
                       process_item_fn, NULL, mbox);
    gtk_signal_connect (GTK_OBJECT (importer), "destroy",
                GTK_SIGNAL_FUNC (importer_destroy_cb), mbox);

    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 (MBOX_FACTORY_IID, 
                          mbox_factory_fn, NULL);

    if (factory == NULL)
        g_warning ("Could not initialise Outlook importer factory.");

    initialised = TRUE;
}