]> diplodocus.org Git - nmh/blob - h/mhparse.h
4052cc3c066cf2fc9f388de66ba14ddfb2653219
[nmh] / h / mhparse.h
1 /* mhparse.h -- definitions for parsing/building of MIME content
2 * -- (mhparse.c/mhbuildsbr.c)
3 */
4
5 /*
6 * Abstract type for header fields
7 */
8 typedef struct hfield *HF;
9
10 /*
11 * Abstract types for MIME parsing/building
12 */
13 typedef struct cefile *CE;
14 typedef struct CTinfo *CI;
15 typedef struct Content *CT;
16 typedef struct Parameter *PM;
17
18 /*
19 * type for Init function (both type and transfer encoding)
20 */
21 typedef int (*InitFunc) (CT);
22
23 /*
24 * types for various transfer encoding access functions
25 */
26 typedef int (*OpenCEFunc) (CT, char **);
27 typedef void (*CloseCEFunc) (CT);
28 typedef unsigned long (*SizeCEFunc) (CT);
29
30 /*
31 * Structure for storing/encoding/decoding
32 * a header field and its value.
33 */
34 struct hfield {
35 char *name; /* field name */
36 char *value; /* field body */
37 HF next; /* link to next header field */
38 };
39
40 /*
41 * Structure for holding MIME parameter elements.
42 */
43 struct Parameter {
44 char *pm_name; /* Parameter name */
45 char *pm_value; /* Parameter value */
46 char *pm_charset; /* Parameter character set (optional) */
47 char *pm_lang; /* Parameter language tag (optional) */
48 PM pm_next; /* Pointer to next element */
49 };
50
51 /*
52 * Structure for storing parsed elements
53 * of the Content-Type component.
54 */
55 struct CTinfo {
56 char *ci_type; /* content type */
57 char *ci_subtype; /* content subtype */
58 PM ci_first_pm; /* Pointer to first MIME parameter */
59 PM ci_last_pm; /* Pointer to last MIME parameter */
60 char *ci_comment; /* RFC-822 comments */
61 char *ci_magic;
62 };
63
64 /*
65 * Structure for storing decoded contents after
66 * removing Content-Transfer-Encoding.
67 */
68 struct cefile {
69 char *ce_file; /* decoded content (file) */
70 FILE *ce_fp; /* decoded content (stream) */
71 int ce_unlink; /* remove file when done? */
72 };
73
74 /*
75 * Primary structure for handling Content (Entity)
76 *
77 * Some more explanation of this:
78 *
79 * This structure recursively describes a complete MIME message.
80 * At the top level, the c_first_hf list has a list of all message
81 * headers. If the content-type is multipart (c_type == CT_MULTIPART)
82 * then c_ctparams will contain a pointer to a struct multipart.
83 * A struct multipart contains (among other things) a linked list
84 * of struct part elements, and THOSE contain a pointer to the sub-part's
85 * Content structure.
86 *
87 * An extra note for message/external-body parts. The enclosing
88 * content structure is marked as a message/external-body; the c_ctparams
89 * contains a pointer to a struct exbody, which contains a pointer to
90 * (among other things) the "real" content (e.g., application/octet-stream).
91 * The "real" content structure has the c_ctexbody pointer back to the
92 * same struct exbody sees in the enclosing content structure (the struct
93 * exbody contains parent pointers if you need to traverse up the content
94 * structure). Hopefully this makes it clearer:
95 *
96 * Enclosing content:
97 * Type: message/external-body
98 * c_ctparams: pointer to "struct exbody"
99 * c_ctexbody: NULL
100 *
101 * "Real" content:
102 * Type: application/octet-stream (or whatever)
103 * c_ctparams: NULL
104 * c_ctexbody: pointer to "struct exbody"
105 *
106 */
107 struct Content {
108 /* source (read) file */
109 char *c_file; /* read contents (file) */
110 FILE *c_fp; /* read contents (stream) */
111 int c_unlink; /* remove file when done? */
112
113 long c_begin; /* where content body starts in file */
114 long c_end; /* where content body ends in file */
115
116 /* linked list of header fields */
117 HF c_first_hf; /* pointer to first header field */
118 HF c_last_hf; /* pointer to last header field */
119
120 /* copies of MIME related header fields */
121 char *c_vrsn; /* MIME-Version: */
122 char *c_ctline; /* Content-Type: */
123 char *c_celine; /* Content-Transfer-Encoding: */
124 char *c_id; /* Content-ID: */
125 char *c_descr; /* Content-Description: */
126 char *c_dispo; /* Content-Disposition: */
127 char *c_dispo_type; /* Type of Content-Disposition */
128 PM c_dispo_first; /* Pointer to first disposition parm */
129 PM c_dispo_last; /* Pointer to last disposition parm */
130 char *c_partno; /* within multipart content */
131
132 /* Content-Type info */
133 struct CTinfo c_ctinfo; /* parsed elements of Content-Type */
134 int c_type; /* internal flag for content type */
135 int c_subtype; /* internal flag for content subtype */
136
137 /* Content-Transfer-Encoding info (decoded contents) */
138 struct cefile c_cefile; /* structure holding decoded content */
139 int c_encoding; /* internal flag for encoding type */
140 int c_reqencoding; /* Requested encoding (by mhbuild) */
141
142 /* pointers to content-specific structures */
143 void *c_ctparams; /* content type specific data */
144 struct exbody *c_ctexbody; /* data for type message/external */
145
146 /* function pointers */
147 InitFunc c_ctinitfnx; /* parse content body */
148 OpenCEFunc c_ceopenfnx; /* get a stream to decoded contents */
149 CloseCEFunc c_ceclosefnx; /* release stream */
150 SizeCEFunc c_cesizefnx; /* size of decoded contents */
151
152 int c_umask; /* associated umask */
153 int c_rfc934; /* RFC 934 compatibility flag */
154
155 char *c_showproc; /* default, if not in profile */
156 char *c_termproc; /* for charset madness... */
157 char *c_storeproc; /* overrides profile entry, if any */
158
159 char *c_storage; /* write contents (file) */
160 char *c_folder; /* write contents (folder) */
161 };
162
163 /*
164 * Flags for Content-Type (Content->c_type)
165 */
166 #define CT_UNKNOWN 0x00
167 #define CT_APPLICATION 0x01
168 #define CT_AUDIO 0x02
169 #define CT_IMAGE 0x03
170 #define CT_MESSAGE 0x04
171 #define CT_MULTIPART 0x05
172 #define CT_TEXT 0x06
173 #define CT_VIDEO 0x07
174 #define CT_EXTENSION 0x08
175
176 /*
177 * Flags for Content-Transfer-Encoding (Content->c_encoding)
178 */
179 #define CE_UNKNOWN 0x00
180 #define CE_BASE64 0x01
181 #define CE_QUOTED 0x02
182 #define CE_8BIT 0x03
183 #define CE_7BIT 0x04
184 #define CE_BINARY 0x05
185 #define CE_EXTENSION 0x06
186 #define CE_EXTERNAL 0x07 /* for external-body */
187
188 /*
189 * TEXT content
190 */
191
192 /* Flags for subtypes of TEXT */
193 #define TEXT_UNKNOWN 0x00
194 #define TEXT_PLAIN 0x01
195 #define TEXT_RICHTEXT 0x02
196 #define TEXT_ENRICHED 0x03
197
198 /* Flags for character sets */
199 #define CHARSET_SPECIFIED 0x00
200 #define CHARSET_UNSPECIFIED 0x01 /* only needed when building drafts */
201
202 /* Structure for text content */
203 struct text {
204 int tx_charset; /* flag for character set */
205 int lf_line_endings; /* Whether to use CR LF (0) or LF (1) line
206 endings. The meaning of 0 was selected so
207 that CR LF is the default, in accordance
208 with RFC 2046, Sec. 4.1.1, Par. 1. */
209 };
210
211 /*
212 * MULTIPART content
213 */
214
215 /* Flags for subtypes of MULTIPART */
216 #define MULTI_UNKNOWN 0x00
217 #define MULTI_MIXED 0x01
218 #define MULTI_ALTERNATE 0x02
219 #define MULTI_DIGEST 0x03
220 #define MULTI_PARALLEL 0x04
221 #define MULTI_RELATED 0x05
222
223 /* Structure for subparts of a multipart content */
224 struct part {
225 CT mp_part; /* Content structure for subpart */
226 struct part *mp_next; /* pointer to next subpart structure */
227 };
228
229 /* Main structure for multipart content */
230 struct multipart {
231 char *mp_start; /* boundary string separating parts */
232 char *mp_stop; /* terminating boundary string */
233 char *mp_content_before; /* any content before the first subpart */
234 char *mp_content_after; /* any content after the last subpart */
235 struct part *mp_parts; /* pointer to first subpart structure */
236 };
237
238 /*
239 * MESSAGE content
240 */
241
242 /* Flags for subtypes of MESSAGE */
243 #define MESSAGE_UNKNOWN 0x00
244 #define MESSAGE_RFC822 0x01
245 #define MESSAGE_PARTIAL 0x02
246 #define MESSAGE_EXTERNAL 0x03
247
248 /* Structure for message/partial */
249 struct partial {
250 char *pm_partid;
251 int pm_partno;
252 int pm_maxno;
253 int pm_marked;
254 int pm_stored;
255 };
256
257 /* Structure for message/external */
258 struct exbody {
259 CT eb_parent; /* pointer to controlling content structure */
260 CT eb_content; /* pointer to internal content structure */
261 char *eb_partno;
262 char *eb_access;
263 int eb_flags;
264 char *eb_name;
265 char *eb_permission;
266 char *eb_site;
267 char *eb_dir;
268 char *eb_mode;
269 unsigned long eb_size;
270 char *eb_server;
271 char *eb_subject;
272 char *eb_body;
273 char *eb_url;
274 };
275
276 /*
277 * APPLICATION content
278 */
279
280 /* Flags for subtype of APPLICATION */
281 #define APPLICATION_UNKNOWN 0x00
282 #define APPLICATION_OCTETS 0x01
283 #define APPLICATION_POSTSCRIPT 0x02
284
285
286 /*
287 * Structures for mapping types to their internal flags
288 */
289 struct k2v {
290 char *kv_key;
291 int kv_value;
292 };
293 extern struct k2v SubText[];
294 extern struct k2v Charset[];
295 extern struct k2v SubMultiPart[];
296 extern struct k2v SubMessage[];
297 extern struct k2v SubApplication[];
298
299 /*
300 * Structures for mapping (content) types to
301 * the functions to handle them.
302 */
303 struct str2init {
304 char *si_key;
305 int si_val;
306 InitFunc si_init;
307 };
308 extern struct str2init str2cts[];
309 extern struct str2init str2ces[];
310 extern struct str2init str2methods[];
311
312 /*
313 * prototypes
314 */
315 CT parse_mime (char *);
316
317 /*
318 * Translate a composition file into a MIME data structure. Arguments are:
319 *
320 * infile - Name of input filename
321 * autobuild - A flag to indicate if the composition file parser is
322 * being run in automatic mode or not. In auto mode,
323 * if a MIME-Version header is encountered it is assumed
324 * that the composition file is already in MIME format
325 * and will not be processed further. Otherwise, an
326 * error is generated.
327 * dist - A flag to indicate if we are being run by "dist". In
328 * that case, add no MIME headers to the message. Existing
329 * headers will still be encoded by RFC 2047.
330 * directives - A flag to control whether or not build directives are
331 * processed by default.
332 * encoding - The default encoding to use when doing RFC 2047 header
333 * encoding. Must be one of CE_UNKNOWN, CE_BASE64, or
334 * CE_QUOTED.
335 * maxunencoded - The maximum line length before the default encoding for
336 * text parts is quoted-printable.
337 * verbose - If 1, output verbose information during message composition
338 *
339 * Returns a CT structure describing the resulting MIME message. If the
340 * -auto flag is set and a MIME-Version header is encountered, the return
341 * value is NULL.
342 */
343 CT build_mime (char *infile, int autobuild, int dist, int directives,
344 int encoding, size_t maxunencoded, int verbose);
345
346 int add_header (CT, char *, char *);
347 int get_ctinfo (char *, CT, int);
348 int params_external (CT, int);
349 int open7Bit (CT, char **);
350 void close_encoding (CT);
351 char *ct_type_str (int) CONST;
352 char *ct_subtype_str (int, int) CONST;
353 int ct_str_type (const char *) PURE;
354 int ct_str_subtype (int, const char *) PURE;
355 const struct str2init *get_ct_init (int) PURE;
356 const char *ce_str (int) CONST;
357 const struct str2init *get_ce_method (const char *) PURE;
358 char *content_charset (CT);
359 void reverse_alternative_parts (CT);
360
361 /*
362 * Given a list of messages, display information about them on standard
363 * output.
364 *
365 * Arguments are:
366 *
367 * cts - An array of CT elements of messages that need to be
368 * displayed. Array is terminated by a NULL.
369 * headsw - If 1, display a column header.
370 * sizesw - If 1, display the size of the part.
371 * verbosw - If 1, display verbose information
372 * debugsw - If 1, turn on debugging for the output.
373 * disposw - If 1, display MIME part disposition information.
374 *
375 */
376 void list_all_messages(CT *cts, int headsw, int sizesw, int verbosw,
377 int debugsw, int disposw);
378
379 /*
380 * List the content information of a single MIME part on stdout.
381 *
382 * Arguments are:
383 *
384 * ct - MIME Content structure to display.
385 * toplevel - If set, we're at the top level of a message
386 * realsize - If set, determine the real size of the content
387 * verbose - If set, output verbose information
388 * debug - If set, turn on debugging for the output
389 * dispo - If set, display MIME part disposition information.
390 *
391 * Returns OK on success, NOTOK otherwise.
392 */
393 int list_content(CT ct, int toplevel, int realsize, int verbose, int debug,
394 int dispo);
395
396 /*
397 * Display content-appropriate information on MIME parts, descending recursively
398 * into multipart content if appropriate. Uses list_content() for displaying
399 * generic information.
400 *
401 * Arguments and return value are the same as list_content().
402 */
403 int list_switch(CT ct, int toplevel, int realsize, int verbose, int debug,
404 int dispo);
405
406 /*
407 * Given a linked list of parameters, build an output string for them. This
408 * string is designed to be concatenated on an already-built header.
409 *
410 * Arguments are:
411 *
412 * initialwidth - Current width of the header. Used to compute when to wrap
413 * parameters on the first line. The following lines will
414 * be prefixed by a tab (\t) character.
415 * params - Pointer to head of linked list of parameters.
416 * offsetout - The final line offset after all the parameters have been
417 * output. May be NULL.
418 * external - If set, outputting an external-body type and will not
419 * output a "body" parameter.
420
421 * Returns a pointer to the resulting parameter string. This string must
422 * be free()'d by the caller. Returns NULL on error.
423 */
424 char *output_params(size_t initialwidth, PM params, int *offsetout,
425 int external);
426
427 /*
428 * Encode a parameter value using RFC 2231 encode.
429 *
430 * Arguments are:
431 *
432 * pm - PM containing the parameter value and related info.
433 * output - Output buffer.
434 * len - Size, in octets, of output buffer.
435 * valuelen - Number of characters in the value
436 * valueoff - Offset into value field (pm->pm_value).
437 * index - If 0, output character set and language tag.
438 */
439 size_t encode_param(PM pm, char *output, size_t len, size_t valuelen,
440 size_t valueoff, int index);
441
442 /*
443 * Add a parameter to the parameter linked list.
444 *
445 * Arguments are:
446 *
447 * first - Pointer to head of linked list
448 * last - Pointer to tail of linked list
449 * name - Name of parameter
450 * value - Value of parameter
451 * nocopy - If set, will use the pointer values directly for "name"
452 * and "value" instead of making their own copy. These
453 * pointers will be free()'d later by the MIME routines, so
454 * they should not be used after calling this function!
455 *
456 * Returns allocated parameter element
457 */
458 PM add_param(PM *first, PM *last, char *name, char *value, int nocopy);
459
460 /*
461 * Replace (or add) a parameter to the parameter linked list.
462 *
463 * If the named parameter already exists on the parameter linked list,
464 * replace the value with the new one. Otherwise add it to the linked
465 * list. All parameters are identical to add_param().
466 */
467 PM replace_param(PM *first, PM *last, char *name, char *value, int nocopy);
468
469 /*
470 * Retrieve a parameter value from a parameter linked list. Convert to the
471 * local character set if required.
472 *
473 * Arguments are:
474 *
475 * first - Pointer to head of parameter linked list.
476 * name - Name of parameter.
477 * replace - If characters in the parameter list cannot be converted to
478 * the local character set, replace with this character.
479 * fetchonly - If true, return pointer to original value, no conversion
480 * performed.
481 *
482 * Returns parameter value if found, NULL otherwise. Memory must be free()'d
483 * unless fetchonly is set.
484 */
485
486 char *get_param(PM first, const char *name, char replace, int fetchonly);
487
488 /*
489 * Fetch a parameter value from a parameter structure, converting it to
490 * the local character set.
491 *
492 * Arguments are:
493 *
494 * pm - Pointer to parameter structure
495 * replace - If characters in the parameter list cannot be converted to
496 * the local character set, replace with this character.
497 *
498 * Returns a pointer to the parameter value. Memory is stored in an
499 * internal buffer, so the returned value is only valid until the next
500 * call to get_param_value() or get_param() (get_param() uses get_param_value()
501 * internally).
502 */
503 char *get_param_value(PM pm, char replace);
504
505 /*
506 * mhstore
507 * Put it here because it uses the CT typedef.
508 */
509 typedef struct mhstoreinfo *mhstoreinfo_t;
510 mhstoreinfo_t mhstoreinfo_create(CT *, char *, const char *, int, int);
511 int mhstoreinfo_files_not_clobbered(const mhstoreinfo_t) PURE;
512 void mhstoreinfo_free(mhstoreinfo_t);
513 void store_all_messages (mhstoreinfo_t);
514
515 extern bool skip_mp_cte_check;
516 extern bool suppress_bogus_mp_content_warning;
517 extern bool bogus_mp_content;
518 extern bool suppress_extraneous_trailing_semicolon_warning;
519
520 extern bool suppress_multiple_mime_version_warning;
521
522 #define NPREFS 20
523 typedef struct {
524 char *type;
525 char *subtype;
526 } mime_type_subtype;
527 extern mime_type_subtype mime_preference[NPREFS];
528 extern int npreferred;