aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/imap4/camel-imap4-utils.c
blob: 55c6431dc7b958e6bfe81d1e910cfc13b2c10d36 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*  Camel
 *  Copyright (C) 1999-2004 Jeffrey Stedfast
 *
 *  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.
 */


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

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

#include <camel/camel-store.h>
#include <camel/camel-i18n.h>

#include "camel-imap4-engine.h"
#include "camel-imap4-stream.h"
#include "camel-imap4-command.h"

#include "camel-imap4-utils.h"

#define d(x) x


void
camel_imap4_flags_diff (flags_diff_t *diff, guint32 old, guint32 new)
{
    diff->changed = old ^ new;
    diff->bits = new & diff->changed;
}


guint32
camel_imap4_flags_merge (flags_diff_t *diff, guint32 flags)
{
    return (flags & ~diff->changed) | diff->bits;
}


/**
 * camel_imap4_merge_flags:
 * @original: original server flags
 * @local: local flags (after changes)
 * @server: new server flags (another client updated the server flags)
 *
 * Merge the local flag changes into the new server flags.
 *
 * Returns the merged flags.
 **/
guint32
camel_imap4_merge_flags (guint32 original, guint32 local, guint32 server)
{
    flags_diff_t diff;
    
    camel_imap4_flags_diff (&diff, original, local);
    
    return camel_imap4_flags_merge (&diff, server);
}


struct _uidset_range {
    struct _uidset_range *next;
    guint32 first, last;
    uint8_t buflen;
    char buf[24];
};

struct _uidset {
    CamelFolderSummary *summary;
    struct _uidset_range *ranges;
    struct _uidset_range *tail;
    size_t maxlen, setlen;
};

static void
uidset_range_free (struct _uidset_range *range)
{
    struct _uidset_range *next;
    
    while (range != NULL) {
        next = range->next;
        g_free (range);
        range = next;
    }
}

static void
uidset_init (struct _uidset *uidset, CamelFolderSummary *summary, size_t maxlen)
{
    uidset->ranges = g_new (struct _uidset_range, 1);
    uidset->ranges->first = (guint32) -1;
    uidset->ranges->last = (guint32) -1;
    uidset->ranges->next = NULL;
    uidset->ranges->buflen = 0;
    
    uidset->tail = uidset->ranges;
    uidset->summary = summary;
    uidset->maxlen = maxlen;
    uidset->setlen = 0;
}

/* returns: -1 on full-and-not-added, 0 on added-and-not-full or 1 on added-and-full */
static int
uidset_add (struct _uidset *uidset, CamelMessageInfo *info)
{
    GPtrArray *messages = uidset->summary->messages;
    struct _uidset_range *node, *tail = uidset->tail;
    const char *iuid = camel_message_info_uid (info);
    size_t uidlen, len;
    const char *colon;
    guint32 index;
    
    /* Note: depends on integer overflow for initial 'add' */
    for (index = tail->last + 1; index < messages->len; index++) {
        if (info == messages->pdata[index])
            break;
    }
    
    g_assert (index < messages->len);
    
    uidlen = strlen (iuid);
    
    if (tail->buflen == 0) {
        /* first add */
        tail->first = tail->last = index;
        strcpy (tail->buf, iuid);
        uidset->setlen = uidlen;
        tail->buflen = uidlen;
    } else if (index == (tail->last + 1)) {
        /* add to last range */
        if (tail->last == tail->first) {
            /* make sure we've got enough room to add this one... */
            if ((uidset->setlen + uidlen + 1) > uidset->maxlen)
                return -1;
            
            tail->buf[tail->buflen++] = ':';
            uidset->setlen++;
        } else {
            colon = strchr (tail->buf, ':') + 1;
            
            len = strlen (colon);
            uidset->setlen -= len;
            tail->buflen -= len;
        }
        
        strcpy (tail->buf + tail->buflen, iuid);
        uidset->setlen += uidlen;
        tail->buflen += uidlen;
        
        tail->last = index;
    } else if ((uidset->setlen + uidlen + 1) < uidset->maxlen) {
        /* the beginning of a new range */
        tail->next = node = g_new (struct _uidset_range, 1);
        node->first = node->last = index;
        strcpy (node->buf, iuid);
        uidset->setlen += uidlen + 1;
        node->buflen = uidlen;
        uidset->tail = node;
        node->next = NULL;
    } else {
        /* can't add this one... */
        return -1;
    }
    
    fprintf (stderr, "added uid %s to uidset (summary index = %u)\n", iuid, index);
    
    if (uidset->setlen < uidset->maxlen)
        return 0;
    
    return 1;
}

