aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-tools.c
blob: d80b3c04fa70ffa2e9b8592222963b81fd945ed0 (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

            !strcmp (node->name, "object"))
            key = xmlGetProp (node, "uid");
        else
            key = node->name;

        value = xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
        if (!key || !value) {
            g_warning ("Found an entry with missing properties!!");
            continue;
        }

        g_hash_table_insert (hash, g_strdup (key), g_strdup (value));
    }

    return hash;
}


struct save_data {
    EXmlHashType type;
    xmlNode *root;
};

static void
foreach_save_func /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * Authors: 
 *  Dan Winship <danw@ximian.com>
 *  Peter Williams <peterw@ximian.com>
 *  Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright 2000 Ximian, Inc. (www.ximian.com)
 *
 * 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 <unistd.h>
#include <pthread.h>
#include <ctype.h>
#include <errno.h>
#include <gal/widgets/e-unicode.h>
#include "camel/camel.h"
#include "camel/camel-vee-folder.h"
#include "mail-vfolder.h"
#include "filter/vfolder-rule.h"
#include "filter/vfolder-context.h"
#include "filter/filter-option.h"
#include "filter/filter-input.h"
#include "mail.h" /*session*/
#include "mail-tools.h"
#include "mail-local.h"
#include "mail-mt.h"
#include "mail-folder-cache.h"
#include "e-util/e-html-utils.h"

/* **************************************** */

CamelFolder *
mail_tool_get_local_inbox (CamelException *ex)
{
    gchar *url;
    CamelFolder *folder;

    url = g_strdup_printf("file://%s/local/Inbox", evolution_dir);
    folder = mail_tool_uri_to_folder (url, 0, ex);
    g_free (url);
    return folder;
}

CamelFolder *
mail_tool_get_inbox (const gchar *url, CamelException *ex)
{
    CamelStore *store;
    CamelFolder *folder;

    store = camel_session_get_store (session, url, ex);
    if (!store)
        return NULL;

    folder = camel_store_get_inbox (store, ex);
    camel_object_unref (CAMEL_OBJECT (store));

    return folder;
}

CamelFolder *
mail_tool_get_trash (const gchar *url, int connect, CamelException *ex)
{
    CamelStore *store;
    CamelFolder *trash;

    if (connect)
        store = camel_session_get_store (session, url, ex);
    else
        store = (CamelStore *)camel_session_get_service(session, url, CAMEL_PROVIDER_STORE, ex);

    if (!store)
        return NULL;
    
    if (connect || ((CamelService *)store)->status == CAMEL_SERVICE_CONNECTED)
        trash = camel_store_get_trash (store, ex);
    else
        trash = NULL;

    camel_object_unref (CAMEL_OBJECT (store));
    
    return trash;
}

static char *
mail_tool_get_local_movemail_path (const unsigned char *uri)
{
    unsigned char *safe_uri, *c;
    char *path;
    
    safe_uri = g_strdup (uri);
    for (c = safe_uri; *c; c++)
        if (strchr ("/:;=|%&#!*^()\\, ", *c) || !isprint ((int) *c))
            *c = '_';
    
    path = g_strdup_printf ("%s/local/Inbox/movemail.%s", evolution_dir, safe_uri);
    g_free (safe_uri);
    
    return path;
}

/* why is this function so stupidly complex when allthe work is done elsehwere? */
char *
mail_tool_do_movemail (const char *source_url, CamelException *ex)
{
    char *dest_path;
    const char *source;
    struct stat sb;
    
    g_return_val_if_fail (strncmp (source_url, "mbox:", 5) == 0, NULL);
    
    /* Set up our destination. */
    dest_path = mail_tool_get_local_movemail_path (source_url);
    
    /* Skip over "mbox:" plus host part (if any) of url. */
    source = source_url + 5;
    if (!strncmp (source, "//", 2))
        source = strchr (source + 2, '/');
    
    /* Movemail from source (source_url) to dest_path */
    camel_movemail (source, dest_path, ex);
    
    if (stat (dest_path, &sb) < 0 || sb.st_size == 0) {
        unlink (dest_path); /* Clean up the movemail.foo file. */
        g_free (dest_path);
        return NULL;
    }
    
    if (camel_exception_is_set (ex)) {
        g_free (dest_path);
        return NULL;
    }
    
    return dest_path;
}

