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'))
24 void reply_hack(buf, host)
25 /* hack message headers so replies will work properly */
26 char *buf; /* header to be hacked */
27 const char *host; /* server hostname */
30 int parendepth, state, has_bare_name_part, has_host_part;
32 if (strncasecmp("From: ", buf, 6)
33 && strncasecmp("To: ", buf, 4)
34 && strncasecmp("Reply-To: ", buf, 10)
35 && strncasecmp("Return-Path: ", buf, 13)
36 && strncasecmp("Cc: ", buf, 4)
37 && strncasecmp("Bcc: ", buf, 5)) {
41 parendepth = state = 0;
42 has_host_part = has_bare_name_part = FALSE;
43 for (from = buf; *from; from++)
48 printf("state %d: %s", state, buf);
49 printf("%*s^\n", from - buf + 10, " ");
55 else if (*from == ')')
58 if (!parendepth && !has_host_part)
61 case 0: /* before header colon */
66 case 1: /* we've seen the colon, we're looking for addresses */
69 else if (*from == '@')
71 else if (*from == '"')
73 else if ((*from == ',' || HEADER_END(from)) && has_bare_name_part && !has_host_part)
77 while (isspace(*from))
80 hostlen = strlen(host);
81 for (cp = from + strlen(from); cp >= from; --cp)
84 memcpy(from, host, hostlen);
88 else if (!isspace(*from))
89 has_bare_name_part = TRUE;
92 case 2: /* we're in a string */
97 case 3: /* we're in a <>-enclosed address */
100 else if (*from == '>' && !has_host_part)
104 hostlen = strlen(host);
105 for (cp = from + strlen(from); cp >= from; --cp)
108 memcpy(from, host, hostlen);
109 from += strlen(from);
110 has_host_part = TRUE;
118 /* parse addresses in succession out of a specified RFC822 header */
119 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
121 static char *tp, address[POPBUFSIZE+1];
122 static const char *hp;
123 static int state, oldstate;
125 static const char *orighdr;
126 #endif /* TESTMAIN */
129 #define START_HDR 0 /* before header colon */
130 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
131 #define BARE_ADDRESS 2 /* collecting address without delimiters */
132 #define INSIDE_DQUOTE 3 /* inside double quotes */
133 #define INSIDE_PARENS 4 /* inside parentheses */
134 #define INSIDE_BRACKETS 5 /* inside bracketed address */
135 #define ENDIT_ALL 6 /* after last address */
143 #endif /* TESTMAIN */
152 printf("state %d: %s", state, orighdr);
153 printf("%*s^\n", hp - orighdr + 10, " ");
155 #endif /* TESTMAIN */
157 if (state == ENDIT_ALL) /* after last address */
159 else if (HEADER_END(hp))
162 while (isspace(*--tp))
165 return(tp > address ? (tp = address) : (char *)NULL);
167 else if (*hp == '\\') /* handle RFC822 escaping */
169 if (state != INSIDE_PARENS)
171 *tp++ = *hp++; /* take the escape */
172 *tp++ = *hp; /* take following char */
177 case START_HDR: /* before header colon */
182 case SKIP_JUNK: /* looking for address start */
183 if (*hp == '"') /* quoted string */
185 oldstate = SKIP_JUNK;
186 state = INSIDE_DQUOTE;
189 else if (*hp == '(') /* address comment -- ignore */
192 state = INSIDE_PARENS;
194 else if (*hp == '<') /* begin <address> */
196 state = INSIDE_BRACKETS;
199 else if (*hp != ',' && !isspace(*hp))
202 state = BARE_ADDRESS;
206 case BARE_ADDRESS: /* collecting address without delimiters */
207 if (*hp == ',') /* end of address */
213 return(tp = address);
216 else if (*hp == '(') /* beginning of comment */
219 state = INSIDE_PARENS;
221 else if (*hp == '<') /* beginning of real address */
223 state = INSIDE_BRACKETS;
226 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
230 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
240 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
249 case INSIDE_BRACKETS: /* possible <>-enclosed address */
250 if (*hp == '>') /* end of address */
255 return(tp = address);
257 else if (*hp == '<') /* nested <> */
259 else if (*hp == '"') /* quoted address */
262 oldstate = INSIDE_BRACKETS;
263 state = INSIDE_DQUOTE;
265 else /* just copy address */
275 static void parsebuf(char *longbuf, int reply)
281 reply_hack(longbuf, "HOSTNAME.NET");
282 printf("Rewritten buffer: %s", longbuf);
285 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
287 printf("\t-> \"%s\"\n", cp);
289 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
294 main(int argc, char *argv[])
296 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
299 verbose = reply = FALSE;
300 while ((ch = getopt(argc, argv, "rv")) != EOF)
312 while (fgets(buf, sizeof(buf)-1, stdin))
314 if (buf[0] == ' ' || buf[0] == '\t')
315 strcat(longbuf, buf);
316 else if (!strncasecmp("From: ", buf, 6)
317 || !strncasecmp("To: ", buf, 4)
318 || !strncasecmp("Reply-", buf, 6)
319 || !strncasecmp("Cc: ", buf, 4)
320 || !strncasecmp("Bcc: ", buf, 5))
321 strcpy(longbuf, buf);
325 fputs(longbuf, stdout);
326 parsebuf(longbuf, reply);
333 fputs(longbuf, stdout);
334 parsebuf(longbuf, reply);
337 #endif /* TESTMAIN */