aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-tools.c
blob: 578ba960269f9ef3b72e34f4291de64793427dce (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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* mail-ops.c: callbacks for the mail toolbar/menus */

/* 
 * 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 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 <unistd.h>
#include <pthread.h>
#include <ctype.h>
#include <errno.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 "e-util/e-html-utils.h"

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

CamelFolder *
mail_tool_get_folder_from_urlname (const gchar *url, const gchar *name,
                   guint32 flags, CamelException *ex)
{
    CamelStore *store;
    CamelFolder *folder;

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

    folder = camel_store_get_folder (store, name, flags, ex);
    camel_object_unref (CAMEL_OBJECT (store));

    return folder;
}

char *
mail_tool_get_folder_name (CamelFolder *folder)
{
    const char *name = camel_folder_get_full_name (folder);
    char *path, *pend;
    
    /* This is a kludge. */
    if (strcmp (name, "mbox") && strcmp (name, "mh") && strcmp (name, "maildir"))
        return g_strdup (name);
    
    /* For mbox/mh, return the parent store's final path component. */
    path = g_strdup (CAMEL_SERVICE (folder->parent_store)->url->path);
    pend = path + strlen (path) - 1;
    if (*pend == '/')
        *pend = '\0';
    
    pend = path;
    path = g_strdup (g_basename (path));
    g_free (pend);
    
    return path;
}

gchar *
mail_tool_get_local_movemail_path (void)
{
    static gint count = 0;
    static pthread_mutex_t movemail_path_lock = PTHREAD_MUTEX_INITIALIZER;
    gint my_count;

    /* Ah, the joys of being multi-threaded... */
    pthread_mutex_lock (&movemail_path_lock);
    my_count = count;
    ++count;
    pthread_mutex_unlock (&movemail_path_lock);

    return g_strdup_printf ("%s/local/Inbox/movemail.%d", evolution_dir, my_count);
}

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, 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;
}

/* why is this function so stupidly complex when allthe work is done elsehwere? */
char *
mail_tool_do_movemail (const gchar *source_url, CamelException *ex)
{
    gchar *dest_path;
    const gchar *source;
    struct stat sb;
#ifndef MOVEMAIL_PATH
    int tmpfd;
#endif
    g_return_val_if_fail (strncmp (source_url, "mbox:", 5) == 0, NULL);

    /* Set up our destination. */

    dest_path = mail_tool_get_local_movemail_path();

    /* Create a new movemail mailbox file of 0 size */

#ifndef MOVEMAIL_PATH
    tmpfd = open (dest_path, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);

    if (tmpfd == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Couldn't create temporary "
                      "mbox `%s': %s"), dest_path, g_strerror (errno));
        g_free (dest_path);
        return NULL;
    }

    close (tmpfd);
#endif

    /* 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, *fromstr;
    const CamelInternetAddress *from;

    from = camel_mime_message_get_from(msg);
    subject = camel_mime_message_get_subject(msg);

    if (from) {
        fromstr = camel_address_format((CamelAddress *)from);
        if (subject && *subject) {
            fwd_subj = g_strdup_printf ("[%s] %s", fromstr, subject);
        } else {
            fwd_subj = g_strdup_printf (_("[%s] (forwarded message)"),
                            fromstr);
        }
        g_free(fromstr);
    } else {
        if (subject && *subject) {
            if (strncmp (subject, "Fwd: ", 5) == 0)
                subject += 4;
            fwd_subj = g_strdup_printf ("Fwd: %s", subject);
        } else
            fwd_subj = g_strdup (_("Fwd: (no subject)"));
    }

    return fwd_subj;
}

CamelMimePart *
mail_tool_make_message_attachment (CamelMimeMessage *message)
{
    CamelMimePart *part;
    const char *subject;
    gchar *desc;

    /*camel_object_ref (CAMEL_OBJECT (message));*/

    subject = camel_mime_message_get_subject (message);
    if (subject)
        desc = g_strdup_printf (_("Forwarded message - %s"), subject);
    else
        desc = g_strdup (_("Forwarded message (no subject)"));

    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);
    /*camel_object_unref (CAMEL_OBJECT (message));*/
    return part;
}

struct folder_cache_info {
    char *uri;
    CamelFolder *folder;
    int unread;
};

