init.c 57.3 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  02111, 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) || defined(USE_GNUTLS)
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 53 54 55 56 57 58
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
59 60 61

void set_quadoption (int opt, int flag)
{
62 63 64 65 66
  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
67 68 69 70
}

int quadoption (int opt)
{
71 72 73 74
  int n = opt/4;
  int b = (opt % 4) * 2;

  return (QuadOptions[n] >> b) & 0x3;
Thomas Roessler's avatar
Thomas Roessler committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

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++)
103
    if (mutt_strcmp (s, MuttVars[i].option) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      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)) ||
126
	  ((flags & M_TOKEN_PATTERN) && strchr ("~=!|", ch)))
Thomas Roessler's avatar
Thomas Roessler committed
127 128 129 130 131 132 133 134 135 136 137
	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 != '\'')
    {
138 139
	if (!*tok->dptr)
	    return -1; /* premature end of token */
Thomas Roessler's avatar
Thomas Roessler committed
140 141 142 143
      switch (ch = *tok->dptr++)
      {
	case 'c':
	case 'C':
144 145
	    if (!*tok->dptr)
		return -1; /* premature end of token */
146 147
	  mutt_buffer_addch (dest, (toupper ((unsigned char) *tok->dptr)
                                    - '@') & 0x7f);
Thomas Roessler's avatar
Thomas Roessler committed
148 149 150
	  tok->dptr++;
	  break;
	case 'r':
151
	  mutt_buffer_addch (dest, '\r');
Thomas Roessler's avatar
Thomas Roessler committed
152 153
	  break;
	case 'n':
154
	  mutt_buffer_addch (dest, '\n');
Thomas Roessler's avatar
Thomas Roessler committed
155 156
	  break;
	case 't':
157
	  mutt_buffer_addch (dest, '\t');
Thomas Roessler's avatar
Thomas Roessler committed
158 159
	  break;
	case 'f':
160
	  mutt_buffer_addch (dest, '\f');
Thomas Roessler's avatar
Thomas Roessler committed
161 162
	  break;
	case 'e':
163
	  mutt_buffer_addch (dest, '\033');
Thomas Roessler's avatar
Thomas Roessler committed
164 165
	  break;
	default:
Thomas Roessler's avatar
Thomas Roessler committed
166 167 168
	  if (isdigit ((unsigned char) ch) &&
	      isdigit ((unsigned char) *tok->dptr) &&
	      isdigit ((unsigned char) *(tok->dptr + 1)))
Thomas Roessler's avatar
Thomas Roessler committed
169 170
	  {

171
	    mutt_buffer_addch (dest, (ch << 6) + (*tok->dptr << 3) + *(tok->dptr + 1) - 3504);
Thomas Roessler's avatar
Thomas Roessler committed
172 173 174
	    tok->dptr += 2;
	  }
	  else
175
	    mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
176 177 178 179
      }
    }
    else if (ch == '^' && (flags & M_TOKEN_CONDENSE))
    {
180 181
	if (!*tok->dptr)
	    return -1; /* premature end of token */
Thomas Roessler's avatar
Thomas Roessler committed
182 183
      ch = *tok->dptr++;
      if (ch == '^')
184
	mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
185
      else if (ch == '[')
186
	mutt_buffer_addch (dest, '\033');
Thomas Roessler's avatar
Thomas Roessler committed
187
      else if (isalpha ((unsigned char) ch))
188
	mutt_buffer_addch (dest, toupper ((unsigned char) ch) - '@');
Thomas Roessler's avatar
Thomas Roessler committed
189 190
      else
      {
191 192
	mutt_buffer_addch (dest, '^');
	mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      }
    }
    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));
222
	FREE (&cmd);
Thomas Roessler's avatar
Thomas Roessler committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236
	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 */
237 238 239 240 241 242 243
      /* 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
244
      else if (expn.data)
Thomas Roessler's avatar
Thomas Roessler committed
245
      {
246 247
	expnlen = mutt_strlen (expn.data);
	tok->dsize = expnlen + mutt_strlen (tok->dptr) + 1;
Thomas Roessler's avatar
Thomas Roessler committed
248 249
	ptr = safe_malloc (tok->dsize);
	memcpy (ptr, expn.data, expnlen);
250
	strcpy (ptr + expnlen, tok->dptr);	/* __STRCPY_CHECKED__ */
Thomas Roessler's avatar
Thomas Roessler committed
251 252 253 254 255 256 257 258 259
	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
260
    else if (ch == '$' && (!qc || qc == '"') && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr)))
