2 * rfc822.c -- code for slicing and dicing RFC822 mail headers
4 * Copyright 1997 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
11 #if defined(STDC_HEADERS)
15 #include "fetchmail.h"
17 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
23 char *reply_hack(buf, host)
24 /* hack message headers so replies will work properly */
25 char *buf; /* header to be hacked */
26 const char *host; /* server hostname */
28 char *from, *cp, last_nws = '\0';
29 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)
38 && strncasecmp("Resent-From: ", buf, 13)
39 && strncasecmp("Resent-To: ", buf, 11)
40 && strncasecmp("Resent-Cc: ", buf, 11)
41 && strncasecmp("Resent-Bcc: ", buf, 12)
42 && strncasecmp("Apparently-From:", buf, 16)
43 && strncasecmp("Apparently-To:", buf, 14)
44 && strncasecmp("Sender:", buf, 7)
45 && strncasecmp("Resent-Sender:", buf, 14)
51 /* make room to hack the address; buf must be malloced */
52 for (cp = buf; *cp; cp++)
53 if (*cp == ',' || isspace(*cp))
55 buf = (char *)xrealloc(buf, strlen(buf) + addresscount * strlen(host) + 1);
59 * This is going to foo up on some ill-formed addresses. For example,
60 * "From: John Smith (Systems) <jsmith@domain>" will get rewritten as
61 * "From: John Smith@my.pop.server (Systems) <jsmith@domain>" because
62 * the state machine can't look ahead to the <> part past the comment
63 * and instead treats `John Smith' as a bareword address.
66 parendepth = state = 0;
67 has_host_part = has_bare_name_part = FALSE;
68 for (from = buf; *from; from++)
73 printf("state %d: %s", state, buf);
74 printf("%*s^\n", from - buf + 10, " ");
80 else if (*from == ')')
83 if (!parendepth && !has_host_part)
86 case 0: /* before header colon */
91 case 1: /* we've seen the colon, we're looking for addresses */
96 else if (*from == '@')
98 else if (*from == '"')
101 * Not expanding on last non-WS == ';' deals with groupnames,
102 * an obscure misfeature described in sections
103 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
105 else if ((*from == ',' || HEADER_END(from) || from[1] == '(')
106 && has_bare_name_part
108 && last_nws != ';' && last_nws != ')')
112 while (isspace(*from) || (*from == ','))
115 hostlen = strlen(host);
116 for (cp = from + strlen(from); cp >= from; --cp)
119 memcpy(from, host, hostlen);
121 has_host_part = TRUE;
123 else if (!isspace(*from))
124 has_bare_name_part = TRUE;
127 case 2: /* we're in a string */
132 case 3: /* we're in a <>-enclosed address */
134 has_host_part = TRUE;
135 else if (*from == '>')
142 hostlen = strlen(host);
143 for (cp = from + strlen(from); cp >= from; --cp)
146 memcpy(from, host, hostlen);
148 has_host_part = TRUE;
155 * If we passed a comma, reset everything.
157 if (from[-1] == ',' && !parendepth) {
158 has_host_part = has_bare_name_part = FALSE;
166 /* parse addresses in succession out of a specified RFC822 header */
167 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
169 static char *tp, address[POPBUFSIZE+1];
170 static const char *hp;
171 static int state, oldstate;
173 static const char *orighdr;
174 #endif /* TESTMAIN */
177 #define START_HDR 0 /* before header colon */
178 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
179 #define BARE_ADDRESS 2 /* collecting address without delimiters */
180 #define INSIDE_DQUOTE 3 /* inside double quotes */
181 #define INSIDE_PARENS 4 /* inside parentheses */
182 #define INSIDE_BRACKETS 5 /* inside bracketed address */
183 #define ENDIT_ALL 6 /* after last address */
191 #endif /* TESTMAIN */
200 printf("state %d: %s", state, orighdr);
201 printf("%*s^\n", hp - orighdr + 10, " ");
203 #endif /* TESTMAIN */
205 if (state == ENDIT_ALL) /* after last address */
207 else if (HEADER_END(hp))
212 while (isspace(*--tp))
216 return(tp > address ? (tp = address) : (char *)NULL);
218 else if (*hp == '\\') /* handle RFC822 escaping */
220 if (state != INSIDE_PARENS)
222 *tp++ = *hp++; /* take the escape */
223 *tp++ = *hp; /* take following char */
228 case START_HDR: /* before header colon */
233 case SKIP_JUNK: /* looking for address start */
234 if (*hp == '"') /* quoted string */
236 oldstate = SKIP_JUNK;
237 state = INSIDE_DQUOTE;
240 else if (*hp == '(') /* address comment -- ignore */
243 oldstate = SKIP_JUNK;
244 state = INSIDE_PARENS;
246 else if (*hp == '<') /* begin <address> */
248 state = INSIDE_BRACKETS;
251 else if (*hp != ',' && !isspace(*hp))
254 state = BARE_ADDRESS;
258 case BARE_ADDRESS: /* collecting address without delimiters */
259 if (*hp == ',') /* end of address */
265 return(tp = address);
268 else if (*hp == '(') /* beginning of comment */
271 oldstate = BARE_ADDRESS;
272 state = INSIDE_PARENS;
274 else if (*hp == '<') /* beginning of real address */
276 state = INSIDE_BRACKETS;
279 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
283 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
293 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
302 case INSIDE_BRACKETS: /* possible <>-enclosed address */
303 if (*hp == '>') /* end of address */
308 return(tp = address);
310 else if (*hp == '<') /* nested <> */
312 else if (*hp == '"') /* quoted address */
315 oldstate = INSIDE_BRACKETS;
316 state = INSIDE_DQUOTE;
318 else /* just copy address */
328 static void parsebuf(char *longbuf, int reply)
334 reply_hack(longbuf, "HOSTNAME.NET");
335 printf("Rewritten buffer: %s", longbuf);
338 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
340 printf("\t-> \"%s\"\n", cp);
342 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
347 main(int argc, char *argv[])
349 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
352 verbose = reply = FALSE;
353 while ((ch = getopt(argc, argv, "rv")) != EOF)
365 while (fgets(buf, sizeof(buf)-1, stdin))
367 if (buf[0] == ' ' || buf[0] == '\t')
368 strcat(longbuf, buf);
369 else if (!strncasecmp("From: ", buf, 6)
370 || !strncasecmp("To: ", buf, 4)
371 || !strncasecmp("Reply-", buf, 6)
372 || !strncasecmp("Cc: ", buf, 4)
373 || !strncasecmp("Bcc: ", buf, 5))
374 strcpy(longbuf, buf);
378 fputs(longbuf, stdout);
379 parsebuf(longbuf, reply);
386 fputs(longbuf, stdout);
387 parsebuf(longbuf, reply);
390 #endif /* TESTMAIN */