static char *
uidset_to_string (struct _uidset *uidset)
{
    struct _uidset_range *range;
    GString *string;
    char *str;
    
    string = g_string_new ("");
    
    range = uidset->ranges;
    while (range != NULL) {
        g_string_append (string, range->buf);
        range = range->next;
        if (range)
            g_string_append_c (string, ',');
    }
    
    str = string->str;
    g_string_free (string, FALSE);
    
    return str;
}

int
camel_imap4_get_uid_set (CamelIMAP4Engine *engine, CamelFolderSummary *summary, GPtrArray *infos, int cur, size_t linelen, char **set)
{
    struct _uidset uidset;
    size_t maxlen;
    int rv = 0;
    int i;
    
    if (engine->maxlentype == CAMEL_IMAP4_ENGINE_MAXLEN_LINE)
        maxlen = engine->maxlen - linelen;
    else
        maxlen = engine->maxlen;
    
    uidset_init (&uidset, summary, maxlen);
    
    for (i = cur; i < infos->len && rv != 1; i++) {
        if ((rv = uidset_add (&uidset, infos->pdata[i])) == -1)
            break;
    }
    
    if (i > cur)
        *set = uidset_to_string (&uidset);
    
    uidset_range_free (uidset.ranges);
    
    return (i - cur);
}


void
camel_imap4_utils_set_unexpected_token_error (CamelException *ex, CamelIMAP4Engine *engine, camel_imap4_token_t *token)
{
    GString *errmsg;
    
    if (ex == NULL)
        return;
    
    errmsg = g_string_new ("");
    g_string_append_printf (errmsg, _("Unexpected token in response from IMAP server %s: "),
                engine->url->host);
    
    switch (token->token) {
    case CAMEL_IMAP4_TOKEN_NIL:
        g_string_append (errmsg, "NIL");
        break;
    case CAMEL_IMAP4_TOKEN_ATOM:
        g_string_append (errmsg, token->v.atom);
        break;
    case CAMEL_IMAP4_TOKEN_FLAG:
        g_string_append (errmsg, token->v.flag);
        break;
    case CAMEL_IMAP4_TOKEN_QSTRING:
        g_string_append (errmsg, token->v.qstring);
        break;
    case CAMEL_IMAP4_TOKEN_LITERAL:
        g_string_append_printf (errmsg, "{%u}", token->v.literal);
        break;
    case CAMEL_IMAP4_TOKEN_NUMBER:
        g_string_append_printf (errmsg, "%u", token->v.number);
        break;
    case CAMEL_IMAP4_TOKEN_NO_DATA:
        g_string_append (errmsg, _("No data"));
        break;
    default:
        g_string_append_c (errmsg, (unsigned char) (token->token & 0xff));
        break;
    }
    
    camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, errmsg->str);
    
    g_string_free (errmsg, TRUE);
}


static struct {
    const char *name;
    guint32 flag;
} imap4_flags[] = {
    { "\\Answered", CAMEL_MESSAGE_ANSWERED    },
    { "\\Deleted",  CAMEL_MESSAGE_DELETED     },
    { "\\Draft",    CAMEL_MESSAGE_DRAFT       },
    { "\\Flagged",  CAMEL_MESSAGE_FLAGGED     },
    { "\\Seen",     CAMEL_MESSAGE_SEEN        },
    /*{ "\\Recent",   CAMEL_MESSAGE_RECENT      },*/
    { "\\*",        CAMEL_MESSAGE_USER        },
};

#if 0
static struct {
    const char *name;
    guint32 flag;
} imap4_user_flags[] = {
    { "Forwarded",  CAMEL_MESSAGE_FORWARDED   },
};
#endif


int
camel_imap4_parse_flags_list (CamelIMAP4Engine *engine, guint32 *flags, CamelException *ex)
{
    camel_imap4_token_t token;
    guint32 new = 0;
    int i;
    
    if (camel_imap4_engine_next_token (engine, &token, ex) == -1)
        return -1;
    
    if (token.token != '(') {
        d(fprintf (stderr, "Expected to find a '(' token starting the flags list\n"));
        camel_imap4_utils_set_unexpected_token_error (ex, engine, &token);
        return -1;
    }
    
    if (camel_imap4_engine_next_token (engine, &token, ex) == -1)
        return -1;
    
    while (token.token == CAMEL_IMAP4_TOKEN_ATOM || token.token == CAMEL_IMAP4_TOKEN_FLAG) {
        /* parse the flags list */
        for (i = 0; i < G_N_ELEMENTS (imap4_flags); i++) {
            if (!g_ascii_strcasecmp (imap4_flags[i].name, token.v.atom)) {
                new |= imap4_flags[i].flag;
                break;
            }
        }
        
#if 0
        if (i == G_N_ELEMENTS (imap4_flags)) {
            for (i = 0; i < G_N_ELEMENTS (imap4_user_flags); i++) {
                if (!g_ascii_strcasecmp (imap4_user_flags[i].name, token.v.atom)) {
                    new |= imap4_user_flags[i].flag;
                    break;
                }
            }
            
            if (i == G_N_ELEMENTS (imap4_user_flags))
                fprintf (stderr, "Encountered unknown flag: %s\n", token.v.atom);
        }
#else
        if (i == G_N_ELEMENTS (imap4_flags))
            fprintf (stderr, "Encountered unknown flag: %s\n", token.v.atom);
#endif
        
        if (camel_imap4_engine_next_token (engine, &token, ex) == -1)
            return -1;
    }
    
    if (token.token != ')') {
        d(fprintf (stderr, "Expected to find a ')' token terminating the flags list\n"));
        camel_imap4_utils_set_unexpected_token_error (ex, engine, &token);
        return -1;
    }
    
    *flags = new;
    
    return 0;
}


