aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/imap/camel-imap-wrapper.c
blob: 20ad0c730a2ced27e04530ce414d272af1b6d1d9 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; -*- */
/* camel-imap-wrapper.c: data wrapper for offline IMAP data */

/*
 * Author: Dan Winship <danw@helixcode.com>
 *
 * Copyright 2000 Helix Code, Inc. (http://www.helixcode.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 <errno.h>
#include <string.h>

#include "camel-imap-folder.h"
#include "camel-imap-wrapper.h"
#include "camel-imap-private.h"
#include "camel-exception.h"
#include "camel-stream-filter.h"
#include "camel-mime-filter-basic.h"
#include "camel-mime-filter-crlf.h"
#include "camel-mime-filter-charset.h"
#include "camel-mime-part.h"

static CamelDataWrapperClass *parent_class = NULL;

/* Returns the class for a CamelDataWrapper */
#define CDW_CLASS(so) CAMEL_DATA_WRAPPER_CLASS (CAMEL_OBJECT_GET_CLASS(so))

static int write_to_stream (CamelDataWrapper *imap_wrapper, CamelStream *stream);

static void
camel_imap_wrapper_class_init (CamelImapWrapperClass *camel_imap_wrapper_class)
{
    CamelDataWrapperClass *camel_data_wrapper_class =
        CAMEL_DATA_WRAPPER_CLASS (camel_imap_wrapper_class);

    parent_class = CAMEL_DATA_WRAPPER_CLASS (camel_type_get_global_classfuncs (camel_data_wrapper_get_type ()));

    /* virtual method override */
    camel_data_wrapper_class->write_to_stream = write_to_stream;
}

static void
camel_imap_wrapper_finalize (CamelObject *object)
{
    CamelImapWrapper *imap_wrapper = CAMEL_IMAP_WRAPPER (object);

    if (imap_wrapper->folder)
        camel_object_unref (CAMEL_OBJECT (imap_wrapper->folder));
    if (imap_wrapper->uid)
        g_free (imap_wrapper->uid);
    if (imap_wrapper->part)
        g_free (imap_wrapper->part_spec);

#ifdef ENABLE_THREADS
    g_mutex_free (imap_wrapper->priv->lock);
#endif
    g_free (imap_wrapper->priv);
}

static void
camel_imap_wrapper_init (gpointer object, gpointer klass)
{
    CamelImapWrapper *imap_wrapper = CAMEL_IMAP_WRAPPER (object);

    imap_wrapper->priv = g_new0 (struct _CamelImapWrapperPrivate, 1);
#ifdef ENABLE_THREADS
    imap_wrapper->priv->lock = g_mutex_new ();
#endif
}

CamelType
camel_imap_wrapper_get_type (void)
{
    static CamelType camel_imap_wrapper_type = CAMEL_INVALID_TYPE;

    if (camel_imap_wrapper_type == CAMEL_INVALID_TYPE) {
        camel_imap_wrapper_type = camel_type_register (
            CAMEL_DATA_WRAPPER_TYPE, "CamelImapWrapper",
            sizeof (CamelImapWrapper),
            sizeof (CamelImapWrapperClass),
            (CamelObjectClassInitFunc) camel_imap_wrapper_class_init,
            NULL,
            (CamelObjectInitFunc) camel_imap_wrapper_init,
            (CamelObjectFinalizeFunc) camel_imap_wrapper_finalize);
    }

    return camel_imap_wrapper_type;
}


