aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-importer.c
blob: a915c0466828c4b082f5d2570a3c19880ba14043 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* mail-importer.c
 *
 * Authors: Iain Holmes <iain@ximian.com>
 *
 * Copyright (C) 2001-2003 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 <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <gmodule.h>
#include <libgnome/gnome-util.h>
#include <evolution-storage.h>
#include <camel/camel-folder.h>
#include <camel/camel-mime-message.h>
#include <camel/camel-stream-mem.h>
#include <camel/camel-exception.h>
#include <e-util/e-path.h>

#include "mail-component.h"
#include "mail-importer.h"
#include "mail-local.h"
#include "mail.h"

static GList *importer_modules = NULL;
static GNOME_Evolution_Storage local_storage = NULL;

void mail_importer_uninit (void);

struct _create_data {
    GNOME_Evolution_Storage_Result create_result;
    int create_done:1;
};

static void
folder_created_cb(BonoboListener *listener, const char *event_name, const BonoboArg *event_data,
          CORBA_Environment *ev, struct _create_data *data)
{
    GNOME_Evolution_Storage_FolderResult *result;

    data->create_done = TRUE;

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

    result = event_data->_value;
    data->create_result = result->result;
}

/**
 * mail_importer_make_local_folder:
 * @folderpath: 
 * 
 * Check a local folder exists at path @folderpath, and if not, create it.
 * 
 * Return value: The physical uri of the folder, or NULL if the folder did
 * not exist and could not be created.
 **/
char *
mail_importer_make_local_folder(const char *folderpath)
{
    CORBA_Environment ev;
    char *uri = NULL, *tmp;
    GNOME_Evolution_Folder *fi;
    BonoboListener *listener;

    CORBA_exception_init (&ev);

    /* first, check, does this folder exist, if so, use the right path */
    fi = GNOME_Evolution_Storage_getFolderAtPath(local_storage, folderpath, &ev);
    if (fi) {
        printf("folder %s exists @ %s\n", folderpath, fi->physicalUri);
        uri = g_strdup(fi->physicalUri);
        CORBA_free(fi);
    } else {
        struct _create_data data = { GNOME_Evolution_Storage_GENERIC_ERROR, FALSE };

        tmp = g_strdup_printf("file://%s/local",
                      mail_component_peek_base_directory (mail_component_peek ()));
        uri = e_path_to_physical(tmp, folderpath);
        g_free(tmp);
        tmp = strrchr(uri, '/');
        tmp[0] = 0;

        printf("Creating folder %s, parent %s\n", folderpath, uri);

        listener = bonobo_listener_new (NULL, NULL);
        g_signal_connect(listener, "event-notify", G_CALLBACK (folder_created_cb), &data);

        GNOME_Evolution_Storage_asyncCreateFolder(local_storage, folderpath, "mail",  "", uri,
                              bonobo_object_corba_objref((BonoboObject *)listener), &ev);

        while (!data.create_done)
            g_main_context_iteration(NULL, TRUE);

        bonobo_object_unref((BonoboObject *)listener);

        if (data.create_result != GNOME_Evolution_Storage_OK) {
            g_free(uri);
            uri = NULL;
        } else {
            *tmp = '/';
        }
    }

    CORBA_exception_free (&ev);

    return uri;
}

/**
 * mail_importer_add_line:
 * importer: A MailImporter structure.
 * str: Next line of the mbox.
 * finished: TRUE if @str is the last line of the message.
 *
 * Adds lines to the message until it is finished, and then adds
 * the complete message to the folder.
 */
void
mail_importer_add_line (MailImporter *importer,
            const char *str,
            gboolean finished)
{
    CamelMimeMessage *msg;
    CamelMessageInfo *info;
    CamelException *ex;
    
    if (importer->mstream == NULL)
        importer->mstream = CAMEL_STREAM_MEM (camel_stream_mem_new ());

    camel_stream_write (CAMEL_STREAM (importer->mstream), str,  strlen (str));
    
    if (finished == FALSE)
        return;

    camel_stream_reset (CAMEL_STREAM (importer->mstream));
    info = g_new0 (CamelMessageInfo, 1);
    info->flags = CAMEL_MESSAGE_SEEN;

    msg = camel_mime_message_new ();
    camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER (msg),
                          CAMEL_STREAM (importer->mstream));
    
    camel_object_unref (importer->mstream);
    importer->mstream = NULL;

    ex = camel_exception_new ();
    camel_folder_append_message (importer->folder, msg, info, NULL, ex);
    camel_object_unref (msg);

    camel_exception_free (ex);
    g_free (info);
}

/* module management */
static GList *
get_importer_list (void)
{
    DIR *dir;
    struct dirent *d;
    GList *importers_ret = NULL;

    dir = opendir (MAIL_IMPORTERSDIR);
    if (!dir) {
        g_warning ("No importers dir: %s", MAIL_IMPORTERSDIR);
        return NULL;
    }

    while ((d = readdir (dir))) {
        char *path, *ext;

        ext = strchr (d->d_name, '.');
        if (!ext || strcmp (ext, ".so") != 0)
            continue;

        path = g_build_filename (MAIL_IMPORTERSDIR, d->d_name, NULL);
        importers_ret = g_list_prepend (importers_ret, path);
    }

    closedir (dir);
    return importers_ret;
}

static void
free_importer_list (GList *list)
{
    for (; list; list = list->next) {
        g_free (list->data);
    }

    g_list_free (list);
}

/**
 * mail_importer_init:
 *
 * Initialises all the importers
 */
void
mail_importer_init (EvolutionShellClient *client)
{
    GList *importers, *l;

    if (importer_modules != NULL) {
        return;
    }

    local_storage = evolution_shell_client_get_local_storage (client);

    if (!g_module_supported ()) {
        g_warning ("Could not initialise the importers as module loading"
               " is not supported on this system");
        return;
    }

    importers = get_importer_list ();
    if (importers == NULL)
        return;

    for (l = importers; l; l = l->next) {
        GModule *module;

        module = g_module_open (l->data, 0);
        if (!module) {
            g_warning ("Could not load: %s: %s", (char *) l->data,
                   g_module_error ());
        } else {
            void *(*mail_importer_module_init) ();

            if (!g_module_symbol (module, "mail_importer_module_init",
                          (gpointer *)&mail_importer_module_init)) {
                g_warning ("Could not load %s: No initialisation",
                       (char *) l->data);
                g_module_close (module);
            }

            mail_importer_module_init ();
            importer_modules = g_list_prepend (importer_modules, module);
        }
    }

    free_importer_list (importers);
}

/**
 * mail_importer_uninit:
 *
 * Unloads all the modules.
 */
void
mail_importer_uninit (void)
{
    CORBA_Environment ev;
    GList *l;

    for (l = importer_modules; l; l = l->next) {
        g_module_close (l->data);
    }

    g_list_free (importer_modules);
    importer_modules = NULL;
    
    CORBA_exception_init (&ev);
    bonobo_object_release_unref (local_storage, &ev);
    local_storage = NULL;
    CORBA_exception_free (&ev);
}