init.c 86.5 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2002,2010,2013,2016 Michael R. Elkins <me@mutt.org>
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
#include "mutt_curses.h"
26
#include "mutt_menu.h"
Thomas Roessler's avatar
Thomas Roessler committed
27
#include "mutt_regex.h"
Thomas Roessler's avatar
Thomas Roessler committed
28
#include "history.h"
29
#include "keymap.h"
30
#include "mbyte.h"
31
#include "charset.h"
32
#include "mutt_crypt.h"
33
#include "mutt_idna.h"
34
#include "group.h"
35

36
#if defined(USE_SSL)
37
#include "mutt_ssl.h"
38 39 40
#endif


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

#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>
53
#include <sys/time.h>
Thomas Roessler's avatar
Thomas Roessler committed
54

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

64 65
typedef struct myvar
{
66 67
  char *name;
  char *value;
68 69 70 71 72
  struct myvar* next;
} myvar_t;

static myvar_t* MyVars;

73 74
static int var_to_string (int idx, char* val, size_t len);

75 76 77 78
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);

79 80
extern char **envlist;

81
static void toggle_quadoption (int opt)
82 83 84 85 86 87
{
  int n = opt/4;
  int b = (opt % 4) * 2;

  QuadOptions[n] ^= (1 << b);
}
Thomas Roessler's avatar
Thomas Roessler committed
88 89 90

void set_quadoption (int opt, int flag)
{
91 92 93 94 95
  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
96 97 98 99
}

int quadoption (int opt)
{
100 101 102 103
  int n = opt/4;
  int b = (opt % 4) * 2;

  return (QuadOptions[n] >> b) & 0x3;
Thomas Roessler's avatar
Thomas Roessler committed
104 105 106 107 108 109 110 111
}

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

  switch (v)
  {
Derek Martin's avatar
Derek Martin committed
112 113
    case MUTT_YES:
    case MUTT_NO:
Thomas Roessler's avatar
Thomas Roessler committed
114 115 116
      return (v);

    default:
Derek Martin's avatar
Derek Martin committed
117
      v = mutt_yesorno (prompt, (v == MUTT_ASKYES));
118
      mutt_window_clearline (MuttMessageWindow, 0);
Thomas Roessler's avatar
Thomas Roessler committed
119 120 121 122 123 124 125 126
      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.  */
127
static int mutt_option_index (char *s)
Thomas Roessler's avatar
Thomas Roessler committed
128 129 130 131
{
  int i;

  for (i = 0; MuttVars[i].option; i++)
132
    if (mutt_strcmp (s, MuttVars[i].option) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
      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)
    {
Derek Martin's avatar
Derek Martin committed
151 152 153 154 155
      if ((ISSPACE (ch) && !(flags & MUTT_TOKEN_SPACE)) ||
	  (ch == '#' && !(flags & MUTT_TOKEN_COMMENT)) ||
	  (ch == '=' && (flags & MUTT_TOKEN_EQUAL)) ||
	  (ch == ';' && !(flags & MUTT_TOKEN_SEMICOLON)) ||
	  ((flags & MUTT_TOKEN_PATTERN) && strchr ("~%=!|", ch)))
Thomas Roessler's avatar
Thomas Roessler committed
156 157 158 159 160 161 162
	break;
    }

    tok->dptr++;

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

200
	    mutt_buffer_addch (dest, (ch << 6) + (*tok->dptr << 3) + *(tok->dptr + 1) - 3504);
Thomas Roessler's avatar
Thomas Roessler committed
201 202 203
	    tok->dptr += 2;
	  }
	  else
204
	    mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
205 206
      }
    }
Derek Martin's avatar
Derek Martin committed
207
    else if (ch == '^' && (flags & MUTT_TOKEN_CONDENSE))
Thomas Roessler's avatar
Thomas Roessler committed
208
    {
209 210
	if (!*tok->dptr)
	    return -1; /* premature end of token */
Thomas Roessler's avatar
Thomas Roessler committed
211 212
      ch = *tok->dptr++;
      if (ch == '^')
213
	mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
214
      else if (ch == '[')
215
	mutt_buffer_addch (dest, '\033');
Thomas Roessler's avatar
Thomas Roessler committed
216
      else if (isalpha ((unsigned char) ch))
217
	mutt_buffer_addch (dest, toupper ((unsigned char) ch) - '@');
Thomas Roessler's avatar
Thomas Roessler committed
218 219
      else
      {
220 221
	mutt_buffer_addch (dest, '^');
	mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      }
    }
    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)
      {
244
	dprint (1, (debugfile, "mutt_get_token: mismatched backticks\n"));
Thomas Roessler's avatar
Thomas Roessler committed
245 246 247 248 249 250
	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));
