~ubuntu-branches/ubuntu/wily/dovecot/wily

« back to all changes in this revision

Viewing changes to src/lib-imap-storage/imap-msgpart-url.c

  • Committer: Package Import Robot
  • Author(s): Jaldhar H. Vyas
  • Date: 2013-09-09 00:57:32 UTC
  • mfrom: (1.13.11)
  • mto: (4.8.5 experimental) (1.16.1)
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: package-import@ubuntu.com-20130909005732-dn1eell8srqbhh0e
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
 
2
 
 
3
#include "lib.h"
 
4
#include "net.h"
 
5
#include "istream.h"
 
6
#include "message-parser.h"
 
7
#include "mail-storage.h"
 
8
#include "mail-namespace.h"
 
9
#include "imap-url.h"
 
10
#include "imap-msgpart.h"
 
11
#include "imap-msgpart-url.h"
 
12
 
 
13
struct imap_msgpart_url {
 
14
        char *mailbox;
 
15
        uint32_t uidvalidity;
 
16
        uint32_t uid;
 
17
        char *section;
 
18
        uoff_t partial_offset, partial_size;
 
19
 
 
20
        struct imap_msgpart *part;
 
21
 
 
22
        struct mail_user *user;
 
23
        struct mailbox *selected_box;
 
24
        struct mailbox *box;
 
25
        struct mailbox_transaction_context *trans;
 
26
        struct mail *mail;
 
27
        
 
28
        struct imap_msgpart_open_result result;
 
29
 
 
30
        unsigned int decode_cte_to_binary:1;
 
31
};
 
32
 
 
33
int imap_msgpart_url_create(struct mail_user *user, const struct imap_url *url,
 
34
                            struct imap_msgpart_url **mpurl_r,
 
35
                            const char **error_r)
 
36
{
 
37
        const char *section = url->section == NULL ? "" : url->section;
 
38
        struct imap_msgpart_url *mpurl;
 
39
        struct imap_msgpart *msgpart;
 
40
 
 
41
        i_assert(url->mailbox != NULL && url->uid != 0 &&
 
42
                 url->search_program == NULL);
 
43
 
 
44
        if (imap_msgpart_parse(section, &msgpart) < 0) {
 
45
                *error_r = "Invalid section";
 
46
                return -1;
 
47
        }
 
48
 
 
49
        mpurl = i_new(struct imap_msgpart_url, 1);
 
50
        mpurl->part = msgpart;
 
51
        mpurl->user = user;
 
52
        mpurl->mailbox = i_strdup(url->mailbox);
 
53
        mpurl->uidvalidity = url->uidvalidity;
 
54
        mpurl->uid = url->uid;
 
55
        if (url->section != NULL)
 
56
                mpurl->section = i_strdup(url->section);
 
57
        mpurl->partial_offset = url->partial_offset;
 
58
        mpurl->partial_size = url->partial_size;
 
59
 
 
60
        imap_msgpart_set_partial(msgpart, url->partial_offset,
 
61
                                 url->partial_size == 0 ?
 
62
                                 (uoff_t)-1 : url->partial_size);
 
63
 
 
64
        *mpurl_r = mpurl;
 
65
        return 0;
 
66
}
 
67
 
 
68
int imap_msgpart_url_parse(struct mail_user *user, struct mailbox *selected_box,
 
69
                           const char *urlstr, struct imap_msgpart_url **mpurl_r,
 
70
                           const char **error_r)
 
71
{
 
72
        struct mailbox_status box_status;
 
73
        struct imap_url base_url, *url;
 
74
        const char  *error;
 
75
 
 
76
        /* build base url */
 
77
        memset(&base_url, 0, sizeof(base_url));
 
78
        if (selected_box != NULL) {
 
79
                mailbox_get_open_status(selected_box, STATUS_UIDVALIDITY,
 
80
                                        &box_status);
 
81
                base_url.mailbox = mailbox_get_vname(selected_box);
 
82
                base_url.uidvalidity = box_status.uidvalidity;
 
83
        }
 
84
 
 
85
        /* parse url */
 
86
        if (imap_url_parse(urlstr, &base_url,
 
87
                           IMAP_URL_PARSE_REQUIRE_RELATIVE, &url, &error) < 0) {
 
88
                *error_r = t_strconcat("Invalid IMAP URL: ", error, NULL);
 
89
                return 0;
 
90
        }
 
91
        if (url->mailbox == NULL) {
 
92
                *error_r = "Mailbox-relative IMAP URL, but no mailbox selected";
 
93
                return 0;
 
94
        }
 
95
        if (url->uid == 0 || url->search_program != NULL) {
 
96
                *error_r = "Invalid messagepart IMAP URL";
 
97
                return 0;
 
98
        }
 
99
        if (imap_msgpart_url_create(user, url, mpurl_r, error_r) < 0)
 
100
                return -1;
 
101
        (*mpurl_r)->selected_box = selected_box;
 
102
        return 1;
 
103
}
 
