aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/imap/camel-imap-command.c
blob: 932bccfde868cf39dc17d1b368ae772b03228fcf (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-imap-command.c: IMAP command sending/parsing routines */

/*
 *  Authors:
 *    Dan Winship <danw@helixcode.com>
 *    Jeffrey Stedfast <fejj@helixcode.com>
 *
 *  Copyright 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 Street #330, Boston, MA 02111-1307, USA.
 *
 */


#include <config.h>

#include <stdio.h>
#include <string.h>

#include "camel-imap-command.h"
#include "camel-imap-utils.h"
#include "camel-imap-folder.h"
#include <camel/camel-exception.h>

static char *imap_read_untagged (CamelImapStore *store, char *line,
                 CamelException *ex);
static CamelImapResponse *imap_read_response (CamelImapStore *store,
                          CamelException *ex);

/**
 * camel_imap_command: Send a command to a IMAP server and get a response
 * @store: the IMAP store
 * @folder: The folder to perform the operation in (or %NULL if not
 * relevant).
 * @ex: a CamelException
 * @fmt: a printf-style format string, followed by arguments
 *
 * This function makes sure that @folder (if non-%NULL) is the
 * currently-selected folder on @store and then sends the IMAP command
 * specified by @fmt and the following arguments. It then reads the
 * server's response(s) and parses the final result.
 *
 * As a special case, if @fmt is %NULL, it will just select @folder
 * and return the response from doing so.
 * 
 * Return value: %NULL if an error occurred (in which case @ex will
 * be set). Otherwise, a CamelImapResponse describing the server's
 * response, which the caller must free with camel_imap_response_free().
 **/
CamelImapResponse *
camel_imap_command (CamelImapStore *store, CamelFolder *folder,
            CamelException *ex, const char *fmt, ...)
{
    gchar *cmdbuf;
    va_list ap;

    /* Check for current folder */
    if (folder && (!fmt || folder != store->current_folder)) {
        char *folder_path;
        CamelImapResponse *response;

        folder_path = camel_imap_store_folder_path (store,
                                folder->full_name);
        store->current_folder = NULL;
        response = camel_imap_command (store, NULL, ex,
                           "SELECT \"%s\"", folder_path);
        g_free (folder_path);

        if (!response)
            return NULL;
        store->current_folder = folder;

        if (!fmt)
            return response;

        camel_imap_response_free (response);
    }

    /* Send the command */
    va_start (ap, fmt);
    cmdbuf = g_strdup_vprintf (fmt, ap);
    va_end (ap);

    camel_remote_store_send_string (CAMEL_REMOTE_STORE (store), ex,
                    "A%.5d %s\r\n", store->command++,
                    cmdbuf);
    g_free (cmdbuf);
    if (camel_exception_is_set (ex))
        return NULL;

    /* Read the response. */
    return imap_read_response (store, ex);
}

/**
 * camel_imap_command_continuation: Send more command data to the IMAP server
 * @store: the IMAP store
 * @ex: a CamelException
 * @cmdbuf: buffer containing the response/request data
 *
 * This method is for sending continuing responses to the IMAP server
 * after camel_imap_command returns a CAMEL_IMAP_PLUS response.
 * 
 * Return value: as for camel_imap_command()
 **/
CamelImapResponse *
camel_imap_command_continuation (CamelImapStore *store, CamelException *ex,
                 const char *cmdbuf)
{
    if (camel_remote_store_send_string (CAMEL_REMOTE_STORE (store), ex,
                        "%s\r\n", cmdbuf) < 0)
        return NULL;

    return imap_read_response (store, ex);
}

/* Read the response to an IMAP command. */
static CamelImapResponse *
imap_read_response (CamelImapStore *store, CamelException *ex)
{
    CamelImapResponse *response;
    int number, exists = 0;
    GArray *expunged = NULL;
    char *respbuf, *retcode, *word, *p;

    /* Read first line */
    if (camel_remote_store_recv_line (CAMEL_REMOTE_STORE (store),
                      &respbuf, ex) < 0)
        return NULL;

    response = g_new0 (CamelImapResponse, 1);
    response->untagged = g_ptr_array_new ();

    /* Check for untagged data */
    while (!strncmp (respbuf, "* ", 2)) {
        /* Read the rest of the response if it is multi-line. */
        respbuf = imap_read_untagged (store, respbuf, ex);
        if (camel_exception_is_set (ex))
            break;

        /* If it starts with a number, we might deal with
         * it ourselves.
         */
        word = imap_next_word (respbuf);
        number = strtoul (word, &p, 10);
        if (p != word && store->current_folder) {
            word = imap_next_word (p);
            if (!g_strcasecmp (word, "EXISTS")) {
                exists = number;
                g_free (respbuf);
                goto next;
            } else if (!g_strcasecmp (word, "EXPUNGE")) {
                if (!expunged) {
                    expunged = g_array_new (FALSE, FALSE,
                                sizeof (int));
                }
                g_array_append_val (expunged, number);
                g_free (respbuf);
                goto next;
            }
        } else {
            if (!g_strncasecmp (word, "BYE", 3)) {
                /* connection was lost, no more data to fetch */
                store->connected = FALSE;
                g_free (respbuf);
                respbuf = NULL;
                break;
            }
        }

        g_ptr_array_add (response->untagged, respbuf);
    next:
        if (camel_remote_store_recv_line (
            CAMEL_REMOTE_STORE (store), &respbuf, ex) < 0)
            break;
    }

    /* Update the summary */
    if (store->current_folder && (exists > 0 || expunged)) {
        camel_imap_folder_changed (store->current_folder, exists,
                       expunged, NULL);
    }
    if (expunged)
        g_array_free (expunged, TRUE);

    if (!respbuf || camel_exception_is_set (ex)) {
        camel_imap_response_free (response);
        return NULL;
    }

    response->status = respbuf;

    /* Check for OK or continuation response. */
    if (!strncmp (respbuf, "+ ", 2))
        return response;
    retcode = imap_next_word (respbuf);
    if (!strncmp (retcode, "OK", 2))
        return response;

    /* We should never get BAD, or anything else but +, OK, or NO
     * for that matter.
     */
    if (strncmp (retcode, "NO", 2) != 0) {
        g_warning ("Unexpected response from IMAP server: %s",
               respbuf);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                      "Unexpected response from IMAP server: "
                      "%s", respbuf);
        camel_imap_response_free (response);
        return NULL;
    }

    retcode = imap_next_word (retcode);
    camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                  "IMAP command failed: %s",
                  retcode ? retcode : "Unknown error");
    camel_imap_response_free (response);
    return NULL;
}

