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.
65 * Also note that we don't rewrite the fake address <> in order to
66 * avoid screwing up bounce suppression with a null Return-Path.
69 parendepth = state = 0;
70 has_host_part = has_bare_name_part = FALSE;
71 for (from = buf; *from; from++)
76 printf("state %d: %s", state, buf);
77 printf("%*s^\n", from - buf + 10, " ");
83 else if (*from == ')')
86 if (!parendepth && !has_host_part)
89 case 0: /* before header colon */
94 case 1: /* we've seen the colon, we're looking for addresses */
99 else if (*from == '@')
100 has_host_part = TRUE;
101 else if (*from == '"')
104 * Not expanding on last non-WS == ';' deals with groupnames,
105 * an obscure misfeature described in sections
106 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
108 else if ((*from == ',' || HEADER_END(from) || from[1] == '(')
109 && has_bare_name_part
111 && last_nws != ';' && last_nws != ')')
115 while (isspace(*from) || (*from == ','))
118 hostlen = strlen(host);
119 for (cp = from + strlen(from); cp >= from; --cp)
122 memcpy(from, host, hostlen);
124 has_host_part = TRUE;
126 else if (!isspace(*from))
127 has_bare_name_part = TRUE;
130 case 2: /* we're in a string */
135 case 3: /* we're in a <>-enclosed address */
137 has_host_part = TRUE;
138 else if (*from == '>' && from[-1] != '<')
145 hostlen = strlen(host);
146 for (cp = from + strlen(from); cp >= from; --cp)
149 memcpy(from, host, hostlen);
151 has_host_part = TRUE;
158 * If we passed a comma, reset everything.
160 if (from[-1] == ',' && !parendepth) {
161 has_host_part = has_bare_name_part = FALSE;
169 /* parse addresses in succession out of a specified RFC822 header */
170 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
172 static char *tp, address[POPBUFSIZE+1];
173 static const char *hp;
174 static int state, oldstate;
176 static const char *orighdr;
177 #endif /* TESTMAIN */
180 #define START_HDR 0 /* before header colon */
181 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
182 #define BARE_ADDRESS 2 /* collecting address without delimiters */
183 #define INSIDE_DQUOTE 3 /* inside double quotes */
184 #define INSIDE_PARENS 4 /* inside parentheses */
185 #define INSIDE_BRACKETS 5 /* inside bracketed address */
186 #define ENDIT_ALL 6 /* after last address */
194 #endif /* TESTMAIN */
203 printf("state %d: %s", state, orighdr);
204 printf("%*s^\n", hp - orighdr + 10, " ");
206 #endif /* TESTMAIN */
208 if (state == ENDIT_ALL) /* after last address */
210 else if (HEADER_END(hp))
215 while (isspace(*--tp))
219 return(tp > address ? (tp = address) : (char *)NULL);
221 else if (*hp == '\\') /* handle RFC822 escaping */
223 if (state != INSIDE_PARENS)
225 *tp++ = *hp++; /* take the escape */
226 *tp++ = *hp; /* take following char */
231 case START_HDR: /* before header colon */
236 case SKIP_JUNK: /* looking for address start */
237 if (*hp == '"') /* quoted string */
239 oldstate = SKIP_JUNK;
240 state = INSIDE_DQUOTE;
243 else if (*hp == '(') /* address comment -- ignore */
246 oldstate = SKIP_JUNK;
247 state = INSIDE_PARENS;
249 else if (*hp == '<') /* begin <address> */
251 state = INSIDE_BRACKETS;
254 else if (*hp != ',' && !isspace(*hp))
257 state = BARE_ADDRESS;
261 case BARE_ADDRESS: /* collecting address without delimiters */
262 if (*hp == ',') /* end of address */
268 return(tp = address);
271 else if (*hp == '(') /* beginning of comment */
274 oldstate = BARE_ADDRESS;
275 state = INSIDE_PARENS;
277 else if (*hp == '<') /* beginning of real address */
279 state = INSIDE_BRACKETS;
282 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
286 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
296 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
305 case INSIDE_BRACKETS: /* possible <>-enclosed address */
306 if (*hp == '>') /* end of address */
311 return(tp = address);
313 else if (*hp == '<') /* nested <> */
315 else if (*hp == '"') /* quoted address */
318 oldstate = INSIDE_BRACKETS;
319 state = INSIDE_DQUOTE;
321 else /* just copy address */
331 static void parsebuf(char *longbuf, int reply)
337 reply_hack(longbuf, "HOSTNAME.NET");
338 printf("Rewritten buffer: %s", longbuf);
341 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
343 printf("\t-> \"%s\"\n", cp);
345 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
350 main(int argc, char *argv[])
352 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
355 verbose = reply = FALSE;
356 while ((ch = getopt(argc, argv, "rv")) != EOF)
368 while (fgets(buf, sizeof(buf)-1, stdin))
370 if (buf[0] == ' ' || buf[0] == '\t')
371 strcat(longbuf, buf);
372 else if (!strncasecmp("From: ", buf, 6)
373 || !strncasecmp("To: ", buf, 4)
374 || !strncasecmp("Reply-", buf, 6)
375 || !strncasecmp("Cc: ", buf, 4)
376 || !strncasecmp("Bcc: ", buf, 5))
377 strcpy(longbuf, buf);
381 fputs(longbuf, stdout);
382 parsebuf(longbuf, reply);
389 fputs(longbuf, stdout);
390 parsebuf(longbuf, reply);
393 #endif /* TESTMAIN */