imapext-2007

annotate src/osdep/nt/mtxnt.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: MTX 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: 22 May 1990
yuuji@0 26 * Last Edited: 15 June 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 /* MTX I/O stream local data */
yuuji@0 52
yuuji@0 53 typedef struct mtx_local {
yuuji@0 54 unsigned int shouldcheck: 1; /* if ping should do a check instead */
yuuji@0 55 unsigned int mustcheck: 1; /* if ping must do a check instead */
yuuji@0 56 int fd; /* file descriptor for I/O */
yuuji@0 57 off_t filesize; /* file size parsed */
yuuji@0 58 time_t filetime; /* last file time */
yuuji@0 59 time_t lastsnarf; /* last snarf time */
yuuji@0 60 unsigned char *buf; /* temporary buffer */
yuuji@0 61 unsigned long buflen; /* current size of temporary buffer */
yuuji@0 62 } MTXLOCAL;
yuuji@0 63
yuuji@0 64
yuuji@0 65 /* Convenient access to local data */
yuuji@0 66
yuuji@0 67 #define LOCAL ((MTXLOCAL *) stream->local)
yuuji@0 68
yuuji@0 69
yuuji@0 70 /* Function prototypes */
yuuji@0 71
yuuji@0 72 DRIVER *mtx_valid (char *name);
yuuji@0 73 int mtx_isvalid (char *name,char *file);
yuuji@0 74 void *mtx_parameters (long function,void *value);
yuuji@0 75 void mtx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
yuuji@0 76 void mtx_list (MAILSTREAM *stream,char *ref,char *pat);
yuuji@0 77 void mtx_lsub (MAILSTREAM *stream,char *ref,char *pat);
yuuji@0 78 long mtx_create (MAILSTREAM *stream,char *mailbox);
yuuji@0 79 long mtx_delete (MAILSTREAM *stream,char *mailbox);
yuuji@0 80 long mtx_rename (MAILSTREAM *stream,char *old,char *newname);
yuuji@0 81 long mtx_status (MAILSTREAM *stream,char *mbx,long flags);
yuuji@0 82 MAILSTREAM *mtx_open (MAILSTREAM *stream);
yuuji@0 83 void mtx_close (MAILSTREAM *stream,long options);
yuuji@0 84 void mtx_flags (MAILSTREAM *stream,char *sequence,long flags);
yuuji@0 85 char *mtx_header (MAILSTREAM *stream,unsigned long msgno,
yuuji@0 86 unsigned long *length,long flags);
yuuji@0 87 long mtx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
yuuji@0 88 void mtx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
yuuji@0 89 void mtx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
yuuji@0 90 long mtx_ping (MAILSTREAM *stream);
yuuji@0 91 void mtx_check (MAILSTREAM *stream);
yuuji@0 92 void mtx_snarf (MAILSTREAM *stream);
yuuji@0 93 long mtx_expunge (MAILSTREAM *stream,char *sequence,long options);
yuuji@0 94 long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
yuuji@0 95 long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
yuuji@0 96
yuuji@0 97 long mtx_parse (MAILSTREAM *stream);
yuuji@0 98 MESSAGECACHE *mtx_elt (MAILSTREAM *stream,unsigned long msgno);
yuuji@0 99 void mtx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt);
yuuji@0 100 void mtx_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag);
yuuji@0 101 unsigned long mtx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
yuuji@0 102 unsigned long *size);
yuuji@0 103
yuuji@0 104
yuuji@0 105 /* MTX mail routines */
yuuji@0 106
yuuji@0 107
yuuji@0 108 /* Driver dispatch used by MAIL */
yuuji@0 109
yuuji@0 110 DRIVER mtxdriver = {
yuuji@0 111 "mtx", /* driver name */
yuuji@0 112 /* driver flags */
yuuji@0 113 DR_LOCAL|DR_MAIL|DR_CRLF|DR_NOSTICKY,
yuuji@0 114 (DRIVER *) NIL, /* next driver */
yuuji@0 115 mtx_valid, /* mailbox is valid for us */
yuuji@0 116 mtx_parameters, /* manipulate parameters */
yuuji@0 117 mtx_scan, /* scan mailboxes */
yuuji@0 118 mtx_list, /* list mailboxes */
yuuji@0 119 mtx_lsub, /* list subscribed mailboxes */
yuuji@0 120 NIL, /* subscribe to mailbox */
yuuji@0 121 NIL, /* unsubscribe from mailbox */
yuuji@0 122 mtx_create, /* create mailbox */
yuuji@0 123 mtx_delete, /* delete mailbox */
yuuji@0 124 mtx_rename, /* rename mailbox */
yuuji@0 125 mail_status_default, /* status of mailbox */
yuuji@0 126 mtx_open, /* open mailbox */
yuuji@0 127 mtx_close, /* close mailbox */
yuuji@0 128 mtx_flags, /* fetch message "fast" attributes */
yuuji@0 129 mtx_flags, /* fetch message flags */
yuuji@0 130 NIL, /* fetch overview */
yuuji@0 131 NIL, /* fetch message envelopes */
yuuji@0 132 mtx_header, /* fetch message header */
yuuji@0 133 mtx_text, /* fetch message body */
yuuji@0 134 NIL, /* fetch partial message text */
yuuji@0 135 NIL, /* unique identifier */
yuuji@0 136 NIL, /* message number */
yuuji@0 137 mtx_flag, /* modify flags */
yuuji@0 138 mtx_flagmsg, /* per-message modify flags */
yuuji@0 139 NIL, /* search for message based on criteria */
yuuji@0 140 NIL, /* sort messages */
yuuji@0 141 NIL, /* thread messages */
yuuji@0 142 mtx_ping, /* ping mailbox to see if still alive */
yuuji@0 143 mtx_check, /* check for new messages */
yuuji@0 144 mtx_expunge, /* expunge deleted messages */
yuuji@0 145 mtx_copy, /* copy messages to another mailbox */
yuuji@0 146 mtx_append, /* append string message to mailbox */
yuuji@0 147 NIL /* garbage collect stream */
yuuji@0 148 };
yuuji@0 149
yuuji@0 150 /* prototype stream */
yuuji@0 151 MAILSTREAM mtxproto = {&mtxdriver};
yuuji@0 152
yuuji@0 153 /* MTX mail validate mailbox
yuuji@0 154 * Accepts: mailbox name
yuuji@0 155 * Returns: our driver if name is valid, NIL otherwise
yuuji@0 156 */
yuuji@0 157
yuuji@0 158 DRIVER *mtx_valid (char *name)
yuuji@0 159 {
yuuji@0 160 char tmp[MAILTMPLEN];
yuuji@0 161 return mtx_isvalid (name,tmp) ? &mtxdriver : NIL;
yuuji@0 162 }
yuuji@0 163
yuuji@0 164
yuuji@0 165 /* MTX mail test for valid mailbox
yuuji@0 166 * Accepts: mailbox name
yuuji@0 167 * buffer to return file name
yuuji@0 168 * Returns: T if valid, NIL otherwise
yuuji@0 169 */
yuuji@0 170
yuuji@0 171 int mtx_isvalid (char *name,char *file)
yuuji@0 172 {
yuuji@0 173 int fd;
yuuji@0 174 int ret = NIL;
yuuji@0 175 char *s,tmp[MAILTMPLEN];
yuuji@0 176 struct stat sbuf;
yuuji@0 177 struct utimbuf times;
yuuji@0 178 errno = EINVAL; /* assume invalid argument */
yuuji@0 179 /* if file, get its status */
yuuji@0 180 if ((s = dummy_file (file,name)) && !stat (s,&sbuf) &&
yuuji@0 181 ((sbuf.st_mode & S_IFMT) == S_IFREG)) {
yuuji@0 182 if (!sbuf.st_size)errno = 0;/* empty file */
yuuji@0 183 else if ((fd = open (file,O_BINARY|O_RDONLY,NIL)) >= 0) {
yuuji@0 184 memset (tmp,'\0',MAILTMPLEN);
yuuji@0 185 if ((read (fd,tmp,64) >= 0) && (s = strchr (tmp,'\015')) &&
yuuji@0 186 (s[1] == '\012')) { /* valid format? */
yuuji@0 187 *s = '\0'; /* tie off header */
yuuji@0 188 /* must begin with dd-mmm-yy" */
yuuji@0 189 ret = (((tmp[2] == '-' && tmp[6] == '-') ||
yuuji@0 190 (tmp[1] == '-' && tmp[5] == '-')) &&
yuuji@0 191 (s = strchr (tmp+18,',')) && strchr (s+2,';')) ? T : NIL;
yuuji@0 192 }
yuuji@0 193 else errno = -1; /* bogus format */
yuuji@0 194 close (fd); /* close the file */
yuuji@0 195 /* \Marked status? */
yuuji@0 196 if (sbuf.st_ctime > sbuf.st_atime) {
yuuji@0 197 /* preserve atime and mtime */
yuuji@0 198 times.actime = sbuf.st_atime;
yuuji@0 199 times.modtime = sbuf.st_mtime;
yuuji@0 200 utime (file,&times); /* set the times */
yuuji@0 201 }
yuuji@0 202 }
yuuji@0 203 }
yuuji@0 204 /* in case INBOX but not mtx format */
yuuji@0 205 else if ((errno == ENOENT) && !compare_cstring (name,"INBOX")) errno = -1;
yuuji@0 206 return ret; /* return what we should */
yuuji@0 207 }
yuuji@0 208
yuuji@0 209
yuuji@0 210 /* MTX manipulate driver parameters
yuuji@0 211 * Accepts: function code
yuuji@0 212 * function-dependent value
yuuji@0 213 * Returns: function-dependent return value
yuuji@0 214 */
yuuji@0 215
yuuji@0 216 void *mtx_parameters (long function,void *value)
yuuji@0 217 {
yuuji@0 218 return NIL;
yuuji@0 219 }
yuuji@0 220
yuuji@0 221 /* MTX mail scan mailboxes
yuuji@0 222 * Accepts: mail stream
yuuji@0 223 * reference
yuuji@0 224 * pattern to search
yuuji@0 225 * string to scan
yuuji@0 226 */
yuuji@0 227
yuuji@0 228 void mtx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
yuuji@0 229 {
yuuji@0 230 if (stream) dummy_scan (NIL,ref,pat,contents);
yuuji@0 231 }
yuuji@0 232
yuuji@0 233
yuuji@0 234 /* MTX mail list mailboxes
yuuji@0 235 * Accepts: mail stream
yuuji@0 236 * reference
yuuji@0 237 * pattern to search
yuuji@0 238 */
yuuji@0 239
yuuji@0 240 void mtx_list (MAILSTREAM *stream,char *ref,char *pat)
yuuji@0 241 {
yuuji@0 242 if (stream) dummy_list (NIL,ref,pat);
yuuji@0 243 }
yuuji@0 244
yuuji@0 245
yuuji@0 246 /* MTX mail list subscribed mailboxes
yuuji@0 247 * Accepts: mail stream
yuuji@0 248 * reference
yuuji@0 249 * pattern to search
yuuji@0 250 */
yuuji@0 251
yuuji@0 252 void mtx_lsub (MAILSTREAM *stream,char *ref,char *pat)
yuuji@0 253 {
yuuji@0 254 if (stream) dummy_lsub (NIL,ref,pat);
yuuji@0 255 }
yuuji@0 256
yuuji@0 257 /* MTX mail create mailbox
yuuji@0 258 * Accepts: MAIL stream
yuuji@0 259 * mailbox name to create
yuuji@0 260 * Returns: T on success, NIL on failure
yuuji@0 261 */
yuuji@0 262
yuuji@0 263 long mtx_create (MAILSTREAM *stream,char *mailbox)
yuuji@0 264 {
yuuji@0 265 char *s,mbx[MAILTMPLEN];
yuuji@0 266 if (s = dummy_file (mbx,mailbox)) return dummy_create (stream,s);
yuuji@0 267 sprintf (mbx,"Can't create %.80s: invalid name",mailbox);
yuuji@0 268 mm_log (mbx,ERROR);
yuuji@0 269 return NIL;
yuuji@0 270 }
yuuji@0 271
yuuji@0 272
yuuji@0 273 /* MTX mail delete mailbox
yuuji@0 274 * Accepts: MAIL stream
yuuji@0 275 * mailbox name to delete
yuuji@0 276 * Returns: T on success, NIL on failure
yuuji@0 277 */
yuuji@0 278
yuuji@0 279 long mtx_delete (MAILSTREAM *stream,char *mailbox)
yuuji@0 280 {
yuuji@0 281 return mtx_rename (stream,mailbox,NIL);
yuuji@0 282 }
yuuji@0 283
yuuji@0 284 /* MTX mail rename mailbox
yuuji@0 285 * Accepts: MAIL stream
yuuji@0 286 * old mailbox name
yuuji@0 287 * new mailbox name (or NIL for delete)
yuuji@0 288 * Returns: T on success, NIL on failure
yuuji@0 289 */
yuuji@0 290
yuuji@0 291 long mtx_rename (MAILSTREAM *stream,char *old,char *newname)
yuuji@0 292 {
yuuji@0 293 long ret = LONGT;
yuuji@0 294 char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
yuuji@0 295 int fd,ld;
yuuji@0 296 struct stat sbuf;
yuuji@0 297 if (!dummy_file (file,old) ||
yuuji@0 298 (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
yuuji@0 299 ((s = strrchr (tmp,'\\')) && !s[1])))) {
yuuji@0 300 sprintf (tmp,newname ?
yuuji@0 301 "Can't rename mailbox %.80s to %.80s: invalid name" :
yuuji@0 302 "Can't delete mailbox %.80s: invalid name",
yuuji@0 303 old,newname);
yuuji@0 304 mm_log (tmp,ERROR);
yuuji@0 305 return NIL;
yuuji@0 306 }
yuuji@0 307 if ((fd = open (file,O_BINARY|O_RDWR,NIL)) < 0) {
yuuji@0 308 sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
yuuji@0 309 mm_log (tmp,ERROR);
yuuji@0 310 return NIL;
yuuji@0 311 }
yuuji@0 312 /* get exclusive parse/append permission */
yuuji@0 313 if ((ld = lockname (lock,file,LOCK_EX)) < 0) {
yuuji@0 314 mm_log ("Unable to lock rename mailbox",ERROR);
yuuji@0 315 return NIL;
yuuji@0 316 }
yuuji@0 317 /* lock out other users */
yuuji@0 318 if (flock (fd,LOCK_EX|LOCK_NB)) {
yuuji@0 319 close (fd); /* couldn't lock, give up on it then */
yuuji@0 320 sprintf (tmp,"Mailbox %.80s is in use by another process",old);
yuuji@0 321 mm_log (tmp,ERROR);
yuuji@0 322 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 323 return NIL;
yuuji@0 324 }
yuuji@0 325
yuuji@0 326 if (newname) { /* want rename? */
yuuji@0 327 /* found superior to destination name? */
yuuji@0 328 if ((s = strrchr (tmp,'\\')) && (s != tmp) &&
yuuji@0 329 ((tmp[1] != ':') || (s != tmp + 2))) {
yuuji@0 330 c = s[1]; /* remember character after delimiter */
yuuji@0 331 *s = s[1] = '\0'; /* tie off name at delimiter */
yuuji@0 332 /* name doesn't exist, create it */
yuuji@0 333 if (stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) {
yuuji@0 334 *s = '\\'; /* restore delimiter */
yuuji@0 335 if (!dummy_create (stream,tmp)) ret = NIL;
yuuji@0 336 }
yuuji@0 337 else *s = '\\'; /* restore delimiter */
yuuji@0 338 s[1] = c; /* restore character after delimiter */
yuuji@0 339 }
yuuji@0 340 flock (fd,LOCK_UN); /* release lock on the file */
yuuji@0 341 close (fd); /* pacify NTFS */
yuuji@0 342 /* rename the file */
yuuji@0 343 if (ret && rename (file,tmp)) {
yuuji@0 344 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
yuuji@0 345 strerror (errno));
yuuji@0 346 mm_log (tmp,ERROR);
yuuji@0 347 ret = NIL; /* set failure */
yuuji@0 348 }
yuuji@0 349 }
yuuji@0 350 else {
yuuji@0 351 flock (fd,LOCK_UN); /* release lock on the file */
yuuji@0 352 close (fd); /* pacify NTFS */
yuuji@0 353 if (unlink (file)) {
yuuji@0 354 sprintf (tmp,"Can't delete mailbox %.80s: %.80s",old,strerror (errno));
yuuji@0 355 mm_log (tmp,ERROR);
yuuji@0 356 ret = NIL; /* set failure */
yuuji@0 357 }
yuuji@0 358 }
yuuji@0 359 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 360 return ret; /* return success */
yuuji@0 361 }
yuuji@0 362
yuuji@0 363 /* MTX mail open
yuuji@0 364 * Accepts: stream to open
yuuji@0 365 * Returns: stream on success, NIL on failure
yuuji@0 366 */
yuuji@0 367
yuuji@0 368 MAILSTREAM *mtx_open (MAILSTREAM *stream)
yuuji@0 369 {
yuuji@0 370 int fd,ld;
yuuji@0 371 char tmp[MAILTMPLEN];
yuuji@0 372 /* return prototype for OP_PROTOTYPE call */
yuuji@0 373 if (!stream) return &mtxproto;
yuuji@0 374 if (stream->local) fatal ("mtx recycle stream");
yuuji@0 375 /* canonicalize the mailbox name */
yuuji@0 376 if (!dummy_file (tmp,stream->mailbox)) {
yuuji@0 377 sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
yuuji@0 378 mm_log (tmp,ERROR);
yuuji@0 379 }
yuuji@0 380 if (stream->rdonly ||
yuuji@0 381 (fd = open (tmp,O_BINARY|O_RDWR,NIL)) < 0) {
yuuji@0 382 if ((fd = open (tmp,O_BINARY|O_RDONLY,NIL)) < 0) {
yuuji@0 383 sprintf (tmp,"Can't open mailbox: %.80s",strerror (errno));
yuuji@0 384 mm_log (tmp,ERROR);
yuuji@0 385 return NIL;
yuuji@0 386 }
yuuji@0 387 else if (!stream->rdonly) { /* got it, but readonly */
yuuji@0 388 mm_log ("Can't get write access to mailbox, access is readonly",WARN);
yuuji@0 389 stream->rdonly = T;
yuuji@0 390 }
yuuji@0 391 }
yuuji@0 392 stream->local = fs_get (sizeof (MTXLOCAL));
yuuji@0 393 LOCAL->fd = fd; /* bind the file */
yuuji@0 394 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
yuuji@0 395 LOCAL->buflen = CHUNKSIZE - 1;
yuuji@0 396 /* note if an INBOX or not */
yuuji@0 397 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
yuuji@0 398 fs_give ((void **) &stream->mailbox);
yuuji@0 399 stream->mailbox = cpystr (tmp);
yuuji@0 400 /* get shared parse permission */
yuuji@0 401 if ((ld = lockname (tmp,stream->mailbox,LOCK_SH)) < 0) {
yuuji@0 402 mm_log ("Unable to lock open mailbox",ERROR);
yuuji@0 403 return NIL;
yuuji@0 404 }
yuuji@0 405 flock (LOCAL->fd,LOCK_SH); /* lock the file */
yuuji@0 406 unlockfd (ld,tmp); /* release shared parse permission */
yuuji@0 407 LOCAL->filesize = 0; /* initialize parsed file size */
yuuji@0 408 LOCAL->filetime = 0; /* time not set up yet */
yuuji@0 409 LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
yuuji@0 410 stream->sequence++; /* bump sequence number */
yuuji@0 411 stream->uid_validity = (unsigned long) time (0);
yuuji@0 412 /* parse mailbox */
yuuji@0 413 stream->nmsgs = stream->recent = 0;
yuuji@0 414 if (mtx_ping (stream) && !stream->nmsgs)
yuuji@0 415 mm_log ("Mailbox is empty",(long) NIL);
yuuji@0 416 if (!LOCAL) return NIL; /* failure if stream died */
yuuji@0 417 stream->perm_seen = stream->perm_deleted =
yuuji@0 418 stream->perm_flagged = stream->perm_answered = stream->perm_draft =
yuuji@0 419 stream->rdonly ? NIL : T;
yuuji@0 420 stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
yuuji@0 421 return stream; /* return stream to caller */
yuuji@0 422 }
yuuji@0 423
yuuji@0 424 /* MTX mail close
yuuji@0 425 * Accepts: MAIL stream
yuuji@0 426 * close options
yuuji@0 427 */
yuuji@0 428
yuuji@0 429 void mtx_close (MAILSTREAM *stream,long options)
yuuji@0 430 {
yuuji@0 431 if (stream && LOCAL) { /* only if a file is open */
yuuji@0 432 int silent = stream->silent;
yuuji@0 433 stream->silent = T; /* note this stream is dying */
yuuji@0 434 if (options & CL_EXPUNGE) mtx_expunge (stream,NIL,NIL);
yuuji@0 435 stream->silent = silent; /* restore previous status */
yuuji@0 436 flock (LOCAL->fd,LOCK_UN); /* unlock local file */
yuuji@0 437 close (LOCAL->fd); /* close the local file */
yuuji@0 438 /* free local text buffer */
yuuji@0 439 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
yuuji@0 440 /* nuke the local data */
yuuji@0 441 fs_give ((void **) &stream->local);
yuuji@0 442 stream->dtb = NIL; /* log out the DTB */
yuuji@0 443 }
yuuji@0 444 }
yuuji@0 445
yuuji@0 446
yuuji@0 447 /* MTX mail fetch flags
yuuji@0 448 * Accepts: MAIL stream
yuuji@0 449 * sequence
yuuji@0 450 * option flags
yuuji@0 451 * Sniffs at file to see if some other process changed the flags
yuuji@0 452 */
yuuji@0 453
yuuji@0 454 void mtx_flags (MAILSTREAM *stream,char *sequence,long flags)
yuuji@0 455 {
yuuji@0 456 unsigned long i;
yuuji@0 457 if (mtx_ping (stream) && /* ping mailbox, get new status for messages */
yuuji@0 458 ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
yuuji@0 459 mail_sequence (stream,sequence)))
yuuji@0 460 for (i = 1; i <= stream->nmsgs; i++)
yuuji@0 461 if (mail_elt (stream,i)->sequence) mtx_elt (stream,i);
yuuji@0 462 }
yuuji@0 463
yuuji@0 464 /* MTX mail fetch message header
yuuji@0 465 * Accepts: MAIL stream
yuuji@0 466 * message # to fetch
yuuji@0 467 * pointer to returned header text length
yuuji@0 468 * option flags
yuuji@0 469 * Returns: message header in RFC822 format
yuuji@0 470 */
yuuji@0 471
yuuji@0 472 char *mtx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
yuuji@0 473 long flags)
yuuji@0 474 {
yuuji@0 475 *length = 0; /* default to empty */
yuuji@0 476 if (flags & FT_UID) return "";/* UID call "impossible" */
yuuji@0 477 /* get to header position */
yuuji@0 478 lseek (LOCAL->fd,mtx_hdrpos (stream,msgno,length),L_SET);
yuuji@0 479 /* is buffer big enough? */
yuuji@0 480 if (*length > LOCAL->buflen) {
yuuji@0 481 fs_give ((void **) &LOCAL->buf);
yuuji@0 482 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = *length) + 1);
yuuji@0 483 }
yuuji@0 484 LOCAL->buf[*length] = '\0'; /* tie off string */
yuuji@0 485 /* slurp the data */
yuuji@0 486 read (LOCAL->fd,LOCAL->buf,*length);
yuuji@0 487 return LOCAL->buf;
yuuji@0 488 }
yuuji@0 489
yuuji@0 490 /* MTX mail fetch message text (body only)
yuuji@0 491 * Accepts: MAIL stream
yuuji@0 492 * message # to fetch
yuuji@0 493 * pointer to returned header text length
yuuji@0 494 * option flags
yuuji@0 495 * Returns: T, always
yuuji@0 496 */
yuuji@0 497
yuuji@0 498 long mtx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
yuuji@0 499 {
yuuji@0 500 FDDATA d;
yuuji@0 501 unsigned long i,j;
yuuji@0 502 MESSAGECACHE *elt;
yuuji@0 503 /* UID call "impossible" */
yuuji@0 504 if (flags & FT_UID) return NIL;
yuuji@0 505 elt = mtx_elt (stream,msgno); /* get message status */
yuuji@0 506 /* if message not seen */
yuuji@0 507 if (!(flags & FT_PEEK) && !elt->seen) {
yuuji@0 508 elt->seen = T; /* mark message as seen */
yuuji@0 509 /* recalculate status */
yuuji@0 510 mtx_update_status (stream,msgno,NIL);
yuuji@0 511 mm_flags (stream,msgno);
yuuji@0 512 }
yuuji@0 513 /* find header position */
yuuji@0 514 i = mtx_hdrpos (stream,msgno,&j);
yuuji@0 515 d.fd = LOCAL->fd; /* set up file descriptor */
yuuji@0 516 d.pos = i + j;
yuuji@0 517 d.chunk = LOCAL->buf; /* initial buffer chunk */
yuuji@0 518 d.chunksize = CHUNKSIZE;
yuuji@0 519 INIT (bs,fd_string,&d,elt->rfc822_size - j);
yuuji@0 520 return T; /* success */
yuuji@0 521 }
yuuji@0 522
yuuji@0 523 /* MTX mail modify flags
yuuji@0 524 * Accepts: MAIL stream
yuuji@0 525 * sequence
yuuji@0 526 * flag(s)
yuuji@0 527 * option flags
yuuji@0 528 */
yuuji@0 529
yuuji@0 530 void mtx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
yuuji@0 531 {
yuuji@0 532 struct utimbuf times;
yuuji@0 533 struct stat sbuf;
yuuji@0 534 if (!stream->rdonly) { /* make sure the update takes */
yuuji@0 535 fsync (LOCAL->fd);
yuuji@0 536 fstat (LOCAL->fd,&sbuf); /* get current write time */
yuuji@0 537 times.modtime = LOCAL->filetime = sbuf.st_mtime;
yuuji@0 538 times.actime = time (0); /* make sure read comes after all that */
yuuji@0 539 utime (stream->mailbox,&times);
yuuji@0 540 }
yuuji@0 541 }
yuuji@0 542
yuuji@0 543
yuuji@0 544 /* MTX mail per-message modify flags
yuuji@0 545 * Accepts: MAIL stream
yuuji@0 546 * message cache element
yuuji@0 547 */
yuuji@0 548
yuuji@0 549 void mtx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
yuuji@0 550 {
yuuji@0 551 struct stat sbuf;
yuuji@0 552 /* maybe need to do a checkpoint? */
yuuji@0 553 if (LOCAL->filetime && !LOCAL->shouldcheck) {
yuuji@0 554 fstat (LOCAL->fd,&sbuf); /* get current write time */
yuuji@0 555 if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
yuuji@0 556 LOCAL->filetime = 0; /* don't do this test for any other messages */
yuuji@0 557 }
yuuji@0 558 /* recalculate status */
yuuji@0 559 mtx_update_status (stream,elt->msgno,NIL);
yuuji@0 560 }
yuuji@0 561
yuuji@0 562 /* MTX mail ping mailbox
yuuji@0 563 * Accepts: MAIL stream
yuuji@0 564 * Returns: T if stream still alive, NIL if not
yuuji@0 565 */
yuuji@0 566
yuuji@0 567 long mtx_ping (MAILSTREAM *stream)
yuuji@0 568 {
yuuji@0 569 unsigned long i = 1;
yuuji@0 570 long r = T;
yuuji@0 571 int ld;
yuuji@0 572 char lock[MAILTMPLEN];
yuuji@0 573 struct stat sbuf;
yuuji@0 574 if (stream && LOCAL) { /* only if stream already open */
yuuji@0 575 fstat (LOCAL->fd,&sbuf); /* get current file poop */
yuuji@0 576 if (LOCAL->filetime && !(LOCAL->mustcheck || LOCAL->shouldcheck) &&
yuuji@0 577 (LOCAL->filetime < sbuf.st_mtime)) LOCAL->shouldcheck = T;
yuuji@0 578 /* check for changed message status */
yuuji@0 579 if (LOCAL->mustcheck || LOCAL->shouldcheck) {
yuuji@0 580 LOCAL->filetime = sbuf.st_mtime;
yuuji@0 581 if (LOCAL->shouldcheck) /* babble when we do this unilaterally */
yuuji@0 582 mm_notify (stream,"[CHECK] Checking for flag updates",NIL);
yuuji@0 583 while (i <= stream->nmsgs) mtx_elt (stream,i++);
yuuji@0 584 LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
yuuji@0 585 }
yuuji@0 586 /* get shared parse/append permission */
yuuji@0 587 if ((sbuf.st_size != LOCAL->filesize) &&
yuuji@0 588 ((ld = lockname (lock,stream->mailbox,LOCK_SH)) >= 0)) {
yuuji@0 589 /* parse resulting mailbox */
yuuji@0 590 r = (mtx_parse (stream)) ? T : NIL;
yuuji@0 591 unlockfd (ld,lock); /* release shared parse/append permission */
yuuji@0 592 }
yuuji@0 593 }
yuuji@0 594 return r; /* return result of the parse */
yuuji@0 595 }
yuuji@0 596
yuuji@0 597
yuuji@0 598 /* MTX mail check mailbox (reparses status too)
yuuji@0 599 * Accepts: MAIL stream
yuuji@0 600 */
yuuji@0 601
yuuji@0 602 void mtx_check (MAILSTREAM *stream)
yuuji@0 603 {
yuuji@0 604 /* mark that a check is desired */
yuuji@0 605 if (LOCAL) LOCAL->mustcheck = T;
yuuji@0 606 if (mtx_ping (stream)) mm_log ("Check completed",(long) NIL);
yuuji@0 607 }
yuuji@0 608
yuuji@0 609 /* MTX mail expunge mailbox
yuuji@0 610 * sequence to expunge if non-NIL
yuuji@0 611 * expunge options
yuuji@0 612 * Returns: T, always
yuuji@0 613 */
yuuji@0 614
yuuji@0 615 long mtx_expunge (MAILSTREAM *stream,char *sequence,long options)
yuuji@0 616 {
yuuji@0 617 long ret;
yuuji@0 618 struct utimbuf times;
yuuji@0 619 struct stat sbuf;
yuuji@0 620 off_t pos = 0;
yuuji@0 621 int ld;
yuuji@0 622 unsigned long i = 1;
yuuji@0 623 unsigned long j,k,m,recent;
yuuji@0 624 unsigned long n = 0;
yuuji@0 625 unsigned long delta = 0;
yuuji@0 626 char lock[MAILTMPLEN];
yuuji@0 627 MESSAGECACHE *elt;
yuuji@0 628 if (!(ret = (sequence ? ((options & EX_UID) ?
yuuji@0 629 mail_uid_sequence (stream,sequence) :
yuuji@0 630 mail_sequence (stream,sequence)) : LONGT) &&
yuuji@0 631 mtx_ping (stream))); /* parse sequence if given, ping stream */
yuuji@0 632 else if (stream->rdonly) mm_log ("Expunge ignored on readonly mailbox",WARN);
yuuji@0 633 else {
yuuji@0 634 if (LOCAL->filetime && !LOCAL->shouldcheck) {
yuuji@0 635 fstat (LOCAL->fd,&sbuf); /* get current write time */
yuuji@0 636 if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
yuuji@0 637 }
yuuji@0 638 /* get exclusive parse/append permission */
yuuji@0 639 if ((ld = lockname (lock,stream->mailbox,LOCK_EX)) < 0)
yuuji@0 640 mm_log ("Unable to lock expunge mailbox",ERROR);
yuuji@0 641 /* make sure see any newly-arrived messages */
yuuji@0 642 else if (!mtx_parse (stream));
yuuji@0 643 /* get exclusive access */
yuuji@0 644 else if (flock (LOCAL->fd,LOCK_EX|LOCK_NB)) {
yuuji@0 645 flock (LOCAL->fd,LOCK_SH);/* recover previous lock */
yuuji@0 646 mm_log ("Can't expunge because mailbox is in use by another process",
yuuji@0 647 ERROR);
yuuji@0 648 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 649 }
yuuji@0 650
yuuji@0 651 else {
yuuji@0 652 mm_critical (stream); /* go critical */
yuuji@0 653 recent = stream->recent; /* get recent now that pinged and locked */
yuuji@0 654 /* for each message */
yuuji@0 655 while (i <= stream->nmsgs) {
yuuji@0 656 /* get cache element */
yuuji@0 657 elt = mtx_elt (stream,i);
yuuji@0 658 /* number of bytes to smash or preserve */
yuuji@0 659 k = elt->private.special.text.size + elt->rfc822_size;
yuuji@0 660 /* if need to expunge this message */
yuuji@0 661 if (elt->deleted && (sequence ? elt->sequence : T)) {
yuuji@0 662 /* if recent, note one less recent message */
yuuji@0 663 if (elt->recent) --recent;
yuuji@0 664 delta += k; /* number of bytes to delete */
yuuji@0 665 /* notify upper levels */
yuuji@0 666 mail_expunged (stream,i);
yuuji@0 667 n++; /* count up one more expunged message */
yuuji@0 668 }
yuuji@0 669 else if (i++ && delta) {/* preserved message */
yuuji@0 670 /* first byte to preserve */
yuuji@0 671 j = elt->private.special.offset;
yuuji@0 672 do { /* read from source position */
yuuji@0 673 m = min (k,LOCAL->buflen);
yuuji@0 674 lseek (LOCAL->fd,j,L_SET);
yuuji@0 675 read (LOCAL->fd,LOCAL->buf,m);
yuuji@0 676 pos = j - delta; /* write to destination position */
yuuji@0 677 while (T) {
yuuji@0 678 lseek (LOCAL->fd,pos,L_SET);
yuuji@0 679 if (write (LOCAL->fd,LOCAL->buf,m) > 0) break;
yuuji@0 680 mm_notify (stream,strerror (errno),WARN);
yuuji@0 681 mm_diskerror (stream,errno,T);
yuuji@0 682 }
yuuji@0 683 pos += m; /* new position */
yuuji@0 684 j += m; /* next chunk, perhaps */
yuuji@0 685 } while (k -= m); /* until done */
yuuji@0 686 /* note the new address of this text */
yuuji@0 687 elt->private.special.offset -= delta;
yuuji@0 688 }
yuuji@0 689 /* preserved but no deleted messages */
yuuji@0 690 else pos = elt->private.special.offset + k;
yuuji@0 691 }
yuuji@0 692 if (n) { /* truncate file after last message */
yuuji@0 693 if (pos != (LOCAL->filesize -= delta)) {
yuuji@0 694 sprintf (LOCAL->buf,
yuuji@0 695 "Calculated size mismatch %lu != %lu, delta = %lu",
yuuji@0 696 (unsigned long) pos,(unsigned long) LOCAL->filesize,delta);
yuuji@0 697 mm_log (LOCAL->buf,WARN);
yuuji@0 698 LOCAL->filesize = pos;/* fix it then */
yuuji@0 699 }
yuuji@0 700 ftruncate (LOCAL->fd,LOCAL->filesize);
yuuji@0 701 sprintf (LOCAL->buf,"Expunged %lu messages",n);
yuuji@0 702 /* output the news */
yuuji@0 703 mm_log (LOCAL->buf,(long) NIL);
yuuji@0 704 }
yuuji@0 705 else mm_log ("No messages deleted, so no update needed",(long) NIL);
yuuji@0 706 fsync (LOCAL->fd); /* force disk update */
yuuji@0 707 fstat (LOCAL->fd,&sbuf); /* get new write time */
yuuji@0 708 times.modtime = LOCAL->filetime = sbuf.st_mtime;
yuuji@0 709 times.actime = time (0); /* reset atime to now */
yuuji@0 710 utime (stream->mailbox,&times);
yuuji@0 711 mm_nocritical (stream); /* release critical */
yuuji@0 712 /* notify upper level of new mailbox size */
yuuji@0 713 mail_exists (stream,stream->nmsgs);
yuuji@0 714 mail_recent (stream,recent);
yuuji@0 715 flock (LOCAL->fd,LOCK_SH);/* allow sharers again */
yuuji@0 716 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 717 }
yuuji@0 718 }
yuuji@0 719 return ret;
yuuji@0 720 }
yuuji@0 721
yuuji@0 722 /* MTX mail copy message(s)
yuuji@0 723 * Accepts: MAIL stream
yuuji@0 724 * sequence
yuuji@0 725 * destination mailbox
yuuji@0 726 * copy options
yuuji@0 727 * Returns: T if success, NIL if failed
yuuji@0 728 */
yuuji@0 729
yuuji@0 730 long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
yuuji@0 731 {
yuuji@0 732 struct stat sbuf;
yuuji@0 733 struct utimbuf times;
yuuji@0 734 MESSAGECACHE *elt;
yuuji@0 735 unsigned long i,j,k;
yuuji@0 736 long ret = LONGT;
yuuji@0 737 int fd,ld;
yuuji@0 738 char file[MAILTMPLEN],lock[MAILTMPLEN];
yuuji@0 739 mailproxycopy_t pc =
yuuji@0 740 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
yuuji@0 741 /* make sure valid mailbox */
yuuji@0 742 if (!mtx_isvalid (mailbox,file)) switch (errno) {
yuuji@0 743 case ENOENT: /* no such file? */
yuuji@0 744 mm_notify (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
yuuji@0 745 return NIL;
yuuji@0 746 case 0: /* merely empty file? */
yuuji@0 747 break;
yuuji@0 748 case EACCES: /* file protected */
yuuji@0 749 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
yuuji@0 750 MM_LOG (LOCAL->buf,ERROR);
yuuji@0 751 return NIL;
yuuji@0 752 case EINVAL:
yuuji@0 753 if (pc) return (*pc) (stream,sequence,mailbox,options);
yuuji@0 754 sprintf (LOCAL->buf,"Invalid MTX-format mailbox name: %.80s",mailbox);
yuuji@0 755 mm_log (LOCAL->buf,ERROR);
yuuji@0 756 return NIL;
yuuji@0 757 default:
yuuji@0 758 if (pc) return (*pc) (stream,sequence,mailbox,options);
yuuji@0 759 sprintf (LOCAL->buf,"Not a MTX-format mailbox: %.80s",mailbox);
yuuji@0 760 mm_log (LOCAL->buf,ERROR);
yuuji@0 761 return NIL;
yuuji@0 762 }
yuuji@0 763 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
yuuji@0 764 mail_sequence (stream,sequence))) return NIL;
yuuji@0 765 /* got file? */
yuuji@0 766 if ((fd = open (file,O_BINARY|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)) < 0) {
yuuji@0 767 sprintf (LOCAL->buf,"Unable to open copy mailbox: %.80s",strerror (errno));
yuuji@0 768 mm_log (LOCAL->buf,ERROR);
yuuji@0 769 return NIL;
yuuji@0 770 }
yuuji@0 771 mm_critical (stream); /* go critical */
yuuji@0 772 /* get exclusive parse/append permission */
yuuji@0 773 if (flock (fd,LOCK_SH) || ((ld = lockname (lock,file,LOCK_EX)) < 0)) {
yuuji@0 774 mm_log ("Unable to lock copy mailbox",ERROR);
yuuji@0 775 mm_nocritical (stream);
yuuji@0 776 return NIL;
yuuji@0 777 }
yuuji@0 778 fstat (fd,&sbuf); /* get current file size */
yuuji@0 779 lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
yuuji@0 780
yuuji@0 781 /* for each requested message */
yuuji@0 782 for (i = 1; ret && (i <= stream->nmsgs); i++)
yuuji@0 783 if ((elt = mail_elt (stream,i))->sequence) {
yuuji@0 784 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
yuuji@0 785 /* number of bytes to copy */
yuuji@0 786 k = elt->private.special.text.size + elt->rfc822_size;
yuuji@0 787 do { /* read from source position */
yuuji@0 788 j = min (k,LOCAL->buflen);
yuuji@0 789 read (LOCAL->fd,LOCAL->buf,j);
yuuji@0 790 if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
yuuji@0 791 } while (ret && (k -= j));/* until done */
yuuji@0 792 }
yuuji@0 793 /* make sure all the updates take */
yuuji@0 794 if (!(ret && (ret = !fsync (fd)))) {
yuuji@0 795 sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
yuuji@0 796 mm_log (LOCAL->buf,ERROR);
yuuji@0 797 ftruncate (fd,sbuf.st_size);
yuuji@0 798 }
yuuji@0 799 /* set atime to now-1 if successful copy */
yuuji@0 800 if (ret) times.actime = time (0) - 1;
yuuji@0 801 /* else preserved \Marked status */
yuuji@0 802 else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
yuuji@0 803 sbuf.st_atime : time (0);
yuuji@0 804 times.modtime = sbuf.st_mtime;/* preserve mtime */
yuuji@0 805 utime (file,&times); /* set the times */
yuuji@0 806 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 807 close (fd); /* close the file */
yuuji@0 808 mm_nocritical (stream); /* release critical */
yuuji@0 809 /* delete all requested messages */
yuuji@0 810 if (ret && (options & CP_MOVE)) {
yuuji@0 811 for (i = 1; i <= stream->nmsgs; i++)
yuuji@0 812 if ((elt = mtx_elt (stream,i))->sequence) {
yuuji@0 813 elt->deleted = T; /* mark message deleted */
yuuji@0 814 /* recalculate status */
yuuji@0 815 mtx_update_status (stream,i,NIL);
yuuji@0 816 }
yuuji@0 817 if (!stream->rdonly) { /* make sure the update takes */
yuuji@0 818 fsync (LOCAL->fd);
yuuji@0 819 fstat (LOCAL->fd,&sbuf); /* get current write time */
yuuji@0 820 times.modtime = LOCAL->filetime = sbuf.st_mtime;
yuuji@0 821 times.actime = time (0); /* make sure atime remains greater */
yuuji@0 822 utime (stream->mailbox,&times);
yuuji@0 823 }
yuuji@0 824 }
yuuji@0 825 if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
yuuji@0 826 mm_log ("Can not return meaningful COPYUID with this mailbox format",WARN);
yuuji@0 827 return ret;
yuuji@0 828 }
yuuji@0 829
yuuji@0 830 /* MTX mail append message from stringstruct
yuuji@0 831 * Accepts: MAIL stream
yuuji@0 832 * destination mailbox
yuuji@0 833 * append callback
yuuji@0 834 * data for callback
yuuji@0 835 * Returns: T if append successful, else NIL
yuuji@0 836 */
yuuji@0 837
yuuji@0 838 long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
yuuji@0 839 {
yuuji@0 840 struct stat sbuf;
yuuji@0 841 int fd,ld,c;
yuuji@0 842 char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
yuuji@0 843 struct utimbuf times;
yuuji@0 844 FILE *df;
yuuji@0 845 MESSAGECACHE elt;
yuuji@0 846 long f;
yuuji@0 847 unsigned long i,uf;
yuuji@0 848 STRING *message;
yuuji@0 849 long ret = LONGT;
yuuji@0 850 /* default stream to prototype */
yuuji@0 851 if (!stream) stream = &mtxproto;
yuuji@0 852 /* make sure valid mailbox */
yuuji@0 853 if (!mtx_isvalid (mailbox,file)) switch (errno) {
yuuji@0 854 case ENOENT: /* no such file? */
yuuji@0 855 if (!compare_cstring (mailbox,"INBOX")) mtx_create (NIL,"INBOX");
yuuji@0 856 else {
yuuji@0 857 mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
yuuji@0 858 return NIL;
yuuji@0 859 }
yuuji@0 860 /* falls through */
yuuji@0 861 case 0: /* merely empty file? */
yuuji@0 862 break;
yuuji@0 863 case EACCES: /* file protected */
yuuji@0 864 sprintf (tmp,"Can't access destination: %.80s",mailbox);
yuuji@0 865 MM_LOG (tmp,ERROR);
yuuji@0 866 return NIL;
yuuji@0 867 case EINVAL:
yuuji@0 868 sprintf (tmp,"Invalid MTX-format mailbox name: %.80s",mailbox);
yuuji@0 869 mm_log (tmp,ERROR);
yuuji@0 870 return NIL;
yuuji@0 871 default:
yuuji@0 872 sprintf (tmp,"Not a MTX-format mailbox: %.80s",mailbox);
yuuji@0 873 mm_log (tmp,ERROR);
yuuji@0 874 return NIL;
yuuji@0 875 }
yuuji@0 876 /* get first message */
yuuji@0 877 if (!(*af) (stream,data,&flags,&date,&message)) return NIL;
yuuji@0 878
yuuji@0 879 /* open destination mailbox */
yuuji@0 880 if (((fd = open (file,O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IREAD|S_IWRITE))
yuuji@0 881 < 0) || !(df = fdopen (fd,"ab"))) {
yuuji@0 882 sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
yuuji@0 883 mm_log (tmp,ERROR);
yuuji@0 884 return NIL;
yuuji@0 885 }
yuuji@0 886 /* get parse/append permission */
yuuji@0 887 if (flock (fd,LOCK_SH) || ((ld = lockname (lock,file,LOCK_EX)) < 0)) {
yuuji@0 888 mm_log ("Unable to lock append mailbox",ERROR);
yuuji@0 889 close (fd);
yuuji@0 890 return NIL;
yuuji@0 891 }
yuuji@0 892 mm_critical (stream); /* go critical */
yuuji@0 893 fstat (fd,&sbuf); /* get current file size */
yuuji@0 894 errno = 0;
yuuji@0 895 do { /* parse flags */
yuuji@0 896 if (!SIZE (message)) { /* guard against zero-length */
yuuji@0 897 mm_log ("Append of zero-length message",ERROR);
yuuji@0 898 ret = NIL;
yuuji@0 899 break;
yuuji@0 900 }
yuuji@0 901 f = mail_parse_flags (stream,flags,&i);
yuuji@0 902 /* reverse bits (dontcha wish we had CIRC?) */
yuuji@0 903 for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));
yuuji@0 904 if (date) { /* parse date if given */
yuuji@0 905 if (!mail_parse_date (&elt,date)) {
yuuji@0 906 sprintf (tmp,"Bad date in append: %.80s",date);
yuuji@0 907 mm_log (tmp,ERROR);
yuuji@0 908 ret = NIL; /* mark failure */
yuuji@0 909 break;
yuuji@0 910 }
yuuji@0 911 mail_date (tmp,&elt); /* write preseved date */
yuuji@0 912 }
yuuji@0 913 else internal_date (tmp); /* get current date in IMAP format */
yuuji@0 914 /* write header */
yuuji@0 915 if (fprintf (df,"%s,%lu;%010lo%02lo\015\012",tmp,i = SIZE (message),uf,
yuuji@0 916 (unsigned long) f) < 0) ret = NIL;
yuuji@0 917 else { /* write message */
yuuji@0 918 if (i) do c = 0xff & SNX (message);
yuuji@0 919 while ((putc (c,df) != EOF) && --i);
yuuji@0 920 /* get next message */
yuuji@0 921 if (i || !(*af) (stream,data,&flags,&date,&message)) ret = NIL;
yuuji@0 922 }
yuuji@0 923 } while (ret && message);
yuuji@0 924 /* if error... */
yuuji@0 925 if (!ret || (fflush (df) == EOF)) {
yuuji@0 926 ftruncate (fd,sbuf.st_size);/* revert file */
yuuji@0 927 close (fd); /* make sure fclose() doesn't corrupt us */
yuuji@0 928 if (errno) {
yuuji@0 929 sprintf (tmp,"Message append failed: %s",strerror (errno));
yuuji@0 930 mm_log (tmp,ERROR);
yuuji@0 931 }
yuuji@0 932 ret = NIL;
yuuji@0 933 }
yuuji@0 934 if (ret) times.actime = time (0) - 1;
yuuji@0 935 /* else preserved \Marked status */
yuuji@0 936 else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
yuuji@0 937 sbuf.st_atime : time (0);
yuuji@0 938 times.modtime = sbuf.st_mtime;/* preserve mtime */
yuuji@0 939 utime (file,&times); /* set the times */
yuuji@0 940 fclose (df); /* close the file */
yuuji@0 941 unlockfd (ld,lock); /* release exclusive parse/append permission */
yuuji@0 942 mm_nocritical (stream); /* release critical */
yuuji@0 943 if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))
yuuji@0 944 mm_log ("Can not return meaningful APPENDUID with this mailbox format",
yuuji@0 945 WARN);
yuuji@0 946 return ret;
yuuji@0 947 }
yuuji@0 948
yuuji@0 949 /* Internal routines */
yuuji@0 950
yuuji@0 951
yuuji@0 952 /* MTX mail parse mailbox
yuuji@0 953 * Accepts: MAIL stream
yuuji@0 954 * Returns: T if parse OK
yuuji@0 955 * NIL if failure, stream aborted
yuuji@0 956 */
yuuji@0 957
yuuji@0 958 long mtx_parse (MAILSTREAM *stream)
yuuji@0 959 {
yuuji@0 960 struct stat sbuf;
yuuji@0 961 MESSAGECACHE *elt = NIL;
yuuji@0 962 unsigned char c,*s,*t,*x;
yuuji@0 963 char tmp[MAILTMPLEN];
yuuji@0 964 unsigned long i,j;
yuuji@0 965 long curpos = LOCAL->filesize;
yuuji@0 966 long nmsgs = stream->nmsgs;
yuuji@0 967 long recent = stream->recent;
yuuji@0 968 short added = NIL;
yuuji@0 969 short silent = stream->silent;
yuuji@0 970 fstat (LOCAL->fd,&sbuf); /* get status */
yuuji@0 971 if (sbuf.st_size < curpos) { /* sanity check */
yuuji@0 972 sprintf (tmp,"Mailbox shrank from %ld to %ld!",curpos,sbuf.st_size);
yuuji@0 973 mm_log (tmp,ERROR);
yuuji@0 974 mtx_close (stream,NIL);
yuuji@0 975 return NIL;
yuuji@0 976 }
yuuji@0 977 stream->silent = T; /* don't pass up mm_exists() events yet */
yuuji@0 978 while (sbuf.st_size - curpos){/* while there is stuff to parse */
yuuji@0 979 /* get to that position in the file */
yuuji@0 980 lseek (LOCAL->fd,curpos,L_SET);
yuuji@0 981 if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
yuuji@0 982 sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
yuuji@0 983 (unsigned long) curpos,(unsigned long) sbuf.st_size,
yuuji@0 984 i ? strerror (errno) : "no data read");
yuuji@0 985 mm_log (tmp,ERROR);
yuuji@0 986 mtx_close (stream,NIL);
yuuji@0 987 return NIL;
yuuji@0 988 }
yuuji@0 989 LOCAL->buf[i] = '\0'; /* tie off buffer just in case */
yuuji@0 990 if (!((s = strchr (LOCAL->buf,'\015')) && (s[1] == '\012'))) {
yuuji@0 991 sprintf (tmp,"Unable to find CRLF at %lu in %lu bytes, text: %s",
yuuji@0 992 (unsigned long) curpos,i,(char *) LOCAL->buf);
yuuji@0 993 mm_log (tmp,ERROR);
yuuji@0 994 mtx_close (stream,NIL);
yuuji@0 995 return NIL;
yuuji@0 996 }
yuuji@0 997 *s = '\0'; /* tie off header line */
yuuji@0 998 i = (s + 2) - LOCAL->buf; /* note start of text offset */
yuuji@0 999 if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
yuuji@0 1000 sprintf (tmp,"Unable to parse internal header at %lu: %s",
yuuji@0 1001 (unsigned long) curpos,(char *) LOCAL->buf);
yuuji@0 1002 mm_log (tmp,ERROR);
yuuji@0 1003 mtx_close (stream,NIL);
yuuji@0 1004 return NIL;
yuuji@0 1005 }
yuuji@0 1006 *s++ = '\0'; *t++ = '\0'; /* tie off fields */
yuuji@0 1007
yuuji@0 1008 added = T; /* note that a new message was added */
yuuji@0 1009 /* swell the cache */
yuuji@0 1010 mail_exists (stream,++nmsgs);
yuuji@0 1011 /* instantiate an elt for this message */
yuuji@0 1012 (elt = mail_elt (stream,nmsgs))->valid = T;
yuuji@0 1013 elt->private.uid = ++stream->uid_last;
yuuji@0 1014 /* note file offset of header */
yuuji@0 1015 elt->private.special.offset = curpos;
yuuji@0 1016 /* in case error */
yuuji@0 1017 elt->private.special.text.size = 0;
yuuji@0 1018 /* header size not known yet */
yuuji@0 1019 elt->private.msg.header.text.size = 0;
yuuji@0 1020 x = s; /* parse the header components */
yuuji@0 1021 if (mail_parse_date (elt,LOCAL->buf) &&
yuuji@0 1022 (elt->rfc822_size = strtoul (s,(char **) &s,10)) && (!(s && *s)) &&
yuuji@0 1023 isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&
yuuji@0 1024 isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&
yuuji@0 1025 isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&
yuuji@0 1026 isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])
yuuji@0 1027 elt->private.special.text.size = i;
yuuji@0 1028 else { /* oops */
yuuji@0 1029 sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",
yuuji@0 1030 curpos,(char *) LOCAL->buf,(char *) x,(char *) t);
yuuji@0 1031 mm_log (tmp,ERROR);
yuuji@0 1032 mtx_close (stream,NIL);
yuuji@0 1033 return NIL;
yuuji@0 1034 }
yuuji@0 1035 /* make sure didn't run off end of file */
yuuji@0 1036 if ((curpos += (elt->rfc822_size + i)) > sbuf.st_size) {
yuuji@0 1037 sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
yuuji@0 1038 elt->private.special.offset,(unsigned long) curpos,
yuuji@0 1039 (unsigned long) sbuf.st_size);
yuuji@0 1040 mm_log (tmp,ERROR);
yuuji@0 1041 mtx_close (stream,NIL);
yuuji@0 1042 return NIL;
yuuji@0 1043 }
yuuji@0 1044 c = t[10]; /* remember first system flags byte */
yuuji@0 1045 t[10] = '\0'; /* tie off flags */
yuuji@0 1046 j = strtoul (t,NIL,8); /* get user flags value */
yuuji@0 1047 t[10] = c; /* restore first system flags byte */
yuuji@0 1048 /* set up all valid user flags (reversed!) */
yuuji@0 1049 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
yuuji@0 1050 stream->user_flags[i]) elt->user_flags |= 1 << i;
yuuji@0 1051 /* calculate system flags */
yuuji@0 1052 if ((j = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;
yuuji@0 1053 if (j & fDELETED) elt->deleted = T;
yuuji@0 1054 if (j & fFLAGGED) elt->flagged = T;
yuuji@0 1055 if (j & fANSWERED) elt->answered = T;
yuuji@0 1056 if (j & fDRAFT) elt->draft = T;
yuuji@0 1057 if (!(j & fOLD)) { /* newly arrived message? */
yuuji@0 1058 elt->recent = T;
yuuji@0 1059 recent++; /* count up a new recent message */
yuuji@0 1060 /* mark it as old */
yuuji@0 1061 mtx_update_status (stream,nmsgs,NIL);
yuuji@0 1062 }
yuuji@0 1063 }
yuuji@0 1064 fsync (LOCAL->fd); /* make sure all the fOLD flags take */
yuuji@0 1065 /* update parsed file size and time */
yuuji@0 1066 LOCAL->filesize = sbuf.st_size;
yuuji@0 1067 fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */
yuuji@0 1068 LOCAL->filetime = sbuf.st_mtime;
yuuji@0 1069 if (added && !stream->rdonly){/* make sure atime updated */
yuuji@0 1070 struct utimbuf times;
yuuji@0 1071 times.actime = time (0);
yuuji@0 1072 times.modtime = LOCAL->filetime;
yuuji@0 1073 utime (stream->mailbox,&times);
yuuji@0 1074 }
yuuji@0 1075 stream->silent = silent; /* can pass up events now */
yuuji@0 1076 mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */
yuuji@0 1077 mail_recent (stream,recent); /* and of change in recent messages */
yuuji@0 1078 return LONGT; /* return the winnage */
yuuji@0 1079 }
yuuji@0 1080
yuuji@0 1081 /* MTX get cache element with status updating from file
yuuji@0 1082 * Accepts: MAIL stream
yuuji@0 1083 * message number
yuuji@0 1084 * Returns: cache element
yuuji@0 1085 */
yuuji@0 1086
yuuji@0 1087 MESSAGECACHE *mtx_elt (MAILSTREAM *stream,unsigned long msgno)
yuuji@0 1088 {
yuuji@0 1089 MESSAGECACHE *elt = mail_elt (stream,msgno);
yuuji@0 1090 struct { /* old flags */
yuuji@0 1091 unsigned int seen : 1;
yuuji@0 1092 unsigned int deleted : 1;
yuuji@0 1093 unsigned int flagged : 1;
yuuji@0 1094 unsigned int answered : 1;
yuuji@0 1095 unsigned int draft : 1;
yuuji@0 1096 unsigned long user_flags;
yuuji@0 1097 } old;
yuuji@0 1098 old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
yuuji@0 1099 old.answered = elt->answered; old.draft = elt->draft;
yuuji@0 1100 old.user_flags = elt->user_flags;
yuuji@0 1101 mtx_read_flags (stream,elt);
yuuji@0 1102 if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
yuuji@0 1103 (old.flagged != elt->flagged) || (old.answered != elt->answered) ||
yuuji@0 1104 (old.draft != elt->draft) || (old.user_flags != elt->user_flags))
yuuji@0 1105 mm_flags (stream,msgno); /* let top level know */
yuuji@0 1106 return elt;
yuuji@0 1107 }
yuuji@0 1108
yuuji@0 1109 /* MTX read flags from file
yuuji@0 1110 * Accepts: MAIL stream
yuuji@0 1111 * Returns: cache element
yuuji@0 1112 */
yuuji@0 1113
yuuji@0 1114 void mtx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
yuuji@0 1115 {
yuuji@0 1116 unsigned long i,j;
yuuji@0 1117 /* noop if readonly and have valid flags */
yuuji@0 1118 if (stream->rdonly && elt->valid) return;
yuuji@0 1119 /* set the seek pointer */
yuuji@0 1120 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
yuuji@0 1121 elt->private.special.text.size - 14,L_SET);
yuuji@0 1122 /* read the new flags */
yuuji@0 1123 if (read (LOCAL->fd,LOCAL->buf,12) < 0) {
yuuji@0 1124 sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
yuuji@0 1125 fatal (LOCAL->buf);
yuuji@0 1126 }
yuuji@0 1127 /* calculate system flags */
yuuji@0 1128 i = (((LOCAL->buf[10]-'0') * 8) + LOCAL->buf[11]-'0');
yuuji@0 1129 elt->seen = i & fSEEN ? T : NIL; elt->deleted = i & fDELETED ? T : NIL;
yuuji@0 1130 elt->flagged = i & fFLAGGED ? T : NIL;
yuuji@0 1131 elt->answered = i & fANSWERED ? T : NIL; elt->draft = i & fDRAFT ? T : NIL;
yuuji@0 1132 LOCAL->buf[10] = '\0'; /* tie off flags */
yuuji@0 1133 j = strtoul(LOCAL->buf,NIL,8);/* get user flags value */
yuuji@0 1134 /* set up all valid user flags (reversed!) */
yuuji@0 1135 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
yuuji@0 1136 stream->user_flags[i]) elt->user_flags |= 1 << i;
yuuji@0 1137 elt->valid = T; /* have valid flags now */
yuuji@0 1138 }
yuuji@0 1139
yuuji@0 1140 /* MTX update status string
yuuji@0 1141 * Accepts: MAIL stream
yuuji@0 1142 * message number
yuuji@0 1143 * flag saying whether or not to sync
yuuji@0 1144 */
yuuji@0 1145
yuuji@0 1146 void mtx_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
yuuji@0 1147 {
yuuji@0 1148 struct utimbuf times;
yuuji@0 1149 struct stat sbuf;
yuuji@0 1150 MESSAGECACHE *elt = mail_elt (stream,msgno);
yuuji@0 1151 unsigned long j,k = 0;
yuuji@0 1152 /* readonly */
yuuji@0 1153 if (stream->rdonly || !elt->valid) mtx_read_flags (stream,elt);
yuuji@0 1154 else { /* readwrite */
yuuji@0 1155 j = elt->user_flags; /* get user flags */
yuuji@0 1156 /* reverse bits (dontcha wish we had CIRC?) */
yuuji@0 1157 while (j) k |= 1 << (29 - find_rightmost_bit (&j));
yuuji@0 1158 /* print new flag string */
yuuji@0 1159 sprintf (LOCAL->buf,"%010lo%02o",k,(unsigned)
yuuji@0 1160 (fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
yuuji@0 1161 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
yuuji@0 1162 (fDRAFT * elt->draft)));
yuuji@0 1163 while (T) { /* get to that place in the file */
yuuji@0 1164 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
yuuji@0 1165 elt->private.special.text.size - 14,L_SET);
yuuji@0 1166 /* write new flags */
yuuji@0 1167 if (write (LOCAL->fd,LOCAL->buf,12) > 0) break;
yuuji@0 1168 mm_notify (stream,strerror (errno),WARN);
yuuji@0 1169 mm_diskerror (stream,errno,T);
yuuji@0 1170 }
yuuji@0 1171 if (syncflag) { /* sync if requested */
yuuji@0 1172 fsync (LOCAL->fd);
yuuji@0 1173 fstat (LOCAL->fd,&sbuf); /* get new write time */
yuuji@0 1174 times.modtime = LOCAL->filetime = sbuf.st_mtime;
yuuji@0 1175 times.actime = time (0); /* make sure read is later */
yuuji@0 1176 utime (stream->mailbox,&times);
yuuji@0 1177 }
yuuji@0 1178 }
yuuji@0 1179 }
yuuji@0 1180
yuuji@0 1181 /* MTX locate header for a message
yuuji@0 1182 * Accepts: MAIL stream
yuuji@0 1183 * message number
yuuji@0 1184 * pointer to returned header size
yuuji@0 1185 * Returns: position of header in file
yuuji@0 1186 */
yuuji@0 1187
yuuji@0 1188 unsigned long mtx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
yuuji@0 1189 unsigned long *size)
yuuji@0 1190 {
yuuji@0 1191 unsigned long siz;
yuuji@0 1192 long i = 0;
yuuji@0 1193 int q = 0;
yuuji@0 1194 char *s,tmp[MAILTMPLEN];
yuuji@0 1195 MESSAGECACHE *elt = mtx_elt (stream,msgno);
yuuji@0 1196 unsigned long ret = elt->private.special.offset +
yuuji@0 1197 elt->private.special.text.size;
yuuji@0 1198 /* is header size known? */
yuuji@0 1199 if (!(*size = elt->private.msg.header.text.size)) {
yuuji@0 1200 lseek (LOCAL->fd,ret,L_SET);/* get to header position */
yuuji@0 1201 /* search message for CRLF CRLF */
yuuji@0 1202 for (siz = 1,s = tmp; siz <= elt->rfc822_size; siz++) {
yuuji@0 1203 /* read another buffer as necessary */
yuuji@0 1204 if ((--i <= 0) && /* buffer empty? */
yuuji@0 1205 (read (LOCAL->fd,s = tmp,
yuuji@0 1206 i = min (elt->rfc822_size - siz,(long) MAILTMPLEN)) < 0))
yuuji@0 1207 return ret; /* I/O error? */
yuuji@0 1208 switch (q) { /* sniff at buffer */
yuuji@0 1209 case 0: /* first character */
yuuji@0 1210 q = (*s++ == '\015') ? 1 : 0;
yuuji@0 1211 break;
yuuji@0 1212 case 1: /* second character */
yuuji@0 1213 q = (*s++ == '\012') ? 2 : 0;
yuuji@0 1214 break;
yuuji@0 1215 case 2: /* third character */
yuuji@0 1216 q = (*s++ == '\015') ? 3 : 0;
yuuji@0 1217 break;
yuuji@0 1218 case 3: /* fourth character */
yuuji@0 1219 if (*s++ == '\012') { /* have the sequence? */
yuuji@0 1220 /* yes, note for later */
yuuji@0 1221 elt->private.msg.header.text.size = *size = siz;
yuuji@0 1222 return ret;
yuuji@0 1223 }
yuuji@0 1224 q = 0; /* lost... */
yuuji@0 1225 break;
yuuji@0 1226 }
yuuji@0 1227 }
yuuji@0 1228 /* header consumes entire message */
yuuji@0 1229 elt->private.msg.header.text.size = *size = elt->rfc822_size;
yuuji@0 1230 }
yuuji@0 1231 return ret;
yuuji@0 1232 }

UW-IMAP'd extensions by yuuji