2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
21 * Copyright notice from original mutt:
22 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
24 * This file is part of mutt-ng, see http://www.muttng.org/.
25 * It's licensed under the GNU General Public License,
26 * please see the file GPL in the top level source directory.
29 #include <lib-lib/lib-lib.h>
31 #include "mutt_idna.h"
33 void rfc822_qualify(address_t *addr, const char *host)
37 for (; addr; addr = addr->next) {
38 if (!addr->group && addr->mailbox && !strchr(addr->mailbox, '@')) {
39 p = p_new(char, m_strlen(addr->mailbox) + m_strlen(host) + 2);
40 sprintf(p, "%s@%s", addr->mailbox, host); /* __SPRINTF_CHECKED__ */
41 p_delete(&addr->mailbox);
47 address_t *address_dup(const address_t *addr)
49 address_t *res = address_new();
51 res->personal = m_strdup(addr->personal);
52 res->mailbox = m_strdup(addr->mailbox);
53 res->group = addr->group;
57 address_t *address_list_dup(const address_t *addr)
59 address_t *res = NULL, **resp = &res;
61 for (; addr; addr = addr->next) {
62 *resp = address_dup(addr);
63 resp = &(*resp)->next;
69 /* given a list of addresses, return a list of unique addresses */
70 void address_list_uniq(address_t *a)
72 for (; a; a = a->next) {
73 address_t **b = &a->next;
79 if ((*b)->mailbox && !ascii_strcasecmp((*b)->mailbox, a->mailbox))
81 address_t *pop = address_list_pop(b);
90 /****************************************************************************/
91 /* Parsing functions */
92 /****************************************************************************/
94 typedef struct static_buf {
99 static inline void stbuf_append(static_buf *buf, int c) {
100 if (buf->len < ssizeof(buf->buf) - 1) {
101 buf->buf[buf->len++] = c;
102 buf->buf[buf->len] = '\0';
106 static inline void stbuf_append_sp(static_buf *buf) {
108 stbuf_append(buf, ' ');
111 static char *rfc822_dequote_comment(static_buf *buf)
113 char *res = p_new(char, buf->len + 1);
118 for (i = 0; i < buf->len; i++) {
123 if (++i >= buf->len) /* should not happen */
134 static const char *parse_comment(const char *s, static_buf *buf)
151 s++; /* if *++s is NUL it will be an error anyway */
158 stbuf_append(buf, *s);
164 static const char *parse_quote(const char *s, static_buf *buf)
169 stbuf_append(buf, *s);
178 stbuf_append(buf, *s);
186 const char RFC822Specials[] = "@.,:;<>[]\\\"()";
188 static const char *next_phrase(const char *s, static_buf *buf)
191 stbuf_append(buf, '"');
192 return parse_quote(s + 1, buf);
195 if (strchr(RFC822Specials, *s)) {
196 stbuf_append(buf, *s);
201 if (ISSPACE(*s) || strchr(RFC822Specials, *s))
203 stbuf_append(buf, *s++);
210 parse_mailboxdomain(const char *s, const char *nonspecial, static_buf *mbox,
216 if (!strchr(nonspecial, *s) && strchr(RFC822Specials, *s))
220 stbuf_append_sp(comment);
221 s = parse_comment(s + 1, comment);
223 s = next_phrase(s, mbox);
234 parse_address(const char *s, static_buf *comment, address_t *cur)
236 static_buf token = {"", 0};
238 s = parse_mailboxdomain(s, ".\"(\\", &token, comment);
243 stbuf_append(&token, '@');
244 s = parse_mailboxdomain(s + 1, ".([]\\", &token, comment);
249 cur->mailbox = p_dupstr(token.buf, token.len);
251 if (comment->len && !cur->personal) {
252 cur->personal = p_dupstr(comment->buf, comment->len);
258 static address_t **rfc822_eotoken(address_t **last, static_buf *phrase, static_buf *comment)
262 address_t *cur = address_new();
264 s = parse_address(phrase->buf, comment, cur);
265 if (s && *s && *s != ',' && *s != ';') {
266 address_list_wipe(&cur);
271 return &(*last)->next;
277 address_t *rfc822_parse_adrlist(address_t *top, const char *s)
279 static_buf comment = {"", 0};
280 static_buf phrase = {"", 0};
282 address_t **last = address_list_last(&top);
286 ws_pending = ISSPACE(*s);
294 stbuf_append_sp(&phrase);
295 s = next_phrase(s, &phrase);
297 address_list_wipe(&top);
303 stbuf_append_sp(&comment);
304 s = parse_comment(s + 1, &comment);
306 address_list_wipe(&top);
315 /* if we get something like "Michael R. Elkins" remove the quotes */
316 cur->personal = rfc822_dequote_comment(&phrase);
319 s = parse_address(skipspaces(s + 1), &comment, cur);
320 if (!s || *s != '>' || !cur->mailbox) {
321 address_list_wipe(&top);
322 address_list_wipe(&cur);
327 last = &(*last)->next;
331 last = rfc822_eotoken(last, &phrase, &comment);
334 case ':': /* group start */
335 *last = address_new();
336 (*last)->mailbox = p_dupstr(phrase.buf, phrase.len);
338 last = &(*last)->next;
342 last = rfc822_eotoken(last, &phrase, &comment);
343 /* add group terminator */
344 *last = address_new();
345 last = &(*last)->next;
349 last = rfc822_eotoken(last, &phrase, &comment);
353 comment.len = phrase.len = 0;
361 /****************************************************************************/
362 /* Output functions */
363 /****************************************************************************/
366 rfc822_strcpy(char *buf, ssize_t buflen, const char *p, const char *specials)
368 if (strpbrk(p, specials)) {
373 while (*p && pos < buflen - 2) {
374 if (*p == '\\' || *p == '"') {
375 if (pos >= buflen - 4)
387 return m_strcpy(buf, buflen, p);
391 ssize_t rfc822_write_address_single(char *buf, ssize_t buflen,
392 address_t *addr, int display)
399 buflen--; /* save room for the terminal nul */
401 if (addr->personal) {
402 pos = rfc822_strcpy(buf, buflen, addr->personal, RFC822Specials);
403 if (pos + 2 >= buflen)
412 pos += m_strcpy(buf + pos, buflen - pos, addr->mailbox);
414 pos += m_strcpy(buf + pos, buflen - pos, mutt_addr_for_display(addr));
417 if (addr->personal) {
418 if (pos + 1 >= buflen)
424 if (pos + 1 >= buflen)
429 if (pos + 1 >= buflen)
435 /* no need to check for length here since we already save space at the
436 beginning of this routine */
441 /* note: it is assumed that `buf' is nul terminated! */
443 rfc822_write_address(char *buf, ssize_t buflen, address_t *addr, int display)
447 buflen--; /* save room for the terminal nul */
448 pos = m_strnlen(buf, buflen);
451 if (pos + 2 >= buflen)
458 for (; addr; addr = addr->next) {
459 pos += rfc822_write_address_single(buf + pos, buflen + 1 - pos,
462 if (!addr->group && addr->next && addr->next->mailbox) {
463 /* if there is another address, and its not a group mailbox name or
464 group terminator, add a comma to separate the addresses */
465 if (pos + 2 >= buflen)
478 address_t *mutt_parse_adrlist(address_t *p, const char *s)
480 /* check for a simple whitespace separated list of addresses */
481 char *q = strpbrk(s, "\"<>():;,\\");
482 char tmp[HUGE_STRING];
485 return rfc822_parse_adrlist(p, s);
487 m_strcpy(tmp, sizeof(tmp), s);
489 while ((q = strtok(q, " \t"))) {
490 p = rfc822_parse_adrlist(p, q);