rfc822.c 19.1 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3
 * Copyright (C) 2011-2013 Michael R. Elkins <me@mutt.org>
Thomas Roessler's avatar
Thomas Roessler committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 * 
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
17
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Thomas Roessler's avatar
Thomas Roessler committed
18 19
 */ 

20 21 22 23
#if HAVE_CONFIG_H
# include "config.h"
#endif

Thomas Roessler's avatar
Thomas Roessler committed
24 25 26 27 28 29 30 31 32 33
#include <string.h>
#include <stdlib.h>

#ifndef TESTING
#include "mutt.h"
#else
#define safe_strdup strdup
#define safe_malloc malloc
#define FREE(x) safe_free(x)
#define strfcpy(a,b,c) {if (c) {strncpy(a,b,c);a[c-1]=0;}}
34
#define LONG_STRING 1024
Thomas Roessler's avatar
Thomas Roessler committed
35 36 37
#include "rfc822.h"
#endif

38 39
#include "mutt_idna.h"

40 41 42 43 44
#define terminate_string(a, b, c) do { if ((b) < (c)) a[(b)] = 0; else \
	a[(c)] = 0; } while (0)

#define terminate_buffer(a, b) terminate_string(a, b, sizeof (a) - 1)

Thomas Roessler's avatar
Thomas Roessler committed
45 46 47 48 49 50
const char RFC822Specials[] = "@.,:;<>[]\\\"()";
#define is_special(x) strchr(RFC822Specials,x)

int RFC822Error = 0;

/* these must defined in the same order as the numerated errors given in rfc822.h */
51
const char * const RFC822Errors[] = {
Thomas Roessler's avatar
Thomas Roessler committed
52 53 54 55 56 57 58 59
  "out of memory",
  "mismatched parenthesis",
  "mismatched quotes",
  "bad route in <>",
  "bad address in <>",
  "bad address spec"
};

60
void rfc822_dequote_comment (char *s)
Thomas Roessler's avatar
Thomas Roessler committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
{
  char *w = s;

  for (; *s; s++)
  {
    if (*s == '\\')
    {
      if (!*++s)
	break; /* error? */
      *w++ = *s;
    }
    else if (*s != '\"')
    {
      if (w != s)
	*w = *s;
      w++;
    }
  }
  *w = 0;
}