251
	FREE (&cmd);
Thomas Roessler's avatar
Thomas Roessler committed
252 253 254 255 256 257 258
	return (-1);
      }
      FREE (&cmd);

      tok->dptr = pc + 1;

      /* read line */
259
      mutt_buffer_init (&expn);
260
      expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line, 0);
261
      safe_fclose (&fp);
Thomas Roessler's avatar
Thomas Roessler committed
262 263
      mutt_wait_filter (pid);

Michael Elkins's avatar
Michael Elkins committed
264
      /* if we got output, make a new string consisting of the shell output
Thomas Roessler's avatar
Thomas Roessler committed
265
	 plus whatever else was left on the original line */
266 267 268 269 270 271 272
      /* 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
273
      else if (expn.data)
Thomas Roessler's avatar
Thomas Roessler committed
274
      {
275 276
	expnlen = mutt_strlen (expn.data);
	tok->dsize = expnlen + mutt_strlen (tok->dptr) + 1;
Thomas Roessler's avatar
Thomas Roessler committed
277 278
	ptr = safe_malloc (tok->dsize);
	memcpy (ptr, expn.data, expnlen);
279
	strcpy (ptr + expnlen, tok->dptr);	/* __STRCPY_CHECKED__ */
Thomas Roessler's avatar
Thomas Roessler committed
280 281 282 283 284 285 286 287 288
	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
289
    else if (ch == '$' && (!qc || qc == '"') && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr)))
Thomas Roessler's avatar
Thomas Roessler committed
290
    {
291 292 293
      const char *env = NULL;
      char *var = NULL;
      int idx;
Thomas Roessler's avatar
Thomas Roessler committed
294 295 296 297 298 299 300 301 302 303 304 305

      if (*tok->dptr == '{')
      {
	tok->dptr++;
	if ((pc = strchr (tok->dptr, '}')))
	{
	  var = mutt_substrdup (tok->dptr, pc);
	  tok->dptr = pc + 1;
	}
      }
      else
      {
306
	for (pc = tok->dptr; isalnum ((unsigned char) *pc) || *pc == '_'; pc++)
Thomas Roessler's avatar
Thomas Roessler committed
307 308 309 310
	  ;
	var = mutt_substrdup (tok->dptr, pc);
	tok->dptr = pc;
      }
311 312 313 314 315 316 317 318 319 320 321 322 323 324
      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
325 326
    }
    else
327
      mutt_buffer_addch (dest, ch);
Thomas Roessler's avatar
Thomas Roessler committed
328
  }
329
  mutt_buffer_addch (dest, 0); /* terminate the string */
Thomas Roessler's avatar
Thomas Roessler committed
330 331 332 333
  SKIPWS (tok->dptr);
  return 0;
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
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:
354
    FREE ((char**)p->data);		/* __FREE_CHECKED__ */
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    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);
}

376
static void add_to_list (LIST **list, const char *str)
Thomas Roessler's avatar
Thomas Roessler committed
377 378 379
{
  LIST *t, *last = NULL;

380 381 382 383
  /* don't add a NULL or empty string to the list */
  if (!str || *str == '\0')
    return;

384 385
  /* 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
386
  {
387
    if (ascii_strcasecmp (str, last->data) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
388
    {
389 390 391
      /* already on the list, so just ignore it */
      last = NULL;
      break;
Thomas Roessler's avatar
Thomas Roessler committed
392
    }
393 394 395
    if (!last->next)
      break;
  }
Thomas Roessler's avatar
Thomas Roessler committed
396

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

408
int mutt_add_to_rx_list (RX_LIST **list, const char *s, int flags, BUFFER *err)
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
{
  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;
    else
442
      *list = t;
443 444 445 446 447 448 449
  }
  else /* duplicate */
    mutt_free_regexp (&rx);

  return 0;
}

450
static int remove_from_replace_list (REPLACE_LIST **list, const char *pat);
451

