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.
33 #include <lib-lib/mem.h>
34 #include <lib-lib/str.h>
35 #include <lib-lib/ascii.h>
36 #include <lib-lib/macros.h>
38 #include "mutt_idna.h"
40 void rfc822_qualify(address_t *addr, const char *host)
44 for (; addr; addr = addr->next) {
45 if (!addr->group && addr->mailbox && !strchr(addr->mailbox, '@')) {
46 p = p_new(char, m_strlen(addr->mailbox) + m_strlen(host) + 2);
47 sprintf(p, "%s@%s", addr->mailbox, host); /* __SPRINTF_CHECKED__ */
48 p_delete(&addr->mailbox);
54 address_t *address_dup(const address_t *addr)
56 address_t *res = address_new();
58 res->personal = m_strdup(addr->personal);
59 res->mailbox = m_strdup(addr->mailbox);
60 res->group = addr->group;
64 address_t *address_list_dup(const address_t *addr)
66 address_t *res = NULL, **resp = &res;
68 for (; addr; addr = addr->next) {
69 *resp = address_dup(addr);
70 resp = &(*resp)->next;
77 /****************************************************************************/
78 /* Parsing functions */
79 /****************************************************************************/
81 typedef struct static_buf {
86 static inline void stbuf_append(static_buf *buf, int c) {
87 if (buf->len < ssizeof(buf->buf) - 1) {
88 buf->buf[buf->len++] = c;
89 buf->buf[buf->len] = '\0';
93 static inline void stbuf_append_sp(static_buf *buf) {
95 stbuf_append(buf, ' ');
98 static char *rfc822_dequote_comment(static_buf *buf)
100 char *res = p_new(char, buf->len + 1);
105 for (i = 0; i < buf->len; i++) {
110 if (++i >= buf->len) /* should not happen */
121 static const char *parse_comment(const char *s, static_buf *buf)
138 s++; /* if *++s is NUL it will be an error anyway */
145 stbuf_append(buf, *s);
151 static const char *parse_quote(const char *s, static_buf *buf)
156 stbuf_append(buf, *s);
165 stbuf_append(buf, *s);
173 const char RFC822Specials[] = "@.,:;<>[]\\\"()";
175 static const char *next_phrase(const char *s, static_buf *buf)
178 stbuf_append(buf, '"');
179 return parse_quote(s + 1, buf);
182 if (strchr(RFC822Specials, *s)) {
183 stbuf_append(buf, *s);
188 if (ISSPACE(*s) || strchr(RFC822Specials, *s))
190 stbuf_append(buf, *s++);
197 parse_mailboxdomain(const char *s, const char *nonspecial, static_buf *mbox,
203 if (!strchr(nonspecial, *s) && strchr(RFC822Specials, *s))
207 stbuf_append_sp(comment);
208 s = parse_comment(s + 1, comment);
210 s = next_phrase(s, mbox);
221 parse_address(const char *s, static_buf *comment, address_t *cur)
223 static_buf token = {"", 0};
225 s = parse_mailboxdomain(s, ".\"(\\", &token, comment);
230 stbuf_append(&token, '@');
231 s = parse_mailboxdomain(s + 1, ".([]\\", &token, comment);
236 cur->mailbox = p_dupstr(token.buf, token.len);
238 if (comment->len && !cur->personal) {
239 cur->personal = p_dupstr(comment->buf, comment->len);
245 address_t **rfc822_eotoken(address_t **last, static_buf *phrase, static_buf *comment)
249 address_t *cur = address_new();
251 s = parse_address(phrase->buf, comment, cur);
252 if (s && *s && *s != ',' && *s != ';') {
253 address_list_wipe(&cur);
258 return &(*last)->next;
264 address_t *rfc822_parse_adrlist(address_t *top, const char *s)
266 static_buf comment = {"", 0};
267 static_buf phrase = {"", 0};
269 address_t **last = address_list_last(&top);
273 ws_pending = ISSPACE(*s);
281 stbuf_append_sp(&phrase);
282 s = next_phrase(s, &phrase);
284 address_list_wipe(&top);
290 stbuf_append_sp(&comment);
291 s = parse_comment(s + 1, &comment);
293 address_list_wipe(&top);
302 /* if we get something like "Michael R. Elkins" remove the quotes */
303 cur->personal = rfc822_dequote_comment(&phrase);
306 s = parse_address(skipspaces(s + 1), &comment, cur);
307 if (!s || *s != '>' || !cur->mailbox) {
308 address_list_wipe(&top);
309 address_list_wipe(&cur);
314 last = &(*last)->next;
318 last = rfc822_eotoken(last, &phrase, &comment);
321 case ':': /* group start */
322 *last = address_new();
323 (*last)->mailbox = p_dupstr(phrase.buf, phrase.len);
325 last = &(*last)->next;
329 last = rfc822_eotoken(last, &phrase, &comment);
330 /* add group terminator */
331 *last = address_new();
332 last = &(*last)->next;
336 last = rfc822_eotoken(last, &phrase, &comment);
340 comment.len = phrase.len = 0;
348 /****************************************************************************/
349 /* Output functions */
350 /****************************************************************************/
353 rfc822_strcpy(char *buf, ssize_t buflen, const char *p, const char *specials)
355 if (strpbrk(p, specials)) {
360 while (*p && pos < buflen - 2) {
361 if (*p == '\\' || *p == '"') {
362 if (pos >= buflen - 4)
374 return m_strcpy(buf, buflen, p);
378 ssize_t rfc822_write_address_single(char *buf, ssize_t buflen,
379 address_t *addr, int display)
386 buflen--; /* save room for the terminal nul */
388 if (addr->personal) {
389 pos = rfc822_strcpy(buf, buflen, addr->personal, RFC822Specials);
390 if (pos + 2 >= buflen)
399 pos += m_strcpy(buf + pos, buflen - pos, addr->mailbox);
401 pos += m_strcpy(buf + pos, buflen - pos, mutt_addr_for_display(addr));
404 if (addr->personal) {
405 if (pos + 1 >= buflen)
411 if (pos + 1 >= buflen)
416 if (pos + 1 >= buflen)
422 /* no need to check for length here since we already save space at the
423 beginning of this routine */
428 /* note: it is assumed that `buf' is nul terminated! */
430 rfc822_write_address(char *buf, ssize_t buflen, address_t *addr, int display)
434 buflen--; /* save room for the terminal nul */
435 pos = m_strnlen(buf, buflen);
438 if (pos + 2 >= buflen)
445 for (; addr; addr = addr->next) {
446 pos += rfc822_write_address_single(buf + pos, buflen + 1 - pos,
449 if (!addr->group && addr->next && addr->next->mailbox) {
450 /* if there is another address, and its not a group mailbox name or
451 group terminator, add a comma to separate the addresses */
452 if (pos + 2 >= buflen)
465 address_t *mutt_parse_adrlist(address_t *p, const char *s)
467 /* check for a simple whitespace separated list of addresses */
468 char *q = strpbrk(s, "\"<>():;,\\");
469 char tmp[HUGE_STRING];
472 return rfc822_parse_adrlist(p, s);
474 m_strcpy(tmp, sizeof(tmp), s);
476 while ((q = strtok(q, " \t"))) {
477 p = rfc822_parse_adrlist(p, q);