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);
362 terminate_buffer (phrase, phraselen);
363 add_addrspec (&top, &last, phrase, comment, &commentlen, sizeof (comment) - 1);
365 else if (commentlen && last && !last->personal)
367 terminate_buffer (comment, commentlen);
368 last->personal = safe_strdup (comment);
372 if (last && !last->val)
373 last->val = mutt_substrdup (begin, s);
383 if (commentlen && commentlen < sizeof (comment) - 1)
384 comment[commentlen++] = ' ';
385 if ((ps = next_token (s, comment, &commentlen, sizeof (comment) - 1)) == NULL)
387 rfc822_free_address (&top);
394 cur = rfc822_new_address ();
395 terminate_buffer (phrase, phraselen);
396 cur->mailbox = safe_strdup (phrase);
406 last->val = mutt_substrdup (begin, s);
419 terminate_buffer (phrase, phraselen);
420 add_addrspec (&top, &last, phrase, comment, &commentlen, sizeof (comment) - 1);
422 else if (commentlen && last && !last->personal)
424 terminate_buffer (comment, commentlen);
425 last->personal = safe_strdup (comment);
428 if (last && !last->val)
429 last->val = mutt_substrdup (begin, s);
432 /* add group terminator */
433 cur = rfc822_new_address ();
448 terminate_buffer (phrase, phraselen);
449 cur = rfc822_new_address ();
453 FREE (&cur->personal);
454 /* if we get something like "Michael R. Elkins" remove the quotes */
455 rfc822_dequote_comment (phrase);
456 cur->personal = safe_strdup (phrase);
458 if ((ps = parse_route_addr (s + 1, comment, &commentlen, sizeof (comment) - 1, cur)) == NULL)
460 rfc822_free_address (&top);
461 rfc822_free_address (&cur);
477 if (phraselen && phraselen < sizeof (phrase) - 1 && ws_pending)
478 phrase[phraselen++] = ' ';
479 if ((ps = next_token (s, phrase, &phraselen, sizeof (phrase) - 1)) == NULL)
481 rfc822_free_address (&top);
486 ws_pending = isspace ((unsigned char) *s);
492 terminate_buffer (phrase, phraselen);
493 terminate_buffer (comment, commentlen);
494 add_addrspec (&top, &last, phrase, comment, &commentlen, sizeof (comment) - 1);
496 else if (commentlen && last && !last->personal)
498 terminate_buffer (comment, commentlen);
499 last->personal = safe_strdup (comment);
503 last->val = mutt_substrdup (begin, s);
509 void rfc822_qualify (ADDRESS *addr, const char *host)
513 for (; addr; addr = addr->next)
514 if (!addr->group && addr->mailbox && strchr (addr->mailbox, '@') == NULL)
516 p = safe_malloc (mutt_strlen (addr->mailbox) + mutt_strlen (host) + 2);
517 sprintf (p, "%s@%s", addr->mailbox, host); /* __SPRINTF_CHECKED__ */
518 FREE (&addr->mailbox);
524 rfc822_cat (char *buf, size_t buflen, const char *value, const char *specials)
526 if (strpbrk (value, specials))
528 char tmp[256], *pc = tmp;
529 size_t tmplen = sizeof (tmp) - 3;
532 for (; *value && tmplen > 1; value++)
534 if (*value == '\\' || *value == '"')
544 strfcpy (buf, tmp, buflen);
547 strfcpy (buf, value, buflen);
550 void rfc822_write_address_single (char *buf, size_t buflen, ADDRESS *addr,
560 buflen--; /* save room for the terminal nul */
567 strfcpy (pbuf, addr->val, buflen);
568 len = mutt_strlen (pbuf);
585 if (strpbrk (addr->personal, RFC822Specials))
591 for (pc = addr->personal; *pc && buflen > 0; pc++)
593 if (*pc == '"' || *pc == '\\')
614 strfcpy (pbuf, addr->personal, buflen);
615 len = mutt_strlen (pbuf);
626 if (addr->personal || (addr->mailbox && *addr->mailbox == '@'))
638 if (ascii_strcmp (addr->mailbox, "@") && !display)
640 strfcpy (pbuf, addr->mailbox, buflen);
641 len = mutt_strlen (pbuf);
643 else if (ascii_strcmp (addr->mailbox, "@") && display)
645 strfcpy (pbuf, mutt_addr_for_display (addr), buflen);
646 len = mutt_strlen (pbuf);
656 if (addr->personal || (addr->mailbox && *addr->mailbox == '@'))
684 /* no need to check for length here since we already save space at the
685 beginning of this routine */
689 /* note: it is assumed that `buf' is nul terminated! */
690 int rfc822_write_address (char *buf, size_t buflen, ADDRESS *addr, int display)
693 size_t len = mutt_strlen (buf);
695 buflen--; /* save room for the terminal nul */
700 return pbuf - buf; /* safety check for bogus arguments */
714 for (; addr && buflen > 0; addr = addr->next)
716 /* use buflen+1 here because we already saved space for the trailing
717 nul char, and the subroutine can make use of it */
718 rfc822_write_address_single (pbuf, buflen + 1, addr, display);
720 /* this should be safe since we always have at least 1 char passed into
721 the above call, which means `pbuf' should always be nul terminated */
722 len = mutt_strlen (pbuf);
726 /* if there is another address, and its not a group mailbox name or
727 group terminator, add a comma to separate the addresses */
728 if (addr->next && addr->next->mailbox && !addr->group)
745 /* this should be rfc822_cpy_adr */
746 ADDRESS *rfc822_cpy_adr_real (ADDRESS *addr)
748 ADDRESS *p = rfc822_new_address ();
751 p->val = safe_strdup (addr->val);
753 p->personal = safe_strdup (addr->personal);
754 p->mailbox = safe_strdup (addr->mailbox);
755 p->group = addr->group;
759 /* this should be rfc822_cpy_adrlist */
760 ADDRESS *rfc822_cpy_adr (ADDRESS *addr)
762 ADDRESS *top = NULL, *last = NULL;
764 for (; addr; addr = addr->next)
768 last->next = rfc822_cpy_adr_real (addr);
772 top = last = rfc822_cpy_adr_real (addr);
777 /* append list 'b' to list 'a' and return the last element in the new list */
778 ADDRESS *rfc822_append (ADDRESS **a, ADDRESS *b)
782 while (tmp && tmp->next)
787 tmp->next = rfc822_cpy_adr (b);
789 tmp = *a = rfc822_cpy_adr (b);
790 while (tmp && tmp->next)
795 /* incomplete. Only used to thwart the APOP MD5 attack (#2846). */
796 int rfc822_valid_msgid (const char *msgid)
798 /* msg-id = "<" addr-spec ">"
799 * addr-spec = local-part "@" domain
800 * local-part = word *("." word)
801 * word = atom / quoted-string
802 * atom = 1*<any CHAR except specials, SPACE and CTLs>
804 * specials = "(" / ")" / "<" / ">" / "@"
805 / "," / ";" / ":" / "\" / <">
808 * CTLS = ( 0.-31., 127.)
809 * quoted-string = <"> *(qtext/quoted-pair) <">
810 * qtext = <any CHAR except <">, "\" and CR>
812 * quoted-pair = "\" CHAR
813 * domain = sub-domain *("." sub-domain)
814 * sub-domain = domain-ref / domain-literal
816 * domain-literal = "[" *(dtext / quoted-pair) "]"
822 if (!msgid || !*msgid)
825 l = mutt_strlen (msgid);
826 if (l < 5) /* <atom@atom> */
828 if (msgid[0] != '<' || msgid[l-1] != '>')
830 if (!(dom = strrchr (msgid, '@')))
833 /* TODO: complete parser */
834 for (i = 0; i < l; i++)
835 if ((unsigned char)msgid[i] > 127)
842 int safe_free (void **p) /* __SAFE_FREE_CHECKED__ */
844 free(*p); /* __MEM_CHECKED__ */
848 int main (int argc, char **argv)
853 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)";
855 char *str = "a b c ";
858 list = rfc822_parse_adrlist (NULL, str);
860 rfc822_write_address (buf, sizeof (buf), list);
861 rfc822_free_address (&list);