Thomas Roessler's avatar
Thomas Roessler committed
261
    {
262
      char *env = NULL, *var = NULL;
Thomas Roessler's avatar
Thomas Roessler committed
263 264 265 266 267 268 269 270 271 272 273 274

      if (*tok->dptr == '{')
      {
	tok->dptr++;
	if ((pc = strchr (tok->dptr, '}')))
	{
	  var = mutt_substrdup (tok->dptr, pc);
	  tok->dptr = pc + 1;
	}
      }
      else
      {
275
	for (pc = tok->dptr; isalnum ((unsigned char) *pc) || *pc == '_'; pc++)
Thomas Roessler's avatar
Thomas Roessler committed
276 277 278 279
	  ;
	var = mutt_substrdup (tok->dptr, pc);
	tok->dptr = pc;
      }
280
      if (var && (env = getenv (var)))
281
	mutt_buffer_addstr (dest, env);
Thomas Roessler's avatar
Thomas Roessler committed
282 283 284
      FREE (&var);
    }
    else
285
      mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
286
  }
287
  mutt_buffer_addch (dest, 0); /* terminate the string */
Thomas Roessler's avatar
Thomas Roessler committed
288 289 290 291
  SKIPWS (tok->dptr);
  return 0;
}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
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:
    FREE ((char**)p->data);
    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);
}

334
static void add_to_list (LIST **list, const char *str)
Thomas Roessler's avatar
Thomas Roessler committed
335 336 337
{
  LIST *t, *last = NULL;

338 339 340 341
  /* don't add a NULL or empty string to the list */
  if (!str || *str == '\0')
    return;

342 343
  /* 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
344
  {
345
    if (ascii_strcasecmp (str, last->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
346
    {
347 348 349
      /* already on the list, so just ignore it */
      last = NULL;
      break;
Thomas Roessler's avatar
Thomas Roessler committed
350
    }
351 352 353
    if (!last->next)
      break;
  }
Thomas Roessler's avatar
Thomas Roessler committed
354

355 356 357 358 359
  if (!*list || last)
  {
    t = (LIST *) safe_calloc (1, sizeof (LIST));
    t->data = safe_strdup (str);
    if (last)
Thomas Roessler's avatar
Thomas Roessler committed
360
    {
361 362
      last->next = t;
      last = last->next;
Thomas Roessler's avatar
Thomas Roessler committed
363
    }
364 365
    else
      *list = last = t;
Thomas Roessler's avatar
Thomas Roessler committed
366 367 368
  }
}

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
static int add_to_rx_list (RX_LIST **list, const char *s, int flags, BUFFER *err)
{
  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;
}


415 416
static int add_to_spam_list (SPAM_LIST **list, const char *pat, const char *templ, BUFFER *err)
{
417
  SPAM_LIST *t = NULL, *last = NULL;
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
  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)
    {
436 437 438 439 440 441 442 443
      /* 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;
      safe_free(&t->template);
444 445 446 447 448 449
      break;
    }
    if (!last->next)
      break;
  }

450 451 452 453
  /* 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)
454 455 456
  {
    t = mutt_new_spam_list();
    t->rx = rx;
457 458 459 460 461
    if (last)
      last->next = t;
    else
      *list = t;
  }
462

463 464 465 466 467 468 469 470
  /* 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 == '%')
471
    {
472 473 474 475 476
        n = atoi(++p);
        if (n > t->nmatch)
          t->nmatch = n;
        while (*p && isdigit((int)*p))
          ++p;
477
    }
478 479 480 481
    else
        ++p;
  }
  t->nmatch++;		/* match 0 is always the whole expr */
482

483 484 485 486 487 488 489 490 491 492
  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;
493 494
  if (!spam)
    return 0;
495 496 497 498 499 500 501 502 503 504 505 506 507
  if (spam->rx && !mutt_strcmp(spam->rx->pattern, pat))
  {
    *list = spam->next;
    mutt_free_regexp(&spam->rx);
    safe_free(&spam->template);
    safe_free(&spam);
    return 1;
  }

  prev = spam;
  for (spam = prev->next; spam;)
  {
    if (!mutt_strcmp(spam->rx->pattern, pat))
508
    {
509 510 511 512 513 514
      prev->next = spam->next;
      mutt_free_regexp(&spam->rx);
      safe_free(&spam->template);
      safe_free(&spam);
      spam = prev->next;
      ++nremoved;
515 516
    }
    else
517
      spam = spam->next;
518 519
  }

520
  return nremoved;
521 522 523
}


