mx.c 30.9 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
3
 * Copyright (C) 1999-2000 Thomas Roessler <roessler@guug.de>
Thomas Roessler's avatar
Thomas Roessler committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 * 
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
17
 *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
Thomas Roessler's avatar
Thomas Roessler committed
18 19 20 21 22 23 24 25 26
 */ 

#include "mutt.h"
#include "mx.h"
#include "rfc2047.h"
#include "sort.h"
#include "mailbox.h"
#include "copy.h"
#include "keymap.h"
27
#include "url.h"
Thomas Roessler's avatar
Thomas Roessler committed
28

29
#ifdef HAVE_PGP
Thomas Roessler's avatar
Thomas Roessler committed
30 31 32
#include "pgp.h"
#endif

Thomas Roessler's avatar
Thomas Roessler committed
33 34 35
#ifdef USE_IMAP
#include "imap.h"
#endif
Thomas Roessler's avatar
Thomas Roessler committed
36 37 38 39 40

#ifdef BUFFY_SIZE
#include "buffy.h"
#endif

Thomas Roessler's avatar
Thomas Roessler committed
41 42 43 44
#ifdef USE_DOTLOCK
#include "dotlock.h"
#endif

Thomas Roessler's avatar
Thomas Roessler committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <dirent.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef BUFFY_SIZE
#include <utime.h>
#endif


59
#define mutt_is_spool(s)  (mutt_strcmp (Spoolfile, s) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
60 61 62 63 64 65 66

#ifdef USE_DOTLOCK
/* parameters: 
 * path - file to lock
 * retry - should retry if unable to lock?
 */

Thomas Roessler's avatar
Thomas Roessler committed
67
#ifdef DL_STANDALONE
Thomas Roessler's avatar
Thomas Roessler committed
68

Thomas Roessler's avatar
Thomas Roessler committed
69
static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
Thomas Roessler's avatar
Thomas Roessler committed
70 71
{
  char cmd[LONG_STRING + _POSIX_PATH_MAX];
72
  char f[SHORT_STRING + _POSIX_PATH_MAX];
Thomas Roessler's avatar
Thomas Roessler committed
73 74
  char r[SHORT_STRING];
  
75 76
  if (flags & DL_FL_RETRY)
    snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
Thomas Roessler's avatar
Thomas Roessler committed
77
  
78
  mutt_quote_filename (f, sizeof (f), path);
79
  
80
  snprintf (cmd, sizeof (cmd),
Thomas Roessler's avatar
Thomas Roessler committed
81
	    "%s %s%s%s%s%s%s%s",
82 83 84 85 86
	    NONULL (MuttDotlock),
	    flags & DL_FL_TRY ? "-t " : "",
	    flags & DL_FL_UNLOCK ? "-u " : "",
	    flags & DL_FL_USEPRIV ? "-p " : "",
	    flags & DL_FL_FORCE ? "-f " : "",
Thomas Roessler's avatar
Thomas Roessler committed
87
	    flags & DL_FL_UNLINK ? "-d " : "",
88 89
	    flags & DL_FL_RETRY ? r : "",
	    f);
90
  
91
  return mutt_system (cmd);
Thomas Roessler's avatar
Thomas Roessler committed
92
}
Thomas Roessler's avatar
Thomas Roessler committed
93

Thomas Roessler's avatar
Thomas Roessler committed
94
#else 
Thomas Roessler's avatar
Thomas Roessler committed
95

Thomas Roessler's avatar
Thomas Roessler committed
96
#define invoke_dotlock dotlock_invoke
Thomas Roessler's avatar
Thomas Roessler committed
97

Thomas Roessler's avatar
Thomas Roessler committed
98
#endif
Thomas Roessler's avatar
Thomas Roessler committed
99

Thomas Roessler's avatar
Thomas Roessler committed
100
static int dotlock_file (const char *path, int fd, int retry)
Thomas Roessler's avatar
Thomas Roessler committed
101 102 103
{
  int r;
  int flags = DL_FL_USEPRIV | DL_FL_RETRY;
104

105
  if (retry) retry = 1;
Thomas Roessler's avatar
Thomas Roessler committed
106

Thomas Roessler's avatar
Thomas Roessler committed
107
retry_lock:
Thomas Roessler's avatar
Thomas Roessler committed
108
  if ((r = invoke_dotlock(path, fd, flags, retry)) == DL_EX_EXIST)
Thomas Roessler's avatar
Thomas Roessler committed
109
  {
110 111 112 113 114 115 116 117 118 119
    if (!option (OPTNOCURSES))
    {
      char msg[LONG_STRING];
      
      snprintf(msg, sizeof(msg), _("Lock count exceeded, remove lock for %s?"),
	       path);
      if(retry && mutt_yesorno(msg, 1) == 1)
      {
	flags |= DL_FL_FORCE;
	retry--;
120
	mutt_clear_error ();
121 122 123 124
	goto retry_lock;
      }
    } 
    else
Thomas Roessler's avatar
Thomas Roessler committed
125
    {
126
      mutt_error ( _("Can't dotlock %s.\n"), path);
Thomas Roessler's avatar
Thomas Roessler committed
127 128
    }
  }
Thomas Roessler's avatar
Thomas Roessler committed
129
  return (r == DL_EX_OK ? 0 : -1);
Thomas Roessler's avatar
Thomas Roessler committed
130 131
}

Thomas Roessler's avatar
Thomas Roessler committed
132
static int undotlock_file (const char *path, int fd)
Thomas Roessler's avatar
Thomas Roessler committed
133
{
Thomas Roessler's avatar
Thomas Roessler committed
134
  return (invoke_dotlock(path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) == DL_EX_OK ? 
Thomas Roessler's avatar
Thomas Roessler committed
135
	  0 : -1);
Thomas Roessler's avatar
Thomas Roessler committed
136
}
Thomas Roessler's avatar
Thomas Roessler committed
137

Thomas Roessler's avatar
Thomas Roessler committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#endif /* USE_DOTLOCK */

/* Args:
 *	excl		if excl != 0, request an exclusive lock
 *	dot		if dot != 0, try to dotlock the file
 *	timeout 	should retry locking?
 */