char *
mail_tool_generate_forward_subject (CamelMimeMessage *msg)
{
    const char *subject;
    char *fwd_subj;
    const int max_subject_length = 1024;
    
    subject = camel_mime_message_get_subject(msg);
    
    if (subject && *subject) {
        /* Truncate insanely long subjects */
        if (strlen (subject) < max_subject_length) {
            fwd_subj = g_strdup_printf ("[Fwd: %s]", subject);
        } else {
            /* We can't use %.*s because it depends on the locale being C/POSIX
               or UTF-8 to work correctly in glibc */
            /*fwd_subj = g_strdup_printf ("[Fwd: %.*s...]", max_subject_length, subject);*/
            fwd_subj = g_malloc (max_subject_length + 11);
            memcpy (fwd_subj, "[Fwd: ", 6);
            memcpy (fwd_subj + 6, subject, max_subject_length);
            memcpy (fwd_subj + 6 + max_subject_length, "...]", 5);
        }
    } else {
        const CamelInternetAddress *from;
        char *fromstr;
        
        from = camel_mime_message_get_from (msg);
        if (from) {
            fromstr = camel_address_format (CAMEL_ADDRESS (from));
            fwd_subj = g_strdup_printf ("[Fwd: %s]", fromstr);
            g_free (fromstr);
        } else
            fwd_subj = g_strdup ("[Fwd: No Subject]");
    }
    
    return fwd_subj;
}

XEvolution *
mail_tool_remove_xevolution_headers (CamelMimeMessage *message)
{
    XEvolution *xev;
    
    xev = g_new (XEvolution, 1);
    xev->flags = g_strdup (camel_medium_get_header (CAMEL_MEDIUM (message), "X-Evolution"));
    xev->source = g_strdup (camel_medium_get_header (CAMEL_MEDIUM (message), "X-Evolution-Source"));
    xev->transport = g_strdup (camel_medium_get_header (CAMEL_MEDIUM (message), "X-Evolution-Transport"));
    xev->account = g_strdup (camel_medium_get_header (CAMEL_MEDIUM (message), "X-Evolution-Account"));
    xev->fcc = g_strdup (camel_medium_get_header (CAMEL_MEDIUM (message), "X-Evolution-Fcc"));
    xev->format = g_strdup (camel_medium_get_header (CAMEL_MEDIUM (message), "X-Evolution-Format"));
    
    /* rip off the X-Evolution* headers */
    camel_medium_remove_header (CAMEL_MEDIUM (message), "X-Evolution");
    camel_medium_remove_header (CAMEL_MEDIUM (message), "X-Evolution-Source");
    camel_medium_remove_header (CAMEL_MEDIUM (message), "X-Evolution-Transport");
    camel_medium_remove_header (CAMEL_MEDIUM (message), "X-Evolution-Account");
    camel_medium_remove_header (CAMEL_MEDIUM (message), "X-Evolution-Fcc");
    camel_medium_remove_header (CAMEL_MEDIUM (message), "X-Evolution-Format");
    
    return xev;
}

void
mail_tool_restore_xevolution_headers (CamelMimeMessage *message, XEvolution *xev)
{
    if (xev->flags)
        camel_medium_set_header (CAMEL_MEDIUM (message), "X-Evolution", xev->flags);
    if (xev->source)
        camel_medium_set_header (CAMEL_MEDIUM (message), "X-Evolution-Source", xev->source);
    if (xev->transport)
        camel_medium_set_header (CAMEL_MEDIUM (message), "X-Evolution-Transport", xev->transport);
    if (xev->account)
        camel_medium_set_header (CAMEL_MEDIUM (message), "X-Evolution-Account", xev->account);
    if (xev->fcc)
        camel_medium_set_header (CAMEL_MEDIUM (message), "X-Evolution-Fcc", xev->fcc);
    if (xev->format)
        camel_medium_set_header (CAMEL_MEDIUM (message), "X-Evolution-Format", xev->format);
}

void
mail_tool_destroy_xevolution (XEvolution *xev)
{
    g_free (xev->flags);
    g_free (xev->source);
    g_free (xev->transport);
    g_free (xev->account);
    g_free (xev->fcc);
    g_free (xev);
}

CamelMimePart *
mail_tool_make_message_attachment (CamelMimeMessage *message)
{
    CamelMimePart *part;
    const char *subject;
    XEvolution *xev;
    char *desc;
    
    subject = camel_mime_message_get_subject (message);
    if (subject) {
        char *fmt;
        
        fmt = e_utf8_from_locale_string (_("Forwarded message - %s"));
        desc = g_strdup_printf (fmt, subject);
        g_free (fmt);
    } else {
        desc = e_utf8_from_locale_string (_("Forwarded message"));
    }
    
    /* rip off the X-Evolution headers */
    xev = mail_tool_remove_xevolution_headers (message);
    mail_tool_destroy_xevolution (xev);
    
    part = camel_mime_part_new ();
    camel_mime_part_set_disposition (part, "inline");
    camel_mime_part_set_description (part, desc);
    camel_medium_set_content_object (CAMEL_MEDIUM (part),
                     CAMEL_DATA_WRAPPER (message));
    camel_mime_part_set_content_type (part, "message/rfc822");
    g_free (desc);
    
    return part;
}