static void
update_unread_count_main (CamelObject *object, gpointer event_data,
              gpointer user_data)
{
    CamelFolder *folder = (CamelFolder *)object;
    struct folder_cache_info *fci = user_data;
    EvolutionStorage *storage;
    char *name;

    storage = mail_lookup_storage (folder->parent_store);
    if (fci->unread == 0)
        name = g_strdup (camel_folder_get_name (folder));
    else
        name = g_strdup_printf ("%s (%d)", camel_folder_get_name (folder), fci->unread);

    evolution_storage_update_folder_by_uri (storage, fci->uri, name,
                        fci->unread != 0);
    g_free (name);
    gtk_object_unref (GTK_OBJECT (storage));
    camel_object_unref (object);
}

static void
update_unread_count (CamelObject *object, gpointer event_data,
             gpointer user_data)
{
    CamelFolder *folder = (CamelFolder *)object;
    struct folder_cache_info *fci = user_data;
    int unread;

    unread = camel_folder_get_unread_message_count (folder);
    if (unread == fci->unread)
        return;
    fci->unread = unread;
    camel_object_ref (object);
    mail_proxy_event (update_unread_count_main, object, event_data, user_data);
}

static GHashTable *folders = NULL;
static GStaticMutex folders_lock = G_STATIC_MUTEX_INIT;

static void
uncache_folder (CamelObject *folder, gpointer event_data, gpointer user_data)
{
    struct folder_cache_info *fci = user_data;

    g_static_mutex_lock (&folders_lock);
    g_hash_table_remove (folders, fci->uri);
    g_static_mutex_unlock (&folders_lock);

    g_free (fci->uri);
    g_free (fci);
}

static void
cache_folder (CamelFolder *folder, const char *uri)
{
    CamelObject *object = CAMEL_OBJECT (folder);
    EvolutionStorage *storage;
    struct folder_cache_info *fci;

    fci = g_new (struct folder_cache_info, 1);
    fci->folder = folder;
    fci->uri = g_strdup (uri);
    fci->unread = 0;

    g_hash_table_insert (folders, fci->uri, fci);
    camel_object_hook_event (object, "finalize", uncache_folder, fci);

    storage = mail_lookup_storage (folder->parent_store);
    if (storage) {
        gtk_object_unref (GTK_OBJECT (storage));
        update_unread_count (object, NULL, fci);
        camel_object_hook_event (object, "message_changed",
                     update_unread_count, fci);
        camel_object_hook_event (object, "folder_changed",
                     update_unread_count, fci);
    }
}

CamelFolder *
mail_tool_uri_to_folder (const char *uri, CamelException *ex)
{
    CamelURL *url;
    CamelStore *store = NULL;
    CamelFolder *folder = NULL;
    int offset = 0;

    g_return_val_if_fail (uri != NULL, NULL);

    g_static_mutex_lock (&folders_lock);
    if (folders) {
        struct folder_cache_info *fci;

        fci = g_hash_table_lookup (folders, uri);
        g_static_mutex_unlock (&folders_lock);
        if (fci) {
            camel_object_ref (CAMEL_OBJECT (fci->folder));
            return fci->folder;
        }
    } else {
        folders = g_hash_table_new (g_str_hash, g_str_equal);
        g_static_mutex_unlock (&folders_lock);
    }

    if (!strncmp (uri, "vtrash:", 7))
        offset = 7;

    url = camel_url_new (uri + offset, ex);
    if (!url) {
        g_static_mutex_unlock (&folders_lock);
        return NULL;
    }

    if (!strcmp (url->protocol, "vfolder")) {
        folder = vfolder_uri_to_folder (uri, ex);
    } else {
        store = camel_session_get_store (session, uri + offset, ex);
        if (store) {
            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,
                                 CAMEL_STORE_FOLDER_CREATE, ex);
            camel_object_unref (CAMEL_OBJECT (store));
        }
    }

    if (camel_exception_is_set (ex)) {
        if (folder) {
            camel_object_unref (CAMEL_OBJECT (folder));
            folder = NULL;
        }
    }
    camel_url_free (url);

    g_static_mutex_lock (&folders_lock);
    if (!g_hash_table_lookup (folders, uri))
        cache_folder (folder, uri);
    g_static_mutex_unlock (&folders_lock);

    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, is_html;
    gchar *text;
    
    want_plain = !mail_config_get_send_html ();
    contents = camel_medium_get_content_object (CAMEL_MEDIUM (message));
    text = mail_get_message_body (contents, want_plain, &is_html);
    
    /* 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);
        }
        
        if (is_html) {
            ret_text = g_strdup_printf ("%s<!--+GtkHTML:<DATA class=\"ClueFlow\" key=\"orig\" value=\"1\">-->"
                            "<blockquote><i><font color=\"%06x\">\n%s\n"
                            "</font></i></blockquote>"
                            "<!--+GtkHTML:<DATA class=\"ClueFlow\" clear=\"orig\">-->",
                            credits ? credits : "",
                            mail_config_get_citation_color (), text);
        } else {
            gchar *s, *d, *quoted_text, *orig_text;
            gint lines, len;
            
            /* Count the number of lines in the body. If
             * the text ends with a \n, this will be one
             * too high, but that's ok. Allocate enough
             * space for the text and the "> "s.
             */
            for (s = text, lines = 0; s; s = strchr (s + 1, '\n'))
                lines++;
            
            /* offset is the size of the credits, strlen (text)
             * covers the body, lines * 2 does the "> "s, and
             * the last +2 covers the final "\0", plus an extra
             * "\n" in case text doesn't end with one.
             */
            quoted_text = g_malloc (strlen (text) + lines * 2 + 2);
            
            s = text;
            d = quoted_text;
            
            /* Copy text to quoted_text line by line,
             * prepending "> ".
             */
            while (1) {
                len = strcspn (s, "\n");
                if (len == 0 && !*s)
                    break;
                sprintf (d, "> %.*s\n", len, s);
                s += len;
                if (!*s++)
                    break;
                d += len + 3;
            }
            *d = '\0';
            
            /* Now convert that to HTML. */
            orig_text = e_text_to_html_full (quoted_text, E_TEXT_TO_HTML_PRE
                             | (mail_config_get_citation_highlight ()
                                ? E_TEXT_TO_HTML_MARK_CITATION : 0),
                             mail_config_get_citation_color ());
            g_free (quoted_text);
            ret_text = g_strdup_printf ("%s<!--+GtkHTML:<DATA class=\"ClueFlow\" key=\"orig\" value=\"1\">-->"
                            "%s"
                            "<!--+GtkHTML:<DATA class=\"ClueFlow\" clear=\"orig\">-->",
                            credits ? credits : "",
                            orig_text);
            g_free (orig_text);
        }
        
        g_free (text);
        printf ("ret: %s\n", ret_text);
        return ret_text;
    }
    
    return NULL;
}

