imapext-2007

view src/osdep/unix/unix.c @ 1:28a55bc1110c

[mq]: imapext
author yuuji@gentei.org
date Mon, 14 Sep 2009 19:23:11 +0900
parents ada5e610ab86
children 2366b362676d
line source
1 /* ========================================================================
2 * Copyright 1988-2008 University of Washington
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: UNIX mail routines
16 *
17 * Author: Mark Crispin
18 * UW Technology
19 * University of Washington
20 * Seattle, WA 98195
21 * Internet: MRC@Washington.EDU
22 *
23 * Date: 20 December 1989
24 * Last Edited: 27 March 2008
25 */
28 /* DEDICATION
29 *
30 * This file is dedicated to my dog, Unix, also known as Yun-chan and
31 * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast. Unix
32 * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
33 * a two-month bout with cirrhosis of the liver.
34 *
35 * He was a dear friend, and I miss him terribly.
36 *
37 * Lift a leg, Yunie. Luv ya forever!!!!
38 */
40 #include <stdio.h>
41 #include <ctype.h>
42 #include <errno.h>
43 extern int errno; /* just in case */
44 #include <signal.h>
45 #include "mail.h"
46 #include "osdep.h"
47 #include <time.h>
48 #include <sys/stat.h>
49 #include "unix.h"
50 #include "pseudo.h"
51 #include "fdstring.h"
52 #include "misc.h"
53 #include "dummy.h"
55 /* UNIX I/O stream local data */
57 typedef struct unix_local {
58 unsigned int dirty : 1; /* disk copy needs updating */
59 unsigned int ddirty : 1; /* double-dirty, ping becomes checkpoint */
60 unsigned int pseudo : 1; /* uses a pseudo message */
61 unsigned int appending : 1; /* don't mark new messages as old */
62 int fd; /* mailbox file descriptor */
63 int ld; /* lock file descriptor */
64 char *lname; /* lock file name */
65 off_t filesize; /* file size parsed */
66 time_t filetime; /* last file time */
67 time_t lastsnarf; /* last snarf time (for mbox driver) */
68 unsigned char *buf; /* temporary buffer */
69 unsigned long buflen; /* current size of temporary buffer */
70 unsigned long uid; /* current text uid */
71 SIZEDTEXT text; /* current text */
72 unsigned long textlen; /* current text length */
73 char *line; /* returned line */
74 char *linebuf; /* line readin buffer */
75 unsigned long linebuflen; /* current line readin buffer length */
76 } UNIXLOCAL;
79 /* Convenient access to local data */
81 #define LOCAL ((UNIXLOCAL *) stream->local)
84 /* UNIX protected file structure */
86 typedef struct unix_file {
87 MAILSTREAM *stream; /* current stream */
88 off_t curpos; /* current file position */
89 off_t protect; /* protected position */
90 off_t filepos; /* current last written file position */
91 char *buf; /* overflow buffer */
92 size_t buflen; /* current overflow buffer length */
93 char *bufpos; /* current buffer position */
94 } UNIXFILE;
96 /* Function prototypes */
98 DRIVER *unix_valid (char *name);
99 long unix_isvalid_fd (int fd);
100 void *unix_parameters (long function,void *value);
101 void unix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
102 void unix_list (MAILSTREAM *stream,char *ref,char *pat);
103 void unix_lsub (MAILSTREAM *stream,char *ref,char *pat);
104 long unix_create (MAILSTREAM *stream,char *mailbox);
105 long unix_delete (MAILSTREAM *stream,char *mailbox);
106 long unix_rename (MAILSTREAM *stream,char *old,char *newname);
107 MAILSTREAM *unix_open (MAILSTREAM *stream);
108 void unix_close (MAILSTREAM *stream,long options);
109 char *unix_header (MAILSTREAM *stream,unsigned long msgno,
110 unsigned long *length,long flags);
111 long unix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
112 char *unix_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
113 unsigned long *length,long flags);
114 void unix_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
115 long unix_ping (MAILSTREAM *stream);
116 void unix_check (MAILSTREAM *stream);
117 long unix_expunge (MAILSTREAM *stream,char *sequence,long options);
118 long unix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
119 long unix_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
120 int unix_collect_msg (MAILSTREAM *stream,FILE *sf,char *flags,char *date,
121 STRING *msg);
122 int unix_append_msgs (MAILSTREAM *stream,FILE *sf,FILE *df,SEARCHSET *set);
124 void unix_abort (MAILSTREAM *stream);
125 char *unix_file (char *dst,char *name);
126 int unix_lock (char *file,int flags,int mode,DOTLOCK *lock,int op);
127 void unix_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock);
128 int unix_parse (MAILSTREAM *stream,DOTLOCK *lock,int op);
129 char *unix_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size);
130 unsigned long unix_pseudo (MAILSTREAM *stream,char *hdr);
131 unsigned long unix_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
132 unsigned long uid,long flag);
133 long unix_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
134 long flags);
135 long unix_extend (MAILSTREAM *stream,unsigned long size);
136 void unix_write (UNIXFILE *f,char *s,unsigned long i);
137 void unix_phys_write (UNIXFILE *f,char *buf,size_t size);
139 /* mbox mail routines */
141 /* Function prototypes */
143 DRIVER *mbox_valid (char *name);
144 long mbox_create (MAILSTREAM *stream,char *mailbox);
145 long mbox_delete (MAILSTREAM *stream,char *mailbox);
146 long mbox_rename (MAILSTREAM *stream,char *old,char *newname);
147 long mbox_status (MAILSTREAM *stream,char *mbx,long flags);
148 MAILSTREAM *mbox_open (MAILSTREAM *stream);
149 long mbox_ping (MAILSTREAM *stream);
150 void mbox_check (MAILSTREAM *stream);
151 long mbox_expunge (MAILSTREAM *stream,char *sequence,long options);
152 long mbox_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
155 /* UNIX mail routines */
158 /* Driver dispatch used by MAIL */
160 DRIVER unixdriver = {
161 "unix", /* driver name */
162 /* driver flags */
163 DR_LOCAL|DR_MAIL|DR_LOCKING|DR_NONEWMAILRONLY|DR_XPOINT,
164 (DRIVER *) NIL, /* next driver */
165 unix_valid, /* mailbox is valid for us */
166 unix_parameters, /* manipulate parameters */
167 unix_scan, /* scan mailboxes */
168 unix_list, /* list mailboxes */
169 unix_lsub, /* list subscribed mailboxes */
170 NIL, /* subscribe to mailbox */
171 NIL, /* unsubscribe from mailbox */
172 unix_create, /* create mailbox */
173 unix_delete, /* delete mailbox */
174 unix_rename, /* rename mailbox */
175 mail_status_default, /* status of mailbox */
176 unix_open, /* open mailbox */
177 unix_close, /* close mailbox */
178 NIL, /* fetch message "fast" attributes */
179 NIL, /* fetch message flags */
180 NIL, /* fetch overview */
181 NIL, /* fetch message envelopes */
182 unix_header, /* fetch message header */
183 unix_text, /* fetch message text */
184 NIL, /* fetch partial message text */
185 NIL, /* unique identifier */
186 NIL, /* message number */
187 NIL, /* modify flags */
188 unix_flagmsg, /* per-message modify flags */
189 NIL, /* search for message based on criteria */
190 NIL, /* sort messages */
191 NIL, /* thread messages */
192 unix_ping, /* ping mailbox to see if still alive */
193 unix_check, /* check for new messages */
194 unix_expunge, /* expunge deleted messages */
195 unix_copy, /* copy messages to another mailbox */
196 unix_append, /* append string message to mailbox */
197 NIL /* garbage collect stream */
198 };
200 /* prototype stream */
201 MAILSTREAM unixproto = {&unixdriver};
203 /* driver parameters */
204 static long unix_fromwidget = T;
205 char *myMailSuffix =NIL; /* home directory name */
207 /* UNIX mail validate mailbox
208 * Accepts: mailbox name
209 * Returns: our driver if name is valid, NIL otherwise
210 */
212 DRIVER *unix_valid (char *name)
213 {
214 int fd;
215 DRIVER *ret = NIL;
216 char *t,file[MAILTMPLEN];
217 struct stat sbuf;
218 time_t tp[2];
219 errno = EINVAL; /* assume invalid argument */
220 /* must be non-empty file */
221 if ((t = dummy_file (file,name)) && !stat (t,&sbuf)) {
222 if (!sbuf.st_size)errno = 0;/* empty file */
223 else if ((fd = open (file,O_RDONLY,NIL)) >= 0) {
224 /* OK if mailbox format good */
225 if (unix_isvalid_fd (fd)) ret = &unixdriver;
226 else errno = -1; /* invalid format */
227 close (fd); /* close the file */
228 /* \Marked status? */
229 if ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) {
230 tp[0] = sbuf.st_atime; /* yes, preserve atime and mtime */
231 tp[1] = sbuf.st_mtime;
232 utime (file,tp); /* set the times */
233 }
234 }
235 }
236 return ret; /* return what we should */
237 }
239 /* UNIX mail test for valid mailbox
240 * Accepts: file descriptor
241 * scratch buffer
242 * Returns: T if valid, NIL otherwise
243 */
245 long unix_isvalid_fd (int fd)
246 {
247 int zn;
248 int ret = NIL;
249 char tmp[MAILTMPLEN],*s,*t,c = '\n';
250 memset (tmp,'\0',MAILTMPLEN);
251 if (read (fd,tmp,MAILTMPLEN-1) >= 0) {
252 for (s = tmp; (*s == '\r') || (*s == '\n') || (*s == ' ') || (*s == '\t');)
253 c = *s++;
254 if (c == '\n') VALID (s,t,ret,zn);
255 }
256 return ret; /* return what we should */
257 }
260 /* UNIX manipulate driver parameters
261 * Accepts: function code
262 * function-dependent value
263 * Returns: function-dependent return value
264 */
266 void *unix_parameters (long function,void *value)
267 {
268 void *ret = NIL;
269 switch ((int) function) {
270 case GET_INBOXPATH:
271 if (value) ret = dummy_file ((char *) value,"INBOX");
272 break;
273 case SET_FROMWIDGET:
274 unix_fromwidget = (long) value;
275 case GET_FROMWIDGET:
276 ret = (void *) unix_fromwidget;
277 break;
278 }
279 return ret;
280 }
282 /* UNIX mail scan mailboxes
283 * Accepts: mail stream
284 * reference
285 * pattern to search
286 * string to scan
287 */
289 void unix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
290 {
291 if (stream) dummy_scan (NIL,ref,pat,contents);
292 }
295 /* UNIX mail list mailboxes
296 * Accepts: mail stream
297 * reference
298 * pattern to search
299 */
301 void unix_list (MAILSTREAM *stream,char *ref,char *pat)
302 {
303 if (stream) dummy_list (NIL,ref,pat);
304 }
307 /* UNIX mail list subscribed mailboxes
308 * Accepts: mail stream
309 * reference
310 * pattern to search
311 */
313 void unix_lsub (MAILSTREAM *stream,char *ref,char *pat)
314 {
315 if (stream) dummy_lsub (NIL,ref,pat);
316 }
318 /* UNIX mail create mailbox
319 * Accepts: MAIL stream
320 * mailbox name to create
321 * Returns: T on success, NIL on failure
322 */
324 long unix_create (MAILSTREAM *stream,char *mailbox)
325 {
326 char *s,mbx[MAILTMPLEN],tmp[MAILTMPLEN];
327 long ret = NIL;
328 int i,fd;
329 time_t ti = time (0);
330 if (!(s = dummy_file (mbx,mailbox))) {
331 sprintf (tmp,"Can't create %.80s: invalid name",mailbox);
332 MM_LOG (tmp,ERROR);
333 }
334 /* create underlying file */
335 else if (dummy_create_path (stream,s,get_dir_protection (mailbox))) {
336 /* done if dir-only or whiner */
337 if (((s = strrchr (s,'/')) && !s[1]) ||
338 mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) ret = T;
339 else if ((fd = open (mbx,O_WRONLY,
340 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) < 0) {
341 sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno));
342 MM_LOG (tmp,ERROR);
343 unlink (mbx); /* delete the file */
344 }
345 else { /* initialize header */
346 memset (tmp,'\0',MAILTMPLEN);
347 sprintf (tmp,"From %s %sDate: ",pseudo_from,ctime (&ti));
348 rfc822_fixed_date (s = tmp + strlen (tmp));
349 /* write the pseudo-header */
350 sprintf (s += strlen (s),
351 "\nFrom: %s <%s@%s>\nSubject: %s\nX-IMAP: %010lu 0000000000",
352 pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,
353 (unsigned long) ti);
354 for (i = 0; i < NUSERFLAGS; ++i) if (default_user_flag (i))
355 sprintf (s += strlen (s)," %s",default_user_flag (i));
356 sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n\n",pseudo_msg);
357 if (write (fd,tmp,strlen (tmp)) > 0) ret = T;
358 else {
359 sprintf (tmp,"Can't initialize mailbox node %.80s: %s",mbx,
360 strerror (errno));
361 MM_LOG (tmp,ERROR);
362 unlink (mbx); /* delete the file */
363 }
364 close (fd); /* close file */
365 }
366 }
367 /* set proper protections */
368 return ret ? set_mbx_protections (mailbox,mbx) : NIL;
369 }
371 /* UNIX mail delete mailbox
372 * Accepts: MAIL stream
373 * mailbox name to delete
374 * Returns: T on success, NIL on failure
375 */
377 long unix_delete (MAILSTREAM *stream,char *mailbox)
378 {
379 return unix_rename (stream,mailbox,NIL);
380 }
383 /* UNIX mail rename mailbox
384 * Accepts: MAIL stream
385 * old mailbox name
386 * new mailbox name (or NIL for delete)
387 * Returns: T on success, NIL on failure
388 */
390 long unix_rename (MAILSTREAM *stream,char *old,char *newname)
391 {
392 long ret = NIL;
393 char c,*s = NIL;
394 char tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
395 DOTLOCK lockx;
396 int fd,ld;
397 long i;
398 struct stat sbuf;
399 MM_CRITICAL (stream); /* get the c-client lock */
400 if (!dummy_file (file,old) ||
401 (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
402 ((s = strrchr (tmp,'/')) && !s[1]))))
403 sprintf (tmp,newname ?
404 "Can't rename mailbox %.80s to %.80s: invalid name" :
405 "Can't delete mailbox %.80s: invalid name",
406 old,newname);
407 /* lock out other c-clients */
408 else if ((ld = lockname (lock,file,LOCK_EX|LOCK_NB,&i)) < 0)
409 sprintf (tmp,"Mailbox %.80s is in use by another process",old);
411 else {
412 if ((fd = unix_lock (file,O_RDWR,
413 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
414 &lockx,LOCK_EX)) < 0)
415 sprintf (tmp,"Can't lock mailbox %.80s: %s",old,strerror (errno));
416 else {
417 if (newname) { /* want rename? */
418 /* found superior to destination name? */
419 if (s = strrchr (s,'/')) {
420 c = *++s; /* remember first character of inferior */
421 *s = '\0'; /* tie off to get just superior */
422 /* name doesn't exist, create it */
423 if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
424 !dummy_create_path (stream,tmp,get_dir_protection (newname))) {
425 unix_unlock (fd,NIL,&lockx);
426 unix_unlock (ld,NIL,NIL);
427 unlink (lock);
428 MM_NOCRITICAL (stream);
429 return ret; /* return success or failure */
430 }
431 *s = c; /* restore full name */
432 }
433 if (rename (file,tmp))
434 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
435 strerror (errno));
436 else ret = T; /* set success */
437 }
438 else if (unlink (file))
439 sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
440 else ret = T; /* set success */
441 unix_unlock (fd,NIL,&lockx);
442 }
443 unix_unlock (ld,NIL,NIL); /* flush the lock */
444 unlink (lock);
445 }
446 MM_NOCRITICAL (stream); /* no longer critical */
447 if (!ret) MM_LOG (tmp,ERROR); /* log error */
448 return ret; /* return success or failure */
449 }
451 /* UNIX mail open
452 * Accepts: Stream to open
453 * Returns: Stream on success, NIL on failure
454 */
456 MAILSTREAM *unix_open (MAILSTREAM *stream)
457 {
458 long i;
459 int fd;
460 char tmp[MAILTMPLEN];
461 DOTLOCK lock;
462 long retry;
463 /* return prototype for OP_PROTOTYPE call */
464 if (!stream) return user_flags (&unixproto);
465 retry = stream->silent ? 1 : KODRETRY;
466 if (stream->local) fatal ("unix recycle stream");
467 stream->local = memset (fs_get (sizeof (UNIXLOCAL)),0,sizeof (UNIXLOCAL));
468 /* note if an INBOX or not */
469 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
470 /* canonicalize the stream mailbox name */
471 if (!dummy_file (tmp,stream->mailbox)) {
472 sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
473 MM_LOG (tmp,ERROR);
474 return NIL;
475 }
476 /* flush old name */
477 fs_give ((void **) &stream->mailbox);
478 /* save canonical name */
479 stream->mailbox = cpystr (tmp);
480 LOCAL->fd = LOCAL->ld = -1; /* no file or state locking yet */
481 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
482 LOCAL->buflen = CHUNKSIZE - 1;
483 LOCAL->text.data = (unsigned char *) fs_get (CHUNKSIZE);
484 LOCAL->text.size = CHUNKSIZE - 1;
485 LOCAL->linebuf = (char *) fs_get (CHUNKSIZE);
486 LOCAL->linebuflen = CHUNKSIZE - 1;
487 stream->sequence++; /* bump sequence number */
489 /* make lock for read/write access */
490 if (!stream->rdonly) while (retry) {
491 /* try to lock file */
492 if ((fd = lockname (tmp,stream->mailbox,LOCK_EX|LOCK_NB,&i)) < 0) {
493 /* suppressing kiss-of-death? */
494 if (stream->nokod) retry = 0;
495 /* no, first time through? */
496 else if (retry-- == KODRETRY) {
497 /* learned other guy's PID and can signal? */
498 if (i && !kill ((int) i,SIGUSR2)) {
499 sprintf (tmp,"Trying to get mailbox lock from process %ld",i);
500 MM_LOG (tmp,WARN);
501 }
502 else retry = 0; /* give up */
503 }
504 if (!stream->silent) { /* nothing if silent stream */
505 if (retry) sleep (1); /* wait a second before trying again */
506 else MM_LOG ("Mailbox is open by another process, access is readonly",
507 WARN);
508 }
509 }
510 else { /* got the lock, nobody else can alter state */
511 LOCAL->ld = fd; /* note lock's fd and name */
512 LOCAL->lname = cpystr (tmp);
513 /* make sure mode OK (don't use fchmod()) */
514 chmod (LOCAL->lname,(long) mail_parameters (NIL,GET_LOCKPROTECTION,NIL));
515 if (stream->silent) i = 0;/* silent streams won't accept KOD */
516 else { /* note our PID in the lock */
517 sprintf (tmp,"%d",getpid ());
518 write (fd,tmp,(i = strlen (tmp))+1);
519 }
520 ftruncate (fd,i); /* make sure tied off */
521 fsync (fd); /* make sure it's available */
522 retry = 0; /* no more need to try */
523 }
524 }
526 /* parse mailbox */
527 stream->nmsgs = stream->recent = 0;
528 /* will we be able to get write access? */
529 if ((LOCAL->ld >= 0) && access (stream->mailbox,W_OK) && (errno == EACCES)) {
530 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);
531 flock (LOCAL->ld,LOCK_UN); /* release the lock */
532 close (LOCAL->ld); /* close the lock file */
533 LOCAL->ld = -1; /* no more lock fd */
534 unlink (LOCAL->lname); /* delete it */
535 }
536 /* reset UID validity */
537 stream->uid_validity = stream->uid_last = 0;
538 if (stream->silent && !stream->rdonly && (LOCAL->ld < 0))
539 unix_abort (stream); /* abort if can't get RW silent stream */
540 /* parse mailbox */
541 else if (unix_parse (stream,&lock,LOCK_SH)) {
542 unix_unlock (LOCAL->fd,stream,&lock);
543 mail_unlock (stream);
544 MM_NOCRITICAL (stream); /* done with critical */
545 }
546 if (!LOCAL) return NIL; /* failure if stream died */
547 /* make sure upper level knows readonly */
548 stream->rdonly = (LOCAL->ld < 0);
549 /* notify about empty mailbox */
550 if (!(stream->nmsgs || stream->silent)) MM_LOG ("Mailbox is empty",NIL);
551 if (!stream->rdonly) { /* flags stick if readwrite */
552 stream->perm_seen = stream->perm_deleted =
553 stream->perm_flagged = stream->perm_answered = stream->perm_draft = T;
554 if (!stream->uid_nosticky) {/* users with lives get permanent keywords */
555 stream->perm_user_flags = 0xffffffff;
556 /* and maybe can create them too! */
557 stream->kwd_create = stream->user_flags[NUSERFLAGS-1] ? NIL : T;
558 }
559 }
560 return stream; /* return stream alive to caller */
561 }
564 /* UNIX mail close
565 * Accepts: MAIL stream
566 * close options
567 */
569 void unix_close (MAILSTREAM *stream,long options)
570 {
571 int silent = stream->silent;
572 stream->silent = T; /* go silent */
573 /* expunge if requested */
574 if (options & CL_EXPUNGE) unix_expunge (stream,NIL,NIL);
575 /* else dump final checkpoint */
576 else if (LOCAL->dirty) unix_check (stream);
577 stream->silent = silent; /* restore old silence state */
578 unix_abort (stream); /* now punt the file and local data */
579 }
581 /* UNIX mail fetch message header
582 * Accepts: MAIL stream
583 * message # to fetch
584 * pointer to returned header text length
585 * option flags
586 * Returns: message header in RFC822 format
587 */
589 /* lines to filter from header */
590 static STRINGLIST *unix_hlines = NIL;
592 char *unix_header (MAILSTREAM *stream,unsigned long msgno,
593 unsigned long *length,long flags)
594 {
595 MESSAGECACHE *elt;
596 unsigned char *s,*t,*tl;
597 *length = 0; /* default to empty */
598 if (flags & FT_UID) return "";/* UID call "impossible" */
599 elt = mail_elt (stream,msgno);/* get cache */
600 if (!unix_hlines) { /* once only code */
601 STRINGLIST *lines = unix_hlines = mail_newstringlist ();
602 lines->text.size = strlen ((char *) (lines->text.data =
603 (unsigned char *) "Status"));
604 lines = lines->next = mail_newstringlist ();
605 lines->text.size = strlen ((char *) (lines->text.data =
606 (unsigned char *) "X-Status"));
607 lines = lines->next = mail_newstringlist ();
608 lines->text.size = strlen ((char *) (lines->text.data =
609 (unsigned char *) "X-Keywords"));
610 lines = lines->next = mail_newstringlist ();
611 lines->text.size = strlen ((char *) (lines->text.data =
612 (unsigned char *) "X-UID"));
613 lines = lines->next = mail_newstringlist ();
614 lines->text.size = strlen ((char *) (lines->text.data =
615 (unsigned char *) "X-IMAP"));
616 lines = lines->next = mail_newstringlist ();
617 lines->text.size = strlen ((char *) (lines->text.data =
618 (unsigned char *) "X-IMAPbase"));
619 }
620 /* go to header position */
621 lseek (LOCAL->fd,elt->private.special.offset +
622 elt->private.msg.header.offset,L_SET);
624 if (flags & FT_INTERNAL) { /* initial data OK? */
625 if (elt->private.msg.header.text.size > LOCAL->buflen) {
626 fs_give ((void **) &LOCAL->buf);
627 LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
628 elt->private.msg.header.text.size) + 1);
629 }
630 /* read message */
631 read (LOCAL->fd,LOCAL->buf,elt->private.msg.header.text.size);
632 /* got text, tie off string */
633 LOCAL->buf[*length = elt->private.msg.header.text.size] = '\0';
634 /* squeeze out CRs (in case from PC) */
635 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
636 if (*t != '\r') *s++ = *t;
637 *s = '\0';
638 *length = s - LOCAL->buf; /* adjust length */
639 }
640 else { /* need to make a CRLF version */
641 read (LOCAL->fd,s = (char *) fs_get (elt->private.msg.header.text.size+1),
642 elt->private.msg.header.text.size);
643 /* tie off string, and convert to CRLF */
644 s[elt->private.msg.header.text.size] = '\0';
645 *length = strcrlfcpy (&LOCAL->buf,&LOCAL->buflen,s,
646 elt->private.msg.header.text.size);
647 fs_give ((void **) &s); /* free readin buffer */
648 /* squeeze out spurious CRs */
649 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
650 if ((*t != '\r') || (t[1] == '\n')) *s++ = *t;
651 *s = '\0';
652 *length = s - LOCAL->buf; /* adjust length */
653 }
654 *length = mail_filter (LOCAL->buf,*length,unix_hlines,FT_NOT);
655 return (char *) LOCAL->buf; /* return processed copy */
656 }
658 /* UNIX mail fetch message text
659 * Accepts: MAIL stream
660 * message # to fetch
661 * pointer to returned stringstruct
662 * option flags
663 * Returns: T on success, NIL if failure
664 */
666 long unix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
667 {
668 char *s;
669 unsigned long i;
670 MESSAGECACHE *elt;
671 /* UID call "impossible" */
672 if (flags & FT_UID) return NIL;
673 elt = mail_elt (stream,msgno);/* get cache element */
674 /* if message not seen */
675 if (!(flags & FT_PEEK) && !elt->seen) {
676 /* mark message seen and dirty */
677 elt->seen = elt->private.dirty = LOCAL->dirty = T;
678 MM_FLAGS (stream,msgno);
679 }
680 s = unix_text_work (stream,elt,&i,flags);
681 INIT (bs,mail_string,s,i); /* set up stringstruct */
682 return T; /* success */
683 }
685 /* UNIX mail fetch message text worker routine
686 * Accepts: MAIL stream
687 * message cache element
688 * pointer to returned header text length
689 * option flags
690 */
692 char *unix_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
693 unsigned long *length,long flags)
694 {
695 FDDATA d;
696 STRING bs;
697 unsigned char c,*s,*t,*tl,tmp[CHUNKSIZE];
698 /* go to text position */
699 lseek (LOCAL->fd,elt->private.special.offset +
700 elt->private.msg.text.offset,L_SET);
701 if (flags & FT_INTERNAL) { /* initial data OK? */
702 if (elt->private.msg.text.text.size > LOCAL->buflen) {
703 fs_give ((void **) &LOCAL->buf);
704 LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
705 elt->private.msg.text.text.size) + 1);
706 }
707 /* read message */
708 read (LOCAL->fd,LOCAL->buf,elt->private.msg.text.text.size);
709 /* got text, tie off string */
710 LOCAL->buf[*length = elt->private.msg.text.text.size] = '\0';
711 /* squeeze out CRs (in case from PC) */
712 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
713 if (*t != '\r') *s++ = *t;
714 *s = '\0';
715 *length = s - LOCAL->buf; /* adjust length */
716 return (char *) LOCAL->buf;
717 }
719 /* have it cached already? */
720 if (elt->private.uid != LOCAL->uid) {
721 /* not cached, cache it now */
722 LOCAL->uid = elt->private.uid;
723 /* is buffer big enough? */
724 if (elt->rfc822_size > LOCAL->text.size) {
725 /* excessively conservative, but the right thing is too hard to do */
726 fs_give ((void **) &LOCAL->text.data);
727 LOCAL->text.data = (unsigned char *)
728 fs_get ((LOCAL->text.size = elt->rfc822_size) + 1);
729 }
730 d.fd = LOCAL->fd; /* yes, set up file descriptor */
731 d.pos = elt->private.special.offset + elt->private.msg.text.offset;
732 d.chunk = tmp; /* initial buffer chunk */
733 d.chunksize = CHUNKSIZE; /* file chunk size */
734 INIT (&bs,fd_string,&d,elt->private.msg.text.text.size);
735 for (s = (char *) LOCAL->text.data; SIZE (&bs);) switch (c = SNX (&bs)) {
736 case '\r': /* carriage return seen */
737 break;
738 case '\n':
739 *s++ = '\r'; /* insert a CR */
740 default:
741 *s++ = c; /* copy characters */
742 }
743 *s = '\0'; /* tie off buffer */
744 /* calculate length of cached data */
745 LOCAL->textlen = s - LOCAL->text.data;
746 }
747 *length = LOCAL->textlen; /* return from cache */
748 return (char *) LOCAL->text.data;
749 }
751 /* UNIX per-message modify flag
752 * Accepts: MAIL stream
753 * message cache element
754 */
756 void unix_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
757 {
758 /* only after finishing */
759 if (elt->valid) elt->private.dirty = LOCAL->dirty = T;
760 }
763 /* UNIX mail ping mailbox
764 * Accepts: MAIL stream
765 * Returns: T if stream alive, else NIL
766 */
768 long unix_ping (MAILSTREAM *stream)
769 {
770 DOTLOCK lock;
771 struct stat sbuf;
772 long reparse;
773 /* big no-op if not readwrite */
774 if (LOCAL && (LOCAL->ld >= 0) && !stream->lock) {
775 if (stream->rdonly) { /* does he want to give up readwrite? */
776 /* checkpoint if we changed something */
777 if (LOCAL->dirty) unix_check (stream);
778 flock (LOCAL->ld,LOCK_UN);/* release readwrite lock */
779 close (LOCAL->ld); /* close the readwrite lock file */
780 LOCAL->ld = -1; /* no more readwrite lock fd */
781 unlink (LOCAL->lname); /* delete the readwrite lock file */
782 }
783 else { /* see if need to reparse */
784 if (!(reparse = (long) mail_parameters (NIL,GET_NETFSSTATBUG,NIL))) {
785 /* get current mailbox size */
786 if (LOCAL->fd >= 0) fstat (LOCAL->fd,&sbuf);
787 else if (stat (stream->mailbox,&sbuf)) {
788 sprintf (LOCAL->buf,"Mailbox stat failed, aborted: %s",
789 strerror (errno));
790 MM_LOG (LOCAL->buf,ERROR);
791 unix_abort (stream);
792 return NIL;
793 }
794 reparse = (sbuf.st_size != LOCAL->filesize);
795 }
796 /* parse if mailbox changed */
797 if ((LOCAL->ddirty || reparse) && unix_parse (stream,&lock,LOCK_EX)) {
798 /* force checkpoint if double-dirty */
799 if (LOCAL->ddirty) unix_rewrite (stream,NIL,&lock,NIL);
800 /* unlock mailbox */
801 else unix_unlock (LOCAL->fd,stream,&lock);
802 mail_unlock (stream); /* and stream */
803 MM_NOCRITICAL (stream); /* done with critical */
804 }
805 }
806 }
807 return LOCAL ? LONGT : NIL; /* return if still alive */
808 }
810 /* UNIX mail check mailbox
811 * Accepts: MAIL stream
812 */
814 void unix_check (MAILSTREAM *stream)
815 {
816 DOTLOCK lock;
817 /* parse and lock mailbox */
818 if (LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
819 unix_parse (stream,&lock,LOCK_EX)) {
820 /* any unsaved changes? */
821 if (LOCAL->dirty && unix_rewrite (stream,NIL,&lock,NIL)) {
822 if (!stream->silent) MM_LOG ("Checkpoint completed",NIL);
823 }
824 /* no checkpoint needed, just unlock */
825 else unix_unlock (LOCAL->fd,stream,&lock);
826 mail_unlock (stream); /* unlock the stream */
827 MM_NOCRITICAL (stream); /* done with critical */
828 }
829 }
832 /* UNIX mail expunge mailbox
833 * Accepts: MAIL stream
834 * sequence to expunge if non-NIL
835 * expunge options
836 * Returns: T, always
837 */
839 long unix_expunge (MAILSTREAM *stream,char *sequence,long options)
840 {
841 long ret;
842 unsigned long i;
843 DOTLOCK lock;
844 char *msg = NIL;
845 /* parse and lock mailbox */
846 if (ret = (sequence ? ((options & EX_UID) ?
847 mail_uid_sequence (stream,sequence) :
848 mail_sequence (stream,sequence)) : LONGT) &&
849 LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
850 unix_parse (stream,&lock,LOCK_EX)) {
851 /* check expunged messages if not dirty */
852 for (i = 1; !LOCAL->dirty && (i <= stream->nmsgs); i++) {
853 MESSAGECACHE *elt = mail_elt (stream,i);
854 if (mail_elt (stream,i)->deleted) LOCAL->dirty = T;
855 }
856 if (!LOCAL->dirty) { /* not dirty and no expunged messages */
857 unix_unlock (LOCAL->fd,stream,&lock);
858 msg = "No messages deleted, so no update needed";
859 }
860 else if (unix_rewrite (stream,&i,&lock,sequence ? LONGT : NIL)) {
861 if (i) sprintf (msg = LOCAL->buf,"Expunged %lu messages",i);
862 else msg = "Mailbox checkpointed, but no messages expunged";
863 }
864 /* rewrite failed */
865 else unix_unlock (LOCAL->fd,stream,&lock);
866 mail_unlock (stream); /* unlock the stream */
867 MM_NOCRITICAL (stream); /* done with critical */
868 if (msg && !stream->silent) MM_LOG (msg,NIL);
869 }
870 else if (!stream->silent) MM_LOG("Expunge ignored on readonly mailbox",WARN);
871 return ret;
872 }
874 /* UNIX mail copy message(s)
875 * Accepts: MAIL stream
876 * sequence
877 * destination mailbox
878 * copy options
879 * Returns: T if copy successful, else NIL
880 */
882 long unix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
883 {
884 struct stat sbuf;
885 int fd;
886 char *s,file[MAILTMPLEN];
887 DOTLOCK lock;
888 time_t tp[2];
889 unsigned long i,j;
890 MESSAGECACHE *elt;
891 long ret = T;
892 mailproxycopy_t pc =
893 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
894 copyuid_t cu = (copyuid_t) (mail_parameters (NIL,GET_USERHASNOLIFE,NIL) ?
895 NIL : mail_parameters (NIL,GET_COPYUID,NIL));
896 SEARCHSET *source = cu ? mail_newsearchset () : NIL;
897 SEARCHSET *dest = cu ? mail_newsearchset () : NIL;
898 MAILSTREAM *tstream = NIL;
899 DRIVER *d;
900 for (d = (DRIVER *) mail_parameters (NIL,GET_DRIVERS,NIL);
901 (d && strcmp (d->name,"mbox") && !(d->flags & DR_DISABLE));
902 d = d->next); /* see if mbox driver active */
903 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
904 mail_sequence (stream,sequence))) return NIL;
905 /* make sure destination is valid */
906 if (!((d && mbox_valid (mailbox) && (mailbox = "mbox")) ||
907 unix_valid (mailbox) || !errno))
908 switch (errno) {
909 case ENOENT: /* no such file? */
910 if (compare_cstring (mailbox,"INBOX")) {
911 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
912 return NIL;
913 }
914 if (pc) return (*pc) (stream,sequence,mailbox,options);
915 unix_create (NIL,"INBOX");/* create empty INBOX */
916 case EACCES: /* file protected */
917 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
918 MM_LOG (LOCAL->buf,ERROR);
919 return NIL;
920 case EINVAL:
921 if (pc) return (*pc) (stream,sequence,mailbox,options);
922 sprintf (LOCAL->buf,"Invalid UNIX-format mailbox name: %.80s",mailbox);
923 MM_LOG (LOCAL->buf,ERROR);
924 return NIL;
925 default:
926 if (pc) return (*pc) (stream,sequence,mailbox,options);
927 sprintf (LOCAL->buf,"Not a UNIX-format mailbox: %.80s",mailbox);
928 MM_LOG (LOCAL->buf,ERROR);
929 return NIL;
930 }
932 /* try to open rewrite for UIDPLUS */
933 if ((tstream = mail_open_work (&unixdriver,NIL,mailbox,
934 OP_SILENT|OP_NOKOD)) && tstream->rdonly)
935 tstream = mail_close (tstream);
936 if (cu && !tstream) { /* wanted a COPYUID? */
937 sprintf (LOCAL->buf,"Unable to write-open mailbox for COPYUID: %.80s",
938 mailbox);
939 MM_LOG (LOCAL->buf,WARN);
940 cu = NIL; /* don't try to do COPYUID */
941 }
942 LOCAL->buf[0] = '\0';
943 MM_CRITICAL (stream); /* go critical */
944 if ((fd = unix_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND,
945 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
946 &lock,LOCK_EX)) < 0) {
947 MM_NOCRITICAL (stream); /* done with critical */
948 sprintf (LOCAL->buf,"Can't open destination mailbox: %s",strerror (errno));
949 MM_LOG (LOCAL->buf,ERROR);/* log the error */
950 return NIL; /* failed */
951 }
952 fstat (fd,&sbuf); /* get current file size */
953 /* write all requested messages to mailbox */
954 for (i = 1; ret && (i <= stream->nmsgs); i++)
955 if ((elt = mail_elt (stream,i))->sequence) {
956 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
957 read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
958 if (write (fd,LOCAL->buf,elt->private.special.text.size) < 0) ret = NIL;
959 else { /* internal header succeeded */
960 s = unix_header (stream,i,&j,FT_INTERNAL);
961 /* header size, sans trailing newline */
962 if (j && (s[j - 2] == '\n')) j--;
963 if (write (fd,s,j) < 0) ret = NIL;
964 else { /* message header succeeded */
965 j = tstream ? /* write UIDPLUS data if have readwrite */
966 unix_xstatus (stream,LOCAL->buf,elt,++(tstream->uid_last),LONGT) :
967 unix_xstatus (stream,LOCAL->buf,elt,NIL,NIL);
968 if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
969 else { /* message status succeeded */
970 s = unix_text_work (stream,elt,&j,FT_INTERNAL);
971 if ((write (fd,s,j) < 0) || (write (fd,"\n",1) < 0)) ret = NIL;
972 else if (cu) { /* need to pass back new UID? */
973 mail_append_set (source,mail_uid (stream,i));
974 mail_append_set (dest,tstream->uid_last);
975 }
976 }
977 }
978 }
979 }
981 if (!ret || fsync (fd)) { /* force out the update */
982 sprintf (LOCAL->buf,"Message copy failed: %s",strerror (errno));
983 ftruncate (fd,sbuf.st_size);
984 ret = NIL;
985 }
986 /* force UIDVALIDITY assignment now */
987 if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
988 /* return sets if doing COPYUID */
989 if (cu && ret) (*cu) (stream,mailbox,tstream->uid_validity,source,dest);
990 else { /* flush any sets we may have built */
991 mail_free_searchset (&source);
992 mail_free_searchset (&dest);
993 }
994 tp[1] = time (0); /* set mtime to now */
995 if (ret) tp[0] = tp[1] - 1; /* set atime to now-1 if successful copy */
996 else tp[0] = /* else preserve \Marked status */
997 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
998 sbuf.st_atime : tp[1];
999 utime (file,tp); /* set the times */
1000 unix_unlock (fd,NIL,&lock); /* unlock and close mailbox */
1001 if (tstream) { /* update last UID if we can */
1002 UNIXLOCAL *local = (UNIXLOCAL *) tstream->local;
1003 local->dirty = T; /* do a rewrite */
1004 local->appending = T; /* but not at the cost of marking as old */
1005 tstream = mail_close (tstream);
1007 /* log the error */
1008 if (!ret) MM_LOG (LOCAL->buf,ERROR);
1009 /* delete if requested message */
1010 else if (options & CP_MOVE) for (i = 1; i <= stream->nmsgs; i++)
1011 if ((elt = mail_elt (stream,i))->sequence)
1012 elt->deleted = elt->private.dirty = LOCAL->dirty = T;
1013 MM_NOCRITICAL (stream); /* release critical */
1014 return ret;
1017 /* UNIX mail append message from stringstruct
1018 * Accepts: MAIL stream
1019 * destination mailbox
1020 * append callback
1021 * data for callback
1022 * Returns: T if append successful, else NIL
1023 */
1025 #define BUFLEN 8*MAILTMPLEN
1027 long unix_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
1029 struct stat sbuf;
1030 int fd;
1031 unsigned long i;
1032 char *flags,*date,buf[BUFLEN],tmp[MAILTMPLEN],file[MAILTMPLEN];
1033 time_t tp[2];
1034 FILE *sf,*df;
1035 MESSAGECACHE elt;
1036 DOTLOCK lock;
1037 STRING *message;
1038 unsigned long uidlocation = 0;
1039 appenduid_t au = (appenduid_t)
1040 (mail_parameters (NIL,GET_USERHASNOLIFE,NIL) ? NIL :
1041 mail_parameters (NIL,GET_APPENDUID,NIL));
1042 SEARCHSET *dst = au ? mail_newsearchset () : NIL;
1043 long ret = LONGT;
1044 MAILSTREAM *tstream = NIL;
1045 if (!stream) { /* stream specified? */
1046 stream = &unixproto; /* no, default stream to prototype */
1047 for (i = 0; i < NUSERFLAGS && stream->user_flags[i]; ++i)
1048 fs_give ((void **) &stream->user_flags[i]);
1050 if (!unix_valid (mailbox)) switch (errno) {
1051 case ENOENT: /* no such file? */
1052 if (compare_cstring (mailbox,"INBOX")) {
1053 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
1054 return NIL;
1056 unix_create (NIL,"INBOX"); /* create empty INBOX */
1057 case 0: /* merely empty file? */
1058 tstream = stream;
1059 break;
1060 case EACCES: /* file protected */
1061 sprintf (tmp,"Can't access destination: %.80s",mailbox);
1062 MM_LOG (tmp,ERROR);
1063 return NIL;
1064 case EINVAL:
1065 sprintf (tmp,"Invalid UNIX-format mailbox name: %.80s",mailbox);
1066 MM_LOG (tmp,ERROR);
1067 return NIL;
1068 default:
1069 sprintf (tmp,"Not a UNIX-format mailbox: %.80s",mailbox);
1070 MM_LOG (tmp,ERROR);
1071 return NIL;
1073 /* get sniffing stream for keywords */
1074 else if (!(tstream = mail_open (NIL,mailbox,
1075 OP_READONLY|OP_SILENT|OP_NOKOD|OP_SNIFF))) {
1076 sprintf (tmp,"Unable to examine mailbox for APPEND: %.80s",mailbox);
1077 MM_LOG (tmp,ERROR);
1078 return NIL;
1081 /* get first message */
1082 if (!MM_APPEND (af) (tstream,data,&flags,&date,&message)) return NIL;
1083 if (!(sf = tmpfile ())) { /* must have scratch file */
1084 sprintf (tmp,".%lx.%lx",(unsigned long) time (0),(unsigned long)getpid ());
1085 if (!stat (tmp,&sbuf) || !(sf = fopen (tmp,"wb+"))) {
1086 sprintf (tmp,"Unable to create scratch file: %.80s",strerror (errno));
1087 MM_LOG (tmp,ERROR);
1088 return NIL;
1090 unlink (tmp);
1092 do { /* parse date */
1093 if (!date) rfc822_date (date = tmp);
1094 if (!mail_parse_date (&elt,date)) {
1095 sprintf (tmp,"Bad date in append: %.80s",date);
1096 MM_LOG (tmp,ERROR);
1098 else { /* user wants to suppress time zones? */
1099 if (mail_parameters (NIL,GET_NOTIMEZONES,NIL)) {
1100 time_t when = mail_longdate (&elt);
1101 date = ctime (&when); /* use traditional date */
1103 /* use POSIX-style date */
1104 else date = mail_cdate (tmp,&elt);
1105 if (!SIZE (message)) MM_LOG ("Append of zero-length message",ERROR);
1106 else if (!unix_collect_msg (tstream,sf,flags,date,message)) {
1107 sprintf (tmp,"Error writing scratch file: %.80s",strerror (errno));
1108 MM_LOG (tmp,ERROR);
1110 /* get next message */
1111 else if (MM_APPEND (af) (tstream,data,&flags,&date,&message)) continue;
1113 fclose (sf); /* punt scratch file */
1114 return NIL; /* give up */
1115 } while (message); /* until no more messages */
1116 if (fflush (sf)) {
1117 sprintf (tmp,"Error finishing scratch file: %.80s",strerror (errno));
1118 MM_LOG (tmp,ERROR);
1119 fclose (sf); /* punt scratch file */
1120 return NIL; /* give up */
1122 i = ftell (sf); /* size of scratch file */
1123 /* close sniffing stream */
1124 if (tstream != stream) tstream = mail_close (tstream);
1126 MM_CRITICAL (stream); /* go critical */
1127 /* try to open readwrite for UIDPLUS */
1128 if ((tstream = mail_open_work (&unixdriver,NIL,mailbox,
1129 OP_SILENT|OP_NOKOD)) && tstream->rdonly)
1130 tstream = mail_close (tstream);
1131 if (au && !tstream) { /* wanted an APPENDUID? */
1132 sprintf (tmp,"Unable to re-open mailbox for APPENDUID: %.80s",mailbox);
1133 MM_LOG (tmp,WARN);
1134 au = NIL;
1136 if (((fd = unix_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND,
1137 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
1138 &lock,LOCK_EX)) < 0) ||
1139 !(df = fdopen (fd,"ab"))) {
1140 MM_NOCRITICAL (stream); /* done with critical */
1141 sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
1142 MM_LOG (tmp,ERROR);
1143 return NIL;
1145 fstat (fd,&sbuf); /* get current file size */
1146 rewind (sf);
1147 tp[1] = time (0); /* set mtime to now */
1148 /* write all messages */
1149 if (!unix_append_msgs (tstream,sf,df,au ? dst : NIL) ||
1150 (fflush (df) == EOF) || fsync (fd)) {
1151 sprintf (buf,"Message append failed: %s",strerror (errno));
1152 MM_LOG (buf,ERROR);
1153 ftruncate (fd,sbuf.st_size);
1154 tp[0] = /* preserve \Marked status */
1155 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
1156 sbuf.st_atime : tp[1];
1157 ret = NIL; /* return error */
1159 else tp[0] = tp[1] - 1; /* set atime to now-1 if successful copy */
1160 utime (file,tp); /* set the times */
1161 fclose (sf); /* done with scratch file */
1162 /* force UIDVALIDITY assignment now */
1163 if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
1164 /* return sets if doing APPENDUID */
1165 if (au && ret) (*au) (mailbox,tstream->uid_validity,dst);
1166 else mail_free_searchset (&dst);
1167 unix_unlock (fd,NIL,&lock); /* unlock and close mailbox */
1168 fclose (df); /* note that unix_unlock() released the fd */
1169 if (tstream) { /* update last UID if we can */
1170 UNIXLOCAL *local = (UNIXLOCAL *) tstream->local;
1171 local->dirty = T; /* do a rewrite */
1172 local->appending = T; /* but not at the cost of marking as old */
1173 tstream = mail_close (tstream);
1175 MM_NOCRITICAL (stream); /* release critical */
1176 return ret;
1179 /* Collect and write single message to append scratch file
1180 * Accepts: MAIL stream
1181 * scratch file
1182 * flags
1183 * date
1184 * message stringstruct
1185 * Returns: NIL if write error, else T
1186 */
1188 int unix_collect_msg (MAILSTREAM *stream,FILE *sf,char *flags,char *date,
1189 STRING *msg)
1191 unsigned char *s,*t;
1192 unsigned long uf;
1193 long f = mail_parse_flags (stream,flags,&uf);
1194 /* write metadata, note date ends with NL */
1195 if (fprintf (sf,"%ld %lu %s",f,SIZE (msg) + 1,date) < 0) return NIL;
1196 while (uf) /* write user flags */
1197 if ((s = stream->user_flags[find_rightmost_bit (&uf)]) &&
1198 (fprintf (sf," %s",s) < 0)) return NIL;
1199 if (putc ('\n',sf) == EOF) return NIL;
1200 while (SIZE (msg)) { /* copy text to scratch file */
1201 for (s = (unsigned char *) msg->curpos, t = s + msg->cursize; s < t; ++s)
1202 if (!*s) *s = 0x80; /* disallow NUL */
1203 /* write buffered text */
1204 if (fwrite (msg->curpos,1,msg->cursize,sf) == msg->cursize)
1205 SETPOS (msg,GETPOS (msg) + msg->cursize);
1206 else return NIL; /* failed */
1208 /* write trailing newline and return */
1209 return (putc ('\n',sf) == EOF) ? NIL : T;
1212 /* Append messages from scratch file to mailbox
1213 * Accepts: MAIL stream
1214 * source file
1215 * destination file
1216 * uidset to update if non-NIL
1217 * Returns: T if success, NIL if failure
1218 */
1220 int unix_append_msgs (MAILSTREAM *stream,FILE *sf,FILE *df,SEARCHSET *set)
1222 int ti,zn,c;
1223 long f;
1224 unsigned long i,j;
1225 char *x,tmp[MAILTMPLEN];
1226 int hdrp = T;
1227 /* get message metadata line */
1228 while (fgets (tmp,MAILTMPLEN,sf)) {
1229 if (!(isdigit (tmp[0]) && strchr (tmp,'\n'))) return NIL;
1230 f = strtol (tmp,&x,10); /* get flags */
1231 if (!((*x++ == ' ') && isdigit (*x))) return NIL;
1232 i = strtoul (x,&x,10); /* get message size */
1233 if ((*x++ != ' ') || /* build initial header */
1234 (fprintf (df,"From %s@%s %sStatus: ",myusername(),mylocalhost(),x)<0)||
1235 (f&fSEEN && (putc ('R',df) == EOF)) ||
1236 (fputs ("\nX-Status: ",df) == EOF) ||
1237 (f&fDELETED && (putc ('D',df) == EOF)) ||
1238 (f&fFLAGGED && (putc ('F',df) == EOF)) ||
1239 (f&fANSWERED && (putc ('A',df) == EOF)) ||
1240 (f&fDRAFT && (putc ('T',df) == EOF)) ||
1241 (fputs ("\nX-Keywords:",df) == EOF)) return NIL;
1242 /* copy keywords */
1243 while ((c = getc (sf)) != '\n') switch (c) {
1244 case EOF:
1245 return NIL;
1246 default:
1247 if (putc (c,df) == EOF) return NIL;
1249 if ((putc ('\n',df) == EOF) ||
1250 (set && (fprintf (df,"X-UID: %lu\n",++(stream->uid_last)) < 0)))
1251 return NIL;
1253 for (c = '\n'; i && fgets (tmp,MAILTMPLEN,sf); c = tmp[j-1]) {
1254 /* get read line length */
1255 if (i < (j = strlen (tmp))) fatal ("unix_append_msgs overrun");
1256 i -= j; /* number of bytes left */
1257 /* squish out CRs (note also copies NUL) */
1258 for (x = tmp; x = strchr (x,'\r'); --j) memmove (x,x+1,j-(x-tmp));
1259 if (!j) continue; /* do nothing if line emptied */
1260 /* start of line? */
1261 if ((c == '\n')) switch (tmp[0]) {
1262 case 'F': /* possible "From " (case counts here) */
1263 if ((j > 4) && (tmp[0] == 'F') && (tmp[1] == 'r') && (tmp[2] == 'o') &&
1264 (tmp[3] == 'm') && (tmp[4] == ' ')) {
1265 if (!unix_fromwidget) {
1266 VALID (tmp,x,ti,zn);/* conditional, only write widget if */
1267 if (!ti) break; /* it looks like a valid header */
1268 } /* write the widget */
1269 if (putc ('>',df) == EOF) return NIL;
1271 break;
1272 case 'S': case 's': /* possible "Status:" */
1273 if (hdrp && (j > 6) && ((tmp[1] == 't') || (tmp[1] == 'T')) &&
1274 ((tmp[2] == 'a') || (tmp[2] == 'A')) &&
1275 ((tmp[3] == 't') || (tmp[3] == 'T')) &&
1276 ((tmp[4] == 'u') || (tmp[4] == 'U')) &&
1277 ((tmp[5] == 's') || (tmp[5] == 'S')) && (tmp[6] == ':') &&
1278 (fputs ("X-Original-",df) == EOF)) return NIL;
1279 break;
1280 case 'X': case 'x': /* possible X-??? header */
1281 if (hdrp && (tmp[1] == '-') &&
1282 /* possible X-UID: */
1283 (((j > 5) && ((tmp[2] == 'U') || (tmp[2] == 'u')) &&
1284 ((tmp[3] == 'I') || (tmp[3] == 'i')) &&
1285 ((tmp[4] == 'D') || (tmp[4] == 'd')) && (tmp[5] == ':')) ||
1286 /* possible X-IMAP: */
1287 ((j > 6) && ((tmp[2] == 'I') || (tmp[2] == 'i')) &&
1288 ((tmp[3] == 'M') || (tmp[3] == 'm')) &&
1289 ((tmp[4] == 'A') || (tmp[4] == 'a')) &&
1290 ((tmp[5] == 'P') || (tmp[5] == 'p')) &&
1291 ((tmp[6] == ':') ||
1292 /* or X-IMAPbase: */
1293 ((j > 10) && ((tmp[6] == 'b') || (tmp[6] == 'B')) &&
1294 ((tmp[7] == 'a') || (tmp[7] == 'A')) &&
1295 ((tmp[8] == 's') || (tmp[8] == 'S')) &&
1296 ((tmp[9] == 'e') || (tmp[9] == 'E')) && (tmp[10] == ':')))) ||
1297 /* possible X-Status: */
1298 ((j > 8) && ((tmp[2] == 'S') || (tmp[2] == 's')) &&
1299 ((tmp[3] == 't') || (tmp[3] == 'T')) &&
1300 ((tmp[4] == 'a') || (tmp[4] == 'A')) &&
1301 ((tmp[5] == 't') || (tmp[5] == 'T')) &&
1302 ((tmp[6] == 'u') || (tmp[6] == 'U')) &&
1303 ((tmp[7] == 's') || (tmp[7] == 'S')) && (tmp[8] == ':')) ||
1304 /* possible X-Keywords: */
1305 ((j > 10) && ((tmp[2] == 'K') || (tmp[2] == 'k')) &&
1306 ((tmp[3] == 'e') || (tmp[3] == 'E')) &&
1307 ((tmp[4] == 'y') || (tmp[4] == 'Y')) &&
1308 ((tmp[5] == 'w') || (tmp[5] == 'W')) &&
1309 ((tmp[6] == 'o') || (tmp[6] == 'O')) &&
1310 ((tmp[7] == 'r') || (tmp[7] == 'R')) &&
1311 ((tmp[8] == 'd') || (tmp[8] == 'D')) &&
1312 ((tmp[9] == 's') || (tmp[9] == 'S')) && (tmp[10] == ':'))) &&
1313 (fputs ("X-Original-",df) == EOF)) return NIL;
1314 case '\n': /* blank line */
1315 hdrp = NIL;
1316 break;
1317 default: /* nothing to do */
1318 break;
1320 /* just write the line */
1321 if (fwrite (tmp,1,j,df) != j) return NIL;
1323 if (i) return NIL; /* didn't read entire message */
1324 /* update set */
1325 if (stream) mail_append_set (set,stream->uid_last);
1327 return T;
1330 /* Internal routines */
1333 /* UNIX mail abort stream
1334 * Accepts: MAIL stream
1335 */
1337 void unix_abort (MAILSTREAM *stream)
1339 if (LOCAL) { /* only if a file is open */
1340 if (LOCAL->fd >= 0) close (LOCAL->fd);
1341 if (LOCAL->ld >= 0) { /* have a mailbox lock? */
1342 flock (LOCAL->ld,LOCK_UN);/* yes, release the lock */
1343 close (LOCAL->ld); /* close the lock file */
1344 unlink (LOCAL->lname); /* and delete it */
1346 if (LOCAL->lname) fs_give ((void **) &LOCAL->lname);
1347 /* free local text buffers */
1348 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
1349 if (LOCAL->text.data) fs_give ((void **) &LOCAL->text.data);
1350 if (LOCAL->linebuf) fs_give ((void **) &LOCAL->linebuf);
1351 if (LOCAL->line) fs_give ((void **) &LOCAL->line);
1352 /* nuke the local data */
1353 fs_give ((void **) &stream->local);
1354 stream->dtb = NIL; /* log out the DTB */
1358 /* UNIX open and lock mailbox
1359 * Accepts: file name to open/lock
1360 * file open mode
1361 * destination buffer for lock file name
1362 * type of locking operation (LOCK_SH or LOCK_EX)
1363 */
1365 int unix_lock (char *file,int flags,int mode,DOTLOCK *lock,int op)
1367 int fd;
1368 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
1369 (*bn) (BLOCK_FILELOCK,NIL);
1370 /* try locking the easy way */
1371 if (dotlock_lock (file,lock,-1)) {
1372 /* got dotlock file, easy open */
1373 if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
1374 else dotlock_unlock (lock); /* open failed, free the dotlock */
1376 /* no dot lock file, open file now */
1377 else if ((fd = open (file,flags,mode)) >= 0) {
1378 /* try paranoid way to make a dot lock file */
1379 if (dotlock_lock (file,lock,fd)) {
1380 close (fd); /* get fresh fd in case of timing race */
1381 if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
1382 /* open failed, free the dotlock */
1383 else dotlock_unlock (lock);
1385 else flock (fd,op); /* paranoid way failed, just flock() it */
1387 (*bn) (BLOCK_NONE,NIL);
1388 return fd;
1391 /* UNIX unlock and close mailbox
1392 * Accepts: file descriptor
1393 * (optional) mailbox stream to check atime/mtime
1394 * (optional) lock file name
1395 */
1397 void unix_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock)
1399 if (stream) { /* need to muck with times? */
1400 struct stat sbuf;
1401 time_t tp[2];
1402 time_t now = time (0);
1403 fstat (fd,&sbuf); /* get file times */
1404 if (LOCAL->ld >= 0) { /* yes, readwrite session? */
1405 tp[0] = now; /* set atime to now */
1406 /* set mtime to (now - 1) if necessary */
1407 tp[1] = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;
1409 else if (stream->recent) { /* readonly with recent messages */
1410 if ((sbuf.st_atime >= sbuf.st_mtime) ||
1411 (sbuf.st_atime >= sbuf.st_ctime))
1412 /* keep past mtime, whack back atime */
1413 tp[0] = (tp[1] = (sbuf.st_mtime < now) ? sbuf.st_mtime : now) - 1;
1414 else now = 0; /* no time change needed */
1416 /* readonly with no recent messages */
1417 else if ((sbuf.st_atime < sbuf.st_mtime) ||
1418 (sbuf.st_atime < sbuf.st_ctime)) {
1419 tp[0] = now; /* set atime to now */
1420 /* set mtime to (now - 1) if necessary */
1421 tp[1] = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;
1423 else now = 0; /* no time change needed */
1424 /* set the times, note change */
1425 if (now && !utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
1427 flock (fd,LOCK_UN); /* release flock'ers */
1428 if (!stream) close (fd); /* close the file if no stream */
1429 dotlock_unlock (lock); /* flush the lock file if any */
1432 /* UNIX mail parse and lock mailbox
1433 * Accepts: MAIL stream
1434 * space to write lock file name
1435 * type of locking operation
1436 * Returns: T if parse OK, critical & mailbox is locked shared; NIL if failure
1437 */
1439 int unix_parse (MAILSTREAM *stream,DOTLOCK *lock,int op)
1441 int zn;
1442 unsigned long i,j,k,m;
1443 unsigned char c,*s,*t,*u,tmp[MAILTMPLEN],date[30];
1444 int ti = 0,retain = T;
1445 unsigned long nmsgs = stream->nmsgs;
1446 unsigned long prevuid = nmsgs ? mail_elt (stream,nmsgs)->private.uid : 0;
1447 unsigned long recent = stream->recent;
1448 unsigned long oldnmsgs = stream->nmsgs;
1449 short silent = stream->silent;
1450 short pseudoseen = NIL;
1451 struct stat sbuf;
1452 STRING bs;
1453 FDDATA d;
1454 MESSAGECACHE *elt;
1455 mail_lock (stream); /* guard against recursion or pingers */
1456 /* toss out previous descriptor */
1457 if (LOCAL->fd >= 0) close (LOCAL->fd);
1458 MM_CRITICAL (stream); /* open and lock mailbox (shared OK) */
1459 if ((LOCAL->fd = unix_lock (stream->mailbox,(LOCAL->ld >= 0) ?
1460 O_RDWR : O_RDONLY,
1461 (long)mail_parameters(NIL,GET_MBXPROTECTION,NIL),
1462 lock,op)) < 0) {
1463 sprintf (tmp,"Mailbox open failed, aborted: %s",strerror (errno));
1464 MM_LOG (tmp,ERROR);
1465 unix_abort (stream);
1466 mail_unlock (stream);
1467 MM_NOCRITICAL (stream); /* done with critical */
1468 return NIL;
1470 fstat (LOCAL->fd,&sbuf); /* get status */
1471 /* validate change in size */
1472 if (sbuf.st_size < LOCAL->filesize) {
1473 sprintf (tmp,"Mailbox shrank from %lu to %lu bytes, aborted",
1474 (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size);
1475 MM_LOG (tmp,ERROR); /* this is pretty bad */
1476 unix_unlock (LOCAL->fd,stream,lock);
1477 unix_abort (stream);
1478 mail_unlock (stream);
1479 MM_NOCRITICAL (stream); /* done with critical */
1480 return NIL;
1483 /* new data? */
1484 else if (i = sbuf.st_size - LOCAL->filesize) {
1485 d.fd = LOCAL->fd; /* yes, set up file descriptor */
1486 d.pos = LOCAL->filesize; /* get to that position in the file */
1487 d.chunk = LOCAL->buf; /* initial buffer chunk */
1488 d.chunksize = CHUNKSIZE; /* file chunk size */
1489 INIT (&bs,fd_string,&d,i); /* initialize stringstruct */
1490 /* skip leading whitespace for broken MTAs */
1491 while (((c = CHR (&bs)) == '\n') || (c == '\r') ||
1492 (c == ' ') || (c == '\t')) SNX (&bs);
1493 if (SIZE (&bs)) { /* read new data */
1494 /* remember internal header position */
1495 j = LOCAL->filesize + GETPOS (&bs);
1496 s = unix_mbxline (stream,&bs,&i);
1497 t = NIL,zn = 0;
1498 if (i) VALID (s,t,ti,zn); /* see if valid From line */
1499 if (!ti) { /* someone pulled the rug from under us */
1500 sprintf (tmp,"Unexpected changes to mailbox (try restarting): %.20s",
1501 (char *) s);
1502 MM_LOG (tmp,ERROR);
1503 unix_unlock (LOCAL->fd,stream,lock);
1504 unix_abort (stream);
1505 mail_unlock (stream);
1506 /* done with critical */
1507 MM_NOCRITICAL (stream);
1508 return NIL;
1510 stream->silent = T; /* quell main program new message events */
1511 do { /* found a message */
1512 /* instantiate first new message */
1513 mail_exists (stream,++nmsgs);
1514 (elt = mail_elt (stream,nmsgs))->valid = T;
1515 recent++; /* assume recent by default */
1516 elt->recent = T;
1517 /* note position/size of internal header */
1518 elt->private.special.offset = j;
1519 elt->private.msg.header.offset = elt->private.special.text.size = i;
1521 /* generate plausible IMAPish date string */
1522 date[2] = date[6] = date[20] = '-'; date[11] = ' ';
1523 date[14] = date[17] = ':';
1524 /* dd */
1525 date[0] = t[ti - 2]; date[1] = t[ti - 1];
1526 /* mmm */
1527 date[3] = t[ti - 6]; date[4] = t[ti - 5]; date[5] = t[ti - 4];
1528 /* hh */
1529 date[12] = t[ti + 1]; date[13] = t[ti + 2];
1530 /* mm */
1531 date[15] = t[ti + 4]; date[16] = t[ti + 5];
1532 if (t[ti += 6] == ':') {/* ss */
1533 date[18] = t[++ti]; date[19] = t[++ti];
1534 ti++; /* move to space */
1536 else date[18] = date[19] = '0';
1537 /* yy -- advance over timezone if necessary */
1538 if (zn == ti) ti += (((t[zn+1] == '+') || (t[zn+1] == '-')) ? 6 : 4);
1539 date[7] = t[ti + 1]; date[8] = t[ti + 2];
1540 date[9] = t[ti + 3]; date[10] = t[ti + 4];
1541 /* zzz */
1542 t = zn ? (t + zn + 1) : (unsigned char *) "LCL";
1543 date[21] = *t++; date[22] = *t++; date[23] = *t++;
1544 if ((date[21] != '+') && (date[21] != '-')) date[24] = '\0';
1545 else { /* numeric time zone */
1546 date[24] = *t++; date[25] = *t++;
1547 date[26] = '\0'; date[20] = ' ';
1549 /* set internal date */
1550 if (!mail_parse_date (elt,date)) {
1551 sprintf (tmp,"Unable to parse internal date: %s",(char *) date);
1552 MM_LOG (tmp,WARN);
1555 do { /* look for message body */
1556 s = t = unix_mbxline (stream,&bs,&i);
1557 if (i) switch (*s) { /* check header lines */
1558 case 'X': /* possible X-???: line */
1559 if (s[1] == '-') { /* must be immediately followed by hyphen */
1560 /* X-Status: becomes Status: in S case */
1561 if (s[2] == 'S' && s[3] == 't' && s[4] == 'a' && s[5] == 't' &&
1562 s[6] == 'u' && s[7] == 's' && s[8] == ':') s += 2;
1563 /* possible X-Keywords */
1564 else if (s[2] == 'K' && s[3] == 'e' && s[4] == 'y' &&
1565 s[5] == 'w' && s[6] == 'o' && s[7] == 'r' &&
1566 s[8] == 'd' && s[9] == 's' && s[10] == ':') {
1567 SIZEDTEXT uf;
1568 retain = NIL; /* don't retain continuation */
1569 s += 11; /* flush leading whitespace */
1570 while (*s && (*s != '\n') && ((*s != '\r') || (s[1] != '\n'))){
1571 while (*s == ' ') s++;
1572 /* find end of keyword */
1573 if (!(u = strpbrk (s," \n\r"))) u = s + strlen (s);
1574 /* got a keyword? */
1575 if ((k = (u - s)) && (k <= MAXUSERFLAG)) {
1576 uf.data = (unsigned char *) s;
1577 uf.size = k;
1578 for (j = 0; (j < NUSERFLAGS) && stream->user_flags[j]; ++j)
1579 if (!compare_csizedtext (stream->user_flags[j],&uf)) {
1580 elt->user_flags |= ((long) 1) << j;
1581 break;
1584 s = u; /* advance to next keyword */
1586 break;
1589 /* possible X-IMAP */
1590 else if ((s[2] == 'I') && (s[3] == 'M') && (s[4] == 'A') &&
1591 (s[5] == 'P') && ((m = (s[6] == ':')) ||
1592 ((s[6] == 'b') && (s[7] == 'a') &&
1593 (s[8] == 's') && (s[9] == 'e') &&
1594 (s[10] == ':')))) {
1595 retain = NIL; /* don't retain continuation */
1596 if ((nmsgs == 1) && !stream->uid_validity) {
1597 /* advance to data */
1598 s += m ? 7 : 11;
1599 /* flush whitespace */
1600 while (*s == ' ') s++;
1601 j = 0; /* slurp UID validity */
1602 /* found a digit? */
1603 while (isdigit (*s)) {
1604 j *= 10; /* yes, add it in */
1605 j += *s++ - '0';
1607 /* flush whitespace */
1608 while (*s == ' ') s++;
1609 /* must have valid UID validity and UID last */
1610 if (j && isdigit (*s)) {
1611 /* pseudo-header seen if X-IMAP */
1612 if (m) pseudoseen = LOCAL->pseudo = T;
1613 /* save UID validity */
1614 stream->uid_validity = j;
1615 j = 0; /* slurp UID last */
1616 while (isdigit (*s)) {
1617 j *= 10; /* yes, add it in */
1618 j += *s++ - '0';
1620 /* save UID last */
1621 stream->uid_last = j;
1622 /* process keywords */
1623 for (j = 0; (*s != '\n') && ((*s != '\r')||(s[1] != '\n'));
1624 s = u,j++) {
1625 /* flush leading whitespace */
1626 while (*s == ' ') s++;
1627 u = strpbrk (s," \n\r");
1628 /* got a keyword? */
1629 if ((j < NUSERFLAGS) && (k = (u - s)) &&
1630 (k <= MAXUSERFLAG)) {
1631 if (stream->user_flags[j])
1632 fs_give ((void **) &stream->user_flags[j]);
1633 stream->user_flags[j] = (char *) fs_get (k + 1);
1634 strncpy (stream->user_flags[j],s,k);
1635 stream->user_flags[j][k] = '\0';
1640 break;
1643 /* possible X-UID */
1644 else if (s[2] == 'U' && s[3] == 'I' && s[4] == 'D' &&
1645 s[5] == ':') {
1646 retain = NIL; /* don't retain continuation */
1647 /* only believe if have a UID validity */
1648 if (stream->uid_validity && ((nmsgs > 1) || !pseudoseen)) {
1649 s += 6; /* advance to UID value */
1650 /* flush whitespace */
1651 while (*s == ' ') s++;
1652 j = 0;
1653 /* found a digit? */
1654 while (isdigit (*s)) {
1655 j *= 10; /* yes, add it in */
1656 j += *s++ - '0';
1658 /* flush remainder of line */
1659 while (*s != '\n') s++;
1660 /* make sure not duplicated */
1661 if (elt->private.uid)
1662 sprintf (tmp,"Message %lu UID %lu already has UID %lu",
1663 pseudoseen ? elt->msgno - 1 : elt->msgno,
1664 j,elt->private.uid);
1665 /* make sure UID doesn't go backwards */
1666 else if (j <= prevuid)
1667 sprintf (tmp,"Message %lu UID %lu less than %lu",
1668 pseudoseen ? elt->msgno - 1 : elt->msgno,
1669 j,prevuid + 1);
1670 #if 0 /* this is currently broken by UIDPLUS */
1671 /* or skip by mailbox's recorded last */
1672 else if (j > stream->uid_last)
1673 sprintf (tmp,"Message %lu UID %lu greater than last %lu",
1674 pseudoseen ? elt->msgno - 1 : elt->msgno,
1675 j,stream->uid_last);
1676 #endif
1677 else { /* normal UID case */
1678 prevuid = elt->private.uid = j;
1679 #if 1 /* temporary kludge for UIDPLUS */
1680 if (prevuid > stream->uid_last) {
1681 stream->uid_last = prevuid;
1682 LOCAL->ddirty = LOCAL->dirty = T;
1684 #endif
1685 break; /* exit this cruft */
1687 MM_LOG (tmp,WARN);
1688 /* invalidate UID validity */
1689 stream->uid_validity = 0;
1690 elt->private.uid = 0;
1692 break;
1695 /* otherwise fall into S case */
1697 case 'S': /* possible Status: line */
1698 if (s[0] == 'S' && s[1] == 't' && s[2] == 'a' && s[3] == 't' &&
1699 s[4] == 'u' && s[5] == 's' && s[6] == ':') {
1700 retain = NIL; /* don't retain continuation */
1701 s += 6; /* advance to status flags */
1702 do switch (*s++) {/* parse flags */
1703 case 'R': /* message read */
1704 elt->seen = T;
1705 break;
1706 case 'O': /* message old */
1707 if (elt->recent) {
1708 elt->recent = NIL;
1709 recent--; /* it really wasn't recent */
1711 break;
1712 case 'D': /* message deleted */
1713 elt->deleted = T;
1714 break;
1715 case 'F': /* message flagged */
1716 elt->flagged = T;
1717 break;
1718 case 'A': /* message answered */
1719 elt->answered = T;
1720 break;
1721 case 'T': /* message is a draft */
1722 elt->draft = T;
1723 break;
1724 default: /* some other crap */
1725 break;
1726 } while (*s && (*s != '\n') && ((*s != '\r') || (s[1] != '\n')));
1727 break; /* all done */
1729 /* otherwise fall into default case */
1731 default: /* ordinary header line */
1732 if ((*s == 'S') || (*s == 's') ||
1733 (((*s == 'X') || (*s == 'x')) && (s[1] == '-'))) {
1734 unsigned char *e,*v;
1735 /* must match what mail_filter() does */
1736 for (u = s,v = tmp,e = u + min (i,MAILTMPLEN - 1);
1737 (u < e) && ((c = (*u ? *u : (*u = ' '))) != ':') &&
1738 ((c > ' ') || ((c != ' ') && (c != '\t') &&
1739 (c != '\r') && (c != '\n')));
1740 *v++ = *u++);
1741 *v = '\0'; /* tie off */
1742 /* matches internal header? */
1743 if (!compare_cstring (tmp,"STATUS") ||
1744 !compare_cstring (tmp,"X-STATUS") ||
1745 !compare_cstring (tmp,"X-KEYWORDS") ||
1746 !compare_cstring (tmp,"X-UID") ||
1747 !compare_cstring (tmp,"X-IMAP") ||
1748 !compare_cstring (tmp,"X-IMAPBASE")) {
1749 char err[MAILTMPLEN];
1750 sprintf (err,"Discarding bogus %s header in message %lu",
1751 (char *) tmp,elt->msgno);
1752 MM_LOG (err,WARN);
1753 retain = NIL; /* don't retain continuation */
1754 break; /* different case or something */
1757 /* retain or non-continuation? */
1758 if (retain || ((*s != ' ') && (*s != '\t'))) {
1759 retain = T; /* retaining continuation now */
1760 /* line length in LF format newline */
1761 for (j = k = 0; j < i; ++j) if (s[j] != '\r') ++k;
1762 /* "internal" header size */
1763 elt->private.spare.data += k;
1764 /* message size */
1765 elt->rfc822_size += k + 1;
1767 else {
1768 char err[MAILTMPLEN];
1769 sprintf (err,"Discarding bogus continuation in msg %lu: %.80s",
1770 elt->msgno,(char *) s);
1771 if (u = strpbrk (err,"\r\n")) *u = '\0';
1772 MM_LOG (err,WARN);
1773 break; /* different case or something */
1775 break;
1777 } while (i && (*t != '\n') && ((*t != '\r') || (t[1] != '\n')));
1778 /* "internal" header sans trailing newline */
1779 if (i) elt->private.spare.data--;
1780 /* assign a UID if none found */
1781 if (((nmsgs > 1) || !pseudoseen) && !elt->private.uid) {
1782 prevuid = elt->private.uid = ++stream->uid_last;
1783 elt->private.dirty = T;
1784 LOCAL->ddirty = T; /* force update */
1786 else elt->private.dirty = elt->recent;
1788 /* note size of header, location of text */
1789 elt->private.msg.header.text.size =
1790 (elt->private.msg.text.offset =
1791 (LOCAL->filesize + GETPOS (&bs)) - elt->private.special.offset) -
1792 elt->private.special.text.size;
1793 k = m = 0; /* no previous line size yet */
1794 /* note current position */
1795 j = LOCAL->filesize + GETPOS (&bs);
1796 if (i) do { /* look for next message */
1797 s = unix_mbxline (stream,&bs,&i);
1798 if (i) { /* got new data? */
1799 VALID (s,t,ti,zn); /* yes, parse line */
1800 if (!ti) { /* not a header line, add it to message */
1801 elt->rfc822_size += i;
1802 for (j = 0; j < i; ++j) switch (s[j]) {
1803 case '\r': /* squeeze out CRs */
1804 elt->rfc822_size -= 1;
1805 break;
1806 case '\n': /* LF becomes CRLF */
1807 elt->rfc822_size += 1;
1808 break;
1809 default:
1810 break;
1812 if ((i == 1) && (*s == '\n')) {
1813 k = 2;
1814 m = 1;
1816 else if ((i == 2) && (*s == '\r') && (s[1] == '\n'))
1817 k = m = 2;
1818 else k = m = 0; /* file does not end with newline! */
1819 /* update current position */
1820 j = LOCAL->filesize + GETPOS (&bs);
1823 } while (i && !ti); /* until found a header */
1824 elt->private.msg.text.text.size = j -
1825 (elt->private.special.offset + elt->private.msg.text.offset);
1826 /* flush ending blank line */
1827 elt->private.msg.text.text.size -= m;
1828 elt->rfc822_size -= k;
1829 /* until end of buffer */
1830 } while (!stream->sniff && i);
1831 if (pseudoseen) { /* flush pseudo-message if present */
1832 /* decrement recent count */
1833 if (mail_elt (stream,1)->recent) recent--;
1834 /* and the exists count */
1835 mail_exists (stream,nmsgs--);
1836 mail_expunged(stream,1);/* fake an expunge of that message */
1838 /* need to start a new UID validity? */
1839 if (!stream->uid_validity) {
1840 stream->uid_validity = time (0);
1841 /* in case a whiner with no life */
1842 if (mail_parameters (NIL,GET_USERHASNOLIFE,NIL))
1843 stream->uid_nosticky = T;
1844 else if (nmsgs) { /* don't bother if empty file */
1845 /* make dirty to restart UID epoch */
1846 LOCAL->ddirty = LOCAL->dirty = T;
1847 /* need to rewrite msg 1 if not pseudo */
1848 if (!LOCAL->pseudo) mail_elt (stream,1)->private.dirty = T;
1849 MM_LOG ("Assigning new unique identifiers to all messages",NIL);
1852 stream->nmsgs = oldnmsgs; /* whack it back down */
1853 stream->silent = silent; /* restore old silent setting */
1854 /* notify upper level of new mailbox sizes */
1855 mail_exists (stream,nmsgs);
1856 mail_recent (stream,recent);
1857 /* mark dirty so O flags are set */
1858 if (recent) LOCAL->dirty = T;
1861 /* no change, don't babble if never got time */
1862 else if (LOCAL->filetime && LOCAL->filetime != sbuf.st_mtime)
1863 MM_LOG ("New mailbox modification time but apparently no changes",WARN);
1864 /* update parsed file size and time */
1865 LOCAL->filesize = sbuf.st_size;
1866 LOCAL->filetime = sbuf.st_mtime;
1867 return T; /* return the winnage */
1870 /* UNIX read line from mailbox
1871 * Accepts: mail stream
1872 * stringstruct
1873 * pointer to line size
1874 * Returns: pointer to input line
1875 */
1877 char *unix_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size)
1879 unsigned long i,j,k,m;
1880 char *s,*t,*te;
1881 char *ret = "";
1882 /* flush old buffer */
1883 if (LOCAL->line) fs_give ((void **) &LOCAL->line);
1884 /* if buffer needs refreshing */
1885 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
1886 if (SIZE (bs)) { /* find newline */
1887 /* end of fast scan */
1888 te = (t = (s = bs->curpos) + bs->cursize) - 12;
1889 while (s < te) if ((*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
1890 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
1891 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
1892 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n')) {
1893 --s; /* back up */
1894 break; /* exit loop */
1896 /* final character-at-a-time scan */
1897 while ((s < t) && (*s != '\n')) ++s;
1898 /* difficult case if line spans buffer */
1899 if ((i = s - bs->curpos) == bs->cursize) {
1900 /* have space in line buffer? */
1901 if (i > LOCAL->linebuflen) {
1902 fs_give ((void **) &LOCAL->linebuf);
1903 LOCAL->linebuf = (char *) fs_get (LOCAL->linebuflen = i);
1905 /* remember what we have so far */
1906 memcpy (LOCAL->linebuf,bs->curpos,i);
1907 /* load next buffer */
1908 SETPOS (bs,k = GETPOS (bs) + i);
1909 /* end of fast scan */
1910 te = (t = (s = bs->curpos) + bs->cursize) - 12;
1911 /* fast scan in overlap buffer */
1912 while (s < te) if ((*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
1913 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
1914 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
1915 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n')) {
1916 --s; /* back up */
1917 break; /* exit loop */
1920 /* final character-at-a-time scan */
1921 while ((s < t) && (*s != '\n')) ++s;
1922 /* huge line? */
1923 if ((j = s - bs->curpos) == bs->cursize) {
1924 SETPOS (bs,GETPOS (bs) + j);
1925 /* look for end of line (s-l-o-w!!) */
1926 for (m = SIZE (bs); m && (SNX (bs) != '\n'); --m,++j);
1927 SETPOS (bs,k); /* go back to where it started */
1929 /* got size of data, make buffer for return */
1930 ret = LOCAL->line = (char *) fs_get (i + j + 2);
1931 /* copy first chunk */
1932 memcpy (ret,LOCAL->linebuf,i);
1933 while (j) { /* copy remainder */
1934 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
1935 memcpy (ret + i,bs->curpos,k = min (j,bs->cursize));
1936 i += k; /* account for this much read in */
1937 j -= k;
1938 bs->curpos += k; /* increment new position */
1939 bs->cursize -= k; /* eat that many bytes */
1941 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
1942 /* read newline at end */
1943 if (SIZE (bs)) ret[i++] = SNX (bs);
1944 ret[i] = '\0'; /* makes debugging easier */
1946 else { /* this is easy */
1947 ret = bs->curpos; /* string it at this position */
1948 bs->curpos += ++i; /* increment new position */
1949 bs->cursize -= i; /* eat that many bytes */
1951 *size = i; /* return that to user */
1953 else *size = 0; /* end of data, return empty */
1954 return ret;
1957 /* UNIX make pseudo-header
1958 * Accepts: MAIL stream
1959 * buffer to write pseudo-header
1960 * Returns: length of pseudo-header
1961 */
1963 unsigned long unix_pseudo (MAILSTREAM *stream,char *hdr)
1965 int i;
1966 char *s,tmp[MAILTMPLEN];
1967 time_t now = time (0);
1968 rfc822_fixed_date (tmp);
1969 sprintf (hdr,"From %s %.24s\nDate: %s\nFrom: %s <%s@%.80s>\nSubject: %s\nMessage-ID: <%lu@%.80s>\nX-IMAP: %010lu %010lu",
1970 pseudo_from,ctime (&now),
1971 tmp,pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,
1972 (unsigned long) now,mylocalhost (),stream->uid_validity,
1973 stream->uid_last);
1974 for (s = hdr + strlen (hdr),i = 0; i < NUSERFLAGS; ++i)
1975 if (stream->user_flags[i])
1976 sprintf (s += strlen (s)," %s",stream->user_flags[i]);
1977 sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n\n",pseudo_msg);
1978 return strlen (hdr); /* return header length */
1981 /* UNIX make status string
1982 * Accepts: MAIL stream
1983 * destination string to write
1984 * message cache entry
1985 * UID to write if non-zero (else use elt->private.uid)
1986 * non-zero flag to write UID (.LT. 0 to write UID base info too)
1987 * Returns: length of string
1988 */
1990 unsigned long unix_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
1991 unsigned long uid,long flag)
1993 char *t,stack[64];
1994 char *s = status;
1995 unsigned long n;
1996 int pad = 50;
1997 int sticky = uid ? T : !stream->uid_nosticky;
1998 /* This used to use sprintf(), but thanks to certain cretinous C libraries
1999 with horribly slow implementations of sprintf() I had to change it to this
2000 mess. At least it should be fast. */
2001 if ((flag < 0) && sticky) { /* need to write X-IMAPbase: header? */
2002 *s++ = 'X'; *s++ = '-'; *s++ = 'I'; *s++ = 'M'; *s++ = 'A'; *s++ = 'P';
2003 *s++ = 'b'; *s++ = 'a'; *s++ = 's'; *s++ = 'e'; *s++ = ':'; *s++ = ' ';
2004 t = stack;
2005 n = stream->uid_validity; /* push UID validity digits on the stack */
2006 do *t++ = (char) (n % 10) + '0';
2007 while (n /= 10);
2008 /* pop UID validity digits from stack */
2009 while (t > stack) *s++ = *--t;
2010 *s++ = ' ';
2011 n = stream->uid_last; /* push UID last digits on the stack */
2012 do *t++ = (char) (n % 10) + '0';
2013 while (n /= 10);
2014 /* pop UID last digits from stack */
2015 while (t > stack) *s++ = *--t;
2016 for (n = 0; n < NUSERFLAGS; ++n) if (t = stream->user_flags[n])
2017 for (*s++ = ' '; *t; *s++ = *t++);
2018 *s++ = '\n';
2019 pad += 30; /* increased padding if have IMAPbase */
2021 *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't'; *s++ = 'u'; *s++ = 's';
2022 *s++ = ':'; *s++ = ' ';
2023 if (elt->seen) *s++ = 'R';
2024 /* only write O if have a UID */
2025 if (flag && (!elt->recent || !LOCAL->appending)) *s++ = 'O';
2026 *s++ = '\n';
2027 *s++ = 'X'; *s++ = '-'; *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't';
2028 *s++ = 'u'; *s++ = 's'; *s++ = ':'; *s++ = ' ';
2029 if (elt->deleted) *s++ = 'D';
2030 if (elt->flagged) *s++ = 'F';
2031 if (elt->answered) *s++ = 'A';
2032 if (elt->draft) *s++ = 'T';
2033 *s++ = '\n';
2035 if (sticky) { /* only do this if UIDs sticky */
2036 *s++ = 'X'; *s++ = '-'; *s++ = 'K'; *s++ = 'e'; *s++ = 'y'; *s++ = 'w';
2037 *s++ = 'o'; *s++ = 'r'; *s++ = 'd'; *s++ = 's'; *s++ = ':';
2038 if (n = elt->user_flags) do {
2039 *s++ = ' ';
2040 for (t = stream->user_flags[find_rightmost_bit (&n)]; *t; *s++ = *t++);
2041 } while (n);
2042 n = s - status; /* get size of stuff so far */
2043 /* pad X-Keywords to make size constant */
2044 if (n < pad) for (n = pad - n; n > 0; --n) *s++ = ' ';
2045 *s++ = '\n';
2046 if (flag) { /* want to include UID? */
2047 t = stack;
2048 /* push UID digits on the stack */
2049 n = uid ? uid : elt->private.uid;
2050 do *t++ = (char) (n % 10) + '0';
2051 while (n /= 10);
2052 *s++ = 'X'; *s++ = '-'; *s++ = 'U'; *s++ = 'I'; *s++ = 'D'; *s++ = ':';
2053 *s++ = ' ';
2054 /* pop UID from stack */
2055 while (t > stack) *s++ = *--t;
2056 *s++ = '\n';
2059 *s++ = '\n'; *s = '\0'; /* end of extended message status */
2060 return s - status; /* return size of resulting string */
2063 /* Rewrite mailbox file
2064 * Accepts: MAIL stream, must be critical and locked
2065 * return pointer to number of expunged messages if want expunge
2066 * lock file name
2067 * expunge sequence, not deleted flag
2068 * Returns: T if success and mailbox unlocked, NIL if failure
2069 */
2071 #define OVERFLOWBUFLEN 8192 /* initial overflow buffer length */
2073 long unix_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
2074 long flags)
2076 MESSAGECACHE *elt;
2077 UNIXFILE f;
2078 char *s;
2079 time_t tp[2];
2080 long ret,flag;
2081 unsigned long i,j;
2082 unsigned long recent = stream->recent;
2083 unsigned long size = LOCAL->pseudo ? unix_pseudo (stream,LOCAL->buf) : 0;
2084 if (nexp) *nexp = 0; /* initially nothing expunged */
2085 /* calculate size of mailbox after rewrite */
2086 for (i = 1,flag = LOCAL->pseudo ? 1 : -1; i <= stream->nmsgs; i++) {
2087 elt = mail_elt (stream,i); /* get cache */
2088 if (!(nexp && elt->deleted && (flags ? elt->sequence : T))) {
2089 /* add RFC822 size of this message */
2090 size += elt->private.special.text.size + elt->private.spare.data +
2091 unix_xstatus (stream,LOCAL->buf,elt,NIL,flag) +
2092 elt->private.msg.text.text.size + 1;
2093 flag = 1; /* only count X-IMAPbase once */
2096 /* no messages, has a life, and no pseudo */
2097 if (!size && !mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) {
2098 LOCAL->pseudo = T; /* so make a pseudo-message now */
2099 size = unix_pseudo (stream,LOCAL->buf);
2101 /* extend the file as necessary */
2102 if (ret = unix_extend (stream,size)) {
2103 /* Set up buffered I/O file structure
2104 * curpos current position being written through buffering
2105 * filepos current position being written physically to the disk
2106 * bufpos current position being written in the buffer
2107 * protect current maximum position that can be written to the disk
2108 * before buffering is forced
2109 * The code tries to buffer so that that disk is written in multiples of
2110 * OVERBLOWBUFLEN bytes.
2111 */
2112 f.stream = stream; /* note mail stream */
2113 f.curpos = f.filepos = 0; /* start of file */
2114 f.protect = stream->nmsgs ? /* initial protection pointer */
2115 mail_elt (stream,1)->private.special.offset : 8192;
2116 f.bufpos = f.buf = (char *) fs_get (f.buflen = OVERFLOWBUFLEN);
2118 if (LOCAL->pseudo) /* update pseudo-header */
2119 unix_write (&f,LOCAL->buf,unix_pseudo (stream,LOCAL->buf));
2120 /* loop through all messages */
2121 for (i = 1,flag = LOCAL->pseudo ? 1 : -1; i <= stream->nmsgs;) {
2122 elt = mail_elt (stream,i);/* get cache */
2123 /* expunge this message? */
2124 if (nexp && elt->deleted && (flags ? elt->sequence : T)) {
2125 /* one less recent message */
2126 if (elt->recent) --recent;
2127 mail_expunged(stream,i);/* notify upper levels */
2128 ++*nexp; /* count up one more expunged message */
2130 else { /* preserve this message */
2131 i++; /* advance to next message */
2132 if ((flag < 0) || /* need to rewrite message? */
2133 elt->private.dirty || (f.curpos != elt->private.special.offset) ||
2134 (elt->private.msg.header.text.size !=
2135 (elt->private.spare.data +
2136 unix_xstatus (stream,LOCAL->buf,elt,NIL,flag)))) {
2137 unsigned long newoffset = f.curpos;
2138 /* yes, seek to internal header */
2139 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
2140 read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
2141 /* see if need to squeeze out a CR */
2142 if (LOCAL->buf[elt->private.special.text.size - 2] == '\r') {
2143 LOCAL->buf[--elt->private.special.text.size - 1] = '\n';
2144 --size; /* squeezed out a CR from PC */
2146 /* protection pointer moves to RFC822 header */
2147 f.protect = elt->private.special.offset +
2148 elt->private.msg.header.offset;
2149 /* write internal header */
2150 unix_write (&f,LOCAL->buf,elt->private.special.text.size);
2151 /* get RFC822 header */
2152 s = unix_header (stream,elt->msgno,&j,FT_INTERNAL);
2153 /* in case this got decremented */
2154 elt->private.msg.header.offset = elt->private.special.text.size;
2155 /* header size, sans trailing newline */
2156 if ((j < 2) || (s[j - 2] == '\n')) j--;
2157 /* this can happen if CRs were squeezed */
2158 if (j < elt->private.spare.data) {
2159 /* so fix up counts */
2160 size -= elt->private.spare.data - j;
2161 elt->private.spare.data = j;
2163 else if (j != elt->private.spare.data)
2164 fatal ("header size inconsistent");
2165 /* protection pointer moves to RFC822 text */
2166 f.protect = elt->private.special.offset +
2167 elt->private.msg.text.offset;
2168 unix_write (&f,s,j); /* write RFC822 header */
2169 /* write status and UID */
2170 unix_write (&f,LOCAL->buf,
2171 j = unix_xstatus (stream,LOCAL->buf,elt,NIL,flag));
2172 flag = 1; /* only write X-IMAPbase once */
2173 /* new file header size */
2174 elt->private.msg.header.text.size = elt->private.spare.data + j;
2176 /* did text move? */
2177 if (f.curpos != f.protect) {
2178 /* get message text */
2179 s = unix_text_work (stream,elt,&j,FT_INTERNAL);
2180 /* this can happen if CRs were squeezed */
2181 if (j < elt->private.msg.text.text.size) {
2182 /* so fix up counts */
2183 size -= elt->private.msg.text.text.size - j;
2184 elt->private.msg.text.text.size = j;
2186 /* can't happen it says here */
2187 else if (j > elt->private.msg.text.text.size)
2188 fatal ("text size inconsistent");
2189 /* new text offset, status/UID may change it */
2190 elt->private.msg.text.offset = f.curpos - newoffset;
2191 /* protection pointer moves to next message */
2192 f.protect = (i <= stream->nmsgs) ?
2193 mail_elt (stream,i)->private.special.offset : (f.curpos + j + 1);
2194 unix_write (&f,s,j);/* write text */
2195 /* write trailing newline */
2196 unix_write (&f,"\n",1);
2198 else { /* tie off header and status */
2199 unix_write (&f,NIL,NIL);
2200 /* protection pointer moves to next message */
2201 f.protect = (i <= stream->nmsgs) ?
2202 mail_elt (stream,i)->private.special.offset : size;
2203 /* locate end of message text */
2204 j = f.filepos + elt->private.msg.text.text.size;
2205 /* trailing newline already there? */
2206 if (f.protect == (j + 1)) f.curpos = f.filepos = f.protect;
2207 else { /* trailing newline missing, write it */
2208 f.curpos = f.filepos = j;
2209 unix_write (&f,"\n",1);
2212 /* new internal header offset */
2213 elt->private.special.offset = newoffset;
2214 elt->private.dirty =NIL;/* message is now clean */
2216 else { /* no need to rewrite this message */
2217 /* tie off previous message if needed */
2218 unix_write (&f,NIL,NIL);
2219 /* protection pointer moves to next message */
2220 f.protect = (i <= stream->nmsgs) ?
2221 mail_elt (stream,i)->private.special.offset : size;
2222 /* locate end of message text */
2223 j = f.filepos + elt->private.special.text.size +
2224 elt->private.msg.header.text.size +
2225 elt->private.msg.text.text.size;
2226 /* trailing newline already there? */
2227 if (f.protect == (j + 1)) f.curpos = f.filepos = f.protect;
2228 else { /* trailing newline missing, write it */
2229 f.curpos = f.filepos = j;
2230 unix_write (&f,"\n",1);
2236 unix_write (&f,NIL,NIL); /* tie off final message */
2237 if (size != f.filepos) fatal ("file size inconsistent");
2238 fs_give ((void **) &f.buf); /* free buffer */
2239 /* make sure tied off */
2240 ftruncate (LOCAL->fd,LOCAL->filesize = size);
2241 fsync (LOCAL->fd); /* make sure the updates take */
2242 if (size && (flag < 0)) fatal ("lost UID base information");
2243 /* no longer dirty */
2244 LOCAL->ddirty = LOCAL->dirty = NIL;
2245 /* notify upper level of new mailbox sizes */
2246 mail_exists (stream,stream->nmsgs);
2247 mail_recent (stream,recent);
2248 /* set atime to now, mtime a second earlier */
2249 tp[1] = (tp[0] = time (0)) - 1;
2250 /* set the times, note change */
2251 if (!utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
2252 close (LOCAL->fd); /* close and reopen file */
2253 if ((LOCAL->fd = open (stream->mailbox,O_RDWR,
2254 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL)))
2255 < 0) {
2256 sprintf (LOCAL->buf,"Mailbox open failed, aborted: %s",strerror (errno));
2257 MM_LOG (LOCAL->buf,ERROR);
2258 unix_abort (stream);
2260 dotlock_unlock (lock); /* flush the lock file */
2262 return ret; /* return state from algorithm */
2265 /* Extend UNIX mailbox file
2266 * Accepts: MAIL stream
2267 * new desired size
2268 * Return: T if success, else NIL
2269 */
2271 long unix_extend (MAILSTREAM *stream,unsigned long size)
2273 unsigned long i = (size > LOCAL->filesize) ? size - LOCAL->filesize : 0;
2274 if (i) { /* does the mailbox need to grow? */
2275 if (i > LOCAL->buflen) { /* make sure have enough space */
2276 /* this user won the lottery all right */
2277 fs_give ((void **) &LOCAL->buf);
2278 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = i) + 1);
2280 memset (LOCAL->buf,'\0',i); /* get a block of nulls */
2281 while (T) { /* until write successful or punt */
2282 lseek (LOCAL->fd,LOCAL->filesize,L_SET);
2283 if ((write (LOCAL->fd,LOCAL->buf,i) >= 0) && !fsync (LOCAL->fd)) break;
2284 else {
2285 long e = errno; /* note error before doing ftruncate */
2286 ftruncate (LOCAL->fd,LOCAL->filesize);
2287 if (MM_DISKERROR (stream,e,NIL)) {
2288 fsync (LOCAL->fd); /* user chose to punt */
2289 sprintf (LOCAL->buf,"Unable to extend mailbox: %s",strerror (e));
2290 if (!stream->silent) MM_LOG (LOCAL->buf,ERROR);
2291 return NIL;
2296 return LONGT;
2299 /* Write data to buffered file
2300 * Accepts: buffered file pointer
2301 * file data or NIL to indicate "flush buffer"
2302 * date size (ignored for "flush buffer")
2303 * Does not return until success
2304 */
2306 void unix_write (UNIXFILE *f,char *buf,unsigned long size)
2308 unsigned long i,j,k;
2309 if (buf) { /* doing buffered write? */
2310 i = f->bufpos - f->buf; /* yes, get size of current buffer data */
2311 /* yes, have space in current buffer chunk? */
2312 if (j = i ? ((f->buflen - i) % OVERFLOWBUFLEN) : f->buflen) {
2313 /* yes, fill up buffer as much as we can */
2314 memcpy (f->bufpos,buf,k = min (j,size));
2315 f->bufpos += k; /* new buffer position */
2316 f->curpos += k; /* new current position */
2317 if (j -= k) return; /* all done if still have buffer free space */
2318 buf += k; /* full, get new unwritten data pointer */
2319 size -= k; /* new data size */
2320 i += k; /* new buffer data size */
2322 /* This chunk of the buffer is full. See if can make some space by
2323 * writing to the disk, if there's enough unprotected space to do so.
2324 * Try to fill out any unaligned chunk, along with any subsequent full
2325 * chunks that will fit in unprotected space.
2326 */
2327 /* any unprotected space we can write to? */
2328 if (j = min (i,f->protect - f->filepos)) {
2329 /* yes, filepos not at chunk boundary? */
2330 if ((k = f->filepos % OVERFLOWBUFLEN) && ((k = OVERFLOWBUFLEN - k) < j))
2331 j -= k; /* yes, and can write out partial chunk */
2332 else k = 0; /* no partial chunk to write */
2333 /* if at least a chunk free, write that too */
2334 if (j > OVERFLOWBUFLEN) k += j - (j % OVERFLOWBUFLEN);
2335 if (k) { /* write data if there is anything we can */
2336 unix_phys_write (f,f->buf,k);
2337 /* slide buffer */
2338 if (i -= k) memmove (f->buf,f->buf + k,i);
2339 f->bufpos = f->buf + i; /* new end of buffer */
2343 /* Have flushed the buffer as best as possible. All done if no more
2344 * data to write. Otherwise, if the buffer is empty AND if the unwritten
2345 * data is larger than a chunk AND the unprotected space is also larger
2346 * than a chunk, then write as many chunks as we can directly from the
2347 * data. Buffer the rest, expanding the buffer as needed.
2348 */
2349 if (size) { /* have more data that we need to buffer? */
2350 /* can write any of it to disk instead? */
2351 if ((f->bufpos == f->buf) &&
2352 ((j = min (f->protect - f->filepos,size)) > OVERFLOWBUFLEN)) {
2353 /* write as much as we can right now */
2354 unix_phys_write (f,buf,j -= (j % OVERFLOWBUFLEN));
2355 buf += j; /* new data pointer */
2356 size -= j; /* new data size */
2357 f->curpos += j; /* advance current pointer */
2359 if (size) { /* still have data that we need to buffer? */
2360 /* yes, need to expand the buffer? */
2361 if ((i = ((f->bufpos + size) - f->buf)) > f->buflen) {
2362 /* note current position in buffer */
2363 j = f->bufpos - f->buf;
2364 i += OVERFLOWBUFLEN; /* yes, grow another chunk */
2365 fs_resize ((void **) &f->buf,f->buflen = i - (i % OVERFLOWBUFLEN));
2366 /* in case buffer relocated */
2367 f->bufpos = f->buf + j;
2369 /* buffer remaining data */
2370 memcpy (f->bufpos,buf,size);
2371 f->bufpos += size; /* new end of buffer */
2372 f->curpos += size; /* advance current pointer */
2376 else { /* flush buffer to disk */
2377 unix_phys_write (f,f->buf,i = f->bufpos - f->buf);
2378 f->bufpos = f->buf; /* reset buffer */
2379 /* update positions */
2380 f->curpos = f->protect = f->filepos;
2384 /* Physical disk write
2385 * Accepts: buffered file pointer
2386 * buffer address
2387 * buffer size
2388 * Does not return until success
2389 */
2391 void unix_phys_write (UNIXFILE *f,char *buf,size_t size)
2393 MAILSTREAM *stream = f->stream;
2394 /* write data at desired position */
2395 while (size && ((lseek (LOCAL->fd,f->filepos,L_SET) < 0) ||
2396 (write (LOCAL->fd,buf,size) < 0))) {
2397 int e;
2398 char tmp[MAILTMPLEN];
2399 sprintf (tmp,"Unable to write to mailbox: %s",strerror (e = errno));
2400 MM_LOG (tmp,ERROR);
2401 MM_DISKERROR (NIL,e,T); /* serious problem, must retry */
2403 f->filepos += size; /* update file position */
2406 /* Return my mail suffix(of qmail or Postfix)
2407 * Returns: my mail address extensional suffix
2408 */
2410 char *mymailsuffix ()
2412 return myMailSuffix ? myMailSuffix : "";
2415 /* MBOX mail routines */
2418 /* Driver dispatch used by MAIL */
2420 DRIVER mboxdriver = {
2421 "mbox", /* driver name */
2422 /* driver flags */
2423 DR_LOCAL|DR_MAIL|DR_LOCKING|DR_NONEWMAILRONLY,
2424 (DRIVER *) NIL, /* next driver */
2425 mbox_valid, /* mailbox is valid for us */
2426 unix_parameters, /* manipulate parameters */
2427 unix_scan, /* scan mailboxes */
2428 unix_list, /* find mailboxes */
2429 unix_lsub, /* find subscribed mailboxes */
2430 NIL, /* subscribe to mailbox */
2431 NIL, /* unsubscribe from mailbox */
2432 mbox_create, /* create mailbox */
2433 mbox_delete, /* delete mailbox */
2434 mbox_rename, /* rename mailbox */
2435 mbox_status, /* status of mailbox */
2436 mbox_open, /* open mailbox */
2437 unix_close, /* close mailbox */
2438 NIL, /* fetch message "fast" attributes */
2439 NIL, /* fetch message flags */
2440 NIL, /* fetch overview */
2441 NIL, /* fetch message structure */
2442 unix_header, /* fetch message header */
2443 unix_text, /* fetch message body */
2444 NIL, /* fetch partial message text */
2445 NIL, /* unique identifier */
2446 NIL, /* message number */
2447 NIL, /* modify flags */
2448 unix_flagmsg, /* per-message modify flags */
2449 NIL, /* search for message based on criteria */
2450 NIL, /* sort messages */
2451 NIL, /* thread messages */
2452 mbox_ping, /* ping mailbox to see if still alive */
2453 mbox_check, /* check for new messages */
2454 mbox_expunge, /* expunge deleted messages */
2455 unix_copy, /* copy messages to another mailbox */
2456 mbox_append, /* append string message to mailbox */
2457 NIL /* garbage collect stream */
2458 };
2460 /* prototype stream */
2461 MAILSTREAM mboxproto = {&mboxdriver};
2463 /* MBOX mail validate mailbox
2464 * Accepts: mailbox name
2465 * Returns: our driver if name is valid, NIL otherwise
2466 */
2468 DRIVER *mbox_valid (char *name)
2470 /* only INBOX, mbox must exist */
2471 if (!compare_cstring (name,"INBOX") && (unix_valid ("mbox") || !errno) &&
2472 (unix_valid (sysinbox()) || !errno || (errno == ENOENT)))
2473 return &mboxdriver;
2474 return NIL; /* can't win (yet, anyway) */
2477 /* MBOX mail create mailbox
2478 * Accepts: MAIL stream
2479 * mailbox name to create
2480 * Returns: T on success, NIL on failure
2481 */
2483 long mbox_create (MAILSTREAM *stream,char *mailbox)
2485 char tmp[MAILTMPLEN];
2486 if (!compare_cstring (mailbox,"INBOX")) return unix_create (NIL,"mbox");
2487 sprintf (tmp,"Can't create non-INBOX name as mbox: %.80s",mailbox);
2488 MM_LOG (tmp,ERROR);
2489 return NIL;
2493 /* MBOX mail delete mailbox
2494 * Accepts: MAIL stream
2495 * mailbox name to delete
2496 * Returns: T on success, NIL on failure
2497 */
2499 long mbox_delete (MAILSTREAM *stream,char *mailbox)
2501 return mbox_rename (stream,mailbox,NIL);
2505 /* MBOX mail rename mailbox
2506 * Accepts: MAIL stream
2507 * old mailbox name
2508 * new mailbox name (or NIL for delete)
2509 * Returns: T on success, NIL on failure
2510 */
2512 long mbox_rename (MAILSTREAM *stream,char *old,char *newname)
2514 char tmp[MAILTMPLEN];
2515 long ret = unix_rename (stream,"~/mbox",newname);
2516 /* recreate file if renamed INBOX */
2517 if (ret) unix_create (NIL,"mbox");
2518 else MM_LOG (tmp,ERROR); /* log error */
2519 return ret; /* return success */
2522 /* MBOX Mail status
2523 * Accepts: mail stream
2524 * mailbox name
2525 * status flags
2526 * Returns: T on success, NIL on failure
2527 */
2529 long mbox_status (MAILSTREAM *stream,char *mbx,long flags)
2531 MAILSTATUS status;
2532 unsigned long i;
2533 MAILSTREAM *tstream = NIL;
2534 MAILSTREAM *systream = NIL;
2535 /* make temporary stream (unless this mbx) */
2536 if (!stream && !(stream = tstream =
2537 mail_open (NIL,mbx,OP_READONLY|OP_SILENT))) return NIL;
2538 status.flags = flags; /* return status values */
2539 status.messages = stream->nmsgs;
2540 status.recent = stream->recent;
2541 if (flags & SA_UNSEEN) /* must search to get unseen messages */
2542 for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++)
2543 if (!mail_elt (stream,i)->seen) status.unseen++;
2544 status.uidnext = stream->uid_last + 1;
2545 status.uidvalidity = stream->uid_validity;
2546 if (!status.recent && /* calculate post-snarf results */
2547 (systream = mail_open (NIL,sysinbox (),OP_READONLY|OP_SILENT))) {
2548 status.messages += systream->nmsgs;
2549 status.recent += systream->recent;
2550 if (flags & SA_UNSEEN) /* must search to get unseen messages */
2551 for (i = 1; i <= systream->nmsgs; i++)
2552 if (!mail_elt (systream,i)->seen) status.unseen++;
2553 /* kludge but probably good enough */
2554 status.uidnext += systream->nmsgs;
2556 MM_STATUS(stream,mbx,&status);/* pass status to main program */
2557 if (tstream) mail_close (tstream);
2558 if (systream) mail_close (systream);
2559 return T; /* success */
2562 /* MBOX mail open
2563 * Accepts: stream to open
2564 * Returns: stream on success, NIL on failure
2565 */
2567 MAILSTREAM *mbox_open (MAILSTREAM *stream)
2569 unsigned long i = 1;
2570 unsigned long recent = 0;
2571 /* return prototype for OP_PROTOTYPE call */
2572 if (!stream) return &mboxproto;
2573 /* change mailbox file name */
2574 fs_give ((void **) &stream->mailbox);
2575 stream->mailbox = cpystr ("mbox");
2576 /* open mailbox, snarf new mail */
2577 if (!(unix_open (stream) && mbox_ping (stream))) return NIL;
2578 stream->inbox = T; /* mark that this is an INBOX */
2579 /* notify upper level of mailbox sizes */
2580 mail_exists (stream,stream->nmsgs);
2581 while (i <= stream->nmsgs) if (mail_elt (stream,i++)->recent) ++recent;
2582 mail_recent (stream,recent); /* including recent messages */
2583 return stream;
2586 /* MBOX mail ping mailbox
2587 * Accepts: MAIL stream
2588 * Returns: T if stream alive, else NIL
2589 * No-op for readonly files, since read/writer can expunge it from under us!
2590 */
2592 static int snarfed = 0; /* number of snarfs */
2594 long mbox_ping (MAILSTREAM *stream)
2596 int sfd;
2597 unsigned long size;
2598 struct stat sbuf;
2599 char *s;
2600 DOTLOCK lock,lockx;
2601 /* time to try snarf and sysinbox non-empty? */
2602 if (LOCAL && !stream->rdonly && !stream->lock &&
2603 (time (0) >= (LOCAL->lastsnarf +
2604 (long) mail_parameters (NIL,GET_SNARFINTERVAL,NIL))) &&
2605 !stat (sysinbox (),&sbuf) && sbuf.st_size) {
2606 MM_CRITICAL (stream); /* yes, go critical */
2607 /* open and lock sysinbox */
2608 if ((sfd = unix_lock (sysinbox (),O_RDWR,
2609 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
2610 &lockx,LOCK_EX)) >= 0) {
2611 /* locked sysinbox in good format? */
2612 if (fstat (sfd,&sbuf) || !(size = sbuf.st_size) ||
2613 !unix_isvalid_fd (sfd)) {
2614 sprintf (LOCAL->buf,"Mail drop %s is not in standard Unix format",
2615 sysinbox ());
2616 MM_LOG (LOCAL->buf,ERROR);
2618 /* sysinbox good, parse and excl-lock mbox */
2619 else if (unix_parse (stream,&lock,LOCK_EX)) {
2620 lseek (sfd,0,L_SET); /* read entire sysinbox into memory */
2621 read (sfd,s = (char *) fs_get (size + 1),size);
2622 s[size] = '\0'; /* tie it off */
2623 /* append to end of mbox */
2624 lseek (LOCAL->fd,LOCAL->filesize,L_SET);
2626 /* copy to mbox */
2627 if ((write (LOCAL->fd,s,size) < 0) || fsync (LOCAL->fd)) {
2628 sprintf (LOCAL->buf,"New mail move failed: %s",strerror (errno));
2629 MM_LOG (LOCAL->buf,WARN);
2630 /* revert mbox to previous size */
2631 ftruncate (LOCAL->fd,LOCAL->filesize);
2633 /* sysinbox better not have changed */
2634 else if (fstat (sfd,&sbuf) || (size != sbuf.st_size)) {
2635 sprintf (LOCAL->buf,"Mail drop %s lock failure, old=%lu now=%lu",
2636 sysinbox (),size,(unsigned long) sbuf.st_size);
2637 MM_LOG (LOCAL->buf,ERROR);
2638 /* revert mbox to previous size */
2639 ftruncate (LOCAL->fd,LOCAL->filesize);
2640 /* Believe it or not, a Singaporean government system actually had
2641 * symlinks from /var/mail/user to ~user/mbox. To compound this
2642 * error, they used an SVR4 system; BSD and OSF locks would have
2643 * prevented it but not SVR4 locks.
2644 */
2645 if (!fstat (sfd,&sbuf) && (size == sbuf.st_size))
2646 syslog (LOG_ALERT,"File %s and %s are the same file!",
2647 sysinbox (),stream->mailbox);
2649 else { /* data copied OK */
2650 ftruncate (sfd,0); /* truncate sysinbox to zero bytes */
2651 if (!snarfed++) { /* have we snarfed before? */
2652 /* syslog if server, else user log */
2653 sprintf (LOCAL->buf,"Moved %lu bytes of new mail to %s from %s",
2654 size,stream->mailbox,sysinbox ());
2655 if (strcmp ((char *) mail_parameters (NIL,GET_SERVICENAME,NIL),
2656 "unknown"))
2657 syslog (LOG_INFO,"%s host= %s",LOCAL->buf,tcp_clienthost ());
2658 else MM_LOG (LOCAL->buf,WARN);
2661 /* done with sysinbox text */
2662 fs_give ((void **) &s);
2663 /* all done with mbox */
2664 unix_unlock (LOCAL->fd,stream,&lock);
2665 mail_unlock (stream); /* unlock the stream */
2666 MM_NOCRITICAL (stream); /* done with critical */
2668 /* all done with sysinbox */
2669 unix_unlock (sfd,NIL,&lockx);
2671 MM_NOCRITICAL (stream); /* done with critical */
2672 LOCAL->lastsnarf = time (0);/* note time of last snarf */
2674 return unix_ping (stream); /* do the unix routine now */
2677 /* MBOX mail check mailbox
2678 * Accepts: MAIL stream
2679 */
2681 void mbox_check (MAILSTREAM *stream)
2683 /* do local ping, then do unix routine */
2684 if (mbox_ping (stream)) unix_check (stream);
2688 /* MBOX mail expunge mailbox
2689 * Accepts: MAIL stream
2690 * sequence to expunge if non-NIL
2691 * expunge options
2692 * Returns: T, always
2693 */
2695 long mbox_expunge (MAILSTREAM *stream,char *sequence,long options)
2697 long ret = unix_expunge (stream,sequence,options);
2698 mbox_ping (stream); /* do local ping */
2699 return ret;
2703 /* MBOX mail append message from stringstruct
2704 * Accepts: MAIL stream
2705 * destination mailbox
2706 * append callback
2707 * data for callback
2708 * Returns: T if append successful, else NIL
2709 */
2711 long mbox_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
2713 char tmp[MAILTMPLEN];
2714 if (mbox_valid (mailbox)) return unix_append (stream,"mbox",af,data);
2715 sprintf (tmp,"Can't append to that name: %.80s",mailbox);
2716 MM_LOG (tmp,ERROR);
2717 return NIL;

UW-IMAP'd extensions by yuuji