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
19 * University of Washington
21 * Internet: MRC@Washington.EDU
23 * Date: 20 December 1989
24 * Last Edited: 27 March 2008
32 extern int errno
; /* just in case */
44 /* Supposedly, this page has everything the MMDF driver needs to know about
45 * the MMDF delimiter. By changing these macros, the MMDF driver should
46 * change with it. Note that if you change the length of MMDFHDRTXT you
47 * also need to change the ISMMDF and RETIFMMDFWRD macros to reflect the new
52 /* Useful MMDF constants */
54 #define MMDFCHR '\01' /* MMDF character */
55 #define MMDFCHRS 0x01010101 /* MMDF header character spread in a word */
56 /* MMDF header text */
57 #define MMDFHDRTXT "\01\01\01\01\n"
58 /* length of MMDF header text */
59 #define MMDFHDRLEN (sizeof (MMDFHDRTXT) - 1)
62 /* Validate MMDF header
63 * Accepts: pointer to candidate string to validate as an MMDF header
64 * Returns: T if valid; else NIL
68 ((*(s) == MMDFCHR) && ((s)[1] == MMDFCHR) && ((s)[2] == MMDFCHR) && \
69 ((s)[3] == MMDFCHR) && ((s)[4] == '\n'))
72 /* Return if a 32-bit word has the start of an MMDF header
73 * Accepts: pointer to word of four bytes to validate as an MMDF header
74 * Returns: pointer to MMDF header, else proceeds
77 #define RETIFMMDFWRD(s) { \
78 if (s[3] == MMDFCHR) { \
79 if ((s[4] == MMDFCHR) && (s[5] == MMDFCHR) && (s[6] == MMDFCHR) && \
80 (s[7] == '\n')) return s + 3; \
81 else if (s[2] == MMDFCHR) { \
82 if ((s[4] == MMDFCHR) && (s[5] == MMDFCHR) && (s[6] == '\n')) \
84 else if (s[1] == MMDFCHR) { \
85 if ((s[4] == MMDFCHR) && (s[5] == '\n')) return s + 1; \
86 else if ((*s == MMDFCHR) && (s[4] == '\n')) return s; \
93 * Accepts: pointer to candidate string to validate as a From header
94 * return pointer to end of date/time field
95 * return pointer to offset from t of time (hours of ``mmm dd hh:mm'')
96 * return pointer to offset from t of time zone (if non-zero)
97 * Returns: t,ti,zn set if valid From string, else ti is NIL
100 #define VALID(s,x,ti,zn) { \
102 if ((*s == 'F') && (s[1] == 'r') && (s[2] == 'o') && (s[3] == 'm') && \
104 for (x = s + 5; *x && *x != '\n'; x++); \
107 for (zn = -1; x[zn] != ' '; zn--); \
108 if ((x[zn-1] == 'm') && (x[zn-2] == 'o') && (x[zn-3] == 'r') && \
109 (x[zn-4] == 'f') && (x[zn-5] == ' ') && (x[zn-6] == 'e') && \
110 (x[zn-7] == 't') && (x[zn-8] == 'o') && (x[zn-9] == 'm') && \
111 (x[zn-10] == 'e') && (x[zn-11] == 'r') && (x[zn-12] == ' '))\
115 if (x[-5] == ' ') { \
116 if (x[-8] == ':') zn = 0,ti = -5; \
117 else if (x[-9] == ' ') ti = zn = -9; \
118 else if ((x[-11] == ' ') && ((x[-10]=='+') || (x[-10]=='-'))) \
121 else if (x[-4] == ' ') { \
122 if (x[-9] == ' ') zn = -4,ti = -9; \
124 else if (x[-6] == ' ') { \
125 if ((x[-11] == ' ') && ((x[-5] == '+') || (x[-5] == '-'))) \
128 if (ti && !((x[ti - 3] == ':') && \
129 (x[ti -= ((x[ti - 6] == ':') ? 9 : 6)] == ' ') && \
130 (x[ti - 3] == ' ') && (x[ti - 7] == ' ') && \
131 (x[ti - 11] == ' '))) ti = 0; \
137 /* You are not expected to understand this macro, but read the next page if
138 * you are not faint of heart.
140 * Known formats to the VALID macro are:
141 * From user Wed Dec 2 05:53 1992
142 * BSD From user Wed Dec 2 05:53:22 1992
143 * SysV From user Wed Dec 2 05:53 PST 1992
144 * rn From user Wed Dec 2 05:53:22 PST 1992
145 * From user Wed Dec 2 05:53 -0700 1992
146 * emacs From user Wed Dec 2 05:53:22 -0700 1992
147 * From user Wed Dec 2 05:53 1992 PST
148 * From user Wed Dec 2 05:53:22 1992 PST
149 * From user Wed Dec 2 05:53 1992 -0700
150 * Solaris From user Wed Dec 2 05:53:22 1992 -0700
152 * Plus all of the above with `` remote from xxx'' after it. Thank you very
153 * much, smail and Solaris, for making my life considerably more complicated.
157 * What? You want to understand the VALID macro anyway? Alright, since you
158 * insist. Actually, it isn't really all that difficult, provided that you
159 * take it step by step.
161 * Line 1 Initializes the return ti value to failure (0);
162 * Lines 2-3 Validates that the 1st-5th characters are ``From ''.
163 * Lines 4-5 Validates that there is an end of line and points x at it.
164 * Lines 6-13 First checks to see if the line is at least 41 characters long.
165 * If so, it scans backwards to find the rightmost space. From
166 * that point, it scans backwards to see if the string matches
167 * `` remote from''. If so, it sets x to point to the space at
168 * the start of the string.
169 * Line 14 Makes sure that there are at least 27 characters in the line.
170 * Lines 15-20 Checks if the date/time ends with the year (there is a space
171 * five characters back). If there is a colon three characters
172 * further back, there is no timezone field, so zn is set to 0
173 * and ti is set in front of the year. Otherwise, there must
174 * either to be a space four characters back for a three-letter
175 * timezone, or a space six characters back followed by a + or -
176 * for a numeric timezone; in either case, zn and ti become the
177 * offset of the space immediately before it.
178 * Lines 21-23 Are the failure case for line 14. If there is a space four
179 * characters back, it is a three-letter timezone; there must be a
180 * space for the year nine characters back. zn is the zone
181 * offset; ti is the offset of the space.
182 * Lines 24-27 Are the failure case for line 20. If there is a space six
183 * characters back, it is a numeric timezone; there must be a
184 * space eleven characters back and a + or - five characters back.
185 * zn is the zone offset; ti is the offset of the space.
186 * Line 28-31 If ti is valid, make sure that the string before ti is of the
187 * form www mmm dd hh:mm or www mmm dd hh:mm:ss, otherwise
188 * invalidate ti. There must be a colon three characters back
189 * and a space six or nine characters back (depending upon
190 * whether or not the character six characters back is a colon).
191 * There must be a space three characters further back (in front
192 * of the day), one seven characters back (in front of the month),
193 * and one eleven characters back (in front of the day of week).
194 * ti is set to be the offset of the space before the time.
196 * Why a macro? It gets invoked a *lot* in a tight loop. On some of the
197 * newer pipelined machines it is faster being open-coded than it would be if
198 * subroutines are called.
200 * Why does it scan backwards from the end of the line, instead of doing the
201 * much easier forward scan? There is no deterministic way to parse the
202 * ``user'' field, because it may contain unquoted spaces! Yes, I tested it to
203 * see if unquoted spaces were possible. They are, and I've encountered enough
204 * evil mail to be totally unwilling to trust that ``it will never happen''.
207 /* Build parameters */
209 #define KODRETRY 15 /* kiss-of-death retry in seconds */
210 #define LOCKTIMEOUT 5 /* lock timeout in minutes */
213 /* MMDF I/O stream local data */
215 typedef struct mmdf_local
{
216 unsigned int dirty
: 1; /* disk copy needs updating */
217 unsigned int ddirty
: 1; /* double-dirty, ping becomes checkpoint */
218 unsigned int pseudo
: 1; /* uses a pseudo message */
219 unsigned int appending
: 1; /* don't mark new messages as old */
220 int fd
; /* mailbox file descriptor */
221 int ld
; /* lock file descriptor */
222 char *lname
; /* lock file name */
223 off_t filesize
; /* file size parsed */
224 time_t filetime
; /* last file time */
225 unsigned char *buf
; /* temporary buffer */
226 unsigned long buflen
; /* current size of temporary buffer */
227 unsigned long uid
; /* current text uid */
228 SIZEDTEXT text
; /* current text */
229 unsigned long textlen
; /* current text length */
230 char *line
; /* returned line */
231 char *linebuf
; /* line readin buffer */
232 unsigned long linebuflen
; /* current line readin buffer length */
236 /* Convenient access to local data */
238 #define LOCAL ((MMDFLOCAL *) stream->local)
241 /* MMDF protected file structure */
243 typedef struct mmdf_file
{
244 MAILSTREAM
*stream
; /* current stream */
245 off_t curpos
; /* current file position */
246 off_t protect
; /* protected position */
247 off_t filepos
; /* current last written file position */
248 char *buf
; /* overflow buffer */
249 size_t buflen
; /* current overflow buffer length */
250 char *bufpos
; /* current buffer position */
253 /* Function prototypes */
255 DRIVER
*mmdf_valid (char *name
);
256 long mmdf_isvalid (char *name
,char *tmp
);
257 long mmdf_isvalid_fd (int fd
,char *tmp
);
258 void *mmdf_parameters (long function
,void *value
);
259 void mmdf_scan (MAILSTREAM
*stream
,char *ref
,char *pat
,char *contents
);
260 void mmdf_list (MAILSTREAM
*stream
,char *ref
,char *pat
);
261 void mmdf_lsub (MAILSTREAM
*stream
,char *ref
,char *pat
);
262 long mmdf_create (MAILSTREAM
*stream
,char *mailbox
);
263 long mmdf_delete (MAILSTREAM
*stream
,char *mailbox
);
264 long mmdf_rename (MAILSTREAM
*stream
,char *old
,char *newname
);
265 MAILSTREAM
*mmdf_open (MAILSTREAM
*stream
);
266 void mmdf_close (MAILSTREAM
*stream
,long options
);
267 char *mmdf_header (MAILSTREAM
*stream
,unsigned long msgno
,
268 unsigned long *length
,long flags
);
269 long mmdf_text (MAILSTREAM
*stream
,unsigned long msgno
,STRING
*bs
,long flags
);
270 char *mmdf_text_work (MAILSTREAM
*stream
,MESSAGECACHE
*elt
,
271 unsigned long *length
,long flags
);
272 void mmdf_flagmsg (MAILSTREAM
*stream
,MESSAGECACHE
*elt
);
273 long mmdf_ping (MAILSTREAM
*stream
);
274 void mmdf_check (MAILSTREAM
*stream
);
275 long mmdf_expunge (MAILSTREAM
*stream
,char *sequence
,long options
);
276 long mmdf_copy (MAILSTREAM
*stream
,char *sequence
,char *mailbox
,long options
);
277 long mmdf_append (MAILSTREAM
*stream
,char *mailbox
,append_t af
,void *data
);
278 int mmdf_collect_msg (MAILSTREAM
*stream
,FILE *sf
,char *flags
,char *date
,
280 int mmdf_append_msgs (MAILSTREAM
*stream
,FILE *sf
,FILE *df
,SEARCHSET
*set
);
282 void mmdf_abort (MAILSTREAM
*stream
);
283 char *mmdf_file (char *dst
,char *name
);
284 int mmdf_lock (char *file
,int flags
,int mode
,DOTLOCK
*lock
,int op
);
285 void mmdf_unlock (int fd
,MAILSTREAM
*stream
,DOTLOCK
*lock
);
286 int mmdf_parse (MAILSTREAM
*stream
,DOTLOCK
*lock
,int op
);
287 char *mmdf_mbxline (MAILSTREAM
*stream
,STRING
*bs
,unsigned long *size
);
288 unsigned long mmdf_pseudo (MAILSTREAM
*stream
,char *hdr
);
289 unsigned long mmdf_xstatus (MAILSTREAM
*stream
,char *status
,MESSAGECACHE
*elt
,
290 unsigned long uid
,long flag
);
291 long mmdf_rewrite (MAILSTREAM
*stream
,unsigned long *nexp
,DOTLOCK
*lock
,
293 long mmdf_extend (MAILSTREAM
*stream
,unsigned long size
);
294 void mmdf_write (MMDFFILE
*f
,char *s
,unsigned long i
);
295 void mmdf_phys_write (MMDFFILE
*f
,char *buf
,size_t size
);
297 /* MMDF mail routines */
300 /* Driver dispatch used by MAIL */
302 DRIVER mmdfdriver
= {
303 "mmdf", /* driver name */
305 DR_LOCAL
|DR_MAIL
|DR_LOCKING
|DR_NONEWMAILRONLY
|DR_XPOINT
,
306 (DRIVER
*) NIL
, /* next driver */
307 mmdf_valid
, /* mailbox is valid for us */
308 mmdf_parameters
, /* manipulate parameters */
309 mmdf_scan
, /* scan mailboxes */
310 mmdf_list
, /* list mailboxes */
311 mmdf_lsub
, /* list subscribed mailboxes */
312 NIL
, /* subscribe to mailbox */
313 NIL
, /* unsubscribe from mailbox */
314 mmdf_create
, /* create mailbox */
315 mmdf_delete
, /* delete mailbox */
316 mmdf_rename
, /* rename mailbox */
317 mail_status_default
, /* status of mailbox */
318 mmdf_open
, /* open mailbox */
319 mmdf_close
, /* close mailbox */
320 NIL
, /* fetch message "fast" attributes */
321 NIL
, /* fetch message flags */
322 NIL
, /* fetch overview */
323 NIL
, /* fetch message envelopes */
324 mmdf_header
, /* fetch message header */
325 mmdf_text
, /* fetch message text */
326 NIL
, /* fetch partial message text */
327 NIL
, /* unique identifier */
328 NIL
, /* message number */
329 NIL
, /* modify flags */
330 mmdf_flagmsg
, /* per-message modify flags */
331 NIL
, /* search for message based on criteria */
332 NIL
, /* sort messages */
333 NIL
, /* thread messages */
334 mmdf_ping
, /* ping mailbox to see if still alive */
335 mmdf_check
, /* check for new messages */
336 mmdf_expunge
, /* expunge deleted messages */
337 mmdf_copy
, /* copy messages to another mailbox */
338 mmdf_append
, /* append string message to mailbox */
339 NIL
/* garbage collect stream */
342 /* prototype stream */
343 MAILSTREAM mmdfproto
= {&mmdfdriver
};
345 char *mmdfhdr
= MMDFHDRTXT
; /* MMDF header */
347 /* MMDF mail validate mailbox
348 * Accepts: mailbox name
349 * Returns: our driver if name is valid, NIL otherwise
352 DRIVER
*mmdf_valid (char *name
)
354 char tmp
[MAILTMPLEN
];
355 return mmdf_isvalid (name
,tmp
) ? &mmdfdriver
: NIL
;
359 /* MMDF mail test for valid mailbox name
360 * Accepts: mailbox name
362 * Returns: T if valid, NIL otherwise
365 long mmdf_isvalid (char *name
,char *tmp
)
369 char *t
,file
[MAILTMPLEN
];
372 errno
= EINVAL
; /* assume invalid argument */
373 /* must be non-empty file */
374 if ((t
= dummy_file (file
,name
)) && !stat (t
,&sbuf
)) {
375 if (!sbuf
.st_size
)errno
= 0;/* empty file */
376 else if ((fd
= open (file
,O_RDONLY
,NIL
)) >= 0) {
377 /* error -1 for invalid format */
378 if (!(ret
= mmdf_isvalid_fd (fd
,tmp
))) errno
= -1;
379 close (fd
); /* close the file */
380 /* \Marked status? */
381 if ((sbuf
.st_ctime
> sbuf
.st_atime
) || (sbuf
.st_mtime
> sbuf
.st_atime
)) {
382 tp
[0] = sbuf
.st_atime
; /* preserve atime and mtime */
383 tp
[1] = sbuf
.st_mtime
;
384 utime (file
,tp
); /* set the times */
388 return ret
; /* return what we should */
391 /* MMDF mail test for valid mailbox
392 * Accepts: file descriptor
394 * Returns: T if valid, NIL otherwise
397 long mmdf_isvalid_fd (int fd
,char *tmp
)
400 memset (tmp
,'\0',MAILTMPLEN
);
401 if (read (fd
,tmp
,MAILTMPLEN
-1) >= 0) ret
= ISMMDF (tmp
) ? T
: NIL
;
402 return ret
; /* return what we should */
406 /* MMDF manipulate driver parameters
407 * Accepts: function code
408 * function-dependent value
409 * Returns: function-dependent return value
412 void *mmdf_parameters (long function
,void *value
)
415 switch ((int) function
) {
417 if (value
) ret
= dummy_file ((char *) value
,"INBOX");
423 /* MMDF mail scan mailboxes
424 * Accepts: mail stream
430 void mmdf_scan (MAILSTREAM
*stream
,char *ref
,char *pat
,char *contents
)
432 if (stream
) dummy_scan (NIL
,ref
,pat
,contents
);
436 /* MMDF mail list mailboxes
437 * Accepts: mail stream
442 void mmdf_list (MAILSTREAM
*stream
,char *ref
,char *pat
)
444 if (stream
) dummy_list (NIL
,ref
,pat
);
448 /* MMDF mail list subscribed mailboxes
449 * Accepts: mail stream
454 void mmdf_lsub (MAILSTREAM
*stream
,char *ref
,char *pat
)
456 if (stream
) dummy_lsub (NIL
,ref
,pat
);
459 /* MMDF mail create mailbox
460 * Accepts: MAIL stream
461 * mailbox name to create
462 * Returns: T on success, NIL on failure
465 long mmdf_create (MAILSTREAM
*stream
,char *mailbox
)
467 char *s
,mbx
[MAILTMPLEN
],tmp
[MAILTMPLEN
];
470 time_t ti
= time (0);
471 if (!(s
= dummy_file (mbx
,mailbox
))) {
472 sprintf (tmp
,"Can't create %.80s: invalid name",mailbox
);
475 /* create underlying file */
476 else if (dummy_create_path (stream
,s
,get_dir_protection (mailbox
))) {
477 /* done if dir-only or whiner */
478 if (((s
= strrchr (s
,'/')) && !s
[1]) ||
479 mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
)) ret
= T
;
480 else if ((fd
= open (mbx
,O_WRONLY
,
481 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
))) < 0) {
482 sprintf (tmp
,"Can't reopen mailbox node %.80s: %s",mbx
,strerror (errno
));
484 unlink (mbx
); /* delete the file */
486 else { /* initialize header */
487 memset (tmp
,'\0',MAILTMPLEN
);
488 sprintf (tmp
,"%sFrom %s %sDate: ",mmdfhdr
,pseudo_from
,ctime (&ti
));
489 rfc822_date (s
= tmp
+ strlen (tmp
));
490 sprintf (s
+= strlen (s
), /* write the pseudo-header */
491 "\nFrom: %s <%s@%s>\nSubject: %s\nX-IMAP: %010lu 0000000000",
492 pseudo_name
,pseudo_from
,mylocalhost (),pseudo_subject
,
494 for (i
= 0; i
< NUSERFLAGS
; ++i
) if (default_user_flag (i
))
495 sprintf (s
+= strlen (s
)," %s",default_user_flag (i
));
496 sprintf (s
+= strlen (s
),"\nStatus: RO\n\n%s\n%s",pseudo_msg
,mmdfhdr
);
497 if (write (fd
,tmp
,strlen (tmp
)) > 0) ret
= T
;
499 sprintf (tmp
,"Can't initialize mailbox node %.80s: %s",mbx
,
502 unlink (mbx
); /* delete the file */
504 close (fd
); /* close file */
507 /* set proper protections */
508 return ret
? set_mbx_protections (mailbox
,mbx
) : NIL
;
511 /* MMDF mail delete mailbox
512 * Accepts: MAIL stream
513 * mailbox name to delete
514 * Returns: T on success, NIL on failure
517 long mmdf_delete (MAILSTREAM
*stream
,char *mailbox
)
519 return mmdf_rename (stream
,mailbox
,NIL
);
523 /* MMDF mail rename mailbox
524 * Accepts: MAIL stream
526 * new mailbox name (or NIL for delete)
527 * Returns: T on success, NIL on failure
530 long mmdf_rename (MAILSTREAM
*stream
,char *old
,char *newname
)
534 char tmp
[MAILTMPLEN
],file
[MAILTMPLEN
],lock
[MAILTMPLEN
];
539 MM_CRITICAL (stream
); /* get the c-client lock */
540 if (!dummy_file (file
,old
) ||
541 (newname
&& (!((s
= mailboxfile (tmp
,newname
)) && *s
) ||
542 ((s
= strrchr (tmp
,'/')) && !s
[1]))))
543 sprintf (tmp
,newname
?
544 "Can't rename mailbox %.80s to %.80s: invalid name" :
545 "Can't delete mailbox %.80s: invalid name",
547 /* lock out other c-clients */
548 else if ((ld
= lockname (lock
,file
,LOCK_EX
|LOCK_NB
,&i
)) < 0)
549 sprintf (tmp
,"Mailbox %.80s is in use by another process",old
);
552 if ((fd
= mmdf_lock (file
,O_RDWR
,
553 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
),
554 &lockx
,LOCK_EX
)) < 0)
555 sprintf (tmp
,"Can't lock mailbox %.80s: %s",old
,strerror (errno
));
557 if (newname
) { /* want rename? */
558 /* found superior to destination name? */
559 if (s
= strrchr (s
,'/')) {
560 c
= *++s
; /* remember first character of inferior */
561 *s
= '\0'; /* tie off to get just superior */
562 /* name doesn't exist, create it */
563 if ((stat (tmp
,&sbuf
) || ((sbuf
.st_mode
& S_IFMT
) != S_IFDIR
)) &&
564 !dummy_create_path (stream
,tmp
,get_dir_protection (newname
))) {
565 mmdf_unlock (fd
,NIL
,&lockx
);
566 mmdf_unlock (ld
,NIL
,NIL
);
568 MM_NOCRITICAL (stream
);
569 return ret
; /* return success or failure */
571 *s
= c
; /* restore full name */
573 if (rename (file
,tmp
))
574 sprintf (tmp
,"Can't rename mailbox %.80s to %.80s: %s",old
,newname
,
576 else ret
= T
; /* set success */
578 else if (unlink (file
))
579 sprintf (tmp
,"Can't delete mailbox %.80s: %s",old
,strerror (errno
));
580 else ret
= T
; /* set success */
581 mmdf_unlock (fd
,NIL
,&lockx
);
583 mmdf_unlock (ld
,NIL
,NIL
); /* flush the lock */
586 MM_NOCRITICAL (stream
); /* no longer critical */
587 if (!ret
) MM_LOG (tmp
,ERROR
); /* log error */
588 return ret
; /* return success or failure */
592 * Accepts: Stream to open
593 * Returns: Stream on success, NIL on failure
596 MAILSTREAM
*mmdf_open (MAILSTREAM
*stream
)
600 char tmp
[MAILTMPLEN
];
603 /* return prototype for OP_PROTOTYPE call */
604 if (!stream
) return user_flags (&mmdfproto
);
605 retry
= stream
->silent
? 1 : KODRETRY
;
606 if (stream
->local
) fatal ("mmdf recycle stream");
607 stream
->local
= memset (fs_get (sizeof (MMDFLOCAL
)),0,sizeof (MMDFLOCAL
));
608 /* note if an INBOX or not */
609 stream
->inbox
= !compare_cstring (stream
->mailbox
,"INBOX");
610 /* canonicalize the stream mailbox name */
611 if (!dummy_file (tmp
,stream
->mailbox
)) {
612 sprintf (tmp
,"Can't open - invalid name: %.80s",stream
->mailbox
);
617 fs_give ((void **) &stream
->mailbox
);
618 /* save canonical name */
619 stream
->mailbox
= cpystr (tmp
);
620 LOCAL
->fd
= LOCAL
->ld
= -1; /* no file or state locking yet */
621 LOCAL
->buf
= (char *) fs_get (CHUNKSIZE
);
622 LOCAL
->buflen
= CHUNKSIZE
- 1;
623 LOCAL
->text
.data
= (unsigned char *) fs_get (CHUNKSIZE
);
624 LOCAL
->text
.size
= CHUNKSIZE
- 1;
625 LOCAL
->linebuf
= (char *) fs_get (CHUNKSIZE
);
626 LOCAL
->linebuflen
= CHUNKSIZE
- 1;
627 stream
->sequence
++; /* bump sequence number */
629 /* make lock for read/write access */
630 if (!stream
->rdonly
) while (retry
) {
631 /* try to lock file */
632 if ((fd
= lockname (tmp
,stream
->mailbox
,LOCK_EX
|LOCK_NB
,&i
)) < 0) {
633 /* suppressing kiss-of-death? */
634 if (stream
->nokod
) retry
= 0;
635 /* no, first time through? */
636 else if (retry
-- == KODRETRY
) {
637 /* learned other guy's PID and can signal? */
638 if (i
&& !kill ((int) i
,SIGUSR2
)) {
639 sprintf (tmp
,"Trying to get mailbox lock from process %ld",i
);
642 else retry
= 0; /* give up */
644 if (!stream
->silent
) { /* nothing if silent stream */
645 if (retry
) sleep (1); /* wait a second before trying again */
646 else MM_LOG ("Mailbox is open by another process, access is readonly",
650 else { /* got the lock, nobody else can alter state */
651 LOCAL
->ld
= fd
; /* note lock's fd and name */
652 LOCAL
->lname
= cpystr (tmp
);
653 /* make sure mode OK (don't use fchmod()) */
654 chmod (LOCAL
->lname
,(long) mail_parameters (NIL
,GET_LOCKPROTECTION
,NIL
));
655 if (stream
->silent
) i
= 0;/* silent streams won't accept KOD */
656 else { /* note our PID in the lock */
657 sprintf (tmp
,"%d",getpid ());
658 write (fd
,tmp
,(i
= strlen (tmp
))+1);
660 ftruncate (fd
,i
); /* make sure tied off */
661 fsync (fd
); /* make sure it's available */
662 retry
= 0; /* no more need to try */
667 stream
->nmsgs
= stream
->recent
= 0;
668 /* will we be able to get write access? */
669 if ((LOCAL
->ld
>= 0) && access (stream
->mailbox
,W_OK
) && (errno
== EACCES
)) {
670 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN
);
671 flock (LOCAL
->ld
,LOCK_UN
); /* release the lock */
672 close (LOCAL
->ld
); /* close the lock file */
673 LOCAL
->ld
= -1; /* no more lock fd */
674 unlink (LOCAL
->lname
); /* delete it */
676 /* reset UID validity */
677 stream
->uid_validity
= stream
->uid_last
= 0;
678 if (stream
->silent
&& !stream
->rdonly
&& (LOCAL
->ld
< 0))
679 mmdf_abort (stream
); /* abort if can't get RW silent stream */
681 else if (mmdf_parse (stream
,&lock
,LOCK_SH
)) {
682 mmdf_unlock (LOCAL
->fd
,stream
,&lock
);
683 mail_unlock (stream
);
684 MM_NOCRITICAL (stream
); /* done with critical */
686 if (!LOCAL
) return NIL
; /* failure if stream died */
687 /* make sure upper level knows readonly */
688 stream
->rdonly
= (LOCAL
->ld
< 0);
689 /* notify about empty mailbox */
690 if (!(stream
->nmsgs
|| stream
->silent
)) MM_LOG ("Mailbox is empty",NIL
);
691 if (!stream
->rdonly
) { /* flags stick if readwrite */
692 stream
->perm_seen
= stream
->perm_deleted
=
693 stream
->perm_flagged
= stream
->perm_answered
= stream
->perm_draft
= T
;
694 if (!stream
->uid_nosticky
) {/* users with lives get permanent keywords */
695 stream
->perm_user_flags
= 0xffffffff;
696 /* and maybe can create them too! */
697 stream
->kwd_create
= stream
->user_flags
[NUSERFLAGS
-1] ? NIL
: T
;
700 return stream
; /* return stream alive to caller */
705 * Accepts: MAIL stream
709 void mmdf_close (MAILSTREAM
*stream
,long options
)
711 int silent
= stream
->silent
;
712 stream
->silent
= T
; /* go silent */
713 /* expunge if requested */
714 if (options
& CL_EXPUNGE
) mmdf_expunge (stream
,NIL
,NIL
);
715 /* else dump final checkpoint */
716 else if (LOCAL
->dirty
) mmdf_check (stream
);
717 stream
->silent
= silent
; /* restore old silence state */
718 mmdf_abort (stream
); /* now punt the file and local data */
721 /* MMDF mail fetch message header
722 * Accepts: MAIL stream
724 * pointer to returned header text length
726 * Returns: message header in RFC822 format
729 /* lines to filter from header */
730 static STRINGLIST
*mmdf_hlines
= NIL
;
732 char *mmdf_header (MAILSTREAM
*stream
,unsigned long msgno
,
733 unsigned long *length
,long flags
)
736 unsigned char *s
,*t
,*tl
;
737 *length
= 0; /* default to empty */
738 if (flags
& FT_UID
) return "";/* UID call "impossible" */
739 elt
= mail_elt (stream
,msgno
);/* get cache */
740 if (!mmdf_hlines
) { /* once only code */
741 STRINGLIST
*lines
= mmdf_hlines
= mail_newstringlist ();
742 lines
->text
.size
= strlen ((char *) (lines
->text
.data
=
743 (unsigned char *) "Status"));
744 lines
= lines
->next
= mail_newstringlist ();
745 lines
->text
.size
= strlen ((char *) (lines
->text
.data
=
746 (unsigned char *) "X-Status"));
747 lines
= lines
->next
= mail_newstringlist ();
748 lines
->text
.size
= strlen ((char *) (lines
->text
.data
=
749 (unsigned char *) "X-Keywords"));
750 lines
= lines
->next
= mail_newstringlist ();
751 lines
->text
.size
= strlen ((char *) (lines
->text
.data
=
752 (unsigned char *) "X-UID"));
753 lines
= lines
->next
= mail_newstringlist ();
754 lines
->text
.size
= strlen ((char *) (lines
->text
.data
=
755 (unsigned char *) "X-IMAP"));
756 lines
= lines
->next
= mail_newstringlist ();
757 lines
->text
.size
= strlen ((char *) (lines
->text
.data
=
758 (unsigned char *) "X-IMAPbase"));
760 /* go to header position */
761 lseek (LOCAL
->fd
,elt
->private.special
.offset
+
762 elt
->private.msg
.header
.offset
,L_SET
);
764 if (flags
& FT_INTERNAL
) { /* initial data OK? */
765 if (elt
->private.msg
.header
.text
.size
> LOCAL
->buflen
) {
766 fs_give ((void **) &LOCAL
->buf
);
767 LOCAL
->buf
= (char *) fs_get ((LOCAL
->buflen
=
768 elt
->private.msg
.header
.text
.size
) + 1);
771 read (LOCAL
->fd
,LOCAL
->buf
,elt
->private.msg
.header
.text
.size
);
772 /* got text, tie off string */
773 LOCAL
->buf
[*length
= elt
->private.msg
.header
.text
.size
] = '\0';
774 /* squeeze out CRs (in case from PC) */
775 for (s
= t
= LOCAL
->buf
,tl
= LOCAL
->buf
+ *length
; t
< tl
; t
++)
776 if (*t
!= '\r') *s
++ = *t
;
778 *length
= s
- LOCAL
->buf
; /* adjust length */
780 else { /* need to make a CRLF version */
781 read (LOCAL
->fd
,s
= (char *) fs_get (elt
->private.msg
.header
.text
.size
+1),
782 elt
->private.msg
.header
.text
.size
);
783 /* tie off string, and convert to CRLF */
784 s
[elt
->private.msg
.header
.text
.size
] = '\0';
785 *length
= strcrlfcpy (&LOCAL
->buf
,&LOCAL
->buflen
,s
,
786 elt
->private.msg
.header
.text
.size
);
787 fs_give ((void **) &s
); /* free readin buffer */
788 /* squeeze out spurious CRs */
789 for (s
= t
= LOCAL
->buf
,tl
= LOCAL
->buf
+ *length
; t
< tl
; t
++)
790 if ((*t
!= '\r') || (t
[1] == '\n')) *s
++ = *t
;
792 *length
= s
- LOCAL
->buf
; /* adjust length */
794 *length
= mail_filter (LOCAL
->buf
,*length
,mmdf_hlines
,FT_NOT
);
795 return (char *) LOCAL
->buf
; /* return processed copy */
798 /* MMDF mail fetch message text
799 * Accepts: MAIL stream
801 * pointer to returned stringstruct
803 * Returns: T on success, NIL if failure
806 long mmdf_text (MAILSTREAM
*stream
,unsigned long msgno
,STRING
*bs
,long flags
)
811 /* UID call "impossible" */
812 if (flags
& FT_UID
) return NIL
;
813 elt
= mail_elt (stream
,msgno
);/* get cache element */
814 /* if message not seen */
815 if (!(flags
& FT_PEEK
) && !elt
->seen
) {
816 /* mark message seen and dirty */
817 elt
->seen
= elt
->private.dirty
= LOCAL
->dirty
= T
;
818 MM_FLAGS (stream
,msgno
);
820 s
= mmdf_text_work (stream
,elt
,&i
,flags
);
821 INIT (bs
,mail_string
,s
,i
); /* set up stringstruct */
822 return T
; /* success */
825 /* MMDF mail fetch message text worker routine
826 * Accepts: MAIL stream
827 * message cache element
828 * pointer to returned header text length
832 char *mmdf_text_work (MAILSTREAM
*stream
,MESSAGECACHE
*elt
,
833 unsigned long *length
,long flags
)
837 unsigned char c
,*s
,*t
,*tl
,tmp
[CHUNKSIZE
];
838 /* go to text position */
839 lseek (LOCAL
->fd
,elt
->private.special
.offset
+
840 elt
->private.msg
.text
.offset
,L_SET
);
841 if (flags
& FT_INTERNAL
) { /* initial data OK? */
842 if (elt
->private.msg
.text
.text
.size
> LOCAL
->buflen
) {
843 fs_give ((void **) &LOCAL
->buf
);
844 LOCAL
->buf
= (char *) fs_get ((LOCAL
->buflen
=
845 elt
->private.msg
.text
.text
.size
) + 1);
848 read (LOCAL
->fd
,LOCAL
->buf
,elt
->private.msg
.text
.text
.size
);
849 /* got text, tie off string */
850 LOCAL
->buf
[*length
= elt
->private.msg
.text
.text
.size
] = '\0';
851 /* squeeze out CRs (in case from PC) */
852 for (s
= t
= LOCAL
->buf
,tl
= LOCAL
->buf
+ *length
; t
< tl
; t
++)
853 if (*t
!= '\r') *s
++ = *t
;
855 *length
= s
- LOCAL
->buf
; /* adjust length */
856 return (char *) LOCAL
->buf
;
859 /* have it cached already? */
860 if (elt
->private.uid
!= LOCAL
->uid
) {
861 /* not cached, cache it now */
862 LOCAL
->uid
= elt
->private.uid
;
863 /* is buffer big enough? */
864 if (elt
->rfc822_size
> LOCAL
->text
.size
) {
865 /* excessively conservative, but the right thing is too hard to do */
866 fs_give ((void **) &LOCAL
->text
.data
);
867 LOCAL
->text
.data
= (unsigned char *)
868 fs_get ((LOCAL
->text
.size
= elt
->rfc822_size
) + 1);
870 d
.fd
= LOCAL
->fd
; /* yes, set up file descriptor */
871 d
.pos
= elt
->private.special
.offset
+ elt
->private.msg
.text
.offset
;
872 d
.chunk
= tmp
; /* initial buffer chunk */
873 d
.chunksize
= CHUNKSIZE
; /* file chunk size */
874 INIT (&bs
,fd_string
,&d
,elt
->private.msg
.text
.text
.size
);
875 for (s
= (char *) LOCAL
->text
.data
; SIZE (&bs
);) switch (c
= SNX (&bs
)) {
876 case '\r': /* carriage return seen */
879 *s
++ = '\r'; /* insert a CR */
881 *s
++ = c
; /* copy characters */
883 *s
= '\0'; /* tie off buffer */
884 /* calculate length of cached data */
885 LOCAL
->textlen
= s
- LOCAL
->text
.data
;
887 *length
= LOCAL
->textlen
; /* return from cache */
888 return (char *) LOCAL
->text
.data
;
891 /* MMDF per-message modify flag
892 * Accepts: MAIL stream
893 * message cache element
896 void mmdf_flagmsg (MAILSTREAM
*stream
,MESSAGECACHE
*elt
)
898 /* only after finishing */
899 if (elt
->valid
) elt
->private.dirty
= LOCAL
->dirty
= T
;
903 /* MMDF mail ping mailbox
904 * Accepts: MAIL stream
905 * Returns: T if stream alive, else NIL
908 long mmdf_ping (MAILSTREAM
*stream
)
913 /* big no-op if not readwrite */
914 if (LOCAL
&& (LOCAL
->ld
>= 0) && !stream
->lock
) {
915 if (stream
->rdonly
) { /* does he want to give up readwrite? */
916 /* checkpoint if we changed something */
917 if (LOCAL
->dirty
) mmdf_check (stream
);
918 flock (LOCAL
->ld
,LOCK_UN
);/* release readwrite lock */
919 close (LOCAL
->ld
); /* close the readwrite lock file */
920 LOCAL
->ld
= -1; /* no more readwrite lock fd */
921 unlink (LOCAL
->lname
); /* delete the readwrite lock file */
923 else { /* see if need to reparse */
924 if (!(reparse
= (long) mail_parameters (NIL
,GET_NETFSSTATBUG
,NIL
))) {
925 /* get current mailbox size */
926 if (LOCAL
->fd
>= 0) fstat (LOCAL
->fd
,&sbuf
);
927 else if (stat (stream
->mailbox
,&sbuf
)) {
928 sprintf (LOCAL
->buf
,"Mailbox stat failed, aborted: %s",
930 MM_LOG (LOCAL
->buf
,ERROR
);
934 reparse
= (sbuf
.st_size
!= LOCAL
->filesize
);
936 /* parse if mailbox changed */
937 if ((LOCAL
->ddirty
|| reparse
) && mmdf_parse (stream
,&lock
,LOCK_EX
)) {
938 /* force checkpoint if double-dirty */
939 if (LOCAL
->ddirty
) mmdf_rewrite (stream
,NIL
,&lock
,NIL
);
941 else mmdf_unlock (LOCAL
->fd
,stream
,&lock
);
942 mail_unlock (stream
); /* and stream */
943 /* done with critical */
944 MM_NOCRITICAL (stream
);
948 return LOCAL
? LONGT
: NIL
; /* return if still alive */
951 /* MMDF mail check mailbox
952 * Accepts: MAIL stream
955 void mmdf_check (MAILSTREAM
*stream
)
958 /* parse and lock mailbox */
959 if (LOCAL
&& (LOCAL
->ld
>= 0) && !stream
->lock
&&
960 mmdf_parse (stream
,&lock
,LOCK_EX
)) {
961 /* any unsaved changes? */
962 if (LOCAL
->dirty
&& mmdf_rewrite (stream
,NIL
,&lock
,NIL
)) {
963 if (!stream
->silent
) MM_LOG ("Checkpoint completed",NIL
);
965 /* no checkpoint needed, just unlock */
966 else mmdf_unlock (LOCAL
->fd
,stream
,&lock
);
967 mail_unlock (stream
); /* unlock the stream */
968 MM_NOCRITICAL (stream
); /* done with critical */
973 /* MMDF mail expunge mailbox
974 * Accepts: MAIL stream
975 * sequence to expunge if non-NIL
980 long mmdf_expunge (MAILSTREAM
*stream
,char *sequence
,long options
)
986 if (ret
= (sequence
? ((options
& EX_UID
) ?
987 mail_uid_sequence (stream
,sequence
) :
988 mail_sequence (stream
,sequence
)) : LONGT
) &&
989 LOCAL
&& (LOCAL
->ld
>= 0) && !stream
->lock
&&
990 mmdf_parse (stream
,&lock
,LOCK_EX
)) {
991 /* check expunged messages if not dirty */
992 for (i
= 1; !LOCAL
->dirty
&& (i
<= stream
->nmsgs
); i
++) {
993 MESSAGECACHE
*elt
= mail_elt (stream
,i
);
994 if (mail_elt (stream
,i
)->deleted
) LOCAL
->dirty
= T
;
996 if (!LOCAL
->dirty
) { /* not dirty and no expunged messages */
997 mmdf_unlock (LOCAL
->fd
,stream
,&lock
);
998 msg
= "No messages deleted, so no update needed";
1000 else if (mmdf_rewrite (stream
,&i
,&lock
,sequence
? LONGT
: NIL
)) {
1001 if (i
) sprintf (msg
= LOCAL
->buf
,"Expunged %lu messages",i
);
1002 else msg
= "Mailbox checkpointed, but no messages expunged";
1004 /* rewrite failed */
1005 else mmdf_unlock (LOCAL
->fd
,stream
,&lock
);
1006 mail_unlock (stream
); /* unlock the stream */
1007 MM_NOCRITICAL (stream
); /* done with critical */
1008 if (msg
&& !stream
->silent
) MM_LOG (msg
,NIL
);
1010 else if (!stream
->silent
)
1011 MM_LOG ("Expunge ignored on readonly mailbox",WARN
);
1015 /* MMDF mail copy message(s)
1016 * Accepts: MAIL stream
1018 * destination mailbox
1020 * Returns: T if copy successful, else NIL
1023 long mmdf_copy (MAILSTREAM
*stream
,char *sequence
,char *mailbox
,long options
)
1027 char *s
,file
[MAILTMPLEN
];
1033 mailproxycopy_t pc
=
1034 (mailproxycopy_t
) mail_parameters (stream
,GET_MAILPROXYCOPY
,NIL
);
1035 copyuid_t cu
= (copyuid_t
) (mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
) ?
1036 NIL
: mail_parameters (NIL
,GET_COPYUID
,NIL
));
1037 SEARCHSET
*source
= cu
? mail_newsearchset () : NIL
;
1038 SEARCHSET
*dest
= cu
? mail_newsearchset () : NIL
;
1039 MAILSTREAM
*tstream
= NIL
;
1040 if (!((options
& CP_UID
) ? mail_uid_sequence (stream
,sequence
) :
1041 mail_sequence (stream
,sequence
))) return NIL
;
1042 /* make sure destination is valid */
1043 if (!(mmdf_valid (mailbox
) || !errno
))
1045 case ENOENT
: /* no such file? */
1046 if (compare_cstring (mailbox
,"INBOX")) {
1047 MM_NOTIFY (stream
,"[TRYCREATE] Must create mailbox before copy",NIL
);
1050 if (pc
) return (*pc
) (stream
,sequence
,mailbox
,options
);
1051 mmdf_create (NIL
,"INBOX");/* create empty INBOX */
1052 case EACCES
: /* file protected */
1053 sprintf (LOCAL
->buf
,"Can't access destination: %.80s",mailbox
);
1054 MM_LOG (LOCAL
->buf
,ERROR
);
1057 if (pc
) return (*pc
) (stream
,sequence
,mailbox
,options
);
1058 sprintf (LOCAL
->buf
,"Invalid MMDF-format mailbox name: %.80s",mailbox
);
1059 MM_LOG (LOCAL
->buf
,ERROR
);
1062 if (pc
) return (*pc
) (stream
,sequence
,mailbox
,options
);
1063 sprintf (LOCAL
->buf
,"Not a MMDF-format mailbox: %.80s",mailbox
);
1064 MM_LOG (LOCAL
->buf
,ERROR
);
1068 /* try to open rewrite for UIDPLUS */
1069 if ((tstream
= mail_open_work (&mmdfdriver
,NIL
,mailbox
,
1070 OP_SILENT
|OP_NOKOD
)) && tstream
->rdonly
)
1071 tstream
= mail_close (tstream
);
1072 if (cu
&& !tstream
) { /* wanted a COPYUID? */
1073 sprintf (LOCAL
->buf
,"Unable to write-open mailbox for COPYUID: %.80s",
1075 MM_LOG (LOCAL
->buf
,WARN
);
1076 cu
= NIL
; /* don't try to do COPYUID */
1078 LOCAL
->buf
[0] = '\0';
1079 MM_CRITICAL (stream
); /* go critical */
1080 if ((fd
= mmdf_lock (dummy_file (file
,mailbox
),O_WRONLY
|O_APPEND
,
1081 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
),
1082 &lock
,LOCK_EX
)) < 0) {
1083 MM_NOCRITICAL (stream
); /* done with critical */
1084 sprintf (LOCAL
->buf
,"Can't open destination mailbox: %s",strerror (errno
));
1085 MM_LOG (LOCAL
->buf
,ERROR
); /* log the error */
1086 return NIL
; /* failed */
1088 fstat (fd
,&sbuf
); /* get current file size */
1089 /* write all requested messages to mailbox */
1090 for (i
= 1; ret
&& (i
<= stream
->nmsgs
); i
++)
1091 if ((elt
= mail_elt (stream
,i
))->sequence
) {
1092 lseek (LOCAL
->fd
,elt
->private.special
.offset
,L_SET
);
1093 read (LOCAL
->fd
,LOCAL
->buf
,elt
->private.special
.text
.size
);
1094 if (write (fd
,LOCAL
->buf
,elt
->private.special
.text
.size
) < 0) ret
= NIL
;
1095 else { /* internal header succeeded */
1096 s
= mmdf_header (stream
,i
,&j
,FT_INTERNAL
);
1097 /* header size, sans trailing newline */
1098 if (j
&& (s
[j
- 2] == '\n')) j
--;
1099 if (write (fd
,s
,j
) < 0) ret
= NIL
;
1100 else { /* message header succeeded */
1101 j
= tstream
? /* write UIDPLUS data if have readwrite */
1102 mmdf_xstatus (stream
,LOCAL
->buf
,elt
,++(tstream
->uid_last
),LONGT
) :
1103 mmdf_xstatus (stream
,LOCAL
->buf
,elt
,NIL
,NIL
);
1104 if (write (fd
,LOCAL
->buf
,j
) < 0) ret
= NIL
;
1105 else { /* message status succeeded */
1106 s
= mmdf_text_work (stream
,elt
,&j
,FT_INTERNAL
);
1107 if ((write (fd
,s
,j
) < 0) || (write (fd
,mmdfhdr
,MMDFHDRLEN
) < 0))
1109 else if (cu
) { /* need to pass back new UID? */
1110 mail_append_set (source
,mail_uid (stream
,i
));
1111 mail_append_set (dest
,tstream
->uid_last
);
1118 if (!ret
|| fsync (fd
)) { /* force out the update */
1119 sprintf (LOCAL
->buf
,"Message copy failed: %s",strerror (errno
));
1120 ftruncate (fd
,sbuf
.st_size
);
1123 /* force UIDVALIDITY assignment now */
1124 if (tstream
&& !tstream
->uid_validity
) tstream
->uid_validity
= time (0);
1125 /* return sets if doing COPYUID */
1126 if (cu
&& ret
) (*cu
) (stream
,mailbox
,tstream
->uid_validity
,source
,dest
);
1127 else { /* flush any sets we may have built */
1128 mail_free_searchset (&source
);
1129 mail_free_searchset (&dest
);
1131 tp
[1] = time (0); /* set mtime to now */
1132 if (ret
) tp
[0] = tp
[1] - 1; /* set atime to now-1 if successful copy */
1133 else tp
[0] = /* else preserve \Marked status */
1134 ((sbuf
.st_ctime
> sbuf
.st_atime
) || (sbuf
.st_mtime
> sbuf
.st_atime
)) ?
1135 sbuf
.st_atime
: tp
[1];
1136 utime (file
,tp
); /* set the times */
1137 mmdf_unlock (fd
,NIL
,&lock
); /* unlock and close mailbox */
1138 if (tstream
) { /* update last UID if we can */
1139 MMDFLOCAL
*local
= (MMDFLOCAL
*) tstream
->local
;
1140 local
->dirty
= T
; /* do a rewrite */
1141 local
->appending
= T
; /* but not at the cost of marking as old */
1142 tstream
= mail_close (tstream
);
1145 if (!ret
) MM_LOG (LOCAL
->buf
,ERROR
);
1146 /* delete if requested message */
1147 else if (options
& CP_MOVE
) for (i
= 1; i
<= stream
->nmsgs
; i
++)
1148 if ((elt
= mail_elt (stream
,i
))->sequence
)
1149 elt
->deleted
= elt
->private.dirty
= LOCAL
->dirty
= T
;
1150 MM_NOCRITICAL (stream
); /* release critical */
1154 /* MMDF mail append message from stringstruct
1155 * Accepts: MAIL stream
1156 * destination mailbox
1159 * Returns: T if append successful, else NIL
1162 #define BUFLEN 8*MAILTMPLEN
1164 long mmdf_append (MAILSTREAM
*stream
,char *mailbox
,append_t af
,void *data
)
1169 char *flags
,*date
,buf
[BUFLEN
],tmp
[MAILTMPLEN
],file
[MAILTMPLEN
];
1175 unsigned long uidlocation
= 0;
1176 appenduid_t au
= (appenduid_t
)
1177 (mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
) ? NIL
:
1178 mail_parameters (NIL
,GET_APPENDUID
,NIL
));
1179 SEARCHSET
*dst
= au
? mail_newsearchset () : NIL
;
1181 MAILSTREAM
*tstream
= NIL
;
1182 /* default stream to prototype */
1183 if (!stream
) { /* stream specified? */
1184 stream
= &mmdfproto
; /* no, default stream to prototype */
1185 for (i
= 0; i
< NUSERFLAGS
&& stream
->user_flags
[i
]; ++i
)
1186 fs_give ((void **) &stream
->user_flags
[i
]);
1188 if (!mmdf_valid (mailbox
)) switch (errno
) {
1189 case ENOENT
: /* no such file? */
1190 if (compare_cstring (mailbox
,"INBOX")) {
1191 MM_NOTIFY (stream
,"[TRYCREATE] Must create mailbox before append",NIL
);
1194 mmdf_create (NIL
,"INBOX"); /* create empty INBOX */
1195 case 0: /* merely empty file? */
1198 case EACCES
: /* file protected */
1199 sprintf (tmp
,"Can't access destination: %.80s",mailbox
);
1203 sprintf (tmp
,"Invalid MMDF-format mailbox name: %.80s",mailbox
);
1207 sprintf (tmp
,"Not a MMDF-format mailbox: %.80s",mailbox
);
1211 /* get sniffing stream for keywords */
1212 else if (!(tstream
= mail_open (NIL
,mailbox
,
1213 OP_READONLY
|OP_SILENT
|OP_NOKOD
|OP_SNIFF
))) {
1214 sprintf (tmp
,"Unable to examine mailbox for APPEND: %.80s",mailbox
);
1219 /* get first message */
1220 if (!MM_APPEND (af
) (tstream
,data
,&flags
,&date
,&message
)) return NIL
;
1221 if (!(sf
= tmpfile ())) { /* must have scratch file */
1222 sprintf (tmp
,".%lx.%lx",(unsigned long) time (0),(unsigned long)getpid ());
1223 if (!stat (tmp
,&sbuf
) || !(sf
= fopen (tmp
,"wb+"))) {
1224 sprintf (tmp
,"Unable to create scratch file: %.80s",strerror (errno
));
1230 do { /* parse date */
1231 if (!date
) rfc822_date (date
= tmp
);
1232 if (!mail_parse_date (&elt
,date
)) {
1233 sprintf (tmp
,"Bad date in append: %.80s",date
);
1236 else { /* user wants to suppress time zones? */
1237 if (mail_parameters (NIL
,GET_NOTIMEZONES
,NIL
)) {
1238 time_t when
= mail_longdate (&elt
);
1239 date
= ctime (&when
); /* use traditional date */
1241 /* use POSIX-style date */
1242 else date
= mail_cdate (tmp
,&elt
);
1243 if (!SIZE (message
)) MM_LOG ("Append of zero-length message",ERROR
);
1244 else if (!mmdf_collect_msg (tstream
,sf
,flags
,date
,message
)) {
1245 sprintf (tmp
,"Error writing scratch file: %.80s",strerror (errno
));
1248 /* get next message */
1249 else if (MM_APPEND (af
) (tstream
,data
,&flags
,&date
,&message
)) continue;
1251 fclose (sf
); /* punt scratch file */
1252 return NIL
; /* give up */
1253 } while (message
); /* until no more messages */
1255 sprintf (tmp
,"Error finishing scratch file: %.80s",strerror (errno
));
1257 fclose (sf
); /* punt scratch file */
1258 return NIL
; /* give up */
1260 i
= ftell (sf
); /* size of scratch file */
1261 if (tstream
!= stream
) tstream
= mail_close (tstream
);
1263 MM_CRITICAL (stream
); /* go critical */
1264 /* try to open readwrite for UIDPLUS */
1265 if ((tstream
= mail_open_work (&mmdfdriver
,NIL
,mailbox
,
1266 OP_SILENT
|OP_NOKOD
)) && tstream
->rdonly
)
1267 tstream
= mail_close (tstream
);
1268 if (au
&& !tstream
) { /* wanted an APPENDUID? */
1269 sprintf (tmp
,"Unable to re-open mailbox for APPENDUID: %.80s",mailbox
);
1273 if (((fd
= mmdf_lock (dummy_file (file
,mailbox
),O_WRONLY
|O_APPEND
,
1274 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
),
1275 &lock
,LOCK_EX
)) < 0) ||
1276 !(df
= fdopen (fd
,"ab"))) {
1277 MM_NOCRITICAL (stream
); /* done with critical */
1278 sprintf (tmp
,"Can't open append mailbox: %s",strerror (errno
));
1282 fstat (fd
,&sbuf
); /* get current file size */
1284 tp
[1] = time (0); /* set mtime to now */
1285 /* write all messages */
1286 if (!mmdf_append_msgs (tstream
,sf
,df
,au
? dst
: NIL
) ||
1287 (fflush (df
) == EOF
) || fsync (fd
)) {
1288 sprintf (buf
,"Message append failed: %s",strerror (errno
));
1290 ftruncate (fd
,sbuf
.st_size
);
1291 tp
[0] = /* preserve \Marked status */
1292 ((sbuf
.st_ctime
> sbuf
.st_atime
) || (sbuf
.st_mtime
> sbuf
.st_atime
)) ?
1293 sbuf
.st_atime
: tp
[1];
1294 ret
= NIL
; /* return error */
1296 else tp
[0] = tp
[1] - 1; /* set atime to now-1 if successful copy */
1297 utime (file
,tp
); /* set the times */
1298 fclose (sf
); /* done with scratch file */
1299 /* force UIDVALIDITY assignment now */
1300 if (tstream
&& !tstream
->uid_validity
) tstream
->uid_validity
= time (0);
1301 /* return sets if doing APPENDUID */
1302 if (au
&& ret
) (*au
) (mailbox
,tstream
->uid_validity
,dst
);
1303 else mail_free_searchset (&dst
);
1304 mmdf_unlock (fd
,NIL
,&lock
); /* unlock and close mailbox */
1306 if (tstream
) { /* update last UID if we can */
1307 MMDFLOCAL
*local
= (MMDFLOCAL
*) tstream
->local
;
1308 local
->dirty
= T
; /* do a rewrite */
1309 local
->appending
= T
; /* but not at the cost of marking as old */
1310 tstream
= mail_close (tstream
);
1312 MM_NOCRITICAL (stream
); /* release critical */
1316 /* Collect and write single message to append scratch file
1317 * Accepts: MAIL stream
1321 * message stringstruct
1322 * Returns: NIL if write error, else T
1325 int mmdf_collect_msg (MAILSTREAM
*stream
,FILE *sf
,char *flags
,char *date
,
1328 unsigned char *s
,*t
;
1330 long f
= mail_parse_flags (stream
,flags
,&uf
);
1331 /* write metadata, note date ends with NL */
1332 if (fprintf (sf
,"%ld %lu %s",f
,SIZE (msg
) + 1,date
) < 0) return NIL
;
1333 while (uf
) /* write user flags */
1334 if ((s
= stream
->user_flags
[find_rightmost_bit (&uf
)]) &&
1335 (fprintf (sf
," %s",s
) < 0)) return NIL
;
1336 if (putc ('\n',sf
) == EOF
) return NIL
;
1337 while (SIZE (msg
)) { /* copy text to scratch file */
1338 for (s
= (unsigned char *) msg
->curpos
, t
= s
+ msg
->cursize
; s
< t
; ++s
)
1339 if (!*s
) *s
= 0x80; /* disallow NUL */
1340 /* write buffered text */
1341 if (fwrite (msg
->curpos
,1,msg
->cursize
,sf
) == msg
->cursize
)
1342 SETPOS (msg
,GETPOS (msg
) + msg
->cursize
);
1343 else return NIL
; /* failed */
1345 /* write trailing newline and return */
1346 return (putc ('\n',sf
) == EOF
) ? NIL
: T
;
1349 /* Append messages from scratch file to mailbox
1350 * Accepts: MAIL stream
1353 * uidset to update if non-NIL
1354 * Returns: T if success, NIL if failure
1357 int mmdf_append_msgs (MAILSTREAM
*stream
,FILE *sf
,FILE *df
,SEARCHSET
*set
)
1362 char *x
,tmp
[MAILTMPLEN
];
1364 /* get message metadata line */
1365 while (fgets (tmp
,MAILTMPLEN
,sf
)) {
1366 if (!(isdigit (tmp
[0]) && strchr (tmp
,'\n'))) return NIL
;
1367 f
= strtol (tmp
,&x
,10); /* get flags */
1368 if (!((*x
++ == ' ') && isdigit (*x
))) return NIL
;
1369 i
= strtoul (x
,&x
,10); /* get message size */
1370 if ((*x
++ != ' ') || /* build initial header */
1371 (fprintf (df
,"%sFrom %s@%s %sStatus: ",mmdfhdr
,myusername(),
1372 mylocalhost(),x
) < 0) ||
1373 (f
&fSEEN
&& (putc ('R',df
) == EOF
)) ||
1374 (fputs ("\nX-Status: ",df
) == EOF
) ||
1375 (f
&fDELETED
&& (putc ('D',df
) == EOF
)) ||
1376 (f
&fFLAGGED
&& (putc ('F',df
) == EOF
)) ||
1377 (f
&fANSWERED
&& (putc ('A',df
) == EOF
)) ||
1378 (f
&fDRAFT
&& (putc ('T',df
) == EOF
)) ||
1379 (fputs ("\nX-Keywords:",df
) == EOF
)) return NIL
;
1381 while ((c
= getc (sf
)) != '\n') switch (c
) {
1385 if (putc (c
,df
) == EOF
) return NIL
;
1387 if ((putc ('\n',df
) == EOF
) ||
1388 (set
&& (fprintf (df
,"X-UID: %lu\n",++(stream
->uid_last
)) < 0)))
1391 for (c
= '\n'; i
&& fgets (tmp
,MAILTMPLEN
,sf
); c
= tmp
[j
-1]) {
1392 /* get read line length */
1393 if (i
< (j
= strlen (tmp
))) fatal ("mmdf_append_msgs overrun");
1394 i
-= j
; /* number of bytes left */
1395 /* squish out ^A and CRs (note copies NUL) */
1396 for (x
= tmp
; x
= strpbrk (x
,"\01\r"); --j
) memmove (x
,x
+1,j
-(x
-tmp
));
1397 if (!j
) continue; /* do nothing if line emptied */
1398 /* start of line? */
1399 if ((c
== '\n')) switch (tmp
[0]) {
1400 case 'S': case 's': /* possible "Status:" */
1401 if (hdrp
&& (j
> 6) && ((tmp
[1] == 't') || (tmp
[1] == 'T')) &&
1402 ((tmp
[2] == 'a') || (tmp
[2] == 'A')) &&
1403 ((tmp
[3] == 't') || (tmp
[3] == 'T')) &&
1404 ((tmp
[4] == 'u') || (tmp
[4] == 'U')) &&
1405 ((tmp
[5] == 's') || (tmp
[5] == 'S')) && (tmp
[6] == ':') &&
1406 (fputs ("X-Original-",df
) == EOF
)) return NIL
;
1408 case 'X': case 'x': /* possible X-??? header */
1409 if (hdrp
&& (tmp
[1] == '-') &&
1410 /* possible X-UID: */
1411 (((j
> 5) && ((tmp
[2] == 'U') || (tmp
[2] == 'u')) &&
1412 ((tmp
[3] == 'I') || (tmp
[3] == 'i')) &&
1413 ((tmp
[4] == 'D') || (tmp
[4] == 'd')) && (tmp
[5] == ':')) ||
1414 /* possible X-IMAP: */
1415 ((j
> 6) && ((tmp
[2] == 'I') || (tmp
[2] == 'i')) &&
1416 ((tmp
[3] == 'M') || (tmp
[3] == 'm')) &&
1417 ((tmp
[4] == 'A') || (tmp
[4] == 'a')) &&
1418 ((tmp
[5] == 'P') || (tmp
[5] == 'p')) &&
1420 /* or X-IMAPbase: */
1421 ((j
> 10) && ((tmp
[6] == 'b') || (tmp
[6] == 'B')) &&
1422 ((tmp
[7] == 'a') || (tmp
[7] == 'A')) &&
1423 ((tmp
[8] == 's') || (tmp
[8] == 'S')) &&
1424 ((tmp
[9] == 'e') || (tmp
[9] == 'E')) && (tmp
[10] == ':')))) ||
1425 /* possible X-Status: */
1426 ((j
> 8) && ((tmp
[2] == 'S') || (tmp
[2] == 's')) &&
1427 ((tmp
[3] == 't') || (tmp
[3] == 'T')) &&
1428 ((tmp
[4] == 'a') || (tmp
[4] == 'A')) &&
1429 ((tmp
[5] == 't') || (tmp
[5] == 'T')) &&
1430 ((tmp
[6] == 'u') || (tmp
[6] == 'U')) &&
1431 ((tmp
[7] == 's') || (tmp
[7] == 'S')) && (tmp
[8] == ':')) ||
1432 /* possible X-Keywords: */
1433 ((j
> 10) && ((tmp
[2] == 'K') || (tmp
[2] == 'k')) &&
1434 ((tmp
[3] == 'e') || (tmp
[3] == 'E')) &&
1435 ((tmp
[4] == 'y') || (tmp
[4] == 'Y')) &&
1436 ((tmp
[5] == 'w') || (tmp
[5] == 'W')) &&
1437 ((tmp
[6] == 'o') || (tmp
[6] == 'O')) &&
1438 ((tmp
[7] == 'r') || (tmp
[7] == 'R')) &&
1439 ((tmp
[8] == 'd') || (tmp
[8] == 'D')) &&
1440 ((tmp
[9] == 's') || (tmp
[9] == 'S')) && (tmp
[10] == ':'))) &&
1441 (fputs ("X-Original-",df
) == EOF
)) return NIL
;
1443 case '\n': /* blank line */
1446 default: /* nothing to do */
1449 /* just write the line */
1450 if (fwrite (tmp
,1,j
,df
) != j
) return NIL
;
1452 /* make sure read entire msg & wrote trailer */
1453 if (i
|| (fputs (mmdfhdr
,df
) == EOF
)) return NIL
;
1455 if (stream
) mail_append_set (set
,stream
->uid_last
);
1460 /* Internal routines */
1463 /* MMDF mail abort stream
1464 * Accepts: MAIL stream
1467 void mmdf_abort (MAILSTREAM
*stream
)
1469 if (LOCAL
) { /* only if a file is open */
1470 if (LOCAL
->fd
>= 0) close (LOCAL
->fd
);
1471 if (LOCAL
->ld
>= 0) { /* have a mailbox lock? */
1472 flock (LOCAL
->ld
,LOCK_UN
);/* yes, release the lock */
1473 close (LOCAL
->ld
); /* close the lock file */
1474 unlink (LOCAL
->lname
); /* and delete it */
1476 if (LOCAL
->lname
) fs_give ((void **) &LOCAL
->lname
);
1477 /* free local text buffers */
1478 if (LOCAL
->buf
) fs_give ((void **) &LOCAL
->buf
);
1479 if (LOCAL
->text
.data
) fs_give ((void **) &LOCAL
->text
.data
);
1480 if (LOCAL
->linebuf
) fs_give ((void **) &LOCAL
->linebuf
);
1481 if (LOCAL
->line
) fs_give ((void **) &LOCAL
->line
);
1482 /* nuke the local data */
1483 fs_give ((void **) &stream
->local
);
1484 stream
->dtb
= NIL
; /* log out the DTB */
1488 /* MMDF open and lock mailbox
1489 * Accepts: file name to open/lock
1491 * destination buffer for lock file name
1492 * type of locking operation (LOCK_SH or LOCK_EX)
1495 int mmdf_lock (char *file
,int flags
,int mode
,DOTLOCK
*lock
,int op
)
1498 blocknotify_t bn
= (blocknotify_t
) mail_parameters (NIL
,GET_BLOCKNOTIFY
,NIL
);
1499 (*bn
) (BLOCK_FILELOCK
,NIL
);
1500 /* try locking the easy way */
1501 if (dotlock_lock (file
,lock
,-1)) {
1502 /* got dotlock file, easy open */
1503 if ((fd
= open (file
,flags
,mode
)) >= 0) flock (fd
,op
);
1504 else dotlock_unlock (lock
); /* open failed, free the dotlock */
1506 /* no dot lock file, open file now */
1507 else if ((fd
= open (file
,flags
,mode
)) >= 0) {
1508 /* try paranoid way to make a dot lock file */
1509 if (dotlock_lock (file
,lock
,fd
)) {
1510 close (fd
); /* get fresh fd in case of timing race */
1511 if ((fd
= open (file
,flags
,mode
)) >= 0) flock (fd
,op
);
1512 /* open failed, free the dotlock */
1513 else dotlock_unlock (lock
);
1515 else flock (fd
,op
); /* paranoid way failed, just flock() it */
1517 (*bn
) (BLOCK_NONE
,NIL
);
1521 /* MMDF unlock and close mailbox
1522 * Accepts: file descriptor
1523 * (optional) mailbox stream to check atime/mtime
1524 * (optional) lock file name
1527 void mmdf_unlock (int fd
,MAILSTREAM
*stream
,DOTLOCK
*lock
)
1529 if (stream
) { /* need to muck with times? */
1532 time_t now
= time (0);
1533 fstat (fd
,&sbuf
); /* get file times */
1534 if (LOCAL
->ld
>= 0) { /* yes, readwrite session? */
1535 tp
[0] = now
; /* set atime to now */
1536 /* set mtime to (now - 1) if necessary */
1537 tp
[1] = (now
> sbuf
.st_mtime
) ? sbuf
.st_mtime
: now
- 1;
1539 else if (stream
->recent
) { /* readonly with recent messages */
1540 if ((sbuf
.st_atime
>= sbuf
.st_mtime
) ||
1541 (sbuf
.st_atime
>= sbuf
.st_ctime
))
1542 /* keep past mtime, whack back atime */
1543 tp
[0] = (tp
[1] = (sbuf
.st_mtime
< now
) ? sbuf
.st_mtime
: now
) - 1;
1544 else now
= 0; /* no time change needed */
1546 /* readonly with no recent messages */
1547 else if ((sbuf
.st_atime
< sbuf
.st_mtime
) ||
1548 (sbuf
.st_atime
< sbuf
.st_ctime
)) {
1549 tp
[0] = now
; /* set atime to now */
1550 /* set mtime to (now - 1) if necessary */
1551 tp
[1] = (now
> sbuf
.st_mtime
) ? sbuf
.st_mtime
: now
- 1;
1553 else now
= 0; /* no time change needed */
1554 /* set the times, note change */
1555 if (now
&& !utime (stream
->mailbox
,tp
)) LOCAL
->filetime
= tp
[1];
1557 flock (fd
,LOCK_UN
); /* release flock'ers */
1558 if (!stream
) close (fd
); /* close the file if no stream */
1559 dotlock_unlock (lock
); /* flush the lock file if any */
1562 /* MMDF mail parse and lock mailbox
1563 * Accepts: MAIL stream
1564 * space to write lock file name
1565 * type of locking operation
1566 * Returns: T if parse OK, critical & mailbox is locked shared; NIL if failure
1569 int mmdf_parse (MAILSTREAM
*stream
,DOTLOCK
*lock
,int op
)
1572 unsigned long i
,j
,k
;
1573 unsigned char c
,*s
,*t
,*u
,tmp
[MAILTMPLEN
],date
[30];
1575 unsigned long nmsgs
= stream
->nmsgs
;
1576 unsigned long prevuid
= nmsgs
? mail_elt (stream
,nmsgs
)->private.uid
: 0;
1577 unsigned long recent
= stream
->recent
;
1578 unsigned long oldnmsgs
= stream
->nmsgs
;
1579 short silent
= stream
->silent
;
1580 short pseudoseen
= NIL
;
1585 mail_lock (stream
); /* guard against recursion or pingers */
1586 /* toss out previous descriptor */
1587 if (LOCAL
->fd
>= 0) close (LOCAL
->fd
);
1588 MM_CRITICAL (stream
); /* open and lock mailbox (shared OK) */
1589 if ((LOCAL
->fd
= mmdf_lock (stream
->mailbox
,(LOCAL
->ld
>= 0) ?
1591 (long)mail_parameters(NIL
,GET_MBXPROTECTION
,NIL
),
1593 sprintf (tmp
,"Mailbox open failed, aborted: %s",strerror (errno
));
1595 mmdf_abort (stream
);
1596 mail_unlock (stream
);
1597 MM_NOCRITICAL (stream
); /* done with critical */
1600 fstat (LOCAL
->fd
,&sbuf
); /* get status */
1601 /* validate change in size */
1602 if (sbuf
.st_size
< LOCAL
->filesize
) {
1603 sprintf (tmp
,"Mailbox shrank from %lu to %lu bytes, aborted",
1604 (unsigned long) LOCAL
->filesize
,(unsigned long) sbuf
.st_size
);
1605 MM_LOG (tmp
,ERROR
); /* this is pretty bad */
1606 mmdf_unlock (LOCAL
->fd
,stream
,lock
);
1607 mmdf_abort (stream
);
1608 mail_unlock (stream
);
1609 MM_NOCRITICAL (stream
); /* done with critical */
1614 else if (i
= sbuf
.st_size
- LOCAL
->filesize
) {
1615 d
.fd
= LOCAL
->fd
; /* yes, set up file descriptor */
1616 d
.pos
= LOCAL
->filesize
; /* get to that position in the file */
1617 d
.chunk
= LOCAL
->buf
; /* initial buffer chunk */
1618 d
.chunksize
= CHUNKSIZE
; /* file chunk size */
1619 INIT (&bs
,fd_string
,&d
,i
); /* initialize stringstruct */
1620 /* skip leading whitespace for broken MTAs */
1621 while (((c
= CHR (&bs
)) == '\n') || (c
== '\r') ||
1622 (c
== ' ') || (c
== '\t')) SNX (&bs
);
1623 if (SIZE (&bs
)) { /* read new data */
1624 /* remember internal header position */
1625 j
= LOCAL
->filesize
+ GETPOS (&bs
);
1626 s
= mmdf_mbxline (stream
,&bs
,&i
);
1627 stream
->silent
= T
; /* quell main program new message events */
1628 do { /* read MMDF header */
1629 if (!(i
&& ISMMDF (s
))){/* see if valid MMDF header */
1630 sprintf (tmp
,"Unexpected changes to mailbox (try restarting): %.20s",
1632 /* see if we can back up to a line */
1633 if (i
&& (j
> MMDFHDRLEN
)) {
1634 SETPOS (&bs
,j
-= MMDFHDRLEN
);
1635 /* read previous line */
1636 s
= mmdf_mbxline (stream
,&bs
,&i
);
1637 /* kill the error if it looks good */
1638 if (i
&& ISMMDF (s
)) tmp
[0] = '\0';
1642 mmdf_unlock (LOCAL
->fd
,stream
,lock
);
1643 mmdf_abort (stream
);
1644 mail_unlock (stream
);
1645 MM_NOCRITICAL (stream
);
1649 /* instantiate first new message */
1650 mail_exists (stream
,++nmsgs
);
1651 (elt
= mail_elt (stream
,nmsgs
))->valid
= T
;
1652 recent
++; /* assume recent by default */
1654 /* note position/size of internal header */
1655 elt
->private.special
.offset
= j
;
1656 elt
->private.special
.text
.size
= i
;
1658 s
= mmdf_mbxline (stream
,&bs
,&i
);
1659 ti
= 0; /* assume not a valid date */
1661 if (i
) VALID (s
,t
,ti
,zn
);
1662 if (ti
) { /* generate plausible IMAPish date string */
1663 /* this is also part of header */
1664 elt
->private.special
.text
.size
+= i
;
1665 date
[2] = date
[6] = date
[20] = '-'; date
[11] = ' ';
1666 date
[14] = date
[17] = ':';
1668 date
[0] = t
[ti
- 2]; date
[1] = t
[ti
- 1];
1670 date
[3] = t
[ti
- 6]; date
[4] = t
[ti
- 5]; date
[5] = t
[ti
- 4];
1672 date
[12] = t
[ti
+ 1]; date
[13] = t
[ti
+ 2];
1674 date
[15] = t
[ti
+ 4]; date
[16] = t
[ti
+ 5];
1675 if (t
[ti
+= 6]==':'){ /* ss */
1676 date
[18] = t
[++ti
]; date
[19] = t
[++ti
];
1677 ti
++; /* move to space */
1679 else date
[18] = date
[19] = '0';
1680 /* yy -- advance over timezone if necessary */
1681 if (zn
== ti
) ti
+= (((t
[zn
+1] == '+') || (t
[zn
+1] == '-')) ? 6 : 4);
1682 date
[7] = t
[ti
+ 1]; date
[8] = t
[ti
+ 2];
1683 date
[9] = t
[ti
+ 3]; date
[10] = t
[ti
+ 4];
1685 t
= zn
? (t
+ zn
+ 1) : (unsigned char *) "LCL";
1686 date
[21] = *t
++; date
[22] = *t
++; date
[23] = *t
++;
1687 if ((date
[21] != '+') && (date
[21] != '-')) date
[24] = '\0';
1688 else { /* numeric time zone */
1689 date
[24] = *t
++; date
[25] = *t
++;
1690 date
[26] = '\0'; date
[20] = ' ';
1692 /* set internal date */
1693 if (!mail_parse_date (elt
,date
)) {
1694 sprintf (tmp
,"Unable to parse internal date: %s",(char *) date
);
1698 else { /* make date from file date */
1699 struct tm
*tm
= gmtime (&sbuf
.st_mtime
);
1700 elt
->day
= tm
->tm_mday
; elt
->month
= tm
->tm_mon
+ 1;
1701 elt
->year
= tm
->tm_year
+ 1900 - BASEYEAR
;
1702 elt
->hours
= tm
->tm_hour
; elt
->minutes
= tm
->tm_min
;
1703 elt
->seconds
= tm
->tm_sec
;
1704 elt
->zhours
= 0; elt
->zminutes
= 0;
1705 t
= NIL
; /* suppress line read */
1707 /* header starts here */
1708 elt
->private.msg
.header
.offset
= elt
->private.special
.text
.size
;
1710 do { /* look for message body */
1711 j
= GETPOS (&bs
); /* note position before line */
1712 if (t
) s
= t
= mmdf_mbxline (stream
,&bs
,&i
);
1713 else t
= s
; /* this line read was suppressed */
1714 if (ISMMDF (s
)) { /* found terminator in header? */
1715 SETPOS (&bs
,j
); /* oops, back up before line */
1716 /* must insert a newline */
1717 elt
->private.spare
.data
++;
1720 /* this line is part of header */
1721 elt
->private.msg
.header
.text
.size
+= i
;
1722 if (i
) switch (*s
) { /* check header lines */
1723 case 'X': /* possible X-???: line */
1724 if (s
[1] == '-') { /* must be immediately followed by hyphen */
1725 /* X-Status: becomes Status: in S case */
1726 if (s
[2] == 'S' && s
[3] == 't' && s
[4] == 'a' && s
[5] == 't' &&
1727 s
[6] == 'u' && s
[7] == 's' && s
[8] == ':') s
+= 2;
1728 /* possible X-Keywords */
1729 else if (s
[2] == 'K' && s
[3] == 'e' && s
[4] == 'y' &&
1730 s
[5] == 'w' && s
[6] == 'o' && s
[7] == 'r' &&
1731 s
[8] == 'd' && s
[9] == 's' && s
[10] == ':') {
1733 retain
= NIL
; /* don't retain continuation */
1734 s
+= 11; /* flush leading whitespace */
1735 while (*s
&& (*s
!= '\n') && ((*s
!= '\r') || (s
[1] != '\n'))){
1736 while (*s
== ' ') s
++;
1737 /* find end of keyword */
1738 if (!(u
= strpbrk (s
," \n\r"))) u
= s
+ strlen (s
);
1739 /* got a keyword? */
1740 if ((k
= (u
- s
)) && (k
<= MAXUSERFLAG
)) {
1741 uf
.data
= (unsigned char *) s
;
1743 for (j
= 0; (j
< NUSERFLAGS
) && stream
->user_flags
[j
]; ++j
)
1744 if (!compare_csizedtext (stream
->user_flags
[j
],&uf
)) {
1745 elt
->user_flags
|= ((long) 1) << j
;
1749 s
= u
; /* advance to next keyword */
1754 /* possible X-IMAP */
1755 else if ((s
[2] == 'I') && (s
[3] == 'M') && (s
[4] == 'A') &&
1756 (s
[5] == 'P') && ((m
= (s
[6] == ':')) ||
1757 ((s
[6] == 'b') && (s
[7] == 'a') &&
1758 (s
[8] == 's') && (s
[9] == 'e') &&
1760 retain
= NIL
; /* don't retain continuation */
1761 if ((nmsgs
== 1) && !stream
->uid_validity
) {
1762 /* advance to data */
1764 /* flush whitespace */
1765 while (*s
== ' ') s
++;
1766 j
= 0; /* slurp UID validity */
1767 /* found a digit? */
1768 while (isdigit (*s
)) {
1769 j
*= 10; /* yes, add it in */
1772 /* flush whitespace */
1773 while (*s
== ' ') s
++;
1774 /* must have valid UID validity and UID last */
1775 if (j
&& isdigit (*s
)) {
1776 /* pseudo-header seen if X-IMAP */
1777 if (m
) pseudoseen
= LOCAL
->pseudo
= T
;
1778 /* save UID validity */
1779 stream
->uid_validity
= j
;
1780 j
= 0; /* slurp UID last */
1781 while (isdigit (*s
)) {
1782 j
*= 10; /* yes, add it in */
1786 stream
->uid_last
= j
;
1787 /* process keywords */
1788 for (j
= 0; (*s
!= '\n') && ((*s
!= '\r')||(s
[1] != '\n'));
1790 /* flush leading whitespace */
1791 while (*s
== ' ') s
++;
1792 u
= strpbrk (s
," \n\r");
1793 /* got a keyword? */
1794 if ((j
< NUSERFLAGS
) && (k
= (u
- s
)) &&
1795 (k
<= MAXUSERFLAG
)) {
1796 if (stream
->user_flags
[j
])
1797 fs_give ((void **) &stream
->user_flags
[j
]);
1798 stream
->user_flags
[j
] = (char *) fs_get (k
+ 1);
1799 strncpy (stream
->user_flags
[j
],s
,k
);
1800 stream
->user_flags
[j
][k
] = '\0';
1808 /* possible X-UID */
1809 else if (s
[2] == 'U' && s
[3] == 'I' && s
[4] == 'D' &&
1811 retain
= NIL
; /* don't retain continuation */
1812 /* only believe if have a UID validity */
1813 if (stream
->uid_validity
&& ((nmsgs
> 1) || !pseudoseen
)) {
1814 s
+= 6; /* advance to UID value */
1815 /* flush whitespace */
1816 while (*s
== ' ') s
++;
1818 /* found a digit? */
1819 while (isdigit (*s
)) {
1820 j
*= 10; /* yes, add it in */
1823 /* flush remainder of line */
1824 while (*s
!= '\n') s
++;
1825 /* make sure not duplicated */
1826 if (elt
->private.uid
)
1827 sprintf (tmp
,"Message %lu UID %lu already has UID %lu",
1828 pseudoseen
? elt
->msgno
- 1 : elt
->msgno
,
1829 j
,elt
->private.uid
);
1830 /* make sure UID doesn't go backwards */
1831 else if (j
<= prevuid
)
1832 sprintf (tmp
,"Message %lu UID %lu less than %lu",
1833 pseudoseen
? elt
->msgno
- 1 : elt
->msgno
,
1835 #if 0 /* this is currently broken by UIDPLUS */
1836 /* or skip by mailbox's recorded last */
1837 else if (j
> stream
->uid_last
)
1838 sprintf (tmp
,"Message %lu UID %lu greater than last %lu",
1839 pseudoseen
? elt
->msgno
- 1 : elt
->msgno
,
1840 j
,stream
->uid_last
);
1842 else { /* normal UID case */
1843 prevuid
= elt
->private.uid
= j
;
1844 #if 1 /* temporary kludge for UIDPLUS */
1845 if (prevuid
> stream
->uid_last
) {
1846 stream
->uid_last
= prevuid
;
1847 LOCAL
->ddirty
= LOCAL
->dirty
= T
;
1850 break; /* exit this cruft */
1853 /* invalidate UID validity */
1854 stream
->uid_validity
= 0;
1855 elt
->private.uid
= 0;
1860 /* otherwise fall into S case */
1862 case 'S': /* possible Status: line */
1863 if (s
[0] == 'S' && s
[1] == 't' && s
[2] == 'a' && s
[3] == 't' &&
1864 s
[4] == 'u' && s
[5] == 's' && s
[6] == ':') {
1865 retain
= NIL
; /* don't retain continuation */
1866 s
+= 6; /* advance to status flags */
1867 do switch (*s
++) {/* parse flags */
1868 case 'R': /* message read */
1871 case 'O': /* message old */
1874 recent
--; /* it really wasn't recent */
1877 case 'D': /* message deleted */
1880 case 'F': /* message flagged */
1883 case 'A': /* message answered */
1886 case 'T': /* message is a draft */
1889 default: /* some other crap */
1891 } while (*s
&& (*s
!= '\n') && ((*s
!= '\r') || (s
[1] != '\n')));
1892 break; /* all done */
1894 /* otherwise fall into default case */
1896 default: /* ordinary header line */
1897 if ((*s
== 'S') || (*s
== 's') ||
1898 (((*s
== 'X') || (*s
== 'x')) && (s
[1] == '-'))) {
1899 unsigned char *e
,*v
;
1900 /* must match what mail_filter() does */
1901 for (u
= s
,v
= tmp
,e
= u
+ min (i
,MAILTMPLEN
- 1);
1902 (u
< e
) && ((c
= (*u
? *u
: (*u
= ' '))) != ':') &&
1903 ((c
> ' ') || ((c
!= ' ') && (c
!= '\t') &&
1904 (c
!= '\r') && (c
!= '\n')));
1906 *v
= '\0'; /* tie off */
1907 /* matches internal header? */
1908 if (!compare_cstring (tmp
,"STATUS") ||
1909 !compare_cstring (tmp
,"X-STATUS") ||
1910 !compare_cstring (tmp
,"X-KEYWORDS") ||
1911 !compare_cstring (tmp
,"X-UID") ||
1912 !compare_cstring (tmp
,"X-IMAP") ||
1913 !compare_cstring (tmp
,"X-IMAPBASE")) {
1914 char err
[MAILTMPLEN
];
1915 sprintf (err
,"Discarding bogus %s header in message %lu",
1916 (char *) tmp
,elt
->msgno
);
1918 retain
= NIL
; /* don't retain continuation */
1919 break; /* different case or something */
1922 /* retain or non-continuation? */
1923 if (retain
|| ((*s
!= ' ') && (*s
!= '\t'))) {
1924 retain
= T
; /* retaining continuation now */
1925 /* line length in LF format newline */
1926 for (j
= k
= 0; j
< i
; ++j
) if (s
[j
] != '\r') ++k
;
1927 /* "internal" header size */
1928 elt
->private.spare
.data
+= k
;
1930 elt
->rfc822_size
+= k
+ 1;
1933 char err
[MAILTMPLEN
];
1934 sprintf (err
,"Discarding bogus continuation in msg %lu: %.80s",
1935 elt
->msgno
,(char *) s
);
1936 if (u
= strpbrk (err
,"\r\n")) *u
= '\0';
1938 break; /* different case or something */
1942 } while (i
&& (*t
!= '\n') && ((*t
!= '\r') || (t
[1] != '\n')));
1943 /* "internal" header sans trailing newline */
1944 if (i
) elt
->private.spare
.data
--;
1945 /* assign a UID if none found */
1946 if (((nmsgs
> 1) || !pseudoseen
) && !elt
->private.uid
) {
1947 prevuid
= elt
->private.uid
= ++stream
->uid_last
;
1948 elt
->private.dirty
= T
;
1950 else elt
->private.dirty
= elt
->recent
;
1952 /* note size of header, location of text */
1953 elt
->private.msg
.header
.text
.size
=
1954 (elt
->private.msg
.text
.offset
=
1955 (LOCAL
->filesize
+ GETPOS (&bs
)) - elt
->private.special
.offset
) -
1956 elt
->private.special
.text
.size
;
1957 /* note current position */
1958 j
= LOCAL
->filesize
+ GETPOS (&bs
);
1959 if (i
) do { /* look for next message */
1960 s
= mmdf_mbxline (stream
,&bs
,&i
);
1961 if (i
) { /* got new data? */
1962 if (ISMMDF (s
)) break;
1963 else { /* not a header line, add it to message */
1964 elt
->rfc822_size
+= i
;
1965 for (j
= 0; j
< i
; ++j
) switch (s
[j
]) {
1966 case '\r': /* squeeze out CRs */
1967 elt
->rfc822_size
-= 1;
1969 case '\n': /* LF becomes CRLF */
1970 elt
->rfc822_size
+= 1;
1975 /* update current position */
1976 j
= LOCAL
->filesize
+ GETPOS (&bs
);
1979 } while (i
); /* until found a header */
1980 elt
->private.msg
.text
.text
.size
= j
-
1981 (elt
->private.special
.offset
+ elt
->private.msg
.text
.offset
);
1982 if (i
) { /* get next header line */
1983 /* remember first internal header position */
1984 j
= LOCAL
->filesize
+ GETPOS (&bs
);
1985 s
= mmdf_mbxline (stream
,&bs
,&i
);
1987 /* until end of buffer */
1988 } while (!stream
->sniff
&& i
);
1989 if (pseudoseen
) { /* flush pseudo-message if present */
1990 /* decrement recent count */
1991 if (mail_elt (stream
,1)->recent
) recent
--;
1992 /* and the exists count */
1993 mail_exists (stream
,nmsgs
--);
1994 mail_expunged(stream
,1);/* fake an expunge of that message */
1996 /* need to start a new UID validity? */
1997 if (!stream
->uid_validity
) {
1998 stream
->uid_validity
= time (0);
1999 /* in case a whiner with no life */
2000 if (mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
))
2001 stream
->uid_nosticky
= T
;
2002 else if (nmsgs
) { /* don't bother if empty file */
2003 /* make dirty to restart UID epoch */
2004 LOCAL
->ddirty
= LOCAL
->dirty
= T
;
2005 /* need to rewrite msg 1 if not pseudo */
2006 if (!LOCAL
->pseudo
) mail_elt (stream
,1)->private.dirty
= T
;
2007 MM_LOG ("Assigning new unique identifiers to all messages",NIL
);
2010 stream
->nmsgs
= oldnmsgs
; /* whack it back down */
2011 stream
->silent
= silent
; /* restore old silent setting */
2012 /* notify upper level of new mailbox sizes */
2013 mail_exists (stream
,nmsgs
);
2014 mail_recent (stream
,recent
);
2015 /* mark dirty so O flags are set */
2016 if (recent
) LOCAL
->dirty
= T
;
2019 /* no change, don't babble if never got time */
2020 else if (LOCAL
->filetime
&& LOCAL
->filetime
!= sbuf
.st_mtime
)
2021 MM_LOG ("New mailbox modification time but apparently no changes",WARN
);
2022 /* update parsed file size and time */
2023 LOCAL
->filesize
= sbuf
.st_size
;
2024 LOCAL
->filetime
= sbuf
.st_mtime
;
2025 return T
; /* return the winnage */
2028 /* MMDF read line from mailbox
2029 * Accepts: mail stream
2031 * pointer to line size
2032 * Returns: pointer to input line
2035 char *mmdf_mbxline (MAILSTREAM
*stream
,STRING
*bs
,unsigned long *size
)
2037 unsigned long i
,j
,k
,m
;
2040 /* flush old buffer */
2041 if (LOCAL
->line
) fs_give ((void **) &LOCAL
->line
);
2042 /* if buffer needs refreshing */
2043 if (!bs
->cursize
) SETPOS (bs
,GETPOS (bs
));
2044 if (SIZE (bs
)) { /* find newline */
2045 /* end of fast scan */
2046 te
= (t
= (s
= bs
->curpos
) + bs
->cursize
) - 12;
2047 while (s
< te
) if ((*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
2048 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
2049 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
2050 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n')) {
2052 break; /* exit loop */
2054 /* final character-at-a-time scan */
2055 while ((s
< t
) && (*s
!= '\n')) ++s
;
2056 /* difficult case if line spans buffer */
2057 if ((i
= s
- bs
->curpos
) == bs
->cursize
) {
2058 /* have space in line buffer? */
2059 if (i
> LOCAL
->linebuflen
) {
2060 fs_give ((void **) &LOCAL
->linebuf
);
2061 LOCAL
->linebuf
= (char *) fs_get (LOCAL
->linebuflen
= i
);
2063 /* remember what we have so far */
2064 memcpy (LOCAL
->linebuf
,bs
->curpos
,i
);
2065 /* load next buffer */
2066 SETPOS (bs
,k
= GETPOS (bs
) + i
);
2067 /* end of fast scan */
2068 te
= (t
= (s
= bs
->curpos
) + bs
->cursize
) - 12;
2069 /* fast scan in overlap buffer */
2070 while (s
< te
) if ((*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
2071 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
2072 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
2073 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n')) {
2075 break; /* exit loop */
2078 /* final character-at-a-time scan */
2079 while ((s
< t
) && (*s
!= '\n')) ++s
;
2081 if ((j
= s
- bs
->curpos
) == bs
->cursize
) {
2082 SETPOS (bs
,GETPOS (bs
) + j
);
2083 /* look for end of line (s-l-o-w!!) */
2084 for (m
= SIZE (bs
); m
&& (SNX (bs
) != '\n'); --m
,++j
);
2085 SETPOS (bs
,k
); /* go back to where it started */
2087 /* got size of data, make buffer for return */
2088 ret
= LOCAL
->line
= (char *) fs_get (i
+ j
+ 2);
2089 /* copy first chunk */
2090 memcpy (ret
,LOCAL
->linebuf
,i
);
2091 while (j
) { /* copy remainder */
2092 if (!bs
->cursize
) SETPOS (bs
,GETPOS (bs
));
2093 memcpy (ret
+ i
,bs
->curpos
,k
= min (j
,bs
->cursize
));
2094 i
+= k
; /* account for this much read in */
2096 bs
->curpos
+= k
; /* increment new position */
2097 bs
->cursize
-= k
; /* eat that many bytes */
2099 /* read newline at end */
2100 if (SIZE (bs
)) ret
[i
++] = SNX (bs
);
2101 ret
[i
] = '\0'; /* makes debugging easier */
2103 else { /* this is easy */
2104 ret
= bs
->curpos
; /* string it at this position */
2105 bs
->curpos
+= ++i
; /* increment new position */
2106 bs
->cursize
-= i
; /* eat that many bytes */
2108 *size
= i
; /* return that to user */
2110 else *size
= 0; /* end of data, return empty */
2111 /* embedded MMDF header at end of line? */
2112 if ((*size
> sizeof (MMDFHDRTXT
)) &&
2113 (s
= ret
+ *size
- (i
= sizeof (MMDFHDRTXT
) - 1)) && ISMMDF (s
)) {
2114 SETPOS (bs
,GETPOS (bs
) - i
);/* back up to start of MMDF header */
2115 *size
-= i
; /* reduce length of line */
2116 ret
[*size
- 1] = '\n'; /* force newline at end */
2121 /* MMDF make pseudo-header
2122 * Accepts: MAIL stream
2123 * buffer to write pseudo-header
2124 * Returns: length of pseudo-header
2127 unsigned long mmdf_pseudo (MAILSTREAM
*stream
,char *hdr
)
2130 char *s
,tmp
[MAILTMPLEN
];
2131 time_t now
= time (0);
2132 rfc822_fixed_date (tmp
);
2133 sprintf (hdr
,"%sFrom %s %.24s\nDate: %s\nFrom: %s <%s@%.80s>\nSubject: %s\nMessage-ID: <%lu@%.80s>\nX-IMAP: %010lu %010lu",
2134 mmdfhdr
,pseudo_from
,ctime (&now
),
2135 tmp
,pseudo_name
,pseudo_from
,mylocalhost (),pseudo_subject
,
2136 (unsigned long) now
,mylocalhost (),stream
->uid_validity
,
2138 for (s
= hdr
+ strlen (hdr
),i
= 0; i
< NUSERFLAGS
; ++i
)
2139 if (stream
->user_flags
[i
])
2140 sprintf (s
+= strlen (s
)," %s",stream
->user_flags
[i
]);
2141 sprintf (s
+= strlen (s
),"\nStatus: RO\n\n%s\n%s",pseudo_msg
,mmdfhdr
);
2142 return strlen (hdr
);
2145 /* MMDF make status string
2146 * Accepts: MAIL stream
2147 * destination string to write
2148 * message cache entry
2149 * UID to write if non-zero (else use elt->private.uid)
2150 * non-zero flag to write UID (.LT. 0 to write UID base info too)
2151 * Returns: length of string
2154 unsigned long mmdf_xstatus (MAILSTREAM
*stream
,char *status
,MESSAGECACHE
*elt
,
2155 unsigned long uid
,long flag
)
2161 int sticky
= uid
? T
: !stream
->uid_nosticky
;
2162 /* This used to use sprintf(), but thanks to certain cretinous C libraries
2163 with horribly slow implementations of sprintf() I had to change it to this
2164 mess. At least it should be fast. */
2165 if ((flag
< 0) && sticky
) { /* need to write X-IMAPbase: header? */
2166 *s
++ = 'X'; *s
++ = '-'; *s
++ = 'I'; *s
++ = 'M'; *s
++ = 'A'; *s
++ = 'P';
2167 *s
++ = 'b'; *s
++ = 'a'; *s
++ = 's'; *s
++ = 'e'; *s
++ = ':'; *s
++ = ' ';
2169 n
= stream
->uid_validity
; /* push UID validity digits on the stack */
2170 do *t
++ = (char) (n
% 10) + '0';
2172 /* pop UID validity digits from stack */
2173 while (t
> stack
) *s
++ = *--t
;
2175 n
= stream
->uid_last
; /* push UID last digits on the stack */
2176 do *t
++ = (char) (n
% 10) + '0';
2178 /* pop UID last digits from stack */
2179 while (t
> stack
) *s
++ = *--t
;
2180 for (n
= 0; n
< NUSERFLAGS
; ++n
) if (t
= stream
->user_flags
[n
])
2181 for (*s
++ = ' '; *t
; *s
++ = *t
++);
2183 pad
+= 30; /* increased padding if have IMAPbase */
2185 *s
++ = 'S'; *s
++ = 't'; *s
++ = 'a'; *s
++ = 't'; *s
++ = 'u'; *s
++ = 's';
2186 *s
++ = ':'; *s
++ = ' ';
2187 if (elt
->seen
) *s
++ = 'R';
2188 /* only write O if have a UID */
2189 if (flag
&& (!elt
->recent
|| !LOCAL
->appending
)) *s
++ = 'O';
2191 *s
++ = 'X'; *s
++ = '-'; *s
++ = 'S'; *s
++ = 't'; *s
++ = 'a'; *s
++ = 't';
2192 *s
++ = 'u'; *s
++ = 's'; *s
++ = ':'; *s
++ = ' ';
2193 if (elt
->deleted
) *s
++ = 'D';
2194 if (elt
->flagged
) *s
++ = 'F';
2195 if (elt
->answered
) *s
++ = 'A';
2196 if (elt
->draft
) *s
++ = 'T';
2199 if (sticky
) { /* only do this if UIDs sticky */
2200 *s
++ = 'X'; *s
++ = '-'; *s
++ = 'K'; *s
++ = 'e'; *s
++ = 'y'; *s
++ = 'w';
2201 *s
++ = 'o'; *s
++ = 'r'; *s
++ = 'd'; *s
++ = 's'; *s
++ = ':';
2202 if (n
= elt
->user_flags
) do {
2204 for (t
= stream
->user_flags
[find_rightmost_bit (&n
)]; *t
; *s
++ = *t
++);
2206 n
= s
- status
; /* get size of stuff so far */
2207 /* pad X-Keywords to make size constant */
2208 if (n
< pad
) for (n
= pad
- n
; n
> 0; --n
) *s
++ = ' ';
2210 if (flag
) { /* want to include UID? */
2212 /* push UID digits on the stack */
2213 n
= uid
? uid
: elt
->private.uid
;
2214 do *t
++ = (char) (n
% 10) + '0';
2216 *s
++ = 'X'; *s
++ = '-'; *s
++ = 'U'; *s
++ = 'I'; *s
++ = 'D'; *s
++ = ':';
2218 /* pop UID from stack */
2219 while (t
> stack
) *s
++ = *--t
;
2223 *s
++ = '\n'; *s
= '\0'; /* end of extended message status */
2224 return s
- status
; /* return size of resulting string */
2227 /* Rewrite mailbox file
2228 * Accepts: MAIL stream, must be critical and locked
2229 * return pointer to number of expunged messages if want expunge
2231 * expunge sequence, not deleted flag
2232 * Returns: T if success and mailbox unlocked, NIL if failure
2235 #define OVERFLOWBUFLEN 8192 /* initial overflow buffer length */
2237 long mmdf_rewrite (MAILSTREAM
*stream
,unsigned long *nexp
,DOTLOCK
*lock
,
2246 unsigned long recent
= stream
->recent
;
2247 unsigned long size
= LOCAL
->pseudo
? mmdf_pseudo (stream
,LOCAL
->buf
) : 0;
2248 if (nexp
) *nexp
= 0; /* initially nothing expunged */
2249 /* calculate size of mailbox after rewrite */
2250 for (i
= 1,flag
= LOCAL
->pseudo
? 1 : -1; i
<= stream
->nmsgs
; i
++) {
2251 elt
= mail_elt (stream
,i
); /* get cache */
2252 if (!(nexp
&& elt
->deleted
&& (flags
? elt
->sequence
: T
))) {
2253 /* add RFC822 size of this message */
2254 size
+= elt
->private.special
.text
.size
+ elt
->private.spare
.data
+
2255 mmdf_xstatus (stream
,LOCAL
->buf
,elt
,NIL
,flag
) +
2256 elt
->private.msg
.text
.text
.size
+ MMDFHDRLEN
;
2257 flag
= 1; /* only count X-IMAPbase once */
2260 /* no messages, has a life, and no pseudo */
2261 if (!size
&& !mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
)) {
2262 LOCAL
->pseudo
= T
; /* so make a pseudo-message now */
2263 size
= mmdf_pseudo (stream
,LOCAL
->buf
);
2265 /* extend the file as necessary */
2266 if (ret
= mmdf_extend (stream
,size
)) {
2267 /* Set up buffered I/O file structure
2268 * curpos current position being written through buffering
2269 * filepos current position being written physically to the disk
2270 * bufpos current position being written in the buffer
2271 * protect current maximum position that can be written to the disk
2272 * before buffering is forced
2273 * The code tries to buffer so that that disk is written in multiples of
2274 * OVERBLOWBUFLEN bytes.
2276 f
.stream
= stream
; /* note mail stream */
2277 f
.curpos
= f
.filepos
= 0; /* start of file */
2278 f
.protect
= stream
->nmsgs
? /* initial protection pointer */
2279 mail_elt (stream
,1)->private.special
.offset
: 8192;
2280 f
.bufpos
= f
.buf
= (char *) fs_get (f
.buflen
= OVERFLOWBUFLEN
);
2282 if (LOCAL
->pseudo
) /* update pseudo-header */
2283 mmdf_write (&f
,LOCAL
->buf
,mmdf_pseudo (stream
,LOCAL
->buf
));
2284 /* loop through all messages */
2285 for (i
= 1,flag
= LOCAL
->pseudo
? 1 : -1; i
<= stream
->nmsgs
;) {
2286 elt
= mail_elt (stream
,i
);/* get cache */
2287 /* expunge this message? */
2288 if (nexp
&& elt
->deleted
&& (flags
? elt
->sequence
: T
)) {
2289 /* one less recent message */
2290 if (elt
->recent
) --recent
;
2291 mail_expunged(stream
,i
);/* notify upper levels */
2292 ++*nexp
; /* count up one more expunged message */
2294 else { /* preserve this message */
2295 i
++; /* advance to next message */
2296 if ((flag
< 0) || /* need to rewrite message? */
2297 elt
->private.dirty
|| (f
.curpos
!= elt
->private.special
.offset
) ||
2298 (elt
->private.msg
.header
.text
.size
!=
2299 (elt
->private.spare
.data
+
2300 mmdf_xstatus (stream
,LOCAL
->buf
,elt
,NIL
,flag
)))) {
2301 unsigned long newoffset
= f
.curpos
;
2302 /* yes, seek to internal header */
2303 lseek (LOCAL
->fd
,elt
->private.special
.offset
,L_SET
);
2304 read (LOCAL
->fd
,LOCAL
->buf
,elt
->private.special
.text
.size
);
2305 /* see if need to squeeze out a CR */
2306 if (LOCAL
->buf
[elt
->private.special
.text
.size
- 2] == '\r') {
2307 LOCAL
->buf
[--elt
->private.special
.text
.size
- 1] = '\n';
2308 --size
; /* squeezed out a CR from PC */
2310 /* protection pointer moves to RFC822 header */
2311 f
.protect
= elt
->private.special
.offset
+
2312 elt
->private.msg
.header
.offset
;
2313 /* write internal header */
2314 mmdf_write (&f
,LOCAL
->buf
,elt
->private.special
.text
.size
);
2315 /* get RFC822 header */
2316 s
= mmdf_header (stream
,elt
->msgno
,&j
,FT_INTERNAL
);
2317 /* in case this got decremented */
2318 elt
->private.msg
.header
.offset
= elt
->private.special
.text
.size
;
2319 /* header size, sans trailing newline */
2320 if ((j
< 2) || (s
[j
- 2] == '\n')) j
--;
2321 /* this can happen if CRs were squeezed */
2322 if (j
< elt
->private.spare
.data
) {
2323 /* so fix up counts */
2324 size
-= elt
->private.spare
.data
- j
;
2325 elt
->private.spare
.data
= j
;
2327 else if (j
!= elt
->private.spare
.data
)
2328 fatal ("header size inconsistent");
2329 /* protection pointer moves to RFC822 text */
2330 f
.protect
= elt
->private.special
.offset
+
2331 elt
->private.msg
.text
.offset
;
2332 mmdf_write (&f
,s
,j
); /* write RFC822 header */
2333 /* write status and UID */
2334 mmdf_write (&f
,LOCAL
->buf
,
2335 j
= mmdf_xstatus (stream
,LOCAL
->buf
,elt
,NIL
,flag
));
2336 flag
= 1; /* only write X-IMAPbase once */
2337 /* new file header size */
2338 elt
->private.msg
.header
.text
.size
= elt
->private.spare
.data
+ j
;
2340 /* did text move? */
2341 if (f
.curpos
!= f
.protect
) {
2342 /* get message text */
2343 s
= mmdf_text_work (stream
,elt
,&j
,FT_INTERNAL
);
2344 /* this can happen if CRs were squeezed */
2345 if (j
< elt
->private.msg
.text
.text
.size
) {
2346 /* so fix up counts */
2347 size
-= elt
->private.msg
.text
.text
.size
- j
;
2348 elt
->private.msg
.text
.text
.size
= j
;
2350 /* can't happen it says here */
2351 else if (j
> elt
->private.msg
.text
.text
.size
)
2352 fatal ("text size inconsistent");
2353 /* new text offset, status/UID may change it */
2354 elt
->private.msg
.text
.offset
= f
.curpos
- newoffset
;
2355 /* protection pointer moves to next message */
2356 f
.protect
= (i
<= stream
->nmsgs
) ?
2357 mail_elt (stream
,i
)->private.special
.offset
:
2358 (f
.curpos
+ j
+ MMDFHDRLEN
);
2359 mmdf_write (&f
,s
,j
);/* write text */
2360 /* write trailing newline */
2361 mmdf_write (&f
,mmdfhdr
,MMDFHDRLEN
);
2363 else { /* tie off header and status */
2364 mmdf_write (&f
,NIL
,NIL
);
2365 f
.curpos
= f
.protect
=/* restart everything at end of message */
2366 f
.filepos
+= elt
->private.msg
.text
.text
.size
+ MMDFHDRLEN
;
2368 /* new internal header offset */
2369 elt
->private.special
.offset
= newoffset
;
2370 elt
->private.dirty
=NIL
;/* message is now clean */
2372 else { /* no need to rewrite this message */
2373 /* tie off previous message if needed */
2374 mmdf_write (&f
,NIL
,NIL
);
2375 f
.curpos
= f
.protect
=/* restart everything at end of message */
2376 f
.filepos
+= elt
->private.special
.text
.size
+
2377 elt
->private.msg
.header
.text
.size
+
2378 elt
->private.msg
.text
.text
.size
+ MMDFHDRLEN
;
2383 mmdf_write (&f
,NIL
,NIL
); /* tie off final message */
2384 if (size
!= f
.filepos
) fatal ("file size inconsistent");
2385 fs_give ((void **) &f
.buf
); /* free buffer */
2386 /* make sure tied off */
2387 ftruncate (LOCAL
->fd
,LOCAL
->filesize
= size
);
2388 fsync (LOCAL
->fd
); /* make sure the updates take */
2389 if (size
&& (flag
< 0)) fatal ("lost UID base information");
2390 /* no longer dirty */
2391 LOCAL
->ddirty
= LOCAL
->dirty
= NIL
;
2392 /* notify upper level of new mailbox sizes */
2393 mail_exists (stream
,stream
->nmsgs
);
2394 mail_recent (stream
,recent
);
2395 /* set atime to now, mtime a second earlier */
2396 tp
[1] = (tp
[0] = time (0)) - 1;
2397 /* set the times, note change */
2398 if (!utime (stream
->mailbox
,tp
)) LOCAL
->filetime
= tp
[1];
2399 close (LOCAL
->fd
); /* close and reopen file */
2400 if ((LOCAL
->fd
= open (stream
->mailbox
,O_RDWR
,
2401 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
)))
2403 sprintf (LOCAL
->buf
,"Mailbox open failed, aborted: %s",strerror (errno
));
2404 MM_LOG (LOCAL
->buf
,ERROR
);
2405 mmdf_abort (stream
);
2407 dotlock_unlock (lock
); /* flush the lock file */
2409 return ret
; /* return state from algorithm */
2412 /* Extend MMDF mailbox file
2413 * Accepts: MAIL stream
2415 * Return: T if success, else NIL
2418 long mmdf_extend (MAILSTREAM
*stream
,unsigned long size
)
2420 unsigned long i
= (size
> LOCAL
->filesize
) ? size
- LOCAL
->filesize
: 0;
2421 if (i
) { /* does the mailbox need to grow? */
2422 if (i
> LOCAL
->buflen
) { /* make sure have enough space */
2423 /* this user won the lottery all right */
2424 fs_give ((void **) &LOCAL
->buf
);
2425 LOCAL
->buf
= (char *) fs_get ((LOCAL
->buflen
= i
) + 1);
2427 memset (LOCAL
->buf
,'\0',i
); /* get a block of nulls */
2428 while (T
) { /* until write successful or punt */
2429 lseek (LOCAL
->fd
,LOCAL
->filesize
,L_SET
);
2430 if ((write (LOCAL
->fd
,LOCAL
->buf
,i
) >= 0) && !fsync (LOCAL
->fd
)) break;
2432 long e
= errno
; /* note error before doing ftruncate */
2433 ftruncate (LOCAL
->fd
,LOCAL
->filesize
);
2434 if (MM_DISKERROR (stream
,e
,NIL
)) {
2435 fsync (LOCAL
->fd
); /* user chose to punt */
2436 sprintf (LOCAL
->buf
,"Unable to extend mailbox: %s",strerror (e
));
2437 if (!stream
->silent
) MM_LOG (LOCAL
->buf
,ERROR
);
2446 /* Write data to buffered file
2447 * Accepts: buffered file pointer
2448 * file data or NIL to indicate "flush buffer"
2449 * date size (ignored for "flush buffer")
2450 * Does not return until success
2453 void mmdf_write (MMDFFILE
*f
,char *buf
,unsigned long size
)
2455 unsigned long i
,j
,k
;
2456 if (buf
) { /* doing buffered write? */
2457 i
= f
->bufpos
- f
->buf
; /* yes, get size of current buffer data */
2458 /* yes, have space in current buffer chunk? */
2459 if (j
= i
? ((f
->buflen
- i
) % OVERFLOWBUFLEN
) : f
->buflen
) {
2460 /* yes, fill up buffer as much as we can */
2461 memcpy (f
->bufpos
,buf
,k
= min (j
,size
));
2462 f
->bufpos
+= k
; /* new buffer position */
2463 f
->curpos
+= k
; /* new current position */
2464 if (j
-= k
) return; /* all done if still have buffer free space */
2465 buf
+= k
; /* full, get new unwritten data pointer */
2466 size
-= k
; /* new data size */
2467 i
+= k
; /* new buffer data size */
2469 /* This chunk of the buffer is full. See if can make some space by
2470 * writing to the disk, if there's enough unprotected space to do so.
2471 * Try to fill out any unaligned chunk, along with any subsequent full
2472 * chunks that will fit in unprotected space.
2474 /* any unprotected space we can write to? */
2475 if (j
= min (i
,f
->protect
- f
->filepos
)) {
2476 /* yes, filepos not at chunk boundary? */
2477 if ((k
= f
->filepos
% OVERFLOWBUFLEN
) && ((k
= OVERFLOWBUFLEN
- k
) < j
))
2478 j
-= k
; /* yes, and can write out partial chunk */
2479 else k
= 0; /* no partial chunk to write */
2480 /* if at least a chunk free, write that too */
2481 if (j
> OVERFLOWBUFLEN
) k
+= j
- (j
% OVERFLOWBUFLEN
);
2482 if (k
) { /* write data if there is anything we can */
2483 mmdf_phys_write (f
,f
->buf
,k
);
2485 if (i
-= k
) memmove (f
->buf
,f
->buf
+ k
,i
);
2486 f
->bufpos
= f
->buf
+ i
; /* new end of buffer */
2490 /* Have flushed the buffer as best as possible. All done if no more
2491 * data to write. Otherwise, if the buffer is empty AND if the unwritten
2492 * data is larger than a chunk AND the unprotected space is also larger
2493 * than a chunk, then write as many chunks as we can directly from the
2494 * data. Buffer the rest, expanding the buffer as needed.
2496 if (size
) { /* have more data that we need to buffer? */
2497 /* can write any of it to disk instead? */
2498 if ((f
->bufpos
== f
->buf
) &&
2499 ((j
= min (f
->protect
- f
->filepos
,size
)) > OVERFLOWBUFLEN
)) {
2500 /* write as much as we can right now */
2501 mmdf_phys_write (f
,buf
,j
-= (j
% OVERFLOWBUFLEN
));
2502 buf
+= j
; /* new data pointer */
2503 size
-= j
; /* new data size */
2504 f
->curpos
+= j
; /* advance current pointer */
2506 if (size
) { /* still have data that we need to buffer? */
2507 /* yes, need to expand the buffer? */
2508 if ((i
= ((f
->bufpos
+ size
) - f
->buf
)) > f
->buflen
) {
2509 /* note current position in buffer */
2510 j
= f
->bufpos
- f
->buf
;
2511 i
+= OVERFLOWBUFLEN
; /* yes, grow another chunk */
2512 fs_resize ((void **) &f
->buf
,f
->buflen
= i
- (i
% OVERFLOWBUFLEN
));
2513 /* in case buffer relocated */
2514 f
->bufpos
= f
->buf
+ j
;
2516 /* buffer remaining data */
2517 memcpy (f
->bufpos
,buf
,size
);
2518 f
->bufpos
+= size
; /* new end of buffer */
2519 f
->curpos
+= size
; /* advance current pointer */
2523 else { /* flush buffer to disk */
2524 mmdf_phys_write (f
,f
->buf
,i
= f
->bufpos
- f
->buf
);
2525 f
->bufpos
= f
->buf
; /* reset buffer */
2526 /* update positions */
2527 f
->curpos
= f
->protect
= f
->filepos
;
2531 /* Physical disk write
2532 * Accepts: buffered file pointer
2535 * Does not return until success
2538 void mmdf_phys_write (MMDFFILE
*f
,char *buf
,size_t size
)
2540 MAILSTREAM
*stream
= f
->stream
;
2541 /* write data at desired position */
2542 while (size
&& ((lseek (LOCAL
->fd
,f
->filepos
,L_SET
) < 0) ||
2543 (write (LOCAL
->fd
,buf
,size
) < 0))) {
2545 char tmp
[MAILTMPLEN
];
2546 sprintf (tmp
,"Unable to write to mailbox: %s",strerror (e
= errno
));
2548 MM_DISKERROR (NIL
,e
,T
); /* serious problem, must retry */
2550 f
->filepos
+= size
; /* update file position */