b21dc940772a96f7ef14a6c7d0b1d05d416f268d
[claws.git] / src / procmime.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifndef __PROCMIME_H__
21 #define __PROCMIME_H__
22
23 #ifdef HAVE_CONFIG_H
24 #include "claws-features.h"
25 #endif
26
27 #include <gio/gio.h>
28
29 #include "utils.h"
30 #include "proctypes.h"
31 typedef enum
32 {
33         ENC_7BIT,
34         ENC_8BIT,
35         ENC_BINARY,
36         ENC_QUOTED_PRINTABLE,
37         ENC_BASE64,
38         ENC_X_UUENCODE,
39         ENC_UNKNOWN
40 } EncodingType;
41
42 typedef enum
43 {
44         MIMETYPE_TEXT,
45         MIMETYPE_IMAGE,
46         MIMETYPE_AUDIO,
47         MIMETYPE_VIDEO,
48         MIMETYPE_APPLICATION,
49         MIMETYPE_MESSAGE,
50         MIMETYPE_MULTIPART,
51         MIMETYPE_UNKNOWN
52 } MimeMediaType;
53
54 typedef enum
55 {
56         DISPOSITIONTYPE_INLINE,
57         DISPOSITIONTYPE_ATTACHMENT,
58         DISPOSITIONTYPE_UNKNOWN
59 } DispositionType;
60
61 typedef enum
62 {
63         MIMECONTENT_EMPTY,
64         MIMECONTENT_FILE,               /* the file contains all content including sub parts */
65         MIMECONTENT_MEM
66 } MimeContent;
67
68 #include <glib.h>
69 #include <stdio.h>
70
71 struct _PrivacyData;
72
73 struct _MimeType
74 {
75         gchar *type;
76         gchar *sub_type;
77
78         gchar *extension;
79 };
80
81 struct _MimeParser
82 {
83         MimeMediaType type;
84         const gchar *sub_type;
85
86         gboolean (*parse)(MimeParser *parser, MimeInfo *mimeinfo);
87 };
88
89 /*
90  * An example of MimeInfo structure:
91  *
92  * 1: +- message/rfc822                 (root)
93  *       |
94  * 2:    +- multipart/mixed             (children of 1)
95  *          |
96  * 3:       +- multipart/alternative    (children of 2)
97  *          |  |
98  * 4:       |  +- text/plain            (children of 3)
99  *          |  |
100  * 5:       |  +- text/html             (next of 4)
101  *          |
102  * 6:       +- message/rfc822           (next of 3)
103  *          |   |
104  * 7:       |   ...                     (children of 6)
105  *          |
106  * 8:       +- image/jpeg               (next of 6)
107  */
108
109 struct _MimeInfo
110 {
111         /* Internal data */
112         MimeContent content;
113         union
114         {
115                 gchar *filename;
116                 gchar *mem;
117         } data;
118         gboolean tmp;
119
120         GNode *node;
121
122         /* --- NEW MIME STUFF --- */
123         /* Content-Type */
124         MimeMediaType    type;
125         gchar           *subtype;
126
127         GHashTable      *typeparameters;
128
129         /* Content-Transfer-Encoding */
130         EncodingType     encoding_type;
131
132         /* Content-Description */
133         gchar           *description;
134
135         /* Content-ID */
136         gchar           *id;
137
138         /* Content-Location */
139         gchar           *location;
140
141         guint            offset;
142         guint            length;
143
144         /* Content-Disposition */
145         DispositionType  disposition;
146         GHashTable      *dispositionparameters;
147
148         /* Privacy */
149         struct _PrivacyData     *privacy;
150
151         gboolean         broken;
152 };
153
154 #define IS_BOUNDARY(s, bnd, len) \
155         (bnd && s[0] == '-' && s[1] == '-' && !strncmp(s + 2, bnd, len))
156
157 #ifdef __cplusplus
158 extern "C" {
159 #endif /* __cplusplus */
160
161 /* MimeInfo handling */
162
163 MimeInfo *procmime_mimeinfo_new         (void);
164 void procmime_mimeinfo_free_all         (MimeInfo       **mimeinfo_ptr);
165
166 MimeInfo *procmime_mimeinfo_insert      (MimeInfo       *parent,
167                                          MimeInfo       *mimeinfo);
168 void procmime_mimeinfo_replace          (MimeInfo       *old_mimeinfo,
169                                          MimeInfo       *new_mimeinfo);
170
171 MimeInfo *procmime_mimeinfo_parent      (MimeInfo       *mimeinfo);
172 MimeInfo *procmime_mimeinfo_next        (MimeInfo       *mimeinfo);
173
174 MimeInfo *procmime_scan_message         (MsgInfo        *msginfo);
175 MimeInfo *procmime_scan_message_short   (MsgInfo        *msginfo);
176 void procmime_scan_multipart_message    (MimeInfo       *mimeinfo,
177                                          FILE           *fp);
178 const gchar *procmime_mimeinfo_get_parameter
179                                         (MimeInfo       *mimeinfo,
180                                          const gchar    *name);
181
182 /* scan headers */
183
184 void procmime_scan_encoding             (MimeInfo       *mimeinfo,
185                                          const gchar    *encoding);
186 void procmime_scan_content_type         (MimeInfo       *mimeinfo,
187                                          const gchar    *content_type);
188 void procmime_scan_content_disposition  (MimeInfo       *mimeinfo,
189                                          const gchar    *content_disposition);
190 void procmime_scan_content_description  (MimeInfo       *mimeinfo,
191                                          const gchar    *content_description);
192 void procmime_scan_subject              (MimeInfo       *mimeinfo,
193                                          const gchar    *subject);
194 MimeInfo *procmime_scan_mime_header     (FILE           *fp);
195
196 gboolean procmime_decode_content        (MimeInfo       *mimeinfo);
197 gboolean procmime_encode_content        (MimeInfo       *mimeinfo, EncodingType encoding);
198 gint procmime_get_part                  (const gchar    *outfile,
199                                          MimeInfo       *mimeinfo);
200 FILE *procmime_get_first_text_content   (MsgInfo        *msginfo);
201 FILE *procmime_get_first_encrypted_text_content
202                                         (MsgInfo        *msginfo);
203
204 gchar *procmime_get_tmp_file_name       (MimeInfo       *mimeinfo);
205 gchar *procmime_get_part_file_name      (MimeInfo       *mimeinfo);
206
207 gchar *procmime_get_mime_type           (const gchar    *filename);
208
209 GList *procmime_get_mime_type_list      (void);
210
211 EncodingType procmime_get_encoding_for_charset  (const gchar    *charset);
212 EncodingType procmime_get_encoding_for_text_file(const gchar    *file,
213                                                  gboolean       *has_binary);
214 const gchar *procmime_get_encoding_str          (EncodingType    encoding);
215 MimeInfo *procmime_scan_file                    (const gchar    *filename);
216 MimeInfo *procmime_scan_queue_file              (const gchar    *filename);
217 const gchar *procmime_get_media_type_str        (MimeMediaType   type);
218 MimeMediaType procmime_get_media_type           (const gchar    *str);
219 gchar *procmime_get_content_type_str            (MimeMediaType   type,
220                                                  const gchar    *subtype);
221 void procmime_force_charset                     (const gchar    *str);
222 void procmime_force_encoding                    (EncodingType    encoding);
223 gboolean procmime_msginfo_is_encrypted          (MsgInfo        *msginfo);
224 int procmime_write_mime_header                  (MimeInfo       *mimeinfo, 
225                                                  FILE           *fp);
226 void renderer_read_config(void);
227
228 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp);
229
230 void procmime_mimeparser_register(MimeParser *mimeparser);
231 void procmime_mimeparser_unregister(MimeParser *mimeparser);
232 FILE *procmime_get_text_content(MimeInfo *mimeinfo);
233 FILE *procmime_get_binary_content(MimeInfo *mimeinfo);
234
235 /* scans mimeinfo contents, calling scan_callback() once per line.
236  * return TRUE and scan is aborted if scan_callback returns TRUE.
237  * return TRUE on error.
238  * return FALSE if scan completed and scan_callback never returned TRUE.
239  */
240 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
241                 gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
242                 gpointer cb_data);
243 void *procmime_get_part_as_string(MimeInfo *mimeinfo,
244                 gboolean null_terminate);
245 GInputStream *procmime_get_part_as_inputstream(MimeInfo *mimeinfo,
246                 GError **error);
247
248 #ifdef __cplusplus
249 }
250 #endif /* __cplusplus */
251
252 #endif /* __PROCMIME_H__ */