struct {
    const char *name;
    guint32 flag;
} list_flags[] = {
    { "\\Marked",        CAMEL_IMAP4_FOLDER_MARKED    },
    { "\\Unmarked",      CAMEL_IMAP4_FOLDER_UNMARKED  },
    { "\\Noselect",      CAMEL_FOLDER_NOSELECT        },
    { "\\Noinferiors",   CAMEL_FOLDER_NOINFERIORS     },
    { "\\HasChildren",   CAMEL_FOLDER_CHILDREN        },
    { "\\HasNoChildren", CAMEL_FOLDER_NOCHILDREN      },
};

int
camel_imap4_untagged_list (CamelIMAP4Engine *engine, CamelIMAP4Command *ic, guint32 index, camel_imap4_token_t *token, CamelException *ex)
{
    GPtrArray *array = ic->user_data;
    camel_imap4_list_t *list;
    unsigned char *buf;
    guint32 flags = 0;
    GString *literal;
    char delim;
    size_t n;
    int i;
    
    if (camel_imap4_engine_next_token (engine, token, ex) == -1)
        return -1;
    
    /* parse the flag list */
    if (token->token != '(')
        goto unexpected;
    
    if (camel_imap4_engine_next_token (engine, token, ex) == -1)
        return -1;
    
    while (token->token == CAMEL_IMAP4_TOKEN_FLAG || token->token == CAMEL_IMAP4_TOKEN_ATOM) {
        for (i = 0; i < G_N_ELEMENTS (list_flags); i++) {
            if (!g_ascii_strcasecmp (list_flags[i].name, token->v.atom)) {
                flags |= list_flags[i].flag;
                break;
            }
        }
        
        if (camel_imap4_engine_next_token (engine, token, ex) == -1)
            return -1;
    }
    
    if (token->token != ')')
        goto unexpected;
    
    /* parse the path delimiter */
    if (camel_imap4_engine_next_token (engine, token, ex) == -1)
        return -1;
    
    switch (token->token) {
    case CAMEL_IMAP4_TOKEN_NIL:
        delim = '\0';
        break;
    case CAMEL_IMAP4_TOKEN_QSTRING:
        delim = *token->v.qstring;
        break;
    default:
        goto unexpected;
    }
    
    /* parse the folder name */
    if (camel_imap4_engine_next_token (engine, token, ex) == -1)
        return -1;
    
    list = g_new (camel_imap4_list_t, 1);
    list->flags = flags;
    list->delim = delim;
    
    switch (token->token) {
    case CAMEL_IMAP4_TOKEN_ATOM:
        list->name = g_strdup (token->v.atom);
        break;
    case CAMEL_IMAP4_TOKEN_QSTRING:
        list->name = g_strdup (token->v.qstring);
        break;
    case CAMEL_IMAP4_TOKEN_LITERAL:
        literal = g_string_new ("");
        while ((i = camel_imap4_stream_literal (engine->istream, &buf, &n)) == 1)
            g_string_append_len (literal, buf, n);
        
        if (i == -1) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          _("IMAP server %s unexpectedly disconnected: %s"),
                          engine->url->host, errno ? g_strerror (errno) : _("Unknown"));
            g_string_free (literal, TRUE);
            return -1;
        }
        
        g_string_append_len (literal, buf, n);
        list->name = literal->str;
        g_string_free (literal, FALSE);
        break;
    default:
        g_free (list);
        goto unexpected;
    }
    
    g_ptr_array_add (array, list);
    
    return camel_imap4_engine_eat_line (engine, ex);
    
 unexpected:
    
    camel_imap4_utils_set_unexpected_token_error (ex, engine, token);
    
    return -1;
}


