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
|
/*
*/
#ifndef rfc822_h
#define rfc822_h
/*
** Copyright 1998 - 2009 Double Precision, Inc.
** See COPYING for distribution information.
*/
#if HAVE_CONFIG_H
#include "rfc822/config.h"
#endif
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
#define RFC822_SPECIALS "()<>[]:;@\\,.\""
/*
** The text string we want to parse is first tokenized into an array of
** struct rfc822token records. 'ptr' points into the original text
** string, and 'len' has how many characters from 'ptr' belongs to this
** token.
*/
struct rfc822token {
struct rfc822token *next; /* Unused by librfc822, for use by
** clients */
int token;
/*
Values for token:
'(' - comment
'"' - quoted string
'<', '>', '@', ',', ';', ':', '.', '[', ']', '%', '!', '=', '?', '/' - RFC atoms.
0 - atom
*/
#define rfc822_is_atom(p) ( (p) == 0 || (p) == '"' || (p) == '(' )
const char *ptr; /* Pointer to value for the token. */
int len; /* Length of token value */
} ;
/*
** After the struct rfc822token array is built, it is used to create
** the rfc822addr array, which is the array of addresses (plus
** syntactical fluff) extracted from those text strings. Each rfc822addr
** record has several possible interpretation:
**
** tokens is NULL - syntactical fluff, look in name/nname for tokens
** representing the syntactical fluff ( which is semicolons
** and list name:
**
** tokens is not NULL - actual address. The tokens representing the actual
** address is in tokens/ntokens. If there are comments in
** the address that are possible "real name" for the address
** they are saved in name/nname (name may be null if there
** is none).
** If nname is 1, and name points to a comment token,
** the address was specified in old-style format. Otherwise
** the address was specified in new-style route-addr format.
**
** The tokens and name pointers are set to point to the original rfc822token
** array.
*/
struct rfc822addr {
struct rfc822token *tokens;
struct rfc822token *name;
} ;
/***************************************************************************
**
** rfc822 tokens
**
***************************************************************************/
struct rfc822t {
struct rfc822token *tokens;
int ntokens;
} ;
struct rfc822t *rfc822t_alloc_new(const char *p,
void (*err_func)(const char *, int, void *), void *);
/* Parse addresses */
void rfc822t_free(struct rfc822t *); /* Free rfc822 structure */
void rfc822tok_print(const struct rfc822token *, void (*)(char, void *), void *);
/* Print the tokens */
/***************************************************************************
**
** rfc822 addresses
**
***************************************************************************/
struct rfc822a {
struct rfc822addr *addrs;
int naddrs;
} ;
struct rfc822a *rfc822a_alloc(struct rfc822t *);
void rfc822a_free(struct rfc822a *); /* Free rfc822 structure */
void rfc822_deladdr(struct rfc822a *, int);
/* rfc822_print "unparses" the rfc822 structure. Each rfc822addr is "printed"
(via the attached function). NOTE: instead of separating addresses by
commas, the print_separator function is called.
*/
int rfc822_print(const struct rfc822a *a,
void (*print_func)(char, void *),
void (*print_separator)(const char *, void *), void *);
/* rfc822_print_common is an internal function */
int rfc822_print_common(const struct rfc822a *a,
char *(*decode_func)(const char *, const char *, int),
const char *chset,
void (*print_func)(char, void *),
void (*print_separator)(const char *, void *), void *);
/* Extra functions */
char *rfc822_gettok(const struct rfc822token *);
char *rfc822_getaddr(const struct rfc822a *, int);
char *rfc822_getaddrs(const struct rfc822a *);
char *rfc822_getaddrs_wrap(const struct rfc822a *, int);
void rfc822_mkdate_buf(time_t, char *);
const char *rfc822_mkdate(time_t);
int rfc822_parsedate_chk(const char *, time_t *);
#define CORESUBJ_RE 1
#define CORESUBJ_FWD 2
char *rfc822_coresubj(const char *, int *);
char *rfc822_coresubj_nouc(const char *, int *);
char *rfc822_coresubj_keepblobs(const char *s);
/*
** Display a header. Takes a raw header value, and formats it for display
** in the given character set.
**
** hdrname -- header name. Determines whether the header contains addresses,
** or unstructured data.
**
** hdrvalue -- the actual value to format.
**
** display_func -- output function.
**
** err_func -- if this function returns a negative value, to indicate an error,
** this may be called just prior to the error return to indicate where the
** formatting error is, in the original header.
**
** ptr -- passthrough last argument to display_func or err_func.
**
** repeatedly invokes display_func to pass the formatted contents.
**
** Returns 0 upon success, -1 upon a failure.
*/
int rfc822_display_hdrvalue(const char *hdrname,
const char *hdrvalue,
const char *charset,
void (*display_func)(const char *, size_t,
void *),
void (*err_func)(const char *, int, void *),
void *ptr);
/*
** Like rfc822_display_hdrvalue, except that the converted header is saved in
** a malloc-ed buffer. The pointer to the malloc-ed buffer is returned, the
** caller is responsible for free-ing it. An error condition is indicated
** by a NULL return value.
*/
char *rfc822_display_hdrvalue_tobuf(const char *hdrname,
const char *hdrvalue,
const char *charset,
void (*err_func)(const char *, int,
void *),
void *ptr);
/*
** Display a recipient's name in a specific character set.
**
** The index-th recipient in the address structure is formatted for the given
** character set. If the index-th entry in the address structure is not
** a recipient address (it represents an obsolete list name indicator),
** this function reproduces it literally.
**
** If the index-th entry in the address structure is a recipient address without
** a name, the address itself is formatted for the given character set.
**
** If 'charset' is NULL, the name is formatted as is, without converting
** it to any character set.
**
** A callback function gets repeatedly invoked to produce the name.
**
** Returns a negative value upon a formatting error.
*/
int rfc822_display_name(const struct rfc822a *rfcp, int index,
const char *chset,
void (*print_func)(const char *, size_t, void *),
void *ptr);
/*
** Display a recipient's name in a specific character set.
**
** Uses rfc822_display_name to place the generated name into a malloc-ed
** buffer. The caller must free it when it is no longer needed.
**
** Returns NULL upon an error.
*/
char *rfc822_display_name_tobuf(const struct rfc822a *rfcp, int index,
const char *chset);
/*
** Display names of all addresses. Each name is followed by a newline
** character.
**
*/
int rfc822_display_namelist(const struct rfc822a *rfcp,
const char *chset,
void (*print_func)(const char *, size_t, void *),
void *ptr);
/*
** Display a recipient's address in a specific character set.
**
** The index-th recipient in the address structure is formatted for the given
** character set. If the index-th entry in the address structure is not
** a recipient address (it represents an obsolete list name indicator),
** this function produces an empty string.
**
** If 'charset' is NULL, the address is formatted as is, without converting
** it to any character set.
**
** A callback function gets repeatedly invoked to produce the address.
**
** Returns a negative value upon a formatting error.
*/
int rfc822_display_addr(const struct rfc822a *rfcp, int index,
const char *chset,
void (*print_func)(const char *, size_t, void *),
void *ptr);
/*
** Like rfc822_display_addr, but the resulting displayable string is
** saved in a buffer. Returns a malloc-ed buffer, the caller is responsible
** for free()ing it. A NULL return indicates an error.
*/
char *rfc822_display_addr_tobuf(const struct rfc822a *rfcp, int index,
const char *chset);
/*
** Like rfc822_display_addr, but the user@domain gets supplied in a string.
*/
int rfc822_display_addr_str(const char *tok,
const char *chset,
void (*print_func)(const char *, size_t, void *),
void *ptr);
/*
** Like rfc822_display_addr_str, but the resulting displayable string is
** saved in a buffer. Returns a malloc-ed buffer, the caller is responsible
** for free()ing it. A NULL return indicates an error.
*/
char *rfc822_display_addr_str_tobuf(const char *tok,
const char *chset);
/*
** address is a hostname, which is IDN-encoded. 'address' may contain an
** optional 'user@', which is preserved. Returns a malloc-ed buffer, the
** caller is responsible for freeing it.
*/
char *rfc822_encode_domain(const char *address,
const char *charset);
#ifdef __cplusplus
}
#endif
#endif
|