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 == ')')
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))
76 case 2: /* we're in a <>-enclosed address */
79 else if (*from == '>' && !has_host_part)
89 /* all characters from the old buffer get copied to the new one */
97 /* parse addresses in succession out of a specified RFC822 header */
98 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
100 static char *tp, address[POPBUFSIZE+1];
101 static const char *hp;
102 static int state, oldstate;
105 #define START_HDR 0 /* before header colon */
106 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
107 #define BARE_ADDRESS 2 /* collecting address without delimiters */
108 #define INSIDE_DQUOTE 3 /* inside double quotes */
109 #define INSIDE_PARENS 4 /* inside parentheses */
110 #define INSIDE_BRACKETS 5 /* inside bracketed address */
111 #define ENDIT_ALL 6 /* after last address */
123 case START_HDR: /* before header colon */
136 case SKIP_JUNK: /* looking for address start */
137 if (HEADER_END(hp)) /* no more addresses */
142 else if (*hp == '\\') /* handle RFC822 escaping */
144 *tp++ = *hp++; /* take the escape */
145 *tp++ = *hp; /* take following char */
147 else if (*hp == '"') /* quoted string */
149 oldstate = SKIP_JUNK;
150 state = INSIDE_DQUOTE;
153 else if (*hp == '(') /* address comment -- ignore */
156 state = INSIDE_PARENS;
158 else if (*hp == '<') /* begin <address> */
160 state = INSIDE_BRACKETS;
163 else if (!isspace(*hp)) /* ignore space */
166 state = BARE_ADDRESS;
170 case BARE_ADDRESS: /* collecting address without delimiters */
171 if (HEADER_END(hp)) /* end of bare address */
175 while (isspace(*--tp))
179 return(tp = address);
182 else if (*hp == '\\') /* handle RFC822 escaping */
184 *tp++ = *hp++; /* take the escape */
185 *tp++ = *hp; /* take following char */
187 else if (*hp == ',') /* end of address */
193 return(tp = address);
196 else if (*hp == '<') /* beginning of real address */
198 state = INSIDE_BRACKETS;
201 else /* just take it */
205 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
206 if (HEADER_END(hp)) /* premature end of string */
211 else if (*hp == '\\') /* handle RFC822 escaping */
213 *tp++ = *hp++; /* take the escape */
214 *tp++ = *hp; /* take following char */
225 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
226 if (HEADER_END(hp)) /* end of line, just bomb out */
231 else if (*hp == '\\') /* handle RFC822 escaping */
233 *tp++ = *hp++; /* take the escape */
234 *tp++ = *hp; /* take following char */
244 case INSIDE_BRACKETS: /* possible <>-enclosed address */
245 if (HEADER_END(hp)) /* end of line, just bomb out */
250 else if (*hp == '\\') /* handle RFC822 escaping */
252 *tp++ = *hp++; /* take the escape */
253 *tp++ = *hp; /* take following char */
255 else if (*hp == '>') /* end of address */
260 return(tp = address);
262 else if (*hp == '<') /* nested <> */
264 else if (*hp == '"') /* quoted address */
267 oldstate = INSIDE_BRACKETS;
268 state = INSIDE_DQUOTE;
270 else /* just copy address */
274 case ENDIT_ALL: /* after last address */
284 main(int argc, char *argv[])
286 char buf[MSGBUFSIZE], *cp;
287 int reply = (argc > 1 && !strcmp(argv[1], "-r"));
289 while (fgets(buf, sizeof(buf)-1, stdin))
291 if (strncmp("From: ", buf, 6)
292 && strncmp("To: ", buf, 4)
293 && strncmp("Reply-", buf, 6)
294 && strncmp("Cc: ", buf, 4)
295 && strncmp("Bcc: ", buf, 5))
302 reply_hack(buf, "HOSTNAME.NET");
303 printf("Rewritten buffer: %s", buf);
306 if ((cp = nxtaddr(buf)) != (char *)NULL)
308 printf("\t%s\n", cp);
310 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
315 #endif /* TESTMAIN */