int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
{
#if defined (USE_FCNTL) || defined (USE_FLOCK)
  int count;
  int attempt;
  struct stat prev_sb;
#endif
  int r = 0;

#ifdef USE_FCNTL
  struct flock lck;
  

  memset (&lck, 0, sizeof (struct flock));
  lck.l_type = excl ? F_WRLCK : F_RDLCK;
  lck.l_whence = SEEK_SET;

  count = 0;
  attempt = 0;
  while (fcntl (fd, F_SETLK, &lck) == -1)
  {
    struct stat sb;
    dprint(1,(debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
    if (errno != EAGAIN && errno != EACCES)
    {
      mutt_perror ("fcntl");
      return (-1);
    }

    if (fstat (fd, &sb) != 0)
     sb.st_size = 0;
    
    if (count == 0)
      prev_sb = sb;

    /* only unlock file if it is unchanged */
Thomas Roessler's avatar
Thomas Roessler committed
181
    if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
Thomas Roessler's avatar
Thomas Roessler committed
182 183
    {
      if (timeout)
184
	mutt_error _("Timeout exceeded while attempting fcntl lock!");
Thomas Roessler's avatar
Thomas Roessler committed
185 186 187 188 189
      return (-1);
    }

    prev_sb = sb;

190
    mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
Thomas Roessler's avatar
Thomas Roessler committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    sleep (1);
  }
#endif /* USE_FCNTL */

#ifdef USE_FLOCK
  count = 0;
  attempt = 0;
  while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1)
  {
    struct stat sb;
    if (errno != EWOULDBLOCK)
    {
      mutt_perror ("flock");
      r = -1;
      break;
    }

    if (fstat(fd,&sb) != 0 )
     sb.st_size=0;
    
    if (count == 0)
      prev_sb=sb;

    /* only unlock file if it is unchanged */
Thomas Roessler's avatar
Thomas Roessler committed
215
    if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
Thomas Roessler's avatar
Thomas Roessler committed
216 217
    {
      if (timeout)
218
	mutt_error _("Timeout exceeded while attempting flock lock!");
Thomas Roessler's avatar
Thomas Roessler committed
219 220 221 222 223 224
      r = -1;
      break;
    }

    prev_sb = sb;

225
    mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
Thomas Roessler's avatar
Thomas Roessler committed
226 227 228 229 230 231
    sleep (1);
  }
#endif /* USE_FLOCK */

#ifdef USE_DOTLOCK
  if (r == 0 && dot)
Thomas Roessler's avatar
Thomas Roessler committed
232
    r = dotlock_file (path, fd, timeout);
Thomas Roessler's avatar
Thomas Roessler committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
#endif /* USE_DOTLOCK */

  if (r == -1)
  {
    /* release any other locks obtained in this routine */

#ifdef USE_FCNTL
    lck.l_type = F_UNLCK;
    fcntl (fd, F_SETLK, &lck);
#endif /* USE_FCNTL */

#ifdef USE_FLOCK
    flock (fd, LOCK_UN);
#endif /* USE_FLOCK */

    return (-1);
  }

  return 0;
}

Thomas Roessler's avatar
Thomas Roessler committed
254
int mx_unlock_file (const char *path, int fd, int dot)
Thomas Roessler's avatar
Thomas Roessler committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
{
#ifdef USE_FCNTL
  struct flock unlockit = { F_UNLCK, 0, 0, 0 };

  memset (&unlockit, 0, sizeof (struct flock));
  unlockit.l_type = F_UNLCK;
  unlockit.l_whence = SEEK_SET;
  fcntl (fd, F_SETLK, &unlockit);
#endif

#ifdef USE_FLOCK
  flock (fd, LOCK_UN);
#endif

#ifdef USE_DOTLOCK
Thomas Roessler's avatar
Thomas Roessler committed
270
  if (dot)
Thomas Roessler's avatar
Thomas Roessler committed
271
    undotlock_file (path, fd);
Thomas Roessler's avatar
Thomas Roessler committed
272 273 274 275 276
#endif
  
  return 0;
}

Thomas Roessler's avatar
Thomas Roessler committed
277 278 279 280
void mx_unlink_empty (const char *path)
{
  int fd;
#ifndef USE_DOTLOCK
281
  struct stat sb;
Thomas Roessler's avatar
Thomas Roessler committed
282 283 284 285
#endif

  if ((fd = open (path, O_RDWR)) == -1)
    return;
286

Thomas Roessler's avatar
Thomas Roessler committed
287 288 289 290 291 292 293
  if (mx_lock_file (path, fd, 1, 0, 1) == -1)
  {
    close (fd);
    return;
  }

#ifdef USE_DOTLOCK
Thomas Roessler's avatar
Thomas Roessler committed
294
  invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
Thomas Roessler's avatar
Thomas Roessler committed
295
#else
296
  if (fstat (fd, &sb) == 0 && sb.st_size == 0)
Thomas Roessler's avatar
Thomas Roessler committed
297 298 299 300
    unlink (path);
#endif

  mx_unlock_file (path, fd, 0);
301
  close (fd);
Thomas Roessler's avatar
Thomas Roessler committed
302 303
}

Thomas Roessler's avatar
Thomas Roessler committed
304 305 306 307 308 309 310
/* try to figure out what type of mailbox ``path'' is
 *
 * return values:
 *	M_*	mailbox type
 *	0	not a mailbox
 *	-1	error
 */
311 312 313

#ifdef USE_IMAP

Thomas Roessler's avatar
Thomas Roessler committed
314
int mx_is_imap(const char *p)
315
{
316 317 318 319 320 321 322 323 324 325 326 327 328
  url_scheme_t scheme;

  if (!p)
    return 0;

  if (*p == '{')
    return 1;

  scheme = url_check_scheme (p);
  if (scheme == U_IMAP || scheme == U_IMAPS)
    return 1;

  return 0;
329 330 331 332
}

#endif

Thomas Roessler's avatar
Thomas Roessler committed
333 334 335 336 337 338 339 340
int mx_get_magic (const char *path)
{
  struct stat st;
  int magic = 0;
  char tmp[_POSIX_PATH_MAX];
  FILE *f;

#ifdef USE_IMAP
341
  if(mx_is_imap(path))
Thomas Roessler's avatar
Thomas Roessler committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    return M_IMAP;
#endif /* USE_IMAP */

  if (stat (path, &st) == -1)
  {
    dprint (1, (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
		path, strerror (errno), errno));
    return (-1);
  }

  if (S_ISDIR (st.st_mode))
  {
    /* check for maildir-style mailbox */

    snprintf (tmp, sizeof (tmp), "%s/cur", path);
    if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
      return (M_MAILDIR);

    /* check for mh-style mailbox */

    snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
    if (access (tmp, F_OK) == 0)
      return (M_MH);

    snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
    if (access (tmp, F_OK) == 0)
      return (M_MH);
369 370 371 372
    
    snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
    if (access (tmp, F_OK) == 0)
      return (M_MH);
373 374 375 376 377 378 379 380 381 382

    /* 
     * ok, this isn't an mh folder, but mh mode can be used to read
     * Usenet news from the spool. ;-) 
     */

    snprintf (tmp, sizeof (tmp), "%s/.overview", path);
    if (access (tmp, F_OK) == 0)
      return (M_MH);

Thomas Roessler's avatar
Thomas Roessler committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  }
  else if (st.st_size == 0)
  {
    /* hard to tell what zero-length files are, so assume the default magic */
    if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
      return (DefaultMagic);
    else
      return (M_MBOX);
  }
  else if ((f = fopen (path, "r")) != NULL)
  {
#ifndef BUFFY_SIZE
    struct utimbuf times;
#endif

    fgets (tmp, sizeof (tmp), f);
399
    if (mutt_strncmp ("From ", tmp, 5) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
400
      magic = M_MBOX;
401
    else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
402
      magic = M_MMDF;
403
    safe_fclose (&f);
Thomas Roessler's avatar
Thomas Roessler committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
#ifndef BUFFY_SIZE
    /* need to restore the times here, the file was not really accessed,
     * only the type was accessed.  This is important, because detection
     * of "new mail" depends on those times set correctly.
     */
    times.actime = st.st_atime;
    times.modtime = st.st_mtime;
    utime (path, &times);
#endif
  }
  else
  {
    dprint (1, (debugfile, "mx_get_magic(): unable to open file %s for reading.\n",
		path));
    mutt_perror (path);
    return (-1);
  }

  return (magic);
}

/*
 * set DefaultMagic to the given value
 */
int mx_set_magic (const char *s)
{
430
  if (mutt_strcasecmp (s, "mbox") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
431
    DefaultMagic = M_MBOX;
432
  else if (mutt_strcasecmp (s, "mmdf") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
433
    DefaultMagic = M_MMDF;
434
  else if (mutt_strcasecmp (s, "mh") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
435
    DefaultMagic = M_MH;
436
  else if (mutt_strcasecmp (s, "maildir") == 0)
Thomas Roessler's avatar
Thomas Roessler committed
437
    DefaultMagic = M_MAILDIR;
438
  else if (mutt_strcasecmp (s, "kendra") == 0)
439
    DefaultMagic = M_KENDRA;
Thomas Roessler's avatar
Thomas Roessler committed
440 441 442 443 444 445 446 447
  else
    return (-1);

  return 0;
}

static int mx_open_mailbox_append (CONTEXT *ctx)
{
448 449
  struct stat sb;

Thomas Roessler's avatar
Thomas Roessler committed
450
  ctx->append = 1;
Thomas Roessler's avatar
Thomas Roessler committed
451 452

#ifdef USE_IMAP
453
  
Thomas Roessler's avatar
Thomas Roessler committed
454
  if(mx_is_imap(ctx->path))  
Thomas Roessler's avatar
Thomas Roessler committed
455
    return imap_open_mailbox_append (ctx);
456

Thomas Roessler's avatar
Thomas Roessler committed
457
#endif
458 459
  
  if(stat(ctx->path, &sb) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
460
  {
461 462
    ctx->magic = mx_get_magic (ctx->path);
    
Thomas Roessler's avatar
Thomas Roessler committed
463
    switch (ctx->magic)
Thomas Roessler's avatar
Thomas Roessler committed
464 465
    {
      case 0:
466
	mutt_error (_("%s is not a mailbox."), ctx->path);
Thomas Roessler's avatar
Thomas Roessler committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	/* fall through */
      case -1:
	return (-1);
    }
  }
  else if (errno == ENOENT)
  {
    ctx->magic = DefaultMagic;

    if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
    {
      char tmp[_POSIX_PATH_MAX];

      if (mkdir (ctx->path, S_IRWXU))
      {
482
	mutt_perror (ctx->path);
Thomas Roessler's avatar
Thomas Roessler committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	return (-1);
      }

      if (ctx->magic == M_MAILDIR)
      {
	snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
	if (mkdir (tmp, S_IRWXU))
	{
	  mutt_perror (tmp);
	  rmdir (ctx->path);
	  return (-1);
	}

	snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
	if (mkdir (tmp, S_IRWXU))
	{
	  mutt_perror (tmp);
	  snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
	  rmdir (tmp);
	  rmdir (ctx->path);
	  return (-1);
	}
	snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
	if (mkdir (tmp, S_IRWXU))
	{
	  mutt_perror (tmp);
	  snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
	  rmdir (tmp);
	  snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
	  rmdir (tmp);
	  rmdir (ctx->path);
	  return (-1);
	}
      }
      else
      {
	int i;

	snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
	if ((i = creat (tmp, S_IRWXU)) == -1)
	{
	  mutt_perror (tmp);
	  rmdir (ctx->path);
	  return (-1);
	}
	close (i);
      }
    }
  }
  else
  {
    mutt_perror (ctx->path);
    return (-1);
  }

  switch (ctx->magic)
  {
    case M_MBOX:
    case M_MMDF:
      if ((ctx->fp = fopen (ctx->path, "a")) == NULL ||
	  mbox_lock_mailbox (ctx, 1, 1) != 0)
      {
	if (!ctx->fp)
	  mutt_perror (ctx->path);
547 548 549
	else
	{
	  mutt_error (_("Couldn't lock %s\n"), ctx->path);
550
	  safe_fclose (&ctx->fp);
551
	}
Thomas Roessler's avatar
Thomas Roessler committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	return (-1);
      }
      fseek (ctx->fp, 0, 2);
      break;

    case M_MH:
    case M_MAILDIR:
      /* nothing to do */
      break;

    default:
      return (-1);
  }

  return 0;
}

/*
 * open a mailbox and parse it
 *
 * Args:
 *	flags	M_NOSORT	do not sort mailbox
 *		M_APPEND	open mailbox for appending
 *		M_READONLY	open mailbox in read-only mode
 *		M_QUIET		only print error messages
 *	ctx	if non-null, context struct to use
 */
CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT *pctx)
{
  CONTEXT *ctx = pctx;
  int rc;

  if (!ctx)
    ctx = safe_malloc (sizeof (CONTEXT));
  memset (ctx, 0, sizeof (CONTEXT));
  ctx->path = safe_strdup (path);

  ctx->msgnotreadyet = -1;
590
  ctx->collapsed = 0;
Thomas Roessler's avatar
Thomas Roessler committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
  
  if (flags & M_QUIET)
    ctx->quiet = 1;
  if (flags & M_READONLY)
    ctx->readonly = 1;

  if (flags & M_APPEND)
  {
    if (mx_open_mailbox_append (ctx) != 0)
    {
      mx_fastclose_mailbox (ctx);
      if (!pctx)
	safe_free ((void **) &ctx);
      return NULL;
    }
    return ctx;
  }

Thomas Roessler's avatar
Thomas Roessler committed
609 610 611
  ctx->magic = mx_get_magic (path);
  
  if(ctx->magic == 0)
612
    mutt_error (_("%s is not a mailbox."), path);
Thomas Roessler's avatar
Thomas Roessler committed
613

Thomas Roessler's avatar
Thomas Roessler committed
614 615 616 617 618 619 620 621 622
  if(ctx->magic == -1)
    mutt_perror(path);
  
  if(ctx->magic <= 0)
  {
    mx_fastclose_mailbox (ctx);
    if (!pctx)
      FREE (&ctx);
    return (NULL);
Thomas Roessler's avatar
Thomas Roessler committed
623
  }
Thomas Roessler's avatar
Thomas Roessler committed
624
  
Thomas Roessler's avatar
Thomas Roessler committed
625 626 627 628 629 630 631 632
  /* if the user has a `push' command in their .muttrc, or in a folder-hook,
   * it will cause the progress messages not to be displayed because
   * mutt_refresh() will think we are in the middle of a macro.  so set a
   * flag to indicate that we should really refresh the screen.
   */
  set_option (OPTFORCEREFRESH);

  /* create hash tables */
633 634
  ctx->id_hash = hash_create (1031);
  ctx->subj_hash = hash_create (1031);
Thomas Roessler's avatar
Thomas Roessler committed
635 636

  if (!ctx->quiet)
637
    mutt_message (_("Reading %s..."), ctx->path);
Thomas Roessler's avatar
Thomas Roessler committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692

  switch (ctx->magic)
  {
    case M_MH:
      rc = mh_read_dir (ctx, NULL);
      break;

    case M_MAILDIR:
      rc = maildir_read_dir (ctx);
      break;

    case M_MMDF:
    case M_MBOX:
      rc = mbox_open_mailbox (ctx);
      break;

#ifdef USE_IMAP
    case M_IMAP:
      rc = imap_open_mailbox (ctx);
      break;
#endif /* USE_IMAP */

    default:
      rc = -1;
      break;
  }

  if (rc == 0)
  {
    if ((flags & M_NOSORT) == 0)
    {
      /* avoid unnecessary work since the mailbox is completely unthreaded
	 to begin with */
      unset_option (OPTSORTSUBTHREADS);
      unset_option (OPTNEEDRESCORE);
      mutt_sort_headers (ctx, 1);
    }
    if (!ctx->quiet)
      mutt_clear_error ();
  }
  else
  {
    mx_fastclose_mailbox (ctx);
    if (!pctx)
      safe_free ((void **) &ctx);
  }

  unset_option (OPTFORCEREFRESH);
  return (ctx);
}

/* free up memory associated with the mailbox context */
void mx_fastclose_mailbox (CONTEXT *ctx)
{
  int i;
693 694 695

  if(!ctx) 
    return;
Thomas Roessler's avatar
Thomas Roessler committed
696 697 698
  
#ifdef USE_IMAP
  if (ctx->magic == M_IMAP)
699
    imap_close_mailbox (ctx);
Thomas Roessler's avatar
Thomas Roessler committed
700 701 702 703 704 705 706 707 708 709 710 711 712
#endif /* USE_IMAP */
  if (ctx->subj_hash)
    hash_destroy (&ctx->subj_hash, NULL);
  if (ctx->id_hash)
    hash_destroy (&ctx->id_hash, NULL);
  for (i = 0; i < ctx->msgcount; i++)
    mutt_free_header (&ctx->hdrs[i]);
  safe_free ((void **) &ctx->hdrs);
  safe_free ((void **) &ctx->v2r);
  safe_free ((void **) &ctx->path);
  safe_free ((void **) &ctx->pattern);
  if (ctx->limit_pattern) 
    mutt_pattern_free (&ctx->limit_pattern);
713
  safe_fclose (&ctx->fp);
Thomas Roessler's avatar
Thomas Roessler committed
714 715 716 717
  memset (ctx, 0, sizeof (CONTEXT));
}

/* save changes to disk */
718
static int sync_mailbox (CONTEXT *ctx, int *index_hint)
Thomas Roessler's avatar
Thomas Roessler committed
719 720 721 722 723 724 725
{
#ifdef BUFFY_SIZE
  BUFFY *tmp = NULL;
#endif
  int rc = -1;

  if (!ctx->quiet)
726
    mutt_message (_("Writing %s..."), ctx->path);
727

Thomas Roessler's avatar
Thomas Roessler committed
728 729 730 731
  switch (ctx->magic)
  {
    case M_MBOX:
    case M_MMDF:
732
      rc = mbox_sync_mailbox (ctx, index_hint);
Thomas Roessler's avatar
Thomas Roessler committed
733 734 735 736 737 738 739
#ifdef BUFFY_SIZE
      tmp = mutt_find_mailbox (ctx->path);
#endif
      break;
      
    case M_MH:
    case M_MAILDIR:
740
      rc = mh_sync_mailbox (ctx, index_hint);
Thomas Roessler's avatar
Thomas Roessler committed
741 742 743 744
      break;
      
#ifdef USE_IMAP
    case M_IMAP:
745
      /* extra argument means EXPUNGE */
746
      rc = imap_sync_mailbox (ctx, 1, index_hint);
Thomas Roessler's avatar
Thomas Roessler committed
747 748 749 750
      break;
#endif /* USE_IMAP */
  }

751
#if 0
752
  if (!ctx->quiet && !ctx->shutup && rc == -1)
753
    mutt_error ( _("Could not synchronize mailbox %s!"), ctx->path);
754
#endif
755
  
Thomas Roessler's avatar
Thomas Roessler committed
756 757 758 759 760 761 762 763
#ifdef BUFFY_SIZE
  if (tmp && tmp->new == 0)
    mutt_update_mailbox (tmp);
#endif
  return rc;
}

/* save changes and close mailbox */
764
int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
Thomas Roessler's avatar
Thomas Roessler committed
765 766
{
  int i, move_messages = 0, purge = 1, read_msgs = 0;
767
  int check;
Thomas Roessler's avatar
Thomas Roessler committed
768 769 770 771 772
  int isSpool = 0;
  CONTEXT f;
  char mbox[_POSIX_PATH_MAX];
  char buf[SHORT_STRING];

773 774
  if (!ctx) return 0;

775 776
  ctx->closing = 1;

Thomas Roessler's avatar
Thomas Roessler committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
  if (ctx->readonly || ctx->dontwrite)
  {
    /* mailbox is readonly or we don't want to write */
    mx_fastclose_mailbox (ctx);
    return 0;
  }

  if (ctx->append)
  {
    /* mailbox was opened in write-mode */
    if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
      mbox_close_mailbox (ctx);
    else
      mx_fastclose_mailbox (ctx);
    return 0;
  }

  for (i = 0; i < ctx->msgcount; i++)
  {
    if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read)
      read_msgs++;
  }

  if (read_msgs && quadoption (OPT_MOVE) != M_NO)
  {
    char *p;

    if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path)))
    {
      isSpool = 1;
      strfcpy (mbox, p, sizeof (mbox));
    }
    else
    {
Thomas Roessler's avatar
Thomas Roessler committed
811
      strfcpy (mbox, NONULL(Inbox), sizeof (mbox));
Thomas Roessler's avatar
Thomas Roessler committed
812 813 814 815 816 817
      isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
    }
    mutt_expand_path (mbox, sizeof (mbox));

    if (isSpool)
    {
818
      snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
Thomas Roessler's avatar
Thomas Roessler committed
819
      if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1)
Thomas Roessler's avatar
Thomas Roessler committed
820 821
      {
	ctx->closing = 0;
Thomas Roessler's avatar
Thomas Roessler committed
822
	return (-1);
Thomas Roessler's avatar
Thomas Roessler committed
823
      }
Thomas Roessler's avatar
Thomas Roessler committed
824 825 826 827 828
    }
  }

  if (ctx->deleted)
  {
829 830 831
    snprintf (buf, sizeof (buf), ctx->deleted == 1
	     ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
	      ctx->deleted);
Thomas Roessler's avatar
Thomas Roessler committed
832
    if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
Thomas Roessler's avatar
Thomas Roessler committed
833 834
    {
      ctx->closing = 0;
Thomas Roessler's avatar
Thomas Roessler committed
835
      return (-1);
Thomas Roessler's avatar
Thomas Roessler committed
836
    }
Thomas Roessler's avatar
Thomas Roessler committed
837 838
  }

839
#ifdef USE_IMAP
840
  /* IMAP servers manage the OLD flag themselves */
841 842
  if (ctx->magic != M_IMAP)
#endif
Thomas Roessler's avatar
Thomas Roessler committed
843 844 845 846 847 848 849 850 851 852 853
  if (option (OPTMARKOLD))
  {
    for (i = 0; i < ctx->msgcount; i++)
    {
      if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
	mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
    }
  }

  if (move_messages)
  {
854
    mutt_message (_("Moving read messages to %s..."), mbox);
Thomas Roessler's avatar
Thomas Roessler committed
855

856 857 858 859 860
#ifdef USE_IMAP
    /* try to use server-side copy first */
    i = 1;
    
    if (ctx->magic == M_IMAP && mx_is_imap (mbox))
Thomas Roessler's avatar
Thomas Roessler committed
861
    {
862 863 864 865
      /* tag messages for moving, and clear old tags, if any */
      for (i = 0; i < ctx->msgcount; i++)
	if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted)
	  ctx->hdrs[i]->tagged = 1;
866
	else
867 868 869 870 871 872 873 874
	  ctx->hdrs[i]->tagged = 0;
      
      i = imap_copy_messages (ctx, NULL, mbox, 1);
    }
    
    if (i == 0) /* success */
      mutt_clear_error ();
    else if (i == -1) /* horrible error, bail */
Thomas Roessler's avatar
Thomas Roessler committed
875 876
    {
      ctx->closing=0;
877
      return -1;
Thomas Roessler's avatar
Thomas Roessler committed
878
    }
879 880 881 882
    else /* use regular append-copy mode */
#endif
    {
      if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL)
Thomas Roessler's avatar
Thomas Roessler committed
883 884
      {
	ctx->closing = 0;
885
	return -1;
Thomas Roessler's avatar
Thomas Roessler committed
886
      }
887 888 889 890 891 892 893 894 895 896 897 898

      for (i = 0; i < ctx->msgcount; i++)
      {
	if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted)
        {
	  if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
	  {
	    ctx->hdrs[i]->deleted = 1;
	    ctx->deleted++;
	  }
	  else
	  {
899
	    mx_close_mailbox (&f, NULL);
Thomas Roessler's avatar
Thomas Roessler committed
900
	    ctx->closing = 0;
901 902
	    return -1;
	  }
903
	}
Thomas Roessler's avatar
Thomas Roessler committed
904
      }
