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'))
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 *tp, address[POPBUFSIZE+1];
194 static const unsigned char *hp;
195 static int state, oldstate;
197 static const unsigned char *orighdr;
198 #endif /* TESTMAIN */
201 #define START_HDR 0 /* before header colon */
202 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
203 #define BARE_ADDRESS 2 /* collecting address without delimiters */
204 #define INSIDE_DQUOTE 3 /* inside double quotes */
205 #define INSIDE_PARENS 4 /* inside parentheses */
206 #define INSIDE_BRACKETS 5 /* inside bracketed address */
207 #define ENDIT_ALL 6 /* after last address */
215 #endif /* TESTMAIN */
224 printf("state %d: %s", state, orighdr);
225 printf("%*s^\n", hp - orighdr + 10, " ");
227 #endif /* TESTMAIN */
229 if (state == ENDIT_ALL) /* after last address */
231 else if (HEADER_END(hp))
236 while (isspace(*--tp))
240 return(tp > address ? (tp = address) : (unsigned char *)NULL);
242 else if (*hp == '\\') /* handle RFC822 escaping */
244 if (state != INSIDE_PARENS)
246 *tp++ = *hp++; /* take the escape */
247 *tp++ = *hp; /* take following unsigned char */
252 case START_HDR: /* before header colon */
257 case SKIP_JUNK: /* looking for address start */
258 if (*hp == '"') /* quoted string */
260 oldstate = SKIP_JUNK;
261 state = INSIDE_DQUOTE;
264 else if (*hp == '(') /* address comment -- ignore */
267 oldstate = SKIP_JUNK;
268 state = INSIDE_PARENS;
270 else if (*hp == '<') /* begin <address> */
272 state = INSIDE_BRACKETS;
275 else if (*hp != ',' && !isspace(*hp))
278 state = BARE_ADDRESS;
282 case BARE_ADDRESS: /* collecting address without delimiters */
283 if (*hp == ',') /* end of address */
289 return(tp = address);
292 else if (*hp == '(') /* beginning of comment */
295 oldstate = BARE_ADDRESS;
296 state = INSIDE_PARENS;
298 else if (*hp == '<') /* beginning of real address */
300 state = INSIDE_BRACKETS;
303 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
307 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
317 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
326 case INSIDE_BRACKETS: /* possible <>-enclosed address */
327 if (*hp == '>') /* end of address */
332 return(tp = address);
334 else if (*hp == '<') /* nested <> */
336 else if (*hp == '"') /* quoted address */
339 oldstate = INSIDE_BRACKETS;
340 state = INSIDE_DQUOTE;
342 else /* just copy address */
352 static void parsebuf(unsigned char *longbuf, int reply)
358 reply_hack(longbuf, "HOSTNAME.NET");
359 printf("Rewritten buffer: %s", longbuf);
362 if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL)
364 printf("\t-> \"%s\"\n", cp);
366 ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL);
371 main(int argc, char *argv[])
373 unsigned char buf[MSGBUFSIZE], longbuf[BUFSIZ];
376 verbose = reply = FALSE;
377 while ((ch = getopt(argc, argv, "rv")) != EOF)
389 while (fgets(buf, sizeof(buf)-1, stdin))
391 if (buf[0] == ' ' || buf[0] == '\t')
392 strcat(longbuf, buf);
393 else if (!strncasecmp("From: ", buf, 6)
394 || !strncasecmp("To: ", buf, 4)
395 || !strncasecmp("Reply-", buf, 6)
396 || !strncasecmp("Cc: ", buf, 4)
397 || !strncasecmp("Bcc: ", buf, 5))
398 strcpy(longbuf, buf);
402 fputs(longbuf, stdout);
403 parsebuf(longbuf, reply);
410 fputs(longbuf, stdout);
411 parsebuf(longbuf, reply);
414 #endif /* TESTMAIN */