imapext-2007

view src/osdep/os2/tenexnt.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line source
1 /* ========================================================================
2 * Copyright 1988-2007 University of Washington
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: Tenex mail routines
16 *
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
24 *
25 * Date: 22 May 1990
26 * Last Edited: 18 June 2007
27 */
30 /* FILE TIME SEMANTICS
31 *
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.
35 *
36 * TEXT SIZE SEMANTICS
37 *
38 * Most of the text sizes are in internal (LF-only) form, except for the
39 * msg.text size. Beware.
40 */
42 #include <stdio.h>
43 #include <ctype.h>
44 #include <errno.h>
45 extern int errno; /* just in case */
46 #include "mail.h"
47 #include "osdep.h"
48 #include <fcntl.h>
49 #include <time.h>
50 #include <sys/stat.h>
51 #include <sys/utime.h>
52 #include "misc.h"
53 #include "dummy.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 */
68 } TENEXLOCAL;
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,
109 long syncflag);
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 */
156 };
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
164 */
166 DRIVER *tenex_valid (char *name)
167 {
168 char tmp[MAILTMPLEN];
169 return tenex_isvalid (name,tmp) ? &tenexdriver : NIL;
170 }
173 /* Tenex mail test for valid mailbox
174 * Accepts: mailbox name
175 * buffer to return file name
176 * Returns: T if valid, NIL otherwise
177 */
179 int tenex_isvalid (char *name,char *file)
180 {
181 int fd;
182 int ret = NIL;
183 char *s,tmp[MAILTMPLEN];
184 struct stat sbuf;
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;
200 }
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,&times); /* set the times */
209 }
210 }
211 }
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 */
215 }
218 /* Tenex manipulate driver parameters
219 * Accepts: function code
220 * function-dependent value
221 * Returns: function-dependent return value
222 */
224 void *tenex_parameters (long function,void *value)
225 {
226 return NIL;
227 }
229 /* Tenex mail scan mailboxes
230 * Accepts: mail stream
231 * reference
232 * pattern to search
233 * string to scan
234 */
236 void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
237 {
238 if (stream) dummy_scan (NIL,ref,pat,contents);
239 }
242 /* Tenex mail list mailboxes
243 * Accepts: mail stream
244 * reference
245 * pattern to search
246 */
248 void tenex_list (MAILSTREAM *stream,char *ref,char *pat)
249 {
250 if (stream) dummy_list (NIL,ref,pat);
251 }
254 /* Tenex mail list subscribed mailboxes
255 * Accepts: mail stream
256 * reference
257 * pattern to search
258 */
260 void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat)
261 {
262 if (stream) dummy_lsub (NIL,ref,pat);
263 }
265 /* Tenex mail create mailbox
266 * Accepts: MAIL stream
267 * mailbox name to create
268 * Returns: T on success, NIL on failure
269 */
271 long tenex_create (MAILSTREAM *stream,char *mailbox)
272 {
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);
276 mm_log (mbx,ERROR);
277 return NIL;
278 }
281 /* Tenex mail delete mailbox
282 * Accepts: MAIL stream
283 * mailbox name to delete
284 * Returns: T on success, NIL on failure
285 */
287 long tenex_delete (MAILSTREAM *stream,char *mailbox)
288 {
289 return tenex_rename (stream,mailbox,NIL);
290 }
292 /* Tenex mail rename mailbox
293 * Accepts: MAIL stream
294 * old mailbox name
295 * new mailbox name (or NIL for delete)
296 * Returns: T on success, NIL on failure
297 */
299 long tenex_rename (MAILSTREAM *stream,char *old,char *newname)
300 {
301 long ret = LONGT;
302 char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
303 int fd,ld;
304 struct stat sbuf;
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",
311 old,newname);
312 mm_log (tmp,ERROR);
313 return NIL;
314 }
315 else if ((fd = open (file,O_BINARY|O_RDWR,NIL)) < 0) {
316 sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
317 mm_log (tmp,ERROR);
318 return NIL;
319 }
320 /* get exclusive parse/append permission */
321 if ((ld = lockname (lock,file,LOCK_EX)) < 0) {
322 mm_log ("Unable to lock rename mailbox",ERROR);
323 return NIL;
324 }
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);
329 mm_log (tmp,ERROR);
330 unlockfd (ld,lock); /* release exclusive parse/append permission */
331 return NIL;
332 }
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;
344 }
345 else *s = '\\'; /* restore delimiter */
346 s[1] = c; /* restore character after delimiter */
347 }
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,
353 strerror (errno));
354 mm_log (tmp,ERROR);
355 ret = NIL; /* set failure */
356 }
357 }
358 else {
359 flock (fd,LOCK_UN); /* release lock on the file */
360 close (fd); /* pacify NTFS */
361 if (unlink (file)) {
362 sprintf (tmp,"Can't delete mailbox %.80s: %.80s",old,strerror (errno));
363 mm_log (tmp,ERROR);
364 ret = NIL; /* set failure */
365 }
366 }
367 unlockfd (ld,lock); /* release exclusive parse/append permission */
368 return ret; /* return success */
369 }
371 /* Tenex mail open
372 * Accepts: stream to open
373 * Returns: stream on success, NIL on failure
374 */
376 MAILSTREAM *tenex_open (MAILSTREAM *stream)
377 {
378 int fd,ld;
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);
386 mm_log (tmp,ERROR);
387 }
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));
392 mm_log (tmp,ERROR);
393 return NIL;
394 }
395 else if (!stream->rdonly) { /* got it, but readonly */
396 mm_log ("Can't get write access to mailbox, access is readonly",WARN);
397 stream->rdonly = T;
398 }
399 }
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);
413 return NIL;
414 }
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);
422 /* parse mailbox */
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 */
432 }
434 /* Tenex mail close
435 * Accepts: MAIL stream
436 * close options
437 */
439 void tenex_close (MAILSTREAM *stream,long options)
440 {
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 */
454 }
455 }
457 /* Tenex mail fetch flags
458 * Accepts: MAIL stream
459 * sequence
460 * option flags
461 * Sniffs at file to get flags
462 */
464 void tenex_flags (MAILSTREAM *stream,char *sequence,long flags)
465 {
466 STRING bs;
467 MESSAGECACHE *elt;
468 unsigned long i;
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);
482 }
483 /* tie off string */
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);
491 }
492 tenex_elt (stream,i); /* get current flags from file */
493 }
494 }
496 /* TENEX mail fetch message header
497 * Accepts: MAIL stream
498 * message # to fetch
499 * pointer to returned header text length
500 * option flags
501 * Returns: message header in RFC822 format
502 */
504 char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
505 unsigned long *length,long flags)
506 {
507 char *s;
508 unsigned long i;
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);
517 }
518 /* slurp the data */
519 read (LOCAL->fd,LOCAL->buf,*length = i);
520 }
521 else {
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 */
528 }
529 return LOCAL->buf;
530 }
532 /* TENEX mail fetch message text (body only)
533 * Accepts: MAIL stream
534 * message # to fetch
535 * pointer to returned stringstruct
536 * option flags
537 * Returns: T, always
538 */
540 long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
541 {
542 char *s;
543 unsigned long i,j;
544 MESSAGECACHE *elt;
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);
555 }
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);
562 }
563 /* go to text position */
564 lseek (LOCAL->fd,i + j,L_SET);
565 /* slurp the data */
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);
569 }
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 */
586 }
587 /* set up stringstruct */
588 INIT (bs,mail_string,LOCAL->text.data,i);
589 }
590 return T; /* success */
591 }
593 /* Tenex mail modify flags
594 * Accepts: MAIL stream
595 * sequence
596 * flag(s)
597 * option flags
598 */
600 void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
601 {
602 struct utimbuf times;
603 struct stat sbuf;
604 if (!stream->rdonly) { /* make sure the update takes */
605 fsync (LOCAL->fd);
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,&times);
610 }
611 }
614 /* Tenex mail per-message modify flags
615 * Accepts: MAIL stream
616 * message cache element
617 */
619 void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
620 {
621 struct stat sbuf;
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 */
627 }
628 /* recalculate status */
629 tenex_update_status (stream,elt->msgno,NIL);
630 }
632 /* Tenex mail ping mailbox
633 * Accepts: MAIL stream
634 * Returns: T if stream still alive, NIL if not
635 */
637 long tenex_ping (MAILSTREAM *stream)
638 {
639 unsigned long i = 1;
640 long r = T;
641 int ld;
642 char lock[MAILTMPLEN];
643 struct stat sbuf;
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;
655 }
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 */
662 }
663 }
664 return r; /* return result of the parse */
665 }
668 /* Tenex mail check mailbox (reparses status too)
669 * Accepts: MAIL stream
670 */
672 void tenex_check (MAILSTREAM *stream)
673 {
674 /* mark that a check is desired */
675 if (LOCAL) LOCAL->mustcheck = T;
676 if (tenex_ping (stream)) mm_log ("Check completed",(long) NIL);
677 }
679 /* Tenex mail expunge mailbox
680 * sequence to expunge if non-NIL
681 * expunge options
682 * Returns: T, always
683 */
685 long tenex_expunge (MAILSTREAM *stream,char *sequence,long options)
686 {
687 long ret;
688 struct utimbuf times;
689 struct stat sbuf;
690 off_t pos = 0;
691 int ld;
692 unsigned long i = 1;
693 unsigned long j,k,m,recent;
694 unsigned long n = 0;
695 unsigned long delta = 0;
696 char lock[MAILTMPLEN];
697 MESSAGECACHE *elt;
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);
703 else {
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;
707 }
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",
717 ERROR);
718 unlockfd (ld,lock); /* release exclusive parse/append permission */
719 }
721 else {
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 */
738 }
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 */
747 while (T) {
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);
752 }
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;
758 }
759 /* preserved but no deleted messages */
760 else pos = elt->private.special.offset + k;
761 }
763 if (n) { /* truncate file after last message */
764 if (pos != (LOCAL->filesize -= delta)) {
765 sprintf (LOCAL->buf,
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 */
770 }
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);
775 }
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,&times);
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 */
788 }
789 }
790 return ret;
791 }
793 /* Tenex mail copy message(s)
794 * Accepts: MAIL stream
795 * sequence
796 * destination mailbox
797 * copy options
798 * Returns: T if success, NIL if failed
799 */
801 long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
802 {
803 struct stat sbuf;
804 struct utimbuf times;
805 MESSAGECACHE *elt;
806 unsigned long i,j,k;
807 long ret = LONGT;
808 int fd,ld;
809 char file[MAILTMPLEN],lock[MAILTMPLEN];
810 mailproxycopy_t pc =
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);
816 return NIL;
817 case 0: /* merely empty file? */
818 break;
819 case EACCES: /* file protected */
820 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
821 MM_LOG (LOCAL->buf,ERROR);
822 return NIL;
823 case EINVAL:
824 if (pc) return (*pc) (stream,sequence,mailbox,options);
825 sprintf (LOCAL->buf,"Invalid Tenex-format mailbox name: %.80s",mailbox);
826 mm_log (LOCAL->buf,ERROR);
827 return NIL;
828 default:
829 if (pc) return (*pc) (stream,sequence,mailbox,options);
830 sprintf (LOCAL->buf,"Not a Tenex-format mailbox: %.80s",mailbox);
831 mm_log (LOCAL->buf,ERROR);
832 return NIL;
833 }
834 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
835 mail_sequence (stream,sequence))) return NIL;
836 /* got file? */
837 if ((fd = open (file,O_BINARY|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)) < 0) {
838 sprintf (LOCAL->buf,"Unable to open copy mailbox: %.80s",strerror (errno));
839 mm_log (LOCAL->buf,ERROR);
840 return NIL;
841 }
842 mm_critical (stream); /* go critical */
843 /* get exclusive parse/append permission */
844 if (flock (fd,LOCK_SH) || ((ld = lockname (lock,file,LOCK_EX)) < 0)) {
845 mm_log ("Unable to lock copy mailbox",ERROR);
846 mm_nocritical (stream);
847 return NIL;
848 }
849 fstat (fd,&sbuf); /* get current file size */
850 lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
852 /* for each requested message */
853 for (i = 1; ret && (i <= stream->nmsgs); i++)
854 if ((elt = mail_elt (stream,i))->sequence) {
855 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
856 /* number of bytes to copy */
857 k = elt->private.special.text.size + tenex_size (stream,i);
858 do { /* read from source position */
859 j = min (k,LOCAL->buflen);
860 read (LOCAL->fd,LOCAL->buf,j);
861 if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
862 } while (ret && (k -= j));/* until done */
863 }
864 /* delete all requested messages */
865 if (ret && (options & CP_MOVE)) {
866 sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
867 mm_log (LOCAL->buf,ERROR);
868 ftruncate (fd,sbuf.st_size);
869 }
870 /* set atime to now-1 if successful copy */
871 if (ret) times.actime = time (0) - 1;
872 /* else preserved \Marked status */
873 else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
874 sbuf.st_atime : time (0);
875 times.modtime = sbuf.st_mtime;/* preserve mtime */
876 utime (file,&times); /* set the times */
877 unlockfd (ld,lock); /* release exclusive parse/append permission */
878 close (fd); /* close the file */
879 mm_nocritical (stream); /* release critical */
880 /* delete all requested messages */
881 if (ret && (options & CP_MOVE)) {
882 for (i = 1; i <= stream->nmsgs; i++)
883 if ((elt = tenex_elt (stream,i))->sequence) {
884 elt->deleted = T; /* mark message deleted */
885 /* recalculate status */
886 tenex_update_status (stream,i,NIL);
887 }
888 if (!stream->rdonly) { /* make sure the update takes */
889 fsync (LOCAL->fd);
890 fstat (LOCAL->fd,&sbuf); /* get current write time */
891 times.modtime = LOCAL->filetime = sbuf.st_mtime;
892 times.actime = time (0); /* make sure atime remains greater */
893 utime (stream->mailbox,&times);
894 }
895 }
896 if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
897 mm_log ("Can not return meaningful COPYUID with this mailbox format",WARN);
898 return ret;
899 }
901 /* Tenex mail append message from stringstruct
902 * Accepts: MAIL stream
903 * destination mailbox
904 * append callback
905 * data for callback
906 * Returns: T if append successful, else NIL
907 */
909 long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
910 {
911 struct stat sbuf;
912 int fd,ld,c;
913 char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
914 struct utimbuf times;
915 FILE *df;
916 MESSAGECACHE elt;
917 long f;
918 unsigned long i,j,uf,size;
919 STRING *message;
920 long ret = LONGT;
921 /* default stream to prototype */
922 if (!stream) stream = &tenexproto;
923 /* make sure valid mailbox */
924 if (!tenex_isvalid (mailbox,file)) switch (errno) {
925 case ENOENT: /* no such file? */
926 if (!compare_cstring (mailbox,"INBOX")) tenex_create (NIL,"INBOX");
927 else {
928 mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
929 return NIL;
930 }
931 /* falls through */
932 case 0: /* merely empty file? */
933 break;
934 case EACCES: /* file protected */
935 sprintf (tmp,"Can't access destination: %.80s",mailbox);
936 MM_LOG (tmp,ERROR);
937 return NIL;
938 case EINVAL:
939 sprintf (tmp,"Invalid TENEX-format mailbox name: %.80s",mailbox);
940 mm_log (tmp,ERROR);
941 return NIL;
942 default:
943 sprintf (tmp,"Not a TENEX-format mailbox: %.80s",mailbox);
944 mm_log (tmp,ERROR);
945 return NIL;
946 }
947 /* get first message */
948 if (!(*af) (stream,data,&flags,&date,&message)) return NIL;
950 /* open destination mailbox */
951 if (((fd = open (file,O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IREAD|S_IWRITE))
952 < 0) || !(df = fdopen (fd,"ab"))) {
953 sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
954 mm_log (tmp,ERROR);
955 return NIL;
956 }
957 /* get parse/append permission */
958 if (flock (fd,LOCK_SH) || ((ld = lockname (lock,file,LOCK_EX)) < 0)) {
959 mm_log ("Unable to lock append mailbox",ERROR);
960 close (fd);
961 return NIL;
962 }
963 mm_critical (stream); /* go critical */
964 fstat (fd,&sbuf); /* get current file size */
965 errno = 0;
966 do { /* parse flags */
967 if (!SIZE (message)) { /* guard against zero-length */
968 mm_log ("Append of zero-length message",ERROR);
969 ret = NIL;
970 break;
971 }
972 f = mail_parse_flags (stream,flags,&i);
973 /* reverse bits (dontcha wish we had CIRC?) */
974 for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));
975 if (date) { /* parse date if given */
976 if (!mail_parse_date (&elt,date)) {
977 sprintf (tmp,"Bad date in append: %.80s",date);
978 mm_log (tmp,ERROR);
979 ret = NIL; /* mark failure */
980 break;
981 }
982 mail_date (tmp,&elt); /* write preseved date */
983 }
984 else internal_date (tmp); /* get current date in IMAP format */
985 i = GETPOS (message); /* remember current position */
986 for (j = SIZE (message), size = 0; j; --j)
987 if (SNX (message) != '\015') ++size;
988 SETPOS (message,i); /* restore position */
989 /* write header */
990 if (fprintf (df,"%s,%lu;%010lo%02lo\n",tmp,size,uf,(unsigned long) f) < 0)
991 ret = NIL;
992 else { /* write message */
993 while (size) if ((c = 0xff & SNX (message)) != '\015') {
994 if (putc (c,df) != EOF) --size;
995 else break;
996 }
997 /* get next message */
998 if (size || !(*af) (stream,data,&flags,&date,&message)) ret = NIL;
999 }
1000 } while (ret && message);
1001 /* if error... */
1002 if (!ret || (fflush (df) == EOF)) {
1003 ftruncate (fd,sbuf.st_size);/* revert file */
1004 close (fd); /* make sure fclose() doesn't corrupt us */
1005 if (errno) {
1006 sprintf (tmp,"Message append failed: %s",strerror (errno));
1007 mm_log (tmp,ERROR);
1009 ret = NIL;
1011 if (ret) times.actime = time (0) - 1;
1012 /* else preserved \Marked status */
1013 else times.actime = (sbuf.st_ctime > sbuf.st_atime) ?
1014 sbuf.st_atime : time (0);
1015 times.modtime = sbuf.st_mtime;/* preserve mtime */
1016 utime (file,&times); /* set the times */
1017 fclose (df); /* close the file */
1018 unlockfd (ld,lock); /* release exclusive parse/append permission */
1019 mm_nocritical (stream); /* release critical */
1020 if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))
1021 mm_log ("Can not return meaningful APPENDUID with this mailbox format",
1022 WARN);
1023 return ret;
1026 /* Internal routines */
1029 /* Tenex mail return internal message size in bytes
1030 * Accepts: MAIL stream
1031 * message #
1032 * Returns: internal size of message
1033 */
1035 unsigned long tenex_size (MAILSTREAM *stream,unsigned long m)
1037 MESSAGECACHE *elt = mail_elt (stream,m);
1038 return ((m < stream->nmsgs) ? mail_elt (stream,m+1)->private.special.offset :
1039 LOCAL->filesize) -
1040 (elt->private.special.offset + elt->private.special.text.size);
1043 /* Tenex mail parse mailbox
1044 * Accepts: MAIL stream
1045 * Returns: T if parse OK
1046 * NIL if failure, stream aborted
1047 */
1049 long tenex_parse (MAILSTREAM *stream)
1051 struct stat sbuf;
1052 MESSAGECACHE *elt = NIL;
1053 unsigned char c,*s,*t,*x;
1054 char tmp[MAILTMPLEN];
1055 unsigned long i,j;
1056 long curpos = LOCAL->filesize;
1057 long nmsgs = stream->nmsgs;
1058 long recent = stream->recent;
1059 short added = NIL;
1060 short silent = stream->silent;
1061 fstat (LOCAL->fd,&sbuf); /* get status */
1062 if (sbuf.st_size < curpos) { /* sanity check */
1063 sprintf (tmp,"Mailbox shrank from %ld to %ld!",curpos,sbuf.st_size);
1064 mm_log (tmp,ERROR);
1065 tenex_close (stream,NIL);
1066 return NIL;
1068 stream->silent = T; /* don't pass up mm_exists() events yet */
1069 while (sbuf.st_size - curpos){/* while there is stuff to parse */
1070 /* get to that position in the file */
1071 lseek (LOCAL->fd,curpos,L_SET);
1072 if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
1073 sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
1074 (unsigned long) curpos,(unsigned long) sbuf.st_size,
1075 i ? strerror (errno) : "no data read");
1076 mm_log (tmp,ERROR);
1077 tenex_close (stream,NIL);
1078 return NIL;
1080 LOCAL->buf[i] = '\0'; /* tie off buffer just in case */
1081 if (!(s = strchr (LOCAL->buf,'\012'))) {
1082 sprintf (tmp,"Unable to find newline at %lu in %lu bytes, text: %s",
1083 (unsigned long) curpos,i,(char *) LOCAL->buf);
1084 mm_log (tmp,ERROR);
1085 tenex_close (stream,NIL);
1086 return NIL;
1088 *s = '\0'; /* tie off header line */
1089 i = (s + 1) - LOCAL->buf; /* note start of text offset */
1090 if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
1091 sprintf (tmp,"Unable to parse internal header at %lu: %s",
1092 (unsigned long) curpos,(char *) LOCAL->buf);
1093 mm_log (tmp,ERROR);
1094 tenex_close (stream,NIL);
1095 return NIL;
1097 *s++ = '\0'; *t++ = '\0'; /* tie off fields */
1099 added = T; /* note that a new message was added */
1100 /* swell the cache */
1101 mail_exists (stream,++nmsgs);
1102 /* instantiate an elt for this message */
1103 (elt = mail_elt (stream,nmsgs))->valid = T;
1104 elt->private.uid = ++stream->uid_last;
1105 /* note file offset of header */
1106 elt->private.special.offset = curpos;
1107 /* in case error */
1108 elt->private.special.text.size = 0;
1109 /* header size not known yet */
1110 elt->private.msg.header.text.size = 0;
1111 x = s; /* parse the header components */
1112 if (mail_parse_date (elt,LOCAL->buf) &&
1113 (elt->private.msg.full.text.size = strtoul (s,(char **) &s,10)) &&
1114 (!(s && *s)) && isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&
1115 isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&
1116 isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&
1117 isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])
1118 elt->private.special.text.size = i;
1119 else { /* oops */
1120 sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",
1121 curpos,(char *) LOCAL->buf,(char *) x,(char *) t);
1122 mm_log (tmp,ERROR);
1123 tenex_close (stream,NIL);
1124 return NIL;
1126 /* make sure didn't run off end of file */
1127 if ((curpos += (elt->private.msg.full.text.size + i)) > sbuf.st_size) {
1128 sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
1129 elt->private.special.offset,(unsigned long) curpos,
1130 (unsigned long) sbuf.st_size);
1131 mm_log (tmp,ERROR);
1132 tenex_close (stream,NIL);
1133 return NIL;
1135 c = t[10]; /* remember first system flags byte */
1136 t[10] = '\0'; /* tie off flags */
1137 j = strtoul (t,NIL,8); /* get user flags value */
1138 t[10] = c; /* restore first system flags byte */
1139 /* set up all valid user flags (reversed!) */
1140 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1141 stream->user_flags[i]) elt->user_flags |= 1 << i;
1142 /* calculate system flags */
1143 if ((j = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;
1144 if (j & fDELETED) elt->deleted = T;
1145 if (j & fFLAGGED) elt->flagged = T;
1146 if (j & fANSWERED) elt->answered = T;
1147 if (j & fDRAFT) elt->draft = T;
1148 if (!(j & fOLD)) { /* newly arrived message? */
1149 elt->recent = T;
1150 recent++; /* count up a new recent message */
1151 /* mark it as old */
1152 tenex_update_status (stream,nmsgs,NIL);
1155 fsync (LOCAL->fd); /* make sure all the fOLD flags take */
1156 /* update parsed file size and time */
1157 LOCAL->filesize = sbuf.st_size;
1158 fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */
1159 LOCAL->filetime = sbuf.st_mtime;
1160 if (added && !stream->rdonly){/* make sure atime updated */
1161 struct utimbuf times;
1162 times.actime = time (0);
1163 times.modtime = LOCAL->filetime;
1164 utime (stream->mailbox,&times);
1166 stream->silent = silent; /* can pass up events now */
1167 mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */
1168 mail_recent (stream,recent); /* and of change in recent messages */
1169 return LONGT; /* return the winnage */
1172 /* Tenex get cache element with status updating from file
1173 * Accepts: MAIL stream
1174 * message number
1175 * Returns: cache element
1176 */
1178 MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno)
1180 MESSAGECACHE *elt = mail_elt (stream,msgno);
1181 struct { /* old flags */
1182 unsigned int seen : 1;
1183 unsigned int deleted : 1;
1184 unsigned int flagged : 1;
1185 unsigned int answered : 1;
1186 unsigned int draft : 1;
1187 unsigned long user_flags;
1188 } old;
1189 old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
1190 old.answered = elt->answered; old.draft = elt->draft;
1191 old.user_flags = elt->user_flags;
1192 tenex_read_flags (stream,elt);
1193 if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
1194 (old.flagged != elt->flagged) || (old.answered != elt->answered) ||
1195 (old.draft != elt->draft) || (old.user_flags != elt->user_flags))
1196 mm_flags (stream,msgno); /* let top level know */
1197 return elt;
1201 /* Tenex read flags from file
1202 * Accepts: MAIL stream
1203 * Returns: cache element
1204 */
1206 void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
1208 unsigned long i,j;
1209 /* noop if readonly and have valid flags */
1210 if (stream->rdonly && elt->valid) return;
1211 /* set the seek pointer */
1212 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1213 elt->private.special.text.size - 13,L_SET);
1214 /* read the new flags */
1215 if (read (LOCAL->fd,LOCAL->buf,12) < 0) {
1216 sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
1217 fatal (LOCAL->buf);
1219 /* calculate system flags */
1220 i = (((LOCAL->buf[10]-'0') * 8) + LOCAL->buf[11]-'0');
1221 elt->seen = i & fSEEN ? T : NIL; elt->deleted = i & fDELETED ? T : NIL;
1222 elt->flagged = i & fFLAGGED ? T : NIL;
1223 elt->answered = i & fANSWERED ? T : NIL; elt->draft = i & fDRAFT ? T : NIL;
1224 LOCAL->buf[10] = '\0'; /* tie off flags */
1225 j = strtoul(LOCAL->buf,NIL,8);/* get user flags value */
1226 /* set up all valid user flags (reversed!) */
1227 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1228 stream->user_flags[i]) elt->user_flags |= 1 << i;
1229 elt->valid = T; /* have valid flags now */
1232 /* Tenex update status string
1233 * Accepts: MAIL stream
1234 * message number
1235 * flag saying whether or not to sync
1236 */
1238 void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
1240 struct utimbuf times;
1241 struct stat sbuf;
1242 MESSAGECACHE *elt = mail_elt (stream,msgno);
1243 unsigned long j,k = 0;
1244 /* readonly */
1245 if (stream->rdonly || !elt->valid) tenex_read_flags (stream,elt);
1246 else { /* readwrite */
1247 j = elt->user_flags; /* get user flags */
1248 /* reverse bits (dontcha wish we had CIRC?) */
1249 while (j) k |= 1 << (29 - find_rightmost_bit (&j));
1250 /* print new flag string */
1251 sprintf (LOCAL->buf,"%010lo%02o",k,(unsigned)
1252 (fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
1253 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
1254 (fDRAFT * elt->draft)));
1255 while (T) { /* get to that place in the file */
1256 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1257 elt->private.special.text.size - 13,L_SET);
1258 /* write new flags */
1259 if (write (LOCAL->fd,LOCAL->buf,12) > 0) break;
1260 mm_notify (stream,strerror (errno),WARN);
1261 mm_diskerror (stream,errno,T);
1263 if (syncflag) { /* sync if requested */
1264 fsync (LOCAL->fd);
1265 fstat (LOCAL->fd,&sbuf); /* get new write time */
1266 times.modtime = LOCAL->filetime = sbuf.st_mtime;
1267 times.actime = time (0); /* make sure read is later */
1268 utime (stream->mailbox,&times);
1273 /* Tenex locate header for a message
1274 * Accepts: MAIL stream
1275 * message number
1276 * pointer to returned header size
1277 * Returns: position of header in file
1278 */
1280 unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
1281 unsigned long *size)
1283 unsigned long siz;
1284 long i = 0;
1285 char c = '\0';
1286 char *s = NIL;
1287 MESSAGECACHE *elt = tenex_elt (stream,msgno);
1288 unsigned long ret = elt->private.special.offset +
1289 elt->private.special.text.size;
1290 unsigned long msiz = tenex_size (stream,msgno);
1291 /* is header size known? */
1292 if (!(*size = elt->private.msg.header.text.size)) {
1293 lseek (LOCAL->fd,ret,L_SET);/* get to header position */
1294 /* search message for LF LF */
1295 for (siz = 0; siz < msiz; siz++) {
1296 if (--i <= 0) /* read another buffer as necessary */
1297 read (LOCAL->fd,s = LOCAL->buf,i = min (msiz-siz,(long) MAILTMPLEN));
1298 /* two newline sequence? */
1299 if ((c == '\012') && (*s == '\012')) {
1300 /* yes, note for later */
1301 elt->private.msg.header.text.size = (*size = siz + 1);
1302 return ret; /* return to caller */
1304 else c = *s++; /* next character */
1306 /* header consumes entire message */
1307 elt->private.msg.header.text.size = *size = msiz;
1309 return ret;

UW-IMAP'd extensions by yuuji