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.
21 #define safe_strdup strdup
22 #define safe_malloc malloc
23 #define SKIPWS(x) while(isspace(*x))x++
24 #define FREE(x) safe_free(x)
25 #define ISSPACE isspace
26 #define strfcpy(a,b,c) {if (c) {strncpy(a,b,c);a[c-1]=0;}}
31 #include "mutt_idna.h"
33 #define terminate_string(a, b, c) do { if ((b) < (c)) a[(b)] = 0; else \
34 a[(c)] = 0; } while (0)
36 #define terminate_buffer(a, b) terminate_string(a, b, sizeof (a) - 1)
39 const char RFC822Specials[] = "@.,:;<>[]\\\"()";
41 #define is_special(x) strchr(RFC822Specials,x)
45 /* these must defined in the same order as the numerated errors given in rfc822.h */
46 const char *RFC822Errors[] = {
48 "mismatched parenthesis",
55 void rfc822_dequote_comment (char *s)
65 else if (*s != '\"') {
74 void rfc822_free_address (ADDRESS ** p)
90 static const char *parse_comment (const char *s,
91 char *comment, size_t * commentlen,
105 else if (*s == '\\') {
109 if (*commentlen < commentmax)
110 comment[(*commentlen)++] = *s;
114 RFC822Error = ERR_MISMATCH_PAREN;
120 static const char *parse_quote (const char *s, char *token, size_t * tokenlen,
123 if (*tokenlen < tokenmax)
124 token[(*tokenlen)++] = '"';
126 if (*tokenlen < tokenmax)
127 token[*tokenlen] = *s;
136 if (*tokenlen < tokenmax)
137 token[*tokenlen] = *s;
142 RFC822Error = ERR_MISMATCH_QUOTE;
146 static const char *next_token (const char *s, char *token, size_t * tokenlen,
150 return (parse_comment (s + 1, token, tokenlen, tokenmax));
152 return (parse_quote (s + 1, token, tokenlen, tokenmax));
153 if (is_special (*s)) {
154 if (*tokenlen < tokenmax)
155 token[(*tokenlen)++] = *s;
159 if (ISSPACE ((unsigned char) *s) || is_special (*s))
161 if (*tokenlen < tokenmax)
162 token[(*tokenlen)++] = *s;
168 static const char *parse_mailboxdomain (const char *s, const char *nonspecial,
169 char *mailbox, size_t * mailboxlen,
170 size_t mailboxmax, char *comment,
178 if (strchr (nonspecial, *s) == NULL && is_special (*s))
182 if (*commentlen && *commentlen < commentmax)
183 comment[(*commentlen)++] = ' ';
184 ps = next_token (s, comment, commentlen, commentmax);
187 ps = next_token (s, mailbox, mailboxlen, mailboxmax);
196 static const char *parse_address (const char *s,
197 char *token, size_t * tokenlen,
198 size_t tokenmax, char *comment,
199 size_t * commentlen, size_t commentmax,
202 s = parse_mailboxdomain (s, ".\"(\\",
203 token, tokenlen, tokenmax,
204 comment, commentlen, commentmax);
209 if (*tokenlen < tokenmax)
210 token[(*tokenlen)++] = '@';
211 s = parse_mailboxdomain (s + 1, ".([]\\",
212 token, tokenlen, tokenmax,
213 comment, commentlen, commentmax);
218 terminate_string (token, *tokenlen, tokenmax);
219 addr->mailbox = safe_strdup (token);
221 if (*commentlen && !addr->personal) {
222 terminate_string (comment, *commentlen, commentmax);
223 addr->personal = safe_strdup (comment);
229 static const char *parse_route_addr (const char *s,
230 char *comment, size_t * commentlen,
231 size_t commentmax, ADDRESS * addr)
238 /* find the end of the route */
240 while (s && *s == '@') {
241 if (tokenlen < sizeof (token) - 1)
242 token[tokenlen++] = '@';
243 s = parse_mailboxdomain (s + 1, ",.\\[](", token,
244 &tokenlen, sizeof (token) - 1,
245 comment, commentlen, commentmax);
247 if (!s || *s != ':') {
248 RFC822Error = ERR_BAD_ROUTE;
249 return NULL; /* invalid route */
252 if (tokenlen < sizeof (token) - 1)
253 token[tokenlen++] = ':';
258 parse_address (s, token, &tokenlen, sizeof (token) - 1, comment,
259 commentlen, commentmax, addr)) == NULL)
263 RFC822Error = ERR_BAD_ROUTE_ADDR;
268 addr->mailbox = safe_strdup ("@");
274 static const char *parse_addr_spec (const char *s,
275 char *comment, size_t * commentlen,
276 size_t commentmax, ADDRESS * addr)
282 parse_address (s, token, &tokenlen, sizeof (token) - 1, comment,
283 commentlen, commentmax, addr);
284 if (s && *s && *s != ',' && *s != ';') {
285 RFC822Error = ERR_BAD_ADDR_SPEC;
292 add_addrspec (ADDRESS ** top, ADDRESS ** last, const char *phrase,
293 char *comment, size_t * commentlen, size_t commentmax)
295 ADDRESS *cur = rfc822_new_address ();
297 if (parse_addr_spec (phrase, comment, commentlen, commentmax, cur) == NULL) {
298 rfc822_free_address (&cur);
309 ADDRESS *rfc822_parse_adrlist (ADDRESS * top, const char *s)
312 const char *begin, *ps;
313 char comment[STRING], phrase[STRING];
314 size_t phraselen = 0, commentlen = 0;
315 ADDRESS *cur, *last = NULL;
320 while (last && last->next)
323 ws_pending = isspace ((unsigned char) *s);
330 terminate_buffer (phrase, phraselen);
331 add_addrspec (&top, &last, phrase, comment, &commentlen,
332 sizeof (comment) - 1);
334 else if (commentlen && last && !last->personal) {
335 terminate_buffer (comment, commentlen);
336 last->personal = safe_strdup (comment);
340 if (last && !last->val)
341 last->val = mutt_substrdup (begin, s);
349 else if (*s == '(') {
350 if (commentlen && commentlen < sizeof (comment) - 1)
351 comment[commentlen++] = ' ';
353 next_token (s, comment, &commentlen,
354 sizeof (comment) - 1)) == NULL) {
355 rfc822_free_address (&top);
360 else if (*s == ':') {
361 cur = rfc822_new_address ();
362 terminate_buffer (phrase, phraselen);
363 cur->mailbox = safe_strdup (phrase);
373 last->val = mutt_substrdup (begin, s);
382 else if (*s == ';') {
384 terminate_buffer (phrase, phraselen);
385 add_addrspec (&top, &last, phrase, comment, &commentlen,
386 sizeof (comment) - 1);
388 else if (commentlen && last && !last->personal) {
389 terminate_buffer (comment, commentlen);
390 last->personal = safe_strdup (comment);
393 if (last && !last->val)
394 last->val = mutt_substrdup (begin, s);
397 /* add group terminator */
398 cur = rfc822_new_address ();
410 else if (*s == '<') {
411 terminate_buffer (phrase, phraselen);
412 cur = rfc822_new_address ();
415 FREE (&cur->personal);
416 /* if we get something like "Michael R. Elkins" remove the quotes */
417 rfc822_dequote_comment (phrase);
418 cur->personal = safe_strdup (phrase);
421 parse_route_addr (s + 1, comment, &commentlen,
422 sizeof (comment) - 1, cur)) == NULL) {
423 rfc822_free_address (&top);
424 rfc822_free_address (&cur);
439 if (phraselen && phraselen < sizeof (phrase) - 1 && ws_pending)
440 phrase[phraselen++] = ' ';
442 next_token (s, phrase, &phraselen, sizeof (phrase) - 1)) == NULL) {
443 rfc822_free_address (&top);
448 ws_pending = isspace ((unsigned char) *s);
453 terminate_buffer (phrase, phraselen);
454 terminate_buffer (comment, commentlen);
455 add_addrspec (&top, &last, phrase, comment, &commentlen,
456 sizeof (comment) - 1);
458 else if (commentlen && last && !last->personal) {
459 terminate_buffer (comment, commentlen);
460 last->personal = safe_strdup (comment);
464 last->val = mutt_substrdup (begin, s);
470 void rfc822_qualify (ADDRESS * addr, const char *host)
474 for (; addr; addr = addr->next)
475 if (!addr->group && addr->mailbox && strchr (addr->mailbox, '@') == NULL) {
476 p = safe_malloc (mutt_strlen (addr->mailbox) + mutt_strlen (host) + 2);
477 sprintf (p, "%s@%s", addr->mailbox, host); /* __SPRINTF_CHECKED__ */
478 FREE (&addr->mailbox);
484 rfc822_cat (char *buf, size_t buflen, const char *value, const char *specials)
486 if (strpbrk (value, specials)) {
487 char tmp[256], *pc = tmp;
488 size_t tmplen = sizeof (tmp) - 3;
491 for (; *value && tmplen > 1; value++) {
492 if (*value == '\\' || *value == '"') {
501 strfcpy (buf, tmp, buflen);
504 strfcpy (buf, value, buflen);
507 void rfc822_write_address_single (char *buf, size_t buflen, ADDRESS * addr,
517 buflen--; /* save room for the terminal nul */
523 strfcpy (pbuf, addr->val, buflen);
524 len = mutt_strlen (pbuf);
538 if (addr->personal) {
539 if (strpbrk (addr->personal, RFC822Specials)) {
544 for (pc = addr->personal; *pc && buflen > 0; pc++) {
545 if (*pc == '"' || *pc == '\\') {
564 strfcpy (pbuf, addr->personal, buflen);
565 len = mutt_strlen (pbuf);
576 if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) {
586 if (ascii_strcmp (addr->mailbox, "@") && !display) {
587 strfcpy (pbuf, addr->mailbox, buflen);
588 len = mutt_strlen (pbuf);
590 else if (ascii_strcmp (addr->mailbox, "@") && display) {
591 strfcpy (pbuf, mutt_addr_for_display (addr), buflen);
592 len = mutt_strlen (pbuf);
601 if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) {
626 /* no need to check for length here since we already save space at the
627 beginning of this routine */
631 /* note: it is assumed that `buf' is nul terminated! */
632 void rfc822_write_address (char *buf, size_t buflen, ADDRESS * addr,
636 size_t len = mutt_strlen (buf);
638 buflen--; /* save room for the terminal nul */
642 return; /* safety check for bogus arguments */
656 for (; addr && buflen > 0; addr = addr->next) {
657 /* use buflen+1 here because we already saved space for the trailing
658 nul char, and the subroutine can make use of it */
659 rfc822_write_address_single (pbuf, buflen + 1, addr, display);
661 /* this should be safe since we always have at least 1 char passed into
662 the above call, which means `pbuf' should always be nul terminated */
663 len = mutt_strlen (pbuf);
667 /* if there is another address, and its not a group mailbox name or
668 group terminator, add a comma to separate the addresses */
669 if (addr->next && addr->next->mailbox && !addr->group) {
684 /* this should be rfc822_cpy_adr */
685 ADDRESS *rfc822_cpy_adr_real (ADDRESS * addr)
687 ADDRESS *p = rfc822_new_address ();
690 p->val = safe_strdup (addr->val);
692 p->personal = safe_strdup (addr->personal);
693 p->mailbox = safe_strdup (addr->mailbox);
694 p->group = addr->group;
698 /* this should be rfc822_cpy_adrlist */
699 ADDRESS *rfc822_cpy_adr (ADDRESS * addr)
701 ADDRESS *top = NULL, *last = NULL;
703 for (; addr; addr = addr->next) {
705 last->next = rfc822_cpy_adr_real (addr);
709 top = last = rfc822_cpy_adr_real (addr);
714 /* append list 'b' to list 'a' and return the last element in the new list */
715 ADDRESS *rfc822_append (ADDRESS ** a, ADDRESS * b)
719 while (tmp && tmp->next)
724 tmp->next = rfc822_cpy_adr (b);
726 tmp = *a = rfc822_cpy_adr (b);
727 while (tmp && tmp->next)
733 int safe_free (void **p)
735 free (*p); /* __MEM_CHECKED__ */
739 int main (int argc, char **argv)
746 "michael, Michael Elkins <me@mutt.org>, testing a really complex address: this example <@contains.a.source.route,@with.multiple.hosts:address@example.com>;, lothar@of.the.hillpeople (lothar)";
748 char *str = "a b c ";
751 list = rfc822_parse_adrlist (NULL, str);
753 rfc822_write_address (buf, sizeof (buf), list);
754 rfc822_free_address (&list);