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"
19 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t' && (p)[1] != '\0'))
23 char *program_name = "rfc822";
26 unsigned char *reply_hack(buf, host)
27 /* hack message headers so replies will work properly */
28 unsigned char *buf; /* header to be hacked */
29 const unsigned char *host; /* server hostname */
31 unsigned char *from, *cp, last_nws = '\0', *parens_from = NULL;
32 int parendepth, state, has_bare_name_part, has_host_part;
37 if (strncasecmp("From:", buf, 5)
38 && strncasecmp("To:", buf, 3)
39 && strncasecmp("Reply-To:", buf, 9)
40 && strncasecmp("Return-Path:", buf, 12)
41 && strncasecmp("Cc:", buf, 3)
42 && strncasecmp("Bcc:", buf, 4)
43 && strncasecmp("Resent-From:", buf, 12)
44 && strncasecmp("Resent-To:", buf, 10)
45 && strncasecmp("Resent-Cc:", buf, 10)
46 && strncasecmp("Resent-Bcc:", buf, 11)
47 && strncasecmp("Apparently-From:", buf, 16)
48 && strncasecmp("Apparently-To:", buf, 14)
49 && strncasecmp("Sender:", buf, 7)
50 && strncasecmp("Resent-Sender:", buf, 14)
56 if (outlevel >= O_DEBUG)
57 report_build(stdout, _("About to rewrite %s"), buf);
59 /* make room to hack the address; buf must be malloced */
60 for (cp = buf; *cp; cp++)
61 if (*cp == ',' || isspace(*cp))
63 buf = (unsigned char *)xrealloc(buf, strlen(buf) + addresscount * strlen(host) + 1);
67 * This is going to foo up on some ill-formed addresses.
68 * Note that we don't rewrite the fake address <> in order to
69 * avoid screwing up bounce suppression with a null Return-Path.
72 parendepth = state = 0;
73 has_host_part = has_bare_name_part = FALSE;
74 for (from = buf; *from; from++)
79 printf("state %d: %s", state, buf);
80 printf("%*s^\n", from - buf + 10, " ");
87 else if (*from == ')')
91 if (!parendepth && !has_host_part)
94 case 0: /* before header colon */
99 case 1: /* we've seen the colon, we're looking for addresses */
104 else if (*from == '@')
105 has_host_part = TRUE;
106 else if (*from == '"')
109 * Not expanding on last non-WS == ';' deals with groupnames,
110 * an obscure misfeature described in sections
111 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
113 else if ((*from == ',' || HEADER_END(from))
114 && has_bare_name_part
124 while (isspace(*from) || (*from == ','))
127 hostlen = strlen(host);
128 for (cp = from + strlen(from); cp >= from; --cp)
131 memcpy(from, host, hostlen);
132 from = p + hostlen + 1;
133 has_host_part = TRUE;
135 else if (from[1] == '('
136 && has_bare_name_part
138 && last_nws != ';' && last_nws != ')')
142 else if (!isspace(*from))
143 has_bare_name_part = TRUE;
146 case 2: /* we're in a string */
151 case 3: /* we're in a <>-enclosed address */
153 has_host_part = TRUE;
154 else if (*from == '>' && from[-1] != '<')
161 hostlen = strlen(host);
162 for (cp = from + strlen(from); cp >= from; --cp)
165 memcpy(from, host, hostlen);
167 has_host_part = TRUE;
174 * If we passed a comma, reset everything.
176 if (from[-1] == ',' && !parendepth) {
177 has_host_part = has_bare_name_part = FALSE;
183 if (outlevel >= O_DEBUG)
184 report_complete(stdout, _("Rewritten version is %s\n"), buf);
185 #endif /* TESTMAIN */
189 unsigned char *nxtaddr(hdr)
190 /* parse addresses in succession out of a specified RFC822 header */
191 const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr */
193 static unsigned char address[POPBUFSIZE+1];
195 static const unsigned char *hp;
196 static int state, oldstate;
198 static const unsigned char *orighdr;
199 #endif /* TESTMAIN */
202 #define START_HDR 0 /* before header colon */
203 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
204 #define BARE_ADDRESS 2 /* collecting address without delimiters */
205 #define INSIDE_DQUOTE 3 /* inside double quotes */
206 #define INSIDE_PARENS 4 /* inside parentheses */
207 #define INSIDE_BRACKETS 5 /* inside bracketed address */
208 #define ENDIT_ALL 6 /* after last address */
210 #define NEXTTP() ((tp < sizeof(address)-1) ? tp++ : tp)
218 #endif /* TESTMAIN */
227 printf("state %d: %s", state, orighdr);
228 printf("%*s^\n", hp - orighdr + 10, " ");
230 #endif /* TESTMAIN */
232 if (state == ENDIT_ALL) /* after last address */
234 else if (HEADER_END(hp))
239 while (isspace(address[--tp]))
241 address[++tp] = '\0';
245 return((unsigned char *)NULL);
247 else if (*hp == '\\') /* handle RFC822 escaping */
249 if (state != INSIDE_PARENS)
251 address[NEXTTP()] = *hp++; /* take the escape */
252 address[NEXTTP()] = *hp; /* take following unsigned char */
257 case START_HDR: /* before header colon */
262 case SKIP_JUNK: /* looking for address start */
263 if (*hp == '"') /* quoted string */
265 oldstate = SKIP_JUNK;
266 state = INSIDE_DQUOTE;
267 address[NEXTTP()] = *hp;
269 else if (*hp == '(') /* address comment -- ignore */
272 oldstate = SKIP_JUNK;
273 state = INSIDE_PARENS;
275 else if (*hp == '<') /* begin <address> */
277 state = INSIDE_BRACKETS;
280 else if (*hp != ',' && !isspace(*hp))
283 state = BARE_ADDRESS;
287 case BARE_ADDRESS: /* collecting address without delimiters */
288 if (*hp == ',') /* end of address */
292 address[NEXTTP()] = '\0';
298 else if (*hp == '(') /* beginning of comment */
301 oldstate = BARE_ADDRESS;
302 state = INSIDE_PARENS;
304 else if (*hp == '<') /* beginning of real address */
306 state = INSIDE_BRACKETS;
309 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
310 address[NEXTTP()] = *hp;
313 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
315 address[NEXTTP()] = *hp;
318 address[NEXTTP()] = *hp;
323 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
332 case INSIDE_BRACKETS: /* possible <>-enclosed address */
333 if (*hp == '>') /* end of address */
335 address[NEXTTP()] = '\0';
341 else if (*hp == '<') /* nested <> */
343 else if (*hp == '"') /* quoted address */
345 address[NEXTTP()] = *hp;
346 oldstate = INSIDE_BRACKETS;
347 state = INSIDE_DQUOTE;
349 else /* just copy address */
350 address[NEXTTP()] = *hp;
359 static void parsebuf(unsigned char *longbuf, int reply)
365 reply_hack(longbuf, "HOSTNAME.NET");
366 printf("Rewritten buffer: %s", longbuf);
369 if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL)
371 printf("\t-> \"%s\"\n", cp);
373 ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL);
378 main(int argc, char *argv[])
380 unsigned char buf[MSGBUFSIZE], longbuf[BUFSIZ];
383 verbose = reply = FALSE;
384 while ((ch = getopt(argc, argv, "rv")) != EOF)
396 while (fgets(buf, sizeof(buf)-1, stdin))
398 if (buf[0] == ' ' || buf[0] == '\t')
399 strcat(longbuf, buf);
400 else if (!strncasecmp("From: ", buf, 6)
401 || !strncasecmp("To: ", buf, 4)
402 || !strncasecmp("Reply-", buf, 6)
403 || !strncasecmp("Cc: ", buf, 4)
404 || !strncasecmp("Bcc: ", buf, 5))
405 strcpy(longbuf, buf);
409 fputs(longbuf, stdout);
410 parsebuf(longbuf, reply);
417 fputs(longbuf, stdout);
418 parsebuf(longbuf, reply);
421 #endif /* TESTMAIN */