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, 5)
37 && strncasecmp("To:", buf, 3)
38 && strncasecmp("Reply-To:", buf, 9)
39 && strncasecmp("Return-Path:", buf, 12)
40 && strncasecmp("Cc:", buf, 3)
41 && strncasecmp("Bcc:", buf, 4)
42 && strncasecmp("Resent-From:", buf, 12)
43 && strncasecmp("Resent-To:", buf, 10)
44 && strncasecmp("Resent-Cc:", buf, 10)
45 && strncasecmp("Resent-Bcc:", buf, 11)
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 report_build(stdout, "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, " ");
86 else if (*from == ')')
90 if (!parendepth && !has_host_part)
93 case 0: /* before header colon */
98 case 1: /* we've seen the colon, we're looking for addresses */
103 else if (*from == '@')
104 has_host_part = TRUE;
105 else if (*from == '"')
108 * Not expanding on last non-WS == ';' deals with groupnames,
109 * an obscure misfeature described in sections
110 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
112 else if ((*from == ',' || HEADER_END(from))
113 && has_bare_name_part
123 while (isspace(*from) || (*from == ','))
126 hostlen = strlen(host);
127 for (cp = from + strlen(from); cp >= from; --cp)
130 memcpy(from, host, hostlen);
131 from = p + hostlen + 1;
132 has_host_part = TRUE;
134 else if (from[1] == '('
135 && has_bare_name_part
137 && last_nws != ';' && last_nws != ')')
141 else if (!isspace(*from))
142 has_bare_name_part = TRUE;
145 case 2: /* we're in a string */
150 case 3: /* we're in a <>-enclosed address */
152 has_host_part = TRUE;
153 else if (*from == '>' && from[-1] != '<')
160 hostlen = strlen(host);
161 for (cp = from + strlen(from); cp >= from; --cp)
164 memcpy(from, host, hostlen);
166 has_host_part = TRUE;
173 * If we passed a comma, reset everything.
175 if (from[-1] == ',' && !parendepth) {
176 has_host_part = has_bare_name_part = FALSE;
182 if (outlevel >= O_DEBUG)
183 report_complete(stdout, "Rewritten version is %s\n", buf);
184 #endif /* TESTMAIN */
189 /* parse addresses in succession out of a specified RFC822 header */
190 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
192 static char *tp, address[POPBUFSIZE+1];
193 static const char *hp;
194 static int state, oldstate;
196 static const char *orighdr;
197 #endif /* TESTMAIN */
200 #define START_HDR 0 /* before header colon */
201 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
202 #define BARE_ADDRESS 2 /* collecting address without delimiters */
203 #define INSIDE_DQUOTE 3 /* inside double quotes */
204 #define INSIDE_PARENS 4 /* inside parentheses */
205 #define INSIDE_BRACKETS 5 /* inside bracketed address */
206 #define ENDIT_ALL 6 /* after last address */
214 #endif /* TESTMAIN */
223 printf("state %d: %s", state, orighdr);
224 printf("%*s^\n", hp - orighdr + 10, " ");
226 #endif /* TESTMAIN */
228 if (state == ENDIT_ALL) /* after last address */
230 else if (HEADER_END(hp))
235 while (isspace(*--tp))
239 return(tp > address ? (tp = address) : (char *)NULL);
241 else if (*hp == '\\') /* handle RFC822 escaping */
243 if (state != INSIDE_PARENS)
245 *tp++ = *hp++; /* take the escape */
246 *tp++ = *hp; /* take following char */
251 case START_HDR: /* before header colon */
256 case SKIP_JUNK: /* looking for address start */
257 if (*hp == '"') /* quoted string */
259 oldstate = SKIP_JUNK;
260 state = INSIDE_DQUOTE;
263 else if (*hp == '(') /* address comment -- ignore */
266 oldstate = SKIP_JUNK;
267 state = INSIDE_PARENS;
269 else if (*hp == '<') /* begin <address> */
271 state = INSIDE_BRACKETS;
274 else if (*hp != ',' && !isspace(*hp))
277 state = BARE_ADDRESS;
281 case BARE_ADDRESS: /* collecting address without delimiters */
282 if (*hp == ',') /* end of address */
288 return(tp = address);
291 else if (*hp == '(') /* beginning of comment */
294 oldstate = BARE_ADDRESS;
295 state = INSIDE_PARENS;
297 else if (*hp == '<') /* beginning of real address */
299 state = INSIDE_BRACKETS;
302 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
306 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
316 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
325 case INSIDE_BRACKETS: /* possible <>-enclosed address */
326 if (*hp == '>') /* end of address */
331 return(tp = address);
333 else if (*hp == '<') /* nested <> */
335 else if (*hp == '"') /* quoted address */
338 oldstate = INSIDE_BRACKETS;
339 state = INSIDE_DQUOTE;
341 else /* just copy address */
351 static void parsebuf(char *longbuf, int reply)
357 reply_hack(longbuf, "HOSTNAME.NET");
358 printf("Rewritten buffer: %s", longbuf);
361 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
363 printf("\t-> \"%s\"\n", cp);
365 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
370 main(int argc, char *argv[])
372 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
375 verbose = reply = FALSE;
376 while ((ch = getopt(argc, argv, "rv")) != EOF)
388 while (fgets(buf, sizeof(buf)-1, stdin))
390 if (buf[0] == ' ' || buf[0] == '\t')
391 strcat(longbuf, buf);
392 else if (!strncasecmp("From: ", buf, 6)
393 || !strncasecmp("To: ", buf, 4)
394 || !strncasecmp("Reply-", buf, 6)
395 || !strncasecmp("Cc: ", buf, 4)
396 || !strncasecmp("Bcc: ", buf, 5))
397 strcpy(longbuf, buf);
401 fputs(longbuf, stdout);
402 parsebuf(longbuf, reply);
409 fputs(longbuf, stdout);
410 parsebuf(longbuf, reply);
413 #endif /* TESTMAIN */