]> diplodocus.org Git - nmh/blob - h/mhparse.h
More cleaned and conversion to the new parameter API.
[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 NPARMS 10
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 trhings) 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 pid_t c_pid; /* process doing display */
145 int c_rfc934; /* rfc934 compatibility flag */
146
147 char *c_showproc; /* default, if not in profile */
148 char *c_termproc; /* for charset madness... */
149 char *c_storeproc; /* overrides profile entry, if any */
150
151 char *c_storage; /* write contents (file) */
152 char *c_folder; /* write contents (folder) */
153 };
154
155 /*
156 * Flags for Content-Type (Content->c_type)
157 */
158 #define CT_UNKNOWN 0x00
159 #define CT_APPLICATION 0x01
160 #define CT_AUDIO 0x02
161 #define CT_IMAGE 0x03
162 #define CT_MESSAGE 0x04
163 #define CT_MULTIPART 0x05
164 #define CT_TEXT 0x06
165 #define CT_VIDEO 0x07
166 #define CT_EXTENSION 0x08
167
168 /*
169 * Flags for Content-Transfer-Encoding (Content->c_encoding)
170 */
171 #define CE_UNKNOWN 0x00
172 #define CE_BASE64 0x01
173 #define CE_QUOTED 0x02
174 #define CE_8BIT 0x03
175 #define CE_7BIT 0x04
176 #define CE_BINARY 0x05
177 #define CE_EXTENSION 0x06
178 #define CE_EXTERNAL 0x07 /* for external-body */
179
180 /*
181 * TEXT content
182 */
183
184 /* Flags for subtypes of TEXT */
185 #define TEXT_UNKNOWN 0x00
186 #define TEXT_PLAIN 0x01
187 #define TEXT_RICHTEXT 0x02
188 #define TEXT_ENRICHED 0x03
189
190 /* Flags for character sets */
191 #define CHARSET_SPECIFIED 0x00
192 #define CHARSET_UNSPECIFIED 0x01 /* only needed when building drafts */
193
194 /* Structure for text content */
195 struct text {
196 int tx_charset; /* flag for character set */
197 };
198
199 /*
200 * MULTIPART content
201 */
202
203 /* Flags for subtypes of MULTIPART */
204 #define MULTI_UNKNOWN 0x00
205 #define MULTI_MIXED 0x01
206 #define MULTI_ALTERNATE 0x02
207 #define MULTI_DIGEST 0x03
208 #define MULTI_PARALLEL 0x04
209
210 /* Structure for subparts of a multipart content */
211 struct part {
212 CT mp_part; /* Content structure for subpart */
213 struct part *mp_next; /* pointer to next subpart structure */
214 };
215
216 /* Main structure for multipart content */
217 struct multipart {
218 char *mp_start; /* boundary string separating parts */
219 char *mp_stop; /* terminating boundary string */
220 char *mp_content_before; /* any content before the first subpart */
221 char *mp_content_after; /* any content after the last subpart */
222 struct part *mp_parts; /* pointer to first subpart structure */
223 };
224
225 /*
226 * MESSAGE content
227 */
228
229 /* Flags for subtypes of MESSAGE */
230 #define MESSAGE_UNKNOWN 0x00
231 #define MESSAGE_RFC822 0x01
232 #define MESSAGE_PARTIAL 0x02
233 #define MESSAGE_EXTERNAL 0x03
234
235 /* Structure for message/partial */
236 struct partial {
237 char *pm_partid;
238 int pm_partno;
239 int pm_maxno;
240 int pm_marked;
241 int pm_stored;
242 };
243
244 /* Structure for message/external */
245 struct exbody {
246 CT eb_parent; /* pointer to controlling content structure */
247 CT eb_content; /* pointer to internal content structure */
248 char *eb_partno;
249 char *eb_access;
250 int eb_flags;
251 char *eb_name;
252 char *eb_permission;
253 char *eb_site;
254 char *eb_dir;
255 char *eb_mode;
256 unsigned long eb_size;
257 char *eb_server;
258 char *eb_subject;
259 char *eb_body;
260 char *eb_url;
261 };
262
263 /*
264 * APPLICATION content
265 */
266
267 /* Flags for subtype of APPLICATION */
268 #define APPLICATION_UNKNOWN 0x00
269 #define APPLICATION_OCTETS 0x01
270 #define APPLICATION_POSTSCRIPT 0x02
271
272
273 /*
274 * Structures for mapping types to their internal flags
275 */
276 struct k2v {
277 char *kv_key;
278 int kv_value;
279 };
280 extern struct k2v SubText[];
281 extern struct k2v Charset[];
282 extern struct k2v SubMultiPart[];
283 extern struct k2v SubMessage[];
284 extern struct k2v SubApplication[];
285
286 /*
287 * Structures for mapping (content) types to
288 * the functions to handle them.
289 */
290 struct str2init {
291 char *si_key;
292 int si_val;
293 InitFunc si_init;
294 };
295 extern struct str2init str2cts[];
296 extern struct str2init str2ces[];
297 extern struct str2init str2methods[];
298
299 /*
300 * prototypes
301 */
302 int pidcheck (int);
303 CT parse_mime (char *);
304
305 /*
306 * Translate a composition file into a MIME data structure. Arguments are:
307 *
308 * infile - Name of input filename
309 * autobuild - A flag to indicate if the composition file parser is
310 * being run in automatic mode or not. In auto mode,
311 * if a MIME-Version header is encountered it is assumed
312 * that the composition file is already in MIME format
313 * and will not be processed further. Otherwise, an
314 * error is generated.
315 * dist - A flag to indicate if we are being run by "dist". In
316 * that case, add no MIME headers to the message. Existing
317 * headers will still be encoded by RFC 2047.
318 * directives - A flag to control whether or not build directives are
319 * processed by default.
320 * encoding - The default encoding to use when doing RFC 2047 header
321 * encoding. Must be one of CE_UNKNOWN, CE_BASE64, or
322 * CE_QUOTED.
323 * maxunencoded - The maximum line length before the default encoding for
324 * text parts is quoted-printable.
325 *
326 * Returns a CT structure describing the resulting MIME message. If the
327 * -auto flag is set and a MIME-Version header is encountered, the return
328 * value is NULL.
329 */
330 CT build_mime (char *infile, int autobuild, int dist, int directives,
331 int encoding, size_t maxunencoded);
332
333 int add_header (CT, char *, char *);
334 int get_ctinfo (char *, CT, int);
335 int params_external (CT, int);
336 int open7Bit (CT, char **);
337 void close_encoding (CT);
338 void free_content (CT);
339 char *ct_type_str (int);
340 char *ct_subtype_str (int, int);
341 const struct str2init *get_ct_init (int);
342 const char *ce_str (int);
343 const struct str2init *get_ce_method (const char *);
344
345 /*
346 * Parse a series of MIME attributes (or parameters) given a header as
347 * input.
348 *
349 * Arguments include:
350 *
351 * filename - Name of input file (for error messages)
352 * fieldname - Name of field being processed
353 * headerp - Pointer to pointer of the beginning of the MIME attributes.
354 * Updated to point to end of attributes when finished.
355 * param_head - Pointer to head of parameter list
356 * param_tail - Pointer to tail of parameter list
357 * commentp - Pointer to header comment pointer (may be NULL)
358 *
359 * Returns OK if parsing was successful, NOTOK if parsing failed, and
360 * DONE to indicate a benign error (minor parsing error, but the program
361 * should continue).
362 */
363 int parse_header_attrs (const char *filename, const char *fieldname,
364 char **headerp, PM *param_head, PM *param_tail,
365 char **commentp);
366
367 /*
368 * Given a linked list of parameters, build an output string for them. This
369 * string is designed to be concatenated on an already-built header.
370 *
371 * Arguments are:
372 *
373 * initialwidth - Current width of the header. Used to compute when to wrap
374 * parameters on the first line. The following lines will
375 * be prefixed by a tab (\t) character.
376 * params - Pointer to head of linked list of parameters.
377 * offsetout - The final line offset after all the parameters have been
378 * output. May be NULL.
379 * external - If set, outputting an external-body type and will not
380 * output a "body" parameter.
381
382 * Returns a pointer to the resulting parameter string. This string must
383 * be free()'d by the caller. Returns NULL on error.
384 */
385 char *output_params(size_t initialwidth, PM params, int *offsetout,
386 int external);
387
388 /*
389 * Add a parameter to the parameter linked list.
390 *
391 * Arguments are:
392 *
393 * first - Pointer to head of linked list
394 * last - Pointer to tail of linked list
395 * name - Name of parameter
396 * value - Value of parameter
397 *
398 * Returned allocated parameter element
399 */
400 PM add_param(PM *first, PM *last, const char *name, const char *value);
401
402 /*
403 * Retrieve a parameter value from a parameter linked list. Convert to the
404 * local character set if required.
405 *
406 * Arguments are:
407 *
408 * first - Pointer to head of parameter linked list.
409 * name - Name of parameter.
410 * replace - If characters in the parameter list cannot be converted to
411 * the local character set, replace with this character.
412 * fetchonly - If true, return pointer to original value, no conversion
413 * performed.
414 *
415 * Returns parameter value if found, NULL otherwise. Memory must be free()'d
416 * unless fetchonly is set.
417 */
418
419 char *get_param(PM first, const char *name, char replace, int fetchonly);
420
421 /*
422 * Fetch a parameter value from a parameter structure, converting it to
423 * the local character set.
424 *
425 * Arguments are:
426 *
427 * pm - Pointer to parameter structure
428 * replace - If characters in the parameter list cannot be converted to
429 * the local character set, replace with this character.
430 *
431 * Returns a pointer to the parameter value. Memory is stored in an
432 * internal buffer, so the returned value is only valid until the next
433 * call to get_param_value() or get_param() (get_param() uses get_param_value()
434 * internally).
435 */
436 char *get_param_value(PM pm, char replace);
437
438 extern int checksw; /* Add Content-MD5 field */