static struct {
    const char *name;
    int type;
} imap4_status[] = {
    { "MESSAGES",    CAMEL_IMAP4_STATUS_MESSAGES    },
    { "RECENT",      CAMEL_IMAP4_STATUS_RECENT      },
    { "UIDNEXT",     CAMEL_IMAP4_STATUS_UIDNEXT     },
    { "UIDVALIDITY", CAMEL_IMAP4_STATUS_UIDVALIDITY },
    { "UNSEEN",      CAMEL_IMAP4_STATUS_UNSEEN      },
};


void
camel_imap4_status_free (camel_imap4_status_t *status)
{
    camel_imap4_status_attr_t *attr, *next;
    
    attr = status->attr_list;
    while (attr != NULL) {
        next = attr->next;
        g_free (attr);
        attr = next;
    }
    
    g_free (status->mailbox);
    g_free (status);
}


int
camel_imap4_untagged_status (CamelIMAP4Engine *engine, CamelIMAP4Command *ic, guint32 index, camel_imap4_token_t *token, CamelException *ex)
{
    camel_imap4_status_attr_t *attr, *tail, *list = NULL;
    GPtrArray *array = ic->user_data;
    camel_imap4_status_t *status;
    char *mailbox;
    size_t len;
    int type;
    int i;
    
    if (camel_imap4_engine_next_token (engine, token, ex) == -1)
        return -1;
    
    switch (token->token) {
    case CAMEL_IMAP4_TOKEN_ATOM:
        mailbox = g_strdup (token->v.atom);
        break;
    case CAMEL_IMAP4_TOKEN_QSTRING:
        mailbox = g_strdup (token->v.qstring);
        break;
    case CAMEL_IMAP4_TOKEN_LITERAL:
        if (camel_imap4_engine_literal (engine, (unsigned char **) &mailbox, &len, ex) == -1)
            return -1;
        break;
    default:
        fprintf (stderr, "Unexpected token in IMAP4 untagged STATUS response: %s%c\n",
             token->token == CAMEL_IMAP4_TOKEN_NIL ? "NIL" : "",
             (unsigned char) (token->token & 0xff));
        camel_imap4_utils_set_unexpected_token_error (ex, engine, token);
        return -1;
    }
    
    if (camel_imap4_engine_next_token (engine, token, ex) == -1) {
        g_free (mailbox);
        return -1;
    }
    
    if (token->token != '(') {
        d(fprintf (stderr, "Expected to find a '(' token after the mailbox token in the STATUS response\n"));
        camel_imap4_utils_set_unexpected_token_error (ex, engine, token);
        g_free (mailbox);
        return -1;
    }
    
    if (camel_imap4_engine_next_token (engine, token, ex) == -1) {
        g_free (mailbox);
        return -1;
    }
    
    tail = (camel_imap4_status_attr_t *) &list;
    
    while (token->token == CAMEL_IMAP4_TOKEN_ATOM) {
        /* parse the status messages list */
        type = CAMEL_IMAP4_STATUS_UNKNOWN;
        for (i = 0; i < G_N_ELEMENTS (imap4_status); i++) {
            if (!g_ascii_strcasecmp (imap4_status[i].name, token->v.atom)) {
                type = imap4_status[i].type;
                break;
            }
        }
        
        if (type == CAMEL_IMAP4_STATUS_UNKNOWN)
            fprintf (stderr, "unrecognized token in STATUS list: %s\n", token->v.atom);
        
        if (camel_imap4_engine_next_token (engine, token, ex) == -1)
            goto exception;
        
        if (token->token != CAMEL_IMAP4_TOKEN_NUMBER)
            break;
        
        attr = g_new (camel_imap4_status_attr_t, 1);
        attr->next = NULL;
        attr->type = type;
        attr->value = token->v.number;
        
        tail->next = attr;
        tail = attr;
        
        if (camel_imap4_engine_next_token (engine, token, ex) == -1)
            goto exception;
    }
    
    status = g_new (camel_imap4_status_t, 1);
    status->mailbox = mailbox;
    status->attr_list = list;
    list = NULL;
    
    g_ptr_array_add (array, status);
    
    if (token->token != ')') {
        d(fprintf (stderr, "Expected to find a ')' token terminating the untagged STATUS response\n"));
        camel_imap4_utils_set_unexpected_token_error (ex, engine, token);
        return -1;
    }
    
    if (camel_imap4_engine_next_token (engine, token, ex) == -1)
        return -1;
    
    if (token->token != '\n') {
        d(fprintf (stderr, "Expected to find a '\\n' token after the STATUS response\n"));
        camel_imap4_utils_set_unexpected_token_error (ex, engine, token);
        return -1;
    }
    
    return 0;
    
 exception:
    
    g_free (mailbox);
    
    attr = list;
    while (attr != NULL) {
        list = attr->next;
        g_free (attr);
        attr = list;
    }
    
    return -1;
}