1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><title>rfc822</title><link rel="stylesheet" type="text/css" href="style.css"/><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"/><link rel="home" href="#idm255231558160" title="rfc822"/><link xmlns="" rel="stylesheet" type="text/css" href="manpage.css"/><meta xmlns="" name="MSSmartTagsPreventParsing" content="TRUE"/><link xmlns="" rel="icon" href="icon.gif" type="image/gif"/><!--
Copyright 1998 - 2009 Double Precision, Inc. See COPYING for distribution
information.
--></head><body><div class="refentry"><a id="idm255231558160" shape="rect"> </a><div class="titlepage"/><div class="refnamediv"><h2>Name</h2><p>rfc822 — RFC 822 parsing library</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="informalexample"><pre class="programlisting" xml:space="preserve">
#include <rfc822.h>
#include <rfc2047.h>
cc ... -lrfc822
</pre></div></div><div class="refsect1"><a id="idm255235769584" shape="rect"> </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 class="ulink" href="http://www.rfc-editor.org/rfc/rfc822.txt" target="_top" shape="rect">RFC 822</a>
(and its successor,
<a class="ulink" href="http://www.rfc-editor.org/rfc/rfc2822.txt" target="_top" shape="rect">RFC 2822</a>).
The format allows the actual E-mail
address and the sender/recipient name to be expressed together, for example:
<code class="literal">John Smith <jsmith@example.com></code></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 id="idm255235763968" shape="rect"> </a><h3>Tokenizing an E-mail header</h3><div class="informalexample"><pre class="programlisting" xml:space="preserve">
struct rfc822t *tokens=rfc822t_alloc_new(const char *header,
void (*err_func)(const char *, int, void *),
void *func_arg);
void rfc822t_free(tokens);
</pre></div><p>
The <code class="function">rfc822t_alloc_new</code>() function (superceeds
<code class="function">rfc822t_alloc</code>(), which is now
obsolete) accepts an E-mail <em class="parameter"><code>header</code></em>, and parses it into
individual tokens. This function allocates and returns a pointer to an
<span class="structname">rfc822t</span>
structure, which is later used by
<code class="function">rfc822a_alloc</code>() to extract
individual addresses from these tokens.</p><p>
If <em class="parameter"><code>err_func</code></em> 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
<em class="parameter"><code>header</code></em> pointer, an index to the syntax error in the
header string, and the <em class="parameter"><code>func_arg</code></em> argument.</p><p>
The semantics of <em class="parameter"><code>err_func</code></em> are subject to change. It is recommended
to leave this argument as NULL in the current version of the library.</p><p>
<code class="function">rfc822t_alloc</code>() returns a pointer to a
dynamically-allocated <span class="structname">rfc822t</span>
structure. A NULL pointer is returned if there's insufficient memory to
allocate this structure. The <code class="function">rfc822t_free</code>() function
destroys
<span class="structname">rfc822t</span> structure and frees all
dynamically allocated memory.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
Until <code class="function">rfc822t_free</code>() is called, the contents of
<em class="parameter"><code>header</code></em> MUST
NOT be destroyed or altered in any way. The contents of
<em class="parameter"><code>header</code></em> are not
modified by <code class="function">rfc822t_alloc</code>(), however the
<span class="structname">rfc822t</span> structure contains
pointers to portions of the supplied <em class="parameter"><code>header</code></em>,
and they must remain valid.</p></div></div><div class="refsect2"><a id="idm255230419584" shape="rect"> </a><h3>Extracting E-mail addresses</h3><div class="informalexample"><pre class="programlisting" xml:space="preserve">
struct rfc822a *addrs=rfc822a_alloc(struct rfc822t *tokens);
void rfc822a_free(addrs);
</pre></div><p>
The <code class="function">rfc822a_alloc</code>() function returns a
dynamically-allocated <span class="structname">rfc822a</span>
structure, that contains individual addresses that were logically parsed
from a <span class="structname">rfc822t</span> structure. The
<code class="function">rfc822a_alloc</code>() function returns NULL if
there was insufficient memory to allocate the <span class="structname">rfc822a</span> structure. The
<code class="function">rfc822a_free</code>() function destroys the <span class="structname">rfc822a</span> function, and frees all
associated dynamically-allocated memory. The <span class="structname">rfc822t</span> structure passed
to <code class="function">rfc822a_alloc</code>() must not be destroyed before <code class="function">rfc822a_free</code>() destroys the
<span class="structname">rfc822a</span> structure.</p><p>
The <span class="structname">rfc822a</span> structure has the following fields:</p><div class="informalexample"><pre class="programlisting" xml:space="preserve">
struct rfc822a {
struct rfc822addr *addrs;
int naddrs;
} ;
</pre></div><p>
The <em class="structfield"><code>naddrs</code></em> field gives the number of
<span class="structname">rfc822addr</span> structures
that are pointed to by <em class="structfield"><code>addrs</code></em>, which is an array.
Each <span class="structname">rfc822addr</span>
structure represents either an address found in the original E-mail header,
<span class="emphasis"><em>or the contents of some legacy "syntactical sugar"</em></span>.
For example, the
following is a valid E-mail header:</p><div class="informalexample"><pre class="programlisting" xml:space="preserve">
To: recipient-list: tom@example.com, john@example.com;
</pre></div><p>Typically, all of this, except for "<code class="literal">To:</code>",
is tokenized by <code class="function">rfc822t_alloc</code>(), then parsed by
<code class="function">rfc822a_alloc</code>().
"<code class="literal">recipient-list:</code>" 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
<span class="structname">rfc822a</span> structure will have four
<span class="structname">rfc822addr</span> structures: one for
"<code class="literal">recipient-list:</code>";
one for each address; and one for the trailing semicolon.
Each <span class="structname">rfc822a</span> structure has the following
fields:</p><div class="informalexample"><pre class="programlisting" xml:space="preserve">
struct rfc822addr {
struct rfc822token *tokens;
struct rfc822token *name;
} ;
</pre></div><p>
If <em class="structfield"><code>tokens</code></em> is a null pointer, this structure
represents some
non-address portion of the original header, such as
"<code class="literal">recipient-list:</code>" or a
semicolon. Otherwise it points to a structure that represents the E-mail
address in tokenized form.</p><p>
<em class="structfield"><code>name</code></em> 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.
<em class="structfield"><code>name</code></em> will be NULL if the recipient name was not provided. For the
following address:
<code class="literal">Tom Jones <tjones@example.com></code> - the
<em class="structfield"><code>tokens</code></em> field points to the tokenized form of
"<code class="literal">tjones@example.com</code>",
and <em class="structfield"><code>name</code></em> points to the tokenized form of
"<code class="literal">Tom Jones</code>".</p><p>
Each <span class="structname">rfc822token</span> structure contains the following
fields:</p><div class="informalexample"><pre class="programlisting" xml:space="preserve">
struct rfc822token {
struct rfc822token *next;
int token;
const char *ptr;
int len;
} ;
</pre></div><p>
The <em class="structfield"><code>next</code></em> pointer builds a linked list of all
tokens in this name or
address. The possible values for the <em class="structfield"><code>token</code></em> field
are:</p><div class="variablelist"><dl class="variablelist"><dt><span class="term">0x00</span></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><span class="term">0x22</span></dt><dd><p>
The value of the ascii quote - this is a quoted string.</p></dd><dt><span class="term">Open parenthesis: '('</span></dt><dd><p>
This is an old style comment. A deprecated form of E-mail
addressing uses - for example -
"<code class="literal">john@example.com (John Smith)</code>" instead of
"<code class="literal">John Smith <john@example.com></code>".
This old-style notation defined
parenthesized content as arbitrary comments.
The <span class="structname">rfc822token</span> with
<em class="structfield"><code>token</code></em> set to '(' is created for the contents of
the entire comment.</p></dd><dt><span class="term">Symbols: '<', '>', '@', and many others</span></dt><dd><p>
The remaining possible values of <em class="structfield"><code>token</code></em> include all
the characters in RFC 822 headers that have special significance.</p></dd></dl></div><p>
When a <span class="structname">rfc822token</span> structure does not represent a
special character, the <em class="structfield"><code>ptr</code></em> field points to a text
string giving its contents.
The contents are NOT null-terminated, the <em class="structfield"><code>len</code></em>
field contains the number of characters included.
The macro rfc822_is_atom(token) indicates whether
<em class="structfield"><code>ptr</code></em> and <em class="structfield"><code>len</code></em> are used for
the given <em class="structfield"><code>token</code></em>.
Currently <code class="function">rfc822_is_atom</code>() returns true if
<em class="structfield"><code>token</code></em> is a zero byte, '<code class="literal">"</code>', or
'<code class="literal">(</code>'.</p><p>
Note that it's possible that <em class="structfield"><code>len</code></em> might be zero.
This happens with null addresses used as return addresses for delivery status
notifications.</p></div><div class="refsect2"><a id="idm255230371984" shape="rect"> </a><h3>Working with E-mail addresses</h3><div class="informalexample"><pre class="programlisting" xml:space="preserve">
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></div><p>
These functions are used to work with individual addresses that are parsed
by <code class="function">rfc822a_alloc</code>().</p><p>
<code class="function">rfc822_deladdr</code>() removes a single
<span class="structname">rfc822addr</span> structure, whose
<em class="parameter"><code>index</code></em> is given, from the address array in
<span class="structname">rfc822addr</span>.
<em class="structfield"><code>naddrs</code></em> is decremented by one.</p><p>
<code class="function">rfc822tok_print</code>() converts a tokenized
<em class="parameter"><code>list</code></em> of <span class="structname">rfc822token</span>
objects into a text string. The callback function,
<em class="parameter"><code>func</code></em>, is called one
character at a time, for every character in the tokenized objects. An
arbitrary pointer, <em class="parameter"><code>func_arg</code></em>, is passed unchanged as
the additional argument to the callback function.
<code class="function">rfc822tok_print</code>() is not usually the most
convenient and efficient function, but it has its uses.</p><p>
<code class="function">rfc822_print</code>() takes an entire
<span class="structname">rfc822a</span> structure, and uses the
callback functions to print the contained addresses, in their original form,
separated by commas. The function pointed to by
<em class="parameter"><code>print_func</code></em> is used to
print each individual address, one character at a time. Between the
addresses, the <em class="parameter"><code>print_separator</code></em> function is called to
print the address separator, usually the string ", ".
The <em class="parameter"><code>callback_arg</code></em> argument is passed
along unchanged, as an additional argument to these functions.</p><p>
The functions <code class="function">rfc822_addrlist</code>() and
<code class="function">rfc822_namelist</code>() also print the
contents of the entire <span class="structname">rfc822a</span> structure, but in a
different way.
<code class="function">rfc822_addrlist</code>() prints just the actual E-mail
addresses, not the recipient
names or comments. Each E-mail address is followed by a newline character.
<code class="function">rfc822_namelist</code>() prints just the names or comments,
followed by newlines.</p><p>
The functions <code class="function">rfc822_praddr</code>() and
<code class="function">rfc822_prname</code>() are just like
<code class="function">rfc822_addrlist</code>() and
<code class="function">rfc822_namelist</code>(), except that they print a single name
or address in the <span class="structname">rfc822a</span> structure, given its
<em class="parameter"><code>index</code></em>. The
functions <code class="function">rfc822_gettok</code>(),
<code class="function">rfc822_getaddrs</code>(), <code class="function">rfc822_getaddr</code>(),
and <code class="function">rfc822_getname</code>() are equivalent to
<code class="function">rfc822tok_print</code>(), <code class="function">rfc822_print</code>(),
<code class="function">rfc822_praddr</code>() and <code class="function">rfc822_prname</code>(),
but, instead of using a callback function
pointer, these functions write the output into a dynamically allocated buffer.
That buffer must be destroyed by <code class="function">free</code>(3) after use.
These functions will
return a null pointer in the event of a failure to allocate memory for the
buffer.</p><p>
<code class="function">rfc822_prname_orlist</code>() is similar to
<code class="function">rfc822_prname</code>(), except that it will
also print the legacy RFC822 group list syntax (which are also parsed by
<code class="function">rfc822a_alloc</code>()). <code class="function">rfc822_praddr</code>()
will print an empty string for an index
that corresponds to a group list name (or terminated semicolon).
<code class="function">rfc822_prname</code>() will also print an empty string.
<code class="function">rfc822_prname_orlist</code>() will
instead print either the name of the group list, or a single string ";".
<code class="function">rfc822_getname_orlist</code>() will instead save it into a
dynamically allocated buffer.</p><p>
The function <code class="function">rfc822_getaddrs_wrap</code>() is similar to
<code class="function">rfc822_getaddrs</code>(), except
that the generated text is wrapped on or about the 73rd column, using
newline characters.</p></div><div class="refsect2"><a id="idm255230331520" shape="rect"> </a><h3>Working with dates</h3><div class="informalexample"><pre class="programlisting" xml:space="preserve">
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></div><p>
These functions convert between timestamps and dates expressed in the
<code class="literal">Date:</code> E-mail header format.</p><p>
<code class="function">rfc822_parsedt</code>() returns the timestamp corresponding to
the given date string (0 if there was a syntax error).</p><p>
<code class="function">rfc822_mkdate</code>() returns a date string corresponding to
the given timestamp.
<code class="function">rfc822_mkdate_buf</code>() writes the date string into the
given buffer instead,
which must be big enough to accommodate it.</p></div><div class="refsect2"><a id="idm255230325184" shape="rect"> </a><h3>Working with 8-bit MIME-encoded headers</h3><div class="informalexample"><pre class="programlisting" xml:space="preserve">
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></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>
<code class="function">rfc2047_decode</code>() 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 <em class="parameter"><code>callback_arg</code></em>, as
its last
argument. If the callback function returns a non-zero value,
<code class="function">rfc2047_decode</code>()
terminates, returning that value. Otherwise,
<code class="function">rfc2047_decode</code>() returns 0 after
a successful decoding. <code class="function">rfc2047_decode</code>() returns -1 if it
was unable to allocate sufficient memory.</p><p>
<code class="function">rfc2047_decode_simple</code>() and
<code class="function">rfc2047_decode_enhanced</code>() are alternatives to
<code class="function">rfc2047_decode</code>() which forego a callback function, and
return the decoded text
in a dynamically-allocated memory buffer. The buffer must be
<code class="function">free</code>(3)-ed after
use. <code class="function">rfc2047_decode_simple</code>() discards all character set
specifications, and
merely decodes any 8-bit text. <code class="function">rfc2047_decode_enhanced</code>()
is a compromise to
discarding all character set information. The local character set being used
is specified as the second argument to
<code class="function">rfc2047_decode_enhanced</code>(). 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>
<code class="function">rfc2047_decode_simple</code>() and
<code class="function">rfc2047_decode_enhanced</code>() return a null pointer
if they are unable to allocate sufficient memory.</p><p>
The <code class="function">rfc2047_print</code>() function is equivalent to
<code class="function">rfc822_print</code>(), followed by
<code class="function">rfc2047_decode_enhanced</code>() 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 <code class="function">rfc2047_encode_str</code>() takes a
<em class="parameter"><code>string</code></em> and <em class="parameter"><code>charset</code></em>
being the name of the local character set, then encodes any 8-bit portions of
<em class="parameter"><code>string</code></em> using RFC 2047 encoding.
<code class="function">rfc2047_encode_str</code>() returns a
dynamically-allocated buffer with the result, which must be
<code class="function">free</code>(3)-ed after
use, or NULL if there was insufficient memory to allocate the buffer.</p><p>
The function <code class="function">rfc2047_encode_callback</code>() is similar to
<code class="function">rfc2047_encode_str</code>()
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 <em class="parameter"><code>callback_arg</code></em>.</p><p>
The function <code class="function">rfc2047_encode_header</code>() is basically
equivalent to <code class="function">rfc822_getaddrs</code>(), followed by
<code class="function">rfc2047_encode_str</code>();</p></div><div class="refsect2"><a id="idm255232366128" shape="rect"> </a><h3>Working with subjects</h3><div class="informalexample"><pre class="programlisting" xml:space="preserve">
char *basesubj=rfc822_coresubj(const char *subj);
char *basesubj=rfc822_coresubj_nouc(const char *subj);
</pre></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, <code class="function">rfc822_coresubj</code>() performs the following transformations:</p><div class="variablelist"><dl class="variablelist"><dt><span class="term">Whitespace</span></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><span class="term">Re:, (fwd) [foo]</span></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
<code class="function">rfc2047_decode</code>() before
calling <code class="function">rfc822_coresubj</code>().</p><p>
This function returns a pointer to a dynamically-allocated buffer, which
must be <code class="function">free</code>(3)-ed after use.</p><p>
<code class="function">rfc822_coresubj_nouc</code>() is like
<code class="function">rfc822_coresubj</code>(), except that the subject
is not converted to uppercase.</p></div></div><div class="refsect1"><a id="idm255230300160" shape="rect"> </a><h2>SEE ALSO</h2><p>
<a class="ulink" href="rfc2045.html" target="_top" shape="rect"><span class="citerefentry"><span class="refentrytitle">rfc2045</span>(3)</span></a>,
<a class="ulink" href="reformail.html" target="_top" shape="rect"><span class="citerefentry"><span class="refentrytitle">reformail</span>(1)</span></a>,
<a class="ulink" href="reformime.html" target="_top" shape="rect"><span class="citerefentry"><span class="refentrytitle">reformime</span>(1)</span></a>.</p></div></div></body></html>
|