imapext-2007

annotate src/osdep/os2/mbxnt.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
rev   line source
yuuji@0 1 /* ========================================================================
yuuji@0 2 * Copyright 1988-2007 University of Washington
yuuji@0 3 *
yuuji@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
yuuji@0 5 * you may not use this file except in compliance with the License.
yuuji@0 6 * You may obtain a copy of the License at
yuuji@0 7 *
yuuji@0 8 * http://www.apache.org/licenses/LICENSE-2.0
yuuji@0 9 *
yuuji@0 10 *
yuuji@0 11 * ========================================================================
yuuji@0 12 */
yuuji@0 13
yuuji@0 14 /*
yuuji@0 15 * Program: MBX mail routines
yuuji@0 16 *
yuuji@0 17 * Author: Mark Crispin
yuuji@0 18 * Networks and Distributed Computing
yuuji@0 19 * Computing & Communications
yuuji@0 20 * University of Washington
yuuji@0 21 * Administration Building, AG-44
yuuji@0 22 * Seattle, WA 98195
yuuji@0 23 * Internet: MRC@CAC.Washington.EDU
yuuji@0 24 *
yuuji@0 25 * Date: 3 October 1995
yuuji@0 26 * Last Edited: 28 September 2007
yuuji@0 27 */
yuuji@0 28
yuuji@0 29
yuuji@0 30 /* FILE TIME SEMANTICS
yuuji@0 31 *
yuuji@0 32 * The atime is the last read time of the file.
yuuji@0 33 * The mtime is the last flags update time of the file.
yuuji@0 34 * The ctime is the last write time of the file.
yuuji@0 35 */
yuuji@0 36
yuuji@0 37 #include <stdio.h>
yuuji@0 38 #include <ctype.h>
yuuji@0 39 #include <errno.h>
yuuji@0 40 extern int errno; /* just in case */
yuuji@0 41 #include "mail.h"
yuuji@0 42 #include "osdep.h"
yuuji@0 43 #include <fcntl.h>
yuuji@0 44 #include <time.h>
yuuji@0 45 #include <sys/stat.h>
yuuji@0 46 #include <sys/utime.h>
yuuji@0 47 #include "misc.h"
yuuji@0 48 #include "dummy.h"
yuuji@0 49 #include "fdstring.h"
yuuji@0 50
yuuji@0 51
yuuji@0 52 /* Build parameters */
yuuji@0 53
yuuji@0 54 #define HDRSIZE 2048
yuuji@0 55
yuuji@0 56 /* MBX I/O stream local data */
yuuji@0 57
yuuji@0 58 typedef struct mbx_local {
yuuji@0 59 unsigned int flagcheck: 1; /* if ping should sweep for flags */
yuuji@0 60 unsigned int expok: 1; /* if expunging OK in ping */
yuuji@0 61 unsigned int expunged : 1; /* if one or more expunged messages */
yuuji@0 62 int fd; /* file descriptor for I/O */
yuuji@0 63 int ld; /* lock file descriptor */
yuuji@0 64 int ffuserflag; /* first free user flag */
yuuji@0 65 off_t filesize; /* file size parsed */
yuuji@0 66 time_t filetime; /* last file time */
yuuji@0 67 time_t lastsnarf; /* last snarf time */
yuuji@0 68 unsigned char *buf; /* temporary buffer */
yuuji@0 69 unsigned long buflen; /* current size of temporary buffer */
yuuji@0 70 char lock[MAILTMPLEN]; /* buffer to write lock name */
yuuji@0 71 } MBXLOCAL;
yuuji@0 72
yuuji@0 73
yuuji@0 74 /* Convenient access to local data */
yuuji@0 75
yuuji@0 76 #define LOCAL ((MBXLOCAL *) stream->local)
yuuji@0 77
yuuji@0 78 /* Function prototypes */
yuuji@0 79
yuuji@0 80 DRIVER *mbx_valid (char *name);
yuuji@0 81 int mbx_isvalid (MAILSTREAM **stream,char *name,char *file,int *ld,char *lock,
yuuji@0 82 long flags);
yuuji@0 83 void *mbx_parameters (long function,void *value);
yuuji@0 84 void mbx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
yuuji@0 85 void mbx_list (MAILSTREAM *stream,char *ref,char *pat);
yuuji@0 86 void mbx_lsub (MAILSTREAM *stream,char *ref,char *pat);
yuuji@0 87 long mbx_create (MAILSTREAM *stream,char *mailbox);
yuuji@0 88 long mbx_delete (MAILSTREAM *stream,char *mailbox);
yuuji@0 89 long mbx_rename (MAILSTREAM *stream,char *old,char *newname);
yuuji@0 90 long mbx_status (MAILSTREAM *stream,char *mbx,long flags);
yuuji@0 91 MAILSTREAM *mbx_open (MAILSTREAM *stream);
yuuji@0 92 void mbx_close (MAILSTREAM *stream,long options);
yuuji@0 93 void mbx_abort (MAILSTREAM *stream);
yuuji@0 94 void mbx_flags (MAILSTREAM *stream,char *sequence,long flags);
yuuji@0 95 char *mbx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
yuuji@0 96 long flags);
yuuji@0 97 long mbx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
yuuji@0 98 void mbx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
yuuji@0 99 void mbx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
yuuji@0 100 long mbx_ping (MAILSTREAM *stream);
yuuji@0 101 void mbx_check (MAILSTREAM *stream);
yuuji@0 102 long mbx_expunge (MAILSTREAM *stream,char *sequence,long options);
yuuji@0 103 void mbx_snarf (MAILSTREAM *stream);
yuuji@0 104 long mbx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
yuuji@0 105 long mbx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
yuuji@0 106
yuuji@0 107 long mbx_parse (MAILSTREAM *stream);
yuuji@0 108 MESSAGECACHE *mbx_elt (MAILSTREAM *stream,unsigned long msgno,long expok);
yuuji@0 109 unsigned long mbx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt);
yuuji@0 110 void mbx_update_header (MAILSTREAM *stream);
yuuji@0 111 void mbx_update_status (MAILSTREAM *stream,unsigned long msgno,long flags);
yuuji@0 112 unsigned long mbx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
yuuji@0 113 unsigned long *size,char **hdr);
yuuji@0 114 unsigned long mbx_rewrite (MAILSTREAM *stream,unsigned long *reclaimed,
yuuji@0 115 long flags);
yuuji@0 116 long mbx_flaglock (MAILSTREAM *stream);
yuuji@0 117
yuuji@0 118 /* MBX mail routines */
yuuji@0 119
yuuji@0 120
yuuji@0 121 /* Driver dispatch used by MAIL */
yuuji@0 122
yuuji@0 123 DRIVER mbxdriver = {
yuuji@0 124 "mbx", /* driver name */
yuuji@0 125 DR_LOCAL|DR_MAIL|DR_CRLF|DR_LOCKING,
yuuji@0 126 /* driver flags */
yuuji@0 127 (DRIVER *) NIL, /* next driver */
yuuji@0 128 mbx_valid, /* mailbox is valid for us */
yuuji@0 129 mbx_parameters, /* manipulate parameters */
yuuji@0 130 mbx_scan, /* scan mailboxes */
yuuji@0 131 mbx_list, /* list mailboxes */
yuuji@0 132 mbx_lsub, /* list subscribed mailboxes */
yuuji@0 133 NIL, /* subscribe to mailbox */
yuuji@0 134 NIL, /* unsubscribe from mailbox */
yuuji@0 135 mbx_create, /* create mailbox */
yuuji@0 136 mbx_delete, /* delete mailbox */
yuuji@0 137 mbx_rename, /* rename mailbox */
yuuji@0 138 mail_status_default, /* status of mailbox */
yuuji@0 139 mbx_open, /* open mailbox */
yuuji@0 140 mbx_close, /* close mailbox */
yuuji@0 141 mbx_flags, /* fetch message "fast" attributes */
yuuji@0 142 mbx_flags, /* fetch message flags */
yuuji@0 143 NIL, /* fetch overview */
yuuji@0 144 NIL, /* fetch message envelopes */
yuuji@0 145 mbx_header, /* fetch message header */
yuuji@0 146 mbx_text, /* fetch message body */
yuuji@0 147 NIL, /* fetch partial message text */
yuuji@0 148 NIL, /* unique identifier */
yuuji@0 149 NIL, /* message number */
yuuji@0 150 mbx_flag, /* modify flags */
yuuji@0 151 mbx_flagmsg, /* per-message modify flags */
yuuji@0 152 NIL, /* search for message based on criteria */
yuuji@0 153 NIL, /* sort messages */
yuuji@0 154 NIL, /* thread messages */
yuuji@0 155 mbx_ping, /* ping mailbox to see if still alive */
yuuji@0 156 mbx_check, /* check for new messages */
yuuji@0 157 mbx_expunge, /* expunge deleted messages */
yuuji@0 158 mbx_copy, /* copy messages to another mailbox */
yuuji@0 159 mbx_append, /* append string message to mailbox */
yuuji@0 160 NIL /* garbage collect stream */
yuuji@0 161 };
yuuji@0 162
yuuji@0 163 /* prototype stream */
yuuji@0 164 MAILSTREAM mbxproto = {&mbxdriver};
yuuji@0 165
yuuji@0 166 /* MBX mail validate mailbox
yuuji@0 167 * Accepts: mailbox name
yuuji@0 168 * Returns: our driver if name is valid, NIL otherwise
yuuji@0 169 */
yuuji@0 170
yuuji@0 171 DRIVER *mbx_valid (char *name)
yuuji@0 172 {
yuuji@0 173 char tmp[MAILTMPLEN];
yuuji@0 174 int fd = mbx_isvalid (NIL,name,tmp,NIL,NIL,NIL);
yuuji@0 175 if (fd < 0) return NIL;
yuuji@0 176 close (fd); /* don't need the fd now */
yuuji@0 177 return &mbxdriver;
yuuji@0 178 }
yuuji@0 179
yuuji@0 180
yuuji@0 181 /* MBX mail test for valid mailbox
yuuji@0 182 * Accepts: returned stream with valid mailbox keywords
yuuji@0 183 * mailbox name
yuuji@0 184 * buffer to write file name
yuuji@0 185 * returned lock fd
yuuji@0 186 * returned lock name
yuuji@0 187 * RW flags or NIL for readonly
yuuji@0 188 * Returns: file descriptor if valid, NIL otherwise
yuuji@0 189 */
yuuji@0 190
yuuji@0 191 #define MBXISVALIDNOUID 0x1 /* RW, don't do UID action */
yuuji@0 192 #define MBXISVALIDUID 0x2 /* RW, do UID action */
yuuji@0 193
yuuji@0 194 int mbx_isvalid (MAILSTREAM **stream,char *name,char *file,int *ld,char *lock,
yuuji@0 195 long flags)
yuuji@0 196 {
yuuji@0 197 int fd,upd;
yuuji@0 198 int ret = -1;
yuuji@0 199 unsigned long i;
yuuji@0 200 long j,k;
yuuji@0 201 off_t pos;
yuuji@0 202 char c,*s,*t,hdr[HDRSIZE];
yuuji@0 203 struct stat sbuf;
yuuji@0 204 struct utimbuf times;
yuuji@0 205 int error = EINVAL; /* assume invalid argument */
yuuji@0 206 if (ld) *ld = -1; /* initially no lock */
yuuji@0 207 /* if file, get its status */
yuuji@0 208 if ((s = dummy_file (file,name)) && !stat (s,&sbuf) &&
yuuji@0 209 ((sbuf.st_mode & S_IFMT) == S_IFREG) &&
yuuji@0 210 ((fd = open (file,(flags ? O_RDWR : O_RDONLY)|O_BINARY,NIL)) >= 0)) {
yuuji@0 211 error = -1; /* assume bogus format */
yuuji@0 212 if (((((j = read (fd,hdr,HDRSIZE)) == HDRSIZE) && (hdr[0] == '*')) ||
yuuji@0 213 /* locked, set byte 0 to "*", read rest */
yuuji@0 214 ((j < 0) && (lseek (fd,1,L_SET) == 1) &&
yuuji@0 215 (read (fd,hdr+1,HDRSIZE-1) == (HDRSIZE-1)) && (hdr[0] = '*'))) &&
yuuji@0 216 (hdr[1] == 'm') && (hdr[2] == 'b') && (hdr[3] == 'x') &&
yuuji@0 217 (hdr[4] == '*') && (hdr[5] == '\015') && (hdr[6] == '\012') &&
yuuji@0 218 isxdigit (hdr[7]) && isxdigit (hdr[8]) && isxdigit (hdr[9]) &&
yuuji@0 219 isxdigit (hdr[10]) && isxdigit (hdr[11]) && isxdigit (hdr[12]) &&
yuuji@0 220 isxdigit (hdr[13]) && isxdigit (hdr[14]) && isxdigit (c = hdr[15]) &&
yuuji@0 221 isxdigit (hdr[16]) && isxdigit (hdr[17]) && isxdigit (hdr[18]) &&
yuuji@0 222 isxdigit (hdr[19]) && isxdigit (hdr[20]) && isxdigit (hdr[21]) &&
yuuji@0 223 isxdigit (hdr[22]) && (hdr[23] == '\015') && (hdr[24] == '\012')) {
yuuji@0 224 ret = fd; /* mbx format */
yuuji@0 225
yuuji@0 226 if (stream) { /* lock if making a mini-stream */
yuuji@0 227 if (flock (fd,LOCK_SH) ||
yuuji@0 228 (flags && ((*ld = lockname (lock,file,LOCK_EX)) < 0))) ret = -1;
yuuji@0 229 /* reread data now that locked */
yuuji@0 230 else if (lseek (fd,0,L_SET) ||
yuuji@0 231 (read (fd,hdr+1,HDRSIZE-1) != (HDRSIZE-1))) ret = -1;
yuuji@0 232 else {
yuuji@0 233 *stream = (MAILSTREAM *) memset (fs_get (sizeof (MAILSTREAM)),0,
yuuji@0 234 sizeof (MAILSTREAM));
yuuji@0 235 hdr[15] = '\0'; /* tie off UIDVALIDITY */
yuuji@0 236 (*stream)->uid_validity = strtoul (hdr+7,NIL,16);
yuuji@0 237 hdr[15] = c; /* now get UIDLAST */
yuuji@0 238 (*stream)->uid_last = strtoul (hdr+15,NIL,16);
yuuji@0 239 /* parse user flags */
yuuji@0 240 for (i = 0, s = hdr + 25;
yuuji@0 241 (i < NUSERFLAGS) && (t = strchr (s,'\015')) && (t - s);
yuuji@0 242 i++, s = t + 2) {
yuuji@0 243 *t = '\0'; /* tie off flag */
yuuji@0 244 if (strlen (s) <= MAXUSERFLAG)
yuuji@0 245 (*stream)->user_flags[i] = cpystr (s);
yuuji@0 246 }
yuuji@0 247 /* make sure have true UIDLAST */
yuuji@0 248 if (flags & MBXISVALIDUID) {
yuuji@0 249 for (upd = NIL,pos = 2048, k = 0; pos < sbuf.st_size;
yuuji@0 250 pos += (j + k)) {
yuuji@0 251 /* read header for this message */
yuuji@0 252 lseek (fd,pos,L_SET);
yuuji@0 253 if ((j = read (fd,hdr,64)) >= 0) {
yuuji@0 254 hdr[j] = '\0';
yuuji@0 255 if ((s = strchr (hdr,'\015')) && (s[1] == '\012')) {
yuuji@0 256 *s = '\0';
yuuji@0 257 k = s + 2 - hdr;
yuuji@0 258 if ((s = strchr (hdr,',')) && (j = strtol (s+1,&s,10)) &&
yuuji@0 259 (*s == ';') && (s = strchr (s+1,'-'))) {
yuuji@0 260 /* get UID if there is any */
yuuji@0 261 i = strtoul (++s,&t,16);
yuuji@0 262 if (!*t && (t == (s + 8)) && (i <= (*stream)->uid_last)) {
yuuji@0 263 if (!i) {
yuuji@0 264 lseek (fd,pos + s - hdr,L_SET);
yuuji@0 265 sprintf (hdr,"%08lx",++(*stream)->uid_last);
yuuji@0 266 write (fd,hdr,8);
yuuji@0 267 upd = T;
yuuji@0 268 }
yuuji@0 269 continue;
yuuji@0 270 }
yuuji@0 271 }
yuuji@0 272 }
yuuji@0 273 ret = -1; /* error, give up */
yuuji@0 274 *stream = mail_close (*stream);
yuuji@0 275 pos = sbuf.st_size + 1;
yuuji@0 276 j = k = 0;
yuuji@0 277 }
yuuji@0 278 }
yuuji@0 279
yuuji@0 280 if (upd) { /* need to update hdr with new UIDLAST? */
yuuji@0 281 lseek (fd,15,L_SET);
yuuji@0 282 sprintf (hdr,"%08lx",(*stream)->uid_last);
yuuji@0 283 write (fd,hdr,8);
yuuji@0 284 }
yuuji@0 285 }
yuuji@0 286 }
yuuji@0 287 }
yuuji@0 288 }
yuuji@0 289 if (ret != fd) close (fd); /* close the file */
yuuji@0 290 else lseek (fd,0,L_SET); /* else rewind to start */
yuuji@0 291 /* \Marked status? */
yuuji@0 292 if (sbuf.st_ctime > sbuf.st_atime) {
yuuji@0 293 /* preserve atime and mtime */
yuuji@0 294 times.actime = sbuf.st_atime;
yuuji@0 295 times.modtime = sbuf.st_mtime;
yuuji@0 296 utime (file,&times); /* set the times */
yuuji@0 297 }
yuuji@0 298 }
yuuji@0 299 /* in case INBOX but not mbx format */
yuuji@0 300 else if (((error = errno) == ENOENT) && !compare_cstring (name,"INBOX"))
yuuji@0 301 error = -1;
yuuji@0 302 if ((ret < 0) && ld && (*ld >= 0)) {
yuuji@0 303 unlockfd (*ld,lock);
yuuji@0 304 *ld = -1;
yuuji@0 305 }
yuuji@0 306 errno = error; /* return as last error */
yuuji@0 307 return ret; /* return what we should */
yuuji@0 308 }
yuuji@0 309
yuuji@0 310 /* MBX manipulate driver parameters
yuuji@0 311 * Accepts: function code
yuuji@0 312 * function-dependent value
yuuji@0 313 * Returns: function-dependent return value
yuuji@0 314 */
yuuji@0 315
yuuji@0 316 void *mbx_parameters (long function,void *value)
yuuji@0 317 {
yuuji@0 318 void *ret = NIL;
yuuji@0 319 switch ((int) function) {
yuuji@0 320 case SET_ONETIMEEXPUNGEATPING:
yuuji@0 321 if (value) ((MBXLOCAL *) ((MAILSTREAM *) value)->local)->expok = T;
yuuji@0 322 case GET_ONETIMEEXPUNGEATPING:
yuuji@0 323 if (value) ret = (void *)
yuuji@0 324 (((MBXLOCAL *) ((MAILSTREAM *) value)->local)->expok ? VOIDT : NIL);
yuuji@0 325 break;
yuuji@0 326 }
yuuji@0 327 return ret;
yuuji@0 328 }
yuuji@0 329
yuuji@0 330
yuuji@0 331 /* MBX mail scan mailboxes
yuuji@0 332 * Accepts: mail stream
yuuji@0 333 * reference
yuuji@0 334 * pattern to search
yuuji@0 335 * string to scan
yuuji@0 336 */
yuuji@0 337
yuuji@0 338 void mbx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
yuuji@0 339 {
yuuji@0 340 if (stream) dummy_scan (NIL,ref,pat,contents);
yuuji@0 341 }
yuuji@0 342
yuuji@0 343
yuuji@0 344 /* MBX mail list mailboxes
yuuji@0 345 * Accepts: mail stream
yuuji@0 346 * reference
yuuji@0 347 * pattern to search
yuuji@0 348 */
yuuji@0 349
yuuji@0 350 void mbx_list (MAILSTREAM *stream,char *ref,char *pat)
yuuji@0 351 {
yuuji@0 352 if (stream) dummy_list (NIL,ref,pat);
yuuji@0 353 }
yuuji@0 354
yuuji@0 355
yuuji@0 356 /* MBX mail list subscribed mailboxes
yuuji@0 357 * Accepts: mail stream
yuuji@0 358 * reference
yuuji@0 359 * pattern to search
yuuji@0 360 */
yuuji@0 361
yuuji@0 362 void mbx_lsub (MAILSTREAM *stream,char *ref,char *pat)
yuuji@0 363 {
yuuji@0 364 if (stream) dummy_lsub (NIL,ref,pat);
yuuji@0 365 }
yuuji@0 366
yuuji@0 367 /* MBX mail create mailbox
yuuji@0 368 * Accepts: MAIL stream
yuuji@0 369 * mailbox name to create
yuuji@0 370 * Returns: T on success, NIL on failure
yuuji@0 371 */
yuuji@0 372
yuuji@0 373 long mbx_create (MAILSTREAM *stream,char *mailbox)
yuuji@0 374 {
yuuji@0 375 char *s,mbx[MAILTMPLEN],tmp[HDRSIZE];
yuuji@0 376 long ret = NIL;
yuuji@0 377 int i,fd;
yuuji@0 378 if (!(s = dummy_file (mbx,mailbox))) {
yuuji@0 379 sprintf (mbx,"Can't create %.80s: invalid name",mailbox);
yuuji@0 380 mm_log (mbx,ERROR);
yuuji@0 381 }
yuuji@0 382 /* create underlying file */
yuuji@0 383 else if (dummy_create (stream,s)) {
yuuji@0 384 /* done if made directory */
yuuji@0 385 if ((s = strrchr (s,'\\')) && !s[1]) return T;
yuuji@0 386 if ((fd = open (mbx,O_WRONLY|O_BINARY,NIL)) < 0) {
yuuji@0 387 sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno));
yuuji@0 388 mm_log (tmp,ERROR);
yuuji@0 389 unlink (mbx); /* delete the file */
yuuji@0 390 }
yuuji@0 391 else {
yuuji@0 392 memset (tmp,'\0',HDRSIZE);/* initialize header */
yuuji@0 393 sprintf (s = tmp,"*mbx*\015\012%08lx00000000\015\012",
yuuji@0 394 (unsigned long) time (0));
yuuji@0 395 for (i = 0; i < NUSERFLAGS; ++i)
yuuji@0 396 sprintf (s += strlen (s),"%s\015\012",
yuuji@0 397 (stream && stream->user_flags[i]) ? stream->user_flags[i] :
yuuji@0 398 "");
yuuji@0 399 if (write (fd,tmp,HDRSIZE) != HDRSIZE) {
yuuji@0 400 sprintf (tmp,"Can't initialize mailbox node %.80s: %s",
yuuji@0 401 mbx,strerror (errno));
yuuji@0 402 mm_log (tmp,ERROR);
yuuji@0 403 unlink (mbx); /* delete the file */
yuuji@0 404 }
yuuji@0 405 else ret = T; /* success */
yuuji@0 406 close (fd); /* close file */
yuuji@0 407 }
yuuji@0 408 }
yuuji@0 409 return ret;
yuuji@0 410 }
yuuji@0 411
yuuji@0 412
yuuji@0 413 /* MBX mail delete mailbox
yuuji@0 414 * Accepts: MAIL stream
yuuji@0 415 * mailbox name to delete
yuuji@0 416 * Returns: T on success, NIL on failure
yuuji@0 417 */
yuuji@0 418
yuuji@0 419 long mbx_delete (MAILSTREAM *stream,char *mailbox)
yuuji@0 420 {
yuuji@0 421 return mbx_rename (stream,mailbox,NIL);
yuuji@0 422 }
yuuji@0 423
yuuji@0 424 /* MBX mail rename mailbox
yuuji@0 425 * Accepts: MAIL stream
yuuji@0 426 * old mailbox name
yuuji@0 427 * new mailbox name (or NIL for delete)
yuuji@0 428 * Returns: T on success, NIL on failure
yuuji@0 429 */
yuuji@0 430
yuuji@0 431 long mbx_rename (MAILSTREAM *stream,char *old,char *newname)
yuuji@0 432 {
yuuji@0 433 long ret = LONGT;
yuuji@0 434 char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
yuuji@0 435 int fd,ld;
yuuji@0 436 struct stat sbuf;
yuuji@0 437 if (!dummy_file (file,old) ||
yuuji@0 438 (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
yuuji@0 439 ((s = strrchr (tmp,'\\')) && !s[1])))) {
yuuji@0 440 sprintf (tmp,newname ?
yuuji@0 441 "Can't rename mailbox %.80s to %.80s: invalid name" :
yuuji@0 442 "Can't delete mailbox %.80s: invalid name",
yuuji@0 443 old,newname);
yuuji@0 444 mm_log (tmp,ERROR);
yuuji@0 445 return NIL;
yuuji@0 446 }
yuuji@0 447 else if ((fd = open (file,O_RDWR|O_BINARY,NIL)) < 0) {
yuuji@0 448 sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
yuuji@0 449 mm_log (tmp,ERROR);
yuuji@0 450 return NIL;
yuuji@0 451 }
yuuji@0 452 /* get parse/append permission */
yuuji@0 453 if ((ld = lockname (lock,file,LOCK_EX)) < 0) {
yuuji@0 454 mm_log ("Unable to lock rename mailbox",ERROR);
yuuji@0 455 return NIL;
yuuji@0 456 }
yuuji@0 457 /* lock out other users */
yuuji@0 458 if (flock (fd,LOCK_EX|LOCK_NB)) {
yuuji@0 459 close (fd); /* couldn't lock, give up on it then */
yuuji@0 460 sprintf (tmp,"Mailbox %.80s is in use by another process",old);
yuuji@0 461 mm_log (tmp,ERROR);
yuuji@0 462 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 463 return NIL;
yuuji@0 464 }
yuuji@0 465
yuuji@0 466 if (newname) { /* want rename? */
yuuji@0 467 /* found superior to destination name? */
yuuji@0 468 if ((s = strrchr (tmp,'\\')) && (s != tmp) &&
yuuji@0 469 ((tmp[1] != ':') || (s != tmp + 2))) {
yuuji@0 470 c = s[1]; /* remember character after delimiter */
yuuji@0 471 *s = s[1] = '\0'; /* tie off name at delimiter */
yuuji@0 472 /* name doesn't exist, create it */
yuuji@0 473 if (stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) {
yuuji@0 474 *s = '\\'; /* restore delimiter */
yuuji@0 475 if (!dummy_create (stream,tmp)) ret = NIL;
yuuji@0 476 }
yuuji@0 477 else *s = '\\'; /* restore delimiter */
yuuji@0 478 s[1] = c; /* restore character after delimiter */
yuuji@0 479 }
yuuji@0 480 flock (fd,LOCK_UN); /* release lock on the file */
yuuji@0 481 close (fd); /* pacify NTFS */
yuuji@0 482 /* rename the file */
yuuji@0 483 if (ret && rename (file,tmp)) {
yuuji@0 484 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
yuuji@0 485 strerror (errno));
yuuji@0 486 mm_log (tmp,ERROR);
yuuji@0 487 ret = NIL; /* set failure */
yuuji@0 488 }
yuuji@0 489 }
yuuji@0 490 else {
yuuji@0 491 flock (fd,LOCK_UN); /* release lock on the file */
yuuji@0 492 close (fd); /* pacify NTFS */
yuuji@0 493 if (unlink (file)) {
yuuji@0 494 sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
yuuji@0 495 mm_log (tmp,ERROR);
yuuji@0 496 ret = NIL; /* set failure */
yuuji@0 497 }
yuuji@0 498 }
yuuji@0 499 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 500 /* recreate file if renamed INBOX */
yuuji@0 501 if (ret && !compare_cstring (old,"INBOX")) mbx_create (NIL,"INBOX");
yuuji@0 502 return ret; /* return success */
yuuji@0 503 }
yuuji@0 504
yuuji@0 505 /* MBX mail open
yuuji@0 506 * Accepts: stream to open
yuuji@0 507 * Returns: stream on success, NIL on failure
yuuji@0 508 */
yuuji@0 509
yuuji@0 510 MAILSTREAM *mbx_open (MAILSTREAM *stream)
yuuji@0 511 {
yuuji@0 512 int fd,ld;
yuuji@0 513 short silent;
yuuji@0 514 char tmp[MAILTMPLEN];
yuuji@0 515 if (!stream) return &mbxproto;/* return prototype for OP_PROTOTYPE call */
yuuji@0 516 if (stream->local) fatal ("mbx recycle stream");
yuuji@0 517 /* canonicalize the mailbox name */
yuuji@0 518 if (!dummy_file (tmp,stream->mailbox)) {
yuuji@0 519 sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
yuuji@0 520 mm_log (tmp,ERROR);
yuuji@0 521 }
yuuji@0 522 if (stream->rdonly ||
yuuji@0 523 (fd = open (tmp,O_RDWR|O_BINARY,NIL)) < 0) {
yuuji@0 524 if ((fd = open (tmp,O_RDONLY|O_BINARY,NIL)) < 0) {
yuuji@0 525 sprintf (tmp,"Can't open mailbox: %s",strerror (errno));
yuuji@0 526 mm_log (tmp,ERROR);
yuuji@0 527 return NIL;
yuuji@0 528 }
yuuji@0 529 else if (!stream->rdonly) { /* got it, but readonly */
yuuji@0 530 mm_log ("Can't get write access to mailbox, access is readonly",WARN);
yuuji@0 531 stream->rdonly = T;
yuuji@0 532 }
yuuji@0 533 }
yuuji@0 534
yuuji@0 535 stream->local = memset (fs_get (sizeof (MBXLOCAL)),NIL,sizeof (MBXLOCAL));
yuuji@0 536 LOCAL->fd = fd; /* bind the file */
yuuji@0 537 LOCAL->ld = -1; /* no flaglock */
yuuji@0 538 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
yuuji@0 539 LOCAL->buflen = CHUNKSIZE - 1;
yuuji@0 540 /* note if an INBOX or not */
yuuji@0 541 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
yuuji@0 542 fs_give ((void **) &stream->mailbox);
yuuji@0 543 stream->mailbox = cpystr (tmp);
yuuji@0 544 /* get parse/append permission */
yuuji@0 545 if ((ld = lockname (tmp,stream->mailbox,LOCK_EX)) < 0) {
yuuji@0 546 mm_log ("Unable to lock open mailbox",ERROR);
yuuji@0 547 return NIL;
yuuji@0 548 }
yuuji@0 549 flock (LOCAL->fd,LOCK_SH); /* lock the file */
yuuji@0 550 unlockfd (ld,tmp); /* release shared parse permission */
yuuji@0 551 LOCAL->filesize = HDRSIZE; /* initialize parsed file size */
yuuji@0 552 LOCAL->filetime = 0; /* time not set up yet */
yuuji@0 553 LOCAL->expok = LOCAL->flagcheck = NIL;
yuuji@0 554 stream->sequence++; /* bump sequence number */
yuuji@0 555 /* parse mailbox */
yuuji@0 556 stream->nmsgs = stream->recent = 0;
yuuji@0 557 silent = stream->silent; /* defer events */
yuuji@0 558 stream->silent = T;
yuuji@0 559 if (mbx_ping (stream) && !stream->nmsgs)
yuuji@0 560 mm_log ("Mailbox is empty",(long) NIL);
yuuji@0 561 stream->silent = silent; /* now notify upper level */
yuuji@0 562 mail_exists (stream,stream->nmsgs);
yuuji@0 563 mail_recent (stream,stream->recent);
yuuji@0 564 if (!LOCAL) return NIL; /* failure if stream died */
yuuji@0 565 stream->perm_seen = stream->perm_deleted = stream->perm_flagged =
yuuji@0 566 stream->perm_answered = stream->perm_draft = stream->rdonly ? NIL : T;
yuuji@0 567 stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
yuuji@0 568 stream->kwd_create = (stream->user_flags[NUSERFLAGS-1] || stream->rdonly) ?
yuuji@0 569 NIL : T; /* can we create new user flags? */
yuuji@0 570 return stream; /* return stream to caller */
yuuji@0 571 }
yuuji@0 572
yuuji@0 573 /* MBX mail close
yuuji@0 574 * Accepts: MAIL stream
yuuji@0 575 * close options
yuuji@0 576 */
yuuji@0 577
yuuji@0 578 void mbx_close (MAILSTREAM *stream,long options)
yuuji@0 579 {
yuuji@0 580 if (stream && LOCAL) { /* only if a file is open */
yuuji@0 581 int silent = stream->silent;
yuuji@0 582 stream->silent = T; /* note this stream is dying */
yuuji@0 583 /* do an expunge if requested */
yuuji@0 584 if (options & CL_EXPUNGE) mbx_expunge (stream,NIL,NIL);
yuuji@0 585 else { /* otherwise do a checkpoint to purge */
yuuji@0 586 LOCAL->expok = T; /* possible expunged messages */
yuuji@0 587 mbx_ping (stream);
yuuji@0 588 }
yuuji@0 589 stream->silent = silent; /* restore previous status */
yuuji@0 590 mbx_abort (stream);
yuuji@0 591 }
yuuji@0 592 }
yuuji@0 593
yuuji@0 594
yuuji@0 595 /* MBX mail abort stream
yuuji@0 596 * Accepts: MAIL stream
yuuji@0 597 */
yuuji@0 598
yuuji@0 599 void mbx_abort (MAILSTREAM *stream)
yuuji@0 600 {
yuuji@0 601 if (stream && LOCAL) { /* only if a file is open */
yuuji@0 602 flock (LOCAL->fd,LOCK_UN); /* unlock local file */
yuuji@0 603 close (LOCAL->fd); /* close the local file */
yuuji@0 604 /* free local text buffer */
yuuji@0 605 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
yuuji@0 606 /* nuke the local data */
yuuji@0 607 fs_give ((void **) &stream->local);
yuuji@0 608 stream->dtb = NIL; /* log out the DTB */
yuuji@0 609 }
yuuji@0 610 }
yuuji@0 611
yuuji@0 612
yuuji@0 613 /* MBX mail fetch flags
yuuji@0 614 * Accepts: MAIL stream
yuuji@0 615 * sequence
yuuji@0 616 * option flags
yuuji@0 617 * Sniffs at file to see if some other process changed the flags
yuuji@0 618 */
yuuji@0 619
yuuji@0 620 void mbx_flags (MAILSTREAM *stream,char *sequence,long flags)
yuuji@0 621 {
yuuji@0 622 MESSAGECACHE *elt;
yuuji@0 623 unsigned long i;
yuuji@0 624 if (mbx_ping (stream) && /* ping mailbox, get new status for messages */
yuuji@0 625 ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
yuuji@0 626 mail_sequence (stream,sequence)))
yuuji@0 627 for (i = 1; i <= stream->nmsgs; i++)
yuuji@0 628 if ((elt = mail_elt (stream,i))->sequence && !elt->valid)
yuuji@0 629 mbx_elt (stream,i,NIL);
yuuji@0 630 }
yuuji@0 631
yuuji@0 632 /* MBX mail fetch message header
yuuji@0 633 * Accepts: MAIL stream
yuuji@0 634 * message # to fetch
yuuji@0 635 * pointer to returned header text length
yuuji@0 636 * option flags
yuuji@0 637 * Returns: message header in RFC822 format
yuuji@0 638 */
yuuji@0 639
yuuji@0 640 char *mbx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
yuuji@0 641 long flags)
yuuji@0 642 {
yuuji@0 643 unsigned long i;
yuuji@0 644 char *s;
yuuji@0 645 *length = 0; /* default to empty */
yuuji@0 646 if (flags & FT_UID) return "";/* UID call "impossible" */
yuuji@0 647 /* get header position, possibly header */
yuuji@0 648 i = mbx_hdrpos (stream,msgno,length,&s);
yuuji@0 649 if (!s) { /* mbx_hdrpos() returned header? */
yuuji@0 650 lseek (LOCAL->fd,i,L_SET); /* no, get to header position */
yuuji@0 651 /* is buffer big enough? */
yuuji@0 652 if (*length > LOCAL->buflen) {
yuuji@0 653 fs_give ((void **) &LOCAL->buf);
yuuji@0 654 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = *length) + 1);
yuuji@0 655 }
yuuji@0 656 /* slurp the data */
yuuji@0 657 read (LOCAL->fd,s = LOCAL->buf,*length);
yuuji@0 658 }
yuuji@0 659 s[*length] = '\0'; /* tie off string */
yuuji@0 660 return s;
yuuji@0 661 }
yuuji@0 662
yuuji@0 663 /* MBX mail fetch message text (body only)
yuuji@0 664 * Accepts: MAIL stream
yuuji@0 665 * message # to fetch
yuuji@0 666 * pointer to returned header text length
yuuji@0 667 * option flags
yuuji@0 668 * Returns: T on success, NIL on failure
yuuji@0 669 */
yuuji@0 670
yuuji@0 671 long mbx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
yuuji@0 672 {
yuuji@0 673 FDDATA d;
yuuji@0 674 unsigned long i,j;
yuuji@0 675 MESSAGECACHE *elt;
yuuji@0 676 /* UID call "impossible" */
yuuji@0 677 if (flags & FT_UID) return NIL;
yuuji@0 678 /* get message status */
yuuji@0 679 elt = mbx_elt (stream,msgno,NIL);
yuuji@0 680 /* if message not seen */
yuuji@0 681 if (!(flags & FT_PEEK) && !elt->seen && mbx_flaglock (stream)) {
yuuji@0 682 elt->seen = T; /* mark message as seen */
yuuji@0 683 /* recalculate status */
yuuji@0 684 mbx_update_status (stream,msgno,NIL);
yuuji@0 685 mm_flags (stream,msgno);
yuuji@0 686 /* update flags */
yuuji@0 687 mbx_flag (stream,NIL,NIL,NIL);
yuuji@0 688 }
yuuji@0 689 if (!LOCAL) return NIL; /* mbx_flaglock() could have aborted */
yuuji@0 690 /* find header position */
yuuji@0 691 i = mbx_hdrpos (stream,msgno,&j,NIL);
yuuji@0 692 d.fd = LOCAL->fd; /* set up file descriptor */
yuuji@0 693 d.pos = i + j;
yuuji@0 694 d.chunk = LOCAL->buf; /* initial buffer chunk */
yuuji@0 695 d.chunksize = CHUNKSIZE;
yuuji@0 696 INIT (bs,fd_string,&d,elt->rfc822_size - j);
yuuji@0 697 return LONGT; /* success */
yuuji@0 698 }
yuuji@0 699
yuuji@0 700 /* MBX mail modify flags
yuuji@0 701 * Accepts: MAIL stream
yuuji@0 702 * sequence
yuuji@0 703 * flag(s)
yuuji@0 704 * option flags
yuuji@0 705 * Unlocks flag lock
yuuji@0 706 */
yuuji@0 707
yuuji@0 708 void mbx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
yuuji@0 709 {
yuuji@0 710 struct utimbuf times;
yuuji@0 711 struct stat sbuf;
yuuji@0 712 /* make sure the update takes */
yuuji@0 713 if (!stream->rdonly && LOCAL && (LOCAL->fd >= 0) && (LOCAL->ld >= 0)) {
yuuji@0 714 fsync (LOCAL->fd);
yuuji@0 715 fstat (LOCAL->fd,&sbuf); /* get current write time */
yuuji@0 716 times.modtime = LOCAL->filetime = sbuf.st_mtime;
yuuji@0 717 /* update header */
yuuji@0 718 if ((LOCAL->ffuserflag < NUSERFLAGS) &&
yuuji@0 719 stream->user_flags[LOCAL->ffuserflag]) mbx_update_header (stream);
yuuji@0 720 times.actime = time (0); /* make sure read comes after all that */
yuuji@0 721 utime (stream->mailbox,&times);
yuuji@0 722 }
yuuji@0 723 if (LOCAL->ld >= 0) { /* unlock now */
yuuji@0 724 unlockfd (LOCAL->ld,LOCAL->lock);
yuuji@0 725 LOCAL->ld = -1;
yuuji@0 726 }
yuuji@0 727 }
yuuji@0 728
yuuji@0 729
yuuji@0 730 /* MBX mail per-message modify flags
yuuji@0 731 * Accepts: MAIL stream
yuuji@0 732 * message cache element
yuuji@0 733 */
yuuji@0 734
yuuji@0 735 void mbx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
yuuji@0 736 {
yuuji@0 737 if (mbx_flaglock (stream)) mbx_update_status (stream,elt->msgno,NIL);
yuuji@0 738 }
yuuji@0 739
yuuji@0 740 /* MBX mail ping mailbox
yuuji@0 741 * Accepts: MAIL stream
yuuji@0 742 * Returns: T if stream still alive, NIL if not
yuuji@0 743 */
yuuji@0 744
yuuji@0 745 long mbx_ping (MAILSTREAM *stream)
yuuji@0 746 {
yuuji@0 747 unsigned long i,pos;
yuuji@0 748 long ret = NIL;
yuuji@0 749 int ld;
yuuji@0 750 char lock[MAILTMPLEN];
yuuji@0 751 MESSAGECACHE *elt;
yuuji@0 752 struct stat sbuf;
yuuji@0 753 if (stream && LOCAL) { /* only if stream already open */
yuuji@0 754 ret = LONGT; /* assume OK */
yuuji@0 755 fstat (LOCAL->fd,&sbuf); /* get current file poop */
yuuji@0 756 /* allow expunge if permitted at ping */
yuuji@0 757 if (mail_parameters (NIL,GET_EXPUNGEATPING,NIL)) LOCAL->expok = T;
yuuji@0 758 /* if external modification */
yuuji@0 759 if (LOCAL->filetime && (LOCAL->filetime < sbuf.st_mtime))
yuuji@0 760 LOCAL->flagcheck = T; /* upgrade to flag checking */
yuuji@0 761 /* new mail or flagcheck handling needed? */
yuuji@0 762 if (((sbuf.st_size - LOCAL->filesize) || LOCAL->flagcheck ||
yuuji@0 763 !stream->nmsgs) &&
yuuji@0 764 ((ld = lockname (lock,stream->mailbox,LOCK_EX)) >= 0)) {
yuuji@0 765 if (!LOCAL->flagcheck) ret = mbx_parse (stream);
yuuji@0 766 /* sweep mailbox for changed message status */
yuuji@0 767 else if (ret = mbx_parse (stream)) {
yuuji@0 768 unsigned long recent = 0;
yuuji@0 769 LOCAL->filetime = sbuf.st_mtime;
yuuji@0 770 for (i = 1; i <= stream->nmsgs; )
yuuji@0 771 if (elt = mbx_elt (stream,i,LOCAL->expok)) {
yuuji@0 772 if (elt->recent) ++recent;
yuuji@0 773 ++i;
yuuji@0 774 }
yuuji@0 775 mail_recent (stream,recent);
yuuji@0 776 LOCAL->flagcheck = NIL; /* got all the updates */
yuuji@0 777 }
yuuji@0 778 unlockfd (ld,lock); /* release shared parse/append permission */
yuuji@0 779 }
yuuji@0 780 if (ret) { /* must still be alive */
yuuji@0 781 if (!LOCAL->expunged) /* look for holes if none known yet */
yuuji@0 782 for (i = 1, pos = HDRSIZE;
yuuji@0 783 !LOCAL->expunged && (i <= stream->nmsgs);
yuuji@0 784 i++, pos += elt->private.special.text.size + elt->rfc822_size)
yuuji@0 785 if ((elt = mail_elt (stream,i))->private.special.offset != pos)
yuuji@0 786 LOCAL->expunged = T;/* found a hole */
yuuji@0 787 /* burp any holes */
yuuji@0 788 if (LOCAL->expunged && !stream->rdonly) {
yuuji@0 789 if (mbx_rewrite (stream,&i,NIL)) fatal ("expunge on check");
yuuji@0 790 if (i) { /* any space reclaimed? */
yuuji@0 791 LOCAL->expunged = NIL;/* no more pending expunge */
yuuji@0 792 sprintf (LOCAL->buf,"Reclaimed %lu bytes of expunged space",i);
yuuji@0 793 mm_log (LOCAL->buf,(long) NIL);
yuuji@0 794 }
yuuji@0 795 }
yuuji@0 796 LOCAL->expok = NIL; /* no more expok */
yuuji@0 797 }
yuuji@0 798 }
yuuji@0 799 return ret; /* return result of the parse */
yuuji@0 800 }
yuuji@0 801
yuuji@0 802 /* MBX mail check mailbox (reparses status too)
yuuji@0 803 * Accepts: MAIL stream
yuuji@0 804 */
yuuji@0 805
yuuji@0 806 void mbx_check (MAILSTREAM *stream)
yuuji@0 807 {
yuuji@0 808 if (LOCAL) LOCAL->expok = T; /* mark that a check is desired */
yuuji@0 809 if (mbx_ping (stream)) mm_log ("Check completed",(long) NIL);
yuuji@0 810 }
yuuji@0 811
yuuji@0 812
yuuji@0 813 /* MBX mail expunge mailbox
yuuji@0 814 * Accepts: MAIL stream
yuuji@0 815 * sequence to expunge if non-NIL
yuuji@0 816 * expunge options
yuuji@0 817 * Returns: T if success, NIL if failure
yuuji@0 818 */
yuuji@0 819
yuuji@0 820 long mbx_expunge (MAILSTREAM *stream,char *sequence,long options)
yuuji@0 821 {
yuuji@0 822 long ret;
yuuji@0 823 unsigned long nexp,reclaimed;
yuuji@0 824 if (ret = sequence ? ((options & EX_UID) ?
yuuji@0 825 mail_uid_sequence (stream,sequence) :
yuuji@0 826 mail_sequence (stream,sequence)) : LONGT) {
yuuji@0 827 if (!mbx_ping (stream)); /* do nothing if stream dead */
yuuji@0 828 else if (stream->rdonly) /* won't do on readonly files! */
yuuji@0 829 mm_log ("Expunge ignored on readonly mailbox",WARN);
yuuji@0 830 /* if expunged any messages */
yuuji@0 831 else if (nexp = mbx_rewrite (stream,&reclaimed,sequence ? -1 : 1)) {
yuuji@0 832 sprintf (LOCAL->buf,"Expunged %lu messages",nexp);
yuuji@0 833 mm_log (LOCAL->buf,(long) NIL);
yuuji@0 834 }
yuuji@0 835 else if (reclaimed) { /* or if any prior expunged space reclaimed */
yuuji@0 836 sprintf (LOCAL->buf,"Reclaimed %lu bytes of expunged space",reclaimed);
yuuji@0 837 mm_log (LOCAL->buf,(long) NIL);
yuuji@0 838 }
yuuji@0 839 else mm_log ("No messages deleted, so no update needed",(long) NIL);
yuuji@0 840 }
yuuji@0 841 return ret;
yuuji@0 842 }
yuuji@0 843
yuuji@0 844 /* MBX mail copy message(s)
yuuji@0 845 * Accepts: MAIL stream
yuuji@0 846 * sequence
yuuji@0 847 * destination mailbox
yuuji@0 848 * copy options
yuuji@0 849 * Returns: T if success, NIL if failed
yuuji@0 850 */
yuuji@0 851
yuuji@0 852 long mbx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
yuuji@0 853 {
yuuji@0 854 struct stat sbuf;
yuuji@0 855 struct utimbuf times;
yuuji@0 856 MESSAGECACHE *elt;
yuuji@0 857 unsigned long i,j,k,m;
yuuji@0 858 long ret = LONGT;
yuuji@0 859 int fd,ld;
yuuji@0 860 char *s,*t,file[MAILTMPLEN],lock[MAILTMPLEN];
yuuji@0 861 mailproxycopy_t pc =
yuuji@0 862 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
yuuji@0 863 copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL);
yuuji@0 864 SEARCHSET *source = cu ? mail_newsearchset () : NIL;
yuuji@0 865 SEARCHSET *dest = cu ? mail_newsearchset () : NIL;
yuuji@0 866 MAILSTREAM *dstream = NIL;
yuuji@0 867 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
yuuji@0 868 mail_sequence (stream,sequence))) return NIL;
yuuji@0 869 /* make sure valid mailbox */
yuuji@0 870 if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock,
yuuji@0 871 cu ? MBXISVALIDUID : MBXISVALIDNOUID)) < 0)
yuuji@0 872 switch (errno) {
yuuji@0 873 case ENOENT: /* no such file? */
yuuji@0 874 mm_notify (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
yuuji@0 875 return NIL;
yuuji@0 876 case EACCES: /* file protected */
yuuji@0 877 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
yuuji@0 878 MM_LOG (LOCAL->buf,ERROR);
yuuji@0 879 return NIL;
yuuji@0 880 case EINVAL:
yuuji@0 881 if (pc) return (*pc) (stream,sequence,mailbox,options);
yuuji@0 882 sprintf (LOCAL->buf,"Invalid MBX-format mailbox name: %.80s",mailbox);
yuuji@0 883 mm_log (LOCAL->buf,ERROR);
yuuji@0 884 return NIL;
yuuji@0 885 default:
yuuji@0 886 if (pc) return (*pc) (stream,sequence,mailbox,options);
yuuji@0 887 sprintf (LOCAL->buf,"Not a MBX-format mailbox: %.80s",mailbox);
yuuji@0 888 mm_log (LOCAL->buf,ERROR);
yuuji@0 889 return NIL;
yuuji@0 890 }
yuuji@0 891 /* got file? */
yuuji@0 892 if ((fd = open (dummy_file (file,mailbox),O_RDWR|O_CREAT|O_BINARY,
yuuji@0 893 S_IREAD|S_IWRITE)) < 0) {
yuuji@0 894 sprintf (LOCAL->buf,"Unable to open copy mailbox: %s",strerror (errno));
yuuji@0 895 mm_log (LOCAL->buf,ERROR);
yuuji@0 896 return NIL;
yuuji@0 897 }
yuuji@0 898 mm_critical (stream); /* go critical */
yuuji@0 899 fstat (fd,&sbuf); /* get current file size */
yuuji@0 900 lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
yuuji@0 901
yuuji@0 902 /* for each requested message */
yuuji@0 903 for (i = 1; ret && (i <= stream->nmsgs); i++)
yuuji@0 904 if ((elt = mail_elt (stream,i))->sequence) {
yuuji@0 905 lseek (LOCAL->fd,elt->private.special.offset +
yuuji@0 906 elt->private.special.text.size,L_SET);
yuuji@0 907 mail_date(LOCAL->buf,elt);/* build target header */
yuuji@0 908 /* get target keyword mask */
yuuji@0 909 for (j = elt->user_flags, k = 0; j; )
yuuji@0 910 if (s = stream->user_flags[find_rightmost_bit (&j)])
yuuji@0 911 for (m = 0; (m < NUSERFLAGS) && (t = dstream->user_flags[m]); m++)
yuuji@0 912 if (!compare_cstring (s,t) && (k |= 1 << m)) break;
yuuji@0 913 sprintf (LOCAL->buf+strlen(LOCAL->buf),",%lu;%08lx%04x-%08lx\015\012",
yuuji@0 914 elt->rfc822_size,k,(unsigned)
yuuji@0 915 ((fSEEN * elt->seen) + (fDELETED * elt->deleted) +
yuuji@0 916 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
yuuji@0 917 (fDRAFT * elt->draft)),cu ? ++dstream->uid_last : 0);
yuuji@0 918 /* write target header */
yuuji@0 919 if (ret = (write (fd,LOCAL->buf,strlen (LOCAL->buf)) > 0)) {
yuuji@0 920 for (k = elt->rfc822_size; ret && (j = min (k,LOCAL->buflen)); k -= j){
yuuji@0 921 read (LOCAL->fd,LOCAL->buf,j);
yuuji@0 922 ret = write (fd,LOCAL->buf,j) >= 0;
yuuji@0 923 }
yuuji@0 924 if (cu) { /* need to pass back new UID? */
yuuji@0 925 mail_append_set (source,mail_uid (stream,i));
yuuji@0 926 mail_append_set (dest,dstream->uid_last);
yuuji@0 927 }
yuuji@0 928 }
yuuji@0 929 }
yuuji@0 930
yuuji@0 931 /* make sure all the updates take */
yuuji@0 932 if (!(ret && (ret = !fsync (fd)))) {
yuuji@0 933 sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
yuuji@0 934 mm_log (LOCAL->buf,ERROR);
yuuji@0 935 ftruncate (fd,sbuf.st_size);
yuuji@0 936 }
yuuji@0 937 if (cu && ret) { /* return sets if doing COPYUID */
yuuji@0 938 (*cu) (stream,mailbox,dstream->uid_validity,source,dest);
yuuji@0 939 lseek (fd,15,L_SET); /* update UIDLAST */
yuuji@0 940 sprintf (LOCAL->buf,"%08lx",dstream->uid_last);
yuuji@0 941 write (fd,LOCAL->buf,8);
yuuji@0 942 }
yuuji@0 943 else { /* flush any sets we may have built */
yuuji@0 944 mail_free_searchset (&source);
yuuji@0 945 mail_free_searchset (&dest);
yuuji@0 946 }
yuuji@0 947 /* set atime to now-1 if successful copy */
yuuji@0 948 if (ret) times.actime = time (0) - 1;
yuuji@0 949 /* else preserved \Marked status */
yuuji@0 950 else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
yuuji@0 951 sbuf.st_atime : time (0);
yuuji@0 952 times.modtime = sbuf.st_mtime;/* preserve mtime */
yuuji@0 953 utime (file,&times); /* set the times */
yuuji@0 954 close (fd); /* close the file */
yuuji@0 955 mm_nocritical (stream); /* release critical */
yuuji@0 956 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 957 /* delete all requested messages */
yuuji@0 958 if (ret && (options & CP_MOVE) && mbx_flaglock (stream)) {
yuuji@0 959 for (i = 1; i <= stream->nmsgs; i++) if (mail_elt (stream,i)->sequence) {
yuuji@0 960 /* mark message deleted */
yuuji@0 961 mbx_elt (stream,i,NIL)->deleted = T;
yuuji@0 962 /* recalculate status */
yuuji@0 963 mbx_update_status (stream,i,NIL);
yuuji@0 964 }
yuuji@0 965 /* update flags */
yuuji@0 966 mbx_flag (stream,NIL,NIL,NIL);
yuuji@0 967 }
yuuji@0 968 if (dstream != stream) mail_close (dstream);
yuuji@0 969 return ret;
yuuji@0 970 }
yuuji@0 971
yuuji@0 972 /* MBX mail append message from stringstruct
yuuji@0 973 * Accepts: MAIL stream
yuuji@0 974 * destination mailbox
yuuji@0 975 * append callback
yuuji@0 976 * data for callback
yuuji@0 977 * Returns: T if append successful, else NIL
yuuji@0 978 */
yuuji@0 979
yuuji@0 980 long mbx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
yuuji@0 981 {
yuuji@0 982 struct stat sbuf;
yuuji@0 983 int fd,ld;
yuuji@0 984 char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
yuuji@0 985 struct utimbuf times;
yuuji@0 986 FILE *df;
yuuji@0 987 MESSAGECACHE elt;
yuuji@0 988 long f;
yuuji@0 989 unsigned long i,uf;
yuuji@0 990 STRING *message;
yuuji@0 991 long ret = NIL;
yuuji@0 992 MAILSTREAM *dstream = NIL;
yuuji@0 993 appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL);
yuuji@0 994 SEARCHSET *dst = au ? mail_newsearchset () : NIL;
yuuji@0 995 /* make sure valid mailbox */
yuuji@0 996 /* make sure valid mailbox */
yuuji@0 997 if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock,
yuuji@0 998 au ? MBXISVALIDUID : MBXISVALIDNOUID)) < 0)
yuuji@0 999 switch (errno) {
yuuji@0 1000 case ENOENT: /* no such file? */
yuuji@0 1001 if (compare_cstring (mailbox,"INBOX")) {
yuuji@0 1002 mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
yuuji@0 1003 return NIL;
yuuji@0 1004 }
yuuji@0 1005 /* can create INBOX here */
yuuji@0 1006 mbx_create (dstream = stream ? stream : &mbxproto,"INBOX");
yuuji@0 1007 if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock,
yuuji@0 1008 au ? MBXISVALIDUID : MBXISVALIDNOUID)) < 0)
yuuji@0 1009 break;
yuuji@0 1010 case EACCES: /* file protected */
yuuji@0 1011 sprintf (tmp,"Can't access destination: %.80s",mailbox);
yuuji@0 1012 MM_LOG (tmp,ERROR);
yuuji@0 1013 return NIL;
yuuji@0 1014 case EINVAL:
yuuji@0 1015 sprintf (tmp,"Invalid MBX-format mailbox name: %.80s",mailbox);
yuuji@0 1016 mm_log (tmp,ERROR);
yuuji@0 1017 return NIL;
yuuji@0 1018 default:
yuuji@0 1019 sprintf (tmp,"Not a MBX-format mailbox: %.80s",mailbox);
yuuji@0 1020 mm_log (tmp,ERROR);
yuuji@0 1021 return NIL;
yuuji@0 1022 }
yuuji@0 1023
yuuji@0 1024 /* get first message */
yuuji@0 1025 if (!(*af) (dstream,data,&flags,&date,&message)) close (fd);
yuuji@0 1026 else if (!(df = fdopen (fd,"r+b"))) {
yuuji@0 1027 MM_LOG ("Unable to reopen append mailbox",ERROR);
yuuji@0 1028 close (fd);
yuuji@0 1029 }
yuuji@0 1030 else {
yuuji@0 1031 mm_critical (dstream); /* go critical */
yuuji@0 1032 fstat (fd,&sbuf); /* get current file size */
yuuji@0 1033 fseek (df,sbuf.st_size,SEEK_SET);
yuuji@0 1034 errno = 0;
yuuji@0 1035 for (ret = LONGT; ret && message; ) {
yuuji@0 1036 if (!SIZE (message)) { /* guard against zero-length */
yuuji@0 1037 mm_log ("Append of zero-length message",ERROR);
yuuji@0 1038 ret = NIL;
yuuji@0 1039 break;
yuuji@0 1040 }
yuuji@0 1041 f = mail_parse_flags (dstream,flags,&uf);
yuuji@0 1042 if (date) { /* parse date if given */
yuuji@0 1043 if (!mail_parse_date (&elt,date)) {
yuuji@0 1044 sprintf (tmp,"Bad date in append: %.80s",date);
yuuji@0 1045 mm_log (tmp,ERROR);
yuuji@0 1046 ret = NIL; /* mark failure */
yuuji@0 1047 break;
yuuji@0 1048 }
yuuji@0 1049 mail_date (tmp,&elt); /* write preseved date */
yuuji@0 1050 }
yuuji@0 1051 else internal_date (tmp); /* get current date in IMAP format */
yuuji@0 1052 /* write header */
yuuji@0 1053 if (fprintf (df,"%s,%lu;%08lx%04lx-%08lx\015\012",tmp,i = SIZE (message),
yuuji@0 1054 uf,(unsigned long) f,au ? ++dstream->uid_last : 0) < 0)
yuuji@0 1055 ret = NIL;
yuuji@0 1056 else { /* write message */
yuuji@0 1057 size_t j;
yuuji@0 1058 if (!message->cursize) SETPOS (message,GETPOS (message));
yuuji@0 1059 while (i && (j = fwrite (message->curpos,1,message->cursize,df))) {
yuuji@0 1060 i -= j;
yuuji@0 1061 SETPOS (message,GETPOS (message) + j);
yuuji@0 1062 }
yuuji@0 1063 /* get next message */
yuuji@0 1064 if (i || !(*af) (dstream,data,&flags,&date,&message)) ret = NIL;
yuuji@0 1065 else if (au) mail_append_set (dst,dstream->uid_last);
yuuji@0 1066 }
yuuji@0 1067 }
yuuji@0 1068
yuuji@0 1069 /* if error... */
yuuji@0 1070 if (!ret || (fflush (df) == EOF)) {
yuuji@0 1071 /* revert file */
yuuji@0 1072 ftruncate (fd,sbuf.st_size);
yuuji@0 1073 close (fd); /* make sure fclose() doesn't corrupt us */
yuuji@0 1074 if (errno) {
yuuji@0 1075 sprintf (tmp,"Message append failed: %s",strerror (errno));
yuuji@0 1076 mm_log (tmp,ERROR);
yuuji@0 1077 }
yuuji@0 1078 ret = NIL;
yuuji@0 1079 }
yuuji@0 1080 if (au && ret) { /* return sets if doing APPENDUID */
yuuji@0 1081 (*au) (mailbox,dstream->uid_validity,dst);
yuuji@0 1082 fseek (df,15,SEEK_SET); /* update UIDLAST */
yuuji@0 1083 fprintf (df,"%08lx",dstream->uid_last);
yuuji@0 1084 }
yuuji@0 1085 else mail_free_searchset (&dst);
yuuji@0 1086 if (ret) times.actime = time (0) - 1;
yuuji@0 1087 /* else preserve \Marked status */
yuuji@0 1088 else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
yuuji@0 1089 sbuf.st_atime : time (0);
yuuji@0 1090 /* preserve mtime */
yuuji@0 1091 times.modtime = sbuf.st_mtime;
yuuji@0 1092 utime (file,&times); /* set the times */
yuuji@0 1093 fclose (df); /* close the file */
yuuji@0 1094 mm_nocritical (dstream); /* release critical */
yuuji@0 1095 }
yuuji@0 1096 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 1097 if (dstream != stream) mail_close (dstream);
yuuji@0 1098 return ret;
yuuji@0 1099 }
yuuji@0 1100
yuuji@0 1101 /* Internal routines */
yuuji@0 1102
yuuji@0 1103
yuuji@0 1104 /* MBX mail parse mailbox
yuuji@0 1105 * Accepts: MAIL stream
yuuji@0 1106 * Returns: T if parse OK
yuuji@0 1107 * NIL if failure, stream aborted
yuuji@0 1108 */
yuuji@0 1109
yuuji@0 1110 long mbx_parse (MAILSTREAM *stream)
yuuji@0 1111 {
yuuji@0 1112 struct stat sbuf;
yuuji@0 1113 MESSAGECACHE *elt = NIL;
yuuji@0 1114 unsigned char c,*s,*t,*x;
yuuji@0 1115 char tmp[MAILTMPLEN];
yuuji@0 1116 unsigned long i,j,k,m;
yuuji@0 1117 off_t curpos = LOCAL->filesize;
yuuji@0 1118 unsigned long nmsgs = stream->nmsgs;
yuuji@0 1119 unsigned long recent = stream->recent;
yuuji@0 1120 unsigned long lastuid = 0;
yuuji@0 1121 short dirty = NIL;
yuuji@0 1122 short added = NIL;
yuuji@0 1123 short silent = stream->silent;
yuuji@0 1124 short uidwarn = T;
yuuji@0 1125 fstat (LOCAL->fd,&sbuf); /* get status */
yuuji@0 1126 if (sbuf.st_size < curpos) { /* sanity check */
yuuji@0 1127 sprintf (tmp,"Mailbox shrank from %lu to %lu!",
yuuji@0 1128 (unsigned long) curpos,(unsigned long) sbuf.st_size);
yuuji@0 1129 mm_log (tmp,ERROR);
yuuji@0 1130 mbx_abort (stream);
yuuji@0 1131 return NIL;
yuuji@0 1132 }
yuuji@0 1133 lseek (LOCAL->fd,0,L_SET); /* rewind file */
yuuji@0 1134 /* read internal header */
yuuji@0 1135 read (LOCAL->fd,LOCAL->buf,HDRSIZE);
yuuji@0 1136 LOCAL->buf[HDRSIZE] = '\0'; /* tie off header */
yuuji@0 1137 c = LOCAL->buf[15]; /* save first character of last UID */
yuuji@0 1138 LOCAL->buf[15] = '\0';
yuuji@0 1139 /* parse UID validity */
yuuji@0 1140 stream->uid_validity = strtoul (LOCAL->buf + 7,NIL,16);
yuuji@0 1141 LOCAL->buf[15] = c; /* restore first character of last UID */
yuuji@0 1142 /* parse last UID */
yuuji@0 1143 i = strtoul (LOCAL->buf + 15,NIL,16);
yuuji@0 1144 stream->uid_last = stream->rdonly ? max (i,stream->uid_last) : i;
yuuji@0 1145 /* parse user flags */
yuuji@0 1146 for (i = 0, s = LOCAL->buf + 25;
yuuji@0 1147 (i < NUSERFLAGS) && (t = strchr (s,'\015')) && (t - s);
yuuji@0 1148 i++, s = t + 2) {
yuuji@0 1149 *t = '\0'; /* tie off flag */
yuuji@0 1150 if (!stream->user_flags[i] && (strlen (s) <= MAXUSERFLAG))
yuuji@0 1151 stream->user_flags[i] = cpystr (s);
yuuji@0 1152 }
yuuji@0 1153 LOCAL->ffuserflag = (int) i; /* first free user flag */
yuuji@0 1154
yuuji@0 1155 stream->silent = T; /* don't pass up mm_exists() events yet */
yuuji@0 1156 while (sbuf.st_size - curpos){/* while there is stuff to parse */
yuuji@0 1157 /* get to that position in the file */
yuuji@0 1158 lseek (LOCAL->fd,curpos,L_SET);
yuuji@0 1159 if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
yuuji@0 1160 sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
yuuji@0 1161 (unsigned long) curpos,(unsigned long) sbuf.st_size,
yuuji@0 1162 i ? strerror (errno) : "no data read");
yuuji@0 1163 mm_log (tmp,ERROR);
yuuji@0 1164 mbx_abort (stream);
yuuji@0 1165 return NIL;
yuuji@0 1166 }
yuuji@0 1167 LOCAL->buf[i] = '\0'; /* tie off buffer just in case */
yuuji@0 1168 if (!((s = strchr (LOCAL->buf,'\015')) && (s[1] == '\012'))) {
yuuji@0 1169 sprintf (tmp,"Unable to find CRLF at %lu in %lu bytes, text: %.80s",
yuuji@0 1170 (unsigned long) curpos,i,(char *) LOCAL->buf);
yuuji@0 1171 mm_log (tmp,ERROR);
yuuji@0 1172 mbx_abort (stream);
yuuji@0 1173 return NIL;
yuuji@0 1174 }
yuuji@0 1175 *s = '\0'; /* tie off header line */
yuuji@0 1176 i = (s + 2) - LOCAL->buf; /* note start of text offset */
yuuji@0 1177 if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
yuuji@0 1178 sprintf (tmp,"Unable to parse internal header at %lu: %.80s",
yuuji@0 1179 (unsigned long) curpos,(char *) LOCAL->buf);
yuuji@0 1180 mm_log (tmp,ERROR);
yuuji@0 1181 mbx_abort (stream);
yuuji@0 1182 return NIL;
yuuji@0 1183 }
yuuji@0 1184 if (!(isxdigit (t[1]) && isxdigit (t[2]) && isxdigit (t[3]) &&
yuuji@0 1185 isxdigit (t[4]) && isxdigit (t[5]) && isxdigit (t[6]) &&
yuuji@0 1186 isxdigit (t[7]) && isxdigit (t[8]) && isxdigit (t[9]) &&
yuuji@0 1187 isxdigit (t[10]) && isxdigit (t[11]) && isxdigit (t[12]))) {
yuuji@0 1188 sprintf (tmp,"Unable to parse message flags at %lu: %.80s",
yuuji@0 1189 (unsigned long) curpos,(char *) LOCAL->buf);
yuuji@0 1190 mm_log (tmp,ERROR);
yuuji@0 1191 mbx_abort (stream);
yuuji@0 1192 return NIL;
yuuji@0 1193 }
yuuji@0 1194 if ((t[13] != '-') || t[22] ||
yuuji@0 1195 !(isxdigit (t[14]) && isxdigit (t[15]) && isxdigit (t[16]) &&
yuuji@0 1196 isxdigit (t[17]) && isxdigit (t[18]) && isxdigit (t[19]) &&
yuuji@0 1197 isxdigit (t[20]) && isxdigit (t[21]))) {
yuuji@0 1198 sprintf (tmp,"Unable to parse message UID at %lu: %.80s",
yuuji@0 1199 (unsigned long) curpos,(char *) LOCAL->buf);
yuuji@0 1200 mm_log (tmp,ERROR);
yuuji@0 1201 mbx_abort (stream);
yuuji@0 1202 return NIL;
yuuji@0 1203 }
yuuji@0 1204
yuuji@0 1205 *s++ = '\0'; *t++ = '\0'; /* break up fields */
yuuji@0 1206 /* get message size */
yuuji@0 1207 if (!(j = strtoul (s,(char **) &x,10)) && (!(x && *x))) {
yuuji@0 1208 sprintf (tmp,"Unable to parse message size at %lu: %.80s,%.80s;%.80s",
yuuji@0 1209 (unsigned long) curpos,(char *) LOCAL->buf,(char *) s,
yuuji@0 1210 (char *) t);
yuuji@0 1211 mm_log (tmp,ERROR);
yuuji@0 1212 mbx_abort (stream);
yuuji@0 1213 return NIL;
yuuji@0 1214 }
yuuji@0 1215 /* make sure didn't run off end of file */
yuuji@0 1216 if (((off_t) (curpos + i + j)) > sbuf.st_size) {
yuuji@0 1217 sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
yuuji@0 1218 (unsigned long) curpos,(unsigned long) (curpos + i + j),
yuuji@0 1219 (unsigned long) sbuf.st_size);
yuuji@0 1220 mm_log (tmp,ERROR);
yuuji@0 1221 mbx_abort (stream);
yuuji@0 1222 return NIL;
yuuji@0 1223 }
yuuji@0 1224 /* parse UID */
yuuji@0 1225 if ((m = strtoul (t+13,NIL,16)) &&
yuuji@0 1226 ((m <= lastuid) || (m > stream->uid_last))) {
yuuji@0 1227 if (uidwarn) {
yuuji@0 1228 sprintf (tmp,"Invalid UID %08lx in message %lu, rebuilding UIDs",
yuuji@0 1229 m,nmsgs+1);
yuuji@0 1230 mm_log (tmp,WARN);
yuuji@0 1231 uidwarn = NIL;
yuuji@0 1232 /* restart UID validity */
yuuji@0 1233 stream->uid_validity = (unsigned long) time (0);
yuuji@0 1234 }
yuuji@0 1235 m = 0; /* lose this UID */
yuuji@0 1236 dirty = T; /* mark dirty, set new lastuid */
yuuji@0 1237 stream->uid_last = lastuid;
yuuji@0 1238 }
yuuji@0 1239
yuuji@0 1240 t[12] = '\0'; /* parse system flags */
yuuji@0 1241 if ((k = strtoul (t+8,NIL,16)) & fEXPUNGED) {
yuuji@0 1242 if (m) lastuid = m; /* expunge message, update last UID seen */
yuuji@0 1243 else { /* no UID assigned? */
yuuji@0 1244 lastuid = ++stream->uid_last;
yuuji@0 1245 dirty = T;
yuuji@0 1246 }
yuuji@0 1247 }
yuuji@0 1248 else { /* not expunged, swell the cache */
yuuji@0 1249 added = T; /* note that a new message was added */
yuuji@0 1250 mail_exists (stream,++nmsgs);
yuuji@0 1251 /* instantiate an elt for this message */
yuuji@0 1252 (elt = mail_elt (stream,nmsgs))->valid = T;
yuuji@0 1253 /* parse the date */
yuuji@0 1254 if (!mail_parse_date (elt,LOCAL->buf)) {
yuuji@0 1255 sprintf (tmp,"Unable to parse message date at %lu: %.80s",
yuuji@0 1256 (unsigned long) curpos,(char *) LOCAL->buf);
yuuji@0 1257 mm_log (tmp,ERROR);
yuuji@0 1258 mbx_abort (stream);
yuuji@0 1259 return NIL;
yuuji@0 1260 }
yuuji@0 1261 /* note file offset of header */
yuuji@0 1262 elt->private.special.offset = curpos;
yuuji@0 1263 /* and internal header size */
yuuji@0 1264 elt->private.special.text.size = i;
yuuji@0 1265 /* header size not known yet */
yuuji@0 1266 elt->private.msg.header.text.size = 0;
yuuji@0 1267 elt->rfc822_size = j; /* note message size */
yuuji@0 1268 /* calculate system flags */
yuuji@0 1269 if (k & fSEEN) elt->seen = T;
yuuji@0 1270 if (k & fDELETED) elt->deleted = T;
yuuji@0 1271 if (k & fFLAGGED) elt->flagged = T;
yuuji@0 1272 if (k & fANSWERED) elt->answered = T;
yuuji@0 1273 if (k & fDRAFT) elt->draft = T;
yuuji@0 1274 t[8] = '\0'; /* get user flags value */
yuuji@0 1275 elt->user_flags = strtoul (t,NIL,16);
yuuji@0 1276 /* UID already assigned? */
yuuji@0 1277 if (!(elt->private.uid = m) || !(k & fOLD)) {
yuuji@0 1278 elt->recent = T; /* no, mark as recent */
yuuji@0 1279 ++recent; /* count up a new recent message */
yuuji@0 1280 dirty = T; /* and must rewrite header */
yuuji@0 1281 /* assign new UID */
yuuji@0 1282 if (!elt->private.uid) elt->private.uid = ++stream->uid_last;
yuuji@0 1283 mbx_update_status (stream,elt->msgno,NIL);
yuuji@0 1284 }
yuuji@0 1285 /* update last parsed UID */
yuuji@0 1286 lastuid = elt->private.uid;
yuuji@0 1287 }
yuuji@0 1288 curpos += i + j; /* update position */
yuuji@0 1289 }
yuuji@0 1290
yuuji@0 1291 if (dirty && !stream->rdonly){/* update header */
yuuji@0 1292 mbx_update_header (stream);
yuuji@0 1293 fsync (LOCAL->fd); /* make sure all the UID updates take */
yuuji@0 1294 }
yuuji@0 1295 /* update parsed file size and time */
yuuji@0 1296 LOCAL->filesize = sbuf.st_size;
yuuji@0 1297 fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */
yuuji@0 1298 LOCAL->filetime = sbuf.st_mtime;
yuuji@0 1299 if (added && !stream->rdonly){/* make sure atime updated */
yuuji@0 1300 struct utimbuf times;
yuuji@0 1301 times.actime = time (0);
yuuji@0 1302 times.modtime = LOCAL->filetime;
yuuji@0 1303 utime (stream->mailbox,&times);
yuuji@0 1304 }
yuuji@0 1305 stream->silent = silent; /* can pass up events now */
yuuji@0 1306 mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */
yuuji@0 1307 mail_recent (stream,recent); /* and of change in recent messages */
yuuji@0 1308 return LONGT; /* return the winnage */
yuuji@0 1309 }
yuuji@0 1310
yuuji@0 1311 /* MBX get cache element with status updating from file
yuuji@0 1312 * Accepts: MAIL stream
yuuji@0 1313 * message number
yuuji@0 1314 * expunge OK flag
yuuji@0 1315 * Returns: cache element
yuuji@0 1316 */
yuuji@0 1317
yuuji@0 1318 MESSAGECACHE *mbx_elt (MAILSTREAM *stream,unsigned long msgno,long expok)
yuuji@0 1319 {
yuuji@0 1320 MESSAGECACHE *elt = mail_elt (stream,msgno);
yuuji@0 1321 struct { /* old flags */
yuuji@0 1322 unsigned int seen : 1;
yuuji@0 1323 unsigned int deleted : 1;
yuuji@0 1324 unsigned int flagged : 1;
yuuji@0 1325 unsigned int answered : 1;
yuuji@0 1326 unsigned int draft : 1;
yuuji@0 1327 unsigned long user_flags;
yuuji@0 1328 } old;
yuuji@0 1329 old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
yuuji@0 1330 old.answered = elt->answered; old.draft = elt->draft;
yuuji@0 1331 old.user_flags = elt->user_flags;
yuuji@0 1332 /* get new flags */
yuuji@0 1333 if (mbx_read_flags (stream,elt) && expok) {
yuuji@0 1334 mail_expunged (stream,elt->msgno);
yuuji@0 1335 return NIL; /* return this message was expunged */
yuuji@0 1336 }
yuuji@0 1337 if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
yuuji@0 1338 (old.flagged != elt->flagged) || (old.answered != elt->answered) ||
yuuji@0 1339 (old.draft != elt->draft) || (old.user_flags != elt->user_flags))
yuuji@0 1340 mm_flags (stream,msgno); /* let top level know */
yuuji@0 1341 return elt;
yuuji@0 1342 }
yuuji@0 1343
yuuji@0 1344 /* MBX read flags from file
yuuji@0 1345 * Accepts: MAIL stream
yuuji@0 1346 * cache element
yuuji@0 1347 * Returns: non-NIL if message expunged
yuuji@0 1348 */
yuuji@0 1349
yuuji@0 1350 unsigned long mbx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
yuuji@0 1351 {
yuuji@0 1352 unsigned long i;
yuuji@0 1353 struct stat sbuf;
yuuji@0 1354 fstat (LOCAL->fd,&sbuf); /* get status */
yuuji@0 1355 /* make sure file size is good */
yuuji@0 1356 if (sbuf.st_size < LOCAL->filesize) {
yuuji@0 1357 sprintf (LOCAL->buf,"Mailbox shrank from %lu to %lu in flag read!",
yuuji@0 1358 (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size);
yuuji@0 1359 fatal (LOCAL->buf);
yuuji@0 1360 }
yuuji@0 1361 /* set the seek pointer */
yuuji@0 1362 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
yuuji@0 1363 elt->private.special.text.size - 24,L_SET);
yuuji@0 1364 /* read the new flags */
yuuji@0 1365 if (read (LOCAL->fd,LOCAL->buf,14) < 0) {
yuuji@0 1366 sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
yuuji@0 1367 fatal (LOCAL->buf);
yuuji@0 1368 }
yuuji@0 1369 if ((LOCAL->buf[0] != ';') || (LOCAL->buf[13] != '-')) {
yuuji@0 1370 LOCAL->buf[14] = '\0'; /* tie off buffer for error message */
yuuji@0 1371 sprintf (LOCAL->buf+50,"Invalid flags for message %lu (%lu %lu): %s",
yuuji@0 1372 elt->msgno,elt->private.special.offset,
yuuji@0 1373 elt->private.special.text.size,(char *) LOCAL->buf);
yuuji@0 1374 fatal (LOCAL->buf+50);
yuuji@0 1375 }
yuuji@0 1376 LOCAL->buf[13] = '\0'; /* tie off buffer */
yuuji@0 1377 /* calculate system flags */
yuuji@0 1378 i = strtoul (LOCAL->buf+9,NIL,16);
yuuji@0 1379 elt->seen = i & fSEEN ? T : NIL;
yuuji@0 1380 elt->deleted = i & fDELETED ? T : NIL;
yuuji@0 1381 elt->flagged = i & fFLAGGED ? T : NIL;
yuuji@0 1382 elt->answered = i & fANSWERED ? T : NIL;
yuuji@0 1383 elt->draft = i & fDRAFT ? T : NIL;
yuuji@0 1384 LOCAL->expunged |= i & fEXPUNGED ? T : NIL;
yuuji@0 1385 LOCAL->buf[9] = '\0'; /* tie off flags */
yuuji@0 1386 /* get user flags value */
yuuji@0 1387 elt->user_flags = strtoul (LOCAL->buf+1,NIL,16);
yuuji@0 1388 elt->valid = T; /* have valid flags now */
yuuji@0 1389 return i & fEXPUNGED;
yuuji@0 1390 }
yuuji@0 1391
yuuji@0 1392 /* MBX update header
yuuji@0 1393 * Accepts: MAIL stream
yuuji@0 1394 */
yuuji@0 1395
yuuji@0 1396 #define NTKLUDGEOFFSET 7
yuuji@0 1397
yuuji@0 1398 void mbx_update_header (MAILSTREAM *stream)
yuuji@0 1399 {
yuuji@0 1400 int i;
yuuji@0 1401 char *s = LOCAL->buf;
yuuji@0 1402 memset (s,'\0',HDRSIZE); /* initialize header */
yuuji@0 1403 sprintf (s,"*mbx*\015\012%08lx%08lx\015\012",
yuuji@0 1404 stream->uid_validity,stream->uid_last);
yuuji@0 1405 for (i = 0; (i < NUSERFLAGS) && stream->user_flags[i]; ++i)
yuuji@0 1406 sprintf (s += strlen (s),"%s\015\012",stream->user_flags[i]);
yuuji@0 1407 LOCAL->ffuserflag = i; /* first free user flag */
yuuji@0 1408 /* can we create more user flags? */
yuuji@0 1409 stream->kwd_create = (i < NUSERFLAGS) ? T : NIL;
yuuji@0 1410 /* write reserved lines */
yuuji@0 1411 while (i++ < NUSERFLAGS) strcat (s,"\015\012");
yuuji@0 1412 while (T) { /* rewind file */
yuuji@0 1413 lseek (LOCAL->fd,NTKLUDGEOFFSET,L_SET);
yuuji@0 1414 /* write new header */
yuuji@0 1415 if (write (LOCAL->fd,LOCAL->buf + NTKLUDGEOFFSET,
yuuji@0 1416 HDRSIZE - NTKLUDGEOFFSET) > 0) break;
yuuji@0 1417 mm_notify (stream,strerror (errno),WARN);
yuuji@0 1418 mm_diskerror (stream,errno,T);
yuuji@0 1419 }
yuuji@0 1420 }
yuuji@0 1421
yuuji@0 1422 /* MBX update status string
yuuji@0 1423 * Accepts: MAIL stream
yuuji@0 1424 * message number
yuuji@0 1425 * flags
yuuji@0 1426 */
yuuji@0 1427
yuuji@0 1428 void mbx_update_status (MAILSTREAM *stream,unsigned long msgno,long flags)
yuuji@0 1429 {
yuuji@0 1430 struct stat sbuf;
yuuji@0 1431 MESSAGECACHE *elt = mail_elt (stream,msgno);
yuuji@0 1432 /* readonly */
yuuji@0 1433 if (stream->rdonly || !elt->valid) mbx_read_flags (stream,elt);
yuuji@0 1434 else { /* readwrite */
yuuji@0 1435 fstat (LOCAL->fd,&sbuf); /* get status */
yuuji@0 1436 /* make sure file size is good */
yuuji@0 1437 if (sbuf.st_size < LOCAL->filesize) {
yuuji@0 1438 sprintf (LOCAL->buf,"Mailbox shrank from %lu to %lu in flag update!",
yuuji@0 1439 (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size);
yuuji@0 1440 fatal (LOCAL->buf);
yuuji@0 1441 }
yuuji@0 1442 /* set the seek pointer */
yuuji@0 1443 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
yuuji@0 1444 elt->private.special.text.size - 24,L_SET);
yuuji@0 1445 /* read the new flags */
yuuji@0 1446 if (read (LOCAL->fd,LOCAL->buf,14) < 0) {
yuuji@0 1447 sprintf (LOCAL->buf,"Unable to read old status: %s",strerror (errno));
yuuji@0 1448 fatal (LOCAL->buf);
yuuji@0 1449 }
yuuji@0 1450 if ((LOCAL->buf[0] != ';') || (LOCAL->buf[13] != '-')) {
yuuji@0 1451 LOCAL->buf[14] = '\0'; /* tie off buffer for error message */
yuuji@0 1452 sprintf (LOCAL->buf+50,"Invalid flags for message %lu (%lu %lu): %s",
yuuji@0 1453 elt->msgno,elt->private.special.offset,
yuuji@0 1454 elt->private.special.text.size,(char *) LOCAL->buf);
yuuji@0 1455 fatal (LOCAL->buf+50);
yuuji@0 1456 }
yuuji@0 1457 /* print new flag string */
yuuji@0 1458 sprintf (LOCAL->buf,"%08lx%04x-%08lx",elt->user_flags,(unsigned)
yuuji@0 1459 (((elt->deleted && flags) ?
yuuji@0 1460 fEXPUNGED : (strtoul (LOCAL->buf+9,NIL,16)) & fEXPUNGED) +
yuuji@0 1461 (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
yuuji@0 1462 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
yuuji@0 1463 (fDRAFT * elt->draft) + fOLD),elt->private.uid);
yuuji@0 1464 while (T) { /* get to that place in the file */
yuuji@0 1465 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
yuuji@0 1466 elt->private.special.text.size - 23,L_SET);
yuuji@0 1467 /* write new flags and UID */
yuuji@0 1468 if (write (LOCAL->fd,LOCAL->buf,21) > 0) break;
yuuji@0 1469 mm_notify (stream,strerror (errno),WARN);
yuuji@0 1470 mm_diskerror (stream,errno,T);
yuuji@0 1471 }
yuuji@0 1472 }
yuuji@0 1473 }
yuuji@0 1474
yuuji@0 1475 /* MBX locate header for a message
yuuji@0 1476 * Accepts: MAIL stream
yuuji@0 1477 * message number
yuuji@0 1478 * pointer to returned header size
yuuji@0 1479 * pointer to possible returned header
yuuji@0 1480 * Returns: position of header in file
yuuji@0 1481 */
yuuji@0 1482
yuuji@0 1483 #define HDRBUFLEN 16384 /* good enough for most headers */
yuuji@0 1484 #define SLOP 4 /* CR LF CR LF */
yuuji@0 1485
yuuji@0 1486 unsigned long mbx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
yuuji@0 1487 unsigned long *size,char **hdr)
yuuji@0 1488 {
yuuji@0 1489 unsigned long siz,done;
yuuji@0 1490 long i;
yuuji@0 1491 unsigned char *s,*t,*te;
yuuji@0 1492 MESSAGECACHE *elt = mail_elt (stream,msgno);
yuuji@0 1493 unsigned long ret = elt->private.special.offset +
yuuji@0 1494 elt->private.special.text.size;
yuuji@0 1495 if (hdr) *hdr = NIL; /* assume no header returned */
yuuji@0 1496 /* is header size known? */
yuuji@0 1497 if (*size = elt->private.msg.header.text.size) return ret;
yuuji@0 1498 /* paranoia check */
yuuji@0 1499 if (LOCAL->buflen < (HDRBUFLEN + SLOP))
yuuji@0 1500 fatal ("LOCAL->buf smaller than HDRBUFLEN");
yuuji@0 1501 lseek (LOCAL->fd,ret,L_SET); /* get to header position */
yuuji@0 1502 /* read HDRBUFLEN chunks with 4 byte slop */
yuuji@0 1503 for (done = siz = 0, s = LOCAL->buf;
yuuji@0 1504 (i = min ((long) (elt->rfc822_size - done),(long) HDRBUFLEN)) &&
yuuji@0 1505 (read (LOCAL->fd,s,i) == i);
yuuji@0 1506 done += i, siz += (t - LOCAL->buf) - SLOP, s = LOCAL->buf + SLOP) {
yuuji@0 1507 te = (t = s + i) - 12; /* calculate end of fast scan */
yuuji@0 1508 /* fast scan for CR */
yuuji@0 1509 for (s = LOCAL->buf; s < te;)
yuuji@0 1510 if (((*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015') ||
yuuji@0 1511 (*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015') ||
yuuji@0 1512 (*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015') ||
yuuji@0 1513 (*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015')) &&
yuuji@0 1514 (*s == '\012') && (*++s == '\015') && (*++s == '\012')) {
yuuji@0 1515 *size = elt->private.msg.header.text.size = siz + (++s - LOCAL->buf);
yuuji@0 1516 if (hdr) *hdr = LOCAL->buf;
yuuji@0 1517 return ret;
yuuji@0 1518 }
yuuji@0 1519 for (te = t - 3; (s < te);) /* final character-at-a-time scan */
yuuji@0 1520 if ((*s++ == '\015') && (*s == '\012') && (*++s == '\015') &&
yuuji@0 1521 (*++s == '\012')) {
yuuji@0 1522 *size = elt->private.msg.header.text.size = siz + (++s - LOCAL->buf);
yuuji@0 1523 if (hdr) *hdr = LOCAL->buf;
yuuji@0 1524 return ret;
yuuji@0 1525 }
yuuji@0 1526 if (i <= SLOP) break; /* end of data */
yuuji@0 1527 /* slide over last 4 bytes */
yuuji@0 1528 memmove (LOCAL->buf,t - SLOP,SLOP);
yuuji@0 1529 hdr = NIL; /* can't return header this way */
yuuji@0 1530 }
yuuji@0 1531 /* not found: header consumes entire message */
yuuji@0 1532 elt->private.msg.header.text.size = *size = elt->rfc822_size;
yuuji@0 1533 if (hdr) *hdr = LOCAL->buf; /* possibly return header too */
yuuji@0 1534 return ret;
yuuji@0 1535 }
yuuji@0 1536
yuuji@0 1537 /* MBX mail rewrite mailbox
yuuji@0 1538 * Accepts: MAIL stream
yuuji@0 1539 * pointer to return reclaimed size
yuuji@0 1540 * flags (0 = no expunge, 1 = expunge deleted, -1 = expunge sequence)
yuuji@0 1541 * Returns: number of expunged messages
yuuji@0 1542 */
yuuji@0 1543
yuuji@0 1544 unsigned long mbx_rewrite (MAILSTREAM *stream,unsigned long *reclaimed,
yuuji@0 1545 long flags)
yuuji@0 1546 {
yuuji@0 1547 struct utimbuf times;
yuuji@0 1548 struct stat sbuf;
yuuji@0 1549 off_t pos,ppos;
yuuji@0 1550 int ld;
yuuji@0 1551 unsigned long i,j,k,m,delta;
yuuji@0 1552 unsigned long n = *reclaimed = 0;
yuuji@0 1553 unsigned long recent = 0;
yuuji@0 1554 char lock[MAILTMPLEN];
yuuji@0 1555 MESSAGECACHE *elt;
yuuji@0 1556 /* get parse/append permission */
yuuji@0 1557 if ((ld = lockname (lock,stream->mailbox,LOCK_EX)) < 0) {
yuuji@0 1558 mm_log ("Unable to lock expunge mailbox",ERROR);
yuuji@0 1559 return 0;
yuuji@0 1560 }
yuuji@0 1561 fstat (LOCAL->fd,&sbuf); /* get current write time */
yuuji@0 1562 if (LOCAL->filetime && !LOCAL->flagcheck &&
yuuji@0 1563 (LOCAL->filetime < sbuf.st_mtime)) LOCAL->flagcheck = T;
yuuji@0 1564 if (!mbx_parse (stream)) { /* make sure see any newly-arrived messages */
yuuji@0 1565 unlockfd (ld,lock); /* failed?? */
yuuji@0 1566 return 0;
yuuji@0 1567 }
yuuji@0 1568 if (LOCAL->flagcheck) { /* sweep flags if need flagcheck */
yuuji@0 1569 LOCAL->filetime = sbuf.st_mtime;
yuuji@0 1570 for (i = 1; i <= stream->nmsgs; ++i) mbx_elt (stream,i,NIL);
yuuji@0 1571 LOCAL->flagcheck = NIL;
yuuji@0 1572 }
yuuji@0 1573
yuuji@0 1574 /* get exclusive access */
yuuji@0 1575 if (!flock (LOCAL->fd,LOCK_EX|LOCK_NB)) {
yuuji@0 1576 mm_critical (stream); /* go critical */
yuuji@0 1577 for (i = 1,delta = 0,pos = ppos = HDRSIZE; i <= stream->nmsgs; ) {
yuuji@0 1578 /* note if message not at predicted location */
yuuji@0 1579 if (m = (elt = mbx_elt (stream,i,NIL))->private.special.offset - ppos) {
yuuji@0 1580 ppos = elt->private.special.offset;
yuuji@0 1581 *reclaimed += m; /* note reclaimed message space */
yuuji@0 1582 delta += m; /* and as expunge delta */
yuuji@0 1583 }
yuuji@0 1584 /* number of bytes to smash or preserve */
yuuji@0 1585 ppos += (k = elt->private.special.text.size + elt->rfc822_size);
yuuji@0 1586 /* if need to expunge this message*/
yuuji@0 1587 if (flags && elt->deleted && ((flags > 0) || elt->sequence)) {
yuuji@0 1588 delta += k; /* number of bytes to delete */
yuuji@0 1589 mail_expunged(stream,i);/* notify upper levels */
yuuji@0 1590 n++; /* count up one more expunged message */
yuuji@0 1591 }
yuuji@0 1592 else { /* preserved message */
yuuji@0 1593 i++; /* count this message */
yuuji@0 1594 if (elt->recent) ++recent;
yuuji@0 1595 if (delta) { /* moved, note first byte to preserve */
yuuji@0 1596 j = elt->private.special.offset;
yuuji@0 1597 do { /* read from source position */
yuuji@0 1598 m = min (k,LOCAL->buflen);
yuuji@0 1599 lseek (LOCAL->fd,j,L_SET);
yuuji@0 1600 read (LOCAL->fd,LOCAL->buf,m);
yuuji@0 1601 pos = j - delta; /* write to destination position */
yuuji@0 1602 while (T) {
yuuji@0 1603 lseek (LOCAL->fd,pos,L_SET);
yuuji@0 1604 if (write (LOCAL->fd,LOCAL->buf,m) > 0) break;
yuuji@0 1605 mm_notify (stream,strerror (errno),WARN);
yuuji@0 1606 mm_diskerror (stream,errno,T);
yuuji@0 1607 }
yuuji@0 1608 pos += m; /* new position */
yuuji@0 1609 j += m; /* next chunk, perhaps */
yuuji@0 1610 } while (k -= m); /* until done */
yuuji@0 1611 /* note the new address of this text */
yuuji@0 1612 elt->private.special.offset -= delta;
yuuji@0 1613 }
yuuji@0 1614 /* preserved but no deleted messages yet */
yuuji@0 1615 else pos = elt->private.special.offset + k;
yuuji@0 1616 }
yuuji@0 1617 }
yuuji@0 1618 /* deltaed file size match position? */
yuuji@0 1619 if (m = (LOCAL->filesize -= delta) - pos) {
yuuji@0 1620 *reclaimed += m; /* probably an fEXPUNGED msg */
yuuji@0 1621 LOCAL->filesize = pos; /* set correct size */
yuuji@0 1622 }
yuuji@0 1623 /* truncate file after last message */
yuuji@0 1624 ftruncate (LOCAL->fd,LOCAL->filesize);
yuuji@0 1625 fsync (LOCAL->fd); /* force disk update */
yuuji@0 1626 mm_nocritical (stream); /* release critical */
yuuji@0 1627 flock (LOCAL->fd,LOCK_SH); /* allow sharers again */
yuuji@0 1628 }
yuuji@0 1629
yuuji@0 1630 else { /* can't get exclusive */
yuuji@0 1631 flock (LOCAL->fd,LOCK_SH); /* recover previous shared mailbox lock */
yuuji@0 1632 /* do hide-expunge when shared */
yuuji@0 1633 if (flags) for (i = 1; i <= stream->nmsgs; ) {
yuuji@0 1634 if (elt = mbx_elt (stream,i,T)) {
yuuji@0 1635 /* make the message invisible */
yuuji@0 1636 if (elt->deleted && ((flags > 0) || elt->sequence)) {
yuuji@0 1637 mbx_update_status (stream,elt->msgno,LONGT);
yuuji@0 1638 /* notify upper levels */
yuuji@0 1639 mail_expunged (stream,i);
yuuji@0 1640 n++; /* count up one more expunged message */
yuuji@0 1641 }
yuuji@0 1642 else {
yuuji@0 1643 i++; /* preserved message */
yuuji@0 1644 if (elt->recent) ++recent;
yuuji@0 1645 }
yuuji@0 1646 }
yuuji@0 1647 else n++; /* count up one more expunged message */
yuuji@0 1648 }
yuuji@0 1649 fsync (LOCAL->fd); /* force disk update */
yuuji@0 1650 }
yuuji@0 1651 fstat (LOCAL->fd,&sbuf); /* get new write time */
yuuji@0 1652 times.modtime = LOCAL->filetime = sbuf.st_mtime;
yuuji@0 1653 times.actime = time (0); /* reset atime to now */
yuuji@0 1654 utime (stream->mailbox,&times);
yuuji@0 1655 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 1656 /* notify upper level of new mailbox size */
yuuji@0 1657 mail_exists (stream,stream->nmsgs);
yuuji@0 1658 mail_recent (stream,recent);
yuuji@0 1659 return n; /* return number of expunged messages */
yuuji@0 1660 }
yuuji@0 1661
yuuji@0 1662 /* MBX mail lock for flag updating
yuuji@0 1663 * Accepts: stream
yuuji@0 1664 * Returns: T if successful, NIL if failure
yuuji@0 1665 */
yuuji@0 1666
yuuji@0 1667 long mbx_flaglock (MAILSTREAM *stream)
yuuji@0 1668 {
yuuji@0 1669 struct stat sbuf;
yuuji@0 1670 unsigned long i;
yuuji@0 1671 int ld;
yuuji@0 1672 char lock[MAILTMPLEN];
yuuji@0 1673 /* no-op if readonly or already locked */
yuuji@0 1674 if (!stream->rdonly && LOCAL && (LOCAL->fd >= 0) && (LOCAL->ld < 0)) {
yuuji@0 1675 /* lock now */
yuuji@0 1676 if ((ld = lockname (lock,stream->mailbox,LOCK_EX)) < 0) return NIL;
yuuji@0 1677 if (!LOCAL->flagcheck) { /* don't do this if flagcheck already needed */
yuuji@0 1678 if (LOCAL->filetime) { /* know previous time? */
yuuji@0 1679 fstat (LOCAL->fd,&sbuf);/* get current write time */
yuuji@0 1680 if (LOCAL->filetime < sbuf.st_mtime) LOCAL->flagcheck = T;
yuuji@0 1681 LOCAL->filetime = 0; /* don't do this test for any other messages */
yuuji@0 1682 }
yuuji@0 1683 if (!mbx_parse (stream)) {/* parse mailbox */
yuuji@0 1684 unlockfd (ld,lock); /* shouldn't happen */
yuuji@0 1685 return NIL;
yuuji@0 1686 }
yuuji@0 1687 if (LOCAL->flagcheck) /* invalidate cache if flagcheck */
yuuji@0 1688 for (i = 1; i <= stream->nmsgs; ++i) mail_elt (stream,i)->valid = NIL;
yuuji@0 1689 }
yuuji@0 1690 LOCAL->ld = ld; /* copy to stream for subsequent calls */
yuuji@0 1691 memcpy (LOCAL->lock,lock,MAILTMPLEN);
yuuji@0 1692 }
yuuji@0 1693 return LONGT;
yuuji@0 1694 }

UW-IMAP'd extensions by yuuji