init.c 72.8 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2002 Michael R. Elkins <[email protected]>
Thomas Roessler's avatar
Thomas Roessler committed
3 4 5 6 7 8 9 10 11 12 13 14 15
 * 
 *     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
16
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Thomas Roessler's avatar
Thomas Roessler committed
17 18
 */ 

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

Thomas Roessler's avatar
Thomas Roessler committed
23
#include "mutt.h"
Thomas Roessler's avatar
Thomas Roessler committed
24
#include "mapping.h"
Thomas Roessler's avatar
Thomas Roessler committed
25 26
#include "mutt_curses.h"
#include "mutt_regex.h"
Thomas Roessler's avatar
Thomas Roessler committed
27
#include "history.h"
28
#include "keymap.h"
29
#include "mbyte.h"
30
#include "charset.h"
31
#include "mutt_crypt.h"
32
#include "mutt_idna.h"
33

34
#if defined(USE_SSL)
35
#include "mutt_ssl.h"
36 37 38
#endif


Thomas Roessler's avatar
Thomas Roessler committed
39 40 41 42 43 44 45 46 47 48 49 50 51

#include "mx.h"
#include "init.h"
#include "mailbox.h"

#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/utsname.h>
#include <errno.h>
#include <sys/wait.h>

52
#define CHECK_PAGER \
53
  if ((CurrentMenu == MENU_PAGER) && (idx >= 0) &&	\
54 55 56 57 58 59 60
	    (MuttVars[idx].flags & R_RESORT)) \
	{ \
	  snprintf (err->data, err->dsize, \
	    _("Not available in this menu.")); \
	  return (-1); \
	} else

61 62
typedef struct myvar
{
63 64
  char *name;
  char *value;
65 66 67 68 69
  struct myvar* next;
} myvar_t;

static myvar_t* MyVars;

70 71
static int var_to_string (int idx, char* val, size_t len);

72 73 74 75
static void myvar_set (const char* var, const char* val);
static const char* myvar_get (const char* var);
static void myvar_del (const char* var);

76 77 78 79 80 81 82
void toggle_quadoption (int opt)
{
  int n = opt/4;
  int b = (opt % 4) * 2;

  QuadOptions[n] ^= (1 << b);
}
Thomas Roessler's avatar
Thomas Roessler committed
83 84 85

void set_quadoption (int opt, int flag)
{
86 87 88 89 90
  int n = opt/4;
  int b = (opt % 4) * 2;

  QuadOptions[n] &= ~(0x3 << b);
  QuadOptions[n] |= (flag & 0x3) << b;
Thomas Roessler's avatar
Thomas Roessler committed
91 92 93 94
}

int quadoption (int opt)
{
95 96 97 98
  int n = opt/4;
  int b = (opt % 4) * 2;

  return (QuadOptions[n] >> b) & 0x3;
Thomas Roessler's avatar
Thomas Roessler committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
}

int query_quadoption (int opt, const char *prompt)
{
  int v = quadoption (opt);

  switch (v)
  {
    case M_YES:
    case M_NO:
      return (v);

    default:
      v = mutt_yesorno (prompt, (v == M_ASKYES));
      CLEARLINE (LINES - 1);
      return (v);
  }

  /* not reached */
}

/* given the variable ``s'', return the index into the rc_vars array which
   matches, or -1 if the variable is not found.  */
