Imported Upstream version 0.63.0
[hcoop/debian/courier-authlib.git] / rfc822 / rfc822.3
CommitLineData
d9898ee8 1.\" <!-- $Id: rfc822.sgml,v 1.5 2007/04/22 15:05:16 mrsam Exp $ -->
2.\" <!-- Copyright 2001-2007 Double Precision, Inc. See COPYING for -->
3.\" <!-- distribution information. -->
4.\" Title: rfc822
5.\" Author:
8d138742
CE
6.\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
7.\" Date: 08/23/2008
d9898ee8 8.\" Manual: Double Precision, Inc.
9.\" Source: Double Precision, Inc.
10.\"
8d138742 11.TH "RFC822" "3" "08/23/2008" "Double Precision, Inc." "Double Precision, Inc."
d9898ee8 12.\" disable hyphenation
13.nh
14.\" disable justification (adjust text to left margin only)
15.ad l
16.SH "NAME"
8d138742 17rfc822 - RFC 822 parsing library
d9898ee8 18.SH "SYNOPSIS"
19.sp
20.RS 4
21.nf
8d138742 22#include <rfc822\.h>
d9898ee8 23
8d138742 24#include <rfc2047\.h>
d9898ee8 25
8d138742 26cc \.\.\. \-lrfc822
d9898ee8 27.fi
28.RE
29.SH "DESCRIPTION"
30.PP
8d138742 31The rfc822 library provides functions for parsing E\-mail headers in the RFC 822 format\. This library also includes some functions to help with encoding and decoding 8\-bit text, as defined by RFC 2047\.
d9898ee8 32.PP
33The format used by E\-mail headers to encode sender and recipient information is defined by
34\fIRFC 822\fR\&[1]
35(and its successor,
8d138742
CE
36\fIRFC 2822\fR\&[2])\. The format allows the actual E\-mail address and the sender/recipient name to be expressed together, for example:
37John Smith <jsmith@example\.com>
d9898ee8 38.PP
39The main purposes of the rfc822 library is to:
40.PP
8d138742 411) Parse a text string containing a list of RFC 822\-formatted address into its logical components: names and E\-mail addresses\.
d9898ee8 42.PP
8d138742 432) Access those individual components\.
d9898ee8 44.PP
8d138742 453) Allow some limited modifications of the parsed structure, and then convert it back into a text string\.
d9898ee8 46.SS "Tokenizing an E\-mail header"
47.sp
48.RS 4
49.nf
50struct rfc822t *tokens=rfc822t_alloc_new(const char *header,
51 void (*err_func)(const char *, int, void *),
52 void *func_arg);
53
54void rfc822t_free(tokens);
55.fi
56.RE
57.PP
58The
59\fBrfc822t_alloc_new\fR() function (superceeds
60\fBrfc822t_alloc\fR(), which is now obsolete) accepts an E\-mail
8d138742 61\fIheader\fR, and parses it into individual tokens\. This function allocates and returns a pointer to an
d9898ee8 62rfc822t
63structure, which is later used by
8d138742 64\fBrfc822a_alloc\fR() to extract individual addresses from these tokens\.
d9898ee8 65.PP
66If
67\fIerr_func\fR
8d138742 68argument, if not NULL, is a pointer to a callback function\. The function is called in the event that the E\-mail header is corrupted to the point that it cannot even be parsed\. This is a rare instance \-\- most forms of corruption are still valid at least on the lexical level\. The only time this error is reported is in the event of mismatched parenthesis, angle brackets, or quotes\. The callback function receives the
d9898ee8 69\fIheader\fR
70pointer, an index to the syntax error in the header string, and the
71\fIfunc_arg\fR
8d138742 72argument\.
d9898ee8 73.PP
74The semantics of
75\fIerr_func\fR
8d138742 76are subject to change\. It is recommended to leave this argument as NULL in the current version of the library\.
d9898ee8 77.PP
78
79\fBrfc822t_alloc\fR() returns a pointer to a dynamically\-allocated
80rfc822t
8d138742 81structure\. A NULL pointer is returned if there\'s insufficient memory to allocate this structure\. The
d9898ee8 82\fBrfc822t_free\fR() function destroys
83rfc822t
8d138742 84structure and frees all dynamically allocated memory\.
d9898ee8 85.sp
86.it 1 an-trap
87.nr an-no-space-flag 1
88.nr an-break-flag 1
89.br
8d138742 90Note
d9898ee8 91.PP
92Until
93\fBrfc822t_free\fR() is called, the contents of
94\fIheader\fR
8d138742 95MUST NOT be destroyed or altered in any way\. The contents of
d9898ee8 96\fIheader\fR
97are not modified by
98\fBrfc822t_alloc\fR(), however the
99rfc822t
100structure contains pointers to portions of the supplied
8d138742 101\fIheader\fR, and they must remain valid\.
d9898ee8 102.SS "Extracting E\-mail addresses"
103.sp
104.RS 4
105.nf
106struct rfc822a *addrs=rfc822a_alloc(struct rfc822t *tokens);
107
108void rfc822a_free(addrs);
109.fi
110.RE
111.PP
112The
113\fBrfc822a_alloc\fR() function returns a dynamically\-allocated
114rfc822a
115structure, that contains individual addresses that were logically parsed from a
116rfc822t
8d138742 117structure\. The
d9898ee8 118\fBrfc822a_alloc\fR() function returns NULL if there was insufficient memory to allocate the
119rfc822a
8d138742 120structure\. The
d9898ee8 121\fBrfc822a_free\fR() function destroys the
122rfc822a
8d138742 123function, and frees all associated dynamically\-allocated memory\. The
d9898ee8 124rfc822t
125structure passed to
126\fBrfc822a_alloc\fR() must not be destroyed before
127\fBrfc822a_free\fR() destroys the
128rfc822a
8d138742 129structure\.
d9898ee8 130.PP
131The
132rfc822a
133structure has the following fields:
134.sp
135.RS 4
136.nf
137struct rfc822a {
138 struct rfc822addr *addrs;
139 int naddrs;
140} ;
141.fi
142.RE
143.PP
144The
145naddrs
146field gives the number of
147rfc822addr
148structures that are pointed to by
8d138742 149addrs, which is an array\. Each
d9898ee8 150rfc822addr
151structure represents either an address found in the original E\-mail header,
8d138742 152\fIor the contents of some legacy "syntactical sugar"\fR\. For example, the following is a valid E\-mail header:
d9898ee8 153.sp
154.RS 4
155.nf
8d138742 156To: recipient\-list: tom@example\.com, john@example\.com;
d9898ee8 157.fi
158.RE
159.PP
160Typically, all of this, except for "To:", is tokenized by
161\fBrfc822t_alloc\fR(), then parsed by
8d138742 162\fBrfc822a_alloc\fR()\. "recipient\-list:" and the trailing semicolon is a legacy mailing list specification that is no longer in widespread use, but must still must be accounted for\. The resulting
d9898ee8 163rfc822a
164structure will have four
165rfc822addr
8d138742 166structures: one for "recipient\-list:"; one for each address; and one for the trailing semicolon\. Each
d9898ee8 167rfc822a
168structure has the following fields:
169.sp
170.RS 4
171.nf
172struct rfc822addr {
173 struct rfc822token *tokens;
174 struct rfc822token *name;
175} ;
176.fi
177.RE
178.PP
179If
180tokens
8d138742 181is a null pointer, this structure represents some non\-address portion of the original header, such as "recipient\-list:" or a semicolon\. Otherwise it points to a structure that represents the E\-mail address in tokenized form\.
d9898ee8 182.PP
183
184name
8d138742 185either points to the tokenized form of a non\-address portion of the original header, or to a tokenized form of the recipient\'s name\.
d9898ee8 186name
8d138742
CE
187will be NULL if the recipient name was not provided\. For the following address:
188Tom Jones <tjones@example\.com>
d9898ee8 189\- the
190tokens
8d138742 191field points to the tokenized form of "tjones@example\.com", and
d9898ee8 192name
8d138742 193points to the tokenized form of "Tom Jones"\.
d9898ee8 194.PP
195Each
196rfc822token
197structure contains the following fields:
198.sp
199.RS 4
200.nf
201struct rfc822token {
202 struct rfc822token *next;
203 int token;
204 const char *ptr;
205 int len;
206} ;
207.fi
208.RE
209.PP
210The
211next
8d138742 212pointer builds a linked list of all tokens in this name or address\. The possible values for the
d9898ee8 213token
214field are:
215.PP
2160x00
217.RS 4
8d138742 218This is a simple atom \- a sequence of non\-special characters that is delimited by whitespace or special characters (see below)\.
d9898ee8 219.RE
220.PP
2210x22
222.RS 4
8d138742 223The value of the ascii quote \- this is a quoted string\.
d9898ee8 224.RE
225.PP
8d138742 226Open parenthesis: \'(\'
d9898ee8 227.RS 4
8d138742 228This is an old style comment\. A deprecated form of E\-mail addressing uses \- for example \- "john@example\.com (John Smith)" instead of "John Smith <john@example\.com>"\. This old\-style notation defined parenthesized content as arbitrary comments\. The
d9898ee8 229rfc822token
230with
231token
8d138742 232set to \'(\' is created for the contents of the entire comment\.
d9898ee8 233.RE
234.PP
8d138742 235Symbols: \'<\', \'>\', \'@\', and many others
d9898ee8 236.RS 4
237The remaining possible values of
238token
8d138742 239include all the characters in RFC 822 headers that have special significance\.
d9898ee8 240.RE
241.PP
242When a
243rfc822token
244structure does not represent a special character, the
245ptr
8d138742 246field points to a text string giving its contents\. The contents are NOT null\-terminated, the
d9898ee8 247len
8d138742 248field contains the number of characters included\. The macro rfc822_is_atom(token) indicates whether
d9898ee8 249ptr
250and
251len
252are used for the given
8d138742 253token\. Currently
d9898ee8 254\fBrfc822_is_atom\fR() returns true if
255token
8d138742 256is a zero byte, \'"\', or \'(\'\.
d9898ee8 257.PP
8d138742 258Note that it\'s possible that
d9898ee8 259len
8d138742 260might be zero\. This happens with null addresses used as return addresses for delivery status notifications\.
d9898ee8 261.SS "Working with E\-mail addresses"
262.sp
263.RS 4
264.nf
265void rfc822_deladdr(struct rfc822a *addrs, int index);
266
267void rfc822tok_print(const struct rfc822token *list,
268 void (*func)(char, void *), void *func_arg);
269
270void rfc822_print(const struct rfc822a *addrs,
271 void (*print_func)(char, void *),
272 void (*print_separator)(const char *, void *), void *callback_arg);
273
274void rfc822_addrlist(const struct rfc822a *addrs,
275 void (*print_func)(char, void *),
276 void *callback_arg);
277
278void rfc822_namelist(const struct rfc822a *addrs,
279 void (*print_func)(char, void *),
280 void *callback_arg);
281
282void rfc822_praddr(const struct rfc822a *addrs,
283 int index,
284 void (*print_func)(char, void *),
285 void *callback_arg);
286
287void rfc822_prname(const struct rfc822a *addrs,
288 int index,
289 void (*print_func)(char, void *),
290 void *callback_arg);
291
292void rfc822_prname_orlist(const struct rfc822a *addrs,
293 int index,
294 void (*print_func)(char, void *),
295 void *callback_arg);
296
297char *rfc822_gettok(const struct rfc822token *list);
298char *rfc822_getaddrs(const struct rfc822a *addrs);
299char *rfc822_getaddr(const struct rfc822a *addrs, int index);
300char *rfc822_getname(const struct rfc822a *addrs, int index);
301char *rfc822_getname_orlist(const struct rfc822a *addrs, int index);
302
303char *rfc822_getaddrs_wrap(const struct rfc822a *, int);
304.fi
305.RE
306.PP
307These functions are used to work with individual addresses that are parsed by
8d138742 308\fBrfc822a_alloc\fR()\.
d9898ee8 309.PP
310
311\fBrfc822_deladdr\fR() removes a single
312rfc822addr
313structure, whose
314\fIindex\fR
315is given, from the address array in
8d138742 316rfc822addr\.
d9898ee8 317naddrs
8d138742 318is decremented by one\.
d9898ee8 319.PP
320
321\fBrfc822tok_print\fR() converts a tokenized
322\fIlist\fR
323of
324rfc822token
8d138742
CE
325objects into a text string\. The callback function,
326\fIfunc\fR, is called one character at a time, for every character in the tokenized objects\. An arbitrary pointer,
327\fIfunc_arg\fR, is passed unchanged as the additional argument to the callback function\.
328\fBrfc822tok_print\fR() is not usually the most convenient and efficient function, but it has its uses\.
d9898ee8 329.PP
330
331\fBrfc822_print\fR() takes an entire
332rfc822a
8d138742 333structure, and uses the callback functions to print the contained addresses, in their original form, separated by commas\. The function pointed to by
d9898ee8 334\fIprint_func\fR
8d138742 335is used to print each individual address, one character at a time\. Between the addresses, the
d9898ee8 336\fIprint_separator\fR
8d138742 337function is called to print the address separator, usually the string ", "\. The
d9898ee8 338\fIcallback_arg\fR
8d138742 339argument is passed along unchanged, as an additional argument to these functions\.
d9898ee8 340.PP
341The functions
342\fBrfc822_addrlist\fR() and
343\fBrfc822_namelist\fR() also print the contents of the entire
344rfc822a
8d138742
CE
345structure, but in a different way\.
346\fBrfc822_addrlist\fR() prints just the actual E\-mail addresses, not the recipient names or comments\. Each E\-mail address is followed by a newline character\.
347\fBrfc822_namelist\fR() prints just the names or comments, followed by newlines\.
d9898ee8 348.PP
349The functions
350\fBrfc822_praddr\fR() and
351\fBrfc822_prname\fR() are just like
352\fBrfc822_addrlist\fR() and
353\fBrfc822_namelist\fR(), except that they print a single name or address in the
354rfc822a
355structure, given its
8d138742 356\fIindex\fR\. The functions
d9898ee8 357\fBrfc822_gettok\fR(),
358\fBrfc822_getaddrs\fR(),
359\fBrfc822_getaddr\fR(), and
360\fBrfc822_getname\fR() are equivalent to
361\fBrfc822tok_print\fR(),
362\fBrfc822_print\fR(),
363\fBrfc822_praddr\fR() and
8d138742
CE
364\fBrfc822_prname\fR(), but, instead of using a callback function pointer, these functions write the output into a dynamically allocated buffer\. That buffer must be destroyed by
365\fBfree\fR(3) after use\. These functions will return a null pointer in the event of a failure to allocate memory for the buffer\.
d9898ee8 366.PP
367
368\fBrfc822_prname_orlist\fR() is similar to
369\fBrfc822_prname\fR(), except that it will also print the legacy RFC822 group list syntax (which are also parsed by
8d138742
CE
370\fBrfc822a_alloc\fR())\.
371\fBrfc822_praddr\fR() will print an empty string for an index that corresponds to a group list name (or terminated semicolon)\.
372\fBrfc822_prname\fR() will also print an empty string\.
373\fBrfc822_prname_orlist\fR() will instead print either the name of the group list, or a single string ";"\.
374\fBrfc822_getname_orlist\fR() will instead save it into a dynamically allocated buffer\.
d9898ee8 375.PP
376The function
377\fBrfc822_getaddrs_wrap\fR() is similar to
8d138742 378\fBrfc822_getaddrs\fR(), except that the generated text is wrapped on or about the 73rd column, using newline characters\.
d9898ee8 379.SS "Working with dates"
380.sp
381.RS 4
382.nf
383time_t timestamp=rfc822_parsedt(const char *datestr)
384const char *datestr=rfc822_mkdate(time_t timestamp);
385void rfc822_mkdate_buf(time_t timestamp, char *buffer);
386.fi
387.RE
388.PP
389These functions convert between timestamps and dates expressed in the
390Date:
8d138742 391E\-mail header format\.
d9898ee8 392.PP
393
8d138742 394\fBrfc822_parsedt\fR() returns the timestamp corresponding to the given date string (0 if there was a syntax error)\.
d9898ee8 395.PP
396
8d138742
CE
397\fBrfc822_mkdate\fR() returns a date string corresponding to the given timestamp\.
398\fBrfc822_mkdate_buf\fR() writes the date string into the given buffer instead, which must be big enough to accommodate it\.
d9898ee8 399.SS "Working with 8\-bit MIME\-encoded headers"
400.sp
401.RS 4
402.nf
403int error=rfc2047_decode(const char *text,
404 int (*callback_func)(const char *, int, const char *, void *),
405 void *callback_arg);
406
407extern char *str=rfc2047_decode_simple(const char *text);
408
409extern char *str=rfc2047_decode_enhanced(const char *text,
410 const char *charset);
411
412void rfc2047_print(const struct rfc822a *a,
413 const char *charset,
414 void (*print_func)(char, void *),
415 void (*print_separator)(const char *, void *), void *);
416
417
418char *buffer=rfc2047_encode_str(const char *string,
419 const char *charset);
420
421int error=rfc2047_encode_callback(const char *string,
422 const char *charset,
423 int (*func)(const char *, size_t, void *),
424 void *callback_arg);
425
426char *buffer=rfc2047_encode_header(const struct rfc822a *a,
427 const char *charset);
428.fi
429.RE
430.PP
8d138742 431These functions provide additional logic to encode or decode 8\-bit content in 7\-bit RFC 822 headers, as specified in RFC 2047\.
d9898ee8 432.PP
433
8d138742 434\fBrfc2047_decode\fR() is a basic RFC 2047 decoding function\. It receives a pointer to some 7bit RFC 2047\-encoded text, and a callback function\. The callback function is repeatedly called\. Each time it\'s called it receives a piece of decoded text\. The arguments are: a pointer to a text fragment, number of bytes in the text fragment, followed by a pointer to the character set of the text fragment\. The character set pointer is NULL for portions of the original text that are not RFC 2047\-encoded\.
d9898ee8 435.PP
436The callback function also receives
8d138742
CE
437\fIcallback_arg\fR, as its last argument\. If the callback function returns a non\-zero value,
438\fBrfc2047_decode\fR() terminates, returning that value\. Otherwise,
439\fBrfc2047_decode\fR() returns 0 after a successful decoding\.
440\fBrfc2047_decode\fR() returns \-1 if it was unable to allocate sufficient memory\.
d9898ee8 441.PP
442
443\fBrfc2047_decode_simple\fR() and
444\fBrfc2047_decode_enhanced\fR() are alternatives to
8d138742
CE
445\fBrfc2047_decode\fR() which forego a callback function, and return the decoded text in a dynamically\-allocated memory buffer\. The buffer must be
446\fBfree\fR(3)\-ed after use\.
447\fBrfc2047_decode_simple\fR() discards all character set specifications, and merely decodes any 8\-bit text\.
448\fBrfc2047_decode_enhanced\fR() is a compromise to discarding all character set information\. The local character set being used is specified as the second argument to
449\fBrfc2047_decode_enhanced\fR()\. Any RFC 2047\-encoded text in a different character set will be prefixed by the name of the character set, in brackets, in the resulting output\.
d9898ee8 450.PP
451
452\fBrfc2047_decode_simple\fR() and
8d138742 453\fBrfc2047_decode_enhanced\fR() return a null pointer if they are unable to allocate sufficient memory\.
d9898ee8 454.PP
455The
456\fBrfc2047_print\fR() function is equivalent to
457\fBrfc822_print\fR(), followed by
8d138742 458\fBrfc2047_decode_enhanced\fR() on the result\. The callback functions are used in an identical fashion, except that they receive text that\'s already decoded\.
d9898ee8 459.PP
460The function
461\fBrfc2047_encode_str\fR() takes a
462\fIstring\fR
463and
464\fIcharset\fR
465being the name of the local character set, then encodes any 8\-bit portions of
466\fIstring\fR
8d138742 467using RFC 2047 encoding\.
d9898ee8 468\fBrfc2047_encode_str\fR() returns a dynamically\-allocated buffer with the result, which must be
8d138742 469\fBfree\fR(3)\-ed after use, or NULL if there was insufficient memory to allocate the buffer\.
d9898ee8 470.PP
471The function
472\fBrfc2047_encode_callback\fR() is similar to
8d138742
CE
473\fBrfc2047_encode_str\fR() except that the callback function is repeatedly called to received the encoding string\. Each invocation of the callback function receives a pointer to a portion of the encoded text, the number of characters in this portion, and
474\fIcallback_arg\fR\.
d9898ee8 475.PP
476The function
477\fBrfc2047_encode_header\fR() is basically equivalent to
478\fBrfc822_getaddrs\fR(), followed by
479\fBrfc2047_encode_str\fR();
480.SS "Working with subjects"
481.sp
482.RS 4
483.nf
484char *basesubj=rfc822_coresubj(const char *subj);
485
486char *basesubj=rfc822_coresubj_nouc(const char *subj);
487.fi
488.RE
489.PP
8d138742 490This function takes the contents of the subject header, and returns the "core" subject header that\'s used in the specification of the IMAP THREAD function\. This function is designed to strip all subject line artifacts that might\'ve been added in the process of forwarding or replying to a message\. Currently,
d9898ee8 491\fBrfc822_coresubj\fR() performs the following transformations:
492.PP
493Whitespace
494.RS 4
8d138742 495Leading and trailing whitespace is removed\. Consecutive whitespace characters are collapsed into a single whitespace character\. All whitespace characters are replaced by a space\.
d9898ee8 496.RE
497.PP
498Re:, (fwd) [foo]
499.RS 4
8d138742 500These artifacts (and several others) are removed from the subject line\.
d9898ee8 501.RE
502.PP
8d138742 503Note that this function does NOT do MIME decoding\. In order to implement IMAP THREAD, it is necessary to call something like
d9898ee8 504\fBrfc2047_decode\fR() before calling
8d138742 505\fBrfc822_coresubj\fR()\.
d9898ee8 506.PP
507This function returns a pointer to a dynamically\-allocated buffer, which must be
8d138742 508\fBfree\fR(3)\-ed after use\.
d9898ee8 509.PP
510
511\fBrfc822_coresubj_nouc\fR() is like
8d138742 512\fBrfc822_coresubj\fR(), except that the subject is not converted to uppercase\.
d9898ee8 513.SH "SEE ALSO"
514.PP
515
516\fI\fBrfc2045\fR(3)\fR\&[3],
517\fI\fBreformail\fR(1)\fR\&[4],
8d138742
CE
518\fI\fBreformime\fR(1)\fR\&[5]\.
519.SH "NOTES"
d9898ee8 520.IP " 1." 4
521RFC 822
522.RS 4
8d138742 523\%http://www.rfc-editor.org/rfc/rfc822.txt
d9898ee8 524.RE
525.IP " 2." 4
526RFC 2822
527.RS 4
8d138742 528\%http://www.rfc-editor.org/rfc/rfc2822.txt
d9898ee8 529.RE
530.IP " 3." 4
531\fBrfc2045\fR(3)
532.RS 4
533\%rfc2045.html
534.RE
535.IP " 4." 4
536\fBreformail\fR(1)
537.RS 4
538\%reformail.html
539.RE
540.IP " 5." 4
541\fBreformime\fR(1)
542.RS 4
543\%reformime.html
544.RE