905
    
906
      mx_close_mailbox (&f, NULL);
Thomas Roessler's avatar
Thomas Roessler committed
907
    }
908
    
Thomas Roessler's avatar
Thomas Roessler committed
909 910 911
  }
  else if (!ctx->changed && ctx->deleted == 0)
  {
912
    mutt_message _("Mailbox is unchanged.");
Thomas Roessler's avatar
Thomas Roessler committed
913 914 915 916
    mx_fastclose_mailbox (ctx);
    return 0;
  }
  
917 918 919 920
#ifdef USE_IMAP
  /* allow IMAP to preserve the deleted flag across sessions */
  if (ctx->magic == M_IMAP)
  {
921
    if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
922 923
    {
      ctx->closing = 0;
924
      return check;
Thomas Roessler's avatar
Thomas Roessler committed
925
    }
926
  }
Thomas Roessler's avatar
Thomas Roessler committed
927
  else
928
#endif
Thomas Roessler's avatar
Thomas Roessler committed
929
  {
Thomas Roessler's avatar
Thomas Roessler committed
930 931 932 933 934 935
    if (!purge)
    {
      for (i = 0; i < ctx->msgcount; i++)
        ctx->hdrs[i]->deleted = 0;
      ctx->deleted = 0;
    }
Thomas Roessler's avatar
Thomas Roessler committed
936

Thomas Roessler's avatar
Thomas Roessler committed
937 938
    if (ctx->changed || ctx->deleted)
    {
939
      if ((check = sync_mailbox (ctx, index_hint)) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
940 941
      {
	ctx->closing = 0;
942
	return check;
Thomas Roessler's avatar
Thomas Roessler committed
943
      }
Thomas Roessler's avatar
Thomas Roessler committed
944
    }
945
  }
Thomas Roessler's avatar
Thomas Roessler committed
946

947 948 949 950 951 952
  if (move_messages)
     mutt_message (_("%d kept, %d moved, %d deleted."),
       ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
  else
    mutt_message (_("%d kept, %d deleted."),
      ctx->msgcount - ctx->deleted, ctx->deleted);
Thomas Roessler's avatar
Thomas Roessler committed
953

954 955 956 957
  if (ctx->msgcount == ctx->deleted &&
      (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
      !mutt_is_spool(ctx->path) && !option (OPTSAVEEMPTY))
    mx_unlink_empty (ctx->path);
Thomas Roessler's avatar
Thomas Roessler committed
958 959 960 961 962 963

  mx_fastclose_mailbox (ctx);

  return 0;
}

964 965 966

/* update a Context structure's internal tables. */

967
void mx_update_tables(CONTEXT *ctx, int committing)
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
{
  int i, j;
  
  /* update memory to reflect the new state of the mailbox */
  ctx->vcount = 0;
  ctx->vsize = 0;
  ctx->tagged = 0;
  ctx->deleted = 0;
  ctx->new = 0;
  ctx->unread = 0;
  ctx->changed = 0;
  ctx->flagged = 0;
#define this_body ctx->hdrs[j]->content
  for (i = 0, j = 0; i < ctx->msgcount; i++)
  {
983 984
    if ((committing && !ctx->hdrs[i]->deleted) || 
	(!committing && ctx->hdrs[i]->active))
985 986 987 988 989 990 991 992 993 994 995 996 997 998
    {
      if (i != j)
      {
	ctx->hdrs[j] = ctx->hdrs[i];
	ctx->hdrs[i] = NULL;
      }
      ctx->hdrs[j]->msgno = j;
      if (ctx->hdrs[j]->virtual != -1)
      {
	ctx->v2r[ctx->vcount] = j;
	ctx->hdrs[j]->virtual = ctx->vcount++;
	ctx->vsize += this_body->length + this_body->offset -
	  this_body->hdr_offset;
      }
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

      if(committing)
	ctx->hdrs[j]->changed = 0;
      else
      {
	if (ctx->hdrs[j]->deleted)
	  ctx->deleted++;
	if (ctx->hdrs[j]->changed)
	  ctx->changed++;
      }

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
      if (ctx->hdrs[j]->tagged)
	ctx->tagged++;
      if (ctx->hdrs[j]->flagged)
	ctx->flagged++;
      if (!ctx->hdrs[j]->read)
      { 
	ctx->unread++;
	if (!ctx->hdrs[j]->old)
	  ctx->new++;
      } 
1020

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
      j++;
    }
    else
    {
      if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
	ctx->size -= (ctx->hdrs[i]->content->length +
		      ctx->hdrs[i]->content->offset -
		      ctx->hdrs[i]->content->hdr_offset);
      /* remove message from the hash tables */
      if (ctx->hdrs[i]->env->real_subj)
	hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj, ctx->hdrs[i], NULL);
      if (ctx->hdrs[i]->env->message_id)
	hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id, ctx->hdrs[i], NULL);
      mutt_free_header (&ctx->hdrs[i]);
    }
  }
#undef this_body
  ctx->msgcount = j;
}


