aboutsummaryrefslogtreecommitdiffstats
path: root/em-format/e-mail-parser-message-external.c
blob: 32ba0515dd5b08f5c1a1412925381b55e13877b0 (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
/*
 * e-mail-parser-message-external.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <ctype.h>
#include <glib/gi18n-lib.h>

#include <e-util/e-util.h>

#include "e-mail-parser-extension.h"

typedef EMailParserExtension EMailParserMessageExternal;
typedef EMailParserExtensionClass EMailParserMessageExternalClass;

GType e_mail_parser_message_external_get_type (void);

G_DEFINE_TYPE (
    EMailParserMessageExternal,
    e_mail_parser_message_external,
    E_TYPE_MAIL_PARSER_EXTENSION)

static const gchar *parser_mime_types[] = {
    "message/external-body",
    NULL
};

static gboolean
empe_msg_external_parse (EMailParserExtension *extension,
                         EMailParser *parser,
                         CamelMimePart *part,
                         GString *part_id,
                         GCancellable *cancellable,
                         GQueue *out_mail_parts)
{
    EMailPart *mail_part;
    CamelMimePart *newpart;
    CamelContentType *type;
    const gchar *access_type;
    gchar *url = NULL, *desc = NULL;
    gchar *content;
    gint len;
    gchar *mime_type;

    newpart = camel_mime_part_new ();

    /* needs to be cleaner */
    type = camel_mime_part_get_content_type (part);
    access_type = camel_content_type_param (type, "access-type");
    if (!access_type) {
        const gchar *msg = _("Malformed external-body part");
        mime_type = g_strdup ("text/plain");
        camel_mime_part_set_content (newpart, msg, strlen (msg), mime_type);
        goto addPart;
    }

    if (!g_ascii_strcasecmp (access_type, "ftp") ||
        !g_ascii_strcasecmp (access_type, "anon-ftp")) {
        const gchar *name, *site, *dir, *mode;
        gchar *path;
        gchar ftype[16];

        name = camel_content_type_param (type, "name");
        site = camel_content_type_param (type, "site");
        dir = camel_content_type_param (type, "directory");
        mode = camel_content_type_param (type, "mode");
        if (name == NULL || site == NULL)
            goto fail;

        /* Generate the path. */
        if (dir)
            path = g_strdup_printf ("/%s/%s", *dir == '/' ? dir + 1 : dir, name);
        else
            path = g_strdup_printf ("/%s", *name == '/' ? name + 1 : name);

        if (mode && *mode)
            sprintf (ftype, ";type=%c",  *mode);
        else
            ftype[0] = 0;

        url = g_strdup_printf ("ftp://%s%s%s", site, path, ftype);
        g_free (path);
        desc = g_strdup_printf (_("Pointer to FTP site (%s)"), url);
    } else if (!g_ascii_strcasecmp (access_type, "local-file")) {
        const gchar *name, *site;

        name = camel_content_type_param (type, "name");
        site = camel_content_type_param (type, "site");
        if (name == NULL)
            goto fail;

        url = g_filename_to_uri (name, NULL, NULL);
        if (site)
            desc = g_strdup_printf (_("Pointer to local file (%s) valid at site \"%s\""), name, site);
        else
            desc = g_strdup_printf (_("Pointer to local file (%s)"), name);
    } else if (!g_ascii_strcasecmp (access_type, "URL")) {
        const gchar *urlparam;
        gchar *s, *d;

        /* RFC 2017 */
        urlparam = camel_content_type_param (type, "url");
        if (urlparam == NULL)
            goto fail;

        /* For obscure MIMEy reasons, the URL may be split into words */
        url = g_strdup (urlparam);
        s = d = url;
        while (*s) {
            if (!isspace ((guchar) * s))
                *d++ = *s;
            s++;
        }
        *d = 0;
        desc = g_strdup_printf (_("Pointer to remote data (%s)"), url);
    } else {
        goto fail;
    }

    mime_type = g_strdup ("text/html");
    content = g_strdup_printf ("<a href=\"%s\">%s</a>", url, desc);
    camel_mime_part_set_content (newpart, content, strlen (content), mime_type);
    g_free (content);

    g_free (url);
    g_free (desc);

    goto addPart;

fail:
    content = g_strdup_printf (
        _("Pointer to unknown external data (\"%s\" type)"),
        access_type);
    mime_type = g_strdup ("text/plain");
    camel_mime_part_set_content (newpart, content, strlen (content), mime_type);
    g_free (content);

addPart:
    len = part_id->len;
    g_string_append (part_id, ".msg_external");
    mail_part = e_mail_part_new (part, part_id->str);
    mail_part->mime_type = mime_type;
    g_string_truncate (part_id, len);

    g_queue_push_tail (out_mail_parts, mail_part);

    return TRUE;
}

static void
e_mail_parser_message_external_class_init (EMailParserExtensionClass *class)
{
    class->mime_types = parser_mime_types;
    class->priority = G_PRIORITY_LOW;
    class->parse = empe_msg_external_parse;
}

static void
e_mail_parser_message_external_init (EMailParserExtension *extension)
{
}