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 1999, 2000 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
33 #include "camel-debug.h"
35 #include "camel-nntp-stream.h"
37 #define dd(x) (camel_debug("nntp:stream")?(x):0)
40 #define ECONNRESET EIO
43 static CamelObjectClass *parent_class = NULL;
45 /* Returns the class for a CamelStream */
46 #define CS_CLASS(so) CAMEL_NNTP_STREAM_CLASS(CAMEL_OBJECT_GET_CLASS(so))
48 #define CAMEL_NNTP_STREAM_SIZE (4096)
49 #define CAMEL_NNTP_STREAM_LINE_SIZE (1024) /* maximum line size */
52 stream_fill(CamelNNTPStream *is)
57 left = is->end - is->ptr;
58 memcpy(is->buf, is->ptr, left);
59 is->end = is->buf + left;
61 left = camel_stream_read(is->source, is->end, CAMEL_NNTP_STREAM_SIZE - (is->end - is->buf));
65 return is->end - is->ptr;
69 dd(printf("NNTP_STREAM_FILL(ERROR): %d - '%s'\n", left, strerror(errno)));
78 stream_read(CamelStream *stream, char *buffer, size_t n)
80 CamelNNTPStream *is = (CamelNNTPStream *)stream;
82 unsigned char *p, *e, c;
85 if (is->mode != CAMEL_NNTP_STREAM_DATA || n == 0)
92 /* Need to copy/strip '.'s and whatnot */
98 case 0: /* start of line, always read at least 3 chars */
101 if (stream_fill(is) == -1)
107 if (p[1] == '\r' && p[2] == '\n') {
109 is->mode = CAMEL_NNTP_STREAM_EOD;
111 dd(printf("NNTP_STREAM_READ(%d):\n%.*s\n", (int)(o-buffer), (int)(o-buffer), buffer));
118 case 1: /* looking for next sol */
122 /* end of input sentinal check */
125 if (stream_fill(is) == -1)
134 } else if (c != '\r') {
144 dd(printf("NNTP_STREAM_READ(%d):\n%.*s\n", (int)(o-buffer), (int)(o-buffer), buffer));
150 stream_write(CamelStream *stream, const char *buffer, size_t n)
152 CamelNNTPStream *is = (CamelNNTPStream *)stream;
154 return camel_stream_write(is->source, buffer, n);
158 stream_close(CamelStream *stream)
165 stream_flush(CamelStream *stream)
172 stream_eos(CamelStream *stream)
174 CamelNNTPStream *is = (CamelNNTPStream *)stream;
176 return is->mode != CAMEL_NNTP_STREAM_DATA;
180 stream_reset(CamelStream *stream)
182 /* nop? reset literal mode? */
187 camel_nntp_stream_class_init (CamelStreamClass *camel_nntp_stream_class)
189 CamelStreamClass *camel_stream_class = (CamelStreamClass *)camel_nntp_stream_class;
191 parent_class = camel_type_get_global_classfuncs( CAMEL_OBJECT_TYPE );
193 /* virtual method definition */
194 camel_stream_class->read = stream_read;
195 camel_stream_class->write = stream_write;
196 camel_stream_class->close = stream_close;
197 camel_stream_class->flush = stream_flush;
198 camel_stream_class->eos = stream_eos;
199 camel_stream_class->reset = stream_reset;
203 camel_nntp_stream_init(CamelNNTPStream *is, CamelNNTPStreamClass *isclass)
205 /* +1 is room for appending a 0 if we need to for a line */
206 is->ptr = is->end = is->buf = g_malloc(CAMEL_NNTP_STREAM_SIZE+1);
207 is->lineptr = is->linebuf = g_malloc(CAMEL_NNTP_STREAM_LINE_SIZE+1);
208 is->lineend = is->linebuf + CAMEL_NNTP_STREAM_LINE_SIZE;
214 is->mode = CAMEL_NNTP_STREAM_LINE;
218 camel_nntp_stream_finalise(CamelNNTPStream *is)
223 camel_object_unref((CamelObject *)is->source);
227 camel_nntp_stream_get_type (void)
229 static CamelType camel_nntp_stream_type = CAMEL_INVALID_TYPE;
231 if (camel_nntp_stream_type == CAMEL_INVALID_TYPE) {
232 camel_nntp_stream_type = camel_type_register( camel_stream_get_type(),
234 sizeof( CamelNNTPStream ),
235 sizeof( CamelNNTPStreamClass ),
236 (CamelObjectClassInitFunc) camel_nntp_stream_class_init,
238 (CamelObjectInitFunc) camel_nntp_stream_init,
239 (CamelObjectFinalizeFunc) camel_nntp_stream_finalise );
242 return camel_nntp_stream_type;
246 * camel_nntp_stream_new:
248 * Returns a NULL stream. A null stream is always at eof, and
249 * always returns success for all reads and writes.
251 * Return value: the stream
254 camel_nntp_stream_new(CamelStream *source)
258 is = (CamelNNTPStream *)camel_object_new(camel_nntp_stream_get_type ());
259 camel_object_ref((CamelObject *)source);
262 return (CamelStream *)is;
265 /* Get one line from the nntp stream */
267 camel_nntp_stream_line(CamelNNTPStream *is, unsigned char **data, unsigned int *len)
269 register unsigned char c, *p, *o, *oe;
273 if (is->mode == CAMEL_NNTP_STREAM_EOD) {
280 oe = is->lineend - 1;
284 /* Data mode, convert leading '..' to '.', and stop when we reach a solitary '.' */
285 if (is->mode == CAMEL_NNTP_STREAM_DATA) {
286 /* need at least 3 chars in buffer */
289 if (stream_fill(is) == -1)
295 /* check for isolated '.\r\n' or begging of line '.' */
297 if (p[1] == '\r' && p[2] == '\n') {
299 is->mode = CAMEL_NNTP_STREAM_EOD;
304 dd(printf("NNTP_STREAM_LINE(END)\n"));
319 if (stream_fill(is) == -1)
326 *len = o - is->linebuf;
329 dd(printf("NNTP_STREAM_LINE(%d): '%s'\n", *len, *data));
333 } else if (c != '\r') {
338 /* limit this for bad server data? */
339 oldlen = o - is->linebuf;
340 newlen = (is->lineend - is->linebuf) * 3 / 2;
341 is->lineptr = is->linebuf = g_realloc(is->linebuf, newlen);
342 is->lineend = is->linebuf + newlen;
343 oe = is->lineend - 1;
344 o = is->linebuf + oldlen;
350 /* returns -1 on error, 0 if last lot of data, >0 if more remaining */
351 int camel_nntp_stream_gets(CamelNNTPStream *is, unsigned char **start, unsigned int *len)
358 max = is->end - is->ptr;
360 max = stream_fill(is);
366 end = memchr(is->ptr, '\n', max);
368 max = (end - is->ptr) + 1;
373 dd(printf("NNTP_STREAM_GETS(%s,%d): '%.*s'\n", end==NULL?"more":"last", *len, (int)*len, *start));
375 return end == NULL?1:0;
378 void camel_nntp_stream_set_mode(CamelNNTPStream *is, camel_nntp_stream_mode_t mode)
383 /* returns -1 on erorr, 0 if last data, >0 if more data left */
384 int camel_nntp_stream_getd(CamelNNTPStream *is, unsigned char **start, unsigned int *len)
386 unsigned char *p, *e, *s;
391 if (is->mode == CAMEL_NNTP_STREAM_EOD)
394 if (is->mode == CAMEL_NNTP_STREAM_LINE) {
395 g_warning("nntp_stream reading data in line mode\n");
405 if (stream_fill(is) == -1)
416 /* check leading '.', ... */
418 if (p[1] == '\r' && p[2] == '\n') {
422 is->mode = CAMEL_NNTP_STREAM_EOD;
425 dd(printf("NNTP_STREAM_GETD(%s,%d): '%.*s'\n", "last", *len, (int)*len, *start));
430 /* If at start, just skip '.', else return data upto '.' but skip it */
440 dd(printf("NNTP_STREAM_GETD(%s,%d): '%.*s'\n", "more", *len, (int)*len, *start));
447 /* Scan for sentinal */
458 } while ((e-p) >= 3);
465 dd(printf("NNTP_STREAM_GETD(%s,%d): '%.*s'\n", "more", *len, (int)*len, *start));