1
/* ========================================================================
2
* Copyright 1988-2006 University of Washington
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
11
* ========================================================================
15
* Program: Tenex mail routines
17
* Author: Mark Crispin
18
* Networks and Distributed Computing
19
* Computing & Communications
20
* University of Washington
21
* Administration Building, AG-44
23
* Internet: MRC@CAC.Washington.EDU
26
* Last Edited: 30 August 2006
30
/* FILE TIME SEMANTICS
32
* The atime is the last read time of the file.
33
* The mtime is the last flags update time of the file.
34
* The ctime is the last write time of the file.
38
* Most of the text sizes are in internal (LF-only) form, except for the
39
* msg.text size. Beware.
45
extern int errno; /* just in case */
51
#include <sys/utime.h>
55
/* TENEX I/O stream local data */
57
typedef struct tenex_local {
58
unsigned int shouldcheck: 1; /* if ping should do a check instead */
59
unsigned int mustcheck: 1; /* if ping must do a check instead */
60
int fd; /* file descriptor for I/O */
61
off_t filesize; /* file size parsed */
62
time_t filetime; /* last file time */
63
time_t lastsnarf; /* local snarf time */
64
unsigned char *buf; /* temporary buffer */
65
unsigned long buflen; /* current size of temporary buffer */
66
unsigned long uid; /* current text uid */
67
SIZEDTEXT text; /* current text */
71
/* Convenient access to local data */
73
#define LOCAL ((TENEXLOCAL *) stream->local)
76
/* Function prototypes */
78
DRIVER *tenex_valid (char *name);
79
int tenex_isvalid (char *name,char *file);
80
void *tenex_parameters (long function,void *value);
81
void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
82
void tenex_list (MAILSTREAM *stream,char *ref,char *pat);
83
void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat);
84
long tenex_create (MAILSTREAM *stream,char *mailbox);
85
long tenex_delete (MAILSTREAM *stream,char *mailbox);
86
long tenex_rename (MAILSTREAM *stream,char *old,char *newname);
87
long tenex_status (MAILSTREAM *stream,char *mbx,long flags);
88
MAILSTREAM *tenex_open (MAILSTREAM *stream);
89
void tenex_close (MAILSTREAM *stream,long options);
90
void tenex_fast (MAILSTREAM *stream,char *sequence,long flags);
91
void tenex_flags (MAILSTREAM *stream,char *sequence,long flags);
92
char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
93
unsigned long *length,long flags);
94
long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
95
void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
96
void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
97
long tenex_ping (MAILSTREAM *stream);
98
void tenex_check (MAILSTREAM *stream);
99
void tenex_snarf (MAILSTREAM *stream);
100
long tenex_expunge (MAILSTREAM *stream,char *sequence,long options);
101
long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
102
long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
104
unsigned long tenex_size (MAILSTREAM *stream,unsigned long m);
105
long tenex_parse (MAILSTREAM *stream);
106
MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno);
107
void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt);
108
void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,
110
unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
111
unsigned long *size);
114
/* Tenex mail routines */
117
/* Driver dispatch used by MAIL */
119
DRIVER tenexdriver = {
120
"tenex", /* driver name */
121
DR_LOCAL|DR_MAIL, /* driver flags */
122
(DRIVER *) NIL, /* next driver */
123
tenex_valid, /* mailbox is valid for us */
124
tenex_parameters, /* manipulate parameters */
125
tenex_scan, /* scan mailboxes */
126
tenex_list, /* list mailboxes */
127
tenex_lsub, /* list subscribed mailboxes */
128
NIL, /* subscribe to mailbox */
129
NIL, /* unsubscribe from mailbox */
130
tenex_create, /* create mailbox */
131
tenex_delete, /* delete mailbox */
132
tenex_rename, /* rename mailbox */
133
mail_status_default, /* status of mailbox */
134
tenex_open, /* open mailbox */
135
tenex_close, /* close mailbox */
136
tenex_flags, /* fetch message "fast" attributes */
137
tenex_flags, /* fetch message flags */
138
NIL, /* fetch overview */
139
NIL, /* fetch message envelopes */
140
tenex_header, /* fetch message header */
141
tenex_text, /* fetch message body */
142
NIL, /* fetch partial message text */
143
NIL, /* unique identifier */
144
NIL, /* message number */
145
tenex_flag, /* modify flags */
146
tenex_flagmsg, /* per-message modify flags */
147
NIL, /* search for message based on criteria */
148
NIL, /* sort messages */
149
NIL, /* thread messages */
150
tenex_ping, /* ping mailbox to see if still alive */
151
tenex_check, /* check for new messages */
152
tenex_expunge, /* expunge deleted messages */
153
tenex_copy, /* copy messages to another mailbox */
154
tenex_append, /* append string message to mailbox */
155
NIL /* garbage collect stream */
158
/* prototype stream */
159
MAILSTREAM tenexproto = {&tenexdriver};
161
/* Tenex mail validate mailbox
162
* Accepts: mailbox name
163
* Returns: our driver if name is valid, NIL otherwise
166
DRIVER *tenex_valid (char *name)
168
char tmp[MAILTMPLEN];
169
return tenex_isvalid (name,tmp) ? &tenexdriver : NIL;
173
/* Tenex mail test for valid mailbox
174
* Accepts: mailbox name
175
* buffer to return file name
176
* Returns: T if valid, NIL otherwise
179
int tenex_isvalid (char *name,char *file)
183
char *s,tmp[MAILTMPLEN];
185
struct utimbuf times;
186
errno = EINVAL; /* assume invalid argument */
187
/* if file, get its status */
188
if ((s = dummy_file (file,name)) && !stat (s,&sbuf) &&
189
((sbuf.st_mode & S_IFMT) == S_IFREG)) {
190
if (!sbuf.st_size)errno = 0;/* empty file */
191
else if ((fd = open (file,O_BINARY|O_RDONLY,NIL)) >= 0) {
192
memset (tmp,'\0',MAILTMPLEN);
193
if ((read (fd,tmp,64) >= 0) && (s = strchr (tmp,'\012')) &&
194
(s[-1] != '\015')) { /* valid format? */
195
*s = '\0'; /* tie off header */
196
/* must begin with dd-mmm-yy" */
197
ret = (((tmp[2] == '-' && tmp[6] == '-') ||
198
(tmp[1] == '-' && tmp[5] == '-')) &&
199
(s = strchr (tmp+18,',')) && strchr (s+2,';')) ? T : NIL;
201
else errno = -1; /* bogus format */
202
close (fd); /* close the file */
203
/* \Marked status? */
204
if (sbuf.st_ctime > sbuf.st_atime) {
205
/* preserve atime and mtime */
206
times.actime = sbuf.st_atime;
207
times.modtime = sbuf.st_mtime;
208
utime (file,×); /* set the times */
212
/* in case INBOX but not tenex format */
213
else if ((errno == ENOENT) && !compare_cstring (name,"INBOX")) errno = -1;
214
return ret; /* return what we should */
218
/* Tenex manipulate driver parameters
219
* Accepts: function code
220
* function-dependent value
221
* Returns: function-dependent return value
224
void *tenex_parameters (long function,void *value)
229
/* Tenex mail scan mailboxes
230
* Accepts: mail stream
236
void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
238
if (stream) dummy_scan (NIL,ref,pat,contents);
242
/* Tenex mail list mailboxes
243
* Accepts: mail stream
248
void tenex_list (MAILSTREAM *stream,char *ref,char *pat)
250
if (stream) dummy_list (NIL,ref,pat);
254
/* Tenex mail list subscribed mailboxes
255
* Accepts: mail stream
260
void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat)
262
if (stream) dummy_lsub (NIL,ref,pat);
265
/* Tenex mail create mailbox
266
* Accepts: MAIL stream
267
* mailbox name to create
268
* Returns: T on success, NIL on failure
271
long tenex_create (MAILSTREAM *stream,char *mailbox)
273
char *s,mbx[MAILTMPLEN];
274
if (s = dummy_file (mbx,mailbox)) return dummy_create (stream,s);
275
sprintf (mbx,"Can't create %.80s: invalid name",mailbox);
281
/* Tenex mail delete mailbox
282
* Accepts: MAIL stream
283
* mailbox name to delete
284
* Returns: T on success, NIL on failure
287
long tenex_delete (MAILSTREAM *stream,char *mailbox)
289
return tenex_rename (stream,mailbox,NIL);
292
/* Tenex mail rename mailbox
293
* Accepts: MAIL stream
295
* new mailbox name (or NIL for delete)
296
* Returns: T on success, NIL on failure
299
long tenex_rename (MAILSTREAM *stream,char *old,char *newname)
302
char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
305
if (!dummy_file (file,old) ||
306
(newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
307
((s = strrchr (tmp,'\\')) && !s[1])))) {
308
sprintf (tmp,newname ?
309
"Can't rename mailbox %.80s to %.80s: invalid name" :
310
"Can't delete mailbox %.80s: invalid name",
315
else if ((fd = open (file,O_BINARY|O_RDWR,NIL)) < 0) {
316
sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
320
/* get exclusive parse/append permission */
321
if ((ld = lockname (lock,file,LOCK_EX)) < 0) {
322
mm_log ("Unable to lock rename mailbox",ERROR);
325
/* lock out other users */
326
if (flock (fd,LOCK_EX|LOCK_NB)) {
327
close (fd); /* couldn't lock, give up on it then */
328
sprintf (tmp,"Mailbox %.80s is in use by another process",old);
330
unlockfd (ld,lock); /* release exclusive parse/append permission */
334
if (newname) { /* want rename? */
335
/* found superior to destination name? */
336
if ((s = strrchr (tmp,'\\')) && (s != tmp) &&
337
((tmp[1] != ':') || (s != tmp + 2))) {
338
c = s[1]; /* remember character after delimiter */
339
*s = s[1] = '\0'; /* tie off name at delimiter */
340
/* name doesn't exist, create it */
341
if (stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) {
342
*s = '\\'; /* restore delimiter */
343
if (!dummy_create (stream,tmp)) ret = NIL;
345
else *s = '\\'; /* restore delimiter */
346
s[1] = c; /* restore character after delimiter */
348
flock (fd,LOCK_UN); /* release lock on the file */
349
close (fd); /* pacify NTFS */
350
/* rename the file */
351
if (ret && rename (file,tmp)) {
352
sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
355
ret = NIL; /* set failure */
359
flock (fd,LOCK_UN); /* release lock on the file */
360
close (fd); /* pacify NTFS */
362
sprintf (tmp,"Can't delete mailbox %.80s: %.80s",old,strerror (errno));
364
ret = NIL; /* set failure */
367
unlockfd (ld,lock); /* release exclusive parse/append permission */
368
return ret; /* return success */
372
* Accepts: stream to open
373
* Returns: stream on success, NIL on failure
376
MAILSTREAM *tenex_open (MAILSTREAM *stream)
379
char tmp[MAILTMPLEN];
380
/* return prototype for OP_PROTOTYPE call */
381
if (!stream) return &tenexproto;
382
if (stream->local) fatal ("tenex recycle stream");
383
/* canonicalize the mailbox name */
384
if (!dummy_file (tmp,stream->mailbox)) {
385
sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
388
if (stream->rdonly ||
389
(fd = open (tmp,O_BINARY|O_RDWR,NIL)) < 0) {
390
if ((fd = open (tmp,O_BINARY|O_RDONLY,NIL)) < 0) {
391
sprintf (tmp,"Can't open mailbox: %.80s",strerror (errno));
395
else if (!stream->rdonly) { /* got it, but readonly */
396
mm_log ("Can't get write access to mailbox, access is readonly",WARN);
400
stream->local = fs_get (sizeof (TENEXLOCAL));
401
LOCAL->fd = fd; /* bind the file */
402
LOCAL->buf = (char *) fs_get (CHUNKSIZE);
403
LOCAL->buflen = CHUNKSIZE - 1;
404
LOCAL->text.data = (unsigned char *) fs_get (CHUNKSIZE);
405
LOCAL->text.size = CHUNKSIZE - 1;
406
/* note if an INBOX or not */
407
stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
408
fs_give ((void **) &stream->mailbox);
409
stream->mailbox = cpystr (tmp);
410
/* get shared parse permission */
411
if ((ld = lockname (tmp,stream->mailbox,LOCK_SH)) < 0) {
412
mm_log ("Unable to lock open mailbox",ERROR);
415
flock (LOCAL->fd,LOCK_SH); /* lock the file */
416
unlockfd (ld,tmp); /* release shared parse permission */
417
LOCAL->filesize = 0; /* initialize parsed file size */
418
LOCAL->filetime = 0; /* time not set up yet */
419
LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
420
stream->sequence++; /* bump sequence number */
421
stream->uid_validity = (unsigned long) time (0);
423
stream->nmsgs = stream->recent = 0;
424
if (tenex_ping (stream) && !stream->nmsgs)
425
mm_log ("Mailbox is empty",(long) NIL);
426
if (!LOCAL) return NIL; /* failure if stream died */
427
stream->perm_seen = stream->perm_deleted =
428
stream->perm_flagged = stream->perm_answered = stream->perm_draft =
429
stream->rdonly ? NIL : T;
430
stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
431
return stream; /* return stream to caller */
435
* Accepts: MAIL stream
439
void tenex_close (MAILSTREAM *stream,long options)
441
if (stream && LOCAL) { /* only if a file is open */
442
int silent = stream->silent;
443
stream->silent = T; /* note this stream is dying */
444
if (options & CL_EXPUNGE) tenex_expunge (stream,NIL,NIL);
445
stream->silent = silent; /* restore previous status */
446
flock (LOCAL->fd,LOCK_UN); /* unlock local file */
447
close (LOCAL->fd); /* close the local file */
448
/* free local text buffer */
449
if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
450
if (LOCAL->text.data) fs_give ((void **) &LOCAL->text.data);
451
/* nuke the local data */
452
fs_give ((void **) &stream->local);
453
stream->dtb = NIL; /* log out the DTB */
457
/* Tenex mail fetch flags
458
* Accepts: MAIL stream
461
* Sniffs at file to get flags
464
void tenex_flags (MAILSTREAM *stream,char *sequence,long flags)
469
if (stream && LOCAL &&
470
((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
471
mail_sequence (stream,sequence)))
472
for (i = 1; i <= stream->nmsgs; i++)
473
if ((elt = mail_elt (stream,i))->sequence) {
474
if (!elt->rfc822_size) { /* have header size yet? */
475
lseek (LOCAL->fd,elt->private.special.offset +
476
elt->private.special.text.size,L_SET);
477
/* resize bigbuf if necessary */
478
if (LOCAL->buflen < elt->private.msg.full.text.size) {
479
fs_give ((void **) &LOCAL->buf);
480
LOCAL->buflen = elt->private.msg.full.text.size;
481
LOCAL->buf = (char *) fs_get (LOCAL->buflen + 1);
484
LOCAL->buf[elt->private.msg.full.text.size] = '\0';
485
/* read in the message */
486
read (LOCAL->fd,LOCAL->buf,elt->private.msg.full.text.size);
487
INIT (&bs,mail_string,(void *) LOCAL->buf,
488
elt->private.msg.full.text.size);
489
/* calculate its CRLF size */
490
elt->rfc822_size = unix_crlflen (&bs);
492
tenex_elt (stream,i); /* get current flags from file */
496
/* TENEX mail fetch message header
497
* Accepts: MAIL stream
499
* pointer to returned header text length
501
* Returns: message header in RFC822 format
504
char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
505
unsigned long *length,long flags)
509
*length = 0; /* default to empty */
510
if (flags & FT_UID) return "";/* UID call "impossible" */
511
/* get to header position */
512
lseek (LOCAL->fd,tenex_hdrpos (stream,msgno,&i),L_SET);
513
if (flags & FT_INTERNAL) {
514
if (i > LOCAL->buflen) { /* resize if not enough space */
515
fs_give ((void **) &LOCAL->buf);
516
LOCAL->buf = (char *) fs_get (LOCAL->buflen = i + 1);
519
read (LOCAL->fd,LOCAL->buf,*length = i);
522
s = (char *) fs_get (i + 1);/* get readin buffer */
523
s[i] = '\0'; /* tie off string */
524
read (LOCAL->fd,s,i); /* slurp the data */
525
/* make CRLF copy of string */
526
*length = unix_crlfcpy (&LOCAL->buf,&LOCAL->buflen,s,i);
527
fs_give ((void **) &s); /* free readin buffer */
532
/* TENEX mail fetch message text (body only)
533
* Accepts: MAIL stream
535
* pointer to returned stringstruct
540
long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
545
/* UID call "impossible" */
546
if (flags & FT_UID) return NIL;
547
/* get message status */
548
elt = tenex_elt (stream,msgno);
549
/* if message not seen */
550
if (!(flags & FT_PEEK) && !elt->seen) {
551
elt->seen = T; /* mark message as seen */
552
/* recalculate status */
553
tenex_update_status (stream,msgno,T);
554
mm_flags (stream,msgno);
556
if (flags & FT_INTERNAL) { /* if internal representation wanted */
557
/* find header position */
558
i = tenex_hdrpos (stream,msgno,&j);
559
if (i > LOCAL->buflen) { /* resize if not enough space */
560
fs_give ((void **) &LOCAL->buf);
561
LOCAL->buf = (char *) fs_get (LOCAL->buflen = i + 1);
563
/* go to text position */
564
lseek (LOCAL->fd,i + j,L_SET);
566
if (read (LOCAL->fd,LOCAL->buf,i) != (long) i) return NIL;
567
/* set up stringstruct for internal */
568
INIT (bs,mail_string,LOCAL->buf,i);
570
else { /* normal form, previous text cached? */
571
if (elt->private.uid == LOCAL->uid)
572
i = elt->private.msg.text.text.size;
573
else { /* not cached, cache it now */
574
LOCAL->uid = elt->private.uid;
575
/* find header position */
576
i = tenex_hdrpos (stream,msgno,&j);
577
/* go to text position */
578
lseek (LOCAL->fd,i + j,L_SET);
579
s = (char *) fs_get ((i = tenex_size (stream,msgno) - j) + 1);
580
s[i] = '\0'; /* tie off string */
581
read (LOCAL->fd,s,i); /* slurp the data */
582
/* make CRLF copy of string */
583
i = elt->private.msg.text.text.size =
584
strcrlfcpy (&LOCAL->text.data,&LOCAL->text.size,s,i);
585
fs_give ((void **) &s); /* free readin buffer */
587
/* set up stringstruct */
588
INIT (bs,mail_string,LOCAL->text.data,i);
590
return T; /* success */
593
/* Tenex mail modify flags
594
* Accepts: MAIL stream
600
void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
602
struct utimbuf times;
604
if (!stream->rdonly) { /* make sure the update takes */
606
fstat (LOCAL->fd,&sbuf); /* get current write time */
607
times.modtime = LOCAL->filetime = sbuf.st_mtime;
608
times.actime = time (0); /* make sure read comes after all that */
609
utime (stream->mailbox,×);
614
/* Tenex mail per-message modify flags
615
* Accepts: MAIL stream
616
* message cache element
619
void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
622
/* maybe need to do a checkpoint? */
623
if (LOCAL->filetime && !LOCAL->shouldcheck) {
624
fstat (LOCAL->fd,&sbuf); /* get current write time */
625
if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
626
LOCAL->filetime = 0; /* don't do this test for any other messages */
628
/* recalculate status */
629
tenex_update_status (stream,elt->msgno,NIL);
632
/* Tenex mail ping mailbox
633
* Accepts: MAIL stream
634
* Returns: T if stream still alive, NIL if not
637
long tenex_ping (MAILSTREAM *stream)
642
char lock[MAILTMPLEN];
644
if (stream && LOCAL) { /* only if stream already open */
645
fstat (LOCAL->fd,&sbuf); /* get current file poop */
646
if (LOCAL->filetime && !(LOCAL->mustcheck || LOCAL->shouldcheck) &&
647
(LOCAL->filetime < sbuf.st_mtime)) LOCAL->shouldcheck = T;
648
/* check for changed message status */
649
if (LOCAL->mustcheck || LOCAL->shouldcheck) {
650
LOCAL->filetime = sbuf.st_mtime;
651
if (LOCAL->shouldcheck) /* babble when we do this unilaterally */
652
mm_notify (stream,"[CHECK] Checking for flag updates",NIL);
653
while (i <= stream->nmsgs) tenex_elt (stream,i++);
654
LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
656
/* get shared parse/append permission */
657
if ((sbuf.st_size != LOCAL->filesize) &&
658
((ld = lockname (lock,stream->mailbox,LOCK_SH)) >= 0)) {
659
/* parse resulting mailbox */
660
r = (tenex_parse (stream)) ? T : NIL;
661
unlockfd (ld,lock); /* release shared parse/append permission */
664
return r; /* return result of the parse */
668
/* Tenex mail check mailbox (reparses status too)
669
* Accepts: MAIL stream
672
void tenex_check (MAILSTREAM *stream)
674
/* mark that a check is desired */
675
if (LOCAL) LOCAL->mustcheck = T;
676
if (tenex_ping (stream)) mm_log ("Check completed",(long) NIL);
679
/* Tenex mail expunge mailbox
680
* sequence to expunge if non-NIL
685
long tenex_expunge (MAILSTREAM *stream,char *sequence,long options)
688
struct utimbuf times;
693
unsigned long j,k,m,recent;
695
unsigned long delta = 0;
696
char lock[MAILTMPLEN];
698
if (!(ret = (sequence ? ((options & EX_UID) ?
699
mail_uid_sequence (stream,sequence) :
700
mail_sequence (stream,sequence)) : LONGT) &&
701
tenex_ping (stream))); /* parse sequence if given, ping stream */
702
else if (stream->rdonly) mm_log ("Expunge ignored on readonly mailbox",WARN);
704
if (LOCAL->filetime && !LOCAL->shouldcheck) {
705
fstat (LOCAL->fd,&sbuf); /* get current write time */
706
if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
708
/* get exclusive access */
709
if ((ld = lockname (lock,stream->mailbox,LOCK_EX)) < 0)
710
mm_log ("Unable to lock expunge mailbox",ERROR);
711
/* make sure see any newly-arrived messages */
712
else if (!tenex_parse (stream));
713
/* get exclusive access */
714
else if (flock (LOCAL->fd,LOCK_EX|LOCK_NB)) {
715
flock (LOCAL->fd,LOCK_SH);/* recover previous lock */
716
mm_log ("Can't expunge because mailbox is in use by another process",
718
unlockfd (ld,lock); /* release exclusive parse/append permission */
722
mm_critical (stream); /* go critical */
723
recent = stream->recent; /* get recent now that pinged and locked */
724
/* for each message */
725
while (i <= stream->nmsgs) {
726
/* get cache element */
727
elt = tenex_elt (stream,i);
728
/* number of bytes to smash or preserve */
729
k = elt->private.special.text.size + tenex_size (stream,i);
730
/* if need to expunge this message */
731
if (elt->deleted && (sequence ? elt->sequence : T)) {
732
/* if recent, note one less recent message */
733
if (elt->recent) --recent;
734
delta += k; /* number of bytes to delete */
735
/* notify upper levels */
736
mail_expunged (stream,i);
737
n++; /* count up one more expunged message */
739
else if (i++ && delta) {/* preserved message */
740
/* first byte to preserve */
741
j = elt->private.special.offset;
742
do { /* read from source position */
743
m = min (k,LOCAL->buflen);
744
lseek (LOCAL->fd,j,L_SET);
745
read (LOCAL->fd,LOCAL->buf,m);
746
pos = j - delta; /* write to destination position */
748
lseek (LOCAL->fd,pos,L_SET);
749
if (write (LOCAL->fd,LOCAL->buf,m) > 0) break;
750
mm_notify (stream,strerror (errno),WARN);
751
mm_diskerror (stream,errno,T);
753
pos += m; /* new position */
754
j += m; /* next chunk, perhaps */
755
} while (k -= m); /* until done */
756
/* note the new address of this text */
757
elt->private.special.offset -= delta;
759
/* preserved but no deleted messages */
760
else pos = elt->private.special.offset + k;
763
if (n) { /* truncate file after last message */
764
if (pos != (LOCAL->filesize -= delta)) {
766
"Calculated size mismatch %lu != %lu, delta = %lu",
767
(unsigned long) pos,(unsigned long) LOCAL->filesize,delta);
768
mm_log (LOCAL->buf,WARN);
769
LOCAL->filesize = pos;/* fix it then */
771
ftruncate (LOCAL->fd,LOCAL->filesize);
772
sprintf (LOCAL->buf,"Expunged %lu messages",n);
773
/* output the news */
774
mm_log (LOCAL->buf,(long) NIL);
776
else mm_log ("No messages deleted, so no update needed",(long) NIL);
777
fsync (LOCAL->fd); /* force disk update */
778
fstat (LOCAL->fd,&sbuf); /* get new write time */
779
times.modtime = LOCAL->filetime = sbuf.st_mtime;
780
times.actime = time (0); /* reset atime to now */
781
utime (stream->mailbox,×);
782
mm_nocritical (stream); /* release critical */
783
/* notify upper level of new mailbox size */
784
mail_exists (stream,stream->nmsgs);
785
mail_recent (stream,recent);
786
flock (LOCAL->fd,LOCK_SH);/* allow sharers again */
787
unlockfd (ld,lock); /* release exclusive parse/append permission */
793
/* Tenex mail copy message(s)
794
* Accepts: MAIL stream
796
* destination mailbox
798
* Returns: T if success, NIL if failed
801
long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
804
struct utimbuf times;
809
char file[MAILTMPLEN],lock[MAILTMPLEN];
811
(mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
812
/* make sure valid mailbox */
813
if (!tenex_isvalid (mailbox,file)) switch (errno) {
814
case ENOENT: /* no such file? */
815
mm_notify (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
817
case 0: /* merely empty file? */
820
if (pc) return (*pc) (stream,sequence,mailbox,options);
821
sprintf (LOCAL->buf,"Invalid Tenex-format mailbox name: %.80s",mailbox);
822
mm_log (LOCAL->buf,ERROR);
825
if (pc) return (*pc) (stream,sequence,mailbox,options);
826
sprintf (LOCAL->buf,"Not a Tenex-format mailbox: %.80s",mailbox);
827
mm_log (LOCAL->buf,ERROR);
830
if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
831
mail_sequence (stream,sequence))) return NIL;
833
if ((fd = open (file,O_BINARY|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)) < 0) {
834
sprintf (LOCAL->buf,"Unable to open copy mailbox: %.80s",strerror (errno));
835
mm_log (LOCAL->buf,ERROR);
838
mm_critical (stream); /* go critical */
839
/* get exclusive parse/append permission */
840
if (flock (fd,LOCK_SH) || ((ld = lockname (lock,file,LOCK_EX)) < 0)) {
841
mm_log ("Unable to lock copy mailbox",ERROR);
842
mm_nocritical (stream);
845
fstat (fd,&sbuf); /* get current file size */
846
lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
848
/* for each requested message */
849
for (i = 1; ret && (i <= stream->nmsgs); i++)
850
if ((elt = mail_elt (stream,i))->sequence) {
851
lseek (LOCAL->fd,elt->private.special.offset,L_SET);
852
/* number of bytes to copy */
853
k = elt->private.special.text.size + tenex_size (stream,i);
854
do { /* read from source position */
855
j = min (k,LOCAL->buflen);
856
read (LOCAL->fd,LOCAL->buf,j);
857
if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
858
} while (ret && (k -= j));/* until done */
860
/* delete all requested messages */
861
if (ret && (options & CP_MOVE)) {
862
sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
863
mm_log (LOCAL->buf,ERROR);
864
ftruncate (fd,sbuf.st_size);
866
/* set atime to now-1 if successful copy */
867
if (ret) times.actime = time (0) - 1;
868
/* else preserved \Marked status */
869
else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
870
sbuf.st_atime : time (0);
871
times.modtime = sbuf.st_mtime;/* preserve mtime */
872
utime (file,×); /* set the times */
873
unlockfd (ld,lock); /* release exclusive parse/append permission */
874
close (fd); /* close the file */
875
mm_nocritical (stream); /* release critical */
876
/* delete all requested messages */
877
if (ret && (options & CP_MOVE)) {
878
for (i = 1; i <= stream->nmsgs; i++)
879
if ((elt = tenex_elt (stream,i))->sequence) {
880
elt->deleted = T; /* mark message deleted */
881
/* recalculate status */
882
tenex_update_status (stream,i,NIL);
884
if (!stream->rdonly) { /* make sure the update takes */
886
fstat (LOCAL->fd,&sbuf); /* get current write time */
887
times.modtime = LOCAL->filetime = sbuf.st_mtime;
888
times.actime = time (0); /* make sure atime remains greater */
889
utime (stream->mailbox,×);
892
if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
893
mm_log ("Can not return meaningful COPYUID with this mailbox format",WARN);
897
/* Tenex mail append message from stringstruct
898
* Accepts: MAIL stream
899
* destination mailbox
902
* Returns: T if append successful, else NIL
905
long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
909
char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
910
struct utimbuf times;
914
unsigned long i,j,uf,size;
917
/* default stream to prototype */
918
if (!stream) stream = &tenexproto;
919
/* make sure valid mailbox */
920
if (!tenex_isvalid (mailbox,file)) switch (errno) {
921
case ENOENT: /* no such file? */
922
if (!compare_cstring (mailbox,"INBOX")) tenex_create (NIL,"INBOX");
924
mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
928
case 0: /* merely empty file? */
931
sprintf (tmp,"Invalid TENEX-format mailbox name: %.80s",mailbox);
935
sprintf (tmp,"Not a TENEX-format mailbox: %.80s",mailbox);
939
/* get first message */
940
if (!(*af) (stream,data,&flags,&date,&message)) return NIL;
942
/* open destination mailbox */
943
if (((fd = open (file,O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IREAD|S_IWRITE))
944
< 0) || !(df = fdopen (fd,"ab"))) {
945
sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
949
/* get parse/append permission */
950
if (flock (fd,LOCK_SH) || ((ld = lockname (lock,file,LOCK_EX)) < 0)) {
951
mm_log ("Unable to lock append mailbox",ERROR);
955
mm_critical (stream); /* go critical */
956
fstat (fd,&sbuf); /* get current file size */
958
do { /* parse flags */
959
if (!SIZE (message)) { /* guard against zero-length */
960
mm_log ("Append of zero-length message",ERROR);
964
f = mail_parse_flags (stream,flags,&i);
965
/* reverse bits (dontcha wish we had CIRC?) */
966
for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));
967
if (date) { /* parse date if given */
968
if (!mail_parse_date (&elt,date)) {
969
sprintf (tmp,"Bad date in append: %.80s",date);
971
ret = NIL; /* mark failure */
974
mail_date (tmp,&elt); /* write preseved date */
976
else internal_date (tmp); /* get current date in IMAP format */
977
i = GETPOS (message); /* remember current position */
978
for (j = SIZE (message), size = 0; j; --j)
979
if (SNX (message) != '\015') ++size;
980
SETPOS (message,i); /* restore position */
982
if (fprintf (df,"%s,%lu;%010lo%02lo\n",tmp,size,uf,(unsigned long) f) < 0)
984
else { /* write message */
985
while (size) if ((c = 0xff & SNX (message)) != '\015') {
986
if (putc (c,df) != EOF) --size;
989
/* get next message */
990
if (size || !(*af) (stream,data,&flags,&date,&message)) ret = NIL;
992
} while (ret && message);
994
if (!ret || (fflush (df) == EOF)) {
995
ftruncate (fd,sbuf.st_size);/* revert file */
996
close (fd); /* make sure fclose() doesn't corrupt us */
998
sprintf (tmp,"Message append failed: %s",strerror (errno));
1003
if (ret) times.actime = time (0) - 1;
1004
/* else preserved \Marked status */
1005
else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
1006
sbuf.st_atime : time (0);
1007
times.modtime = sbuf.st_mtime;/* preserve mtime */
1008
utime (file,×); /* set the times */
1009
fclose (df); /* close the file */
1010
unlockfd (ld,lock); /* release exclusive parse/append permission */
1011
mm_nocritical (stream); /* release critical */
1012
if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))
1013
mm_log ("Can not return meaningful APPENDUID with this mailbox format",
1018
/* Internal routines */
1021
/* Tenex mail return internal message size in bytes
1022
* Accepts: MAIL stream
1024
* Returns: internal size of message
1027
unsigned long tenex_size (MAILSTREAM *stream,unsigned long m)
1029
MESSAGECACHE *elt = mail_elt (stream,m);
1030
return ((m < stream->nmsgs) ? mail_elt (stream,m+1)->private.special.offset :
1032
(elt->private.special.offset + elt->private.special.text.size);
1035
/* Tenex mail parse mailbox
1036
* Accepts: MAIL stream
1037
* Returns: T if parse OK
1038
* NIL if failure, stream aborted
1041
long tenex_parse (MAILSTREAM *stream)
1044
MESSAGECACHE *elt = NIL;
1045
unsigned char c,*s,*t,*x;
1046
char tmp[MAILTMPLEN];
1048
long curpos = LOCAL->filesize;
1049
long nmsgs = stream->nmsgs;
1050
long recent = stream->recent;
1052
short silent = stream->silent;
1053
fstat (LOCAL->fd,&sbuf); /* get status */
1054
if (sbuf.st_size < curpos) { /* sanity check */
1055
sprintf (tmp,"Mailbox shrank from %ld to %ld!",curpos,sbuf.st_size);
1057
tenex_close (stream,NIL);
1060
stream->silent = T; /* don't pass up mm_exists() events yet */
1061
while (sbuf.st_size - curpos){/* while there is stuff to parse */
1062
/* get to that position in the file */
1063
lseek (LOCAL->fd,curpos,L_SET);
1064
if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
1065
sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
1066
(unsigned long) curpos,(unsigned long) sbuf.st_size,
1067
i ? strerror (errno) : "no data read");
1069
tenex_close (stream,NIL);
1072
LOCAL->buf[i] = '\0'; /* tie off buffer just in case */
1073
if (!(s = strchr (LOCAL->buf,'\012'))) {
1074
sprintf (tmp,"Unable to find newline at %lu in %lu bytes, text: %s",
1075
(unsigned long) curpos,i,(char *) LOCAL->buf);
1077
tenex_close (stream,NIL);
1080
*s = '\0'; /* tie off header line */
1081
i = (s + 1) - LOCAL->buf; /* note start of text offset */
1082
if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
1083
sprintf (tmp,"Unable to parse internal header at %lu: %s",
1084
(unsigned long) curpos,(char *) LOCAL->buf);
1086
tenex_close (stream,NIL);
1089
*s++ = '\0'; *t++ = '\0'; /* tie off fields */
1091
added = T; /* note that a new message was added */
1092
/* swell the cache */
1093
mail_exists (stream,++nmsgs);
1094
/* instantiate an elt for this message */
1095
(elt = mail_elt (stream,nmsgs))->valid = T;
1096
elt->private.uid = ++stream->uid_last;
1097
/* note file offset of header */
1098
elt->private.special.offset = curpos;
1100
elt->private.special.text.size = 0;
1101
/* header size not known yet */
1102
elt->private.msg.header.text.size = 0;
1103
x = s; /* parse the header components */
1104
if (mail_parse_date (elt,LOCAL->buf) &&
1105
(elt->private.msg.full.text.size = strtoul (s,(char **) &s,10)) &&
1106
(!(s && *s)) && isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&
1107
isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&
1108
isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&
1109
isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])
1110
elt->private.special.text.size = i;
1112
sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",
1113
curpos,(char *) LOCAL->buf,(char *) x,(char *) t);
1115
tenex_close (stream,NIL);
1118
/* make sure didn't run off end of file */
1119
if ((curpos += (elt->private.msg.full.text.size + i)) > sbuf.st_size) {
1120
sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
1121
elt->private.special.offset,(unsigned long) curpos,
1122
(unsigned long) sbuf.st_size);
1124
tenex_close (stream,NIL);
1127
c = t[10]; /* remember first system flags byte */
1128
t[10] = '\0'; /* tie off flags */
1129
j = strtoul (t,NIL,8); /* get user flags value */
1130
t[10] = c; /* restore first system flags byte */
1131
/* set up all valid user flags (reversed!) */
1132
while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1133
stream->user_flags[i]) elt->user_flags |= 1 << i;
1134
/* calculate system flags */
1135
if ((j = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;
1136
if (j & fDELETED) elt->deleted = T;
1137
if (j & fFLAGGED) elt->flagged = T;
1138
if (j & fANSWERED) elt->answered = T;
1139
if (j & fDRAFT) elt->draft = T;
1140
if (!(j & fOLD)) { /* newly arrived message? */
1142
recent++; /* count up a new recent message */
1143
/* mark it as old */
1144
tenex_update_status (stream,nmsgs,NIL);
1147
fsync (LOCAL->fd); /* make sure all the fOLD flags take */
1148
/* update parsed file size and time */
1149
LOCAL->filesize = sbuf.st_size;
1150
fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */
1151
LOCAL->filetime = sbuf.st_mtime;
1152
if (added && !stream->rdonly){/* make sure atime updated */
1153
struct utimbuf times;
1154
times.actime = time (0);
1155
times.modtime = LOCAL->filetime;
1156
utime (stream->mailbox,×);
1158
stream->silent = silent; /* can pass up events now */
1159
mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */
1160
mail_recent (stream,recent); /* and of change in recent messages */
1161
return LONGT; /* return the winnage */
1164
/* Tenex get cache element with status updating from file
1165
* Accepts: MAIL stream
1167
* Returns: cache element
1170
MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno)
1172
MESSAGECACHE *elt = mail_elt (stream,msgno);
1173
struct { /* old flags */
1174
unsigned int seen : 1;
1175
unsigned int deleted : 1;
1176
unsigned int flagged : 1;
1177
unsigned int answered : 1;
1178
unsigned int draft : 1;
1179
unsigned long user_flags;
1181
old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
1182
old.answered = elt->answered; old.draft = elt->draft;
1183
old.user_flags = elt->user_flags;
1184
tenex_read_flags (stream,elt);
1185
if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
1186
(old.flagged != elt->flagged) || (old.answered != elt->answered) ||
1187
(old.draft != elt->draft) || (old.user_flags != elt->user_flags))
1188
mm_flags (stream,msgno); /* let top level know */
1193
/* Tenex read flags from file
1194
* Accepts: MAIL stream
1195
* Returns: cache element
1198
void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
1201
/* noop if readonly and have valid flags */
1202
if (stream->rdonly && elt->valid) return;
1203
/* set the seek pointer */
1204
lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1205
elt->private.special.text.size - 13,L_SET);
1206
/* read the new flags */
1207
if (read (LOCAL->fd,LOCAL->buf,12) < 0) {
1208
sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
1211
/* calculate system flags */
1212
i = (((LOCAL->buf[10]-'0') * 8) + LOCAL->buf[11]-'0');
1213
elt->seen = i & fSEEN ? T : NIL; elt->deleted = i & fDELETED ? T : NIL;
1214
elt->flagged = i & fFLAGGED ? T : NIL;
1215
elt->answered = i & fANSWERED ? T : NIL; elt->draft = i & fDRAFT ? T : NIL;
1216
LOCAL->buf[10] = '\0'; /* tie off flags */
1217
j = strtoul(LOCAL->buf,NIL,8);/* get user flags value */
1218
/* set up all valid user flags (reversed!) */
1219
while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1220
stream->user_flags[i]) elt->user_flags |= 1 << i;
1221
elt->valid = T; /* have valid flags now */
1224
/* Tenex update status string
1225
* Accepts: MAIL stream
1227
* flag saying whether or not to sync
1230
void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
1232
struct utimbuf times;
1234
MESSAGECACHE *elt = mail_elt (stream,msgno);
1235
unsigned long j,k = 0;
1237
if (stream->rdonly || !elt->valid) tenex_read_flags (stream,elt);
1238
else { /* readwrite */
1239
j = elt->user_flags; /* get user flags */
1240
/* reverse bits (dontcha wish we had CIRC?) */
1241
while (j) k |= 1 << (29 - find_rightmost_bit (&j));
1242
/* print new flag string */
1243
sprintf (LOCAL->buf,"%010lo%02o",k,(unsigned)
1244
(fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
1245
(fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
1246
(fDRAFT * elt->draft)));
1247
while (T) { /* get to that place in the file */
1248
lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1249
elt->private.special.text.size - 13,L_SET);
1250
/* write new flags */
1251
if (write (LOCAL->fd,LOCAL->buf,12) > 0) break;
1252
mm_notify (stream,strerror (errno),WARN);
1253
mm_diskerror (stream,errno,T);
1255
if (syncflag) { /* sync if requested */
1257
fstat (LOCAL->fd,&sbuf); /* get new write time */
1258
times.modtime = LOCAL->filetime = sbuf.st_mtime;
1259
times.actime = time (0); /* make sure read is later */
1260
utime (stream->mailbox,×);
1265
/* Tenex locate header for a message
1266
* Accepts: MAIL stream
1268
* pointer to returned header size
1269
* Returns: position of header in file
1272
unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
1273
unsigned long *size)
1279
MESSAGECACHE *elt = tenex_elt (stream,msgno);
1280
unsigned long ret = elt->private.special.offset +
1281
elt->private.special.text.size;
1282
unsigned long msiz = tenex_size (stream,msgno);
1283
/* is header size known? */
1284
if (!(*size = elt->private.msg.header.text.size)) {
1285
lseek (LOCAL->fd,ret,L_SET);/* get to header position */
1286
/* search message for LF LF */
1287
for (siz = 0; siz < msiz; siz++) {
1288
if (--i <= 0) /* read another buffer as necessary */
1289
read (LOCAL->fd,s = LOCAL->buf,i = min (msiz-siz,(long) MAILTMPLEN));
1290
/* two newline sequence? */
1291
if ((c == '\012') && (*s == '\012')) {
1292
/* yes, note for later */
1293
elt->private.msg.header.text.size = (*size = siz + 1);
1294
return ret; /* return to caller */
1296
else c = *s++; /* next character */
1298
/* header consumes entire message */
1299
elt->private.msg.header.text.size = *size = msiz;