2 * rfc822.c -- code for slicing and dicing RFC822 mail headers
4 * Copyright 1996 by Eric S. Raymond
6 * For license terms, see the file COPYING in this directory.
12 #if defined(STDC_HEADERS)
16 #include "fetchmail.h"
18 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
20 void reply_hack(buf, host)
21 /* hack message headers so replies will work properly */
22 char *buf; /* header to be hacked */
23 const char *host; /* server hostname */
26 int parendepth, state, has_host_part;
27 char mycopy[MSGBUFSIZE+1];
29 if (strncmp("From: ", buf, 6)
30 && strncmp("To: ", buf, 4)
31 && strncmp("Reply-", buf, 6)
32 && strncmp("Cc: ", buf, 4)
33 && strncmp("Bcc: ", buf, 5)) {
38 parendepth = state = 0;
39 has_host_part = FALSE;
40 for (from = mycopy; *from; from++)
43 printf("state %d: %s", state, mycopy);
44 printf("%*s^\n", from - mycopy + 10, " ");
48 else if (*from == ')')
51 if (!parendepth && !has_host_part)
54 case 0: /* before header colon */
59 case 1: /* we've seen the colon, we're looking for addresses */
62 else if (*from == '@')
64 else if ((*from == ',' || HEADER_END(from)) && !has_host_part)
66 while (isspace(*from))
79 case 2: /* we're in a <>-enclosed address */
82 else if (*from == '>' && !has_host_part)
92 /* all characters from the old buffer get copied to the new one */
100 /* parse addresses in succession out of a specified RFC822 header */
101 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
103 static char *tp, address[POPBUFSIZE+1];
104 static const char *hp;
105 static int state, oldstate;
108 #define START_HDR 0 /* before header colon */
109 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
110 #define BARE_ADDRESS 2 /* collecting address without delimiters */
111 #define INSIDE_DQUOTE 3 /* inside double quotes */
112 #define INSIDE_PARENS 4 /* inside parentheses */
113 #define INSIDE_BRACKETS 5 /* inside bracketed address */
114 #define ENDIT_ALL 6 /* after last address */
126 case START_HDR: /* before header colon */
139 case SKIP_JUNK: /* looking for address start */
140 if (HEADER_END(hp)) /* no more addresses */
145 else if (*hp == '\\') /* handle RFC822 escaping */
147 *tp++ = *hp++; /* take the escape */
148 *tp++ = *hp; /* take following char */
150 else if (*hp == '"') /* quoted string */
152 oldstate = SKIP_JUNK;
153 state = INSIDE_DQUOTE;
156 else if (*hp == '(') /* address comment -- ignore */
159 state = INSIDE_PARENS;
161 else if (*hp == '<') /* begin <address> */
163 state = INSIDE_BRACKETS;
166 else if (!isspace(*hp)) /* ignore space */
169 state = BARE_ADDRESS;
173 case BARE_ADDRESS: /* collecting address without delimiters */
174 if (HEADER_END(hp)) /* end of bare address */
178 while (isspace(*--tp))
182 return(tp = address);
185 else if (*hp == '\\') /* handle RFC822 escaping */
187 *tp++ = *hp++; /* take the escape */
188 *tp++ = *hp; /* take following char */
190 else if (*hp == ',') /* end of address */
196 return(tp = address);
199 else if (*hp == '<') /* beginning of real address */
201 state = INSIDE_BRACKETS;
204 else /* just take it */
208 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
209 if (HEADER_END(hp)) /* premature end of string */
214 else if (*hp == '\\') /* handle RFC822 escaping */
216 *tp++ = *hp++; /* take the escape */
217 *tp++ = *hp; /* take following char */
228 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
229 if (HEADER_END(hp)) /* end of line, just bomb out */
234 else if (*hp == '\\') /* handle RFC822 escaping */
236 *tp++ = *hp++; /* take the escape */
237 *tp++ = *hp; /* take following char */
247 case INSIDE_BRACKETS: /* possible <>-enclosed address */
248 if (HEADER_END(hp)) /* end of line, just bomb out */
253 else if (*hp == '\\') /* handle RFC822 escaping */
255 *tp++ = *hp++; /* take the escape */
256 *tp++ = *hp; /* take following char */
258 else if (*hp == '>') /* end of address */
263 return(tp = address);
265 else if (*hp == '<') /* nested <> */
267 else if (*hp == '"') /* quoted address */
270 oldstate = INSIDE_BRACKETS;
271 state = INSIDE_DQUOTE;
273 else /* just copy address */
277 case ENDIT_ALL: /* after last address */
287 main(int argc, char *argv[])
289 char buf[MSGBUFSIZE], *cp;
290 int reply = (argc > 1 && !strcmp(argv[1], "-r"));
292 while (fgets(buf, sizeof(buf)-1, stdin))
294 if (strncmp("From: ", buf, 6)
295 && strncmp("To: ", buf, 4)
296 && strncmp("Reply-", buf, 6)
297 && strncmp("Cc: ", buf, 4)
298 && strncmp("Bcc: ", buf, 5))
305 reply_hack(buf, "HOSTNAME.NET");
306 printf("Rewritten buffer: %s", buf);
309 if ((cp = nxtaddr(buf)) != (char *)NULL)
311 printf("\t%s\n", cp);
313 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
318 #endif /* TESTMAIN */