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