2 * rfc822.c -- code for slicing and dicing RFC822 mail headers
4 * Copyright 1996 by Eric S. Raymond
6 * For license terms, see the file COPYING in this directory.
12 #if defined(STDC_HEADERS)
16 #include "fetchmail.h"
18 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
24 void 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 */
30 int parendepth, state, has_host_part;
32 if (strncmp("From: ", buf, 6)
33 && strncmp("To: ", buf, 4)
34 && strncmp("Reply-", buf, 6)
35 && strncmp("Cc: ", buf, 4)
36 && strncmp("Bcc: ", buf, 5)) {
40 parendepth = state = 0;
41 has_host_part = FALSE;
42 for (from = buf; *from; from++)
47 printf("state %d: %s", state, buf);
48 printf("%*s^\n", from - buf + 10, " ");
54 else if (*from == ')')
57 if (!parendepth && !has_host_part)
60 case 0: /* before header colon */
65 case 1: /* we've seen the colon, we're looking for addresses */
68 else if (*from == '@')
70 else if (*from == '"')
72 else if ((*from == ',' || HEADER_END(from)) && !has_host_part)
76 while (isspace(*from))
79 hostlen = strlen(host);
80 for (cp = from + strlen(from); cp >= from; --cp)
83 memcpy(from, host, hostlen);
89 case 2: /* we're in a string */
94 case 3: /* we're in a <>-enclosed address */
97 else if (*from == '>' && !has_host_part)
101 hostlen = strlen(host);
102 for (cp = from + strlen(from); cp >= from; --cp)
105 memcpy(from, host, hostlen);
106 from += strlen(from);
107 has_host_part = TRUE;
115 /* parse addresses in succession out of a specified RFC822 header */
116 const char *hdr; /* header to be parsed, NUL to continue previous hdr */
118 static char *tp, address[POPBUFSIZE+1];
119 static const char *hp;
120 static int state, oldstate;
122 static const char *orighdr;
123 #endif /* TESTMAIN */
126 #define START_HDR 0 /* before header colon */
127 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
128 #define BARE_ADDRESS 2 /* collecting address without delimiters */
129 #define INSIDE_DQUOTE 3 /* inside double quotes */
130 #define INSIDE_PARENS 4 /* inside parentheses */
131 #define INSIDE_BRACKETS 5 /* inside bracketed address */
132 #define ENDIT_ALL 6 /* after last address */
140 #endif /* TESTMAIN */
148 printf("state %d: %s", state, orighdr);
149 printf("%*s^\n", hp - orighdr + 10, " ");
151 #endif /* TESTMAIN */
153 if (state == ENDIT_ALL) /* after last address */
155 else if (HEADER_END(hp))
158 while (isspace(*--tp))
161 return(tp > address ? (tp = address) : (char *)NULL);
163 else if (*hp == '\\') /* handle RFC822 escaping */
165 if (state != INSIDE_PARENS)
167 *tp++ = *hp++; /* take the escape */
168 *tp++ = *hp; /* take following char */
173 case START_HDR: /* before header colon */
181 case SKIP_JUNK: /* looking for address start */
182 if (*hp == '"') /* quoted string */
184 oldstate = SKIP_JUNK;
185 state = INSIDE_DQUOTE;
188 else if (*hp == '(') /* address comment -- ignore */
191 state = INSIDE_PARENS;
193 else if (*hp == '<') /* begin <address> */
195 state = INSIDE_BRACKETS;
198 else if (*hp != ',' && !isspace(*hp))
201 state = BARE_ADDRESS;
205 case BARE_ADDRESS: /* collecting address without delimiters */
206 if (*hp == ',') /* end of address */
212 return(tp = address);
215 else if (*hp == '(') /* beginning of comment */
218 state = INSIDE_PARENS;
220 else if (*hp == '<') /* beginning of real address */
222 state = INSIDE_BRACKETS;
225 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
229 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
239 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
248 case INSIDE_BRACKETS: /* possible <>-enclosed address */
249 if (*hp == '>') /* end of address */
254 return(tp = address);
256 else if (*hp == '<') /* nested <> */
258 else if (*hp == '"') /* quoted address */
261 oldstate = INSIDE_BRACKETS;
262 state = INSIDE_DQUOTE;
264 else /* just copy address */
274 static void parsebuf(char *longbuf, int reply)
280 reply_hack(longbuf, "HOSTNAME.NET");
281 printf("Rewritten buffer: %s", longbuf);
284 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
286 printf("\t-> \"%s\"\n", cp);
288 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
293 main(int argc, char *argv[])
295 char buf[MSGBUFSIZE], longbuf[BUFSIZ];
298 verbose = reply = FALSE;
299 while ((ch = getopt(argc, argv, "rv")) != EOF)
311 while (fgets(buf, sizeof(buf)-1, stdin))
313 if (buf[0] == ' ' || buf[0] == '\t')
314 strcat(longbuf, buf);
315 else if (!strncmp("From: ", buf, 6)
316 || !strncmp("To: ", buf, 4)
317 || !strncmp("Reply-", buf, 6)
318 || !strncmp("Cc: ", buf, 4)
319 || !strncmp("Bcc: ", buf, 5))
320 strcpy(longbuf, buf);
324 fputs(longbuf, stdout);
325 parsebuf(longbuf, reply);
332 fputs(longbuf, stdout);
333 parsebuf(longbuf, reply);
336 #endif /* TESTMAIN */