imapext-2007
diff src/osdep/unix/mbx.c @ 0:ada5e610ab86
imap-2007e
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 15:17:45 +0900 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/osdep/unix/mbx.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,1855 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2007 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: MBX mail routines 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * Internet: MRC@CAC.Washington.EDU 1.27 + * 1.28 + * Date: 3 October 1995 1.29 + * Last Edited: 11 October 2007 1.30 + */ 1.31 + 1.32 + 1.33 +/* FILE TIME SEMANTICS 1.34 + * 1.35 + * The atime is the last read time of the file. 1.36 + * The mtime is the last flags update time of the file. 1.37 + * The ctime is the last write time of the file. 1.38 + */ 1.39 + 1.40 +#include <stdio.h> 1.41 +#include <ctype.h> 1.42 +#include <errno.h> 1.43 +extern int errno; /* just in case */ 1.44 +#include "mail.h" 1.45 +#include "osdep.h" 1.46 +#include <pwd.h> 1.47 +#include <sys/stat.h> 1.48 +#include <sys/time.h> 1.49 +#include "misc.h" 1.50 +#include "dummy.h" 1.51 +#include "fdstring.h" 1.52 + 1.53 + 1.54 +/* Build parameters */ 1.55 + 1.56 +#define HDRSIZE 2048 1.57 + 1.58 + 1.59 +/* Kludge to make Cygwin happy */ 1.60 + 1.61 +#ifndef O_BINARY 1.62 +#define O_BINARY 0 1.63 +#endif 1.64 + 1.65 +/* MBX I/O stream local data */ 1.66 + 1.67 +typedef struct mbx_local { 1.68 + unsigned int flagcheck: 1; /* if ping should sweep for flags */ 1.69 + unsigned int expok: 1; /* if expunging OK in ping */ 1.70 + unsigned int expunged : 1; /* if one or more expunged messages */ 1.71 + int fd; /* file descriptor for I/O */ 1.72 + int ld; /* lock file descriptor */ 1.73 + int ffuserflag; /* first free user flag */ 1.74 + off_t filesize; /* file size parsed */ 1.75 + time_t filetime; /* last file time */ 1.76 + time_t lastsnarf; /* last snarf time */ 1.77 + unsigned long lastpid; /* PID of last writer */ 1.78 + unsigned char *buf; /* temporary buffer */ 1.79 + unsigned long buflen; /* current size of temporary buffer */ 1.80 + char lock[MAILTMPLEN]; /* buffer to write lock name */ 1.81 +} MBXLOCAL; 1.82 + 1.83 + 1.84 +/* Convenient access to local data */ 1.85 + 1.86 +#define LOCAL ((MBXLOCAL *) stream->local) 1.87 + 1.88 +/* Function prototypes */ 1.89 + 1.90 +DRIVER *mbx_valid (char *name); 1.91 +int mbx_isvalid (MAILSTREAM **stream,char *name,char *tmp,int *ld,char *lock, 1.92 + long flags); 1.93 +void *mbx_parameters (long function,void *value); 1.94 +void mbx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents); 1.95 +void mbx_list (MAILSTREAM *stream,char *ref,char *pat); 1.96 +void mbx_lsub (MAILSTREAM *stream,char *ref,char *pat); 1.97 +long mbx_create (MAILSTREAM *stream,char *mailbox); 1.98 +long mbx_delete (MAILSTREAM *stream,char *mailbox); 1.99 +long mbx_rename (MAILSTREAM *stream,char *old,char *newname); 1.100 +long mbx_status (MAILSTREAM *stream,char *mbx,long flags); 1.101 +MAILSTREAM *mbx_open (MAILSTREAM *stream); 1.102 +void mbx_close (MAILSTREAM *stream,long options); 1.103 +void mbx_abort (MAILSTREAM *stream); 1.104 +void mbx_flags (MAILSTREAM *stream,char *sequence,long flags); 1.105 +char *mbx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length, 1.106 + long flags); 1.107 +long mbx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags); 1.108 +void mbx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags); 1.109 +void mbx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt); 1.110 +long mbx_ping (MAILSTREAM *stream); 1.111 +void mbx_check (MAILSTREAM *stream); 1.112 +long mbx_expunge (MAILSTREAM *stream,char *sequence,long options); 1.113 +void mbx_snarf (MAILSTREAM *stream); 1.114 +long mbx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options); 1.115 +long mbx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data); 1.116 + 1.117 +char *mbx_file (char *dst,char *name); 1.118 +long mbx_parse (MAILSTREAM *stream); 1.119 +MESSAGECACHE *mbx_elt (MAILSTREAM *stream,unsigned long msgno,long expok); 1.120 +unsigned long mbx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt); 1.121 +void mbx_update_header (MAILSTREAM *stream); 1.122 +void mbx_update_status (MAILSTREAM *stream,unsigned long msgno,long flags); 1.123 +unsigned long mbx_hdrpos (MAILSTREAM *stream,unsigned long msgno, 1.124 + unsigned long *size,char **hdr); 1.125 +unsigned long mbx_rewrite (MAILSTREAM *stream,unsigned long *reclaimed, 1.126 + long flags); 1.127 +long mbx_flaglock (MAILSTREAM *stream); 1.128 + 1.129 +/* MBX mail routines */ 1.130 + 1.131 + 1.132 +/* Driver dispatch used by MAIL */ 1.133 + 1.134 +DRIVER mbxdriver = { 1.135 + "mbx", /* driver name */ 1.136 + DR_LOCAL|DR_MAIL|DR_CRLF|DR_LOCKING, 1.137 + /* driver flags */ 1.138 + (DRIVER *) NIL, /* next driver */ 1.139 + mbx_valid, /* mailbox is valid for us */ 1.140 + mbx_parameters, /* manipulate parameters */ 1.141 + mbx_scan, /* scan mailboxes */ 1.142 + mbx_list, /* list mailboxes */ 1.143 + mbx_lsub, /* list subscribed mailboxes */ 1.144 + NIL, /* subscribe to mailbox */ 1.145 + NIL, /* unsubscribe from mailbox */ 1.146 + mbx_create, /* create mailbox */ 1.147 + mbx_delete, /* delete mailbox */ 1.148 + mbx_rename, /* rename mailbox */ 1.149 + mbx_status, /* status of mailbox */ 1.150 + mbx_open, /* open mailbox */ 1.151 + mbx_close, /* close mailbox */ 1.152 + mbx_flags, /* fetch message "fast" attributes */ 1.153 + mbx_flags, /* fetch message flags */ 1.154 + NIL, /* fetch overview */ 1.155 + NIL, /* fetch message envelopes */ 1.156 + mbx_header, /* fetch message header */ 1.157 + mbx_text, /* fetch message body */ 1.158 + NIL, /* fetch partial message text */ 1.159 + NIL, /* unique identifier */ 1.160 + NIL, /* message number */ 1.161 + mbx_flag, /* modify flags */ 1.162 + mbx_flagmsg, /* per-message modify flags */ 1.163 + NIL, /* search for message based on criteria */ 1.164 + NIL, /* sort messages */ 1.165 + NIL, /* thread messages */ 1.166 + mbx_ping, /* ping mailbox to see if still alive */ 1.167 + mbx_check, /* check for new messages */ 1.168 + mbx_expunge, /* expunge deleted messages */ 1.169 + mbx_copy, /* copy messages to another mailbox */ 1.170 + mbx_append, /* append string message to mailbox */ 1.171 + NIL /* garbage collect stream */ 1.172 +}; 1.173 + 1.174 + /* prototype stream */ 1.175 +MAILSTREAM mbxproto = {&mbxdriver}; 1.176 + 1.177 +/* MBX mail validate mailbox 1.178 + * Accepts: mailbox name 1.179 + * Returns: our driver if name is valid, NIL otherwise 1.180 + */ 1.181 + 1.182 +DRIVER *mbx_valid (char *name) 1.183 +{ 1.184 + char tmp[MAILTMPLEN]; 1.185 + int fd = mbx_isvalid (NIL,name,tmp,NIL,NIL,NIL); 1.186 + if (fd < 0) return NIL; 1.187 + close (fd); /* don't need the fd now */ 1.188 + return &mbxdriver; 1.189 +} 1.190 + 1.191 + 1.192 +/* MBX mail test for valid mailbox 1.193 + * Accepts: returned stream with valid mailbox keywords 1.194 + * mailbox name 1.195 + * scratch buffer 1.196 + * returned lock fd 1.197 + * returned lock name 1.198 + * RW flags or NIL for readonly 1.199 + * Returns: file descriptor if valid, NIL otherwise 1.200 + */ 1.201 + 1.202 +#define MBXISVALIDNOUID 0x1 /* RW, don't do UID action */ 1.203 +#define MBXISVALIDUID 0x2 /* RW, do UID action */ 1.204 + 1.205 +int mbx_isvalid (MAILSTREAM **stream,char *name,char *tmp,int *ld,char *lock, 1.206 + long flags) 1.207 +{ 1.208 + int fd,upd; 1.209 + int ret = -1; 1.210 + unsigned long i; 1.211 + long j,k; 1.212 + off_t pos; 1.213 + char c,*s,*t,hdr[HDRSIZE]; 1.214 + struct stat sbuf; 1.215 + time_t tp[2]; 1.216 + int error = EINVAL; /* assume invalid argument */ 1.217 + if (ld) *ld = -1; /* initially no lock */ 1.218 + if ((s = mbx_file (tmp,name)) && !stat (s,&sbuf) && 1.219 + ((fd = open (tmp,(flags ? O_RDWR : O_RDONLY)|O_BINARY,NIL)) >= 0)) { 1.220 + error = -1; /* bogus format */ 1.221 + /* I love cretinous C compilers -- don't you? */ 1.222 + if (read (fd,hdr,HDRSIZE) == HDRSIZE) 1.223 + if ((hdr[0] == '*') && (hdr[1] == 'm') && (hdr[2] == 'b') && 1.224 + (hdr[3] == 'x') && (hdr[4] == '*') && (hdr[5] == '\015') && 1.225 + (hdr[6] == '\012') && isxdigit (hdr[7]) && isxdigit (hdr[8])) 1.226 + if (isxdigit (hdr[9]) && isxdigit (hdr[10]) && isxdigit (hdr[11]) && 1.227 + isxdigit (hdr[12]) && isxdigit (hdr[13]) && isxdigit (hdr[14]) && 1.228 + isxdigit (c = hdr[15]) && isxdigit (hdr[16])) 1.229 + if (isxdigit (hdr[17]) && isxdigit (hdr[18]) && 1.230 + isxdigit (hdr[19]) && isxdigit (hdr[20]) && 1.231 + isxdigit (hdr[21]) && isxdigit (hdr[22]) && 1.232 + (hdr[23] == '\015') && (hdr[24] == '\012')) { 1.233 + ret = fd; /* mbx format */ 1.234 + 1.235 + if (stream) { /* lock if making mini-stream */ 1.236 + if (flock (fd,LOCK_SH) || 1.237 + (flags && ((*ld = lockfd (fd,lock,LOCK_EX)) < 0))) ret = -1; 1.238 + /* reread data now that locked */ 1.239 + else if (lseek (fd,0,L_SET) || 1.240 + (read (fd,hdr,HDRSIZE) != HDRSIZE)) ret = -1; 1.241 + else { 1.242 + *stream = (MAILSTREAM *) memset (fs_get (sizeof (MAILSTREAM)), 1.243 + 0,sizeof (MAILSTREAM)); 1.244 + hdr[15] = '\0'; /* tie off UIDVALIDITY */ 1.245 + (*stream)->uid_validity = strtoul (hdr+7,NIL,16); 1.246 + hdr[15] = c; /* now get UIDLAST */ 1.247 + (*stream)->uid_last = strtoul (hdr+15,NIL,16); 1.248 + /* parse user flags */ 1.249 + for (i = 0, s = hdr + 25; 1.250 + (i < NUSERFLAGS) && (t = strchr (s,'\015')) && (t - s); 1.251 + i++, s = t + 2) { 1.252 + *t = '\0'; /* tie off flag */ 1.253 + if (strlen (s) <= MAXUSERFLAG) 1.254 + (*stream)->user_flags[i] = cpystr (s); 1.255 + } 1.256 + /* make sure have true UIDLAST */ 1.257 + if (flags & MBXISVALIDUID) { 1.258 + for (upd = NIL,pos = 2048, k = 0; pos < sbuf.st_size; 1.259 + pos += (j + k)) { 1.260 + /* read header for this message */ 1.261 + lseek (fd,pos,L_SET); 1.262 + if ((j = read (fd,hdr,64)) >= 0) { 1.263 + hdr[j] = '\0'; 1.264 + if ((s = strchr (hdr,'\015')) && (s[1] == '\012')) { 1.265 + *s = '\0'; 1.266 + k = s + 2 - hdr; 1.267 + if ((s = strchr (hdr,',')) && 1.268 + (j = strtol (s+1,&s,10)) && (*s == ';') && 1.269 + (s = strchr (s+1,'-'))) { 1.270 + /* get UID if there is any */ 1.271 + i = strtoul (++s,&t,16); 1.272 + if (!*t && (t == (s + 8)) && 1.273 + (i <= (*stream)->uid_last)) { 1.274 + if (!i) { 1.275 + lseek (fd,pos + s - hdr,L_SET); 1.276 + sprintf (hdr,"%08lx",++(*stream)->uid_last); 1.277 + write (fd,hdr,8); 1.278 + upd = T; 1.279 + } 1.280 + continue; 1.281 + } 1.282 + } 1.283 + } 1.284 + ret = -1; /* error, give up */ 1.285 + *stream = mail_close (*stream); 1.286 + pos = sbuf.st_size + 1; 1.287 + j = k = 0; 1.288 + } 1.289 + } 1.290 + 1.291 + if (upd) { /* need to update hdr with new UIDLAST? */ 1.292 + lseek (fd,15,L_SET); 1.293 + sprintf (hdr,"%08lx",(*stream)->uid_last); 1.294 + write (fd,hdr,8); 1.295 + } 1.296 + } 1.297 + } 1.298 + } 1.299 + } 1.300 + if (ret != fd) close (fd); /* close the file */ 1.301 + else lseek (fd,0,L_SET); /* else rewind to start */ 1.302 + /* \Marked status? */ 1.303 + if (sbuf.st_ctime > sbuf.st_atime) { 1.304 + tp[0] = sbuf.st_atime; /* preserve atime and mtime */ 1.305 + tp[1] = sbuf.st_mtime; 1.306 + utime (tmp,tp); /* set the times */ 1.307 + } 1.308 + } 1.309 + /* in case INBOX but not mbx format */ 1.310 + else if (((error = errno) == ENOENT) && !compare_cstring (name,"INBOX")) 1.311 + error = -1; 1.312 + if ((ret < 0) && ld && (*ld >= 0)) { 1.313 + unlockfd (*ld,lock); 1.314 + *ld = -1; 1.315 + } 1.316 + errno = error; /* return as last error */ 1.317 + return ret; /* return what we should */ 1.318 +} 1.319 + 1.320 +/* MBX manipulate driver parameters 1.321 + * Accepts: function code 1.322 + * function-dependent value 1.323 + * Returns: function-dependent return value 1.324 + */ 1.325 + 1.326 +void *mbx_parameters (long function,void *value) 1.327 +{ 1.328 + void *ret = NIL; 1.329 + switch ((int) function) { 1.330 + case GET_INBOXPATH: 1.331 + if (value) ret = mbx_file ((char *) value,"INBOX"); 1.332 + break; 1.333 + case SET_ONETIMEEXPUNGEATPING: 1.334 + if (value) ((MBXLOCAL *) ((MAILSTREAM *) value)->local)->expok = T; 1.335 + case GET_ONETIMEEXPUNGEATPING: 1.336 + if (value) ret = (void *) 1.337 + (((MBXLOCAL *) ((MAILSTREAM *) value)->local)->expok ? VOIDT : NIL); 1.338 + break; 1.339 + } 1.340 + return ret; 1.341 +} 1.342 + 1.343 + 1.344 +/* MBX mail scan mailboxes 1.345 + * Accepts: mail stream 1.346 + * reference 1.347 + * pattern to search 1.348 + * string to scan 1.349 + */ 1.350 + 1.351 +void mbx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents) 1.352 +{ 1.353 + if (stream) dummy_scan (NIL,ref,pat,contents); 1.354 +} 1.355 + 1.356 + 1.357 +/* MBX mail list mailboxes 1.358 + * Accepts: mail stream 1.359 + * reference 1.360 + * pattern to search 1.361 + */ 1.362 + 1.363 +void mbx_list (MAILSTREAM *stream,char *ref,char *pat) 1.364 +{ 1.365 + if (stream) dummy_list (NIL,ref,pat); 1.366 +} 1.367 + 1.368 + 1.369 +/* MBX mail list subscribed mailboxes 1.370 + * Accepts: mail stream 1.371 + * reference 1.372 + * pattern to search 1.373 + */ 1.374 + 1.375 +void mbx_lsub (MAILSTREAM *stream,char *ref,char *pat) 1.376 +{ 1.377 + if (stream) dummy_lsub (NIL,ref,pat); 1.378 +} 1.379 + 1.380 +/* MBX mail create mailbox 1.381 + * Accepts: MAIL stream 1.382 + * mailbox name to create 1.383 + * Returns: T on success, NIL on failure 1.384 + */ 1.385 + 1.386 +long mbx_create (MAILSTREAM *stream,char *mailbox) 1.387 +{ 1.388 + char *s,*t,mbx[MAILTMPLEN],tmp[HDRSIZE]; 1.389 + long ret = NIL; 1.390 + int i,fd; 1.391 + if (!(s = mbx_file (mbx,mailbox))) { 1.392 + sprintf (mbx,"Can't create %.80s: invalid name",mailbox); 1.393 + MM_LOG (mbx,ERROR); 1.394 + } 1.395 + /* create underlying file */ 1.396 + else if (dummy_create_path (stream,s,get_dir_protection (mailbox))) { 1.397 + /* done if made directory */ 1.398 + if ((s = strrchr (s,'/')) && !s[1]) return T; 1.399 + if ((fd = open (mbx,O_WRONLY|O_BINARY,NIL)) < 0) { 1.400 + sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno)); 1.401 + MM_LOG (tmp,ERROR); 1.402 + unlink (mbx); /* delete the file */ 1.403 + } 1.404 + else { 1.405 + memset (tmp,'\0',HDRSIZE);/* initialize header */ 1.406 + sprintf (s = tmp,"*mbx*\015\012%08lx00000000\015\012", 1.407 + (unsigned long) time (0)); 1.408 + for (i = 0; i < NUSERFLAGS; ++i) { 1.409 + t = (stream && stream->user_flags[i]) ? stream->user_flags[i] : 1.410 + ((t = default_user_flag (i)) ? t : ""); 1.411 + sprintf (s += strlen (s),"%s\015\012",t); 1.412 + } 1.413 + if (write (fd,tmp,HDRSIZE) != HDRSIZE) { 1.414 + sprintf (tmp,"Can't initialize mailbox node %.80s: %s", 1.415 + mbx,strerror (errno)); 1.416 + MM_LOG (tmp,ERROR); 1.417 + unlink (mbx); /* delete the file */ 1.418 + } 1.419 + else ret = T; /* success */ 1.420 + close (fd); /* close file */ 1.421 + } 1.422 + } 1.423 + /* set proper protections */ 1.424 + return ret ? set_mbx_protections (mailbox,mbx) : NIL; 1.425 +} 1.426 + 1.427 + 1.428 +/* MBX mail delete mailbox 1.429 + * Accepts: MAIL stream 1.430 + * mailbox name to delete 1.431 + * Returns: T on success, NIL on failure 1.432 + */ 1.433 + 1.434 +long mbx_delete (MAILSTREAM *stream,char *mailbox) 1.435 +{ 1.436 + return mbx_rename (stream,mailbox,NIL); 1.437 +} 1.438 + 1.439 +/* MBX mail rename mailbox 1.440 + * Accepts: MAIL stream 1.441 + * old mailbox name 1.442 + * new mailbox name (or NIL for delete) 1.443 + * Returns: T on success, NIL on failure 1.444 + */ 1.445 + 1.446 +long mbx_rename (MAILSTREAM *stream,char *old,char *newname) 1.447 +{ 1.448 + long ret = LONGT; 1.449 + char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN]; 1.450 + int fd,ld; 1.451 + struct stat sbuf; 1.452 + if (!mbx_file (file,old) || 1.453 + (newname && (!((s = mailboxfile (tmp,newname)) && *s) || 1.454 + ((s = strrchr (tmp,'/')) && !s[1])))) { 1.455 + sprintf (tmp,newname ? 1.456 + "Can't rename mailbox %.80s to %.80s: invalid name" : 1.457 + "Can't delete mailbox %.80s: invalid name", 1.458 + old,newname); 1.459 + MM_LOG (tmp,ERROR); 1.460 + return NIL; 1.461 + } 1.462 + else if ((fd = open (file,O_RDWR|O_BINARY,NIL)) < 0) { 1.463 + sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno)); 1.464 + MM_LOG (tmp,ERROR); 1.465 + return NIL; 1.466 + } 1.467 + /* get parse/append permission */ 1.468 + if ((ld = lockfd (fd,lock,LOCK_EX)) < 0) { 1.469 + MM_LOG ("Unable to lock rename mailbox",ERROR); 1.470 + return NIL; 1.471 + } 1.472 + /* lock out other users */ 1.473 + if (flock (fd,LOCK_EX|LOCK_NB)) { 1.474 + close (fd); /* couldn't lock, give up on it then */ 1.475 + sprintf (tmp,"Mailbox %.80s is in use by another process",old); 1.476 + MM_LOG (tmp,ERROR); 1.477 + unlockfd (ld,lock); /* release exclusive parse/append permission */ 1.478 + return NIL; 1.479 + } 1.480 + 1.481 + if (newname) { /* want rename? */ 1.482 + /* found superior to destination name? */ 1.483 + if (s = strrchr (tmp,'/')) { 1.484 + c = *++s; /* remember first character of inferior */ 1.485 + *s = '\0'; /* tie off to get just superior */ 1.486 + /* superior name doesn't exist, create it */ 1.487 + if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) && 1.488 + !dummy_create_path (stream,tmp,get_dir_protection (newname))) 1.489 + ret = NIL; 1.490 + else *s = c; /* restore full name */ 1.491 + } 1.492 + /* rename the file */ 1.493 + if (ret && rename (file,tmp)) { 1.494 + sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname, 1.495 + strerror (errno)); 1.496 + MM_LOG (tmp,ERROR); 1.497 + ret = NIL; /* set failure */ 1.498 + } 1.499 + } 1.500 + else if (unlink (file)) { 1.501 + sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno)); 1.502 + MM_LOG (tmp,ERROR); 1.503 + ret = NIL; /* set failure */ 1.504 + } 1.505 + flock (fd,LOCK_UN); /* release lock on the file */ 1.506 + unlockfd (ld,lock); /* release exclusive parse/append permission */ 1.507 + close (fd); /* close the file */ 1.508 + /* recreate file if renamed INBOX */ 1.509 + if (ret && !compare_cstring (old,"INBOX")) mbx_create (NIL,"INBOX"); 1.510 + return ret; /* return success */ 1.511 +} 1.512 + 1.513 +/* MBX Mail status 1.514 + * Accepts: mail stream 1.515 + * mailbox name 1.516 + * status flags 1.517 + * Returns: T on success, NIL on failure 1.518 + */ 1.519 + 1.520 +long mbx_status (MAILSTREAM *stream,char *mbx,long flags) 1.521 +{ 1.522 + MAILSTATUS status; 1.523 + unsigned long i; 1.524 + MAILSTREAM *tstream = NIL; 1.525 + MAILSTREAM *systream = NIL; 1.526 + /* make temporary stream (unless this mbx) */ 1.527 + if (!stream && !(stream = tstream = 1.528 + mail_open (NIL,mbx,OP_READONLY|OP_SILENT))) 1.529 + return NIL; 1.530 + status.flags = flags; /* return status values */ 1.531 + status.messages = stream->nmsgs; 1.532 + status.recent = stream->recent; 1.533 + if (flags & SA_UNSEEN) /* must search to get unseen messages */ 1.534 + for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++) 1.535 + if (!mail_elt (stream,i)->seen) status.unseen++; 1.536 + status.uidnext = stream->uid_last + 1; 1.537 + status.uidvalidity = stream->uid_validity; 1.538 + /* calculate post-snarf results */ 1.539 + if (!status.recent && stream->inbox && 1.540 + (systream = mail_open (NIL,sysinbox (),OP_READONLY|OP_SILENT))) { 1.541 + status.messages += systream->nmsgs; 1.542 + status.recent += systream->recent; 1.543 + if (flags & SA_UNSEEN) /* must search to get unseen messages */ 1.544 + for (i = 1; i <= systream->nmsgs; i++) 1.545 + if (!mail_elt (systream,i)->seen) status.unseen++; 1.546 + /* kludge but probably good enough */ 1.547 + status.uidnext += systream->nmsgs; 1.548 + } 1.549 + MM_STATUS(stream,mbx,&status);/* pass status to main program */ 1.550 + if (tstream) mail_close (tstream); 1.551 + if (systream) mail_close (systream); 1.552 + return T; /* success */ 1.553 +} 1.554 + 1.555 +/* MBX mail open 1.556 + * Accepts: stream to open 1.557 + * Returns: stream on success, NIL on failure 1.558 + */ 1.559 + 1.560 +MAILSTREAM *mbx_open (MAILSTREAM *stream) 1.561 +{ 1.562 + int fd,ld; 1.563 + short silent; 1.564 + char tmp[MAILTMPLEN]; 1.565 + blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.566 + /* return prototype for OP_PROTOTYPE call */ 1.567 + if (!stream) return user_flags (&mbxproto); 1.568 + if (stream->local) fatal ("mbx recycle stream"); 1.569 + /* canonicalize the mailbox name */ 1.570 + if (!mbx_file (tmp,stream->mailbox)) { 1.571 + sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox); 1.572 + MM_LOG (tmp,ERROR); 1.573 + } 1.574 + if (stream->rdonly || 1.575 + (fd = open (tmp,O_RDWR|O_BINARY,NIL)) < 0) { 1.576 + if ((fd = open (tmp,O_RDONLY|O_BINARY,NIL)) < 0) { 1.577 + sprintf (tmp,"Can't open mailbox: %s",strerror (errno)); 1.578 + MM_LOG (tmp,ERROR); 1.579 + return NIL; 1.580 + } 1.581 + else if (!stream->rdonly) { /* got it, but readonly */ 1.582 + MM_LOG ("Can't get write access to mailbox, access is readonly",WARN); 1.583 + stream->rdonly = T; 1.584 + } 1.585 + } 1.586 + 1.587 + stream->local = memset (fs_get (sizeof (MBXLOCAL)),NIL,sizeof (MBXLOCAL)); 1.588 + LOCAL->fd = fd; /* bind the file */ 1.589 + LOCAL->ld = -1; /* no flaglock */ 1.590 + LOCAL->buf = (char *) fs_get (CHUNKSIZE); 1.591 + LOCAL->buflen = CHUNKSIZE - 1; 1.592 + /* note if an INBOX or not */ 1.593 + stream->inbox = !compare_cstring (stream->mailbox,"INBOX"); 1.594 + fs_give ((void **) &stream->mailbox); 1.595 + stream->mailbox = cpystr (tmp); 1.596 + /* get parse/append permission */ 1.597 + if ((ld = lockfd (LOCAL->fd,tmp,LOCK_EX)) < 0) { 1.598 + MM_LOG ("Unable to lock open mailbox",ERROR); 1.599 + return NIL; 1.600 + } 1.601 + (*bn) (BLOCK_FILELOCK,NIL); 1.602 + flock (LOCAL->fd,LOCK_SH); /* lock the file */ 1.603 + (*bn) (BLOCK_NONE,NIL); 1.604 + unlockfd (ld,tmp); /* release shared parse permission */ 1.605 + LOCAL->filesize = HDRSIZE; /* initialize parsed file size */ 1.606 + /* time not set up yet */ 1.607 + LOCAL->lastsnarf = LOCAL->filetime = 0; 1.608 + LOCAL->expok = LOCAL->flagcheck = NIL; 1.609 + stream->sequence++; /* bump sequence number */ 1.610 + /* parse mailbox */ 1.611 + stream->nmsgs = stream->recent = 0; 1.612 + silent = stream->silent; /* defer events */ 1.613 + stream->silent = T; 1.614 + if (mbx_ping (stream) && !stream->nmsgs) 1.615 + MM_LOG ("Mailbox is empty",(long) NIL); 1.616 + stream->silent = silent; /* now notify upper level */ 1.617 + mail_exists (stream,stream->nmsgs); 1.618 + mail_recent (stream,stream->recent); 1.619 + if (!LOCAL) return NIL; /* failure if stream died */ 1.620 + stream->perm_seen = stream->perm_deleted = stream->perm_flagged = 1.621 + stream->perm_answered = stream->perm_draft = stream->rdonly ? NIL : T; 1.622 + stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff; 1.623 + stream->kwd_create = (stream->user_flags[NUSERFLAGS-1] || stream->rdonly) ? 1.624 + NIL : T; /* can we create new user flags? */ 1.625 + return stream; /* return stream to caller */ 1.626 +} 1.627 + 1.628 +/* MBX mail close 1.629 + * Accepts: MAIL stream 1.630 + * close options 1.631 + */ 1.632 + 1.633 +void mbx_close (MAILSTREAM *stream,long options) 1.634 +{ 1.635 + if (stream && LOCAL) { /* only if a file is open */ 1.636 + int silent = stream->silent; 1.637 + stream->silent = T; /* note this stream is dying */ 1.638 + /* do an expunge if requested */ 1.639 + if (options & CL_EXPUNGE) mbx_expunge (stream,NIL,NIL); 1.640 + else { /* otherwise do a checkpoint to purge */ 1.641 + LOCAL->expok = T; /* possible expunged messages */ 1.642 + mbx_ping (stream); 1.643 + } 1.644 + stream->silent = silent; /* restore previous status */ 1.645 + mbx_abort (stream); 1.646 + } 1.647 +} 1.648 + 1.649 + 1.650 +/* MBX mail abort stream 1.651 + * Accepts: MAIL stream 1.652 + */ 1.653 + 1.654 +void mbx_abort (MAILSTREAM *stream) 1.655 +{ 1.656 + if (stream && LOCAL) { /* only if a file is open */ 1.657 + flock (LOCAL->fd,LOCK_UN); /* unlock local file */ 1.658 + close (LOCAL->fd); /* close the local file */ 1.659 + /* free local text buffer */ 1.660 + if (LOCAL->buf) fs_give ((void **) &LOCAL->buf); 1.661 + /* nuke the local data */ 1.662 + fs_give ((void **) &stream->local); 1.663 + stream->dtb = NIL; /* log out the DTB */ 1.664 + } 1.665 +} 1.666 + 1.667 + 1.668 +/* MBX mail fetch flags 1.669 + * Accepts: MAIL stream 1.670 + * sequence 1.671 + * option flags 1.672 + * Sniffs at file to see if some other process changed the flags 1.673 + */ 1.674 + 1.675 +void mbx_flags (MAILSTREAM *stream,char *sequence,long flags) 1.676 +{ 1.677 + MESSAGECACHE *elt; 1.678 + unsigned long i; 1.679 + if (mbx_ping (stream) && /* ping mailbox, get new status for messages */ 1.680 + ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) : 1.681 + mail_sequence (stream,sequence))) 1.682 + for (i = 1; i <= stream->nmsgs; i++) 1.683 + if ((elt = mail_elt (stream,i))->sequence && !elt->valid) 1.684 + mbx_elt (stream,i,NIL); 1.685 +} 1.686 + 1.687 +/* MBX mail fetch message header 1.688 + * Accepts: MAIL stream 1.689 + * message # to fetch 1.690 + * pointer to returned header text length 1.691 + * option flags 1.692 + * Returns: message header in RFC822 format 1.693 + */ 1.694 + 1.695 +char *mbx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length, 1.696 + long flags) 1.697 +{ 1.698 + unsigned long i; 1.699 + char *s; 1.700 + *length = 0; /* default to empty */ 1.701 + if (flags & FT_UID) return "";/* UID call "impossible" */ 1.702 + /* get header position, possibly header */ 1.703 + i = mbx_hdrpos (stream,msgno,length,&s); 1.704 + if (!s) { /* mbx_hdrpos() returned header? */ 1.705 + lseek (LOCAL->fd,i,L_SET); /* no, get to header position */ 1.706 + /* is buffer big enough? */ 1.707 + if (*length > LOCAL->buflen) { 1.708 + fs_give ((void **) &LOCAL->buf); 1.709 + LOCAL->buf = (char *) fs_get ((LOCAL->buflen = *length) + 1); 1.710 + } 1.711 + /* slurp the data */ 1.712 + read (LOCAL->fd,s = LOCAL->buf,*length); 1.713 + } 1.714 + s[*length] = '\0'; /* tie off string */ 1.715 + return s; 1.716 +} 1.717 + 1.718 +/* MBX mail fetch message text (body only) 1.719 + * Accepts: MAIL stream 1.720 + * message # to fetch 1.721 + * pointer to returned header text length 1.722 + * option flags 1.723 + * Returns: T on success, NIL on failure 1.724 + */ 1.725 + 1.726 +long mbx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags) 1.727 +{ 1.728 + FDDATA d; 1.729 + unsigned long i,j; 1.730 + MESSAGECACHE *elt; 1.731 + /* UID call "impossible" */ 1.732 + if (flags & FT_UID) return NIL; 1.733 + /* get message status */ 1.734 + elt = mbx_elt (stream,msgno,NIL); 1.735 + /* if message not seen */ 1.736 + if (!(flags & FT_PEEK) && !elt->seen && mbx_flaglock (stream)) { 1.737 + elt->seen = T; /* mark message as seen */ 1.738 + /* recalculate status */ 1.739 + mbx_update_status (stream,msgno,NIL); 1.740 + MM_FLAGS (stream,msgno); 1.741 + /* update flags */ 1.742 + mbx_flag (stream,NIL,NIL,NIL); 1.743 + } 1.744 + if (!LOCAL) return NIL; /* mbx_flaglock() could have aborted */ 1.745 + /* find header position */ 1.746 + i = mbx_hdrpos (stream,msgno,&j,NIL); 1.747 + d.fd = LOCAL->fd; /* set up file descriptor */ 1.748 + d.pos = i + j; 1.749 + d.chunk = LOCAL->buf; /* initial buffer chunk */ 1.750 + d.chunksize = CHUNKSIZE; 1.751 + INIT (bs,fd_string,&d,elt->rfc822_size - j); 1.752 + return LONGT; /* success */ 1.753 +} 1.754 + 1.755 +/* MBX mail modify flags 1.756 + * Accepts: MAIL stream 1.757 + * sequence 1.758 + * flag(s) 1.759 + * option flags 1.760 + * Unlocks flag lock 1.761 + */ 1.762 + 1.763 +void mbx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags) 1.764 +{ 1.765 + time_t tp[2]; 1.766 + struct stat sbuf; 1.767 + unsigned long oldpid = LOCAL->lastpid; 1.768 + /* make sure the update takes */ 1.769 + if (!stream->rdonly && LOCAL && (LOCAL->fd >= 0) && (LOCAL->ld >= 0)) { 1.770 + fsync (LOCAL->fd); 1.771 + fstat (LOCAL->fd,&sbuf); /* get current write time */ 1.772 + tp[1] = LOCAL->filetime = sbuf.st_mtime; 1.773 + /* we are the last flag updater */ 1.774 + LOCAL->lastpid = (unsigned long) getpid (); 1.775 + /* update header if needed */ 1.776 + if (((LOCAL->ffuserflag < NUSERFLAGS) && 1.777 + stream->user_flags[LOCAL->ffuserflag]) || (oldpid != LOCAL->lastpid)) 1.778 + mbx_update_header (stream); 1.779 + tp[0] = time (0); /* make sure read comes after all that */ 1.780 + utime (stream->mailbox,tp); 1.781 + } 1.782 + if (LOCAL->ld >= 0) { /* unlock now */ 1.783 + unlockfd (LOCAL->ld,LOCAL->lock); 1.784 + LOCAL->ld = -1; 1.785 + } 1.786 +} 1.787 + 1.788 + 1.789 +/* MBX mail per-message modify flags 1.790 + * Accepts: MAIL stream 1.791 + * message cache element 1.792 + */ 1.793 + 1.794 +void mbx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt) 1.795 +{ 1.796 + if (mbx_flaglock (stream)) mbx_update_status (stream,elt->msgno,NIL); 1.797 +} 1.798 + 1.799 +/* MBX mail ping mailbox 1.800 + * Accepts: MAIL stream 1.801 + * Returns: T if stream still alive, NIL if not 1.802 + */ 1.803 + 1.804 +long mbx_ping (MAILSTREAM *stream) 1.805 +{ 1.806 + unsigned long i,pos; 1.807 + long ret = NIL; 1.808 + int ld; 1.809 + char lock[MAILTMPLEN]; 1.810 + MESSAGECACHE *elt; 1.811 + struct stat sbuf; 1.812 + if (stream && LOCAL) { /* only if stream already open */ 1.813 + int snarf = stream->inbox && !stream->rdonly; 1.814 + ret = LONGT; /* assume OK */ 1.815 + fstat (LOCAL->fd,&sbuf); /* get current file poop */ 1.816 + /* allow expunge if permitted at ping */ 1.817 + if (mail_parameters (NIL,GET_EXPUNGEATPING,NIL)) LOCAL->expok = T; 1.818 + /* if external modification */ 1.819 + if (LOCAL->filetime && (LOCAL->filetime < sbuf.st_mtime)) 1.820 + LOCAL->flagcheck = T; /* upgrade to flag checking */ 1.821 + /* new mail or flagcheck handling needed? */ 1.822 + if (((sbuf.st_size - LOCAL->filesize) || LOCAL->flagcheck || 1.823 + !stream->nmsgs || snarf) && 1.824 + ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) >= 0)) { 1.825 + /* reparse header if not flagchecking */ 1.826 + if (!LOCAL->flagcheck) ret = mbx_parse (stream); 1.827 + /* sweep mailbox for changed message status */ 1.828 + else if (ret = mbx_parse (stream)) { 1.829 + unsigned long recent = 0; 1.830 + LOCAL->filetime = sbuf.st_mtime; 1.831 + for (i = 1; i <= stream->nmsgs; ) 1.832 + if (elt = mbx_elt (stream,i,LOCAL->expok)) { 1.833 + if (elt->recent) ++recent; 1.834 + ++i; 1.835 + } 1.836 + mail_recent (stream,recent); 1.837 + LOCAL->flagcheck = NIL; /* got all the updates */ 1.838 + } 1.839 + /* always reparse header at least */ 1.840 + if (ret && snarf) { /* snarf new messages if still OK */ 1.841 + mbx_snarf (stream); 1.842 + /* parse snarfed messages */ 1.843 + ret = mbx_parse (stream); 1.844 + } 1.845 + unlockfd (ld,lock); /* release shared parse/append permission */ 1.846 + } 1.847 + if (ret) { /* must still be alive */ 1.848 + if (!LOCAL->expunged) /* look for holes if none known yet */ 1.849 + for (i = 1, pos = HDRSIZE; 1.850 + !LOCAL->expunged && (i <= stream->nmsgs); 1.851 + i++, pos += elt->private.special.text.size + elt->rfc822_size) 1.852 + if ((elt = mail_elt (stream,i))->private.special.offset != pos) 1.853 + LOCAL->expunged = T;/* found a hole */ 1.854 + /* burp any holes */ 1.855 + if (LOCAL->expunged && !stream->rdonly) { 1.856 + if (mbx_rewrite (stream,&i,NIL)) fatal ("expunge on check"); 1.857 + if (i) { /* any space reclaimed? */ 1.858 + LOCAL->expunged = NIL;/* no more pending expunge */ 1.859 + sprintf (LOCAL->buf,"Reclaimed %lu bytes of expunged space",i); 1.860 + MM_LOG (LOCAL->buf,(long) NIL); 1.861 + } 1.862 + } 1.863 + LOCAL->expok = NIL; /* no more expok */ 1.864 + } 1.865 + } 1.866 + return ret; /* return result of the parse */ 1.867 +} 1.868 + 1.869 +/* MBX mail check mailbox (reparses status too) 1.870 + * Accepts: MAIL stream 1.871 + */ 1.872 + 1.873 +void mbx_check (MAILSTREAM *stream) 1.874 +{ 1.875 + if (LOCAL) LOCAL->expok = T; /* mark that a check is desired */ 1.876 + if (mbx_ping (stream)) MM_LOG ("Check completed",(long) NIL); 1.877 +} 1.878 + 1.879 + 1.880 +/* MBX mail expunge mailbox 1.881 + * Accepts: MAIL stream 1.882 + * sequence to expunge if non-NIL 1.883 + * expunge options 1.884 + * Returns: T if success, NIL if failure 1.885 + */ 1.886 + 1.887 +long mbx_expunge (MAILSTREAM *stream,char *sequence,long options) 1.888 +{ 1.889 + long ret; 1.890 + unsigned long nexp,reclaimed; 1.891 + if (ret = sequence ? ((options & EX_UID) ? 1.892 + mail_uid_sequence (stream,sequence) : 1.893 + mail_sequence (stream,sequence)) : LONGT) { 1.894 + if (!mbx_ping (stream)); /* do nothing if stream dead */ 1.895 + else if (stream->rdonly) /* won't do on readonly files! */ 1.896 + MM_LOG ("Expunge ignored on readonly mailbox",WARN); 1.897 + /* if expunged any messages */ 1.898 + else if (nexp = mbx_rewrite (stream,&reclaimed,sequence ? -1 : 1)) { 1.899 + sprintf (LOCAL->buf,"Expunged %lu messages",nexp); 1.900 + MM_LOG (LOCAL->buf,(long) NIL); 1.901 + } 1.902 + else if (reclaimed) { /* or if any prior expunged space reclaimed */ 1.903 + sprintf (LOCAL->buf,"Reclaimed %lu bytes of expunged space",reclaimed); 1.904 + MM_LOG (LOCAL->buf,(long) NIL); 1.905 + } 1.906 + else MM_LOG ("No messages deleted, so no update needed",(long) NIL); 1.907 + } 1.908 + return ret; 1.909 +} 1.910 + 1.911 +/* MBX mail snarf messages from system inbox 1.912 + * Accepts: MAIL stream, already locked 1.913 + */ 1.914 + 1.915 +void mbx_snarf (MAILSTREAM *stream) 1.916 +{ 1.917 + unsigned long i = 0; 1.918 + unsigned long j,r,hdrlen,txtlen; 1.919 + struct stat sbuf; 1.920 + char *hdr,*txt,tmp[MAILTMPLEN]; 1.921 + MESSAGECACHE *elt; 1.922 + MAILSTREAM *sysibx = NIL; 1.923 + /* give up if can't get exclusive permission */ 1.924 + if ((time (0) >= (LOCAL->lastsnarf + 1.925 + (long) mail_parameters (NIL,GET_SNARFINTERVAL,NIL))) && 1.926 + strcmp (sysinbox (),stream->mailbox)) { 1.927 + MM_CRITICAL (stream); /* go critical */ 1.928 + /* sizes match and anything in sysinbox? */ 1.929 + if (!stat (sysinbox (),&sbuf) && sbuf.st_size && 1.930 + !fstat (LOCAL->fd,&sbuf) && (sbuf.st_size == LOCAL->filesize) && 1.931 + (sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) && 1.932 + (!sysibx->rdonly) && (r = sysibx->nmsgs)) { 1.933 + /* yes, go to end of file in our mailbox */ 1.934 + lseek (LOCAL->fd,sbuf.st_size,L_SET); 1.935 + /* for each message in sysibx mailbox */ 1.936 + while (r && (++i <= sysibx->nmsgs)) { 1.937 + /* snarf message from system INBOX */ 1.938 + hdr = cpystr (mail_fetchheader_full (sysibx,i,NIL,&hdrlen,NIL)); 1.939 + txt = mail_fetchtext_full (sysibx,i,&txtlen,FT_PEEK); 1.940 + /* if have a message */ 1.941 + if (j = hdrlen + txtlen) { 1.942 + /* build header line */ 1.943 + mail_date (LOCAL->buf,elt = mail_elt (sysibx,i)); 1.944 + sprintf (LOCAL->buf + strlen (LOCAL->buf), 1.945 + ",%lu;00000000%04x-00000000\015\012",j,(unsigned) 1.946 + ((fSEEN * elt->seen) + 1.947 + (fDELETED * elt->deleted) + (fFLAGGED * elt->flagged) + 1.948 + (fANSWERED * elt->answered) + (fDRAFT * elt->draft))); 1.949 + /* copy message */ 1.950 + if ((write (LOCAL->fd,LOCAL->buf,strlen (LOCAL->buf)) < 0) || 1.951 + (write (LOCAL->fd,hdr,hdrlen) < 0) || 1.952 + (write (LOCAL->fd,txt,txtlen) < 0)) r = 0; 1.953 + } 1.954 + fs_give ((void **) &hdr); 1.955 + } 1.956 + 1.957 + /* make sure all the updates take */ 1.958 + if (fsync (LOCAL->fd)) r = 0; 1.959 + if (r) { /* delete all the messages we copied */ 1.960 + if (r == 1) strcpy (tmp,"1"); 1.961 + else sprintf (tmp,"1:%lu",r); 1.962 + mail_setflag (sysibx,tmp,"\\Deleted"); 1.963 + mail_expunge (sysibx); /* now expunge all those messages */ 1.964 + } 1.965 + else { 1.966 + sprintf (LOCAL->buf,"Can't copy new mail: %s",strerror (errno)); 1.967 + MM_LOG (LOCAL->buf,WARN); 1.968 + ftruncate (LOCAL->fd,sbuf.st_size); 1.969 + } 1.970 + fstat (LOCAL->fd,&sbuf); /* yes, get current file size */ 1.971 + LOCAL->filetime = sbuf.st_mtime; 1.972 + } 1.973 + if (sysibx) mail_close (sysibx); 1.974 + MM_NOCRITICAL (stream); /* release critical */ 1.975 + LOCAL->lastsnarf = time (0);/* note time of last snarf */ 1.976 + } 1.977 +} 1.978 + 1.979 +/* MBX mail copy message(s) 1.980 + * Accepts: MAIL stream 1.981 + * sequence 1.982 + * destination mailbox 1.983 + * copy options 1.984 + * Returns: T if success, NIL if failed 1.985 + */ 1.986 + 1.987 +long mbx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options) 1.988 +{ 1.989 + struct stat sbuf; 1.990 + time_t tp[2]; 1.991 + MESSAGECACHE *elt; 1.992 + unsigned long i,j,k,m; 1.993 + long ret = LONGT; 1.994 + int fd,ld; 1.995 + char *s,*t,file[MAILTMPLEN],lock[MAILTMPLEN]; 1.996 + mailproxycopy_t pc = 1.997 + (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL); 1.998 + copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL); 1.999 + SEARCHSET *source = cu ? mail_newsearchset () : NIL; 1.1000 + SEARCHSET *dest = cu ? mail_newsearchset () : NIL; 1.1001 + MAILSTREAM *dstream = NIL; 1.1002 + if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) : 1.1003 + mail_sequence (stream,sequence))) return NIL; 1.1004 + /* make sure valid mailbox */ 1.1005 + if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock, 1.1006 + cu ? MBXISVALIDUID : MBXISVALIDNOUID)) < 0) 1.1007 + switch (errno) { 1.1008 + case ENOENT: /* no such file? */ 1.1009 + MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL); 1.1010 + return NIL; 1.1011 + case EACCES: /* file protected */ 1.1012 + sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox); 1.1013 + MM_LOG (LOCAL->buf,ERROR); 1.1014 + return NIL; 1.1015 + case EINVAL: 1.1016 + if (pc) return (*pc) (stream,sequence,mailbox,options); 1.1017 + sprintf (LOCAL->buf,"Invalid MBX-format mailbox name: %.80s",mailbox); 1.1018 + MM_LOG (LOCAL->buf,ERROR); 1.1019 + return NIL; 1.1020 + default: 1.1021 + if (pc) return (*pc) (stream,sequence,mailbox,options); 1.1022 + sprintf (LOCAL->buf,"Not a MBX-format mailbox: %.80s",mailbox); 1.1023 + MM_LOG (LOCAL->buf,ERROR); 1.1024 + return NIL; 1.1025 + } 1.1026 + MM_CRITICAL (stream); /* go critical */ 1.1027 + fstat (fd,&sbuf); /* get current file size */ 1.1028 + lseek (fd,sbuf.st_size,L_SET);/* move to end of file */ 1.1029 + 1.1030 + /* for each requested message */ 1.1031 + for (i = 1; ret && (i <= stream->nmsgs); i++) 1.1032 + if ((elt = mail_elt (stream,i))->sequence) { 1.1033 + lseek (LOCAL->fd,elt->private.special.offset + 1.1034 + elt->private.special.text.size,L_SET); 1.1035 + mail_date(LOCAL->buf,elt);/* build target header */ 1.1036 + /* get target keyword mask */ 1.1037 + for (j = elt->user_flags, k = 0; j; ) 1.1038 + if (s = stream->user_flags[find_rightmost_bit (&j)]) 1.1039 + for (m = 0; (m < NUSERFLAGS) && (t = dstream->user_flags[m]); m++) 1.1040 + if (!compare_cstring (s,t) && (k |= 1 << m)) break; 1.1041 + sprintf (LOCAL->buf+strlen(LOCAL->buf),",%lu;%08lx%04x-%08lx\015\012", 1.1042 + elt->rfc822_size,k,(unsigned) 1.1043 + ((fSEEN * elt->seen) + (fDELETED * elt->deleted) + 1.1044 + (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) + 1.1045 + (fDRAFT * elt->draft)),cu ? ++dstream->uid_last : 0); 1.1046 + /* write target header */ 1.1047 + if (ret = (write (fd,LOCAL->buf,strlen (LOCAL->buf)) > 0)) { 1.1048 + for (k = elt->rfc822_size; ret && (j = min (k,LOCAL->buflen)); k -= j){ 1.1049 + read (LOCAL->fd,LOCAL->buf,j); 1.1050 + ret = write (fd,LOCAL->buf,j) >= 0; 1.1051 + } 1.1052 + if (cu) { /* need to pass back new UID? */ 1.1053 + mail_append_set (source,mail_uid (stream,i)); 1.1054 + mail_append_set (dest,dstream->uid_last); 1.1055 + } 1.1056 + } 1.1057 + } 1.1058 + 1.1059 + /* make sure all the updates take */ 1.1060 + if (!(ret && (ret = !fsync (fd)))) { 1.1061 + sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno)); 1.1062 + MM_LOG (LOCAL->buf,ERROR); 1.1063 + ftruncate (fd,sbuf.st_size); 1.1064 + } 1.1065 + if (cu && ret) { /* return sets if doing COPYUID */ 1.1066 + (*cu) (stream,mailbox,dstream->uid_validity,source,dest); 1.1067 + lseek (fd,15,L_SET); /* update UIDLAST */ 1.1068 + sprintf (LOCAL->buf,"%08lx",dstream->uid_last); 1.1069 + write (fd,LOCAL->buf,8); 1.1070 + } 1.1071 + else { /* flush any sets we may have built */ 1.1072 + mail_free_searchset (&source); 1.1073 + mail_free_searchset (&dest); 1.1074 + } 1.1075 + if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */ 1.1076 + /* else preserve \Marked status */ 1.1077 + else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0); 1.1078 + tp[1] = sbuf.st_mtime; /* preserve mtime */ 1.1079 + utime (file,tp); /* set the times */ 1.1080 + close (fd); /* close the file */ 1.1081 + MM_NOCRITICAL (stream); /* release critical */ 1.1082 + unlockfd (ld,lock); /* release exclusive parse/append permission */ 1.1083 + /* delete all requested messages */ 1.1084 + if (ret && (options & CP_MOVE) && mbx_flaglock (stream)) { 1.1085 + for (i = 1; i <= stream->nmsgs; i++) if (mail_elt (stream,i)->sequence) { 1.1086 + /* mark message deleted */ 1.1087 + mbx_elt (stream,i,NIL)->deleted = T; 1.1088 + /* recalculate status */ 1.1089 + mbx_update_status (stream,i,NIL); 1.1090 + } 1.1091 + /* update flags */ 1.1092 + mbx_flag (stream,NIL,NIL,NIL); 1.1093 + } 1.1094 + if (dstream != stream) mail_close (dstream); 1.1095 + return ret; 1.1096 +} 1.1097 + 1.1098 +/* MBX mail append message from stringstruct 1.1099 + * Accepts: MAIL stream 1.1100 + * destination mailbox 1.1101 + * append callback 1.1102 + * data for callback 1.1103 + * Returns: T if append successful, else NIL 1.1104 + */ 1.1105 + 1.1106 +long mbx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data) 1.1107 +{ 1.1108 + struct stat sbuf; 1.1109 + int fd,ld; 1.1110 + char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN]; 1.1111 + time_t tp[2]; 1.1112 + FILE *df; 1.1113 + MESSAGECACHE elt; 1.1114 + long f; 1.1115 + unsigned long i,uf; 1.1116 + STRING *message; 1.1117 + long ret = NIL; 1.1118 + MAILSTREAM *dstream = NIL; 1.1119 + appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL); 1.1120 + SEARCHSET *dst = au ? mail_newsearchset () : NIL; 1.1121 + /* make sure valid mailbox */ 1.1122 + if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock, 1.1123 + au ? MBXISVALIDUID : MBXISVALIDNOUID)) < 0) 1.1124 + switch (errno) { 1.1125 + case ENOENT: /* no such file? */ 1.1126 + if (compare_cstring (mailbox,"INBOX")) { 1.1127 + MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL); 1.1128 + return NIL; 1.1129 + } 1.1130 + /* can create INBOX here */ 1.1131 + mbx_create (dstream = stream ? stream : user_flags (&mbxproto),"INBOX"); 1.1132 + if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock, 1.1133 + au ? MBXISVALIDUID : MBXISVALIDNOUID)) >= 0) 1.1134 + break; 1.1135 + case EACCES: /* file protected */ 1.1136 + sprintf (tmp,"Can't access destination: %.80s",mailbox); 1.1137 + MM_LOG (tmp,ERROR); 1.1138 + return NIL; 1.1139 + case EINVAL: 1.1140 + sprintf (tmp,"Invalid MBX-format mailbox name: %.80s",mailbox); 1.1141 + MM_LOG (tmp,ERROR); 1.1142 + return NIL; 1.1143 + default: 1.1144 + sprintf (tmp,"Not a MBX-format mailbox: %.80s",mailbox); 1.1145 + MM_LOG (tmp,ERROR); 1.1146 + return NIL; 1.1147 + } 1.1148 + 1.1149 + /* get first message */ 1.1150 + if (!MM_APPEND (af) (dstream,data,&flags,&date,&message)) close (fd); 1.1151 + else if (!(df = fdopen (fd,"r+b"))) { 1.1152 + MM_LOG ("Unable to reopen append mailbox",ERROR); 1.1153 + close (fd); 1.1154 + } 1.1155 + else { 1.1156 + MM_CRITICAL (dstream); /* go critical */ 1.1157 + fstat (fd,&sbuf); /* get current file size */ 1.1158 + fseek (df,sbuf.st_size,SEEK_SET); 1.1159 + errno = 0; 1.1160 + for (ret = LONGT; ret && message; ) { 1.1161 + if (!SIZE (message)) { /* guard against zero-length */ 1.1162 + MM_LOG ("Append of zero-length message",ERROR); 1.1163 + ret = NIL; 1.1164 + break; 1.1165 + } 1.1166 + f = mail_parse_flags (dstream,flags,&uf); 1.1167 + if (date) { /* parse date if given */ 1.1168 + if (!mail_parse_date (&elt,date)) { 1.1169 + sprintf (tmp,"Bad date in append: %.80s",date); 1.1170 + MM_LOG (tmp,ERROR); 1.1171 + ret = NIL; /* mark failure */ 1.1172 + break; 1.1173 + } 1.1174 + mail_date (tmp,&elt); /* write preseved date */ 1.1175 + } 1.1176 + else internal_date (tmp); /* get current date in IMAP format */ 1.1177 + /* write header */ 1.1178 + if (fprintf (df,"%s,%lu;%08lx%04lx-%08lx\015\012",tmp,i = SIZE (message), 1.1179 + uf,(unsigned long) f,au ? ++dstream->uid_last : 0) < 0) 1.1180 + ret = NIL; 1.1181 + else { /* write message */ 1.1182 + size_t j; 1.1183 + if (!message->cursize) SETPOS (message,GETPOS (message)); 1.1184 + while (i && (j = fwrite (message->curpos,1,message->cursize,df))) { 1.1185 + i -= j; 1.1186 + SETPOS (message,GETPOS (message) + j); 1.1187 + } 1.1188 + /* get next message */ 1.1189 + if (i || !MM_APPEND (af) (dstream,data,&flags,&date,&message)) 1.1190 + ret = NIL; 1.1191 + else if (au) mail_append_set (dst,dstream->uid_last); 1.1192 + } 1.1193 + } 1.1194 + 1.1195 + /* if error... */ 1.1196 + if (!ret || (fflush (df) == EOF)) { 1.1197 + /* revert file */ 1.1198 + ftruncate (fd,sbuf.st_size); 1.1199 + close (fd); /* make sure fclose() doesn't corrupt us */ 1.1200 + if (errno) { 1.1201 + sprintf (tmp,"Message append failed: %s",strerror (errno)); 1.1202 + MM_LOG (tmp,ERROR); 1.1203 + } 1.1204 + ret = NIL; 1.1205 + } 1.1206 + if (au && ret) { /* return sets if doing APPENDUID */ 1.1207 + (*au) (mailbox,dstream->uid_validity,dst); 1.1208 + fseek (df,15,SEEK_SET); /* update UIDLAST */ 1.1209 + fprintf (df,"%08lx",dstream->uid_last); 1.1210 + } 1.1211 + else mail_free_searchset (&dst); 1.1212 + /* set atime to now-1 if successful copy */ 1.1213 + if (ret) tp[0] = time (0) - 1; 1.1214 + /* else preserve \Marked status */ 1.1215 + else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0); 1.1216 + tp[1] = sbuf.st_mtime; /* preserve mtime */ 1.1217 + utime (file,tp); /* set the times */ 1.1218 + fclose (df); /* close the file */ 1.1219 + MM_NOCRITICAL (dstream); /* release critical */ 1.1220 + } 1.1221 + unlockfd (ld,lock); /* release exclusive parse/append permission */ 1.1222 + if (dstream != stream) mail_close (dstream); 1.1223 + return ret; 1.1224 +} 1.1225 + 1.1226 +/* Internal routines */ 1.1227 + 1.1228 + 1.1229 +/* MBX mail generate file string 1.1230 + * Accepts: temporary buffer to write into 1.1231 + * mailbox name string 1.1232 + * Returns: local file string or NIL if failure 1.1233 + */ 1.1234 + 1.1235 +char *mbx_file (char *dst,char *name) 1.1236 +{ 1.1237 + char *s = mailboxfile (dst,name); 1.1238 + return (s && !*s) ? mailboxfile (dst,"~/INBOX") : s; 1.1239 +} 1.1240 + 1.1241 +/* MBX mail parse mailbox 1.1242 + * Accepts: MAIL stream 1.1243 + * Returns: T if parse OK 1.1244 + * NIL if failure, stream aborted 1.1245 + */ 1.1246 + 1.1247 +long mbx_parse (MAILSTREAM *stream) 1.1248 +{ 1.1249 + struct stat sbuf; 1.1250 + MESSAGECACHE *elt = NIL; 1.1251 + unsigned char c,*s,*t,*x; 1.1252 + char tmp[MAILTMPLEN]; 1.1253 + unsigned long i,j,k,m; 1.1254 + off_t curpos = LOCAL->filesize; 1.1255 + unsigned long nmsgs = stream->nmsgs; 1.1256 + unsigned long recent = stream->recent; 1.1257 + unsigned long lastuid = 0; 1.1258 + short dirty = NIL; 1.1259 + short added = NIL; 1.1260 + short silent = stream->silent; 1.1261 + short uidwarn = T; 1.1262 + fstat (LOCAL->fd,&sbuf); /* get status */ 1.1263 + if (sbuf.st_size < curpos) { /* sanity check */ 1.1264 + sprintf (tmp,"Mailbox shrank from %lu to %lu!", 1.1265 + (unsigned long) curpos,(unsigned long) sbuf.st_size); 1.1266 + MM_LOG (tmp,ERROR); 1.1267 + mbx_abort (stream); 1.1268 + return NIL; 1.1269 + } 1.1270 + lseek (LOCAL->fd,0,L_SET); /* rewind file */ 1.1271 + /* read internal header */ 1.1272 + read (LOCAL->fd,LOCAL->buf,HDRSIZE); 1.1273 + LOCAL->buf[HDRSIZE] = '\0'; /* tie off header */ 1.1274 + c = LOCAL->buf[15]; /* save first character of last UID */ 1.1275 + LOCAL->buf[15] = '\0'; 1.1276 + /* parse UID validity */ 1.1277 + stream->uid_validity = strtoul (LOCAL->buf + 7,NIL,16); 1.1278 + LOCAL->buf[15] = c; /* restore first character of last UID */ 1.1279 + /* parse last UID */ 1.1280 + i = strtoul (LOCAL->buf + 15,NIL,16); 1.1281 + stream->uid_last = stream->rdonly ? max (i,stream->uid_last) : i; 1.1282 + /* parse user flags */ 1.1283 + for (i = 0, s = LOCAL->buf + 25; 1.1284 + (i < NUSERFLAGS) && (t = strchr (s,'\015')) && (t - s); 1.1285 + i++, s = t + 2) { 1.1286 + *t = '\0'; /* tie off flag */ 1.1287 + if (!stream->user_flags[i] && (strlen (s) <= MAXUSERFLAG)) 1.1288 + stream->user_flags[i] = cpystr (s); 1.1289 + } 1.1290 + LOCAL->ffuserflag = (int) i; /* first free user flag */ 1.1291 + 1.1292 + /* get current last flag updater PID */ 1.1293 + i = (isxdigit (LOCAL->buf[HDRSIZE-10]) && isxdigit (LOCAL->buf[HDRSIZE-9]) && 1.1294 + isxdigit (LOCAL->buf[HDRSIZE-8]) && isxdigit (LOCAL->buf[HDRSIZE-7]) && 1.1295 + isxdigit (LOCAL->buf[HDRSIZE-6]) && isxdigit (LOCAL->buf[HDRSIZE-5]) && 1.1296 + isxdigit (LOCAL->buf[HDRSIZE-4]) && isxdigit (LOCAL->buf[HDRSIZE-3]) && 1.1297 + (LOCAL->buf[HDRSIZE-2] == '\015') && (LOCAL->buf[HDRSIZE-1] == '\012'))? 1.1298 + strtoul (LOCAL->buf + HDRSIZE - 8,NIL,16) : 0; 1.1299 + /* set flagcheck if lastpid changed */ 1.1300 + if (LOCAL->lastpid && (LOCAL->lastpid != i)) LOCAL->flagcheck = T; 1.1301 + LOCAL->lastpid = i; /* set as last PID */ 1.1302 + stream->silent = T; /* don't pass up exists events yet */ 1.1303 + while (sbuf.st_size - curpos){/* while there is stuff to parse */ 1.1304 + /* get to that position in the file */ 1.1305 + lseek (LOCAL->fd,curpos,L_SET); 1.1306 + if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) { 1.1307 + sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s", 1.1308 + (unsigned long) curpos,(unsigned long) sbuf.st_size, 1.1309 + i ? strerror (errno) : "no data read"); 1.1310 + MM_LOG (tmp,ERROR); 1.1311 + mbx_abort (stream); 1.1312 + return NIL; 1.1313 + } 1.1314 + LOCAL->buf[i] = '\0'; /* tie off buffer just in case */ 1.1315 + if (!((s = strchr (LOCAL->buf,'\015')) && (s[1] == '\012'))) { 1.1316 + sprintf (tmp,"Unable to find CRLF at %lu in %lu bytes, text: %.80s", 1.1317 + (unsigned long) curpos,i,(char *) LOCAL->buf); 1.1318 + MM_LOG (tmp,ERROR); 1.1319 + mbx_abort (stream); 1.1320 + return NIL; 1.1321 + } 1.1322 + *s = '\0'; /* tie off header line */ 1.1323 + i = (s + 2) - LOCAL->buf; /* note start of text offset */ 1.1324 + if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) { 1.1325 + sprintf (tmp,"Unable to parse internal header at %lu: %.80s", 1.1326 + (unsigned long) curpos,(char *) LOCAL->buf); 1.1327 + MM_LOG (tmp,ERROR); 1.1328 + mbx_abort (stream); 1.1329 + return NIL; 1.1330 + } 1.1331 + if (!(isxdigit (t[1]) && isxdigit (t[2]) && isxdigit (t[3]) && 1.1332 + isxdigit (t[4]) && isxdigit (t[5]) && isxdigit (t[6]) && 1.1333 + isxdigit (t[7]) && isxdigit (t[8]) && isxdigit (t[9]) && 1.1334 + isxdigit (t[10]) && isxdigit (t[11]) && isxdigit (t[12]))) { 1.1335 + sprintf (tmp,"Unable to parse message flags at %lu: %.80s", 1.1336 + (unsigned long) curpos,(char *) LOCAL->buf); 1.1337 + MM_LOG (tmp,ERROR); 1.1338 + mbx_abort (stream); 1.1339 + return NIL; 1.1340 + } 1.1341 + if ((t[13] != '-') || t[22] || 1.1342 + !(isxdigit (t[14]) && isxdigit (t[15]) && isxdigit (t[16]) && 1.1343 + isxdigit (t[17]) && isxdigit (t[18]) && isxdigit (t[19]) && 1.1344 + isxdigit (t[20]) && isxdigit (t[21]))) { 1.1345 + sprintf (tmp,"Unable to parse message UID at %lu: %.80s", 1.1346 + (unsigned long) curpos,(char *) LOCAL->buf); 1.1347 + MM_LOG (tmp,ERROR); 1.1348 + mbx_abort (stream); 1.1349 + return NIL; 1.1350 + } 1.1351 + 1.1352 + *s++ = '\0'; *t++ = '\0'; /* break up fields */ 1.1353 + /* get message size */ 1.1354 + if (!(j = strtoul (s,(char **) &x,10)) && (!(x && *x))) { 1.1355 + sprintf (tmp,"Unable to parse message size at %lu: %.80s,%.80s;%.80s", 1.1356 + (unsigned long) curpos,(char *) LOCAL->buf,(char *) s, 1.1357 + (char *) t); 1.1358 + MM_LOG (tmp,ERROR); 1.1359 + mbx_abort (stream); 1.1360 + return NIL; 1.1361 + } 1.1362 + /* make sure didn't run off end of file */ 1.1363 + if (((off_t) (curpos + i + j)) > sbuf.st_size) { 1.1364 + sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)", 1.1365 + (unsigned long) curpos,(unsigned long) (curpos + i + j), 1.1366 + (unsigned long) sbuf.st_size); 1.1367 + MM_LOG (tmp,ERROR); 1.1368 + mbx_abort (stream); 1.1369 + return NIL; 1.1370 + } 1.1371 + /* parse UID */ 1.1372 + if ((m = strtoul (t+13,NIL,16)) && 1.1373 + ((m <= lastuid) || (m > stream->uid_last))) { 1.1374 + if (uidwarn) { 1.1375 + sprintf (tmp,"Invalid UID %08lx in message %lu, rebuilding UIDs", 1.1376 + m,nmsgs+1); 1.1377 + MM_LOG (tmp,WARN); 1.1378 + uidwarn = NIL; 1.1379 + /* restart UID validity */ 1.1380 + stream->uid_validity = time (0); 1.1381 + } 1.1382 + m = 0; /* lose this UID */ 1.1383 + dirty = T; /* mark dirty, set new lastuid */ 1.1384 + stream->uid_last = lastuid; 1.1385 + } 1.1386 + 1.1387 + t[12] = '\0'; /* parse system flags */ 1.1388 + if ((k = strtoul (t+8,NIL,16)) & fEXPUNGED) { 1.1389 + if (m) lastuid = m; /* expunge message, update last UID seen */ 1.1390 + else { /* no UID assigned? */ 1.1391 + lastuid = ++stream->uid_last; 1.1392 + dirty = T; 1.1393 + } 1.1394 + } 1.1395 + else { /* not expunged, swell the cache */ 1.1396 + added = T; /* note that a new message was added */ 1.1397 + mail_exists (stream,++nmsgs); 1.1398 + /* instantiate an elt for this message */ 1.1399 + (elt = mail_elt (stream,nmsgs))->valid = T; 1.1400 + /* parse the date */ 1.1401 + if (!mail_parse_date (elt,LOCAL->buf)) { 1.1402 + sprintf (tmp,"Unable to parse message date at %lu: %.80s", 1.1403 + (unsigned long) curpos,(char *) LOCAL->buf); 1.1404 + MM_LOG (tmp,ERROR); 1.1405 + mbx_abort (stream); 1.1406 + return NIL; 1.1407 + } 1.1408 + /* note file offset of header */ 1.1409 + elt->private.special.offset = curpos; 1.1410 + /* and internal header size */ 1.1411 + elt->private.special.text.size = i; 1.1412 + /* header size not known yet */ 1.1413 + elt->private.msg.header.text.size = 0; 1.1414 + elt->rfc822_size = j; /* note message size */ 1.1415 + /* calculate system flags */ 1.1416 + if (k & fSEEN) elt->seen = T; 1.1417 + if (k & fDELETED) elt->deleted = T; 1.1418 + if (k & fFLAGGED) elt->flagged = T; 1.1419 + if (k & fANSWERED) elt->answered = T; 1.1420 + if (k & fDRAFT) elt->draft = T; 1.1421 + t[8] = '\0'; /* get user flags value */ 1.1422 + elt->user_flags = strtoul (t,NIL,16); 1.1423 + /* UID already assigned? */ 1.1424 + if (!(elt->private.uid = m) || !(k & fOLD)) { 1.1425 + elt->recent = T; /* no, mark as recent */ 1.1426 + ++recent; /* count up a new recent message */ 1.1427 + dirty = T; /* and must rewrite header */ 1.1428 + /* assign new UID */ 1.1429 + if (!elt->private.uid) elt->private.uid = ++stream->uid_last; 1.1430 + mbx_update_status (stream,elt->msgno,NIL); 1.1431 + } 1.1432 + /* update last parsed UID */ 1.1433 + lastuid = elt->private.uid; 1.1434 + } 1.1435 + curpos += i + j; /* update position */ 1.1436 + } 1.1437 + 1.1438 + if (dirty && !stream->rdonly){/* update header */ 1.1439 + mbx_update_header (stream); 1.1440 + fsync (LOCAL->fd); /* make sure all the UID updates take */ 1.1441 + } 1.1442 + /* update parsed file size and time */ 1.1443 + LOCAL->filesize = sbuf.st_size; 1.1444 + fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */ 1.1445 + LOCAL->filetime = sbuf.st_mtime; 1.1446 + if (added && !stream->rdonly){/* make sure atime updated */ 1.1447 + time_t tp[2]; 1.1448 + tp[0] = time (0); 1.1449 + tp[1] = LOCAL->filetime; 1.1450 + utime (stream->mailbox,tp); 1.1451 + } 1.1452 + stream->silent = silent; /* can pass up events now */ 1.1453 + mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */ 1.1454 + mail_recent (stream,recent); /* and of change in recent messages */ 1.1455 + return LONGT; /* return the winnage */ 1.1456 +} 1.1457 + 1.1458 +/* MBX get cache element with status updating from file 1.1459 + * Accepts: MAIL stream 1.1460 + * message number 1.1461 + * expunge OK flag 1.1462 + * Returns: cache element 1.1463 + */ 1.1464 + 1.1465 +MESSAGECACHE *mbx_elt (MAILSTREAM *stream,unsigned long msgno,long expok) 1.1466 +{ 1.1467 + MESSAGECACHE *elt = mail_elt (stream,msgno); 1.1468 + struct { /* old flags */ 1.1469 + unsigned int seen : 1; 1.1470 + unsigned int deleted : 1; 1.1471 + unsigned int flagged : 1; 1.1472 + unsigned int answered : 1; 1.1473 + unsigned int draft : 1; 1.1474 + unsigned long user_flags; 1.1475 + } old; 1.1476 + old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged; 1.1477 + old.answered = elt->answered; old.draft = elt->draft; 1.1478 + old.user_flags = elt->user_flags; 1.1479 + /* get new flags */ 1.1480 + if (mbx_read_flags (stream,elt) && expok) { 1.1481 + mail_expunged (stream,elt->msgno); 1.1482 + return NIL; /* return this message was expunged */ 1.1483 + } 1.1484 + if ((old.seen != elt->seen) || (old.deleted != elt->deleted) || 1.1485 + (old.flagged != elt->flagged) || (old.answered != elt->answered) || 1.1486 + (old.draft != elt->draft) || (old.user_flags != elt->user_flags)) 1.1487 + MM_FLAGS (stream,msgno); /* let top level know */ 1.1488 + return elt; 1.1489 +} 1.1490 + 1.1491 +/* MBX read flags from file 1.1492 + * Accepts: MAIL stream 1.1493 + * cache element 1.1494 + * Returns: non-NIL if message expunged 1.1495 + */ 1.1496 + 1.1497 +unsigned long mbx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt) 1.1498 +{ 1.1499 + unsigned long i; 1.1500 + struct stat sbuf; 1.1501 + fstat (LOCAL->fd,&sbuf); /* get status */ 1.1502 + /* make sure file size is good */ 1.1503 + if (sbuf.st_size < LOCAL->filesize) { 1.1504 + sprintf (LOCAL->buf,"Mailbox shrank from %lu to %lu in flag read!", 1.1505 + (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size); 1.1506 + fatal (LOCAL->buf); 1.1507 + } 1.1508 + /* set the seek pointer */ 1.1509 + lseek (LOCAL->fd,(off_t) elt->private.special.offset + 1.1510 + elt->private.special.text.size - 24,L_SET); 1.1511 + /* read the new flags */ 1.1512 + if (read (LOCAL->fd,LOCAL->buf,14) < 0) { 1.1513 + sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno)); 1.1514 + fatal (LOCAL->buf); 1.1515 + } 1.1516 + if ((LOCAL->buf[0] != ';') || (LOCAL->buf[13] != '-')) { 1.1517 + LOCAL->buf[14] = '\0'; /* tie off buffer for error message */ 1.1518 + sprintf (LOCAL->buf+50,"Invalid flags for message %lu (%lu %lu): %s", 1.1519 + elt->msgno,elt->private.special.offset, 1.1520 + elt->private.special.text.size,(char *) LOCAL->buf); 1.1521 + fatal (LOCAL->buf+50); 1.1522 + } 1.1523 + LOCAL->buf[13] = '\0'; /* tie off buffer */ 1.1524 + /* calculate system flags */ 1.1525 + i = strtoul (LOCAL->buf+9,NIL,16); 1.1526 + elt->seen = i & fSEEN ? T : NIL; 1.1527 + elt->deleted = i & fDELETED ? T : NIL; 1.1528 + elt->flagged = i & fFLAGGED ? T : NIL; 1.1529 + elt->answered = i & fANSWERED ? T : NIL; 1.1530 + elt->draft = i & fDRAFT ? T : NIL; 1.1531 + LOCAL->expunged |= i & fEXPUNGED ? T : NIL; 1.1532 + LOCAL->buf[9] = '\0'; /* tie off flags */ 1.1533 + /* get user flags value */ 1.1534 + elt->user_flags = strtoul (LOCAL->buf+1,NIL,16); 1.1535 + elt->valid = T; /* have valid flags now */ 1.1536 + return i & fEXPUNGED; 1.1537 +} 1.1538 + 1.1539 +/* MBX update header 1.1540 + * Accepts: MAIL stream 1.1541 + */ 1.1542 + 1.1543 +#ifndef CYGKLUDGEOFFSET 1.1544 +#define CYGKLUDGEOFFSET 0 1.1545 +#endif 1.1546 + 1.1547 +void mbx_update_header (MAILSTREAM *stream) 1.1548 +{ 1.1549 + int i; 1.1550 + char *s = LOCAL->buf; 1.1551 + memset (s,'\0',HDRSIZE); /* initialize header */ 1.1552 + sprintf (s,"*mbx*\015\012%08lx%08lx\015\012", 1.1553 + stream->uid_validity,stream->uid_last); 1.1554 + for (i = 0; (i < NUSERFLAGS) && stream->user_flags[i]; ++i) 1.1555 + sprintf (s += strlen (s),"%s\015\012",stream->user_flags[i]); 1.1556 + LOCAL->ffuserflag = i; /* first free user flag */ 1.1557 + /* can we create more user flags? */ 1.1558 + stream->kwd_create = (i < NUSERFLAGS) ? T : NIL; 1.1559 + /* write reserved lines */ 1.1560 + while (i++ < NUSERFLAGS) strcat (s,"\015\012"); 1.1561 + sprintf (LOCAL->buf + HDRSIZE - 10,"%08lx\015\012",LOCAL->lastpid); 1.1562 + while (T) { /* rewind file */ 1.1563 + lseek (LOCAL->fd,CYGKLUDGEOFFSET,L_SET); 1.1564 + /* write new header */ 1.1565 + if (write (LOCAL->fd,LOCAL->buf + CYGKLUDGEOFFSET, 1.1566 + HDRSIZE - CYGKLUDGEOFFSET) > 0) break; 1.1567 + MM_NOTIFY (stream,strerror (errno),WARN); 1.1568 + MM_DISKERROR (stream,errno,T); 1.1569 + } 1.1570 +} 1.1571 + 1.1572 +/* MBX update status string 1.1573 + * Accepts: MAIL stream 1.1574 + * message number 1.1575 + * flags 1.1576 + */ 1.1577 + 1.1578 +void mbx_update_status (MAILSTREAM *stream,unsigned long msgno,long flags) 1.1579 +{ 1.1580 + struct stat sbuf; 1.1581 + MESSAGECACHE *elt = mail_elt (stream,msgno); 1.1582 + /* readonly */ 1.1583 + if (stream->rdonly || !elt->valid) mbx_read_flags (stream,elt); 1.1584 + else { /* readwrite */ 1.1585 + fstat (LOCAL->fd,&sbuf); /* get status */ 1.1586 + /* make sure file size is good */ 1.1587 + if (sbuf.st_size < LOCAL->filesize) { 1.1588 + sprintf (LOCAL->buf,"Mailbox shrank from %lu to %lu in flag update!", 1.1589 + (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size); 1.1590 + fatal (LOCAL->buf); 1.1591 + } 1.1592 + /* set the seek pointer */ 1.1593 + lseek (LOCAL->fd,(off_t) elt->private.special.offset + 1.1594 + elt->private.special.text.size - 24,L_SET); 1.1595 + /* read the new flags */ 1.1596 + if (read (LOCAL->fd,LOCAL->buf,14) < 0) { 1.1597 + sprintf (LOCAL->buf,"Unable to read old status: %s",strerror (errno)); 1.1598 + fatal (LOCAL->buf); 1.1599 + } 1.1600 + if ((LOCAL->buf[0] != ';') || (LOCAL->buf[13] != '-')) { 1.1601 + LOCAL->buf[14] = '\0'; /* tie off buffer for error message */ 1.1602 + sprintf (LOCAL->buf+50,"Invalid flags for message %lu (%lu %lu): %s", 1.1603 + elt->msgno,elt->private.special.offset, 1.1604 + elt->private.special.text.size,(char *) LOCAL->buf); 1.1605 + fatal (LOCAL->buf+50); 1.1606 + } 1.1607 + /* print new flag string */ 1.1608 + sprintf (LOCAL->buf,"%08lx%04x-%08lx",elt->user_flags,(unsigned) 1.1609 + (((elt->deleted && flags) ? 1.1610 + fEXPUNGED : (strtoul (LOCAL->buf+9,NIL,16)) & fEXPUNGED) + 1.1611 + (fSEEN * elt->seen) + (fDELETED * elt->deleted) + 1.1612 + (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) + 1.1613 + (fDRAFT * elt->draft) + fOLD),elt->private.uid); 1.1614 + while (T) { /* get to that place in the file */ 1.1615 + lseek (LOCAL->fd,(off_t) elt->private.special.offset + 1.1616 + elt->private.special.text.size - 23,L_SET); 1.1617 + /* write new flags and UID */ 1.1618 + if (write (LOCAL->fd,LOCAL->buf,21) > 0) break; 1.1619 + MM_NOTIFY (stream,strerror (errno),WARN); 1.1620 + MM_DISKERROR (stream,errno,T); 1.1621 + } 1.1622 + } 1.1623 +} 1.1624 + 1.1625 +/* MBX locate header for a message 1.1626 + * Accepts: MAIL stream 1.1627 + * message number 1.1628 + * pointer to returned header size 1.1629 + * pointer to possible returned header 1.1630 + * Returns: position of header in file 1.1631 + */ 1.1632 + 1.1633 +#define HDRBUFLEN 16384 /* good enough for most headers */ 1.1634 +#define SLOP 4 /* CR LF CR LF */ 1.1635 + 1.1636 +unsigned long mbx_hdrpos (MAILSTREAM *stream,unsigned long msgno, 1.1637 + unsigned long *size,char **hdr) 1.1638 +{ 1.1639 + unsigned long siz,done; 1.1640 + long i; 1.1641 + unsigned char *s,*t,*te; 1.1642 + MESSAGECACHE *elt = mail_elt (stream,msgno); 1.1643 + unsigned long ret = elt->private.special.offset + 1.1644 + elt->private.special.text.size; 1.1645 + if (hdr) *hdr = NIL; /* assume no header returned */ 1.1646 + /* is header size known? */ 1.1647 + if (*size = elt->private.msg.header.text.size) return ret; 1.1648 + /* paranoia check */ 1.1649 + if (LOCAL->buflen < (HDRBUFLEN + SLOP)) 1.1650 + fatal ("LOCAL->buf smaller than HDRBUFLEN"); 1.1651 + lseek (LOCAL->fd,ret,L_SET); /* get to header position */ 1.1652 + /* read HDRBUFLEN chunks with 4 byte slop */ 1.1653 + for (done = siz = 0, s = LOCAL->buf; 1.1654 + (i = min ((long) (elt->rfc822_size - done),(long) HDRBUFLEN)) && 1.1655 + (read (LOCAL->fd,s,i) == i); 1.1656 + done += i, siz += (t - LOCAL->buf) - SLOP, s = LOCAL->buf + SLOP) { 1.1657 + te = (t = s + i) - 12; /* calculate end of fast scan */ 1.1658 + /* fast scan for CR */ 1.1659 + for (s = LOCAL->buf; s < te;) 1.1660 + if (((*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015') || 1.1661 + (*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015') || 1.1662 + (*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015') || 1.1663 + (*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015')) && 1.1664 + (*s == '\012') && (*++s == '\015') && (*++s == '\012')) { 1.1665 + *size = elt->private.msg.header.text.size = siz + (++s - LOCAL->buf); 1.1666 + if (hdr) *hdr = LOCAL->buf; 1.1667 + return ret; 1.1668 + } 1.1669 + for (te = t - 3; (s < te);) /* final character-at-a-time scan */ 1.1670 + if ((*s++ == '\015') && (*s == '\012') && (*++s == '\015') && 1.1671 + (*++s == '\012')) { 1.1672 + *size = elt->private.msg.header.text.size = siz + (++s - LOCAL->buf); 1.1673 + if (hdr) *hdr = LOCAL->buf; 1.1674 + return ret; 1.1675 + } 1.1676 + if (i <= SLOP) break; /* end of data */ 1.1677 + /* slide over last 4 bytes */ 1.1678 + memmove (LOCAL->buf,t - SLOP,SLOP); 1.1679 + hdr = NIL; /* can't return header this way */ 1.1680 + } 1.1681 + /* not found: header consumes entire message */ 1.1682 + elt->private.msg.header.text.size = *size = elt->rfc822_size; 1.1683 + if (hdr) *hdr = LOCAL->buf; /* possibly return header too */ 1.1684 + return ret; 1.1685 +} 1.1686 + 1.1687 +/* MBX mail rewrite mailbox 1.1688 + * Accepts: MAIL stream 1.1689 + * pointer to return reclaimed size 1.1690 + * flags (0 = no expunge, 1 = expunge deleted, -1 = expunge sequence) 1.1691 + * Returns: number of expunged messages 1.1692 + */ 1.1693 + 1.1694 +unsigned long mbx_rewrite (MAILSTREAM *stream,unsigned long *reclaimed, 1.1695 + long flags) 1.1696 +{ 1.1697 + time_t tp[2]; 1.1698 + struct stat sbuf; 1.1699 + off_t pos,ppos; 1.1700 + int ld; 1.1701 + unsigned long i,j,k,m,delta; 1.1702 + unsigned long n = *reclaimed = 0; 1.1703 + unsigned long recent = 0; 1.1704 + char lock[MAILTMPLEN]; 1.1705 + MESSAGECACHE *elt; 1.1706 + blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.1707 + /* The cretins who designed flock() created a window of vulnerability in 1.1708 + * upgrading locks from shared to exclusive or downgrading from exclusive 1.1709 + * to shared. Rather than maintain the lock at shared status at a minimum, 1.1710 + * flock() actually *releases* the former lock. Obviously they never talked 1.1711 + * to any database guys. Fortunately, we have the parse/append permission 1.1712 + * lock. If we require this lock before going exclusive on the mailbox, 1.1713 + * another process can not sneak in and steal the exclusive mailbox lock on 1.1714 + * us, because it will block on trying to get parse/append permission first. 1.1715 + */ 1.1716 + /* get parse/append permission */ 1.1717 + if ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) < 0) { 1.1718 + MM_LOG ("Unable to lock mailbox for rewrite",ERROR); 1.1719 + return 0; 1.1720 + } 1.1721 + fstat (LOCAL->fd,&sbuf); /* get current write time */ 1.1722 + if (LOCAL->filetime && !LOCAL->flagcheck && 1.1723 + (LOCAL->filetime < sbuf.st_mtime)) LOCAL->flagcheck = T; 1.1724 + if (!mbx_parse (stream)) { /* make sure see any newly-arrived messages */ 1.1725 + unlockfd (ld,lock); /* failed?? */ 1.1726 + return 0; 1.1727 + } 1.1728 + if (LOCAL->flagcheck) { /* sweep flags if need flagcheck */ 1.1729 + LOCAL->filetime = sbuf.st_mtime; 1.1730 + for (i = 1; i <= stream->nmsgs; ++i) mbx_elt (stream,i,NIL); 1.1731 + LOCAL->flagcheck = NIL; 1.1732 + } 1.1733 + 1.1734 + /* get exclusive access */ 1.1735 + if (!flock (LOCAL->fd,LOCK_EX|LOCK_NB)) { 1.1736 + MM_CRITICAL (stream); /* go critical */ 1.1737 + for (i = 1,delta = 0,pos = ppos = HDRSIZE; i <= stream->nmsgs; ) { 1.1738 + /* note if message not at predicted location */ 1.1739 + if (m = (elt = mbx_elt (stream,i,NIL))->private.special.offset - ppos) { 1.1740 + ppos = elt->private.special.offset; 1.1741 + *reclaimed += m; /* note reclaimed message space */ 1.1742 + delta += m; /* and as expunge delta */ 1.1743 + } 1.1744 + /* number of bytes to smash or preserve */ 1.1745 + ppos += (k = elt->private.special.text.size + elt->rfc822_size); 1.1746 + /* if need to expunge this message*/ 1.1747 + if (flags && elt->deleted && ((flags > 0) || elt->sequence)) { 1.1748 + delta += k; /* number of bytes to delete */ 1.1749 + mail_expunged(stream,i);/* notify upper levels */ 1.1750 + n++; /* count up one more expunged message */ 1.1751 + } 1.1752 + else { /* preserved message */ 1.1753 + i++; /* count this message */ 1.1754 + if (elt->recent) ++recent; 1.1755 + if (delta) { /* moved, note first byte to preserve */ 1.1756 + j = elt->private.special.offset; 1.1757 + do { /* read from source position */ 1.1758 + m = min (k,LOCAL->buflen); 1.1759 + lseek (LOCAL->fd,j,L_SET); 1.1760 + read (LOCAL->fd,LOCAL->buf,m); 1.1761 + pos = j - delta; /* write to destination position */ 1.1762 + while (T) { 1.1763 + lseek (LOCAL->fd,pos,L_SET); 1.1764 + if (write (LOCAL->fd,LOCAL->buf,m) > 0) break; 1.1765 + MM_NOTIFY (stream,strerror (errno),WARN); 1.1766 + MM_DISKERROR (stream,errno,T); 1.1767 + } 1.1768 + pos += m; /* new position */ 1.1769 + j += m; /* next chunk, perhaps */ 1.1770 + } while (k -= m); /* until done */ 1.1771 + /* note the new address of this text */ 1.1772 + elt->private.special.offset -= delta; 1.1773 + } 1.1774 + /* preserved but no deleted messages yet */ 1.1775 + else pos = elt->private.special.offset + k; 1.1776 + } 1.1777 + } 1.1778 + /* deltaed file size match position? */ 1.1779 + if (m = (LOCAL->filesize -= delta) - pos) { 1.1780 + *reclaimed += m; /* probably an fEXPUNGED msg */ 1.1781 + LOCAL->filesize = pos; /* set correct size */ 1.1782 + } 1.1783 + /* truncate file after last message */ 1.1784 + ftruncate (LOCAL->fd,LOCAL->filesize); 1.1785 + fsync (LOCAL->fd); /* force disk update */ 1.1786 + MM_NOCRITICAL (stream); /* release critical */ 1.1787 + (*bn) (BLOCK_FILELOCK,NIL); 1.1788 + flock (LOCAL->fd,LOCK_SH); /* allow sharers again */ 1.1789 + (*bn) (BLOCK_NONE,NIL); 1.1790 + } 1.1791 + 1.1792 + else { /* can't get exclusive */ 1.1793 + (*bn) (BLOCK_FILELOCK,NIL); 1.1794 + flock (LOCAL->fd,LOCK_SH); /* recover previous shared mailbox lock */ 1.1795 + (*bn) (BLOCK_NONE,NIL); 1.1796 + /* do hide-expunge when shared */ 1.1797 + if (flags) for (i = 1; i <= stream->nmsgs; ) { 1.1798 + if (elt = mbx_elt (stream,i,T)) { 1.1799 + /* make the message invisible */ 1.1800 + if (elt->deleted && ((flags > 0) || elt->sequence)) { 1.1801 + mbx_update_status (stream,elt->msgno,LONGT); 1.1802 + /* notify upper levels */ 1.1803 + mail_expunged (stream,i); 1.1804 + n++; /* count up one more expunged message */ 1.1805 + } 1.1806 + else { 1.1807 + i++; /* preserved message */ 1.1808 + if (elt->recent) ++recent; 1.1809 + } 1.1810 + } 1.1811 + else n++; /* count up one more expunged message */ 1.1812 + } 1.1813 + fsync (LOCAL->fd); /* force disk update */ 1.1814 + } 1.1815 + fstat (LOCAL->fd,&sbuf); /* get new write time */ 1.1816 + tp[1] = LOCAL->filetime = sbuf.st_mtime; 1.1817 + tp[0] = time (0); /* reset atime to now */ 1.1818 + utime (stream->mailbox,tp); 1.1819 + unlockfd (ld,lock); /* release exclusive parse/append permission */ 1.1820 + /* notify upper level of new mailbox size */ 1.1821 + mail_exists (stream,stream->nmsgs); 1.1822 + mail_recent (stream,recent); 1.1823 + return n; /* return number of expunged messages */ 1.1824 +} 1.1825 + 1.1826 +/* MBX mail lock for flag updating 1.1827 + * Accepts: stream 1.1828 + * Returns: T if successful, NIL if failure 1.1829 + */ 1.1830 + 1.1831 +long mbx_flaglock (MAILSTREAM *stream) 1.1832 +{ 1.1833 + struct stat sbuf; 1.1834 + unsigned long i; 1.1835 + int ld; 1.1836 + char lock[MAILTMPLEN]; 1.1837 + /* no-op if readonly or already locked */ 1.1838 + if (!stream->rdonly && LOCAL && (LOCAL->fd >= 0) && (LOCAL->ld < 0)) { 1.1839 + /* lock now */ 1.1840 + if ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) < 0) return NIL; 1.1841 + if (!LOCAL->flagcheck) { /* don't do this if flagcheck already needed */ 1.1842 + if (LOCAL->filetime) { /* know previous time? */ 1.1843 + fstat (LOCAL->fd,&sbuf);/* get current write time */ 1.1844 + if (LOCAL->filetime < sbuf.st_mtime) LOCAL->flagcheck = T; 1.1845 + LOCAL->filetime = 0; /* don't do this test for any other messages */ 1.1846 + } 1.1847 + if (!mbx_parse (stream)) {/* parse mailbox */ 1.1848 + unlockfd (ld,lock); /* shouldn't happen */ 1.1849 + return NIL; 1.1850 + } 1.1851 + if (LOCAL->flagcheck) /* invalidate cache if flagcheck */ 1.1852 + for (i = 1; i <= stream->nmsgs; ++i) mail_elt (stream,i)->valid = NIL; 1.1853 + } 1.1854 + LOCAL->ld = ld; /* copy to stream for subsequent calls */ 1.1855 + memcpy (LOCAL->lock,lock,MAILTMPLEN); 1.1856 + } 1.1857 + return LONGT; 1.1858 +}