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 #include <gtk/gtk.h>
29 
30 #include "utils.h"
31 #include "proctypes.h"
32 typedef enum
33 {
34 	ENC_7BIT,
35 	ENC_8BIT,
36 	ENC_BINARY,
37 	ENC_QUOTED_PRINTABLE,
38 	ENC_BASE64,
39 	ENC_X_UUENCODE,
40 	ENC_UNKNOWN
41 } EncodingType;
42 
43 typedef enum
44 {
45 	MIMETYPE_TEXT,
46 	MIMETYPE_IMAGE,
47 	MIMETYPE_AUDIO,
48 	MIMETYPE_VIDEO,
49 	MIMETYPE_APPLICATION,
50 	MIMETYPE_MESSAGE,
51 	MIMETYPE_MULTIPART,
52 	MIMETYPE_MODEL,
53 	MIMETYPE_UNKNOWN
54 } MimeMediaType;
55 
56 typedef enum
57 {
58 	DISPOSITIONTYPE_INLINE,
59 	DISPOSITIONTYPE_ATTACHMENT,
60 	DISPOSITIONTYPE_UNKNOWN
61 } DispositionType;
62 
63 typedef enum
64 {
65 	MIMECONTENT_EMPTY,
66 	MIMECONTENT_FILE,		/* the file contains all content including sub parts */
67 	MIMECONTENT_MEM
68 } MimeContent;
69 
70 #include <glib.h>
71 #include <stdio.h>
72 
73 struct _PrivacyData;
74 
75 struct _MimeType
76 {
77 	gchar *type;
78 	gchar *sub_type;
79 
80 	gchar *extension;
81 };
82 
83 struct _MimeParser
84 {
85 	MimeMediaType type;
86 	const gchar *sub_type;
87 
88 	gboolean (*parse)(MimeParser *parser, MimeInfo *mimeinfo);
89 };
90 
91 /*
92  * An example of MimeInfo structure:
93  *
94  * 1: +- message/rfc822			(root)
95  *       |
96  * 2:    +- multipart/mixed		(children of 1)
97  *          |
98  * 3:       +- multipart/alternative	(children of 2)
99  *          |  |
100  * 4:       |  +- text/plain		(children of 3)
101  *          |  |
102  * 5:       |  +- text/html		(next of 4)
103  *          |
104  * 6:       +- message/rfc822		(next of 3)
105  *          |   |
106  * 7:       |   ...			(children of 6)
107  *          |
108  * 8:       +- image/jpeg		(next of 6)
109  */
110 
111 struct _MimeInfo
112 {
113 	/* Internal data */
114 	MimeContent content;
115 	union
116 	{
117 		gchar *filename;
118 		gchar *mem;
119 	} data;
120 	gboolean tmp;
121 
122 	GNode *node;
123 
124 	/* --- NEW MIME STUFF --- */
125 	/* Content-Type */
126 	MimeMediaType 	 type;
127 	gchar		*subtype;
128 
129 	GHashTable	*typeparameters;
130 
131 	/* Content-Transfer-Encoding */
132 	EncodingType	 encoding_type;
133 
134 	/* Content-Description */
135 	gchar		*description;
136 
137 	/* Content-ID */
138 	gchar		*id;
139 
140 	/* Content-Location */
141 	gchar 		*location;
142 
143 	guint		 offset;
144 	guint		 length;
145 
146 	/* Content-Disposition */
147 	DispositionType	 disposition;
148 	GHashTable	*dispositionparameters;
149 
150 	/* Privacy */
151 	struct _PrivacyData	*privacy;
152 
153 	gboolean	 broken;
154 };
155 
156 #define IS_BOUNDARY(s, bnd, len) \
157 	(bnd && s[0] == '-' && s[1] == '-' && !strncmp(s + 2, bnd, len))
158 
159 #ifdef __cplusplus
160 extern "C" {
161 #endif /* __cplusplus */
162 
163 /* MimeInfo handling */
164 
165 MimeInfo *procmime_mimeinfo_new		(void);
166 void procmime_mimeinfo_free_all		(MimeInfo	**mimeinfo_ptr);
167 
168 MimeInfo *procmime_mimeinfo_insert	(MimeInfo	*parent,
169 					 MimeInfo	*mimeinfo);
170 void procmime_mimeinfo_replace		(MimeInfo	*old_mimeinfo,
171 					 MimeInfo	*new_mimeinfo);
172 
173 MimeInfo *procmime_mimeinfo_parent	(MimeInfo	*mimeinfo);
174 MimeInfo *procmime_mimeinfo_next	(MimeInfo	*mimeinfo);
175 
176 MimeInfo *procmime_scan_message		(MsgInfo	*msginfo);
177 MimeInfo *procmime_scan_message_short	(MsgInfo	*msginfo);
178 void procmime_scan_multipart_message	(MimeInfo	*mimeinfo,
179 					 FILE		*fp);
180 const gchar *procmime_mimeinfo_get_parameter
181 					(MimeInfo	*mimeinfo,
182 					 const gchar	*name);
183 
184 /* scan headers */
185 
186 void procmime_scan_encoding		(MimeInfo	*mimeinfo,
187 					 const gchar	*encoding);
188 void procmime_scan_content_type		(MimeInfo	*mimeinfo,
189 					 const gchar	*content_type);
190 void procmime_scan_content_disposition	(MimeInfo	*mimeinfo,
191 					 const gchar	*content_disposition);
192 void procmime_scan_content_description	(MimeInfo	*mimeinfo,
193 					 const gchar	*content_description);
194 void procmime_scan_subject              (MimeInfo       *mimeinfo,
195 			                 const gchar    *subject);
196 MimeInfo *procmime_scan_mime_header	(FILE		*fp);
197 
198 gboolean procmime_decode_content	(MimeInfo	*mimeinfo);
199 gboolean procmime_encode_content	(MimeInfo	*mimeinfo, EncodingType encoding);
200 gint procmime_get_part			(const gchar	*outfile,
201 					 MimeInfo	*mimeinfo);
202 FILE *procmime_get_first_text_content	(MsgInfo	*msginfo);
203 FILE *procmime_get_first_encrypted_text_content
204 					(MsgInfo 	*msginfo);
205 
206 gchar *procmime_get_tmp_file_name	(MimeInfo	*mimeinfo);
207 gchar *procmime_get_part_file_name	(MimeInfo 	*mimeinfo);
208 
209 gchar *procmime_get_mime_type		(const gchar	*filename);
210 
211 GList *procmime_get_mime_type_list	(void);
212 
213 EncodingType procmime_get_encoding_for_charset	(const gchar	*charset);
214 EncodingType procmime_get_encoding_for_text_file(const gchar	*file,
215 						 gboolean	*has_binary);
216 const gchar *procmime_get_encoding_str		(EncodingType	 encoding);
217 MimeInfo *procmime_scan_file			(const gchar	*filename);
218 MimeInfo *procmime_scan_queue_file		(const gchar 	*filename);
219 const gchar *procmime_get_media_type_str	(MimeMediaType 	 type);
220 MimeMediaType procmime_get_media_type		(const gchar 	*str);
221 gchar *procmime_get_content_type_str		(MimeMediaType   type,
222 						 const gchar	*subtype);
223 void procmime_force_charset			(const gchar 	*str);
224 void procmime_force_encoding			(EncodingType	 encoding);
225 gboolean procmime_msginfo_is_encrypted		(MsgInfo 	*msginfo);
226 int procmime_write_mime_header			(MimeInfo 	*mimeinfo,
227 						 FILE 		*fp);
228 void renderer_read_config(void);
229 
230 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp);
231 
232 void procmime_mimeparser_register(MimeParser *mimeparser);
233 void procmime_mimeparser_unregister(MimeParser *mimeparser);
234 FILE *procmime_get_text_content(MimeInfo *mimeinfo);
235 FILE *procmime_get_binary_content(MimeInfo *mimeinfo);
236 
237 /* scans mimeinfo contents, calling scan_callback() once per line.
238  * return TRUE and scan is aborted if scan_callback returns TRUE.
239  * return TRUE on error.
240  * return FALSE if scan completed and scan_callback never returned TRUE.
241  */
242 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
243 		gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
244 		gpointer cb_data);
245 void *procmime_get_part_as_string(MimeInfo *mimeinfo,
246 		gboolean null_terminate);
247 GInputStream *procmime_get_part_as_inputstream(MimeInfo *mimeinfo);
248 GdkPixbuf *procmime_get_part_as_pixbuf(MimeInfo *mimeinfo, GError **error);
249 
250 #ifdef __cplusplus
251 }
252 #endif /* __cplusplus */
253 
254 #endif /* __PROCMIME_H__ */
255