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 */
28 char *from, *cp, last_nws = '\0';
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 */
79 else if (*from == '@')
81 else if (*from == '"')
84 * Not expanding on last non-WS == ';' deals with groupnames,
85 * an obscure misfeature described in sections
86 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
88 else if ((*from == ',' || HEADER_END(from) || from[1] == '(')
95 while (isspace(*from) || (*from == ','))
98 hostlen = strlen(host);
99 for (cp = from + strlen(from); cp >= from; --cp)
102 memcpy(from, host, hostlen);
104 has_host_part = TRUE;
106 else if (!isspace(*from))
107 has_bare_name_part = TRUE;
110 case 2: /* we're in a string */
115 case 3: /* we're in a <>-enclosed address */
117 has_host_part = TRUE;
118 else if (*from == '>')
125 hostlen = strlen(host);
126 for (cp = from + strlen(from); cp >= from; --cp)
129 memcpy(from, host, hostlen);
131 has_host_part = TRUE;
138 * If we passed a comma, reset everything.
140 if (from[-1] == ',' && !parendepth) {
141 has_host_part = has_bare_name_part = FALSE;
149 /* parse addresses in succession out of a specified RFC822 header */
150 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
152 static char *tp, address[POPBUFSIZE+1];
153 static const char *hp;
154 static int state, oldstate;
156 static const char *orighdr;
157 #endif /* TESTMAIN */
160 #define START_HDR 0 /* before header colon */
161 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
162 #define BARE_ADDRESS 2 /* collecting address without delimiters */
163 #define INSIDE_DQUOTE 3 /* inside double quotes */
164 #define INSIDE_PARENS 4 /* inside parentheses */
165 #define INSIDE_BRACKETS 5 /* inside bracketed address */
166 #define ENDIT_ALL 6 /* after last address */
174 #endif /* TESTMAIN */
183 printf("state %d: %s", state, orighdr);
184 printf("%*s^\n", hp - orighdr + 10, " ");
186 #endif /* TESTMAIN */
188 if (state == ENDIT_ALL) /* after last address */
190 else if (HEADER_END(hp))
195 while (isspace(*--tp))
199 return(tp > address ? (tp = address) : (char *)NULL);
201 else if (*hp == '\\') /* handle RFC822 escaping */
203 if (state != INSIDE_PARENS)
205 *tp++ = *hp++; /* take the escape */
206 *tp++ = *hp; /* take following char */
211 case START_HDR: /* before header colon */
216 case SKIP_JUNK: /* looking for address start */
217 if (*hp == '"') /* quoted string */
219 oldstate = SKIP_JUNK;
220 state = INSIDE_DQUOTE;
223 else if (*hp == '(') /* address comment -- ignore */
226 oldstate = SKIP_JUNK;
227 state = INSIDE_PARENS;
229 else if (*hp == '<') /* begin <address> */
231 state = INSIDE_BRACKETS;
234 else if (*hp != ',' && !isspace(*hp))
237 state = BARE_ADDRESS;
241 case BARE_ADDRESS: /* collecting address without delimiters */
242 if (*hp == ',') /* end of address */
248 return(tp = address);
251 else if (*hp == '(') /* beginning of comment */
254 oldstate = BARE_ADDRESS;
255 state = INSIDE_PARENS;
257 else if (*hp == '<') /* beginning of real address */
259 state = INSIDE_BRACKETS;
262 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
266 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
276 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
285 case INSIDE_BRACKETS: /* possible <>-enclosed address */
286 if (*hp == '>') /* end of address */
291 return(tp = address);
293 else if (*hp == '<') /* nested <> */
295 else if (*hp == '"') /* quoted address */
298 oldstate = INSIDE_BRACKETS;
299 state = INSIDE_DQUOTE;
301 else /* just copy address */
311 static void parsebuf(char *longbuf, int reply)
317 reply_hack(longbuf, "HOSTNAME.NET");
318 printf("Rewritten buffer: %s", longbuf);
321 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
323 printf("\t-> \"%s\"\n", cp);
325 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
330 main(int argc, char *argv[])
332 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
335 verbose = reply = FALSE;
336 while ((ch = getopt(argc, argv, "rv")) != EOF)
348 while (fgets(buf, sizeof(buf)-1, stdin))
350 if (buf[0] == ' ' || buf[0] == '\t')
351 strcat(longbuf, buf);
352 else if (!strncasecmp("From: ", buf, 6)
353 || !strncasecmp("To: ", buf, 4)
354 || !strncasecmp("Reply-", buf, 6)
355 || !strncasecmp("Cc: ", buf, 4)
356 || !strncasecmp("Bcc: ", buf, 5))
357 strcpy(longbuf, buf);
361 fputs(longbuf, stdout);
362 parsebuf(longbuf, reply);
369 fputs(longbuf, stdout);
370 parsebuf(longbuf, reply);
373 #endif /* TESTMAIN */