1 /* ========================================================================
2 * Copyright 1988-2008 University of Washington
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
15 * Program: MX mail routines
17 * Author(s): Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
23 * Internet: MRC@CAC.Washington.EDU
26 * Last Edited: 6 January 2008
34 extern int errno
; /* just in case */
47 #define MXINDEXNAME "/.mxindex"
48 #define MXINDEX(d,s) strcat (mx_file (d,s),MXINDEXNAME)
51 /* MX I/O stream local data */
53 typedef struct mx_local
{
54 int fd
; /* file descriptor of open index */
55 unsigned char *buf
; /* temporary buffer */
56 unsigned long buflen
; /* current size of temporary buffer */
57 unsigned long cachedtexts
; /* total size of all cached texts */
58 time_t scantime
; /* last time directory scanned */
62 /* Convenient access to local data */
64 #define LOCAL ((MXLOCAL *) stream->local)
67 /* Function prototypes */
69 DRIVER
*mx_valid (char *name
);
70 int mx_isvalid (char *name
,char *tmp
);
71 int mx_namevalid (char *name
);
72 void *mx_parameters (long function
,void *value
);
73 long mx_dirfmttest (char *name
);
74 void mx_scan (MAILSTREAM
*stream
,char *ref
,char *pat
,char *contents
);
75 long mx_scan_contents (char *name
,char *contents
,unsigned long csiz
,
77 void mx_list (MAILSTREAM
*stream
,char *ref
,char *pat
);
78 void mx_lsub (MAILSTREAM
*stream
,char *ref
,char *pat
);
79 long mx_subscribe (MAILSTREAM
*stream
,char *mailbox
);
80 long mx_unsubscribe (MAILSTREAM
*stream
,char *mailbox
);
81 long mx_create (MAILSTREAM
*stream
,char *mailbox
);
82 long mx_delete (MAILSTREAM
*stream
,char *mailbox
);
83 long mx_rename (MAILSTREAM
*stream
,char *old
,char *newname
);
84 int mx_rename_work (char *src
,size_t srcl
,char *dst
,size_t dstl
,char *name
);
85 MAILSTREAM
*mx_open (MAILSTREAM
*stream
);
86 void mx_close (MAILSTREAM
*stream
,long options
);
87 void mx_fast (MAILSTREAM
*stream
,char *sequence
,long flags
);
88 char *mx_fast_work (MAILSTREAM
*stream
,MESSAGECACHE
*elt
);
89 char *mx_header (MAILSTREAM
*stream
,unsigned long msgno
,unsigned long *length
,
91 long mx_text (MAILSTREAM
*stream
,unsigned long msgno
,STRING
*bs
,long flags
);
92 void mx_flag (MAILSTREAM
*stream
,char *sequence
,char *flag
,long flags
);
93 void mx_flagmsg (MAILSTREAM
*stream
,MESSAGECACHE
*elt
);
94 long mx_ping (MAILSTREAM
*stream
);
95 void mx_check (MAILSTREAM
*stream
);
96 long mx_expunge (MAILSTREAM
*stream
,char *sequence
,long options
);
97 long mx_copy (MAILSTREAM
*stream
,char *sequence
,char *mailbox
,
99 long mx_append (MAILSTREAM
*stream
,char *mailbox
,append_t af
,void *data
);
100 long mx_append_msg (MAILSTREAM
*stream
,char *flags
,MESSAGECACHE
*elt
,
101 STRING
*st
,SEARCHSET
*set
);
103 int mx_select (const struct direct
*name
);
104 int mx_numsort (const struct direct
**d1
,const struct direct
**d2
);
105 char *mx_file (char *dst
,char *name
);
106 long mx_lockindex (MAILSTREAM
*stream
);
107 void mx_unlockindex (MAILSTREAM
*stream
);
108 void mx_setdate (char *file
,MESSAGECACHE
*elt
);
111 /* MX mail routines */
114 /* Driver dispatch used by MAIL */
117 "mx", /* driver name */
119 DR_MAIL
|DR_LOCAL
|DR_NOFAST
|DR_CRLF
|DR_LOCKING
|DR_DIRFMT
,
120 (DRIVER
*) NIL
, /* next driver */
121 mx_valid
, /* mailbox is valid for us */
122 mx_parameters
, /* manipulate parameters */
123 mx_scan
, /* scan mailboxes */
124 mx_list
, /* find mailboxes */
125 mx_lsub
, /* find subscribed mailboxes */
126 mx_subscribe
, /* subscribe to mailbox */
127 mx_unsubscribe
, /* unsubscribe from mailbox */
128 mx_create
, /* create mailbox */
129 mx_delete
, /* delete mailbox */
130 mx_rename
, /* rename mailbox */
131 mail_status_default
, /* status of mailbox */
132 mx_open
, /* open mailbox */
133 mx_close
, /* close mailbox */
134 mx_fast
, /* fetch message "fast" attributes */
135 NIL
, /* fetch message flags */
136 NIL
, /* fetch overview */
137 NIL
, /* fetch message envelopes */
138 mx_header
, /* fetch message header only */
139 mx_text
, /* fetch message body only */
140 NIL
, /* fetch partial message test */
141 NIL
, /* unique identifier */
142 NIL
, /* message number */
143 mx_flag
, /* modify flags */
144 mx_flagmsg
, /* per-message modify flags */
145 NIL
, /* search for message based on criteria */
146 NIL
, /* sort messages */
147 NIL
, /* thread messages */
148 mx_ping
, /* ping mailbox to see if still alive */
149 mx_check
, /* check for new messages */
150 mx_expunge
, /* expunge deleted messages */
151 mx_copy
, /* copy messages to another mailbox */
152 mx_append
, /* append string message to mailbox */
153 NIL
, /* garbage collect stream */
154 NIL
/* renew stream */
157 /* prototype stream */
158 MAILSTREAM mxproto
= {&mxdriver
};
160 /* MX mail validate mailbox
161 * Accepts: mailbox name
162 * Returns: our driver if name is valid, NIL otherwise
165 DRIVER
*mx_valid (char *name
)
167 char tmp
[MAILTMPLEN
];
168 return mx_isvalid (name
,tmp
) ? &mxdriver
: NIL
;
172 /* MX mail test for valid mailbox
173 * Accepts: mailbox name
174 * temporary buffer to use
175 * Returns: T if valid, NIL otherwise with errno holding dir stat error
178 int mx_isvalid (char *name
,char *tmp
)
181 errno
= NIL
; /* zap error */
182 if ((strlen (name
) <= NETMAXMBX
) && *mx_file (tmp
,name
) &&
183 !stat (tmp
,&sbuf
) && ((sbuf
.st_mode
& S_IFMT
) == S_IFDIR
)) {
184 /* name is directory; is it mx? */
185 if (!stat (MXINDEX (tmp
,name
),&sbuf
) &&
186 ((sbuf
.st_mode
& S_IFMT
) == S_IFREG
)) return T
;
187 errno
= NIL
; /* directory but not mx */
189 else if (!compare_cstring (name
,"INBOX")) errno
= NIL
;
194 /* MX mail test for valid mailbox
195 * Accepts: mailbox name
196 * Returns: T if valid, NIL otherwise
199 int mx_namevalid (char *name
)
201 char *s
= (*name
== '/') ? name
+ 1 : name
;
202 while (s
&& *s
) { /* make sure valid name */
203 if (isdigit (*s
)) s
++; /* digit, check this node further... */
204 else if (*s
== '/') break; /* all digit node, barf */
205 /* non-digit, skip to next node or return */
206 else if (!((s
= strchr (s
+1,'/')) && *++s
)) return T
;
208 return NIL
; /* all numeric or empty node */
211 /* MX manipulate driver parameters
212 * Accepts: function code
213 * function-dependent value
214 * Returns: function-dependent return value
217 void *mx_parameters (long function
,void *value
)
220 switch ((int) function
) {
222 if (value
) ret
= mailboxfile ((char *) value
,"~/INBOX");
225 ret
= (void *) mx_dirfmttest
;
227 case GET_SCANCONTENTS
:
228 ret
= (void *) mx_scan_contents
;
235 /* MX test for directory format internal node
236 * Accepts: candidate node name
237 * Returns: T if internal name, NIL otherwise
240 long mx_dirfmttest (char *name
)
243 /* success if index name or all-numberic */
244 if (strcmp (name
,MXINDEXNAME
+1))
245 while ((c
= *name
++) != '\0') if (!isdigit (c
)) return NIL
;
250 * Accepts: mail stream
256 void mx_scan (MAILSTREAM
*stream
,char *ref
,char *pat
,char *contents
)
258 if (stream
) dummy_scan (NIL
,ref
,pat
,contents
);
262 /* MX scan mailbox for contents
263 * Accepts: mailbox name
266 * file size (ignored)
267 * Returns: NIL if contents not found, T if found
270 long mx_scan_contents (char *name
,char *contents
,unsigned long csiz
,
277 size_t namelen
= strlen (name
);
279 struct direct
**names
= NIL
;
280 if ((nfiles
= scandir (name
,&names
,mx_select
,mx_numsort
)) > 0)
281 for (i
= 0; i
< nfiles
; ++i
) {
283 sprintf (s
= (char *) fs_get (namelen
+ strlen (names
[i
]->d_name
) + 2),
284 "%s/%s",name
,names
[i
]->d_name
);
285 if (!stat (s
,&sbuf
) && (csiz
<= sbuf
.st_size
))
286 ret
= dummy_scan_contents (s
,contents
,csiz
,sbuf
.st_size
);
287 fs_give ((void **) &s
);
289 fs_give ((void **) &names
[i
]);
291 /* free directory list */
292 if ((a
= (void *) names
) != NULL
) fs_give ((void **) &a
);
297 * Accepts: mail stream
302 void mx_list (MAILSTREAM
*stream
,char *ref
,char *pat
)
304 if (stream
) dummy_list (NIL
,ref
,pat
);
308 /* MX list subscribed mailboxes
309 * Accepts: mail stream
314 void mx_lsub (MAILSTREAM
*stream
,char *ref
,char *pat
)
316 if (stream
) dummy_lsub (NIL
,ref
,pat
);
319 /* MX mail subscribe to mailbox
320 * Accepts: mail stream
321 * mailbox to add to subscription list
322 * Returns: T on success, NIL on failure
325 long mx_subscribe (MAILSTREAM
*stream
,char *mailbox
)
327 return sm_subscribe (mailbox
);
331 /* MX mail unsubscribe to mailbox
332 * Accepts: mail stream
333 * mailbox to delete from subscription list
334 * Returns: T on success, NIL on failure
337 long mx_unsubscribe (MAILSTREAM
*stream
,char *mailbox
)
339 return sm_unsubscribe (mailbox
);
342 /* MX mail create mailbox
343 * Accepts: mail stream
344 * mailbox name to create
345 * Returns: T on success, NIL on failure
348 long mx_create (MAILSTREAM
*stream
,char *mailbox
)
352 char *s
,tmp
[MAILTMPLEN
];
353 int mask
= umask (0);
355 if (!mx_namevalid (mailbox
)) /* validate name */
356 sprintf (tmp
,"Can't create mailbox %.80s: invalid MX-format name",mailbox
);
357 /* must not already exist */
358 else if ((test
= mail_valid (NIL
,mailbox
,NIL
)) &&
359 strcmp (test
->name
,"dummy"))
360 sprintf (tmp
,"Can't create mailbox %.80s: mailbox already exists",mailbox
);
361 /* create directory */
362 else if (!dummy_create_path (stream
,MXINDEX (tmp
,mailbox
),
363 get_dir_protection (mailbox
)))
364 sprintf (tmp
,"Can't create mailbox %.80s: %s",mailbox
,strerror (errno
));
366 /* set index protection */
367 set_mbx_protections (mailbox
,tmp
);
368 /* tie off directory name */
369 *(s
= strrchr (tmp
,'/') + 1) = '\0';
370 /* set directory protection */
371 set_mbx_protections (mailbox
,tmp
);
374 umask (mask
); /* restore mask */
375 if (!ret
) MM_LOG (tmp
,ERROR
); /* some error */
379 /* MX mail delete mailbox
380 * mailbox name to delete
381 * Returns: T on success, NIL on failure
384 long mx_delete (MAILSTREAM
*stream
,char *mailbox
)
389 char tmp
[MAILTMPLEN
];
390 if (!mx_isvalid (mailbox
,tmp
))
391 sprintf (tmp
,"Can't delete mailbox %.80s: no such mailbox",mailbox
);
393 else if (unlink (MXINDEX (tmp
,mailbox
)))
394 sprintf (tmp
,"Can't delete mailbox %.80s index: %s",
395 mailbox
,strerror (errno
));
396 else { /* get directory name */
397 *(s
= strrchr (tmp
,'/')) = '\0';
398 if ((dirp
= opendir (tmp
)) != NULL
) { /* open directory */
399 *s
++ = '/'; /* restore delimiter */
400 /* massacre messages */
401 while ((d
= readdir (dirp
)) != NULL
) if (mx_select (d
)) {
402 strcpy (s
,d
->d_name
); /* make path */
403 unlink (tmp
); /* sayonara */
405 closedir (dirp
); /* flush directory */
406 *(s
= strrchr (tmp
,'/')) = '\0';
407 if (rmdir (tmp
)) { /* try to remove the directory */
408 sprintf (tmp
,"Can't delete name %.80s: %s",mailbox
,strerror (errno
));
412 return T
; /* always success */
414 MM_LOG (tmp
,ERROR
); /* something failed */
418 /* MX mail rename mailbox
419 * Accepts: MX mail stream
422 * Returns: T on success, NIL on failure
425 long mx_rename (MAILSTREAM
*stream
,char *old
,char *newname
)
427 char c
,*s
,tmp
[MAILTMPLEN
],tmp1
[MAILTMPLEN
];
429 if (!mx_isvalid (old
,tmp
))
430 sprintf (tmp
,"Can't rename mailbox %.80s: no such mailbox",old
);
431 else if (!mx_namevalid (newname
))
432 sprintf (tmp
,"Can't rename to mailbox %.80s: invalid MX-format name",
434 /* new mailbox name must not be valid */
435 else if (mx_isvalid (newname
,tmp
))
436 sprintf (tmp
,"Can't rename to mailbox %.80s: destination already exists",
439 mx_file (tmp
,old
); /* build old directory name */
440 mx_file (tmp1
,newname
); /* and new directory name */
441 /* easy if not INBOX */
442 if (compare_cstring (old
,"INBOX")) {
443 /* found superior to destination name? */
444 if ((s
= strrchr (mx_file (tmp1
,newname
),'/')) != NULL
) {
445 c
= *++s
; /* remember first character of inferior */
446 *s
= '\0'; /* tie off to get just superior */
447 /* name doesn't exist, create it */
448 if ((stat (tmp1
,&sbuf
) || ((sbuf
.st_mode
& S_IFMT
) != S_IFDIR
)) &&
449 !dummy_create_path (stream
,tmp1
,get_dir_protection (newname
)))
451 *s
= c
; /* restore full name */
453 if (!rename (tmp
,tmp1
)) return LONGT
;
456 /* RFC 3501 requires this */
457 else if (dummy_create_path (stream
,strcat (tmp1
,"/"),
458 get_dir_protection (newname
))) {
461 struct direct
**names
= NIL
;
462 size_t srcl
= strlen (tmp
);
463 size_t dstl
= strlen (tmp1
);
464 /* rename each mx file to new directory */
465 for (i
= lasterror
= 0,n
= scandir (tmp
,&names
,mx_select
,mx_numsort
);
467 if (mx_rename_work (tmp
,srcl
,tmp1
,dstl
,names
[i
]->d_name
))
469 fs_give ((void **) &names
[i
]);
471 /* free directory list */
472 if ((a
= (void *) names
) != NULL
) fs_give ((void **) &a
);
473 if (lasterror
|| mx_rename_work (tmp
,srcl
,tmp1
,dstl
,MXINDEXNAME
+1))
475 else return mx_create (NIL
,"INBOX");
477 sprintf (tmp
,"Can't rename mailbox %.80s to %.80s: %s",
478 old
,newname
,strerror (errno
));
480 MM_LOG (tmp
,ERROR
); /* something failed */
485 /* MX rename worker routine
486 * Accepts: source directory name
487 * source directory name length
488 * destination directory name
489 * destination directory name length
490 * name of node to move
491 * Returns: zero if success, non-zero if error
494 int mx_rename_work (char *src
,size_t srcl
,char *dst
,size_t dstl
,char *name
)
497 size_t len
= strlen (name
);
498 char *s
= (char *) fs_get (srcl
+ len
+ 2);
499 char *d
= (char *) fs_get (dstl
+ len
+ 1);
500 sprintf (s
,"%s/%s",src
,name
);
501 sprintf (d
,"%s%s",dst
,name
);
503 fs_give ((void **) &s
);
504 fs_give ((void **) &d
);
509 * Accepts: stream to open
510 * Returns: stream on success, NIL on failure
513 MAILSTREAM
*mx_open (MAILSTREAM
*stream
)
515 char tmp
[MAILTMPLEN
];
516 /* return prototype for OP_PROTOTYPE call */
517 if (!stream
) return user_flags (&mxproto
);
518 if (stream
->local
) fatal ("mx recycle stream");
519 stream
->local
= fs_get (sizeof (MXLOCAL
));
520 /* note if an INBOX or not */
521 stream
->inbox
= !compare_cstring (stream
->mailbox
,"INBOX");
522 mx_file (tmp
,stream
->mailbox
);/* get directory name */
523 /* canonicalize mailbox name */
524 fs_give ((void **) &stream
->mailbox
);
525 stream
->mailbox
= cpystr (tmp
);
526 /* make temporary buffer */
527 LOCAL
->buf
= (char *) fs_get (CHUNKSIZE
);
528 LOCAL
->buflen
= CHUNKSIZE
- 1;
529 LOCAL
->scantime
= 0; /* not scanned yet */
530 LOCAL
->fd
= -1; /* no index yet */
531 LOCAL
->cachedtexts
= 0; /* no cached texts */
532 stream
->sequence
++; /* bump sequence number */
534 stream
->nmsgs
= stream
->recent
= 0;
535 if (mx_ping (stream
) && !(stream
->nmsgs
|| stream
->silent
))
536 MM_LOG ("Mailbox is empty",(long) NIL
);
537 stream
->perm_seen
= stream
->perm_deleted
= stream
->perm_flagged
=
538 stream
->perm_answered
= stream
->perm_draft
= stream
->rdonly
? NIL
: T
;
539 stream
->perm_user_flags
= stream
->rdonly
? NIL
: 0xffffffff;
540 stream
->kwd_create
= (stream
->user_flags
[NUSERFLAGS
-1] || stream
->rdonly
) ?
541 NIL
: T
; /* can we create new user flags? */
542 return stream
; /* return stream to caller */
546 * Accepts: MAIL stream
550 void mx_close (MAILSTREAM
*stream
,long options
)
552 if (LOCAL
) { /* only if a file is open */
553 int silent
= stream
->silent
;
554 stream
->silent
= T
; /* note this stream is dying */
555 if (options
& CL_EXPUNGE
) mx_expunge (stream
,NIL
,NIL
);
556 /* free local scratch buffer */
557 if (LOCAL
->buf
) fs_give ((void **) &LOCAL
->buf
);
558 /* nuke the local data */
559 fs_give ((void **) &stream
->local
);
560 stream
->dtb
= NIL
; /* log out the DTB */
561 stream
->silent
= silent
; /* reset silent state */
565 /* MX mail fetch fast information
566 * Accepts: MAIL stream
571 void mx_fast (MAILSTREAM
*stream
,char *sequence
,long flags
)
575 if (stream
&& LOCAL
&&
576 ((flags
& FT_UID
) ? mail_uid_sequence (stream
,sequence
) :
577 mail_sequence (stream
,sequence
)))
578 for (i
= 1; i
<= stream
->nmsgs
; i
++)
579 if ((elt
= mail_elt (stream
,i
))->sequence
) mx_fast_work (stream
,elt
);
583 /* MX mail fetch fast information
584 * Accepts: MAIL stream
585 * message cache element
586 * Returns: name of message file
589 char *mx_fast_work (MAILSTREAM
*stream
,MESSAGECACHE
*elt
)
593 /* build message file name */
594 sprintf (LOCAL
->buf
,"%s/%lu",stream
->mailbox
,elt
->private.uid
);
596 if (!elt
->rfc822_size
&& !stat (LOCAL
->buf
,&sbuf
)) {
597 /* make plausible IMAPish date string */
598 tm
= gmtime (&sbuf
.st_mtime
);
599 elt
->day
= tm
->tm_mday
; elt
->month
= tm
->tm_mon
+ 1;
600 elt
->year
= tm
->tm_year
+ 1900 - BASEYEAR
;
601 elt
->hours
= tm
->tm_hour
; elt
->minutes
= tm
->tm_min
;
602 elt
->seconds
= tm
->tm_sec
;
603 elt
->zhours
= 0; elt
->zminutes
= 0; elt
->zoccident
= 0;
604 elt
->rfc822_size
= sbuf
.st_size
;
606 return (char *) LOCAL
->buf
; /* return file name */
609 /* MX mail fetch message header
610 * Accepts: MAIL stream
612 * pointer to returned header text length
614 * Returns: message header in RFC822 format
617 char *mx_header (MAILSTREAM
*stream
,unsigned long msgno
,unsigned long *length
,
623 *length
= 0; /* default to empty */
624 if (flags
& FT_UID
) return "";/* UID call "impossible" */
625 elt
= mail_elt (stream
,msgno
);/* get elt */
626 if (!elt
->private.msg
.header
.text
.data
) {
627 /* purge cache if too big */
628 if (LOCAL
->cachedtexts
> max (stream
->nmsgs
* 4096,2097152)) {
629 mail_gc (stream
,GC_TEXTS
);/* just can't keep that much */
630 LOCAL
->cachedtexts
= 0;
632 if ((fd
= open (mx_fast_work (stream
,elt
),O_RDONLY
,NIL
)) < 0) return "";
633 /* is buffer big enough? */
634 if (elt
->rfc822_size
> LOCAL
->buflen
) {
635 fs_give ((void **) &LOCAL
->buf
);
636 LOCAL
->buf
= (char *) fs_get ((LOCAL
->buflen
= elt
->rfc822_size
) + 1);
639 read (fd
,LOCAL
->buf
,elt
->rfc822_size
);
641 LOCAL
->buf
[elt
->rfc822_size
] = '\0';
642 close (fd
); /* flush message file */
643 /* find end of header */
644 if (elt
->rfc822_size
< 4) i
= 0;
645 else for (i
= 4; (i
< elt
->rfc822_size
) &&
646 !((LOCAL
->buf
[i
- 4] == '\015') &&
647 (LOCAL
->buf
[i
- 3] == '\012') &&
648 (LOCAL
->buf
[i
- 2] == '\015') &&
649 (LOCAL
->buf
[i
- 1] == '\012')); i
++);
651 cpytxt (&elt
->private.msg
.header
.text
,LOCAL
->buf
,i
);
652 cpytxt (&elt
->private.msg
.text
.text
,LOCAL
->buf
+i
,elt
->rfc822_size
- i
);
653 /* add to cached size */
654 LOCAL
->cachedtexts
+= elt
->rfc822_size
;
656 *length
= elt
->private.msg
.header
.text
.size
;
657 return (char *) elt
->private.msg
.header
.text
.data
;
660 /* MX mail fetch message text (body only)
661 * Accepts: MAIL stream
663 * pointer to returned stringstruct
665 * Returns: T on success, NIL on failure
668 long mx_text (MAILSTREAM
*stream
,unsigned long msgno
,STRING
*bs
,long flags
)
672 /* UID call "impossible" */
673 if (flags
& FT_UID
) return NIL
;
674 elt
= mail_elt (stream
,msgno
);
675 /* snarf message if don't have it yet */
676 if (!elt
->private.msg
.text
.text
.data
) {
677 mx_header (stream
,msgno
,&i
,flags
);
678 if (!elt
->private.msg
.text
.text
.data
) return NIL
;
681 if (!(flags
& FT_PEEK
) && mx_lockindex (stream
)) {
683 mx_unlockindex (stream
);
684 MM_FLAGS (stream
,msgno
);
686 INIT (bs
,mail_string
,elt
->private.msg
.text
.text
.data
,
687 elt
->private.msg
.text
.text
.size
);
691 /* MX mail modify flags
692 * Accepts: MAIL stream
698 void mx_flag (MAILSTREAM
*stream
,char *sequence
,char *flag
,long flags
)
700 mx_unlockindex (stream
); /* finished with index */
704 /* MX per-message modify flags
705 * Accepts: MAIL stream
706 * message cache element
709 void mx_flagmsg (MAILSTREAM
*stream
,MESSAGECACHE
*elt
)
711 mx_lockindex (stream
); /* lock index if not already locked */
714 /* MX mail ping mailbox
715 * Accepts: MAIL stream
716 * Returns: T if stream alive, else NIL
719 long mx_ping (MAILSTREAM
*stream
)
721 MAILSTREAM
*sysibx
= NIL
;
722 MESSAGECACHE
*elt
,*selt
;
724 char *s
,tmp
[MAILTMPLEN
];
726 unsigned long i
,j
,r
,old
;
727 long nmsgs
= stream
->nmsgs
;
728 long recent
= stream
->recent
;
729 int silent
= stream
->silent
;
730 if (stat (stream
->mailbox
,&sbuf
)) return NIL
;
731 stream
->silent
= T
; /* don't pass up exists events yet */
732 if (sbuf
.st_ctime
!= LOCAL
->scantime
) {
733 struct direct
**names
= NIL
;
734 long nfiles
= scandir (stream
->mailbox
,&names
,mx_select
,mx_numsort
);
735 if (nfiles
< 0) nfiles
= 0; /* in case error */
736 old
= stream
->uid_last
;
737 /* note scanned now */
738 LOCAL
->scantime
= sbuf
.st_ctime
;
740 for (i
= 0; i
< nfiles
; ++i
) {
741 /* if newly seen, add to list */
742 if ((j
= atoi (names
[i
]->d_name
)) > old
) {
743 /* swell the cache */
744 mail_exists (stream
,++nmsgs
);
745 stream
->uid_last
= (elt
= mail_elt (stream
,nmsgs
))->private.uid
= j
;
746 elt
->valid
= T
; /* note valid flags */
747 if (old
) { /* other than the first pass? */
748 elt
->recent
= T
; /* yup, mark as recent */
749 recent
++; /* bump recent count */
752 fs_give ((void **) &names
[i
]);
755 if ((s
= (void *) names
) != NULL
) fs_give ((void **) &s
);
757 stream
->nmsgs
= nmsgs
; /* don't upset mail_uid() */
759 /* if INBOX, snarf from system INBOX */
760 if (mx_lockindex (stream
) && stream
->inbox
&&
761 !strcmp (sysinbox (),stream
->mailbox
)) {
762 old
= stream
->uid_last
;
763 MM_CRITICAL (stream
); /* go critical */
764 /* see if anything in system inbox */
765 if (!stat (sysinbox (),&sbuf
) && sbuf
.st_size
&&
766 (sysibx
= mail_open (sysibx
,sysinbox (),OP_SILENT
)) &&
767 !sysibx
->rdonly
&& (r
= sysibx
->nmsgs
)) {
768 for (i
= 1; i
<= r
; ++i
) {/* for each message in sysinbox mailbox */
769 /* build file name we will use */
770 sprintf (LOCAL
->buf
,"%s/%lu",stream
->mailbox
,++old
);
771 /* snarf message from Berkeley mailbox */
772 selt
= mail_elt (sysibx
,i
);
773 if (((fd
= open (LOCAL
->buf
,O_WRONLY
|O_CREAT
|O_EXCL
,
774 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
)))
776 (s
= mail_fetchheader_full (sysibx
,i
,NIL
,&j
,FT_PEEK
)) &&
777 (write (fd
,s
,j
) == j
) &&
778 (s
= mail_fetchtext_full (sysibx
,i
,&j
,FT_PEEK
)) &&
779 (write (fd
,s
,j
) == j
) && !fsync (fd
) && !close (fd
)) {
780 /* swell the cache */
781 mail_exists (stream
,++nmsgs
);
782 stream
->uid_last
= /* create new elt, note its file number */
783 (elt
= mail_elt (stream
,nmsgs
))->private.uid
= old
;
784 recent
++; /* bump recent count */
785 /* set up initial flags and date */
786 elt
->valid
= elt
->recent
= T
;
787 elt
->seen
= selt
->seen
;
788 elt
->deleted
= selt
->deleted
;
789 elt
->flagged
= selt
->flagged
;
790 elt
->answered
= selt
->answered
;
791 elt
->draft
= selt
->draft
;
792 elt
->day
= selt
->day
;elt
->month
= selt
->month
;elt
->year
= selt
->year
;
793 elt
->hours
= selt
->hours
;elt
->minutes
= selt
->minutes
;
794 elt
->seconds
= selt
->seconds
;
795 elt
->zhours
= selt
->zhours
; elt
->zminutes
= selt
->zminutes
;
796 elt
->zoccident
= selt
->zoccident
;
797 mx_setdate (LOCAL
->buf
,elt
);
798 sprintf (tmp
,"%lu",i
);/* delete it from the sysinbox */
799 mail_flag (sysibx
,tmp
,"\\Deleted",ST_SET
);
801 else { /* failed to snarf */
802 if (fd
) { /* did it ever get opened? */
803 close (fd
); /* close descriptor */
804 unlink (LOCAL
->buf
);/* flush this file */
806 sprintf (tmp
,"Message copy to MX mailbox failed: %.80s",
809 r
= 0; /* stop the snarf in its tracks */
812 /* update scan time */
813 if (!stat (stream
->mailbox
,&sbuf
)) LOCAL
->scantime
= sbuf
.st_ctime
;
814 mail_expunge (sysibx
); /* now expunge all those messages */
816 if (sysibx
) mail_close (sysibx
);
817 MM_NOCRITICAL (stream
); /* release critical */
819 mx_unlockindex (stream
); /* done with index */
820 stream
->silent
= silent
; /* can pass up events now */
821 mail_exists (stream
,nmsgs
); /* notify upper level of mailbox size */
822 mail_recent (stream
,recent
);
823 return T
; /* return that we are alive */
826 /* MX mail check mailbox
827 * Accepts: MAIL stream
830 void mx_check (MAILSTREAM
*stream
)
832 if (mx_ping (stream
)) MM_LOG ("Check completed",(long) NIL
);
836 /* MX mail expunge mailbox
837 * Accepts: MAIL stream
838 * sequence to expunge if non-NIL
843 long mx_expunge (MAILSTREAM
*stream
,char *sequence
,long options
)
849 unsigned long recent
= stream
->recent
;
850 if ((ret
= (sequence
? ((options
& EX_UID
) ?
851 mail_uid_sequence (stream
,sequence
) :
852 mail_sequence (stream
,sequence
)) : LONGT
) != 0L) &&
853 mx_lockindex (stream
)) { /* lock the index */
854 MM_CRITICAL (stream
); /* go critical */
855 while (i
<= stream
->nmsgs
) {/* for each message */
856 elt
= mail_elt (stream
,i
);/* if deleted, need to trash it */
857 if (elt
->deleted
&& (sequence
? elt
->sequence
: T
)) {
858 sprintf (LOCAL
->buf
,"%s/%lu",stream
->mailbox
,elt
->private.uid
);
859 if (unlink (LOCAL
->buf
)) {/* try to delete the message */
860 sprintf (LOCAL
->buf
,"Expunge of message %lu failed, aborted: %s",i
,
862 MM_LOG (LOCAL
->buf
,(long) NIL
);
866 LOCAL
->cachedtexts
-= ((elt
->private.msg
.header
.text
.data
?
867 elt
->private.msg
.header
.text
.size
: 0) +
868 (elt
->private.msg
.text
.text
.data
?
869 elt
->private.msg
.text
.text
.size
: 0));
870 mail_gc_msg (&elt
->private.msg
,GC_ENV
| GC_TEXTS
);
871 if(elt
->recent
)--recent
;/* if recent, note one less recent message */
872 mail_expunged(stream
,i
);/* notify upper levels */
873 n
++; /* count up one more expunged message */
875 else i
++; /* otherwise try next message */
877 if (n
) { /* output the news if any expunged */
878 sprintf (LOCAL
->buf
,"Expunged %lu messages",n
);
879 MM_LOG (LOCAL
->buf
,(long) NIL
);
881 else MM_LOG ("No messages deleted, so no update needed",(long) NIL
);
882 MM_NOCRITICAL (stream
); /* release critical */
883 mx_unlockindex (stream
); /* finished with index */
884 /* notify upper level of new mailbox size */
885 mail_exists (stream
,stream
->nmsgs
);
886 mail_recent (stream
,recent
);
891 /* MX mail copy message(s)
892 * Accepts: MAIL stream
894 * destination mailbox
896 * Returns: T if copy successful, else NIL
899 long mx_copy (MAILSTREAM
*stream
,char *sequence
,char *mailbox
,long options
)
907 unsigned long i
,j
,uid
,uidv
;
908 char *t
,tmp
[MAILTMPLEN
];
911 (mailproxycopy_t
) mail_parameters (stream
,GET_MAILPROXYCOPY
,NIL
);
912 /* make sure valid mailbox */
913 if (!mx_valid (mailbox
)) switch (errno
) {
914 case NIL
: /* no error in stat() */
915 if (pc
) return (*pc
) (stream
,sequence
,mailbox
,options
);
916 sprintf (LOCAL
->buf
,"Not a MX-format mailbox: %.80s",mailbox
);
917 MM_LOG (LOCAL
->buf
,ERROR
);
919 default: /* some stat() error */
920 MM_NOTIFY (stream
,"[TRYCREATE] Must create mailbox before copy",NIL
);
923 /* copy the messages */
924 if (!(ret
= ((options
& CP_UID
) ? mail_uid_sequence (stream
,sequence
) :
925 mail_sequence (stream
,sequence
))));
926 /* acquire stream to append to */
927 else if (!(astream
= mail_open (NIL
,mailbox
,OP_SILENT
))) {
928 MM_LOG ("Can't open copy mailbox",ERROR
);
932 MM_CRITICAL (stream
); /* go critical */
933 if (!(ret
= mx_lockindex (astream
)))
934 MM_LOG ("Message copy failed: unable to lock index",ERROR
);
937 copyuid_t cu
= (copyuid_t
) mail_parameters (NIL
,GET_COPYUID
,NIL
);
938 SEARCHSET
*source
= cu
? mail_newsearchset () : NIL
;
939 SEARCHSET
*dest
= cu
? mail_newsearchset () : NIL
;
940 for (i
= 1,uid
= uidv
= 0; ret
&& (i
<= stream
->nmsgs
); i
++)
941 if ((elt
= mail_elt (stream
,i
))->sequence
) {
942 if ((ret
= ((fd
= open (mx_fast_work (stream
,elt
),O_RDONLY
,NIL
))
944 fstat (fd
,&sbuf
); /* get size of message */
945 d
.fd
= fd
; /* set up file descriptor */
946 d
.pos
= 0; /* start of file */
947 d
.chunk
= LOCAL
->buf
;
948 d
.chunksize
= CHUNKSIZE
;
949 INIT (&st
,fd_string
,&d
,sbuf
.st_size
);
950 /* init flag string */
951 tmp
[0] = tmp
[1] = '\0';
952 if ((j
= elt
->user_flags
) != 0L) do
953 if ((t
= stream
->user_flags
[find_rightmost_bit (&j
)]) != NULL
)
954 strcat (strcat (tmp
," "),t
);
956 if (elt
->seen
) strcat (tmp
," \\Seen");
957 if (elt
->deleted
) strcat (tmp
," \\Deleted");
958 if (elt
->flagged
) strcat (tmp
," \\Flagged");
959 if (elt
->answered
) strcat (tmp
," \\Answered");
960 if (elt
->draft
) strcat (tmp
," \\Draft");
961 tmp
[0] = '('; /* open list */
962 strcat (tmp
,")"); /* close list */
963 if ((ret
= mx_append_msg (astream
,tmp
,elt
,&st
,dest
)) != 0L) {
964 /* add to source set if needed */
965 if (source
) mail_append_set (source
,mail_uid (stream
,i
));
966 /* delete if doing a move */
967 if (options
& CP_MOVE
) elt
->deleted
= T
;
971 /* return sets if doing COPYUID */
972 if (cu
&& ret
) (*cu
) (stream
,mailbox
,astream
->uid_validity
,source
,dest
);
973 else { /* flush any sets we may have built */
974 mail_free_searchset (&source
);
975 mail_free_searchset (&dest
);
977 mx_unlockindex (astream
); /* unlock index */
979 MM_NOCRITICAL (stream
);
980 mail_close (astream
); /* finished with append stream */
982 return ret
; /* return success */
985 /* MX mail append message from stringstruct
986 * Accepts: MAIL stream
987 * destination mailbox
990 * Returns: T if append successful, else NIL
993 long mx_append (MAILSTREAM
*stream
,char *mailbox
,append_t af
,void *data
)
997 char *flags
,*date
,tmp
[MAILTMPLEN
];
1000 /* default stream to prototype */
1001 if (!stream
) stream
= user_flags (&mxproto
);
1002 /* N.B.: can't use LOCAL->buf for tmp */
1003 /* make sure valid mailbox */
1004 if (!mx_isvalid (mailbox
,tmp
)) switch (errno
) {
1005 case ENOENT
: /* no such file? */
1006 if (!compare_cstring (mailbox
,"INBOX")) mx_create (NIL
,"INBOX");
1008 MM_NOTIFY (stream
,"[TRYCREATE] Must create mailbox before append",NIL
);
1012 case 0: /* merely empty file? */
1015 sprintf (tmp
,"Invalid MX-format mailbox name: %.80s",mailbox
);
1019 sprintf (tmp
,"Not a MX-format mailbox: %.80s",mailbox
);
1024 /* get first message */
1025 if (!MM_APPEND (af
) (stream
,data
,&flags
,&date
,&message
)) return NIL
;
1026 if (!(astream
= mail_open (NIL
,mailbox
,OP_SILENT
))) {
1027 MM_LOG ("Can't open append mailbox",ERROR
);
1030 MM_CRITICAL (astream
); /* go critical */
1031 /* lock the index */
1032 if (!(ret
= mx_lockindex (astream
)))
1033 MM_LOG ("Message append failed: unable to lock index",ERROR
);
1035 appenduid_t au
= (appenduid_t
) mail_parameters (NIL
,GET_APPENDUID
,NIL
);
1036 SEARCHSET
*dst
= au
? mail_newsearchset () : NIL
;
1038 /* guard against zero-length */
1039 if (!(ret
= SIZE (message
)))
1040 MM_LOG ("Append of zero-length message",ERROR
);
1041 else if (date
&& !(ret
= mail_parse_date (&elt
,date
))) {
1042 sprintf (tmp
,"Bad date in append: %.80s",date
);
1045 else ret
= mx_append_msg (astream
,flags
,date
? &elt
: NIL
,message
,dst
)&&
1046 MM_APPEND (af
) (stream
,data
,&flags
,&date
,&message
);
1047 } while (ret
&& message
);
1048 /* return sets if doing APPENDUID */
1049 if (au
&& ret
) (*au
) (mailbox
,astream
->uid_validity
,dst
);
1050 else mail_free_searchset (&dst
);
1051 mx_unlockindex (astream
); /* unlock index */
1053 MM_NOCRITICAL (astream
); /* release critical */
1054 mail_close (astream
);
1058 /* MX mail append single message
1059 * Accepts: MAIL stream
1060 * flags for new message if non-NIL
1061 * elt with source date if non-NIL
1062 * stringstruct of message text
1063 * searchset to place UID
1064 * Returns: T if success, NIL if failure
1067 long mx_append_msg (MAILSTREAM
*stream
,char *flags
,MESSAGECACHE
*elt
,
1068 STRING
*st
,SEARCHSET
*set
)
1070 char tmp
[MAILTMPLEN
];
1073 long f
= mail_parse_flags (stream
,flags
,&uf
);
1074 /* make message file name */
1075 sprintf (tmp
,"%s/%lu",stream
->mailbox
,++stream
->uid_last
);
1076 if ((fd
= open (tmp
,O_WRONLY
|O_CREAT
|O_EXCL
,
1077 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
))) < 0) {
1078 sprintf (tmp
,"Can't create append message: %s",strerror (errno
));
1082 while (SIZE (st
)) { /* copy the file */
1083 if (st
->cursize
&& (write (fd
,st
->curpos
,st
->cursize
) < 0)) {
1084 unlink (tmp
); /* delete file */
1085 close (fd
); /* close the file */
1086 sprintf (tmp
,"Message append failed: %s",strerror (errno
));
1090 SETPOS (st
,GETPOS (st
) + st
->cursize
);
1092 close (fd
); /* close the file */
1093 if (elt
) mx_setdate (tmp
,elt
);/* set file date */
1094 /* swell the cache */
1095 mail_exists (stream
,++stream
->nmsgs
);
1097 mail_append_set (set
,(elt
= mail_elt (stream
,stream
->nmsgs
))->private.uid
=
1099 if (f
&fSEEN
) elt
->seen
= T
;
1100 if (f
&fDELETED
) elt
->deleted
= T
;
1101 if (f
&fFLAGGED
) elt
->flagged
= T
;
1102 if (f
&fANSWERED
) elt
->answered
= T
;
1103 if (f
&fDRAFT
) elt
->draft
= T
;
1104 elt
->user_flags
|= uf
;
1108 /* Internal routines */
1111 /* MX file name selection test
1112 * Accepts: candidate directory entry
1113 * Returns: T to use file name, NIL to skip it
1116 int mx_select (const struct direct
*name
)
1119 char *s
= (char *) name
->d_name
;
1120 while ((c
= *s
++) != '\0') if (!isdigit (c
)) return NIL
;
1125 /* MX file name comparison
1126 * Accepts: first candidate directory entry
1127 * second candidate directory entry
1128 * Returns: negative if d1 < d2, 0 if d1 == d2, positive if d1 > d2
1131 int mx_numsort (const struct direct
**d1
,const struct direct
**d2
)
1133 return atoi ((*(struct direct
**) d1
)->d_name
) -
1134 atoi ((*(struct direct
**) d2
)->d_name
);
1138 /* MX mail build file name
1139 * Accepts: destination string
1141 * Returns: destination
1144 char *mx_file (char *dst
,char *name
)
1147 /* empty string if mailboxfile fails */
1148 if (!mailboxfile (dst
,name
)) *dst
= '\0';
1149 /* driver-selected INBOX */
1150 else if (!*dst
) mailboxfile (dst
,"~/INBOX");
1151 /* tie off unnecessary trailing / */
1152 else if ((s
= strrchr (dst
,'/')) && !s
[1]) *s
= '\0';
1156 /* MX read and lock index
1157 * Accepts: MAIL stream
1158 * Returns: T if success, NIL if failure
1161 long mx_lockindex (MAILSTREAM
*stream
)
1163 unsigned long uf
,sf
,uid
;
1165 unsigned long msgno
= 1;
1167 char *s
,*t
,*idx
,tmp
[2*MAILTMPLEN
];
1169 blocknotify_t bn
= (blocknotify_t
) mail_parameters (NIL
,GET_BLOCKNOTIFY
,NIL
);
1170 if ((LOCAL
->fd
< 0) && /* get index file, no-op if already have it */
1171 (LOCAL
->fd
= open (strcat (strcpy (tmp
,stream
->mailbox
),MXINDEXNAME
),
1173 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
)))
1175 (*bn
) (BLOCK_FILELOCK
,NIL
);
1176 flock (LOCAL
->fd
,LOCK_EX
); /* get exclusive lock */
1177 (*bn
) (BLOCK_NONE
,NIL
);
1178 fstat (LOCAL
->fd
,&sbuf
); /* get size of index */
1180 read (LOCAL
->fd
,s
= idx
= (char *) fs_get (sbuf
.st_size
+ 1),sbuf
.st_size
);
1181 idx
[sbuf
.st_size
] = '\0'; /* tie off index */
1183 if (sbuf
.st_size
) while (s
&& *s
) switch (*s
) {
1184 case 'V': /* UID validity record */
1185 stream
->uid_validity
= strtoul (s
+1,&s
,16);
1187 case 'L': /* UID last record */
1188 stream
->uid_last
= strtoul (s
+1,&s
,16);
1190 case 'K': /* keyword */
1191 /* find end of keyword */
1192 if ((s
= strchr (t
= ++s
,'\n')) != NULL
) {
1193 *s
++ = '\0'; /* tie off keyword */
1195 if ((k
< NUSERFLAGS
) && !stream
->user_flags
[k
] &&
1196 (strlen (t
) <= MAXUSERFLAG
)) stream
->user_flags
[k
] = cpystr (t
);
1197 k
++; /* one more keyword */
1201 case 'M': /* message status record */
1202 uid
= strtoul (s
+1,&s
,16);/* get UID for this message */
1203 if (*s
== ';') { /* get user flags */
1204 uf
= strtoul (s
+1,&s
,16);
1205 if (*s
== '.') { /* get system flags */
1206 sf
= strtoul (s
+1,&s
,16);
1207 while ((msgno
<= stream
->nmsgs
) && (mail_uid (stream
,msgno
) < uid
))
1208 msgno
++; /* find message number for this UID */
1209 if ((msgno
<= stream
->nmsgs
) && (mail_uid (stream
,msgno
) == uid
)) {
1210 (elt
= mail_elt (stream
,msgno
))->valid
= T
;
1211 elt
->user_flags
=uf
; /* set user and system flags in elt */
1212 if (sf
& fSEEN
) elt
->seen
= T
;
1213 if (sf
& fDELETED
) elt
->deleted
= T
;
1214 if (sf
& fFLAGGED
) elt
->flagged
= T
;
1215 if (sf
& fANSWERED
) elt
->answered
= T
;
1216 if (sf
& fDRAFT
) elt
->draft
= T
;
1221 default: /* bad news */
1222 sprintf (tmp
,"Error in index: %.80s",s
);
1224 *s
= NIL
; /* ignore remainder of index */
1226 else { /* new index */
1227 stream
->uid_validity
= time (0);
1228 user_flags (stream
); /* init stream with default user flags */
1230 fs_give ((void **) &idx
); /* flush index */
1232 return (LOCAL
->fd
>= 0) ? T
: NIL
;
1235 /* MX write and unlock index
1236 * Accepts: MAIL stream
1239 #define MXIXBUFLEN 2048
1241 void mx_unlockindex (MAILSTREAM
*stream
)
1245 char *s
,tmp
[MXIXBUFLEN
+ 64];
1247 if (LOCAL
->fd
>= 0) {
1248 lseek (LOCAL
->fd
,0,L_SET
); /* rewind file */
1250 sprintf (s
= tmp
,"V%08lxL%08lx",stream
->uid_validity
,stream
->uid_last
);
1251 for (i
= 0; (i
< NUSERFLAGS
) && stream
->user_flags
[i
]; ++i
)
1252 sprintf (s
+= strlen (s
),"K%s\n",stream
->user_flags
[i
]);
1253 /* write messages */
1254 for (i
= 1; i
<= stream
->nmsgs
; i
++) {
1255 /* filled buffer? */
1256 if (((s
+= strlen (s
)) - tmp
) > MXIXBUFLEN
) {
1257 write (LOCAL
->fd
,tmp
,j
= s
- tmp
);
1259 *(s
= tmp
) = '\0'; /* dump out and restart buffer */
1261 elt
= mail_elt (stream
,i
);
1262 sprintf(s
,"M%08lx;%08lx.%04x",elt
->private.uid
,elt
->user_flags
,(unsigned)
1263 ((fSEEN
* elt
->seen
) + (fDELETED
* elt
->deleted
) +
1264 (fFLAGGED
* elt
->flagged
) + (fANSWERED
* elt
->answered
) +
1265 (fDRAFT
* elt
->draft
)));
1267 /* write tail end of buffer */
1268 if ((s
+= strlen (s
)) != tmp
) {
1269 write (LOCAL
->fd
,tmp
,j
= s
- tmp
);
1272 ftruncate (LOCAL
->fd
,size
);
1273 flock (LOCAL
->fd
,LOCK_UN
); /* unlock the index */
1274 close (LOCAL
->fd
); /* finished with file */
1275 LOCAL
->fd
= -1; /* no index now */
1279 /* Set date for message
1280 * Accepts: file name
1281 * elt containing date
1284 void mx_setdate (char *file
,MESSAGECACHE
*elt
)
1287 tp
[0] = time (0); /* atime is now */
1288 tp
[1] = mail_longdate (elt
); /* modification time */
1289 utime (file
,tp
); /* set the times */