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;}}
36 #define LONG_STRING 1024
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 static 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,
259 char token[LONG_STRING];
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,
307 char token[LONG_STRING];
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[LONG_STRING], phrase[LONG_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 == '\\')
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);