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, has_host_part;
25 char mycopy[MSGBUFSIZE+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)) {
36 parendepth = state = 0;
37 has_host_part = FALSE;
38 for (from = mycopy; *from; from++)
41 printf("state %d: %s", state, mycopy);
42 printf("%*s^\n", from - mycopy + 10, " ");
46 else if (*from == ')')
52 case 0: /* before header colon */
57 case 1: /* we've seen the colon, we're looking for addresses */
60 else if (*from == '@')
62 else if ((*from == ',' || *from == '\n') && !has_host_part)
70 case 2: /* we're in a <>-enclosed address */
73 else if (*from == '>' && !has_host_part)
83 /* all characters from the old buffer get copied to the new one */
91 /* parse addresses in succession out of a specified RFC822 header */
92 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
94 static char *tp, address[POPBUFSIZE+1];
95 static const char *hp;
96 static int state, oldstate;
100 * Note: it is important that this routine not stop on \r, since
101 * we use \r as a marker for RFC822 continuations elsewhere.
103 #define START_HDR 0 /* before header colon */
104 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
105 #define BARE_ADDRESS 2 /* collecting address without delimiters */
106 #define INSIDE_DQUOTE 3 /* inside double quotes */
107 #define INSIDE_PARENS 4 /* inside parentheses */
108 #define INSIDE_BRACKETS 5 /* inside bracketed address */
109 #define ENDIT_ALL 6 /* after last address */
121 case START_HDR: /* before header colon */
134 case SKIP_JUNK: /* looking for address start */
135 if (*hp == '\n') /* no more addresses */
140 else if (*hp == '\\') /* handle RFC822 escaping */
142 *tp++ = *hp++; /* take the escape */
143 *tp++ = *hp; /* take following char */
145 else if (*hp == '"') /* quoted string */
147 oldstate = SKIP_JUNK;
148 state = INSIDE_DQUOTE;
151 else if (*hp == '(') /* address comment -- ignore */
154 state = INSIDE_PARENS;
156 else if (*hp == '<') /* begin <address> */
158 state = INSIDE_BRACKETS;
161 else if (!isspace(*hp)) /* ignore space */
164 state = BARE_ADDRESS;
168 case BARE_ADDRESS: /* collecting address without delimiters */
169 if (*hp == '\n') /* end of bare address */
175 return(tp = address);
178 else if (*hp == '\\') /* handle RFC822 escaping */
180 *tp++ = *hp++; /* take the escape */
181 *tp++ = *hp; /* take following char */
183 else if (*hp == ',') /* end of address */
189 return(tp = address);
192 else if (*hp == '<') /* beginning of real address */
194 state = INSIDE_BRACKETS;
197 else /* just take it */
201 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
202 if (*hp == '\n') /* premature end of string */
207 else if (*hp == '\\') /* handle RFC822 escaping */
209 *tp++ = *hp++; /* take the escape */
210 *tp++ = *hp; /* take following char */
221 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
222 if (*hp == '\n') /* end of line, just bomb out */
224 else if (*hp == '\\') /* handle RFC822 escaping */
226 *tp++ = *hp++; /* take the escape */
227 *tp++ = *hp; /* take following char */
237 case INSIDE_BRACKETS: /* possible <>-enclosed address */
238 if (*hp == '\\') /* handle RFC822 escaping */
240 *tp++ = *hp++; /* take the escape */
241 *tp++ = *hp; /* take following char */
243 else if (*hp == '>') /* end of address */
248 return(tp = address);
250 else if (*hp == '<') /* nested <> */
252 else if (*hp == '"') /* quoted address */
255 oldstate = INSIDE_BRACKETS;
256 state = INSIDE_DQUOTE;
258 else /* just copy address */
262 case ENDIT_ALL: /* after last address */
272 main(int argc, char *argv[])
274 char buf[MSGBUFSIZE], *cp;
275 int reply = (argc > 1 && !strcmp(argv[1], "-r"));
277 while (fgets(buf, sizeof(buf)-1, stdin))
279 if (strncmp("From: ", buf, 6)
280 && strncmp("To: ", buf, 4)
281 && strncmp("Reply-", buf, 6)
282 && strncmp("Cc: ", buf, 4)
283 && strncmp("Bcc: ", buf, 5))
290 reply_hack(buf, "HOSTNAME.NET");
291 printf("Rewritten buffer: %s", buf);
294 if ((cp = nxtaddr(buf)) != (char *)NULL)
296 printf("\t%s\n", cp);
298 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
303 #endif /* TESTMAIN */