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.
19 #include "mutt_idna.h"
25 #define terminate_string(a, b, c) do { if ((b) < (c)) a[(b)] = 0; else \
26 a[(c)] = 0; } while (0)
28 #define terminate_buffer(a, b) terminate_string(a, b, sizeof (a) - 1)
31 const char RFC822Specials[] = "@.,:;<>[]\\\"()";
33 #define is_special(x) strchr(RFC822Specials,x)
37 /* these must defined in the same order as the numerated errors given in rfc822.h */
38 const char *RFC822Errors[] = {
40 "mismatched parenthesis",
47 void rfc822_dequote_comment (char *s)
57 else if (*s != '\"') {
66 void rfc822_free_address (ADDRESS ** p)
82 static const char *parse_comment (const char *s,
83 char *comment, size_t * commentlen,
97 else if (*s == '\\') {
101 if (*commentlen < commentmax)
102 comment[(*commentlen)++] = *s;
106 RFC822Error = ERR_MISMATCH_PAREN;
112 static const char *parse_quote (const char *s, char *token, size_t * tokenlen,
115 if (*tokenlen < tokenmax)
116 token[(*tokenlen)++] = '"';
118 if (*tokenlen < tokenmax)
119 token[*tokenlen] = *s;
128 if (*tokenlen < tokenmax)
129 token[*tokenlen] = *s;
134 RFC822Error = ERR_MISMATCH_QUOTE;
138 static const char *next_token (const char *s, char *token, size_t * tokenlen,
142 return (parse_comment (s + 1, token, tokenlen, tokenmax));
144 return (parse_quote (s + 1, token, tokenlen, tokenmax));
145 if (is_special (*s)) {
146 if (*tokenlen < tokenmax)
147 token[(*tokenlen)++] = *s;
151 if (ISSPACE ((unsigned char) *s) || is_special (*s))
153 if (*tokenlen < tokenmax)
154 token[(*tokenlen)++] = *s;
160 static const char *parse_mailboxdomain (const char *s, const char *nonspecial,
161 char *mailbox, size_t * mailboxlen,
162 size_t mailboxmax, char *comment,
170 if (strchr (nonspecial, *s) == NULL && is_special (*s))
174 if (*commentlen && *commentlen < commentmax)
175 comment[(*commentlen)++] = ' ';
176 ps = next_token (s, comment, commentlen, commentmax);
179 ps = next_token (s, mailbox, mailboxlen, mailboxmax);
188 static const char *parse_address (const char *s,
189 char *token, size_t * tokenlen,
190 size_t tokenmax, char *comment,
191 size_t * commentlen, size_t commentmax,
194 s = parse_mailboxdomain (s, ".\"(\\",
195 token, tokenlen, tokenmax,
196 comment, commentlen, commentmax);
201 if (*tokenlen < tokenmax)
202 token[(*tokenlen)++] = '@';
203 s = parse_mailboxdomain (s + 1, ".([]\\",
204 token, tokenlen, tokenmax,
205 comment, commentlen, commentmax);
210 terminate_string (token, *tokenlen, tokenmax);
211 addr->mailbox = safe_strdup (token);
213 if (*commentlen && !addr->personal) {
214 terminate_string (comment, *commentlen, commentmax);
215 addr->personal = safe_strdup (comment);
221 static const char *parse_route_addr (const char *s,
222 char *comment, size_t * commentlen,
223 size_t commentmax, ADDRESS * addr)
230 /* find the end of the route */
232 while (s && *s == '@') {
233 if (tokenlen < sizeof (token) - 1)
234 token[tokenlen++] = '@';
235 s = parse_mailboxdomain (s + 1, ",.\\[](", token,
236 &tokenlen, sizeof (token) - 1,
237 comment, commentlen, commentmax);
239 if (!s || *s != ':') {
240 RFC822Error = ERR_BAD_ROUTE;
241 return NULL; /* invalid route */
244 if (tokenlen < sizeof (token) - 1)
245 token[tokenlen++] = ':';
250 parse_address (s, token, &tokenlen, sizeof (token) - 1, comment,
251 commentlen, commentmax, addr)) == NULL)
255 RFC822Error = ERR_BAD_ROUTE_ADDR;
260 addr->mailbox = safe_strdup ("@");
266 static const char *parse_addr_spec (const char *s,
267 char *comment, size_t * commentlen,
268 size_t commentmax, ADDRESS * addr)
274 parse_address (s, token, &tokenlen, sizeof (token) - 1, comment,
275 commentlen, commentmax, addr);
276 if (s && *s && *s != ',' && *s != ';') {
277 RFC822Error = ERR_BAD_ADDR_SPEC;
284 add_addrspec (ADDRESS ** top, ADDRESS ** last, const char *phrase,
285 char *comment, size_t * commentlen, size_t commentmax)
287 ADDRESS *cur = rfc822_new_address ();
289 if (parse_addr_spec (phrase, comment, commentlen, commentmax, cur) == NULL) {
290 rfc822_free_address (&cur);
301 ADDRESS *rfc822_parse_adrlist (ADDRESS * top, const char *s)
304 const char *begin, *ps;
305 char comment[STRING], phrase[STRING];
306 size_t phraselen = 0, commentlen = 0;
307 ADDRESS *cur, *last = NULL;
312 while (last && last->next)
315 ws_pending = isspace ((unsigned char) *s);
322 terminate_buffer (phrase, phraselen);
323 add_addrspec (&top, &last, phrase, comment, &commentlen,
324 sizeof (comment) - 1);
326 else if (commentlen && last && !last->personal) {
327 terminate_buffer (comment, commentlen);
328 last->personal = safe_strdup (comment);
332 if (last && !last->val)
333 last->val = mutt_substrdup (begin, s);
341 else if (*s == '(') {
342 if (commentlen && commentlen < sizeof (comment) - 1)
343 comment[commentlen++] = ' ';
345 next_token (s, comment, &commentlen,
346 sizeof (comment) - 1)) == NULL) {
347 rfc822_free_address (&top);
352 else if (*s == ':') {
353 cur = rfc822_new_address ();
354 terminate_buffer (phrase, phraselen);
355 cur->mailbox = safe_strdup (phrase);
365 last->val = mutt_substrdup (begin, s);
374 else if (*s == ';') {
376 terminate_buffer (phrase, phraselen);
377 add_addrspec (&top, &last, phrase, comment, &commentlen,
378 sizeof (comment) - 1);
380 else if (commentlen && last && !last->personal) {
381 terminate_buffer (comment, commentlen);
382 last->personal = safe_strdup (comment);
385 if (last && !last->val)
386 last->val = mutt_substrdup (begin, s);
389 /* add group terminator */
390 cur = rfc822_new_address ();
402 else if (*s == '<') {
403 terminate_buffer (phrase, phraselen);
404 cur = rfc822_new_address ();
407 FREE (&cur->personal);
408 /* if we get something like "Michael R. Elkins" remove the quotes */
409 rfc822_dequote_comment (phrase);
410 cur->personal = safe_strdup (phrase);
413 parse_route_addr (s + 1, comment, &commentlen,
414 sizeof (comment) - 1, cur)) == NULL) {
415 rfc822_free_address (&top);
416 rfc822_free_address (&cur);
431 if (phraselen && phraselen < sizeof (phrase) - 1 && ws_pending)
432 phrase[phraselen++] = ' ';
434 next_token (s, phrase, &phraselen, sizeof (phrase) - 1)) == NULL) {
435 rfc822_free_address (&top);
440 ws_pending = isspace ((unsigned char) *s);
445 terminate_buffer (phrase, phraselen);
446 terminate_buffer (comment, commentlen);
447 add_addrspec (&top, &last, phrase, comment, &commentlen,
448 sizeof (comment) - 1);
450 else if (commentlen && last && !last->personal) {
451 terminate_buffer (comment, commentlen);
452 last->personal = safe_strdup (comment);
456 last->val = mutt_substrdup (begin, s);
462 void rfc822_qualify (ADDRESS * addr, const char *host)
466 for (; addr; addr = addr->next)
467 if (!addr->group && addr->mailbox && strchr (addr->mailbox, '@') == NULL) {
468 p = safe_malloc (mutt_strlen (addr->mailbox) + mutt_strlen (host) + 2);
469 sprintf (p, "%s@%s", addr->mailbox, host); /* __SPRINTF_CHECKED__ */
470 FREE (&addr->mailbox);
476 rfc822_cat (char *buf, size_t buflen, const char *value, const char *specials)
478 if (strpbrk (value, specials)) {
479 char tmp[256], *pc = tmp;
480 size_t tmplen = sizeof (tmp) - 3;
483 for (; *value && tmplen > 1; value++) {
484 if (*value == '\\' || *value == '"') {
493 strfcpy (buf, tmp, buflen);
496 strfcpy (buf, value, buflen);
499 void rfc822_write_address_single (char *buf, size_t buflen, ADDRESS * addr,
509 buflen--; /* save room for the terminal nul */
515 strfcpy (pbuf, addr->val, buflen);
516 len = mutt_strlen (pbuf);
530 if (addr->personal) {
531 if (strpbrk (addr->personal, RFC822Specials)) {
536 for (pc = addr->personal; *pc && buflen > 0; pc++) {
537 if (*pc == '"' || *pc == '\\') {
556 strfcpy (pbuf, addr->personal, buflen);
557 len = mutt_strlen (pbuf);
568 if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) {
578 if (ascii_strcmp (addr->mailbox, "@") && !display) {
579 strfcpy (pbuf, addr->mailbox, buflen);
580 len = mutt_strlen (pbuf);
582 else if (ascii_strcmp (addr->mailbox, "@") && display) {
583 strfcpy (pbuf, mutt_addr_for_display (addr), buflen);
584 len = mutt_strlen (pbuf);
593 if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) {
618 /* no need to check for length here since we already save space at the
619 beginning of this routine */
623 /* note: it is assumed that `buf' is nul terminated! */
624 void rfc822_write_address (char *buf, size_t buflen, ADDRESS * addr,
628 size_t len = mutt_strlen (buf);
630 buflen--; /* save room for the terminal nul */
634 return; /* safety check for bogus arguments */
648 for (; addr && buflen > 0; addr = addr->next) {
649 /* use buflen+1 here because we already saved space for the trailing
650 nul char, and the subroutine can make use of it */
651 rfc822_write_address_single (pbuf, buflen + 1, addr, display);
653 /* this should be safe since we always have at least 1 char passed into
654 the above call, which means `pbuf' should always be nul terminated */
655 len = mutt_strlen (pbuf);
659 /* if there is another address, and its not a group mailbox name or
660 group terminator, add a comma to separate the addresses */
661 if (addr->next && addr->next->mailbox && !addr->group) {
676 /* this should be rfc822_cpy_adr */
677 ADDRESS *rfc822_cpy_adr_real (ADDRESS * addr)
679 ADDRESS *p = rfc822_new_address ();
682 p->val = safe_strdup (addr->val);
684 p->personal = safe_strdup (addr->personal);
685 p->mailbox = safe_strdup (addr->mailbox);
686 p->group = addr->group;
690 /* this should be rfc822_cpy_adrlist */
691 ADDRESS *rfc822_cpy_adr (ADDRESS * addr)
693 ADDRESS *top = NULL, *last = NULL;
695 for (; addr; addr = addr->next) {
697 last->next = rfc822_cpy_adr_real (addr);
701 top = last = rfc822_cpy_adr_real (addr);
706 /* append list 'b' to list 'a' and return the last element in the new list */
707 ADDRESS *rfc822_append (ADDRESS ** a, ADDRESS * b)
711 while (tmp && tmp->next)
716 tmp->next = rfc822_cpy_adr (b);
718 tmp = *a = rfc822_cpy_adr (b);
719 while (tmp && tmp->next)