2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
18 #include <lib-lib/mem.h>
19 #include <lib-lib/macros.h>
23 #include "mutt_idna.h"
27 #define terminate_string(a, b, c) do { if ((b) < (c)) a[(b)] = 0; else \
28 a[(c)] = 0; } while (0)
30 #define terminate_buffer(a, b) terminate_string(a, b, sizeof (a) - 1)
33 const char RFC822Specials[] = "@.,:;<>[]\\\"()";
35 #define is_special(x) strchr(RFC822Specials,x)
39 /* these must defined in the same order as the numerated errors given in rfc822.h */
40 const char *RFC822Errors[] = {
42 "mismatched parenthesis",
49 void rfc822_dequote_comment (char *s)
59 else if (*s != '\"') {
68 void rfc822_free_address (ADDRESS ** p)
75 p_delete(&t->personal);
76 p_delete(&t->mailbox);
81 static const char *parse_comment (const char *s,
82 char *comment, size_t * commentlen,
96 else if (*s == '\\') {
100 if (*commentlen < commentmax)
101 comment[(*commentlen)++] = *s;
105 RFC822Error = ERR_MISMATCH_PAREN;
111 static const char *parse_quote (const char *s, char *token, size_t * tokenlen,
114 if (*tokenlen < tokenmax)
115 token[(*tokenlen)++] = '"';
117 if (*tokenlen < tokenmax)
118 token[*tokenlen] = *s;
127 if (*tokenlen < tokenmax)
128 token[*tokenlen] = *s;
133 RFC822Error = ERR_MISMATCH_QUOTE;
137 static const char *next_token (const char *s, char *token, size_t * tokenlen,
141 return (parse_comment (s + 1, token, tokenlen, tokenmax));
143 return (parse_quote (s + 1, token, tokenlen, tokenmax));
144 if (is_special (*s)) {
145 if (*tokenlen < tokenmax)
146 token[(*tokenlen)++] = *s;
150 if (ISSPACE ((unsigned char) *s) || is_special (*s))
152 if (*tokenlen < tokenmax)
153 token[(*tokenlen)++] = *s;
159 static const char *parse_mailboxdomain (const char *s, const char *nonspecial,
160 char *mailbox, size_t * mailboxlen,
161 size_t mailboxmax, char *comment,
169 if (strchr (nonspecial, *s) == NULL && is_special (*s))
173 if (*commentlen && *commentlen < commentmax)
174 comment[(*commentlen)++] = ' ';
175 ps = next_token (s, comment, commentlen, commentmax);
178 ps = next_token (s, mailbox, mailboxlen, mailboxmax);
187 static const char *parse_address (const char *s,
188 char *token, size_t * tokenlen,
189 size_t tokenmax, char *comment,
190 size_t * commentlen, size_t commentmax,
193 s = parse_mailboxdomain (s, ".\"(\\",
194 token, tokenlen, tokenmax,
195 comment, commentlen, commentmax);
200 if (*tokenlen < tokenmax)
201 token[(*tokenlen)++] = '@';
202 s = parse_mailboxdomain (s + 1, ".([]\\",
203 token, tokenlen, tokenmax,
204 comment, commentlen, commentmax);
209 terminate_string (token, *tokenlen, tokenmax);
210 addr->mailbox = str_dup (token);
212 if (*commentlen && !addr->personal) {
213 terminate_string (comment, *commentlen, commentmax);
214 addr->personal = str_dup (comment);
220 static const char *parse_route_addr (const char *s,
221 char *comment, size_t * commentlen,
222 size_t commentmax, ADDRESS * addr)
229 /* find the end of the route */
231 while (s && *s == '@') {
232 if (tokenlen < sizeof (token) - 1)
233 token[tokenlen++] = '@';
234 s = parse_mailboxdomain (s + 1, ",.\\[](", token,
235 &tokenlen, sizeof (token) - 1,
236 comment, commentlen, commentmax);
238 if (!s || *s != ':') {
239 RFC822Error = ERR_BAD_ROUTE;
240 return NULL; /* invalid route */
243 if (tokenlen < sizeof (token) - 1)
244 token[tokenlen++] = ':';
249 parse_address (s, token, &tokenlen, sizeof (token) - 1, comment,
250 commentlen, commentmax, addr)) == NULL)
254 RFC822Error = ERR_BAD_ROUTE_ADDR;
259 addr->mailbox = str_dup ("@");
265 static const char *parse_addr_spec (const char *s,
266 char *comment, size_t * commentlen,
267 size_t commentmax, ADDRESS * addr)
273 parse_address (s, token, &tokenlen, sizeof (token) - 1, comment,
274 commentlen, commentmax, addr);
275 if (s && *s && *s != ',' && *s != ';') {
276 RFC822Error = ERR_BAD_ADDR_SPEC;
283 add_addrspec (ADDRESS ** top, ADDRESS ** last, const char *phrase,
284 char *comment, size_t * commentlen, size_t commentmax)
286 ADDRESS *cur = rfc822_new_address ();
288 if (parse_addr_spec (phrase, comment, commentlen, commentmax, cur) == NULL) {
289 rfc822_free_address (&cur);
300 ADDRESS *rfc822_parse_adrlist (ADDRESS * top, const char *s)
303 const char *begin, *ps;
304 char comment[STRING], phrase[STRING];
305 size_t phraselen = 0, commentlen = 0;
306 ADDRESS *cur, *last = NULL;
311 while (last && last->next)
314 ws_pending = isspace ((unsigned char) *s);
321 terminate_buffer (phrase, phraselen);
322 add_addrspec (&top, &last, phrase, comment, &commentlen,
323 sizeof (comment) - 1);
325 else if (commentlen && last && !last->personal) {
326 terminate_buffer (comment, commentlen);
327 last->personal = str_dup (comment);
336 else if (*s == '(') {
337 if (commentlen && commentlen < sizeof (comment) - 1)
338 comment[commentlen++] = ' ';
340 next_token (s, comment, &commentlen,
341 sizeof (comment) - 1)) == NULL) {
342 rfc822_free_address (&top);
347 else if (*s == ':') {
348 cur = rfc822_new_address ();
349 terminate_buffer (phrase, phraselen);
350 cur->mailbox = str_dup (phrase);
365 else if (*s == ';') {
367 terminate_buffer (phrase, phraselen);
368 add_addrspec (&top, &last, phrase, comment, &commentlen,
369 sizeof (comment) - 1);
371 else if (commentlen && last && !last->personal) {
372 terminate_buffer (comment, commentlen);
373 last->personal = str_dup (comment);
376 /* add group terminator */
377 cur = rfc822_new_address ();
389 else if (*s == '<') {
390 terminate_buffer (phrase, phraselen);
391 cur = rfc822_new_address ();
394 p_delete(&cur->personal);
395 /* if we get something like "Michael R. Elkins" remove the quotes */
396 rfc822_dequote_comment (phrase);
397 cur->personal = str_dup (phrase);
400 parse_route_addr (s + 1, comment, &commentlen,
401 sizeof (comment) - 1, cur)) == NULL) {
402 rfc822_free_address (&top);
403 rfc822_free_address (&cur);
418 if (phraselen && phraselen < sizeof (phrase) - 1 && ws_pending)
419 phrase[phraselen++] = ' ';
421 next_token (s, phrase, &phraselen, sizeof (phrase) - 1)) == NULL) {
422 rfc822_free_address (&top);
427 ws_pending = isspace ((unsigned char) *s);
432 terminate_buffer (phrase, phraselen);
433 terminate_buffer (comment, commentlen);
434 add_addrspec (&top, &last, phrase, comment, &commentlen,
435 sizeof (comment) - 1);
437 else if (commentlen && last && !last->personal) {
438 terminate_buffer (comment, commentlen);
439 last->personal = str_dup (comment);
445 void rfc822_qualify (ADDRESS * addr, const char *host)
449 for (; addr; addr = addr->next)
450 if (!addr->group && addr->mailbox && strchr (addr->mailbox, '@') == NULL) {
451 p = p_new(char, str_len(addr->mailbox) + str_len(host) + 2);
452 sprintf (p, "%s@%s", addr->mailbox, host); /* __SPRINTF_CHECKED__ */
453 p_delete(&addr->mailbox);
459 rfc822_cat (char *buf, size_t buflen, const char *value, const char *specials)
461 if (strpbrk (value, specials)) {
462 char tmp[256], *pc = tmp;
463 size_t tmplen = sizeof (tmp) - 3;
466 for (; *value && tmplen > 1; value++) {
467 if (*value == '\\' || *value == '"') {
476 strfcpy (buf, tmp, buflen);
479 strfcpy (buf, value, buflen);
482 void rfc822_write_address_single (char *buf, size_t buflen, ADDRESS * addr,
492 buflen--; /* save room for the terminal nul */
494 if (addr->personal) {
495 if (strpbrk (addr->personal, RFC822Specials)) {
500 for (pc = addr->personal; *pc && buflen > 0; pc++) {
501 if (*pc == '"' || *pc == '\\') {
520 strfcpy (pbuf, addr->personal, buflen);
521 len = str_len (pbuf);
532 if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) {
542 if (ascii_strcmp (addr->mailbox, "@") && !display) {
543 strfcpy (pbuf, addr->mailbox, buflen);
544 len = str_len (pbuf);
546 else if (ascii_strcmp (addr->mailbox, "@") && display) {
547 strfcpy (pbuf, mutt_addr_for_display (addr), buflen);
548 len = str_len (pbuf);
557 if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) {
582 /* no need to check for length here since we already save space at the
583 beginning of this routine */
587 /* note: it is assumed that `buf' is nul terminated! */
588 void rfc822_write_address (char *buf, size_t buflen, ADDRESS * addr,
592 size_t len = str_len (buf);
594 buflen--; /* save room for the terminal nul */
598 return; /* safety check for bogus arguments */
612 for (; addr && buflen > 0; addr = addr->next) {
613 /* use buflen+1 here because we already saved space for the trailing
614 nul char, and the subroutine can make use of it */
615 rfc822_write_address_single (pbuf, buflen + 1, addr, display);
617 /* this should be safe since we always have at least 1 char passed into
618 the above call, which means `pbuf' should always be nul terminated */
619 len = str_len (pbuf);
623 /* if there is another address, and its not a group mailbox name or
624 group terminator, add a comma to separate the addresses */
625 if (addr->next && addr->next->mailbox && !addr->group) {
640 /* this should be rfc822_cpy_adr */
641 ADDRESS *rfc822_cpy_adr_real (ADDRESS * addr)
643 ADDRESS *p = rfc822_new_address ();
645 p->personal = str_dup (addr->personal);
646 p->mailbox = str_dup (addr->mailbox);
647 p->group = addr->group;
651 /* this should be rfc822_cpy_adrlist */
652 ADDRESS *rfc822_cpy_adr (ADDRESS * addr)
654 ADDRESS *top = NULL, *last = NULL;
656 for (; addr; addr = addr->next) {
658 last->next = rfc822_cpy_adr_real (addr);
662 top = last = rfc822_cpy_adr_real (addr);
667 /* append list 'b' to list 'a' and return the last element in the new list */
668 ADDRESS *rfc822_append (ADDRESS ** a, ADDRESS * b)
672 while (tmp && tmp->next)
677 tmp->next = rfc822_cpy_adr (b);
679 tmp = *a = rfc822_cpy_adr (b);
680 while (tmp && tmp->next)