82 83 84 85 86 87 88
static void free_address (ADDRESS *a)
{
  FREE(&a->personal);
  FREE(&a->mailbox);
#ifdef EXACT_ADDRESS
  FREE(&a->val);
#endif
89
  FREE(&a);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

int rfc822_remove_from_adrlist (ADDRESS **a, const char *mailbox)
{
  ADDRESS *p, *last = NULL, *t;
  int rv = -1;

  p = *a;
  last = NULL;
  while (p)
  {
    if (ascii_strcasecmp (mailbox, p->mailbox) == 0)
    {
      if (last)
	last->next = p->next;
      else
	(*a) = p->next;
      t = p;
      p = p->next;
      free_address (t);
      rv = 0;
    }
    else
    {
      last = p;
      p = p->next;
    }
  }

  return (rv);
}

Thomas Roessler's avatar
Thomas Roessler committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
void rfc822_free_address (ADDRESS **p)
{
  ADDRESS *t;

  while (*p)
  {
    t = *p;
    *p = (*p)->next;
#ifdef EXACT_ADDRESS
    FREE (&t->val);
#endif
    FREE (&t->personal);
    FREE (&t->mailbox);
    FREE (&t);
  }
}

static const char *
parse_comment (const char *s,
	       char *comment, size_t *commentlen, size_t commentmax)
{
  int level = 1;
  
  while (*s && level)
  {
    if (*s == '(')
      level++;
    else if (*s == ')')
    {
      if (--level == 0)
      {
	s++;
	break;
      }
    }
    else if (*s == '\\')
    {
      if (!*++s)
	break;
    }
    if (*commentlen < commentmax)
      comment[(*commentlen)++] = *s;
    s++;
  }
  if (level)
  {
    RFC822Error = ERR_MISMATCH_PAREN;
    return NULL;
  }
  return s;
}

static const char *
parse_quote (const char *s, char *token, size_t *tokenlen, size_t tokenmax)
{
  while (*s)
  {
    if (*tokenlen < tokenmax)
180
      token[*tokenlen] = *s;
Thomas Roessler's avatar
Thomas Roessler committed
181 182 183 184
    if (*s == '\\')
    {
      if (!*++s)
	break;
185

186 187
      if (*tokenlen < tokenmax)
	token[*tokenlen] = *s;
Thomas Roessler's avatar
Thomas Roessler committed
188
    }
TAKAHASHI Tamotsu's avatar
TAKAHASHI Tamotsu committed
189 190
    else if (*s == '"')
      return (s + 1);
191
    (*tokenlen)++;
Thomas Roessler's avatar
Thomas Roessler committed
192 193 194 195 196 197 198 199 200 201 202 203 204
    s++;
  }
  RFC822Error = ERR_MISMATCH_QUOTE;
  return NULL;
}

static const char *
next_token (const char *s, char *token, size_t *tokenlen, size_t tokenmax)
{
  if (*s == '(')
    return (parse_comment (s + 1, token, tokenlen, tokenmax));
  if (*s == '"')
    return (parse_quote (s + 1, token, tokenlen, tokenmax));
205
  if (*s && is_special (*s))
Thomas Roessler's avatar
Thomas Roessler committed
206 207 208 209 210 211 212
  {
    if (*tokenlen < tokenmax)
      token[(*tokenlen)++] = *s;
    return (s + 1);
  }
  while (*s)
  {
213
    if (is_email_wsp(*s) || is_special (*s))
Thomas Roessler's avatar
Thomas Roessler committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
      break;
    if (*tokenlen < tokenmax)
      token[(*tokenlen)++] = *s;
    s++;
  }
  return s;
}

static const char *
parse_mailboxdomain (const char *s, const char *nonspecial,
		     char *mailbox, size_t *mailboxlen, size_t mailboxmax,
		     char *comment, size_t *commentlen, size_t commentmax)
{
  const char *ps;

  while (*s)
  {
231
    s = skip_email_wsp(s);
232 233 234
    if (! *s)
      return s;

Thomas Roessler's avatar
Thomas Roessler committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    if (strchr (nonspecial, *s) == NULL && is_special (*s))
      return s;

    if (*s == '(')
    {
      if (*commentlen && *commentlen < commentmax)
	comment[(*commentlen)++] = ' ';
      ps = next_token (s, comment, commentlen, commentmax);
    }
    else
      ps = next_token (s, mailbox, mailboxlen, mailboxmax);
    if (!ps)
      return NULL;
    s = ps;
  }

  return s;
}

static const char *
parse_address (const char *s,
Thomas Roessler's avatar
Thomas Roessler committed
256
               char *token, size_t *tokenlen, size_t tokenmax,
Thomas Roessler's avatar
Thomas Roessler committed
257 258 259 260
	       char *comment, size_t *commentlen, size_t commentmax,
	       ADDRESS *addr)
{
  s = parse_mailboxdomain (s, ".\"(\\",
Thomas Roessler's avatar
Thomas Roessler committed
261
			   token, tokenlen, tokenmax,
Thomas Roessler's avatar
Thomas Roessler committed
262 263 264 265 266 267
			   comment, commentlen, commentmax);
  if (!s)
    return NULL;

  if (*s == '@')
  {
Thomas Roessler's avatar
Thomas Roessler committed
268 269
    if (*tokenlen < tokenmax)
      token[(*tokenlen)++] = '@';
Thomas Roessler's avatar
Thomas Roessler committed
270
    s = parse_mailboxdomain (s + 1, ".([]\\",
Thomas Roessler's avatar
Thomas Roessler committed
271
			     token, tokenlen, tokenmax,
Thomas Roessler's avatar
Thomas Roessler committed
272 273 274 275 276
			     comment, commentlen, commentmax);
    if (!s)
      return NULL;
  }

277
  terminate_string (token, *tokenlen, tokenmax);
Thomas Roessler's avatar
Thomas Roessler committed
278
  addr->mailbox = safe_strdup (token);
279

Thomas Roessler's avatar
Thomas Roessler committed
280 281
  if (*commentlen && !addr->personal)
  {
282
    terminate_string (comment, *commentlen, commentmax);
Thomas Roessler's avatar
Thomas Roessler committed
283 284 285 286 287 288 289 290 291 292 293
    addr->personal = safe_strdup (comment);
  }

  return s;
}

static const char *
parse_route_addr (const char *s,
		  char *comment, size_t *commentlen, size_t commentmax,
		  ADDRESS *addr)
{
294
  char token[LONG_STRING];
Thomas Roessler's avatar
Thomas Roessler committed
295 296
  size_t tokenlen = 0;

297
  s = skip_email_wsp(s);
Thomas Roessler's avatar
Thomas Roessler committed
298 299 300 301 302

  /* find the end of the route */
  if (*s == '@')
  {
    while (s && *s == '@')
Thomas Roessler's avatar
Thomas Roessler committed
303 304 305
    {
      if (tokenlen < sizeof (token) - 1)
	token[tokenlen++] = '@';
306
      s = parse_mailboxdomain (s + 1, ",.\\[](", token,
Thomas Roessler's avatar
Thomas Roessler committed
307 308
			       &tokenlen, sizeof (token) - 1,
			       comment, commentlen, commentmax);
Thomas Roessler's avatar
Thomas Roessler committed
309
    }
Thomas Roessler's avatar
Thomas Roessler committed
310 311 312 313 314
    if (!s || *s != ':')
    {
      RFC822Error = ERR_BAD_ROUTE;
      return NULL; /* invalid route */
    }
Thomas Roessler's avatar
Thomas Roessler committed
315 316 317

    if (tokenlen < sizeof (token) - 1)
      token[tokenlen++] = ':';
Thomas Roessler's avatar
Thomas Roessler committed
318 319 320
    s++;
  }

Thomas Roessler's avatar
Thomas Roessler committed
321
  if ((s = parse_address (s, token, &tokenlen, sizeof (token) - 1, comment, commentlen, commentmax, addr)) == NULL)
Thomas Roessler's avatar
Thomas Roessler committed
322 323
    return NULL;

324
  if (*s != '>')
Thomas Roessler's avatar
Thomas Roessler committed
325 326 327 328 329
  {
    RFC822Error = ERR_BAD_ROUTE_ADDR;
    return NULL;
  }

330 331 332
  if (!addr->mailbox)
    addr->mailbox = safe_strdup ("@");

Thomas Roessler's avatar
Thomas Roessler committed
333 334 335 336 337 338 339 340 341
  s++;
  return s;
}

static const char *
parse_addr_spec (const char *s,
		 char *comment, size_t *commentlen, size_t commentmax,
		 ADDRESS *addr)
{
342
  char token[LONG_STRING];
Thomas Roessler's avatar
Thomas Roessler committed
343 344 345
  size_t tokenlen = 0;

  s = parse_address (s, token, &tokenlen, sizeof (token) - 1, comment, commentlen, commentmax, addr);
Thomas Roessler's avatar
Thomas Roessler committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  if (s && *s && *s != ',' && *s != ';')
  {
    RFC822Error = ERR_BAD_ADDR_SPEC;
    return NULL;
  }
  return s;
}

static void
add_addrspec (ADDRESS **top, ADDRESS **last, const char *phrase,
	      char *comment, size_t *commentlen, size_t commentmax)
{
  ADDRESS *cur = rfc822_new_address ();
  
  if (parse_addr_spec (phrase, comment, commentlen, commentmax, cur) == NULL)
361 362
  {
    rfc822_free_address (&cur);
Thomas Roessler's avatar
Thomas Roessler committed
363
    return;
364
  }
Thomas Roessler's avatar
Thomas Roessler committed
365 366 367 368 369 370 371 372 373 374

  if (*last)
    (*last)->next = cur;
  else
    *top = cur;
  *last = cur;
}

ADDRESS *rfc822_parse_adrlist (ADDRESS *top, const char *s)
{
375
  int ws_pending, nl;
376
#ifdef EXACT_ADDRESS
377
  const char *begin;
378 379
#endif
  const char *ps;
380
  char comment[LONG_STRING], phrase[LONG_STRING];
Thomas Roessler's avatar
Thomas Roessler committed
381 382 383 384 385 386 387 388 389
  size_t phraselen = 0, commentlen = 0;
  ADDRESS *cur, *last = NULL;
  
  RFC822Error = 0;

  last = top;
  while (last && last->next)
    last = last->next;

390
  ws_pending = is_email_wsp (*s);
391 392
  if ((nl = mutt_strlen (s)))
    nl = s[nl - 1] == '\n';
393
  
394 395
  s = skip_email_wsp(s);
#ifdef EXACT_ADDRESS
Thomas Roessler's avatar
Thomas Roessler committed
396
  begin = s;
397
#endif
Thomas Roessler's avatar
Thomas Roessler committed
398 399 400 401 402 403
  while (*s)
  {
    if (*s == ',')
    {
      if (phraselen)
      {
404
	terminate_buffer (phrase, phraselen);
Thomas Roessler's avatar
Thomas Roessler committed
405 406 407 408
	add_addrspec (&top, &last, phrase, comment, &commentlen, sizeof (comment) - 1);
      }
      else if (commentlen && last && !last->personal)
      {
409
	terminate_buffer (comment, commentlen);
Thomas Roessler's avatar
Thomas Roessler committed
410 411 412 413
	last->personal = safe_strdup (comment);
      }

#ifdef EXACT_ADDRESS
414
      if (last && !last->val)
Thomas Roessler's avatar
Thomas Roessler committed
415 416 417 418 419
	last->val = mutt_substrdup (begin, s);
#endif
      commentlen = 0;
      phraselen = 0;
      s++;
420 421 422
#ifdef EXACT_ADDRESS
      begin = skip_email_wsp(s);
#endif
Thomas Roessler's avatar
Thomas Roessler committed
423 424 425 426 427 428 429 430 431 432 433 434
    }
    else if (*s == '(')
    {
      if (commentlen && commentlen < sizeof (comment) - 1)
	comment[commentlen++] = ' ';
      if ((ps = next_token (s, comment, &commentlen, sizeof (comment) - 1)) == NULL)
      {
	rfc822_free_address (&top);
	return NULL;
      }
      s = ps;
    }
TAKAHASHI Tamotsu's avatar
TAKAHASHI Tamotsu committed
435 436 437 438 439 440 441 442 443 444 445
    else if (*s == '"')
    {
      if (phraselen && phraselen < sizeof (phrase) - 1)
        phrase[phraselen++] = ' ';
      if ((ps = parse_quote (s + 1, phrase, &phraselen, sizeof (phrase) - 1)) == NULL)
      {
        rfc822_free_address (&top);
        return NULL;
      }
      s = ps;
    }
Thomas Roessler's avatar
Thomas Roessler committed
446 447 448
    else if (*s == ':')
    {
      cur = rfc822_new_address ();
449
      terminate_buffer (phrase, phraselen);
Thomas Roessler's avatar
Thomas Roessler committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
      cur->mailbox = safe_strdup (phrase);
      cur->group = 1;

      if (last)
	last->next = cur;
      else
	top = cur;
      last = cur;

#ifdef EXACT_ADDRESS
      last->val = mutt_substrdup (begin, s);
#endif

      phraselen = 0;
      commentlen = 0;
      s++;
466 467 468
#ifdef EXACT_ADDRESS
      begin = skip_email_wsp(s);
#endif
Thomas Roessler's avatar
Thomas Roessler committed
469 470 471 472 473
    }
    else if (*s == ';')
    {
      if (phraselen)
      {
474
	terminate_buffer (phrase, phraselen);
Thomas Roessler's avatar
Thomas Roessler committed
475 476
	add_addrspec (&top, &last, phrase, comment, &commentlen, sizeof (comment) - 1);
      }
477
      else if (commentlen && last && !last->personal)
Thomas Roessler's avatar
Thomas Roessler committed
478
      {
479
	terminate_buffer (comment, commentlen);
Thomas Roessler's avatar
Thomas Roessler committed
480 481 482
	last->personal = safe_strdup (comment);
      }
#ifdef EXACT_ADDRESS
483
      if (last && !last->val)
Thomas Roessler's avatar
Thomas Roessler committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497
	last->val = mutt_substrdup (begin, s);
#endif

      /* add group terminator */
      cur = rfc822_new_address ();
      if (last)
      {
	last->next = cur;
	last = cur;
      }

      phraselen = 0;
      commentlen = 0;
      s++;
498 499 500
#ifdef EXACT_ADDRESS
      begin = skip_email_wsp(s);
#endif
Thomas Roessler's avatar
Thomas Roessler committed
501 502 503
    }
    else if (*s == '<')
    {
504
      terminate_buffer (phrase, phraselen);
Thomas Roessler's avatar
Thomas Roessler committed
505 506
      cur = rfc822_new_address ();
      if (phraselen)
507
	cur->personal = safe_strdup (phrase);
Thomas Roessler's avatar
Thomas Roessler committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
      if ((ps = parse_route_addr (s + 1, comment, &commentlen, sizeof (comment) - 1, cur)) == NULL)
      {
	rfc822_free_address (&top);
	rfc822_free_address (&cur);
	return NULL;
      }

      if (last)
	last->next = cur;
      else
	top = cur;
      last = cur;

      phraselen = 0;
      commentlen = 0;
      s = ps;
    }
    else
    {
527
      if (phraselen && phraselen < sizeof (phrase) - 1 && ws_pending)
Thomas Roessler's avatar
Thomas Roessler committed
528 529 530 531 532 533 534 535
	phrase[phraselen++] = ' ';
      if ((ps = next_token (s, phrase, &phraselen, sizeof (phrase) - 1)) == NULL)
      {
	rfc822_free_address (&top);
	return NULL;
      }
      s = ps;
    }
536 537
    ws_pending = is_email_wsp(*s);
    s = skip_email_wsp(s);
Thomas Roessler's avatar
Thomas Roessler committed
538 539 540 541
  }
  
  if (phraselen)
  {
542 543
    terminate_buffer (phrase, phraselen);
    terminate_buffer (comment, commentlen);
Thomas Roessler's avatar
Thomas Roessler committed
544 545 546 547
    add_addrspec (&top, &last, phrase, comment, &commentlen, sizeof (comment) - 1);
  }
  else if (commentlen && last && !last->personal)
  {
548
    terminate_buffer (comment, commentlen);
Thomas Roessler's avatar
Thomas Roessler committed
549 550 551 552
    last->personal = safe_strdup (comment);
  }
#ifdef EXACT_ADDRESS
  if (last)
553
    last->val = mutt_substrdup (begin, s - nl < begin ? begin : s - nl);
Thomas Roessler's avatar
Thomas Roessler committed
554 555 556 557 558 559 560 561 562 563 564 565
#endif

  return top;
}

void rfc822_qualify (ADDRESS *addr, const char *host)
{
  char *p;

  for (; addr; addr = addr->next)
    if (!addr->group && addr->mailbox && strchr (addr->mailbox, '@') == NULL)
    {
566
      p = safe_malloc (mutt_strlen (addr->mailbox) + mutt_strlen (host) + 2);
567
      sprintf (p, "%s@%s", addr->mailbox, host);	/* __SPRINTF_CHECKED__ */
568
      FREE (&addr->mailbox);
Thomas Roessler's avatar
Thomas Roessler committed
569 570 571 572 573 574 575 576 577 578 579 580 581
      addr->mailbox = p;
    }
}

void
rfc822_cat (char *buf, size_t buflen, const char *value, const char *specials)
{
  if (strpbrk (value, specials))
  {
    char tmp[256], *pc = tmp;
    size_t tmplen = sizeof (tmp) - 3;

    *pc++ = '"';
Thomas Roessler's avatar
Thomas Roessler committed
582
    for (; *value && tmplen > 1; value++)
Thomas Roessler's avatar
Thomas Roessler committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    {
      if (*value == '\\' || *value == '"')
      {
	*pc++ = '\\';
	tmplen--;
      }
      *pc++ = *value;
      tmplen--;
    }
    *pc++ = '"';
    *pc = 0;
    strfcpy (buf, tmp, buflen);
  }
  else
    strfcpy (buf, value, buflen);
}

600 601
void rfc822_write_address_single (char *buf, size_t buflen, ADDRESS *addr,
				  int display)
Thomas Roessler's avatar
Thomas Roessler committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
{
  size_t len;
  char *pbuf = buf;
  char *pc;
  
  if (!addr)
    return;

  buflen--; /* save room for the terminal nul */

#ifdef EXACT_ADDRESS
  if (addr->val)
  {
    if (!buflen)
      goto done;
    strfcpy (pbuf, addr->val, buflen);
618
    len = mutt_strlen (pbuf);
Thomas Roessler's avatar
Thomas Roessler committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    pbuf += len;
    buflen -= len;
    if (addr->group)
    {
      if (!buflen)
	goto done;
      *pbuf++ = ':';
      buflen--;
      *pbuf = 0;
    }
    return;
  }
#endif

  if (addr->personal)
  {
    if (strpbrk (addr->personal, RFC822Specials))
    {
      if (!buflen)
	goto done;
      *pbuf++ = '"';
      buflen--;
      for (pc = addr->personal; *pc && buflen > 0; pc++)
      {
	if (*pc == '"' || *pc == '\\')
	{
	  *pbuf++ = '\\';
	  buflen--;
	}
	if (!buflen)
	  goto done;
	*pbuf++ = *pc;
	buflen--;
      }
      if (!buflen)
	goto done;
      *pbuf++ = '"';
      buflen--;
    }
    else
    {
      if (!buflen)
	goto done;
      strfcpy (pbuf, addr->personal, buflen);
663
      len = mutt_strlen (pbuf);
Thomas Roessler's avatar
Thomas Roessler committed
664 665 666 667 668 669 670 671
      pbuf += len;
      buflen -= len;
    }

    if (!buflen)
      goto done;
    *pbuf++ = ' ';
    buflen--;
Thomas Roessler's avatar
Thomas Roessler committed
672
  }
Thomas Roessler's avatar
Thomas Roessler committed
673

Thomas Roessler's avatar
Thomas Roessler committed
674 675
  if (addr->personal || (addr->mailbox && *addr->mailbox == '@'))
  {
Thomas Roessler's avatar
Thomas Roessler committed
676 677 678 679 680 681 682 683 684 685
    if (!buflen)
      goto done;
    *pbuf++ = '<';
    buflen--;
  }

  if (addr->mailbox)
  {
    if (!buflen)
      goto done;
686
    if (ascii_strcmp (addr->mailbox, "@") && !display)
687 688 689 690
    {
      strfcpy (pbuf, addr->mailbox, buflen);
      len = mutt_strlen (pbuf);
    }
691 692 693 694 695
    else if (ascii_strcmp (addr->mailbox, "@") && display)
    {
      strfcpy (pbuf, mutt_addr_for_display (addr), buflen);
      len = mutt_strlen (pbuf);
    }
696 697 698 699 700
    else
    {
      *pbuf = '\0';
      len = 0;
    }
Thomas Roessler's avatar
Thomas Roessler committed
701 702 703
    pbuf += len;
    buflen -= len;

Thomas Roessler's avatar
Thomas Roessler committed
704
    if (addr->personal || (addr->mailbox && *addr->mailbox == '@'))
Thomas Roessler's avatar
Thomas Roessler committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
    {
      if (!buflen)
	goto done;
      *pbuf++ = '>';
      buflen--;
    }

    if (addr->group)
    {
      if (!buflen)
	goto done;
      *pbuf++ = ':';
      buflen--;
      if (!buflen)
	goto done;
      *pbuf++ = ' ';
      buflen--;
    }
  }
  else
  {
    if (!buflen)
      goto done;
    *pbuf++ = ';';
    buflen--;
  }
done:
  /* no need to check for length here since we already save space at the
     beginning of this routine */
  *pbuf = 0;
}

/* note: it is assumed that `buf' is nul terminated! */
738
int rfc822_write_address (char *buf, size_t buflen, ADDRESS *addr, int display)
Thomas Roessler's avatar
Thomas Roessler committed
739 740
{
  char *pbuf = buf;
741
  size_t len = mutt_strlen (buf);
Thomas Roessler's avatar
Thomas Roessler committed
742 743 744 745 746 747
  
  buflen--; /* save room for the terminal nul */

  if (len > 0)
  {
    if (len > buflen)
748
      return pbuf - buf; /* safety check for bogus arguments */
Thomas Roessler's avatar
Thomas Roessler committed
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765

    pbuf += len;
    buflen -= len;
    if (!buflen)
      goto done;
    *pbuf++ = ',';
    buflen--;
    if (!buflen)
      goto done;
    *pbuf++ = ' ';
    buflen--;
  }

  for (; addr && buflen > 0; addr = addr->next)
  {
    /* use buflen+1 here because we already saved space for the trailing
       nul char, and the subroutine can make use of it */
766
    rfc822_write_address_single (pbuf, buflen + 1, addr, display);
Thomas Roessler's avatar
Thomas Roessler committed
767 768 769

    /* this should be safe since we always have at least 1 char passed into
       the above call, which means `pbuf' should always be nul terminated */
770
    len = mutt_strlen (pbuf);
Thomas Roessler's avatar
Thomas Roessler committed
771 772
    pbuf += len;
    buflen -= len;
Thomas Roessler's avatar
Thomas Roessler committed
773 774 775 776

    /* if there is another address, and its not a group mailbox name or
       group terminator, add a comma to separate the addresses */
    if (addr->next && addr->next->mailbox && !addr->group)
Thomas Roessler's avatar
Thomas Roessler committed
777 778 779 780 781 782 783 784 785 786 787 788 789
    {
      if (!buflen)
	goto done;
      *pbuf++ = ',';
      buflen--;
      if (!buflen)
	goto done;
      *pbuf++ = ' ';
      buflen--;
    }
  }
done:
  *pbuf = 0;
790
  return pbuf - buf;
Thomas Roessler's avatar
Thomas Roessler committed
791 792 793 794 795 796 797 798 799 800 801 802 803
}

/* this should be rfc822_cpy_adr */
ADDRESS *rfc822_cpy_adr_real (ADDRESS *addr)
{
  ADDRESS *p = rfc822_new_address ();

#ifdef EXACT_ADDRESS
  p->val = safe_strdup (addr->val);
#endif
  p->personal = safe_strdup (addr->personal);
  p->mailbox = safe_strdup (addr->mailbox);
  p->group = addr->group;
804 805
  p->is_intl = addr->is_intl;
  p->intl_checked = addr->intl_checked;
Thomas Roessler's avatar
Thomas Roessler committed
806 807 808 809
  return p;
}

/* this should be rfc822_cpy_adrlist */
810
ADDRESS *rfc822_cpy_adr (ADDRESS *addr, int prune)
Thomas Roessler's avatar
Thomas Roessler committed
811 812 813 814 815
{
  ADDRESS *top = NULL, *last = NULL;
  
  for (; addr; addr = addr->next)
  {
816 817
    if (prune && addr->group && (!addr->next || !addr->next->mailbox))
    {
818
      /* ignore this element of the list */
819
    }
820
    else if (last)
Thomas Roessler's avatar
Thomas Roessler committed
821 822 823 824 825 826 827 828 829 830 831
    {
      last->next = rfc822_cpy_adr_real (addr);
      last = last->next;
    }
    else
      top = last = rfc822_cpy_adr_real (addr);
  }
  return top;
}

/* append list 'b' to list 'a' and return the last element in the new list */
832
ADDRESS *rfc822_append (ADDRESS **a, ADDRESS *b, int prune)
Thomas Roessler's avatar
Thomas Roessler committed
833 834 835 836 837 838 839 840
{
  ADDRESS *tmp = *a;

  while (tmp && tmp->next)
    tmp = tmp->next;
  if (!b)
    return tmp;
  if (tmp)
841
    tmp->next = rfc822_cpy_adr (b, prune);
Thomas Roessler's avatar
Thomas Roessler committed
842
  else
843
    tmp = *a = rfc822_cpy_adr (b, prune);
Thomas Roessler's avatar
Thomas Roessler committed
844 845 846 847 848
  while (tmp && tmp->next)
    tmp = tmp->next;
  return tmp;
}

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
/* incomplete. Only used to thwart the APOP MD5 attack (#2846). */
int rfc822_valid_msgid (const char *msgid)
{
  /* msg-id         = "<" addr-spec ">"
   * addr-spec      = local-part "@" domain
   * local-part     = word *("." word)
   * word           = atom / quoted-string
   * atom           = 1*<any CHAR except specials, SPACE and CTLs>
   * CHAR           = ( 0.-127. )
   * specials       = "(" / ")" / "<" / ">" / "@"
                    / "," / ";" / ":" / "\" / <">
		    / "." / "[" / "]"
   * SPACE          = ( 32. )
   * CTLS           = ( 0.-31., 127.)
   * quoted-string  = <"> *(qtext/quoted-pair) <">
   * qtext          = <any CHAR except <">, "\" and CR>
   * CR             = ( 13. )
   * quoted-pair    = "\" CHAR
   * domain         = sub-domain *("." sub-domain)
   * sub-domain     = domain-ref / domain-literal
   * domain-ref     = atom
   * domain-literal = "[" *(dtext / quoted-pair) "]"
   */

  unsigned int l, i;

  if (!msgid || !*msgid)
    return -1;

  l = mutt_strlen (msgid);
  if (l < 5) /* <atom@atom> */
    return -1;
  if (msgid[0] != '<' || msgid[l-1] != '>')
    return -1;
883
  if (!(strrchr (msgid, '@')))
884 885 886 887 888 889 890 891 892 893
    return -1;

  /* TODO: complete parser */
  for (i = 0; i < l; i++)
    if ((unsigned char)msgid[i] > 127)
      return -1;

  return 0;
}

Thomas Roessler's avatar
Thomas Roessler committed
894
#ifdef TESTING
895
int safe_free (void **p)	/* __SAFE_FREE_CHECKED__ */
Thomas Roessler's avatar
Thomas Roessler committed
896
{
897
  free(*p);		/* __MEM_CHECKED__ */
Thomas Roessler's avatar
Thomas Roessler committed
898 899 900 901 902 903 904
  *p = 0;
}

int main (int argc, char **argv)
{
  ADDRESS *list;
  char buf[256];
Thomas Roessler's avatar
Thomas Roessler committed
905
# if 0
906
  char *str = "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)";
Thomas Roessler's avatar
Thomas Roessler committed
907 908 909
# else
  char *str = "a b c ";
# endif
Thomas Roessler's avatar
Thomas Roessler committed
910 911 912 913 914 915 916 917 918
  
  list = rfc822_parse_adrlist (NULL, str);
  buf[0] = 0;
  rfc822_write_address (buf, sizeof (buf), list);
  rfc822_free_address (&list);
  puts (buf);
  exit (0);
}
#endif