2 ** $Id: rfc822.h,v 1.31 2009/11/22 19:39:52 mrsam Exp $
8 ** Copyright 1998 - 2009 Double Precision, Inc.
9 ** See COPYING for distribution information.
13 #include "../rfc822/config.h" /* VPATH build */
22 #define RFC822_SPECIALS "()<>[]:;@\\,.\""
25 ** The text string we want to parse is first tokenized into an array of
26 ** struct rfc822token records. 'ptr' points into the original text
27 ** string, and 'len' has how many characters from 'ptr' belongs to this
32 struct rfc822token
*next
; /* Unused by librfc822, for use by
40 '<', '>', '@', ',', ';', ':', '.', '[', ']', '%', '!', '=', '?', '/' - RFC atoms.
44 #define rfc822_is_atom(p) ( (p) == 0 || (p) == '"' || (p) == '(' )
46 const char *ptr
; /* Pointer to value for the token. */
47 int len
; /* Length of token value */
51 ** After the struct rfc822token array is built, it is used to create
52 ** the rfc822addr array, which is the array of addresses (plus
53 ** syntactical fluff) extracted from those text strings. Each rfc822addr
54 ** record has several possible interpretation:
56 ** tokens is NULL - syntactical fluff, look in name/nname for tokens
57 ** representing the syntactical fluff ( which is semicolons
60 ** tokens is not NULL - actual address. The tokens representing the actual
61 ** address is in tokens/ntokens. If there are comments in
62 ** the address that are possible "real name" for the address
63 ** they are saved in name/nname (name may be null if there
65 ** If nname is 1, and name points to a comment token,
66 ** the address was specified in old-style format. Otherwise
67 ** the address was specified in new-style route-addr format.
69 ** The tokens and name pointers are set to point to the original rfc822token
74 struct rfc822token
*tokens
;
75 struct rfc822token
*name
;
78 /***************************************************************************
82 ***************************************************************************/
85 struct rfc822token
*tokens
;
89 struct rfc822t
*rfc822t_alloc_new(const char *p
,
90 void (*err_func
)(const char *, int, void *), void *);
93 void rfc822t_free(struct rfc822t
*); /* Free rfc822 structure */
95 void rfc822tok_print(const struct rfc822token
*, void (*)(char, void *), void *);
96 /* Print the tokens */
98 /***************************************************************************
102 ***************************************************************************/
105 struct rfc822addr
*addrs
;
109 struct rfc822a
*rfc822a_alloc(struct rfc822t
*);
110 void rfc822a_free(struct rfc822a
*); /* Free rfc822 structure */
112 void rfc822_deladdr(struct rfc822a
*, int);
114 /* rfc822_print "unparses" the rfc822 structure. Each rfc822addr is "printed"
115 (via the attached function). NOTE: instead of separating addresses by
116 commas, the print_separator function is called.
119 int rfc822_print(const struct rfc822a
*a
,
120 void (*print_func
)(char, void *),
121 void (*print_separator
)(const char *, void *), void *);
123 /* rfc822_print_common is an internal function */
125 int rfc822_print_common(const struct rfc822a
*a
,
126 char *(*decode_func
)(const char *, const char *, int),
128 void (*print_func
)(char, void *),
129 void (*print_separator
)(const char *, void *), void *);
131 /* Extra functions */
133 char *rfc822_gettok(const struct rfc822token
*);
134 char *rfc822_getaddr(const struct rfc822a
*, int);
135 char *rfc822_getaddrs(const struct rfc822a
*);
136 char *rfc822_getaddrs_wrap(const struct rfc822a
*, int);
138 void rfc822_mkdate_buf(time_t, char *);
139 const char *rfc822_mkdate(time_t);
140 time_t rfc822_parsedt(const char *);
142 #define CORESUBJ_RE 1
143 #define CORESUBJ_FWD 2
145 char *rfc822_coresubj(const char *, int *);
146 char *rfc822_coresubj_nouc(const char *, int *);
147 char *rfc822_coresubj_keepblobs(const char *s
);
150 ** Display a header. Takes a raw header value, and formats it for display
151 ** in the given character set.
153 ** hdrname -- header name. Determines whether the header contains addresses,
154 ** or unstructured data.
156 ** hdrvalue -- the actual value to format.
158 ** display_func -- output function.
160 ** err_func -- if this function returns a negative value, to indicate an error,
161 ** this may be called just prior to the error return to indicate where the
162 ** formatting error is, in the original header.
164 ** ptr -- passthrough last argument to display_func or err_func.
166 ** repeatedly invokes display_func to pass the formatted contents.
168 ** Returns 0 upon success, -1 upon a failure.
171 int rfc822_display_hdrvalue(const char *hdrname
,
172 const char *hdrvalue
,
174 void (*display_func
)(const char *, size_t,
176 void (*err_func
)(const char *, int, void *),
180 ** Like rfc822_display_hdrvalue, except that the converted header is saved in
181 ** a malloc-ed buffer. The pointer to the malloc-ed buffer is returned, the
182 ** caller is responsible for free-ing it. An error condition is indicated
183 ** by a NULL return value.
186 char *rfc822_display_hdrvalue_tobuf(const char *hdrname
,
187 const char *hdrvalue
,
189 void (*err_func
)(const char *, int,
194 ** Display a recipient's name in a specific character set.
196 ** The index-th recipient in the address structure is formatted for the given
197 ** character set. If the index-th entry in the address structure is not
198 ** a recipient address (it represents an obsolete list name indicator),
199 ** this function reproduces it literally.
201 ** If the index-th entry in the address structure is a recipient address without
202 ** a name, the address itself is formatted for the given character set.
204 ** If 'charset' is NULL, the name is formatted as is, without converting
205 ** it to any character set.
207 ** A callback function gets repeatedly invoked to produce the name.
209 ** Returns a negative value upon a formatting error.
212 int rfc822_display_name(const struct rfc822a
*rfcp
, int index
,
214 void (*print_func
)(const char *, size_t, void *),
218 ** Display a recipient's name in a specific character set.
220 ** Uses rfc822_display_name to place the generated name into a malloc-ed
221 ** buffer. The caller must free it when it is no longer needed.
223 ** Returns NULL upon an error.
226 char *rfc822_display_name_tobuf(const struct rfc822a
*rfcp
, int index
,
230 ** Display names of all addresses. Each name is followed by a newline
234 int rfc822_display_namelist(const struct rfc822a
*rfcp
,
236 void (*print_func
)(const char *, size_t, void *),
240 ** Display a recipient's address in a specific character set.
242 ** The index-th recipient in the address structure is formatted for the given
243 ** character set. If the index-th entry in the address structure is not
244 ** a recipient address (it represents an obsolete list name indicator),
245 ** this function produces an empty string.
247 ** If 'charset' is NULL, the address is formatted as is, without converting
248 ** it to any character set.
250 ** A callback function gets repeatedly invoked to produce the address.
252 ** Returns a negative value upon a formatting error.
255 int rfc822_display_addr(const struct rfc822a
*rfcp
, int index
,
257 void (*print_func
)(const char *, size_t, void *),
261 ** Like rfc822_display_addr, but the resulting displayable string is
262 ** saved in a buffer. Returns a malloc-ed buffer, the caller is responsible
263 ** for free()ing it. A NULL return indicates an error.
266 char *rfc822_display_addr_tobuf(const struct rfc822a
*rfcp
, int index
,
270 ** Like rfc822_display_addr, but the user@domain gets supplied in a string.
272 int rfc822_display_addr_str(const char *tok
,
274 void (*print_func
)(const char *, size_t, void *),
278 ** Like rfc822_display_addr_str, but the resulting displayable string is
279 ** saved in a buffer. Returns a malloc-ed buffer, the caller is responsible
280 ** for free()ing it. A NULL return indicates an error.
282 char *rfc822_display_addr_str_tobuf(const char *tok
,
286 ** address is a hostname, which is IDN-encoded. 'address' may contain an
287 ** optional 'user@', which is preserved. Returns a malloc-ed buffer, the
288 ** caller is responsible for freeing it.
290 char *rfc822_encode_domain(const char *address
,
291 const char *charset
);