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)
16 #include "fetchmail.h"
18 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
22 char *program_name = "rfc822";
25 char *reply_hack(buf, host)
26 /* hack message headers so replies will work properly */
27 char *buf; /* header to be hacked */
28 const char *host; /* server hostname */
30 char *from, *cp, last_nws = '\0', *parens_from = NULL;
31 int parendepth, state, has_bare_name_part, has_host_part;
36 if (strncasecmp("From: ", buf, 6)
37 && strncasecmp("To: ", buf, 4)
38 && strncasecmp("Reply-To: ", buf, 10)
39 && strncasecmp("Return-Path: ", buf, 13)
40 && strncasecmp("Cc: ", buf, 4)
41 && strncasecmp("Bcc: ", buf, 5)
42 && strncasecmp("Resent-From: ", buf, 13)
43 && strncasecmp("Resent-To: ", buf, 11)
44 && strncasecmp("Resent-Cc: ", buf, 11)
45 && strncasecmp("Resent-Bcc: ", buf, 12)
46 && strncasecmp("Apparently-From:", buf, 16)
47 && strncasecmp("Apparently-To:", buf, 14)
48 && strncasecmp("Sender:", buf, 7)
49 && strncasecmp("Resent-Sender:", buf, 14)
55 if (outlevel >= O_DEBUG)
56 error_build("About to rewrite %s", buf);
58 /* make room to hack the address; buf must be malloced */
59 for (cp = buf; *cp; cp++)
60 if (*cp == ',' || isspace(*cp))
62 buf = (char *)xrealloc(buf, strlen(buf) + addresscount * strlen(host) + 1);
66 * This is going to foo up on some ill-formed addresses.
67 * Note that we don't rewrite the fake address <> in order to
68 * avoid screwing up bounce suppression with a null Return-Path.
71 parendepth = state = 0;
72 has_host_part = has_bare_name_part = FALSE;
73 for (from = buf; *from; from++)
78 printf("state %d: %s", state, buf);
79 printf("%*s^\n", from - buf + 10, " ");
85 else if (*from == ')')
88 if (!parendepth && !has_host_part)
91 case 0: /* before header colon */
96 case 1: /* we've seen the colon, we're looking for addresses */
101 else if (*from == '@')
102 has_host_part = TRUE;
103 else if (*from == '"')
106 * Not expanding on last non-WS == ';' deals with groupnames,
107 * an obscure misfeature described in sections
108 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
110 else if ((*from == ',' || HEADER_END(from))
111 && has_bare_name_part
121 while (isspace(*from) || (*from == ','))
124 hostlen = strlen(host);
125 for (cp = from + strlen(from); cp >= from; --cp)
128 memcpy(from, host, hostlen);
129 from = p + hostlen + 1;
130 has_host_part = TRUE;
132 else if (from[1] == '('
133 && has_bare_name_part
135 && last_nws != ';' && last_nws != ')')
139 else if (!isspace(*from))
140 has_bare_name_part = TRUE;
143 case 2: /* we're in a string */
148 case 3: /* we're in a <>-enclosed address */
150 has_host_part = TRUE;
151 else if (*from == '>' && from[-1] != '<')
158 hostlen = strlen(host);
159 for (cp = from + strlen(from); cp >= from; --cp)
162 memcpy(from, host, hostlen);
164 has_host_part = TRUE;
171 * If we passed a comma, reset everything.
173 if (from[-1] == ',' && !parendepth) {
174 has_host_part = has_bare_name_part = FALSE;
180 if (outlevel >= O_DEBUG)
181 error_complete(0, 0, "Rewritten version is %s", buf);
182 #endif /* TESTMAIN */
187 /* parse addresses in succession out of a specified RFC822 header */
188 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
190 static char *tp, address[POPBUFSIZE+1];
191 static const char *hp;
192 static int state, oldstate;
194 static const char *orighdr;
195 #endif /* TESTMAIN */
198 #define START_HDR 0 /* before header colon */
199 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
200 #define BARE_ADDRESS 2 /* collecting address without delimiters */
201 #define INSIDE_DQUOTE 3 /* inside double quotes */
202 #define INSIDE_PARENS 4 /* inside parentheses */
203 #define INSIDE_BRACKETS 5 /* inside bracketed address */
204 #define ENDIT_ALL 6 /* after last address */
212 #endif /* TESTMAIN */
221 printf("state %d: %s", state, orighdr);
222 printf("%*s^\n", hp - orighdr + 10, " ");
224 #endif /* TESTMAIN */
226 if (state == ENDIT_ALL) /* after last address */
228 else if (HEADER_END(hp))
233 while (isspace(*--tp))
237 return(tp > address ? (tp = address) : (char *)NULL);
239 else if (*hp == '\\') /* handle RFC822 escaping */
241 if (state != INSIDE_PARENS)
243 *tp++ = *hp++; /* take the escape */
244 *tp++ = *hp; /* take following char */
249 case START_HDR: /* before header colon */
254 case SKIP_JUNK: /* looking for address start */
255 if (*hp == '"') /* quoted string */
257 oldstate = SKIP_JUNK;
258 state = INSIDE_DQUOTE;
261 else if (*hp == '(') /* address comment -- ignore */
264 oldstate = SKIP_JUNK;
265 state = INSIDE_PARENS;
267 else if (*hp == '<') /* begin <address> */
269 state = INSIDE_BRACKETS;
272 else if (*hp != ',' && !isspace(*hp))
275 state = BARE_ADDRESS;
279 case BARE_ADDRESS: /* collecting address without delimiters */
280 if (*hp == ',') /* end of address */
286 return(tp = address);
289 else if (*hp == '(') /* beginning of comment */
292 oldstate = BARE_ADDRESS;
293 state = INSIDE_PARENS;
295 else if (*hp == '<') /* beginning of real address */
297 state = INSIDE_BRACKETS;
300 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
304 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
314 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
323 case INSIDE_BRACKETS: /* possible <>-enclosed address */
324 if (*hp == '>') /* end of address */
329 return(tp = address);
331 else if (*hp == '<') /* nested <> */
333 else if (*hp == '"') /* quoted address */
336 oldstate = INSIDE_BRACKETS;
337 state = INSIDE_DQUOTE;
339 else /* just copy address */
349 static void parsebuf(char *longbuf, int reply)
355 reply_hack(longbuf, "HOSTNAME.NET");
356 printf("Rewritten buffer: %s", longbuf);
359 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
361 printf("\t-> \"%s\"\n", cp);
363 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
368 main(int argc, char *argv[])
370 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
373 verbose = reply = FALSE;
374 while ((ch = getopt(argc, argv, "rv")) != EOF)
386 while (fgets(buf, sizeof(buf)-1, stdin))
388 if (buf[0] == ' ' || buf[0] == '\t')
389 strcat(longbuf, buf);
390 else if (!strncasecmp("From: ", buf, 6)
391 || !strncasecmp("To: ", buf, 4)
392 || !strncasecmp("Reply-", buf, 6)
393 || !strncasecmp("Cc: ", buf, 4)
394 || !strncasecmp("Bcc: ", buf, 5))
395 strcpy(longbuf, buf);
399 fputs(longbuf, stdout);
400 parsebuf(longbuf, reply);
407 fputs(longbuf, stdout);
408 parsebuf(longbuf, reply);
411 #endif /* TESTMAIN */