452
static int add_to_replace_list (REPLACE_LIST **list, const char *pat, const char *templ, BUFFER *err)
453
{
454
  REPLACE_LIST *t = NULL, *last = NULL;
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  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)
    {
473 474 475 476 477 478 479
      /* 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;
480
      FREE(&t->template);
481 482 483 484 485 486
      break;
    }
    if (!last->next)
      break;
  }

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

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

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

  if (t->nmatch > t->rx->rx->re_nsub)
  {
521
    snprintf (err->data, err->dsize, "%s", _("Not enough subexpressions for "
522
                                       "template"));
523
    remove_from_replace_list(list, pat);
524 525 526 527
    return -1;
  }

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

529 530 531
  return 0;
}

532
static int remove_from_replace_list (REPLACE_LIST **list, const char *pat)
533
{
534
  REPLACE_LIST *cur, *prev;
535 536 537
  int nremoved = 0;

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

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

566
  return nremoved;
567 568 569
}


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

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

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 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
static void free_mbchar_table (mbchar_table **t)
{
  if (!t || !*t)
    return;

  FREE (&(*t)->chars);
  FREE (&(*t)->segmented_str);
  FREE (&(*t)->orig_str);
  FREE (t);		/* __FREE_CHECKED__ */
}

static mbchar_table *parse_mbchar_table (const char *s)
{
  mbchar_table *t;
  size_t slen, k;
  mbstate_t mbstate;
  char *d;

  t = safe_calloc (1, sizeof (mbchar_table));
  slen = mutt_strlen (s);
  if (!slen)
    return t;

  t->orig_str = safe_strdup (s);
  /* This could be more space efficient.  However, being used on tiny
   * strings (Tochars and StChars), the overhead is not great. */
  t->chars = safe_calloc (slen, sizeof (char *));
  d = t->segmented_str = safe_calloc (slen * 2, sizeof (char));

  memset (&mbstate, 0, sizeof (mbstate));
  while (slen && (k = mbrtowc (NULL, s, slen, &mbstate)))
  {
    if (k == (size_t)(-1) || k == (size_t)(-2))
    {
      dprint (1, (debugfile,
                  "parse_mbchar_table: mbrtowc returned %d converting %s in %s\n",
                  (k == (size_t)(-1)) ? -1 : -2,
                  s, t->orig_str));
      if (k == (size_t)(-1))
        memset (&mbstate, 0, sizeof (mbstate));
      k = (k == (size_t)(-1)) ? 1 : slen;
    }

    slen -= k;
    t->chars[t->len++] = d;
    while (k--)
      *d++ = *s++;
    *d++ = '\0';
  }

  return t;
}

Thomas Roessler's avatar
Thomas Roessler committed
653 654
static int parse_unignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
655 656 657
  do
  {
    mutt_extract_token (buf, s, 0);
658 659 660 661 662

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

663 664 665 666
    remove_from_list (&Ignore, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
667 668 669 670 671
  return 0;
}

static int parse_ignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
672 673 674 675 676 677 678 679
  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
680 681 682 683 684
  return 0;
}

static int parse_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
685 686 687 688 689 690 691
  do
  {
    mutt_extract_token (buf, s, 0);
    add_to_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
692 693 694
  return 0;
}

695 696 697 698 699 700 701 702 703 704 705 706
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)
{
707 708
  group_context_t *gc = NULL;
  
709
  _alternates_clean();
710

711 712 713
  do
  {
    mutt_extract_token (buf, s, 0);
714 715 716 717

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

718
    mutt_remove_from_rx_list (&UnAlternates, buf->data);
719

720 721 722 723 724
    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;
725 726
  }
  while (MoreArgs (s));
727 728
  
  mutt_group_context_destroy (&gc);
729
  return 0;
730 731 732 733
  
 bail:
  mutt_group_context_destroy (&gc);
  return -1;
734 735 736 737 738
}

static int parse_unalternates (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  _alternates_clean();
739 740 741
  do
  {
    mutt_extract_token (buf, s, 0);
742
    mutt_remove_from_rx_list (&Alternates, buf->data);
743 744

    if (mutt_strcmp (buf->data, "*") &&
745
	mutt_add_to_rx_list (&UnAlternates, buf->data, REG_ICASE, err) != 0)
746 747 748 749 750 751
      return -1;

  }
  while (MoreArgs (s));

  return 0;
752 753
}

