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'))
21 char *program_name = "rfc822";
24 char *reply_hack(buf, host)
25 /* hack message headers so replies will work properly */
26 char *buf; /* header to be hacked */
27 const char *host; /* server hostname */
29 char *from, *cp, last_nws = '\0', *parens_from = NULL;
30 int parendepth, state, has_bare_name_part, has_host_part;
33 if (strncasecmp("From: ", buf, 6)
34 && strncasecmp("To: ", buf, 4)
35 && strncasecmp("Reply-To: ", buf, 10)
36 && strncasecmp("Return-Path: ", buf, 13)
37 && strncasecmp("Cc: ", buf, 4)
38 && strncasecmp("Bcc: ", buf, 5)
39 && strncasecmp("Resent-From: ", buf, 13)
40 && strncasecmp("Resent-To: ", buf, 11)
41 && strncasecmp("Resent-Cc: ", buf, 11)
42 && strncasecmp("Resent-Bcc: ", buf, 12)
43 && strncasecmp("Apparently-From:", buf, 16)
44 && strncasecmp("Apparently-To:", buf, 14)
45 && strncasecmp("Sender:", buf, 7)
46 && strncasecmp("Resent-Sender:", buf, 14)
52 /* make room to hack the address; buf must be malloced */
53 for (cp = buf; *cp; cp++)
54 if (*cp == ',' || isspace(*cp))
56 buf = (char *)xrealloc(buf, strlen(buf) + addresscount * strlen(host) + 1);
60 * This is going to foo up on some ill-formed addresses.
61 * Note that we don't rewrite the fake address <> in order to
62 * avoid screwing up bounce suppression with a null Return-Path.
65 parendepth = state = 0;
66 has_host_part = has_bare_name_part = FALSE;
67 for (from = buf; *from; from++)
72 printf("state %d: %s", state, buf);
73 printf("%*s^\n", from - buf + 10, " ");
79 else if (*from == ')')
82 if (!parendepth && !has_host_part)
85 case 0: /* before header colon */
90 case 1: /* we've seen the colon, we're looking for addresses */
95 else if (*from == '@')
97 else if (*from == '"')
100 * Not expanding on last non-WS == ';' deals with groupnames,
101 * an obscure misfeature described in sections
102 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
104 else if ((*from == ',' || HEADER_END(from))
105 && has_bare_name_part
115 while (isspace(*from) || (*from == ','))
118 hostlen = strlen(host);
119 for (cp = from + strlen(from); cp >= from; --cp)
122 memcpy(from, host, hostlen);
123 from = p + hostlen + 1;
124 has_host_part = TRUE;
126 else if (from[1] == '('
127 && has_bare_name_part
129 && last_nws != ';' && last_nws != ')')
133 else if (!isspace(*from))
134 has_bare_name_part = TRUE;
137 case 2: /* we're in a string */
142 case 3: /* we're in a <>-enclosed address */
144 has_host_part = TRUE;
145 else if (*from == '>' && from[-1] != '<')
152 hostlen = strlen(host);
153 for (cp = from + strlen(from); cp >= from; --cp)
156 memcpy(from, host, hostlen);
158 has_host_part = TRUE;
165 * If we passed a comma, reset everything.
167 if (from[-1] == ',' && !parendepth) {
168 has_host_part = has_bare_name_part = FALSE;
177 /* parse addresses in succession out of a specified RFC822 header */
178 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
180 static char *tp, address[POPBUFSIZE+1];
181 static const char *hp;
182 static int state, oldstate;
184 static const char *orighdr;
185 #endif /* TESTMAIN */
188 #define START_HDR 0 /* before header colon */
189 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
190 #define BARE_ADDRESS 2 /* collecting address without delimiters */
191 #define INSIDE_DQUOTE 3 /* inside double quotes */
192 #define INSIDE_PARENS 4 /* inside parentheses */
193 #define INSIDE_BRACKETS 5 /* inside bracketed address */
194 #define ENDIT_ALL 6 /* after last address */
202 #endif /* TESTMAIN */
211 printf("state %d: %s", state, orighdr);
212 printf("%*s^\n", hp - orighdr + 10, " ");
214 #endif /* TESTMAIN */
216 if (state == ENDIT_ALL) /* after last address */
218 else if (HEADER_END(hp))
223 while (isspace(*--tp))
227 return(tp > address ? (tp = address) : (char *)NULL);
229 else if (*hp == '\\') /* handle RFC822 escaping */
231 if (state != INSIDE_PARENS)
233 *tp++ = *hp++; /* take the escape */
234 *tp++ = *hp; /* take following char */
239 case START_HDR: /* before header colon */
244 case SKIP_JUNK: /* looking for address start */
245 if (*hp == '"') /* quoted string */
247 oldstate = SKIP_JUNK;
248 state = INSIDE_DQUOTE;
251 else if (*hp == '(') /* address comment -- ignore */
254 oldstate = SKIP_JUNK;
255 state = INSIDE_PARENS;
257 else if (*hp == '<') /* begin <address> */
259 state = INSIDE_BRACKETS;
262 else if (*hp != ',' && !isspace(*hp))
265 state = BARE_ADDRESS;
269 case BARE_ADDRESS: /* collecting address without delimiters */
270 if (*hp == ',') /* end of address */
276 return(tp = address);
279 else if (*hp == '(') /* beginning of comment */
282 oldstate = BARE_ADDRESS;
283 state = INSIDE_PARENS;
285 else if (*hp == '<') /* beginning of real address */
287 state = INSIDE_BRACKETS;
290 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
294 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
304 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
313 case INSIDE_BRACKETS: /* possible <>-enclosed address */
314 if (*hp == '>') /* end of address */
319 return(tp = address);
321 else if (*hp == '<') /* nested <> */
323 else if (*hp == '"') /* quoted address */
326 oldstate = INSIDE_BRACKETS;
327 state = INSIDE_DQUOTE;
329 else /* just copy address */
339 static void parsebuf(char *longbuf, int reply)
345 reply_hack(longbuf, "HOSTNAME.NET");
346 printf("Rewritten buffer: %s", longbuf);
349 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
351 printf("\t-> \"%s\"\n", cp);
353 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
358 main(int argc, char *argv[])
360 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
363 verbose = reply = FALSE;
364 while ((ch = getopt(argc, argv, "rv")) != EOF)
376 while (fgets(buf, sizeof(buf)-1, stdin))
378 if (buf[0] == ' ' || buf[0] == '\t')
379 strcat(longbuf, buf);
380 else if (!strncasecmp("From: ", buf, 6)
381 || !strncasecmp("To: ", buf, 4)
382 || !strncasecmp("Reply-", buf, 6)
383 || !strncasecmp("Cc: ", buf, 4)
384 || !strncasecmp("Bcc: ", buf, 5))
385 strcpy(longbuf, buf);
389 fputs(longbuf, stdout);
390 parsebuf(longbuf, reply);
397 fputs(longbuf, stdout);
398 parsebuf(longbuf, reply);
401 #endif /* TESTMAIN */