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 2002 Ximian, Inc. (www.ximian.com)
8 * This program 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
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23 /* This is *identical* to the camel-nntp-stream, so should probably
24 work out a way to merge them */
37 #include "camel-pop3-stream.h"
39 extern int camel_verbose_debug;
40 #define dd(x) (camel_verbose_debug?(x):0)
42 static CamelObjectClass *parent_class = NULL;
44 /* Returns the class for a CamelStream */
45 #define CS_CLASS(so) CAMEL_POP3_STREAM_CLASS(CAMEL_OBJECT_GET_CLASS(so))
47 #define CAMEL_POP3_STREAM_SIZE (4096)
48 #define CAMEL_POP3_STREAM_LINE (1024) /* maximum line size */
51 stream_fill(CamelPOP3Stream *is)
56 left = is->end - is->ptr;
57 memcpy(is->buf, is->ptr, left);
58 is->end = is->buf + left;
60 left = camel_stream_read(is->source, is->end, CAMEL_POP3_STREAM_SIZE - (is->end - is->buf));
64 return is->end - is->ptr;
66 dd(printf("POP3_STREAM_FILL(ERROR): '%s'\n", strerror (errno)));
75 stream_read(CamelStream *stream, char *buffer, size_t n)
77 CamelPOP3Stream *is = (CamelPOP3Stream *)stream;
79 unsigned char *p, *e, c;
82 if (is->mode != CAMEL_POP3_STREAM_DATA || n == 0)
89 /* Need to copy/strip '.'s and whatnot */
95 case 0: /* start of line, always read at least 3 chars */
98 if (stream_fill(is) == -1)
104 if (p[1] == '\r' && p[2] == '\n') {
106 is->mode = CAMEL_POP3_STREAM_EOD;
108 dd(printf("POP3_STREAM_READ(%d):\n%.*s\n", (int)(o-buffer), (int)(o-buffer), buffer));
115 case 1: /* looking for next sol */
119 /* end of input sentinal check */
122 if (stream_fill(is) == -1)
131 } else if (c != '\r') {
141 dd(printf("POP3_STREAM_READ(%d):\n%.*s\n", (int)(o-buffer), (int)(o-buffer), buffer));
147 stream_write(CamelStream *stream, const char *buffer, size_t n)
149 CamelPOP3Stream *is = (CamelPOP3Stream *)stream;
151 if (strncmp (buffer, "PASS ", 5) != 0)
152 dd(printf("POP3_STREAM_WRITE(%d):\n%.*s\n", (int)n, (int)n, buffer));
154 dd(printf("POP3_STREAM_WRITE(%d):\nPASS xxxxxxxx\n", (int)n));
156 return camel_stream_write(is->source, buffer, n);
160 stream_close(CamelStream *stream)
167 stream_flush(CamelStream *stream)
174 stream_eos(CamelStream *stream)
176 CamelPOP3Stream *is = (CamelPOP3Stream *)stream;
178 return is->mode != CAMEL_POP3_STREAM_DATA;
182 stream_reset(CamelStream *stream)
184 /* nop? reset literal mode? */
189 camel_pop3_stream_class_init (CamelStreamClass *camel_pop3_stream_class)
191 CamelStreamClass *camel_stream_class = (CamelStreamClass *)camel_pop3_stream_class;
193 parent_class = camel_type_get_global_classfuncs( CAMEL_OBJECT_TYPE );
195 /* virtual method definition */
196 camel_stream_class->read = stream_read;
197 camel_stream_class->write = stream_write;
198 camel_stream_class->close = stream_close;
199 camel_stream_class->flush = stream_flush;
200 camel_stream_class->eos = stream_eos;
201 camel_stream_class->reset = stream_reset;
205 camel_pop3_stream_init(CamelPOP3Stream *is, CamelPOP3StreamClass *isclass)
207 /* +1 is room for appending a 0 if we need to for a line */
208 is->ptr = is->end = is->buf = g_malloc(CAMEL_POP3_STREAM_SIZE+1);
209 is->lineptr = is->linebuf = g_malloc(CAMEL_POP3_STREAM_LINE+1);
210 is->lineend = is->linebuf + CAMEL_POP3_STREAM_LINE;
216 is->mode = CAMEL_POP3_STREAM_LINE;
220 camel_pop3_stream_finalise(CamelPOP3Stream *is)
225 camel_object_unref((CamelObject *)is->source);
229 camel_pop3_stream_get_type (void)
231 static CamelType camel_pop3_stream_type = CAMEL_INVALID_TYPE;
233 if (camel_pop3_stream_type == CAMEL_INVALID_TYPE) {
234 camel_pop3_stream_type = camel_type_register( camel_stream_get_type(),
236 sizeof( CamelPOP3Stream ),
237 sizeof( CamelPOP3StreamClass ),
238 (CamelObjectClassInitFunc) camel_pop3_stream_class_init,
240 (CamelObjectInitFunc) camel_pop3_stream_init,
241 (CamelObjectFinalizeFunc) camel_pop3_stream_finalise );
244 return camel_pop3_stream_type;
248 * camel_pop3_stream_new:
250 * Returns a NULL stream. A null stream is always at eof, and
251 * always returns success for all reads and writes.
253 * Return value: the stream
256 camel_pop3_stream_new(CamelStream *source)
260 is = (CamelPOP3Stream *)camel_object_new(camel_pop3_stream_get_type ());
261 camel_object_ref((CamelObject *)source);
264 return (CamelStream *)is;
267 /* Get one line from the pop3 stream */
269 camel_pop3_stream_line(CamelPOP3Stream *is, unsigned char **data, unsigned int *len)
271 register unsigned char c, *p, *o, *oe;
275 if (is->mode == CAMEL_POP3_STREAM_EOD) {
282 oe = is->lineend - 1;
286 /* Data mode, convert leading '..' to '.', and stop when we reach a solitary '.' */
287 if (is->mode == CAMEL_POP3_STREAM_DATA) {
288 /* need at least 3 chars in buffer */
291 if (stream_fill(is) == -1)
297 /* check for isolated '.\r\n' or begging of line '.' */
299 if (p[1] == '\r' && p[2] == '\n') {
301 is->mode = CAMEL_POP3_STREAM_EOD;
306 dd(printf("POP3_STREAM_LINE(END)\n"));
321 if (stream_fill(is) == -1)
328 *len = o - is->linebuf;
331 dd(printf("POP3_STREAM_LINE(%d): '%s'\n", *len, *data));
335 } else if (c != '\r') {
340 /* limit this for bad server data? */
341 oldlen = o - is->linebuf;
342 newlen = (is->lineend - is->linebuf) * 3 / 2;
343 is->lineptr = is->linebuf = g_realloc(is->linebuf, newlen);
344 is->lineend = is->linebuf + newlen;
345 oe = is->lineend - 1;
346 o = is->linebuf + oldlen;
352 /* returns -1 on error, 0 if last lot of data, >0 if more remaining */
353 int camel_pop3_stream_gets(CamelPOP3Stream *is, unsigned char **start, unsigned int *len)
360 max = is->end - is->ptr;
362 max = stream_fill(is);
368 end = memchr(is->ptr, '\n', max);
370 max = (end - is->ptr) + 1;
375 dd(printf("POP3_STREAM_GETS(%s,%d): '%.*s'\n", end==NULL?"more":"last", *len, (int)*len, *start));
377 return end == NULL?1:0;
380 void camel_pop3_stream_set_mode(CamelPOP3Stream *is, camel_pop3_stream_mode_t mode)
385 /* returns -1 on erorr, 0 if last data, >0 if more data left */
386 int camel_pop3_stream_getd(CamelPOP3Stream *is, unsigned char **start, unsigned int *len)
388 unsigned char *p, *e, *s;
393 if (is->mode == CAMEL_POP3_STREAM_EOD)
396 if (is->mode == CAMEL_POP3_STREAM_LINE) {
397 g_warning("pop3_stream reading data in line mode\n");
407 if (stream_fill(is) == -1)
418 /* check leading '.', ... */
420 if (p[1] == '\r' && p[2] == '\n') {
424 is->mode = CAMEL_POP3_STREAM_EOD;
427 dd(printf("POP3_STREAM_GETD(%s,%d): '%.*s'\n", "last", *len, (int)*len, *start));
432 /* If at start, just skip '.', else return data upto '.' but skip it */
442 dd(printf("POP3_STREAM_GETD(%s,%d): '%.*s'\n", "more", *len, (int)*len, *start));
449 /* Scan for sentinal */
460 } while ((e-p) >= 3);
467 dd(printf("POP3_STREAM_GETD(%s,%d): '%.*s'\n", "more", *len, (int)*len, *start));