postpone.c 18.5 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2002,2012-2013 Michael R. Elkins <me@mutt.org>
3
 * Copyright (C) 1999-2002,2004 Thomas Roessler <roessler@does-not-exist.org>
4
 *
Thomas Roessler's avatar
Thomas Roessler committed
5 6 7 8
 *     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.
9
 *
Thomas Roessler's avatar
Thomas Roessler committed
10 11 12 13
 *     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.
14
 *
Thomas Roessler's avatar
Thomas Roessler committed
15 16
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
17
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
 */
Thomas Roessler's avatar
Thomas Roessler committed
19

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

Thomas Roessler's avatar
Thomas Roessler committed
24 25 26 27 28
#include "mutt.h"
#include "mutt_menu.h"
#include "mime.h"
#include "mailbox.h"
#include "mapping.h"
29
#include "sort.h"
30 31 32
#ifdef USE_IMAP
#include "imap.h"
#endif
33
#include "mutt_crypt.h"
Thomas Roessler's avatar
Thomas Roessler committed
34 35 36 37 38 39

#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

40
static const struct mapping_t PostponeHelp[] = {
41 42 43 44
  { N_("Exit"),  OP_EXIT },
  { N_("Del"),   OP_DELETE },
  { N_("Undel"), OP_UNDELETE },
  { N_("Help"),  OP_HELP },
45
  { NULL,        0 }
Thomas Roessler's avatar
Thomas Roessler committed
46 47 48 49
};



50
static short PostCount = 0;
Thomas Roessler's avatar
Thomas Roessler committed
51
static CONTEXT *PostContext = NULL;
52
static short UpdateNumPostponed = 0;
Thomas Roessler's avatar
Thomas Roessler committed
53

54 55 56 57 58
/* Return the number of postponed messages.
 * if force is 0, use a cached value if it is costly to get a fresh
 * count (IMAP) - else check.
 */
int mutt_num_postponed (int force)
Thomas Roessler's avatar
Thomas Roessler committed
59 60 61 62
{
  struct stat st;
  CONTEXT ctx;

63 64 65 66 67 68 69 70 71
  static time_t LastModify = 0;
  static char *OldPostponed = NULL;

  if (UpdateNumPostponed)
  {
    UpdateNumPostponed = 0;
    force = 1;
  }

72
  if (mutt_strcmp (Postponed, OldPostponed))
73
  {
74
    FREE (&OldPostponed);
75
    OldPostponed = safe_strdup (Postponed);
76 77 78 79 80 81 82
    LastModify = 0;
    force = 1;
  }

  if (!Postponed)
    return 0;

83 84
#ifdef USE_IMAP
  /* LastModify is useless for IMAP */
85
  if (mx_is_imap (Postponed))
86
  {
87 88
    if (force)
    {
Thomas Roessler's avatar
Thomas Roessler committed
89 90
      short newpc;

91
      newpc = imap_status (Postponed, 0);
Thomas Roessler's avatar
Thomas Roessler committed
92 93 94
      if (newpc >= 0)
      {
	PostCount = newpc;
95
	dprint (3, (debugfile, "mutt_num_postponed: %d postponed IMAP messages found.\n", PostCount));
Thomas Roessler's avatar
Thomas Roessler committed
96 97
      }
      else
98
	dprint (3, (debugfile, "mutt_num_postponed: using old IMAP postponed count.\n"));
99
    }
100 101 102 103
    return PostCount;
  }
#endif

104
  if (stat (Postponed, &st) == -1)
Thomas Roessler's avatar
Thomas Roessler committed
105 106 107 108
  {
     PostCount = 0;
     LastModify = 0;
     return (0);
109
  }
110 111

  if (S_ISDIR (st.st_mode))
Thomas Roessler's avatar
Thomas Roessler committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  {
    /* if we have a maildir mailbox, we need to stat the "new" dir */

    char buf[_POSIX_PATH_MAX];

    snprintf (buf, sizeof (buf), "%s/new", Postponed);
    if (access (buf, F_OK) == 0 && stat (buf, &st) == -1)
    {
      PostCount = 0;
      LastModify = 0;
      return 0;
    }
  }

  if (LastModify < st.st_mtime)
  {
    LastModify = st.st_mtime;

    if (access (Postponed, R_OK | F_OK) != 0)
      return (PostCount = 0);
132
    if (mx_open_mailbox (Postponed, MUTT_NOSORT | MUTT_QUIET, &ctx) == NULL)
Thomas Roessler's avatar
Thomas Roessler committed
133 134 135 136 137 138 139 140 141
      PostCount = 0;
    else
      PostCount = ctx.msgcount;
    mx_fastclose_mailbox (&ctx);
  }

  return (PostCount);
}