int mutt_option_index (char *s)
{
  int i;

  for (i = 0; MuttVars[i].option; i++)
127
    if (mutt_strcmp (s, MuttVars[i].option) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      return (MuttVars[i].type == DT_SYN ?  mutt_option_index ((char *) MuttVars[i].data) : i);
  return (-1);
}

int mutt_extract_token (BUFFER *dest, BUFFER *tok, int flags)
{
  char		ch;
  char		qc = 0; /* quote char */
  char		*pc;

  /* reset the destination pointer to the beginning of the buffer */
  dest->dptr = dest->data;

  SKIPWS (tok->dptr);
  while ((ch = *tok->dptr))
  {
    if (!qc)
    {
      if ((ISSPACE (ch) && !(flags & M_TOKEN_SPACE)) ||
	  (ch == '#' && !(flags & M_TOKEN_COMMENT)) ||
	  (ch == '=' && (flags & M_TOKEN_EQUAL)) ||
	  (ch == ';' && !(flags & M_TOKEN_SEMICOLON)) ||
150
	  ((flags & M_TOKEN_PATTERN) && strchr ("~%=!|", ch)))
Thomas Roessler's avatar
Thomas Roessler committed
151 152 153 154 155 156 157 158 159 160 161
	break;
    }

    tok->dptr++;

    if (ch == qc)
      qc = 0; /* end of quote */
    else if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE))
      qc = ch;
    else if (ch == '\\' && qc != '\'')
    {
162 163
	if (!*tok->dptr)
	    return -1; /* premature end of token */
Thomas Roessler's avatar
Thomas Roessler committed
164 165 166 167
      switch (ch = *tok->dptr++)
      {
	case 'c':
	case 'C':
168 169
	    if (!*tok->dptr)
		return -1; /* premature end of token */
170 171
	  mutt_buffer_addch (dest, (toupper ((unsigned char) *tok->dptr)
                                    - '@') & 0x7f);
Thomas Roessler's avatar
Thomas Roessler committed
172 173 174
	  tok->dptr++;
	  break;
	case 'r':
175
	  mutt_buffer_addch (dest, '\r');
Thomas Roessler's avatar
Thomas Roessler committed
176 177
	  break;
	case 'n':
178
	  mutt_buffer_addch (dest, '\n');
Thomas Roessler's avatar
Thomas Roessler committed
179 180
	  break;
	case 't':
181
	  mutt_buffer_addch (dest, '\t');
Thomas Roessler's avatar
Thomas Roessler committed
182 183
	  break;
	case 'f':
184
	  mutt_buffer_addch (dest, '\f');
Thomas Roessler's avatar
Thomas Roessler committed
185 186
	  break;
	case 'e':
187
	  mutt_buffer_addch (dest, '\033');
Thomas Roessler's avatar
Thomas Roessler committed
188 189
	  break;
	default:
Thomas Roessler's avatar
Thomas Roessler committed
190 191 192
	  if (isdigit ((unsigned char) ch) &&
	      isdigit ((unsigned char) *tok->dptr) &&
	      isdigit ((unsigned char) *(tok->dptr + 1)))
Thomas Roessler's avatar
Thomas Roessler committed
193 194
	  {

195
	    mutt_buffer_addch (dest, (ch << 6) + (*tok->dptr << 3) + *(tok->dptr + 1) - 3504);
Thomas Roessler's avatar
Thomas Roessler committed
196 197 198
	    tok->dptr += 2;
	  }
	  else
199
	    mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
200 201 202 203
      }
    }
    else if (ch == '^' && (flags & M_TOKEN_CONDENSE))
    {
204 205
	if (!*tok->dptr)
	    return -1; /* premature end of token */
Thomas Roessler's avatar
Thomas Roessler committed
206 207
      ch = *tok->dptr++;
      if (ch == '^')
208
	mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
209
      else if (ch == '[')
210
	mutt_buffer_addch (dest, '\033');
Thomas Roessler's avatar
Thomas Roessler committed
211
      else if (isalpha ((unsigned char) ch))
212
	mutt_buffer_addch (dest, toupper ((unsigned char) ch) - '@');
Thomas Roessler's avatar
Thomas Roessler committed
213 214
      else
      {
215 216
	mutt_buffer_addch (dest, '^');
	mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
      }
    }
    else if (ch == '`' && (!qc || qc == '"'))
    {
      FILE	*fp;
      pid_t	pid;
      char	*cmd, *ptr;
      size_t	expnlen;
      BUFFER	expn;
      int	line = 0;

      pc = tok->dptr;
      do {
	if ((pc = strpbrk (pc, "\\`")))
	{
	  /* skip any quoted chars */
	  if (*pc == '\\')
	    pc += 2;
	}
      } while (pc && *pc != '`');
      if (!pc)
      {
	dprint (1, (debugfile, "mutt_get_token: mismatched backtics\n"));
	return (-1);
      }
      cmd = mutt_substrdup (tok->dptr, pc);
      if ((pid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0)
      {
	dprint (1, (debugfile, "mutt_get_token: unable to fork command: %s", cmd));
246
	FREE (&cmd);
Thomas Roessler's avatar
Thomas Roessler committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260
	return (-1);
      }
      FREE (&cmd);

      tok->dptr = pc + 1;

      /* read line */
      memset (&expn, 0, sizeof (expn));
      expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line);
      fclose (fp);
      mutt_wait_filter (pid);

      /* if we got output, make a new string consiting of the shell ouptput
	 plus whatever else was left on the original line */
261 262 263 264 265 266 267
      /* BUT: If this is inside a quoted string, directly add output to 
       * the token */
      if (expn.data && qc)
      {
	mutt_buffer_addstr (dest, expn.data);
	FREE (&expn.data);
      }
Thomas Roessler's avatar
Thomas Roessler committed
268
      else if (expn.data)
Thomas Roessler's avatar
Thomas Roessler committed
269
      {
270 271
	expnlen = mutt_strlen (expn.data);
	tok->dsize = expnlen + mutt_strlen (tok->dptr) + 1;
Thomas Roessler's avatar
Thomas Roessler committed
272 273
	ptr = safe_malloc (tok->dsize);
	memcpy (ptr, expn.data, expnlen);
274
	strcpy (ptr + expnlen, tok->dptr);	/* __STRCPY_CHECKED__ */
Thomas Roessler's avatar
Thomas Roessler committed
275 276 277 278 279 280 281 282 283
	if (tok->destroy)
	  FREE (&tok->data);
	tok->data = ptr;
	tok->dptr = ptr;
	tok->destroy = 1; /* mark that the caller should destroy this data */
	ptr = NULL;
	FREE (&expn.data);
      }
    }
Thomas Roessler's avatar
Thomas Roessler committed
284
    else if (ch == '$' && (!qc || qc == '"') && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr)))
Thomas Roessler's avatar
Thomas Roessler committed
285
    {
286 287 288
      const char *env = NULL;
      char *var = NULL;
      int idx;
Thomas Roessler's avatar
Thomas Roessler committed
289 290 291 292 293 294 295 296 297 298 299 300

      if (*tok->dptr == '{')
      {
	tok->dptr++;
	if ((pc = strchr (tok->dptr, '}')))
	{
	  var = mutt_substrdup (tok->dptr, pc);
	  tok->dptr = pc + 1;
	}
      }
      else
      {
301
	for (pc = tok->dptr; isalnum ((unsigned char) *pc) || *pc == '_'; pc++)
Thomas Roessler's avatar
Thomas Roessler committed
302 303 304 305
	  ;
	var = mutt_substrdup (tok->dptr, pc);
	tok->dptr = pc;
      }
306 307 308 309 310 311 312 313 314 315 316 317 318 319
      if (var)
      {
        if ((env = getenv (var)) || (env = myvar_get (var)))
          mutt_buffer_addstr (dest, env);
        else if ((idx = mutt_option_index (var)) != -1)
        {
          /* expand settable mutt variables */
          char val[LONG_STRING];

          if (var_to_string (idx, val, sizeof (val)))
            mutt_buffer_addstr (dest, val);
        }
        FREE (&var);
      }
Thomas Roessler's avatar
Thomas Roessler committed
320 321
    }
    else
322
      mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
323
  }
324
  mutt_buffer_addch (dest, 0); /* terminate the string */
