summaryrefslogtreecommitdiff
path: root/libproto/imap/resplist.c
blob: 65d78dc29fc9ecdbc4d7b7ab0a7877711d36e4bc (plain)
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010, 2011 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
#include <mailutils/stream.h>
#include <mailutils/errno.h>
#include <mailutils/sys/imap.h>

static void
_imap_list_free (void *ptr)
{
  struct imap_list_element *elt = ptr;

  switch (elt->type)
    {
    case imap_eltype_string:
      free (elt->v.string);
      break;
      
    case imap_eltype_list:
      mu_list_destroy (&elt->v.list);
    }
  free (ptr);
}

static int
_mu_imap_response_list_create (mu_imap_t imap, mu_list_t *plist)
{
  mu_list_t list;
  int status = mu_list_create (&list);
  MU_IMAP_CHECK_ERROR (imap, status);
  mu_list_set_destroy_item (list, _imap_list_free);
  *plist = list;
  return 0;
}

#define IS_LBRACE(p) ((p)[0] == '(')
#define IS_RBRACE(p) ((p)[0] == ')')
#define IS_NIL(p) (strcmp (p, "NIL") == 0)

static struct imap_list_element *
_new_imap_list_element (mu_imap_t imap, enum imap_eltype type)
{
  struct imap_list_element *elt = calloc (1, sizeof (*elt));
  if (!elt)
    {
      imap->client_state = MU_IMAP_CLIENT_ERROR;
    }
  else
    elt->type = type;
  return elt;
}

struct parsebuf
{
  mu_imap_t pb_imap;
  size_t pb_count;
  char **pb_arr;
  int pb_err;
  int pb_inlist;
};

static void
parsebuf_init (struct parsebuf *pb, mu_imap_t imap)
{
  memset (pb, 0, sizeof *pb);
  pb->pb_imap = imap;
}

static int
parsebuf_advance (struct parsebuf *pb)
{
  if (pb->pb_count == 0)
    return MU_ERR_NOENT;
  pb->pb_count--;
  pb->pb_arr++;
  return 0;
}

static char *
parsebuf_gettok (struct parsebuf *pb)
{
  char *p;
  
  if (pb->pb_count == 0)
    return NULL;
  p = *pb->pb_arr;
  parsebuf_advance (pb);
  return p;
}

static char *
parsebuf_peek (struct parsebuf *pb)
{
  if (pb->pb_count == 0)
    return NULL;
  return *pb->pb_arr;
}

static void
parsebuf_seterr (struct parsebuf *pb, int err)
{
  pb->pb_err = err;
}

static struct imap_list_element *_parse_element (struct parsebuf *pb);

static struct imap_list_element *
_parse_list (struct parsebuf *pb)
{
  int rc;
  struct imap_list_element *elt, *list_elt;

  elt = _new_imap_list_element (pb->pb_imap, imap_eltype_list);
  if (!elt)
    {
      parsebuf_seterr (pb, ENOMEM);
      return NULL;
    }

  rc = _mu_imap_response_list_create (pb->pb_imap, &elt->v.list);
  if (rc)
    {
      free (elt);
      parsebuf_seterr (pb, rc);
      return NULL;
    }

  while ((list_elt = _parse_element (pb)))
    mu_list_append (elt->v.list, list_elt);

  return elt;
}

static struct imap_list_element *
_parse_element (struct parsebuf *pb)
{
  struct imap_list_element *elt;
  char *tok;

  if (pb->pb_err)
    return NULL;

  tok = parsebuf_gettok (pb);
  
  if (!tok)
    {
      if (pb->pb_inlist)
	parsebuf_seterr (pb, MU_ERR_PARSE);
      return NULL;
    }
  
  if (IS_LBRACE (tok))
    {
      tok = parsebuf_peek (pb);
      if (!tok)
	{
	  parsebuf_seterr (pb, MU_ERR_PARSE);
	  return NULL;
	}
      
      if (IS_RBRACE (tok))
	{
	  parsebuf_gettok (pb);
	  elt = _new_imap_list_element (pb->pb_imap, imap_eltype_list);
	  if (!elt)
	    {
	      parsebuf_seterr (pb, ENOMEM);
	      return NULL;
	    }
	  elt->v.list = NULL;
	}
      else
	{
	  pb->pb_inlist++;
	  elt = _parse_list (pb);
	}
    }
  else if (IS_RBRACE (tok))
    {
      if (pb->pb_inlist)
	pb->pb_inlist--;
      else
	parsebuf_seterr (pb, MU_ERR_PARSE);
      return NULL;
    }
  else if (IS_NIL (tok))
    {
      elt = _new_imap_list_element (pb->pb_imap, imap_eltype_list);
      if (!elt)
	{
	  parsebuf_seterr (pb, ENOMEM);
	  return NULL;
	}
      elt->v.list = NULL;
    }
  else
    {
      char *s;
      elt = _new_imap_list_element (pb->pb_imap, imap_eltype_string);
      if (!elt)
	{
	  parsebuf_seterr (pb, ENOMEM);
	  return NULL;
	}
      s = strdup (tok);
      if (!s)
	{
	  free (elt);
	  parsebuf_seterr (pb, ENOMEM);
	  return NULL;
	}
      elt->v.string = s;
    }
  return elt;
}

int
_mu_imap_untagged_response_to_list (mu_imap_t imap, mu_list_t *plist)
{
  struct imap_list_element *elt;
  struct parsebuf pb;

  parsebuf_init (&pb, imap);
  mu_imapio_get_words (imap->io, &pb.pb_count, &pb.pb_arr);
  parsebuf_advance (&pb); /* Skip initial '*' */
  elt = _parse_list (&pb);
  if (pb.pb_err)
    {
      if (elt)
	_imap_list_free (elt);
      imap->client_state = MU_IMAP_CLIENT_ERROR;
      return pb.pb_err;
    }
  *plist = elt->v.list;
  free (elt);
  return 0;
}

int
_mu_imap_list_element_is_string (struct imap_list_element *elt,
				 const char *str)
{
  if (elt->type != imap_eltype_string)
    return 0;
  return strcmp (elt->v.string, str) == 0;
}

int
_mu_imap_list_element_is_nil (struct imap_list_element *elt)
{
  return elt->type == imap_eltype_list && mu_list_is_empty (elt->v.list);
}

struct imap_list_element *
_mu_imap_list_at (mu_list_t list, int idx)
{
  struct imap_list_element *arg;
  int rc = mu_list_get (list, idx, (void*) &arg);
  if (rc)
    {
      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
		("%s:%d: cannot get list element: %s",
		 __FILE__, __LINE__, mu_strerror (rc)));
      return NULL;
    }
  return arg;
}

int
_mu_imap_list_nth_element_is_string (mu_list_t list, size_t n,
				     const char *str)
{
  struct imap_list_element *elt = _mu_imap_list_at (list, n);
  return elt && elt->type == imap_eltype_string &&
	 strcmp (elt->v.string, str) == 0;
}


Return to:

Send suggestions and report system problems to the System administrator.