Thomas Roessler's avatar
Thomas Roessler committed
1042 1043 1044 1045 1046 1047
/* save changes to mailbox
 *
 * return values:
 *	0		success
 *	-1		error
 */
1048
int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
Thomas Roessler's avatar
Thomas Roessler committed
1049
{
1050
  int rc, i;
1051
  int purge = 1;
1052
  int msgcount, deleted;
Thomas Roessler's avatar
Thomas Roessler committed
1053 1054 1055 1056 1057 1058

  if (ctx->dontwrite)
  {
    char buf[STRING], tmp[STRING];
    if (km_expand_key (buf, sizeof(buf),
                       km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1059
      snprintf (tmp, sizeof(tmp), _(" Press '%s' to toggle write"), buf);
Thomas Roessler's avatar
Thomas Roessler committed
1060
    else
1061
      strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"), sizeof(tmp));
Thomas Roessler's avatar
Thomas Roessler committed
1062

1063
    mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
Thomas Roessler's avatar
Thomas Roessler committed
1064 1065 1066 1067
    return -1;
  }
  else if (ctx->readonly)
  {
1068
    mutt_error _("Mailbox is read-only.");
Thomas Roessler's avatar
Thomas Roessler committed
1069 1070 1071 1072 1073
    return -1;
  }

  if (!ctx->changed && !ctx->deleted)
  {
1074
    mutt_message _("Mailbox is unchanged.");
Thomas Roessler's avatar
Thomas Roessler committed
1075 1076 1077 1078 1079 1080 1081
    return (0);
  }

  if (ctx->deleted)
  {
    char buf[SHORT_STRING];

1082 1083 1084
    snprintf (buf, sizeof (buf), ctx->deleted == 1
	     ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
	      ctx->deleted);
1085
    if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
Thomas Roessler's avatar
Thomas Roessler committed
1086
      return (-1);
1087
    else if (purge == M_NO)
Thomas Roessler's avatar
Thomas Roessler committed
1088 1089 1090
    {
      if (!ctx->changed)
	return 0; /* nothing to do! */
1091 1092 1093 1094 1095 1096 1097 1098 1099
#ifdef USE_IMAP
      /* let IMAP servers hold on to D flags */
      if (ctx->magic != M_IMAP)
#endif
      {
        for (i = 0 ; i < ctx->msgcount ; i++)
          ctx->hdrs[i]->deleted = 0;
        ctx->deleted = 0;
      }
Thomas Roessler's avatar
Thomas Roessler committed
1100 1101 1102
    }
  }

1103 1104 1105 1106 1107
  /* really only for IMAP - imap_sync_mailbox results in a call to
   * mx_update_tables, so ctx->deleted is 0 when it comes back */
  msgcount = ctx->msgcount;
  deleted = ctx->deleted;

1108 1109
#ifdef USE_IMAP
  if (ctx->magic == M_IMAP)
1110
    rc = imap_sync_mailbox (ctx, purge, index_hint);
1111 1112
  else
#endif
1113
    rc = sync_mailbox (ctx, index_hint);
1114
  if (rc == 0)
Thomas Roessler's avatar
Thomas Roessler committed
1115
  {
1116 1117
#ifdef USE_IMAP
    if (ctx->magic == M_IMAP && !purge)
1118
      mutt_message _("Mailbox checkpointed.");
1119 1120
    else
#endif
1121 1122
    mutt_message (_("%d kept, %d deleted."), msgcount - deleted,
      deleted);
1123

Thomas Roessler's avatar
Thomas Roessler committed
1124 1125 1126 1127
    sleep (1); /* allow the user time to read the message */

    if (ctx->msgcount == ctx->deleted &&
	(ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
Thomas Roessler's avatar
Thomas Roessler committed
1128
	!mutt_is_spool(ctx->path) && !option (OPTSAVEEMPTY))
Thomas Roessler's avatar
Thomas Roessler committed
1129 1130 1131 1132 1133 1134
    {
      unlink (ctx->path);
      mx_fastclose_mailbox (ctx);
      return 0;
    }

1135 1136 1137
    /* if we haven't deleted any messages, we don't need to resort */
    if (purge)
    {
1138
#ifdef USE_IMAP
1139 1140
      /* IMAP does this automatically after handling EXPUNGE */
      if (ctx->magic != M_IMAP)
1141
#endif
1142
      {
1143
	mx_update_tables (ctx, 1);
1144 1145
	mutt_sort_headers (ctx, 1); /* rethread from scratch */
      }
1146
    }
Thomas Roessler's avatar
Thomas Roessler committed
1147 1148 1149 1150 1151 1152
  }

  return (rc);
}


1153
/* {maildir,mh}_open_new_message are in mh.c. */
Thomas Roessler's avatar
Thomas Roessler committed
1154 1155 1156 1157 1158 1159 1160

int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
{
  msg->fp = dest->fp;
  return 0;
}

Thomas Roessler's avatar
Thomas Roessler committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
#ifdef USE_IMAP
int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
{
  char tmp[_POSIX_PATH_MAX];

  mutt_mktemp(tmp);
  if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
    return (-1);
  msg->path = safe_strdup(tmp);
  return 0;
}
#endif

Thomas Roessler's avatar
Thomas Roessler committed
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
/* args:
 *	dest	destintation mailbox
 *	hdr	message being copied (required for maildir support, because
 *		the filename depends on the message flags)
 */
MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
{
  MESSAGE *msg;
  int (*func) (MESSAGE *, CONTEXT *, HEADER *);
  ADDRESS *p = NULL;
  time_t t;

  switch (dest->magic)
  {
    case M_MMDF:
    case M_MBOX:
      func = mbox_open_new_message;
      break;
    case M_MAILDIR:
      func = maildir_open_new_message;
      break;
    case M_MH:
      func = mh_open_new_message;
      break;
Thomas Roessler's avatar
Thomas Roessler committed
1198 1199 1200 1201 1202
#ifdef USE_IMAP
    case M_IMAP:
      func = imap_open_new_message;
      break;
#endif
Thomas Roessler's avatar
Thomas Roessler committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
    default:
      dprint (1, (debugfile, "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
		  dest->magic));
      return (NULL);
  }

  msg = safe_calloc (1, sizeof (MESSAGE));
  msg->magic = dest->magic;
  msg->write = 1;

  if (func (msg, dest, hdr) == 0)
  {
    if (dest->magic == M_MMDF)
      fputs (MMDF_SEP, msg->fp);

1218 1219
    if ((msg->magic == M_MBOX || msg->magic ==  M_MMDF) &&
	flags & M_ADD_FROM)
Thomas Roessler's avatar
Thomas Roessler committed
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
    {
      if (hdr)
      {
	if (hdr->env->return_path)
	  p = hdr->env->return_path;
	else if (hdr->env->sender)
	  p = hdr->env->sender;
	else
	  p = hdr->env->from;

	if (!hdr->received)
	  hdr->received = time (NULL);
	t = hdr->received;
      }
      else
	t = time (NULL);

Thomas Roessler's avatar
Thomas Roessler committed
1237
      fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(Username), ctime (&t));
Thomas Roessler's avatar
Thomas Roessler committed
1238 1239 1240 1241 1242 1243 1244 1245 1246
    }
  }
  else
    safe_free ((void **) &msg);

  return msg;
}

/* check for new mail */
1247
int mx_check_mailbox (CONTEXT *ctx, int *index_hint, int lock)
Thomas Roessler's avatar
Thomas Roessler committed
1248
{
1249 1250
  int rc;

Thomas Roessler's avatar
Thomas Roessler committed
1251 1252
  if (ctx)
  {
1253 1254
    if (ctx->locked) lock = 0;

Thomas Roessler's avatar
Thomas Roessler committed
1255 1256 1257 1258
    switch (ctx->magic)
    {
      case M_MBOX:
      case M_MMDF:
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

	if (lock)
	{
	  mutt_block_signals ();
	  if (mbox_lock_mailbox (ctx, 0, 0) == -1)
	  {
	    mutt_unblock_signals ();
	    return M_LOCKED;
	  }
	}
	
	rc = mbox_check_mailbox (ctx, index_hint);

	if (lock)
	{
	  mutt_unblock_signals ();
	  mbox_unlock_mailbox (ctx);
	}
	
	return rc;

Thomas Roessler's avatar
Thomas Roessler committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

      case M_MH:
      case M_MAILDIR:
	return (mh_check_mailbox (ctx, index_hint));

#ifdef USE_IMAP
      case M_IMAP:
	return (imap_check_mailbox (ctx, index_hint));
#endif /* USE_IMAP */
    }
  }

  dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
  return (-1);
}

/* return a stream pointer for a message */
MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
{
  MESSAGE *msg;
  
  msg = safe_calloc (1, sizeof (MESSAGE));
  switch (msg->magic = ctx->magic)
  {
    case M_MBOX:
    case M_MMDF:
      msg->fp = ctx->fp;
      break;

    case M_MH:
    case M_MAILDIR:
1311 1312 1313 1314 1315 1316
    {
      HEADER *cur = ctx->hdrs[msgno];
      char path[_POSIX_PATH_MAX];
      
      snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
      if ((msg->fp = fopen (path, "r")) == NULL)
Thomas Roessler's avatar
Thomas Roessler committed
1317
      {
1318 1319 1320 1321
	mutt_perror (path);
	dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
		    path, strerror (errno), errno));
	FREE (&msg);
Thomas Roessler's avatar
Thomas Roessler committed
1322
      }
1323 1324 1325
    }
    break;
    
Thomas Roessler's avatar
Thomas Roessler committed
1326 1327
#ifdef USE_IMAP
    case M_IMAP:
1328
    {
Thomas Roessler's avatar
Thomas Roessler committed
1329
      if (imap_fetch_message (msg, ctx, msgno) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
1330
	FREE (&msg);
Thomas Roessler's avatar
Thomas Roessler committed
1331
      break;
1332
    }
Thomas Roessler's avatar
Thomas Roessler committed
1333 1334 1335 1336
#endif /* USE_IMAP */

    default:
      dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic));
Thomas Roessler's avatar
Thomas Roessler committed
1337
      FREE (&msg);
Thomas Roessler's avatar
Thomas Roessler committed
1338 1339 1340 1341 1342
      break;
  }
  return (msg);
}

