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 void reply_hack(buf, host)
19 /* hack message headers so replies will work properly */
20 char *buf; /* header to be hacked */
21 const char *host; /* server hostname */
24 int parendepth, state = 0, tokencount = 0;
25 char mycopy[POPBUFSIZE+1];
27 if (strncmp("From: ", buf, 6)
28 && strncmp("To: ", buf, 4)
29 && strncmp("Reply-", buf, 6)
30 && strncmp("Cc: ", buf, 4)
31 && strncmp("Bcc: ", buf, 5)) {
37 for (from = mycopy; *from; from++)
40 printf("state %d: %s", state, mycopy);
41 printf("%*s^\n", from - mycopy + 10, " ");
44 #define INSERT_HOSTNAME \
52 case 0: /* before header colon */
57 case 1: /* we've seen the colon, we're looking for addresses */
60 else if (*from == '(')
65 else if (*from == '<')
67 else if (isalnum(*from))
69 else if (isspace(*from))
73 case 2: /* found a token boundary -- reset without copying */
83 case 3: /* we're in a quoted human name, copy and ignore */
88 case 4: /* we're in a parenthesized human name, copy and ignore */
91 else if (*from == ')')
97 case 5: /* we're in a <>-enclosed address */
100 else if (*from == '>')
106 case 6: /* not string or comment, could be a bare address */
110 else if (*from == '<')
113 /* on proper termination with no @, insert hostname */
114 else if (*from == ',')
120 /* If the address token is not properly terminated, ignore it. */
121 else if (isspace(*from))
126 * The only lookahead case. If we're looking at space or tab,
127 * we might be looking at a local name immediately followed
130 for (cp = from; isspace(*cp); cp++)
138 /* everything else, including alphanumerics, just passes through */
141 case 7: /* we're done with this address, skip to end */
150 /* all characters from the old buffer get copied to the new one */
153 #undef INSERT_HOSTNAME
155 /* back up and nuke the appended comma sentinel */
160 /* parse addresses in succession out of a specified RFC822 header */
161 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
163 static char *tp, address[POPBUFSIZE+1];
164 static const char *hp;
165 static int state, oldstate;
169 * Note: it is important that this routine not stop on \r, since
170 * we use \r as a marker for RFC822 continuations elsewhere.
172 #define START_HDR 0 /* before header colon */
173 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
174 #define BARE_ADDRESS 2 /* collecting address without delimiters */
175 #define INSIDE_DQUOTE 3 /* inside double quotes */
176 #define INSIDE_PARENS 4 /* inside parentheses */
177 #define INSIDE_BRACKETS 5 /* inside bracketed address */
178 #define ENDIT_ALL 6 /* after last address */
190 case START_HDR: /* before header colon */
203 case SKIP_JUNK: /* looking for address start */
204 if (*hp == '\n') /* no more addresses */
209 else if (*hp == '\\') /* handle RFC822 escaping */
211 *tp++ = *hp++; /* take the escape */
212 *tp++ = *hp; /* take following char */
214 else if (*hp == '"') /* quoted string */
216 oldstate = SKIP_JUNK;
217 state = INSIDE_DQUOTE;
220 else if (*hp == '(') /* address comment -- ignore */
223 state = INSIDE_PARENS;
225 else if (*hp == '<') /* begin <address> */
227 state = INSIDE_BRACKETS;
230 else if (!isspace(*hp)) /* ignore space */
233 state = BARE_ADDRESS;
237 case BARE_ADDRESS: /* collecting address without delimiters */
238 if (*hp == '\n') /* end of bare address */
244 return(tp = address);
247 else if (*hp == '\\') /* handle RFC822 escaping */
249 *tp++ = *hp++; /* take the escape */
250 *tp++ = *hp; /* take following char */
252 else if (*hp == ',') /* end of address */
258 return(tp = address);
261 else if (*hp == '<') /* beginning of real address */
263 state = INSIDE_BRACKETS;
266 else /* just take it */
270 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
271 if (*hp == '\n') /* premature end of string */
276 else if (*hp == '\\') /* handle RFC822 escaping */
278 *tp++ = *hp++; /* take the escape */
279 *tp++ = *hp; /* take following char */
290 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
291 if (*hp == '\n') /* end of line, just bomb out */
293 else if (*hp == '\\') /* handle RFC822 escaping */
295 *tp++ = *hp++; /* take the escape */
296 *tp++ = *hp; /* take following char */
306 case INSIDE_BRACKETS: /* possible <>-enclosed address */
307 if (*hp == '\\') /* handle RFC822 escaping */
309 *tp++ = *hp++; /* take the escape */
310 *tp++ = *hp; /* take following char */
312 else if (*hp == '>') /* end of address */
317 return(tp = address);
319 else if (*hp == '<') /* nested <> */
321 else if (*hp == '"') /* quoted address */
324 oldstate = INSIDE_BRACKETS;
325 state = INSIDE_DQUOTE;
327 else /* just copy address */
331 case ENDIT_ALL: /* after last address */
341 main(int argc, char *argv[])
343 char buf[POPBUFSIZE], *cp;
344 int reply = (argc > 1 && !strcmp(argv[1], "-r"));
346 while (fgets(buf, sizeof(buf)-1, stdin))
348 if (strncmp("From: ", buf, 6)
349 && strncmp("To: ", buf, 4)
350 && strncmp("Reply-", buf, 6)
351 && strncmp("Cc: ", buf, 4)
352 && strncmp("Bcc: ", buf, 5))
359 reply_hack(buf, "HOSTNAME.NET");
360 printf("Rewritten buffer: %s", buf);
363 if ((cp = nxtaddr(buf)) != (char *)NULL)
365 printf("\t%s\n", cp);
367 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
372 #endif /* TESTMAIN */