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: 20 December 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 */
52
/* TENEX I/O stream local data */
54
typedef struct tenex_local {
55
unsigned int shouldcheck: 1; /* if ping should do a check instead */
56
unsigned int mustcheck: 1; /* if ping must do a check instead */
57
int fd; /* file descriptor for I/O */
58
off_t filesize; /* file size parsed */
59
time_t filetime; /* last file time */
60
time_t lastsnarf; /* local snarf time */
61
unsigned char *buf; /* temporary buffer */
62
unsigned long buflen; /* current size of temporary buffer */
63
unsigned long uid; /* current text uid */
64
SIZEDTEXT text; /* current text */
68
/* Convenient access to local data */
70
#define LOCAL ((TENEXLOCAL *) stream->local)
73
/* Function prototypes */
75
DRIVER *tenex_valid (char *name);
76
int tenex_isvalid (char *name,char *tmp);
77
void *tenex_parameters (long function,void *value);
78
void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
79
void tenex_list (MAILSTREAM *stream,char *ref,char *pat);
80
void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat);
81
long tenex_create (MAILSTREAM *stream,char *mailbox);
82
long tenex_delete (MAILSTREAM *stream,char *mailbox);
83
long tenex_rename (MAILSTREAM *stream,char *old,char *newname);
84
long tenex_status (MAILSTREAM *stream,char *mbx,long flags);
85
MAILSTREAM *tenex_open (MAILSTREAM *stream);
86
void tenex_close (MAILSTREAM *stream,long options);
87
void tenex_fast (MAILSTREAM *stream,char *sequence,long flags);
88
void tenex_flags (MAILSTREAM *stream,char *sequence,long flags);
89
char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
90
unsigned long *length,long flags);
91
long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
92
void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
93
void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
94
long tenex_ping (MAILSTREAM *stream);
95
void tenex_check (MAILSTREAM *stream);
96
void tenex_snarf (MAILSTREAM *stream);
97
long tenex_expunge (MAILSTREAM *stream,char *sequence,long options);
98
long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
99
long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
101
unsigned long tenex_size (MAILSTREAM *stream,unsigned long m);
102
char *tenex_file (char *dst,char *name);
103
long tenex_parse (MAILSTREAM *stream);
104
MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno);
105
void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt);
106
void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,
108
unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
109
unsigned long *size);
111
/* Tenex mail routines */
114
/* Driver dispatch used by MAIL */
116
DRIVER tenexdriver = {
117
"tenex", /* driver name */
118
DR_LOCAL|DR_MAIL|DR_NOSTICKY|DR_LOCKING,
120
(DRIVER *) NIL, /* next driver */
121
tenex_valid, /* mailbox is valid for us */
122
tenex_parameters, /* manipulate parameters */
123
tenex_scan, /* scan mailboxes */
124
tenex_list, /* list mailboxes */
125
tenex_lsub, /* list subscribed mailboxes */
126
NIL, /* subscribe to mailbox */
127
NIL, /* unsubscribe from mailbox */
128
dummy_create, /* create mailbox */
129
tenex_delete, /* delete mailbox */
130
tenex_rename, /* rename mailbox */
131
tenex_status, /* status of mailbox */
132
tenex_open, /* open mailbox */
133
tenex_close, /* close mailbox */
134
tenex_fast, /* fetch message "fast" attributes */
135
tenex_flags, /* fetch message flags */
136
NIL, /* fetch overview */
137
NIL, /* fetch message envelopes */
138
tenex_header, /* fetch message header */
139
tenex_text, /* fetch message body */
140
NIL, /* fetch partial message text */
141
NIL, /* unique identifier */
142
NIL, /* message number */
143
tenex_flag, /* modify flags */
144
tenex_flagmsg, /* per-message modify flags */
145
NIL, /* search for message based on criteria */
146
NIL, /* sort messages */
147
NIL, /* thread messages */
148
tenex_ping, /* ping mailbox to see if still alive */
149
tenex_check, /* check for new messages */
150
tenex_expunge, /* expunge deleted messages */
151
tenex_copy, /* copy messages to another mailbox */
152
tenex_append, /* append string message to mailbox */
153
NIL /* garbage collect stream */
156
/* prototype stream */
157
MAILSTREAM tenexproto = {&tenexdriver};
159
/* Tenex mail validate mailbox
160
* Accepts: mailbox name
161
* Returns: our driver if name is valid, NIL otherwise
164
DRIVER *tenex_valid (char *name)
166
char tmp[MAILTMPLEN];
167
return tenex_isvalid (name,tmp) ? &tenexdriver : NIL;
171
/* Tenex mail test for valid mailbox
172
* Accepts: mailbox name
173
* Returns: T if valid, NIL otherwise
176
int tenex_isvalid (char *name,char *tmp)
180
char *s,file[MAILTMPLEN];
183
errno = EINVAL; /* assume invalid argument */
184
/* if file, get its status */
185
if ((s = tenex_file (file,name)) && !stat (s,&sbuf)) {
186
if (!sbuf.st_size) { /* allow empty file if INBOX */
187
if ((s = mailboxfile (tmp,name)) && !*s) ret = T;
188
else errno = 0; /* empty file */
190
else if ((fd = open (file,O_RDONLY,NIL)) >= 0) {
191
memset (tmp,'\0',MAILTMPLEN);
192
if ((read (fd,tmp,64) >= 0) && (s = strchr (tmp,'\012')) &&
193
(s[-1] != '\015')) { /* valid format? */
194
*s = '\0'; /* tie off header */
195
/* must begin with dd-mmm-yy" */
196
ret = (((tmp[2] == '-' && tmp[6] == '-') ||
197
(tmp[1] == '-' && tmp[5] == '-')) &&
198
(s = strchr (tmp+18,',')) && strchr (s+2,';')) ? T : NIL;
200
else errno = -1; /* bogus format */
201
close (fd); /* close the file */
202
/* \Marked status? */
203
if (sbuf.st_ctime > sbuf.st_atime) {
204
tp[0] = sbuf.st_atime; /* preserve atime and mtime */
205
tp[1] = sbuf.st_mtime;
206
utime (file,tp); /* set the times */
210
/* in case INBOX but not tenex format */
211
else if ((errno == ENOENT) && !compare_cstring (name,"INBOX")) errno = -1;
212
return ret; /* return what we should */
215
/* Tenex manipulate driver parameters
216
* Accepts: function code
217
* function-dependent value
218
* Returns: function-dependent return value
221
void *tenex_parameters (long function,void *value)
224
switch ((int) function) {
226
if (value) ret = tenex_file ((char *) value,"INBOX");
233
/* Tenex mail scan mailboxes
234
* Accepts: mail stream
240
void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
242
if (stream) dummy_scan (NIL,ref,pat,contents);
246
/* Tenex mail list mailboxes
247
* Accepts: mail stream
252
void tenex_list (MAILSTREAM *stream,char *ref,char *pat)
254
if (stream) dummy_list (NIL,ref,pat);
258
/* Tenex mail list subscribed mailboxes
259
* Accepts: mail stream
264
void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat)
266
if (stream) dummy_lsub (NIL,ref,pat);
269
/* Tenex mail delete mailbox
270
* Accepts: MAIL stream
271
* mailbox name to delete
272
* Returns: T on success, NIL on failure
275
long tenex_delete (MAILSTREAM *stream,char *mailbox)
277
return tenex_rename (stream,mailbox,NIL);
281
/* Tenex mail rename mailbox
282
* Accepts: MAIL stream
284
* new mailbox name (or NIL for delete)
285
* Returns: T on success, NIL on failure
288
long tenex_rename (MAILSTREAM *stream,char *old,char *newname)
291
char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
294
if (!tenex_file (file,old) ||
295
(newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
296
((s = strrchr (tmp,'/')) && !s[1])))) {
297
sprintf (tmp,newname ?
298
"Can't rename mailbox %.80s to %.80s: invalid name" :
299
"Can't delete mailbox %.80s: invalid name",
304
else if ((fd = open (file,O_RDWR,NIL)) < 0) {
305
sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
309
/* get exclusive parse/append permission */
310
if ((ld = lockfd (fd,lock,LOCK_EX)) < 0) {
311
MM_LOG ("Unable to lock rename mailbox",ERROR);
314
/* lock out other users */
315
if (flock (fd,LOCK_EX|LOCK_NB)) {
316
close (fd); /* couldn't lock, give up on it then */
317
sprintf (tmp,"Mailbox %.80s is in use by another process",old);
319
unlockfd (ld,lock); /* release exclusive parse/append permission */
323
if (newname) { /* want rename? */
324
if (s = strrchr (tmp,'/')) {/* found superior to destination name? */
325
c = *++s; /* remember first character of inferior */
326
*s = '\0'; /* tie off to get just superior */
327
/* name doesn't exist, create it */
328
if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
329
!dummy_create_path (stream,tmp,get_dir_protection (newname)))
331
else *s = c; /* restore full name */
333
/* rename the file */
334
if (ret && rename (file,tmp)) {
335
sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
338
ret = NIL; /* set failure */
341
else if (unlink (file)) {
342
sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
344
ret = NIL; /* set failure */
346
flock (fd,LOCK_UN); /* release lock on the file */
347
close (fd); /* close the file */
348
unlockfd (ld,lock); /* release exclusive parse/append permission */
349
/* recreate file if renamed INBOX */
350
if (ret && !compare_cstring (old,"INBOX")) dummy_create (NIL,"mail.txt");
351
return ret; /* return success */
355
* Accepts: mail stream
358
* Returns: T on success, NIL on failure
361
long tenex_status (MAILSTREAM *stream,char *mbx,long flags)
365
MAILSTREAM *tstream = NIL;
366
MAILSTREAM *systream = NIL;
367
/* make temporary stream (unless this mbx) */
368
if (!stream && !(stream = tstream =
369
mail_open (NIL,mbx,OP_READONLY|OP_SILENT))) return NIL;
370
status.flags = flags; /* return status values */
371
status.messages = stream->nmsgs;
372
status.recent = stream->recent;
373
if (flags & SA_UNSEEN) /* must search to get unseen messages */
374
for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++)
375
if (!mail_elt (stream,i)->seen) status.unseen++;
376
status.uidnext = stream->uid_last + 1;
377
status.uidvalidity = stream->uid_validity;
378
/* calculate post-snarf results */
379
if (!status.recent && stream->inbox &&
380
(systream = mail_open (NIL,sysinbox (),OP_READONLY|OP_SILENT))) {
381
status.messages += systream->nmsgs;
382
status.recent += systream->recent;
383
if (flags & SA_UNSEEN) /* must search to get unseen messages */
384
for (i = 1; i <= systream->nmsgs; i++)
385
if (!mail_elt (systream,i)->seen) status.unseen++;
386
/* kludge but probably good enough */
387
status.uidnext += systream->nmsgs;
389
MM_STATUS(stream,mbx,&status);/* pass status to main program */
390
if (tstream) mail_close (tstream);
391
if (systream) mail_close (systream);
392
return T; /* success */
396
* Accepts: stream to open
397
* Returns: stream on success, NIL on failure
400
MAILSTREAM *tenex_open (MAILSTREAM *stream)
403
char tmp[MAILTMPLEN];
404
blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
405
/* return prototype for OP_PROTOTYPE call */
406
if (!stream) return user_flags (&tenexproto);
407
if (stream->local) fatal ("tenex recycle stream");
408
user_flags (stream); /* set up user flags */
409
/* canonicalize the mailbox name */
410
if (!tenex_file (tmp,stream->mailbox)) {
411
sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
414
if (stream->rdonly ||
415
(fd = open (tmp,O_RDWR,NIL)) < 0) {
416
if ((fd = open (tmp,O_RDONLY,NIL)) < 0) {
417
sprintf (tmp,"Can't open mailbox: %s",strerror (errno));
421
else if (!stream->rdonly) { /* got it, but readonly */
422
MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);
426
stream->local = fs_get (sizeof (TENEXLOCAL));
427
LOCAL->buf = (char *) fs_get (CHUNKSIZE);
428
LOCAL->buflen = CHUNKSIZE - 1;
429
LOCAL->text.data = (unsigned char *) fs_get (CHUNKSIZE);
430
LOCAL->text.size = CHUNKSIZE - 1;
432
/* note if an INBOX or not */
433
stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
434
LOCAL->fd = fd; /* bind the file */
436
fs_give ((void **) &stream->mailbox);
437
/* save canonical name */
438
stream->mailbox = cpystr (tmp);
439
/* get shared parse permission */
440
if ((ld = lockfd (fd,tmp,LOCK_SH)) < 0) {
441
MM_LOG ("Unable to lock open mailbox",ERROR);
444
(*bn) (BLOCK_FILELOCK,NIL);
445
flock (LOCAL->fd,LOCK_SH); /* lock the file */
446
(*bn) (BLOCK_NONE,NIL);
447
unlockfd (ld,tmp); /* release shared parse permission */
448
LOCAL->filesize = 0; /* initialize parsed file size */
449
/* time not set up yet */
450
LOCAL->lastsnarf = LOCAL->filetime = 0;
451
LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
452
stream->sequence++; /* bump sequence number */
454
stream->nmsgs = stream->recent = 0;
455
if (tenex_ping (stream) && !stream->nmsgs)
456
MM_LOG ("Mailbox is empty",(long) NIL);
457
if (!LOCAL) return NIL; /* failure if stream died */
458
stream->perm_seen = stream->perm_deleted =
459
stream->perm_flagged = stream->perm_answered = stream->perm_draft =
460
stream->rdonly ? NIL : T;
461
stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
462
return stream; /* return stream to caller */
466
* Accepts: MAIL stream
470
void tenex_close (MAILSTREAM *stream,long options)
472
if (stream && LOCAL) { /* only if a file is open */
473
int silent = stream->silent;
474
stream->silent = T; /* note this stream is dying */
475
if (options & CL_EXPUNGE) tenex_expunge (stream,NIL,NIL);
476
stream->silent = silent; /* restore previous status */
477
flock (LOCAL->fd,LOCK_UN); /* unlock local file */
478
close (LOCAL->fd); /* close the local file */
479
/* free local text buffer */
480
if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
481
if (LOCAL->text.data) fs_give ((void **) &LOCAL->text.data);
482
/* nuke the local data */
483
fs_give ((void **) &stream->local);
484
stream->dtb = NIL; /* log out the DTB */
488
/* Tenex mail fetch fast data
489
* Accepts: MAIL stream
494
void tenex_fast (MAILSTREAM *stream,char *sequence,long flags)
499
if (stream && LOCAL &&
500
((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
501
mail_sequence (stream,sequence)))
502
for (i = 1; i <= stream->nmsgs; i++)
503
if ((elt = mail_elt (stream,i))->sequence) {
504
if (!elt->rfc822_size) { /* have header size yet? */
505
lseek (LOCAL->fd,elt->private.special.offset +
506
elt->private.special.text.size,L_SET);
507
/* resize bigbuf if necessary */
508
if (LOCAL->buflen < elt->private.msg.full.text.size) {
509
fs_give ((void **) &LOCAL->buf);
510
LOCAL->buflen = elt->private.msg.full.text.size;
511
LOCAL->buf = (char *) fs_get (LOCAL->buflen + 1);
514
LOCAL->buf[elt->private.msg.full.text.size] = '\0';
515
/* read in the message */
516
read (LOCAL->fd,LOCAL->buf,elt->private.msg.full.text.size);
517
INIT (&bs,mail_string,(void *) LOCAL->buf,
518
elt->private.msg.full.text.size);
519
/* calculate its CRLF size */
520
elt->rfc822_size = strcrlflen (&bs);
522
tenex_elt (stream,i); /* get current flags from file */
527
/* Tenex mail fetch flags
528
* Accepts: MAIL stream
531
* Sniffs at file to get flags
534
void tenex_flags (MAILSTREAM *stream,char *sequence,long flags)
537
if (stream && LOCAL &&
538
((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
539
mail_sequence (stream,sequence)))
540
for (i = 1; i <= stream->nmsgs; i++)
541
if (mail_elt (stream,i)->sequence) tenex_elt (stream,i);
544
/* TENEX mail fetch message header
545
* Accepts: MAIL stream
547
* pointer to returned header text length
549
* Returns: message header in RFC822 format
552
char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
553
unsigned long *length,long flags)
557
*length = 0; /* default to empty */
558
if (flags & FT_UID) return "";/* UID call "impossible" */
559
/* get to header position */
560
lseek (LOCAL->fd,tenex_hdrpos (stream,msgno,&i),L_SET);
561
if (flags & FT_INTERNAL) {
562
if (i > LOCAL->buflen) { /* resize if not enough space */
563
fs_give ((void **) &LOCAL->buf);
564
LOCAL->buf = (char *) fs_get (LOCAL->buflen = i + 1);
567
read (LOCAL->fd,LOCAL->buf,*length = i);
570
s = (char *) fs_get (i + 1);/* get readin buffer */
571
s[i] = '\0'; /* tie off string */
572
read (LOCAL->fd,s,i); /* slurp the data */
573
/* make CRLF copy of string */
574
*length = strcrlfcpy (&LOCAL->buf,&LOCAL->buflen,s,i);
575
fs_give ((void **) &s); /* free readin buffer */
577
return (char *) LOCAL->buf;
580
/* TENEX mail fetch message text (body only)
581
* Accepts: MAIL stream
583
* pointer to returned stringstruct
588
long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
593
/* UID call "impossible" */
594
if (flags & FT_UID) return NIL;
595
/* get message status */
596
elt = tenex_elt (stream,msgno);
597
/* if message not seen */
598
if (!(flags & FT_PEEK) && !elt->seen) {
599
elt->seen = T; /* mark message as seen */
600
/* recalculate status */
601
tenex_update_status (stream,msgno,T);
602
MM_FLAGS (stream,msgno);
604
if (flags & FT_INTERNAL) { /* if internal representation wanted */
605
/* find header position */
606
i = tenex_hdrpos (stream,msgno,&j);
607
if (i > LOCAL->buflen) { /* resize if not enough space */
608
fs_give ((void **) &LOCAL->buf);
609
LOCAL->buf = (char *) fs_get (LOCAL->buflen = i + 1);
611
/* go to text position */
612
lseek (LOCAL->fd,i + j,L_SET);
614
read (LOCAL->fd,LOCAL->buf,i);
615
/* set up stringstruct for internal */
616
INIT (bs,mail_string,LOCAL->buf,i);
618
else { /* normal form, previous text cached? */
619
if (elt->private.uid == LOCAL->uid)
620
i = elt->private.msg.text.text.size;
621
else { /* not cached, cache it now */
622
LOCAL->uid = elt->private.uid;
623
/* find header position */
624
i = tenex_hdrpos (stream,msgno,&j);
625
/* go to text position */
626
lseek (LOCAL->fd,i + j,L_SET);
627
s = (char *) fs_get ((i = tenex_size (stream,msgno) - j) + 1);
628
s[i] = '\0'; /* tie off string */
629
read (LOCAL->fd,s,i); /* slurp the data */
630
/* make CRLF copy of string */
631
i = elt->private.msg.text.text.size =
632
strcrlfcpy (&LOCAL->text.data,&LOCAL->text.size,s,i);
633
fs_give ((void **) &s); /* free readin buffer */
635
/* set up stringstruct */
636
INIT (bs,mail_string,LOCAL->text.data,i);
638
return T; /* success */
641
/* Tenex mail modify flags
642
* Accepts: MAIL stream
648
void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
652
if (!stream->rdonly) { /* make sure the update takes */
654
fstat (LOCAL->fd,&sbuf); /* get current write time */
655
tp[1] = LOCAL->filetime = sbuf.st_mtime;
656
tp[0] = time (0); /* make sure read comes after all that */
657
utime (stream->mailbox,tp);
662
/* Tenex mail per-message modify flags
663
* Accepts: MAIL stream
664
* message cache element
667
void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
670
/* maybe need to do a checkpoint? */
671
if (LOCAL->filetime && !LOCAL->shouldcheck) {
672
fstat (LOCAL->fd,&sbuf); /* get current write time */
673
if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
674
LOCAL->filetime = 0; /* don't do this test for any other messages */
676
/* recalculate status */
677
tenex_update_status (stream,elt->msgno,NIL);
680
/* Tenex mail ping mailbox
681
* Accepts: MAIL stream
682
* Returns: T if stream still alive, NIL if not
685
long tenex_ping (MAILSTREAM *stream)
690
char lock[MAILTMPLEN];
692
if (stream && LOCAL) { /* only if stream already open */
693
fstat (LOCAL->fd,&sbuf); /* get current file poop */
694
if (LOCAL->filetime && !(LOCAL->mustcheck || LOCAL->shouldcheck) &&
695
(LOCAL->filetime < sbuf.st_mtime)) LOCAL->shouldcheck = T;
696
/* check for changed message status */
697
if (LOCAL->mustcheck || LOCAL->shouldcheck) {
698
LOCAL->filetime = sbuf.st_mtime;
699
if (LOCAL->shouldcheck) /* babble when we do this unilaterally */
700
MM_NOTIFY (stream,"[CHECK] Checking for flag updates",NIL);
701
while (i <= stream->nmsgs) tenex_elt (stream,i++);
702
LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
704
/* get shared parse/append permission */
705
if ((sbuf.st_size != LOCAL->filesize) &&
706
((ld = lockfd (LOCAL->fd,lock,LOCK_SH)) >= 0)) {
707
/* parse resulting mailbox */
708
r = (tenex_parse (stream)) ? T : NIL;
709
unlockfd (ld,lock); /* release shared parse/append permission */
711
if (LOCAL) { /* stream must still be alive */
712
/* snarf if this is a read-write inbox */
713
if (stream->inbox && !stream->rdonly) {
714
tenex_snarf (stream);
715
fstat (LOCAL->fd,&sbuf);/* see if file changed now */
716
if ((sbuf.st_size != LOCAL->filesize) &&
717
((ld = lockfd (LOCAL->fd,lock,LOCK_SH)) >= 0)) {
718
/* parse resulting mailbox */
719
r = (tenex_parse (stream)) ? T : NIL;
720
unlockfd (ld,lock); /* release shared parse/append permission */
725
return r; /* return result of the parse */
729
/* Tenex mail check mailbox (reparses status too)
730
* Accepts: MAIL stream
733
void tenex_check (MAILSTREAM *stream)
735
/* mark that a check is desired */
736
if (LOCAL) LOCAL->mustcheck = T;
737
if (tenex_ping (stream)) MM_LOG ("Check completed",(long) NIL);
740
/* Tenex mail snarf messages from system inbox
741
* Accepts: MAIL stream
744
void tenex_snarf (MAILSTREAM *stream)
747
unsigned long j,r,hdrlen,txtlen;
749
char *hdr,*txt,lock[MAILTMPLEN],tmp[MAILTMPLEN];
751
MAILSTREAM *sysibx = NIL;
753
/* give up if can't get exclusive permission */
754
if ((time (0) >= (LOCAL->lastsnarf +
755
(long) mail_parameters (NIL,GET_SNARFINTERVAL,NIL))) &&
756
strcmp (sysinbox (),stream->mailbox) &&
757
((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) >= 0)) {
758
MM_CRITICAL (stream); /* go critical */
759
/* sizes match and anything in sysinbox? */
760
if (!stat (sysinbox (),&sbuf) && sbuf.st_size &&
761
!fstat (LOCAL->fd,&sbuf) && (sbuf.st_size == LOCAL->filesize) &&
762
(sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) &&
763
(!sysibx->rdonly) && (r = sysibx->nmsgs)) {
764
/* yes, go to end of file in our mailbox */
765
lseek (LOCAL->fd,sbuf.st_size,L_SET);
766
/* for each message in sysibx mailbox */
767
while (r && (++i <= sysibx->nmsgs)) {
768
/* snarf message from system INBOX */
769
hdr = cpystr (mail_fetchheader_full(sysibx,i,NIL,&hdrlen,FT_INTERNAL));
770
txt = mail_fetchtext_full (sysibx,i,&txtlen,FT_INTERNAL|FT_PEEK);
771
/* if have a message */
772
if (j = hdrlen + txtlen) {
773
/* calculate header line */
774
mail_date (LOCAL->buf,elt = mail_elt (sysibx,i));
775
sprintf (LOCAL->buf + strlen (LOCAL->buf),
776
",%lu;0000000000%02o\n",j,(unsigned)
777
((fSEEN * elt->seen) + (fDELETED * elt->deleted) +
778
(fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
779
(fDRAFT * elt->draft)));
781
if ((write (LOCAL->fd,LOCAL->buf,strlen (LOCAL->buf)) < 0) ||
782
(write (LOCAL->fd,hdr,hdrlen) < 0) ||
783
(write (LOCAL->fd,txt,txtlen) < 0)) r = 0;
785
fs_give ((void **) &hdr);
788
/* make sure all the updates take */
789
if (fsync (LOCAL->fd)) r = 0;
790
if (r) { /* delete all the messages we copied */
791
if (r == 1) strcpy (tmp,"1");
792
else sprintf (tmp,"1:%lu",r);
793
mail_flag (sysibx,tmp,"\\Deleted",ST_SET);
794
mail_expunge (sysibx); /* now expunge all those messages */
797
sprintf (LOCAL->buf,"Can't copy new mail: %s",strerror (errno));
798
MM_LOG (LOCAL->buf,WARN);
799
ftruncate (LOCAL->fd,sbuf.st_size);
801
fstat (LOCAL->fd,&sbuf); /* yes, get current file size */
802
LOCAL->filetime = sbuf.st_mtime;
804
if (sysibx) mail_close (sysibx);
805
MM_NOCRITICAL (stream); /* release critical */
806
unlockfd (ld,lock); /* release exclusive parse/append permission */
807
LOCAL->lastsnarf = time (0);/* note time of last snarf */
811
/* Tenex mail expunge mailbox
812
* Accepts: MAIL stream
813
* sequence to expunge if non-NIL
818
long tenex_expunge (MAILSTREAM *stream,char *sequence,long options)
826
unsigned long j,k,m,recent;
828
unsigned long delta = 0;
829
char lock[MAILTMPLEN];
831
blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
832
if (!(ret = (sequence ? ((options & EX_UID) ?
833
mail_uid_sequence (stream,sequence) :
834
mail_sequence (stream,sequence)) : LONGT) &&
835
tenex_ping (stream))); /* parse sequence if given, ping stream */
836
else if (stream->rdonly) MM_LOG ("Expunge ignored on readonly mailbox",WARN);
838
if (LOCAL->filetime && !LOCAL->shouldcheck) {
839
fstat (LOCAL->fd,&sbuf); /* get current write time */
840
if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
842
/* The cretins who designed flock() created a window of vulnerability in
843
* upgrading locks from shared to exclusive or downgrading from exclusive
844
* to shared. Rather than maintain the lock at shared status at a minimum,
845
* flock() actually *releases* the former lock. Obviously they never talked
846
* to any database guys. Fortunately, we have the parse/append permission
847
* lock. If we require this lock before going exclusive on the mailbox,
848
* another process can not sneak in and steal the exclusive mailbox lock on
849
* us, because it will block on trying to get parse/append permission first.
851
/* get exclusive parse/append permission */
852
if ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) < 0)
853
MM_LOG ("Unable to lock expunge mailbox",ERROR);
854
/* make sure see any newly-arrived messages */
855
else if (!tenex_parse (stream));
856
/* get exclusive access */
857
else if (flock (LOCAL->fd,LOCK_EX|LOCK_NB)) {
858
(*bn) (BLOCK_FILELOCK,NIL);
859
flock (LOCAL->fd,LOCK_SH);/* recover previous lock */
860
(*bn) (BLOCK_NONE,NIL);
861
MM_LOG ("Can't expunge because mailbox is in use by another process",
863
unlockfd (ld,lock); /* release exclusive parse/append permission */
867
MM_CRITICAL (stream); /* go critical */
868
recent = stream->recent; /* get recent now that pinged and locked */
869
/* for each message */
870
while (i <= stream->nmsgs) {
871
/* get cache element */
872
elt = tenex_elt (stream,i);
873
/* number of bytes to smash or preserve */
874
k = elt->private.special.text.size + tenex_size (stream,i);
875
/* if need to expunge this message */
876
if (elt->deleted && (sequence ? elt->sequence : T)) {
877
/* if recent, note one less recent message */
878
if (elt->recent) --recent;
879
delta += k; /* number of bytes to delete */
880
/* notify upper levels */
881
mail_expunged (stream,i);
882
n++; /* count up one more expunged message */
884
else if (i++ && delta) {/* preserved message */
885
/* first byte to preserve */
886
j = elt->private.special.offset;
887
do { /* read from source position */
888
m = min (k,LOCAL->buflen);
889
lseek (LOCAL->fd,j,L_SET);
890
read (LOCAL->fd,LOCAL->buf,m);
891
pos = j - delta; /* write to destination position */
892
lseek (LOCAL->fd,pos,L_SET);
894
lseek (LOCAL->fd,pos,L_SET);
895
if (write (LOCAL->fd,LOCAL->buf,m) > 0) break;
896
MM_NOTIFY (stream,strerror (errno),WARN);
897
MM_DISKERROR (stream,errno,T);
899
pos += m; /* new position */
900
j += m; /* next chunk, perhaps */
901
} while (k -= m); /* until done */
902
/* note the new address of this text */
903
elt->private.special.offset -= delta;
905
/* preserved but no deleted messages */
906
else pos = elt->private.special.offset + k;
909
if (n) { /* truncate file after last message */
910
if (pos != (LOCAL->filesize -= delta)) {
912
"Calculated size mismatch %lu != %lu, delta = %lu",
913
(unsigned long) pos,(unsigned long) LOCAL->filesize,delta);
914
MM_LOG (LOCAL->buf,WARN);
915
LOCAL->filesize = pos;/* fix it then */
917
ftruncate (LOCAL->fd,LOCAL->filesize);
918
sprintf (LOCAL->buf,"Expunged %lu messages",n);
919
/* output the news */
920
MM_LOG (LOCAL->buf,(long) NIL);
922
else MM_LOG ("No messages deleted, so no update needed",(long) NIL);
923
fsync (LOCAL->fd); /* force disk update */
924
fstat (LOCAL->fd,&sbuf); /* get new write time */
925
tp[1] = LOCAL->filetime = sbuf.st_mtime;
926
tp[0] = time (0); /* reset atime to now */
927
utime (stream->mailbox,tp);
928
MM_NOCRITICAL (stream); /* release critical */
929
/* notify upper level of new mailbox size */
930
mail_exists (stream,stream->nmsgs);
931
mail_recent (stream,recent);
932
(*bn) (BLOCK_FILELOCK,NIL);
933
flock (LOCAL->fd,LOCK_SH);/* allow sharers again */
934
(*bn) (BLOCK_NONE,NIL);
935
unlockfd (ld,lock); /* release exclusive parse/append permission */
941
/* Tenex mail copy message(s)
942
* Accepts: MAIL stream
944
* destination mailbox
946
* Returns: T if success, NIL if failed
949
long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
957
char file[MAILTMPLEN],lock[MAILTMPLEN];
959
(mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
960
/* make sure valid mailbox */
961
if (!tenex_isvalid (mailbox,LOCAL->buf)) switch (errno) {
962
case ENOENT: /* no such file? */
963
MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
965
case 0: /* merely empty file? */
968
if (pc) return (*pc) (stream,sequence,mailbox,options);
969
sprintf (LOCAL->buf,"Invalid Tenex-format mailbox name: %.80s",mailbox);
970
MM_LOG (LOCAL->buf,ERROR);
973
if (pc) return (*pc) (stream,sequence,mailbox,options);
974
sprintf (LOCAL->buf,"Not a Tenex-format mailbox: %.80s",mailbox);
975
MM_LOG (LOCAL->buf,ERROR);
978
if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
979
mail_sequence (stream,sequence))) return NIL;
981
if ((fd = open (tenex_file(file,mailbox),O_RDWR,NIL)) < 0) {
982
sprintf (LOCAL->buf,"Unable to open copy mailbox: %s",strerror (errno));
983
MM_LOG (LOCAL->buf,ERROR);
986
MM_CRITICAL (stream); /* go critical */
987
/* get exclusive parse/append permission */
988
if (flock (fd,LOCK_SH) || ((ld = lockfd (fd,lock,LOCK_EX)) < 0)) {
989
MM_LOG ("Unable to lock copy mailbox",ERROR);
990
MM_NOCRITICAL (stream);
993
fstat (fd,&sbuf); /* get current file size */
994
lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
996
/* for each requested message */
997
for (i = 1; ret && (i <= stream->nmsgs); i++)
998
if ((elt = mail_elt (stream,i))->sequence) {
999
lseek (LOCAL->fd,elt->private.special.offset,L_SET);
1000
/* number of bytes to copy */
1001
k = elt->private.special.text.size + tenex_size (stream,i);
1002
do { /* read from source position */
1003
j = min (k,LOCAL->buflen);
1004
read (LOCAL->fd,LOCAL->buf,j);
1005
if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
1006
} while (ret && (k -= j));/* until done */
1008
/* make sure all the updates take */
1009
if (!(ret && (ret = !fsync (fd)))) {
1010
sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
1011
MM_LOG (LOCAL->buf,ERROR);
1012
ftruncate (fd,sbuf.st_size);
1014
if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */
1015
/* else preserve \Marked status */
1016
else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
1017
tp[1] = sbuf.st_mtime; /* preserve mtime */
1018
utime (file,tp); /* set the times */
1019
close (fd); /* close the file */
1020
unlockfd (ld,lock); /* release exclusive parse/append permission */
1021
MM_NOCRITICAL (stream); /* release critical */
1022
/* delete all requested messages */
1023
if (ret && (options & CP_MOVE)) {
1024
for (i = 1; i <= stream->nmsgs; i++)
1025
if ((elt = tenex_elt (stream,i))->sequence) {
1026
elt->deleted = T; /* mark message deleted */
1027
/* recalculate status */
1028
tenex_update_status (stream,i,NIL);
1030
if (!stream->rdonly) { /* make sure the update takes */
1032
fstat (LOCAL->fd,&sbuf); /* get current write time */
1033
tp[1] = LOCAL->filetime = sbuf.st_mtime;
1034
tp[0] = time (0); /* make sure atime remains greater */
1035
utime (stream->mailbox,tp);
1038
if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
1039
mm_log ("Can not return meaningful COPYUID with this mailbox format",WARN);
1043
/* Tenex mail append message from stringstruct
1044
* Accepts: MAIL stream
1045
* destination mailbox
1048
* Returns: T if append successful, else NIL
1051
long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
1055
char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
1060
unsigned long i,j,uf,size;
1063
/* default stream to prototype */
1064
if (!stream) stream = user_flags (&tenexproto);
1065
/* make sure valid mailbox */
1066
if (!tenex_isvalid (mailbox,tmp)) switch (errno) {
1067
case ENOENT: /* no such file? */
1068
if (!compare_cstring (mailbox,"INBOX")) dummy_create (NIL,"mail.txt");
1070
MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
1074
case 0: /* merely empty file? */
1077
sprintf (tmp,"Invalid TENEX-format mailbox name: %.80s",mailbox);
1081
sprintf (tmp,"Not a TENEX-format mailbox: %.80s",mailbox);
1085
/* get first message */
1086
if (!MM_APPEND (af) (stream,data,&flags,&date,&message)) return NIL;
1088
/* open destination mailbox */
1089
if (((fd = open (tenex_file (file,mailbox),O_WRONLY|O_APPEND,NIL)) < 0) ||
1090
!(df = fdopen (fd,"ab"))) {
1091
sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
1095
/* get parse/append permission */
1096
if (flock (fd,LOCK_SH) || ((ld = lockfd (fd,lock,LOCK_EX)) < 0)) {
1097
MM_LOG ("Unable to lock append mailbox",ERROR);
1101
MM_CRITICAL (stream); /* go critical */
1102
fstat (fd,&sbuf); /* get current file size */
1104
do { /* parse flags */
1105
if (!SIZE (message)) { /* guard against zero-length */
1106
MM_LOG ("Append of zero-length message",ERROR);
1110
f = mail_parse_flags (stream,flags,&i);
1111
/* reverse bits (dontcha wish we had CIRC?) */
1112
for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));
1113
if (date) { /* parse date if given */
1114
if (!mail_parse_date (&elt,date)) {
1115
sprintf (tmp,"Bad date in append: %.80s",date);
1117
ret = NIL; /* mark failure */
1120
mail_date (tmp,&elt); /* write preseved date */
1122
else internal_date (tmp); /* get current date in IMAP format */
1123
i = GETPOS (message); /* remember current position */
1124
for (j = SIZE (message), size = 0; j; --j)
1125
if (SNX (message) != '\015') ++size;
1126
SETPOS (message,i); /* restore position */
1128
if (fprintf (df,"%s,%lu;%010lo%02lo\n",tmp,size,uf,(unsigned long) f) < 0)
1130
else { /* write message */
1131
while (size) if ((c = 0xff & SNX (message)) != '\015') {
1132
if (putc (c,df) != EOF) --size;
1135
/* get next message */
1136
if (size || !MM_APPEND (af) (stream,data,&flags,&date,&message))
1139
} while (ret && message);
1141
if (!ret || (fflush (df) == EOF)) {
1142
ftruncate (fd,sbuf.st_size);/* revert file */
1143
close (fd); /* make sure fclose() doesn't corrupt us */
1145
sprintf (tmp,"Message append failed: %s",strerror (errno));
1150
if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */
1151
/* else preserve \Marked status */
1152
else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
1153
tp[1] = sbuf.st_mtime; /* preserve mtime */
1154
utime (file,tp); /* set the times */
1155
fclose (df); /* close the file */
1156
unlockfd (ld,lock); /* release exclusive parse/append permission */
1157
MM_NOCRITICAL (stream); /* release critical */
1158
if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))
1159
mm_log ("Can not return meaningful APPENDUID with this mailbox format",
1164
/* Internal routines */
1167
/* Tenex mail return internal message size in bytes
1168
* Accepts: MAIL stream
1170
* Returns: internal size of message
1173
unsigned long tenex_size (MAILSTREAM *stream,unsigned long m)
1175
MESSAGECACHE *elt = mail_elt (stream,m);
1176
return ((m < stream->nmsgs) ? mail_elt (stream,m+1)->private.special.offset :
1178
(elt->private.special.offset + elt->private.special.text.size);
1182
/* Tenex mail generate file string
1183
* Accepts: temporary buffer to write into
1184
* mailbox name string
1185
* Returns: local file string or NIL if failure
1188
char *tenex_file (char *dst,char *name)
1190
char tmp[MAILTMPLEN];
1191
char *s = mailboxfile (dst,name);
1192
/* return our standard inbox */
1193
return (s && !*s) ? mailboxfile (dst,tenex_isvalid ("~/INBOX",tmp) ?
1194
"~/INBOX" : "mail.txt") : s;
1197
/* Tenex mail parse mailbox
1198
* Accepts: MAIL stream
1199
* Returns: T if parse OK
1200
* NIL if failure, stream aborted
1203
long tenex_parse (MAILSTREAM *stream)
1206
MESSAGECACHE *elt = NIL;
1207
unsigned char c,*s,*t,*x;
1208
char tmp[MAILTMPLEN];
1210
long curpos = LOCAL->filesize;
1211
long nmsgs = stream->nmsgs;
1212
long recent = stream->recent;
1214
short silent = stream->silent;
1215
fstat (LOCAL->fd,&sbuf); /* get status */
1216
if (sbuf.st_size < curpos) { /* sanity check */
1217
sprintf (tmp,"Mailbox shrank from %lu to %lu!",
1218
(unsigned long) curpos,(unsigned long) sbuf.st_size);
1220
tenex_close (stream,NIL);
1223
stream->silent = T; /* don't pass up mm_exists() events yet */
1224
while (sbuf.st_size - curpos){/* while there is stuff to parse */
1225
/* get to that position in the file */
1226
lseek (LOCAL->fd,curpos,L_SET);
1227
if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
1228
sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
1229
(unsigned long) curpos,(unsigned long) sbuf.st_size,
1230
i ? strerror (errno) : "no data read");
1232
tenex_close (stream,NIL);
1235
LOCAL->buf[i] = '\0'; /* tie off buffer just in case */
1236
if (!(s = strchr (LOCAL->buf,'\012'))) {
1237
sprintf (tmp,"Unable to find newline at %lu in %lu bytes, text: %s",
1238
(unsigned long) curpos,i,(char *) LOCAL->buf);
1240
tenex_close (stream,NIL);
1243
*s = '\0'; /* tie off header line */
1244
i = (s + 1) - LOCAL->buf; /* note start of text offset */
1245
if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
1246
sprintf (tmp,"Unable to parse internal header at %lu: %s",
1247
(unsigned long) curpos,(char *) LOCAL->buf);
1249
tenex_close (stream,NIL);
1252
*s++ = '\0'; *t++ = '\0'; /* tie off fields */
1254
added = T; /* note that a new message was added */
1255
/* swell the cache */
1256
mail_exists (stream,++nmsgs);
1257
/* instantiate an elt for this message */
1258
(elt = mail_elt (stream,nmsgs))->valid = T;
1259
elt->private.uid = ++stream->uid_last;
1260
/* note file offset of header */
1261
elt->private.special.offset = curpos;
1263
elt->private.special.text.size = 0;
1264
/* header size not known yet */
1265
elt->private.msg.header.text.size = 0;
1266
x = s; /* parse the header components */
1267
if (mail_parse_date (elt,LOCAL->buf) &&
1268
(elt->private.msg.full.text.size = strtoul (s,(char **) &s,10)) &&
1269
(!(s && *s)) && isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&
1270
isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&
1271
isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&
1272
isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])
1273
elt->private.special.text.size = i;
1275
sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",
1276
curpos,(char *) LOCAL->buf,(char *) x,(char *) t);
1278
tenex_close (stream,NIL);
1281
/* make sure didn't run off end of file */
1282
if ((curpos += (elt->private.msg.full.text.size + i)) > sbuf.st_size) {
1283
sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
1284
elt->private.special.offset,(unsigned long) curpos,
1285
(unsigned long) sbuf.st_size);
1287
tenex_close (stream,NIL);
1290
c = t[10]; /* remember first system flags byte */
1291
t[10] = '\0'; /* tie off flags */
1292
j = strtoul (t,NIL,8); /* get user flags value */
1293
t[10] = c; /* restore first system flags byte */
1294
/* set up all valid user flags (reversed!) */
1295
while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1296
stream->user_flags[i]) elt->user_flags |= 1 << i;
1297
/* calculate system flags */
1298
if ((j = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;
1299
if (j & fDELETED) elt->deleted = T;
1300
if (j & fFLAGGED) elt->flagged = T;
1301
if (j & fANSWERED) elt->answered = T;
1302
if (j & fDRAFT) elt->draft = T;
1303
if (!(j & fOLD)) { /* newly arrived message? */
1305
recent++; /* count up a new recent message */
1306
/* mark it as old */
1307
tenex_update_status (stream,nmsgs,NIL);
1310
fsync (LOCAL->fd); /* make sure all the fOLD flags take */
1311
/* update parsed file size and time */
1312
LOCAL->filesize = sbuf.st_size;
1313
fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */
1314
LOCAL->filetime = sbuf.st_mtime;
1315
if (added && !stream->rdonly){/* make sure atime updated */
1318
tp[1] = LOCAL->filetime;
1319
utime (stream->mailbox,tp);
1321
stream->silent = silent; /* can pass up events now */
1322
mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */
1323
mail_recent (stream,recent); /* and of change in recent messages */
1324
return LONGT; /* return the winnage */
1327
/* Tenex get cache element with status updating from file
1328
* Accepts: MAIL stream
1330
* Returns: cache element
1333
MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno)
1335
MESSAGECACHE *elt = mail_elt (stream,msgno);
1336
struct { /* old flags */
1337
unsigned int seen : 1;
1338
unsigned int deleted : 1;
1339
unsigned int flagged : 1;
1340
unsigned int answered : 1;
1341
unsigned int draft : 1;
1342
unsigned long user_flags;
1344
old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
1345
old.answered = elt->answered; old.draft = elt->draft;
1346
old.user_flags = elt->user_flags;
1347
tenex_read_flags (stream,elt);
1348
if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
1349
(old.flagged != elt->flagged) || (old.answered != elt->answered) ||
1350
(old.draft != elt->draft) || (old.user_flags != elt->user_flags))
1351
MM_FLAGS (stream,msgno); /* let top level know */
1355
/* Tenex read flags from file
1356
* Accepts: MAIL stream
1357
* Returns: cache element
1360
void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
1363
/* noop if readonly and have valid flags */
1364
if (stream->rdonly && elt->valid) return;
1365
/* set the seek pointer */
1366
lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1367
elt->private.special.text.size - 13,L_SET);
1368
/* read the new flags */
1369
if (read (LOCAL->fd,LOCAL->buf,12) < 0) {
1370
sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
1373
/* calculate system flags */
1374
i = (((LOCAL->buf[10]-'0') * 8) + LOCAL->buf[11]-'0');
1375
elt->seen = i & fSEEN ? T : NIL; elt->deleted = i & fDELETED ? T : NIL;
1376
elt->flagged = i & fFLAGGED ? T : NIL;
1377
elt->answered = i & fANSWERED ? T : NIL; elt->draft = i & fDRAFT ? T : NIL;
1378
LOCAL->buf[10] = '\0'; /* tie off flags */
1379
j = strtoul(LOCAL->buf,NIL,8);/* get user flags value */
1380
/* set up all valid user flags (reversed!) */
1381
while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1382
stream->user_flags[i]) elt->user_flags |= 1 << i;
1383
elt->valid = T; /* have valid flags now */
1386
/* Tenex update status string
1387
* Accepts: MAIL stream
1389
* flag saying whether or not to sync
1392
void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
1396
MESSAGECACHE *elt = mail_elt (stream,msgno);
1397
unsigned long j,k = 0;
1399
if (stream->rdonly || !elt->valid) tenex_read_flags (stream,elt);
1400
else { /* readwrite */
1401
j = elt->user_flags; /* get user flags */
1402
/* reverse bits (dontcha wish we had CIRC?) */
1403
while (j) k |= 1 << (29 - find_rightmost_bit (&j));
1404
/* print new flag string */
1405
sprintf (LOCAL->buf,"%010lo%02o",k,(unsigned)
1406
(fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
1407
(fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
1408
(fDRAFT * elt->draft)));
1409
/* get to that place in the file */
1410
lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1411
elt->private.special.text.size - 13,L_SET);
1412
/* write new flags */
1413
write (LOCAL->fd,LOCAL->buf,12);
1414
if (syncflag) { /* sync if requested */
1416
fstat (LOCAL->fd,&sbuf); /* get new write time */
1417
tp[1] = LOCAL->filetime = sbuf.st_mtime;
1418
tp[0] = time (0); /* make sure read is later */
1419
utime (stream->mailbox,tp);
1424
/* Tenex locate header for a message
1425
* Accepts: MAIL stream
1427
* pointer to returned header size
1428
* Returns: position of header in file
1431
unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
1432
unsigned long *size)
1438
MESSAGECACHE *elt = tenex_elt (stream,msgno);
1439
unsigned long ret = elt->private.special.offset +
1440
elt->private.special.text.size;
1441
unsigned long msiz = tenex_size (stream,msgno);
1442
/* is header size known? */
1443
if (!(*size = elt->private.msg.header.text.size)) {
1444
lseek (LOCAL->fd,ret,L_SET);/* get to header position */
1445
/* search message for LF LF */
1446
for (siz = 0; siz < msiz; siz++) {
1447
if (--i <= 0) /* read another buffer as necessary */
1448
read (LOCAL->fd,s = LOCAL->buf,i = min (msiz-siz,(long) MAILTMPLEN));
1449
/* two newline sequence? */
1450
if ((c == '\012') && (*s == '\012')) {
1451
/* yes, note for later */
1452
elt->private.msg.header.text.size = (*size = siz + 1);
1454
return ret; /* return to caller */
1456
else c = *s++; /* next character */
1458
/* header consumes entire message */
1459
elt->private.msg.header.text.size = *size = msiz;