142 143 144 145 146
void mutt_update_num_postponed (void)
{
  UpdateNumPostponed = 1;
}

Thomas Roessler's avatar
Thomas Roessler committed
147 148 149 150
static void post_entry (char *s, size_t slen, MUTTMENU *menu, int entry)
{
  CONTEXT *ctx = (CONTEXT *) menu->data;

151
  _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
152
		     MUTT_FORMAT_ARROWCURSOR);
Thomas Roessler's avatar
Thomas Roessler committed
153 154 155 156 157 158
}

static HEADER *select_msg (void)
{
  MUTTMENU *menu;
  int i, done=0, r=-1;
159
  char helpstr[LONG_STRING];
160
  short orig_sort;
Thomas Roessler's avatar
Thomas Roessler committed
161

162
  menu = mutt_new_menu (MENU_POST);
Thomas Roessler's avatar
Thomas Roessler committed
163 164
  menu->make_entry = post_entry;
  menu->max = PostContext->msgcount;
165
  menu->title = _("Postponed Messages");
Thomas Roessler's avatar
Thomas Roessler committed
166 167
  menu->data = PostContext;
  menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
168
  mutt_push_current_menu (menu);
Thomas Roessler's avatar
Thomas Roessler committed
169

170 171 172 173 174
  /* The postponed mailbox is setup to have sorting disabled, but the global
   * Sort variable may indicate something different.   Sorting has to be
   * disabled while the postpone menu is being displayed. */
  orig_sort = Sort;
  Sort = SORT_ORDER;
175

Thomas Roessler's avatar
Thomas Roessler committed
176 177 178 179 180 181
  while (!done)
  {
    switch (i = mutt_menuLoop (menu))
    {
      case OP_DELETE:
      case OP_UNDELETE:
182
        /* should deleted draft messages be saved in the trash folder? */
183
	mutt_set_flag (PostContext, PostContext->hdrs[menu->current], MUTT_DELETE, (i == OP_DELETE) ? 1 : 0);
Thomas Roessler's avatar
Thomas Roessler committed
184 185 186 187 188 189 190 191
	PostCount = PostContext->msgcount - PostContext->deleted;
	if (option (OPTRESOLVE) && menu->current < menu->max - 1)
	{
	  menu->oldcurrent = menu->current;
	  menu->current++;
	  if (menu->current >= menu->top + menu->pagelen)
	  {
	    menu->top = menu->current;
192
	    menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
Thomas Roessler's avatar
Thomas Roessler committed
193 194 195 196 197
	  }
	  else
	    menu->redraw |= REDRAW_MOTION_RESYNCH;
	}
	else
198
	  menu->redraw |= REDRAW_CURRENT;
Thomas Roessler's avatar
Thomas Roessler committed
199 200 201 202 203 204 205 206 207 208 209 210 211
	break;

      case OP_GENERIC_SELECT_ENTRY:
	r = menu->current;
	done = 1;
	break;

      case OP_EXIT:
	done = 1;
	break;
    }
  }

212
  Sort = orig_sort;
213
  mutt_pop_current_menu (menu);
Thomas Roessler's avatar
Thomas Roessler committed
214 215 216 217 218 219 220 221 222 223
  mutt_menuDestroy (&menu);
  return (r > -1 ? PostContext->hdrs[r] : NULL);
}