524
static void remove_from_list (LIST **l, const char *str)
Thomas Roessler's avatar
Thomas Roessler committed
525 526 527
{
  LIST *p, *last = NULL;

528 529 530
  if (mutt_strcmp ("*", str) == 0)
    mutt_free_list (l);    /* ``unCMD *'' means delete all current entries */
  else
Thomas Roessler's avatar
Thomas Roessler committed
531
  {
532 533 534
    p = *l;
    last = NULL;
    while (p)
Thomas Roessler's avatar
Thomas Roessler committed
535
    {
536
      if (ascii_strcasecmp (str, p->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
537
      {
538
	FREE (&p->data);
539 540
	if (last)
	  last->next = p->next;
Thomas Roessler's avatar
Thomas Roessler committed
541
	else
542
	  (*l) = p->next;
543
	FREE (&p);
544 545 546 547 548
      }
      else
      {
	last = p;
	p = p->next;
Thomas Roessler's avatar
Thomas Roessler committed
549 550 551 552 553
      }
    }
  }
}

554
static int remove_from_rx_list (RX_LIST **l, const char *str)
555 556
{
  RX_LIST *p, *last = NULL;
557
  int rv = -1;
558 559

  if (mutt_strcmp ("*", str) == 0)
560
  {
561
    mutt_free_rx_list (l);    /* ``unCMD *'' means delete all current entries */
562 563
    rv = 0;
  }
564 565 566 567 568 569 570 571 572 573 574 575 576 577
  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);
578
	rv = 0;
579 580 581 582 583 584 585 586
      }
      else
      {
	last = p;
	p = p->next;
      }
    }
  }
587
  return (rv);
588 589
}

Thomas Roessler's avatar
Thomas Roessler committed
590 591
static int parse_unignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
592 593 594
  do
  {
    mutt_extract_token (buf, s, 0);
595 596 597 598 599

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

600 601 602 603
    remove_from_list (&Ignore, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
604 605 606 607 608
  return 0;
}

static int parse_ignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
609 610 611 612 613 614 615 616
  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
617 618 619 620 621
  return 0;
}

static int parse_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
622 623 624 625 626 627 628
  do
  {
    mutt_extract_token (buf, s, 0);
    add_to_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
629 630 631
  return 0;
}

632 633 634 635 636 637 638 639 640 641 642 643 644
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)
{
  _alternates_clean();
645 646 647 648 649 650 651 652 653 654 655
  do
  {
    mutt_extract_token (buf, s, 0);
    remove_from_rx_list (&UnAlternates, buf->data);

    if (add_to_rx_list (&Alternates, buf->data, REG_ICASE, err) != 0)
      return -1;
  }
  while (MoreArgs (s));

  return 0;
656 657 658 659 660
}

static int parse_unalternates (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  _alternates_clean();
661 662 663 664 665 666 667 668 669 670 671 672 673
  do
  {
    mutt_extract_token (buf, s, 0);
    remove_from_rx_list (&Alternates, buf->data);

    if (mutt_strcmp (buf->data, "*") &&
	add_to_rx_list (&UnAlternates, buf->data, REG_ICASE, err) != 0)
      return -1;

  }
  while (MoreArgs (s));

  return 0;
674 675
}

676 677 678 679 680 681
static int parse_spam_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  BUFFER templ;

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

682
  /* Insist on at least one parameter */
683 684
  if (!MoreArgs(s))
  {
685 686 687 688
    if (data == M_SPAM)
      strfcpy(err->data, _("spam: no matching pattern"), err->dsize);
    else
      strfcpy(err->data, _("nospam: no matching pattern"), err->dsize);
689 690
    return -1;
  }
691 692

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

695 696
  /* data should be either M_SPAM or M_NOSPAM. M_SPAM is for spam commands. */
  if (data == M_SPAM)
697
  {
698 699 700 701 702 703
    /* 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. */
704 705
      if (add_to_spam_list (&SpamList, buf->data, templ.data, err) != 0) {
	  FREE(&templ.data);
706
          return -1;
707 708
      }
      FREE(&templ.data);
709 710 711 712 713 714 715 716 717
    }

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

    return 0;
718
  }
719 720 721

  /* M_NOSPAM is for nospam commands. */
  else if (data == M_NOSPAM)
722
  {
723
    /* nospam only ever has one parameter. */
724

725 726 727 728 729 730 731 732 733 734 735 736 737 738
    /* "*" 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. */
    if (add_to_rx_list (&NoSpamList, buf->data, REG_ICASE, err) != 0)
739
      return -1;
740 741 742 743 744 745 746

    return 0;
  }

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

749

