imapext-2007

view src/osdep/amiga/mtx.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: MTX 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 */
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <errno.h>
40 extern int errno; /* just in case */
41 #include "mail.h"
42 #include "osdep.h"
43 #include <pwd.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include "misc.h"
47 #include "dummy.h"
48 #include "fdstring.h"
50 /* MTX I/O stream local data */
52 typedef struct mtx_local {
53 unsigned int shouldcheck: 1; /* if ping should do a check instead */
54 unsigned int mustcheck: 1; /* if ping must do a check instead */
55 int fd; /* file descriptor for I/O */
56 off_t filesize; /* file size parsed */
57 time_t filetime; /* last file time */
58 time_t lastsnarf; /* last snarf time */
59 unsigned char *buf; /* temporary buffer */
60 unsigned long buflen; /* current size of temporary buffer */
61 } MTXLOCAL;
64 /* Convenient access to local data */
66 #define LOCAL ((MTXLOCAL *) stream->local)
69 /* Function prototypes */
71 DRIVER *mtx_valid (char *name);
72 int mtx_isvalid (char *name,char *tmp);
73 void *mtx_parameters (long function,void *value);
74 void mtx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
75 void mtx_list (MAILSTREAM *stream,char *ref,char *pat);
76 void mtx_lsub (MAILSTREAM *stream,char *ref,char *pat);
77 long mtx_create (MAILSTREAM *stream,char *mailbox);
78 long mtx_delete (MAILSTREAM *stream,char *mailbox);
79 long mtx_rename (MAILSTREAM *stream,char *old,char *newname);
80 long mtx_status (MAILSTREAM *stream,char *mbx,long flags);
81 MAILSTREAM *mtx_open (MAILSTREAM *stream);
82 void mtx_close (MAILSTREAM *stream,long options);
83 void mtx_flags (MAILSTREAM *stream,char *sequence,long flags);
84 char *mtx_header (MAILSTREAM *stream,unsigned long msgno,
85 unsigned long *length,long flags);
86 long mtx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
87 void mtx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
88 void mtx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
89 long mtx_ping (MAILSTREAM *stream);
90 void mtx_check (MAILSTREAM *stream);
91 void mtx_snarf (MAILSTREAM *stream);
92 long mtx_expunge (MAILSTREAM *stream,char *sequence,long options);
93 long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
94 long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
96 char *mtx_file (char *dst,char *name);
97 long mtx_parse (MAILSTREAM *stream);
98 MESSAGECACHE *mtx_elt (MAILSTREAM *stream,unsigned long msgno);
99 void mtx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt);
100 void mtx_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag);
101 unsigned long mtx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
102 unsigned long *size);
104 /* MTX mail routines */
107 /* Driver dispatch used by MAIL */
109 DRIVER mtxdriver = {
110 "mtx", /* driver name */
111 /* driver flags */
112 DR_LOCAL|DR_MAIL|DR_CRLF|DR_NOSTICKY|DR_LOCKING,
113 (DRIVER *) NIL, /* next driver */
114 mtx_valid, /* mailbox is valid for us */
115 mtx_parameters, /* manipulate parameters */
116 mtx_scan, /* scan mailboxes */
117 mtx_list, /* list mailboxes */
118 mtx_lsub, /* list subscribed mailboxes */
119 NIL, /* subscribe to mailbox */
120 NIL, /* unsubscribe from mailbox */
121 dummy_create, /* create mailbox */
122 mtx_delete, /* delete mailbox */
123 mtx_rename, /* rename mailbox */
124 mtx_status, /* status of mailbox */
125 mtx_open, /* open mailbox */
126 mtx_close, /* close mailbox */
127 mtx_flags, /* fetch message "fast" attributes */
128 mtx_flags, /* fetch message flags */
129 NIL, /* fetch overview */
130 NIL, /* fetch message envelopes */
131 mtx_header, /* fetch message header */
132 mtx_text, /* fetch message body */
133 NIL, /* fetch partial message text */
134 NIL, /* unique identifier */
135 NIL, /* message number */
136 mtx_flag, /* modify flags */
137 mtx_flagmsg, /* per-message modify flags */
138 NIL, /* search for message based on criteria */
139 NIL, /* sort messages */
140 NIL, /* thread messages */
141 mtx_ping, /* ping mailbox to see if still alive */
142 mtx_check, /* check for new messages */
143 mtx_expunge, /* expunge deleted messages */
144 mtx_copy, /* copy messages to another mailbox */
145 mtx_append, /* append string message to mailbox */
146 NIL /* garbage collect stream */
147 };
149 /* prototype stream */
150 MAILSTREAM mtxproto = {&mtxdriver};
152 /* MTX mail validate mailbox
153 * Accepts: mailbox name
154 * Returns: our driver if name is valid, NIL otherwise
155 */
157 DRIVER *mtx_valid (char *name)
158 {
159 char tmp[MAILTMPLEN];
160 return mtx_isvalid (name,tmp) ? &mtxdriver : NIL;
161 }
164 /* MTX mail test for valid mailbox
165 * Accepts: mailbox name
166 * Returns: T if valid, NIL otherwise
167 */
169 int mtx_isvalid (char *name,char *tmp)
170 {
171 int fd;
172 int ret = NIL;
173 char *s,file[MAILTMPLEN];
174 struct stat sbuf;
175 time_t tp[2];
176 errno = EINVAL; /* assume invalid argument */
177 /* if file, get its status */
178 if ((s = mtx_file (file,name)) && !stat (s,&sbuf)) {
179 if (!sbuf.st_size) { /* allow empty file if INBOX */
180 if ((s = mailboxfile (tmp,name)) && !*s) ret = T;
181 else errno = 0; /* empty file */
182 }
183 else if ((fd = open (file,O_RDONLY,NIL)) >= 0) {
184 memset (tmp,'\0',MAILTMPLEN);
185 if ((read (fd,tmp,64) >= 0) && (s = strchr (tmp,'\015')) &&
186 (s[1] == '\012')) { /* valid format? */
187 *s = '\0'; /* tie off header */
188 /* must begin with dd-mmm-yy" */
189 ret = (((tmp[2] == '-' && tmp[6] == '-') ||
190 (tmp[1] == '-' && tmp[5] == '-')) &&
191 (s = strchr (tmp+18,',')) && strchr (s+2,';')) ? T : NIL;
192 }
193 else errno = -1; /* bogus format */
194 close (fd); /* close the file */
195 /* \Marked status? */
196 if (sbuf.st_ctime > sbuf.st_atime) {
197 tp[0] = sbuf.st_atime; /* preserve atime and mtime */
198 tp[1] = sbuf.st_mtime;
199 utime (file,tp); /* set the times */
200 }
201 }
202 }
203 /* in case INBOX but not mtx format */
204 else if ((errno == ENOENT) && !compare_cstring (name,"INBOX")) errno = -1;
205 return ret; /* return what we should */
206 }
208 /* MTX manipulate driver parameters
209 * Accepts: function code
210 * function-dependent value
211 * Returns: function-dependent return value
212 */
214 void *mtx_parameters (long function,void *value)
215 {
216 void *ret = NIL;
217 switch ((int) function) {
218 case GET_INBOXPATH:
219 if (value) ret = mtx_file ((char *) value,"INBOX");
220 break;
221 }
222 return ret;
223 }
226 /* MTX mail scan mailboxes
227 * Accepts: mail stream
228 * reference
229 * pattern to search
230 * string to scan
231 */
233 void mtx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
234 {
235 if (stream) dummy_scan (NIL,ref,pat,contents);
236 }
239 /* MTX mail list mailboxes
240 * Accepts: mail stream
241 * reference
242 * pattern to search
243 */
245 void mtx_list (MAILSTREAM *stream,char *ref,char *pat)
246 {
247 if (stream) dummy_list (NIL,ref,pat);
248 }
251 /* MTX mail list subscribed mailboxes
252 * Accepts: mail stream
253 * reference
254 * pattern to search
255 */
257 void mtx_lsub (MAILSTREAM *stream,char *ref,char *pat)
258 {
259 if (stream) dummy_lsub (NIL,ref,pat);
260 }
262 /* MTX mail delete mailbox
263 * Accepts: MAIL stream
264 * mailbox name to delete
265 * Returns: T on success, NIL on failure
266 */
268 long mtx_delete (MAILSTREAM *stream,char *mailbox)
269 {
270 return mtx_rename (stream,mailbox,NIL);
271 }
274 /* MTX mail rename mailbox
275 * Accepts: MAIL stream
276 * old mailbox name
277 * new mailbox name (or NIL for delete)
278 * Returns: T on success, NIL on failure
279 */
281 long mtx_rename (MAILSTREAM *stream,char *old,char *newname)
282 {
283 long ret = T;
284 char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
285 int fd,ld;
286 struct stat sbuf;
287 if (!mtx_file (file,old) ||
288 (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
289 ((s = strrchr (tmp,'/')) && !s[1])))) {
290 sprintf (tmp,newname ?
291 "Can't rename mailbox %.80s to %.80s: invalid name" :
292 "Can't delete mailbox %.80s: invalid name",
293 old,newname);
294 MM_LOG (tmp,ERROR);
295 return NIL;
296 }
297 else if ((fd = open (file,O_RDWR,NIL)) < 0) {
298 sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
299 MM_LOG (tmp,ERROR);
300 return NIL;
301 }
302 /* get exclusive parse/append permission */
303 if ((ld = lockfd (fd,lock,LOCK_EX)) < 0) {
304 MM_LOG ("Unable to lock rename mailbox",ERROR);
305 return NIL;
306 }
307 /* lock out other users */
308 if (flock (fd,LOCK_EX|LOCK_NB)) {
309 close (fd); /* couldn't lock, give up on it then */
310 sprintf (tmp,"Mailbox %.80s is in use by another process",old);
311 MM_LOG (tmp,ERROR);
312 unlockfd (ld,lock); /* release exclusive parse/append permission */
313 return NIL;
314 }
316 if (newname) { /* want rename? */
317 if (s = strrchr (tmp,'/')) {/* found superior to destination name? */
318 c = *++s; /* remember first character of inferior */
319 *s = '\0'; /* tie off to get just superior */
320 /* name doesn't exist, create it */
321 if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
322 !dummy_create_path (stream,tmp,get_dir_protection (newname)))
323 ret = NIL;
324 else *s = c; /* restore full name */
325 }
326 /* rename the file */
327 if (ret && rename (file,tmp)) {
328 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
329 strerror (errno));
330 MM_LOG (tmp,ERROR);
331 ret = NIL; /* set failure */
332 }
333 }
334 else if (unlink (file)) {
335 sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
336 MM_LOG (tmp,ERROR);
337 ret = NIL; /* set failure */
338 }
339 flock (fd,LOCK_UN); /* release lock on the file */
340 close (fd); /* close the file */
341 unlockfd (ld,lock); /* release exclusive parse/append permission */
342 /* recreate file if renamed INBOX */
343 if (ret && !compare_cstring (old,"INBOX")) dummy_create (NIL,"INBOX.MTX");
344 return ret; /* return success */
345 }
347 /* Mtx Mail status
348 * Accepts: mail stream
349 * mailbox name
350 * status flags
351 * Returns: T on success, NIL on failure
352 */
354 long mtx_status (MAILSTREAM *stream,char *mbx,long flags)
355 {
356 MAILSTATUS status;
357 unsigned long i;
358 MAILSTREAM *tstream = NIL;
359 MAILSTREAM *systream = NIL;
360 /* make temporary stream (unless this mbx) */
361 if (!stream && !(stream = tstream =
362 mail_open (NIL,mbx,OP_READONLY|OP_SILENT))) return NIL;
363 status.flags = flags; /* return status values */
364 status.messages = stream->nmsgs;
365 status.recent = stream->recent;
366 if (flags & SA_UNSEEN) /* must search to get unseen messages */
367 for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++)
368 if (!mail_elt (stream,i)->seen) status.unseen++;
369 status.uidnext = stream->uid_last + 1;
370 status.uidvalidity = stream->uid_validity;
371 /* calculate post-snarf results */
372 if (!status.recent && stream->inbox &&
373 (systream = mail_open (NIL,sysinbox (),OP_READONLY|OP_SILENT))) {
374 status.messages += systream->nmsgs;
375 status.recent += systream->recent;
376 if (flags & SA_UNSEEN) /* must search to get unseen messages */
377 for (i = 1; i <= systream->nmsgs; i++)
378 if (!mail_elt (systream,i)->seen) status.unseen++;
379 /* kludge but probably good enough */
380 status.uidnext += systream->nmsgs;
381 }
382 MM_STATUS(stream,mbx,&status);/* pass status to main program */
383 if (tstream) mail_close (tstream);
384 if (systream) mail_close (systream);
385 return T; /* success */
386 }
388 /* MTX mail open
389 * Accepts: stream to open
390 * Returns: stream on success, NIL on failure
391 */
393 MAILSTREAM *mtx_open (MAILSTREAM *stream)
394 {
395 int fd,ld;
396 char tmp[MAILTMPLEN];
397 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
398 /* return prototype for OP_PROTOTYPE call */
399 if (!stream) return user_flags (&mtxproto);
400 if (stream->local) fatal ("mtx recycle stream");
401 user_flags (stream); /* set up user flags */
402 /* canonicalize the mailbox name */
403 if (!mtx_file (tmp,stream->mailbox)) {
404 sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
405 MM_LOG (tmp,ERROR);
406 }
407 if (stream->rdonly ||
408 (fd = open (tmp,O_RDWR,NIL)) < 0) {
409 if ((fd = open (tmp,O_RDONLY,NIL)) < 0) {
410 sprintf (tmp,"Can't open mailbox: %.80s",strerror (errno));
411 MM_LOG (tmp,ERROR);
412 return NIL;
413 }
414 else if (!stream->rdonly) { /* got it, but readonly */
415 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);
416 stream->rdonly = T;
417 }
418 }
419 stream->local = fs_get (sizeof (MTXLOCAL));
420 LOCAL->fd = fd; /* bind the file */
421 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
422 LOCAL->buflen = CHUNKSIZE - 1;
423 /* note if an INBOX or not */
424 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
425 fs_give ((void **) &stream->mailbox);
426 stream->mailbox = cpystr (tmp);
427 /* get shared parse permission */
428 if ((ld = lockfd (fd,tmp,LOCK_SH)) < 0) {
429 MM_LOG ("Unable to lock open mailbox",ERROR);
430 return NIL;
431 }
432 (*bn) (BLOCK_FILELOCK,NIL);
433 flock (LOCAL->fd,LOCK_SH); /* lock the file */
434 (*bn) (BLOCK_NONE,NIL);
435 unlockfd (ld,tmp); /* release shared parse permission */
436 LOCAL->filesize = 0; /* initialize parsed file size */
437 /* time not set up yet */
438 LOCAL->lastsnarf = LOCAL->filetime = 0;
439 LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
440 stream->sequence++; /* bump sequence number */
441 /* parse mailbox */
442 stream->nmsgs = stream->recent = 0;
443 if (mtx_ping (stream) && !stream->nmsgs)
444 MM_LOG ("Mailbox is empty",(long) NIL);
445 if (!LOCAL) return NIL; /* failure if stream died */
446 stream->perm_seen = stream->perm_deleted =
447 stream->perm_flagged = stream->perm_answered = stream->perm_draft =
448 stream->rdonly ? NIL : T;
449 stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
450 return stream; /* return stream to caller */
451 }
453 /* MTX mail close
454 * Accepts: MAIL stream
455 * close options
456 */
458 void mtx_close (MAILSTREAM *stream,long options)
459 {
460 if (stream && LOCAL) { /* only if a file is open */
461 int silent = stream->silent;
462 stream->silent = T; /* note this stream is dying */
463 if (options & CL_EXPUNGE) mtx_expunge (stream,NIL,NIL);
464 stream->silent = silent; /* restore previous status */
465 flock (LOCAL->fd,LOCK_UN); /* unlock local file */
466 close (LOCAL->fd); /* close the local file */
467 /* free local text buffer */
468 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
469 /* nuke the local data */
470 fs_give ((void **) &stream->local);
471 stream->dtb = NIL; /* log out the DTB */
472 }
473 }
476 /* MTX mail fetch flags
477 * Accepts: MAIL stream
478 * sequence
479 * option flags
480 * Sniffs at file to see if some other process changed the flags
481 */
483 void mtx_flags (MAILSTREAM *stream,char *sequence,long flags)
484 {
485 unsigned long i;
486 if (mtx_ping (stream) && /* ping mailbox, get new status for messages */
487 ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
488 mail_sequence (stream,sequence)))
489 for (i = 1; i <= stream->nmsgs; i++)
490 if (mail_elt (stream,i)->sequence) mtx_elt (stream,i);
491 }
493 /* MTX mail fetch message header
494 * Accepts: MAIL stream
495 * message # to fetch
496 * pointer to returned header text length
497 * option flags
498 * Returns: message header in RFC822 format
499 */
501 char *mtx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
502 long flags)
503 {
504 *length = 0; /* default to empty */
505 if (flags & FT_UID) return "";/* UID call "impossible" */
506 /* get to header position */
507 lseek (LOCAL->fd,mtx_hdrpos (stream,msgno,length),L_SET);
508 /* is buffer big enough? */
509 if (*length > LOCAL->buflen) {
510 fs_give ((void **) &LOCAL->buf);
511 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = *length) + 1);
512 }
513 LOCAL->buf[*length] = '\0'; /* tie off string */
514 /* slurp the data */
515 read (LOCAL->fd,LOCAL->buf,*length);
516 return (char *) LOCAL->buf;
517 }
519 /* MTX mail fetch message text (body only)
520 * Accepts: MAIL stream
521 * message # to fetch
522 * pointer to returned header text length
523 * option flags
524 * Returns: T, always
525 */
527 long mtx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
528 {
529 FDDATA d;
530 unsigned long i,j;
531 MESSAGECACHE *elt;
532 /* UID call "impossible" */
533 if (flags & FT_UID) return NIL;
534 elt = mtx_elt (stream,msgno); /* get message status */
535 /* if message not seen */
536 if (!(flags & FT_PEEK) && !elt->seen) {
537 elt->seen = T; /* mark message as seen */
538 /* recalculate status */
539 mtx_update_status (stream,msgno,NIL);
540 MM_FLAGS (stream,msgno);
541 }
542 /* find header position */
543 i = mtx_hdrpos (stream,msgno,&j);
544 d.fd = LOCAL->fd; /* set up file descriptor */
545 d.pos = i + j;
546 d.chunk = LOCAL->buf; /* initial buffer chunk */
547 d.chunksize = CHUNKSIZE;
548 INIT (bs,fd_string,&d,elt->rfc822_size - j);
549 return T; /* success */
550 }
552 /* MTX mail modify flags
553 * Accepts: MAIL stream
554 * sequence
555 * flag(s)
556 * option flags
557 */
559 void mtx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
560 {
561 time_t tp[2];
562 struct stat sbuf;
563 if (!stream->rdonly) { /* make sure the update takes */
564 fsync (LOCAL->fd);
565 fstat (LOCAL->fd,&sbuf); /* get current write time */
566 tp[1] = LOCAL->filetime = sbuf.st_mtime;
567 tp[0] = time (0); /* make sure read comes after all that */
568 utime (stream->mailbox,tp);
569 }
570 }
573 /* MTX mail per-message modify flags
574 * Accepts: MAIL stream
575 * message cache element
576 */
578 void mtx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
579 {
580 struct stat sbuf;
581 /* maybe need to do a checkpoint? */
582 if (LOCAL->filetime && !LOCAL->shouldcheck) {
583 fstat (LOCAL->fd,&sbuf); /* get current write time */
584 if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
585 LOCAL->filetime = 0; /* don't do this test for any other messages */
586 }
587 /* recalculate status */
588 mtx_update_status (stream,elt->msgno,NIL);
589 }
591 /* MTX mail ping mailbox
592 * Accepts: MAIL stream
593 * Returns: T if stream still alive, NIL if not
594 */
596 long mtx_ping (MAILSTREAM *stream)
597 {
598 unsigned long i = 1;
599 long r = T;
600 int ld;
601 char lock[MAILTMPLEN];
602 struct stat sbuf;
603 if (stream && LOCAL) { /* only if stream already open */
604 fstat (LOCAL->fd,&sbuf); /* get current file poop */
605 if (LOCAL->filetime && !(LOCAL->mustcheck || LOCAL->shouldcheck) &&
606 (LOCAL->filetime < sbuf.st_mtime)) LOCAL->shouldcheck = T;
607 /* check for changed message status */
608 if (LOCAL->mustcheck || LOCAL->shouldcheck) {
609 LOCAL->filetime = sbuf.st_mtime;
610 if (LOCAL->shouldcheck) /* babble when we do this unilaterally */
611 MM_NOTIFY (stream,"[CHECK] Checking for flag updates",NIL);
612 while (i <= stream->nmsgs) mtx_elt (stream,i++);
613 LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
614 }
615 /* get shared parse/append permission */
616 if ((sbuf.st_size != LOCAL->filesize) &&
617 ((ld = lockfd (LOCAL->fd,lock,LOCK_SH)) >= 0)) {
618 /* parse resulting mailbox */
619 r = (mtx_parse (stream)) ? T : NIL;
620 unlockfd (ld,lock); /* release shared parse/append permission */
621 }
622 if (LOCAL) { /* stream must still be alive */
623 /* snarf if this is a read-write inbox */
624 if (stream->inbox && !stream->rdonly) {
625 mtx_snarf (stream);
626 fstat (LOCAL->fd,&sbuf);/* see if file changed now */
627 if ((sbuf.st_size != LOCAL->filesize) &&
628 ((ld = lockfd (LOCAL->fd,lock,LOCK_SH)) >= 0)) {
629 /* parse resulting mailbox */
630 r = (mtx_parse (stream)) ? T : NIL;
631 unlockfd (ld,lock); /* release shared parse/append permission */
632 }
633 }
634 }
635 }
636 return r; /* return result of the parse */
637 }
640 /* MTX mail check mailbox (reparses status too)
641 * Accepts: MAIL stream
642 */
644 void mtx_check (MAILSTREAM *stream)
645 {
646 /* mark that a check is desired */
647 if (LOCAL) LOCAL->mustcheck = T;
648 if (mtx_ping (stream)) MM_LOG ("Check completed",(long) NIL);
649 }
651 /* MTX mail snarf messages from system inbox
652 * Accepts: MAIL stream
653 */
655 void mtx_snarf (MAILSTREAM *stream)
656 {
657 unsigned long i = 0;
658 unsigned long j,r,hdrlen,txtlen;
659 struct stat sbuf;
660 char *hdr,*txt,lock[MAILTMPLEN],tmp[MAILTMPLEN];
661 MESSAGECACHE *elt;
662 MAILSTREAM *sysibx = NIL;
663 int ld;
664 /* give up if can't get exclusive permission */
665 if ((time (0) >= (LOCAL->lastsnarf +
666 (long) mail_parameters (NIL,GET_SNARFINTERVAL,NIL))) &&
667 strcmp (sysinbox (),stream->mailbox) &&
668 ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) >= 0)) {
669 MM_CRITICAL (stream); /* go critical */
670 /* sizes match and anything in sysinbox? */
671 if (!stat (sysinbox (),&sbuf) && sbuf.st_size &&
672 !fstat (LOCAL->fd,&sbuf) && (sbuf.st_size == LOCAL->filesize) &&
673 (sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) &&
674 (!sysibx->rdonly) && (r = sysibx->nmsgs)) {
675 /* yes, go to end of file in our mailbox */
676 lseek (LOCAL->fd,sbuf.st_size,L_SET);
677 /* for each message in sysibx mailbox */
678 while (r && (++i <= sysibx->nmsgs)) {
679 /* snarf message from system INBOX */
680 hdr = cpystr (mail_fetchheader_full (sysibx,i,NIL,&hdrlen,NIL));
681 txt = mail_fetchtext_full (sysibx,i,&txtlen,FT_PEEK);
682 /* if have a message */
683 if (j = hdrlen + txtlen) {
684 /* calculate header line */
685 mail_date (LOCAL->buf,elt = mail_elt (sysibx,i));
686 sprintf (LOCAL->buf + strlen (LOCAL->buf),
687 ",%lu;0000000000%02o\015\012",j,(unsigned)
688 ((fSEEN * elt->seen) + (fDELETED * elt->deleted) +
689 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
690 (fDRAFT * elt->draft)));
691 /* copy message */
692 if ((write (LOCAL->fd,LOCAL->buf,strlen (LOCAL->buf)) < 0) ||
693 (write (LOCAL->fd,hdr,hdrlen) < 0) ||
694 (write (LOCAL->fd,txt,txtlen) < 0)) r = 0;
695 }
696 fs_give ((void **) &hdr);
697 }
699 /* make sure all the updates take */
700 if (fsync (LOCAL->fd)) r = 0;
701 if (r) { /* delete all the messages we copied */
702 if (r == 1) strcpy (tmp,"1");
703 else sprintf (tmp,"1:%lu",r);
704 mail_flag (sysibx,tmp,"\\Deleted",ST_SET);
705 mail_expunge (sysibx); /* now expunge all those messages */
706 }
707 else {
708 sprintf (LOCAL->buf,"Can't copy new mail: %s",strerror (errno));
709 MM_LOG (LOCAL->buf,WARN);
710 ftruncate (LOCAL->fd,sbuf.st_size);
711 }
712 fstat (LOCAL->fd,&sbuf); /* yes, get current file size */
713 LOCAL->filetime = sbuf.st_mtime;
714 }
715 if (sysibx) mail_close (sysibx);
716 MM_NOCRITICAL (stream); /* release critical */
717 unlockfd (ld,lock); /* release exclusive parse/append permission */
718 LOCAL->lastsnarf = time (0);/* note time of last snarf */
719 }
720 }
722 /* MTX mail expunge mailbox
723 * Accepts: MAIL stream
724 * sequence to expunge if non-NIL
725 * expunge options
726 * Returns: T, always
727 */
729 long mtx_expunge (MAILSTREAM *stream,char *sequence,long options)
730 {
731 long ret;
732 time_t tp[2];
733 struct stat sbuf;
734 off_t pos = 0;
735 int ld;
736 unsigned long i = 1;
737 unsigned long j,k,m,recent;
738 unsigned long n = 0;
739 unsigned long delta = 0;
740 char lock[MAILTMPLEN];
741 MESSAGECACHE *elt;
742 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
743 if (!(ret = (sequence ? ((options & EX_UID) ?
744 mail_uid_sequence (stream,sequence) :
745 mail_sequence (stream,sequence)) : LONGT) &&
746 mtx_ping (stream))); /* parse sequence if given, ping stream */
747 else if (stream->rdonly) MM_LOG ("Expunge ignored on readonly mailbox",WARN);
748 else {
749 if (LOCAL->filetime && !LOCAL->shouldcheck) {
750 fstat (LOCAL->fd,&sbuf); /* get current write time */
751 if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
752 }
753 /* The cretins who designed flock() created a window of vulnerability in
754 * upgrading locks from shared to exclusive or downgrading from exclusive
755 * to shared. Rather than maintain the lock at shared status at a minimum,
756 * flock() actually *releases* the former lock. Obviously they never talked
757 * to any database guys. Fortunately, we have the parse/append permission
758 * lock. If we require this lock before going exclusive on the mailbox,
759 * another process can not sneak in and steal the exclusive mailbox lock on
760 * us, because it will block on trying to get parse/append permission first.
761 */
762 /* get exclusive parse/append permission */
763 if ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) < 0)
764 MM_LOG ("Unable to lock expunge mailbox",ERROR);
765 /* make sure see any newly-arrived messages */
766 else if (!mtx_parse (stream));
767 /* get exclusive access */
768 else if (flock (LOCAL->fd,LOCK_EX|LOCK_NB)) {
769 (*bn) (BLOCK_FILELOCK,NIL);
770 flock (LOCAL->fd,LOCK_SH);/* recover previous lock */
771 (*bn) (BLOCK_NONE,NIL);
772 MM_LOG ("Can't expunge because mailbox is in use by another process",
773 ERROR);
774 unlockfd (ld,lock); /* release exclusive parse/append permission */
775 }
777 else {
778 MM_CRITICAL (stream); /* go critical */
779 recent = stream->recent; /* get recent now that pinged and locked */
780 /* for each message */
781 while (i <= stream->nmsgs) {
782 /* get cache element */
783 elt = mtx_elt (stream,i);
784 /* number of bytes to smash or preserve */
785 k = elt->private.special.text.size + elt->rfc822_size;
786 /* if need to expunge this message */
787 if (elt->deleted && (sequence ? elt->sequence : T)) {
788 /* if recent, note one less recent message */
789 if (elt->recent) --recent;
790 delta += k; /* number of bytes to delete */
791 /* notify upper levels */
792 mail_expunged (stream,i);
793 n++; /* count up one more expunged message */
794 }
795 else if (i++ && delta) {/* preserved message */
796 /* first byte to preserve */
797 j = elt->private.special.offset;
798 do { /* read from source position */
799 m = min (k,LOCAL->buflen);
800 lseek (LOCAL->fd,j,L_SET);
801 read (LOCAL->fd,LOCAL->buf,m);
802 pos = j - delta; /* write to destination position */
803 lseek (LOCAL->fd,pos,L_SET);
804 while (T) {
805 lseek (LOCAL->fd,pos,L_SET);
806 if (write (LOCAL->fd,LOCAL->buf,m) > 0) break;
807 MM_NOTIFY (stream,strerror (errno),WARN);
808 MM_DISKERROR (stream,errno,T);
809 }
810 pos += m; /* new position */
811 j += m; /* next chunk, perhaps */
812 } while (k -= m); /* until done */
813 /* note the new address of this text */
814 elt->private.special.offset -= delta;
815 }
816 /* preserved but no deleted messages */
817 else pos = elt->private.special.offset + k;
818 }
819 if (n) { /* truncate file after last message */
820 if (pos != (LOCAL->filesize -= delta)) {
821 sprintf (LOCAL->buf,
822 "Calculated size mismatch %lu != %lu, delta = %lu",
823 (unsigned long) pos,(unsigned long) LOCAL->filesize,delta);
824 MM_LOG (LOCAL->buf,WARN);
825 LOCAL->filesize = pos;/* fix it then */
826 }
827 ftruncate (LOCAL->fd,LOCAL->filesize);
828 sprintf (LOCAL->buf,"Expunged %lu messages",n);
829 /* output the news */
830 MM_LOG (LOCAL->buf,(long) NIL);
831 }
832 else MM_LOG ("No messages deleted, so no update needed",(long) NIL);
833 fsync (LOCAL->fd); /* force disk update */
834 fstat (LOCAL->fd,&sbuf); /* get new write time */
835 tp[1] = LOCAL->filetime = sbuf.st_mtime;
836 tp[0] = time (0); /* reset atime to now */
837 utime (stream->mailbox,tp);
838 MM_NOCRITICAL (stream); /* release critical */
839 /* notify upper level of new mailbox size */
840 mail_exists (stream,stream->nmsgs);
841 mail_recent (stream,recent);
842 (*bn) (BLOCK_FILELOCK,NIL);
843 flock (LOCAL->fd,LOCK_SH);/* allow sharers again */
844 (*bn) (BLOCK_NONE,NIL);
845 unlockfd (ld,lock); /* release exclusive parse/append permission */
846 }
847 }
848 return ret;
849 }
851 /* MTX mail copy message(s)
852 * Accepts: MAIL stream
853 * sequence
854 * destination mailbox
855 * copy options
856 * Returns: T if success, NIL if failed
857 */
859 long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
860 {
861 struct stat sbuf;
862 time_t tp[2];
863 MESSAGECACHE *elt;
864 unsigned long i,j,k;
865 long ret = LONGT;
866 int fd,ld;
867 char file[MAILTMPLEN],lock[MAILTMPLEN];
868 mailproxycopy_t pc =
869 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
870 /* make sure valid mailbox */
871 if (!mtx_isvalid (mailbox,LOCAL->buf)) switch (errno) {
872 case ENOENT: /* no such file? */
873 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
874 return NIL;
875 case 0: /* merely empty file? */
876 break;
877 case EACCES: /* file protected */
878 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
879 MM_LOG (LOCAL->buf,ERROR);
880 return NIL;
881 case EINVAL:
882 if (pc) return (*pc) (stream,sequence,mailbox,options);
883 sprintf (LOCAL->buf,"Invalid MTX-format mailbox name: %.80s",mailbox);
884 MM_LOG (LOCAL->buf,ERROR);
885 return NIL;
886 default:
887 if (pc) return (*pc) (stream,sequence,mailbox,options);
888 sprintf (LOCAL->buf,"Not a MTX-format mailbox: %.80s",mailbox);
889 MM_LOG (LOCAL->buf,ERROR);
890 return NIL;
891 }
892 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
893 mail_sequence (stream,sequence))) return NIL;
894 /* got file? */
895 if ((fd = open (mtx_file (file,mailbox),O_RDWR,NIL)) < 0) {
896 sprintf (LOCAL->buf,"Unable to open copy mailbox: %s",strerror (errno));
897 MM_LOG (LOCAL->buf,ERROR);
898 return NIL;
899 }
900 MM_CRITICAL (stream); /* go critical */
901 /* get exclusive parse/append permission */
902 if (flock (fd,LOCK_SH) || ((ld = lockfd (fd,lock,LOCK_EX)) < 0)) {
903 MM_LOG ("Unable to lock copy mailbox",ERROR);
904 MM_NOCRITICAL (stream);
905 return NIL;
906 }
907 fstat (fd,&sbuf); /* get current file size */
908 lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
910 /* for each requested message */
911 for (i = 1; ret && (i <= stream->nmsgs); i++)
912 if ((elt = mail_elt (stream,i))->sequence) {
913 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
914 /* number of bytes to copy */
915 k = elt->private.special.text.size + elt->rfc822_size;
916 do { /* read from source position */
917 j = min (k,LOCAL->buflen);
918 read (LOCAL->fd,LOCAL->buf,j);
919 if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
920 } while (ret && (k -= j));/* until done */
921 }
922 /* make sure all the updates take */
923 if (!(ret && (ret = !fsync (fd)))) {
924 sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
925 MM_LOG (LOCAL->buf,ERROR);
926 ftruncate (fd,sbuf.st_size);
927 }
928 if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */
929 /* else preserve \Marked status */
930 else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
931 tp[1] = sbuf.st_mtime; /* preserve mtime */
932 utime (file,tp); /* set the times */
933 close (fd); /* close the file */
934 unlockfd (ld,lock); /* release exclusive parse/append permission */
935 MM_NOCRITICAL (stream); /* release critical */
936 /* delete all requested messages */
937 if (ret && (options & CP_MOVE)) {
938 for (i = 1; i <= stream->nmsgs; i++)
939 if ((elt = mtx_elt (stream,i))->sequence) {
940 elt->deleted = T; /* mark message deleted */
941 /* recalculate status */
942 mtx_update_status (stream,i,NIL);
943 }
944 if (!stream->rdonly) { /* make sure the update takes */
945 fsync (LOCAL->fd);
946 fstat (LOCAL->fd,&sbuf); /* get current write time */
947 tp[1] = LOCAL->filetime = sbuf.st_mtime;
948 tp[0] = time (0); /* make sure atime remains greater */
949 utime (stream->mailbox,tp);
950 }
951 }
952 if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
953 MM_LOG ("Can not return meaningful COPYUID with this mailbox format",WARN);
954 return ret;
955 }
957 /* MTX mail append message from stringstruct
958 * Accepts: MAIL stream
959 * destination mailbox
960 * append callback
961 * data for callback
962 * Returns: T if append successful, else NIL
963 */
965 long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
966 {
967 struct stat sbuf;
968 int fd,ld,c;
969 char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
970 time_t tp[2];
971 FILE *df;
972 MESSAGECACHE elt;
973 long f;
974 unsigned long i,uf;
975 STRING *message;
976 long ret = LONGT;
977 /* default stream to prototype */
978 if (!stream) stream = user_flags (&mtxproto);
979 /* make sure valid mailbox */
980 if (!mtx_isvalid (mailbox,tmp)) switch (errno) {
981 case ENOENT: /* no such file? */
982 if (!compare_cstring (mailbox,"INBOX")) dummy_create (NIL,"INBOX.MTX");
983 else {
984 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
985 return NIL;
986 }
987 /* falls through */
988 case 0: /* merely empty file? */
989 break;
990 case EACCES: /* file protected */
991 sprintf (tmp,"Can't access destination: %.80s",mailbox);
992 MM_LOG (tmp,ERROR);
993 return NIL;
994 case EINVAL:
995 sprintf (tmp,"Invalid MTX-format mailbox name: %.80s",mailbox);
996 MM_LOG (tmp,ERROR);
997 return NIL;
998 default:
999 sprintf (tmp,"Not a MTX-format mailbox: %.80s",mailbox);
1000 MM_LOG (tmp,ERROR);
1001 return NIL;
1003 /* get first message */
1004 if (!MM_APPEND (af) (stream,data,&flags,&date,&message)) return NIL;
1006 /* open destination mailbox */
1007 if (((fd = open (mtx_file (file,mailbox),O_WRONLY|O_APPEND,NIL)) < 0) ||
1008 !(df = fdopen (fd,"ab"))) {
1009 sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
1010 MM_LOG (tmp,ERROR);
1011 return NIL;
1013 /* get parse/append permission */
1014 if (flock (fd,LOCK_SH) || ((ld = lockfd (fd,lock,LOCK_EX)) < 0)) {
1015 MM_LOG ("Unable to lock append mailbox",ERROR);
1016 close (fd);
1017 return NIL;
1019 MM_CRITICAL (stream); /* go critical */
1020 fstat (fd,&sbuf); /* get current file size */
1021 errno = 0;
1022 do { /* parse flags */
1023 if (!SIZE (message)) { /* guard against zero-length */
1024 MM_LOG ("Append of zero-length message",ERROR);
1025 ret = NIL;
1026 break;
1028 f = mail_parse_flags (stream,flags,&i);
1029 /* reverse bits (dontcha wish we had CIRC?) */
1030 for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));
1031 if (date) { /* parse date if given */
1032 if (!mail_parse_date (&elt,date)) {
1033 sprintf (tmp,"Bad date in append: %.80s",date);
1034 MM_LOG (tmp,ERROR);
1035 ret = NIL; /* mark failure */
1036 break;
1038 mail_date (tmp,&elt); /* write preseved date */
1040 else internal_date (tmp); /* get current date in IMAP format */
1041 /* write header */
1042 if (fprintf (df,"%s,%lu;%010lo%02lo\015\012",tmp,i = SIZE (message),uf,
1043 (unsigned long) f) < 0) ret = NIL;
1044 else { /* write message */
1045 if (i) do c = 0xff & SNX (message);
1046 while ((putc (c,df) != EOF) && --i);
1047 /* get next message */
1048 if (i || !MM_APPEND (af) (stream,data,&flags,&date,&message)) ret = NIL;
1050 } while (ret && message);
1051 /* if error... */
1052 if (!ret || (fflush (df) == EOF)) {
1053 ftruncate (fd,sbuf.st_size);/* revert file */
1054 close (fd); /* make sure fclose() doesn't corrupt us */
1055 if (errno) {
1056 sprintf (tmp,"Message append failed: %s",strerror (errno));
1057 MM_LOG (tmp,ERROR);
1059 ret = NIL;
1061 if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */
1062 /* else preserve \Marked status */
1063 else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
1064 tp[1] = sbuf.st_mtime; /* preserve mtime */
1065 utime (file,tp); /* set the times */
1066 fclose (df); /* close the file */
1067 unlockfd (ld,lock); /* release exclusive parse/append permission */
1068 MM_NOCRITICAL (stream); /* release critical */
1069 if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))
1070 MM_LOG ("Can not return meaningful APPENDUID with this mailbox format",
1071 WARN);
1072 return ret;
1075 /* Internal routines */
1078 /* MTX mail generate file string
1079 * Accepts: temporary buffer to write into
1080 * mailbox name string
1081 * Returns: local file string or NIL if failure
1082 */
1084 char *mtx_file (char *dst,char *name)
1086 char tmp[MAILTMPLEN];
1087 char *s = mailboxfile (dst,name);
1088 /* return our standard inbox */
1089 return (s && !*s) ? mailboxfile (dst,mtx_isvalid ("~/INBOX",tmp) ?
1090 "~/INBOX" : "INBOX.MTX") : s;
1093 /* MTX mail parse mailbox
1094 * Accepts: MAIL stream
1095 * Returns: T if parse OK
1096 * NIL if failure, stream aborted
1097 */
1099 long mtx_parse (MAILSTREAM *stream)
1101 struct stat sbuf;
1102 MESSAGECACHE *elt = NIL;
1103 unsigned char c,*s,*t,*x;
1104 char tmp[MAILTMPLEN];
1105 unsigned long i,j;
1106 long curpos = LOCAL->filesize;
1107 long nmsgs = stream->nmsgs;
1108 long recent = stream->recent;
1109 short added = NIL;
1110 short silent = stream->silent;
1111 fstat (LOCAL->fd,&sbuf); /* get status */
1112 if (sbuf.st_size < curpos) { /* sanity check */
1113 sprintf (tmp,"Mailbox shrank from %lu to %lu!",
1114 (unsigned long) curpos,(unsigned long) sbuf.st_size);
1115 MM_LOG (tmp,ERROR);
1116 mtx_close (stream,NIL);
1117 return NIL;
1119 stream->silent = T; /* don't pass up exists events yet */
1120 while (sbuf.st_size - curpos){/* while there is stuff to parse */
1121 /* get to that position in the file */
1122 lseek (LOCAL->fd,curpos,L_SET);
1123 if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
1124 sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
1125 (unsigned long) curpos,(unsigned long) sbuf.st_size,
1126 i ? strerror (errno) : "no data read");
1127 MM_LOG (tmp,ERROR);
1128 mtx_close (stream,NIL);
1129 return NIL;
1131 LOCAL->buf[i] = '\0'; /* tie off buffer just in case */
1132 if (!((s = strchr (LOCAL->buf,'\015')) && (s[1] == '\012'))) {
1133 sprintf (tmp,"Unable to find CRLF at %lu in %lu bytes, text: %s",
1134 (unsigned long) curpos,i,(char *) LOCAL->buf);
1135 MM_LOG (tmp,ERROR);
1136 mtx_close (stream,NIL);
1137 return NIL;
1139 *s = '\0'; /* tie off header line */
1140 i = (s + 2) - LOCAL->buf; /* note start of text offset */
1141 if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
1142 sprintf (tmp,"Unable to parse internal header at %lu: %s",
1143 (unsigned long) curpos,(char *) LOCAL->buf);
1144 MM_LOG (tmp,ERROR);
1145 mtx_close (stream,NIL);
1146 return NIL;
1148 *s++ = '\0'; *t++ = '\0'; /* tie off fields */
1150 added = T; /* note that a new message was added */
1151 /* swell the cache */
1152 mail_exists (stream,++nmsgs);
1153 /* instantiate an elt for this message */
1154 (elt = mail_elt (stream,nmsgs))->valid = T;
1155 elt->private.uid = ++stream->uid_last;
1156 /* note file offset of header */
1157 elt->private.special.offset = curpos;
1158 /* in case error */
1159 elt->private.special.text.size = 0;
1160 /* header size not known yet */
1161 elt->private.msg.header.text.size = 0;
1162 x = s; /* parse the header components */
1163 if (mail_parse_date (elt,LOCAL->buf) &&
1164 (elt->rfc822_size = strtoul (s,(char **) &s,10)) && (!(s && *s)) &&
1165 isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&
1166 isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&
1167 isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&
1168 isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])
1169 elt->private.special.text.size = i;
1170 else { /* oops */
1171 sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",
1172 curpos,(char *) LOCAL->buf,(char *) x,(char *) t);
1173 MM_LOG (tmp,ERROR);
1174 mtx_close (stream,NIL);
1175 return NIL;
1177 /* make sure didn't run off end of file */
1178 if ((curpos += (elt->rfc822_size + i)) > sbuf.st_size) {
1179 sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
1180 elt->private.special.offset,(unsigned long) curpos,
1181 (unsigned long) sbuf.st_size);
1182 MM_LOG (tmp,ERROR);
1183 mtx_close (stream,NIL);
1184 return NIL;
1186 c = t[10]; /* remember first system flags byte */
1187 t[10] = '\0'; /* tie off flags */
1188 j = strtoul (t,NIL,8); /* get user flags value */
1189 t[10] = c; /* restore first system flags byte */
1190 /* set up all valid user flags (reversed!) */
1191 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1192 stream->user_flags[i]) elt->user_flags |= 1 << i;
1193 /* calculate system flags */
1194 if ((j = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;
1195 if (j & fDELETED) elt->deleted = T;
1196 if (j & fFLAGGED) elt->flagged = T;
1197 if (j & fANSWERED) elt->answered = T;
1198 if (j & fDRAFT) elt->draft = T;
1199 if (!(j & fOLD)) { /* newly arrived message? */
1200 elt->recent = T;
1201 recent++; /* count up a new recent message */
1202 /* mark it as old */
1203 mtx_update_status (stream,nmsgs,NIL);
1206 fsync (LOCAL->fd); /* make sure all the fOLD flags take */
1207 /* update parsed file size and time */
1208 LOCAL->filesize = sbuf.st_size;
1209 fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */
1210 LOCAL->filetime = sbuf.st_mtime;
1211 if (added && !stream->rdonly){/* make sure atime updated */
1212 time_t tp[2];
1213 tp[0] = time (0);
1214 tp[1] = LOCAL->filetime;
1215 utime (stream->mailbox,tp);
1217 stream->silent = silent; /* can pass up events now */
1218 mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */
1219 mail_recent (stream,recent); /* and of change in recent messages */
1220 return LONGT; /* return the winnage */
1223 /* MTX get cache element with status updating from file
1224 * Accepts: MAIL stream
1225 * message number
1226 * Returns: cache element
1227 */
1229 MESSAGECACHE *mtx_elt (MAILSTREAM *stream,unsigned long msgno)
1231 MESSAGECACHE *elt = mail_elt (stream,msgno);
1232 struct { /* old flags */
1233 unsigned int seen : 1;
1234 unsigned int deleted : 1;
1235 unsigned int flagged : 1;
1236 unsigned int answered : 1;
1237 unsigned int draft : 1;
1238 unsigned long user_flags;
1239 } old;
1240 old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
1241 old.answered = elt->answered; old.draft = elt->draft;
1242 old.user_flags = elt->user_flags;
1243 mtx_read_flags (stream,elt);
1244 if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
1245 (old.flagged != elt->flagged) || (old.answered != elt->answered) ||
1246 (old.draft != elt->draft) || (old.user_flags != elt->user_flags))
1247 MM_FLAGS (stream,msgno); /* let top level know */
1248 return elt;
1251 /* MTX read flags from file
1252 * Accepts: MAIL stream
1253 * Returns: cache element
1254 */
1256 void mtx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
1258 unsigned long i,j;
1259 /* noop if readonly and have valid flags */
1260 if (stream->rdonly && elt->valid) return;
1261 /* set the seek pointer */
1262 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1263 elt->private.special.text.size - 14,L_SET);
1264 /* read the new flags */
1265 if (read (LOCAL->fd,LOCAL->buf,12) < 0) {
1266 sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
1267 fatal (LOCAL->buf);
1269 /* calculate system flags */
1270 i = (((LOCAL->buf[10]-'0') * 8) + LOCAL->buf[11]-'0');
1271 elt->seen = i & fSEEN ? T : NIL; elt->deleted = i & fDELETED ? T : NIL;
1272 elt->flagged = i & fFLAGGED ? T : NIL;
1273 elt->answered = i & fANSWERED ? T : NIL; elt->draft = i & fDRAFT ? T : NIL;
1274 LOCAL->buf[10] = '\0'; /* tie off flags */
1275 j = strtoul(LOCAL->buf,NIL,8);/* get user flags value */
1276 /* set up all valid user flags (reversed!) */
1277 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
1278 stream->user_flags[i]) elt->user_flags |= 1 << i;
1279 elt->valid = T; /* have valid flags now */
1282 /* MTX update status string
1283 * Accepts: MAIL stream
1284 * message number
1285 * flag saying whether or not to sync
1286 */
1288 void mtx_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
1290 time_t tp[2];
1291 struct stat sbuf;
1292 MESSAGECACHE *elt = mail_elt (stream,msgno);
1293 unsigned long j,k = 0;
1294 /* readonly */
1295 if (stream->rdonly || !elt->valid) mtx_read_flags (stream,elt);
1296 else { /* readwrite */
1297 j = elt->user_flags; /* get user flags */
1298 /* reverse bits (dontcha wish we had CIRC?) */
1299 while (j) k |= 1 << (29 - find_rightmost_bit (&j));
1300 /* print new flag string */
1301 sprintf (LOCAL->buf,"%010lo%02o",k,(unsigned)
1302 (fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
1303 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
1304 (fDRAFT * elt->draft)));
1305 /* get to that place in the file */
1306 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
1307 elt->private.special.text.size - 14,L_SET);
1308 /* write new flags */
1309 write (LOCAL->fd,LOCAL->buf,12);
1310 if (syncflag) { /* sync if requested */
1311 fsync (LOCAL->fd);
1312 fstat (LOCAL->fd,&sbuf); /* get new write time */
1313 tp[1] = LOCAL->filetime = sbuf.st_mtime;
1314 tp[0] = time (0); /* make sure read is later */
1315 utime (stream->mailbox,tp);
1320 /* MTX locate header for a message
1321 * Accepts: MAIL stream
1322 * message number
1323 * pointer to returned header size
1324 * Returns: position of header in file
1325 */
1327 unsigned long mtx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
1328 unsigned long *size)
1330 unsigned long siz;
1331 long i = 0;
1332 int q = 0;
1333 char *s,tmp[MAILTMPLEN];
1334 MESSAGECACHE *elt = mtx_elt (stream,msgno);
1335 unsigned long ret = elt->private.special.offset +
1336 elt->private.special.text.size;
1337 /* is header size known? */
1338 if (!(*size = elt->private.msg.header.text.size)) {
1339 lseek (LOCAL->fd,ret,L_SET);/* get to header position */
1340 /* search message for CRLF CRLF */
1341 for (siz = 1,s = tmp; siz <= elt->rfc822_size; siz++) {
1342 /* read another buffer as necessary */
1343 if ((--i <= 0) && /* buffer empty? */
1344 (read (LOCAL->fd,s = tmp,
1345 i = min (elt->rfc822_size - siz,(long) MAILTMPLEN)) < 0))
1346 return ret; /* I/O error? */
1347 switch (q) { /* sniff at buffer */
1348 case 0: /* first character */
1349 q = (*s++ == '\015') ? 1 : 0;
1350 break;
1351 case 1: /* second character */
1352 q = (*s++ == '\012') ? 2 : 0;
1353 break;
1354 case 2: /* third character */
1355 q = (*s++ == '\015') ? 3 : 0;
1356 break;
1357 case 3: /* fourth character */
1358 if (*s++ == '\012') { /* have the sequence? */
1359 /* yes, note for later */
1360 elt->private.msg.header.text.size = *size = siz;
1361 return ret;
1363 q = 0; /* lost... */
1364 break;
1367 /* header consumes entire message */
1368 elt->private.msg.header.text.size = *size = elt->rfc822_size;
1370 return ret;

UW-IMAP'd extensions by yuuji