1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
4 * Michael Zucchi <notzed@ximian.com>
6 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU Lesser General Public
10 * License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
32 #include <glib/gi18n-lib.h>
34 #include "camel-imapx-utils.h"
35 #include "camel-imapx-stream.h"
37 #define t(...) camel_imapx_debug(token, __VA_ARGS__)
38 #define io(...) camel_imapx_debug(io, __VA_ARGS__)
40 G_DEFINE_TYPE (CamelIMAPXStream, camel_imapx_stream, CAMEL_TYPE_STREAM)
43 imapx_stream_fill (CamelIMAPXStream *is,
44 GCancellable *cancellable,
50 left = is->end - is->ptr;
51 memcpy (is->buf, is->ptr, left);
52 is->end = is->buf + left;
54 left = camel_stream_read (
55 is->source, (gchar *) is->end,
56 is->bufsize - (is->end - is->buf),
60 io(is->tagprefix, "camel_imapx_read: buffer is '%.*s'\n", (gint)(is->end - is->ptr), is->ptr);
61 return is->end - is->ptr;
63 io(is->tagprefix, "camel_imapx_read: -1\n");
64 /* If returning zero, camel_stream_read() doesn't consider
65 * that to be an error. But we do -- we should only be here
66 * if we *know* there are data to receive. So set the error
69 g_set_error (error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
70 _("Source stream returned no data"));
75 io(is->tagprefix, "camel_imapx_read: -1\n");
78 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
79 _("Source stream unavailable"));
85 imapx_stream_dispose (GObject *object)
87 CamelIMAPXStream *stream = CAMEL_IMAPX_STREAM (object);
89 if (stream->source != NULL) {
90 g_object_unref (stream->source);
91 stream->source = NULL;
94 /* Chain up to parent's dispose() method. */
95 G_OBJECT_CLASS (camel_imapx_stream_parent_class)->dispose (object);
99 imapx_stream_finalize (GObject *object)
101 CamelIMAPXStream *stream = CAMEL_IMAPX_STREAM (object);
103 g_free (stream->buf);
104 g_free (stream->tokenbuf);
106 /* Chain up to parent's finalize() method. */
107 G_OBJECT_CLASS (camel_imapx_stream_parent_class)->finalize (object);
111 imapx_stream_read (CamelStream *stream,
114 GCancellable *cancellable,
117 CamelIMAPXStream *is = (CamelIMAPXStream *) stream;
120 if (is->literal == 0 || n == 0)
123 max = is->end - is->ptr;
125 max = MIN (max, is->literal);
127 memcpy (buffer, is->ptr, max);
130 max = MIN (is->literal, n);
131 max = camel_stream_read (is->source, buffer, max, cancellable, error);
136 io(is->tagprefix, "camel_imapx_read(literal): '%.*s'\n", (gint)max, buffer);
144 imapx_stream_write (CamelStream *stream,
147 GCancellable *cancellable,
150 CamelIMAPXStream *is = (CamelIMAPXStream *) stream;
152 if (g_strstr_len (buffer, n, "LOGIN")) {
153 io(is->tagprefix, "camel_imapx_write: 'LOGIN...'\n");
155 io(is->tagprefix, "camel_imapx_write: '%.*s'\n", (gint)n, buffer);
158 return camel_stream_write (is->source, buffer, n, cancellable, error);
162 imapx_stream_close (CamelStream *stream,
163 GCancellable *cancellable,
171 imapx_stream_flush (CamelStream *stream,
172 GCancellable *cancellable,
180 imapx_stream_eos (CamelStream *stream)
182 CamelIMAPXStream *is = (CamelIMAPXStream *) stream;
184 return is->literal == 0;
188 camel_imapx_stream_class_init (CamelIMAPXStreamClass *class)
190 GObjectClass *object_class;
191 CamelStreamClass *stream_class;
193 object_class = G_OBJECT_CLASS (class);
194 object_class->dispose = imapx_stream_dispose;
195 object_class->finalize = imapx_stream_finalize;
197 stream_class = CAMEL_STREAM_CLASS (class);
198 stream_class->read = imapx_stream_read;
199 stream_class->write = imapx_stream_write;
200 stream_class->close = imapx_stream_close;
201 stream_class->flush = imapx_stream_flush;
202 stream_class->eos = imapx_stream_eos;
206 camel_imapx_stream_init (CamelIMAPXStream *is)
208 /* +1 is room for appending a 0 if we need to for a token */
210 is->ptr = is->end = is->buf = g_malloc (is->bufsize + 1);
211 is->tokenbuf = g_malloc (is->bufsize + 1);
214 static void camel_imapx_stream_grow (CamelIMAPXStream *is, guint len, guchar **bufptr, guchar **tokptr)
216 guchar *oldtok = is->tokenbuf;
217 guchar *oldbuf = is->buf;
221 } while (is->bufsize <= len);
223 io(is->tagprefix, "Grow imapx buffers to %d bytes\n", is->bufsize);
225 is->tokenbuf = g_realloc (is->tokenbuf, is->bufsize + 1);
227 *tokptr = is->tokenbuf + (*tokptr - oldtok);
229 is->unget_token = is->tokenbuf + (is->unget_token - oldtok);
231 //io(is->tagprefix, "buf was %p, ptr %p end %p\n", is->buf, is->ptr, is->end);
232 is->buf = g_realloc (is->buf, is->bufsize + 1);
233 is->ptr = is->buf + (is->ptr - oldbuf);
234 is->end = is->buf + (is->end - oldbuf);
235 //io(is->tagprefix, "buf now %p, ptr %p end %p\n", is->buf, is->ptr, is->end);
237 *bufptr = is->buf + (*bufptr - oldbuf);
241 * camel_imapx_stream_new:
243 * Returns a NULL stream. A null stream is always at eof, and
244 * always returns success for all reads and writes.
246 * Returns: the stream
249 camel_imapx_stream_new (CamelStream *source)
251 CamelIMAPXStream *is;
253 is = g_object_new (CAMEL_TYPE_IMAPX_STREAM, NULL);
254 is->source = g_object_ref (source);
256 return (CamelStream *) is;
260 camel_imapx_error_quark (void)
262 static GQuark quark = 0;
264 if (G_UNLIKELY (quark == 0)) {
265 const gchar *string = "camel-imapx-error-quark";
266 quark = g_quark_from_static_string (string);
272 /* Returns if there is any data buffered that is ready for processing */
274 camel_imapx_stream_buffered (CamelIMAPXStream *is)
276 return is->end - is->ptr;
282 skip_ws (CamelIMAPXStream *is,
286 register guchar c, *p;
295 if (imapx_stream_fill (is, NULL) == IMAPX_TOK_ERROR)
296 return IMAPX_TOK_ERROR;
301 } while (c == ' ' || c == '\r');
310 /* FIXME: these should probably handle it themselves,
311 * and get rid of the token interface? */
313 camel_imapx_stream_atom (CamelIMAPXStream *is,
316 GCancellable *cancellable,
320 GError *local_error = NULL;
322 /* this is only 'approximate' atom */
323 switch (camel_imapx_stream_token (is, data, lenp, cancellable, &local_error)) {
324 case IMAPX_TOK_TOKEN:
330 case IMAPX_TOK_ERROR:
331 if (local_error != NULL)
332 g_propagate_error (error, local_error);
333 return IMAPX_TOK_ERROR;
335 if (local_error == NULL)
336 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "expecting atom");
338 g_propagate_error (error, local_error);
339 io(is->tagprefix, "expecting atom!\n");
340 return IMAPX_TOK_PROTOCOL;
344 /* gets an atom, a quoted_string, or a literal */
346 camel_imapx_stream_astring (CamelIMAPXStream *is,
348 GCancellable *cancellable,
354 GError *local_error = NULL;
356 switch (camel_imapx_stream_token (is, data, &len, cancellable, &local_error)) {
357 case IMAPX_TOK_TOKEN:
359 case IMAPX_TOK_STRING:
361 case IMAPX_TOK_LITERAL:
362 if (len >= is->bufsize)
363 camel_imapx_stream_grow (is, len, NULL, NULL);
365 camel_imapx_stream_set_literal (is, len);
367 ret = camel_imapx_stream_getl (is, &start, &inlen, cancellable, error);
370 memcpy (p, start, inlen);
374 *data = is->tokenbuf;
376 case IMAPX_TOK_ERROR:
377 /* wont get unless no exception hanlder*/
378 if (local_error != NULL)
379 g_propagate_error (error, local_error);
380 return IMAPX_TOK_ERROR;
382 if (local_error == NULL)
383 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "expecting astring");
385 g_propagate_error (error, local_error);
386 io(is->tagprefix, "expecting astring!\n");
387 return IMAPX_TOK_PROTOCOL;
391 /* check for NIL or (small) quoted_string or literal */
393 camel_imapx_stream_nstring (CamelIMAPXStream *is,
395 GCancellable *cancellable,
401 GError *local_error = NULL;
403 switch (camel_imapx_stream_token (is, data, &len, cancellable, &local_error)) {
404 case IMAPX_TOK_STRING:
406 case IMAPX_TOK_LITERAL:
407 if (len >= is->bufsize)
408 camel_imapx_stream_grow (is, len, NULL, NULL);
410 camel_imapx_stream_set_literal (is, len);
412 ret = camel_imapx_stream_getl (is, &start, &inlen, cancellable, error);
415 memcpy (p, start, inlen);
419 *data = is->tokenbuf;
421 case IMAPX_TOK_TOKEN:
423 if (toupper (p[0]) == 'N' && toupper (p[1]) == 'I' && toupper (p[2]) == 'L' && p[3] == 0) {
428 if (local_error == NULL)
429 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "expecting nstring");
431 g_propagate_error (error, local_error);
432 return IMAPX_TOK_PROTOCOL;
433 case IMAPX_TOK_ERROR:
434 /* we'll never get this unless there are no exception handlers anyway */
435 if (local_error != NULL)
436 g_propagate_error (error, local_error);
437 return IMAPX_TOK_ERROR;
442 /* parse an nstring as a stream */
444 camel_imapx_stream_nstring_stream (CamelIMAPXStream *is,
445 CamelStream **stream,
446 GCancellable *cancellable,
448 /* throws IO,PARSE exception */
453 CamelStream * mem = NULL;
454 GError *local_error = NULL;
458 switch (camel_imapx_stream_token (is, &token, &len, cancellable, &local_error)) {
459 case IMAPX_TOK_STRING:
460 mem = camel_stream_mem_new_with_buffer ((gchar *) token, len);
463 case IMAPX_TOK_LITERAL:
464 /* if len is big, we could automatically use a file backing */
465 camel_imapx_stream_set_literal (is, len);
466 mem = camel_stream_mem_new ();
467 if (camel_stream_write_to_stream ((CamelStream *) is, mem, cancellable, error) == -1) {
468 g_object_unref (mem);
475 G_SEEK_SET, NULL, NULL);
479 case IMAPX_TOK_TOKEN:
480 if (toupper (token[0]) == 'N' && toupper (token[1]) == 'I' && toupper (token[2]) == 'L' && token[3] == 0) {
486 if (local_error == NULL)
487 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "nstring: token not string");
489 g_propagate_error (error, local_error);
496 camel_imapx_stream_number (CamelIMAPXStream *is,
497 GCancellable *cancellable,
502 GError *local_error = NULL;
504 if (camel_imapx_stream_token (is, &token, &len, cancellable, &local_error) != IMAPX_TOK_INT) {
505 if (local_error == NULL)
506 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "expecting number");
508 g_propagate_error (error, local_error);
512 return strtoull ((gchar *) token, 0, 10);
516 camel_imapx_stream_text (CamelIMAPXStream *is,
518 GCancellable *cancellable,
521 GByteArray *build = g_byte_array_new ();
526 while (is->unget > 0) {
527 switch (is->unget_tok) {
528 case IMAPX_TOK_TOKEN:
529 case IMAPX_TOK_STRING:
531 g_byte_array_append (build, (guint8 *) is->unget_token, is->unget_len);
532 g_byte_array_append(build, (guint8 *) " ", 1);
533 default: /* invalid, but we'll ignore */
540 tok = camel_imapx_stream_gets (is, &token, &len, cancellable, error);
543 g_byte_array_free (build, TRUE);
547 g_byte_array_append (build, token, len);
550 g_byte_array_append(build, (guint8 *) "", 1);
552 g_byte_array_free (build, FALSE);
557 /* Get one token from the imap stream */
559 /* throws IO,PARSE exception */
560 camel_imapx_stream_token (CamelIMAPXStream *is,
563 GCancellable *cancellable,
566 register guchar c, *oe;
573 *data = is->unget_token;
574 *len = is->unget_len;
575 /*printf("token UNGET '%c' %s\n", is->unget_tok, is->unget_token);*/
576 return is->unget_tok;
580 g_warning("stream_token called with literal %d", is->literal);
585 /* skip whitespace/prefill buffer */
589 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
590 return IMAPX_TOK_ERROR;
595 } while (c == ' ' || c == '\r');
597 /*strchr("\n*()[]+", c)*/
598 if (imapx_is_token_char (c)) {
600 t(is->tagprefix, "token '%c'\n", c);
602 } else if (c == '{') {
608 if (isdigit (c) && literal < (UINT_MAX / 10)) {
609 literal = literal * 10 + (c - '0');
610 } else if (c == '}') {
617 is->literal = literal;
618 t(is->tagprefix, "token LITERAL %d\n", literal);
619 return IMAPX_TOK_LITERAL;
623 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
624 return IMAPX_TOK_ERROR;
630 io(is->tagprefix, "Protocol error: literal too big\n");
632 io(is->tagprefix, "Protocol error: literal contains invalid gchar %02x '%c'\n", c, isprint(c)?c:c);
638 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
639 return IMAPX_TOK_ERROR;
643 } else if (c == '"') {
645 oe = is->tokenbuf + is->bufsize - 1;
652 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
653 return IMAPX_TOK_ERROR;
658 } else if (c == '\"') {
661 *data = is->tokenbuf;
662 *len = o - is->tokenbuf;
663 t(is->tagprefix, "token STRING '%s'\n", is->tokenbuf);
664 return IMAPX_TOK_STRING;
666 if (c == '\n' || c == '\r') {
667 io(is->tagprefix, "Protocol error: truncated string\n");
671 camel_imapx_stream_grow (is, 0, &p, &o);
672 oe = is->tokenbuf + is->bufsize - 1;
678 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
679 return IMAPX_TOK_ERROR;
685 oe = is->tokenbuf + is->bufsize - 1;
686 digits = isdigit (c);
691 /*if (strchr(" \r\n*()[]+", c) != NULL) {*/
692 if (imapx_is_notid_char (c)) {
693 if (c == ' ' || c == '\r')
698 *data = is->tokenbuf;
699 *len = o - is->tokenbuf;
700 t(is->tagprefix, "token TOKEN '%s'\n", is->tokenbuf);
701 return digits ? IMAPX_TOK_INT : IMAPX_TOK_TOKEN;
705 camel_imapx_stream_grow (is, 0, &p, &o);
706 oe = is->tokenbuf + is->bufsize - 1;
709 digits &= isdigit (c);
713 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
714 return IMAPX_TOK_ERROR;
720 /* Protocol error, skip until next lf? */
722 io(is->tagprefix, "Got protocol error\n");
729 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "protocol error");
730 return IMAPX_TOK_PROTOCOL;
734 camel_imapx_stream_ungettoken (CamelIMAPXStream *is,
735 camel_imapx_token_t tok,
739 /*printf("ungettoken: '%c' '%s'\n", tok, token);*/
741 is->unget_token = token;
746 /* returns -1 on error, 0 if last lot of data, >0 if more remaining */
748 camel_imapx_stream_gets (CamelIMAPXStream *is,
751 GCancellable *cancellable,
759 max = is->end - is->ptr;
761 max = imapx_stream_fill (is, cancellable, error);
767 end = memchr (is->ptr, '\n', max);
769 max = (end - is->ptr) + 1;
774 return end == NULL ? 1 : 0;
777 void camel_imapx_stream_set_literal (CamelIMAPXStream *is, guint literal)
779 is->literal = literal;
782 /* returns -1 on erorr, 0 if last data, >0 if more data left */
784 camel_imapx_stream_getl (CamelIMAPXStream *is,
787 GCancellable *cancellable,
794 if (is->literal > 0) {
795 max = is->end - is->ptr;
797 max = imapx_stream_fill (is, cancellable, error);
802 max = MIN (max, is->literal);
815 /* skip the rest of the line of tokens */
817 camel_imapx_stream_skip (CamelIMAPXStream *is,
818 GCancellable *cancellable,
826 tok = camel_imapx_stream_token (is, &token, &len, cancellable, error);
827 if (tok == IMAPX_TOK_LITERAL) {
828 camel_imapx_stream_set_literal (is, len);
829 while ((tok = camel_imapx_stream_getl (is, &token, &len, cancellable, error)) > 0) {
830 io(is->tagprefix, "Skip literal data '%.*s'\n", (gint)len, token);
833 } while (tok != '\n' && tok >= 0);