2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #define safe_strdup strdup
31 #define safe_malloc malloc
32 #define SKIPWS(x) while(isspace(*x))x++
33 #define FREE(x) safe_free(x)
34 #define ISSPACE isspace
35 #define strfcpy(a,b,c) {if (c) {strncpy(a,b,c);a[c-1]=0;}}
40 #include "mutt_idna.h"
42 #define terminate_string(a, b, c) do { if ((b) < (c)) a[(b)] = 0; else \
43 a[(c)] = 0; } while (0)
45 #define terminate_buffer(a, b) terminate_string(a, b, sizeof (a) - 1)
48 const char RFC822Specials[] = "@.,:;<>[]\\\"()";
49 #define is_special(x) strchr(RFC822Specials,x)
53 /* these must defined in the same order as the numerated errors given in rfc822.h */
54 const char *RFC822Errors[] = {
56 "mismatched parenthesis",
63 void rfc822_dequote_comment (char *s)
85 void rfc822_free_address (ADDRESS **p)
103 parse_comment (const char *s,
104 char *comment, size_t *commentlen, size_t commentmax)
125 if (*commentlen < commentmax)
126 comment[(*commentlen)++] = *s;
131 RFC822Error = ERR_MISMATCH_PAREN;
138 parse_quote (const char *s, char *token, size_t *tokenlen, size_t tokenmax)
140 if (*tokenlen < tokenmax)
141 token[(*tokenlen)++] = '"';
144 if (*tokenlen < tokenmax)
145 token[*tokenlen] = *s;
156 if (*tokenlen < tokenmax)
157 token[*tokenlen] = *s;
162 RFC822Error = ERR_MISMATCH_QUOTE;
167 next_token (const char *s, char *token, size_t *tokenlen, size_t tokenmax)
170 return (parse_comment (s + 1, token, tokenlen, tokenmax));
172 return (parse_quote (s + 1, token, tokenlen, tokenmax));
175 if (*tokenlen < tokenmax)
176 token[(*tokenlen)++] = *s;
181 if (ISSPACE ((unsigned char) *s) || is_special (*s))
183 if (*tokenlen < tokenmax)
184 token[(*tokenlen)++] = *s;
191 parse_mailboxdomain (const char *s, const char *nonspecial,
192 char *mailbox, size_t *mailboxlen, size_t mailboxmax,
193 char *comment, size_t *commentlen, size_t commentmax)
200 if (strchr (nonspecial, *s) == NULL && is_special (*s))
205 if (*commentlen && *commentlen < commentmax)
206 comment[(*commentlen)++] = ' ';
207 ps = next_token (s, comment, commentlen, commentmax);
210 ps = next_token (s, mailbox, mailboxlen, mailboxmax);
220 parse_address (const char *s,
221 char *token, size_t *tokenlen, size_t tokenmax,
222 char *comment, size_t *commentlen, size_t commentmax,
225 s = parse_mailboxdomain (s, ".\"(\\",
226 token, tokenlen, tokenmax,
227 comment, commentlen, commentmax);
233 if (*tokenlen < tokenmax)
234 token[(*tokenlen)++] = '@';
235 s = parse_mailboxdomain (s + 1, ".([]\\",
236 token, tokenlen, tokenmax,
237 comment, commentlen, commentmax);
242 terminate_string (token, *tokenlen, tokenmax);
243 addr->mailbox = safe_strdup (token);
245 if (*commentlen && !addr->personal)
247 terminate_string (comment, *commentlen, commentmax);
248 addr->personal = safe_strdup (comment);
255 parse_route_addr (const char *s,
256 char *comment, size_t *commentlen, size_t commentmax,
264 /* find the end of the route */
267 while (s && *s == '@')
269 if (tokenlen < sizeof (token) - 1)
270 token[tokenlen++] = '@';
271 s = parse_mailboxdomain (s + 1, ",.\\[](", token,
272 &tokenlen, sizeof (token) - 1,
273 comment, commentlen, commentmax);
277 RFC822Error = ERR_BAD_ROUTE;
278 return NULL; /* invalid route */
281 if (tokenlen < sizeof (token) - 1)
282 token[tokenlen++] = ':';
286 if ((s = parse_address (s, token, &tokenlen, sizeof (token) - 1, comment, commentlen, commentmax, addr)) == NULL)
291 RFC822Error = ERR_BAD_ROUTE_ADDR;
296 addr->mailbox = safe_strdup ("@");
303 parse_addr_spec (const char *s,
304 char *comment, size_t *commentlen, size_t commentmax,
310 s = parse_address (s, token, &tokenlen, sizeof (token) - 1, comment, commentlen, commentmax, addr);
311 if (s && *s && *s != ',' && *s != ';')
313 RFC822Error = ERR_BAD_ADDR_SPEC;
320 add_addrspec (ADDRESS **top, ADDRESS **last, const char *phrase,
321 char *comment, size_t *commentlen, size_t commentmax)
323 ADDRESS *cur = rfc822_new_address ();
325 if (parse_addr_spec (phrase, comment, commentlen, commentmax, cur) == NULL)
327 rfc822_free_address (&cur);
338 ADDRESS *rfc822_parse_adrlist (ADDRESS *top, const char *s)
341 const char *begin, *ps;
342 char comment[STRING], phrase[STRING];
343 size_t phraselen = 0, commentlen = 0;
344 ADDRESS *cur, *last = NULL;
349 while (last && last->next)
352 ws_pending = isspace ((unsigned char) *s);
353 if ((nl = mutt_strlen (s)))
354 nl = s[nl - 1] == '\n';
364 terminate_buffer (phrase, phraselen);
365 add_addrspec (&top, &last, phrase, comment, &commentlen, sizeof (comment) - 1);
367 else if (commentlen && last && !last->personal)
369 terminate_buffer (comment, commentlen);
370 last->personal = safe_strdup (comment);
374 if (last && !last->val)
375 last->val = mutt_substrdup (begin, s);
385 if (commentlen && commentlen < sizeof (comment) - 1)
386 comment[commentlen++] = ' ';
387 if ((ps = next_token (s, comment, &commentlen, sizeof (comment) - 1)) == NULL)
389 rfc822_free_address (&top);
396 cur = rfc822_new_address ();
397 terminate_buffer (phrase, phraselen);
398 cur->mailbox = safe_strdup (phrase);
408 last->val = mutt_substrdup (begin, s);
421 terminate_buffer (phrase, phraselen);
422 add_addrspec (&top, &last, phrase, comment, &commentlen, sizeof (comment) - 1);
424 else if (commentlen && last && !last->personal)
426 terminate_buffer (comment, commentlen);
427 last->personal = safe_strdup (comment);
430 if (last && !last->val)
431 last->val = mutt_substrdup (begin, s);
434 /* add group terminator */
435 cur = rfc822_new_address ();
450 terminate_buffer (phrase, phraselen);
451 cur = rfc822_new_address ();
455 FREE (&cur->personal);
456 /* if we get something like "Michael R. Elkins" remove the quotes */
457 rfc822_dequote_comment (phrase);
458 cur->personal = safe_strdup (phrase);
460 if ((ps = parse_route_addr (s + 1, comment, &commentlen, sizeof (comment) - 1, cur)) == NULL)
462 rfc822_free_address (&top);
463 rfc822_free_address (&cur);
479 if (phraselen && phraselen < sizeof (phrase) - 1 && ws_pending)
480 phrase[phraselen++] = ' ';
481 if ((ps = next_token (s, phrase, &phraselen, sizeof (phrase) - 1)) == NULL)
483 rfc822_free_address (&top);
488 ws_pending = isspace ((unsigned char) *s);
494 terminate_buffer (phrase, phraselen);
495 terminate_buffer (comment, commentlen);
496 add_addrspec (&top, &last, phrase, comment, &commentlen, sizeof (comment) - 1);
498 else if (commentlen && last && !last->personal)
500 terminate_buffer (comment, commentlen);
501 last->personal = safe_strdup (comment);
505 last->val = mutt_substrdup (begin, s - nl < begin ? begin : s - nl);
511 void rfc822_qualify (ADDRESS *addr, const char *host)
515 for (; addr; addr = addr->next)
516 if (!addr->group && addr->mailbox && strchr (addr->mailbox, '@') == NULL)
518 p = safe_malloc (mutt_strlen (addr->mailbox) + mutt_strlen (host) + 2);
519 sprintf (p, "%s@%s", addr->mailbox, host); /* __SPRINTF_CHECKED__ */
520 FREE (&addr->mailbox);
526 rfc822_cat (char *buf, size_t buflen, const char *value, const char *specials)
528 if (strpbrk (value, specials))
530 char tmp[256], *pc = tmp;
531 size_t tmplen = sizeof (tmp) - 3;
534 for (; *value && tmplen > 1; value++)
536 if (*value == '\\' || *value == '"')
546 strfcpy (buf, tmp, buflen);
549 strfcpy (buf, value, buflen);
552 void rfc822_write_address_single (char *buf, size_t buflen, ADDRESS *addr,
562 buflen--; /* save room for the terminal nul */
569 strfcpy (pbuf, addr->val, buflen);
570 len = mutt_strlen (pbuf);
587 if (strpbrk (addr->personal, RFC822Specials))
593 for (pc = addr->personal; *pc && buflen > 0; pc++)
595 if (*pc == '"' || *pc == '\\')
616 strfcpy (pbuf, addr->personal, buflen);
617 len = mutt_strlen (pbuf);
628 if (addr->personal || (addr->mailbox && *addr->mailbox == '@'))
640 if (ascii_strcmp (addr->mailbox, "@") && !display)
642 strfcpy (pbuf, addr->mailbox, buflen);
643 len = mutt_strlen (pbuf);
645 else if (ascii_strcmp (addr->mailbox, "@") && display)
647 strfcpy (pbuf, mutt_addr_for_display (addr), buflen);
648 len = mutt_strlen (pbuf);
658 if (addr->personal || (addr->mailbox && *addr->mailbox == '@'))
686 /* no need to check for length here since we already save space at the
687 beginning of this routine */
691 /* note: it is assumed that `buf' is nul terminated! */
692 int rfc822_write_address (char *buf, size_t buflen, ADDRESS *addr, int display)
695 size_t len = mutt_strlen (buf);
697 buflen--; /* save room for the terminal nul */
702 return pbuf - buf; /* safety check for bogus arguments */
716 for (; addr && buflen > 0; addr = addr->next)
718 /* use buflen+1 here because we already saved space for the trailing
719 nul char, and the subroutine can make use of it */
720 rfc822_write_address_single (pbuf, buflen + 1, addr, display);
722 /* this should be safe since we always have at least 1 char passed into
723 the above call, which means `pbuf' should always be nul terminated */
724 len = mutt_strlen (pbuf);
728 /* if there is another address, and its not a group mailbox name or
729 group terminator, add a comma to separate the addresses */
730 if (addr->next && addr->next->mailbox && !addr->group)
747 /* this should be rfc822_cpy_adr */
748 ADDRESS *rfc822_cpy_adr_real (ADDRESS *addr)
750 ADDRESS *p = rfc822_new_address ();
753 p->val = safe_strdup (addr->val);
755 p->personal = safe_strdup (addr->personal);
756 p->mailbox = safe_strdup (addr->mailbox);
757 p->group = addr->group;
761 /* this should be rfc822_cpy_adrlist */
762 ADDRESS *rfc822_cpy_adr (ADDRESS *addr)
764 ADDRESS *top = NULL, *last = NULL;
766 for (; addr; addr = addr->next)
770 last->next = rfc822_cpy_adr_real (addr);
774 top = last = rfc822_cpy_adr_real (addr);
779 /* append list 'b' to list 'a' and return the last element in the new list */
780 ADDRESS *rfc822_append (ADDRESS **a, ADDRESS *b)
784 while (tmp && tmp->next)
789 tmp->next = rfc822_cpy_adr (b);
791 tmp = *a = rfc822_cpy_adr (b);
792 while (tmp && tmp->next)
797 /* incomplete. Only used to thwart the APOP MD5 attack (#2846). */
798 int rfc822_valid_msgid (const char *msgid)
800 /* msg-id = "<" addr-spec ">"
801 * addr-spec = local-part "@" domain
802 * local-part = word *("." word)
803 * word = atom / quoted-string
804 * atom = 1*<any CHAR except specials, SPACE and CTLs>
806 * specials = "(" / ")" / "<" / ">" / "@"
807 / "," / ";" / ":" / "\" / <">
810 * CTLS = ( 0.-31., 127.)
811 * quoted-string = <"> *(qtext/quoted-pair) <">
812 * qtext = <any CHAR except <">, "\" and CR>
814 * quoted-pair = "\" CHAR
815 * domain = sub-domain *("." sub-domain)
816 * sub-domain = domain-ref / domain-literal
818 * domain-literal = "[" *(dtext / quoted-pair) "]"
824 if (!msgid || !*msgid)
827 l = mutt_strlen (msgid);
828 if (l < 5) /* <atom@atom> */
830 if (msgid[0] != '<' || msgid[l-1] != '>')
832 if (!(dom = strrchr (msgid, '@')))
835 /* TODO: complete parser */
836 for (i = 0; i < l; i++)
837 if ((unsigned char)msgid[i] > 127)
844 int safe_free (void **p) /* __SAFE_FREE_CHECKED__ */
846 free(*p); /* __MEM_CHECKED__ */
850 int main (int argc, char **argv)
855 char *str = "michael, Michael Elkins <me@mutt.org>, testing a really complex address: this example <@contains.a.source.route,@with.multiple.hosts:address@example.com>;, lothar@of.the.hillpeople (lothar)";
857 char *str = "a b c ";
860 list = rfc822_parse_adrlist (NULL, str);
862 rfc822_write_address (buf, sizeof (buf), list);
863 rfc822_free_address (&list);