/* Given a line that is the start of an untagged response, read and
 * return the complete response. (This will be a no-op if the line
 * in question doesn't end with a literal.)
 *
 * FIXME: this won't deal with multiple literals in a single response.
 */
static char *
imap_read_untagged (CamelImapStore *store, char *line, CamelException *ex)
{
    int fulllen, length, left, i;
    GPtrArray *data;
    char *end, *p;

    p = strrchr (line, '{');
    if (!p)
        return line;

    length = strtoul (p + 1, &end, 10);
    if (*end != '}' || *(end + 1) || end == p + 1)
        return line;

    fulllen = length + strlen (line) + 1;

    /* OK. We have a literal. @length is the length including CRLF
     * pairs, which camel_remote_store_recv_line won't return.
     */
    data = g_ptr_array_new ();
    g_ptr_array_add (data, line);
    left = length;
    while (1) {
        if (camel_remote_store_recv_line (CAMEL_REMOTE_STORE (store),
                          &line, ex) < 0) {
            for (i = 0; i < data->len; i++)
                g_free (data->pdata[i]);
            g_ptr_array_free (data, TRUE);
            return NULL;
        }
        g_ptr_array_add (data, line);

        if (left <= 0)
            break;

        left -= strlen (line) + 2;

        /* The output string will have only LF, not CRLF, so
         * decrement the length by one.
         */
        length--;
    }

    /* Add the length of the post-literal line. */
    fulllen += strlen (line);

    /* p points to the "{" in the line that starts the literal.
     * The length of the CR-less response must be less than or
     * equal to the length of the response with CRs, therefore
     * overwriting the old value with the new value cannot cause
     * an overrun.
     */
    sprintf (p, "{%d}", length);

    /* Now reassemble the data. */
    p = line = g_malloc (fulllen + 1);
    for (i = 0; i < data->len; i++) {
        length = strlen (data->pdata[i]);
        memcpy (p, data->pdata[i], length);
        g_free (data->pdata[i]);
        p += length;
        *p++ = '\n';
    }
    *p = '\0';
    g_ptr_array_free (data, TRUE);
    return line;
}


/**
 * camel_imap_response_free:
 * response: a CamelImapResponse:
 *
 * Frees all of the data in @response.
 **/
void
camel_imap_response_free (CamelImapResponse *response)
{
    int i;

    if (!response)
        return;
    for (i = 0; i < response->untagged->len; i++)
        g_free (response->untagged->pdata[i]);
    g_ptr_array_free (response->untagged, TRUE);
    g_free (response->status);
    g_free (response);
}

/**
 * camel_imap_response_extract:
 * @response: the response data returned from camel_imap_command
 * @type: the response type to extract
 * @ex: a CamelException
 *
 * This checks that @response contains a single untagged response of
 * type @type and returns just that response data. If @response
 * doesn't contain the right information, the function will set @ex and
 * return %NULL. Either way, @response will be freed.
 *
 * Return value: the desired response string, which the caller must free.
 **/
char *
camel_imap_response_extract (CamelImapResponse *response, const char *type,
                 CamelException *ex)
{
    int len = strlen (type), i;
    char *resp;

    for (i = 0; i < response->untagged->len; i++) {
        resp = response->untagged->pdata[i];
        /* Skip "* ", and initial sequence number, if present */
        strtoul (resp + 2, &resp, 10);
        if (*resp == ' ')
            resp = imap_next_word (resp);

        if (!g_strncasecmp (resp, type, len))
            break;
        
        g_free (response->untagged->pdata[i]);
    }

    if (i < response->untagged->len) {
        resp = response->untagged->pdata[i];
        for (i++; i < response->untagged->len; i++)
            g_free (response->untagged->pdata[i]);
    } else {
        resp = NULL;
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                      "IMAP server response did not contain "
                      "%s information", type);
    }

    g_ptr_array_free (response->untagged, TRUE);
    g_free (response->status);
    g_free (response);
    return resp;
}

/**
 * camel_imap_response_extract_continuation:
 * @response: the response data returned from camel_imap_command
 * @ex: a CamelException
 *
 * This checks that @response contains a continuation response, and
 * returns just that data. If @response doesn't contain a continuation
 * response, the function will set @ex and return %NULL. Either way,
 * @response will be freed.
 *
 * Return value: the desired response string, which the caller must free.
 **/
char *
camel_imap_response_extract_continuation (CamelImapResponse *response,
                      CamelException *ex)
{
    char *status;

    if (response->status && !strncmp (response->status, "+ ", 2)) {
        status = response->status;
        response->status = NULL;
        camel_imap_response_free (response);
        return status;
    }

    camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                  "Unexpected OK response from IMAP server: %s",
                  response->status);
    camel_imap_response_free (response);
    return NULL;
}