/* args:
 *      ctx	Context info, used when recalling a message to which
 *              we reply.
 *	hdr	envelope/attachment info for recalled message
 *	cur	if message was a reply, `cur' is set to the message which
 *		`hdr' is in reply to
224 225
 *	fcc	fcc for the recalled message
 *	fcclen	max length of fcc
Thomas Roessler's avatar
Thomas Roessler committed
226 227 228 229 230 231
 *
 * return vals:
 *	-1		error/no messages
 *	0		normal exit
 *	SENDREPLY	recalled message is a reply
 */
232
int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size_t fcclen)
Thomas Roessler's avatar
Thomas Roessler committed
233 234 235 236 237 238
{
  HEADER *h;
  int code = SENDPOSTPONED;
  LIST *tmp;
  LIST *last = NULL;
  LIST *next;
239
  const char *p;
Thomas Roessler's avatar
Thomas Roessler committed
240 241 242 243 244
  int opt_delete;

  if (!Postponed)
    return (-1);

245
  if ((PostContext = mx_open_mailbox (Postponed, MUTT_NOSORT, NULL)) == NULL)
Thomas Roessler's avatar
Thomas Roessler committed
246 247
  {
    PostCount = 0;
248
    mutt_error _("No postponed messages.");
Thomas Roessler's avatar
Thomas Roessler committed
249 250
    return (-1);
  }
251

Thomas Roessler's avatar
Thomas Roessler committed
252 253 254
  if (! PostContext->msgcount)
  {
    PostCount = 0;
255
    mx_close_mailbox (PostContext, NULL);
256
    FREE (&PostContext);
257
    mutt_error _("No postponed messages.");
Thomas Roessler's avatar
Thomas Roessler committed
258 259 260 261 262 263 264 265 266 267
    return (-1);
  }

  if (PostContext->msgcount == 1)
  {
    /* only one message, so just use that one. */
    h = PostContext->hdrs[0];
  }
  else if ((h = select_msg ()) == NULL)
  {
268
    mx_close_mailbox (PostContext, NULL);
269
    FREE (&PostContext);
Thomas Roessler's avatar
Thomas Roessler committed
270 271 272
    return (-1);
  }

273
  if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0)
Thomas Roessler's avatar
Thomas Roessler committed
274
  {
275
    mx_fastclose_mailbox (PostContext);
276
    FREE (&PostContext);
277
    return (-1);
Thomas Roessler's avatar
Thomas Roessler committed
278 279 280
  }

  /* finished with this message, so delete it. */
281
  mutt_set_flag (PostContext, h, MUTT_DELETE, 1);
282
  mutt_set_flag (PostContext, h, MUTT_PURGE, 1);
Thomas Roessler's avatar
Thomas Roessler committed
283 284 285 286 287 288

  /* update the count for the status display */
  PostCount = PostContext->msgcount - PostContext->deleted;

  /* avoid the "purge deleted messages" prompt */
  opt_delete = quadoption (OPT_DELETE);
289
  set_quadoption (OPT_DELETE, MUTT_YES);
290
  mx_close_mailbox (PostContext, NULL);
Thomas Roessler's avatar
Thomas Roessler committed
291 292
  set_quadoption (OPT_DELETE, opt_delete);

293
  FREE (&PostContext);
Thomas Roessler's avatar
Thomas Roessler committed
294 295 296

  for (tmp = hdr->env->userhdrs; tmp; )
  {
297
    if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
298 299 300
    {
      if (ctx)
      {
301
	/* if a mailbox is currently open, look to see if the original message
Thomas Roessler's avatar
Thomas Roessler committed
302
	   the user attempted to reply to is in this mailbox */
303
	p = skip_email_wsp(tmp->data + 18);
304 305
	if (!ctx->id_hash)
	  ctx->id_hash = mutt_make_id_hash (ctx);
Thomas Roessler's avatar
Thomas Roessler committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	*cur = hash_find (ctx->id_hash, p);
      }

      /* Remove the X-Mutt-References: header field. */
      next = tmp->next;
      if (last)
	last->next = tmp->next;
      else
	hdr->env->userhdrs = tmp->next;
      tmp->next = NULL;
      mutt_free_list (&tmp);
      tmp = next;
      if (*cur)
	code |= SENDREPLY;
    }
321
    else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0)