104
 
 
105
struct mailbox *imap_msgpart_url_get_mailbox(struct imap_msgpart_url *mpurl)
 
106
{
 
107
        return mpurl->box;
 
108
}
 
109
 
 
110
int imap_msgpart_url_open_mailbox(struct imap_msgpart_url *mpurl,
 
111
                                  struct mailbox **box_r, enum mail_error *error_code_r,
 
112
                                  const char **error_r)
 
113
{
 
114
        struct mailbox_status box_status;
 
115
        enum mailbox_flags flags = MAILBOX_FLAG_READONLY;
 
116
        struct mail_namespace *ns;
 
117
        struct mailbox *box;
 
118
 
 
119
        if (mpurl->box != NULL) {
 
120
                *box_r = mpurl->box;
 
121
                *error_code_r = MAIL_ERROR_NONE;
 
122
                return 1;
 
123
        }
 
124
 
 
125
        /* find mailbox namespace */
 
126
        ns = mail_namespace_find(mpurl->user->namespaces, mpurl->mailbox);
 
127
 
 
128
        /* open mailbox */
 
129
        if (mpurl->selected_box != NULL &&
 
130
            mailbox_equals(mpurl->selected_box, ns, mpurl->mailbox))
 
131
                box = mpurl->selected_box;
 
132
        else
 
133
                box = mailbox_alloc(ns->list, mpurl->mailbox, flags);
 
134
        if (mailbox_open(box) < 0) {
 
135
                *error_r = mail_storage_get_last_error(mailbox_get_storage(box),
 
136
                                                       error_code_r);
 
137
                if (box != mpurl->selected_box)
 
138
                        mailbox_free(&box);
 
139
                return *error_code_r == MAIL_ERROR_TEMP ? -1 : 0;
 
140
        }
 
141
 
 
142
        /* verify UIDVALIDITY */
 
143
        mailbox_get_open_status(box, STATUS_UIDVALIDITY, &box_status);
 
144
        if (mpurl->uidvalidity > 0 &&
 
145
            box_status.uidvalidity != mpurl->uidvalidity) {
 
146
                *error_r = "Invalid UIDVALIDITY";
 
147
                *error_code_r = MAIL_ERROR_EXPUNGED;
 
148
                if (box != mpurl->selected_box)
 
149
                        mailbox_free(&box);
 
150
                return 0;
 
151
        }
 
152
        mpurl->box = box;
 
153
        *box_r = box;
 
154
        return 1;
 
155
}
 
156
 
 
157
int imap_msgpart_url_open_mail(struct imap_msgpart_url *mpurl,
 
158
                               struct mail **mail_r, const char **error_r)
 
159
{
 
160
        struct mailbox_transaction_context *t;
 
161
        struct mailbox *box;
 
162
        enum mail_error error_code;
 
163
        struct mail *mail;
 
164
        int ret;
 
165
 
 
166
        if (mpurl->mail != NULL) {
 
167
                *mail_r = mpurl->mail;
 
168
                return 1;
 
169
        }
 
170
 
 
171
        /* open mailbox if it is not yet open */
 
172
        if ((ret = imap_msgpart_url_open_mailbox(mpurl, &box, &error_code,
 
173
                                                 error_r)) <= 0)
 
174
                return ret;
 
175
 
 
176
        /* start transaction */
 
177
        t = mailbox_transaction_begin(box, 0);
 
178
        mail = mail_alloc(t, MAIL_FETCH_MESSAGE_PARTS |
 
179
                          MAIL_FETCH_IMAP_BODYSTRUCTURE, NULL);
 
180
 
 
181
        /* find the message */
 
182
        if (!mail_set_uid(mail, mpurl->uid)) {
 
183
                *error_r = "Message not found";
 
184
                mail_free(&mail);
 
185
                mailbox_transaction_rollback(&t);       
 
186
                return 0;
 
187
        }
 
188
 
 
189
        mpurl->trans = t;
 
190
        mpurl->mail = mail;
 
191
        *mail_r = mail;
 
192
        return 1;
 
193
}
 
