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. -->
6 .\" Generator: DocBook XSL Stylesheets v1.72.0 <http://docbook.sf.net/>
8 .\" Manual: Double Precision, Inc.
9 .\" Source: Double Precision, Inc.
11 .TH "RFC822" "3" "04/22/2007" "Double Precision, Inc." "Double Precision, Inc."
12 .\" disable hyphenation
14 .\" disable justification (adjust text to left margin only)
17 rfc822 \- RFC 822 parsing library
31 The 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.
33 The format used by E\-mail headers to encode sender and recipient information is defined by
36 \fIRFC 2822\fR\&[2]). The format allows the actual E\-mail address and the sender/recipient name to be expressed together, for example:
37 John Smith <jsmith@example.com>
39 The main purposes of the rfc822 library is to:
41 1) Parse a text string containing a list of RFC 822\-formatted address into its logical components: names and E\-mail addresses.
43 2) Access those individual components.
45 3) Allow some limited modifications of the parsed structure, and then convert it back into a text string.
46 .SS "Tokenizing an E\-mail header"
50 struct rfc822t *tokens=rfc822t_alloc_new(const char *header,
51 void (*err_func)(const char *, int, void *),
54 void rfc822t_free(tokens);
59 \fBrfc822t_alloc_new\fR() function (superceeds
60 \fBrfc822t_alloc\fR(), which is now obsolete) accepts an E\-mail
61 \fIheader\fR, and parses it into individual tokens. This function allocates and returns a pointer to an
63 structure, which is later used by
64 \fBrfc822a_alloc\fR() to extract individual addresses from these tokens.
68 argument, 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
70 pointer, an index to the syntax error in the header string, and the
76 are subject to change. It is recommended to leave this argument as NULL in the current version of the library.
79 \fBrfc822t_alloc\fR() returns a pointer to a dynamically\-allocated
81 structure. A NULL pointer is returned if there's insufficient memory to allocate this structure. The
82 \fBrfc822t_free\fR() function destroys
84 structure and frees all dynamically allocated memory.
87 .nr an-no-space-flag 1
93 \fBrfc822t_free\fR() is called, the contents of
95 MUST NOT be destroyed or altered in any way. The contents of
98 \fBrfc822t_alloc\fR(), however the
100 structure contains pointers to portions of the supplied
101 \fIheader\fR, and they must remain valid.
102 .SS "Extracting E\-mail addresses"
106 struct rfc822a *addrs=rfc822a_alloc(struct rfc822t *tokens);
108 void rfc822a_free(addrs);
113 \fBrfc822a_alloc\fR() function returns a dynamically\-allocated
115 structure, that contains individual addresses that were logically parsed from a
118 \fBrfc822a_alloc\fR() function returns NULL if there was insufficient memory to allocate the
121 \fBrfc822a_free\fR() function destroys the
123 function, and frees all associated dynamically\-allocated memory. The
126 \fBrfc822a_alloc\fR() must not be destroyed before
127 \fBrfc822a_free\fR() destroys the
133 structure has the following fields:
138 struct rfc822addr *addrs;
146 field gives the number of
148 structures that are pointed to by
149 addrs, which is an array. Each
151 structure represents either an address found in the original E\-mail header,
152 \fIor the contents of some legacy "syntactical sugar"\fR. For example, the following is a valid E\-mail header:
156 To: recipient\-list: tom@example.com, john@example.com;
160 Typically, all of this, except for "To:", is tokenized by
161 \fBrfc822t_alloc\fR(), then parsed by
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
164 structure will have four
166 structures: one for "recipient\-list:"; one for each address; and one for the trailing semicolon. Each
168 structure has the following fields:
173 struct rfc822token *tokens;
174 struct rfc822token *name;
181 is 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.
185 either points to the tokenized form of a non\-address portion of the original header, or to a tokenized form of the recipient's name.
187 will be NULL if the recipient name was not provided. For the following address:
188 Tom Jones <tjones@example.com>
191 field points to the tokenized form of "tjones@example.com", and
193 points to the tokenized form of "Tom Jones".
197 structure contains the following fields:
202 struct rfc822token *next;
212 pointer builds a linked list of all tokens in this name or address. The possible values for the
218 This is a simple atom \- a sequence of non\-special characters that is delimited by whitespace or special characters (see below).
223 The value of the ascii quote \- this is a quoted string.
226 Open parenthesis: '('
228 This 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
232 set to '(' is created for the contents of the entire comment.
235 Symbols: '<', '>', '@', and many others
237 The remaining possible values of
239 include all the characters in RFC 822 headers that have special significance.
244 structure does not represent a special character, the
246 field points to a text string giving its contents. The contents are NOT null\-terminated, the
248 field contains the number of characters included. The macro rfc822_is_atom(token) indicates whether
252 are used for the given
254 \fBrfc822_is_atom\fR() returns true if
256 is a zero byte, '"', or '('.
258 Note that it's possible that
260 might be zero. This happens with null addresses used as return addresses for delivery status notifications.
261 .SS "Working with E\-mail addresses"
265 void rfc822_deladdr(struct rfc822a *addrs, int index);
267 void rfc822tok_print(const struct rfc822token *list,
268 void (*func)(char, void *), void *func_arg);
270 void rfc822_print(const struct rfc822a *addrs,
271 void (*print_func)(char, void *),
272 void (*print_separator)(const char *, void *), void *callback_arg);
274 void rfc822_addrlist(const struct rfc822a *addrs,
275 void (*print_func)(char, void *),
278 void rfc822_namelist(const struct rfc822a *addrs,
279 void (*print_func)(char, void *),
282 void rfc822_praddr(const struct rfc822a *addrs,
284 void (*print_func)(char, void *),
287 void rfc822_prname(const struct rfc822a *addrs,
289 void (*print_func)(char, void *),
292 void rfc822_prname_orlist(const struct rfc822a *addrs,
294 void (*print_func)(char, void *),
297 char *rfc822_gettok(const struct rfc822token *list);
298 char *rfc822_getaddrs(const struct rfc822a *addrs);
299 char *rfc822_getaddr(const struct rfc822a *addrs, int index);
300 char *rfc822_getname(const struct rfc822a *addrs, int index);
301 char *rfc822_getname_orlist(const struct rfc822a *addrs, int index);
303 char *rfc822_getaddrs_wrap(const struct rfc822a *, int);
307 These functions are used to work with individual addresses that are parsed by
308 \fBrfc822a_alloc\fR().
311 \fBrfc822_deladdr\fR() removes a single
315 is given, from the address array in
318 is decremented by one.
321 \fBrfc822tok_print\fR() converts a tokenized
325 objects 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.
331 \fBrfc822_print\fR() takes an entire
333 structure, and uses the callback functions to print the contained addresses, in their original form, separated by commas. The function pointed to by
335 is used to print each individual address, one character at a time. Between the addresses, the
336 \fIprint_separator\fR
337 function is called to print the address separator, usually the string ", ". The
339 argument is passed along unchanged, as an additional argument to these functions.
342 \fBrfc822_addrlist\fR() and
343 \fBrfc822_namelist\fR() also print the contents of the entire
345 structure, 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.
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
356 \fIindex\fR. The functions
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
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.
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
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.
377 \fBrfc822_getaddrs_wrap\fR() is similar to
378 \fBrfc822_getaddrs\fR(), except that the generated text is wrapped on or about the 73rd column, using newline characters.
379 .SS "Working with dates"
383 time_t timestamp=rfc822_parsedt(const char *datestr)
384 const char *datestr=rfc822_mkdate(time_t timestamp);
385 void rfc822_mkdate_buf(time_t timestamp, char *buffer);
389 These functions convert between timestamps and dates expressed in the
391 E\-mail header format.
394 \fBrfc822_parsedt\fR() returns the timestamp corresponding to the given date string (0 if there was a syntax error).
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.
399 .SS "Working with 8\-bit MIME\-encoded headers"
403 int error=rfc2047_decode(const char *text,
404 int (*callback_func)(const char *, int, const char *, void *),
407 extern char *str=rfc2047_decode_simple(const char *text);
409 extern char *str=rfc2047_decode_enhanced(const char *text,
410 const char *charset);
412 void rfc2047_print(const struct rfc822a *a,
414 void (*print_func)(char, void *),
415 void (*print_separator)(const char *, void *), void *);
418 char *buffer=rfc2047_encode_str(const char *string,
419 const char *charset);
421 int error=rfc2047_encode_callback(const char *string,
423 int (*func)(const char *, size_t, void *),
426 char *buffer=rfc2047_encode_header(const struct rfc822a *a,
427 const char *charset);
431 These functions provide additional logic to encode or decode 8\-bit content in 7\-bit RFC 822 headers, as specified in RFC 2047.
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.
436 The callback function also receives
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.
443 \fBrfc2047_decode_simple\fR() and
444 \fBrfc2047_decode_enhanced\fR() are alternatives to
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.
452 \fBrfc2047_decode_simple\fR() and
453 \fBrfc2047_decode_enhanced\fR() return a null pointer if they are unable to allocate sufficient memory.
456 \fBrfc2047_print\fR() function is equivalent to
457 \fBrfc822_print\fR(), followed by
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.
461 \fBrfc2047_encode_str\fR() takes a
465 being the name of the local character set, then encodes any 8\-bit portions of
467 using RFC 2047 encoding.
468 \fBrfc2047_encode_str\fR() returns a dynamically\-allocated buffer with the result, which must be
469 \fBfree\fR(3)\-ed after use, or NULL if there was insufficient memory to allocate the buffer.
472 \fBrfc2047_encode_callback\fR() is similar to
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
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"
484 char *basesubj=rfc822_coresubj(const char *subj);
486 char *basesubj=rfc822_coresubj_nouc(const char *subj);
490 This 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,
491 \fBrfc822_coresubj\fR() performs the following transformations:
495 Leading and trailing whitespace is removed. Consecutive whitespace characters are collapsed into a single whitespace character. All whitespace characters are replaced by a space.
500 These artifacts (and several others) are removed from the subject line.
503 Note that this function does NOT do MIME decoding. In order to implement IMAP THREAD, it is necessary to call something like
504 \fBrfc2047_decode\fR() before calling
505 \fBrfc822_coresubj\fR().
507 This function returns a pointer to a dynamically\-allocated buffer, which must be
508 \fBfree\fR(3)\-ed after use.
511 \fBrfc822_coresubj_nouc\fR() is like
512 \fBrfc822_coresubj\fR(), except that the subject is not converted to uppercase.
516 \fI\fBrfc2045\fR(3)\fR\&[3],
517 \fI\fBreformail\fR(1)\fR\&[4],
518 \fI\fBreformime\fR(1)\fR\&[5].
523 \%http://www.rfc\-editor.org/rfc/rfc822.txt
528 \%http://www.rfc\-editor.org/rfc/rfc2822.txt