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, " ");
45 case 0: /* before header colon */
50 case 1: /* we've seen the colon, we're looking for addresses */
53 else if (*from == '(')
58 else if (*from == '<')
60 else if (isalnum(*from))
62 else if (isspace(*from))
66 case 2: /* found a token boundary -- reset without copying */
75 case 3: /* we're in a quoted human name, copy and ignore */
80 case 4: /* we're in a parenthesized human name, copy and ignore */
83 else if (*from == ')')
89 case 5: /* we're in a <>-enclosed address */
92 else if (*from == '>')
102 case 6: /* not string or comment, could be a bare address */
106 /* on proper termination with no @, insert hostname */
107 else if (*from == ',')
116 /* If the address token is not properly terminated, ignore it. */
117 else if (*from == ' ' || *from == '\t')
122 * The only lookahead case. If we're looking at space or tab,
123 * we might be looking at a local name immediately followed
126 for (cp = from; isspace(*cp); cp++)
137 /* everything else, including alphanumerics, just passes through */
140 case 7: /* we're done with this address, skip to end */
149 /* all characters from the old buffer get copied to the new one */
153 /* back up and nuke the appended comma sentinel */
158 /* parse addresses in succession out of a specified RFC822 header */
159 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
161 static char *tp, address[POPBUFSIZE+1];
162 static const char *hp;
163 static int state, oldstate;
167 * Note: it is important that this routine not stop on \r, since
168 * we use \r as a marker for RFC822 continuations elsewhere.
170 #define START_HDR 0 /* before header colon */
171 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
172 #define BARE_ADDRESS 2 /* collecting address without delimiters */
173 #define INSIDE_DQUOTE 3 /* inside double quotes */
174 #define INSIDE_PARENS 4 /* inside parentheses */
175 #define INSIDE_BRACKETS 5 /* inside bracketed address */
176 #define ENDIT_ALL 6 /* after last address */
188 case START_HDR: /* before header colon */
201 case SKIP_JUNK: /* looking for address start */
202 if (*hp == '\n') /* no more addresses */
207 else if (*hp == '\\') /* handle RFC822 escaping */
209 *tp++ = *hp++; /* take the escape */
210 *tp++ = *hp; /* take following char */
212 else if (*hp == '"') /* quoted string */
214 oldstate = SKIP_JUNK;
215 state = INSIDE_DQUOTE;
218 else if (*hp == '(') /* address comment -- ignore */
221 state = INSIDE_PARENS;
223 else if (*hp == '<') /* begin <address> */
225 state = INSIDE_BRACKETS;
228 else if (!isspace(*hp)) /* ignore space */
231 state = BARE_ADDRESS;
235 case BARE_ADDRESS: /* collecting address without delimiters */
236 if (*hp == '\n') /* end of bare address */
242 return(tp = address);
245 else if (*hp == '\\') /* handle RFC822 escaping */
247 *tp++ = *hp++; /* take the escape */
248 *tp++ = *hp; /* take following char */
250 else if (*hp == ',') /* end of address */
256 return(tp = address);
259 else if (*hp == '<') /* beginning of real address */
261 state = INSIDE_BRACKETS;
264 else /* just take it */
268 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
269 if (*hp == '\n') /* premature end of string */
274 else if (*hp == '\\') /* handle RFC822 escaping */
276 *tp++ = *hp++; /* take the escape */
277 *tp++ = *hp; /* take following char */
288 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
289 if (*hp == '\n') /* end of line, just bomb out */
291 else if (*hp == '\\') /* handle RFC822 escaping */
293 *tp++ = *hp++; /* take the escape */
294 *tp++ = *hp; /* take following char */
304 case INSIDE_BRACKETS: /* possible <>-enclosed address */
305 if (*hp == '\\') /* handle RFC822 escaping */
307 *tp++ = *hp++; /* take the escape */
308 *tp++ = *hp; /* take following char */
310 else if (*hp == '>') /* end of address */
315 return(tp = address);
317 else if (*hp == '<') /* nested <> */
319 else if (*hp == '"') /* quoted address */
322 oldstate = INSIDE_BRACKETS;
323 state = INSIDE_DQUOTE;
325 else /* just copy address */
329 case ENDIT_ALL: /* after last address */
339 main(int argc, char *argv[])
341 char buf[POPBUFSIZE], *cp;
342 int reply = (argc > 1 && !strcmp(argv[1], "-r"));
344 while (fgets(buf, sizeof(buf)-1, stdin))
346 if (strncmp("From: ", buf, 6)
347 && strncmp("To: ", buf, 4)
348 && strncmp("Reply-", buf, 6)
349 && strncmp("Cc: ", buf, 4)
350 && strncmp("Bcc: ", buf, 5))
357 reply_hack(buf, "HOSTNAME.NET");
358 printf("Rewritten buffer: %s", buf);
361 if ((cp = nxtaddr(buf)) != (char *)NULL)
363 printf("\t%s\n", cp);
365 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
370 #endif /* TESTMAIN */