static void
imap_wrapper_hydrate (CamelImapWrapper *imap_wrapper, CamelStream *stream)
{
    CamelDataWrapper *data_wrapper = CAMEL_DATA_WRAPPER (imap_wrapper);
    CamelStreamFilter *filterstream;
    CamelMimeFilter *filter;
    CamelContentType *ct;

    filterstream = camel_stream_filter_new_with_stream (stream);

    if (camel_mime_part_get_encoding (imap_wrapper->part) ==
        CAMEL_MIME_PART_ENCODING_BASE64) {
        filter = (CamelMimeFilter *)camel_mime_filter_basic_new_type (CAMEL_MIME_FILTER_BASIC_BASE64_DEC);
        camel_stream_filter_add (filterstream, filter);
    } else if (camel_mime_part_get_encoding (imap_wrapper->part) ==
           CAMEL_MIME_PART_ENCODING_QUOTEDPRINTABLE) {
        filter = (CamelMimeFilter *)camel_mime_filter_basic_new_type (CAMEL_MIME_FILTER_BASIC_QP_DEC);
        camel_stream_filter_add (filterstream, filter);
    } else
        filter = NULL;

    ct = camel_mime_part_get_content_type (imap_wrapper->part);
    if (header_content_type_is (ct, "text", "*")) {
        const char *charset;

        /* If we just did B64/QP, need to also do CRLF->LF */
        if (filter) {
            filter = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_DECODE,
                                 CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY);
            camel_stream_filter_add (filterstream, filter);
        }

        charset = header_content_type_param (ct, "charset");
        if (charset && !(strcasecmp (charset, "us-ascii") == 0
                 || strcasecmp (charset, "utf-8") == 0)) {
            filter = (CamelMimeFilter *)camel_mime_filter_charset_new_convert (charset, "UTF-8");
            if (filter)
                camel_stream_filter_add (filterstream, filter);
        }
    }

    data_wrapper->stream = CAMEL_STREAM (filterstream);
    data_wrapper->offline = FALSE;

    camel_object_unref (CAMEL_OBJECT (imap_wrapper->folder));
    imap_wrapper->folder = NULL;
    g_free (imap_wrapper->uid);
    imap_wrapper->uid = NULL;
    g_free (imap_wrapper->part_spec);
    imap_wrapper->part = NULL;
}


static int
write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream)
{
    CamelImapWrapper *imap_wrapper = CAMEL_IMAP_WRAPPER (data_wrapper);

    CAMEL_IMAP_WRAPPER_LOCK (imap_wrapper, lock);
    if (data_wrapper->offline) {
        CamelStream *datastream;

        datastream = camel_imap_folder_fetch_data (
            imap_wrapper->folder, imap_wrapper->uid,
            imap_wrapper->part_spec, FALSE, NULL);
        if (!datastream) {
            CAMEL_IMAP_WRAPPER_UNLOCK (imap_wrapper, lock);
            errno = ENETUNREACH;
            return -1;
        }

        imap_wrapper_hydrate (imap_wrapper, datastream);
        camel_object_unref (CAMEL_OBJECT (datastream));
    }
    CAMEL_IMAP_WRAPPER_UNLOCK (imap_wrapper, lock);

    return parent_class->write_to_stream (data_wrapper, stream);
}


CamelDataWrapper *
camel_imap_wrapper_new (CamelImapFolder *imap_folder, CamelContentType *type,
            const char *uid, const char *part_spec,
            CamelMimePart *part)
{
    CamelImapWrapper *imap_wrapper;
    CamelStream *stream;

    imap_wrapper = (CamelImapWrapper *)camel_object_new(camel_imap_wrapper_get_type());

    camel_data_wrapper_set_mime_type_field (CAMEL_DATA_WRAPPER (imap_wrapper), type);
    ((CamelDataWrapper *)imap_wrapper)->offline = TRUE;

    imap_wrapper->folder = imap_folder;
    camel_object_ref (CAMEL_OBJECT (imap_folder));
    imap_wrapper->uid = g_strdup (uid);
    imap_wrapper->part_spec = g_strdup (part_spec);

    /* Don't ref this, it's our parent. */
    imap_wrapper->part = part;

    /* Try the cache. */
    stream = camel_imap_folder_fetch_data (imap_folder, uid, part_spec,
                           TRUE, NULL);
    if (stream) {
        imap_wrapper_hydrate (imap_wrapper, stream);
        camel_object_unref (CAMEL_OBJECT (stream));
    }

    return (CamelDataWrapper *)imap_wrapper;
}