/**
 * mail_tool_forward_message:
 * @message: mime message to quote
 *
 * Returns an allocated buffer containing the forwarded message.
 */
gchar *
mail_tool_forward_message (CamelMimeMessage *message)
{
    CamelDataWrapper *contents;
    gboolean want_plain, is_html;
    gchar *text;
    
    want_plain = !mail_config_get_send_html ();
    contents = camel_medium_get_content_object (CAMEL_MEDIUM (message));
    text = mail_get_message_body (contents, want_plain, &is_html);
    
    /* Set the quoted reply text. */
    if (text) {
        gchar *ret_text, *credits = NULL;
        const CamelInternetAddress *cia;
        char *buf, *from, *to, *subject;
        
        /* create credits */
        cia = camel_mime_message_get_from (message);
        buf = camel_address_format (CAMEL_ADDRESS (cia));
        if (buf) {
            from = e_text_to_html (buf, E_TEXT_TO_HTML_CONVERT_NL | E_TEXT_TO_HTML_CONVERT_URLS);
            g_free (buf);
        } else
            from = NULL;
        
        cia = camel_mime_message_get_recipients (message, CAMEL_RECIPIENT_TYPE_TO);
        buf = camel_address_format (CAMEL_ADDRESS (cia));
        if (buf) {
            to = e_text_to_html (buf, E_TEXT_TO_HTML_CONVERT_NL | E_TEXT_TO_HTML_CONVERT_URLS);
            g_free (buf);
        } else
            to = NULL;
        
        buf = (char *) camel_mime_message_get_subject (message);
        if (buf)
            subject = e_text_to_html (buf, E_TEXT_TO_HTML_CONVERT_NL | E_TEXT_TO_HTML_CONVERT_URLS);
        else
            subject = "";
        
        credits = g_strdup_printf (_("-----Forwarded Message-----<br>"
                         "<b>From:</b> %s<br>"
                         "<b>To:</b> %s<br>"
                         "<b>Subject:</b> %s<br>"),
                       from ? from : "", to ? to : "", subject);
        g_free (from);
        g_free (to);
        
        if (!is_html) {
            /* Now convert that to HTML. */
            ret_text = e_text_to_html (text, E_TEXT_TO_HTML_PRE);
            g_free (text);
            text = ret_text;
        }
        
        ret_text = g_strdup_printf ("%s<br>%s\n", credits, text);
        
        g_free (credits);
        g_free (text);
        
        return ret_text;
    }
    
    return NULL;
}