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;
28 if (strncmp("From: ", buf, 6)
29 && strncmp("To: ", buf, 4)
30 && strncmp("Reply-", buf, 6)
31 && strncmp("Cc: ", buf, 4)
32 && strncmp("Bcc: ", buf, 5)) {
36 parendepth = state = 0;
37 has_host_part = FALSE;
38 for (from = buf; *from; from++)
41 printf("state %d: %s", state, mycopy);
42 printf("%*s^\n", from - mycopy + 10, " ");
47 else if (*from == ')')
50 if (!parendepth && !has_host_part)
53 case 0: /* before header colon */
58 case 1: /* we've seen the colon, we're looking for addresses */
61 else if (*from == '@')
63 else if (*from == '"')
65 else if ((*from == ',' || HEADER_END(from)) && !has_host_part)
69 while (isspace(*from))
72 hostlen = strlen(host);
73 for (cp = from + strlen(from); cp >= from; --cp)
76 memcpy(from, host, hostlen);
82 case 2: /* we're in a string */
87 case 3: /* we're in a <>-enclosed address */
90 else if (*from == '>' && !has_host_part)
94 hostlen = strlen(host);
95 for (cp = from + strlen(from); cp >= from; --cp)
98 memcpy(from, host, hostlen);
100 has_host_part = TRUE;
108 /* parse addresses in succession out of a specified RFC822 header */
109 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
111 static char *tp, address[POPBUFSIZE+1];
112 static const char *hp;
113 static int state, oldstate;
116 #define START_HDR 0 /* before header colon */
117 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
118 #define BARE_ADDRESS 2 /* collecting address without delimiters */
119 #define INSIDE_DQUOTE 3 /* inside double quotes */
120 #define INSIDE_PARENS 4 /* inside parentheses */
121 #define INSIDE_BRACKETS 5 /* inside bracketed address */
122 #define ENDIT_ALL 6 /* after last address */
133 printf("state %d: %s", state, hdr);
134 printf("%*s^\n", hp - hdr + 10, " ");
135 #endif /* TESTMAIN */
137 if (state == ENDIT_ALL) /* after last address */
139 else if (HEADER_END(hp))
142 while (isspace(*--tp))
145 return(tp > address ? (tp = address) : (char *)NULL);
147 else if (*hp == '\\') /* handle RFC822 escaping */
149 *tp++ = *hp++; /* take the escape */
150 *tp++ = *hp; /* take following char */
154 case START_HDR: /* before header colon */
162 case SKIP_JUNK: /* looking for address start */
163 if (*hp == '"') /* quoted string */
165 oldstate = SKIP_JUNK;
166 state = INSIDE_DQUOTE;
169 else if (*hp == '(') /* address comment -- ignore */
172 state = INSIDE_PARENS;
174 else if (*hp == '<') /* begin <address> */
176 state = INSIDE_BRACKETS;
179 else if (!isspace(*hp)) /* ignore space */
182 state = BARE_ADDRESS;
186 case BARE_ADDRESS: /* collecting address without delimiters */
187 if (*hp == ',') /* end of address */
193 return(tp = address);
196 else if (*hp == '(') /* beginning of comment */
199 state = INSIDE_PARENS;
201 else if (*hp == '<') /* beginning of real address */
203 state = INSIDE_BRACKETS;
206 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
210 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
220 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
229 case INSIDE_BRACKETS: /* possible <>-enclosed address */
230 if (*hp == '>') /* end of address */
235 return(tp = address);
237 else if (*hp == '<') /* nested <> */
239 else if (*hp == '"') /* quoted address */
242 oldstate = INSIDE_BRACKETS;
243 state = INSIDE_DQUOTE;
245 else /* just copy address */
255 main(int argc, char *argv[])
257 char buf[MSGBUFSIZE], *cp;
258 int reply = (argc > 1 && !strcmp(argv[1], "-r"));
260 while (fgets(buf, sizeof(buf)-1, stdin))
262 if (strncmp("From: ", buf, 6)
263 && strncmp("To: ", buf, 4)
264 && strncmp("Reply-", buf, 6)
265 && strncmp("Cc: ", buf, 4)
266 && strncmp("Bcc: ", buf, 5))
273 reply_hack(buf, "HOSTNAME.NET");
274 printf("Rewritten buffer: %s", buf);
277 if ((cp = nxtaddr(buf)) != (char *)NULL)
279 printf("\t\"%s\"\n", cp);
281 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
286 #endif /* TESTMAIN */