imapext-2007

view src/osdep/unix/tenex.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: 11 October 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 <sys/stat.h>
49 #include "misc.h"
50 #include "dummy.h"
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 */
65 } TENEXLOCAL;
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,
107 long syncflag);
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,
119 /* driver flags */
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 */
154 };
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
162 */
164 DRIVER *tenex_valid (char *name)
165 {
166 char tmp[MAILTMPLEN];
167 return tenex_isvalid (name,tmp) ? &tenexdriver : NIL;
168 }
171 /* Tenex mail test for valid mailbox
172 * Accepts: mailbox name
173 * Returns: T if valid, NIL otherwise
174 */
176 int tenex_isvalid (char *name,char *tmp)
177 {
178 int fd;
179 int ret = NIL;
180 char *s,file[MAILTMPLEN];
181 struct stat sbuf;
182 time_t tp[2];
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 */
189 }
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;
199 }
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 */
207 }
208 }
209 }
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 */
213 }
215 /* Tenex manipulate driver parameters
216 * Accepts: function code
217 * function-dependent value
218 * Returns: function-dependent return value
219 */
221 void *tenex_parameters (long function,void *value)
222 {
223 void *ret = NIL;
224 switch ((int) function) {
225 case GET_INBOXPATH:
226 if (value) ret = tenex_file ((char *) value,"INBOX");
227 break;
228 }
229 return ret;
230 }
233 /* Tenex mail scan mailboxes
234 * Accepts: mail stream
235 * reference
236 * pattern to search
237 * string to scan
238 */
240 void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
241 {
242 if (stream) dummy_scan (NIL,ref,pat,contents);
243 }
246 /* Tenex mail list mailboxes
247 * Accepts: mail stream
248 * reference
249 * pattern to search
250 */
252 void tenex_list (MAILSTREAM *stream,char *ref,char *pat)
253 {
254 if (stream) dummy_list (NIL,ref,pat);
255 }
258 /* Tenex mail list subscribed mailboxes
259 * Accepts: mail stream
260 * reference
261 * pattern to search
262 */
264 void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat)
265 {
266 if (stream) dummy_lsub (NIL,ref,pat);
267 }
269 /* Tenex mail delete mailbox
270 * Accepts: MAIL stream
271 * mailbox name to delete
272 * Returns: T on success, NIL on failure
273 */
275 long tenex_delete (MAILSTREAM *stream,char *mailbox)
276 {
277 return tenex_rename (stream,mailbox,NIL);
278 }
281 /* Tenex mail rename mailbox
282 * Accepts: MAIL stream
283 * old mailbox name
284 * new mailbox name (or NIL for delete)
285 * Returns: T on success, NIL on failure
286 */
288 long tenex_rename (MAILSTREAM *stream,char *old,char *newname)
289 {
290 long ret = T;
291 char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
292 int fd,ld;
293 struct stat sbuf;
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",
300 old,newname);
301 MM_LOG (tmp,ERROR);
302 return NIL;
303 }
304 else if ((fd = open (file,O_RDWR,NIL)) < 0) {
305 sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
306 MM_LOG (tmp,ERROR);
307 return NIL;
308 }
309 /* get exclusive parse/append permission */
310 if ((ld = lockfd (fd,lock,LOCK_EX)) < 0) {
311 MM_LOG ("Unable to lock rename mailbox",ERROR);
312 return NIL;
313 }
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);
318 MM_LOG (tmp,ERROR);
319 unlockfd (ld,lock); /* release exclusive parse/append permission */
320 return NIL;
321 }
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)))
330 ret = NIL;
331 else *s = c; /* restore full name */
332 }
333 /* rename the file */
334 if (ret && rename (file,tmp)) {
335 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
336 strerror (errno));
337 MM_LOG (tmp,ERROR);
338 ret = NIL; /* set failure */
339 }
340 }
341 else if (unlink (file)) {
342 sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
343 MM_LOG (tmp,ERROR);
344 ret = NIL; /* set failure */
345 }
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 */
352 }
354 /* Tenex Mail status
355 * Accepts: mail stream
356 * mailbox name
357 * status flags
358 * Returns: T on success, NIL on failure
359 */
361 long tenex_status (MAILSTREAM *stream,char *mbx,long flags)
362 {
363 MAILSTATUS status;
364 unsigned long i;
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;
388 }
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 */
393 }
395 /* Tenex mail open
396 * Accepts: stream to open
397 * Returns: stream on success, NIL on failure
398 */
400 MAILSTREAM *tenex_open (MAILSTREAM *stream)
401 {
402 int fd,ld;
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);
412 MM_LOG (tmp,ERROR);
413 }
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));
418 MM_LOG (tmp,ERROR);
419 return NIL;
420 }
421 else if (!stream->rdonly) { /* got it, but readonly */
422 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);
423 stream->rdonly = T;
424 }
425 }
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 */
435 /* flush old name */
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);
442 return NIL;
443 }
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 */
453 /* parse mailbox */
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 */
463 }
465 /* Tenex mail close
466 * Accepts: MAIL stream
467 * close options
468 */
470 void tenex_close (MAILSTREAM *stream,long options)
471 {
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 */
485 }
486 }
488 /* Tenex mail fetch fast data
489 * Accepts: MAIL stream
490 * sequence
491 * option flags
492 */
494 void tenex_fast (MAILSTREAM *stream,char *sequence,long flags)
495 {
496 STRING bs;
497 MESSAGECACHE *elt;
498 unsigned long i;
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);
512 }
513 /* tie off string */
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);
521 }
522 tenex_elt (stream,i); /* get current flags from file */
523 }
524 }
527 /* Tenex mail fetch flags
528 * Accepts: MAIL stream
529 * sequence
530 * option flags
531 * Sniffs at file to get flags
532 */
534 void tenex_flags (MAILSTREAM *stream,char *sequence,long flags)
535 {
536 unsigned long i;
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);
542 }
544 /* TENEX mail fetch message header
545 * Accepts: MAIL stream
546 * message # to fetch
547 * pointer to returned header text length
548 * option flags
549 * Returns: message header in RFC822 format
550 */
552 char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
553 unsigned long *length,long flags)
554 {
555 char *s;
556 unsigned long i;
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);
565 }
566 /* slurp the data */
567 read (LOCAL->fd,LOCAL->buf,*length = i);
568 }
569 else {
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 */
576 }
577 return (char *) LOCAL->buf;
578 }
580 /* TENEX mail fetch message text (body only)
581 * Accepts: MAIL stream
582 * message # to fetch
583 * pointer to returned stringstruct
584 * option flags
585 * Returns: T, always
586 */
588 long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
589 {
590 char *s;
591 unsigned long i,j;
592 MESSAGECACHE *elt;
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);
603 }
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);
610 }
611 /* go to text position */
612 lseek (LOCAL->fd,i + j,L_SET);
613 /* slurp the data */
614 read (LOCAL->fd,LOCAL->buf,i);
615 /* set up stringstruct for internal */
616 INIT (bs,mail_string,LOCAL->buf,i);
617 }
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 */
634 }
635 /* set up stringstruct */
636 INIT (bs,mail_string,LOCAL->text.data,i);
637 }
638 return T; /* success */
639 }
641 /* Tenex mail modify flags
642 * Accepts: MAIL stream
643 * sequence
644 * flag(s)
645 * option flags
646 */
648 void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
649 {
650 time_t tp[2];
651 struct stat sbuf;
652 if (!stream->rdonly) { /* make sure the update takes */
653 fsync (LOCAL->fd);
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);
658 }
659 }
662 /* Tenex mail per-message modify flags
663 * Accepts: MAIL stream
664 * message cache element
665 */
667 void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
668 {
669 struct stat sbuf;
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 */
675 }
676 /* recalculate status */
677 tenex_update_status (stream,elt->msgno,NIL);
678 }
680 /* Tenex mail ping mailbox
681 * Accepts: MAIL stream
682 * Returns: T if stream still alive, NIL if not
683 */
685 long tenex_ping (MAILSTREAM *stream)
686 {
687 unsigned long i = 1;
688 long r = T;
689 int ld;
690 char lock[MAILTMPLEN];
691 struct stat sbuf;
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;
703 }
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 */
710 }
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 */
721 }
722 }
723 }
724 }
725 return r; /* return result of the parse */
726 }
729 /* Tenex mail check mailbox (reparses status too)
730 * Accepts: MAIL stream
731 */
733 void tenex_check (MAILSTREAM *stream)
734 {
735 /* mark that a check is desired */
736 if (LOCAL) LOCAL->mustcheck = T;
737 if (tenex_ping (stream)) MM_LOG ("Check completed",(long) NIL);
738 }
740 /* Tenex mail snarf messages from system inbox
741 * Accepts: MAIL stream
742 */
744 void tenex_snarf (MAILSTREAM *stream)
745 {
746 unsigned long i = 0;
747 unsigned long j,r,hdrlen,txtlen;
748 struct stat sbuf;
749 char *hdr,*txt,lock[MAILTMPLEN],tmp[MAILTMPLEN];
750 MESSAGECACHE *elt;
751 MAILSTREAM *sysibx = NIL;
752 int ld;
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)));
780 /* copy message */
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;
784 }
785 fs_give ((void **) &hdr);
786 }
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 */
795 }
796 else {
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);
800 }
801 fstat (LOCAL->fd,&sbuf); /* yes, get current file size */
802 LOCAL->filetime = sbuf.st_mtime;
803 }
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 */
808 }
809 }
811 /* Tenex mail expunge mailbox
812 * Accepts: MAIL stream
813 * sequence to expunge if non-NIL
814 * expunge options
815 * Returns: T, always
816 */
818 long tenex_expunge (MAILSTREAM *stream,char *sequence,long options)
819 {
820 long ret;
821 time_t tp[2];
822 struct stat sbuf;
823 off_t pos = 0;
824 int ld;
825 unsigned long i = 1;
826 unsigned long j,k,m,recent;
827 unsigned long n = 0;
828 unsigned long delta = 0;
829 char lock[MAILTMPLEN];
830 MESSAGECACHE *elt;
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);
837 else {
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;
841 }
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.
850 */
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",
862 ERROR);
863 unlockfd (ld,lock); /* release exclusive parse/append permission */
864 }
866 else {
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 */
883 }
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);
893 while (T) {
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);
898 }
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;
904 }
905 /* preserved but no deleted messages */
906 else pos = elt->private.special.offset + k;
907 }
909 if (n) { /* truncate file after last message */
910 if (pos != (LOCAL->filesize -= delta)) {
911 sprintf (LOCAL->buf,
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 */
916 }
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);
921 }
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 */
936 }
937 }
938 return LONGT;
939 }
941 /* Tenex mail copy message(s)
942 * Accepts: MAIL stream
943 * sequence
944 * destination mailbox
945 * copy options
946 * Returns: T if success, NIL if failed
947 */
949 long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
950 {
951 struct stat sbuf;
952 time_t tp[2];
953 MESSAGECACHE *elt;
954 unsigned long i,j,k;
955 long ret = LONGT;
956 int fd,ld;
957 char file[MAILTMPLEN],lock[MAILTMPLEN];
958 mailproxycopy_t pc =
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);
964 return NIL;
965 case 0: /* merely empty file? */
966 break;
967 case EACCES: /* file protected */
968 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
969 MM_LOG (LOCAL->buf,ERROR);
970 return NIL;
971 case EINVAL:
972 if (pc) return (*pc) (stream,sequence,mailbox,options);
973 sprintf (LOCAL->buf,"Invalid Tenex-format mailbox name: %.80s",mailbox);
974 MM_LOG (LOCAL->buf,ERROR);
975 return NIL;
976 default:
977 if (pc) return (*pc) (stream,sequence,mailbox,options);
978 sprintf (LOCAL->buf,"Not a Tenex-format mailbox: %.80s",mailbox);
979 MM_LOG (LOCAL->buf,ERROR);
980 return NIL;
981 }
982 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
983 mail_sequence (stream,sequence))) return NIL;
984 /* got file? */
985 if ((fd = open (tenex_file(file,mailbox),O_RDWR,NIL)) < 0) {
986 sprintf (LOCAL->buf,"Unable to open copy mailbox: %s",strerror (errno));
987 MM_LOG (LOCAL->buf,ERROR);
988 return NIL;
989 }
990 MM_CRITICAL (stream); /* go critical */
991 /* get exclusive parse/append permission */
992 if (flock (fd,LOCK_SH) || ((ld = lockfd (fd,lock,LOCK_EX)) < 0)) {
993 MM_LOG ("Unable to lock copy mailbox",ERROR);
994 MM_NOCRITICAL (stream);
995 return NIL;
996 }
997 fstat (fd,&sbuf); /* get current file size */
998 lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
1000 /* for each requested message */
1001 for (i = 1; ret && (i <= stream->nmsgs); i++)
1002 if ((elt = mail_elt (stream,i))->sequence) {
1003 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
1004 /* number of bytes to copy */
1005 k = elt->private.special.text.size + tenex_size (stream,i);
1006 do { /* read from source position */
1007 j = min (k,LOCAL->buflen);
1008 read (LOCAL->fd,LOCAL->buf,j);
1009 if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
1010 } while (ret && (k -= j));/* until done */
1012 /* make sure all the updates take */
1013 if (!(ret && (ret = !fsync (fd)))) {
1014 sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
1015 MM_LOG (LOCAL->buf,ERROR);
1016 ftruncate (fd,sbuf.st_size);
1018 if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */
1019 /* else preserve \Marked status */
1020 else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
1021 tp[1] = sbuf.st_mtime; /* preserve mtime */
1022 utime (file,tp); /* set the times */
1023 close (fd); /* close the file */
1024 unlockfd (ld,lock); /* release exclusive parse/append permission */
1025 MM_NOCRITICAL (stream); /* release critical */
1026 /* delete all requested messages */
1027 if (ret && (options & CP_MOVE)) {
1028 for (i = 1; i <= stream->nmsgs; i++)
1029 if ((elt = tenex_elt (stream,i))->sequence) {
1030 elt->deleted = T; /* mark message deleted */
1031 /* recalculate status */
1032 tenex_update_status (stream,i,NIL);
1034 if (!stream->rdonly) { /* make sure the update takes */
1035 fsync (LOCAL->fd);
1036 fstat (LOCAL->fd,&sbuf); /* get current write time */
1037 tp[1] = LOCAL->filetime = sbuf.st_mtime;
1038 tp[0] = time (0); /* make sure atime remains greater */
1039 utime (stream->mailbox,tp);
1042 if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
1043 MM_LOG ("Can not return meaningful COPYUID with this mailbox format",WARN);
1044 return ret;
1047 /* Tenex mail append message from stringstruct
1048 * Accepts: MAIL stream
1049 * destination mailbox
1050 * append callback
1051 * data for callback
1052 * Returns: T if append successful, else NIL
1053 */
1055 long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
1057 struct stat sbuf;
1058 int fd,ld,c;
1059 char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
1060 time_t tp[2];
1061 FILE *df;
1062 MESSAGECACHE elt;
1063 long f;
1064 unsigned long i,j,uf,size;
1065 STRING *message;
1066 long ret = LONGT;
1067 /* default stream to prototype */
1068 if (!stream) stream = user_flags (&tenexproto);
1069 /* make sure valid mailbox */
1070 if (!tenex_isvalid (mailbox,tmp)) switch (errno) {
1071 case ENOENT: /* no such file? */
1072 if (!compare_cstring (mailbox,"INBOX")) dummy_create (NIL,"mail.txt");
1073 else {
1074 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
1075 return NIL;
1077 /* falls through */
1078 case 0: /* merely empty file? */
1079 break;
1080 case EACCES: /* file protected */
1081 sprintf (tmp,"Can't access destination: %.80s",mailbox);
1082 MM_LOG (tmp,ERROR);
1083 return NIL;
1084 case EINVAL:
1085 sprintf (tmp,"Invalid TENEX-format mailbox name: %.80s",mailbox);
1086 MM_LOG (tmp,ERROR);
1087 return NIL;
1088 default:
1089 sprintf (tmp,"Not a TENEX-format mailbox: %.80s",mailbox);
1090 MM_LOG (tmp,ERROR);
1091 return NIL;
1093 /* get first message */
1094 if (!MM_APPEND (af) (stream,data,&flags,&date,&message)) return NIL;
1096 /* open destination mailbox */
1097 if (((fd = open (tenex_file (file,mailbox),O_WRONLY|O_APPEND,NIL)) < 0) ||
1098 !(df = fdopen (fd,"ab"))) {
1099 sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
1100 MM_LOG (tmp,ERROR);
1101 return NIL;
1103 /* get parse/append permission */
1104 if (flock (fd,LOCK_SH) || ((ld = lockfd (fd,lock,LOCK_EX)) < 0)) {
1105 MM_LOG ("Unable to lock append mailbox",ERROR);
1106 close (fd);
1107 return NIL;
1109 MM_CRITICAL (stream); /* go critical */
1110 fstat (fd,&sbuf); /* get current file size */
1111 errno = 0;
1112 do { /* parse flags */
1113 if (!SIZE (message)) { /* guard against zero-length */
1114 MM_LOG ("Append of zero-length message",ERROR);
1115 ret = NIL;
1116 break;
1118 f = mail_parse_flags (stream,flags,&i);
1119 /* reverse bits (dontcha wish we had CIRC?) */
1120 for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));
1121 if (date) { /* parse date if given */
1122 if (!mail_parse_date (&elt,date)) {
1123 sprintf (tmp,"Bad date in append: %.80s",date);
1124 MM_LOG (tmp,ERROR);
1125 ret = NIL; /* mark failure */
1126 break;
1128 mail_date (tmp,&elt); /* write preseved date */
1130 else internal_date (tmp); /* get current date in IMAP format */
1131 i = GETPOS (message); /* remember current position */
1132 for (j = SIZE (message), size = 0; j; --j)
1133 if (SNX (message) != '\015') ++size;
1134 SETPOS (message,i); /* restore position */
1135 /* write header */
1136 if (fprintf (df,"%s,%lu;%010lo%02lo\n",tmp,size,uf,(unsigned long) f) < 0)
1137 ret = NIL;
1138 else { /* write message */
1139 while (size) if ((c = 0xff & SNX (message)) != '\015') {
1140 if (putc (c,df) != EOF) --size;
1141 else break;
1143 /* get next message */
1144 if (size || !MM_APPEND (af) (stream,data,&flags,&date,&message))
1145 ret = NIL;
1147 } while (ret && message);
1148 /* if error... */
1149 if (!ret || (fflush (df) == EOF)) {
1150 ftruncate (fd,sbuf.st_size);/* revert file */
1151 close (fd); /* make sure fclose() doesn't corrupt us */
1152 if (errno) {
1153 sprintf (tmp,"Message append failed: %s",strerror (errno));
1154 MM_LOG (tmp,ERROR);
1156 ret = NIL;
1158 if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */
1159 /* else preserve \Marked status */
1160 else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
1161 tp[1] = sbuf.st_mtime; /* preserve mtime */
1162 utime (file,tp); /* set the times */
1163 fclose (df); /* close the file */
1164 unlockfd (ld,lock); /* release exclusive parse/append permission */
1165 MM_NOCRITICAL (stream); /* release critical */
1166 if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))
1167 MM_LOG ("Can not return meaningful APPENDUID with this mailbox format",
1168 WARN);
1169 return ret;
1172 /* Internal routines */
1175 /* Tenex mail return internal message size in bytes
1176 * Accepts: MAIL stream
1177 * message #
1178 * Returns: internal size of message
1179 */
1181 unsigned long tenex_size (MAILSTREAM *stream,unsigned long m)
1183 MESSAGECACHE *elt = mail_elt (stream,m);
1184 return ((m < stream->nmsgs) ? mail_elt (stream,m+1)->private.special.offset :
1185 LOCAL->filesize) -
1186 (elt->private.special.offset + elt->private.special.text.size);
1190 /* Tenex mail generate file string
1191 * Accepts: temporary buffer to write into
1192 * mailbox name string
1193 * Returns: local file string or NIL if failure
1194 */
1196 char *tenex_file (char *dst,char *name)
1198 char tmp[MAILTMPLEN];
1199 char *s = mailboxfile (dst,name);
1200 /* return our standard inbox */
1201 return (s && !*s) ? mailboxfile (dst,tenex_isvalid ("~/INBOX",tmp) ?
1202 "~/INBOX" : "mail.txt") : s;
1205 /* Tenex mail parse mailbox
1206 * Accepts: MAIL stream
1207 * Returns: T if parse OK
1208 * NIL if failure, stream aborted
1209 */
1211 long tenex_parse (MAILSTREAM *stream)
1213 struct stat sbuf;
1214 MESSAGECACHE *elt = NIL;
1215 unsigned char c,*s,*t,*x;
1216 char tmp[MAILTMPLEN];
1217 unsigned long i,j;
1218 long curpos = LOCAL->filesize;
1219 long nmsgs = stream->nmsgs;
1220 long recent = stream->recent;
1221 short added = NIL;
1222 short silent = stream->silent;
1223 fstat (LOCAL->fd,&sbuf); /* get status */
1224 if (sbuf.st_size < curpos) { /* sanity check */
1225 sprintf (tmp,"Mailbox shrank from %lu to %lu!",
1226 (unsigned long) curpos,(unsigned long) sbuf.st_size);
1227 MM_LOG (tmp,ERROR);
1228 tenex_close (stream,NIL);
1229 return NIL;
1231 stream->silent = T; /* don't pass up exists events yet */
1232 while (sbuf.st_size - curpos){/* while there is stuff to parse */
1233 /* get to that position in the file */
1234 lseek (LOCAL->fd,curpos,L_SET);
1235 if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
1236 sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
1237 (unsigned long) curpos,(unsigned long) sbuf.st_size,
1238 i ? strerror (errno) : "no data read");
1239 MM_LOG (tmp,ERROR);
1240 tenex_close (stream,NIL);
1241 return NIL;
1243 LOCAL->buf[i] = '\0'; /* tie off buffer just in case */
1244 if (!(s = strchr (LOCAL->buf,'\012'))) {
1245 sprintf (tmp,"Unable to find newline at %lu in %lu bytes, text: %s",
1246 (unsigned long) curpos,i,(char *) LOCAL->buf);
1247 MM_LOG (tmp,ERROR);
1248 tenex_close (stream,NIL);
1249 return NIL;
1251 *s = '\0'; /* tie off header line */
1252 i = (s + 1) - LOCAL->buf; /* note start of text offset */
1253 if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
1254 sprintf (tmp,"Unable to parse internal header at %lu: %s",
1255 (unsigned long) curpos,(char *) LOCAL->buf);
1256 MM_LOG (tmp,ERROR);
1257 tenex_close (stream,NIL);
1258 return NIL;
1260 *s++ = '\0'; *t++ = '\0'; /* tie off fields */
1262 added = T; /* note that a new message was added */
1263 /* swell the cache */
1264 mail_exists (stream,++nmsgs);
1265 /* instantiate an elt for this message */
1266 (elt = mail_elt (stream,nmsgs))->valid = T;
1267 elt->private.uid = ++stream->uid_last;
1268 /* note file offset of header */
1269 elt->private.special.offset = curpos;
1270 /* in case error */
1271 elt->private.special.text.size = 0;
1272 /* header size not known yet */
1273 elt->private.msg.header.text.size = 0;
1274 x = s; /* parse the header components */
1275 if (mail_parse_date (elt,LOCAL->buf) &&
1276 (elt->private.msg.full.text.size = strtoul (s,(char **) &s,10)) &&
1277 (!(s && *s)) && isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&
1278 isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&
1279 isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&
1280 isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])
1281 elt->private.special.text.size = i;
1282 else { /* oops */
1283 sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",
1284 curpos,(char *) LOCAL->buf,(char *) x,(char *) t);
1285 MM_LOG (tmp,ERROR);
1286 tenex_close (stream,NIL);
1287 return NIL;
1289 /* make sure didn't run off end of file */
1290 if ((curpos += (elt->private.msg.full.text.size + i)) > sbuf.st_size) {
1291 sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
1292 elt->private.special.offset,(unsigned long) curpos,
1293 (unsigned long) sbuf.st_size);
1294 MM_LOG (tmp,ERROR);
1295 tenex_close (stream,NIL);
1296 return NIL;
1298 c = t[10]; /* remember first system flags byte */
1299 t[10] = '\0'; /* tie off flags */
1300 j = strtoul (t,NIL,8); /* get user flags value */
1301 t[10] = c; /* restore first system flags byte */
1302 /* set up all valid user flags (reversed!) */
1303 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1304 stream->user_flags[i]) elt->user_flags |= 1 << i;
1305 /* calculate system flags */
1306 if ((j = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;
1307 if (j & fDELETED) elt->deleted = T;
1308 if (j & fFLAGGED) elt->flagged = T;
1309 if (j & fANSWERED) elt->answered = T;
1310 if (j & fDRAFT) elt->draft = T;
1311 if (!(j & fOLD)) { /* newly arrived message? */
1312 elt->recent = T;
1313 recent++; /* count up a new recent message */
1314 /* mark it as old */
1315 tenex_update_status (stream,nmsgs,NIL);
1318 fsync (LOCAL->fd); /* make sure all the fOLD flags take */
1319 /* update parsed file size and time */
1320 LOCAL->filesize = sbuf.st_size;
1321 fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */
1322 LOCAL->filetime = sbuf.st_mtime;
1323 if (added && !stream->rdonly){/* make sure atime updated */
1324 time_t tp[2];
1325 tp[0] = time (0);
1326 tp[1] = LOCAL->filetime;
1327 utime (stream->mailbox,tp);
1329 stream->silent = silent; /* can pass up events now */
1330 mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */
1331 mail_recent (stream,recent); /* and of change in recent messages */
1332 return LONGT; /* return the winnage */
1335 /* Tenex get cache element with status updating from file
1336 * Accepts: MAIL stream
1337 * message number
1338 * Returns: cache element
1339 */
1341 MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno)
1343 MESSAGECACHE *elt = mail_elt (stream,msgno);
1344 struct { /* old flags */
1345 unsigned int seen : 1;
1346 unsigned int deleted : 1;
1347 unsigned int flagged : 1;
1348 unsigned int answered : 1;
1349 unsigned int draft : 1;
1350 unsigned long user_flags;
1351 } old;
1352 old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
1353 old.answered = elt->answered; old.draft = elt->draft;
1354 old.user_flags = elt->user_flags;
1355 tenex_read_flags (stream,elt);
1356 if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
1357 (old.flagged != elt->flagged) || (old.answered != elt->answered) ||
1358 (old.draft != elt->draft) || (old.user_flags != elt->user_flags))
1359 MM_FLAGS (stream,msgno); /* let top level know */
1360 return elt;
1363 /* Tenex read flags from file
1364 * Accepts: MAIL stream
1365 * Returns: cache element
1366 */
1368 void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
1370 unsigned long i,j;
1371 /* noop if readonly and have valid flags */
1372 if (stream->rdonly && elt->valid) return;
1373 /* set the seek pointer */
1374 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1375 elt->private.special.text.size - 13,L_SET);
1376 /* read the new flags */
1377 if (read (LOCAL->fd,LOCAL->buf,12) < 0) {
1378 sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
1379 fatal (LOCAL->buf);
1381 /* calculate system flags */
1382 i = (((LOCAL->buf[10]-'0') * 8) + LOCAL->buf[11]-'0');
1383 elt->seen = i & fSEEN ? T : NIL; elt->deleted = i & fDELETED ? T : NIL;
1384 elt->flagged = i & fFLAGGED ? T : NIL;
1385 elt->answered = i & fANSWERED ? T : NIL; elt->draft = i & fDRAFT ? T : NIL;
1386 LOCAL->buf[10] = '\0'; /* tie off flags */
1387 j = strtoul(LOCAL->buf,NIL,8);/* get user flags value */
1388 /* set up all valid user flags (reversed!) */
1389 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1390 stream->user_flags[i]) elt->user_flags |= 1 << i;
1391 elt->valid = T; /* have valid flags now */
1394 /* Tenex update status string
1395 * Accepts: MAIL stream
1396 * message number
1397 * flag saying whether or not to sync
1398 */
1400 void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
1402 time_t tp[2];
1403 struct stat sbuf;
1404 MESSAGECACHE *elt = mail_elt (stream,msgno);
1405 unsigned long j,k = 0;
1406 /* readonly */
1407 if (stream->rdonly || !elt->valid) tenex_read_flags (stream,elt);
1408 else { /* readwrite */
1409 j = elt->user_flags; /* get user flags */
1410 /* reverse bits (dontcha wish we had CIRC?) */
1411 while (j) k |= 1 << (29 - find_rightmost_bit (&j));
1412 /* print new flag string */
1413 sprintf (LOCAL->buf,"%010lo%02o",k,(unsigned)
1414 (fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
1415 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
1416 (fDRAFT * elt->draft)));
1417 /* get to that place in the file */
1418 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1419 elt->private.special.text.size - 13,L_SET);
1420 /* write new flags */
1421 write (LOCAL->fd,LOCAL->buf,12);
1422 if (syncflag) { /* sync if requested */
1423 fsync (LOCAL->fd);
1424 fstat (LOCAL->fd,&sbuf); /* get new write time */
1425 tp[1] = LOCAL->filetime = sbuf.st_mtime;
1426 tp[0] = time (0); /* make sure read is later */
1427 utime (stream->mailbox,tp);
1432 /* Tenex locate header for a message
1433 * Accepts: MAIL stream
1434 * message number
1435 * pointer to returned header size
1436 * Returns: position of header in file
1437 */
1439 unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
1440 unsigned long *size)
1442 unsigned long siz;
1443 long i = 0;
1444 char c = '\0';
1445 char *s = NIL;
1446 MESSAGECACHE *elt = tenex_elt (stream,msgno);
1447 unsigned long ret = elt->private.special.offset +
1448 elt->private.special.text.size;
1449 unsigned long msiz = tenex_size (stream,msgno);
1450 /* is header size known? */
1451 if (!(*size = elt->private.msg.header.text.size)) {
1452 lseek (LOCAL->fd,ret,L_SET);/* get to header position */
1453 /* search message for LF LF */
1454 for (siz = 0; siz < msiz; siz++) {
1455 if (--i <= 0) /* read another buffer as necessary */
1456 read (LOCAL->fd,s = LOCAL->buf,i = min (msiz-siz,(long) MAILTMPLEN));
1457 /* two newline sequence? */
1458 if ((c == '\012') && (*s == '\012')) {
1459 /* yes, note for later */
1460 elt->private.msg.header.text.size = (*size = siz + 1);
1462 return ret; /* return to caller */
1464 else c = *s++; /* next character */
1466 /* header consumes entire message */
1467 elt->private.msg.header.text.size = *size = msiz;
1469 return ret;

UW-IMAP'd extensions by yuuji