1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This library is free software you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #if !defined (__CAMEL_H_INSIDE__) && !defined (CAMEL_COMPILATION)
21 #error "Only <camel/camel.h> can be included directly."
24 #ifndef CAMEL_MIME_PARSER_H
25 #define CAMEL_MIME_PARSER_H
27 #include <camel/camel-mime-utils.h>
28 #include <camel/camel-mime-filter.h>
29 #include <camel/camel-stream.h>
31 /* Stardard GObject macros */
32 #define CAMEL_TYPE_MIME_PARSER \
33 (camel_mime_parser_get_type ())
34 #define CAMEL_MIME_PARSER(obj) \
35 (G_TYPE_CHECK_INSTANCE_CAST \
36 ((obj), CAMEL_TYPE_MIME_PARSER, CamelMimeParser))
37 #define CAMEL_MIME_PARSER_CLASS(cls) \
38 (G_TYPE_CHECK_CLASS_CAST \
39 ((cls), CAMEL_TYPE_MIME_PARSER, CamelMimeParserClass))
40 #define CAMEL_IS_MIME_PARSER(obj) \
41 (G_TYPE_CHECK_INSTANCE_TYPE \
42 ((obj), CAMEL_TYPE_MIME_PARSER))
43 #define CAMEL_IS_MIME_PARSER_CLASS(cls) \
44 (G_TYPE_CHECK_CLASS_TYPE \
45 ((cls), CAMEL_TYPE_MIME_PARSER))
46 #define CAMEL_MIME_PARSER_GET_CLASS(obj) \
47 (G_TYPE_INSTANCE_GET_CLASS \
48 ((obj), CAMEL_TYPE_MIME_PARSER, CamelMimeParserClass))
52 typedef struct _CamelMimeParser CamelMimeParser;
53 typedef struct _CamelMimeParserClass CamelMimeParserClass;
54 typedef struct _CamelMimeParserPrivate CamelMimeParserPrivate;
56 /* NOTE: if you add more states, you may need to bump the
57 * start of the END tags to 16 or 32, etc - so they are
58 * the same as the matching start tag, with a bit difference */
59 typedef enum _camel_mime_parser_state_t {
60 CAMEL_MIME_PARSER_STATE_INITIAL,
61 CAMEL_MIME_PARSER_STATE_PRE_FROM, /* data before a 'From' line */
62 CAMEL_MIME_PARSER_STATE_FROM, /* got 'From' line */
63 CAMEL_MIME_PARSER_STATE_HEADER, /* toplevel header */
64 CAMEL_MIME_PARSER_STATE_BODY, /* scanning body of message */
65 CAMEL_MIME_PARSER_STATE_MULTIPART, /* got multipart header */
66 CAMEL_MIME_PARSER_STATE_MESSAGE, /* rfc822 message */
68 CAMEL_MIME_PARSER_STATE_PART, /* part of a multipart */
70 CAMEL_MIME_PARSER_STATE_END = 8, /* bit mask for 'end' flags */
72 CAMEL_MIME_PARSER_STATE_EOF = 8, /* end of file */
73 CAMEL_MIME_PARSER_STATE_PRE_FROM_END, /* pre from end */
74 CAMEL_MIME_PARSER_STATE_FROM_END, /* end of whole from bracket */
75 CAMEL_MIME_PARSER_STATE_HEADER_END, /* dummy value */
76 CAMEL_MIME_PARSER_STATE_BODY_END, /* end of message */
77 CAMEL_MIME_PARSER_STATE_MULTIPART_END, /* end of multipart */
78 CAMEL_MIME_PARSER_STATE_MESSAGE_END /* end of message */
79 } camel_mime_parser_state_t;
81 struct _CamelMimeParser {
83 CamelMimeParserPrivate *priv;
86 struct _CamelMimeParserClass {
87 GObjectClass parent_class;
89 void (*message) (CamelMimeParser *parser, gpointer headers);
90 void (*part) (CamelMimeParser *parser);
91 void (*content) (CamelMimeParser *parser);
94 GType camel_mime_parser_get_type (void);
95 CamelMimeParser *camel_mime_parser_new (void);
97 /* quick-fix for parser not erroring, we can find out if it had an error afterwards */
98 gint camel_mime_parser_errno (CamelMimeParser *parser);
100 gint camel_mime_parser_init_with_fd (CamelMimeParser *m, gint fd);
101 gint camel_mime_parser_init_with_stream (CamelMimeParser *m, CamelStream *stream, GError **error);
102 void camel_mime_parser_init_with_input_stream (CamelMimeParser *parser, GInputStream *input_stream);
103 void camel_mime_parser_init_with_bytes (CamelMimeParser *parser, GBytes *bytes);
105 CamelStream *camel_mime_parser_stream (CamelMimeParser *parser);
107 /* scan 'From' separators? */
108 void camel_mime_parser_scan_from (CamelMimeParser *parser, gboolean scan_from);
109 /* Do we want to know about the pre-from data? */
110 void camel_mime_parser_scan_pre_from (CamelMimeParser *parser, gboolean scan_pre_from);
112 /* what headers to save, MUST include ^Content-Type: */
113 gint camel_mime_parser_set_header_regex (CamelMimeParser *parser, gchar *matchstr);
115 /* normal interface */
116 camel_mime_parser_state_t camel_mime_parser_step (CamelMimeParser *parser, gchar **databuffer, gsize *datalength);
117 void camel_mime_parser_unstep (CamelMimeParser *parser);
118 void camel_mime_parser_drop_step (CamelMimeParser *parser);
119 camel_mime_parser_state_t camel_mime_parser_state (CamelMimeParser *parser);
120 void camel_mime_parser_push_state (CamelMimeParser *mp, camel_mime_parser_state_t newstate, const gchar *boundary);
122 /* read through the parser */
123 gint camel_mime_parser_read (CamelMimeParser *parser, const gchar **databuffer, gint len, GError **error);
125 /* get content type for the current part/header */
126 CamelContentType *camel_mime_parser_content_type (CamelMimeParser *parser);
128 /* get/change raw header by name */
129 const gchar *camel_mime_parser_header (CamelMimeParser *m, const gchar *name, gint *offset);
131 /* get all raw headers. READ ONLY! */
132 struct _camel_header_raw *camel_mime_parser_headers_raw (CamelMimeParser *m);
134 /* get multipart pre/postface */
135 const gchar *camel_mime_parser_preface (CamelMimeParser *m);
136 const gchar *camel_mime_parser_postface (CamelMimeParser *m);
138 /* return the from line content */
139 const gchar *camel_mime_parser_from_line (CamelMimeParser *m);
141 /* add a processing filter for body contents */
142 gint camel_mime_parser_filter_add (CamelMimeParser *m, CamelMimeFilter *mf);
143 void camel_mime_parser_filter_remove (CamelMimeParser *m, gint id);
145 /* these should be used with caution, because the state will not
146 * track the seeked position */
147 /* FIXME: something to bootstrap the state? */
148 goffset camel_mime_parser_tell (CamelMimeParser *parser);
149 goffset camel_mime_parser_seek (CamelMimeParser *parser, goffset offset, gint whence);
151 goffset camel_mime_parser_tell_start_headers (CamelMimeParser *parser);
152 goffset camel_mime_parser_tell_start_from (CamelMimeParser *parser);
153 goffset camel_mime_parser_tell_start_boundary (CamelMimeParser *parser);
157 #endif /* CAMEL_MIME_PARSER_H */