1343 1344 1345
/* commit a message to a folder */

int mx_commit_message (MESSAGE *msg, CONTEXT *ctx)
Thomas Roessler's avatar
Thomas Roessler committed
1346 1347 1348
{
  int r = 0;

1349 1350 1351 1352 1353 1354 1355 1356
  if (!(msg->write && ctx->append))
  {
    dprint (1, (debugfile, "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
		msg->write, ctx->append));
    return -1;
  }

  switch (msg->magic)
Thomas Roessler's avatar
Thomas Roessler committed
1357
  {
1358
    case M_MMDF:
Thomas Roessler's avatar
Thomas Roessler committed
1359
    {
1360 1361
      if (fputs (MMDF_SEP, msg->fp) == EOF)
	r = -1;
1362 1363 1364 1365 1366
      break;
    }
    
    case M_MBOX:
    {
1367 1368
      if (fputc ('\n', msg->fp) == EOF)
	r = -1;
1369 1370
      break;
    }
Thomas Roessler's avatar
Thomas Roessler committed
1371

1372 1373
    case M_KENDRA:
    {
1374 1375
      if (fputs (KENDRA_SEP, msg->fp) == EOF)
	r = -1;
1376
      break;
Thomas Roessler's avatar
Thomas Roessler committed
1377 1378
    }

1379 1380 1381 1382 1383 1384 1385 1386 1387
#ifdef USE_IMAP
    case M_IMAP:
    {
      if ((r = safe_fclose (&msg->fp)) == 0)
	r = imap_append_message (ctx, msg);
      break;
    }
#endif
    
Thomas Roessler's avatar
Thomas Roessler committed
1388
    case M_MAILDIR:
1389
    {
1390
      r = maildir_commit_message (ctx, msg, NULL);
1391 1392 1393 1394 1395
      break;
    }
    
    case M_MH:
    {
1396
      r = mh_commit_message (ctx, msg, NULL);
Thomas Roessler's avatar
Thomas Roessler committed
1397
      break;
1398 1399
    }
  }
1400 1401 1402 1403 1404 1405 1406 1407
  
  if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF || ctx->magic == M_KENDRA)
      && fflush (msg->fp) == EOF)
  {
    mutt_perror _("Can't write message");
    r = -1;
  }
 
