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)
15 #include "fetchmail.h"
17 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
23 char *reply_hack(buf, host)
24 /* hack message headers so replies will work properly */
25 char *buf; /* header to be hacked */
26 const char *host; /* server hostname */
29 int parendepth, state, has_bare_name_part, has_host_part;
32 if (strncasecmp("From: ", buf, 6)
33 && strncasecmp("To: ", buf, 4)
34 && strncasecmp("Reply-To: ", buf, 10)
35 && strncasecmp("Return-Path: ", buf, 13)
36 && strncasecmp("Cc: ", buf, 4)
37 && strncasecmp("Bcc: ", buf, 5)) {
42 /* make room to hack the address; buf must be malloced */
43 for (cp = buf; *cp; cp++)
44 if (*cp == ',' || isspace(*cp))
46 buf = (char *)realloc(buf, strlen(buf) + addresscount * strlen(host) + 1);
49 parendepth = state = 0;
50 has_host_part = has_bare_name_part = FALSE;
51 for (from = buf; *from; from++)
56 printf("state %d: %s", state, buf);
57 printf("%*s^\n", from - buf + 10, " ");
63 else if (*from == ')')
66 if (!parendepth && !has_host_part)
69 case 0: /* before header colon */
74 case 1: /* we've seen the colon, we're looking for addresses */
77 else if (*from == '@')
79 else if (*from == '"')
82 * Not expanding on from[-1] == ';' deals with groupnames,
83 * an obscure misfeature described in sections
84 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
86 else if ((*from == ',' || HEADER_END(from) || from[1] == '(')
93 while (isspace(*from) || (*from == ','))
96 hostlen = strlen(host);
97 for (cp = from + strlen(from); cp >= from; --cp)
100 memcpy(from, host, hostlen);
102 has_host_part = TRUE;
104 else if (!isspace(*from))
105 has_bare_name_part = TRUE;
108 case 2: /* we're in a string */
113 case 3: /* we're in a <>-enclosed address */
115 has_host_part = TRUE;
116 else if (*from == '>')
123 hostlen = strlen(host);
124 for (cp = from + strlen(from); cp >= from; --cp)
127 memcpy(from, host, hostlen);
129 has_host_part = TRUE;
136 * If we passed a comma, reset everything.
138 if (from[-1] == ',' && !parendepth) {
139 has_host_part = has_bare_name_part = FALSE;
147 /* parse addresses in succession out of a specified RFC822 header */
148 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
150 static char *tp, address[POPBUFSIZE+1];
151 static const char *hp;
152 static int state, oldstate;
154 static const char *orighdr;
155 #endif /* TESTMAIN */
158 #define START_HDR 0 /* before header colon */
159 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
160 #define BARE_ADDRESS 2 /* collecting address without delimiters */
161 #define INSIDE_DQUOTE 3 /* inside double quotes */
162 #define INSIDE_PARENS 4 /* inside parentheses */
163 #define INSIDE_BRACKETS 5 /* inside bracketed address */
164 #define ENDIT_ALL 6 /* after last address */
172 #endif /* TESTMAIN */
181 printf("state %d: %s", state, orighdr);
182 printf("%*s^\n", hp - orighdr + 10, " ");
184 #endif /* TESTMAIN */
186 if (state == ENDIT_ALL) /* after last address */
188 else if (HEADER_END(hp))
193 while (isspace(*--tp))
197 return(tp > address ? (tp = address) : (char *)NULL);
199 else if (*hp == '\\') /* handle RFC822 escaping */
201 if (state != INSIDE_PARENS)
203 *tp++ = *hp++; /* take the escape */
204 *tp++ = *hp; /* take following char */
209 case START_HDR: /* before header colon */
214 case SKIP_JUNK: /* looking for address start */
215 if (*hp == '"') /* quoted string */
217 oldstate = SKIP_JUNK;
218 state = INSIDE_DQUOTE;
221 else if (*hp == '(') /* address comment -- ignore */
224 oldstate = SKIP_JUNK;
225 state = INSIDE_PARENS;
227 else if (*hp == '<') /* begin <address> */
229 state = INSIDE_BRACKETS;
232 else if (*hp != ',' && !isspace(*hp))
235 state = BARE_ADDRESS;
239 case BARE_ADDRESS: /* collecting address without delimiters */
240 if (*hp == ',') /* end of address */
246 return(tp = address);
249 else if (*hp == '(') /* beginning of comment */
252 oldstate = BARE_ADDRESS;
253 state = INSIDE_PARENS;
255 else if (*hp == '<') /* beginning of real address */
257 state = INSIDE_BRACKETS;
260 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
264 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
274 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
283 case INSIDE_BRACKETS: /* possible <>-enclosed address */
284 if (*hp == '>') /* end of address */
289 return(tp = address);
291 else if (*hp == '<') /* nested <> */
293 else if (*hp == '"') /* quoted address */
296 oldstate = INSIDE_BRACKETS;
297 state = INSIDE_DQUOTE;
299 else /* just copy address */
309 static void parsebuf(char *longbuf, int reply)
315 reply_hack(longbuf, "HOSTNAME.NET");
316 printf("Rewritten buffer: %s", longbuf);
319 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
321 printf("\t-> \"%s\"\n", cp);
323 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
328 main(int argc, char *argv[])
330 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
333 verbose = reply = FALSE;
334 while ((ch = getopt(argc, argv, "rv")) != EOF)
346 while (fgets(buf, sizeof(buf)-1, stdin))
348 if (buf[0] == ' ' || buf[0] == '\t')
349 strcat(longbuf, buf);
350 else if (!strncasecmp("From: ", buf, 6)
351 || !strncasecmp("To: ", buf, 4)
352 || !strncasecmp("Reply-", buf, 6)
353 || !strncasecmp("Cc: ", buf, 4)
354 || !strncasecmp("Bcc: ", buf, 5))
355 strcpy(longbuf, buf);
359 fputs(longbuf, stdout);
360 parsebuf(longbuf, reply);
367 fputs(longbuf, stdout);
368 parsebuf(longbuf, reply);
371 #endif /* TESTMAIN */