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 address_wipe(address_t *addr)
42 p_delete(&addr->personal);
43 p_delete(&addr->mailbox);
44 address_delete(&addr->next);
48 void rfc822_qualify(address_t *addr, const char *host)
52 for (; addr; addr = addr->next) {
53 if (!addr->group && addr->mailbox && !strchr(addr->mailbox, '@')) {
54 p = p_new(char, m_strlen(addr->mailbox) + m_strlen(host) + 2);
55 sprintf(p, "%s@%s", addr->mailbox, host); /* __SPRINTF_CHECKED__ */
56 p_delete(&addr->mailbox);
62 address_t *address_dup(address_t *addr)
64 address_t *res = address_new();
66 res->personal = m_strdup(addr->personal);
67 res->mailbox = m_strdup(addr->mailbox);
68 res->group = addr->group;
72 address_t *address_list_dup(address_t *addr)
74 address_t *res = NULL, **resp = &res;
76 for (; addr; addr = addr->next) {
77 *resp = address_dup(addr);
78 resp = &(*resp)->next;
85 /****************************************************************************/
86 /* Parsing functions */
87 /****************************************************************************/
89 typedef struct static_buf {
94 static inline void stbuf_append(static_buf *buf, int c) {
95 if (buf->len < ssizeof(buf->buf) - 1) {
96 buf->buf[buf->len++] = c;
97 buf->buf[buf->len] = '\0';
101 static inline void stbuf_append_sp(static_buf *buf) {
103 stbuf_append(buf, ' ');
106 static char *rfc822_dequote_comment(static_buf *buf)
108 char *res = p_new(char, buf->len + 1);
113 for (i = 0; i < buf->len; i++) {
118 if (++i >= buf->len) /* should not happen */
129 static const char *parse_comment(const char *s, static_buf *buf)
146 s++; /* if *++s is NUL it will be an error anyway */
153 stbuf_append(buf, *s);
159 static const char *parse_quote(const char *s, static_buf *buf)
164 stbuf_append(buf, *s);
173 stbuf_append(buf, *s);
181 const char RFC822Specials[] = "@.,:;<>[]\\\"()";
183 static const char *next_phrase(const char *s, static_buf *buf)
186 stbuf_append(buf, '"');
187 return parse_quote(s + 1, buf);
190 if (strchr(RFC822Specials, *s)) {
191 stbuf_append(buf, *s);
196 if (ISSPACE(*s) || strchr(RFC822Specials, *s))
198 stbuf_append(buf, *s++);
205 parse_mailboxdomain(const char *s, const char *nonspecial, static_buf *mbox,
211 if (!strchr(nonspecial, *s) && strchr(RFC822Specials, *s))
215 stbuf_append_sp(comment);
216 s = parse_comment(s + 1, comment);
218 s = next_phrase(s, mbox);
229 parse_address(const char *s, static_buf *comment, address_t *cur)
231 static_buf token = {"", 0};
233 s = parse_mailboxdomain(s, ".\"(\\", &token, comment);
238 stbuf_append(&token, '@');
239 s = parse_mailboxdomain(s + 1, ".([]\\", &token, comment);
244 cur->mailbox = p_dupstr(token.buf, token.len);
246 if (comment->len && !cur->personal) {
247 cur->personal = p_dupstr(comment->buf, comment->len);
253 address_t **rfc822_eotoken(address_t **last, static_buf *phrase, static_buf *comment)
257 address_t *cur = address_new();
259 s = parse_address(phrase->buf, comment, cur);
260 if (s && *s && *s != ',' && *s != ';') {
261 address_delete(&cur);
266 return &(*last)->next;
272 address_t *rfc822_parse_adrlist(address_t *top, const char *s)
274 static_buf comment = {"", 0};
275 static_buf phrase = {"", 0};
277 address_t **last = address_list_last(&top);
281 ws_pending = ISSPACE(*s);
289 stbuf_append_sp(&phrase);
290 s = next_phrase(s, &phrase);
292 address_delete(&top);
298 stbuf_append_sp(&comment);
299 s = parse_comment(s + 1, &comment);
301 address_delete(&top);
310 /* if we get something like "Michael R. Elkins" remove the quotes */
311 cur->personal = rfc822_dequote_comment(&phrase);
314 s = parse_address(skipspaces(s + 1), &comment, cur);
315 if (!s || *s != '>' || !cur->mailbox) {
316 address_delete(&top);
317 address_delete(&cur);
322 last = &(*last)->next;
326 last = rfc822_eotoken(last, &phrase, &comment);
329 case ':': /* group start */
330 *last = address_new();
331 (*last)->mailbox = p_dupstr(phrase.buf, phrase.len);
333 last = &(*last)->next;
337 last = rfc822_eotoken(last, &phrase, &comment);
338 /* add group terminator */
339 *last = address_new();
340 last = &(*last)->next;
344 last = rfc822_eotoken(last, &phrase, &comment);
348 comment.len = phrase.len = 0;
356 /****************************************************************************/
357 /* Output functions */
358 /****************************************************************************/
361 rfc822_strcpy(char *buf, ssize_t buflen, const char *p, const char *specials)
363 if (strpbrk(p, specials)) {
368 while (*p && pos < buflen - 2) {
369 if (*p == '\\' || *p == '"') {
370 if (pos >= buflen - 4)
382 return m_strcpy(buf, buflen, p);
386 ssize_t rfc822_write_address_single(char *buf, ssize_t buflen,
387 address_t *addr, int display)
394 buflen--; /* save room for the terminal nul */
396 if (addr->personal) {
397 pos = rfc822_strcpy(buf, buflen, addr->personal, RFC822Specials);
398 if (pos + 2 >= buflen)
407 pos += m_strcpy(buf + pos, buflen - pos, addr->mailbox);
409 pos += m_strcpy(buf + pos, buflen - pos, mutt_addr_for_display(addr));
412 if (addr->personal) {
413 if (pos + 1 >= buflen)
419 if (pos + 1 >= buflen)
424 if (pos + 1 >= buflen)
430 /* no need to check for length here since we already save space at the
431 beginning of this routine */
436 /* note: it is assumed that `buf' is nul terminated! */
438 rfc822_write_address(char *buf, ssize_t buflen, address_t *addr, int display)
442 buflen--; /* save room for the terminal nul */
443 pos = m_strnlen(buf, buflen);
446 if (pos + 2 >= buflen)
453 for (; addr; addr = addr->next) {
454 pos += rfc822_write_address_single(buf + pos, buflen + 1 - pos,
457 if (!addr->group && addr->next && addr->next->mailbox) {
458 /* if there is another address, and its not a group mailbox name or
459 group terminator, add a comma to separate the addresses */
460 if (pos + 2 >= buflen)