aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/pop3/camel-pop3-folder.c
blob: 7b66c87f269eaaf4bd615c38515a194cd22f3192 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-pop3-folder.c : class for a pop3 folder */

/* 
 * Authors:
 *   Dan Winship <danw@helixcode.com>
 *
 * Copyright (C) 2000 Helix Code, Inc. (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
 */

#include "camel-pop3-folder.h"
#include "camel-pop3-store.h"
#include "camel-exception.h"
#include "camel-stream-mem.h"
#include "camel-stream-filter.h"
#include "camel-mime-message.h"

#include <stdlib.h>
#include <string.h>

#define CF_CLASS(o) (CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(o)))
static CamelFolderClass *parent_class;

static void pop3_finalize (CamelObject *object);

static void pop3_refresh_info (CamelFolder *folder, CamelException *ex);
static void pop3_sync (CamelFolder *folder, gboolean expunge,
               CamelException *ex);

static gint pop3_get_message_count (CamelFolder *folder);
static GPtrArray *pop3_get_uids (CamelFolder *folder);
static CamelMimeMessage *pop3_get_message (CamelFolder *folder, 
                       const char *uid,
                       CamelException *ex);
static void pop3_set_message_flags (CamelFolder *folder, const char *uid,
                    guint32 flags, guint32 set);

static GPtrArray *parse_listing (int count, char *data);

static void
camel_pop3_folder_class_init (CamelPop3FolderClass *camel_pop3_folder_class)
{
    CamelFolderClass *camel_folder_class =
        CAMEL_FOLDER_CLASS (camel_pop3_folder_class);

    parent_class = CAMEL_FOLDER_CLASS(camel_type_get_global_classfuncs (camel_folder_get_type ()));

    /* virtual method overload */
    camel_folder_class->refresh_info = pop3_refresh_info;
    camel_folder_class->sync = pop3_sync;

    camel_folder_class->get_message_count = pop3_get_message_count;
    camel_folder_class->get_uids = pop3_get_uids;
    camel_folder_class->free_uids = camel_folder_free_nop;

    camel_folder_class->get_message = pop3_get_message;
    camel_folder_class->set_message_flags = pop3_set_message_flags;
}

static void
camel_pop3_folder_init (gpointer object)
{
    CamelFolder *folder = CAMEL_FOLDER (object);
    CamelPop3Folder *pop3_folder = CAMEL_POP3_FOLDER (object);

    folder->has_summary_capability = FALSE;
    folder->has_search_capability = FALSE;

    pop3_folder->uids = NULL;
    pop3_folder->flags = NULL;
}

CamelType
camel_pop3_folder_get_type (void)
{
    static CamelType camel_pop3_folder_type = CAMEL_INVALID_TYPE;

    if (!camel_pop3_folder_type) {
        camel_pop3_folder_type = camel_type_register (CAMEL_FOLDER_TYPE, "CamelPop3Folder",
                                  sizeof (CamelPop3Folder),
                                  sizeof (CamelPop3FolderClass),
                                  (CamelObjectClassInitFunc) camel_pop3_folder_class_init,
                                  NULL,
                                  (CamelObjectInitFunc) camel_pop3_folder_init,
                                  (CamelObjectFinalizeFunc) pop3_finalize);
    }

    return camel_pop3_folder_type;
}

void
pop3_finalize (CamelObject *object)
{
    CamelPop3Folder *pop3_folder = CAMEL_POP3_FOLDER (object);

    camel_folder_free_deep (NULL, pop3_folder->uids);
    g_free (pop3_folder->flags);
}

CamelFolder *
camel_pop3_folder_new (CamelStore *parent, CamelException *ex)
{
    CamelFolder *folder;

    folder = CAMEL_FOLDER (camel_object_new (CAMEL_POP3_FOLDER_TYPE));
    camel_folder_construct (folder, parent, "inbox", "inbox");

    camel_folder_refresh_info (folder, ex);
    if (camel_exception_is_set (ex)) {
        camel_object_unref (CAMEL_OBJECT (folder));
        folder = NULL;
    }

    return folder;
}

