]>
diplodocus.org Git - nmh/blob - h/mhparse.h
3 * mhparse.h -- definitions for parsing/building of MIME content
4 * -- (mhparse.c/mhbuildsbr.c)
12 * Abstract type for header fields
14 typedef struct hfield
*HF
;
17 * Abstract types for MIME parsing/building
19 typedef struct cefile
*CE
;
20 typedef struct CTinfo
*CI
;
21 typedef struct Content
*CT
;
22 typedef struct Parameter
*PM
;
25 * type for Init function (both type and transfer encoding)
27 typedef int (*InitFunc
) (CT
);
30 * types for various transfer encoding access functions
32 typedef int (*OpenCEFunc
) (CT
, char **);
33 typedef void (*CloseCEFunc
) (CT
);
34 typedef unsigned long (*SizeCEFunc
) (CT
);
37 * Structure for storing/encoding/decoding
38 * a header field and its value.
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 */
48 * Structure for holding MIME parameter elements.
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 */
59 * Structure for storing parsed elements
60 * of the Content-Type component.
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 */
72 * Structure for storing decoded contents after
73 * removing Content-Transfer-Encoding.
76 char *ce_file
; /* decoded content (file) */
77 FILE *ce_fp
; /* decoded content (stream) */
78 int ce_unlink
; /* remove file when done? */
82 * Primary structure for handling Content (Entity)
84 * Some more explanation of this:
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
94 * An extra note for message/external-body parts. The enclosing
95 * content structure is marked as a message/external-body; the c_ctparams
96 * contains a pointer to a struct exbody, which contains a pointer to
97 * (among other things) the "real" content (e.g., application/octet-stream).
98 * The "real" content structure has the c_ctexbody pointer back to the
99 * same struct exbody sees in the enclosing content structure (the struct
100 * exbody contains parent pointers if you need to traverse up the content
101 * structure). Hopefully this makes it clearer:
104 * Type: message/external-body
105 * c_ctparams: pointer to "struct exbody"
109 * Type: application/octet-stream (or whatever)
111 * c_ctexbody: pointer to "struct exbody"
115 /* source (read) file */
116 char *c_file
; /* read contents (file) */
117 FILE *c_fp
; /* read contents (stream) */
118 int c_unlink
; /* remove file when done? */
120 long c_begin
; /* where content body starts in file */
121 long c_end
; /* where content body ends in file */
123 /* linked list of header fields */
124 HF c_first_hf
; /* pointer to first header field */
125 HF c_last_hf
; /* pointer to last header field */
127 /* copies of MIME related header fields */
128 char *c_vrsn
; /* MIME-Version: */
129 char *c_ctline
; /* Content-Type: */
130 char *c_celine
; /* Content-Transfer-Encoding: */
131 char *c_id
; /* Content-ID: */
132 char *c_descr
; /* Content-Description: */
133 char *c_dispo
; /* Content-Disposition: */
134 char *c_dispo_type
; /* Type of Content-Disposition */
135 PM c_dispo_first
; /* Pointer to first disposition parm */
136 PM c_dispo_last
; /* Pointer to last disposition parm */
137 char *c_partno
; /* within multipart content */
139 /* Content-Type info */
140 struct CTinfo c_ctinfo
; /* parsed elements of Content-Type */
141 int c_type
; /* internal flag for content type */
142 int c_subtype
; /* internal flag for content subtype */
144 /* Content-Transfer-Encoding info (decoded contents) */
145 struct cefile c_cefile
; /* structure holding decoded content */
146 int c_encoding
; /* internal flag for encoding type */
147 int c_reqencoding
; /* Requested encoding (by mhbuild) */
149 /* Content-MD5 info */
150 int c_digested
; /* have we seen this header before? */
151 unsigned char c_digest
[16]; /* decoded MD5 checksum */
153 /* pointers to content-specific structures */
154 void *c_ctparams
; /* content type specific data */
155 struct exbody
*c_ctexbody
; /* data for type message/external */
157 /* function pointers */
158 InitFunc c_ctinitfnx
; /* parse content body */
159 OpenCEFunc c_ceopenfnx
; /* get a stream to decoded contents */
160 CloseCEFunc c_ceclosefnx
; /* release stream */
161 SizeCEFunc c_cesizefnx
; /* size of decoded contents */
163 int c_umask
; /* associated umask */
164 int c_rfc934
; /* rfc934 compatibility flag */
166 char *c_showproc
; /* default, if not in profile */
167 char *c_termproc
; /* for charset madness... */
168 char *c_storeproc
; /* overrides profile entry, if any */
170 char *c_storage
; /* write contents (file) */
171 char *c_folder
; /* write contents (folder) */
175 * Flags for Content-Type (Content->c_type)
177 #define CT_UNKNOWN 0x00
178 #define CT_APPLICATION 0x01
179 #define CT_AUDIO 0x02
180 #define CT_IMAGE 0x03
181 #define CT_MESSAGE 0x04
182 #define CT_MULTIPART 0x05
184 #define CT_VIDEO 0x07
185 #define CT_EXTENSION 0x08
188 * Flags for Content-Transfer-Encoding (Content->c_encoding)
190 #define CE_UNKNOWN 0x00
191 #define CE_BASE64 0x01
192 #define CE_QUOTED 0x02
195 #define CE_BINARY 0x05
196 #define CE_EXTENSION 0x06
197 #define CE_EXTERNAL 0x07 /* for external-body */
203 /* Flags for subtypes of TEXT */
204 #define TEXT_UNKNOWN 0x00
205 #define TEXT_PLAIN 0x01
206 #define TEXT_RICHTEXT 0x02
207 #define TEXT_ENRICHED 0x03
209 /* Flags for character sets */
210 #define CHARSET_SPECIFIED 0x00
211 #define CHARSET_UNSPECIFIED 0x01 /* only needed when building drafts */
213 /* Structure for text content */
215 int tx_charset
; /* flag for character set */
216 int lf_line_endings
; /* Whether to use CR LF (0) or LF (1) line
217 endings. The meaning of 0 was selected so
218 that CR LF is the default, in accordance
219 with RFC 2046, Sec. 4.1.1, Par. 1. */
226 /* Flags for subtypes of MULTIPART */
227 #define MULTI_UNKNOWN 0x00
228 #define MULTI_MIXED 0x01
229 #define MULTI_ALTERNATE 0x02
230 #define MULTI_DIGEST 0x03
231 #define MULTI_PARALLEL 0x04
232 #define MULTI_RELATED 0x05
234 /* Structure for subparts of a multipart content */
236 CT mp_part
; /* Content structure for subpart */
237 struct part
*mp_next
; /* pointer to next subpart structure */
240 /* Main structure for multipart content */
242 char *mp_start
; /* boundary string separating parts */
243 char *mp_stop
; /* terminating boundary string */
244 char *mp_content_before
; /* any content before the first subpart */
245 char *mp_content_after
; /* any content after the last subpart */
246 struct part
*mp_parts
; /* pointer to first subpart structure */
253 /* Flags for subtypes of MESSAGE */
254 #define MESSAGE_UNKNOWN 0x00
255 #define MESSAGE_RFC822 0x01
256 #define MESSAGE_PARTIAL 0x02
257 #define MESSAGE_EXTERNAL 0x03
259 /* Structure for message/partial */
268 /* Structure for message/external */
270 CT eb_parent
; /* pointer to controlling content structure */
271 CT eb_content
; /* pointer to internal content structure */
280 unsigned long eb_size
;
288 * APPLICATION content
291 /* Flags for subtype of APPLICATION */
292 #define APPLICATION_UNKNOWN 0x00
293 #define APPLICATION_OCTETS 0x01
294 #define APPLICATION_POSTSCRIPT 0x02
298 * Structures for mapping types to their internal flags
304 extern struct k2v SubText
[];
305 extern struct k2v Charset
[];
306 extern struct k2v SubMultiPart
[];
307 extern struct k2v SubMessage
[];
308 extern struct k2v SubApplication
[];
311 * Structures for mapping (content) types to
312 * the functions to handle them.
319 extern struct str2init str2cts
[];
320 extern struct str2init str2ces
[];
321 extern struct str2init str2methods
[];
326 CT
parse_mime (char *);
329 * Translate a composition file into a MIME data structure. Arguments are:
331 * infile - Name of input filename
332 * autobuild - A flag to indicate if the composition file parser is
333 * being run in automatic mode or not. In auto mode,
334 * if a MIME-Version header is encountered it is assumed
335 * that the composition file is already in MIME format
336 * and will not be processed further. Otherwise, an
337 * error is generated.
338 * dist - A flag to indicate if we are being run by "dist". In
339 * that case, add no MIME headers to the message. Existing
340 * headers will still be encoded by RFC 2047.
341 * directives - A flag to control whether or not build directives are
342 * processed by default.
343 * encoding - The default encoding to use when doing RFC 2047 header
344 * encoding. Must be one of CE_UNKNOWN, CE_BASE64, or
346 * maxunencoded - The maximum line length before the default encoding for
347 * text parts is quoted-printable.
348 * verbose - If 1, output verbose information during message composition
350 * Returns a CT structure describing the resulting MIME message. If the
351 * -auto flag is set and a MIME-Version header is encountered, the return
354 CT
build_mime (char *infile
, int autobuild
, int dist
, int directives
,
355 int encoding
, size_t maxunencoded
, int verbose
);
357 int add_header (CT
, char *, char *);
358 int get_ctinfo (char *, CT
, int);
359 int params_external (CT
, int);
360 int open7Bit (CT
, char **);
361 void close_encoding (CT
);
362 void free_content (CT
);
363 char *ct_type_str (int);
364 char *ct_subtype_str (int, int);
365 int ct_str_type (const char *);
366 int ct_str_subtype (int, const char *);
367 const struct str2init
*get_ct_init (int);
368 const char *ce_str (int);
369 const struct str2init
*get_ce_method (const char *);
370 char *content_charset (CT
);
371 int convert_charset (CT
, char *, int *);
372 void reverse_alternative_parts (CT
);
375 * Given a content structure, return true if the content has a disposition
380 * ct - Content structure to examine
382 int is_inline(CT ct
);
385 * Given a list of messages, display information about them on standard
390 * cts - An array of CT elements of messages that need to be
391 * displayed. Array is terminated by a NULL.
392 * headsw - If 1, display a column header.
393 * sizesw - If 1, display the size of the part.
394 * verbosw - If 1, display verbose information
395 * debugsw - If 1, turn on debugging for the output.
396 * disposw - If 1, display MIME part disposition information.
399 void list_all_messages(CT
*cts
, int headsw
, int sizesw
, int verbosw
,
400 int debugsw
, int disposw
);
403 * List the content information of a single MIME part on stdout.
407 * ct - MIME Content structure to display.
408 * toplevel - If set, we're at the top level of a message
409 * realsize - If set, determine the real size of the content
410 * verbose - If set, output verbose information
411 * debug - If set, turn on debugging for the output
412 * dispo - If set, display MIME part disposition information.
414 * Returns OK on success, NOTOK otherwise.
416 int list_content(CT ct
, int toplevel
, int realsize
, int verbose
, int debug
,
420 * Display content-appropriate information on MIME parts, decending recursively
421 * into multipart content if appropriate. Uses list_content() for displaying
422 * generic information.
424 * Arguments and return value are the same as list_content().
426 int list_switch(CT ct
, int toplevel
, int realsize
, int verbose
, int debug
,
430 * Given a linked list of parameters, build an output string for them. This
431 * string is designed to be concatenated on an already-built header.
435 * initialwidth - Current width of the header. Used to compute when to wrap
436 * parameters on the first line. The following lines will
437 * be prefixed by a tab (\t) character.
438 * params - Pointer to head of linked list of parameters.
439 * offsetout - The final line offset after all the parameters have been
440 * output. May be NULL.
441 * external - If set, outputting an external-body type and will not
442 * output a "body" parameter.
444 * Returns a pointer to the resulting parameter string. This string must
445 * be free()'d by the caller. Returns NULL on error.
447 char *output_params(size_t initialwidth
, PM params
, int *offsetout
,
451 * Encode a parameter value using RFC 2231 encode.
455 * pm - PM containing the parameter value and related info.
456 * output - Output buffer.
457 * len - Size, in octets, of output buffer.
458 * valuelen - Number of characters in the value
459 * valueoff - Offset into value field (pm->pm_value).
460 * index - If 0, output character set and language tag.
462 size_t encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
463 size_t valueoff
, int index
);
466 * Add a parameter to the parameter linked list.
470 * first - Pointer to head of linked list
471 * last - Pointer to tail of linked list
472 * name - Name of parameter
473 * value - Value of parameter
474 * nocopy - If set, will use the pointer values directly for "name"
475 * and "value" instead of making their own copy. These
476 * pointers will be free()'d later by the MIME routines, so
477 * they should not be used after calling this function!
479 * Returns allocated parameter element
481 PM
add_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
);
484 * Replace (or add) a parameter to the parameter linked list.
486 * If the named parameter already exists on the parameter linked list,
487 * replace the value with the new one. Otherwise add it to the linked
488 * list. All parameters are identical to add_param().
490 PM
replace_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
);
493 * Retrieve a parameter value from a parameter linked list. Convert to the
494 * local character set if required.
498 * first - Pointer to head of parameter linked list.
499 * name - Name of parameter.
500 * replace - If characters in the parameter list cannot be converted to
501 * the local character set, replace with this character.
502 * fetchonly - If true, return pointer to original value, no conversion
505 * Returns parameter value if found, NULL otherwise. Memory must be free()'d
506 * unless fetchonly is set.
509 char *get_param(PM first
, const char *name
, char replace
, int fetchonly
);
512 * Fetch a parameter value from a parameter structure, converting it to
513 * the local character set.
517 * pm - Pointer to parameter structure
518 * replace - If characters in the parameter list cannot be converted to
519 * the local character set, replace with this character.
521 * Returns a pointer to the parameter value. Memory is stored in an
522 * internal buffer, so the returned value is only valid until the next
523 * call to get_param_value() or get_param() (get_param() uses get_param_value()
526 char *get_param_value(PM pm
, char replace
);
529 * Display MIME message(s) on standard out.
533 * cts - NULL terminated array of CT structures for messages
535 * concat - If true, concatenate all MIME parts. If false, show each
536 * MIME part under a separate pager.
537 * textonly - If true, only display "text" MIME parts
538 * inlineonly - If true, only display MIME parts that are marked with
539 * a disposition of "inline" (includes parts that lack a
540 * Content-Disposition header).
541 * markerform - The name of a file containing mh-format(5) code used to
542 * display markers about non-displayed MIME parts.
544 void show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
);
547 * Display (or store) a single MIME part using the specified command
551 * ct - The Content structure of the MIME part we wish to display
552 * alternate - Set this to true if this is one part of a MIME
553 * multipart/alternative part. Will suppress some errors and
554 * will cause the function to return DONE instead of OK on
556 * cp - The command string to execute. Will be run through the
557 * parser for %-escapes as described in mhshow(1).
558 * cracked - If set, chdir() to this directory before executing the
559 * command in "cp". Only used by mhstore(1).
560 * fmt - A series of mh-format(5) instructions to execute if the
561 * command string indicates a marker is desired. Can be NULL.
563 * Returns NOTOK if we could not display the part, DONE if alternate was
564 * set and we could display the part, and OK if alternate was not set and
565 * we could display the part.
568 int show_content_aux(CT ct
, int alternate
, char *cp
, char *cracked
,
571 extern int checksw
; /* Add Content-MD5 field */
575 * Put it here because it uses the CT typedef.
577 typedef struct mhstoreinfo
*mhstoreinfo_t
;
578 mhstoreinfo_t
mhstoreinfo_create(CT
*, char *, const char *, int, int);
579 int mhstoreinfo_files_not_clobbered(const mhstoreinfo_t
);
580 void mhstoreinfo_free(mhstoreinfo_t
);
581 void store_all_messages (mhstoreinfo_t
);