754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
static int parse_replace_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  REPLACE_LIST **list = (REPLACE_LIST **)data;
  BUFFER templ;

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

  /* First token is a regexp. */
  if (!MoreArgs(s))
  {
    strfcpy(err->data, _("not enough arguments"), err->dsize);
    return -1;
  }
  mutt_extract_token(buf, s, 0);

  /* Second token is a replacement template */
  if (!MoreArgs(s))
  {
    strfcpy(err->data, _("not enough arguments"), err->dsize);
    return -1;
  }
  mutt_extract_token(&templ, s, 0);

  if (add_to_replace_list(list, buf->data, templ.data, err) != 0) {
    FREE(&templ.data);
    return -1;
  }
  FREE(&templ.data);

  return 0;
}

static int parse_unreplace_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  REPLACE_LIST **list = (REPLACE_LIST **)data;

  /* First token is a regexp. */
  if (!MoreArgs(s))
  {
    strfcpy(err->data, _("not enough arguments"), err->dsize);
    return -1;
  }

  mutt_extract_token(buf, s, 0);
798 799 800 801 802 803 804 805

  /* "*" is a special case. */
  if (!mutt_strcmp (buf->data, "*"))
  {
    mutt_free_replace_list (list);
    return 0;
  }

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
  remove_from_replace_list(list, buf->data);
  return 0;
}


static void clear_subject_mods (void)
{
  int i;
  if (Context && Context->msgcount) 
  {
    for (i = 0; i < Context->msgcount; i++)
      FREE(&Context->hdrs[i]->env->disp_subj);
  }
}


static int parse_subjectrx_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  int rc;

  rc = parse_replace_list(buf, s, data, err);
  if (rc == 0)
    clear_subject_mods();
  return rc;
}


static int parse_unsubjectrx_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  int rc;

  rc = parse_unreplace_list(buf, s, data, err);
  if (rc == 0)
    clear_subject_mods();
  return rc;
}


844 845 846 847
static int parse_spam_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  BUFFER templ;

848
  mutt_buffer_init (&templ);
849

850
  /* Insist on at least one parameter */
851 852
  if (!MoreArgs(s))
  {
Derek Martin's avatar
Derek Martin committed
853
    if (data == MUTT_SPAM)
854 855 856
      strfcpy(err->data, _("spam: no matching pattern"), err->dsize);
    else
      strfcpy(err->data, _("nospam: no matching pattern"), err->dsize);
857 858
    return -1;
  }
859 860

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

Derek Martin's avatar
Derek Martin committed
863 864
  /* data should be either MUTT_SPAM or MUTT_NOSPAM. MUTT_SPAM is for spam commands. */
  if (data == MUTT_SPAM)
865
  {
866 867 868 869 870 871
    /* 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. */
872
      if (add_to_replace_list (&SpamList, buf->data, templ.data, err) != 0) {
873
	  FREE(&templ.data);
874
          return -1;
875 876
      }
      FREE(&templ.data);
877 878 879 880 881
    }

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

    return 0;
886
  }
887

Derek Martin's avatar
Derek Martin committed
888 889
  /* MUTT_NOSPAM is for nospam commands. */
  else if (data == MUTT_NOSPAM)
890
  {
891
    /* nospam only ever has one parameter. */
892

893 894 895
    /* "*" is a special case. */
    if (!mutt_strcmp(buf->data, "*"))
    {
896
      mutt_free_replace_list (&SpamList);
897 898 899 900 901
      mutt_free_rx_list (&NoSpamList);
      return 0;
    }

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

    /* Otherwise, add it to the nospam list. */
906
    if (mutt_add_to_rx_list (&NoSpamList, buf->data, REG_ICASE, err) != 0)
907
      return -1;
908 909 910 911 912 913 914

    return 0;
  }

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

917

Thomas Roessler's avatar
Thomas Roessler committed
918 919
static int parse_unlist (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
920 921 922
  do
  {
    mutt_extract_token (buf, s, 0);
923 924 925 926 927 928 929 930
    /*
     * Check for deletion of entire list
     */
    if (mutt_strcmp (buf->data, "*") == 0)
    {
      mutt_free_list ((LIST **) data);
      break;
    }
931 932 933 934
    remove_from_list ((LIST **) data, buf->data);
  }
  while (MoreArgs (s));

Thomas Roessler's avatar
Thomas Roessler committed
935 936 937
  return 0;
}

938 939 940 941 942 943
/* These two functions aren't sidebar-specific, but they are currently only
 * used by the sidebar_whitelist/unsidebar_whitelist commands.
 * Putting in an #ifdef to silence an unused function warning when the sidebar
 * is disabled.
 */
