aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/imap/camel-imap-utils.c
blob: 6560481505aac5a9a92cb2516b98da247b09ea77 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: 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 <stdio.h>
#include <string.h>

#include "camel-imap-utils.h"
#include "string-utils.h"

#define d(x) x

char *
imap_next_word (char *buf)
{
    char *word;

    /* skip over current word */
    for (word = buf; *word && *word != ' '; word++);

    /* skip over white space */
    for ( ; *word && *word == ' '; word++);

    return word;
}

gboolean
imap_parse_list_response (char *buf, char *namespace, char **flags, char **sep, char **folder)
{
    char *word, *ep, *f;
    
    *flags = NULL;
    *sep = NULL;
    *folder = NULL;
    
    if (*buf != '*')
        return FALSE;
    
    word = imap_next_word (buf);
    if (g_strncasecmp (word, "LIST", 4) && g_strncasecmp (word, "LSUB", 4))
        return FALSE;
    
    /* get the flags */
    word = imap_next_word (word);
    if (*word != '(')
        return FALSE;
    
    word++;
    for (ep = word; *ep && *ep != ')'; ep++);
    if (*ep != ')')
        return FALSE;
    
    *flags = g_strndup (word, (gint)(ep - word));
    
    /* get the directory separator */
    word = imap_next_word (ep);
    if (*word) {
        for (ep = word; *ep && *ep != ' '; ep++);
        *sep = g_strndup (word, (gint)(ep - word));
        string_unquote (*sep);
    } else {
        return FALSE;
    }
    
    /* get the folder name */
    word = imap_next_word (word);
    *folder = g_strdup (word);
    g_strstrip (*folder);
    
    /* chop out the folder prefix */
    if (*namespace && !strncmp (*folder, namespace, strlen (namespace))) {
        f = *folder + strlen (namespace) + strlen (*sep);
        memmove (*folder, f, strlen (f) + 1);
    }
    
    string_unquote (*folder);  /* unquote the mailbox if it's quoted */
    
    return TRUE;
}