Thomas Roessler's avatar
Thomas Roessler committed
750 751
static int parse_unlist (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
752 753 754
  do
  {
    mutt_extract_token (buf, s, 0);
755 756 757 758 759 760 761 762
    /*
     * Check for deletion of entire list
     */
    if (mutt_strcmp (buf->data, "*") == 0)
    {
      mutt_free_list ((LIST **) data);
      break;
    }
763 764 765 766
    remove_from_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
767 768 769
  return 0;
}

770 771 772 773 774 775 776 777 778 779 780 781 782 783
static int parse_lists (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  do
  {
    mutt_extract_token (buf, s, 0);
    remove_from_rx_list (&UnMailLists, buf->data);
    
    if (add_to_rx_list (&MailLists, buf->data, REG_ICASE, err) != 0)
      return -1;
  }
  while (MoreArgs (s));

  return 0;
}
784 785 786 787 788 789

static int parse_unlists (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  do
  {
    mutt_extract_token (buf, s, 0);
790
    remove_from_rx_list (&SubscribedLists, buf->data);
791 792 793 794 795
    remove_from_rx_list (&MailLists, buf->data);
    
    if (mutt_strcmp (buf->data, "*") && 
	add_to_rx_list (&UnMailLists, buf->data, REG_ICASE, err) != 0)
      return -1;
796 797 798 799 800 801 802 803 804 805 806
  }
  while (MoreArgs (s));

  return 0;
}

static int parse_subscribe (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  do
  {
    mutt_extract_token (buf, s, 0);
807 808 809
    remove_from_rx_list (&UnMailLists, buf->data);
    remove_from_rx_list (&UnSubscribedLists, buf->data);

810 811 812 813
    if (add_to_rx_list (&MailLists, buf->data, REG_ICASE, err) != 0)
      return -1;
    if (add_to_rx_list (&SubscribedLists, buf->data, REG_ICASE, err) != 0)
      return -1;
814 815 816 817 818
  }
  while (MoreArgs (s));

  return 0;
}
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834

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, "*") &&
	add_to_rx_list (&UnSubscribedLists, buf->data, REG_ICASE, err) != 0)
      return -1;
  }
  while (MoreArgs (s));

  return 0;
}
835
  