322
    {
323
      p = skip_email_wsp(tmp->data + 11);
324
      strfcpy (fcc, p, fcclen);
325
      mutt_pretty_mailbox (fcc, fcclen);
326 327 328 329 330 331 332 333 334 335

      /* remove the X-Mutt-Fcc: header field */
      next = tmp->next;
      if (last)
	last->next = tmp->next;
      else
	hdr->env->userhdrs = tmp->next;
      tmp->next = NULL;
      mutt_free_list (&tmp);
      tmp = next;
336 337 338 339 340 341
     /* note that x-mutt-fcc was present.  we do this because we want to add a
      * default fcc if the header was missing, but preserve the request of the
      * user to not make a copy if the header field is present, but empty.
      * see http://dev.mutt.org/trac/ticket/3653
      */
      code |= SENDPOSTPONEDFCC;
342
    }
343 344
    else if ((WithCrypto & APPLICATION_PGP)
             && (mutt_strncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
345 346
						       * by old mutt versions
						       */
347
                 || mutt_strncmp ("X-Mutt-PGP:", tmp->data, 11) == 0))
Thomas Roessler's avatar
Thomas Roessler committed
348
    {
349 350
      hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1,
					    APPLICATION_PGP);
351
      hdr->security |= APPLICATION_PGP;
352

Thomas Roessler's avatar
Thomas Roessler committed
353 354 355 356 357 358 359 360 361 362
      /* remove the pgp field */
      next = tmp->next;
      if (last)
	last->next = tmp->next;
      else
	hdr->env->userhdrs = tmp->next;
      tmp->next = NULL;
      mutt_free_list (&tmp);
      tmp = next;
    }
363 364
    else if ((WithCrypto & APPLICATION_SMIME)
             && mutt_strncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0)
365
    {
366 367
      hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1,
					    APPLICATION_SMIME);
368
      hdr->security |= APPLICATION_SMIME;
369

370 371 372 373 374 375 376 377 378 379 380
      /* remove the smime field */
      next = tmp->next;
      if (last)
	last->next = tmp->next;
      else
	hdr->env->userhdrs = tmp->next;
      tmp->next = NULL;
      mutt_free_list (&tmp);
      tmp = next;
    }

381 382 383 384 385
#ifdef MIXMASTER
    else if (mutt_strncmp ("X-Mutt-Mix:", tmp->data, 11) == 0)
    {
      char *t;
      mutt_free_list (&hdr->chain);
386

387 388 389 390 391 392
      t = strtok (tmp->data + 11, " \t\n");
      while (t)
      {
	hdr->chain = mutt_add_list (hdr->chain, t);
	t = strtok (NULL, " \t\n");
      }
393

394
      next = tmp->next;
395
      if (last)
396 397 398 399 400 401 402 403
	last->next = tmp->next;
      else
	hdr->env->userhdrs = tmp->next;
      tmp->next = NULL;
      mutt_free_list (&tmp);
      tmp = next;
    }
#endif
Thomas Roessler's avatar
Thomas Roessler committed
404 405 406 407 408 409 410

    else
    {
      last = tmp;
      tmp = tmp->next;
    }
  }
411 412 413 414

  if (option (OPTCRYPTOPPORTUNISTICENCRYPT))
    crypt_opportunistic_encrypt (hdr);

Thomas Roessler's avatar
Thomas Roessler committed
415 416 417 418 419
  return (code);
}



