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, GT_("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 == '@' || *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 */
153 for (bp = from - 1; *bp == '\\'; bp--)
160 case 3: /* we're in a <>-enclosed address */
161 if (*from == '@' || *from == '!')
162 has_host_part = TRUE;
163 else if (*from == '>' && from[-1] != '<')
170 hostlen = strlen(host);
171 for (cp = from + strlen(from); cp >= from; --cp)
174 memcpy(from, host, hostlen);
176 has_host_part = TRUE;
183 * If we passed a comma, reset everything.
185 if (from[-1] == ',' && !parendepth) {
186 has_host_part = has_bare_name_part = FALSE;
192 if (outlevel >= O_DEBUG)
193 report_complete(stdout, GT_("Rewritten version is %s\n"), buf);
194 #endif /* TESTMAIN */
198 unsigned char *nxtaddr(hdr)
199 /* parse addresses in succession out of a specified RFC822 header */
200 const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr */
202 static unsigned char address[POPBUFSIZE+1];
204 static const unsigned char *hp;
205 static int state, oldstate;
207 static const unsigned char *orighdr;
208 #endif /* TESTMAIN */
211 #define START_HDR 0 /* before header colon */
212 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
213 #define BARE_ADDRESS 2 /* collecting address without delimiters */
214 #define INSIDE_DQUOTE 3 /* inside double quotes */
215 #define INSIDE_PARENS 4 /* inside parentheses */
216 #define INSIDE_BRACKETS 5 /* inside bracketed address */
217 #define ENDIT_ALL 6 /* after last address */
219 #define NEXTTP() ((tp < sizeof(address)-1) ? tp++ : tp)
227 #endif /* TESTMAIN */
236 printf("state %d: %s", state, orighdr);
237 printf("%*s^\n", hp - orighdr + 10, " ");
239 #endif /* TESTMAIN */
241 if (state == ENDIT_ALL) /* after last address */
243 else if (HEADER_END(hp))
248 while (isspace(address[--tp]))
250 address[++tp] = '\0';
254 return((unsigned char *)NULL);
256 else if (*hp == '\\') /* handle RFC822 escaping */
258 if (state != INSIDE_PARENS)
260 address[NEXTTP()] = *hp++; /* take the escape */
261 address[NEXTTP()] = *hp; /* take following unsigned char */
266 case START_HDR: /* before header colon */
271 case SKIP_JUNK: /* looking for address start */
272 if (*hp == '"') /* quoted string */
274 oldstate = SKIP_JUNK;
275 state = INSIDE_DQUOTE;
276 address[NEXTTP()] = *hp;
278 else if (*hp == '(') /* address comment -- ignore */
281 oldstate = SKIP_JUNK;
282 state = INSIDE_PARENS;
284 else if (*hp == '<') /* begin <address> */
286 state = INSIDE_BRACKETS;
289 else if (*hp != ',' && !isspace(*hp))
292 state = BARE_ADDRESS;
296 case BARE_ADDRESS: /* collecting address without delimiters */
297 if (*hp == ',') /* end of address */
301 address[NEXTTP()] = '\0';
307 else if (*hp == '(') /* beginning of comment */
310 oldstate = BARE_ADDRESS;
311 state = INSIDE_PARENS;
313 else if (*hp == '<') /* beginning of real address */
315 state = INSIDE_BRACKETS;
318 else if (*hp == '"') /* quoted word, copy verbatim */
321 state = INSIDE_DQUOTE;
322 address[NEXTTP()] = *hp;
324 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
325 address[NEXTTP()] = *hp;
328 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
330 address[NEXTTP()] = *hp;
333 address[NEXTTP()] = *hp;
338 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
347 case INSIDE_BRACKETS: /* possible <>-enclosed address */
348 if (*hp == '>') /* end of address */
350 address[NEXTTP()] = '\0';
356 else if (*hp == '<') /* nested <> */
358 else if (*hp == '"') /* quoted address */
360 address[NEXTTP()] = *hp;
361 oldstate = INSIDE_BRACKETS;
362 state = INSIDE_DQUOTE;
364 else /* just copy address */
365 address[NEXTTP()] = *hp;
374 static void parsebuf(unsigned char *longbuf, int reply)
380 reply_hack(longbuf, "HOSTNAME.NET");
381 printf("Rewritten buffer: %s", longbuf);
384 if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL)
386 printf("\t-> \"%s\"\n", cp);
388 ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL);
393 main(int argc, char *argv[])
395 unsigned char buf[MSGBUFSIZE], longbuf[BUFSIZ];
398 verbose = reply = FALSE;
399 while ((ch = getopt(argc, argv, "rv")) != EOF)
411 while (fgets(buf, sizeof(buf)-1, stdin))
413 if (buf[0] == ' ' || buf[0] == '\t')
414 strcat(longbuf, buf);
415 else if (!strncasecmp("From: ", buf, 6)
416 || !strncasecmp("To: ", buf, 4)
417 || !strncasecmp("Reply-", buf, 6)
418 || !strncasecmp("Cc: ", buf, 4)
419 || !strncasecmp("Bcc: ", buf, 5))
420 strcpy(longbuf, buf);
424 fputs(longbuf, stdout);
425 parsebuf(longbuf, reply);
432 fputs(longbuf, stdout);
433 parsebuf(longbuf, reply);
436 #endif /* TESTMAIN */