Thomas Roessler's avatar
Thomas Roessler committed
325 326 327 328
  SKIPWS (tok->dptr);
  return 0;
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
static void mutt_free_opt (struct option_t* p)
{
  REGEXP* pp;

  switch (p->type & DT_MASK)
  {
  case DT_ADDR:
    rfc822_free_address ((ADDRESS**)p->data);
    break;
  case DT_RX:
    pp = (REGEXP*)p->data;
    FREE (&pp->pattern);
    if (pp->rx)
    {
      regfree (pp->rx);
      FREE (&pp->rx);
    }
    break;
  case DT_PATH:
  case DT_STR:
349
    FREE ((char**)p->data);		/* __FREE_CHECKED__ */
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    break;
  }
}

/* clean up before quitting */
void mutt_free_opts (void)
{
  int i;

  for (i = 0; MuttVars[i].option; i++)
    mutt_free_opt (MuttVars + i);

  mutt_free_rx_list (&Alternates);
  mutt_free_rx_list (&UnAlternates);
  mutt_free_rx_list (&MailLists);
  mutt_free_rx_list (&UnMailLists);
  mutt_free_rx_list (&SubscribedLists);
  mutt_free_rx_list (&UnSubscribedLists);
  mutt_free_rx_list (&NoSpamList);
}

