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 if (outlevel >= O_DEBUG)
53 error_build(0, 0, "About to rewrite %s", buf);
55 /* make room to hack the address; buf must be malloced */
56 for (cp = buf; *cp; cp++)
57 if (*cp == ',' || isspace(*cp))
59 buf = (char *)xrealloc(buf, strlen(buf) + addresscount * strlen(host) + 1);
63 * This is going to foo up on some ill-formed addresses.
64 * Note that we don't rewrite the fake address <> in order to
65 * avoid screwing up bounce suppression with a null Return-Path.
68 parendepth = state = 0;
69 has_host_part = has_bare_name_part = FALSE;
70 for (from = buf; *from; from++)
75 printf("state %d: %s", state, buf);
76 printf("%*s^\n", from - buf + 10, " ");
82 else if (*from == ')')
85 if (!parendepth && !has_host_part)
88 case 0: /* before header colon */
93 case 1: /* we've seen the colon, we're looking for addresses */
98 else if (*from == '@')
100 else if (*from == '"')
103 * Not expanding on last non-WS == ';' deals with groupnames,
104 * an obscure misfeature described in sections
105 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
107 else if ((*from == ',' || HEADER_END(from))
108 && has_bare_name_part
118 while (isspace(*from) || (*from == ','))
121 hostlen = strlen(host);
122 for (cp = from + strlen(from); cp >= from; --cp)
125 memcpy(from, host, hostlen);
126 from = p + hostlen + 1;
127 has_host_part = TRUE;
129 else if (from[1] == '('
130 && has_bare_name_part
132 && last_nws != ';' && last_nws != ')')
136 else if (!isspace(*from))
137 has_bare_name_part = TRUE;
140 case 2: /* we're in a string */
145 case 3: /* we're in a <>-enclosed address */
147 has_host_part = TRUE;
148 else if (*from == '>' && from[-1] != '<')
155 hostlen = strlen(host);
156 for (cp = from + strlen(from); cp >= from; --cp)
159 memcpy(from, host, hostlen);
161 has_host_part = TRUE;
168 * If we passed a comma, reset everything.
170 if (from[-1] == ',' && !parendepth) {
171 has_host_part = has_bare_name_part = FALSE;
177 if (outlevel >= O_DEBUG)
178 error_complete(0, 0, "Rewritten version is %s", buf);
179 #endif /* TESTMAIN */
184 /* parse addresses in succession out of a specified RFC822 header */
185 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
187 static char *tp, address[POPBUFSIZE+1];
188 static const char *hp;
189 static int state, oldstate;
191 static const char *orighdr;
192 #endif /* TESTMAIN */
195 #define START_HDR 0 /* before header colon */
196 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
197 #define BARE_ADDRESS 2 /* collecting address without delimiters */
198 #define INSIDE_DQUOTE 3 /* inside double quotes */
199 #define INSIDE_PARENS 4 /* inside parentheses */
200 #define INSIDE_BRACKETS 5 /* inside bracketed address */
201 #define ENDIT_ALL 6 /* after last address */
209 #endif /* TESTMAIN */
218 printf("state %d: %s", state, orighdr);
219 printf("%*s^\n", hp - orighdr + 10, " ");
221 #endif /* TESTMAIN */
223 if (state == ENDIT_ALL) /* after last address */
225 else if (HEADER_END(hp))
230 while (isspace(*--tp))
234 return(tp > address ? (tp = address) : (char *)NULL);
236 else if (*hp == '\\') /* handle RFC822 escaping */
238 if (state != INSIDE_PARENS)
240 *tp++ = *hp++; /* take the escape */
241 *tp++ = *hp; /* take following char */
246 case START_HDR: /* before header colon */
251 case SKIP_JUNK: /* looking for address start */
252 if (*hp == '"') /* quoted string */
254 oldstate = SKIP_JUNK;
255 state = INSIDE_DQUOTE;
258 else if (*hp == '(') /* address comment -- ignore */
261 oldstate = SKIP_JUNK;
262 state = INSIDE_PARENS;
264 else if (*hp == '<') /* begin <address> */
266 state = INSIDE_BRACKETS;
269 else if (*hp != ',' && !isspace(*hp))
272 state = BARE_ADDRESS;
276 case BARE_ADDRESS: /* collecting address without delimiters */
277 if (*hp == ',') /* end of address */
283 return(tp = address);
286 else if (*hp == '(') /* beginning of comment */
289 oldstate = BARE_ADDRESS;
290 state = INSIDE_PARENS;
292 else if (*hp == '<') /* beginning of real address */
294 state = INSIDE_BRACKETS;
297 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
301 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
311 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
320 case INSIDE_BRACKETS: /* possible <>-enclosed address */
321 if (*hp == '>') /* end of address */
326 return(tp = address);
328 else if (*hp == '<') /* nested <> */
330 else if (*hp == '"') /* quoted address */
333 oldstate = INSIDE_BRACKETS;
334 state = INSIDE_DQUOTE;
336 else /* just copy address */
346 static void parsebuf(char *longbuf, int reply)
352 reply_hack(longbuf, "HOSTNAME.NET");
353 printf("Rewritten buffer: %s", longbuf);
356 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
358 printf("\t-> \"%s\"\n", cp);
360 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
365 main(int argc, char *argv[])
367 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
370 verbose = reply = FALSE;
371 while ((ch = getopt(argc, argv, "rv")) != EOF)
383 while (fgets(buf, sizeof(buf)-1, stdin))
385 if (buf[0] == ' ' || buf[0] == '\t')
386 strcat(longbuf, buf);
387 else if (!strncasecmp("From: ", buf, 6)
388 || !strncasecmp("To: ", buf, 4)
389 || !strncasecmp("Reply-", buf, 6)
390 || !strncasecmp("Cc: ", buf, 4)
391 || !strncasecmp("Bcc: ", buf, 5))
392 strcpy(longbuf, buf);
396 fputs(longbuf, stdout);
397 parsebuf(longbuf, reply);
404 fputs(longbuf, stdout);
405 parsebuf(longbuf, reply);
408 #endif /* TESTMAIN */