#ifdef USE_SIDEBAR
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
static int parse_path_list (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  char path[_POSIX_PATH_MAX];

  do
  {
    mutt_extract_token (buf, s, 0);
    strfcpy (path, buf->data, sizeof (path));
    mutt_expand_path (path, sizeof (path));
    add_to_list ((LIST **) data, path);
  }
  while (MoreArgs (s));

  return 0;
}

static int parse_path_unlist (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  char path[_POSIX_PATH_MAX];

  do
  {
    mutt_extract_token (buf, s, 0);
    /*
     * Check for deletion of entire list
     */
    if (mutt_strcmp (buf->data, "*") == 0)
    {
      mutt_free_list ((LIST **) data);
      break;
    }
    strfcpy (path, buf->data, sizeof (path));
    mutt_expand_path (path, sizeof (path));
    remove_from_list ((LIST **) data, path);
  }
  while (MoreArgs (s));

  return 0;
}
983
#endif /* USE_SIDEBAR */
984

985 986
static int parse_lists (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
987 988
  group_context_t *gc = NULL;

989 990 991
  do
  {
    mutt_extract_token (buf, s, 0);
992 993 994 995
    
    if (parse_group_context (&gc, buf, s, data, err) == -1)
      goto bail;
    
996
    mutt_remove_from_rx_list (&UnMailLists, buf->data);
997
    
998 999 1000 1001 1002
    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;
1003 1004 1005
  }
  while (MoreArgs (s));

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
  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;
1024 1025

  do
1026 1027 1028 1029
  {
    mutt_extract_token (buf, s, 0);
    if (parse_group_context (&gc, buf, s, data, err) == -1)
      goto bail;
1030

Derek Martin's avatar
Derek Martin committed
1031
    if (data == MUTT_UNGROUP && !mutt_strcasecmp (buf->data, "*"))
1032
    {
1033
      if (mutt_group_context_clear (&gc) < 0)
1034 1035 1036 1037
	goto bail;
      goto out;
    }

1038 1039 1040 1041
    if (!mutt_strcasecmp (buf->data, "-rx"))
      state = RX;
    else if (!mutt_strcasecmp (buf->data, "-addr"))
      state = ADDR;
1042
    else
1043
    {
1044
      switch (state)
1045 1046
      {
	case NONE:
1047
	  snprintf (err->data, err->dsize, _("%sgroup: missing -rx or -addr."),
Derek Martin's avatar
Derek Martin committed
1048
		   data == MUTT_UNGROUP ? "un" : "");
1049
	  goto bail;
1050

1051
	case RX:
Derek Martin's avatar
Derek Martin committed
1052
	  if (data == MUTT_GROUP &&
1053 1054
	      mutt_group_context_add_rx (gc, buf->data, REG_ICASE, err) != 0)
	    goto bail;
Derek Martin's avatar
Derek Martin committed
1055
	  else if (data == MUTT_UNGROUP &&
1056
		   mutt_group_context_remove_rx (gc, buf->data) < 0)
1057 1058
	    goto bail;
	  break;
1059

1060 1061 1062
	case ADDR:
	  if ((addr = mutt_parse_adrlist (NULL, buf->data)) == NULL)
	    goto bail;
1063
	  if (mutt_addrlist_to_intl (addr, &estr))
1064 1065 1066
	  { 
	    snprintf (err->data, err->dsize, _("%sgroup: warning: bad IDN '%s'.\n"),
		      data == 1 ? "un" : "", estr);
1067 1068
	    goto bail;
	  }
Derek Martin's avatar
Derek Martin committed
1069
	  if (data == MUTT_GROUP)
1070
	    mutt_group_context_add_adrlist (gc, addr);
Derek Martin's avatar
Derek Martin committed
1071
	  else if (data == MUTT_UNGROUP)
1072
	    mutt_group_context_remove_adrlist (gc, addr);
1073 1074 1075 1076 1077 1078
	  rfc822_free_address (&addr);
	  break;
      }
    }
  } while (MoreArgs (s));

1079
out:
1080
  mutt_group_context_destroy (&gc);
1081
  return 0;
1082

1083
bail:
1084 1085 1086 1087
  mutt_group_context_destroy (&gc);
  return -1;
}

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
/* 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;
1106
  int ret;
1107 1108 1109

  /* Find the last item in the list that data points to. */
  lastp = NULL;
1110 1111
  dprint(5, (debugfile, "parse_attach_list: ldata = %p, *ldata = %p\n",
	      (void *)ldata, (void *)*ldata));
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
  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 */