194
 
 
195
struct imap_msgpart *
 
196
imap_msgpart_url_get_part(struct imap_msgpart_url *mpurl)
 
197
{
 
198
        return mpurl->part;
 
199
}
 
200
 
 
201
void imap_msgpart_url_set_decode_to_binary(struct imap_msgpart_url *mpurl)
 
202
{
 
203
        imap_msgpart_set_decode_to_binary(mpurl->part);
 
204
}
 
205
 
 
206
int imap_msgpart_url_read_part(struct imap_msgpart_url *mpurl,
 
207
                               struct imap_msgpart_open_result *result_r,
 
208
                               const char **error_r)
 
209
{
 
210
        struct mail *mail;
 
211
        int ret;
 
212
 
 
213
        if (mpurl->result.input != NULL) {
 
214
                i_stream_seek(mpurl->result.input, 0);
 
215
                *result_r = mpurl->result;
 
216
                return 1;
 
217
        }
 
218
 
 
219
        /* open mail if it is not yet open */
 
220
        ret = imap_msgpart_url_open_mail(mpurl, &mail, error_r);
 
221
        if (ret <= 0)
 
222
                return ret;
 
223
 
 
224
        /* open the referenced part as a stream */
 
225
        ret = imap_msgpart_open(mail, mpurl->part, result_r);
 
226
        if (ret < 0) {
 
227
                *error_r = mailbox_get_last_error(mpurl->box, NULL);
 
228
                return ret;
 
229
        }
 
230
 
 
231
        mpurl->result = *result_r;
 
232
        return 1;
 
233
}
 
234
 
 
235
int imap_msgpart_url_verify(struct imap_msgpart_url *mpurl,
 
236
                            const char **error_r)
 
237
{
 
238
        struct mail *mail;
 
239
        int ret;
 
240
 
 
241
        if (mpurl->result.input != NULL)
 
242
                return 1;
 
243
 
 
244
        /* open mail if it is not yet open */
 
245
        ret = imap_msgpart_url_open_mail(mpurl, &mail, error_r);
 
246
        return ret;
 
247
}
 
248
 
 
249
int imap_msgpart_url_get_bodypartstructure(struct imap_msgpart_url *mpurl,
 
250
                                           const char **bpstruct_r,
 
251
                                           const char **error_r)
 
252
{
 
253
        struct mail *mail;
 
254
        int ret;
 
255
 
 
256
        /* open mail if it is not yet open */
 
257
        ret = imap_msgpart_url_open_mail(mpurl, &mail, error_r);
 
258
        if (ret <= 0)
 
259
                return ret;
 
260
 
 
261
        ret = imap_msgpart_bodypartstructure(mail, mpurl->part, bpstruct_r);
 
262
        if (ret < 0)
 
263
                *error_r = mailbox_get_last_error(mpurl->box, NULL);
 
264
        else if (ret == 0)
 
265
                *error_r = "Message part not found";
 
266
        return ret;
 
267
}
 
268
 
 
269
void imap_msgpart_url_free(struct imap_msgpart_url **_mpurl)
 
270
{
 
271
        struct imap_msgpart_url *mpurl = *_mpurl;
 
272
 
 
273
        *_mpurl = NULL;
 
274
 
 
275
        if (mpurl->result.input != NULL)
 
276
                i_stream_unref(&mpurl->result.input);
 
277
        if (mpurl->part != NULL)
 
278
                imap_msgpart_free(&mpurl->part);
 
279
        if (mpurl->mail != NULL)
 
280
                mail_free(&mpurl->mail);
 
281
        if (mpurl->trans != NULL)
 
282
                mailbox_transaction_rollback(&mpurl->trans);
 
283
        if (mpurl->box != NULL && mpurl->box != mpurl->selected_box)
 
284
                mailbox_free(&mpurl->box);      
 
285
        if (mpurl->section != NULL)
 
286
                i_free(mpurl->section);
 
287
        i_free(mpurl->mailbox);
 
288
        i_free(mpurl);
 
289
}