371
static void add_to_list (LIST **list, const char *str)
Thomas Roessler's avatar
Thomas Roessler committed
372 373 374
{
  LIST *t, *last = NULL;

375 376 377 378
  /* don't add a NULL or empty string to the list */
  if (!str || *str == '\0')
    return;

379 380
  /* check to make sure the item is not already on this list */
  for (last = *list; last; last = last->next)
Thomas Roessler's avatar
Thomas Roessler committed
381
  {
382
    if (ascii_strcasecmp (str, last->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
383
    {
384 385 386
      /* already on the list, so just ignore it */
      last = NULL;
      break;
Thomas Roessler's avatar
Thomas Roessler committed
387
    }
388 389 390
    if (!last->next)
      break;
  }
Thomas Roessler's avatar
Thomas Roessler committed
391

392 393 394 395 396
  if (!*list || last)
  {
    t = (LIST *) safe_calloc (1, sizeof (LIST));
    t->data = safe_strdup (str);
    if (last)
Thomas Roessler's avatar
Thomas Roessler committed
397
    {
398 399
      last->next = t;
      last = last->next;
Thomas Roessler's avatar
Thomas Roessler committed
400
    }
401 402
    else
      *list = last = t;
Thomas Roessler's avatar
Thomas Roessler committed
403 404 405
  }
}

406
int mutt_add_to_rx_list (RX_LIST **list, const char *s, int flags, BUFFER *err)
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
{
  RX_LIST *t, *last = NULL;
  REGEXP *rx;

  if (!s || !*s)
    return 0;

  if (!(rx = mutt_compile_regexp (s, flags)))
  {
    snprintf (err->data, err->dsize, "Bad regexp: %s\n", s);
    return -1;
  }

  /* check to make sure the item is not already on this list */
  for (last = *list; last; last = last->next)
  {
    if (ascii_strcasecmp (rx->pattern, last->rx->pattern) == 0)
    {
      /* already on the list, so just ignore it */
      last = NULL;
      break;
    }
    if (!last->next)
      break;
  }

  if (!*list || last)
  {
    t = mutt_new_rx_list();
    t->rx = rx;
    if (last)
    {
      last->next = t;
      last = last->next;
    }
    else
      *list = last = t;
  }
  else /* duplicate */
    mutt_free_regexp (&rx);

  return 0;
}

451
static int remove_from_spam_list (SPAM_LIST **list, const char *pat);
452

453 454
static int add_to_spam_list (SPAM_LIST **list, const char *pat, const char *templ, BUFFER *err)
{
455
  SPAM_LIST *t = NULL, *last = NULL;
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  REGEXP *rx;
  int n;
  const char *p;

  if (!pat || !*pat || !templ)
    return 0;

  if (!(rx = mutt_compile_regexp (pat, REG_ICASE)))
  {
    snprintf (err->data, err->dsize, _("Bad regexp: %s"), pat);
    return -1;
  }

  /* check to make sure the item is not already on this list */
  for (last = *list; last; last = last->next)
  {
    if (ascii_strcasecmp (rx->pattern, last->rx->pattern) == 0)
    {
474 475 476 477 478 479 480
      /* Already on the list. Formerly we just skipped this case, but
       * now we're supporting removals, which means we're supporting
       * re-adds conceptually. So we probably want this to imply a
       * removal, then do an add. We can achieve the removal by freeing
       * the template, and leaving t pointed at the current item.
       */
      t = last;
481
      FREE(&t->template);
482 483 484 485 486 487
      break;
    }
    if (!last->next)
      break;
  }

488 489 490 491
  /* If t is set, it's pointing into an extant SPAM_LIST* that we want to
   * update. Otherwise we want to make a new one to link at the list's end.
   */
  if (!t)
492 493 494
  {
    t = mutt_new_spam_list();
    t->rx = rx;
495 496 497 498 499
    if (last)
      last->next = t;
    else
      *list = t;
  }
500

501 502 503 504 505 506 507 508
  /* Now t is the SPAM_LIST* that we want to modify. It is prepared. */
  t->template = safe_strdup(templ);

  /* Find highest match number in template string */
  t->nmatch = 0;
  for (p = templ; *p;)
  {
    if (*p == '%')
509
    {
510 511 512 513 514
        n = atoi(++p);
        if (n > t->nmatch)
          t->nmatch = n;
        while (*p && isdigit((int)*p))
          ++p;
515
    }
516 517 518
    else
        ++p;
  }
519 520 521 522 523 524 525 526 527 528

  if (t->nmatch > t->rx->rx->re_nsub)
  {
    snprintf (err->data, err->dsize, _("Not enough subexpressions for spam "
                                       "template"));
    remove_from_spam_list(list, pat);
    return -1;
  }

  t->nmatch++;         /* match 0 is always the whole expr */
529

530 531 532 533 534 535 536 537 538 539
  return 0;
}

static int remove_from_spam_list (SPAM_LIST **list, const char *pat)
{
  SPAM_LIST *spam, *prev;
  int nremoved = 0;

  /* Being first is a special case. */
  spam = *list;
540 541
  if (!spam)
    return 0;
542 543 544 545
  if (spam->rx && !mutt_strcmp(spam->rx->pattern, pat))
  {
    *list = spam->next;
    mutt_free_regexp(&spam->rx);
546 547
    FREE(&spam->template);
    FREE(&spam);
548 549 550 551 552 553 554
    return 1;
  }

  prev = spam;
  for (spam = prev->next; spam;)
  {
    if (!mutt_strcmp(spam->rx->pattern, pat))
555
    {
556 557
      prev->next = spam->next;
      mutt_free_regexp(&spam->rx);
558 559
      FREE(&spam->template);
      FREE(&spam);
560 561
      spam = prev->next;
      ++nremoved;
562 563
    }
    else
564
      spam = spam->next;
565 566
  }

567
  return nremoved;
568 569 570
}


571
static void remove_from_list (LIST **l, const char *str)
Thomas Roessler's avatar
Thomas Roessler committed
572 573 574
{
  LIST *p, *last = NULL;

575 576 577
  if (mutt_strcmp ("*", str) == 0)
    mutt_free_list (l);    /* ``unCMD *'' means delete all current entries */
  else
Thomas Roessler's avatar
Thomas Roessler committed
578
  {
579 580 581
    p = *l;
    last = NULL;
    while (p)
Thomas Roessler's avatar
Thomas Roessler committed
582
    {
583
      if (ascii_strcasecmp (str, p->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
584
      {
585
	FREE (&p->data);
586 587
	if (last)
	  last->next = p->next;
Thomas Roessler's avatar
Thomas Roessler committed
588
	else
589
	  (*l) = p->next;
590
	FREE (&p);
591 592 593 594 595
      }
      else
      {
	last = p;
	p = p->next;
Thomas Roessler's avatar
Thomas Roessler committed
596 597 598 599 600
      }
    }
  }
}

601
static int remove_from_rx_list (RX_LIST **l, const char *str)
602 603
{
  RX_LIST *p, *last = NULL;
604
  int rv = -1;
605 606

  if (mutt_strcmp ("*", str) == 0)
607
  {
608
    mutt_free_rx_list (l);    /* ``unCMD *'' means delete all current entries */
609 610
    rv = 0;
  }
611 612 613 614 615 616 617 618 619 620 621 622 623 624
  else
  {
    p = *l;
    last = NULL;
    while (p)
    {
      if (ascii_strcasecmp (str, p->rx->pattern) == 0)
      {
	mutt_free_regexp (&p->rx);
	if (last)
	  last->next = p->next;
	else
	  (*l) = p->next;
	FREE (&p);
625
	rv = 0;
626 627 628 629 630 631 632 633
      }
      else
      {
	last = p;
	p = p->next;
      }
    }
  }
634
  return (rv);
635 636
}

Thomas Roessler's avatar
Thomas Roessler committed
637 638
static int parse_unignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
639 640 641
  do
  {
    mutt_extract_token (buf, s, 0);
642 643 644 645 646

    /* don't add "*" to the unignore list */
    if (strcmp (buf->data, "*")) 
      add_to_list (&UnIgnore, buf->data);

647 648 649 650
    remove_from_list (&Ignore, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
651 652 653 654 655
  return 0;
}

static int parse_ignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
656 657 658 659 660 661 662 663
  do
  {
    mutt_extract_token (buf, s, 0);
    remove_from_list (&UnIgnore, buf->data);
    add_to_list (&Ignore, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
664 665 666 667 668
  return 0;
}

static int parse_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
669 670 671 672 673 674 675
  do
  {
    mutt_extract_token (buf, s, 0);
    add_to_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
676 677 678
  return 0;
}

679 680 681 682 683 684 685 686 687 688 689 690
static void _alternates_clean (void)
{
  int i;
  if (Context && Context->msgcount) 
  {
    for (i = 0; i < Context->msgcount; i++)
      Context->hdrs[i]->recip_valid = 0;
  }
}

static int parse_alternates (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
691 692
  group_context_t *gc = NULL;
  
693
  _alternates_clean();
694

695 696 697
  do
  {
    mutt_extract_token (buf, s, 0);
698 699 700 701

    if (parse_group_context (&gc, buf, s, data, err) == -1)
      goto bail;

702 703
    remove_from_rx_list (&UnAlternates, buf->data);

704 705 706 707 708
    if (mutt_add_to_rx_list (&Alternates, buf->data, REG_ICASE, err) != 0)
      goto bail;

    if (mutt_group_context_add_rx (gc, buf->data, REG_ICASE, err) != 0)
      goto bail;
709 710
  }
  while (MoreArgs (s));
711 712
  
  mutt_group_context_destroy (&gc);
713
  return 0;
714 715 716 717
  
 bail:
  mutt_group_context_destroy (&gc);
  return -1;
718 719 720 721 722
}

static int parse_unalternates (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  _alternates_clean();
723 724 725 726 727 728
  do
  {
    mutt_extract_token (buf, s, 0);
    remove_from_rx_list (&Alternates, buf->data);

    if (mutt_strcmp (buf->data, "*") &&
729
	mutt_add_to_rx_list (&UnAlternates, buf->data, REG_ICASE, err) != 0)
730 731 732 733 734 735
      return -1;

  }
  while (MoreArgs (s));

  return 0;
736 737
}

738 739 740 741 742 743
static int parse_spam_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  BUFFER templ;

  memset(&templ, 0, sizeof(templ));

744
  /* Insist on at least one parameter */
745 746
  if (!MoreArgs(s))
  {
747 748 749 750
    if (data == M_SPAM)
      strfcpy(err->data, _("spam: no matching pattern"), err->dsize);
    else
      strfcpy(err->data, _("nospam: no matching pattern"), err->dsize);
751 752
    return -1;
  }
753 754

  /* Extract the first token, a regexp */
755 756
  mutt_extract_token (buf, s, 0);

757 758
  /* data should be either M_SPAM or M_NOSPAM. M_SPAM is for spam commands. */
  if (data == M_SPAM)
759
  {
760 761 762 763 764 765
    /* If there's a second parameter, it's a template for the spam tag. */
    if (MoreArgs(s))
    {
      mutt_extract_token (&templ, s, 0);

      /* Add to the spam list. */
766 767
      if (add_to_spam_list (&SpamList, buf->data, templ.data, err) != 0) {
	  FREE(&templ.data);
768
          return -1;
769 770
      }
      FREE(&templ.data);
771 772 773 774 775 776 777 778 779
    }

    /* If not, try to remove from the nospam list. */
    else
    {
      remove_from_rx_list(&NoSpamList, buf->data);
    }

    return 0;
780
  }
781 782 783

  /* M_NOSPAM is for nospam commands. */
  else if (data == M_NOSPAM)
784
  {
785
    /* nospam only ever has one parameter. */
786

787 788 789 790 791 792 793 794 795 796 797 798 799
    /* "*" is a special case. */
    if (!mutt_strcmp(buf->data, "*"))
    {
      mutt_free_spam_list (&SpamList);
      mutt_free_rx_list (&NoSpamList);
      return 0;
    }

    /* If it's on the spam list, just remove it. */
    if (remove_from_spam_list(&SpamList, buf->data) != 0)
      return 0;

    /* Otherwise, add it to the nospam list. */
800
    if (mutt_add_to_rx_list (&NoSpamList, buf->data, REG_ICASE, err) != 0)
801
      return -1;
802 803 804 805 806 807 808

    return 0;
  }

  /* This should not happen. */
  strfcpy(err->data, "This is no good at all.", err->dsize);
  return -1;
809 810
}

811

Thomas Roessler's avatar
Thomas Roessler committed
812 813
static int parse_unlist (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
814 815 816
  do
  {
    mutt_extract_token (buf, s, 0);
817 818 819 820 821 822 823 824
    /*
     * Check for deletion of entire list
     */
    if (mutt_strcmp (buf->data, "*") == 0)
    {
      mutt_free_list ((LIST **) data);
      break;
    }
825 826 827 828
    remove_from_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
829 830 831
  return 0;
}

832 833
static int parse_lists (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
834 835
  group_context_t *gc = NULL;

836 837 838
  do
  {
    mutt_extract_token (buf, s, 0);
839 840 841 842
    
    if (parse_group_context (&gc, buf, s, data, err) == -1)
      goto bail;
    
843 844
    remove_from_rx_list (&UnMailLists, buf->data);
    
845 846 847 848 849
    if (mutt_add_to_rx_list (&MailLists, buf->data, REG_ICASE, err) != 0)
      goto bail;
    
    if (mutt_group_context_add_rx (gc, buf->data, REG_ICASE, err) != 0)
      goto bail;
850 851 852
  }
  while (MoreArgs (s));

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 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
  mutt_group_context_destroy (&gc);
  return 0;
  
 bail:
  mutt_group_context_destroy (&gc);
  return -1;
}

typedef enum group_state_t {
  NONE, RX, ADDR
} group_state_t;

static int parse_group (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  group_context_t *gc = NULL;
  group_state_t state = NONE;
  ADDRESS *addr = NULL;
  char *estr = NULL;
  
  do 
  {
    mutt_extract_token (buf, s, 0);
    if (parse_group_context (&gc, buf, s, data, err) == -1)
      goto bail;
    
    if (!mutt_strcasecmp (buf->data, "-rx"))
      state = RX;
    else if (!mutt_strcasecmp (buf->data, "-addr"))
      state = ADDR;
    else 
    {
      switch (state) 
      {
	case NONE:
	  strfcpy (err->data, _("Missing -rx or -addr."), err->dsize);
	  goto bail;
	
	case RX:
	  if (mutt_group_context_add_rx (gc, buf->data, REG_ICASE, err) != 0)
	    goto bail;
	  break;
	
	case ADDR:
	  if ((addr = mutt_parse_adrlist (NULL, buf->data)) == NULL)
	    goto bail;
	  if (mutt_addrlist_to_idna (addr, &estr)) 
	  {
	    snprintf (err->data, err->dsize, _("Warning: Bad IDN '%s'.\n"),
		      estr);
	    goto bail;
	  }
	  mutt_group_context_add_adrlist (gc, addr);
	  rfc822_free_address (&addr);
	  break;
      }
    }
  } while (MoreArgs (s));

  mutt_group_context_destroy (&gc);
912
  return 0;
913 914 915 916 917 918 919 920 921 922

  bail:
  mutt_group_context_destroy (&gc);
  return -1;
}

static int parse_ungroup (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  strfcpy (err->data, "not implemented", err->dsize);
  return -1;
923
}
924

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
/* always wise to do what someone else did before */
static void _attachments_clean (void)
{
  int i;
  if (Context && Context->msgcount) 
  {
    for (i = 0; i < Context->msgcount; i++)
      Context->hdrs[i]->attach_valid = 0;
  }
}

static int parse_attach_list (BUFFER *buf, BUFFER *s, LIST **ldata, BUFFER *err)
{
  ATTACH_MATCH *a;
  LIST *listp, *lastp;
  char *p;
  char *tmpminor;
  int len;

  /* Find the last item in the list that data points to. */
  lastp = NULL;
  dprint(5, (debugfile, "parse_attach_list: ldata = %08x, *ldata = %08x\n",
	      (unsigned int)ldata, (unsigned int)*ldata));
  for (listp = *ldata; listp; listp = listp->next)
  {
    a = (ATTACH_MATCH *)listp->data;
    dprint(5, (debugfile, "parse_attach_list: skipping %s/%s\n",
		a->major, a->minor));
    lastp = listp;
  }

  do
  {
    mutt_extract_token (buf, s, 0);

    if (!buf->data || *buf->data == '\0')
      continue;
   
    a = safe_malloc(sizeof(ATTACH_MATCH));

    /* some cheap hacks that I expect to remove */
966
    if (!ascii_strcasecmp(buf->data, "any"))
967
      a->major = safe_strdup("*/.*");
968
    else if (!ascii_strcasecmp(buf->data, "none"))
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
      a->major = safe_strdup("cheap_hack/this_should_never_match");
    else
      a->major = safe_strdup(buf->data);

    if ((p = strchr(a->major, '/')))
    {
      *p = '\0';
      ++p;
      a->minor = p;
    }
    else
    {
      a->minor = "unknown";
    }

    len = strlen(a->minor);
    tmpminor = safe_malloc(len+3);
    strcpy(&tmpminor[1], a->minor); /* __STRCPY_CHECKED__ */
    tmpminor[0] = '^';
    tmpminor[len+1] = '$';
    tmpminor[len+2] = '\0';

    a->major_int = mutt_check_mime_type(a->major);
    regcomp(&a->minor_rx, tmpminor, REG_ICASE|REG_EXTENDED);

994
    FREE(&tmpminor);
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

    dprint(5, (debugfile, "parse_attach_list: added %s/%s [%d]\n",
		a->major, a->minor, a->major_int));

    listp = safe_malloc(sizeof(LIST));
    listp->data = (char *)a;
    listp->next = NULL;
    if (lastp)
    {
      lastp->next = listp;
    }
    else
    {
      *ldata = listp;
    }
    lastp = listp;
  }
  while (MoreArgs (s));
   
  _attachments_clean();
  return 0;
}

static int parse_unattach_list (BUFFER *buf, BUFFER *s, LIST **ldata, BUFFER *err)
{
  ATTACH_MATCH *a;
1021
  LIST *lp, *lastp, *newlp;
1022 1023 1024 1025 1026 1027 1028 1029
  char *tmp;
  int major;
  char *minor;

  do
  {
    mutt_extract_token (buf, s, 0);

1030
    if (!ascii_strcasecmp(buf->data, "any"))
1031
      tmp = safe_strdup("*/.*");
1032
    else if (!ascii_strcasecmp(buf->data, "none"))
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
      tmp = safe_strdup("cheap_hack/this_should_never_match");
    else
      tmp = safe_strdup(buf->data);

    if ((minor = strchr(tmp, '/')))
    {
      *minor = '\0';
      ++minor;
    }
    else
    {
      minor = "unknown";
    }
    major = mutt_check_mime_type(tmp);

1048 1049
    /* We must do our own walk here because remove_from_list() will only
     * remove the LIST->data, not anything pointed to by the LIST->data. */
1050
    lastp = NULL;
1051
    for(lp = *ldata; lp; )
1052 1053 1054 1055 1056 1057 1058 1059 1060
    {
      a = (ATTACH_MATCH *)lp->data;
      dprint(5, (debugfile, "parse_unattach_list: check %s/%s [%d] : %s/%s [%d]\n",
		  a->major, a->minor, a->major_int, tmp, minor, major));
      if (a->major_int == major && !mutt_strcasecmp(minor, a->minor))
      {
	dprint(5, (debugfile, "parse_unattach_list: removed %s/%s [%d]\n",
		    a->major, a->minor, a->major_int));
	regfree(&a->minor_rx);
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	FREE(&a->major);

	/* Relink backward */
	if (lastp)
	  lastp->next = lp->next;
	else
	  *ldata = lp->next;

        newlp = lp->next;
        FREE(&lp->data);	/* same as a */
        FREE(&lp);
        lp = newlp;
        continue;
1074 1075 1076 1077 1078 1079 1080 1081 1082
      }

      lastp = lp;
      lp = lp->next;
    }

  }
  while (MoreArgs (s));
   
1083
  FREE(&tmp);
1084 1085 1086 1087
  _attachments_clean();
  return 0;
}

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
static int print_attach_list (LIST *lp, char op, char *name)
{
  while (lp) {
    printf("attachments %c%s %s/%s\n", op, name,
           ((ATTACH_MATCH *)lp->data)->major,
           ((ATTACH_MATCH *)lp->data)->minor);
    lp = lp->next;
  }

  return 0;
}

1100 1101 1102

static int parse_attachments (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
1103
  char op, *category;
1104 1105 1106 1107 1108 1109 1110 1111
  LIST **listp;

  mutt_extract_token(buf, s, 0);
  if (!buf->data || *buf->data == '\0') {
    strfcpy(err->data, _("attachments: no disposition"), err->dsize);
    return -1;
  }

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
  category = buf->data;
  op = *category++;

  if (op == '?') {
    mutt_endwin (NULL);
    fflush (stdout);
    printf("\nCurrent attachments settings:\n\n");
    print_attach_list(AttachAllow,   '+', "A");
    print_attach_list(AttachExclude, '-', "A");
    print_attach_list(InlineAllow,   '+', "I");
    print_attach_list(InlineExclude, '-', "I");
    set_option (OPTFORCEREDRAWINDEX);
    set_option (OPTFORCEREDRAWPAGER);
    mutt_any_key_to_continue (NULL);
    return 0;
  }

1129 1130
  if (op != '+' && op != '-') {
    op = '+';
1131
    category--;
1132
  }
1133
  if (!ascii_strncasecmp(category, "attachment", strlen(category))) {
1134 1135 1136 1137 1138
    if (op == '+')
      listp = &AttachAllow;
    else
      listp = &AttachExclude;
  }
1139
  else if (!ascii_strncasecmp(category, "inline", strlen(category))) {
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
    if (op == '+')
      listp = &InlineAllow;
    else
      listp = &InlineExclude;
  }
  else {
    strfcpy(err->data, _("attachments: invalid disposition"), err->dsize);
    return -1;
  }

  return parse_attach_list(buf, s, listp, err);
}

static int parse_unattachments (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  char op, *p;
  LIST **listp;

  mutt_extract_token(buf, s, 0);
  if (!buf->data || *buf->data == '\0') {
    strfcpy(err->data, _("unattachments: no disposition"), err->dsize);
    return -1;
  }

  p = buf->data;
  op = *p++;
  if (op != '+' && op != '-') {
    op = '+';
    p--;
  }
1170
  if (!ascii_strncasecmp(p, "attachment", strlen(p))) {
1171 1172 1173 1174 1175
    if (op == '+')
      listp = &AttachAllow;
    else
      listp = &AttachExclude;
  }
1176
  else if (!ascii_strncasecmp(p, "inline", strlen(p))) {
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    if (op == '+')
      listp = &InlineAllow;
    else
      listp = &InlineExclude;
  }
  else {
    strfcpy(err->data, _("unattachments: invalid disposition"), err->dsize);
    return -1;
  }

  return parse_unattach_list(buf, s, listp, err);
}

1190 1191 1192 1193 1194
static int parse_unlists (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  do
  {
    mutt_extract_token (buf, s, 0);
1195
    remove_from_rx_list (&SubscribedLists, buf->data);
1196 1197 1198
    remove_from_rx_list (&MailLists, buf->data);
    
    if (mutt_strcmp (buf->data, "*") && 
1199
	mutt_add_to_rx_list (&UnMailLists, buf->data, REG_ICASE, err) != 0)
1200
      return -1;
1201 1202 1203 1204 1205 1206 1207 1208
  }
  while (MoreArgs (s));

  return 0;
}

static int parse_subscribe (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
1209 1210
  group_context_t *gc = NULL;
  
1211 1212 1213
  do
  {
    mutt_extract_token (buf, s, 0);
1214 1215 1216 1217

    if (parse_group_context (&gc, buf, s, data, err) == -1)
      goto bail;
    
1218 1219 1220
    remove_from_rx_list (&UnMailLists, buf->data);
    remove_from_rx_list (&UnSubscribedLists, buf->data);

1221 1222 1223 1224 1225 1226
    if (mutt_add_to_rx_list (&MailLists, buf->data, REG_ICASE, err) != 0)
      goto bail;
    if (mutt_add_to_rx_list (&SubscribedLists, buf->data, REG_ICASE, err) != 0)
      goto bail;
    if (mutt_group_context_add_rx (gc, buf->data, REG_ICASE, err) != 0)
      goto bail;
1227 1228
  }
  while (MoreArgs (s));
1229 1230
  
  mutt_group_context_destroy (&gc);
1231
  return 0;
1232 1233 1234 1235
  
 bail:
  mutt_group_context_destroy (&gc);
  return -1;
1236
}
1237 1238 1239 1240 1241 1242 1243 1244 1245

static int parse_unsubscribe (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  do
  {
    mutt_extract_token (buf, s, 0);
    remove_from_rx_list (&SubscribedLists, buf->data);
    
    if (mutt_strcmp (buf->data, "*") &&
1246
	mutt_add_to_rx_list (&UnSubscribedLists, buf->data, REG_ICASE, err) != 0)
1247 1248 1249 1250 1251 1252
      return -1;
  }
  while (MoreArgs (s));

  return 0;
}
1253
  
Thomas Roessler's avatar
Thomas Roessler committed
1254 1255 1256 1257 1258 1259 1260 1261
static int parse_unalias (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  ALIAS *tmp, *last = NULL;

  do
  {
    mutt_extract_token (buf, s, 0);

1262
    if (mutt_strcmp ("*", buf->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1263
    {
1264
      if (CurrentMenu == MENU_ALIAS)
Thomas Roessler's avatar
Thomas Roessler committed
1265
      {
1266
	for (tmp = Aliases; tmp ; tmp = tmp->next)
1267
	  tmp->del = 1;
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
	set_option (OPTFORCEREDRAWINDEX);
      }
      else
	mutt_free_alias (&Aliases);
      break;
    }
    else
      for (tmp = Aliases; tmp; tmp = tmp->next)
      {
	if (mutt_strcasecmp (buf->data, tmp->name) == 0)
	{
	  if (CurrentMenu == MENU_ALIAS)
	  {
	    tmp->del = 1;
	    set_option (OPTFORCEREDRAWINDEX);
	    break;
	  }

	  if (last)
	    last->next = tmp->next;
	  else
	    Aliases = tmp->next;
	  tmp->next = NULL;
	  mutt_free_alias (&tmp);
1292 1293
	  break;
	}
1294
	last = tmp;
Thomas Roessler's avatar
Thomas Roessler committed
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
      }
  }
  while (MoreArgs (s));
  return 0;
}

static int parse_alias (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  ALIAS *tmp = Aliases;
  ALIAS *last = NULL;
1305
  char *estr = NULL;
1306
  group_context_t *gc = NULL;
1307
  
1308
  if (!MoreArgs (s))
Thomas Roessler's avatar
Thomas Roessler committed
1309
  {
1310
    strfcpy (err->data, _("alias: no address"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
1311 1312 1313
    return (-1);
  }

1314
  mutt_extract_token (buf, s, 0);
Thomas Roessler's avatar
Thomas Roessler committed
1315

1316 1317
  if (parse_group_context (&gc, buf, s, data, err) == -1)
    return -1;
1318
  
Thomas Roessler's avatar
Thomas Roessler committed
1319 1320 1321
  /* check to see if an alias with this name already exists */
  for (; tmp; tmp = tmp->next)
  {
1322
    if (!mutt_strcasecmp (tmp->name, buf->data))
Thomas Roessler's avatar
Thomas Roessler committed
1323 1324 1325 1326 1327 1328 1329 1330
      break;
    last = tmp;
  }

  if (!tmp)
  {
    /* create a new alias */
    tmp = (ALIAS *) safe_calloc (1, sizeof (ALIAS));
Thomas Roessler's avatar
Thomas Roessler committed
1331
    tmp->self = tmp;
1332
    tmp->name = safe_strdup (buf->data);
Thomas Roessler's avatar
Thomas Roessler committed
1333 1334 1335
    /* give the main addressbook code a chance */
    if (CurrentMenu == MENU_ALIAS)
      set_option (OPTMENUCALLER);
Thomas Roessler's avatar
Thomas Roessler committed
1336 1337 1338 1339 1340
  }
  else
  {
    /* override the previous value */
    rfc822_free_address (&tmp->addr);
Thomas Roessler's avatar
Thomas Roessler committed
1341
    if (CurrentMenu == MENU_ALIAS)
1342
      set_option (OPTFORCEREDRAWINDEX);
Thomas Roessler's avatar
Thomas Roessler committed
1343 1344
  }

1345
  mutt_extract_token (buf, s, M_TOKEN_QUOTE | M_TOKEN_SPACE | M_TOKEN_SEMICOLON);
1346 1347
  dprint (2, (debugfile, "parse_alias: Second token is '%s'.\n",
	      buf->data));
1348

Thomas Roessler's avatar
Thomas Roessler committed
1349
  tmp->addr = mutt_parse_adrlist (tmp->addr, buf->data);
1350

Thomas Roessler's avatar
Thomas Roessler committed
1351 1352 1353 1354
  if (last)
    last->next = tmp;
  else
    Aliases = tmp;
1355 1356 1357 1358
  if (mutt_addrlist_to_idna (tmp->addr, &estr))
  {
    snprintf (err->data, err->dsize, _("Warning: Bad IDN '%s' in alias '%s'.\n"),
	      estr, tmp->name);
1359
    goto bail;
1360
  }
1361 1362 1363 1364

  mutt_group_context_add_adrlist (gc, tmp->addr);


1365 1366 1367 1368
#ifdef DEBUG
  if (debuglevel >= 2) 
  {
    ADDRESS *a;
1369 1370
    /* A group is terminated with an empty address, so check a->mailbox */
    for (a = tmp->addr; a && a->mailbox; a = a->next)
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
    {
      if (!a->group)
	dprint (2, (debugfile, "parse_alias:   %s\n",
		    a->mailbox));
      else
	dprint (2, (debugfile, "parse_alias:   Group %s\n",
		    a->mailbox));
    }
  }
#endif
1381
  mutt_group_context_destroy (&gc);
Thomas Roessler's avatar
Thomas Roessler committed
1382
  return 0;
1383 1384 1385 1386
  
  bail:
  mutt_group_context_destroy (&gc);
  return -1;
Thomas Roessler's avatar
Thomas Roessler committed
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
}

static int
parse_unmy_hdr (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  LIST *last = NULL;
  LIST *tmp = UserHeader;
  LIST *ptr;
  size_t l;

  do
  {
    mutt_extract_token (buf, s, 0);
1400
    if (mutt_strcmp ("*", buf->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1401 1402 1403 1404 1405 1406
      mutt_free_list (&UserHeader);
    else
    {
      tmp = UserHeader;
      last = NULL;

1407
      l = mutt_strlen (buf->data);
Thomas Roessler's avatar
Thomas Roessler committed
1408 1409 1410 1411 1412
      if (buf->data[l - 1] == ':')
	l--;

      while (tmp)
      {
1413
	if (ascii_strncasecmp (buf->data, tmp->data, l) == 0 && tmp->data[l] == ':')
Thomas Roessler's avatar
Thomas Roessler committed
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
	{
	  ptr = tmp;
	  if (last)
	    last->next = tmp->next;
	  else
	    UserHeader = tmp->next;
	  tmp = tmp->next;
	  ptr->next = NULL;
	  mutt_free_list (&ptr);
	}
	else
	{
	  last = tmp;
	  tmp = tmp->next;
	}
      }
    }
  }
  while (MoreArgs (s));
  return 0;
}

static int parse_my_hdr (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  LIST *tmp;
  size_t keylen;
  char *p;

  mutt_extract_token (buf, s, M_TOKEN_SPACE | M_TOKEN_QUOTE);
  if ((p = strpbrk (buf->data, ": \t")) == NULL || *p != ':')
  {
1445
    strfcpy (err->data, _("invalid header field"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
1446 1447 1448 1449 1450 1451 1452 1453 1454
    return (-1);
  }
  keylen = p - buf->data + 1;

  if (UserHeader)
  {
    for (tmp = UserHeader; ; tmp = tmp->next)
    {
      /* see if there is already a field by this name */
1455
      if (ascii_strncasecmp (buf->data, tmp->data, keylen) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1456 1457
      {
	/* replace the old value */
1458
	FREE (&tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
	tmp->data = buf->data;
	memset (buf, 0, sizeof (BUFFER));
	return 0;
      }
      if (!tmp->next)
	break;
    }
    tmp->next = mutt_new_list ();
    tmp = tmp->next;
  }
  else
  {
    tmp = mutt_new_list ();
    UserHeader = tmp;
  }
  tmp->data = buf->data;
  memset (buf, 0, sizeof (BUFFER));
  return 0;
}

static int
parse_sort (short *val, const char *s, const struct mapping_t *map, BUFFER *err)
{
  int i, flags = 0;

1484
  if (mutt_strncmp ("reverse-", s, 8) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1485 1486 1487 1488 1489
  {
    s += 8;
    flags = SORT_REVERSE;
  }
  
1490
  if (mutt_strncmp ("last-", s, 5) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1491 1492 1493 1494 1495 1496 1497
  {
    s += 5;
    flags |= SORT_LAST