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