imapext-2007

view src/osdep/unix/mx.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-2008 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: MX mail routines
16 *
17 * Author(s): 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: 3 May 1996
26 * Last Edited: 6 January 2008
27 */
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <errno.h>
33 extern int errno; /* just in case */
34 #include "mail.h"
35 #include "osdep.h"
36 #include <pwd.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include "misc.h"
40 #include "dummy.h"
41 #include "fdstring.h"
43 /* Index file */
45 #define MXINDEXNAME "/.mxindex"
46 #define MXINDEX(d,s) strcat (mx_file (d,s),MXINDEXNAME)
49 /* MX I/O stream local data */
51 typedef struct mx_local {
52 int fd; /* file descriptor of open index */
53 unsigned char *buf; /* temporary buffer */
54 unsigned long buflen; /* current size of temporary buffer */
55 unsigned long cachedtexts; /* total size of all cached texts */
56 time_t scantime; /* last time directory scanned */
57 } MXLOCAL;
60 /* Convenient access to local data */
62 #define LOCAL ((MXLOCAL *) stream->local)
65 /* Function prototypes */
67 DRIVER *mx_valid (char *name);
68 int mx_isvalid (char *name,char *tmp);
69 int mx_namevalid (char *name);
70 void *mx_parameters (long function,void *value);
71 long mx_dirfmttest (char *name);
72 void mx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
73 long mx_scan_contents (char *name,char *contents,unsigned long csiz,
74 unsigned long fsiz);
75 void mx_list (MAILSTREAM *stream,char *ref,char *pat);
76 void mx_lsub (MAILSTREAM *stream,char *ref,char *pat);
77 long mx_subscribe (MAILSTREAM *stream,char *mailbox);
78 long mx_unsubscribe (MAILSTREAM *stream,char *mailbox);
79 long mx_create (MAILSTREAM *stream,char *mailbox);
80 long mx_delete (MAILSTREAM *stream,char *mailbox);
81 long mx_rename (MAILSTREAM *stream,char *old,char *newname);
82 int mx_rename_work (char *src,size_t srcl,char *dst,size_t dstl,char *name);
83 MAILSTREAM *mx_open (MAILSTREAM *stream);
84 void mx_close (MAILSTREAM *stream,long options);
85 void mx_fast (MAILSTREAM *stream,char *sequence,long flags);
86 char *mx_fast_work (MAILSTREAM *stream,MESSAGECACHE *elt);
87 char *mx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
88 long flags);
89 long mx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
90 void mx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
91 void mx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
92 long mx_ping (MAILSTREAM *stream);
93 void mx_check (MAILSTREAM *stream);
94 long mx_expunge (MAILSTREAM *stream,char *sequence,long options);
95 long mx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,
96 long options);
97 long mx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
98 long mx_append_msg (MAILSTREAM *stream,char *flags,MESSAGECACHE *elt,
99 STRING *st,SEARCHSET *set);
101 int mx_select (struct direct *name);
102 int mx_numsort (const void *d1,const void *d2);
103 char *mx_file (char *dst,char *name);
104 long mx_lockindex (MAILSTREAM *stream);
105 void mx_unlockindex (MAILSTREAM *stream);
106 void mx_setdate (char *file,MESSAGECACHE *elt);
109 /* MX mail routines */
112 /* Driver dispatch used by MAIL */
114 DRIVER mxdriver = {
115 "mx", /* driver name */
116 /* driver flags */
117 DR_MAIL|DR_LOCAL|DR_NOFAST|DR_CRLF|DR_LOCKING|DR_DIRFMT,
118 (DRIVER *) NIL, /* next driver */
119 mx_valid, /* mailbox is valid for us */
120 mx_parameters, /* manipulate parameters */
121 mx_scan, /* scan mailboxes */
122 mx_list, /* find mailboxes */
123 mx_lsub, /* find subscribed mailboxes */
124 mx_subscribe, /* subscribe to mailbox */
125 mx_unsubscribe, /* unsubscribe from mailbox */
126 mx_create, /* create mailbox */
127 mx_delete, /* delete mailbox */
128 mx_rename, /* rename mailbox */
129 mail_status_default, /* status of mailbox */
130 mx_open, /* open mailbox */
131 mx_close, /* close mailbox */
132 mx_fast, /* fetch message "fast" attributes */
133 NIL, /* fetch message flags */
134 NIL, /* fetch overview */
135 NIL, /* fetch message envelopes */
136 mx_header, /* fetch message header only */
137 mx_text, /* fetch message body only */
138 NIL, /* fetch partial message test */
139 NIL, /* unique identifier */
140 NIL, /* message number */
141 mx_flag, /* modify flags */
142 mx_flagmsg, /* per-message modify flags */
143 NIL, /* search for message based on criteria */
144 NIL, /* sort messages */
145 NIL, /* thread messages */
146 mx_ping, /* ping mailbox to see if still alive */
147 mx_check, /* check for new messages */
148 mx_expunge, /* expunge deleted messages */
149 mx_copy, /* copy messages to another mailbox */
150 mx_append, /* append string message to mailbox */
151 NIL /* garbage collect stream */
152 };
154 /* prototype stream */
155 MAILSTREAM mxproto = {&mxdriver};
157 /* MX mail validate mailbox
158 * Accepts: mailbox name
159 * Returns: our driver if name is valid, NIL otherwise
160 */
162 DRIVER *mx_valid (char *name)
163 {
164 char tmp[MAILTMPLEN];
165 return mx_isvalid (name,tmp) ? &mxdriver : NIL;
166 }
169 /* MX mail test for valid mailbox
170 * Accepts: mailbox name
171 * temporary buffer to use
172 * Returns: T if valid, NIL otherwise with errno holding dir stat error
173 */
175 int mx_isvalid (char *name,char *tmp)
176 {
177 struct stat sbuf;
178 errno = NIL; /* zap error */
179 if ((strlen (name) <= NETMAXMBX) && *mx_file (tmp,name) &&
180 !stat (tmp,&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFDIR)) {
181 /* name is directory; is it mx? */
182 if (!stat (MXINDEX (tmp,name),&sbuf) &&
183 ((sbuf.st_mode & S_IFMT) == S_IFREG)) return T;
184 errno = NIL; /* directory but not mx */
185 }
186 else if (!compare_cstring (name,"INBOX")) errno = NIL;
187 return NIL;
188 }
191 /* MX mail test for valid mailbox
192 * Accepts: mailbox name
193 * Returns: T if valid, NIL otherwise
194 */
196 int mx_namevalid (char *name)
197 {
198 char *s = (*name == '/') ? name + 1 : name;
199 while (s && *s) { /* make sure valid name */
200 if (isdigit (*s)) s++; /* digit, check this node further... */
201 else if (*s == '/') break; /* all digit node, barf */
202 /* non-digit, skip to next node or return */
203 else if (!((s = strchr (s+1,'/')) && *++s)) return T;
204 }
205 return NIL; /* all numeric or empty node */
206 }
208 /* MX manipulate driver parameters
209 * Accepts: function code
210 * function-dependent value
211 * Returns: function-dependent return value
212 */
214 void *mx_parameters (long function,void *value)
215 {
216 void *ret = NIL;
217 switch ((int) function) {
218 case GET_INBOXPATH:
219 if (value) ret = mailboxfile ((char *) value,"~/INBOX");
220 break;
221 case GET_DIRFMTTEST:
222 ret = (void *) mx_dirfmttest;
223 break;
224 case GET_SCANCONTENTS:
225 ret = (void *) mx_scan_contents;
226 break;
227 }
228 return ret;
229 }
232 /* MX test for directory format internal node
233 * Accepts: candidate node name
234 * Returns: T if internal name, NIL otherwise
235 */
237 long mx_dirfmttest (char *name)
238 {
239 int c;
240 /* success if index name or all-numberic */
241 if (strcmp (name,MXINDEXNAME+1))
242 while (c = *name++) if (!isdigit (c)) return NIL;
243 return LONGT;
244 }
246 /* MX scan mailboxes
247 * Accepts: mail stream
248 * reference
249 * pattern to search
250 * string to scan
251 */
253 void mx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
254 {
255 if (stream) dummy_scan (NIL,ref,pat,contents);
256 }
259 /* MX scan mailbox for contents
260 * Accepts: mailbox name
261 * desired contents
262 * contents size
263 * file size (ignored)
264 * Returns: NIL if contents not found, T if found
265 */
267 long mx_scan_contents (char *name,char *contents,unsigned long csiz,
268 unsigned long fsiz)
269 {
270 long i,nfiles;
271 void *a;
272 char *s;
273 long ret = NIL;
274 size_t namelen = strlen (name);
275 struct stat sbuf;
276 struct direct **names = NIL;
277 if ((nfiles = scandir (name,&names,mx_select,mx_numsort)) > 0)
278 for (i = 0; i < nfiles; ++i) {
279 if (!ret) {
280 sprintf (s = (char *) fs_get (namelen + strlen (names[i]->d_name) + 2),
281 "%s/%s",name,names[i]->d_name);
282 if (!stat (s,&sbuf) && (csiz <= sbuf.st_size))
283 ret = dummy_scan_contents (s,contents,csiz,sbuf.st_size);
284 fs_give ((void **) &s);
285 }
286 fs_give ((void **) &names[i]);
287 }
288 /* free directory list */
289 if (a = (void *) names) fs_give ((void **) &a);
290 return ret;
291 }
293 /* MX list mailboxes
294 * Accepts: mail stream
295 * reference
296 * pattern to search
297 */
299 void mx_list (MAILSTREAM *stream,char *ref,char *pat)
300 {
301 if (stream) dummy_list (NIL,ref,pat);
302 }
305 /* MX list subscribed mailboxes
306 * Accepts: mail stream
307 * reference
308 * pattern to search
309 */
311 void mx_lsub (MAILSTREAM *stream,char *ref,char *pat)
312 {
313 if (stream) dummy_lsub (NIL,ref,pat);
314 }
316 /* MX mail subscribe to mailbox
317 * Accepts: mail stream
318 * mailbox to add to subscription list
319 * Returns: T on success, NIL on failure
320 */
322 long mx_subscribe (MAILSTREAM *stream,char *mailbox)
323 {
324 return sm_subscribe (mailbox);
325 }
328 /* MX mail unsubscribe to mailbox
329 * Accepts: mail stream
330 * mailbox to delete from subscription list
331 * Returns: T on success, NIL on failure
332 */
334 long mx_unsubscribe (MAILSTREAM *stream,char *mailbox)
335 {
336 return sm_unsubscribe (mailbox);
337 }
339 /* MX mail create mailbox
340 * Accepts: mail stream
341 * mailbox name to create
342 * Returns: T on success, NIL on failure
343 */
345 long mx_create (MAILSTREAM *stream,char *mailbox)
346 {
347 DRIVER *test;
348 int fd;
349 char *s,tmp[MAILTMPLEN];
350 int mask = umask (0);
351 long ret = NIL;
352 if (!mx_namevalid (mailbox)) /* validate name */
353 sprintf (tmp,"Can't create mailbox %.80s: invalid MX-format name",mailbox);
354 /* must not already exist */
355 else if ((test = mail_valid (NIL,mailbox,NIL)) &&
356 strcmp (test->name,"dummy"))
357 sprintf (tmp,"Can't create mailbox %.80s: mailbox already exists",mailbox);
358 /* create directory */
359 else if (!dummy_create_path (stream,MXINDEX (tmp,mailbox),
360 get_dir_protection (mailbox)))
361 sprintf (tmp,"Can't create mailbox %.80s: %s",mailbox,strerror (errno));
362 else { /* success */
363 /* set index protection */
364 set_mbx_protections (mailbox,tmp);
365 /* tie off directory name */
366 *(s = strrchr (tmp,'/') + 1) = '\0';
367 /* set directory protection */
368 set_mbx_protections (mailbox,tmp);
369 ret = LONGT;
370 }
371 umask (mask); /* restore mask */
372 if (!ret) MM_LOG (tmp,ERROR); /* some error */
373 return ret;
374 }
376 /* MX mail delete mailbox
377 * mailbox name to delete
378 * Returns: T on success, NIL on failure
379 */
381 long mx_delete (MAILSTREAM *stream,char *mailbox)
382 {
383 DIR *dirp;
384 struct direct *d;
385 char *s;
386 char tmp[MAILTMPLEN];
387 if (!mx_isvalid (mailbox,tmp))
388 sprintf (tmp,"Can't delete mailbox %.80s: no such mailbox",mailbox);
389 /* delete index */
390 else if (unlink (MXINDEX (tmp,mailbox)))
391 sprintf (tmp,"Can't delete mailbox %.80s index: %s",
392 mailbox,strerror (errno));
393 else { /* get directory name */
394 *(s = strrchr (tmp,'/')) = '\0';
395 if (dirp = opendir (tmp)) { /* open directory */
396 *s++ = '/'; /* restore delimiter */
397 /* massacre messages */
398 while (d = readdir (dirp)) if (mx_select (d)) {
399 strcpy (s,d->d_name); /* make path */
400 unlink (tmp); /* sayonara */
401 }
402 closedir (dirp); /* flush directory */
403 *(s = strrchr (tmp,'/')) = '\0';
404 if (rmdir (tmp)) { /* try to remove the directory */
405 sprintf (tmp,"Can't delete name %.80s: %s",mailbox,strerror (errno));
406 MM_LOG (tmp,WARN);
407 }
408 }
409 return T; /* always success */
410 }
411 MM_LOG (tmp,ERROR); /* something failed */
412 return NIL;
413 }
415 /* MX mail rename mailbox
416 * Accepts: MX mail stream
417 * old mailbox name
418 * new mailbox name
419 * Returns: T on success, NIL on failure
420 */
422 long mx_rename (MAILSTREAM *stream,char *old,char *newname)
423 {
424 char c,*s,tmp[MAILTMPLEN],tmp1[MAILTMPLEN];
425 struct stat sbuf;
426 if (!mx_isvalid (old,tmp))
427 sprintf (tmp,"Can't rename mailbox %.80s: no such mailbox",old);
428 else if (!mx_namevalid (newname))
429 sprintf (tmp,"Can't rename to mailbox %.80s: invalid MX-format name",
430 newname);
431 /* new mailbox name must not be valid */
432 else if (mx_isvalid (newname,tmp))
433 sprintf (tmp,"Can't rename to mailbox %.80s: destination already exists",
434 newname);
435 else {
436 mx_file (tmp,old); /* build old directory name */
437 mx_file (tmp1,newname); /* and new directory name */
438 /* easy if not INBOX */
439 if (compare_cstring (old,"INBOX")) {
440 /* found superior to destination name? */
441 if (s = strrchr (mx_file (tmp1,newname),'/')) {
442 c = *++s; /* remember first character of inferior */
443 *s = '\0'; /* tie off to get just superior */
444 /* name doesn't exist, create it */
445 if ((stat (tmp1,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
446 !dummy_create_path (stream,tmp1,get_dir_protection (newname)))
447 return NIL;
448 *s = c; /* restore full name */
449 }
450 if (!rename (tmp,tmp1)) return LONGT;
451 }
453 /* RFC 3501 requires this */
454 else if (dummy_create_path (stream,strcat (tmp1,"/"),
455 get_dir_protection (newname))) {
456 void *a;
457 int i,n,lasterror;
458 struct direct **names = NIL;
459 size_t srcl = strlen (tmp);
460 size_t dstl = strlen (tmp1);
461 /* rename each mx file to new directory */
462 for (i = lasterror = 0,n = scandir (tmp,&names,mx_select,mx_numsort);
463 i < n; ++i) {
464 if (mx_rename_work (tmp,srcl,tmp1,dstl,names[i]->d_name))
465 lasterror = errno;
466 fs_give ((void **) &names[i]);
467 }
468 /* free directory list */
469 if (a = (void *) names) fs_give ((void **) &a);
470 if (lasterror || mx_rename_work (tmp,srcl,tmp1,dstl,MXINDEXNAME+1))
471 errno = lasterror;
472 else return mx_create (NIL,"INBOX");
473 }
474 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",
475 old,newname,strerror (errno));
476 }
477 MM_LOG (tmp,ERROR); /* something failed */
478 return NIL;
479 }
482 /* MX rename worker routine
483 * Accepts: source directory name
484 * source directory name length
485 * destination directory name
486 * destination directory name length
487 * name of node to move
488 * Returns: zero if success, non-zero if error
489 */
491 int mx_rename_work (char *src,size_t srcl,char *dst,size_t dstl,char *name)
492 {
493 int ret;
494 size_t len = strlen (name);
495 char *s = (char *) fs_get (srcl + len + 2);
496 char *d = (char *) fs_get (dstl + len + 1);
497 sprintf (s,"%s/%s",src,name);
498 sprintf (d,"%s%s",dst,name);
499 ret = rename (s,d);
500 fs_give ((void **) &s);
501 fs_give ((void **) &d);
502 return ret;
503 }
505 /* MX mail open
506 * Accepts: stream to open
507 * Returns: stream on success, NIL on failure
508 */
510 MAILSTREAM *mx_open (MAILSTREAM *stream)
511 {
512 char tmp[MAILTMPLEN];
513 /* return prototype for OP_PROTOTYPE call */
514 if (!stream) return user_flags (&mxproto);
515 if (stream->local) fatal ("mx recycle stream");
516 stream->local = fs_get (sizeof (MXLOCAL));
517 /* note if an INBOX or not */
518 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
519 mx_file (tmp,stream->mailbox);/* get directory name */
520 /* canonicalize mailbox name */
521 fs_give ((void **) &stream->mailbox);
522 stream->mailbox = cpystr (tmp);
523 /* make temporary buffer */
524 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
525 LOCAL->buflen = CHUNKSIZE - 1;
526 LOCAL->scantime = 0; /* not scanned yet */
527 LOCAL->fd = -1; /* no index yet */
528 LOCAL->cachedtexts = 0; /* no cached texts */
529 stream->sequence++; /* bump sequence number */
530 /* parse mailbox */
531 stream->nmsgs = stream->recent = 0;
532 if (mx_ping (stream) && !(stream->nmsgs || stream->silent))
533 MM_LOG ("Mailbox is empty",(long) NIL);
534 stream->perm_seen = stream->perm_deleted = stream->perm_flagged =
535 stream->perm_answered = stream->perm_draft = stream->rdonly ? NIL : T;
536 stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
537 stream->kwd_create = (stream->user_flags[NUSERFLAGS-1] || stream->rdonly) ?
538 NIL : T; /* can we create new user flags? */
539 return stream; /* return stream to caller */
540 }
542 /* MX mail close
543 * Accepts: MAIL stream
544 * close options
545 */
547 void mx_close (MAILSTREAM *stream,long options)
548 {
549 if (LOCAL) { /* only if a file is open */
550 int silent = stream->silent;
551 stream->silent = T; /* note this stream is dying */
552 if (options & CL_EXPUNGE) mx_expunge (stream,NIL,NIL);
553 /* free local scratch buffer */
554 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
555 /* nuke the local data */
556 fs_give ((void **) &stream->local);
557 stream->dtb = NIL; /* log out the DTB */
558 stream->silent = silent; /* reset silent state */
559 }
560 }
562 /* MX mail fetch fast information
563 * Accepts: MAIL stream
564 * sequence
565 * option flags
566 */
568 void mx_fast (MAILSTREAM *stream,char *sequence,long flags)
569 {
570 unsigned long i;
571 MESSAGECACHE *elt;
572 if (stream && LOCAL &&
573 ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
574 mail_sequence (stream,sequence)))
575 for (i = 1; i <= stream->nmsgs; i++)
576 if ((elt = mail_elt (stream,i))->sequence) mx_fast_work (stream,elt);
577 }
580 /* MX mail fetch fast information
581 * Accepts: MAIL stream
582 * message cache element
583 * Returns: name of message file
584 */
586 char *mx_fast_work (MAILSTREAM *stream,MESSAGECACHE *elt)
587 {
588 struct stat sbuf;
589 struct tm *tm;
590 /* build message file name */
591 sprintf (LOCAL->buf,"%s/%lu",stream->mailbox,elt->private.uid);
592 /* have size yet? */
593 if (!elt->rfc822_size && !stat (LOCAL->buf,&sbuf)) {
594 /* make plausible IMAPish date string */
595 tm = gmtime (&sbuf.st_mtime);
596 elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
597 elt->year = tm->tm_year + 1900 - BASEYEAR;
598 elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
599 elt->seconds = tm->tm_sec;
600 elt->zhours = 0; elt->zminutes = 0; elt->zoccident = 0;
601 elt->rfc822_size = sbuf.st_size;
602 }
603 return (char *) LOCAL->buf; /* return file name */
604 }
606 /* MX mail fetch message header
607 * Accepts: MAIL stream
608 * message # to fetch
609 * pointer to returned header text length
610 * option flags
611 * Returns: message header in RFC822 format
612 */
614 char *mx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
615 long flags)
616 {
617 unsigned long i;
618 int fd;
619 MESSAGECACHE *elt;
620 *length = 0; /* default to empty */
621 if (flags & FT_UID) return "";/* UID call "impossible" */
622 elt = mail_elt (stream,msgno);/* get elt */
623 if (!elt->private.msg.header.text.data) {
624 /* purge cache if too big */
625 if (LOCAL->cachedtexts > max (stream->nmsgs * 4096,2097152)) {
626 mail_gc (stream,GC_TEXTS);/* just can't keep that much */
627 LOCAL->cachedtexts = 0;
628 }
629 if ((fd = open (mx_fast_work (stream,elt),O_RDONLY,NIL)) < 0) return "";
630 /* is buffer big enough? */
631 if (elt->rfc822_size > LOCAL->buflen) {
632 fs_give ((void **) &LOCAL->buf);
633 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = elt->rfc822_size) + 1);
634 }
635 /* slurp message */
636 read (fd,LOCAL->buf,elt->rfc822_size);
637 /* tie off file */
638 LOCAL->buf[elt->rfc822_size] = '\0';
639 close (fd); /* flush message file */
640 /* find end of header */
641 if (elt->rfc822_size < 4) i = 0;
642 else for (i = 4; (i < elt->rfc822_size) &&
643 !((LOCAL->buf[i - 4] == '\015') &&
644 (LOCAL->buf[i - 3] == '\012') &&
645 (LOCAL->buf[i - 2] == '\015') &&
646 (LOCAL->buf[i - 1] == '\012')); i++);
647 /* copy header */
648 cpytxt (&elt->private.msg.header.text,LOCAL->buf,i);
649 cpytxt (&elt->private.msg.text.text,LOCAL->buf+i,elt->rfc822_size - i);
650 /* add to cached size */
651 LOCAL->cachedtexts += elt->rfc822_size;
652 }
653 *length = elt->private.msg.header.text.size;
654 return (char *) elt->private.msg.header.text.data;
655 }
657 /* MX mail fetch message text (body only)
658 * Accepts: MAIL stream
659 * message # to fetch
660 * pointer to returned stringstruct
661 * option flags
662 * Returns: T on success, NIL on failure
663 */
665 long mx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
666 {
667 unsigned long i;
668 MESSAGECACHE *elt;
669 /* UID call "impossible" */
670 if (flags & FT_UID) return NIL;
671 elt = mail_elt (stream,msgno);
672 /* snarf message if don't have it yet */
673 if (!elt->private.msg.text.text.data) {
674 mx_header (stream,msgno,&i,flags);
675 if (!elt->private.msg.text.text.data) return NIL;
676 }
677 /* mark as seen */
678 if (!(flags & FT_PEEK) && mx_lockindex (stream)) {
679 elt->seen = T;
680 mx_unlockindex (stream);
681 MM_FLAGS (stream,msgno);
682 }
683 INIT (bs,mail_string,elt->private.msg.text.text.data,
684 elt->private.msg.text.text.size);
685 return T;
686 }
688 /* MX mail modify flags
689 * Accepts: MAIL stream
690 * sequence
691 * flag(s)
692 * option flags
693 */
695 void mx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
696 {
697 mx_unlockindex (stream); /* finished with index */
698 }
701 /* MX per-message modify flags
702 * Accepts: MAIL stream
703 * message cache element
704 */
706 void mx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
707 {
708 mx_lockindex (stream); /* lock index if not already locked */
709 }
711 /* MX mail ping mailbox
712 * Accepts: MAIL stream
713 * Returns: T if stream alive, else NIL
714 */
716 long mx_ping (MAILSTREAM *stream)
717 {
718 MAILSTREAM *sysibx = NIL;
719 MESSAGECACHE *elt,*selt;
720 struct stat sbuf;
721 char *s,tmp[MAILTMPLEN];
722 int fd;
723 unsigned long i,j,r,old;
724 long nmsgs = stream->nmsgs;
725 long recent = stream->recent;
726 int silent = stream->silent;
727 if (stat (stream->mailbox,&sbuf)) return NIL;
728 stream->silent = T; /* don't pass up exists events yet */
729 if (sbuf.st_ctime != LOCAL->scantime) {
730 struct direct **names = NIL;
731 long nfiles = scandir (stream->mailbox,&names,mx_select,mx_numsort);
732 if (nfiles < 0) nfiles = 0; /* in case error */
733 old = stream->uid_last;
734 /* note scanned now */
735 LOCAL->scantime = sbuf.st_ctime;
736 /* scan directory */
737 for (i = 0; i < nfiles; ++i) {
738 /* if newly seen, add to list */
739 if ((j = atoi (names[i]->d_name)) > old) {
740 /* swell the cache */
741 mail_exists (stream,++nmsgs);
742 stream->uid_last = (elt = mail_elt (stream,nmsgs))->private.uid = j;
743 elt->valid = T; /* note valid flags */
744 if (old) { /* other than the first pass? */
745 elt->recent = T; /* yup, mark as recent */
746 recent++; /* bump recent count */
747 }
748 }
749 fs_give ((void **) &names[i]);
750 }
751 /* free directory */
752 if (s = (void *) names) fs_give ((void **) &s);
753 }
754 stream->nmsgs = nmsgs; /* don't upset mail_uid() */
756 /* if INBOX, snarf from system INBOX */
757 if (mx_lockindex (stream) && stream->inbox &&
758 !strcmp (sysinbox (),stream->mailbox)) {
759 old = stream->uid_last;
760 MM_CRITICAL (stream); /* go critical */
761 /* see if anything in system inbox */
762 if (!stat (sysinbox (),&sbuf) && sbuf.st_size &&
763 (sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) &&
764 !sysibx->rdonly && (r = sysibx->nmsgs)) {
765 for (i = 1; i <= r; ++i) {/* for each message in sysinbox mailbox */
766 /* build file name we will use */
767 sprintf (LOCAL->buf,"%s/%lu",stream->mailbox,++old);
768 /* snarf message from Berkeley mailbox */
769 selt = mail_elt (sysibx,i);
770 if (((fd = open (LOCAL->buf,O_WRONLY|O_CREAT|O_EXCL,
771 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL)))
772 >= 0) &&
773 (s = mail_fetchheader_full (sysibx,i,NIL,&j,FT_PEEK)) &&
774 (write (fd,s,j) == j) &&
775 (s = mail_fetchtext_full (sysibx,i,&j,FT_PEEK)) &&
776 (write (fd,s,j) == j) && !fsync (fd) && !close (fd)) {
777 /* swell the cache */
778 mail_exists (stream,++nmsgs);
779 stream->uid_last = /* create new elt, note its file number */
780 (elt = mail_elt (stream,nmsgs))->private.uid = old;
781 recent++; /* bump recent count */
782 /* set up initial flags and date */
783 elt->valid = elt->recent = T;
784 elt->seen = selt->seen;
785 elt->deleted = selt->deleted;
786 elt->flagged = selt->flagged;
787 elt->answered = selt->answered;
788 elt->draft = selt->draft;
789 elt->day = selt->day;elt->month = selt->month;elt->year = selt->year;
790 elt->hours = selt->hours;elt->minutes = selt->minutes;
791 elt->seconds = selt->seconds;
792 elt->zhours = selt->zhours; elt->zminutes = selt->zminutes;
793 elt->zoccident = selt->zoccident;
794 mx_setdate (LOCAL->buf,elt);
795 sprintf (tmp,"%lu",i);/* delete it from the sysinbox */
796 mail_flag (sysibx,tmp,"\\Deleted",ST_SET);
797 }
798 else { /* failed to snarf */
799 if (fd) { /* did it ever get opened? */
800 close (fd); /* close descriptor */
801 unlink (LOCAL->buf);/* flush this file */
802 }
803 sprintf (tmp,"Message copy to MX mailbox failed: %.80s",
804 s,strerror (errno));
805 MM_LOG (tmp,ERROR);
806 r = 0; /* stop the snarf in its tracks */
807 }
808 }
809 /* update scan time */
810 if (!stat (stream->mailbox,&sbuf)) LOCAL->scantime = sbuf.st_ctime;
811 mail_expunge (sysibx); /* now expunge all those messages */
812 }
813 if (sysibx) mail_close (sysibx);
814 MM_NOCRITICAL (stream); /* release critical */
815 }
816 mx_unlockindex (stream); /* done with index */
817 stream->silent = silent; /* can pass up events now */
818 mail_exists (stream,nmsgs); /* notify upper level of mailbox size */
819 mail_recent (stream,recent);
820 return T; /* return that we are alive */
821 }
823 /* MX mail check mailbox
824 * Accepts: MAIL stream
825 */
827 void mx_check (MAILSTREAM *stream)
828 {
829 if (mx_ping (stream)) MM_LOG ("Check completed",(long) NIL);
830 }
833 /* MX mail expunge mailbox
834 * Accepts: MAIL stream
835 * sequence to expunge if non-NIL
836 * expunge options
837 * Returns: T, always
838 */
840 long mx_expunge (MAILSTREAM *stream,char *sequence,long options)
841 {
842 long ret;
843 MESSAGECACHE *elt;
844 unsigned long i = 1;
845 unsigned long n = 0;
846 unsigned long recent = stream->recent;
847 if (ret = (sequence ? ((options & EX_UID) ?
848 mail_uid_sequence (stream,sequence) :
849 mail_sequence (stream,sequence)) : LONGT) &&
850 mx_lockindex (stream)) { /* lock the index */
851 MM_CRITICAL (stream); /* go critical */
852 while (i <= stream->nmsgs) {/* for each message */
853 elt = mail_elt (stream,i);/* if deleted, need to trash it */
854 if (elt->deleted && (sequence ? elt->sequence : T)) {
855 sprintf (LOCAL->buf,"%s/%lu",stream->mailbox,elt->private.uid);
856 if (unlink (LOCAL->buf)) {/* try to delete the message */
857 sprintf (LOCAL->buf,"Expunge of message %lu failed, aborted: %s",i,
858 strerror (errno));
859 MM_LOG (LOCAL->buf,(long) NIL);
860 break;
861 }
862 /* note uncached */
863 LOCAL->cachedtexts -= ((elt->private.msg.header.text.data ?
864 elt->private.msg.header.text.size : 0) +
865 (elt->private.msg.text.text.data ?
866 elt->private.msg.text.text.size : 0));
867 mail_gc_msg (&elt->private.msg,GC_ENV | GC_TEXTS);
868 if(elt->recent)--recent;/* if recent, note one less recent message */
869 mail_expunged(stream,i);/* notify upper levels */
870 n++; /* count up one more expunged message */
871 }
872 else i++; /* otherwise try next message */
873 }
874 if (n) { /* output the news if any expunged */
875 sprintf (LOCAL->buf,"Expunged %lu messages",n);
876 MM_LOG (LOCAL->buf,(long) NIL);
877 }
878 else MM_LOG ("No messages deleted, so no update needed",(long) NIL);
879 MM_NOCRITICAL (stream); /* release critical */
880 mx_unlockindex (stream); /* finished with index */
881 /* notify upper level of new mailbox size */
882 mail_exists (stream,stream->nmsgs);
883 mail_recent (stream,recent);
884 }
885 return ret;
886 }
888 /* MX mail copy message(s)
889 * Accepts: MAIL stream
890 * sequence
891 * destination mailbox
892 * copy options
893 * Returns: T if copy successful, else NIL
894 */
896 long mx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
897 {
898 FDDATA d;
899 STRING st;
900 MESSAGECACHE *elt;
901 MAILSTREAM *astream;
902 struct stat sbuf;
903 int fd;
904 unsigned long i,j,uid,uidv;
905 char *t,tmp[MAILTMPLEN];
906 long ret;
907 mailproxycopy_t pc =
908 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
909 /* make sure valid mailbox */
910 if (!mx_valid (mailbox)) switch (errno) {
911 case NIL: /* no error in stat() */
912 if (pc) return (*pc) (stream,sequence,mailbox,options);
913 sprintf (LOCAL->buf,"Not a MX-format mailbox: %.80s",mailbox);
914 MM_LOG (LOCAL->buf,ERROR);
915 return NIL;
916 default: /* some stat() error */
917 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
918 return NIL;
919 }
920 /* copy the messages */
921 if (!(ret = ((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
922 mail_sequence (stream,sequence))));
923 /* acquire stream to append to */
924 else if (!(astream = mail_open (NIL,mailbox,OP_SILENT))) {
925 MM_LOG ("Can't open copy mailbox",ERROR);
926 ret = NIL;
927 }
928 else {
929 MM_CRITICAL (stream); /* go critical */
930 if (!(ret = mx_lockindex (astream)))
931 MM_LOG ("Message copy failed: unable to lock index",ERROR);
932 else {
934 copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL);
935 SEARCHSET *source = cu ? mail_newsearchset () : NIL;
936 SEARCHSET *dest = cu ? mail_newsearchset () : NIL;
937 for (i = 1,uid = uidv = 0; ret && (i <= stream->nmsgs); i++)
938 if ((elt = mail_elt (stream,i))->sequence) {
939 if (ret = ((fd = open (mx_fast_work (stream,elt),O_RDONLY,NIL))
940 >= 0)) {
941 fstat (fd,&sbuf); /* get size of message */
942 d.fd = fd; /* set up file descriptor */
943 d.pos = 0; /* start of file */
944 d.chunk = LOCAL->buf;
945 d.chunksize = CHUNKSIZE;
946 INIT (&st,fd_string,&d,sbuf.st_size);
947 /* init flag string */
948 tmp[0] = tmp[1] = '\0';
949 if (j = elt->user_flags) do
950 if (t = stream->user_flags[find_rightmost_bit (&j)])
951 strcat (strcat (tmp," "),t);
952 while (j);
953 if (elt->seen) strcat (tmp," \\Seen");
954 if (elt->deleted) strcat (tmp," \\Deleted");
955 if (elt->flagged) strcat (tmp," \\Flagged");
956 if (elt->answered) strcat (tmp," \\Answered");
957 if (elt->draft) strcat (tmp," \\Draft");
958 tmp[0] = '('; /* open list */
959 strcat (tmp,")"); /* close list */
960 if (ret = mx_append_msg (astream,tmp,elt,&st,dest)) {
961 /* add to source set if needed */
962 if (source) mail_append_set (source,mail_uid (stream,i));
963 /* delete if doing a move */
964 if (options & CP_MOVE) elt->deleted = T;
965 }
966 }
967 }
968 /* return sets if doing COPYUID */
969 if (cu && ret) (*cu) (stream,mailbox,astream->uid_validity,source,dest);
970 else { /* flush any sets we may have built */
971 mail_free_searchset (&source);
972 mail_free_searchset (&dest);
973 }
974 mx_unlockindex (astream); /* unlock index */
975 }
976 MM_NOCRITICAL (stream);
977 mail_close (astream); /* finished with append stream */
978 }
979 return ret; /* return success */
980 }
982 /* MX mail append message from stringstruct
983 * Accepts: MAIL stream
984 * destination mailbox
985 * append callback
986 * data for callback
987 * Returns: T if append successful, else NIL
988 */
990 long mx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
991 {
992 MESSAGECACHE elt;
993 MAILSTREAM *astream;
994 char *flags,*date,tmp[MAILTMPLEN];
995 STRING *message;
996 long ret = LONGT;
997 /* default stream to prototype */
998 if (!stream) stream = user_flags (&mxproto);
999 /* N.B.: can't use LOCAL->buf for tmp */
1000 /* make sure valid mailbox */
1001 if (!mx_isvalid (mailbox,tmp)) switch (errno) {
1002 case ENOENT: /* no such file? */
1003 if (!compare_cstring (mailbox,"INBOX")) mx_create (NIL,"INBOX");
1004 else {
1005 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
1006 return NIL;
1008 /* falls through */
1009 case 0: /* merely empty file? */
1010 break;
1011 case EINVAL:
1012 sprintf (tmp,"Invalid MX-format mailbox name: %.80s",mailbox);
1013 MM_LOG (tmp,ERROR);
1014 return NIL;
1015 default:
1016 sprintf (tmp,"Not a MX-format mailbox: %.80s",mailbox);
1017 MM_LOG (tmp,ERROR);
1018 return NIL;
1021 /* get first message */
1022 if (!MM_APPEND (af) (stream,data,&flags,&date,&message)) return NIL;
1023 if (!(astream = mail_open (NIL,mailbox,OP_SILENT))) {
1024 MM_LOG ("Can't open append mailbox",ERROR);
1025 return NIL;
1027 MM_CRITICAL (astream); /* go critical */
1028 /* lock the index */
1029 if (!(ret = mx_lockindex (astream)))
1030 MM_LOG ("Message append failed: unable to lock index",ERROR);
1031 else {
1032 appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL);
1033 SEARCHSET *dst = au ? mail_newsearchset () : NIL;
1034 do {
1035 /* guard against zero-length */
1036 if (!(ret = SIZE (message)))
1037 MM_LOG ("Append of zero-length message",ERROR);
1038 else if (date && !(ret = mail_parse_date (&elt,date))) {
1039 sprintf (tmp,"Bad date in append: %.80s",date);
1040 MM_LOG (tmp,ERROR);
1042 else ret = mx_append_msg (astream,flags,date ? &elt : NIL,message,dst)&&
1043 MM_APPEND (af) (stream,data,&flags,&date,&message);
1044 } while (ret && message);
1045 /* return sets if doing APPENDUID */
1046 if (au && ret) (*au) (mailbox,astream->uid_validity,dst);
1047 else mail_free_searchset (&dst);
1048 mx_unlockindex (astream); /* unlock index */
1050 MM_NOCRITICAL (astream); /* release critical */
1051 mail_close (astream);
1052 return ret;
1055 /* MX mail append single message
1056 * Accepts: MAIL stream
1057 * flags for new message if non-NIL
1058 * elt with source date if non-NIL
1059 * stringstruct of message text
1060 * searchset to place UID
1061 * Returns: T if success, NIL if failure
1062 */
1064 long mx_append_msg (MAILSTREAM *stream,char *flags,MESSAGECACHE *elt,
1065 STRING *st,SEARCHSET *set)
1067 char tmp[MAILTMPLEN];
1068 int fd;
1069 unsigned long uf;
1070 long f = mail_parse_flags (stream,flags,&uf);
1071 /* make message file name */
1072 sprintf (tmp,"%s/%lu",stream->mailbox,++stream->uid_last);
1073 if ((fd = open (tmp,O_WRONLY|O_CREAT|O_EXCL,
1074 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) < 0) {
1075 sprintf (tmp,"Can't create append message: %s",strerror (errno));
1076 MM_LOG (tmp,ERROR);
1077 return NIL;
1079 while (SIZE (st)) { /* copy the file */
1080 if (st->cursize && (write (fd,st->curpos,st->cursize) < 0)) {
1081 unlink (tmp); /* delete file */
1082 close (fd); /* close the file */
1083 sprintf (tmp,"Message append failed: %s",strerror (errno));
1084 MM_LOG (tmp,ERROR);
1085 return NIL;
1087 SETPOS (st,GETPOS (st) + st->cursize);
1089 close (fd); /* close the file */
1090 if (elt) mx_setdate (tmp,elt);/* set file date */
1091 /* swell the cache */
1092 mail_exists (stream,++stream->nmsgs);
1093 /* copy flags */
1094 mail_append_set (set,(elt = mail_elt (stream,stream->nmsgs))->private.uid =
1095 stream->uid_last);
1096 if (f&fSEEN) elt->seen = T;
1097 if (f&fDELETED) elt->deleted = T;
1098 if (f&fFLAGGED) elt->flagged = T;
1099 if (f&fANSWERED) elt->answered = T;
1100 if (f&fDRAFT) elt->draft = T;
1101 elt->user_flags |= uf;
1102 return LONGT;
1105 /* Internal routines */
1108 /* MX file name selection test
1109 * Accepts: candidate directory entry
1110 * Returns: T to use file name, NIL to skip it
1111 */
1113 int mx_select (struct direct *name)
1115 char c;
1116 char *s = name->d_name;
1117 while (c = *s++) if (!isdigit (c)) return NIL;
1118 return T;
1122 /* MX file name comparision
1123 * Accepts: first candidate directory entry
1124 * second candidate directory entry
1125 * Returns: negative if d1 < d2, 0 if d1 == d2, postive if d1 > d2
1126 */
1128 int mx_numsort (const void *d1,const void *d2)
1130 return atoi ((*(struct direct **) d1)->d_name) -
1131 atoi ((*(struct direct **) d2)->d_name);
1135 /* MX mail build file name
1136 * Accepts: destination string
1137 * source
1138 * Returns: destination
1139 */
1141 char *mx_file (char *dst,char *name)
1143 char *s;
1144 /* empty string if mailboxfile fails */
1145 if (!mailboxfile (dst,name)) *dst = '\0';
1146 /* driver-selected INBOX */
1147 else if (!*dst) mailboxfile (dst,"~/INBOX");
1148 /* tie off unnecessary trailing / */
1149 else if ((s = strrchr (dst,'/')) && !s[1]) *s = '\0';
1150 return dst;
1153 /* MX read and lock index
1154 * Accepts: MAIL stream
1155 * Returns: T if success, NIL if failure
1156 */
1158 long mx_lockindex (MAILSTREAM *stream)
1160 unsigned long uf,sf,uid;
1161 int k = 0;
1162 unsigned long msgno = 1;
1163 struct stat sbuf;
1164 char *s,*t,*idx,tmp[2*MAILTMPLEN];
1165 MESSAGECACHE *elt;
1166 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
1167 if ((LOCAL->fd < 0) && /* get index file, no-op if already have it */
1168 (LOCAL->fd = open (strcat (strcpy (tmp,stream->mailbox),MXINDEXNAME),
1169 O_RDWR|O_CREAT,
1170 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL)))
1171 >= 0) {
1172 (*bn) (BLOCK_FILELOCK,NIL);
1173 flock (LOCAL->fd,LOCK_EX); /* get exclusive lock */
1174 (*bn) (BLOCK_NONE,NIL);
1175 fstat (LOCAL->fd,&sbuf); /* get size of index */
1176 /* slurp index */
1177 read (LOCAL->fd,s = idx = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
1178 idx[sbuf.st_size] = '\0'; /* tie off index */
1179 /* parse index */
1180 if (sbuf.st_size) while (s && *s) switch (*s) {
1181 case 'V': /* UID validity record */
1182 stream->uid_validity = strtoul (s+1,&s,16);
1183 break;
1184 case 'L': /* UID last record */
1185 stream->uid_last = strtoul (s+1,&s,16);
1186 break;
1187 case 'K': /* keyword */
1188 /* find end of keyword */
1189 if (s = strchr (t = ++s,'\n')) {
1190 *s++ = '\0'; /* tie off keyword */
1191 /* copy keyword */
1192 if ((k < NUSERFLAGS) && !stream->user_flags[k] &&
1193 (strlen (t) <= MAXUSERFLAG)) stream->user_flags[k] = cpystr (t);
1194 k++; /* one more keyword */
1196 break;
1198 case 'M': /* message status record */
1199 uid = strtoul (s+1,&s,16);/* get UID for this message */
1200 if (*s == ';') { /* get user flags */
1201 uf = strtoul (s+1,&s,16);
1202 if (*s == '.') { /* get system flags */
1203 sf = strtoul (s+1,&s,16);
1204 while ((msgno <= stream->nmsgs) && (mail_uid (stream,msgno) < uid))
1205 msgno++; /* find message number for this UID */
1206 if ((msgno <= stream->nmsgs) && (mail_uid (stream,msgno) == uid)) {
1207 (elt = mail_elt (stream,msgno))->valid = T;
1208 elt->user_flags=uf; /* set user and system flags in elt */
1209 if (sf & fSEEN) elt->seen = T;
1210 if (sf & fDELETED) elt->deleted = T;
1211 if (sf & fFLAGGED) elt->flagged = T;
1212 if (sf & fANSWERED) elt->answered = T;
1213 if (sf & fDRAFT) elt->draft = T;
1215 break;
1218 default: /* bad news */
1219 sprintf (tmp,"Error in index: %.80s",s);
1220 MM_LOG (tmp,ERROR);
1221 *s = NIL; /* ignore remainder of index */
1223 else { /* new index */
1224 stream->uid_validity = time (0);
1225 user_flags (stream); /* init stream with default user flags */
1227 fs_give ((void **) &idx); /* flush index */
1229 return (LOCAL->fd >= 0) ? T : NIL;
1232 /* MX write and unlock index
1233 * Accepts: MAIL stream
1234 */
1236 #define MXIXBUFLEN 2048
1238 void mx_unlockindex (MAILSTREAM *stream)
1240 unsigned long i,j;
1241 off_t size = 0;
1242 char *s,tmp[MXIXBUFLEN + 64];
1243 MESSAGECACHE *elt;
1244 if (LOCAL->fd >= 0) {
1245 lseek (LOCAL->fd,0,L_SET); /* rewind file */
1246 /* write header */
1247 sprintf (s = tmp,"V%08lxL%08lx",stream->uid_validity,stream->uid_last);
1248 for (i = 0; (i < NUSERFLAGS) && stream->user_flags[i]; ++i)
1249 sprintf (s += strlen (s),"K%s\n",stream->user_flags[i]);
1250 /* write messages */
1251 for (i = 1; i <= stream->nmsgs; i++) {
1252 /* filled buffer? */
1253 if (((s += strlen (s)) - tmp) > MXIXBUFLEN) {
1254 write (LOCAL->fd,tmp,j = s - tmp);
1255 size += j;
1256 *(s = tmp) = '\0'; /* dump out and restart buffer */
1258 elt = mail_elt (stream,i);
1259 sprintf(s,"M%08lx;%08lx.%04x",elt->private.uid,elt->user_flags,(unsigned)
1260 ((fSEEN * elt->seen) + (fDELETED * elt->deleted) +
1261 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
1262 (fDRAFT * elt->draft)));
1264 /* write tail end of buffer */
1265 if ((s += strlen (s)) != tmp) {
1266 write (LOCAL->fd,tmp,j = s - tmp);
1267 size += j;
1269 ftruncate (LOCAL->fd,size);
1270 flock (LOCAL->fd,LOCK_UN); /* unlock the index */
1271 close (LOCAL->fd); /* finished with file */
1272 LOCAL->fd = -1; /* no index now */
1276 /* Set date for message
1277 * Accepts: file name
1278 * elt containing date
1279 */
1281 void mx_setdate (char *file,MESSAGECACHE *elt)
1283 time_t tp[2];
1284 tp[0] = time (0); /* atime is now */
1285 tp[1] = mail_longdate (elt); /* modification time */
1286 utime (file,tp); /* set the times */

UW-IMAP'd extensions by yuuji