1408 1409 1410 1411 1412 1413 1414
  return r;
}

/* close a pointer to a message */
int mx_close_message (MESSAGE **msg)
{
  int r = 0;
Thomas Roessler's avatar
Thomas Roessler committed
1415

1416
  if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
Thomas Roessler's avatar
Thomas Roessler committed
1417
#ifdef USE_IMAP
1418
      || (*msg)->magic == M_IMAP
Thomas Roessler's avatar
Thomas Roessler committed
1419
#endif
1420 1421 1422 1423 1424 1425
      )
  {
    r = safe_fclose (&(*msg)->fp);
  }
  else
    (*msg)->fp = NULL;
Thomas Roessler's avatar
Thomas Roessler committed
1426

1427 1428 1429 1430 1431 1432
  if ((*msg)->path)
  {
    dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
		(*msg)->path));
    unlink ((*msg)->path);
    FREE (&(*msg)->path);
Thomas Roessler's avatar
Thomas Roessler committed
1433
  }
Thomas Roessler's avatar
Thomas Roessler committed
1434

Thomas Roessler's avatar
Thomas Roessler committed
1435
  FREE (msg);
Thomas Roessler's avatar
Thomas Roessler committed
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
  return (r);
}

void mx_alloc_memory (CONTEXT *ctx)
{
  int i;

  if (ctx->hdrs)
  {
    safe_realloc ((void **) &ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
    safe_realloc ((void **) &ctx->v2r, sizeof (int) * ctx->hdrmax);
  }
  else
  {
    ctx->hdrs = safe_malloc (sizeof (HEADER *) * (ctx->hdrmax += 25));
    ctx->v2r = safe_malloc (sizeof (int) * ctx->hdrmax);
  }
  for (i = ctx->msgcount ; i < ctx->hdrmax ; i++)
  {
    ctx->hdrs[i] = NULL;
    ctx->v2r[i] = -1;
  }
}

/* this routine is called to update the counts in the context structure for
 * the last message header parsed.
 */
void mx_update_context (CONTEXT *ctx)
{
  HEADER *h = ctx->hdrs[ctx->msgcount];




1470
#ifdef HAVE_PGP
Thomas Roessler's avatar
Thomas Roessler committed
1471 1472
  /* NOTE: this _must_ be done before the check for mailcap! */
  h->pgp = pgp_query (h->content);
1473
#endif /* HAVE_PGP */
Thomas Roessler's avatar
Thomas Roessler committed
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492

  if (!ctx->pattern)
  {
    ctx->v2r[ctx->vcount] = ctx->msgcount;
    h->virtual = ctx->vcount++;
  }
  else
    h->virtual = -1;
  h->msgno = ctx->msgcount;
  ctx->msgcount++;

  if (h->env->supersedes)
  {
    HEADER *h2 = hash_find (ctx->id_hash, h->env->supersedes);

    /* safe_free (&h->env->supersedes); should I ? */
    if (h2)
    {
      h2->superseded = 1;
Thomas Roessler's avatar
Thomas Roessler committed
1493 1494
      if (option (OPTSCORE)) 
	mutt_score_message (ctx, h2, 1);
Thomas Roessler's avatar
Thomas Roessler committed
1495 1496 1497 1498 1499 1500 1501 1502 1503
    }
  }

  /* add this message to the hash tables */
  if (h->env->message_id)
    hash_insert (ctx->id_hash, h->env->message_id, h, 0);
  if (h->env->real_subj)
    hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);

Thomas Roessler's avatar
Thomas Roessler committed
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
  if (option (OPTSCORE)) 
    mutt_score_message (ctx, h, 0);
  
  if (h->changed)
    ctx->changed = 1;
  if (h->flagged)
    ctx->flagged++;
  if (h->deleted)
    ctx->deleted++;
  if (!h->read)
  {
    ctx->unread++;
    if (!h->old)
      ctx->new++;
  }
Thomas Roessler's avatar
Thomas Roessler committed
1519
}