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