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