aboutsummaryrefslogtreecommitdiffstats
path: root/mail/evolution-outlook-importer.c
blob: 35fa2e73151e60631f59b757f428ec04c7d733d0 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* evolution-outlook-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 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 "evolution-outlook-importer.h"

#include <stdio.h>

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

#include "mail-importer.h"

#include <camel/camel-exception.h>

extern char *evolution_dir;
typedef struct {
    MailImporter importer;

    char *filename;
    gboolean oe4; /* Is file OE4 or not? */
    FILE *handle;
    long pos;
    off_t size;

    gboolean busy;
} OutlookImporter;

struct oe_msg_segmentheader {
    int self;
    int increase;
    int include;
    int next;
    int usenet;
};

typedef struct oe_msg_segmentheader oe_msg_segmentheader;


/* EvolutionImporter methods */

/* Based on code from liboe 0.92 (STABLE)
   Copyright (C) 2000 Stephan B. Nedregård (stephan@micropop.com)
   Modified 2001 Iain Holmes  <iain@ximian.com>
   Copyright (C) 2001 Ximian, Inc. */

static void
process_item_fn (EvolutionImporter *eimporter,
         CORBA_Object listener,
         void *closure,
         CORBA_Environment *ev)
{
    OutlookImporter *oli = (OutlookImporter *) closure;
    MailImporter *importer = (MailImporter *) oli;
    oe_msg_segmentheader *header;
    gboolean more = TRUE;
    char *cb, *sfull, *s;
    long end_pos = 0;
    int i;

    if (oli->busy == TRUE) {
        GNOME_Evolution_ImporterListener_notifyResult (listener, 
                                   GNOME_Evolution_ImporterListener_BUSY,
                                   more, ev);
        return;
    }

    oli->busy = TRUE;
    header = g_new (oe_msg_segmentheader, 1);
    fread (header, 16, 1, oli->handle);

    /* Write a From line */
    mail_importer_add_line (importer, 
                "From evolution-outlook-importer", FALSE);
    end_pos = oli->pos + header->include;
    if (end_pos >= oli->size) {
        end_pos = oli->size;
        more = FALSE;
    }

    oli->pos += 4;

    cb = g_new (char, 4);
    sfull = g_new (char, 65536);
    s = sfull;

    while (oli->pos < end_pos) {
        fread (cb, 1, 4, oli->handle);
        for (i = 0; i < 4; i++, oli->pos++) {
            if (*(cb + i ) != 0x0d) {
                *s++ = *(cb + i);

                if (*(cb + i) == 0x0a) {
                    *s = '\0';
                    mail_importer_add_line (importer, 
                                sfull, FALSE);
                    s = sfull;
                }
            }
        }
    }

    if (s != sfull) {
        *s = '\0';
        mail_importer_add_line (importer, sfull, FALSE);
        s = sfull;
    }

    mail_importer_add_line (importer, "\n", TRUE);

    oli->pos = end_pos;
    fseek (oli->handle, oli->pos, SEEK_SET);

    g_free (header);
    g_free (sfull);
    g_free (cb);

    GNOME_Evolution_ImporterListener_notifyResult (listener, 
                               GNOME_Evolution_ImporterListener_OK,
                               more, ev);
    if (more == FALSE) {
        CamelException *ex;

        ex = camel_exception_new ();
        camel_folder_thaw (importer->folder);
        camel_folder_sync (importer->folder, FALSE, ex);
        camel_exception_free (ex);
        fclose (oli->handle);
        oli->handle = NULL;
    }

    oli->busy = FALSE;
    return;
}


/* EvolutionImporterFactory methods */

static gboolean
support_format_fn (EvolutionImporter *importer,
           const char *filename,
           void *closure)
{
    FILE *handle;
    int signature[4];

    /* Outlook Express sniffer.
       Taken from liboe 0.92 (STABLE)
       Copyright (C) 2000 Stephan B. Nedregård (stephan@micropop.com) */

    handle = fopen (filename, "rb");
    if (handle == NULL)
        return FALSE; /* Can't open file: Can't support it :) */

      /* SIGNATURE */
    fread (&signature, 16, 1, handle); 
    if ((signature[0]!=0xFE12ADCF) || /* OE 5 & OE 5 BETA SIGNATURE */
        (signature[1]!=0x6F74FDC5) ||
        (signature[2]!=0x11D1E366) ||
        (signature[3]!=0xC0004E9A)) {
        if ((signature[0]==0x36464D4A) &&
            (signature[1]==0x00010003)) /* OE4 SIGNATURE */ {
            fclose (handle);
            return TRUE; /* OE 4 */
        }
        fclose (handle);
        return FALSE; /* Not Outlook 4 or 5 */
    }

    fclose (handle);
    return FALSE; /* Can't handle OE 5 yet */
}

static void
importer_destroy_cb (GtkObject *object,
             OutlookImporter *oli)
{
    MailImporter *importer;

    importer = (MailImporter *) oli;
    if (importer->folder)
        camel_object_unref (CAMEL_OBJECT (importer->folder));

    g_free (oli->filename);
    if (oli->handle)
        fclose (oli->handle);

    g_free (oli);
}

static gboolean
load_file_fn (EvolutionImporter *eimporter,
          const char *filename,
          void *closure)
{
    OutlookImporter *oli;
    MailImporter *importer;
    struct stat buf;
    long pos = 0x54;

    oli = (OutlookImporter *) closure;
    importer = (MailImporter *) oli;

    oli->filename = g_strdup (filename);
    /* Will return TRUE if oe4 format */
    oli->oe4 = support_format_fn (NULL, filename, NULL);
    if (oli->oe4 == FALSE) {
        g_warning ("Not OE4 format");
        return FALSE;
    }

    oli->handle = fopen (filename, "rb");
    if (oli->handle == NULL) {
        g_warning ("Cannot open the file");
        return FALSE;
    }

    /* Get size of file */
    if (stat (filename, &buf) == -1) {
        g_warning ("Cannot stat file");
        return FALSE;
    }
    
    oli->size = buf.st_size;

    /* Set the fposition to the begining */
    fseek (oli->handle, pos, SEEK_SET);
    oli->pos = pos;

    importer->mstream = NULL;

    importer->folder = mail_importer_get_folder ("Inbox", NULL);

    if (importer->folder == NULL){
        g_warning ("Bad folder");
        return FALSE;
    }

    camel_folder_freeze (importer->folder);
    oli->busy = FALSE;
    return TRUE;
}

BonoboObject *
outlook_factory_fn (BonoboGenericFactory *_factory,
            void *closure)
{
    EvolutionImporter *importer;
    OutlookImporter *oli;

    oli = g_new0 (OutlookImporter, 1);

    importer = evolution_importer_new (support_format_fn, load_file_fn, 
                       process_item_fn, NULL, oli);
    gtk_signal_connect (GTK_OBJECT (importer), "destroy",
                GTK_SIGNAL_FUNC (importer_destroy_cb), oli);

    return BONOBO_OBJECT (importer);
}