* Do not define _BSD_SOURCE but define _DEFAULT_SOURCE instead.
[alpine.git] / imap / src / osdep / unix / mmdf.c
blob994c34a3c5337dca728a78bc4b3d67ffe3e98448
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: MMDF mail routines
17 * Author: Mark Crispin
18 * UW Technology
19 * University of Washington
20 * Seattle, WA 98195
21 * Internet: MRC@Washington.EDU
23 * Date: 20 December 1989
24 * Last Edited: 27 March 2008
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <errno.h>
31 extern int errno; /* just in case */
32 #include <signal.h>
33 #include "mail.h"
34 #include "osdep.h"
35 #include <time.h>
36 #include <sys/stat.h>
37 #include "pseudo.h"
38 #include "fdstring.h"
39 #include "misc.h"
40 #include "dummy.h"
42 /* Supposedly, this page has everything the MMDF driver needs to know about
43 * the MMDF delimiter. By changing these macros, the MMDF driver should
44 * change with it. Note that if you change the length of MMDFHDRTXT you
45 * also need to change the ISMMDF and RETIFMMDFWRD macros to reflect the new
46 * size.
50 /* Useful MMDF constants */
52 #define MMDFCHR '\01' /* MMDF character */
53 #define MMDFCHRS 0x01010101 /* MMDF header character spread in a word */
54 /* MMDF header text */
55 #define MMDFHDRTXT "\01\01\01\01\n"
56 /* length of MMDF header text */
57 #define MMDFHDRLEN (sizeof (MMDFHDRTXT) - 1)
60 /* Validate MMDF header
61 * Accepts: pointer to candidate string to validate as an MMDF header
62 * Returns: T if valid; else NIL
65 #define ISMMDF(s) \
66 ((*(s) == MMDFCHR) && ((s)[1] == MMDFCHR) && ((s)[2] == MMDFCHR) && \
67 ((s)[3] == MMDFCHR) && ((s)[4] == '\n'))
70 /* Return if a 32-bit word has the start of an MMDF header
71 * Accepts: pointer to word of four bytes to validate as an MMDF header
72 * Returns: pointer to MMDF header, else proceeds
75 #define RETIFMMDFWRD(s) { \
76 if (s[3] == MMDFCHR) { \
77 if ((s[4] == MMDFCHR) && (s[5] == MMDFCHR) && (s[6] == MMDFCHR) && \
78 (s[7] == '\n')) return s + 3; \
79 else if (s[2] == MMDFCHR) { \
80 if ((s[4] == MMDFCHR) && (s[5] == MMDFCHR) && (s[6] == '\n')) \
81 return s + 2; \
82 else if (s[1] == MMDFCHR) { \
83 if ((s[4] == MMDFCHR) && (s[5] == '\n')) return s + 1; \
84 else if ((*s == MMDFCHR) && (s[4] == '\n')) return s; \
85 } \
86 } \
87 } \
90 /* Validate line
91 * Accepts: pointer to candidate string to validate as a From header
92 * return pointer to end of date/time field
93 * return pointer to offset from t of time (hours of ``mmm dd hh:mm'')
94 * return pointer to offset from t of time zone (if non-zero)
95 * Returns: t,ti,zn set if valid From string, else ti is NIL
98 #define VALID(s,x,ti,zn) { \
99 ti = 0; \
100 if ((*s == 'F') && (s[1] == 'r') && (s[2] == 'o') && (s[3] == 'm') && \
101 (s[4] == ' ')) { \
102 for (x = s + 5; *x && *x != '\n'; x++); \
103 if (*x) { \
104 if (x - s >= 41) { \
105 for (zn = -1; x[zn] != ' '; zn--); \
106 if ((x[zn-1] == 'm') && (x[zn-2] == 'o') && (x[zn-3] == 'r') && \
107 (x[zn-4] == 'f') && (x[zn-5] == ' ') && (x[zn-6] == 'e') && \
108 (x[zn-7] == 't') && (x[zn-8] == 'o') && (x[zn-9] == 'm') && \
109 (x[zn-10] == 'e') && (x[zn-11] == 'r') && (x[zn-12] == ' '))\
110 x += zn - 12; \
112 if (x - s >= 27) { \
113 if (x[-5] == ' ') { \
114 if (x[-8] == ':') zn = 0,ti = -5; \
115 else if (x[-9] == ' ') ti = zn = -9; \
116 else if ((x[-11] == ' ') && ((x[-10]=='+') || (x[-10]=='-'))) \
117 ti = zn = -11; \
119 else if (x[-4] == ' ') { \
120 if (x[-9] == ' ') zn = -4,ti = -9; \
122 else if (x[-6] == ' ') { \
123 if ((x[-11] == ' ') && ((x[-5] == '+') || (x[-5] == '-'))) \
124 zn = -6,ti = -11; \
126 if (ti && !((x[ti - 3] == ':') && \
127 (x[ti -= ((x[ti - 6] == ':') ? 9 : 6)] == ' ') && \
128 (x[ti - 3] == ' ') && (x[ti - 7] == ' ') && \
129 (x[ti - 11] == ' '))) ti = 0; \
135 /* You are not expected to understand this macro, but read the next page if
136 * you are not faint of heart.
138 * Known formats to the VALID macro are:
139 * From user Wed Dec 2 05:53 1992
140 * BSD From user Wed Dec 2 05:53:22 1992
141 * SysV From user Wed Dec 2 05:53 PST 1992
142 * rn From user Wed Dec 2 05:53:22 PST 1992
143 * From user Wed Dec 2 05:53 -0700 1992
144 * emacs From user Wed Dec 2 05:53:22 -0700 1992
145 * From user Wed Dec 2 05:53 1992 PST
146 * From user Wed Dec 2 05:53:22 1992 PST
147 * From user Wed Dec 2 05:53 1992 -0700
148 * Solaris From user Wed Dec 2 05:53:22 1992 -0700
150 * Plus all of the above with `` remote from xxx'' after it. Thank you very
151 * much, smail and Solaris, for making my life considerably more complicated.
155 * What? You want to understand the VALID macro anyway? Alright, since you
156 * insist. Actually, it isn't really all that difficult, provided that you
157 * take it step by step.
159 * Line 1 Initializes the return ti value to failure (0);
160 * Lines 2-3 Validates that the 1st-5th characters are ``From ''.
161 * Lines 4-5 Validates that there is an end of line and points x at it.
162 * Lines 6-13 First checks to see if the line is at least 41 characters long.
163 * If so, it scans backwards to find the rightmost space. From
164 * that point, it scans backwards to see if the string matches
165 * `` remote from''. If so, it sets x to point to the space at
166 * the start of the string.
167 * Line 14 Makes sure that there are at least 27 characters in the line.
168 * Lines 15-20 Checks if the date/time ends with the year (there is a space
169 * five characters back). If there is a colon three characters
170 * further back, there is no timezone field, so zn is set to 0
171 * and ti is set in front of the year. Otherwise, there must
172 * either to be a space four characters back for a three-letter
173 * timezone, or a space six characters back followed by a + or -
174 * for a numeric timezone; in either case, zn and ti become the
175 * offset of the space immediately before it.
176 * Lines 21-23 Are the failure case for line 14. If there is a space four
177 * characters back, it is a three-letter timezone; there must be a
178 * space for the year nine characters back. zn is the zone
179 * offset; ti is the offset of the space.
180 * Lines 24-27 Are the failure case for line 20. If there is a space six
181 * characters back, it is a numeric timezone; there must be a
182 * space eleven characters back and a + or - five characters back.
183 * zn is the zone offset; ti is the offset of the space.
184 * Line 28-31 If ti is valid, make sure that the string before ti is of the
185 * form www mmm dd hh:mm or www mmm dd hh:mm:ss, otherwise
186 * invalidate ti. There must be a colon three characters back
187 * and a space six or nine characters back (depending upon
188 * whether or not the character six characters back is a colon).
189 * There must be a space three characters further back (in front
190 * of the day), one seven characters back (in front of the month),
191 * and one eleven characters back (in front of the day of week).
192 * ti is set to be the offset of the space before the time.
194 * Why a macro? It gets invoked a *lot* in a tight loop. On some of the
195 * newer pipelined machines it is faster being open-coded than it would be if
196 * subroutines are called.
198 * Why does it scan backwards from the end of the line, instead of doing the
199 * much easier forward scan? There is no deterministic way to parse the
200 * ``user'' field, because it may contain unquoted spaces! Yes, I tested it to
201 * see if unquoted spaces were possible. They are, and I've encountered enough
202 * evil mail to be totally unwilling to trust that ``it will never happen''.
205 /* Build parameters */
207 #define KODRETRY 15 /* kiss-of-death retry in seconds */
208 #define LOCKTIMEOUT 5 /* lock timeout in minutes */
211 /* MMDF I/O stream local data */
213 typedef struct mmdf_local {
214 unsigned int dirty : 1; /* disk copy needs updating */
215 unsigned int ddirty : 1; /* double-dirty, ping becomes checkpoint */
216 unsigned int pseudo : 1; /* uses a pseudo message */
217 unsigned int appending : 1; /* don't mark new messages as old */
218 int fd; /* mailbox file descriptor */
219 int ld; /* lock file descriptor */
220 char *lname; /* lock file name */
221 off_t filesize; /* file size parsed */
222 time_t filetime; /* last file time */
223 unsigned char *buf; /* temporary buffer */
224 unsigned long buflen; /* current size of temporary buffer */
225 unsigned long uid; /* current text uid */
226 SIZEDTEXT text; /* current text */
227 unsigned long textlen; /* current text length */
228 char *line; /* returned line */
229 char *linebuf; /* line readin buffer */
230 unsigned long linebuflen; /* current line readin buffer length */
231 } MMDFLOCAL;
234 /* Convenient access to local data */
236 #define LOCAL ((MMDFLOCAL *) stream->local)
239 /* MMDF protected file structure */
241 typedef struct mmdf_file {
242 MAILSTREAM *stream; /* current stream */
243 off_t curpos; /* current file position */
244 off_t protect; /* protected position */
245 off_t filepos; /* current last written file position */
246 char *buf; /* overflow buffer */
247 size_t buflen; /* current overflow buffer length */
248 char *bufpos; /* current buffer position */
249 } MMDFFILE;
251 /* Function prototypes */
253 DRIVER *mmdf_valid (char *name);
254 long mmdf_isvalid (char *name,char *tmp);
255 long mmdf_isvalid_fd (int fd,char *tmp);
256 void *mmdf_parameters (long function,void *value);
257 void mmdf_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
258 void mmdf_list (MAILSTREAM *stream,char *ref,char *pat);
259 void mmdf_lsub (MAILSTREAM *stream,char *ref,char *pat);
260 long mmdf_create (MAILSTREAM *stream,char *mailbox);
261 long mmdf_delete (MAILSTREAM *stream,char *mailbox);
262 long mmdf_rename (MAILSTREAM *stream,char *old,char *newname);
263 MAILSTREAM *mmdf_open (MAILSTREAM *stream);
264 void mmdf_close (MAILSTREAM *stream,long options);
265 char *mmdf_header (MAILSTREAM *stream,unsigned long msgno,
266 unsigned long *length,long flags);
267 long mmdf_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
268 char *mmdf_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
269 unsigned long *length,long flags);
270 void mmdf_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
271 long mmdf_ping (MAILSTREAM *stream);
272 void mmdf_check (MAILSTREAM *stream);
273 long mmdf_expunge (MAILSTREAM *stream,char *sequence,long options);
274 long mmdf_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
275 long mmdf_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
276 int mmdf_collect_msg (MAILSTREAM *stream,FILE *sf,char *flags,char *date,
277 STRING *msg);
278 int mmdf_append_msgs (MAILSTREAM *stream,FILE *sf,FILE *df,SEARCHSET *set);
280 void mmdf_abort (MAILSTREAM *stream);
281 char *mmdf_file (char *dst,char *name);
282 int mmdf_lock (char *file,int flags,int mode,DOTLOCK *lock,int op);
283 void mmdf_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock);
284 int mmdf_parse (MAILSTREAM *stream,DOTLOCK *lock,int op);
285 char *mmdf_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size);
286 unsigned long mmdf_pseudo (MAILSTREAM *stream,char *hdr);
287 unsigned long mmdf_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
288 unsigned long uid,long flag);
289 long mmdf_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
290 long flags);
291 long mmdf_extend (MAILSTREAM *stream,unsigned long size);
292 void mmdf_write (MMDFFILE *f,char *s,unsigned long i);
293 void mmdf_phys_write (MMDFFILE *f,char *buf,size_t size);
295 /* MMDF mail routines */
298 /* Driver dispatch used by MAIL */
300 DRIVER mmdfdriver = {
301 "mmdf", /* driver name */
302 /* driver flags */
303 DR_LOCAL|DR_MAIL|DR_LOCKING|DR_NONEWMAILRONLY|DR_XPOINT,
304 (DRIVER *) NIL, /* next driver */
305 mmdf_valid, /* mailbox is valid for us */
306 mmdf_parameters, /* manipulate parameters */
307 mmdf_scan, /* scan mailboxes */
308 mmdf_list, /* list mailboxes */
309 mmdf_lsub, /* list subscribed mailboxes */
310 NIL, /* subscribe to mailbox */
311 NIL, /* unsubscribe from mailbox */
312 mmdf_create, /* create mailbox */
313 mmdf_delete, /* delete mailbox */
314 mmdf_rename, /* rename mailbox */
315 mail_status_default, /* status of mailbox */
316 mmdf_open, /* open mailbox */
317 mmdf_close, /* close mailbox */
318 NIL, /* fetch message "fast" attributes */
319 NIL, /* fetch message flags */
320 NIL, /* fetch overview */
321 NIL, /* fetch message envelopes */
322 mmdf_header, /* fetch message header */
323 mmdf_text, /* fetch message text */
324 NIL, /* fetch partial message text */
325 NIL, /* unique identifier */
326 NIL, /* message number */
327 NIL, /* modify flags */
328 mmdf_flagmsg, /* per-message modify flags */
329 NIL, /* search for message based on criteria */
330 NIL, /* sort messages */
331 NIL, /* thread messages */
332 mmdf_ping, /* ping mailbox to see if still alive */
333 mmdf_check, /* check for new messages */
334 mmdf_expunge, /* expunge deleted messages */
335 mmdf_copy, /* copy messages to another mailbox */
336 mmdf_append, /* append string message to mailbox */
337 NIL /* garbage collect stream */
340 /* prototype stream */
341 MAILSTREAM mmdfproto = {&mmdfdriver};
343 char *mmdfhdr = MMDFHDRTXT; /* MMDF header */
345 /* MMDF mail validate mailbox
346 * Accepts: mailbox name
347 * Returns: our driver if name is valid, NIL otherwise
350 DRIVER *mmdf_valid (char *name)
352 char tmp[MAILTMPLEN];
353 return mmdf_isvalid (name,tmp) ? &mmdfdriver : NIL;
357 /* MMDF mail test for valid mailbox name
358 * Accepts: mailbox name
359 * scratch buffer
360 * Returns: T if valid, NIL otherwise
363 long mmdf_isvalid (char *name,char *tmp)
365 int fd;
366 int ret = NIL;
367 char *t,file[MAILTMPLEN];
368 struct stat sbuf;
369 time_t tp[2];
370 errno = EINVAL; /* assume invalid argument */
371 /* must be non-empty file */
372 if ((t = dummy_file (file,name)) && !stat (t,&sbuf)) {
373 if (!sbuf.st_size)errno = 0;/* empty file */
374 else if ((fd = open (file,O_RDONLY,NIL)) >= 0) {
375 /* error -1 for invalid format */
376 if (!(ret = mmdf_isvalid_fd (fd,tmp))) errno = -1;
377 close (fd); /* close the file */
378 /* \Marked status? */
379 if ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) {
380 tp[0] = sbuf.st_atime; /* preserve atime and mtime */
381 tp[1] = sbuf.st_mtime;
382 utime (file,tp); /* set the times */
386 return ret; /* return what we should */
389 /* MMDF mail test for valid mailbox
390 * Accepts: file descriptor
391 * scratch buffer
392 * Returns: T if valid, NIL otherwise
395 long mmdf_isvalid_fd (int fd,char *tmp)
397 int ret = NIL;
398 memset (tmp,'\0',MAILTMPLEN);
399 if (read (fd,tmp,MAILTMPLEN-1) >= 0) ret = ISMMDF (tmp) ? T : NIL;
400 return ret; /* return what we should */
404 /* MMDF manipulate driver parameters
405 * Accepts: function code
406 * function-dependent value
407 * Returns: function-dependent return value
410 void *mmdf_parameters (long function,void *value)
412 void *ret = NIL;
413 switch ((int) function) {
414 case GET_INBOXPATH:
415 if (value) ret = dummy_file ((char *) value,"INBOX");
416 break;
418 return ret;
421 /* MMDF mail scan mailboxes
422 * Accepts: mail stream
423 * reference
424 * pattern to search
425 * string to scan
428 void mmdf_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
430 if (stream) dummy_scan (NIL,ref,pat,contents);
434 /* MMDF mail list mailboxes
435 * Accepts: mail stream
436 * reference
437 * pattern to search
440 void mmdf_list (MAILSTREAM *stream,char *ref,char *pat)
442 if (stream) dummy_list (NIL,ref,pat);
446 /* MMDF mail list subscribed mailboxes
447 * Accepts: mail stream
448 * reference
449 * pattern to search
452 void mmdf_lsub (MAILSTREAM *stream,char *ref,char *pat)
454 if (stream) dummy_lsub (NIL,ref,pat);
457 /* MMDF mail create mailbox
458 * Accepts: MAIL stream
459 * mailbox name to create
460 * Returns: T on success, NIL on failure
463 long mmdf_create (MAILSTREAM *stream,char *mailbox)
465 char *s,mbx[MAILTMPLEN],tmp[MAILTMPLEN];
466 long ret = NIL;
467 int i,fd;
468 time_t ti = time (0);
469 if (!(s = dummy_file (mbx,mailbox))) {
470 sprintf (tmp,"Can't create %.80s: invalid name",mailbox);
471 MM_LOG (tmp,ERROR);
473 /* create underlying file */
474 else if (dummy_create_path (stream,s,get_dir_protection (mailbox))) {
475 /* done if dir-only or whiner */
476 if (((s = strrchr (s,'/')) && !s[1]) ||
477 mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) ret = T;
478 else if ((fd = open (mbx,O_WRONLY,
479 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) < 0) {
480 sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno));
481 MM_LOG (tmp,ERROR);
482 unlink (mbx); /* delete the file */
484 else { /* initialize header */
485 memset (tmp,'\0',MAILTMPLEN);
486 sprintf (tmp,"%sFrom %s %sDate: ",mmdfhdr,pseudo_from,ctime (&ti));
487 rfc822_date (s = tmp + strlen (tmp));
488 sprintf (s += strlen (s), /* write the pseudo-header */
489 "\nFrom: %s <%s@%s>\nSubject: %s\nX-IMAP: %010lu 0000000000",
490 pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,
491 (unsigned long) ti);
492 for (i = 0; i < NUSERFLAGS; ++i) if (default_user_flag (i))
493 sprintf (s += strlen (s)," %s",default_user_flag (i));
494 sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n%s",pseudo_msg,mmdfhdr);
495 if (write (fd,tmp,strlen (tmp)) > 0) ret = T;
496 else {
497 sprintf (tmp,"Can't initialize mailbox node %.80s: %s",mbx,
498 strerror (errno));
499 MM_LOG (tmp,ERROR);
500 unlink (mbx); /* delete the file */
502 close (fd); /* close file */
505 /* set proper protections */
506 return ret ? set_mbx_protections (mailbox,mbx) : NIL;
509 /* MMDF mail delete mailbox
510 * Accepts: MAIL stream
511 * mailbox name to delete
512 * Returns: T on success, NIL on failure
515 long mmdf_delete (MAILSTREAM *stream,char *mailbox)
517 return mmdf_rename (stream,mailbox,NIL);
521 /* MMDF mail rename mailbox
522 * Accepts: MAIL stream
523 * old mailbox name
524 * new mailbox name (or NIL for delete)
525 * Returns: T on success, NIL on failure
528 long mmdf_rename (MAILSTREAM *stream,char *old,char *newname)
530 long ret = NIL;
531 char c,*s = NIL;
532 char tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
533 DOTLOCK lockx;
534 int fd,ld;
535 long i;
536 struct stat sbuf;
537 MM_CRITICAL (stream); /* get the c-client lock */
538 if (!dummy_file (file,old) ||
539 (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
540 ((s = strrchr (tmp,'/')) && !s[1])))){
541 if(newname) sprintf (tmp,
542 "Can't rename mailbox %.80s to %.80s: invalid name",
543 old,newname);
544 else
545 sprintf (tmp, "Can't delete mailbox %.80s: invalid name", old);
546 } /* lock out other c-clients */
547 else if ((ld = lockname (lock,file,LOCK_EX|LOCK_NB,&i)) < 0)
548 sprintf (tmp,"Mailbox %.80s is in use by another process",old);
550 else {
551 if ((fd = mmdf_lock (file,O_RDWR,
552 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
553 &lockx,LOCK_EX)) < 0)
554 sprintf (tmp,"Can't lock mailbox %.80s: %s",old,strerror (errno));
555 else {
556 if (newname) { /* want rename? */
557 /* found superior to destination name? */
558 if ((s = strrchr (s,'/')) != NULL) {
559 c = *++s; /* remember first character of inferior */
560 *s = '\0'; /* tie off to get just superior */
561 /* name doesn't exist, create it */
562 if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
563 !dummy_create_path (stream,tmp,get_dir_protection (newname))) {
564 mmdf_unlock (fd,NIL,&lockx);
565 mmdf_unlock (ld,NIL,NIL);
566 unlink (lock);
567 MM_NOCRITICAL (stream);
568 return ret; /* return success or failure */
570 *s = c; /* restore full name */
572 if (rename (file,tmp))
573 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
574 strerror (errno));
575 else ret = T; /* set success */
577 else if (unlink (file))
578 sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
579 else ret = T; /* set success */
580 mmdf_unlock (fd,NIL,&lockx);
582 mmdf_unlock (ld,NIL,NIL); /* flush the lock */
583 unlink (lock);
585 MM_NOCRITICAL (stream); /* no longer critical */
586 if (!ret) MM_LOG (tmp,ERROR); /* log error */
587 return ret; /* return success or failure */
590 /* MMDF mail open
591 * Accepts: Stream to open
592 * Returns: Stream on success, NIL on failure
595 MAILSTREAM *mmdf_open (MAILSTREAM *stream)
597 long i;
598 int fd;
599 char tmp[MAILTMPLEN];
600 DOTLOCK lock;
601 long retry;
602 /* return prototype for OP_PROTOTYPE call */
603 if (!stream) return user_flags (&mmdfproto);
604 retry = stream->silent ? 1 : KODRETRY;
605 if (stream->local) fatal ("mmdf recycle stream");
606 stream->local = memset (fs_get (sizeof (MMDFLOCAL)),0,sizeof (MMDFLOCAL));
607 /* note if an INBOX or not */
608 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
609 /* canonicalize the stream mailbox name */
610 if (!dummy_file (tmp,stream->mailbox)) {
611 sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
612 MM_LOG (tmp,ERROR);
613 return NIL;
615 /* flush old name */
616 fs_give ((void **) &stream->mailbox);
617 /* save canonical name */
618 stream->mailbox = cpystr (tmp);
619 LOCAL->fd = LOCAL->ld = -1; /* no file or state locking yet */
620 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
621 LOCAL->buflen = CHUNKSIZE - 1;
622 LOCAL->text.data = (unsigned char *) fs_get (CHUNKSIZE);
623 LOCAL->text.size = CHUNKSIZE - 1;
624 LOCAL->linebuf = (char *) fs_get (CHUNKSIZE);
625 LOCAL->linebuflen = CHUNKSIZE - 1;
626 stream->sequence++; /* bump sequence number */
628 /* make lock for read/write access */
629 if (!stream->rdonly) while (retry) {
630 /* try to lock file */
631 if ((fd = lockname (tmp,stream->mailbox,LOCK_EX|LOCK_NB,&i)) < 0) {
632 /* suppressing kiss-of-death? */
633 if (stream->nokod) retry = 0;
634 /* no, first time through? */
635 else if (retry-- == KODRETRY) {
636 /* learned other guy's PID and can signal? */
637 if (i && !kill ((int) i,SIGUSR2)) {
638 sprintf (tmp,"Trying to get mailbox lock from process %ld",i);
639 MM_LOG (tmp,WARN);
641 else retry = 0; /* give up */
643 if (!stream->silent) { /* nothing if silent stream */
644 if (retry) sleep (1); /* wait a second before trying again */
645 else MM_LOG ("Mailbox is open by another process, access is readonly",
646 WARN);
649 else { /* got the lock, nobody else can alter state */
650 LOCAL->ld = fd; /* note lock's fd and name */
651 LOCAL->lname = cpystr (tmp);
652 /* make sure mode OK (don't use fchmod()) */
653 chmod (LOCAL->lname,(long) mail_parameters (NIL,GET_LOCKPROTECTION,NIL));
654 if (stream->silent) i = 0;/* silent streams won't accept KOD */
655 else { /* note our PID in the lock */
656 sprintf (tmp,"%d",getpid ());
657 write (fd,tmp,(i = strlen (tmp))+1);
659 ftruncate (fd,i); /* make sure tied off */
660 fsync (fd); /* make sure it's available */
661 retry = 0; /* no more need to try */
665 /* parse mailbox */
666 stream->nmsgs = stream->recent = 0;
667 /* will we be able to get write access? */
668 if ((LOCAL->ld >= 0) && access (stream->mailbox,W_OK) && (errno == EACCES)) {
669 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);
670 flock (LOCAL->ld,LOCK_UN); /* release the lock */
671 close (LOCAL->ld); /* close the lock file */
672 LOCAL->ld = -1; /* no more lock fd */
673 unlink (LOCAL->lname); /* delete it */
675 /* reset UID validity */
676 stream->uid_validity = stream->uid_last = 0;
677 if (stream->silent && !stream->rdonly && (LOCAL->ld < 0))
678 mmdf_abort (stream); /* abort if can't get RW silent stream */
679 /* parse mailbox */
680 else if (mmdf_parse (stream,&lock,LOCK_SH)) {
681 mmdf_unlock (LOCAL->fd,stream,&lock);
682 mail_unlock (stream);
683 MM_NOCRITICAL (stream); /* done with critical */
685 if (!LOCAL) return NIL; /* failure if stream died */
686 /* make sure upper level knows readonly */
687 stream->rdonly = (LOCAL->ld < 0);
688 /* notify about empty mailbox */
689 if (!(stream->nmsgs || stream->silent)) MM_LOG ("Mailbox is empty",NIL);
690 if (!stream->rdonly) { /* flags stick if readwrite */
691 stream->perm_seen = stream->perm_deleted =
692 stream->perm_flagged = stream->perm_answered = stream->perm_draft = T;
693 if (!stream->uid_nosticky) {/* users with lives get permanent keywords */
694 stream->perm_user_flags = 0xffffffff;
695 /* and maybe can create them too! */
696 stream->kwd_create = stream->user_flags[NUSERFLAGS-1] ? NIL : T;
699 return stream; /* return stream alive to caller */
703 /* MMDF mail close
704 * Accepts: MAIL stream
705 * close options
708 void mmdf_close (MAILSTREAM *stream,long options)
710 int silent = stream->silent;
711 stream->silent = T; /* go silent */
712 /* expunge if requested */
713 if (options & CL_EXPUNGE) mmdf_expunge (stream,NIL,NIL);
714 /* else dump final checkpoint */
715 else if (LOCAL->dirty) mmdf_check (stream);
716 stream->silent = silent; /* restore old silence state */
717 mmdf_abort (stream); /* now punt the file and local data */
720 /* MMDF mail fetch message header
721 * Accepts: MAIL stream
722 * message # to fetch
723 * pointer to returned header text length
724 * option flags
725 * Returns: message header in RFC822 format
728 /* lines to filter from header */
729 static STRINGLIST *mmdf_hlines = NIL;
731 char *mmdf_header (MAILSTREAM *stream,unsigned long msgno,
732 unsigned long *length,long flags)
734 MESSAGECACHE *elt;
735 unsigned char *s,*t,*tl;
736 *length = 0; /* default to empty */
737 if (flags & FT_UID) return "";/* UID call "impossible" */
738 elt = mail_elt (stream,msgno);/* get cache */
739 if (!mmdf_hlines) { /* once only code */
740 STRINGLIST *lines = mmdf_hlines = mail_newstringlist ();
741 lines->text.size = strlen ((char *) (lines->text.data =
742 (unsigned char *) "Status"));
743 lines = lines->next = mail_newstringlist ();
744 lines->text.size = strlen ((char *) (lines->text.data =
745 (unsigned char *) "X-Status"));
746 lines = lines->next = mail_newstringlist ();
747 lines->text.size = strlen ((char *) (lines->text.data =
748 (unsigned char *) "X-Keywords"));
749 lines = lines->next = mail_newstringlist ();
750 lines->text.size = strlen ((char *) (lines->text.data =
751 (unsigned char *) "X-UID"));
752 lines = lines->next = mail_newstringlist ();
753 lines->text.size = strlen ((char *) (lines->text.data =
754 (unsigned char *) "X-IMAP"));
755 lines = lines->next = mail_newstringlist ();
756 lines->text.size = strlen ((char *) (lines->text.data =
757 (unsigned char *) "X-IMAPbase"));
759 /* go to header position */
760 lseek (LOCAL->fd,elt->private.special.offset +
761 elt->private.msg.header.offset,L_SET);
763 if (flags & FT_INTERNAL) { /* initial data OK? */
764 if (elt->private.msg.header.text.size > LOCAL->buflen) {
765 fs_give ((void **) &LOCAL->buf);
766 LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
767 elt->private.msg.header.text.size) + 1);
769 /* read message */
770 read (LOCAL->fd,LOCAL->buf,elt->private.msg.header.text.size);
771 /* got text, tie off string */
772 LOCAL->buf[*length = elt->private.msg.header.text.size] = '\0';
773 /* squeeze out CRs (in case from PC) */
774 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
775 if (*t != '\r') *s++ = *t;
776 *s = '\0';
777 *length = s - LOCAL->buf; /* adjust length */
779 else { /* need to make a CRLF version */
780 read (LOCAL->fd,s = (char *) fs_get (elt->private.msg.header.text.size+1),
781 elt->private.msg.header.text.size);
782 /* tie off string, and convert to CRLF */
783 s[elt->private.msg.header.text.size] = '\0';
784 *length = strcrlfcpy (&LOCAL->buf,&LOCAL->buflen,s,
785 elt->private.msg.header.text.size);
786 fs_give ((void **) &s); /* free readin buffer */
787 /* squeeze out spurious CRs */
788 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
789 if ((*t != '\r') || (t[1] == '\n')) *s++ = *t;
790 *s = '\0';
791 *length = s - LOCAL->buf; /* adjust length */
793 *length = mail_filter (LOCAL->buf,*length,mmdf_hlines,FT_NOT);
794 return (char *) LOCAL->buf; /* return processed copy */
797 /* MMDF mail fetch message text
798 * Accepts: MAIL stream
799 * message # to fetch
800 * pointer to returned stringstruct
801 * option flags
802 * Returns: T on success, NIL if failure
805 long mmdf_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
807 char *s;
808 unsigned long i;
809 MESSAGECACHE *elt;
810 /* UID call "impossible" */
811 if (flags & FT_UID) return NIL;
812 elt = mail_elt (stream,msgno);/* get cache element */
813 /* if message not seen */
814 if (!(flags & FT_PEEK) && !elt->seen) {
815 /* mark message seen and dirty */
816 elt->seen = elt->private.dirty = LOCAL->dirty = T;
817 MM_FLAGS (stream,msgno);
819 s = mmdf_text_work (stream,elt,&i,flags);
820 INIT (bs,mail_string,s,i); /* set up stringstruct */
821 return T; /* success */
824 /* MMDF mail fetch message text worker routine
825 * Accepts: MAIL stream
826 * message cache element
827 * pointer to returned header text length
828 * option flags
831 char *mmdf_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
832 unsigned long *length,long flags)
834 FDDATA d;
835 STRING bs;
836 unsigned char c,*s,*t,*tl,tmp[CHUNKSIZE];
837 /* go to text position */
838 lseek (LOCAL->fd,elt->private.special.offset +
839 elt->private.msg.text.offset,L_SET);
840 if (flags & FT_INTERNAL) { /* initial data OK? */
841 if (elt->private.msg.text.text.size > LOCAL->buflen) {
842 fs_give ((void **) &LOCAL->buf);
843 LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
844 elt->private.msg.text.text.size) + 1);
846 /* read message */
847 read (LOCAL->fd,LOCAL->buf,elt->private.msg.text.text.size);
848 /* got text, tie off string */
849 LOCAL->buf[*length = elt->private.msg.text.text.size] = '\0';
850 /* squeeze out CRs (in case from PC) */
851 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
852 if (*t != '\r') *s++ = *t;
853 *s = '\0';
854 *length = s - LOCAL->buf; /* adjust length */
855 return (char *) LOCAL->buf;
858 /* have it cached already? */
859 if (elt->private.uid != LOCAL->uid) {
860 /* not cached, cache it now */
861 LOCAL->uid = elt->private.uid;
862 /* is buffer big enough? */
863 if (elt->rfc822_size > LOCAL->text.size) {
864 /* excessively conservative, but the right thing is too hard to do */
865 fs_give ((void **) &LOCAL->text.data);
866 LOCAL->text.data = (unsigned char *)
867 fs_get ((LOCAL->text.size = elt->rfc822_size) + 1);
869 d.fd = LOCAL->fd; /* yes, set up file descriptor */
870 d.pos = elt->private.special.offset + elt->private.msg.text.offset;
871 d.chunk = tmp; /* initial buffer chunk */
872 d.chunksize = CHUNKSIZE; /* file chunk size */
873 INIT (&bs,fd_string,&d,elt->private.msg.text.text.size);
874 for (s = (char *) LOCAL->text.data; SIZE (&bs);) switch (c = SNX (&bs)) {
875 case '\r': /* carriage return seen */
876 break;
877 case '\n':
878 *s++ = '\r'; /* insert a CR */
879 default:
880 *s++ = c; /* copy characters */
882 *s = '\0'; /* tie off buffer */
883 /* calculate length of cached data */
884 LOCAL->textlen = s - LOCAL->text.data;
886 *length = LOCAL->textlen; /* return from cache */
887 return (char *) LOCAL->text.data;
890 /* MMDF per-message modify flag
891 * Accepts: MAIL stream
892 * message cache element
895 void mmdf_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
897 /* only after finishing */
898 if (elt->valid) elt->private.dirty = LOCAL->dirty = T;
902 /* MMDF mail ping mailbox
903 * Accepts: MAIL stream
904 * Returns: T if stream alive, else NIL
907 long mmdf_ping (MAILSTREAM *stream)
909 DOTLOCK lock;
910 struct stat sbuf;
911 long reparse;
912 /* big no-op if not readwrite */
913 if (LOCAL && (LOCAL->ld >= 0) && !stream->lock) {
914 if (stream->rdonly) { /* does he want to give up readwrite? */
915 /* checkpoint if we changed something */
916 if (LOCAL->dirty) mmdf_check (stream);
917 flock (LOCAL->ld,LOCK_UN);/* release readwrite lock */
918 close (LOCAL->ld); /* close the readwrite lock file */
919 LOCAL->ld = -1; /* no more readwrite lock fd */
920 unlink (LOCAL->lname); /* delete the readwrite lock file */
922 else { /* see if need to reparse */
923 if (!(reparse = (long) mail_parameters (NIL,GET_NETFSSTATBUG,NIL))) {
924 /* get current mailbox size */
925 if (LOCAL->fd >= 0) fstat (LOCAL->fd,&sbuf);
926 else if (stat (stream->mailbox,&sbuf)) {
927 sprintf (LOCAL->buf,"Mailbox stat failed, aborted: %s",
928 strerror (errno));
929 MM_LOG (LOCAL->buf,ERROR);
930 mmdf_abort (stream);
931 return NIL;
933 reparse = (sbuf.st_size != LOCAL->filesize);
935 /* parse if mailbox changed */
936 if ((LOCAL->ddirty || reparse) && mmdf_parse (stream,&lock,LOCK_EX)) {
937 /* force checkpoint if double-dirty */
938 if (LOCAL->ddirty) mmdf_rewrite (stream,NIL,&lock,NIL);
939 /* unlock mailbox */
940 else mmdf_unlock (LOCAL->fd,stream,&lock);
941 mail_unlock (stream); /* and stream */
942 /* done with critical */
943 MM_NOCRITICAL (stream);
947 return LOCAL ? LONGT : NIL; /* return if still alive */
950 /* MMDF mail check mailbox
951 * Accepts: MAIL stream
954 void mmdf_check (MAILSTREAM *stream)
956 DOTLOCK lock;
957 /* parse and lock mailbox */
958 if (LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
959 mmdf_parse (stream,&lock,LOCK_EX)) {
960 /* any unsaved changes? */
961 if (LOCAL->dirty && mmdf_rewrite (stream,NIL,&lock,NIL)) {
962 if (!stream->silent) MM_LOG ("Checkpoint completed",NIL);
964 /* no checkpoint needed, just unlock */
965 else mmdf_unlock (LOCAL->fd,stream,&lock);
966 mail_unlock (stream); /* unlock the stream */
967 MM_NOCRITICAL (stream); /* done with critical */
972 /* MMDF mail expunge mailbox
973 * Accepts: MAIL stream
974 * sequence to expunge if non-NIL
975 * expunge options
976 * Returns: T, always
979 long mmdf_expunge (MAILSTREAM *stream,char *sequence,long options)
981 long ret;
982 unsigned long i;
983 DOTLOCK lock;
984 char *msg = NIL;
985 if ((ret = (sequence ? ((options & EX_UID) ?
986 mail_uid_sequence (stream,sequence) :
987 mail_sequence (stream,sequence)) : LONGT) != 0L) &&
988 LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
989 mmdf_parse (stream,&lock,LOCK_EX)) {
990 /* check expunged messages if not dirty */
991 for (i = 1; !LOCAL->dirty && (i <= stream->nmsgs); i++) {
992 MESSAGECACHE *elt = mail_elt (stream,i);
993 if (mail_elt (stream,i)->deleted) LOCAL->dirty = T;
995 if (!LOCAL->dirty) { /* not dirty and no expunged messages */
996 mmdf_unlock (LOCAL->fd,stream,&lock);
997 msg = "No messages deleted, so no update needed";
999 else if (mmdf_rewrite (stream,&i,&lock,sequence ? LONGT : NIL)) {
1000 if (i) sprintf (msg = LOCAL->buf,"Expunged %lu messages",i);
1001 else msg = "Mailbox checkpointed, but no messages expunged";
1003 /* rewrite failed */
1004 else mmdf_unlock (LOCAL->fd,stream,&lock);
1005 mail_unlock (stream); /* unlock the stream */
1006 MM_NOCRITICAL (stream); /* done with critical */
1007 if (msg && !stream->silent) MM_LOG (msg,NIL);
1009 else if (!stream->silent)
1010 MM_LOG ("Expunge ignored on readonly mailbox",WARN);
1011 return ret;
1014 /* MMDF mail copy message(s)
1015 * Accepts: MAIL stream
1016 * sequence
1017 * destination mailbox
1018 * copy options
1019 * Returns: T if copy successful, else NIL
1022 long mmdf_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
1024 struct stat sbuf;
1025 int fd;
1026 char *s,file[MAILTMPLEN];
1027 DOTLOCK lock;
1028 time_t tp[2];
1029 unsigned long i,j;
1030 MESSAGECACHE *elt;
1031 long ret = T;
1032 mailproxycopy_t pc =
1033 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
1034 copyuid_t cu = (copyuid_t) (mail_parameters (NIL,GET_USERHASNOLIFE,NIL) ?
1035 NIL : mail_parameters (NIL,GET_COPYUID,NIL));
1036 SEARCHSET *source = cu ? mail_newsearchset () : NIL;
1037 SEARCHSET *dest = cu ? mail_newsearchset () : NIL;
1038 MAILSTREAM *tstream = NIL;
1039 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
1040 mail_sequence (stream,sequence))) return NIL;
1041 /* make sure destination is valid */
1042 if (!(mmdf_valid (mailbox) || !errno))
1043 switch (errno) {
1044 case ENOENT: /* no such file? */
1045 if (compare_cstring (mailbox,"INBOX")) {
1046 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
1047 return NIL;
1049 if (pc) return (*pc) (stream,sequence,mailbox,options);
1050 mmdf_create (NIL,"INBOX");/* create empty INBOX */
1051 case EACCES: /* file protected */
1052 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
1053 MM_LOG (LOCAL->buf,ERROR);
1054 return NIL;
1055 case EINVAL:
1056 if (pc) return (*pc) (stream,sequence,mailbox,options);
1057 sprintf (LOCAL->buf,"Invalid MMDF-format mailbox name: %.80s",mailbox);
1058 MM_LOG (LOCAL->buf,ERROR);
1059 return NIL;
1060 default:
1061 if (pc) return (*pc) (stream,sequence,mailbox,options);
1062 sprintf (LOCAL->buf,"Not a MMDF-format mailbox: %.80s",mailbox);
1063 MM_LOG (LOCAL->buf,ERROR);
1064 return NIL;
1067 /* try to open rewrite for UIDPLUS */
1068 if ((tstream = mail_open_work (&mmdfdriver,NIL,mailbox,
1069 OP_SILENT|OP_NOKOD)) && tstream->rdonly)
1070 tstream = mail_close (tstream);
1071 if (cu && !tstream) { /* wanted a COPYUID? */
1072 sprintf (LOCAL->buf,"Unable to write-open mailbox for COPYUID: %.80s",
1073 mailbox);
1074 MM_LOG (LOCAL->buf,WARN);
1075 cu = NIL; /* don't try to do COPYUID */
1077 LOCAL->buf[0] = '\0';
1078 MM_CRITICAL (stream); /* go critical */
1079 if ((fd = mmdf_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND,
1080 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
1081 &lock,LOCK_EX)) < 0) {
1082 MM_NOCRITICAL (stream); /* done with critical */
1083 sprintf (LOCAL->buf,"Can't open destination mailbox: %s",strerror (errno));
1084 MM_LOG (LOCAL->buf,ERROR); /* log the error */
1085 return NIL; /* failed */
1087 fstat (fd,&sbuf); /* get current file size */
1088 /* write all requested messages to mailbox */
1089 for (i = 1; ret && (i <= stream->nmsgs); i++)
1090 if ((elt = mail_elt (stream,i))->sequence) {
1091 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
1092 read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
1093 if (write (fd,LOCAL->buf,elt->private.special.text.size) < 0) ret = NIL;
1094 else { /* internal header succeeded */
1095 s = mmdf_header (stream,i,&j,FT_INTERNAL);
1096 /* header size, sans trailing newline */
1097 if (j && (s[j - 2] == '\n')) j--;
1098 if (write (fd,s,j) < 0) ret = NIL;
1099 else { /* message header succeeded */
1100 j = tstream ? /* write UIDPLUS data if have readwrite */
1101 mmdf_xstatus (stream,LOCAL->buf,elt,++(tstream->uid_last),LONGT) :
1102 mmdf_xstatus (stream,LOCAL->buf,elt,NIL,NIL);
1103 if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
1104 else { /* message status succeeded */
1105 s = mmdf_text_work (stream,elt,&j,FT_INTERNAL);
1106 if ((write (fd,s,j) < 0) || (write (fd,mmdfhdr,MMDFHDRLEN) < 0))
1107 ret = NIL;
1108 else if (cu) { /* need to pass back new UID? */
1109 mail_append_set (source,mail_uid (stream,i));
1110 mail_append_set (dest,tstream->uid_last);
1117 if (!ret || fsync (fd)) { /* force out the update */
1118 sprintf (LOCAL->buf,"Message copy failed: %s",strerror (errno));
1119 ftruncate (fd,sbuf.st_size);
1120 ret = NIL;
1122 /* force UIDVALIDITY assignment now */
1123 if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
1124 /* return sets if doing COPYUID */
1125 if (cu && ret) (*cu) (stream,mailbox,tstream->uid_validity,source,dest);
1126 else { /* flush any sets we may have built */
1127 mail_free_searchset (&source);
1128 mail_free_searchset (&dest);
1130 tp[1] = time (0); /* set mtime to now */
1131 if (ret) tp[0] = tp[1] - 1; /* set atime to now-1 if successful copy */
1132 else tp[0] = /* else preserve \Marked status */
1133 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
1134 sbuf.st_atime : tp[1];
1135 utime (file,tp); /* set the times */
1136 mmdf_unlock (fd,NIL,&lock); /* unlock and close mailbox */
1137 if (tstream) { /* update last UID if we can */
1138 MMDFLOCAL *local = (MMDFLOCAL *) tstream->local;
1139 local->dirty = T; /* do a rewrite */
1140 local->appending = T; /* but not at the cost of marking as old */
1141 tstream = mail_close (tstream);
1143 /* log the error */
1144 if (!ret) MM_LOG (LOCAL->buf,ERROR);
1145 /* delete if requested message */
1146 else if (options & CP_MOVE) for (i = 1; i <= stream->nmsgs; i++)
1147 if ((elt = mail_elt (stream,i))->sequence)
1148 elt->deleted = elt->private.dirty = LOCAL->dirty = T;
1149 MM_NOCRITICAL (stream); /* release critical */
1150 return ret;
1153 /* MMDF mail append message from stringstruct
1154 * Accepts: MAIL stream
1155 * destination mailbox
1156 * append callback
1157 * data for callback
1158 * Returns: T if append successful, else NIL
1161 #define BUFLEN 8*MAILTMPLEN
1163 long mmdf_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
1165 struct stat sbuf;
1166 int fd;
1167 unsigned long i;
1168 char *flags,*date,buf[BUFLEN],tmp[MAILTMPLEN],file[MAILTMPLEN];
1169 time_t tp[2];
1170 FILE *sf,*df;
1171 MESSAGECACHE elt;
1172 DOTLOCK lock;
1173 STRING *message;
1174 unsigned long uidlocation = 0;
1175 appenduid_t au = (appenduid_t)
1176 (mail_parameters (NIL,GET_USERHASNOLIFE,NIL) ? NIL :
1177 mail_parameters (NIL,GET_APPENDUID,NIL));
1178 SEARCHSET *dst = au ? mail_newsearchset () : NIL;
1179 long ret = LONGT;
1180 MAILSTREAM *tstream = NIL;
1181 /* default stream to prototype */
1182 if (!stream) { /* stream specified? */
1183 stream = &mmdfproto; /* no, default stream to prototype */
1184 for (i = 0; i < NUSERFLAGS && stream->user_flags[i]; ++i)
1185 fs_give ((void **) &stream->user_flags[i]);
1187 if (!mmdf_valid (mailbox)) switch (errno) {
1188 case ENOENT: /* no such file? */
1189 if (compare_cstring (mailbox,"INBOX")) {
1190 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
1191 return NIL;
1193 mmdf_create (NIL,"INBOX"); /* create empty INBOX */
1194 case 0: /* merely empty file? */
1195 tstream = stream;
1196 break;
1197 case EACCES: /* file protected */
1198 sprintf (tmp,"Can't access destination: %.80s",mailbox);
1199 MM_LOG (tmp,ERROR);
1200 return NIL;
1201 case EINVAL:
1202 sprintf (tmp,"Invalid MMDF-format mailbox name: %.80s",mailbox);
1203 MM_LOG (tmp,ERROR);
1204 return NIL;
1205 default:
1206 sprintf (tmp,"Not a MMDF-format mailbox: %.80s",mailbox);
1207 MM_LOG (tmp,ERROR);
1208 return NIL;
1210 /* get sniffing stream for keywords */
1211 else if (!(tstream = mail_open (NIL,mailbox,
1212 OP_READONLY|OP_SILENT|OP_NOKOD|OP_SNIFF))) {
1213 sprintf (tmp,"Unable to examine mailbox for APPEND: %.80s",mailbox);
1214 MM_LOG (tmp,ERROR);
1215 return NIL;
1218 /* get first message */
1219 if (!MM_APPEND (af) (tstream,data,&flags,&date,&message)) return NIL;
1220 if (!(sf = tmpfile ())) { /* must have scratch file */
1221 sprintf (tmp,".%lx.%lx",(unsigned long) time (0),(unsigned long)getpid ());
1222 if (!stat (tmp,&sbuf) || !(sf = fopen (tmp,"wb+"))) {
1223 sprintf (tmp,"Unable to create scratch file: %.80s",strerror (errno));
1224 MM_LOG (tmp,ERROR);
1225 return NIL;
1227 unlink (tmp);
1229 do { /* parse date */
1230 if (!date) rfc822_date (date = tmp);
1231 if (!mail_parse_date (&elt,date)) {
1232 sprintf (tmp,"Bad date in append: %.80s",date);
1233 MM_LOG (tmp,ERROR);
1235 else { /* user wants to suppress time zones? */
1236 if (mail_parameters (NIL,GET_NOTIMEZONES,NIL)) {
1237 time_t when = mail_longdate (&elt);
1238 date = ctime (&when); /* use traditional date */
1240 /* use POSIX-style date */
1241 else date = mail_cdate (tmp,&elt);
1242 if (!SIZE (message)) MM_LOG ("Append of zero-length message",ERROR);
1243 else if (!mmdf_collect_msg (tstream,sf,flags,date,message)) {
1244 sprintf (tmp,"Error writing scratch file: %.80s",strerror (errno));
1245 MM_LOG (tmp,ERROR);
1247 /* get next message */
1248 else if (MM_APPEND (af) (tstream,data,&flags,&date,&message)) continue;
1250 fclose (sf); /* punt scratch file */
1251 return NIL; /* give up */
1252 } while (message); /* until no more messages */
1253 if (fflush (sf)) {
1254 sprintf (tmp,"Error finishing scratch file: %.80s",strerror (errno));
1255 MM_LOG (tmp,ERROR);
1256 fclose (sf); /* punt scratch file */
1257 return NIL; /* give up */
1259 i = ftell (sf); /* size of scratch file */
1260 if (tstream != stream) tstream = mail_close (tstream);
1262 MM_CRITICAL (stream); /* go critical */
1263 /* try to open readwrite for UIDPLUS */
1264 if ((tstream = mail_open_work (&mmdfdriver,NIL,mailbox,
1265 OP_SILENT|OP_NOKOD)) && tstream->rdonly)
1266 tstream = mail_close (tstream);
1267 if (au && !tstream) { /* wanted an APPENDUID? */
1268 sprintf (tmp,"Unable to re-open mailbox for APPENDUID: %.80s",mailbox);
1269 MM_LOG (tmp,WARN);
1270 au = NIL;
1272 if (((fd = mmdf_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND,
1273 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
1274 &lock,LOCK_EX)) < 0) ||
1275 !(df = fdopen (fd,"ab"))) {
1276 MM_NOCRITICAL (stream); /* done with critical */
1277 sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
1278 MM_LOG (tmp,ERROR);
1279 return NIL;
1281 fstat (fd,&sbuf); /* get current file size */
1282 rewind (sf);
1283 tp[1] = time (0); /* set mtime to now */
1284 /* write all messages */
1285 if (!mmdf_append_msgs (tstream,sf,df,au ? dst : NIL) ||
1286 (fflush (df) == EOF) || fsync (fd)) {
1287 sprintf (buf,"Message append failed: %s",strerror (errno));
1288 MM_LOG (buf,ERROR);
1289 ftruncate (fd,sbuf.st_size);
1290 tp[0] = /* preserve \Marked status */
1291 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
1292 sbuf.st_atime : tp[1];
1293 ret = NIL; /* return error */
1295 else tp[0] = tp[1] - 1; /* set atime to now-1 if successful copy */
1296 utime (file,tp); /* set the times */
1297 fclose (sf); /* done with scratch file */
1298 /* force UIDVALIDITY assignment now */
1299 if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
1300 /* return sets if doing APPENDUID */
1301 if (au && ret) (*au) (mailbox,tstream->uid_validity,dst);
1302 else mail_free_searchset (&dst);
1303 mmdf_unlock (fd,NIL,&lock); /* unlock and close mailbox */
1304 fclose (df);
1305 if (tstream) { /* update last UID if we can */
1306 MMDFLOCAL *local = (MMDFLOCAL *) tstream->local;
1307 local->dirty = T; /* do a rewrite */
1308 local->appending = T; /* but not at the cost of marking as old */
1309 tstream = mail_close (tstream);
1311 MM_NOCRITICAL (stream); /* release critical */
1312 return ret;
1315 /* Collect and write single message to append scratch file
1316 * Accepts: MAIL stream
1317 * scratch file
1318 * flags
1319 * date
1320 * message stringstruct
1321 * Returns: NIL if write error, else T
1324 int mmdf_collect_msg (MAILSTREAM *stream,FILE *sf,char *flags,char *date,
1325 STRING *msg)
1327 unsigned char *s,*t;
1328 unsigned long uf;
1329 long f = mail_parse_flags (stream,flags,&uf);
1330 /* write metadata, note date ends with NL */
1331 if (fprintf (sf,"%ld %lu %s",f,SIZE (msg) + 1,date) < 0) return NIL;
1332 while (uf) /* write user flags */
1333 if ((s = stream->user_flags[find_rightmost_bit (&uf)]) &&
1334 (fprintf (sf," %s",s) < 0)) return NIL;
1335 if (putc ('\n',sf) == EOF) return NIL;
1336 while (SIZE (msg)) { /* copy text to scratch file */
1337 for (s = (unsigned char *) msg->curpos, t = s + msg->cursize; s < t; ++s)
1338 if (!*s) *s = 0x80; /* disallow NUL */
1339 /* write buffered text */
1340 if (fwrite (msg->curpos,1,msg->cursize,sf) == msg->cursize)
1341 SETPOS (msg,GETPOS (msg) + msg->cursize);
1342 else return NIL; /* failed */
1344 /* write trailing newline and return */
1345 return (putc ('\n',sf) == EOF) ? NIL : T;
1348 /* Append messages from scratch file to mailbox
1349 * Accepts: MAIL stream
1350 * source file
1351 * destination file
1352 * uidset to update if non-NIL
1353 * Returns: T if success, NIL if failure
1356 int mmdf_append_msgs (MAILSTREAM *stream,FILE *sf,FILE *df,SEARCHSET *set)
1358 int c;
1359 long f;
1360 unsigned long i,j;
1361 char *x,tmp[MAILTMPLEN];
1362 int hdrp = T;
1363 /* get message metadata line */
1364 while (fgets (tmp,MAILTMPLEN,sf)) {
1365 if (!(isdigit (tmp[0]) && strchr (tmp,'\n'))) return NIL;
1366 f = strtol (tmp,&x,10); /* get flags */
1367 if (!((*x++ == ' ') && isdigit (*x))) return NIL;
1368 i = strtoul (x,&x,10); /* get message size */
1369 if ((*x++ != ' ') || /* build initial header */
1370 (fprintf (df,"%sFrom %s@%s %sStatus: ",mmdfhdr,myusername(),
1371 mylocalhost(),x) < 0) ||
1372 (f&fSEEN && (putc ('R',df) == EOF)) ||
1373 (fputs ("\nX-Status: ",df) == EOF) ||
1374 (f&fDELETED && (putc ('D',df) == EOF)) ||
1375 (f&fFLAGGED && (putc ('F',df) == EOF)) ||
1376 (f&fANSWERED && (putc ('A',df) == EOF)) ||
1377 (f&fDRAFT && (putc ('T',df) == EOF)) ||
1378 (fputs ("\nX-Keywords:",df) == EOF)) return NIL;
1379 /* copy keywords */
1380 while ((c = getc (sf)) != '\n') switch (c) {
1381 case EOF:
1382 return NIL;
1383 default:
1384 if (putc (c,df) == EOF) return NIL;
1386 if ((putc ('\n',df) == EOF) ||
1387 (set && (fprintf (df,"X-UID: %lu\n",++(stream->uid_last)) < 0)))
1388 return NIL;
1390 for (c = '\n'; i && fgets (tmp,MAILTMPLEN,sf); c = tmp[j-1]) {
1391 /* get read line length */
1392 if (i < (j = strlen (tmp))) fatal ("mmdf_append_msgs overrun");
1393 i -= j; /* number of bytes left */
1394 /* squish out ^A and CRs (note copies NUL) */
1395 for (x = tmp; (x = strpbrk (x,"\01\r")) != NULL; --j) memmove (x,x+1,j-(x-tmp));
1396 if (!j) continue; /* do nothing if line emptied */
1397 /* start of line? */
1398 if (c == '\n') switch (tmp[0]) {
1399 case 'S': case 's': /* possible "Status:" */
1400 if (hdrp && (j > 6) && ((tmp[1] == 't') || (tmp[1] == 'T')) &&
1401 ((tmp[2] == 'a') || (tmp[2] == 'A')) &&
1402 ((tmp[3] == 't') || (tmp[3] == 'T')) &&
1403 ((tmp[4] == 'u') || (tmp[4] == 'U')) &&
1404 ((tmp[5] == 's') || (tmp[5] == 'S')) && (tmp[6] == ':') &&
1405 (fputs ("X-Original-",df) == EOF)) return NIL;
1406 break;
1407 case 'X': case 'x': /* possible X-??? header */
1408 if (hdrp && (tmp[1] == '-') &&
1409 /* possible X-UID: */
1410 (((j > 5) && ((tmp[2] == 'U') || (tmp[2] == 'u')) &&
1411 ((tmp[3] == 'I') || (tmp[3] == 'i')) &&
1412 ((tmp[4] == 'D') || (tmp[4] == 'd')) && (tmp[5] == ':')) ||
1413 /* possible X-IMAP: */
1414 ((j > 6) && ((tmp[2] == 'I') || (tmp[2] == 'i')) &&
1415 ((tmp[3] == 'M') || (tmp[3] == 'm')) &&
1416 ((tmp[4] == 'A') || (tmp[4] == 'a')) &&
1417 ((tmp[5] == 'P') || (tmp[5] == 'p')) &&
1418 ((tmp[6] == ':') ||
1419 /* or X-IMAPbase: */
1420 ((j > 10) && ((tmp[6] == 'b') || (tmp[6] == 'B')) &&
1421 ((tmp[7] == 'a') || (tmp[7] == 'A')) &&
1422 ((tmp[8] == 's') || (tmp[8] == 'S')) &&
1423 ((tmp[9] == 'e') || (tmp[9] == 'E')) && (tmp[10] == ':')))) ||
1424 /* possible X-Status: */
1425 ((j > 8) && ((tmp[2] == 'S') || (tmp[2] == 's')) &&
1426 ((tmp[3] == 't') || (tmp[3] == 'T')) &&
1427 ((tmp[4] == 'a') || (tmp[4] == 'A')) &&
1428 ((tmp[5] == 't') || (tmp[5] == 'T')) &&
1429 ((tmp[6] == 'u') || (tmp[6] == 'U')) &&
1430 ((tmp[7] == 's') || (tmp[7] == 'S')) && (tmp[8] == ':')) ||
1431 /* possible X-Keywords: */
1432 ((j > 10) && ((tmp[2] == 'K') || (tmp[2] == 'k')) &&
1433 ((tmp[3] == 'e') || (tmp[3] == 'E')) &&
1434 ((tmp[4] == 'y') || (tmp[4] == 'Y')) &&
1435 ((tmp[5] == 'w') || (tmp[5] == 'W')) &&
1436 ((tmp[6] == 'o') || (tmp[6] == 'O')) &&
1437 ((tmp[7] == 'r') || (tmp[7] == 'R')) &&
1438 ((tmp[8] == 'd') || (tmp[8] == 'D')) &&
1439 ((tmp[9] == 's') || (tmp[9] == 'S')) && (tmp[10] == ':'))) &&
1440 (fputs ("X-Original-",df) == EOF)) return NIL;
1441 break;
1442 case '\n': /* blank line */
1443 hdrp = NIL;
1444 break;
1445 default: /* nothing to do */
1446 break;
1448 /* just write the line */
1449 if (fwrite (tmp,1,j,df) != j) return NIL;
1451 /* make sure read entire msg & wrote trailer */
1452 if (i || (fputs (mmdfhdr,df) == EOF)) return NIL;
1453 /* update set */
1454 if (stream) mail_append_set (set,stream->uid_last);
1456 return T;
1459 /* Internal routines */
1462 /* MMDF mail abort stream
1463 * Accepts: MAIL stream
1466 void mmdf_abort (MAILSTREAM *stream)
1468 if (LOCAL) { /* only if a file is open */
1469 if (LOCAL->fd >= 0) close (LOCAL->fd);
1470 if (LOCAL->ld >= 0) { /* have a mailbox lock? */
1471 flock (LOCAL->ld,LOCK_UN);/* yes, release the lock */
1472 close (LOCAL->ld); /* close the lock file */
1473 unlink (LOCAL->lname); /* and delete it */
1475 if (LOCAL->lname) fs_give ((void **) &LOCAL->lname);
1476 /* free local text buffers */
1477 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
1478 if (LOCAL->text.data) fs_give ((void **) &LOCAL->text.data);
1479 if (LOCAL->linebuf) fs_give ((void **) &LOCAL->linebuf);
1480 if (LOCAL->line) fs_give ((void **) &LOCAL->line);
1481 /* nuke the local data */
1482 fs_give ((void **) &stream->local);
1483 stream->dtb = NIL; /* log out the DTB */
1487 /* MMDF open and lock mailbox
1488 * Accepts: file name to open/lock
1489 * file open mode
1490 * destination buffer for lock file name
1491 * type of locking operation (LOCK_SH or LOCK_EX)
1494 int mmdf_lock (char *file,int flags,int mode,DOTLOCK *lock,int op)
1496 int fd;
1497 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
1498 (*bn) (BLOCK_FILELOCK,NIL);
1499 /* try locking the easy way */
1500 if (dotlock_lock (file,lock,-1)) {
1501 /* got dotlock file, easy open */
1502 if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
1503 else dotlock_unlock (lock); /* open failed, free the dotlock */
1505 /* no dot lock file, open file now */
1506 else if ((fd = open (file,flags,mode)) >= 0) {
1507 /* try paranoid way to make a dot lock file */
1508 if (dotlock_lock (file,lock,fd)) {
1509 close (fd); /* get fresh fd in case of timing race */
1510 if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
1511 /* open failed, free the dotlock */
1512 else dotlock_unlock (lock);
1514 else flock (fd,op); /* paranoid way failed, just flock() it */
1516 (*bn) (BLOCK_NONE,NIL);
1517 return fd;
1520 /* MMDF unlock and close mailbox
1521 * Accepts: file descriptor
1522 * (optional) mailbox stream to check atime/mtime
1523 * (optional) lock file name
1526 void mmdf_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock)
1528 if (stream) { /* need to muck with times? */
1529 struct stat sbuf;
1530 time_t tp[2];
1531 time_t now = time (0);
1532 fstat (fd,&sbuf); /* get file times */
1533 if (LOCAL->ld >= 0) { /* yes, readwrite session? */
1534 tp[0] = now; /* set atime to now */
1535 /* set mtime to (now - 1) if necessary */
1536 tp[1] = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;
1538 else if (stream->recent) { /* readonly with recent messages */
1539 if ((sbuf.st_atime >= sbuf.st_mtime) ||
1540 (sbuf.st_atime >= sbuf.st_ctime))
1541 /* keep past mtime, whack back atime */
1542 tp[0] = (tp[1] = (sbuf.st_mtime < now) ? sbuf.st_mtime : now) - 1;
1543 else now = 0; /* no time change needed */
1545 /* readonly with no recent messages */
1546 else if ((sbuf.st_atime < sbuf.st_mtime) ||
1547 (sbuf.st_atime < sbuf.st_ctime)) {
1548 tp[0] = now; /* set atime to now */
1549 /* set mtime to (now - 1) if necessary */
1550 tp[1] = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;
1552 else now = 0; /* no time change needed */
1553 /* set the times, note change */
1554 if (now && !utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
1556 flock (fd,LOCK_UN); /* release flock'ers */
1557 if (!stream) close (fd); /* close the file if no stream */
1558 dotlock_unlock (lock); /* flush the lock file if any */
1561 /* MMDF mail parse and lock mailbox
1562 * Accepts: MAIL stream
1563 * space to write lock file name
1564 * type of locking operation
1565 * Returns: T if parse OK, critical & mailbox is locked shared; NIL if failure
1568 int mmdf_parse (MAILSTREAM *stream,DOTLOCK *lock,int op)
1570 int ti,zn,m;
1571 unsigned long i,j,k;
1572 unsigned char c,*s,*t,*u,tmp[MAILTMPLEN],date[30];
1573 int retain = T;
1574 unsigned long nmsgs = stream->nmsgs;
1575 unsigned long prevuid = nmsgs ? mail_elt (stream,nmsgs)->private.uid : 0;
1576 unsigned long recent = stream->recent;
1577 unsigned long oldnmsgs = stream->nmsgs;
1578 short silent = stream->silent;
1579 short pseudoseen = NIL;
1580 struct stat sbuf;
1581 STRING bs;
1582 FDDATA d;
1583 MESSAGECACHE *elt;
1584 mail_lock (stream); /* guard against recursion or pingers */
1585 /* toss out previous descriptor */
1586 if (LOCAL->fd >= 0) close (LOCAL->fd);
1587 MM_CRITICAL (stream); /* open and lock mailbox (shared OK) */
1588 if ((LOCAL->fd = mmdf_lock (stream->mailbox,(LOCAL->ld >= 0) ?
1589 O_RDWR : O_RDONLY,
1590 (long)mail_parameters(NIL,GET_MBXPROTECTION,NIL),
1591 lock,op)) < 0) {
1592 sprintf (tmp,"Mailbox open failed, aborted: %s",strerror (errno));
1593 MM_LOG (tmp,ERROR);
1594 mmdf_abort (stream);
1595 mail_unlock (stream);
1596 MM_NOCRITICAL (stream); /* done with critical */
1597 return NIL;
1599 fstat (LOCAL->fd,&sbuf); /* get status */
1600 /* validate change in size */
1601 if (sbuf.st_size < LOCAL->filesize) {
1602 sprintf (tmp,"Mailbox shrank from %lu to %lu bytes, aborted",
1603 (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size);
1604 MM_LOG (tmp,ERROR); /* this is pretty bad */
1605 mmdf_unlock (LOCAL->fd,stream,lock);
1606 mmdf_abort (stream);
1607 mail_unlock (stream);
1608 MM_NOCRITICAL (stream); /* done with critical */
1609 return NIL;
1612 /* new data? */
1613 else if ((i = sbuf.st_size - LOCAL->filesize) != 0L) {
1614 d.fd = LOCAL->fd; /* yes, set up file descriptor */
1615 d.pos = LOCAL->filesize; /* get to that position in the file */
1616 d.chunk = LOCAL->buf; /* initial buffer chunk */
1617 d.chunksize = CHUNKSIZE; /* file chunk size */
1618 INIT (&bs,fd_string,&d,i); /* initialize stringstruct */
1619 /* skip leading whitespace for broken MTAs */
1620 while (((c = CHR (&bs)) == '\n') || (c == '\r') ||
1621 (c == ' ') || (c == '\t')) SNX (&bs);
1622 if (SIZE (&bs)) { /* read new data */
1623 /* remember internal header position */
1624 j = LOCAL->filesize + GETPOS (&bs);
1625 s = mmdf_mbxline (stream,&bs,&i);
1626 stream->silent = T; /* quell main program new message events */
1627 do { /* read MMDF header */
1628 if (!(i && ISMMDF (s))){/* see if valid MMDF header */
1629 sprintf (tmp,"Unexpected changes to mailbox (try restarting): %.20s",
1630 (char *) s);
1631 /* see if we can back up to a line */
1632 if (i && (j > MMDFHDRLEN)) {
1633 SETPOS (&bs,j -= MMDFHDRLEN);
1634 /* read previous line */
1635 s = mmdf_mbxline (stream,&bs,&i);
1636 /* kill the error if it looks good */
1637 if (i && ISMMDF (s)) tmp[0] = '\0';
1639 if (tmp[0]) {
1640 MM_LOG (tmp,ERROR);
1641 mmdf_unlock (LOCAL->fd,stream,lock);
1642 mmdf_abort (stream);
1643 mail_unlock (stream);
1644 MM_NOCRITICAL (stream);
1645 return NIL;
1648 /* instantiate first new message */
1649 mail_exists (stream,++nmsgs);
1650 (elt = mail_elt (stream,nmsgs))->valid = T;
1651 recent++; /* assume recent by default */
1652 elt->recent = T;
1653 /* note position/size of internal header */
1654 elt->private.special.offset = j;
1655 elt->private.special.text.size = i;
1657 s = mmdf_mbxline (stream,&bs,&i);
1658 ti = 0; /* assume not a valid date */
1659 zn = 0,t = NIL;
1660 if (i) VALID (s,t,ti,zn);
1661 if (ti) { /* generate plausible IMAPish date string */
1662 /* this is also part of header */
1663 elt->private.special.text.size += i;
1664 date[2] = date[6] = date[20] = '-'; date[11] = ' ';
1665 date[14] = date[17] = ':';
1666 /* dd */
1667 date[0] = t[ti - 2]; date[1] = t[ti - 1];
1668 /* mmm */
1669 date[3] = t[ti - 6]; date[4] = t[ti - 5]; date[5] = t[ti - 4];
1670 /* hh */
1671 date[12] = t[ti + 1]; date[13] = t[ti + 2];
1672 /* mm */
1673 date[15] = t[ti + 4]; date[16] = t[ti + 5];
1674 if (t[ti += 6]==':'){ /* ss */
1675 date[18] = t[++ti]; date[19] = t[++ti];
1676 ti++; /* move to space */
1678 else date[18] = date[19] = '0';
1679 /* yy -- advance over timezone if necessary */
1680 if (zn == ti) ti += (((t[zn+1] == '+') || (t[zn+1] == '-')) ? 6 : 4);
1681 date[7] = t[ti + 1]; date[8] = t[ti + 2];
1682 date[9] = t[ti + 3]; date[10] = t[ti + 4];
1683 /* zzz */
1684 t = zn ? (t + zn + 1) : (unsigned char *) "LCL";
1685 date[21] = *t++; date[22] = *t++; date[23] = *t++;
1686 if ((date[21] != '+') && (date[21] != '-')) date[24] = '\0';
1687 else { /* numeric time zone */
1688 date[24] = *t++; date[25] = *t++;
1689 date[26] = '\0'; date[20] = ' ';
1691 /* set internal date */
1692 if (!mail_parse_date (elt,date)) {
1693 sprintf (tmp,"Unable to parse internal date: %s",(char *) date);
1694 MM_LOG (tmp,WARN);
1697 else { /* make date from file date */
1698 struct tm *tm = gmtime (&sbuf.st_mtime);
1699 elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
1700 elt->year = tm->tm_year + 1900 - BASEYEAR;
1701 elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
1702 elt->seconds = tm->tm_sec;
1703 elt->zhours = 0; elt->zminutes = 0;
1704 t = NIL; /* suppress line read */
1706 /* header starts here */
1707 elt->private.msg.header.offset = elt->private.special.text.size;
1709 do { /* look for message body */
1710 j = GETPOS (&bs); /* note position before line */
1711 if (t) s = t = mmdf_mbxline (stream,&bs,&i);
1712 else t = s; /* this line read was suppressed */
1713 if (ISMMDF (s)) { /* found terminator in header? */
1714 SETPOS (&bs,j); /* oops, back up before line */
1715 /* must insert a newline */
1716 elt->private.spare.data++;
1717 break; /* punt */
1719 /* this line is part of header */
1720 elt->private.msg.header.text.size += i;
1721 if (i) switch (*s) { /* check header lines */
1722 case 'X': /* possible X-???: line */
1723 if (s[1] == '-') { /* must be immediately followed by hyphen */
1724 /* X-Status: becomes Status: in S case */
1725 if (s[2] == 'S' && s[3] == 't' && s[4] == 'a' && s[5] == 't' &&
1726 s[6] == 'u' && s[7] == 's' && s[8] == ':') s += 2;
1727 /* possible X-Keywords */
1728 else if (s[2] == 'K' && s[3] == 'e' && s[4] == 'y' &&
1729 s[5] == 'w' && s[6] == 'o' && s[7] == 'r' &&
1730 s[8] == 'd' && s[9] == 's' && s[10] == ':') {
1731 SIZEDTEXT uf;
1732 retain = NIL; /* don't retain continuation */
1733 s += 11; /* flush leading whitespace */
1734 while (*s && (*s != '\n') && ((*s != '\r') || (s[1] != '\n'))){
1735 while (*s == ' ') s++;
1736 /* find end of keyword */
1737 if (!(u = strpbrk (s," \n\r"))) u = s + strlen (s);
1738 /* got a keyword? */
1739 if ((k = (u - s)) && (k <= MAXUSERFLAG)) {
1740 uf.data = (unsigned char *) s;
1741 uf.size = k;
1742 for (j = 0; (j < NUSERFLAGS) && stream->user_flags[j]; ++j)
1743 if (!compare_csizedtext (stream->user_flags[j],&uf)) {
1744 elt->user_flags |= ((long) 1) << j;
1745 break;
1748 s = u; /* advance to next keyword */
1750 break;
1753 /* possible X-IMAP */
1754 else if ((s[2] == 'I') && (s[3] == 'M') && (s[4] == 'A') &&
1755 (s[5] == 'P') && ((m = (s[6] == ':')) ||
1756 ((s[6] == 'b') && (s[7] == 'a') &&
1757 (s[8] == 's') && (s[9] == 'e') &&
1758 (s[10] == ':')))) {
1759 retain = NIL; /* don't retain continuation */
1760 if ((nmsgs == 1) && !stream->uid_validity) {
1761 /* advance to data */
1762 s += m ? 7 : 11;
1763 /* flush whitespace */
1764 while (*s == ' ') s++;
1765 j = 0; /* slurp UID validity */
1766 /* found a digit? */
1767 while (isdigit (*s)) {
1768 j *= 10; /* yes, add it in */
1769 j += *s++ - '0';
1771 /* flush whitespace */
1772 while (*s == ' ') s++;
1773 /* must have valid UID validity and UID last */
1774 if (j && isdigit (*s)) {
1775 /* pseudo-header seen if X-IMAP */
1776 if (m) pseudoseen = LOCAL->pseudo = T;
1777 /* save UID validity */
1778 stream->uid_validity = j;
1779 j = 0; /* slurp UID last */
1780 while (isdigit (*s)) {
1781 j *= 10; /* yes, add it in */
1782 j += *s++ - '0';
1784 /* save UID last */
1785 stream->uid_last = j;
1786 /* process keywords */
1787 for (j = 0; (*s != '\n') && ((*s != '\r')||(s[1] != '\n'));
1788 s = u,j++) {
1789 /* flush leading whitespace */
1790 while (*s == ' ') s++;
1791 u = strpbrk (s," \n\r");
1792 /* got a keyword? */
1793 if ((j < NUSERFLAGS) && (k = (u - s)) &&
1794 (k <= MAXUSERFLAG)) {
1795 if (stream->user_flags[j])
1796 fs_give ((void **) &stream->user_flags[j]);
1797 stream->user_flags[j] = (char *) fs_get (k + 1);
1798 strncpy (stream->user_flags[j],s,k);
1799 stream->user_flags[j][k] = '\0';
1804 break;
1807 /* possible X-UID */
1808 else if (s[2] == 'U' && s[3] == 'I' && s[4] == 'D' &&
1809 s[5] == ':') {
1810 retain = NIL; /* don't retain continuation */
1811 /* only believe if have a UID validity */
1812 if (stream->uid_validity && ((nmsgs > 1) || !pseudoseen)) {
1813 s += 6; /* advance to UID value */
1814 /* flush whitespace */
1815 while (*s == ' ') s++;
1816 j = 0;
1817 /* found a digit? */
1818 while (isdigit (*s)) {
1819 j *= 10; /* yes, add it in */
1820 j += *s++ - '0';
1822 /* flush remainder of line */
1823 while (*s != '\n') s++;
1824 /* make sure not duplicated */
1825 if (elt->private.uid)
1826 sprintf (tmp,"Message %lu UID %lu already has UID %lu",
1827 pseudoseen ? elt->msgno - 1 : elt->msgno,
1828 j,elt->private.uid);
1829 /* make sure UID doesn't go backwards */
1830 else if (j <= prevuid)
1831 sprintf (tmp,"Message %lu UID %lu less than %lu",
1832 pseudoseen ? elt->msgno - 1 : elt->msgno,
1833 j,prevuid + 1);
1834 #if 0 /* this is currently broken by UIDPLUS */
1835 /* or skip by mailbox's recorded last */
1836 else if (j > stream->uid_last)
1837 sprintf (tmp,"Message %lu UID %lu greater than last %lu",
1838 pseudoseen ? elt->msgno - 1 : elt->msgno,
1839 j,stream->uid_last);
1840 #endif
1841 else { /* normal UID case */
1842 prevuid = elt->private.uid = j;
1843 #if 1 /* temporary kludge for UIDPLUS */
1844 if (prevuid > stream->uid_last) {
1845 stream->uid_last = prevuid;
1846 LOCAL->ddirty = LOCAL->dirty = T;
1848 #endif
1849 break; /* exit this cruft */
1851 MM_LOG (tmp,WARN);
1852 /* invalidate UID validity */
1853 stream->uid_validity = 0;
1854 elt->private.uid = 0;
1856 break;
1859 /* otherwise fall into S case */
1861 case 'S': /* possible Status: line */
1862 if (s[0] == 'S' && s[1] == 't' && s[2] == 'a' && s[3] == 't' &&
1863 s[4] == 'u' && s[5] == 's' && s[6] == ':') {
1864 retain = NIL; /* don't retain continuation */
1865 s += 6; /* advance to status flags */
1866 do switch (*s++) {/* parse flags */
1867 case 'R': /* message read */
1868 elt->seen = T;
1869 break;
1870 case 'O': /* message old */
1871 if (elt->recent) {
1872 elt->recent = NIL;
1873 recent--; /* it really wasn't recent */
1875 break;
1876 case 'D': /* message deleted */
1877 elt->deleted = T;
1878 break;
1879 case 'F': /* message flagged */
1880 elt->flagged = T;
1881 break;
1882 case 'A': /* message answered */
1883 elt->answered = T;
1884 break;
1885 case 'T': /* message is a draft */
1886 elt->draft = T;
1887 break;
1888 default: /* some other crap */
1889 break;
1890 } while (*s && (*s != '\n') && ((*s != '\r') || (s[1] != '\n')));
1891 break; /* all done */
1893 /* otherwise fall into default case */
1895 default: /* ordinary header line */
1896 if ((*s == 'S') || (*s == 's') ||
1897 (((*s == 'X') || (*s == 'x')) && (s[1] == '-'))) {
1898 unsigned char *e,*v;
1899 /* must match what mail_filter() does */
1900 for (u = s,v = tmp,e = u + min (i,MAILTMPLEN - 1);
1901 (u < e) && ((c = (*u ? *u : (*u = ' '))) != ':') &&
1902 ((c > ' ') || ((c != ' ') && (c != '\t') &&
1903 (c != '\r') && (c != '\n')));
1904 *v++ = *u++);
1905 *v = '\0'; /* tie off */
1906 /* matches internal header? */
1907 if (!compare_cstring (tmp,"STATUS") ||
1908 !compare_cstring (tmp,"X-STATUS") ||
1909 !compare_cstring (tmp,"X-KEYWORDS") ||
1910 !compare_cstring (tmp,"X-UID") ||
1911 !compare_cstring (tmp,"X-IMAP") ||
1912 !compare_cstring (tmp,"X-IMAPBASE")) {
1913 char err[MAILTMPLEN];
1914 sprintf (err,"Discarding bogus %s header in message %lu",
1915 (char *) tmp,elt->msgno);
1916 MM_LOG (err,WARN);
1917 retain = NIL; /* don't retain continuation */
1918 break; /* different case or something */
1921 /* retain or non-continuation? */
1922 if (retain || ((*s != ' ') && (*s != '\t'))) {
1923 retain = T; /* retaining continuation now */
1924 /* line length in LF format newline */
1925 for (j = k = 0; j < i; ++j) if (s[j] != '\r') ++k;
1926 /* "internal" header size */
1927 elt->private.spare.data += k;
1928 /* message size */
1929 elt->rfc822_size += k + 1;
1931 else {
1932 char err[MAILTMPLEN];
1933 sprintf (err,"Discarding bogus continuation in msg %lu: %.80s",
1934 elt->msgno,(char *) s);
1935 if ((u = strpbrk (err,"\r\n")) != NULL) *u = '\0';
1936 MM_LOG (err,WARN);
1937 break; /* different case or something */
1939 break;
1941 } while (i && (*t != '\n') && ((*t != '\r') || (t[1] != '\n')));
1942 /* "internal" header sans trailing newline */
1943 if (i) elt->private.spare.data--;
1944 /* assign a UID if none found */
1945 if (((nmsgs > 1) || !pseudoseen) && !elt->private.uid) {
1946 prevuid = elt->private.uid = ++stream->uid_last;
1947 elt->private.dirty = T;
1949 else elt->private.dirty = elt->recent;
1951 /* note size of header, location of text */
1952 elt->private.msg.header.text.size =
1953 (elt->private.msg.text.offset =
1954 (LOCAL->filesize + GETPOS (&bs)) - elt->private.special.offset) -
1955 elt->private.special.text.size;
1956 /* note current position */
1957 j = LOCAL->filesize + GETPOS (&bs);
1958 if (i) do { /* look for next message */
1959 s = mmdf_mbxline (stream,&bs,&i);
1960 if (i) { /* got new data? */
1961 if (ISMMDF (s)) break;
1962 else { /* not a header line, add it to message */
1963 elt->rfc822_size += i;
1964 for (j = 0; j < i; ++j) switch (s[j]) {
1965 case '\r': /* squeeze out CRs */
1966 elt->rfc822_size -= 1;
1967 break;
1968 case '\n': /* LF becomes CRLF */
1969 elt->rfc822_size += 1;
1970 break;
1971 default:
1972 break;
1974 /* update current position */
1975 j = LOCAL->filesize + GETPOS (&bs);
1978 } while (i); /* until found a header */
1979 elt->private.msg.text.text.size = j -
1980 (elt->private.special.offset + elt->private.msg.text.offset);
1981 if (i) { /* get next header line */
1982 /* remember first internal header position */
1983 j = LOCAL->filesize + GETPOS (&bs);
1984 s = mmdf_mbxline (stream,&bs,&i);
1986 /* until end of buffer */
1987 } while (!stream->sniff && i);
1988 if (pseudoseen) { /* flush pseudo-message if present */
1989 /* decrement recent count */
1990 if (mail_elt (stream,1)->recent) recent--;
1991 /* and the exists count */
1992 mail_exists (stream,nmsgs--);
1993 mail_expunged(stream,1);/* fake an expunge of that message */
1995 /* need to start a new UID validity? */
1996 if (!stream->uid_validity) {
1997 stream->uid_validity = time (0);
1998 /* in case a whiner with no life */
1999 if (mail_parameters (NIL,GET_USERHASNOLIFE,NIL))
2000 stream->uid_nosticky = T;
2001 else if (nmsgs) { /* don't bother if empty file */
2002 /* make dirty to restart UID epoch */
2003 LOCAL->ddirty = LOCAL->dirty = T;
2004 /* need to rewrite msg 1 if not pseudo */
2005 if (!LOCAL->pseudo) mail_elt (stream,1)->private.dirty = T;
2006 MM_LOG ("Assigning new unique identifiers to all messages",NIL);
2009 stream->nmsgs = oldnmsgs; /* whack it back down */
2010 stream->silent = silent; /* restore old silent setting */
2011 /* notify upper level of new mailbox sizes */
2012 mail_exists (stream,nmsgs);
2013 mail_recent (stream,recent);
2014 /* mark dirty so O flags are set */
2015 if (recent) LOCAL->dirty = T;
2018 /* no change, don't babble if never got time */
2019 else if (LOCAL->filetime && LOCAL->filetime != sbuf.st_mtime)
2020 MM_LOG ("New mailbox modification time but apparently no changes",WARN);
2021 /* update parsed file size and time */
2022 LOCAL->filesize = sbuf.st_size;
2023 LOCAL->filetime = sbuf.st_mtime;
2024 return T; /* return the winnage */
2027 /* MMDF read line from mailbox
2028 * Accepts: mail stream
2029 * stringstruct
2030 * pointer to line size
2031 * Returns: pointer to input line
2034 char *mmdf_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size)
2036 unsigned long i,j,k,m;
2037 char *s,*t,*te;
2038 char *ret = "";
2039 /* flush old buffer */
2040 if (LOCAL->line) fs_give ((void **) &LOCAL->line);
2041 /* if buffer needs refreshing */
2042 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
2043 if (SIZE (bs)) { /* find newline */
2044 /* end of fast scan */
2045 te = (t = (s = bs->curpos) + bs->cursize) - 12;
2046 while (s < te) if ((*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
2047 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
2048 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
2049 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n')) {
2050 --s; /* back up */
2051 break; /* exit loop */
2053 /* final character-at-a-time scan */
2054 while ((s < t) && (*s != '\n')) ++s;
2055 /* difficult case if line spans buffer */
2056 if ((i = s - bs->curpos) == bs->cursize) {
2057 /* have space in line buffer? */
2058 if (i > LOCAL->linebuflen) {
2059 fs_give ((void **) &LOCAL->linebuf);
2060 LOCAL->linebuf = (char *) fs_get (LOCAL->linebuflen = i);
2062 /* remember what we have so far */
2063 memcpy (LOCAL->linebuf,bs->curpos,i);
2064 /* load next buffer */
2065 SETPOS (bs,k = GETPOS (bs) + i);
2066 /* end of fast scan */
2067 te = (t = (s = bs->curpos) + bs->cursize) - 12;
2068 /* fast scan in overlap buffer */
2069 while (s < te) if ((*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
2070 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
2071 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
2072 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n')) {
2073 --s; /* back up */
2074 break; /* exit loop */
2077 /* final character-at-a-time scan */
2078 while ((s < t) && (*s != '\n')) ++s;
2079 /* huge line? */
2080 if ((j = s - bs->curpos) == bs->cursize) {
2081 SETPOS (bs,GETPOS (bs) + j);
2082 /* look for end of line (s-l-o-w!!) */
2083 for (m = SIZE (bs); m && (SNX (bs) != '\n'); --m,++j);
2084 SETPOS (bs,k); /* go back to where it started */
2086 /* got size of data, make buffer for return */
2087 ret = LOCAL->line = (char *) fs_get (i + j + 2);
2088 /* copy first chunk */
2089 memcpy (ret,LOCAL->linebuf,i);
2090 while (j) { /* copy remainder */
2091 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
2092 memcpy (ret + i,bs->curpos,k = min (j,bs->cursize));
2093 i += k; /* account for this much read in */
2094 j -= k;
2095 bs->curpos += k; /* increment new position */
2096 bs->cursize -= k; /* eat that many bytes */
2098 /* read newline at end */
2099 if (SIZE (bs)) ret[i++] = SNX (bs);
2100 ret[i] = '\0'; /* makes debugging easier */
2102 else { /* this is easy */
2103 ret = bs->curpos; /* string it at this position */
2104 bs->curpos += ++i; /* increment new position */
2105 bs->cursize -= i; /* eat that many bytes */
2107 *size = i; /* return that to user */
2109 else *size = 0; /* end of data, return empty */
2110 /* embedded MMDF header at end of line? */
2111 if ((*size > sizeof (MMDFHDRTXT)) &&
2112 (s = ret + *size - (i = sizeof (MMDFHDRTXT) - 1)) && ISMMDF (s)) {
2113 SETPOS (bs,GETPOS (bs) - i);/* back up to start of MMDF header */
2114 *size -= i; /* reduce length of line */
2115 ret[*size - 1] = '\n'; /* force newline at end */
2117 return ret;
2120 /* MMDF make pseudo-header
2121 * Accepts: MAIL stream
2122 * buffer to write pseudo-header
2123 * Returns: length of pseudo-header
2126 unsigned long mmdf_pseudo (MAILSTREAM *stream,char *hdr)
2128 int i;
2129 char *s,tmp[MAILTMPLEN];
2130 time_t now = time (0);
2131 rfc822_fixed_date (tmp);
2132 sprintf (hdr,"%sFrom %s %.24s\nDate: %s\nFrom: %s <%s@%.80s>\nSubject: %s\nMessage-ID: <%lu@%.80s>\nX-IMAP: %010lu %010lu",
2133 mmdfhdr,pseudo_from,ctime (&now),
2134 tmp,pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,
2135 (unsigned long) now,mylocalhost (),stream->uid_validity,
2136 stream->uid_last);
2137 for (s = hdr + strlen (hdr),i = 0; i < NUSERFLAGS; ++i)
2138 if (stream->user_flags[i])
2139 sprintf (s += strlen (s)," %s",stream->user_flags[i]);
2140 sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n%s",pseudo_msg,mmdfhdr);
2141 return strlen (hdr);
2144 /* MMDF make status string
2145 * Accepts: MAIL stream
2146 * destination string to write
2147 * message cache entry
2148 * UID to write if non-zero (else use elt->private.uid)
2149 * non-zero flag to write UID (.LT. 0 to write UID base info too)
2150 * Returns: length of string
2153 unsigned long mmdf_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
2154 unsigned long uid,long flag)
2156 char *t,stack[64];
2157 char *s = status;
2158 unsigned long n;
2159 int pad = 50;
2160 int sticky = uid ? T : !stream->uid_nosticky;
2161 /* This used to use sprintf(), but thanks to certain cretinous C libraries
2162 with horribly slow implementations of sprintf() I had to change it to this
2163 mess. At least it should be fast. */
2164 if ((flag < 0) && sticky) { /* need to write X-IMAPbase: header? */
2165 *s++ = 'X'; *s++ = '-'; *s++ = 'I'; *s++ = 'M'; *s++ = 'A'; *s++ = 'P';
2166 *s++ = 'b'; *s++ = 'a'; *s++ = 's'; *s++ = 'e'; *s++ = ':'; *s++ = ' ';
2167 t = stack;
2168 n = stream->uid_validity; /* push UID validity digits on the stack */
2169 do *t++ = (char) (n % 10) + '0';
2170 while (n /= 10);
2171 /* pop UID validity digits from stack */
2172 while (t > stack) *s++ = *--t;
2173 *s++ = ' ';
2174 n = stream->uid_last; /* push UID last digits on the stack */
2175 do *t++ = (char) (n % 10) + '0';
2176 while (n /= 10);
2177 /* pop UID last digits from stack */
2178 while (t > stack) *s++ = *--t;
2179 for (n = 0; n < NUSERFLAGS; ++n) if ((t = stream->user_flags[n]) != NULL)
2180 for (*s++ = ' '; *t; *s++ = *t++);
2181 *s++ = '\n';
2182 pad += 30; /* increased padding if have IMAPbase */
2184 *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't'; *s++ = 'u'; *s++ = 's';
2185 *s++ = ':'; *s++ = ' ';
2186 if (elt->seen) *s++ = 'R';
2187 /* only write O if have a UID */
2188 if (flag && (!elt->recent || !LOCAL->appending)) *s++ = 'O';
2189 *s++ = '\n';
2190 *s++ = 'X'; *s++ = '-'; *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't';
2191 *s++ = 'u'; *s++ = 's'; *s++ = ':'; *s++ = ' ';
2192 if (elt->deleted) *s++ = 'D';
2193 if (elt->flagged) *s++ = 'F';
2194 if (elt->answered) *s++ = 'A';
2195 if (elt->draft) *s++ = 'T';
2196 *s++ = '\n';
2198 if (sticky) { /* only do this if UIDs sticky */
2199 *s++ = 'X'; *s++ = '-'; *s++ = 'K'; *s++ = 'e'; *s++ = 'y'; *s++ = 'w';
2200 *s++ = 'o'; *s++ = 'r'; *s++ = 'd'; *s++ = 's'; *s++ = ':';
2201 if ((n = elt->user_flags) != 0L) do {
2202 *s++ = ' ';
2203 for (t = stream->user_flags[find_rightmost_bit (&n)]; *t; *s++ = *t++);
2204 } while (n);
2205 n = s - status; /* get size of stuff so far */
2206 /* pad X-Keywords to make size constant */
2207 if (n < pad) for (n = pad - n; n > 0; --n) *s++ = ' ';
2208 *s++ = '\n';
2209 if (flag) { /* want to include UID? */
2210 t = stack;
2211 /* push UID digits on the stack */
2212 n = uid ? uid : elt->private.uid;
2213 do *t++ = (char) (n % 10) + '0';
2214 while (n /= 10);
2215 *s++ = 'X'; *s++ = '-'; *s++ = 'U'; *s++ = 'I'; *s++ = 'D'; *s++ = ':';
2216 *s++ = ' ';
2217 /* pop UID from stack */
2218 while (t > stack) *s++ = *--t;
2219 *s++ = '\n';
2222 *s++ = '\n'; *s = '\0'; /* end of extended message status */
2223 return s - status; /* return size of resulting string */
2226 /* Rewrite mailbox file
2227 * Accepts: MAIL stream, must be critical and locked
2228 * return pointer to number of expunged messages if want expunge
2229 * lock file name
2230 * expunge sequence, not deleted flag
2231 * Returns: T if success and mailbox unlocked, NIL if failure
2234 #define OVERFLOWBUFLEN 8192 /* initial overflow buffer length */
2236 long mmdf_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
2237 long flags)
2239 MESSAGECACHE *elt;
2240 MMDFFILE f;
2241 char *s;
2242 time_t tp[2];
2243 long ret,flag;
2244 unsigned long i,j;
2245 unsigned long recent = stream->recent;
2246 unsigned long size = LOCAL->pseudo ? mmdf_pseudo (stream,LOCAL->buf) : 0;
2247 if (nexp) *nexp = 0; /* initially nothing expunged */
2248 /* calculate size of mailbox after rewrite */
2249 for (i = 1,flag = LOCAL->pseudo ? 1 : -1; i <= stream->nmsgs; i++) {
2250 elt = mail_elt (stream,i); /* get cache */
2251 if (!(nexp && elt->deleted && (flags ? elt->sequence : T))) {
2252 /* add RFC822 size of this message */
2253 size += elt->private.special.text.size + elt->private.spare.data +
2254 mmdf_xstatus (stream,LOCAL->buf,elt,NIL,flag) +
2255 elt->private.msg.text.text.size + MMDFHDRLEN;
2256 flag = 1; /* only count X-IMAPbase once */
2259 /* no messages, has a life, and no pseudo */
2260 if (!size && !mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) {
2261 LOCAL->pseudo = T; /* so make a pseudo-message now */
2262 size = mmdf_pseudo (stream,LOCAL->buf);
2264 /* extend the file as necessary */
2265 if ((ret = mmdf_extend (stream,size)) != 0L){
2266 /* Set up buffered I/O file structure
2267 * curpos current position being written through buffering
2268 * filepos current position being written physically to the disk
2269 * bufpos current position being written in the buffer
2270 * protect current maximum position that can be written to the disk
2271 * before buffering is forced
2272 * The code tries to buffer so that that disk is written in multiples of
2273 * OVERBLOWBUFLEN bytes.
2275 f.stream = stream; /* note mail stream */
2276 f.curpos = f.filepos = 0; /* start of file */
2277 f.protect = stream->nmsgs ? /* initial protection pointer */
2278 mail_elt (stream,1)->private.special.offset : 8192;
2279 f.bufpos = f.buf = (char *) fs_get (f.buflen = OVERFLOWBUFLEN);
2281 if (LOCAL->pseudo) /* update pseudo-header */
2282 mmdf_write (&f,LOCAL->buf,mmdf_pseudo (stream,LOCAL->buf));
2283 /* loop through all messages */
2284 for (i = 1,flag = LOCAL->pseudo ? 1 : -1; i <= stream->nmsgs;) {
2285 elt = mail_elt (stream,i);/* get cache */
2286 /* expunge this message? */
2287 if (nexp && elt->deleted && (flags ? elt->sequence : T)) {
2288 /* one less recent message */
2289 if (elt->recent) --recent;
2290 mail_expunged(stream,i);/* notify upper levels */
2291 ++*nexp; /* count up one more expunged message */
2293 else { /* preserve this message */
2294 i++; /* advance to next message */
2295 if ((flag < 0) || /* need to rewrite message? */
2296 elt->private.dirty || (f.curpos != elt->private.special.offset) ||
2297 (elt->private.msg.header.text.size !=
2298 (elt->private.spare.data +
2299 mmdf_xstatus (stream,LOCAL->buf,elt,NIL,flag)))) {
2300 unsigned long newoffset = f.curpos;
2301 /* yes, seek to internal header */
2302 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
2303 read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
2304 /* see if need to squeeze out a CR */
2305 if (LOCAL->buf[elt->private.special.text.size - 2] == '\r') {
2306 LOCAL->buf[--elt->private.special.text.size - 1] = '\n';
2307 --size; /* squeezed out a CR from PC */
2309 /* protection pointer moves to RFC822 header */
2310 f.protect = elt->private.special.offset +
2311 elt->private.msg.header.offset;
2312 /* write internal header */
2313 mmdf_write (&f,LOCAL->buf,elt->private.special.text.size);
2314 /* get RFC822 header */
2315 s = mmdf_header (stream,elt->msgno,&j,FT_INTERNAL);
2316 /* in case this got decremented */
2317 elt->private.msg.header.offset = elt->private.special.text.size;
2318 /* header size, sans trailing newline */
2319 if ((j < 2) || (s[j - 2] == '\n')) j--;
2320 /* this can happen if CRs were squeezed */
2321 if (j < elt->private.spare.data) {
2322 /* so fix up counts */
2323 size -= elt->private.spare.data - j;
2324 elt->private.spare.data = j;
2326 else if (j != elt->private.spare.data)
2327 fatal ("header size inconsistent");
2328 /* protection pointer moves to RFC822 text */
2329 f.protect = elt->private.special.offset +
2330 elt->private.msg.text.offset;
2331 mmdf_write (&f,s,j); /* write RFC822 header */
2332 /* write status and UID */
2333 mmdf_write (&f,LOCAL->buf,
2334 j = mmdf_xstatus (stream,LOCAL->buf,elt,NIL,flag));
2335 flag = 1; /* only write X-IMAPbase once */
2336 /* new file header size */
2337 elt->private.msg.header.text.size = elt->private.spare.data + j;
2339 /* did text move? */
2340 if (f.curpos != f.protect) {
2341 /* get message text */
2342 s = mmdf_text_work (stream,elt,&j,FT_INTERNAL);
2343 /* this can happen if CRs were squeezed */
2344 if (j < elt->private.msg.text.text.size) {
2345 /* so fix up counts */
2346 size -= elt->private.msg.text.text.size - j;
2347 elt->private.msg.text.text.size = j;
2349 /* can't happen it says here */
2350 else if (j > elt->private.msg.text.text.size)
2351 fatal ("text size inconsistent");
2352 /* new text offset, status/UID may change it */
2353 elt->private.msg.text.offset = f.curpos - newoffset;
2354 /* protection pointer moves to next message */
2355 f.protect = (i <= stream->nmsgs) ?
2356 mail_elt (stream,i)->private.special.offset :
2357 (f.curpos + j + MMDFHDRLEN);
2358 mmdf_write (&f,s,j);/* write text */
2359 /* write trailing newline */
2360 mmdf_write (&f,mmdfhdr,MMDFHDRLEN);
2362 else { /* tie off header and status */
2363 mmdf_write (&f,NIL,NIL);
2364 f.curpos = f.protect =/* restart everything at end of message */
2365 f.filepos += elt->private.msg.text.text.size + MMDFHDRLEN;
2367 /* new internal header offset */
2368 elt->private.special.offset = newoffset;
2369 elt->private.dirty =NIL;/* message is now clean */
2371 else { /* no need to rewrite this message */
2372 /* tie off previous message if needed */
2373 mmdf_write (&f,NIL,NIL);
2374 f.curpos = f.protect =/* restart everything at end of message */
2375 f.filepos += elt->private.special.text.size +
2376 elt->private.msg.header.text.size +
2377 elt->private.msg.text.text.size + MMDFHDRLEN;
2382 mmdf_write (&f,NIL,NIL); /* tie off final message */
2383 if (size != f.filepos) fatal ("file size inconsistent");
2384 fs_give ((void **) &f.buf); /* free buffer */
2385 /* make sure tied off */
2386 ftruncate (LOCAL->fd,LOCAL->filesize = size);
2387 fsync (LOCAL->fd); /* make sure the updates take */
2388 if (size && (flag < 0)) fatal ("lost UID base information");
2389 /* no longer dirty */
2390 LOCAL->ddirty = LOCAL->dirty = NIL;
2391 /* notify upper level of new mailbox sizes */
2392 mail_exists (stream,stream->nmsgs);
2393 mail_recent (stream,recent);
2394 /* set atime to now, mtime a second earlier */
2395 tp[1] = (tp[0] = time (0)) - 1;
2396 /* set the times, note change */
2397 if (!utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
2398 close (LOCAL->fd); /* close and reopen file */
2399 if ((LOCAL->fd = open (stream->mailbox,O_RDWR,
2400 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL)))
2401 < 0) {
2402 sprintf (LOCAL->buf,"Mailbox open failed, aborted: %s",strerror (errno));
2403 MM_LOG (LOCAL->buf,ERROR);
2404 mmdf_abort (stream);
2406 dotlock_unlock (lock); /* flush the lock file */
2408 return ret; /* return state from algorithm */
2411 /* Extend MMDF mailbox file
2412 * Accepts: MAIL stream
2413 * new desired size
2414 * Return: T if success, else NIL
2417 long mmdf_extend (MAILSTREAM *stream,unsigned long size)
2419 unsigned long i = (size > LOCAL->filesize) ? size - LOCAL->filesize : 0;
2420 if (i) { /* does the mailbox need to grow? */
2421 if (i > LOCAL->buflen) { /* make sure have enough space */
2422 /* this user won the lottery all right */
2423 fs_give ((void **) &LOCAL->buf);
2424 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = i) + 1);
2426 memset (LOCAL->buf,'\0',i); /* get a block of nulls */
2427 while (T) { /* until write successful or punt */
2428 lseek (LOCAL->fd,LOCAL->filesize,L_SET);
2429 if ((write (LOCAL->fd,LOCAL->buf,i) >= 0) && !fsync (LOCAL->fd)) break;
2430 else {
2431 long e = errno; /* note error before doing ftruncate */
2432 ftruncate (LOCAL->fd,LOCAL->filesize);
2433 if (MM_DISKERROR (stream,e,NIL)) {
2434 fsync (LOCAL->fd); /* user chose to punt */
2435 sprintf (LOCAL->buf,"Unable to extend mailbox: %s",strerror (e));
2436 if (!stream->silent) MM_LOG (LOCAL->buf,ERROR);
2437 return NIL;
2442 return LONGT;
2445 /* Write data to buffered file
2446 * Accepts: buffered file pointer
2447 * file data or NIL to indicate "flush buffer"
2448 * date size (ignored for "flush buffer")
2449 * Does not return until success
2452 void mmdf_write (MMDFFILE *f,char *buf,unsigned long size)
2454 unsigned long i,j,k;
2455 if (buf) { /* doing buffered write? */
2456 i = f->bufpos - f->buf; /* yes, get size of current buffer data */
2457 /* yes, have space in current buffer chunk? */
2458 if ((j = i ? ((f->buflen - i) % OVERFLOWBUFLEN) : f->buflen) != 0L) {
2459 /* yes, fill up buffer as much as we can */
2460 memcpy (f->bufpos,buf,k = min (j,size));
2461 f->bufpos += k; /* new buffer position */
2462 f->curpos += k; /* new current position */
2463 if (j -= k) return; /* all done if still have buffer free space */
2464 buf += k; /* full, get new unwritten data pointer */
2465 size -= k; /* new data size */
2466 i += k; /* new buffer data size */
2468 /* This chunk of the buffer is full. See if can make some space by
2469 * writing to the disk, if there's enough unprotected space to do so.
2470 * Try to fill out any unaligned chunk, along with any subsequent full
2471 * chunks that will fit in unprotected space.
2473 /* any unprotected space we can write to? */
2474 if ((j = min (i,f->protect - f->filepos)) != 0L) {
2475 /* yes, filepos not at chunk boundary? */
2476 if ((k = f->filepos % OVERFLOWBUFLEN) && ((k = OVERFLOWBUFLEN - k) < j))
2477 j -= k; /* yes, and can write out partial chunk */
2478 else k = 0; /* no partial chunk to write */
2479 /* if at least a chunk free, write that too */
2480 if (j > OVERFLOWBUFLEN) k += j - (j % OVERFLOWBUFLEN);
2481 if (k) { /* write data if there is anything we can */
2482 mmdf_phys_write (f,f->buf,k);
2483 /* slide buffer */
2484 if (i -= k) memmove (f->buf,f->buf + k,i);
2485 f->bufpos = f->buf + i; /* new end of buffer */
2489 /* Have flushed the buffer as best as possible. All done if no more
2490 * data to write. Otherwise, if the buffer is empty AND if the unwritten
2491 * data is larger than a chunk AND the unprotected space is also larger
2492 * than a chunk, then write as many chunks as we can directly from the
2493 * data. Buffer the rest, expanding the buffer as needed.
2495 if (size) { /* have more data that we need to buffer? */
2496 /* can write any of it to disk instead? */
2497 if ((f->bufpos == f->buf) &&
2498 ((j = min (f->protect - f->filepos,size)) > OVERFLOWBUFLEN)) {
2499 /* write as much as we can right now */
2500 mmdf_phys_write (f,buf,j -= (j % OVERFLOWBUFLEN));
2501 buf += j; /* new data pointer */
2502 size -= j; /* new data size */
2503 f->curpos += j; /* advance current pointer */
2505 if (size) { /* still have data that we need to buffer? */
2506 /* yes, need to expand the buffer? */
2507 if ((i = ((f->bufpos + size) - f->buf)) > f->buflen) {
2508 /* note current position in buffer */
2509 j = f->bufpos - f->buf;
2510 i += OVERFLOWBUFLEN; /* yes, grow another chunk */
2511 fs_resize ((void **) &f->buf,f->buflen = i - (i % OVERFLOWBUFLEN));
2512 /* in case buffer relocated */
2513 f->bufpos = f->buf + j;
2515 /* buffer remaining data */
2516 memcpy (f->bufpos,buf,size);
2517 f->bufpos += size; /* new end of buffer */
2518 f->curpos += size; /* advance current pointer */
2522 else { /* flush buffer to disk */
2523 mmdf_phys_write (f,f->buf,i = f->bufpos - f->buf);
2524 f->bufpos = f->buf; /* reset buffer */
2525 /* update positions */
2526 f->curpos = f->protect = f->filepos;
2530 /* Physical disk write
2531 * Accepts: buffered file pointer
2532 * buffer address
2533 * buffer size
2534 * Does not return until success
2537 void mmdf_phys_write (MMDFFILE *f,char *buf,size_t size)
2539 MAILSTREAM *stream = f->stream;
2540 /* write data at desired position */
2541 while (size && ((lseek (LOCAL->fd,f->filepos,L_SET) < 0) ||
2542 (write (LOCAL->fd,buf,size) < 0))) {
2543 int e;
2544 char tmp[MAILTMPLEN];
2545 sprintf (tmp,"Unable to write to mailbox: %s",strerror (e = errno));
2546 MM_LOG (tmp,ERROR);
2547 MM_DISKERROR (NIL,e,T); /* serious problem, must retry */
2549 f->filepos += size; /* update file position */