420
int mutt_parse_crypt_hdr (const char *p, int set_empty_signas, int crypt_app)
Thomas Roessler's avatar
Thomas Roessler committed
421
{
422
  char smime_cryptalg[LONG_STRING] = "\0";
423 424
  char sign_as[LONG_STRING] = "\0", *q;
  int flags = 0;
425 426 427

  if (!WithCrypto)
    return 0;
428

429
  p = skip_email_wsp(p);
Thomas Roessler's avatar
Thomas Roessler committed
430
  for (; *p; p++)
431 432
  {

Thomas Roessler's avatar
Thomas Roessler committed
433 434 435 436
    switch (*p)
    {
      case 'e':
      case 'E':
437
        flags |= ENCRYPT;
Thomas Roessler's avatar
Thomas Roessler committed
438 439
        break;

440 441 442 443 444
      case 'o':
      case 'O':
        flags |= OPPENCRYPT;
        break;

445
      case 's':
Thomas Roessler's avatar
Thomas Roessler committed
446
      case 'S':
447 448
        flags |= SIGN;
        q = sign_as;
449

Thomas Roessler's avatar
Thomas Roessler committed
450 451
        if (*(p+1) == '<')
        {
452
          for (p += 2;
453
	       *p && *p != '>' && q < sign_as + sizeof (sign_as) - 1;
Thomas Roessler's avatar
Thomas Roessler committed
454 455 456 457 458
               *q++ = *p++)
	    ;

          if (*p!='>')
          {
459
            mutt_error _("Illegal crypto header");
Thomas Roessler's avatar
Thomas Roessler committed
460 461 462
            return 0;
          }
        }
463

Thomas Roessler's avatar
Thomas Roessler committed
464 465 466
        *q = '\0';
        break;

467
      /* This used to be the micalg parameter.
468
       *
469 470 471
       * It's no longer needed, so we just skip the parameter in order
       * to be able to recall old messages.
       */
Thomas Roessler's avatar
Thomas Roessler committed
472 473 474
      case 'm':
      case 'M':
        if(*(p+1) == '<')
475 476
        {
	  for (p += 2; *p && *p != '>'; p++)
Thomas Roessler's avatar
Thomas Roessler committed
477 478 479
	    ;
	  if(*p != '>')
	  {
480
	    mutt_error _("Illegal crypto header");
Thomas Roessler's avatar
Thomas Roessler committed
481 482 483 484 485
	    return 0;
	  }
	}

	break;
486 487


488 489 490
      case 'c':
      case 'C':
   	q = smime_cryptalg;
491

492 493 494 495 496
        if(*(p+1) == '<')
	{
	  for(p += 2; *p && *p != '>' && q < smime_cryptalg + sizeof(smime_cryptalg) - 1;
	      *q++ = *p++)
	    ;
497

498 499 500 501 502 503 504 505 506 507
	  if(*p != '>')
	  {
	    mutt_error _("Illegal S/MIME header");
	    return 0;
	  }
	}

	*q = '\0';
	break;

508 509
      case 'i':
      case 'I':
510
	flags |= INLINE;
511 512
	break;

Thomas Roessler's avatar
Thomas Roessler committed
513
      default:
514
        mutt_error _("Illegal crypto header");
Thomas Roessler's avatar
Thomas Roessler committed
515 516
        return 0;
    }
517

Thomas Roessler's avatar
Thomas Roessler committed
518
  }
519

520
  /* the cryptalg field must not be empty */
521
  if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
522 523
    mutt_str_replace (&SmimeCryptAlg, smime_cryptalg);

524 525 526
  /* Set {Smime,Pgp}SignAs, if desired. */

  if ((WithCrypto & APPLICATION_PGP) && (crypt_app == APPLICATION_PGP)
527 528
      && (flags & SIGN)
      && (set_empty_signas || *sign_as))
529 530 531
    mutt_str_replace (&PgpSignAs, sign_as);

  if ((WithCrypto & APPLICATION_SMIME) && (crypt_app == APPLICATION_SMIME)
532 533
      && (flags & SIGN)
      && (set_empty_signas || *sign_as))
534
    mutt_str_replace (&SmimeSignAs, sign_as);
Thomas Roessler's avatar
Thomas Roessler committed
535

536
  return flags;
Thomas Roessler's avatar
Thomas Roessler committed
537
}
538 539


