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.
13 #include "fetchmail.h"
21 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
25 char *program_name = "rfc822";
28 unsigned char *reply_hack(buf, host)
29 /* hack message headers so replies will work properly */
30 unsigned char *buf; /* header to be hacked */
31 const unsigned char *host; /* server hostname */
33 unsigned char *from, *cp, last_nws = '\0', *parens_from = NULL;
34 int parendepth, state, has_bare_name_part, has_host_part;
39 if (strncasecmp("From:", buf, 5)
40 && strncasecmp("To:", buf, 3)
41 && strncasecmp("Reply-To:", buf, 9)
42 && strncasecmp("Return-Path:", buf, 12)
43 && strncasecmp("Cc:", buf, 3)
44 && strncasecmp("Bcc:", buf, 4)
45 && strncasecmp("Resent-From:", buf, 12)
46 && strncasecmp("Resent-To:", buf, 10)
47 && strncasecmp("Resent-Cc:", buf, 10)
48 && strncasecmp("Resent-Bcc:", buf, 11)
49 && strncasecmp("Apparently-From:", buf, 16)
50 && strncasecmp("Apparently-To:", buf, 14)
51 && strncasecmp("Sender:", buf, 7)
52 && strncasecmp("Resent-Sender:", buf, 14)
58 if (outlevel >= O_DEBUG)
59 report_build(stdout, GT_("About to rewrite %s"), buf);
61 /* make room to hack the address; buf must be malloced */
62 for (cp = buf; *cp; cp++)
63 if (*cp == ',' || isspace(*cp))
65 buf = (unsigned char *)xrealloc(buf, strlen(buf) + addresscount * strlen(host) + 1);
69 * This is going to foo up on some ill-formed addresses.
70 * Note that we don't rewrite the fake address <> in order to
71 * avoid screwing up bounce suppression with a null Return-Path.
74 parendepth = state = 0;
75 has_host_part = has_bare_name_part = FALSE;
76 for (from = buf; *from; from++)
81 printf("state %d: %s", state, buf);
82 printf("%*s^\n", from - buf + 10, " ");
89 else if (*from == ')')
93 if (!parendepth && !has_host_part)
96 case 0: /* before header colon */
101 case 1: /* we've seen the colon, we're looking for addresses */
106 else if (*from == '@' || *from == '!')
107 has_host_part = TRUE;
108 else if (*from == '"')
111 * Not expanding on last non-WS == ';' deals with groupnames,
112 * an obscure misfeature described in sections
113 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
115 else if ((*from == ',' || HEADER_END(from))
116 && has_bare_name_part
126 while (isspace(*from) || (*from == ','))
129 hostlen = strlen(host);
130 for (cp = from + strlen(from); cp >= from; --cp)
133 memcpy(from, host, hostlen);
134 from = p + hostlen + 1;
135 has_host_part = TRUE;
137 else if (from[1] == '('
138 && has_bare_name_part
140 && last_nws != ';' && last_nws != ')')
144 else if (!isspace(*from))
145 has_bare_name_part = TRUE;
148 case 2: /* we're in a string */
155 for (bp = from - 1; *bp == '\\'; bp--)
162 case 3: /* we're in a <>-enclosed address */
163 if (*from == '@' || *from == '!')
164 has_host_part = TRUE;
165 else if (*from == '>' && from[-1] != '<')
172 hostlen = strlen(host);
173 for (cp = from + strlen(from); cp >= from; --cp)
176 memcpy(from, host, hostlen);
178 has_host_part = TRUE;
185 * If we passed a comma, reset everything.
187 if (from[-1] == ',' && !parendepth) {
188 has_host_part = has_bare_name_part = FALSE;
194 if (outlevel >= O_DEBUG)
195 report_complete(stdout, GT_("Rewritten version is %s\n"), buf);
200 unsigned char *nxtaddr(hdr)
201 /* parse addresses in succession out of a specified RFC822 header */
202 const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr */
204 static unsigned char address[POPBUFSIZE+1];
206 static const unsigned char *hp;
207 static int state, oldstate;
209 static const unsigned char *orighdr;
213 #define START_HDR 0 /* before header colon */
214 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
215 #define BARE_ADDRESS 2 /* collecting address without delimiters */
216 #define INSIDE_DQUOTE 3 /* inside double quotes */
217 #define INSIDE_PARENS 4 /* inside parentheses */
218 #define INSIDE_BRACKETS 5 /* inside bracketed address */
219 #define ENDIT_ALL 6 /* after last address */
221 #define NEXTTP() ((tp < sizeof(address)-1) ? tp++ : tp)
238 printf("state %d: %s", state, orighdr);
239 printf("%*s^\n", hp - orighdr + 10, " ");
243 if (state == ENDIT_ALL) /* after last address */
245 else if (HEADER_END(hp))
250 while (isspace(address[--tp]))
252 address[++tp] = '\0';
256 return((unsigned char *)NULL);
258 else if (*hp == '\\') /* handle RFC822 escaping */
260 if (state != INSIDE_PARENS)
262 address[NEXTTP()] = *hp++; /* take the escape */
263 address[NEXTTP()] = *hp; /* take following unsigned char */
268 case START_HDR: /* before header colon */
273 case SKIP_JUNK: /* looking for address start */
274 if (*hp == '"') /* quoted string */
276 oldstate = SKIP_JUNK;
277 state = INSIDE_DQUOTE;
278 address[NEXTTP()] = *hp;
280 else if (*hp == '(') /* address comment -- ignore */
283 oldstate = SKIP_JUNK;
284 state = INSIDE_PARENS;
286 else if (*hp == '<') /* begin <address> */
288 state = INSIDE_BRACKETS;
291 else if (*hp != ',' && !isspace(*hp))
294 state = BARE_ADDRESS;
298 case BARE_ADDRESS: /* collecting address without delimiters */
299 if (*hp == ',') /* end of address */
303 address[NEXTTP()] = '\0';
309 else if (*hp == '(') /* beginning of comment */
312 oldstate = BARE_ADDRESS;
313 state = INSIDE_PARENS;
315 else if (*hp == '<') /* beginning of real address */
317 state = INSIDE_BRACKETS;
320 else if (*hp == '"') /* quoted word, copy verbatim */
323 state = INSIDE_DQUOTE;
324 address[NEXTTP()] = *hp;
326 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
327 address[NEXTTP()] = *hp;
330 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
332 address[NEXTTP()] = *hp;
335 address[NEXTTP()] = *hp;
340 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
349 case INSIDE_BRACKETS: /* possible <>-enclosed address */
350 if (*hp == '>') /* end of address */
352 address[NEXTTP()] = '\0';
358 else if (*hp == '<') /* nested <> */
360 else if (*hp == '"') /* quoted address */
362 address[NEXTTP()] = *hp;
363 oldstate = INSIDE_BRACKETS;
364 state = INSIDE_DQUOTE;
366 else /* just copy address */
367 address[NEXTTP()] = *hp;
376 static void parsebuf(unsigned char *longbuf, int reply)
382 reply_hack(longbuf, "HOSTNAME.NET");
383 printf("Rewritten buffer: %s", longbuf);
386 if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL)
388 printf("\t-> \"%s\"\n", cp);
390 ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL);
395 main(int argc, char *argv[])
397 unsigned char buf[MSGBUFSIZE], longbuf[BUFSIZ];
400 verbose = reply = FALSE;
401 while ((ch = getopt(argc, argv, "rv")) != EOF)
413 while (fgets(buf, sizeof(buf)-1, stdin))
415 if (buf[0] == ' ' || buf[0] == '\t')
416 strcat(longbuf, buf);
417 else if (!strncasecmp("From: ", buf, 6)
418 || !strncasecmp("To: ", buf, 4)
419 || !strncasecmp("Reply-", buf, 6)
420 || !strncasecmp("Cc: ", buf, 4)
421 || !strncasecmp("Bcc: ", buf, 5))
422 strcpy(longbuf, buf);
426 fputs(longbuf, stdout);
427 parsebuf(longbuf, reply);
434 fputs(longbuf, stdout);
435 parsebuf(longbuf, reply);