SamVarshavchikAuthorCourier Mail Server
rfc822
3
Double Precision, Inc.
rfc822
RFC 822 parsing library
#include <rfc822.h>
#include <rfc2047.h>
cc ... -lrfc822
DESCRIPTION
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.
The format used by E-mail headers to encode sender and recipient
information is defined by
RFC 822
(and its successor,
RFC 2822).
The format allows the actual E-mail
address and the sender/recipient name to be expressed together, for example:
John Smith <jsmith@example.com>
The main purposes of the rfc822 library is to:
1) Parse a text string containing a list of RFC 822-formatted address into
its logical components: names and E-mail addresses.
2) Access those individual components.
3) Allow some limited modifications of the parsed structure, and then
convert it back into a text string.
Working with E-mail addresses
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);
These functions are used to work with individual addresses that are parsed
by rfc822a_alloc().
rfc822_deladdr() removes a single
rfc822addr structure, whose
index is given, from the address array in
rfc822addr.
naddrs is decremented by one.
rfc822tok_print() converts a tokenized
list of rfc822token
objects into a text string. The callback function,
func, is called one
character at a time, for every character in the tokenized objects. An
arbitrary pointer, func_arg, is passed unchanged as
the additional argument to the callback function.
rfc822tok_print() is not usually the most
convenient and efficient function, but it has its uses.
rfc822_print() takes an entire
rfc822a structure, and uses the
callback functions to print the contained addresses, in their original form,
separated by commas. The function pointed to by
print_func is used to
print each individual address, one character at a time. Between the
addresses, the print_separator function is called to
print the address separator, usually the string ", ".
The callback_arg argument is passed
along unchanged, as an additional argument to these functions.
The functions rfc822_addrlist() and
rfc822_namelist() also print the
contents of the entire rfc822a structure, but in a
different way.
rfc822_addrlist() prints just the actual E-mail
addresses, not the recipient
names or comments. Each E-mail address is followed by a newline character.
rfc822_namelist() prints just the names or comments,
followed by newlines.
The functions rfc822_praddr() and
rfc822_prname() are just like
rfc822_addrlist() and
rfc822_namelist(), except that they print a single name
or address in the rfc822a structure, given its
index. The
functions rfc822_gettok(),
rfc822_getaddrs(), rfc822_getaddr(),
and rfc822_getname() are equivalent to
rfc822tok_print(), rfc822_print(),
rfc822_praddr() and rfc822_prname(),
but, instead of using a callback function
pointer, these functions write the output into a dynamically allocated buffer.
That buffer must be destroyed by free(3) after use.
These functions will
return a null pointer in the event of a failure to allocate memory for the
buffer.
rfc822_prname_orlist() is similar to
rfc822_prname(), except that it will
also print the legacy RFC822 group list syntax (which are also parsed by
rfc822a_alloc()). rfc822_praddr()
will print an empty string for an index
that corresponds to a group list name (or terminated semicolon).
rfc822_prname() will also print an empty string.
rfc822_prname_orlist() will
instead print either the name of the group list, or a single string ";".
rfc822_getname_orlist() will instead save it into a
dynamically allocated buffer.
The function rfc822_getaddrs_wrap() is similar to
rfc822_getaddrs(), except
that the generated text is wrapped on or about the 73rd column, using
newline characters.
Working with dates
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);
These functions convert between timestamps and dates expressed in the
Date: E-mail header format.
rfc822_parsedt() returns the timestamp corresponding to
the given date string (0 if there was a syntax error).
rfc822_mkdate() returns a date string corresponding to
the given timestamp.
rfc822_mkdate_buf() writes the date string into the
given buffer instead,
which must be big enough to accommodate it.
Working with subjects
char *basesubj=rfc822_coresubj(const char *subj);
char *basesubj=rfc822_coresubj_nouc(const char *subj);
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, rfc822_coresubj() performs the following transformations:
Whitespace
Leading and trailing whitespace is removed. Consecutive
whitespace characters are collapsed into a single whitespace character.
All whitespace characters are replaced by a space.
Re:, (fwd) [foo]
These artifacts (and several others) are removed from
the subject line.
Note that this function does NOT do MIME decoding. In order to
implement IMAP THREAD, it is necessary to call something like
rfc2047_decode() before
calling rfc822_coresubj().
This function returns a pointer to a dynamically-allocated buffer, which
must be free(3)-ed after use.
rfc822_coresubj_nouc() is like
rfc822_coresubj(), except that the subject
is not converted to uppercase.
SEE ALSO
rfc20453,
reformail1,
reformime1.