540 541 542 543 544 545 546 547 548
/* args:
 *     fp      If not NULL, file containing the template
 *     ctx     If fp is NULL, the context containing the header with the template
 *     newhdr  The template is read into this HEADER
 *     hdr     The message to recall/resend
 *     resend  Set if resending (as opposed to recalling a postponed msg).
 *             Resent messages enable header weeding, and also
 *             discard any existing Message-ID and Mail-Followup-To.
 */
549
int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER *newhdr, HEADER *hdr,
550
                           short resend)
551
{
552
  MESSAGE *msg = NULL;
553
  char file[_POSIX_PATH_MAX];
554
  BODY *b;
555
  FILE *bfp;
556 557
  int rv = -1;
  STATE s;
558
  int sec_type;
559

560
  memset (&s, 0, sizeof (s));
561

562
  if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
563 564
    return (-1);

565 566
  if (!fp) fp = msg->fp;

567 568 569 570
  bfp = fp;

  /* parse the message header and MIME structure */

571
  fseeko (fp, hdr->offset, 0);
572
  newhdr->offset = hdr->offset;
573 574
  /* enable header weeding for resent messages */
  newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, resend);
575 576
  newhdr->content->length = hdr->content->length;
  mutt_parse_part (fp, newhdr->content);
577

578 579 580
  /* If resending a message, don't keep message_id or mail_followup_to.
   * Otherwise, we are resuming a postponed message, and want to keep those
   * headers if they exist.
581
   */
582
  if (resend)
583 584 585 586
  {
    FREE (&newhdr->env->message_id);
    FREE (&newhdr->env->mail_followup_to);
  }
587

588
  /* decrypt pgp/mime encoded messages */
589

590 591
  if ((WithCrypto & APPLICATION_PGP) &&
      (sec_type = mutt_is_multipart_encrypted (newhdr->content)))
