Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-mime-parser.h
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Copyright (C) 2000-2003 Ximian Inc.
4  *
5  *  Authors: Michael Zucchi <notzed@ximian.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22
23 #ifndef _CAMEL_MIME_PARSER_H
24 #define _CAMEL_MIME_PARSER_H
25
26 #include <camel/camel-object.h>
27
28 #include <camel/camel-mime-utils.h>
29 #include <camel/camel-mime-filter.h>
30 #include <camel/camel-stream.h>
31
32 #define CAMEL_MIME_PARSER(obj)         CAMEL_CHECK_CAST (obj, camel_mime_parser_get_type (), CamelMimeParser)
33 #define CAMEL_MIME_PARSER_CLASS(klass) CAMEL_CHECK_CLASS_CAST (klass, camel_mime_parser_get_type (), CamelMimeParserClass)
34 #define CAMEL_IS_MIME_PARSER(obj)      CAMEL_CHECK_TYPE (obj, camel_mime_parser_get_type ())
35
36 G_BEGIN_DECLS
37
38 typedef struct _CamelMimeParserClass CamelMimeParserClass;
39
40 /* NOTE: if you add more states, you may need to bump the
41    start of the END tags to 16 or 32, etc - so they are
42    the same as the matching start tag, with a bit difference */
43 typedef enum _camel_mime_parser_state_t {
44         CAMEL_MIME_PARSER_STATE_INITIAL,
45         CAMEL_MIME_PARSER_STATE_PRE_FROM,       /* data before a 'From' line */
46         CAMEL_MIME_PARSER_STATE_FROM,           /* got 'From' line */
47         CAMEL_MIME_PARSER_STATE_HEADER,         /* toplevel header */
48         CAMEL_MIME_PARSER_STATE_BODY,           /* scanning body of message */
49         CAMEL_MIME_PARSER_STATE_MULTIPART,      /* got multipart header */
50         CAMEL_MIME_PARSER_STATE_MESSAGE,        /* rfc822 message */
51         
52         CAMEL_MIME_PARSER_STATE_PART,           /* part of a multipart */
53         
54         CAMEL_MIME_PARSER_STATE_END = 8,        /* bit mask for 'end' flags */
55         
56         CAMEL_MIME_PARSER_STATE_EOF = 8,        /* end of file */
57         CAMEL_MIME_PARSER_STATE_PRE_FROM_END,   /* pre from end */
58         CAMEL_MIME_PARSER_STATE_FROM_END,       /* end of whole from bracket */
59         CAMEL_MIME_PARSER_STATE_HEADER_END,     /* dummy value */
60         CAMEL_MIME_PARSER_STATE_BODY_END,       /* end of message */
61         CAMEL_MIME_PARSER_STATE_MULTIPART_END,  /* end of multipart  */
62         CAMEL_MIME_PARSER_STATE_MESSAGE_END,    /* end of message */
63 } camel_mime_parser_state_t;
64
65 struct _CamelMimeParser {
66         CamelObject parent;
67
68         struct _CamelMimeParserPrivate *priv;
69 };
70
71 struct _CamelMimeParserClass {
72         CamelObjectClass parent_class;
73
74         void (*message) (CamelMimeParser *parser, void *headers);
75         void (*part) (CamelMimeParser *parser);
76         void (*content) (CamelMimeParser *parser);
77 };
78
79 CamelType camel_mime_parser_get_type (void);
80 CamelMimeParser *camel_mime_parser_new (void);
81
82 /* quick-fix for parser not erroring, we can find out if it had an error afterwards */
83 int             camel_mime_parser_errno (CamelMimeParser *parser);
84
85 /* using an fd will be a little faster, but not much (over a simple stream) */
86 int             camel_mime_parser_init_with_fd (CamelMimeParser *parser, int fd);
87 int             camel_mime_parser_init_with_stream (CamelMimeParser *parser, CamelStream *stream);
88
89 /* get the stream or fd back of the parser */
90 CamelStream    *camel_mime_parser_stream (CamelMimeParser *parser);
91 int             camel_mime_parser_fd (CamelMimeParser *parser);
92
93 /* scan 'From' separators? */
94 void camel_mime_parser_scan_from (CamelMimeParser *parser, gboolean scan_from);
95 /* Do we want to know about the pre-from data? */
96 void camel_mime_parser_scan_pre_from (CamelMimeParser *parser, gboolean scan_pre_from);
97
98 /* what headers to save, MUST include ^Content-Type: */
99 int camel_mime_parser_set_header_regex (CamelMimeParser *parser, char *matchstr);
100
101 /* normal interface */
102 camel_mime_parser_state_t camel_mime_parser_step (CamelMimeParser *parser, char **buf, size_t *buflen);
103 void camel_mime_parser_unstep (CamelMimeParser *parser);
104 void camel_mime_parser_drop_step (CamelMimeParser *parser);
105 camel_mime_parser_state_t camel_mime_parser_state (CamelMimeParser *parser);
106 void camel_mime_parser_push_state(CamelMimeParser *mp, camel_mime_parser_state_t newstate, const char *boundary);
107
108 /* read through the parser */
109 int camel_mime_parser_read (CamelMimeParser *parser, const char **databuffer, int len);
110
111 /* get content type for the current part/header */
112 CamelContentType *camel_mime_parser_content_type (CamelMimeParser *parser);
113
114 /* get/change raw header by name */
115 const char *camel_mime_parser_header (CamelMimeParser *parser, const char *name, int *offset);
116
117 /* get all raw headers. READ ONLY! */
118 struct _camel_header_raw *camel_mime_parser_headers_raw (CamelMimeParser *parser);
119
120 /* get multipart pre/postface */
121 const char *camel_mime_parser_preface (CamelMimeParser *parser);
122 const char *camel_mime_parser_postface (CamelMimeParser *parser);
123
124 /* return the from line content */
125 const char *camel_mime_parser_from_line (CamelMimeParser *parser);
126
127 /* add a processing filter for body contents */
128 int camel_mime_parser_filter_add (CamelMimeParser *parser, CamelMimeFilter *filter);
129 void camel_mime_parser_filter_remove (CamelMimeParser *parser, int id);
130
131 /* these should be used with caution, because the state will not
132    track the seeked position */
133 /* FIXME: something to bootstrap the state? */
134 off_t camel_mime_parser_tell (CamelMimeParser *parser);
135 off_t camel_mime_parser_seek (CamelMimeParser *parser, off_t offset, int whence);
136
137 off_t camel_mime_parser_tell_start_headers (CamelMimeParser *parser);
138 off_t camel_mime_parser_tell_start_from (CamelMimeParser *parser);
139 off_t camel_mime_parser_tell_start_boundary(CamelMimeParser *parser);
140
141 G_END_DECLS
142
143 #endif /* ! _CAMEL_MIME_PARSER_H */