Thomas Roessler's avatar
Thomas Roessler committed
836 837 838 839 840 841 842 843
static int parse_unalias (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  ALIAS *tmp, *last = NULL;

  do
  {
    mutt_extract_token (buf, s, 0);

844
    if (mutt_strcmp ("*", buf->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
845
    {
846
      if (CurrentMenu == MENU_ALIAS)
Thomas Roessler's avatar
Thomas Roessler committed
847
      {
848
	for (tmp = Aliases; tmp ; tmp = tmp->next)
849
	  tmp->del = 1;
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
	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);
874 875
	  break;
	}
876
	last = tmp;
Thomas Roessler's avatar
Thomas Roessler committed
877 878 879 880 881 882 883 884 885 886
      }
  }
  while (MoreArgs (s));
  return 0;
}

static int parse_alias (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  ALIAS *tmp = Aliases;
  ALIAS *last = NULL;
887 888
  char *estr = NULL;
  
889
  if (!MoreArgs (s))
Thomas Roessler's avatar
Thomas Roessler committed
890
  {
891
    strfcpy (err->data, _("alias: no address"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
892 893 894
    return (-1);
  }

895
  mutt_extract_token (buf, s, 0);
Thomas Roessler's avatar
Thomas Roessler committed
896

897 898 899
  dprint (2, (debugfile, "parse_alias: First token is '%s'.\n",
	      buf->data));
  
Thomas Roessler's avatar
Thomas Roessler committed
900 901 902
  /* check to see if an alias with this name already exists */
  for (; tmp; tmp = tmp->next)
  {
903
    if (!mutt_strcasecmp (tmp->name, buf->data))
Thomas Roessler's avatar
Thomas Roessler committed
904 905 906 907 908 909 910 911
      break;
    last = tmp;
  }

  if (!tmp)
  {
    /* create a new alias */
    tmp = (ALIAS *) safe_calloc (1, sizeof (ALIAS));
Thomas Roessler's avatar
Thomas Roessler committed
912
    tmp->self = tmp;
913
    tmp->name = safe_strdup (buf->data);
Thomas Roessler's avatar
Thomas Roessler committed
914 915 916
    /* give the main addressbook code a chance */
    if (CurrentMenu == MENU_ALIAS)
      set_option (OPTMENUCALLER);
Thomas Roessler's avatar
Thomas Roessler committed
917 918 919 920 921
  }
  else
  {
    /* override the previous value */
    rfc822_free_address (&tmp->addr);
Thomas Roessler's avatar
Thomas Roessler committed
922
    if (CurrentMenu == MENU_ALIAS)
923
      set_option (OPTFORCEREDRAWINDEX);
Thomas Roessler's avatar
Thomas Roessler committed
924 925
  }

926
  mutt_extract_token (buf, s, M_TOKEN_QUOTE | M_TOKEN_SPACE | M_TOKEN_SEMICOLON);
927 928
  dprint (2, (debugfile, "parse_alias: Second token is '%s'.\n",
	      buf->data));
Thomas Roessler's avatar
Thomas Roessler committed
929 930 931 932 933
  tmp->addr = mutt_parse_adrlist (tmp->addr, buf->data);
  if (last)
    last->next = tmp;
  else
    Aliases = tmp;
934 935 936 937 938 939
  if (mutt_addrlist_to_idna (tmp->addr, &estr))
  {
    snprintf (err->data, err->dsize, _("Warning: Bad IDN '%s' in alias '%s'.\n"),
	      estr, tmp->name);
    return -1;
  }
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
#ifdef DEBUG
  if (debuglevel >= 2) 
  {
    ADDRESS *a;
    for (a = tmp->addr; a; a = a->next)
    {
      if (!a->group)
	dprint (2, (debugfile, "parse_alias:   %s\n",
		    a->mailbox));
      else
	dprint (2, (debugfile, "parse_alias:   Group %s\n",
		    a->mailbox));
    }
  }
#endif
Thomas Roessler's avatar
Thomas Roessler committed
955 956 957 958 959 960 961 962 963 964 965 966 967 968
  return 0;
}

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);
969
    if (mutt_strcmp ("*", buf->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
970 971 972 973 974 975
      mutt_free_list (&UserHeader);
    else
    {
      tmp = UserHeader;
      last = NULL;

976
      l = mutt_strlen (buf->data);
Thomas Roessler's avatar
Thomas Roessler committed
977 978 979 980 981
      if (buf->data[l - 1] == ':')
	l--;

      while (tmp)
      {
982
	if (ascii_strncasecmp (buf->data, tmp->data, l) == 0 && tmp->data[l] == ':')
Thomas Roessler's avatar
Thomas Roessler committed
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
	{
	  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 != ':')
  {
1014
    strfcpy (err->data, _("invalid header field"), err->dsize);
Thomas Roessler's avatar
Thomas Roessler committed
1015 1016 1017 1018 1019 1020 1021 1022 1023
    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 */
1024
      if (ascii_strncasecmp (buf->data, tmp->data, keylen) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1025 1026
      {
	/* replace the old value */
1027
	FREE (&tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
	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;

1053
  if (mutt_strncmp ("reverse-", s, 8) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1054 1055 1056 1057 1058
  {
    s += 8;
    flags = SORT_REVERSE;
  }
  
1059
  if (mutt_strncmp ("last-", s, 5) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1060 1061 1062 1063 1064 1065 1066
  {
    s += 5;
    flags |= SORT_LAST;
  }

  if ((i = mutt_getvaluebyname (s, map)) == -1)
  {
1067
    snprintf (err->data, err->dsize, _("%s: unknown sorting method"), s);
Thomas Roessler's avatar
Thomas Roessler committed
1068 1069 1070 1071 1072 1073 1074 1075
    return (-1);
  }

  *val = i | flags;

  return 0;
}

1076 1077 1078 1079 1080
static void mutt_set_default (struct option_t *p)
{
  switch (p->type & DT_MASK)
  {
    case DT_STR:
1081
      if (!p->init && *((char **) p->data))
1082 1083 1084
        p->init = (unsigned long) safe_strdup (* ((char **) p->data));
      break;
    case DT_PATH:
1085
      if (!p->init && *((char **) p->data))
1086 1087
      {
	char *cp = safe_strdup (*((char **) p->data));
1088
	/* mutt_pretty_mailbox (cp); */
1089 1090 1091 1092
        p->init = (unsigned long) cp;
      }
      break;
    case DT_ADDR:
1093
      if (!p->init && *((ADDRESS **) p->data))
1094 1095
      {
	char tmp[HUGE_STRING];
1096
	*tmp = '\0';
1097
	rfc822_write_address (tmp, sizeof (tmp), *((ADDRESS **) p->data), 0);
1098 1099 1100 1101 1102 1103
	p->init = (unsigned long) safe_strdup (tmp);
      }
      break;
    case DT_RX:
    {
      REGEXP *pp = (REGEXP *) p->data;
1104
      if (!p->init && pp->pattern)
1105 1106 1107 1108 1109 1110
	p->init = (unsigned long) safe_strdup (pp->pattern);
      break;
    }
  }
}

Thomas Roessler's avatar
Thomas Roessler committed
1111 1112 1113 1114 1115 1116
static void mutt_restore_default (struct option_t *p)
{
  switch (p->type & DT_MASK)
  {
    case DT_STR:
      if (p->init)
1117
	mutt_str_replace ((char **) p->data, (char *) p->init); 
Thomas Roessler's avatar
Thomas Roessler committed
1118 1119 1120 1121 1122 1123 1124 1125
      break;
    case DT_PATH:
      if (p->init)
      {
	char path[_POSIX_PATH_MAX];

	strfcpy (path, (char *) p->init, sizeof (path));
	mutt_expand_path (path, sizeof (path));
1126
	mutt_str_replace ((char **) p->data, path);
Thomas Roessler's avatar
Thomas Roessler committed
1127 1128
      }
      break;
1129 1130 1131 1132 1133 1134 1135
    case DT_ADDR:
      if (p->init)
      {
	rfc822_free_address ((ADDRESS **) p->data);
	*((ADDRESS **) p->data) = rfc822_parse_adrlist (NULL, (char *) p->init);
      }
      break;
Thomas Roessler's avatar
Thomas Roessler committed
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
    case DT_BOOL:
      if (p->init)
	set_option (p->data);
      else
	unset_option (p->data);
      break;
    case DT_QUAD:
      set_quadoption (p->data, p->init);
      break;
    case DT_NUM:
    case DT_SORT:
    case DT_MAGIC:
      *((short *) p->data) = p->init;
      break;
    case DT_RX:
      {
	REGEXP *pp = (REGEXP *) p->data;
Thomas Roessler's avatar
Thomas Roessler committed
1153 1154
	int flags = 0;

Thomas Roessler's avatar
Thomas Roessler committed
1155
	FREE (&pp->pattern);
Thomas Roessler's avatar
Thomas Roessler committed
1156 1157 1158 1159 1160
	if (pp->rx)
	{
	  regfree (pp->rx);
	  FREE (&pp->rx);
	}
1161

Thomas Roessler's avatar
Thomas Roessler committed
1162 1163
	if (p->init)
	{
Thomas Roessler's avatar
Thomas Roessler committed
1164 1165 1166
	  char *s = (char *) p->init;

	  pp->rx = safe_calloc (1, sizeof (regex_t));
Thomas Roessler's avatar
Thomas Roessler committed
1167
	  pp->pattern = safe_strdup ((char *) p->init);
1168
	  if (mutt_strcmp (p->option, "mask") != 0)
Thomas Roessler's avatar
Thomas Roessler committed
1169
	    flags |= mutt_which_case ((const char *) p->init);
1170
	  if (mutt_strcmp (p->option, "mask") == 0 && *s == '!')
Thomas Roessler's avatar
Thomas Roessler committed
1171 1172 1173 1174 1175
	  {
	    s++;
	    pp->not = 1;
	  }
	  if (REGCOMP (pp->rx, s, flags) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
1176
	  {
1177 1178
	    fprintf (stderr, _("mutt_restore_default(%s): error in regexp: %s\n"),
		     p->option, pp->pattern);
Thomas Roessler's avatar
Thomas Roessler committed
1179 1180 1181
	    FREE (&pp->pattern);
	    regfree (pp->rx);
	    FREE (&pp->rx);
Thomas Roessler's avatar
Thomas Roessler committed
1182 1183 1184 1185 1186
	  }
	}
      }
      break;
  }
1187

Thomas Roessler's avatar
Thomas Roessler committed
1188 1189 1190 1191 1192 1193 1194 1195
  if (p->flags & R_INDEX)
    set_option (OPTFORCEREDRAWINDEX);
  if (p->flags & R_PAGER)
    set_option (OPTFORCEREDRAWPAGER);
  if (p->flags & R_RESORT_SUB)
    set_option (OPTSORTSUBTHREADS);
  if (p->flags & R_RESORT)
    set_option (OPTNEEDRESORT);
1196 1197
  if (p->flags & R_RESORT_INIT)
    set_option (OPTRESORTINIT);
1198 1199
  if (p->flags & R_TREE)
    set_option (OPTREDRAWTREE);
Thomas Roessler's avatar
Thomas Roessler committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
}

static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
{
  int idx, query, unset, inv, reset, r = 0;
  char *p, scratch[_POSIX_PATH_MAX];

  while (MoreArgs (s))
  {
    /* reset state variables */
    query = 0;
    unset = data & M_SET_UNSET;
    inv = data & M_SET_INV;
    reset = data & M_SET_RESET;

    if (*s->dptr == '?')
    {
      query = 1;
      s->dptr++;
    }
1220
    else if (mutt_strncmp ("no", s->dptr, 2) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1221 1222 1223 1224
    {
      s->dptr += 2;
      unset = !unset;
    }
1225
    else if (mutt_strncmp ("inv", s->dptr, 3) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
    {
      s->dptr += 3;
      inv = !inv;
    }
    else if (*s->dptr == '&')
    {
      reset = 1;
      s->dptr++;
    }

    /* get the variable name */
    mutt_extract_token (tmp, s, M_TOKEN_EQUAL);

    if ((idx = mutt_option_index (tmp->data)) == -1 &&
1240
	!(reset && !mutt_strcmp ("all", tmp->data)))
Thomas Roessler's avatar
Thomas Roessler committed
1241
    {
1242
      snprintf (err->data, err->dsize, _("%s: unknown variable"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1243 1244 1245 1246 1247 1248 1249 1250
      return (-1);
    }
    SKIPWS (s->dptr);

    if (reset)
    {
      if (query || unset || inv)
      {
1251
	snprintf (err->data, err->dsize, _("prefix is illegal with reset"));
Thomas Roessler's avatar
Thomas Roessler committed
1252 1253 1254 1255 1256
	return (-1);
      }

      if (s && *s->dptr == '=')
      {
1257
	snprintf (err->data, err->dsize, _("value is illegal with reset"));
Thomas Roessler's avatar
Thomas Roessler committed
1258 1259 1260
	return (-1);
      }
     
1261
      if (!mutt_strcmp ("all", tmp->data))
Thomas Roessler's avatar
Thomas Roessler committed
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
      {
	for (idx = 0; MuttVars[idx].option; idx++)
	  mutt_restore_default (&MuttVars[idx]);
	return 0;
      }
      else
	mutt_restore_default (&MuttVars[idx]);
    } 
    else if (DTYPE (MuttVars[idx].type) == DT_BOOL)
    { 
      if (s && *s->dptr == '=')
      {
Thomas Roessler's avatar
Thomas Roessler committed
1274 1275 1276 1277 1278 1279 1280 1281
	if (unset || inv || query)
	{
	  snprintf (err->data, err->dsize, "Usage: set variable=yes|no");
	  return (-1);
	}

	s->dptr++;
	mutt_extract_token (tmp, s, 0);
1282
	if (ascii_strcasecmp ("yes", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1283
	  unset = inv = 0;
1284
	else if (ascii_strcasecmp ("no", tmp->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1285 1286 1287 1288 1289 1290
	  unset = 1;
	else
	{
	  snprintf (err->data, err->dsize, "Usage: set variable=yes|no");
	  return (-1);
	}
Thomas Roessler's avatar
Thomas Roessler committed
1291 1292 1293 1294
      }

      if (query)
      {
1295 1296
	snprintf (err->data, err->dsize, option (MuttVars[idx].data)
			? _("%s is set") : _("%s is unset"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	return 0;
      }

      if (unset)
	unset_option (MuttVars[idx].data);
      else if (inv)
	toggle_option (MuttVars[idx].data);
      else
	set_option (MuttVars[idx].data);
    }
    else if (DTYPE (MuttVars[idx].type) == DT_STR ||
1308 1309
	     DTYPE (MuttVars[idx].type) == DT_PATH ||
	     DTYPE (MuttVars[idx].type) == DT_ADDR)
Thomas Roessler's avatar
Thomas Roessler committed
1310
    {
1311 1312 1313 1314 1315
      if (unset)
      {
	if (DTYPE (MuttVars[idx].type) == DT_ADDR)
	  rfc822_free_address ((ADDRESS **) MuttVars[idx].data);
	else
Mads Martin Joergensen's avatar
Mads Martin Joergensen committed
1316
	  FREE ((void *) MuttVars[idx].data);
1317 1318
      }
      else if (query || *s->dptr != '=')
Thomas Roessler's avatar
Thomas Roessler committed
1319
      {
1320 1321 1322 1323 1324 1325
	char _tmp[STRING];
	char *val = NULL;
	
	if (DTYPE (MuttVars[idx].type) == DT_ADDR)
	{
	  _tmp[0] = '\0';
1326
	  rfc822_write_address (_tmp, sizeof (_tmp), *((ADDRESS **) MuttVars[idx].data), 0);
1327 1328 1329 1330 1331
	  val = _tmp;
	}
	else
	  val = *((char **) MuttVars[idx].data);
	
Thomas Roessler's avatar
Thomas Roessler committed
1332 1333
	/* user requested the value of this variable */
	snprintf (err->data, err->dsize, "%s=\"%s\"", MuttVars[idx].option,
1334
		  NONULL (val));
Thomas Roessler's avatar
Thomas Roessler committed
1335 1336
	break;
      }
1337 1338
      else
      {
1339 1340 1341 1342 1343 1344
        s->dptr++;

        /* copy the value of the string */
        if (DTYPE (MuttVars[idx].type) == DT_ADDR)
	  rfc822_free_address ((ADDRESS **) MuttVars[idx].data);
        else
Mads Martin Joergensen's avatar
Mads Martin Joergensen committed
1345
	  FREE ((void *) MuttVars[idx].data);
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

        mutt_extract_token (tmp, s, 0);
        if (DTYPE (MuttVars[idx].type) == DT_PATH)
        {
	  strfcpy (scratch, tmp->data, sizeof (scratch));
	  mutt_expand_path (scratch, sizeof (scratch));
	  *((char **) MuttVars[idx].data) = safe_strdup (scratch);
        }
        else if (DTYPE (MuttVars[idx].type) == DT_STR)
        {
	  *((char **) MuttVars[idx].data) = safe_strdup (tmp->data);
1357 1358
	  if (mutt_strcmp (MuttVars[idx].option, "charset") == 0)
	    mutt_set_charset (Charset);
1359 1360 1361 1362 1363
        }
        else
        {
	  *((ADDRESS **) MuttVars[idx].data) = rfc822_parse_adrlist (NULL, tmp->data);
        }
1364
      }
Thomas Roessler's avatar
Thomas Roessler committed
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
    }
    else if (DTYPE(MuttVars[idx].type) == DT_RX)
    {
      REGEXP *ptr = (REGEXP *) MuttVars[idx].data;
      regex_t *rx;
      int e, flags = 0;

      if (query || *s->dptr != '=')
      {
	/* user requested the value of this variable */
	snprintf (err->data, err->dsize, "%s=\"%s\"", MuttVars[idx].option,
		  NONULL (ptr->pattern));
	break;
      }

1380
      if (option(OPTATTACHMSG) && !mutt_strcmp(MuttVars[idx].option, "reply_regexp"))
1381 1382 1383 1384 1385 1386
      {
	snprintf (err->data, err->dsize, "Operation not permitted when in attach-message mode.");
	r = -1;
	break;
      }
      
Thomas Roessler's avatar
Thomas Roessler committed
1387 1388 1389 1390 1391
      s->dptr++;

      /* copy the value of the string */
      mutt_extract_token (tmp, s, 0);

1392
      if (!ptr->pattern || mutt_strcmp (ptr->pattern, tmp->data) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
1393
      {
Thomas Roessler's avatar
Thomas Roessler committed
1394 1395
	int not = 0;

1396 1397
	/* $mask is case-sensitive */
	if (mutt_strcmp (MuttVars[idx].option, "mask") != 0)
Thomas Roessler's avatar
Thomas Roessler committed
1398
	  flags |= mutt_which_case (tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1399 1400

	p = tmp->data;
1401
	if (mutt_strcmp (MuttVars[idx].option, "mask") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1402 1403 1404 1405 1406 1407 1408 1409
	{
	  if (*p == '!')
	  {
	    not = 1;
	    p++;
	  }
	}
	  
Thomas Roessler's avatar
Thomas Roessler committed
1410
	rx = (regex_t *) safe_malloc (sizeof (regex_t));
Thomas Roessler's avatar
Thomas Roessler committed
1411
	if ((e = REGCOMP (rx, p, flags)) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
	{
	  regerror (e, rx, err->data, err->dsize);
	  regfree (rx);
	  FREE (&rx);
	  break;
	}

	/* get here only if everything went smootly */
	if (ptr->pattern)
	{
	  FREE (&ptr->pattern);
	  regfree ((regex_t *) ptr->rx);
	  FREE (&ptr->rx);
	}

	ptr->pattern = safe_strdup (tmp->data);
	ptr->rx = rx;
Thomas Roessler's avatar
Thomas Roessler committed
1429
	ptr->not = not;
Thomas Roessler's avatar
Thomas Roessler committed
1430

1431 1432
	/* $reply_regexp and $alterantes require special treatment */
	
Thomas Roessler's avatar
Thomas Roessler committed
1433
	if (Context && Context->msgcount &&
1434
	    mutt_strcmp (MuttVars[idx].option, "reply_regexp") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 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 1484 1485
	{
	  regmatch_t pmatch[1];
	  int i;
	  
#define CUR_ENV Context->hdrs[i]->env
	  for (i = 0; i < Context->msgcount; i++)
	  {
	    if (CUR_ENV && CUR_ENV->subject)
	    {
	      CUR_ENV->real_subj = (regexec (ReplyRegexp.rx,
				    CUR_ENV->subject, 1, pmatch, 0)) ?
				    CUR_ENV->subject : 
				    CUR_ENV->subject + pmatch[0].rm_eo;
	    }
	  }
#undef CUR_ENV
	}
      }
    }
    else if (DTYPE(MuttVars[idx].type) == DT_MAGIC)
    {
      if (query || *s->dptr != '=')
      {
	switch (DefaultMagic)
	{
	  case M_MBOX:
	    p = "mbox";
	    break;
	  case M_MMDF:
	    p = "MMDF";
	    break;
	  case M_MH:
	    p = "MH";
	    break;
	  case M_MAILDIR:
	    p = "Maildir";
	    break;
	  default:
	    p = "unknown";
	    break;
	}
	snprintf (err->data, err->dsize, "%s=%s", MuttVars[idx].option, p);
	break;
      }

      s->dptr++;

      /* copy the value of the string */
      mutt_extract_token (tmp, s, 0);
      if (mx_set_magic (tmp->data))
      {
1486
	snprintf (err->data, err->dsize, _("%s: invalid mailbox type"), tmp->data);
Thomas Roessler's avatar
Thomas Roessler committed
1487 1488 1489 1490 1491 1492 1493
	r = -1;
	break;
      }
    }
    else if (DTYPE(MuttVars[idx].type) == DT_NUM)
    {
      short *ptr = (short *) MuttVars[idx].data;
1494 1495
      int val;
      char *t;
Thomas Roessler's avatar
Thomas Roessler committed
1496