592
  {
593 594
    newhdr->security |= sec_type;
    if (!crypt_valid_passphrase (sec_type))
595
      goto bail;
596

597
    mutt_message _("Decrypting message...");
598
    if ((crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
599
	|| b == NULL)
600
    {
601
      mutt_error _("Decryption failed.");
602
      goto bail;
603
    }
604

605 606 607 608
    mutt_free_body (&newhdr->content);
    newhdr->content = b;

    mutt_clear_error ();
609
  }
610

611
  /*
612
   * remove a potential multipart/signed layer - useful when
613
   * resending messages
614
   */
615

616
  if (WithCrypto && mutt_is_multipart_signed (newhdr->content))
617
  {
618
    newhdr->security |= SIGN;
619 620
    if ((WithCrypto & APPLICATION_PGP)
        && ascii_strcasecmp (mutt_get_parameter ("protocol", newhdr->content->parameter), "application/pgp-signature") == 0)
621
      newhdr->security |= APPLICATION_PGP;
622
    else if ((WithCrypto & APPLICATION_SMIME))
623
      newhdr->security |= APPLICATION_SMIME;
624

625
    /* destroy the signature */
626 627
    mutt_free_body (&newhdr->content->parts->next);
    newhdr->content = mutt_remove_multipart (newhdr->content);
628
  }
629

630

631
  /*
632 633
   * We don't need no primary multipart.
   * Note: We _do_ preserve messages!
634 635
   *
   * XXX - we don't handle multipart/alternative in any
636 637
   * smart way when sending messages.  However, one may
   * consider this a feature.
638
   *
639
   */
640 641 642 643

  if (newhdr->content->type == TYPEMULTIPART)
    newhdr->content = mutt_remove_multipart (newhdr->content);

644
  s.fpin = bfp;
645

646
  /* create temporary files for all attachments */
647
  for (b = newhdr->content; b; b = b->next)
648
  {
649

650 651 652 653
    /* what follows is roughly a receive-mode variant of
     * mutt_get_tmp_attachment () from muttlib.c
     */

654
    file[0] = '\0';
655
    if (b->filename)
656
    {
657
      strfcpy (file, b->filename, sizeof (file));
658 659
      b->d_filename = safe_strdup (b->filename);
    }
660
    else
661
    {
662 663
      /* avoid Content-Disposition: header with temporary filename */
      b->use_disp = 0;
664
    }
665 666 667 668 669 670

    /* set up state flags */

    s.flags = 0;

    if (b->type == TYPETEXT)
671
    {
672
      if (!ascii_strcasecmp ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
673 674 675
	b->noconv = 1;
      else
      {
676
	s.flags |= MUTT_CHARCONV;
677 678
	b->noconv = 0;
      }
679

680 681
      mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
    }
682

683
    mutt_adv_mktemp (file, sizeof(file));
684 685 686
    if ((s.fpout = safe_fopen (file, "w")) == NULL)
      goto bail;

687

688 689
    if ((WithCrypto & APPLICATION_PGP) &&
	((sec_type = mutt_is_application_pgp (b)) & (ENCRYPT|SIGN)))
690
    {
691 692 693 694 695 696 697 698 699 700 701 702
      if (sec_type & ENCRYPT)
      {
        if (!crypt_valid_passphrase (APPLICATION_PGP))
          goto bail;
        mutt_message _("Decrypting message...");
      }

      if (mutt_body_handler (b, &s) < 0)
      {
        mutt_error _("Decryption failed.");
        goto bail;
      }
703

704
      newhdr->security |= sec_type;
705 706 707 708 709

      b->type = TYPETEXT;
      mutt_str_replace (&b->subtype, "plain");
      mutt_delete_parameter ("x-action", &b->parameter);
    }
710 711 712
    else if ((WithCrypto & APPLICATION_SMIME) &&
             ((sec_type = mutt_is_application_smime (b)) & (ENCRYPT|SIGN)))
    {
713 714 715 716 717 718 719 720 721 722 723 724 725
      if (sec_type & ENCRYPT)
      {
        if (!crypt_valid_passphrase (APPLICATION_SMIME))
          goto bail;
        crypt_smime_getkeys (newhdr->env);
        mutt_message _("Decrypting message...");
      }

      if (mutt_body_handler (b, &s) < 0)
      {
        mutt_error _("Decryption failed.");
        goto bail;
      }
726 727 728 729 730

      newhdr->security |= sec_type;
      b->type = TYPETEXT;
      mutt_str_replace (&b->subtype, "plain");
    }
731 732
    else
      mutt_decode_attachment (b, &s);
733

734 735 736
    if (safe_fclose (&s.fpout) != 0)
      goto bail;

737
    mutt_str_replace (&b->filename, file);
738
    b->unlink = 1;
739

740
    mutt_stamp_attachment (b);
741

742
    mutt_free_body (&b->parts);
743
    if (b->hdr) b->hdr->content = NULL; /* avoid dangling pointer */
744 745
  }

746
  /* Fix encryption flags. */
747

748 749 750
  /* No inline if multipart. */
  if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
    newhdr->security &= ~INLINE;
751

752 753
  /* Do we even support multiple mechanisms? */
  newhdr->security &= WithCrypto | ~(APPLICATION_PGP|APPLICATION_SMIME);
754

755 756 757 758 759 760 761 762 763
  /* Theoretically, both could be set. Take the one the user wants to set by default. */
  if ((newhdr->security & APPLICATION_PGP) && (newhdr->security & APPLICATION_SMIME))
  {
    if (option (OPTSMIMEISDEFAULT))
      newhdr->security &= ~APPLICATION_PGP;
    else
      newhdr->security &= ~APPLICATION_SMIME;
  }

764
  rv = 0;
765

766
  bail:
767

768
  /* that's it. */
769
  if (bfp != fp) safe_fclose (&bfp);
770
  if (msg) mx_close_message (ctx, &msg);
771

772 773 774 775 776
  if (rv == -1)
  {
    mutt_free_envelope (&newhdr->env);
    mutt_free_body (&newhdr->content);
  }
777

778
  return rv;
779
}