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)
38 for (; addr; addr = addr->next) {
39 if (!addr->group && addr->mailbox && !strchr(addr->mailbox, '@')) {
40 char *p = p_new(char, m_strlen(addr->mailbox) + m_strlen(host) + 2);
41 sprintf(p, "%s@%s", addr->mailbox, host);
42 p_delete(&addr->mailbox);
48 address_t *address_dup(const address_t *addr)
50 address_t *res = address_new();
52 res->personal = m_strdup(addr->personal);
53 res->mailbox = m_strdup(addr->mailbox);
54 res->group = addr->group;
58 address_t *address_list_dup(const address_t *addr)
60 address_t *res = NULL, **resp = &res;
62 for (; addr; addr = addr->next) {
63 *resp = address_dup(addr);
64 resp = &(*resp)->next;
70 /* given a list of addresses, return a list of unique addresses */
71 void address_list_uniq(address_t *a)
73 for (; a; a = a->next) {
74 address_t **b = &a->next;
80 if ((*b)->mailbox && !ascii_strcasecmp((*b)->mailbox, a->mailbox))
82 address_t *pop = address_list_pop(b);
91 /****************************************************************************/
92 /* Parsing functions */
93 /****************************************************************************/
95 typedef struct static_buf {
100 static inline void stbuf_append(static_buf *buf, int c) {
101 if (buf->len < ssizeof(buf->buf) - 1) {
102 buf->buf[buf->len++] = c;
103 buf->buf[buf->len] = '\0';
107 static inline void stbuf_append_sp(static_buf *buf) {
109 stbuf_append(buf, ' ');
112 static char *rfc822_dequote_comment(static_buf *buf)
114 char *res = p_new(char, buf->len + 1);
119 for (i = 0; i < buf->len; i++) {
124 if (++i >= buf->len) /* should not happen */
135 static const char *parse_comment(const char *s, static_buf *buf)
152 s++; /* if *++s is NUL it will be an error anyway */
159 stbuf_append(buf, *s);
165 static const char *parse_quote(const char *s, static_buf *buf)
170 stbuf_append(buf, *s);
179 stbuf_append(buf, *s);
187 const char RFC822Specials[] = "@.,:;<>[]\\\"()";
189 static const char *next_phrase(const char *s, static_buf *buf)
192 stbuf_append(buf, '"');
193 return parse_quote(s + 1, buf);
196 if (strchr(RFC822Specials, *s)) {
197 stbuf_append(buf, *s);
202 if (ISSPACE(*s) || strchr(RFC822Specials, *s))
204 stbuf_append(buf, *s++);
211 parse_mailboxdomain(const char *s, const char *nonspecial, static_buf *mbox,
217 if (!strchr(nonspecial, *s) && strchr(RFC822Specials, *s))
221 stbuf_append_sp(comment);
222 s = parse_comment(s + 1, comment);
224 s = next_phrase(s, mbox);
235 parse_address(const char *s, static_buf *comment, address_t *cur)
237 static_buf token = {"", 0};
239 s = parse_mailboxdomain(s, ".\"(\\", &token, comment);
244 stbuf_append(&token, '@');
245 s = parse_mailboxdomain(s + 1, ".([]\\", &token, comment);
250 cur->mailbox = p_dupstr(token.buf, token.len);
252 if (comment->len && !cur->personal) {
253 cur->personal = p_dupstr(comment->buf, comment->len);
259 static address_t **rfc822_eotoken(address_t **last, static_buf *phrase, static_buf *comment)
263 address_t *cur = address_new();
265 s = parse_address(phrase->buf, comment, cur);
266 if (s && *s && *s != ',' && *s != ';') {
267 address_list_wipe(&cur);
272 return &(*last)->next;
278 address_t *rfc822_parse_adrlist(address_t *top, const char *s)
280 static_buf comment = {"", 0};
281 static_buf phrase = {"", 0};
283 address_t **last = address_list_last(&top);
287 ws_pending = ISSPACE(*s);
295 stbuf_append_sp(&phrase);
296 s = next_phrase(s, &phrase);
298 address_list_wipe(&top);
304 stbuf_append_sp(&comment);
305 s = parse_comment(s + 1, &comment);
307 address_list_wipe(&top);
316 /* if we get something like "Michael R. Elkins" remove the quotes */
317 cur->personal = rfc822_dequote_comment(&phrase);
320 s = parse_address(skipspaces(s + 1), &comment, cur);
321 if (!s || *s != '>' || !cur->mailbox) {
322 address_list_wipe(&top);
323 address_list_wipe(&cur);
328 last = &(*last)->next;
332 last = rfc822_eotoken(last, &phrase, &comment);
335 case ':': /* group start */
336 *last = address_new();
337 (*last)->mailbox = p_dupstr(phrase.buf, phrase.len);
339 last = &(*last)->next;
343 last = rfc822_eotoken(last, &phrase, &comment);
344 /* add group terminator */
345 *last = address_new();
346 last = &(*last)->next;
350 last = rfc822_eotoken(last, &phrase, &comment);
354 comment.len = phrase.len = 0;
362 /****************************************************************************/
363 /* Output functions */
364 /****************************************************************************/
367 rfc822_strcpy(char *buf, ssize_t buflen, const char *p, const char *specials)
369 if (strpbrk(p, specials)) {
374 while (*p && pos < buflen - 2) {
375 if (*p == '\\' || *p == '"') {
376 if (pos >= buflen - 4)
388 return m_strcpy(buf, buflen, p);
393 rfc822_addrcpy(char *buf, ssize_t buflen, address_t *addr, int display)
400 if (addr->personal) {
401 pos = rfc822_strcpy(buf, buflen, addr->personal, RFC822Specials);
402 pos += m_strcpy(buf + pos, buflen - pos, " <");
406 pos += m_strcpy(buf + pos, buflen - pos,
407 display ? mutt_addr_for_display(addr) : addr->mailbox);
409 if (addr->personal) {
410 pos += m_strputc(buf + pos, buflen - pos, '>');
414 pos += m_strputc(buf + pos, buflen - pos, ':');
417 pos += m_strputc(buf + pos, buflen - pos, ';');
423 /* note: it is assumed that `buf' is nul terminated! */
425 rfc822_addrcat(char *buf, ssize_t buflen, address_t *addr, int display)
427 ssize_t pos = m_strnlen(buf, buflen);
430 pos += m_strcpy(buf + pos, buflen - pos, ", ");
432 for (; addr; addr = addr->next) {
433 pos += rfc822_addrcpy(buf + pos, buflen - pos, addr, display);
435 if (!addr->group && addr->next && addr->next->mailbox) {
436 /* if there is another address, and its not a group mailbox name or
437 group terminator, add a comma to separate the addresses */
438 pos += m_strcpy(buf + pos, buflen - pos, ", ");
445 address_t *mutt_parse_adrlist(address_t *p, const char *s)
447 /* check for a simple whitespace separated list of addresses */
448 char *q = strpbrk(s, "\"<>():;,\\");
449 char tmp[HUGE_STRING];
452 return rfc822_parse_adrlist(p, s);
454 m_strcpy(tmp, sizeof(tmp), s);
456 while ((q = strtok(q, " \t"))) {
457 p = rfc822_parse_adrlist(p, q);