CamelFolder *
mail_tool_uri_to_folder (const char *uri, guint32 flags, CamelException *ex)
{
    CamelURL *url;
    CamelStore *store = NULL;
    CamelFolder *folder = NULL;
    int offset = 0;
    
    g_return_val_if_fail (uri != NULL, NULL);
    
    /* This hack is still needed for file:/ since it's its own EvolutionStorage type */
    if (!strncmp (uri, "vtrash:", 7))
        offset = 7;
    
    url = camel_url_new (uri + offset, ex);
    if (!url) {
        return NULL;
    }

    store = camel_session_get_store (session, uri + offset, ex);
    if (store) {
        const char *name;
        
        /* if we have a fragment, then the path is actually used by the store,
           so the fragment is the path to the folder instead */
        if (url->fragment) {
            name = url->fragment;
        } else {
            if (url->path && *url->path)
                name = url->path + 1;
            else
                name = "";
        }
        
        if (offset)
            folder = camel_store_get_trash (store, ex);
        else
            folder = camel_store_get_folder (store, name, flags, ex);
        camel_object_unref (CAMEL_OBJECT (store));
    }
    
    if (folder)
        mail_note_folder (folder);
    
    camel_url_free (url);
    
    return folder;
}

/**
 * mail_tool_quote_message:
 * @message: mime message to quote
 * @fmt: credits format - example: "On %s, %s wrote:\n"
 * @Varargs: arguments
 *
 * Returns an allocated buffer containing the quoted message.
 */
gchar *
mail_tool_quote_message (CamelMimeMessage *message, const char *fmt, ...)
{
    CamelDataWrapper *contents;
    gboolean want_plain;
    gchar *text;
    
    want_plain = !mail_config_get_send_html ();
    contents = camel_medium_get_content_object (CAMEL_MEDIUM (message));
    /* We pass "want_plain" for "cite", since if it's HTML, we'll
     * do the citing ourself below.
     */
    text = mail_get_message_body (contents, want_plain, want_plain);
    
    /* Set the quoted reply text. */
    if (text) {
        gchar *ret_text, *credits = NULL;
        
        /* create credits */
        if (fmt) {
            va_list ap;
            
            va_start (ap, fmt);
            credits = g_strdup_vprintf (fmt, ap);
            va_end (ap);
        }
        
        ret_text = g_strdup_printf ("%s<!--+GtkHTML:<DATA class=\"ClueFlow\" key=\"orig\" value=\"1\">-->"
                        "<font color=\"%06x\">\n%s%s%s</font>"
                        "<!--+GtkHTML:<DATA class=\"ClueFlow\" clear=\"orig\">-->",
                        credits ? credits : "",
                        mail_config_get_citation_color (),
                        want_plain ? "" : "<blockquote><i>",
                        text,
                        want_plain ? "" : "</i></blockquote>");
        g_free (text);
        g_free (credits);
        return ret_text;
    }
    
    return NULL;
}


/**
 * mail_tool_forward_message:
 * @message: mime message to forward
 * @quoted: whether to forwarded it quoted (%TRUE) or inline (%FALSE)
 *
 * Returns an allocated buffer containing the forwarded message.
 */
gchar *
mail_tool_forward_message (CamelMimeMessage *message, gboolean quoted)
{
    gchar *title, *body, *ret;
    
    body = mail_get_message_body (CAMEL_DATA_WRAPPER (message),
                      !mail_config_get_send_html (),
                      quoted);
    title = e_utf8_from_locale_string (_("Forwarded Message"));
    ret = g_strdup_printf ("-----%s-----<br>%s", title, body ? body : "");
    g_free (title);
    g_free (body);
    return ret;
}


/**
 * mail_tools_x_evolution_message_parse:
 * @in: GtkSelectionData->data
 * @inlen: GtkSelectionData->length
 * @uids: pointer to a gptrarray that will be filled with uids on success
 *
 * Parses the GtkSelectionData and returns a CamelFolder and a list of
 * UIDs specified by the selection.
 **/
CamelFolder *
mail_tools_x_evolution_message_parse (char *in, unsigned int inlen, GPtrArray **uids)
{
    /* format: "uri\0uid1\0uid2\0uid3\0...\0uidn" */
    char *inptr, *inend;
    CamelFolder *folder;
    
    if (in == NULL)
        return NULL;
    
    folder = mail_tool_uri_to_folder (in, 0, NULL);
    
    if (!folder)
        return NULL;
    
    /* split the uids */
    inend = in + inlen;
    inptr = in + strlen (in) + 1;
    *uids = g_ptr_array_new ();
    while (inptr < inend) {
        char *start = inptr;
        
        while (inptr < inend && *inptr)
            inptr++;
        
        g_ptr_array_add (*uids, g_strndup (start, inptr - start));
        inptr++;
    }
    
    return folder;
}