<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML ><HEAD ><link rel='stylesheet' type='text/css' href='manpage.css'> <!-- $Id: rfc822.sgml,v 1.1 2001/12/23 19:37:14 mrsam Exp $ --> <!-- Copyright 2001 Double Precision, Inc. See COPYING for --> <!-- distribution information. --> <meta name="MSSmartTagsPreventParsing" content="TRUE"> <link rel="icon" href="icon.gif" type="image/gif" /> <TITLE >rfc822</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"></HEAD ><BODY CLASS="REFENTRY" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><H1 ><A NAME="RFC822" ></A >rfc822</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN10" ></A ><H2 >Name</H2 >rfc822 -- RFC 822 parsing library</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN13" ></A ><H2 >Synopsis</H2 ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN14" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >#include <rfc822.h> #include <rfc2047.h> cc ... -lrfc822</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN16" ></A ><H2 >DESCRIPTION</H2 ><P >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.</P ><P >The format used by E-mail headers to encode sender and recipient information is defined by <A HREF="http://www.rfc-editor.org/rfc/rfc822.txt" TARGET="_top" >RFC 822</A > (and its successor, <A HREF="http://www.rfc-editor.org/rfc/rfc2822.txt" TARGET="_top" >RFC 2822</A >). The format allows the actual E-mail address and the sender/recipient name to be expressed together, for example: <TT CLASS="LITERAL" >John Smith <jsmith@example.com></TT ></P ><P >The main purposes of the rfc822 library is to:</P ><P >1) Parse a text string containing a list of RFC 822-formatted address into its logical components: names and E-mail addresses.</P ><P >2) Access those individual components.</P ><P >3) Allow some limited modifications of the parsed structure, and then convert it back into a text string.</P ><DIV CLASS="REFSECT2" ><A NAME="AEN27" ></A ><H3 >Tokenizing an E-mail header</H3 ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN29" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >struct rfc822t *tokens=rfc822t_alloc_new(const char *header, void (*err_func)(const char *, int, void *), void *func_arg); void rfc822t_free(tokens);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >The <TT CLASS="FUNCTION" >rfc822t_alloc_new</TT >() function (superceeds <TT CLASS="FUNCTION" >rfc822t_alloc</TT >(), which is now obsolete) accepts an E-mail <TT CLASS="PARAMETER" ><I >header</I ></TT >, and parses it into individual tokens. This function allocates and returns a pointer to an <CODE CLASS="STRUCTNAME" >rfc822t</CODE > structure, which is later used by <TT CLASS="FUNCTION" >rfc822a_alloc</TT >() to extract individual addresses from these tokens.</P ><P >If <TT CLASS="PARAMETER" ><I >err_func</I ></TT > 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 <TT CLASS="PARAMETER" ><I >header</I ></TT > pointer, an index to the syntax error in the header string, and the <TT CLASS="PARAMETER" ><I >func_arg</I ></TT > argument.</P ><P >The semantics of <TT CLASS="PARAMETER" ><I >err_func</I ></TT > are subject to change. It is recommended to leave this argument as NULL in the current version of the library.</P ><P ><TT CLASS="FUNCTION" >rfc822t_alloc</TT >() returns a pointer to a dynamically-allocated <CODE CLASS="STRUCTNAME" >rfc822t</CODE > structure. A NULL pointer is returned if there's insufficient memory to allocate this structure. The <TT CLASS="FUNCTION" >rfc822t_free</TT >() function destroys <CODE CLASS="STRUCTNAME" >rfc822t</CODE > structure and frees all dynamically allocated memory.</P ><DIV CLASS="NOTE" ><P ></P ><TABLE CLASS="NOTE" WIDTH="100%" BORDER="0" ><TR ><TD WIDTH="25" ALIGN="CENTER" VALIGN="TOP" >NOTE:</TD ><TD ALIGN="LEFT" VALIGN="TOP" ><P >Until <TT CLASS="FUNCTION" >rfc822t_free</TT >() is called, the contents of <TT CLASS="PARAMETER" ><I >header</I ></TT > MUST NOT be destroyed or altered in any way. The contents of <TT CLASS="PARAMETER" ><I >header</I ></TT > are not modified by <TT CLASS="FUNCTION" >rfc822t_alloc</TT >(), however the <CODE CLASS="STRUCTNAME" >rfc822t</CODE > structure contains pointers to portions of the supplied <TT CLASS="PARAMETER" ><I >header</I ></TT >, and they must remain valid.</P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="REFSECT2" ><A NAME="AEN56" ></A ><H3 >Extracting E-mail addresses</H3 ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN58" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >struct rfc822a *addrs=rfc822a_alloc(struct rfc822t *tokens); void rfc822a_free(addrs);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >The <TT CLASS="FUNCTION" >rfc822a_alloc</TT >() function returns a dynamically-allocated <CODE CLASS="STRUCTNAME" >rfc822a</CODE > structure, that contains individual addresses that were logically parsed from a <CODE CLASS="STRUCTNAME" >rfc822t</CODE > structure. The <TT CLASS="FUNCTION" >rfc822a_alloc</TT >() function returns NULL if there was insufficient memory to allocate the <CODE CLASS="STRUCTNAME" >rfc822a</CODE > structure. The <TT CLASS="FUNCTION" >rfc822a_free</TT >() function destroys the <CODE CLASS="STRUCTNAME" >rfc822a</CODE > function, and frees all associated dynamically-allocated memory. The <CODE CLASS="STRUCTNAME" >rfc822t</CODE > structure passed to <TT CLASS="FUNCTION" >rfc822a_alloc</TT >() must not be destroyed before <TT CLASS="FUNCTION" >rfc822a_free</TT >() destroys the <CODE CLASS="STRUCTNAME" >rfc822a</CODE > structure.</P ><P >The <CODE CLASS="STRUCTNAME" >rfc822a</CODE > structure has the following fields:</P ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN74" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >struct rfc822a { struct rfc822addr *addrs; int naddrs; } ;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >The <CODE CLASS="STRUCTFIELD" >naddrs</CODE > field gives the number of <CODE CLASS="STRUCTNAME" >rfc822addr</CODE > structures that are pointed to by <CODE CLASS="STRUCTFIELD" >addrs</CODE >, which is an array. Each <CODE CLASS="STRUCTNAME" >rfc822addr</CODE > structure represents either an address found in the original E-mail header, <I CLASS="EMPHASIS" >or the contents of some legacy "syntactical sugar"</I >. For example, the following is a valid E-mail header:</P ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN82" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >To: recipient-list: tom@example.com, john@example.com;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >Typically, all of this, except for "<TT CLASS="LITERAL" >To:</TT >", is tokenized by <TT CLASS="FUNCTION" >rfc822t_alloc</TT >(), then parsed by <TT CLASS="FUNCTION" >rfc822a_alloc</TT >(). "<TT CLASS="LITERAL" >recipient-list:</TT >" 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 <CODE CLASS="STRUCTNAME" >rfc822a</CODE > structure will have four <CODE CLASS="STRUCTNAME" >rfc822addr</CODE > structures: one for "<TT CLASS="LITERAL" >recipient-list:</TT >"; one for each address; and one for the trailing semicolon. Each <CODE CLASS="STRUCTNAME" >rfc822a</CODE > structure has the following fields:</P ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN93" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >struct rfc822addr { struct rfc822token *tokens; struct rfc822token *name; } ;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >If <CODE CLASS="STRUCTFIELD" >tokens</CODE > is a null pointer, this structure represents some non-address portion of the original header, such as "<TT CLASS="LITERAL" >recipient-list:</TT >" or a semicolon. Otherwise it points to a structure that represents the E-mail address in tokenized form.</P ><P ><CODE CLASS="STRUCTFIELD" >name</CODE > 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. <CODE CLASS="STRUCTFIELD" >name</CODE > will be NULL if the recipient name was not provided. For the following address: <TT CLASS="LITERAL" >Tom Jones <tjones@example.com></TT > - the <CODE CLASS="STRUCTFIELD" >tokens</CODE > field points to the tokenized form of "<TT CLASS="LITERAL" >tjones@example.com</TT >", and <CODE CLASS="STRUCTFIELD" >name</CODE > points to the tokenized form of "<TT CLASS="LITERAL" >Tom Jones</TT >".</P ><P >Each <CODE CLASS="STRUCTNAME" >rfc822token</CODE > structure contains the following fields:</P ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN108" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >struct rfc822token { struct rfc822token *next; int token; const char *ptr; int len; } ;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >The <CODE CLASS="STRUCTFIELD" >next</CODE > pointer builds a linked list of all tokens in this name or address. The possible values for the <CODE CLASS="STRUCTFIELD" >token</CODE > field are:</P ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT >0x00</DT ><DD ><P >This is a simple atom - a sequence of non-special characters that is delimited by whitespace or special characters (see below).</P ></DD ><DT >0x22</DT ><DD ><P >The value of the ascii quote - this is a quoted string.</P ></DD ><DT >Open parenthesis: '('</DT ><DD ><P >This is an old style comment. A deprecated form of E-mail addressing uses - for example - "<TT CLASS="LITERAL" >john@example.com (John Smith)</TT >" instead of "<TT CLASS="LITERAL" >John Smith <john@example.com></TT >". This old-style notation defined parenthesized content as arbitrary comments. The <CODE CLASS="STRUCTNAME" >rfc822token</CODE > with <CODE CLASS="STRUCTFIELD" >token</CODE > set to '(' is created for the contents of the entire comment.</P ></DD ><DT >Symbols: '<', '>', '@', and many others</DT ><DD ><P >The remaining possible values of <CODE CLASS="STRUCTFIELD" >token</CODE > include all the characters in RFC 822 headers that have special significance.</P ></DD ></DL ></DIV ><P >When a <CODE CLASS="STRUCTNAME" >rfc822token</CODE > structure does not represent a special character, the <CODE CLASS="STRUCTFIELD" >ptr</CODE > field points to a text string giving its contents. The contents are NOT null-terminated, the <CODE CLASS="STRUCTFIELD" >len</CODE > field contains the number of characters included. The macro rfc822_is_atom(token) indicates whether <CODE CLASS="STRUCTFIELD" >ptr</CODE > and <CODE CLASS="STRUCTFIELD" >len</CODE > are used for the given <CODE CLASS="STRUCTFIELD" >token</CODE >. Currently <TT CLASS="FUNCTION" >rfc822_is_atom</TT >() returns true if <CODE CLASS="STRUCTFIELD" >token</CODE > is a zero byte, '<TT CLASS="LITERAL" >"</TT >', or '<TT CLASS="LITERAL" >(</TT >'.</P ><P >Note that it's possible that <CODE CLASS="STRUCTFIELD" >len</CODE > might be zero. This happens with null addresses used as return addresses for delivery status notifications.</P ></DIV ><DIV CLASS="REFSECT2" ><A NAME="AEN148" ></A ><H3 >Working with E-mail addresses</H3 ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN150" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >void rfc822_deladdr(struct rfc822a *addrs, int index); void rfc822tok_print(const struct rfc822token *list, void (*func)(char, void *), void *func_arg); void rfc822_print(const struct rfc822a *addrs, void (*print_func)(char, void *), void (*print_separator)(const char *, void *), void *callback_arg); void rfc822_addrlist(const struct rfc822a *addrs, void (*print_func)(char, void *), void *callback_arg); void rfc822_namelist(const struct rfc822a *addrs, void (*print_func)(char, void *), void *callback_arg); void rfc822_praddr(const struct rfc822a *addrs, int index, void (*print_func)(char, void *), void *callback_arg); void rfc822_prname(const struct rfc822a *addrs, int index, void (*print_func)(char, void *), void *callback_arg); void rfc822_prname_orlist(const struct rfc822a *addrs, int index, void (*print_func)(char, void *), void *callback_arg); char *rfc822_gettok(const struct rfc822token *list); char *rfc822_getaddrs(const struct rfc822a *addrs); char *rfc822_getaddr(const struct rfc822a *addrs, int index); char *rfc822_getname(const struct rfc822a *addrs, int index); char *rfc822_getname_orlist(const struct rfc822a *addrs, int index); char *rfc822_getaddrs_wrap(const struct rfc822a *, int);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >These functions are used to work with individual addresses that are parsed by <TT CLASS="FUNCTION" >rfc822a_alloc</TT >().</P ><P ><TT CLASS="FUNCTION" >rfc822_deladdr</TT >() removes a single <CODE CLASS="STRUCTNAME" >rfc822addr</CODE > structure, whose <TT CLASS="PARAMETER" ><I >index</I ></TT > is given, from the address array in <CODE CLASS="STRUCTNAME" >rfc822addr</CODE >. <CODE CLASS="STRUCTFIELD" >naddrs</CODE > is decremented by one.</P ><P ><TT CLASS="FUNCTION" >rfc822tok_print</TT >() converts a tokenized <TT CLASS="PARAMETER" ><I >list</I ></TT > of <CODE CLASS="STRUCTNAME" >rfc822token</CODE > objects into a text string. The callback function, <TT CLASS="PARAMETER" ><I >func</I ></TT >, is called one character at a time, for every character in the tokenized objects. An arbitrary pointer, <TT CLASS="PARAMETER" ><I >func_arg</I ></TT >, is passed unchanged as the additional argument to the callback function. <TT CLASS="FUNCTION" >rfc822tok_print</TT >() is not usually the most convenient and efficient function, but it has its uses.</P ><P ><TT CLASS="FUNCTION" >rfc822_print</TT >() takes an entire <CODE CLASS="STRUCTNAME" >rfc822a</CODE > structure, and uses the callback functions to print the contained addresses, in their original form, separated by commas. The function pointed to by <TT CLASS="PARAMETER" ><I >print_func</I ></TT > is used to print each individual address, one character at a time. Between the addresses, the <TT CLASS="PARAMETER" ><I >print_separator</I ></TT > function is called to print the address separator, usually the string ", ". The <TT CLASS="PARAMETER" ><I >callback_arg</I ></TT > argument is passed along unchanged, as an additional argument to these functions.</P ><P >The functions <TT CLASS="FUNCTION" >rfc822_addrlist</TT >() and <TT CLASS="FUNCTION" >rfc822_namelist</TT >() also print the contents of the entire <CODE CLASS="STRUCTNAME" >rfc822a</CODE > structure, but in a different way. <TT CLASS="FUNCTION" >rfc822_addrlist</TT >() prints just the actual E-mail addresses, not the recipient names or comments. Each E-mail address is followed by a newline character. <TT CLASS="FUNCTION" >rfc822_namelist</TT >() prints just the names or comments, followed by newlines.</P ><P >The functions <TT CLASS="FUNCTION" >rfc822_praddr</TT >() and <TT CLASS="FUNCTION" >rfc822_prname</TT >() are just like <TT CLASS="FUNCTION" >rfc822_addrlist</TT >() and <TT CLASS="FUNCTION" >rfc822_namelist</TT >(), except that they print a single name or address in the <CODE CLASS="STRUCTNAME" >rfc822a</CODE > structure, given its <TT CLASS="PARAMETER" ><I >index</I ></TT >. The functions <TT CLASS="FUNCTION" >rfc822_gettok</TT >(), <TT CLASS="FUNCTION" >rfc822_getaddrs</TT >(), <TT CLASS="FUNCTION" >rfc822_getaddr</TT >(), and <TT CLASS="FUNCTION" >rfc822_getname</TT >() are equivalent to <TT CLASS="FUNCTION" >rfc822tok_print</TT >(), <TT CLASS="FUNCTION" >rfc822_print</TT >(), <TT CLASS="FUNCTION" >rfc822_praddr</TT >() and <TT CLASS="FUNCTION" >rfc822_prname</TT >(), but, instead of using a callback function pointer, these functions write the output into a dynamically allocated buffer. That buffer must be destroyed by <TT CLASS="FUNCTION" >free</TT >(3) after use. These functions will return a null pointer in the event of a failure to allocate memory for the buffer.</P ><P ><TT CLASS="FUNCTION" >rfc822_prname_orlist</TT >() is similar to <TT CLASS="FUNCTION" >rfc822_prname</TT >(), except that it will also print the legacy RFC822 group list syntax (which are also parsed by <TT CLASS="FUNCTION" >rfc822a_alloc</TT >()). <TT CLASS="FUNCTION" >rfc822_praddr</TT >() will print an empty string for an index that corresponds to a group list name (or terminated semicolon). <TT CLASS="FUNCTION" >rfc822_prname</TT >() will also print an empty string. <TT CLASS="FUNCTION" >rfc822_prname_orlist</TT >() will instead print either the name of the group list, or a single string ";". <TT CLASS="FUNCTION" >rfc822_getname_orlist</TT >() will instead save it into a dynamically allocated buffer.</P ><P >The function <TT CLASS="FUNCTION" >rfc822_getaddrs_wrap</TT >() is similar to <TT CLASS="FUNCTION" >rfc822_getaddrs</TT >(), except that the generated text is wrapped on or about the 73rd column, using newline characters.</P ></DIV ><DIV CLASS="REFSECT2" ><A NAME="AEN206" ></A ><H3 >Working with dates</H3 ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN208" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >time_t timestamp=rfc822_parsedt(const char *datestr) const char *datestr=rfc822_mkdate(time_t timestamp); void rfc822_mkdate_buf(time_t timestamp, char *buffer);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >These functions convert between timestamps and dates expressed in the <TT CLASS="LITERAL" >Date:</TT > E-mail header format.</P ><P ><TT CLASS="FUNCTION" >rfc822_parsedt</TT >() returns the timestamp corresponding to the given date string (0 if there was a syntax error).</P ><P ><TT CLASS="FUNCTION" >rfc822_mkdate</TT >() returns a date string corresponding to the given timestamp. <TT CLASS="FUNCTION" >rfc822_mkdate_buf</TT >() writes the date string into the given buffer instead, which must be big enough to accommodate it.</P ></DIV ><DIV CLASS="REFSECT2" ><A NAME="AEN217" ></A ><H3 >Working with 8-bit MIME-encoded headers</H3 ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN219" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >int error=rfc2047_decode(const char *text, int (*callback_func)(const char *, int, const char *, void *), void *callback_arg); extern char *str=rfc2047_decode_simple(const char *text); extern char *str=rfc2047_decode_enhanced(const char *text, const char *charset); void rfc2047_print(const struct rfc822a *a, const char *charset, void (*print_func)(char, void *), void (*print_separator)(const char *, void *), void *); char *buffer=rfc2047_encode_str(const char *string, const char *charset); int error=rfc2047_encode_callback(const char *string, const char *charset, int (*func)(const char *, size_t, void *), void *callback_arg); char *buffer=rfc2047_encode_header(const struct rfc822a *a, const char *charset);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >These functions provide additional logic to encode or decode 8-bit content in 7-bit RFC 822 headers, as specified in RFC 2047.</P ><P ><TT CLASS="FUNCTION" >rfc2047_decode</TT >() 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.</P ><P >The callback function also receives <TT CLASS="PARAMETER" ><I >callback_arg</I ></TT >, as its last argument. If the callback function returns a non-zero value, <TT CLASS="FUNCTION" >rfc2047_decode</TT >() terminates, returning that value. Otherwise, <TT CLASS="FUNCTION" >rfc2047_decode</TT >() returns 0 after a successful decoding. <TT CLASS="FUNCTION" >rfc2047_decode</TT >() returns -1 if it was unable to allocate sufficient memory.</P ><P ><TT CLASS="FUNCTION" >rfc2047_decode_simple</TT >() and <TT CLASS="FUNCTION" >rfc2047_decode_enhanced</TT >() are alternatives to <TT CLASS="FUNCTION" >rfc2047_decode</TT >() which forego a callback function, and return the decoded text in a dynamically-allocated memory buffer. The buffer must be <TT CLASS="FUNCTION" >free</TT >(3)-ed after use. <TT CLASS="FUNCTION" >rfc2047_decode_simple</TT >() discards all character set specifications, and merely decodes any 8-bit text. <TT CLASS="FUNCTION" >rfc2047_decode_enhanced</TT >() is a compromise to discarding all character set information. The local character set being used is specified as the second argument to <TT CLASS="FUNCTION" >rfc2047_decode_enhanced</TT >(). 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.</P ><P ><TT CLASS="FUNCTION" >rfc2047_decode_simple</TT >() and <TT CLASS="FUNCTION" >rfc2047_decode_enhanced</TT >() return a null pointer if they are unable to allocate sufficient memory.</P ><P >The <TT CLASS="FUNCTION" >rfc2047_print</TT >() function is equivalent to <TT CLASS="FUNCTION" >rfc822_print</TT >(), followed by <TT CLASS="FUNCTION" >rfc2047_decode_enhanced</TT >() on the result. The callback functions are used in an identical fashion, except that they receive text that's already decoded.</P ><P >The function <TT CLASS="FUNCTION" >rfc2047_encode_str</TT >() takes a <TT CLASS="PARAMETER" ><I >string</I ></TT > and <TT CLASS="PARAMETER" ><I >charset</I ></TT > being the name of the local character set, then encodes any 8-bit portions of <TT CLASS="PARAMETER" ><I >string</I ></TT > using RFC 2047 encoding. <TT CLASS="FUNCTION" >rfc2047_encode_str</TT >() returns a dynamically-allocated buffer with the result, which must be <TT CLASS="FUNCTION" >free</TT >(3)-ed after use, or NULL if there was insufficient memory to allocate the buffer.</P ><P >The function <TT CLASS="FUNCTION" >rfc2047_encode_callback</TT >() is similar to <TT CLASS="FUNCTION" >rfc2047_encode_str</TT >() 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 <TT CLASS="PARAMETER" ><I >callback_arg</I ></TT >.</P ><P >The function <TT CLASS="FUNCTION" >rfc2047_encode_header</TT >() is basically equivalent to <TT CLASS="FUNCTION" >rfc822_getaddrs</TT >(), followed by <TT CLASS="FUNCTION" >rfc2047_encode_str</TT >();</P ></DIV ><DIV CLASS="REFSECT2" ><A NAME="AEN259" ></A ><H3 >Working with subjects</H3 ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN261" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >char *basesubj=rfc822_coresubj(const char *subj); char *basesubj=rfc822_coresubj_nouc(const char *subj);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >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, <TT CLASS="FUNCTION" >rfc822_coresubj</TT >() performs the following transformations:</P ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT >Whitespace</DT ><DD ><P >Leading and trailing whitespace is removed. Consecutive whitespace characters are collapsed into a single whitespace character. All whitespace characters are replaced by a space.</P ></DD ><DT >Re:, (fwd) [foo]</DT ><DD ><P >These artifacts (and several others) are removed from the subject line.</P ></DD ></DL ></DIV ><P >Note that this function does NOT do MIME decoding. In order to implement IMAP THREAD, it is necessary to call something like <TT CLASS="FUNCTION" >rfc2047_decode</TT >() before calling <TT CLASS="FUNCTION" >rfc822_coresubj</TT >().</P ><P >This function returns a pointer to a dynamically-allocated buffer, which must be <TT CLASS="FUNCTION" >free</TT >(3)-ed after use.</P ><P ><TT CLASS="FUNCTION" >rfc822_coresubj_nouc</TT >() is like <TT CLASS="FUNCTION" >rfc822_coresubj</TT >(), except that the subject is not converted to uppercase.</P ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN282" ></A ><H2 >SEE ALSO</H2 ><P ><A HREF="rfc2045.html" TARGET="_top" ><SPAN CLASS="CITEREFENTRY" ><SPAN CLASS="REFENTRYTITLE" >rfc2045</SPAN >(3)</SPAN ></A >, <A HREF="reformail.html" TARGET="_top" ><SPAN CLASS="CITEREFENTRY" ><SPAN CLASS="REFENTRYTITLE" >reformail</SPAN >(1)</SPAN ></A >, <A HREF="reformime.html" TARGET="_top" ><SPAN CLASS="CITEREFENTRY" ><SPAN CLASS="REFENTRYTITLE" >reformime</SPAN >(1)</SPAN ></A >.</P ></DIV ></BODY ></HTML >