1130
    if (!ascii_strcasecmp(buf->data, "any"))
1131
      a->major = safe_strdup("*/.*");
1132
    else if (!ascii_strcasecmp(buf->data, "none"))
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
      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);
1156
    ret = REGCOMP(&a->minor_rx, tmpminor, REG_ICASE);
1157

1158
    FREE(&tmpminor);
1159

1160 1161 1162 1163 1164 1165 1166 1167
    if (ret)
    {
      regerror(ret, &a->minor_rx, err->data, err->dsize);
      FREE(&a->major);
      FREE(&a);
      return -1;
    }

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
    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;
1193
  LIST *lp, *lastp, *newlp;
1194 1195 1196 1197 1198 1199 1200 1201
  char *tmp;
  int major;
  char *minor;

  do
  {
    mutt_extract_token (buf, s, 0);

1202
    if (!ascii_strcasecmp(buf->data, "any"))
1203
      tmp = safe_strdup("*/.*");
1204
    else if (!ascii_strcasecmp(buf->data, "none"))
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
      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);

1220 1221
    /* 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. */
1222
    lastp = NULL;
1223
    for(lp = *ldata; lp; )
1224 1225 1226 1227 1228 1229 1230 1231 1232
    {
      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);
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	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;
1246 1247 1248 1249 1250 1251 1252 1253 1254
      }

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

  }
  while (MoreArgs (s));
   
1255
  FREE(&tmp);
1256 1257 1258 1259
  _attachments_clean();
  return 0;
}

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
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;
}

1272 1273 1274

static int parse_attachments (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
1275
  char op, *category;
1276 1277 1278 1279 1280 1281 1282 1283
  LIST **listp;

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

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
  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");
    mutt_any_key_to_continue (NULL);
    return 0;
  }

1299 1300
  if (op != '+' && op != '-') {
    op = '+';
1301
    category--;
1302
  }
1303
  if (!ascii_strncasecmp(category, "attachment", strlen(category))) {
1304 1305 1306 1307 1308
    if (op == '+')
      listp = &AttachAllow;
    else
      listp = &AttachExclude;
  }
1309
  else if (!ascii_strncasecmp(category, "inline", strlen(category))) {
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
    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--;
  }
1340
  if (!ascii_strncasecmp(p, "attachment", strlen(p))) {
1341 1342 1343 1344 1345
    if (op == '+')
      listp = &AttachAllow;
    else
      listp = &AttachExclude;
  }
1346
  else if (!ascii_strncasecmp(p, "inline", strlen(p))) {
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    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);
}

1360 1361 1362 1363 1364
static int parse_unlists (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  do
  {
    mutt_extract_token (buf, s, 0);
1365 1366
    mutt_remove_from_rx_list (&SubscribedLists, buf->data);
    mutt_remove_from_rx_list (&MailLists, buf->data);
1367 1368
    
    if (mutt_strcmp (buf->data, "*") && 
1369
	mutt_add_to_rx_list (&UnMailLists, buf->data, REG_ICASE, err) != 0)
1370
      return -1;
1371 1372 1373 1374 1375 1376 1377 1378
  }
  while (MoreArgs (s));

  return 0;
}

static int parse_subscribe (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
1379 1380
  group_context_t *gc = NULL;
  
1381 1382 1383
  do
  {
    mutt_extract_token (buf, s, 0);
1384 1385 1386 1387

    if (parse_group_context (&gc, buf, s, data, err) == -1)
      goto bail;
    
1388 1389
    mutt_remove_from_rx_list (&UnMailLists, buf->data);
    mutt_remove_from_rx_list (&UnSubscribedLists, buf->data);
1390

1391 1392 1393 1394 1395 1396
    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;
1397 1398
  }
  while (MoreArgs (s));
1399 1400
  
  mutt_group_context_destroy (&gc);
1401
  return 0;
1402 1403 1404 1405
  
 bail:
  mutt_group_context_destroy (&gc);
  return -1;
1406
}
1407 1408 1409 1410 1411 1412

static int parse_unsubscribe (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
  do
  {
    mutt_extract_token (buf, s, 0);
1413
    mutt_remove_from_rx_list (&SubscribedLists, buf->data);
1414 1415
    
    if (mutt_strcmp (buf->data, "*") &&
1416
	mutt_add_to_rx_list (&UnSubscribedLists, buf->data, REG_ICASE, err) != 0)
1417