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