static void 
pop3_refresh_info (CamelFolder *folder, CamelException *ex)
{
    GPtrArray *uids;
    int status, count;
    char *data;
    CamelPop3Folder *pop3_folder = (CamelPop3Folder *) folder;
    CamelPop3Store *pop3_store = CAMEL_POP3_STORE (folder->parent_store);

    status = camel_pop3_command (pop3_store, &data, ex, "STAT");
    if (status != CAMEL_POP3_OK)
        return;

    count = atoi (data);
    g_free (data);

    if (pop3_store->supports_uidl != FALSE) {
        status = camel_pop3_command (pop3_store, NULL, ex, "UIDL");
        switch (status) {
        case CAMEL_POP3_ERR:
            pop3_store->supports_uidl = FALSE;
            break;
        case CAMEL_POP3_FAIL:
            return;
        }
    }

    if (pop3_store->supports_uidl == FALSE) {
        int i;

        uids = g_ptr_array_new ();
        g_ptr_array_set_size (uids, count);

        for (i = 0; i < count; i++)
            uids->pdata[i] = g_strdup_printf ("%d", i + 1);
    } else {
        data = camel_pop3_command_get_additional_data (pop3_store, ex);
        if (camel_exception_is_set (ex))
            return;

        uids = parse_listing (count, data);
        g_free (data);

        if (!uids) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          "Could not open folder: message "
                          "listing was incomplete.");
            return;
        }
    }

    pop3_folder->uids = uids;
    pop3_folder->flags = g_new0 (guint32, uids->len);
}

static void
pop3_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
{
    CamelPop3Folder *pop3_folder;
    CamelPop3Store *pop3_store;
    int i, status;
    char *resp;

    if (!expunge)
        return;

    pop3_folder = CAMEL_POP3_FOLDER (folder);
    pop3_store = CAMEL_POP3_STORE (folder->parent_store);

    for (i = 0; i < pop3_folder->uids->len; i++) {
        if (pop3_folder->flags[i] & CAMEL_MESSAGE_DELETED) {
            status = camel_pop3_command (pop3_store, &resp, ex,
                             "DELE %d", i + 1);
            if (status != CAMEL_POP3_OK)
                return;
        }
    }

    camel_pop3_store_expunge (pop3_store, ex);
}


static GPtrArray *
parse_listing (int count, char *data)
{
    GPtrArray *ans;
    char *p;
    int index, len;

    ans = g_ptr_array_new ();
    g_ptr_array_set_size (ans, count);

    p = data;
    while (*p) {
        index = strtoul (p, &p, 10);
        len = strcspn (p, "\n");
        if (index <= count && *p == ' ')
            ans->pdata[index - 1] = g_strndup (p + 1, len - 1);
        p += len;
        if (*p == '\n')
            p++;
    }

    for (index = 0; index < count; index++) {
        if (ans->pdata[index] == NULL) {
            g_ptr_array_free (ans, TRUE);
            return NULL;
        }
    }

    return ans;
}

static int
uid_to_number (CamelPop3Folder *pop3_folder, const char *uid)
{
    int i;

    for (i = 0; i < pop3_folder->uids->len; i++) {
        if (!strcmp (uid, pop3_folder->uids->pdata[i]))
            return i + 1;
    }

    return -1;
}


static CamelMimeMessage *
pop3_get_message (CamelFolder *folder, const char *uid, CamelException *ex)
{
    int status, num;
    char *result, *body;
    CamelStream *msgstream;
    CamelMimeMessage *msg;

    num = uid_to_number (CAMEL_POP3_FOLDER (folder), uid);
    if (num == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
                      "No message with uid %s", uid);
        return NULL;
    }

    status = camel_pop3_command (CAMEL_POP3_STORE (folder->parent_store),
                     &result, ex, "RETR %d", num);
    if (status != CAMEL_POP3_OK)
        return NULL;
    g_free (result);

    body = camel_pop3_command_get_additional_data (CAMEL_POP3_STORE (folder->parent_store), ex);
    if (!body) {
        CamelService *service = CAMEL_SERVICE (folder->parent_store);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                      "Could not retrieve message from POP "
                      "server %s: %s", service->url->host,
                      camel_exception_get_description (ex));
        return NULL;
    }

    msgstream = camel_stream_mem_new_with_buffer (body, strlen (body));
    g_free (body);
    
    msg = camel_mime_message_new ();
    camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER (msg),
                          CAMEL_STREAM (msgstream));

    camel_object_unref (CAMEL_OBJECT (msgstream));

    return msg;
}

static void
pop3_set_message_flags (CamelFolder *folder, const char *uid,
            guint32 flags, guint32 set)
{
    CamelPop3Folder *pop3_folder = CAMEL_POP3_FOLDER (folder);
    int num;

    num = uid_to_number (pop3_folder, uid);
    if (num == -1)
        return;

    pop3_folder->flags[num - 1] =
        (pop3_folder->flags[num] & ~flags) | (set & flags);
}

static gint
pop3_get_message_count (CamelFolder *folder)
{
    CamelPop3Folder *pop3_folder = CAMEL_POP3_FOLDER (folder);

    return pop3_folder->uids->len;
}

static GPtrArray *
pop3_get_uids (CamelFolder *folder)
{
    CamelPop3Folder *pop3_folder = CAMEL_POP3_FOLDER (folder);

    return pop3_folder->uids;
}