imapext-2007

diff src/osdep/unix/maildir.c @ 1:28a55bc1110c

[mq]: imapext
author yuuji@gentei.org
date Mon, 14 Sep 2009 19:23:11 +0900
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/osdep/unix/maildir.c	Mon Sep 14 19:23:11 2009 +0900
     1.3 @@ -0,0 +1,1541 @@
     1.4 +/*
     1.5 + * Maildir Module for PINE 4.0x - fourth release, use with CARE! 
     1.6 + *
     1.7 + * Author:      Mattias Larsson <ml@techno.org>
     1.8 + *
     1.9 + * Version:     21.07.98
    1.10 + *
    1.11 + * Please read the README.maildir file before using this module!
    1.12 + *
    1.13 + * If you have any questions, please e-mail ml@techno.org 
    1.14 + *
    1.15 + * Multiple inboxes patch by Dean Gaudet <dgaudet@arctic.org>
    1.16 + *
    1.17 + * =================================================
    1.18 + *
    1.19 + * Based on the IMAP2 maildir routines by:
    1.20 + *
    1.21 + * Author:      Eric Green
    1.22 + *              Bloodhounds International Inc.
    1.23 + *              thrytis@imaxx.net
    1.24 + *
    1.25 + * Additional contributions from:
    1.26 + *              Aidas Kasparas (kaspar@soften.ktu.lt)
    1.27 + *
    1.28 + * Date:        27 April 1997
    1.29 + * Last Edited: 13 June 1997
    1.30 + *
    1.31 + * Based (heavily) on mh.c and other c-client library files by Mark Crispin:
    1.32 + *
    1.33 + *              Mark Crispin
    1.34 + *              Networks and Distributed Computing
    1.35 + *              Computing & Communications
    1.36 + *              University of Washington
    1.37 + *              Administration Building, AG-44
    1.38 + *              Seattle, WA  98195
    1.39 + *              Internet: MRC@CAC.Washington.EDU
    1.40 + *
    1.41 + * Copyright 1995 by the University of Washington
    1.42 + *
    1.43 + *  Permission to use, copy, modify, and distribute this software and its
    1.44 + * documentation for any purpose and without fee is hereby granted, provided
    1.45 + * that the above copyright notice appears in all copies and that both the
    1.46 + * above copyright notice and this permission notice appear in supporting
    1.47 + * documentation, and that the name of the University of Washington not be
    1.48 + * used in advertising or publicity pertaining to distribution of the software
    1.49 + * without specific, written prior permission.  This software is made
    1.50 + * available "as is", and
    1.51 + * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
    1.52 + * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
    1.53 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
    1.54 + * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
    1.55 + * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
    1.56 + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
    1.57 + * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
    1.58 + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    1.59 + *
    1.60 + */
    1.61 +
    1.62 +/* CONFIGURABLE OPTIONS - PLEASE CHECK THESE OUT */
    1.63 +
    1.64 +#define NO_MAILDIR_FIDDLE	/* disallow Maildir with Maildir in the
    1.65 +				name. This is useful in an ISP setup
    1.66 +				using the IMAP daemon. #undef it if you
    1.67 +				are running a normal pine and know what
    1.68 +				you are doing */
    1.69 +
    1.70 +/* #define NO_ABSOLUTE_PATHS	/* if you define this, all paths
    1.71 +				use your HOMEDIR is the root instead
    1.72 +				of the actual root of the machine. This
    1.73 +				is also useful in an ISP setup with
    1.74 +				IMAP */
    1.75 +
    1.76 +#undef NO_UID_VALIDITIY		/* define this if you want the UID's not
    1.77 +				to be persistent over sessions. Use this
    1.78 +				if you use another client to read the
    1.79 +				maildir that screws up the special way
    1.80 +				in which we store UIDs. Do not enable
    1.81 +				unless you are sure you need it. */
    1.82 +	
    1.83 +/* END CONFIGURATION */
    1.84 +
    1.85 +#define MTA_DEBUG	/* debugging sent to stdout */
    1.86 +#undef MTA_DEBUG
    1.87 +
    1.88 +#include <stdio.h>
    1.89 +#include <ctype.h>
    1.90 +#include <errno.h>
    1.91 +extern int errno;		/* just in case */
    1.92 +#include "mail.h"
    1.93 +#include "osdep.h"
    1.94 +#include <pwd.h>
    1.95 +#include <sys/stat.h>
    1.96 +#include <sys/time.h>
    1.97 +#include <sys/types.h>
    1.98 +#include <dirent.h>
    1.99 +#include <utime.h>
   1.100 +#include "maildir.h"
   1.101 +#include "misc.h"
   1.102 +#include "dummy.h"
   1.103 +
   1.104 +/* Driver dispatch used by MAIL */
   1.105 +
   1.106 +DRIVER maildirdriver = {
   1.107 +  "maildir",			/* driver name */
   1.108 +				/* driver flags */
   1.109 +  DR_MAIL|DR_LOCAL|DR_NOFAST|DR_NAMESPACE,
   1.110 +  (DRIVER *) NIL,		/* next driver */
   1.111 +  maildir_valid,			/* mailbox is valid for us */
   1.112 +  maildir_parameters,		/* manipulate parameters */
   1.113 +  NIL,			/* scan mailboxes */
   1.114 +  maildir_list,			/* find mailboxes */
   1.115 +  maildir_lsub,			/* find subscribed mailboxes */
   1.116 +  maildir_sub,			/* subscribe to mailbox */
   1.117 +  maildir_unsub,		/* unsubscribe from mailbox */
   1.118 +  maildir_create,			/* create mailbox */
   1.119 +  maildir_delete,			/* delete mailbox */
   1.120 +  maildir_rename,			/* rename mailbox */
   1.121 +  NIL,				/* status of mailbox */
   1.122 +  maildir_open,			/* open mailbox */
   1.123 +  maildir_close,		/* close mailbox */
   1.124 +  maildir_fast,			/* fetch message "fast" attributes */
   1.125 +  NIL,				/* fetch message flags */
   1.126 +  NIL,				/* fetch overview */
   1.127 +  NIL,				/* fetch message envelopes */
   1.128 +  maildir_fetchheader,		/* fetch message header */
   1.129 +  maildir_fetchtext,		/* fetch message body */
   1.130 +  NIL,				/* fetch partial message text */
   1.131 +  NIL,				/* unique identifier */
   1.132 +  NIL,				/* message number */
   1.133 +  NIL,				/* modify flags */
   1.134 +  maildir_flagmsg,		/* per-message modify flags */
   1.135 +  NIL,				/* search for message based on criteria */
   1.136 +  NIL,				/* sort messages */
   1.137 +  NIL,				/* thread messages */
   1.138 +  maildir_ping,			/* ping mailbox to see if still alive */
   1.139 +  maildir_check,		/* check for new messages */
   1.140 +  maildir_expunge,			/* expunge deleted messages */
   1.141 +  maildir_copy,			/* copy messages to another mailbox */
   1.142 +  maildir_append,			/* append string message to mailbox */
   1.143 +  maildir_gc				/* garbage collect stream */
   1.144 +};
   1.145 +
   1.146 +				/* prototype stream */
   1.147 +MAILSTREAM maildirproto = {&maildirdriver};
   1.148 +
   1.149 +/* Check validity of mailbox
   1.150 + */
   1.151 + 
   1.152 +DRIVER *maildir_valid (char *name)
   1.153 +{
   1.154 +  return maildir_isvalid(name,T) ? &maildirdriver : NIL;
   1.155 +}
   1.156 +
   1.157 +int maildir_isvalid (char *name,long justname)
   1.158 +{
   1.159 +  char tmp[MAILTMPLEN];
   1.160 +  struct stat sbuf;
   1.161 +  
   1.162 +  if (!name || (!*name) || 
   1.163 +      ((*name == '#') && 
   1.164 +       (*(name+1) == 0 ||
   1.165 +	(*(name+1) != 'm' && *(name+1) != 'M') ||
   1.166 +	(*(name+2) != 'd' && *(name+1) != 'D') ||
   1.167 +	*(name+3) != '/')) || (*name == '.'))
   1.168 +    return NIL;
   1.169 +  
   1.170 +  /* okay, anything containing the name Maildir will be ignored
   1.171 +     this is to prevent anyone from fiddling with their incoming Maildir
   1.172 +     directly, it should be accessed via the INBOX alias */
   1.173 +
   1.174 +  #ifdef NO_MAILDIR_FIDDLE
   1.175 +  if (strstr(name, "Maildir")) {
   1.176 +	return NIL; 
   1.177 +	}
   1.178 +  #endif
   1.179 + 				/* If we are requested only to check 
   1.180 +  				   if the name is appropriate then we
   1.181 +  				   have done! */
   1.182 +  if (justname && *name == '#') return T;
   1.183 +  
   1.184 +
   1.185 +				/* must be valid local mailbox */
   1.186 +  if ((*name != '*') && (*name != '{') &&
   1.187 +      maildir_file (tmp,name) &&
   1.188 +				/* assume its maildir if its a dir */
   1.189 +      stat (tmp,&sbuf) == 0 && S_ISDIR (sbuf.st_mode))
   1.190 +    return T;
   1.191 +
   1.192 +				/* INBOX is for default Maildir */
   1.193 +  if (!strcmp (ucase (strncpy (tmp,name,MAILTMPLEN)), "INBOX") &&
   1.194 +      (stat (maildir_file (tmp,name),&sbuf) == 0) &&
   1.195 +      S_ISDIR (sbuf.st_mode))
   1.196 +    return T;
   1.197 +
   1.198 +  return NIL;
   1.199 +}
   1.200 +#ifdef QMAIL
   1.201 +#ifndef QMAILCONTROL
   1.202 +# define QMAILCONTROL "/var/qmail/control"
   1.203 +#endif
   1.204 +#ifndef DOTQMAIL
   1.205 +# ifdef POSTFIX
   1.206 +#  define DOTQMAIL ".forward"
   1.207 +# else
   1.208 +#  define DOTQMAIL ".qmail"
   1.209 +# endif
   1.210 +#endif
   1.211 +#ifndef XADDR_DELIM
   1.212 +# ifdef POSTFIX
   1.213 +#  define XADDR_DELIM "+"
   1.214 +# else
   1.215 +#  define XADDR_DELIM "-"
   1.216 +# endif
   1.217 +#endif
   1.218 +
   1.219 +/* Convert virtual domain user
   1.220 + */
   1.221 +char* conv_virtualdomain(char *account) {
   1.222 +  char *dom = strchr(account, '@'), *p;
   1.223 +  char vd[MAILTMPLEN+1], rewrite[MAILTMPLEN+1], previous[MAILTMPLEN+1];
   1.224 +  FILE *vdfd;
   1.225 +  int match=0;
   1.226 +  snprintf(vd, MAILTMPLEN, "%s/%s", QMAILCONTROL, "virtualdomains");
   1.227 +  if (NIL == dom) return account;
   1.228 +  dom++;		/* set position of domain part beginning */
   1.229 +  if (dom && NIL != (vdfd = fopen (vd, "r"))) {
   1.230 +    char buf[MAILTMPLEN+1], *s;
   1.231 +    int l = strlen(dom);
   1.232 +    int L = strlen(account);
   1.233 +    while ((s=fgets(buf, MAILTMPLEN, vdfd))) {
   1.234 +      if (p=strchr(s, '#'))
   1.235 +        *p = '\0';			/* zap comments */
   1.236 +      if (!strchr(buf, ':'))
   1.237 +        continue;
   1.238 +      while (s && (strrchr(s, '\n') || strrchr(s, '\r') || strrchr(s, ' ')))
   1.239 +        s[strlen(s)-1] = '\0';
   1.240 +      if (!strncmp(account, s, L) && s[L] == ':' && s[L+1]) { /* user matches */
   1.241 +	match = 3;
   1.242 +        snprintf(rewrite, MAILTMPLEN, "%s%s%s", s+L+1, XADDR_DELIM, account);
   1.243 +	break;
   1.244 +      }
   1.245 +      if (!strncmp(dom, s, l) && s[l] == ':' && s[l+1]) { /* domain matches */
   1.246 +        match = 2;
   1.247 +	snprintf(rewrite, MAILTMPLEN, "%s%s%s", s+l+1, XADDR_DELIM, account);
   1.248 +	continue;
   1.249 +      }
   1.250 +      if (match < 2 && s[0] == '.') { /* if domain described in wildcard */
   1.251 +        if (p=strchr(s, ':')) {
   1.252 +	  *p = '\0';
   1.253 +	  if (!strcmp(dom+(strlen(dom)-strlen(s)), s)) {
   1.254 +	    if (match == 0
   1.255 +	        || strlen(previous) < strlen(s)) {
   1.256 +	      match = 1;
   1.257 +	      strncpy(previous, s, MAILTMPLEN);
   1.258 +	      snprintf(rewrite, MAILTMPLEN, "%s%s%s", p+1, XADDR_DELIM, account);
   1.259 +	    }
   1.260 +	  }
   1.261 +	}
   1.262 +      }
   1.263 +    }
   1.264 +    fclose(vdfd);
   1.265 +    if (match) {
   1.266 +      p = strchr(rewrite, '@');
   1.267 +      /* fprintf(stderr, "m=%d, rwr=[%s]\n", match, rewrite); */
   1.268 +      if (p) {
   1.269 +        *p = '\0';
   1.270 +      }
   1.271 +      /* fprintf(stderr, "rwr=[%s]\n", rewrite); */
   1.272 +      s = cpystr(rewrite);
   1.273 +      memset(vd, 0, sizeof(vd));
   1.274 +      memset(rewrite, 0, sizeof(rewrite));
   1.275 +      memset(previous, 0, sizeof(previous));
   1.276 +      return s;
   1.277 +    }
   1.278 +  }
   1.279 +  return account; /* return itself */
   1.280 +}
   1.281 +/* -------------------------------------------------
   1.282 +   Qmail virtual domain hack ends
   1.283 +   ------------------------------------------------- */
   1.284 +#endif
   1.285 +
   1.286 +char *maildir_getmaildir() {
   1.287 +  static char dir[MAILTMPLEN];
   1.288 +  extern char *mymailsuffix();
   1.289 +  static char qfile[MAILTMPLEN];
   1.290 +  FILE *qmail;
   1.291 +  dir[0] = '\0';
   1.292 +  snprintf(qfile, MAILTMPLEN, "%s/%s", myhomedir(), DOTQMAIL);
   1.293 +  strncat(qfile, mymailsuffix(), MAILTMPLEN);
   1.294 +  qmail = fopen(qfile, "r");
   1.295 +  if (NULL != qmail) {	/* if ~/.qmail file exists */
   1.296 +    char buf[MAILTMPLEN], *s;
   1.297 +    while ((s=fgets(buf, MAILTMPLEN, qmail))) {
   1.298 +	/* Skip preceding spaces and trailing newlines */
   1.299 +	while (*s && (*s == ' ' || *s == '\t')) {s++;}
   1.300 +	while (*s && (s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n')) {
   1.301 +	s[strlen(s)-1] = '\0';
   1.302 +	}
   1.303 +	if (*s == '#') continue;
   1.304 +	if (s[strlen(s)-1] != '/') continue; /* Skip non maildir format */
   1.305 +        if (*s == '~' && *(s+1) == '/')
   1.306 +            *s = '.';           /* Convert `~/' to `./' (for Postfix) */
   1.307 +	strncpy(dir, s, MAILTMPLEN);
   1.308 +        break;
   1.309 +    }
   1.310 +    fclose(qmail);
   1.311 +  }
   1.312 +#ifdef DEFAULTMAILDIR
   1.313 +  else {
   1.314 +    strncpy(dir, DEFAULTMAILDIR, MAILTMPLEN);
   1.315 +  }
   1.316 +#endif
   1.317 +  if (dir[0]) {
   1.318 +    return dir;
   1.319 +  } else {
   1.320 +#ifdef DEFAULTMAILDIR
   1.321 +    snprintf(dir, MAILTMPLEN, "%s/%s/", myhomedir(), DEFAULTMAILDIR);
   1.322 +#else
   1.323 +    snprintf(dir, MAILTMPLEN, "%s/%s/", myhomedir(), MAILDIRPATH);
   1.324 +#endif
   1.325 +    return dir;
   1.326 +  }
   1.327 +}
   1.328 +
   1.329 +/* Maildir mail generate file string
   1.330 + */
   1.331 +
   1.332 +char *maildir_file (char *dst,char *name)
   1.333 +{
   1.334 +  char tmp[MAILTMPLEN];
   1.335 +  
   1.336 +  if (strlen (name) > 3 &&	/* safe do other comparisons */
   1.337 +      (*name == '#') &&
   1.338 +      (name[1] == 'm' || name[1] == 'M') &&
   1.339 +      (name[2] == 'd' || name[2] == 'D') &&
   1.340 +      (name[3] == '/'))
   1.341 +    name += 4;
   1.342 +
   1.343 +#ifdef NO_ABSOLUTE_PATHS  
   1.344 +  if (*name == '/') {	
   1.345 +  /* we do not want to accept / absolute paths, so lets strip the first
   1.346 +     / ... */
   1.347 +    snprintf(dst, MAILTMPLEN, "%s/%s/cur", myhomedir(), name+1);
   1.348 +
   1.349 +/*    strncpy (dst, name, MAILTMPLEN - 2);
   1.350 +    strncat (dst, "/cur", MAILTMPLEN - 2);
   1.351 +    dst[MAILTMPLEN - 1] = '\0'; */
   1.352 +  }
   1.353 +  else
   1.354 +    snprintf (dst, MAILTMPLEN, "%s/%s/cur",myhomedir (),
   1.355 +	    strcmp (ucase (strcnpy (tmp, name, MAILTMPLEN)), "INBOX") ? name : maildir_getmaildir());
   1.356 +#else
   1.357 +  if (*name == '/') {	
   1.358 +    strncpy (dst, name, MAILTMPLEN - 2);
   1.359 +    strncat (dst, "/cur", MAILTMPLEN - 2);
   1.360 +    dst[MAILTMPLEN - 1] = '\0';
   1.361 +  }
   1.362 +  else {
   1.363 +      char *maildir = maildir_getmaildir();
   1.364 +      if (*maildir == '/') {
   1.365 +          snprintf (dst,MAILTMPLEN,"%scur",maildir);
   1.366 +      } else {
   1.367 +          snprintf (dst,MAILTMPLEN,"%s/%s/cur",myhomedir (),
   1.368 +                   strcmp (ucase (strncpy (tmp, name, MAILTMPLEN)), "INBOX") ? name : maildir);
   1.369 +      }
   1.370 +  }
   1.371 +
   1.372 +#endif 
   1.373 +  
   1.374 +  #ifdef MTA_DEBUG
   1.375 +  printf("maildir_file '%s'\n", dst);
   1.376 +  #endif
   1.377 +  return dst;
   1.378 +}
   1.379 +
   1.380 +/* Maildir open
   1.381 + */
   1.382 + 
   1.383 +MAILSTREAM *maildir_open (MAILSTREAM *stream)
   1.384 +{
   1.385 + char tmp[MAILTMPLEN],tmp2[MAILTMPLEN];
   1.386 +
   1.387 + if (!stream) return &maildirproto;
   1.388 + if (LOCAL) {		/* recycle stream */
   1.389 +	maildir_close (stream, 0);
   1.390 +	stream->dtb = &maildirdriver;
   1.391 +	mail_free_cache (stream);
   1.392 +	stream->uid_last = 0;       /* default UID validity */
   1.393 +	stream->uid_validity = time (0);
   1.394 +	}
   1.395 +
   1.396 +	stream->uid_validity = 0; /* was time(0) */
   1.397 +
   1.398 +  if (stream->uid_last < time(0))
   1.399 +  	stream->uid_last = time (0);  
   1.400 +  
   1.401 +    stream->local = fs_get (sizeof (MAILDIRLOCAL));
   1.402 +  LOCAL->inbox = !strcmp (ucase (strncpy (tmp,stream->mailbox,MAILTMPLEN)),"INBOX") ||
   1.403 +      !strcmp (stream->mailbox,maildir_file (tmp2,"INBOX"));
   1.404 +  LOCAL->dir = cpystr (maildir_file (tmp,stream->mailbox)); /* copy dir name */
   1.405 +                                /* make temporary buffer */
   1.406 +  LOCAL->buf = (char *) fs_get ((LOCAL->buflen = CHUNKSIZE-1) + 1);
   1.407 +  LOCAL->scantime = 0;          /* not scanned yet */
   1.408 +  stream->sequence++;
   1.409 +  stream->nmsgs = stream->recent = 0;
   1.410 +
   1.411 +  maildir_ping_core (stream);
   1.412 +  maildir_ping (stream);
   1.413 +/*  if (maildir_ping (stream) && !(stream->nmsgs || stream->silent))
   1.414 +    printf("Mailbox is empty\n");
   1.415 +*/
   1.416 +  return stream;
   1.417 +
   1.418 +}
   1.419 +
   1.420 +/* Maildir ping mailbox
   1.421 + */
   1.422 +
   1.423 +long maildir_ping_core (MAILSTREAM *stream)
   1.424 +{
   1.425 +  char tmp[MAILTMPLEN];
   1.426 +  MESSAGECACHE *elt;
   1.427 +  struct stat sbuf, sbuf2;
   1.428 +  DIR *dir;
   1.429 +  struct direct *d;
   1.430 +  int reloadall = NIL;
   1.431 +  int uidinvalid = NIL;
   1.432 +  unsigned long old;
   1.433 +  long i;
   1.434 +  long nmsgs = stream->nmsgs;
   1.435 +  long recent = stream->recent;
   1.436 +  long nfiles = stream->nmsgs;
   1.437 +  int silent = stream->silent;
   1.438 +  char *s, *s2;
   1.439 +  mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
   1.440 +
   1.441 +/*  maildir_copynew (LOCAL->dir);
   1.442 + */
   1.443 +  
   1.444 +  if (stat (LOCAL->dir,&sbuf) < 0) {
   1.445 +    snprintf (tmp,MAILTMPLEN,"Unable to open maildir: %s",strerror (errno));
   1.446 +    mm_log (tmp,ERROR);
   1.447 +    return NIL;
   1.448 +  }
   1.449 +
   1.450 +  /* okay, lets try to figure out the Maildir UID validity. This is done
   1.451 +     by checking the last modification time of the file .uidvalidity
   1.452 +     in the rootdir of the Maildir. Any program reordering the files
   1.453 +     in the directory have to touch this file */
   1.454 +
   1.455 +  snprintf(tmp, MAILTMPLEN, "%s/../.uidvalidity", LOCAL->dir);
   1.456 +  
   1.457 +  if (stat (tmp,&sbuf2) < 0) {
   1.458 +	/* no uid validity file found, if uid_validity == 0, we have
   1.459 +	   to set it to something other than 0, and then create the
   1.460 +	   .uidvalidity file for future accesses */
   1.461 +
   1.462 +	if (stream->uid_validity == 0) {
   1.463 +		FILE *fl;
   1.464 +		struct utimbuf tbuf;
   1.465 +		
   1.466 +		stream->uid_validity = time(0);
   1.467 +		tbuf.actime = stream->uid_validity;
   1.468 +		tbuf.modtime = stream->uid_validity;
   1.469 +
   1.470 +		if ((fl = fopen(tmp, "w"))) {
   1.471 +			fclose(fl);
   1.472 +			chmod (tmp, S_IRUSR|S_IWUSR);
   1.473 +			utime(tmp, &tbuf);
   1.474 +			}
   1.475 +		}
   1.476 +		uidinvalid = T; /* UID's are invalid, update them */
   1.477 +	} else {
   1.478 +	/* valid file, lets set UID if uid_validity = 0 */
   1.479 +	if (stream->uid_validity == 0) {
   1.480 +	stream->uid_validity = sbuf2.st_mtime;
   1.481 +		}
   1.482 +	}
   1.483 +
   1.484 +  #ifdef NO_UID_VALIDITY
   1.485 +  uidinvalid = T; /* force the UIDs to be invalid and reset every time,
   1.486 +  		     useful in an environment without imap servers and
   1.487 +  		     clients that screw up the UIDs.. i'd leave it to
   1.488 +  		     OFF until I really need it though... */
   1.489 +  #endif
   1.490 +
   1.491 +  stream->silent = T;  
   1.492 +  if (sbuf.st_ctime != LOCAL->scantime) {
   1.493 +    /* update the message list */
   1.494 +    /* int maildir_namesort(const void *,const void *);
   1.495 +       int (*mn)(struct direct **, struct direct **) = maildir_namesort; */
   1.496 +    struct direct **names = NIL;
   1.497 +    nfiles = scandir (LOCAL->dir,&names,maildir_select,maildir_namesort);
   1.498 +
   1.499 +    for (i = 0; i < nfiles; i++) {
   1.500 +
   1.501 +	/* check if file has executable bit set */
   1.502 +	snprintf(tmp, MAILTMPLEN, "%s/%s", LOCAL->dir, names[i]->d_name);
   1.503 +  	stat (tmp,&sbuf2);
   1.504 +	if (sbuf2.st_mode & S_IXUSR) {
   1.505 +		/* executable bit set, modtime is uid */
   1.506 +		if (sbuf2.st_mtime > stream->uid_last)
   1.507 +			stream->uid_last = sbuf2.st_mtime+1;
   1.508 +		}
   1.509 +	/* this is kept for backwards compatibility */
   1.510 +        if ((s = strstr (names[i]->d_name,":3,")))
   1.511 +		s += 3;
   1.512 +        if (s && (s2 = strstr (s, ",U"))) {
   1.513 +      		s2 += 2;
   1.514 +		sscanf(s2, "%d", &old);
   1.515 +		if (old > stream->uid_last) {
   1.516 +			stream->uid_last = old+1;
   1.517 +			}
   1.518 +		}	
   1.519 +	
   1.520 +    }
   1.521 +
   1.522 +    mm_critical (stream);	/* go critical */
   1.523 +    old = stream->uid_last;
   1.524 +    LOCAL->scantime = sbuf.st_ctime;
   1.525 +
   1.526 +				/* check if old files same */
   1.527 +    for (i = 0; i < stream->nmsgs; i++) {
   1.528 +
   1.529 +      if (strcmp ((char *) mail_elt (stream, i + 1)->maildirp,
   1.530 +		  names[i]->d_name)) {
   1.531 +	reloadall = T;
   1.532 +	break;
   1.533 +      }
   1.534 +    }
   1.535 +
   1.536 +    if (reloadall) { /* files are out of order, rebuild cache */
   1.537 +
   1.538 +      i = 1;
   1.539 +      while (i <= stream->nmsgs)
   1.540 +				/* clean out cache */
   1.541 +	if ((elt = (MESSAGECACHE *) (*mc) (stream,i,CH_ELT))) {
   1.542 +	  fs_give ((void **) &elt->maildirp);
   1.543 +	  mail_expunged (stream,i);
   1.544 +	}
   1.545 +	else
   1.546 +	  i++;
   1.547 +      
   1.548 +      mm_log ("Warning: Mailbox has changed in an unexpected way.  Reloading.",
   1.549 +	      WARN);
   1.550 +      stream->nmsgs = 0;
   1.551 +    }
   1.552 +    nmsgs = stream->nmsgs;
   1.553 +    
   1.554 +    stream->nmsgs = nfiles; /* hm? */    
   1.555 +
   1.556 +    for (i = nmsgs; i < nfiles; i++) {
   1.557 +
   1.558 +      mail_exists(stream, i+1);
   1.559 +				/* if newly seen, add to list */
   1.560 +      (elt = mail_elt (stream, i + 1))->maildirp = (long) cpystr (names[i]->d_name);
   1.561 +       elt->valid = T;
   1.562 +
   1.563 +      /* grab the flags */
   1.564 +      if ((s = strstr (names[i]->d_name,":3,"))) {
   1.565 +	s += 3;
   1.566 +	if (strchr (s,'F'))
   1.567 +	  elt->flagged = T;
   1.568 +	if (strchr (s,'R'))
   1.569 +	  elt->answered = T;
   1.570 +	if (strchr (s,'S'))
   1.571 +	  elt->seen = T;
   1.572 +	if (strchr (s,'T'))
   1.573 +	  elt->deleted = T;
   1.574 +      } else if ((s = strstr (names[i]->d_name,":2,"))) {
   1.575 +	/* this is the :2, id where all files go nowadays */
   1.576 +	s += 3;
   1.577 +	if (strchr (s,'F'))
   1.578 +	  elt->flagged = T;
   1.579 +	if (strchr (s,'R'))
   1.580 +	  elt->answered = T;
   1.581 +	if (strchr (s,'S'))
   1.582 +	  elt->seen = T;
   1.583 +	if (strchr (s,'T'))
   1.584 +	  elt->deleted = T;
   1.585 +	snprintf(tmp, MAILTMPLEN, "%s/%s", LOCAL->dir, names[i]->d_name);
   1.586 +  	stat (tmp,&sbuf2);
   1.587 +	if (sbuf2.st_mode & S_IXUSR) {
   1.588 +		/* executable bit set, modtime is uid */
   1.589 +		elt->private.uid = sbuf2.st_mtime;
   1.590 +		}
   1.591 +	/* and if we could not retrieve UID from modtime, or if
   1.592 +	   UIDs are invalid, go here */
   1.593 +	if (elt->private.uid == 0 || uidinvalid) {
   1.594 +        	stream->uid_last = (elt = mail_elt (stream,i+1))->private.uid = stream->uid_last+1;
   1.595 +		maildir_flagmsg(stream, elt);
   1.596 +		}	
   1.597 +	s = 0; /* make sure next if statement does not trigger */      
   1.598 +      	}
   1.599 +
   1.600 +      if (s)
   1.601 +      if ((s2 = strstr (s, ",U"))) {
   1.602 +      	s2 += 2;
   1.603 +	sscanf(s2, "%d", &elt->private.uid);
   1.604 +	if (elt->private.uid == 0 || uidinvalid) {
   1.605 +        	stream->uid_last = (elt = mail_elt (stream,i+1))->private.uid = stream->uid_last+1;
   1.606 +		maildir_flagmsg(stream, elt);
   1.607 +		}	
   1.608 +	
   1.609 +      	} else { /* assign new UID */
   1.610 +        stream->uid_last = (elt = mail_elt (stream,i+1))->private.uid = stream->uid_last+1;
   1.611 +	elt->recent = T;
   1.612 +	recent++;
   1.613 +	maildir_flagmsg(stream, elt); /* store the UID that we assigned to it */
   1.614 +	}	
   1.615 +
   1.616 +
   1.617 +
   1.618 +    }
   1.619 +
   1.620 +    mm_nocritical (stream);	/* release critical */
   1.621 +				/* free the names stuff */
   1.622 +    for (i = 0; i < nfiles; i++)
   1.623 +      fs_give ((void **) &names[i]);
   1.624 +    if (names)
   1.625 +      fs_give ((void **) &names);
   1.626 +  }
   1.627 +  stream->silent = silent;
   1.628 +  mail_exists(stream,nfiles);
   1.629 +/*  if (!reloadall)  */
   1.630 +  	mail_recent (stream,recent);
   1.631 +
   1.632 +  return T;			/* return that we are alive */
   1.633 +}
   1.634 +
   1.635 +long maildir_ping (MAILSTREAM *stream)
   1.636 +{
   1.637 +  maildir_copynew (LOCAL->dir);
   1.638 +  return maildir_ping_core (stream);
   1.639 +}
   1.640 +
   1.641 +void maildir_copynew (const char *mailbox)
   1.642 +{
   1.643 +  char tmp[MAILTMPLEN],file[MAILTMPLEN],newfile[MAILTMPLEN];
   1.644 +  DIR *dir;
   1.645 +  struct dirent *d;
   1.646 +  struct stat sbuf;
   1.647 +  
   1.648 +  snprintf (tmp,MAILTMPLEN,"%s/../new",mailbox);
   1.649 +  if (!(dir = opendir (tmp)))
   1.650 +    return;
   1.651 +
   1.652 +  while (d = readdir (dir)) {
   1.653 +    if (d->d_name[0] == '.')
   1.654 +      continue;			/* skip .files */
   1.655 +
   1.656 +    snprintf (file,MAILTMPLEN,"%s/%s",tmp,d->d_name);
   1.657 +				/* make sure this is a normal file */
   1.658 +    if (stat (file,&sbuf) == 0 && S_ISREG (sbuf.st_mode)) {
   1.659 +      
   1.660 +      if (strstr (d->d_name,":3,")) /* this message already has flags */
   1.661 +	snprintf (newfile,MAILTMPLEN,"%s/%s",mailbox,d->d_name);
   1.662 +      else
   1.663 +	snprintf (newfile,MAILTMPLEN,"%s/%s:3,",mailbox,d->d_name);
   1.664 +      
   1.665 +				/* move the new mail to the cur dir */
   1.666 +      if (link (file,newfile) == -1)
   1.667 +	mm_log("Unable to read new mail!",WARN);
   1.668 +      else
   1.669 +	unlink (file);	
   1.670 +    }
   1.671 +  }
   1.672 +  closedir (dir);
   1.673 +}
   1.674 +
   1.675 +int maildir_select (struct direct *name)
   1.676 +{
   1.677 +  if (name->d_name[0] != '.')
   1.678 +    return T;
   1.679 +
   1.680 +  return NIL;
   1.681 +}
   1.682 +
   1.683 +/* int maildir_namesort (struct direct **d1,struct direct **d2) */
   1.684 +int maildir_namesort (const void *d1, const void *d2)
   1.685 +{
   1.686 +  return strcmp ((*(struct direct **)d1)->d_name,
   1.687 +                 (*(struct direct **)d2)->d_name);
   1.688 +}
   1.689 +
   1.690 +
   1.691 +/* Maildir garbage collect stream
   1.692 + */
   1.693 +
   1.694 +void maildir_gc (MAILSTREAM *stream,long gcflags)
   1.695 +{
   1.696 +  unsigned long i;
   1.697 +  
   1.698 +  if (gcflags & GC_TEXTS) {	/* garbage collect texts? */
   1.699 +				/* flush texts from cache */
   1.700 +/*    if (LOCAL->hdr) fs_give ((void **) &LOCAL->hdr);
   1.701 +//    if (stream->text) fs_give ((void **) &stream->text);
   1.702 +//    stream->msgno = 0;		invalidate stream text
   1.703 +*/
   1.704 +  }
   1.705 +}
   1.706 +
   1.707 +/* Maildir close
   1.708 + */
   1.709 +
   1.710 +void maildir_close (MAILSTREAM *stream, long options)
   1.711 +{
   1.712 +  MESSAGECACHE *elt;
   1.713 +  int i;
   1.714 +  mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
   1.715 + 
   1.716 +/*  CL_EXPUNGE OPTION SUPPORT HERE SOMEWHERE! */
   1.717 +				/* clean out the cached paths */
   1.718 +  for (i = 1; i <= stream->nmsgs; i++)
   1.719 +    if ((elt = (MESSAGECACHE *) (*mc) (stream,i,CH_ELT)) && elt->maildirp) {
   1.720 +      fs_give ((void **) &elt->maildirp);
   1.721 +      elt->maildirp = 0; /* otherwise pine coredumps */  
   1.722 +	}  
   1.723 +  
   1.724 +  if (LOCAL) {			/* only if a stream is open */
   1.725 +    if (LOCAL->dir) fs_give ((void **) &LOCAL->dir);
   1.726 +    maildir_gc (stream,GC_TEXTS); /* free local cache */
   1.727 +				/* free local scratch buffer */
   1.728 +    if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
   1.729 +				/* nuke the local data */
   1.730 +    fs_give ((void **) &stream->local);
   1.731 +    stream->dtb = NIL;		/* log out the DTB */
   1.732 +  }
   1.733 +}
   1.734 +
   1.735 +void maildir_check (MAILSTREAM *stream)
   1.736 +{
   1.737 +  /* Perhaps in the future this will preserve flags */
   1.738 +  if (maildir_ping (stream)) mm_log ("Check completed",(long) NIL);   
   1.739 +}
   1.740 +
   1.741 +long maildir_fetchtext (MAILSTREAM *stream,unsigned long msgno,STRING *bs, long flags)
   1.742 +{
   1.743 +  unsigned long i;
   1.744 +  MESSAGECACHE *elt;
   1.745 +                                /* UID call "impossible" */
   1.746 +  if (flags & FT_UID) return NIL;
   1.747 +  elt = mail_elt (stream,msgno);/* get elt */
   1.748 +                                /* snarf message if don't have it yet */
   1.749 +  if (!elt->private.msg.text.text.data) {
   1.750 +    maildir_fetchheader (stream,msgno,&i,flags);
   1.751 +    if (!elt->private.msg.text.text.data) return NIL;
   1.752 +  }
   1.753 +  if (!(flags & FT_PEEK)) {     /* mark as seen */
   1.754 +    mail_elt (stream,msgno)->seen = T;
   1.755 +    maildir_flagmsg (stream, mail_elt(stream,msgno));
   1.756 +    mm_flags (stream,msgno);
   1.757 +  }
   1.758 +  if (!elt->private.msg.text.text.data) return NIL;
   1.759 +  INIT (bs,mail_string,elt->private.msg.text.text.data,
   1.760 +        elt->private.msg.text.text.size);
   1.761 +  return T;
   1.762 +}
   1.763 +
   1.764 +
   1.765 +/* Maildir fetch message header
   1.766 + */
   1.767 +
   1.768 +char *maildir_fetchheader (MAILSTREAM *stream,unsigned long msgno,
   1.769 +		unsigned long *length, long flags)
   1.770 +{
   1.771 +  unsigned long i,hdrsize;
   1.772 +  int fd;
   1.773 +  char *t;
   1.774 +  char tmp[MAILTMPLEN];
   1.775 +  char *s,*b;
   1.776 +  struct stat sbuf;
   1.777 +  struct tm *tm;
   1.778 +  MESSAGECACHE *elt;
   1.779 +  *length = 0;                  /* default to empty */
   1.780 +  if (flags & FT_UID) return "";/* UID call "impossible" */
   1.781 +  elt = mail_elt (stream,msgno);/* get elt */
   1.782 +  if (!elt->private.msg.header.text.data) {
   1.783 +
   1.784 +/*    maildir_gc (stream,GC_TEXTS);  invalidate current cache */
   1.785 +				/* build message file name */
   1.786 +    snprintf (tmp,MAILTMPLEN,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
   1.787 +    if ((fd = open (tmp,O_RDONLY,NIL)) >= 0) {
   1.788 +      fstat (fd,&sbuf);		/* get size of message */
   1.789 +				/* make plausible IMAPish date string */
   1.790 +      tm = gmtime (&sbuf.st_mtime);
   1.791 +      elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
   1.792 +      elt->year = tm->tm_year + 1900 - BASEYEAR;
   1.793 +      elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
   1.794 +      elt->seconds = tm->tm_sec;
   1.795 +      elt->zhours = 0; elt->zminutes = 0;
   1.796 +				/* slurp message */
   1.797 +      read (fd,s = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
   1.798 +      s[sbuf.st_size] = '\0';	/* tie off file */
   1.799 +      close (fd);		/* close file */
   1.800 +
   1.801 +      for (i = 0,b = s; *b && !(i && (*b == '\n')); i = (*b++ == '\n'));
   1.802 +      hdrsize = (*b ? ++b:b)-s;	/* number of header bytes */
   1.803 +
   1.804 +      elt->rfc822_size =          /* size of entire message in CRLF form */
   1.805 +       (elt->private.msg.header.text.size =
   1.806 +        strcrlfcpy ((char **) &elt->private.msg.header.text.data,&i,s,
   1.807 +                    hdrsize)) +
   1.808 +          (elt->private.msg.text.text.size =
   1.809 +           strcrlfcpy ((char **) &elt->private.msg.text.text.data,&i,b,
   1.810 +                       sbuf.st_size - hdrsize));
   1.811 +      fs_give ((void **) &s);
   1.812 +    } else return "";
   1.813 +
   1.814 +  }
   1.815 +
   1.816 +  *length = elt->private.msg.header.text.size;
   1.817 +  return (char *) elt->private.msg.header.text.data;
   1.818 +}
   1.819 +
   1.820 +void maildir_fast (MAILSTREAM *stream,char *sequence,long flags)
   1.821 +{
   1.822 +  unsigned long i,j;
   1.823 +				/* ugly and slow */
   1.824 +  if (stream && LOCAL && ((flags & FT_UID) ?
   1.825 +			  mail_uid_sequence (stream,sequence) :
   1.826 +			  mail_sequence (stream,sequence)))
   1.827 +    for (i = 1; i <= stream->nmsgs; i++)
   1.828 +      if (mail_elt (stream,i)->sequence) maildir_fetchheader (stream,i,&j,NIL);
   1.829 +}
   1.830 +
   1.831 +/* Maildir find list of subscribed mailboxes
   1.832 + * Accepts: mail stream
   1.833 + *	    pattern to search
   1.834 + */
   1.835 +
   1.836 +void maildir_list (MAILSTREAM *stream,char *ref, char *pat)
   1.837 +{
   1.838 +  DIR *dp;
   1.839 +  struct direct *d;
   1.840 +  struct stat sbuf;
   1.841 +  char *s,*s2,*cp,*np,curdir[MAILTMPLEN],name[MAILTMPLEN],subdir[MAILTMPLEN],rname[MAILTMPLEN];
   1.842 +
   1.843 +  strncpy(subdir, pat, MAILTMPLEN);
   1.844 +
   1.845 +  s = subdir;
   1.846 +  s2 = 0;
   1.847 +  while ((s = strstr(s, "/"))) {
   1.848 +  	s2 = s;
   1.849 +	s++;
   1.850 +	}
   1.851 +
   1.852 +  if (s2) { /* pointer to last / */
   1.853 +  	*s2 = '\0'; /* snip off string */
   1.854 +	s2++;
   1.855 +	/*syslog(LOG_INFO, "dir: '%s' pat: '%s'", subdir, s2);*/
   1.856 +  	snprintf(name, MAILTMPLEN, "%s/%s", myhomedir(), subdir);
   1.857 +	} else {
   1.858 +	s2 = pat;
   1.859 +	snprintf(name, MAILTMPLEN, "%s", myhomedir());
   1.860 +	strcpy(subdir, "");
   1.861 +	}
   1.862 +
   1.863 + maildir_listwork(name, stream, s2, subdir, 1);
   1.864 +
   1.865 + if (pmatch ("INBOX", pat))
   1.866 + 	mm_list (stream,NIL,"INBOX",LATT_NOINFERIORS);
   1.867 +
   1.868 +}
   1.869 +
   1.870 +void maildir_listwork(char *name1, MAILSTREAM *stream, char *s2, char *subdir, int flag)
   1.871 +{
   1.872 +  DIR *dp;
   1.873 +  struct direct *d;
   1.874 +  struct stat sbuf;
   1.875 +  char *s,*cp,*np,curdir[MAILTMPLEN],rname[MAILTMPLEN],name[MAILTMPLEN];
   1.876 +  char tmpdir[MAILTMPLEN];
   1.877 +  
   1.878 +  strncpy(name, name1, MAILTMPLEN);
   1.879 +  
   1.880 +  /*syslog(LOG_INFO, "opendir: '%s'", name);
   1.881 +  //syslog(LOG_INFO, "subdir: '%s'", subdir);
   1.882 +   */ 
   1.883 +  if (dp = opendir (name)) {	/* open directory */
   1.884 +    while (d = readdir (dp))	/* scan directory, ignore all . names */
   1.885 +      if (d->d_name[0] != '.' &&
   1.886 +	  strcmp(d->d_name, "Maildir")) {/* build file name */
   1.887 +	/* we do not want to list Maildir to customers, because
   1.888 +	   that might give them weird ideas :) */
   1.889 +
   1.890 +	if (subdir[0] != '\0' && subdir[0] != '%' && subdir[0] != '*')
   1.891 +		snprintf(name, MAILTMPLEN, "%s/%s", subdir, d->d_name);
   1.892 +	else
   1.893 +		snprintf(name, MAILTMPLEN, "%s", d->d_name);
   1.894 +	/*syslog(LOG_INFO, "ls: '%s'", name);*/
   1.895 +
   1.896 +	/* this matching might not be correctly implemented	*/
   1.897 +	if (maildir_file(curdir, name) &&
   1.898 +      		stat (curdir,&sbuf) == 0 && S_ISDIR (sbuf.st_mode)) {
   1.899 +		/*syslog(LOG_INFO, "here for some stange reason");*/
   1.900 +		if (pmatch(d->d_name, s2)) {
   1.901 +			mm_list (stream,'/',name,NIL);
   1.902 +			/* call function recursive if last char % */
   1.903 +			/*syslog(LOG_INFO, "s2: '%s'",s2);*/
   1.904 +			if (s2[strlen(s2)-1] == '%'
   1.905 +			||  s2[strlen(s2)-1] == '*') {
   1.906 +				snprintf(rname, MAILTMPLEN, "%s/%s", name1, name);
   1.907 +				/*syslog(LOG_INFO, "rname: '%s'", rname);*/
   1.908 +				maildir_listwork(rname, stream, "%", name, 1);
   1.909 +				}			
   1.910 +			}
   1.911 +		} else
   1.912 +	if (strlen(curdir) > 4 && pmatch(d->d_name, s2)) { 
   1.913 +  		int fnlen = strlen (curdir);
   1.914 +  		curdir[fnlen - 4] = '\0';
   1.915 +  		/*syslog(LOG_INFO, "fname: '%s'", curdir);*/
   1.916 +	if (stat (curdir,&sbuf) == 0 && S_ISDIR (sbuf.st_mode)) {
   1.917 +		/* okay, do not list with NoSelect, since Netscape
   1.918 +		   does not f*cking honor this flag.. stupid netscape */
   1.919 +		/*mm_list (stream,'/',name,LATT_NOSELECT);*/
   1.920 +
   1.921 +			/* call function recursive if last char % */
   1.922 +			/*syslog(LOG_INFO, "s2: '%s'",s2);*/
   1.923 +			if (s2[strlen(s2)-1] == '%' 
   1.924 +			||  s2[strlen(s2)-1] == '*') {
   1.925 +				snprintf(rname, MAILTMPLEN, "%s/%s", name1, name);
   1.926 +				/*syslog(LOG_INFO, "rname: '%s'", rname);*/
   1.927 +				maildir_listwork(rname, stream, "%", name, 1);
   1.928 +				}			
   1.929 +
   1.930 +		}
   1.931 +	    }
   1.932 +
   1.933 +	}
   1.934 +      closedir (dp);		/* all done, flush directory */
   1.935 +      }
   1.936 +
   1.937 +}
   1.938 +
   1.939 +
   1.940 +
   1.941 +
   1.942 +
   1.943 +void *maildir_parameters (long function,void *value)
   1.944 +{
   1.945 +  return NIL;
   1.946 +}
   1.947 +
   1.948 +long maildir_create (MAILSTREAM *stream,char *mailbox)
   1.949 +{
   1.950 +  char tmp[MAILTMPLEN];
   1.951 +  char err[MAILTMPLEN];
   1.952 +  char *s, *s2;
   1.953 +  int fnlen, i;
   1.954 +  char *subdir_names[] = {"/cur","/new","/tmp",NULL};
   1.955 +
   1.956 +				/* must not already exist */
   1.957 +  if (access (maildir_file (tmp,mailbox),F_OK) == 0) {
   1.958 +    snprintf (err,MAILTMPLEN,"Can't create mailbox %s: mailbox already exists",mailbox);
   1.959 +    mm_log (err,ERROR);
   1.960 +    return NIL;
   1.961 +  }
   1.962 +  
   1.963 +  maildir_file (tmp,mailbox);	/* get file name */
   1.964 +  fnlen = strlen (tmp);
   1.965 +  /*syslog(LOG_INFO, "fname: '%s'", tmp);*/
   1.966 +  tmp[fnlen - 4] = '\0';	/* making main directory's name */
   1.967 +  fnlen -= 4;
   1.968 +
   1.969 +  /* okay, try to add support for adding hiearchys of directories, this
   1.970 +     is done by scanning for /'s.... */
   1.971 +     
   1.972 +  /*syslog(LOG_INFO, "tmp '%s'", tmp);*/
   1.973 +  s = tmp;
   1.974 +
   1.975 +  while ((s = strstr(s, "/")) != 0) {
   1.976 +	/*syslog(LOG_INFO, "Before make: '%s'", s);*/
   1.977 +  	*s = '\0';
   1.978 +  	/*syslog(LOG_INFO, "Trying to make: '%s'", tmp);*/
   1.979 +	if (mkdir (tmp,0700) && *s != '\0') /* trying to make the dir */
   1.980 +	  if (errno != EEXIST) {
   1.981 +	    snprintf (err,MAILTMPLEN,"Can't create mailbox %s: %s %s",
   1.982 +		     mailbox,tmp,strerror (errno));
   1.983 +    	     mm_log (err,ERROR);
   1.984 +             return NIL;
   1.985 +	  }
   1.986 +  	*s = '/';
   1.987 +	s++;
   1.988 +  	} 
   1.989 +
   1.990 +  if (mkdir (tmp,0700)) {	/* try to make new dir */
   1.991 +    snprintf (err,MAILTMPLEN,"Can't create mailbox %s: %s %s",
   1.992 +	     mailbox,tmp,strerror (errno));
   1.993 +    mm_log (err,ERROR);
   1.994 +    return NIL;
   1.995 +  }
   1.996 +
   1.997 +  /*syslog(LOG_INFO, "create maildir");*/
   1.998 +  for (i = 0; subdir_names[i]; i++) {
   1.999 +    strncpy (tmp + fnlen,subdir_names[i],MAILTMPLEN);
  1.1000 +
  1.1001 +    if (mkdir (tmp,0700)) {	/* try to make new dir */
  1.1002 +      snprintf (err,MAILTMPLEN,"Can't create mailbox %s: %s %s",
  1.1003 +	       mailbox,tmp,strerror (errno));
  1.1004 +      mm_log (err,ERROR);
  1.1005 +      return NIL;
  1.1006 +    }
  1.1007 +  }
  1.1008 +
  1.1009 +  return T;			/* return success */
  1.1010 +}
  1.1011 +
  1.1012 +void maildir_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
  1.1013 +{
  1.1014 +  char oldfile[MAILTMPLEN],newfile[MAILTMPLEN],fn[MAILTMPLEN];
  1.1015 +  struct utimbuf tbuf;
  1.1016 +  char *s;
  1.1017 +
  1.1018 +                                /* build the new filename */
  1.1019 +  snprintf (oldfile,MAILTMPLEN,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
  1.1020 +  if ((s = strchr ((char *) elt->maildirp,':'))) *s = '\0';
  1.1021 +  snprintf (fn,MAILTMPLEN,"%s:2,%s%s%s%s",(char *) elt->maildirp,elt->flagged ? "F" : "",
  1.1022 +           elt->answered ? "R" : "",elt->seen ? "S" : "",
  1.1023 +           elt->deleted ? "T" : "");
  1.1024 +  snprintf (newfile,MAILTMPLEN,"%s/%s",LOCAL->dir,fn);
  1.1025 +                                /* rename the file with new flags */
  1.1026 +  if (rename (oldfile,newfile) < 0) {
  1.1027 +    snprintf(oldfile,MAILTMPLEN,"Unable to write flags to disk: %s",strerror (errno));
  1.1028 +    mm_log(oldfile,ERROR);
  1.1029 +    return;
  1.1030 +  }
  1.1031 +                                /* update the file name in cache */
  1.1032 +  fs_give ((void **) &elt->maildirp);
  1.1033 +  elt->maildirp = (long) cpystr (fn);
  1.1034 +
  1.1035 +  /* fix the UID on the file */
  1.1036 +  tbuf.actime = elt->private.uid;
  1.1037 +  tbuf.modtime = elt->private.uid;
  1.1038 +  chmod (newfile, S_IRUSR|S_IWUSR|S_IXUSR);
  1.1039 +  utime (newfile, &tbuf);
  1.1040 +
  1.1041 +}
  1.1042 +
  1.1043 +void maildir_expunge (MAILSTREAM *stream)
  1.1044 +{
  1.1045 +  MESSAGECACHE *elt;
  1.1046 +  unsigned long i = 1;
  1.1047 +  unsigned long n = 0;
  1.1048 +  unsigned long recent = stream->recent;
  1.1049 +  
  1.1050 +  maildir_gc (stream,GC_TEXTS);	/* invalidate texts */
  1.1051 +  mm_critical (stream);		/* go critical */
  1.1052 +  while (i <= stream->nmsgs) {	/* for each message */
  1.1053 +				/* if deleted, need to trash it */
  1.1054 +    if ((elt = mail_elt (stream,i))->deleted) {
  1.1055 +      snprintf (LOCAL->buf,MAILTMPLEN,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
  1.1056 +      if (unlink (LOCAL->buf)) {/* try to delete the message */
  1.1057 +	snprintf (LOCAL->buf,MAILTMPLEN,"Expunge of message %ld failed, aborted: %s",i,
  1.1058 +		 strerror (errno));
  1.1059 +	mm_log (LOCAL->buf,WARN);
  1.1060 +	break;
  1.1061 +      }
  1.1062 +				/* free the cached filename */
  1.1063 +      if (elt->maildirp) {
  1.1064 +	      fs_give ((void **) &elt->maildirp);
  1.1065 +	      elt->maildirp = 0; /* otherwise pine coredumps */
  1.1066 +		}
  1.1067 +      if (elt->recent) --recent;/* if recent, note one less recent message */
  1.1068 +      mail_expunged (stream,i);	/* notify upper levels */
  1.1069 +      n++;			/* count up one more expunged message */
  1.1070 +    }
  1.1071 +    else i++;			/* otherwise try next message */
  1.1072 +  }
  1.1073 +  if (n) {			/* output the news if any expunged */
  1.1074 +    snprintf (LOCAL->buf,MAILTMPLEN,"Expunged %ld messages",n);
  1.1075 +    mm_log (LOCAL->buf,(long) NIL);
  1.1076 +  }
  1.1077 +  else mm_log ("No messages deleted, so no update needed",(long) NIL);
  1.1078 +  mm_nocritical (stream);	/* release critical */
  1.1079 +				/* notify upper level of new mailbox size */
  1.1080 +  mail_exists (stream,stream->nmsgs);
  1.1081 +  mail_recent (stream,recent);
  1.1082 +}
  1.1083 +
  1.1084 +/* dont forget to process options in here */
  1.1085 +
  1.1086 +/* modified by sakira@sundent.mine.nu (03.01.13) */
  1.1087 +long maildir_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
  1.1088 +{
  1.1089 +  STRING st;
  1.1090 +  MESSAGECACHE *elt;
  1.1091 +  struct stat sbuf;
  1.1092 +  int fd;
  1.1093 +  unsigned long i;
  1.1094 +  char flags[MAILTMPLEN], date[MAILTMPLEN];
  1.1095 +
  1.1096 +				/* copy the messages */
  1.1097 +  if ((options & CP_UID) ? mail_uid_sequence (stream, sequence) : 
  1.1098 +  	mail_sequence (stream,sequence)) 
  1.1099 +  for (i = 1; i <= stream->nmsgs; i++)
  1.1100 +    if ((elt = mail_elt (stream,i))->sequence) {
  1.1101 +      sprintf (LOCAL->buf, "%s/%s", LOCAL->dir, (char *) elt->maildirp);
  1.1102 +      /*      snprintf (LOCAL->buf,MAILTMPLEN,"%s/%s",LOCAL->dir,(char *) elt->maildirp); */
  1.1103 +      if ((fd = open (LOCAL->buf,O_RDONLY,NIL)) < 0) return NIL;
  1.1104 +      fstat (fd,&sbuf);		/* get size of message */
  1.1105 +
  1.1106 +      if (!elt->day) {
  1.1107 +	struct tm *tm = gmtime (&sbuf.st_mtime);
  1.1108 +	elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
  1.1109 +	elt->year = tm->tm_year + 1900 - BASEYEAR;
  1.1110 +	elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
  1.1111 +	elt->seconds = tm->tm_sec;
  1.1112 +	elt->zhours = 0; elt->zminutes = 0;
  1.1113 +      }
  1.1114 +
  1.1115 +      if (sbuf.st_size > LOCAL->buflen) {
  1.1116 +	fs_give((void **) &LOCAL->buf);
  1.1117 +	LOCAL->buf = (char *) fs_get ((LOCAL->buflen = sbuf.st_size) + 1);
  1.1118 +      }
  1.1119 +
  1.1120 +				/* slurp message */
  1.1121 +      read(fd, LOCAL->buf, sbuf.st_size);
  1.1122 +      LOCAL->buf[sbuf.st_size] = '\0';
  1.1123 +      close(fd);
  1.1124 +      /* read (fd, s = (char *) fs_get (sbuf.st_size +1),sbuf.st_size); 
  1.1125 +      s[sbuf.st_size] = '\0';
  1.1126 +      close (fd); */
  1.1127 +
  1.1128 +      INIT (&st,mail_string,(void *) LOCAL->buf,sbuf.st_size);
  1.1129 +      flags[0] = flags[1] = '\0';
  1.1130 +      if (elt->seen) strcat(flags, " \\Seen");
  1.1131 +      if (elt->deleted) strcat(flags, " \\Deleted");
  1.1132 +      if (elt->answered) strcat(flags, " \\Answered");
  1.1133 +      if (elt->draft) strcat(flags, " \\Draft");
  1.1134 +      flags[0] = '(';
  1.1135 +      strcat(flags, ")");
  1.1136 +      mail_date(date, elt);
  1.1137 +      /* snprintf (LOCAL->buf,MAILTMPLEN,"%s%s%s%s%s)",
  1.1138 +	       elt->seen ? " \\Seen" : "",
  1.1139 +	       elt->deleted ? " \\Deleted" : "",
  1.1140 +	       elt->flagged ? " \\Flagged" : "",
  1.1141 +	       elt->answered ? " \\Answered" : "",
  1.1142 +	       (elt->seen || elt->deleted || elt->flagged || elt->answered) ?
  1.1143 +	       "" : " ");
  1.1144 +      LOCAL->buf[0] = '(';
  1.1145 +      mail_date (tmp,elt); */
  1.1146 +
  1.1147 +      if (!mail_append_full (NIL, mailbox, flags, date, &st)) return NIL;
  1.1148 +      if (options & CP_MOVE) elt->deleted = T;
  1.1149 +
  1.1150 +      /* if (!maildir_append (stream,mailbox,LOCAL->buf,tmp,&st)) {
  1.1151 +	fs_give ((void **) &s);	
  1.1152 +	return NIL; 
  1.1153 +	} */
  1.1154 +      /*      fs_give ((void **) &s); */	/* give back temporary space */
  1.1155 +    }
  1.1156 +  return T;			/* return success */
  1.1157 +}
  1.1158 +
  1.1159 +#if 0
  1.1160 +long maildir_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
  1.1161 +{
  1.1162 +  STRING st;
  1.1163 +  MESSAGECACHE *elt;
  1.1164 +  struct stat sbuf;
  1.1165 +  int fd;
  1.1166 +  long i;
  1.1167 +  char *s,tmp[MAILTMPLEN];
  1.1168 +				/* copy the messages */
  1.1169 +  if ((options & CP_UID) ? mail_uid_sequence (stream, sequence) : 
  1.1170 +  	mail_sequence (stream,sequence)) 
  1.1171 +  for (i = 1; i <= stream->nmsgs; i++)
  1.1172 +    if ((elt = mail_elt (stream,i))->sequence) {
  1.1173 +      snprintf (LOCAL->buf,MAILTMPLEN,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
  1.1174 +      if ((fd = open (LOCAL->buf,O_RDONLY,NIL)) < 0) return NIL;
  1.1175 +      fstat (fd,&sbuf);		/* get size of message */
  1.1176 +				/* slurp message */
  1.1177 +      read (fd,s = (char *) fs_get (sbuf.st_size +1),sbuf.st_size);
  1.1178 +      s[sbuf.st_size] = '\0';	/* tie off file */
  1.1179 +      close (fd);		/* flush message file */
  1.1180 +      INIT (&st,mail_string,(void *) s,sbuf.st_size);
  1.1181 +      snprintf (LOCAL->buf,MAILTMPLEN,"%s%s%s%s%s)",
  1.1182 +	       elt->seen ? " \\Seen" : "",
  1.1183 +	       elt->deleted ? " \\Deleted" : "",
  1.1184 +	       elt->flagged ? " \\Flagged" : "",
  1.1185 +	       elt->answered ? " \\Answered" : "",
  1.1186 +	       (elt->seen || elt->deleted || elt->flagged || elt->answered) ?
  1.1187 +	       "" : " ");
  1.1188 +      LOCAL->buf[0] = '(';	/* open list */
  1.1189 +      mail_date (tmp,elt);	/* generate internal date */
  1.1190 +      if (!maildir_append (stream,mailbox,LOCAL->buf,tmp,&st)) {
  1.1191 +	fs_give ((void **) &s);	/* give back temporary space */
  1.1192 +	return NIL;
  1.1193 +      }
  1.1194 +      fs_give ((void **) &s);	/* give back temporary space */
  1.1195 +    }
  1.1196 +  return T;			/* return success */
  1.1197 +}
  1.1198 +#endif
  1.1199 +
  1.1200 +long maildir_append (MAILSTREAM *stream, char *mailbox, append_t af, void *data)
  1.1201 +{
  1.1202 +  int fd;
  1.1203 +  char c, *s;
  1.1204 +  char *flags, *date;
  1.1205 +  char tmp[MAILTMPLEN],file[MAILTMPLEN],path1[MAILTMPLEN],path2[MAILTMPLEN];
  1.1206 +  MESSAGECACHE elt;
  1.1207 +  STRING *message;
  1.1208 +  long i;
  1.1209 +  long size = 0;
  1.1210 +  long ret = LONGT;
  1.1211 +  short uf = 0;
  1.1212 +  
  1.1213 +  /*  
  1.1214 +     This is intentionaly made static.  Users can ask to save a LOT of messages
  1.1215 +     at once and this program can do that within one second. Dan's assumption
  1.1216 +     that time+pid+hostname always will be unique stops being true in this
  1.1217 +     case. So we will add yet another number to host part of message file's
  1.1218 +     name. Hostname is used only to make filename unique and Dan  explicitly
  1.1219 +     says that "<...>  Other than this [skipping filenames starting at dot] ,
  1.1220 +     readers should not attempt to parse filenames. <...>". Therefore this 
  1.1221 +     addition should be no problem. Am I right, Dan?   --AK
  1.1222 +  */ 
  1.1223 +  
  1.1224 +  static unsigned int transact = 0;
  1.1225 +
  1.1226 +  if (!stream) stream = &maildirproto;
  1.1227 +
  1.1228 +  if (flags) 			/* get flags if given */
  1.1229 +    uf = maildir_getflags (user_flags (&maildirproto),flags);
  1.1230 +
  1.1231 +				/* N.B.: can't use LOCAL->buf for tmp */
  1.1232 +				/* make sure valid mailbox */
  1.1233 +  if (!maildir_isvalid (mailbox, NIL)) {
  1.1234 +    snprintf (tmp,MAILTMPLEN,"Not a valid Maildir mailbox: %s",mailbox);
  1.1235 +    mm_log (tmp,ERROR);
  1.1236 +    return NIL;
  1.1237 +  }
  1.1238 +
  1.1239 +  if (!(*af)(stream, data, &flags, &date, &message)) return NIL;
  1.1240 +
  1.1241 +  mm_critical(stream);	/* go critical */
  1.1242 +  do {
  1.1243 +    if (!SIZE(message)) { /* guard againt zero-length */
  1.1244 +      mm_log("Append of zero-length message", ERROR);
  1.1245 +      ret = NIL;
  1.1246 +      break;
  1.1247 +    }
  1.1248 +
  1.1249 +    /* build file name we will use */
  1.1250 +    snprintf (file,MAILTMPLEN,"%u.%d.%09u.%s:3,%s%s%s%s",
  1.1251 +	      time (0),getpid (),transact++,mylocalhost (),
  1.1252 +	      uf&fFLAGGED ? "F" : "",uf&fANSWERED ? "R" : "",
  1.1253 +	      uf&fSEEN ? "S" : "",uf&fDELETED ? "T" : "");
  1.1254 +				/* build tmp file name */
  1.1255 +    snprintf (path1,MAILTMPLEN,"%s/../tmp/%s",maildir_file (tmp,mailbox),file);
  1.1256 +  
  1.1257 +    if ((fd = open (path1,O_WRONLY|O_CREAT|O_EXCL,S_IREAD|S_IWRITE)) < 0) {
  1.1258 +      snprintf (tmp,MAILTMPLEN,"Can't open append mailbox: %s",strerror (errno));
  1.1259 +      mm_log (tmp,ERROR);
  1.1260 +      return NIL;
  1.1261 +    }
  1.1262 +
  1.1263 +    i = SIZE (message);		/* get size of message */
  1.1264 +    s = (char *) fs_get (i + 1);	/* get space for the data */
  1.1265 +				/* copy the data w/o CR's */
  1.1266 +    while (i--) if ((c = SNX (message)) != '\015') s[size++] = c;
  1.1267 +				/* write the data */
  1.1268 +    if ((write (fd,s,size) < 0) || fsync (fd)) {
  1.1269 +      unlink (path1);		/* delete message */
  1.1270 +      snprintf (tmp,MAILTMPLEN,"Message append failed: %s",strerror (errno));
  1.1271 +      mm_log (tmp,ERROR);
  1.1272 +      ret = NIL;
  1.1273 +    }
  1.1274 +				/* build final filename to use */
  1.1275 +    snprintf (path2,MAILTMPLEN,"%s/../new/%s",maildir_file (tmp,mailbox),file);
  1.1276 +    if (link (path1,path2) < 0) {
  1.1277 +      snprintf (tmp,MAILTMPLEN,"Message append failed: %s",strerror (errno));
  1.1278 +      mm_log (tmp,ERROR);
  1.1279 +      ret = NIL;
  1.1280 +    }
  1.1281 +    unlink (path1);
  1.1282 +    fs_give ((void **) &s);	/* flush the buffer */
  1.1283 +    close (fd);			/* close the file */
  1.1284 +
  1.1285 +    if (ret) {
  1.1286 +      if (!(*af)(stream, data, &flags, &date, &message)) ret = NIL;
  1.1287 +    }
  1.1288 +  } while(ret && message);
  1.1289 +  
  1.1290 +  mm_nocritical (stream);	/* release critical */
  1.1291 +  return ret;
  1.1292 +}
  1.1293 +
  1.1294 +#if 0
  1.1295 +long maildir_append (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
  1.1296 +		   STRING *message)
  1.1297 +{
  1.1298 +  int fd;
  1.1299 +  char c,*s;
  1.1300 +  char tmp[MAILTMPLEN],file[MAILTMPLEN],path1[MAILTMPLEN],path2[MAILTMPLEN];
  1.1301 +  MESSAGECACHE elt;
  1.1302 +  long i;
  1.1303 +  long size = 0;
  1.1304 +  long ret = LONGT;
  1.1305 +  short uf = 0;
  1.1306 +  
  1.1307 +  /*  
  1.1308 +     This is intentionaly made static.  Users can ask to save a LOT of messages
  1.1309 +     at once and this program can do that within one second. Dan's assumption
  1.1310 +     that time+pid+hostname always will be unique stops being true in this
  1.1311 +     case. So we will add yet another number to host part of message file's
  1.1312 +     name. Hostname is used only to make filename unique and Dan  explicitly
  1.1313 +     says that "<...>  Other than this [skipping filenames starting at dot] ,
  1.1314 +     readers should not attempt to parse filenames. <...>". Therefore this 
  1.1315 +     addition should be no problem. Am I right, Dan?   --AK
  1.1316 +  */ 
  1.1317 +  
  1.1318 +  static unsigned int transact = 0;
  1.1319 +
  1.1320 +  if (flags) 			/* get flags if given */
  1.1321 +    uf = maildir_getflags (user_flags (&maildirproto),flags);
  1.1322 +
  1.1323 +/*  if (date) {			want to preserve date?
  1.1324 +    //syslog(LOG_INFO, "date: '%s'", date);
  1.1325 +				// yes, parse date into an elt
  1.1326 +    if (!mail_parse_date (&elt,date)) {
  1.1327 +      snprintf (tmp,MAILTMPLEN,"Bad date in append: %s",date);
  1.1328 +      mm_log (tmp,ERROR);
  1.1329 +      return NIL;
  1.1330 +    }
  1.1331 +  } */
  1.1332 +				/* N.B.: can't use LOCAL->buf for tmp */
  1.1333 +				/* make sure valid mailbox */
  1.1334 +  if (!maildir_isvalid (mailbox, NIL)) {
  1.1335 +    snprintf (tmp,MAILTMPLEN,"Not a valid Maildir mailbox: %s",mailbox);
  1.1336 +    mm_log (tmp,ERROR);
  1.1337 +    return NIL;
  1.1338 +  }
  1.1339 +				/* build file name we will use */
  1.1340 +  snprintf (file,MAILTMPLEN,"%u.%d.%09u.%s:3,%s%s%s%s",
  1.1341 +	   time (0),getpid (),transact++,mylocalhost (),
  1.1342 +	   uf&fFLAGGED ? "F" : "",uf&fANSWERED ? "R" : "",
  1.1343 +	   uf&fSEEN ? "S" : "",uf&fDELETED ? "T" : "");
  1.1344 +				/* build tmp file name */
  1.1345 +  snprintf (path1,MAILTMPLEN,"%s/../tmp/%s",maildir_file (tmp,mailbox),file);
  1.1346 +  
  1.1347 +  if ((fd = open (path1,O_WRONLY|O_CREAT|O_EXCL,S_IREAD|S_IWRITE)) < 0) {
  1.1348 +    snprintf (tmp,MAILTMPLEN,"Can't open append mailbox: %s",strerror (errno));
  1.1349 +    mm_log (tmp,ERROR);
  1.1350 +    return NIL;
  1.1351 +  }
  1.1352 +  i = SIZE (message);		/* get size of message */
  1.1353 +  s = (char *) fs_get (i + 1);	/* get space for the data */
  1.1354 +				/* copy the data w/o CR's */
  1.1355 +  while (i--) if ((c = SNX (message)) != '\015') s[size++] = c;
  1.1356 +  mm_critical (stream);		/* go critical */
  1.1357 +				/* write the data */
  1.1358 +  if ((write (fd,s,size) < 0) || fsync (fd)) {
  1.1359 +    unlink (path1);		/* delete message */
  1.1360 +    snprintf (tmp,MAILTMPLEN,"Message append failed: %s",strerror (errno));
  1.1361 +    mm_log (tmp,ERROR);
  1.1362 +    ret = NIL;
  1.1363 +  }
  1.1364 +				/* build final filename to use */
  1.1365 +  snprintf (path2,MAILTMPLEN,"%s/../new/%s",maildir_file (tmp,mailbox),file);
  1.1366 +  if (link (path1,path2) < 0) {
  1.1367 +    snprintf (tmp,MAILTMPLEN,"Message append failed: %s",strerror (errno));
  1.1368 +    mm_log (tmp,ERROR);
  1.1369 +    ret = NIL;
  1.1370 +  }
  1.1371 +  unlink (path1);
  1.1372 +  
  1.1373 +  close (fd);			/* close the file */
  1.1374 +  mm_nocritical (stream);	/* release critical */
  1.1375 +  fs_give ((void **) &s);	/* flush the buffer */
  1.1376 +  return ret;
  1.1377 +}
  1.1378 +#endif
  1.1379 +
  1.1380 +short bezerk_getflags (MAILSTREAM *stream,char *flag)
  1.1381 +{
  1.1382 +  char *t,tmp[MAILTMPLEN],err[MAILTMPLEN];
  1.1383 +  short f = 0;
  1.1384 +  short i,j;
  1.1385 +  if (flag && *flag) {		/* no-op if no flag string */
  1.1386 +				/* check if a list and make sure valid */
  1.1387 +    if ((i = (*flag == '(')) ^ (flag[strlen (flag)-1] == ')')) {
  1.1388 +      mm_log ("Bad flag list",ERROR);
  1.1389 +      return NIL;
  1.1390 +    }
  1.1391 +				/* copy the flag string w/o list construct */
  1.1392 +    strncpy (tmp,flag+i,(j = strlen (flag) - (2*i)));
  1.1393 +    tmp[j] = '\0';
  1.1394 +    t = ucase (tmp);		/* uppercase only from now on */
  1.1395 +
  1.1396 +    while (t && *t) {		/* parse the flags */
  1.1397 +      if (*t == '\\') {		/* system flag? */
  1.1398 +	switch (*++t) {		/* dispatch based on first character */
  1.1399 +	case 'S':		/* possible \Seen flag */
  1.1400 +	  if (t[1] == 'E' && t[2] == 'E' && t[3] == 'N') i = fSEEN;
  1.1401 +	  t += 4;		/* skip past flag name */
  1.1402 +	  break;
  1.1403 +	case 'D':		/* possible \Deleted flag */
  1.1404 +	  if (t[1] == 'E' && t[2] == 'L' && t[3] == 'E' && t[4] == 'T' &&
  1.1405 +	      t[5] == 'E' && t[6] == 'D') i = fDELETED;
  1.1406 +	  t += 7;		/* skip past flag name */
  1.1407 +	  break;
  1.1408 +	case 'F':		/* possible \Flagged flag */
  1.1409 +	  if (t[1] == 'L' && t[2] == 'A' && t[3] == 'G' && t[4] == 'G' &&
  1.1410 +	      t[5] == 'E' && t[6] == 'D') i = fFLAGGED;
  1.1411 +	  t += 7;		/* skip past flag name */
  1.1412 +	  break;
  1.1413 +	case 'A':		/* possible \Answered flag */
  1.1414 +	  if (t[1] == 'N' && t[2] == 'S' && t[3] == 'W' && t[4] == 'E' &&
  1.1415 +	      t[5] == 'R' && t[6] == 'E' && t[7] == 'D') i = fANSWERED;
  1.1416 +	  t += 8;		/* skip past flag name */
  1.1417 +	  break;
  1.1418 +	default:		/* unknown */
  1.1419 +	  i = 0;
  1.1420 +	  break;
  1.1421 +	}
  1.1422 +				/* add flag to flags list */
  1.1423 +	if (i && ((*t == '\0') || (*t++ == ' '))) f |= i;
  1.1424 +      }
  1.1425 +      else {			/* no user flags yet */
  1.1426 +	return f;
  1.1427 +	t = strtok (t," ");	/* isolate flag name */
  1.1428 +	/*	snprintf (err,MAILTMPLEN,"Unknown flag: %.80s",t); */
  1.1429 +	t = strtok (NIL," ");	/* get next flag */
  1.1430 +	/*	mm_log (err,ERROR); */
  1.1431 +      }
  1.1432 +    }
  1.1433 +  }
  1.1434 +  return f;
  1.1435 +}
  1.1436 +
  1.1437 +short maildir_getflags (MAILSTREAM *stream,char *flag)
  1.1438 +{
  1.1439 +  return bezerk_getflags (stream,flag); /* nothing exciting, reuse old code */
  1.1440 +}
  1.1441 +
  1.1442 +long maildir_delete (MAILSTREAM *stream,char *mailbox)
  1.1443 +{
  1.1444 +  DIR *dirp;
  1.1445 +  struct direct *d;
  1.1446 +  int i,j;
  1.1447 +  char tmp[MAILTMPLEN],err[MAILTMPLEN];
  1.1448 +  char *subdir_names[] = {"cur/","new/","tmp/",NULL};
  1.1449 +
  1.1450 +				/* check if mailbox even exists */
  1.1451 +  if (!maildir_isvalid (mailbox,NIL)) {
  1.1452 +/*    sprintf (tmp,"Can't delete mailbox %s: no such mailbox",mailbox);
  1.1453 +    mm_log (tmp,ERROR);
  1.1454 +    return NIL; */
  1.1455 +    /*syslog(LOG_INFO, "Invalid maildir in delete()"); */
  1.1456 +    return T; /* well.. a stupid hack to get by a problem in netscape ..
  1.1457 +    		it remembers folders locally, and if a folder is deleted on
  1.1458 +    		another machine, you have no way removing it on any other
  1.1459 +    		netscapes... */
  1.1460 +  }
  1.1461 +
  1.1462 +				/* get name of directory */
  1.1463 +  i = strlen (maildir_file (tmp,mailbox)) + 1;
  1.1464 +  for (j = 0; subdir_names[j]; j++) {
  1.1465 +    strncpy (tmp + i - 4,subdir_names[j], MAILTMPLEN);
  1.1466 +    if (dirp = opendir (tmp)) {	/* open directory */
  1.1467 +      while (d = readdir (dirp))	/* empty the directory */
  1.1468 +	if (strcmp (d->d_name,".") && strcmp (d->d_name,"..")) {
  1.1469 +	  strncpy (tmp + i,d->d_name, MAILTMPLEN);
  1.1470 +	  /*syslog(LOG_INFO, "unlink1: '%s'");*/
  1.1471 +	  unlink (tmp);
  1.1472 +	}
  1.1473 +      closedir (dirp);		/* flush directory */
  1.1474 +    }
  1.1475 +				/* remove the subdir */
  1.1476 +    tmp[i + 3] = '\0';
  1.1477 +    /*syslog(LOG_INFO, "tmp: '%s'", tmp);*/
  1.1478 +    if (rmdir (tmp)) {
  1.1479 +    /*  sprintf (err,"Can't delete directory %s: %s",tmp,strerror (errno));
  1.1480 +      mm_log (err,ERROR);*/
  1.1481 +    }
  1.1482 +  }
  1.1483 +
  1.1484 +				/* try to remove the directory */
  1.1485 +  *(tmp + i - 5) = '\0';
  1.1486 +  /*syslog(LOG_INFO, "tmp2: '%s'", tmp);*/
  1.1487 +  if (rmdir (tmp)) {
  1.1488 +/*    sprintf (err,"Can't delete mailbox %s: %s",mailbox,strerror (errno));
  1.1489 +    mm_log (err,ERROR);
  1.1490 +    return NIL; */
  1.1491 +  }
  1.1492 +  return T;			/* return success */
  1.1493 +}
  1.1494 +
  1.1495 +long maildir_rename (MAILSTREAM *stream,char *old,char *new)
  1.1496 +{
  1.1497 +  char tmp[MAILTMPLEN],tmpnew[MAILTMPLEN];
  1.1498 +
  1.1499 +				/* old mailbox name must be valid */
  1.1500 +  if (!maildir_isvalid (old,NIL)) {
  1.1501 +    snprintf (tmp,MAILTMPLEN,"Can't rename mailbox %s: no such mailbox",old);
  1.1502 +    mm_log (tmp,ERROR);
  1.1503 +    return NIL;
  1.1504 +  }
  1.1505 +
  1.1506 +				/* new mailbox name must not exist */
  1.1507 +  if (access (maildir_file (tmp,new),F_OK) == 0) {
  1.1508 +    snprintf (tmp,MAILTMPLEN,"Can't rename to mailbox %s: destination already exists",new);
  1.1509 +    mm_log (tmp,ERROR);
  1.1510 +    return NIL;
  1.1511 +  }
  1.1512 +
  1.1513 +				/* try to rename the directory */
  1.1514 +  if (rename (maildir_file (tmp,old),maildir_file (tmpnew,new))) {
  1.1515 +    snprintf (tmp,MAILTMPLEN,"Can't rename mailbox %s to %s: %s",old,new,strerror (errno));
  1.1516 +    mm_log (tmp,ERROR);
  1.1517 +    return NIL;
  1.1518 +  }
  1.1519 +  return T;			/* return success */
  1.1520 +}
  1.1521 +
  1.1522 +long maildir_sub (MAILSTREAM *stream,char *mailbox)
  1.1523 +{
  1.1524 +  char tmp[MAILTMPLEN];
  1.1525 +  return sm_subscribe (mailbox);
  1.1526 +}
  1.1527 +
  1.1528 +long maildir_unsub (MAILSTREAM *stream,char *mailbox)
  1.1529 +{
  1.1530 +  char tmp[MAILTMPLEN];
  1.1531 +  return sm_unsubscribe (mailbox);
  1.1532 +}
  1.1533 +
  1.1534 +void maildir_lsub (MAILSTREAM *stream,char *ref,char *pat)
  1.1535 +{
  1.1536 + void *sdb = NIL;
  1.1537 +  char *s;
  1.1538 +                                /* get canonical form of name */
  1.1539 +  if ((s = sm_read (&sdb))) {
  1.1540 +    do if (pmatch_full (s,pat,'/')) mm_lsub (stream,'/',s,NIL);
  1.1541 +    while (s = sm_read (&sdb)); /* until no more subscriptions */
  1.1542 +  }
  1.1543 +
  1.1544 +}

UW-IMAP'd extensions by yuuji