1 /* ========================================================================
2 * Copyright 1988-2008 University of Washington
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
15 * Program: UNIX mail routines
17 * Author: Mark Crispin
19 * University of Washington
21 * Internet: MRC@Washington.EDU
23 * Date: 20 December 1989
24 * Last Edited: June 13 2018 by Eduardo Chappa
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.
35 * He was a dear friend, and I miss him terribly.
37 * Lift a leg, Yunie. Luv ya forever!!!!
44 extern int errno
; /* just in case */
57 /* UNIX I/O stream local data */
59 typedef struct unix_local
{
60 unsigned int dirty
: 1; /* disk copy needs updating */
61 unsigned int ddirty
: 1; /* double-dirty, ping becomes checkpoint */
62 unsigned int pseudo
: 1; /* uses a pseudo message */
63 unsigned int appending
: 1; /* don't mark new messages as old */
64 int fd
; /* mailbox file descriptor */
65 int ld
; /* lock file descriptor */
66 char *lname
; /* lock file name */
67 off_t filesize
; /* file size parsed */
68 time_t filetime
; /* last file time */
69 time_t lastsnarf
; /* last snarf time (for mbox driver) */
70 unsigned char *buf
; /* temporary buffer */
71 unsigned long buflen
; /* current size of temporary buffer */
72 unsigned long uid
; /* current text uid */
73 SIZEDTEXT text
; /* current text */
74 unsigned long textlen
; /* current text length */
75 char *line
; /* returned line */
76 char *linebuf
; /* line readin buffer */
77 unsigned long linebuflen
; /* current line readin buffer length */
81 /* Convenient access to local data */
83 #define LOCAL ((UNIXLOCAL *) stream->local)
86 /* UNIX protected file structure */
88 typedef struct unix_file
{
89 MAILSTREAM
*stream
; /* current stream */
90 off_t curpos
; /* current file position */
91 off_t protect
; /* protected position */
92 off_t filepos
; /* current last written file position */
93 char *buf
; /* overflow buffer */
94 size_t buflen
; /* current overflow buffer length */
95 char *bufpos
; /* current buffer position */
98 /* Function prototypes */
100 DRIVER
*unix_valid (char *name
);
101 long unix_isvalid_fd (int fd
);
102 void *unix_parameters (long function
,void *value
);
103 void unix_scan (MAILSTREAM
*stream
,char *ref
,char *pat
,char *contents
);
104 void unix_list (MAILSTREAM
*stream
,char *ref
,char *pat
);
105 void unix_lsub (MAILSTREAM
*stream
,char *ref
,char *pat
);
106 long unix_create (MAILSTREAM
*stream
,char *mailbox
);
107 long unix_delete (MAILSTREAM
*stream
,char *mailbox
);
108 long unix_rename (MAILSTREAM
*stream
,char *old
,char *newname
);
109 MAILSTREAM
*unix_open (MAILSTREAM
*stream
);
110 void unix_close (MAILSTREAM
*stream
,long options
);
111 char *unix_header (MAILSTREAM
*stream
,unsigned long msgno
,
112 unsigned long *length
,long flags
);
113 long unix_text (MAILSTREAM
*stream
,unsigned long msgno
,STRING
*bs
,long flags
);
114 char *unix_text_work (MAILSTREAM
*stream
,MESSAGECACHE
*elt
,
115 unsigned long *length
,long flags
);
116 void unix_flagmsg (MAILSTREAM
*stream
,MESSAGECACHE
*elt
);
117 long unix_ping (MAILSTREAM
*stream
);
118 void unix_check (MAILSTREAM
*stream
);
119 long unix_expunge (MAILSTREAM
*stream
,char *sequence
,long options
);
120 long unix_copy (MAILSTREAM
*stream
,char *sequence
,char *mailbox
,long options
);
121 long unix_append (MAILSTREAM
*stream
,char *mailbox
,append_t af
,void *data
);
122 int unix_collect_msg (MAILSTREAM
*stream
,FILE *sf
,char *flags
,char *date
,
124 int unix_append_msgs (MAILSTREAM
*stream
,FILE *sf
,FILE *df
,SEARCHSET
*set
);
126 void unix_abort (MAILSTREAM
*stream
);
127 char *unix_file (char *dst
,char *name
);
128 int unix_lock (char *file
,int flags
,int mode
,DOTLOCK
*lock
,int op
);
129 void unix_unlock (int fd
,MAILSTREAM
*stream
,DOTLOCK
*lock
);
130 int unix_parse (MAILSTREAM
*stream
,DOTLOCK
*lock
,int op
);
131 char *unix_mbxline (MAILSTREAM
*stream
,STRING
*bs
,unsigned long *size
);
132 unsigned long unix_pseudo (MAILSTREAM
*stream
,char *hdr
);
133 unsigned long unix_xstatus (MAILSTREAM
*stream
,char *status
,MESSAGECACHE
*elt
,
134 unsigned long uid
,long flag
);
135 long unix_rewrite (MAILSTREAM
*stream
,unsigned long *nexp
,DOTLOCK
*lock
,
137 long unix_extend (MAILSTREAM
*stream
,unsigned long size
);
138 void unix_write (UNIXFILE
*f
,char *s
,unsigned long i
);
139 void unix_phys_write (UNIXFILE
*f
,char *buf
,size_t size
);
141 /* mbox mail routines */
143 /* Function prototypes */
145 DRIVER
*mbox_valid (char *name
);
146 long mbox_create (MAILSTREAM
*stream
,char *mailbox
);
147 long mbox_delete (MAILSTREAM
*stream
,char *mailbox
);
148 long mbox_rename (MAILSTREAM
*stream
,char *old
,char *newname
);
149 long mbox_status (MAILSTREAM
*stream
,char *mbx
,long flags
);
150 MAILSTREAM
*mbox_open (MAILSTREAM
*stream
);
151 long mbox_ping (MAILSTREAM
*stream
);
152 void mbox_check (MAILSTREAM
*stream
);
153 long mbox_expunge (MAILSTREAM
*stream
,char *sequence
,long options
);
154 long mbox_append (MAILSTREAM
*stream
,char *mailbox
,append_t af
,void *data
);
157 /* UNIX mail routines */
160 /* Driver dispatch used by MAIL */
162 DRIVER unixdriver
= {
163 "unix", /* driver name */
165 DR_LOCAL
|DR_MAIL
|DR_LOCKING
|DR_NONEWMAILRONLY
|DR_XPOINT
,
166 (DRIVER
*) NIL
, /* next driver */
167 unix_valid
, /* mailbox is valid for us */
168 unix_parameters
, /* manipulate parameters */
169 unix_scan
, /* scan mailboxes */
170 unix_list
, /* list mailboxes */
171 unix_lsub
, /* list subscribed mailboxes */
172 NIL
, /* subscribe to mailbox */
173 NIL
, /* unsubscribe from mailbox */
174 unix_create
, /* create mailbox */
175 unix_delete
, /* delete mailbox */
176 unix_rename
, /* rename mailbox */
177 mail_status_default
, /* status of mailbox */
178 unix_open
, /* open mailbox */
179 unix_close
, /* close mailbox */
180 NIL
, /* fetch message "fast" attributes */
181 NIL
, /* fetch message flags */
182 NIL
, /* fetch overview */
183 NIL
, /* fetch message envelopes */
184 unix_header
, /* fetch message header */
185 unix_text
, /* fetch message text */
186 NIL
, /* fetch partial message text */
187 NIL
, /* unique identifier */
188 NIL
, /* message number */
189 NIL
, /* modify flags */
190 unix_flagmsg
, /* per-message modify flags */
191 NIL
, /* search for message based on criteria */
192 NIL
, /* sort messages */
193 NIL
, /* thread messages */
194 unix_ping
, /* ping mailbox to see if still alive */
195 unix_check
, /* check for new messages */
196 unix_expunge
, /* expunge deleted messages */
197 unix_copy
, /* copy messages to another mailbox */
198 unix_append
, /* append string message to mailbox */
199 NIL
, /* garbage collect stream */
200 NIL
/* renew stream */
203 /* prototype stream */
204 MAILSTREAM unixproto
= {&unixdriver
};
206 /* driver parameters */
207 static long unix_fromwidget
= T
;
209 /* UNIX mail validate mailbox
210 * Accepts: mailbox name
211 * Returns: our driver if name is valid, NIL otherwise
214 DRIVER
*unix_valid (char *name
)
218 char *t
,file
[MAILTMPLEN
];
221 errno
= EINVAL
; /* assume invalid argument */
222 /* must be non-empty file */
223 if ((t
= dummy_file (file
,name
)) && !stat (t
,&sbuf
)) {
224 if (!sbuf
.st_size
)errno
= 0;/* empty file */
225 else if ((fd
= open (file
,O_RDONLY
,NIL
)) >= 0) {
226 /* OK if mailbox format good */
227 if (unix_isvalid_fd (fd
)) ret
= &unixdriver
;
228 else errno
= -1; /* invalid format */
229 close (fd
); /* close the file */
230 /* \Marked status? */
231 if ((sbuf
.st_ctime
> sbuf
.st_atime
) || (sbuf
.st_mtime
> sbuf
.st_atime
)) {
232 tp
[0] = sbuf
.st_atime
; /* yes, preserve atime and mtime */
233 tp
[1] = sbuf
.st_mtime
;
234 utime (file
,tp
); /* set the times */
238 return ret
; /* return what we should */
241 /* UNIX mail test for valid mailbox
242 * Accepts: file descriptor
244 * Returns: T if valid, NIL otherwise
247 long unix_isvalid_fd (int fd
)
251 char tmp
[MAILTMPLEN
],*s
,*t
,c
= '\n';
252 memset (tmp
,'\0',MAILTMPLEN
);
253 if (read (fd
,tmp
,MAILTMPLEN
-1) >= 0) {
254 for (s
= tmp
; (*s
== '\r') || (*s
== '\n') || (*s
== ' ') || (*s
== '\t');)
256 if (c
== '\n') VALID (s
,t
,ret
,zn
);
258 return ret
; /* return what we should */
262 /* UNIX manipulate driver parameters
263 * Accepts: function code
264 * function-dependent value
265 * Returns: function-dependent return value
268 void *unix_parameters (long function
,void *value
)
271 switch ((int) function
) {
273 if (value
) ret
= dummy_file ((char *) value
,"INBOX");
276 unix_fromwidget
= (long) value
;
278 ret
= (void *) unix_fromwidget
;
284 /* UNIX mail scan mailboxes
285 * Accepts: mail stream
291 void unix_scan (MAILSTREAM
*stream
,char *ref
,char *pat
,char *contents
)
293 if (stream
) dummy_scan (NIL
,ref
,pat
,contents
);
297 /* UNIX mail list mailboxes
298 * Accepts: mail stream
303 void unix_list (MAILSTREAM
*stream
,char *ref
,char *pat
)
305 if (stream
) dummy_list (NIL
,ref
,pat
);
309 /* UNIX mail list subscribed mailboxes
310 * Accepts: mail stream
315 void unix_lsub (MAILSTREAM
*stream
,char *ref
,char *pat
)
317 if (stream
) dummy_lsub (NIL
,ref
,pat
);
320 /* UNIX mail create mailbox
321 * Accepts: MAIL stream
322 * mailbox name to create
323 * Returns: T on success, NIL on failure
326 long unix_create (MAILSTREAM
*stream
,char *mailbox
)
328 char *s
,mbx
[MAILTMPLEN
],tmp
[MAILTMPLEN
];
331 time_t ti
= time (0);
332 if (!(s
= dummy_file (mbx
,mailbox
))) {
333 sprintf (tmp
,"Can't create %.80s: invalid name",mailbox
);
336 /* create underlying file */
337 else if (dummy_create_path (stream
,s
,get_dir_protection (mailbox
))) {
338 /* done if dir-only or whiner */
339 if (((s
= strrchr (s
,'/')) && !s
[1]) ||
340 mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
)) ret
= T
;
341 else if ((fd
= open (mbx
,O_WRONLY
,
342 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
))) < 0) {
343 sprintf (tmp
,"Can't reopen mailbox node %.80s: %s",mbx
,strerror (errno
));
345 unlink (mbx
); /* delete the file */
347 else { /* initialize header */
348 memset (tmp
,'\0',MAILTMPLEN
);
349 sprintf (tmp
,"From %s %sDate: ",pseudo_from
,ctime (&ti
));
350 rfc822_fixed_date (s
= tmp
+ strlen (tmp
));
351 /* write the pseudo-header */
352 sprintf (s
+= strlen (s
),
353 "\nFrom: %s <%s@%s>\nSubject: %s\nX-IMAP: %010lu 0000000000",
354 pseudo_name
,pseudo_from
,mylocalhost (),pseudo_subject
,
356 for (i
= 0; i
< NUSERFLAGS
; ++i
) if (default_user_flag (i
))
357 sprintf (s
+= strlen (s
)," %s",default_user_flag (i
));
358 sprintf (s
+= strlen (s
),"\nStatus: RO\n\n%s\n\n",pseudo_msg
);
359 if (write (fd
,tmp
,strlen (tmp
)) > 0) ret
= T
;
361 sprintf (tmp
,"Can't initialize mailbox node %.80s: %s",mbx
,
364 unlink (mbx
); /* delete the file */
366 close (fd
); /* close file */
369 /* set proper protections */
370 return ret
? set_mbx_protections (mailbox
,mbx
) : NIL
;
373 /* UNIX mail delete mailbox
374 * Accepts: MAIL stream
375 * mailbox name to delete
376 * Returns: T on success, NIL on failure
379 long unix_delete (MAILSTREAM
*stream
,char *mailbox
)
381 return unix_rename (stream
,mailbox
,NIL
);
385 /* UNIX mail rename mailbox
386 * Accepts: MAIL stream
388 * new mailbox name (or NIL for delete)
389 * Returns: T on success, NIL on failure
392 long unix_rename (MAILSTREAM
*stream
,char *old
,char *newname
)
396 char tmp
[MAILTMPLEN
],file
[MAILTMPLEN
],lock
[MAILTMPLEN
];
401 MM_CRITICAL (stream
); /* get the c-client lock */
402 if (!dummy_file (file
,old
) ||
403 (newname
&& (!((s
= mailboxfile (tmp
,newname
)) && *s
) ||
404 ((s
= strrchr (tmp
,'/')) && !s
[1])))){
406 sprintf (tmp
, "Can't rename mailbox %.80s to %.80s: invalid name",
409 sprintf (tmp
, "Can't delete mailbox %.80s: invalid name",old
);
410 /* lock out other c-clients */
411 } else if ((ld
= lockname (lock
,file
,LOCK_EX
|LOCK_NB
,&i
)) < 0)
412 sprintf (tmp
,"Mailbox %.80s is in use by another process",old
);
415 if ((fd
= unix_lock (file
,O_RDWR
,
416 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
),
417 &lockx
,LOCK_EX
)) < 0)
418 sprintf (tmp
,"Can't lock mailbox %.80s: %s",old
,strerror (errno
));
420 if (newname
) { /* want rename? */
421 /* found superior to destination name? */
422 if ((s
= strrchr (s
,'/')) != NULL
) {
423 c
= *++s
; /* remember first character of inferior */
424 *s
= '\0'; /* tie off to get just superior */
425 /* name doesn't exist, create it */
426 if ((stat (tmp
,&sbuf
) || ((sbuf
.st_mode
& S_IFMT
) != S_IFDIR
)) &&
427 !dummy_create_path (stream
,tmp
,get_dir_protection (newname
))) {
428 unix_unlock (fd
,NIL
,&lockx
);
429 unix_unlock (ld
,NIL
,NIL
);
431 MM_NOCRITICAL (stream
);
432 return ret
; /* return success or failure */
434 *s
= c
; /* restore full name */
436 if (rename (file
,tmp
))
437 sprintf (tmp
,"Can't rename mailbox %.80s to %.80s: %s",old
,newname
,
439 else ret
= T
; /* set success */
441 else if (unlink (file
))
442 sprintf (tmp
,"Can't delete mailbox %.80s: %s",old
,strerror (errno
));
443 else ret
= T
; /* set success */
444 unix_unlock (fd
,NIL
,&lockx
);
446 unix_unlock (ld
,NIL
,NIL
); /* flush the lock */
449 MM_NOCRITICAL (stream
); /* no longer critical */
450 if (!ret
) MM_LOG (tmp
,ERROR
); /* log error */
451 return ret
; /* return success or failure */
455 * Accepts: Stream to open
456 * Returns: Stream on success, NIL on failure
459 MAILSTREAM
*unix_open (MAILSTREAM
*stream
)
463 char tmp
[MAILTMPLEN
];
466 /* return prototype for OP_PROTOTYPE call */
467 if (!stream
) return user_flags (&unixproto
);
468 retry
= stream
->silent
? 1 : KODRETRY
;
469 if (stream
->local
) fatal ("unix recycle stream");
470 stream
->local
= memset (fs_get (sizeof (UNIXLOCAL
)),0,sizeof (UNIXLOCAL
));
471 /* note if an INBOX or not */
472 stream
->inbox
= !compare_cstring (stream
->mailbox
,"INBOX");
473 /* canonicalize the stream mailbox name */
474 if (!dummy_file (tmp
,stream
->mailbox
)) {
475 sprintf (tmp
,"Can't open - invalid name: %.80s",stream
->mailbox
);
480 fs_give ((void **) &stream
->mailbox
);
481 /* save canonical name */
482 stream
->mailbox
= cpystr (tmp
);
483 LOCAL
->fd
= LOCAL
->ld
= -1; /* no file or state locking yet */
484 LOCAL
->buf
= (char *) fs_get (CHUNKSIZE
);
485 LOCAL
->buflen
= CHUNKSIZE
- 1;
486 LOCAL
->text
.data
= (unsigned char *) fs_get (CHUNKSIZE
);
487 LOCAL
->text
.size
= CHUNKSIZE
- 1;
488 LOCAL
->linebuf
= (char *) fs_get (CHUNKSIZE
);
489 LOCAL
->linebuflen
= CHUNKSIZE
- 1;
490 stream
->sequence
++; /* bump sequence number */
492 /* make lock for read/write access */
493 if (!stream
->rdonly
) while (retry
) {
494 /* try to lock file */
495 if ((fd
= lockname (tmp
,stream
->mailbox
,LOCK_EX
|LOCK_NB
,&i
)) < 0) {
496 /* suppressing kiss-of-death? */
497 if (stream
->nokod
) retry
= 0;
498 /* no, first time through? */
499 else if (retry
-- == KODRETRY
) {
500 /* learned other guy's PID and can signal? */
501 if (i
&& !kill ((int) i
,SIGUSR2
)) {
502 sprintf (tmp
,"Trying to get mailbox lock from process %ld",i
);
505 else retry
= 0; /* give up */
507 if (!stream
->silent
) { /* nothing if silent stream */
508 if (retry
) sleep (1); /* wait a second before trying again */
509 else MM_LOG ("Mailbox is open by another process, access is readonly",
513 else { /* got the lock, nobody else can alter state */
514 LOCAL
->ld
= fd
; /* note lock's fd and name */
515 LOCAL
->lname
= cpystr (tmp
);
516 /* make sure mode OK (don't use fchmod()) */
517 chmod (LOCAL
->lname
,(long) mail_parameters (NIL
,GET_LOCKPROTECTION
,NIL
));
518 if (stream
->silent
) i
= 0;/* silent streams won't accept KOD */
519 else { /* note our PID in the lock */
520 sprintf (tmp
,"%d",getpid ());
521 write (fd
,tmp
,(i
= strlen (tmp
))+1);
523 ftruncate (fd
,i
); /* make sure tied off */
524 fsync (fd
); /* make sure it's available */
525 retry
= 0; /* no more need to try */
530 stream
->nmsgs
= stream
->recent
= 0;
531 /* will we be able to get write access? */
532 if ((LOCAL
->ld
>= 0) && access (stream
->mailbox
,W_OK
) && (errno
== EACCES
)) {
533 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN
);
534 flock (LOCAL
->ld
,LOCK_UN
); /* release the lock */
535 close (LOCAL
->ld
); /* close the lock file */
536 LOCAL
->ld
= -1; /* no more lock fd */
537 unlink (LOCAL
->lname
); /* delete it */
539 /* reset UID validity */
540 stream
->uid_validity
= stream
->uid_last
= 0;
541 if (stream
->silent
&& !stream
->rdonly
&& (LOCAL
->ld
< 0))
542 unix_abort (stream
); /* abort if can't get RW silent stream */
544 else if (unix_parse (stream
,&lock
,LOCK_SH
)) {
545 unix_unlock (LOCAL
->fd
,stream
,&lock
);
546 mail_unlock (stream
);
547 MM_NOCRITICAL (stream
); /* done with critical */
549 if (!LOCAL
) return NIL
; /* failure if stream died */
550 /* make sure upper level knows readonly */
551 stream
->rdonly
= (LOCAL
->ld
< 0);
552 /* notify about empty mailbox */
553 if (!(stream
->nmsgs
|| stream
->silent
)) MM_LOG ("Mailbox is empty",NIL
);
554 if (!stream
->rdonly
) { /* flags stick if readwrite */
555 stream
->perm_seen
= stream
->perm_deleted
=
556 stream
->perm_flagged
= stream
->perm_answered
= stream
->perm_draft
= T
;
557 if (!stream
->uid_nosticky
) {/* users with lives get permanent keywords */
558 stream
->perm_user_flags
= 0xffffffff;
559 /* and maybe can create them too! */
560 stream
->kwd_create
= stream
->user_flags
[NUSERFLAGS
-1] ? NIL
: T
;
563 return stream
; /* return stream alive to caller */
568 * Accepts: MAIL stream
572 void unix_close (MAILSTREAM
*stream
,long options
)
574 int silent
= stream
->silent
;
575 stream
->silent
= T
; /* go silent */
576 /* expunge if requested */
577 if (options
& CL_EXPUNGE
) unix_expunge (stream
,NIL
,NIL
);
578 /* else dump final checkpoint */
579 else if (LOCAL
->dirty
) unix_check (stream
);
580 stream
->silent
= silent
; /* restore old silence state */
581 unix_abort (stream
); /* now punt the file and local data */
584 /* UNIX mail fetch message header
585 * Accepts: MAIL stream
587 * pointer to returned header text length
589 * Returns: message header in RFC822 format
593 STRINGLIST XIMAPbase
= {{"X-IMAPbase", 10}, NIL
};
594 STRINGLIST XIMAP
= {{"X-IMAP", 6}, &XIMAPbase
};
595 STRINGLIST XUID
= {{"X-UID", 5}, &XIMAP
};
596 STRINGLIST XKeywords
= {{"X-Keywords", 10}, &XUID
};
597 STRINGLIST XStatus
= {{"X-Status", 8}, &XKeywords
};
598 STRINGLIST Status
= {{"Status", 6}, &XStatus
};
600 static STRINGLIST
*unix_hlines
= &Status
;
602 char *unix_header (MAILSTREAM
*stream
,unsigned long msgno
,
603 unsigned long *length
,long flags
)
606 unsigned char *s
,*t
,*tl
;
607 *length
= 0; /* default to empty */
608 if (flags
& FT_UID
) return "";/* UID call "impossible" */
609 elt
= mail_elt (stream
,msgno
);/* get cache */
610 /* go to header position */
611 lseek (LOCAL
->fd
,elt
->private.special
.offset
+
612 elt
->private.msg
.header
.offset
,L_SET
);
614 if (flags
& FT_INTERNAL
) { /* initial data OK? */
615 if (elt
->private.msg
.header
.text
.size
> LOCAL
->buflen
) {
616 fs_give ((void **) &LOCAL
->buf
);
617 LOCAL
->buf
= (char *) fs_get ((LOCAL
->buflen
=
618 elt
->private.msg
.header
.text
.size
) + 1);
621 read (LOCAL
->fd
,LOCAL
->buf
,elt
->private.msg
.header
.text
.size
);
622 /* got text, tie off string */
623 LOCAL
->buf
[*length
= elt
->private.msg
.header
.text
.size
] = '\0';
624 /* squeeze out CRs (in case from PC) */
625 for (s
= t
= LOCAL
->buf
,tl
= LOCAL
->buf
+ *length
; t
< tl
; t
++)
626 if (*t
!= '\r') *s
++ = *t
;
628 *length
= s
- LOCAL
->buf
; /* adjust length */
630 else { /* need to make a CRLF version */
631 read (LOCAL
->fd
,s
= (char *) fs_get (elt
->private.msg
.header
.text
.size
+1),
632 elt
->private.msg
.header
.text
.size
);
633 /* tie off string, and convert to CRLF */
634 s
[elt
->private.msg
.header
.text
.size
] = '\0';
635 *length
= strcrlfcpy (&LOCAL
->buf
,&LOCAL
->buflen
,s
,
636 elt
->private.msg
.header
.text
.size
);
637 fs_give ((void **) &s
); /* free readin buffer */
638 /* squeeze out spurious CRs */
639 for (s
= t
= LOCAL
->buf
,tl
= LOCAL
->buf
+ *length
; t
< tl
; t
++)
640 if ((*t
!= '\r') || (t
[1] == '\n')) *s
++ = *t
;
642 *length
= s
- LOCAL
->buf
; /* adjust length */
644 *length
= mail_filter (LOCAL
->buf
,*length
,unix_hlines
,FT_NOT
);
645 return (char *) LOCAL
->buf
; /* return processed copy */
648 /* UNIX mail fetch message text
649 * Accepts: MAIL stream
651 * pointer to returned stringstruct
653 * Returns: T on success, NIL if failure
656 long unix_text (MAILSTREAM
*stream
,unsigned long msgno
,STRING
*bs
,long flags
)
661 /* UID call "impossible" */
662 if (flags
& FT_UID
) return NIL
;
663 elt
= mail_elt (stream
,msgno
);/* get cache element */
664 /* if message not seen */
665 if (!(flags
& FT_PEEK
) && !elt
->seen
) {
666 /* mark message seen and dirty */
667 elt
->seen
= elt
->private.dirty
= LOCAL
->dirty
= T
;
668 MM_FLAGS (stream
,msgno
);
670 s
= unix_text_work (stream
,elt
,&i
,flags
);
671 INIT (bs
,mail_string
,s
,i
); /* set up stringstruct */
672 return T
; /* success */
675 /* UNIX mail fetch message text worker routine
676 * Accepts: MAIL stream
677 * message cache element
678 * pointer to returned header text length
682 char *unix_text_work (MAILSTREAM
*stream
,MESSAGECACHE
*elt
,
683 unsigned long *length
,long flags
)
687 unsigned char c
,*s
,*t
,*tl
,tmp
[CHUNKSIZE
];
688 /* go to text position */
689 lseek (LOCAL
->fd
,elt
->private.special
.offset
+
690 elt
->private.msg
.text
.offset
,L_SET
);
691 if (flags
& FT_INTERNAL
) { /* initial data OK? */
692 if (elt
->private.msg
.text
.text
.size
> LOCAL
->buflen
) {
693 fs_give ((void **) &LOCAL
->buf
);
694 LOCAL
->buf
= (char *) fs_get ((LOCAL
->buflen
=
695 elt
->private.msg
.text
.text
.size
) + 1);
698 read (LOCAL
->fd
,LOCAL
->buf
,elt
->private.msg
.text
.text
.size
);
699 /* got text, tie off string */
700 LOCAL
->buf
[*length
= elt
->private.msg
.text
.text
.size
] = '\0';
701 /* squeeze out CRs (in case from PC) */
702 for (s
= t
= LOCAL
->buf
,tl
= LOCAL
->buf
+ *length
; t
< tl
; t
++)
703 if (*t
!= '\r') *s
++ = *t
;
705 *length
= s
- LOCAL
->buf
; /* adjust length */
706 return (char *) LOCAL
->buf
;
709 /* have it cached already? */
710 if (elt
->private.uid
!= LOCAL
->uid
) {
711 /* not cached, cache it now */
712 LOCAL
->uid
= elt
->private.uid
;
713 /* is buffer big enough? */
714 if (elt
->rfc822_size
> LOCAL
->text
.size
) {
715 /* excessively conservative, but the right thing is too hard to do */
716 fs_give ((void **) &LOCAL
->text
.data
);
717 LOCAL
->text
.data
= (unsigned char *)
718 fs_get ((LOCAL
->text
.size
= elt
->rfc822_size
) + 1);
720 d
.fd
= LOCAL
->fd
; /* yes, set up file descriptor */
721 d
.pos
= elt
->private.special
.offset
+ elt
->private.msg
.text
.offset
;
722 d
.chunk
= tmp
; /* initial buffer chunk */
723 d
.chunksize
= CHUNKSIZE
; /* file chunk size */
724 INIT (&bs
,fd_string
,&d
,elt
->private.msg
.text
.text
.size
);
725 for (s
= (char *) LOCAL
->text
.data
; SIZE (&bs
);) switch (c
= SNX (&bs
)) {
726 case '\r': /* carriage return seen */
729 *s
++ = '\r'; /* insert a CR */
731 *s
++ = c
; /* copy characters */
733 *s
= '\0'; /* tie off buffer */
734 /* calculate length of cached data */
735 LOCAL
->textlen
= s
- LOCAL
->text
.data
;
737 *length
= LOCAL
->textlen
; /* return from cache */
738 return (char *) LOCAL
->text
.data
;
741 /* UNIX per-message modify flag
742 * Accepts: MAIL stream
743 * message cache element
746 void unix_flagmsg (MAILSTREAM
*stream
,MESSAGECACHE
*elt
)
748 /* only after finishing */
749 if (elt
->valid
) elt
->private.dirty
= LOCAL
->dirty
= T
;
753 /* UNIX mail ping mailbox
754 * Accepts: MAIL stream
755 * Returns: T if stream alive, else NIL
758 long unix_ping (MAILSTREAM
*stream
)
763 /* big no-op if not readwrite */
764 if (LOCAL
&& (LOCAL
->ld
>= 0) && !stream
->lock
) {
765 if (stream
->rdonly
) { /* does he want to give up readwrite? */
766 /* checkpoint if we changed something */
767 if (LOCAL
->dirty
) unix_check (stream
);
768 flock (LOCAL
->ld
,LOCK_UN
);/* release readwrite lock */
769 close (LOCAL
->ld
); /* close the readwrite lock file */
770 LOCAL
->ld
= -1; /* no more readwrite lock fd */
771 unlink (LOCAL
->lname
); /* delete the readwrite lock file */
773 else { /* see if need to reparse */
774 if (!(reparse
= (long) mail_parameters (NIL
,GET_NETFSSTATBUG
,NIL
))) {
775 /* get current mailbox size */
776 if (LOCAL
->fd
>= 0) fstat (LOCAL
->fd
,&sbuf
);
777 else if (stat (stream
->mailbox
,&sbuf
)) {
778 sprintf (LOCAL
->buf
,"Mailbox stat failed, aborted: %s",
780 MM_LOG (LOCAL
->buf
,ERROR
);
784 reparse
= (sbuf
.st_size
!= LOCAL
->filesize
);
786 /* parse if mailbox changed */
787 if ((LOCAL
->ddirty
|| reparse
) && unix_parse (stream
,&lock
,LOCK_EX
)) {
788 /* force checkpoint if double-dirty */
789 if (LOCAL
->ddirty
) unix_rewrite (stream
,NIL
,&lock
,NIL
);
791 else unix_unlock (LOCAL
->fd
,stream
,&lock
);
792 mail_unlock (stream
); /* and stream */
793 MM_NOCRITICAL (stream
); /* done with critical */
797 return LOCAL
? LONGT
: NIL
; /* return if still alive */
800 /* UNIX mail check mailbox
801 * Accepts: MAIL stream
804 void unix_check (MAILSTREAM
*stream
)
807 /* parse and lock mailbox */
808 if (LOCAL
&& (LOCAL
->ld
>= 0) && !stream
->lock
&&
809 unix_parse (stream
,&lock
,LOCK_EX
)) {
810 /* any unsaved changes? */
811 if (LOCAL
->dirty
&& unix_rewrite (stream
,NIL
,&lock
,NIL
)) {
812 if (!stream
->silent
) MM_LOG ("Checkpoint completed",NIL
);
814 /* no checkpoint needed, just unlock */
815 else unix_unlock (LOCAL
->fd
,stream
,&lock
);
816 mail_unlock (stream
); /* unlock the stream */
817 MM_NOCRITICAL (stream
); /* done with critical */
822 /* UNIX mail expunge mailbox
823 * Accepts: MAIL stream
824 * sequence to expunge if non-NIL
829 long unix_expunge (MAILSTREAM
*stream
,char *sequence
,long options
)
835 /* parse and lock mailbox */
836 if ((ret
= (sequence
? ((options
& EX_UID
) ?
837 mail_uid_sequence (stream
,sequence
) :
838 mail_sequence (stream
,sequence
)) : LONGT
) != 0L) &&
839 LOCAL
&& (LOCAL
->ld
>= 0) && !stream
->lock
&&
840 unix_parse (stream
,&lock
,LOCK_EX
)) {
841 /* check expunged messages if not dirty */
842 for (i
= 1; !LOCAL
->dirty
&& (i
<= stream
->nmsgs
); i
++) {
843 MESSAGECACHE
*elt
= mail_elt (stream
,i
);
844 if (mail_elt (stream
,i
)->deleted
) LOCAL
->dirty
= T
;
846 if (!LOCAL
->dirty
) { /* not dirty and no expunged messages */
847 unix_unlock (LOCAL
->fd
,stream
,&lock
);
848 msg
= "No messages deleted, so no update needed";
850 else if (unix_rewrite (stream
,&i
,&lock
,sequence
? LONGT
: NIL
)) {
851 if (i
) sprintf (msg
= LOCAL
->buf
,"Expunged %lu messages",i
);
852 else msg
= "Mailbox checkpointed, but no messages expunged";
855 else unix_unlock (LOCAL
->fd
,stream
,&lock
);
856 mail_unlock (stream
); /* unlock the stream */
857 MM_NOCRITICAL (stream
); /* done with critical */
858 if (msg
&& !stream
->silent
) MM_LOG (msg
,NIL
);
860 else if (!stream
->silent
) MM_LOG("Expunge ignored on readonly mailbox",WARN
);
864 /* UNIX mail copy message(s)
865 * Accepts: MAIL stream
867 * destination mailbox
869 * Returns: T if copy successful, else NIL
872 long unix_copy (MAILSTREAM
*stream
,char *sequence
,char *mailbox
,long options
)
876 char *s
,file
[MAILTMPLEN
];
883 (mailproxycopy_t
) mail_parameters (stream
,GET_MAILPROXYCOPY
,NIL
);
884 copyuid_t cu
= (copyuid_t
) (mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
) ?
885 NIL
: mail_parameters (NIL
,GET_COPYUID
,NIL
));
886 SEARCHSET
*source
= cu
? mail_newsearchset () : NIL
;
887 SEARCHSET
*dest
= cu
? mail_newsearchset () : NIL
;
888 MAILSTREAM
*tstream
= NIL
;
890 for (d
= (DRIVER
*) mail_parameters (NIL
,GET_DRIVERS
,NIL
);
891 (d
&& strcmp (d
->name
,"mbox") && !(d
->flags
& DR_DISABLE
));
892 d
= d
->next
); /* see if mbox driver active */
893 if (!((options
& CP_UID
) ? mail_uid_sequence (stream
,sequence
) :
894 mail_sequence (stream
,sequence
))) return NIL
;
895 /* make sure destination is valid */
896 if (!((d
&& mbox_valid (mailbox
) && (mailbox
= "mbox")) ||
897 unix_valid (mailbox
) || !errno
))
899 case ENOENT
: /* no such file? */
900 if (compare_cstring (mailbox
,"INBOX")) {
901 MM_NOTIFY (stream
,"[TRYCREATE] Must create mailbox before copy",NIL
);
904 if (pc
) return (*pc
) (stream
,sequence
,mailbox
,options
);
905 unix_create (NIL
,"INBOX");/* create empty INBOX */
906 case EACCES
: /* file protected */
907 sprintf (LOCAL
->buf
,"Can't access destination: %.80s",mailbox
);
908 MM_LOG (LOCAL
->buf
,ERROR
);
911 if (pc
) return (*pc
) (stream
,sequence
,mailbox
,options
);
912 sprintf (LOCAL
->buf
,"Invalid UNIX-format mailbox name: %.80s",mailbox
);
913 MM_LOG (LOCAL
->buf
,ERROR
);
916 if (pc
) return (*pc
) (stream
,sequence
,mailbox
,options
);
917 sprintf (LOCAL
->buf
,"Not a UNIX-format mailbox: %.80s",mailbox
);
918 MM_LOG (LOCAL
->buf
,ERROR
);
922 /* try to open readwrite for UIDPLUS */
923 if (cu
&& (tstream
= mail_open_work (&unixdriver
,NIL
,mailbox
,
924 OP_SILENT
|OP_NOKOD
)) && tstream
->rdonly
)
925 tstream
= mail_close (tstream
);
926 if (cu
&& !tstream
) { /* wanted a COPYUID? */
927 sprintf (LOCAL
->buf
,"Unable to write-open mailbox for COPYUID: %.80s",
929 MM_LOG (LOCAL
->buf
,WARN
);
930 cu
= NIL
; /* don't try to do COPYUID */
932 LOCAL
->buf
[0] = '\0';
933 MM_CRITICAL (stream
); /* go critical */
934 if ((fd
= unix_lock (dummy_file (file
,mailbox
),O_WRONLY
|O_APPEND
,
935 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
),
936 &lock
,LOCK_EX
)) < 0) {
937 MM_NOCRITICAL (stream
); /* done with critical */
938 sprintf (LOCAL
->buf
,"Can't open destination mailbox: %s",strerror (errno
));
939 MM_LOG (LOCAL
->buf
,ERROR
);/* log the error */
940 return NIL
; /* failed */
942 fstat (fd
,&sbuf
); /* get current file size */
943 /* write all requested messages to mailbox */
944 for (i
= 1; ret
&& (i
<= stream
->nmsgs
); i
++)
945 if ((elt
= mail_elt (stream
,i
))->sequence
) {
946 lseek (LOCAL
->fd
,elt
->private.special
.offset
,L_SET
);
947 read (LOCAL
->fd
,LOCAL
->buf
,elt
->private.special
.text
.size
);
948 if (write (fd
,LOCAL
->buf
,elt
->private.special
.text
.size
) < 0) ret
= NIL
;
949 else { /* internal header succeeded */
950 s
= unix_header (stream
,i
,&j
,FT_INTERNAL
);
951 /* header size, sans trailing newline */
952 if (j
&& (s
[j
- 2] == '\n')) j
--;
953 if (write (fd
,s
,j
) < 0) ret
= NIL
;
954 else { /* message header succeeded */
955 j
= tstream
? /* write UIDPLUS data if have readwrite */
956 unix_xstatus (stream
,LOCAL
->buf
,elt
,++(tstream
->uid_last
),LONGT
) :
957 unix_xstatus (stream
,LOCAL
->buf
,elt
,NIL
,NIL
);
958 if (write (fd
,LOCAL
->buf
,j
) < 0) ret
= NIL
;
959 else { /* message status succeeded */
960 s
= unix_text_work (stream
,elt
,&j
,FT_INTERNAL
);
961 if ((write (fd
,s
,j
) < 0) || (write (fd
,"\n",1) < 0)) ret
= NIL
;
962 else if (cu
) { /* need to pass back new UID? */
963 mail_append_set (source
,mail_uid (stream
,i
));
964 mail_append_set (dest
,tstream
->uid_last
);
971 if (!ret
|| fsync (fd
)) { /* force out the update */
972 sprintf (LOCAL
->buf
,"Message copy failed: %s",strerror (errno
));
973 ftruncate (fd
,sbuf
.st_size
);
976 /* force UIDVALIDITY assignment now */
977 if (tstream
&& !tstream
->uid_validity
) tstream
->uid_validity
= time (0);
978 /* return sets if doing COPYUID */
979 if (cu
&& ret
) (*cu
) (stream
,mailbox
,tstream
->uid_validity
,source
,dest
);
980 else { /* flush any sets we may have built */
981 mail_free_searchset (&source
);
982 mail_free_searchset (&dest
);
984 tp
[1] = time (0); /* set mtime to now */
985 if (ret
) tp
[0] = tp
[1] - 1; /* set atime to now-1 if successful copy */
986 else tp
[0] = /* else preserve \Marked status */
987 ((sbuf
.st_ctime
> sbuf
.st_atime
) || (sbuf
.st_mtime
> sbuf
.st_atime
)) ?
988 sbuf
.st_atime
: tp
[1];
989 utime (file
,tp
); /* set the times */
990 unix_unlock (fd
,NIL
,&lock
); /* unlock and close mailbox */
991 if (tstream
) { /* update last UID if we can */
992 UNIXLOCAL
*local
= (UNIXLOCAL
*) tstream
->local
;
993 local
->dirty
= T
; /* do a rewrite */
994 local
->appending
= T
; /* but not at the cost of marking as old */
995 tstream
= mail_close (tstream
);
998 if (!ret
) MM_LOG (LOCAL
->buf
,ERROR
);
999 /* delete if requested message */
1000 else if (options
& CP_MOVE
) for (i
= 1; i
<= stream
->nmsgs
; i
++)
1001 if ((elt
= mail_elt (stream
,i
))->sequence
)
1002 elt
->deleted
= elt
->private.dirty
= LOCAL
->dirty
= T
;
1003 MM_NOCRITICAL (stream
); /* release critical */
1007 /* UNIX mail append message from stringstruct
1008 * Accepts: MAIL stream
1009 * destination mailbox
1012 * Returns: T if append successful, else NIL
1015 #define BUFLEN 8*MAILTMPLEN
1017 long unix_append (MAILSTREAM
*stream
,char *mailbox
,append_t af
,void *data
)
1022 char *flags
,*date
,buf
[BUFLEN
],tmp
[MAILTMPLEN
],file
[MAILTMPLEN
];
1028 unsigned long uidlocation
= 0;
1029 appenduid_t au
= (appenduid_t
)
1030 (mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
) ? NIL
:
1031 mail_parameters (NIL
,GET_APPENDUID
,NIL
));
1032 SEARCHSET
*dst
= au
? mail_newsearchset () : NIL
;
1034 MAILSTREAM
*tstream
= NIL
;
1035 if (!stream
) { /* stream specified? */
1036 stream
= &unixproto
; /* no, default stream to prototype */
1037 for (i
= 0; i
< NUSERFLAGS
&& stream
->user_flags
[i
]; ++i
)
1038 fs_give ((void **) &stream
->user_flags
[i
]);
1040 if (!unix_valid (mailbox
)) switch (errno
) {
1041 case ENOENT
: /* no such file? */
1042 if (compare_cstring (mailbox
,"INBOX")) {
1043 MM_NOTIFY (stream
,"[TRYCREATE] Must create mailbox before append",NIL
);
1046 unix_create (NIL
,"INBOX"); /* create empty INBOX */
1047 case 0: /* merely empty file? */
1050 case EACCES
: /* file protected */
1051 sprintf (tmp
,"Can't access destination: %.80s",mailbox
);
1055 sprintf (tmp
,"Invalid UNIX-format mailbox name: %.80s",mailbox
);
1059 sprintf (tmp
,"Not a UNIX-format mailbox: %.80s",mailbox
);
1063 /* get sniffing stream for keywords */
1064 else if (!(tstream
= mail_open (NIL
,mailbox
,
1065 OP_READONLY
|OP_SILENT
|OP_NOKOD
|OP_SNIFF
))) {
1066 sprintf (tmp
,"Unable to examine mailbox for APPEND: %.80s",mailbox
);
1071 /* get first message */
1072 if (!MM_APPEND (af
) (tstream
,data
,&flags
,&date
,&message
)) return NIL
;
1073 if (!(sf
= tmpfile ())) { /* must have scratch file */
1074 sprintf (tmp
,".%lx.%lx",(unsigned long) time (0),(unsigned long)getpid ());
1075 if (!stat (tmp
,&sbuf
) || !(sf
= fopen (tmp
,"wb+"))) {
1076 sprintf (tmp
,"Unable to create scratch file: %.80s",strerror (errno
));
1082 do { /* parse date */
1083 if (!date
) rfc822_date (date
= tmp
);
1084 if (!mail_parse_date (&elt
,date
)) {
1085 sprintf (tmp
,"Bad date in append: %.80s",date
);
1088 else { /* user wants to suppress time zones? */
1089 if (mail_parameters (NIL
,GET_NOTIMEZONES
,NIL
)) {
1090 time_t when
= mail_longdate (&elt
);
1091 date
= ctime (&when
); /* use traditional date */
1093 /* use POSIX-style date */
1094 else date
= mail_cdate (tmp
,&elt
);
1095 if (!SIZE (message
)) MM_LOG ("Append of zero-length message",ERROR
);
1096 else if (!unix_collect_msg (tstream
,sf
,flags
,date
,message
)) {
1097 sprintf (tmp
,"Error writing scratch file: %.80s",strerror (errno
));
1100 /* get next message */
1101 else if (MM_APPEND (af
) (tstream
,data
,&flags
,&date
,&message
)) continue;
1103 fclose (sf
); /* punt scratch file */
1104 return NIL
; /* give up */
1105 } while (message
); /* until no more messages */
1107 sprintf (tmp
,"Error finishing scratch file: %.80s",strerror (errno
));
1109 fclose (sf
); /* punt scratch file */
1110 return NIL
; /* give up */
1112 i
= ftell (sf
); /* size of scratch file */
1113 /* close sniffing stream */
1114 if (tstream
!= stream
) tstream
= mail_close (tstream
);
1116 MM_CRITICAL (stream
); /* go critical */
1117 /* try to open readwrite for UIDPLUS */
1118 if ((tstream
= mail_open_work (&unixdriver
,NIL
,mailbox
,
1119 OP_SILENT
|OP_NOKOD
)) && tstream
->rdonly
)
1120 tstream
= mail_close (tstream
);
1121 if (au
&& !tstream
) { /* wanted an APPENDUID? */
1122 sprintf (tmp
,"Unable to re-open mailbox for APPENDUID: %.80s",mailbox
);
1126 if (((fd
= unix_lock (dummy_file (file
,mailbox
),O_WRONLY
|O_APPEND
,
1127 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
),
1128 &lock
,LOCK_EX
)) < 0) ||
1129 !(df
= fdopen (fd
,"ab"))) {
1130 MM_NOCRITICAL (stream
); /* done with critical */
1131 sprintf (tmp
,"Can't open append mailbox: %s",strerror (errno
));
1135 fstat (fd
,&sbuf
); /* get current file size */
1137 tp
[1] = time (0); /* set mtime to now */
1138 /* write all messages */
1139 if (!unix_append_msgs (tstream
,sf
,df
,au
? dst
: NIL
) ||
1140 (fflush (df
) == EOF
) || fsync (fd
)) {
1141 sprintf (buf
,"Message append failed: %s",strerror (errno
));
1143 ftruncate (fd
,sbuf
.st_size
);
1144 tp
[0] = /* preserve \Marked status */
1145 ((sbuf
.st_ctime
> sbuf
.st_atime
) || (sbuf
.st_mtime
> sbuf
.st_atime
)) ?
1146 sbuf
.st_atime
: tp
[1];
1147 ret
= NIL
; /* return error */
1149 else tp
[0] = tp
[1] - 1; /* set atime to now-1 if successful copy */
1150 utime (file
,tp
); /* set the times */
1151 fclose (sf
); /* done with scratch file */
1152 /* force UIDVALIDITY assignment now */
1153 if (tstream
&& !tstream
->uid_validity
) tstream
->uid_validity
= time (0);
1154 /* return sets if doing APPENDUID */
1155 if (au
&& ret
) (*au
) (mailbox
,tstream
->uid_validity
,dst
);
1156 else mail_free_searchset (&dst
);
1157 unix_unlock (fd
,NIL
,&lock
); /* unlock and close mailbox */
1158 fclose (df
); /* note that unix_unlock() released the fd */
1159 if (tstream
) { /* update last UID if we can */
1160 UNIXLOCAL
*local
= (UNIXLOCAL
*) tstream
->local
;
1161 local
->dirty
= T
; /* do a rewrite */
1162 local
->appending
= T
; /* but not at the cost of marking as old */
1163 tstream
= mail_close (tstream
);
1165 MM_NOCRITICAL (stream
); /* release critical */
1169 /* Collect and write single message to append scratch file
1170 * Accepts: MAIL stream
1174 * message stringstruct
1175 * Returns: NIL if write error, else T
1178 int unix_collect_msg (MAILSTREAM
*stream
,FILE *sf
,char *flags
,char *date
,
1181 unsigned char *s
,*t
;
1183 long f
= mail_parse_flags (stream
,flags
,&uf
);
1184 /* write metadata, note date ends with NL */
1185 if (fprintf (sf
,"%ld %lu %s",f
,SIZE (msg
) + 1,date
) < 0) return NIL
;
1186 while (uf
) /* write user flags */
1187 if ((s
= stream
->user_flags
[find_rightmost_bit (&uf
)]) &&
1188 (fprintf (sf
," %s",s
) < 0)) return NIL
;
1189 if (putc ('\n',sf
) == EOF
) return NIL
;
1190 while (SIZE (msg
)) { /* copy text to scratch file */
1191 for (s
= (unsigned char *) msg
->curpos
, t
= s
+ msg
->cursize
; s
< t
; ++s
)
1192 if (!*s
) *s
= 0x80; /* disallow NUL */
1193 /* write buffered text */
1194 if (fwrite (msg
->curpos
,1,msg
->cursize
,sf
) == msg
->cursize
)
1195 SETPOS (msg
,GETPOS (msg
) + msg
->cursize
);
1196 else return NIL
; /* failed */
1198 /* write trailing newline and return */
1199 return (putc ('\n',sf
) == EOF
) ? NIL
: T
;
1202 /* Append messages from scratch file to mailbox
1203 * Accepts: MAIL stream
1206 * uidset to update if non-NIL
1207 * Returns: T if success, NIL if failure
1210 int unix_append_msgs (MAILSTREAM
*stream
,FILE *sf
,FILE *df
,SEARCHSET
*set
)
1215 char *x
,tmp
[MAILTMPLEN
];
1217 /* get message metadata line */
1218 while (fgets (tmp
,MAILTMPLEN
,sf
)) {
1219 if (!(isdigit (tmp
[0]) && strchr (tmp
,'\n'))) return NIL
;
1220 f
= strtol (tmp
,&x
,10); /* get flags */
1221 if (!((*x
++ == ' ') && isdigit (*x
))) return NIL
;
1222 i
= strtoul (x
,&x
,10); /* get message size */
1223 if ((*x
++ != ' ') || /* build initial header */
1224 (fprintf (df
,"From %s@%s %sStatus: ",myusername(),mylocalhost(),x
)<0)||
1225 (f
&fSEEN
&& (putc ('R',df
) == EOF
)) ||
1226 (fputs ("\nX-Status: ",df
) == EOF
) ||
1227 (f
&fDELETED
&& (putc ('D',df
) == EOF
)) ||
1228 (f
&fFLAGGED
&& (putc ('F',df
) == EOF
)) ||
1229 (f
&fANSWERED
&& (putc ('A',df
) == EOF
)) ||
1230 (f
&fDRAFT
&& (putc ('T',df
) == EOF
)) ||
1231 (fputs ("\nX-Keywords:",df
) == EOF
)) return NIL
;
1233 while ((c
= getc (sf
)) != '\n') switch (c
) {
1237 if (putc (c
,df
) == EOF
) return NIL
;
1239 if ((putc ('\n',df
) == EOF
) ||
1240 (set
&& (fprintf (df
,"X-UID: %lu\n",++(stream
->uid_last
)) < 0)))
1243 for (c
= '\n'; i
&& fgets (tmp
,MAILTMPLEN
,sf
); c
= tmp
[j
-1]) {
1244 /* get read line length */
1245 if (i
< (j
= strlen (tmp
))) fatal ("unix_append_msgs overrun");
1246 i
-= j
; /* number of bytes left */
1247 /* squish out CRs (note also copies NUL) */
1248 for (x
= tmp
; (x
= strchr (x
,'\r')) != NULL
; --j
) memmove (x
,x
+1,j
-(x
-tmp
));
1249 if (!j
) continue; /* do nothing if line emptied */
1250 /* start of line? */
1251 if (c
== '\n') switch (tmp
[0]) {
1252 case 'F': /* possible "From " (case counts here) */
1253 if ((j
> 4) && (tmp
[0] == 'F') && (tmp
[1] == 'r') && (tmp
[2] == 'o') &&
1254 (tmp
[3] == 'm') && (tmp
[4] == ' ')) {
1255 if (!unix_fromwidget
) {
1256 VALID (tmp
,x
,ti
,zn
);/* conditional, only write widget if */
1257 if (!ti
) break; /* it looks like a valid header */
1258 } /* write the widget */
1259 if (putc ('>',df
) == EOF
) return NIL
;
1262 case 'S': case 's': /* possible "Status:" */
1263 if (hdrp
&& (j
> 6) && ((tmp
[1] == 't') || (tmp
[1] == 'T')) &&
1264 ((tmp
[2] == 'a') || (tmp
[2] == 'A')) &&
1265 ((tmp
[3] == 't') || (tmp
[3] == 'T')) &&
1266 ((tmp
[4] == 'u') || (tmp
[4] == 'U')) &&
1267 ((tmp
[5] == 's') || (tmp
[5] == 'S')) && (tmp
[6] == ':') &&
1268 (fputs ("X-Original-",df
) == EOF
)) return NIL
;
1270 case 'X': case 'x': /* possible X-??? header */
1271 if (hdrp
&& (tmp
[1] == '-') &&
1272 /* possible X-UID: */
1273 (((j
> 5) && ((tmp
[2] == 'U') || (tmp
[2] == 'u')) &&
1274 ((tmp
[3] == 'I') || (tmp
[3] == 'i')) &&
1275 ((tmp
[4] == 'D') || (tmp
[4] == 'd')) && (tmp
[5] == ':')) ||
1276 /* possible X-IMAP: */
1277 ((j
> 6) && ((tmp
[2] == 'I') || (tmp
[2] == 'i')) &&
1278 ((tmp
[3] == 'M') || (tmp
[3] == 'm')) &&
1279 ((tmp
[4] == 'A') || (tmp
[4] == 'a')) &&
1280 ((tmp
[5] == 'P') || (tmp
[5] == 'p')) &&
1282 /* or X-IMAPbase: */
1283 ((j
> 10) && ((tmp
[6] == 'b') || (tmp
[6] == 'B')) &&
1284 ((tmp
[7] == 'a') || (tmp
[7] == 'A')) &&
1285 ((tmp
[8] == 's') || (tmp
[8] == 'S')) &&
1286 ((tmp
[9] == 'e') || (tmp
[9] == 'E')) && (tmp
[10] == ':')))) ||
1287 /* possible X-Status: */
1288 ((j
> 8) && ((tmp
[2] == 'S') || (tmp
[2] == 's')) &&
1289 ((tmp
[3] == 't') || (tmp
[3] == 'T')) &&
1290 ((tmp
[4] == 'a') || (tmp
[4] == 'A')) &&
1291 ((tmp
[5] == 't') || (tmp
[5] == 'T')) &&
1292 ((tmp
[6] == 'u') || (tmp
[6] == 'U')) &&
1293 ((tmp
[7] == 's') || (tmp
[7] == 'S')) && (tmp
[8] == ':')) ||
1294 /* possible X-Keywords: */
1295 ((j
> 10) && ((tmp
[2] == 'K') || (tmp
[2] == 'k')) &&
1296 ((tmp
[3] == 'e') || (tmp
[3] == 'E')) &&
1297 ((tmp
[4] == 'y') || (tmp
[4] == 'Y')) &&
1298 ((tmp
[5] == 'w') || (tmp
[5] == 'W')) &&
1299 ((tmp
[6] == 'o') || (tmp
[6] == 'O')) &&
1300 ((tmp
[7] == 'r') || (tmp
[7] == 'R')) &&
1301 ((tmp
[8] == 'd') || (tmp
[8] == 'D')) &&
1302 ((tmp
[9] == 's') || (tmp
[9] == 'S')) && (tmp
[10] == ':'))) &&
1303 (fputs ("X-Original-",df
) == EOF
)) return NIL
;
1304 case '\n': /* blank line */
1307 default: /* nothing to do */
1310 /* just write the line */
1311 if (fwrite (tmp
,1,j
,df
) != j
) return NIL
;
1313 if (i
) return NIL
; /* didn't read entire message */
1315 if (stream
) mail_append_set (set
,stream
->uid_last
);
1320 /* Internal routines */
1323 /* UNIX mail abort stream
1324 * Accepts: MAIL stream
1327 void unix_abort (MAILSTREAM
*stream
)
1329 if (LOCAL
) { /* only if a file is open */
1330 if (LOCAL
->fd
>= 0) close (LOCAL
->fd
);
1331 if (LOCAL
->ld
>= 0) { /* have a mailbox lock? */
1332 flock (LOCAL
->ld
,LOCK_UN
);/* yes, release the lock */
1333 close (LOCAL
->ld
); /* close the lock file */
1334 unlink (LOCAL
->lname
); /* and delete it */
1336 if (LOCAL
->lname
) fs_give ((void **) &LOCAL
->lname
);
1337 /* free local text buffers */
1338 if (LOCAL
->buf
) fs_give ((void **) &LOCAL
->buf
);
1339 if (LOCAL
->text
.data
) fs_give ((void **) &LOCAL
->text
.data
);
1340 if (LOCAL
->linebuf
) fs_give ((void **) &LOCAL
->linebuf
);
1341 if (LOCAL
->line
) fs_give ((void **) &LOCAL
->line
);
1342 /* nuke the local data */
1343 fs_give ((void **) &stream
->local
);
1344 stream
->dtb
= NIL
; /* log out the DTB */
1348 /* UNIX open and lock mailbox
1349 * Accepts: file name to open/lock
1351 * destination buffer for lock file name
1352 * type of locking operation (LOCK_SH or LOCK_EX)
1355 int unix_lock (char *file
,int flags
,int mode
,DOTLOCK
*lock
,int op
)
1358 blocknotify_t bn
= (blocknotify_t
) mail_parameters (NIL
,GET_BLOCKNOTIFY
,NIL
);
1359 (*bn
) (BLOCK_FILELOCK
,NIL
);
1360 /* try locking the easy way */
1361 if (dotlock_lock (file
,lock
,-1)) {
1362 /* got dotlock file, easy open */
1363 if ((fd
= open (file
,flags
,mode
)) >= 0) flock (fd
,op
);
1364 else dotlock_unlock (lock
); /* open failed, free the dotlock */
1366 /* no dot lock file, open file now */
1367 else if ((fd
= open (file
,flags
,mode
)) >= 0) {
1368 /* try paranoid way to make a dot lock file */
1369 if (dotlock_lock (file
,lock
,fd
)) {
1370 close (fd
); /* get fresh fd in case of timing race */
1371 if ((fd
= open (file
,flags
,mode
)) >= 0) flock (fd
,op
);
1372 /* open failed, free the dotlock */
1373 else dotlock_unlock (lock
);
1375 else flock (fd
,op
); /* paranoid way failed, just flock() it */
1377 (*bn
) (BLOCK_NONE
,NIL
);
1381 /* UNIX unlock and close mailbox
1382 * Accepts: file descriptor
1383 * (optional) mailbox stream to check atime/mtime
1384 * (optional) lock file name
1387 void unix_unlock (int fd
,MAILSTREAM
*stream
,DOTLOCK
*lock
)
1389 if (stream
) { /* need to muck with times? */
1392 time_t now
= time (0);
1393 fstat (fd
,&sbuf
); /* get file times */
1394 if (LOCAL
->ld
>= 0) { /* yes, readwrite session? */
1395 tp
[0] = now
; /* set atime to now */
1396 /* set mtime to (now - 1) if necessary */
1397 tp
[1] = (now
> sbuf
.st_mtime
) ? sbuf
.st_mtime
: now
- 1;
1399 else if (stream
->recent
) { /* readonly with recent messages */
1400 if ((sbuf
.st_atime
>= sbuf
.st_mtime
) ||
1401 (sbuf
.st_atime
>= sbuf
.st_ctime
))
1402 /* keep past mtime, whack back atime */
1403 tp
[0] = (tp
[1] = (sbuf
.st_mtime
< now
) ? sbuf
.st_mtime
: now
) - 1;
1404 else now
= 0; /* no time change needed */
1406 /* readonly with no recent messages */
1407 else if ((sbuf
.st_atime
< sbuf
.st_mtime
) ||
1408 (sbuf
.st_atime
< sbuf
.st_ctime
)) {
1409 tp
[0] = now
; /* set atime to now */
1410 /* set mtime to (now - 1) if necessary */
1411 tp
[1] = (now
> sbuf
.st_mtime
) ? sbuf
.st_mtime
: now
- 1;
1413 else now
= 0; /* no time change needed */
1414 /* set the times, note change */
1415 if (now
&& !utime (stream
->mailbox
,tp
)) LOCAL
->filetime
= tp
[1];
1417 flock (fd
,LOCK_UN
); /* release flock'ers */
1418 if (!stream
) close (fd
); /* close the file if no stream */
1419 dotlock_unlock (lock
); /* flush the lock file if any */
1422 /* UNIX mail parse and lock mailbox
1423 * Accepts: MAIL stream
1424 * space to write lock file name
1425 * type of locking operation
1426 * Returns: T if parse OK, critical & mailbox is locked shared; NIL if failure
1429 int unix_parse (MAILSTREAM
*stream
,DOTLOCK
*lock
,int op
)
1432 unsigned long i
,j
,k
,m
;
1433 unsigned char c
,*s
,*t
,*u
,tmp
[MAILTMPLEN
],date
[30];
1434 int ti
= 0,retain
= T
;
1435 unsigned long nmsgs
= stream
->nmsgs
;
1436 unsigned long prevuid
= nmsgs
? mail_elt (stream
,nmsgs
)->private.uid
: 0;
1437 unsigned long recent
= stream
->recent
;
1438 unsigned long oldnmsgs
= stream
->nmsgs
;
1439 short silent
= stream
->silent
;
1440 short pseudoseen
= NIL
;
1445 mail_lock (stream
); /* guard against recursion or pingers */
1446 /* toss out previous descriptor */
1447 if (LOCAL
->fd
>= 0) close (LOCAL
->fd
);
1448 MM_CRITICAL (stream
); /* open and lock mailbox (shared OK) */
1449 if ((LOCAL
->fd
= unix_lock (stream
->mailbox
,(LOCAL
->ld
>= 0) ?
1451 (long)mail_parameters(NIL
,GET_MBXPROTECTION
,NIL
),
1453 sprintf (tmp
,"Mailbox open failed, aborted: %s",strerror (errno
));
1455 unix_abort (stream
);
1456 mail_unlock (stream
);
1457 MM_NOCRITICAL (stream
); /* done with critical */
1460 fstat (LOCAL
->fd
,&sbuf
); /* get status */
1461 /* validate change in size */
1462 if (sbuf
.st_size
< LOCAL
->filesize
) {
1463 sprintf (tmp
,"Mailbox shrank from %lu to %lu bytes, aborted",
1464 (unsigned long) LOCAL
->filesize
,(unsigned long) sbuf
.st_size
);
1465 MM_LOG (tmp
,ERROR
); /* this is pretty bad */
1466 unix_unlock (LOCAL
->fd
,stream
,lock
);
1467 unix_abort (stream
);
1468 mail_unlock (stream
);
1469 MM_NOCRITICAL (stream
); /* done with critical */
1474 else if ((i
= sbuf
.st_size
- LOCAL
->filesize
) != 0L) {
1475 d
.fd
= LOCAL
->fd
; /* yes, set up file descriptor */
1476 d
.pos
= LOCAL
->filesize
; /* get to that position in the file */
1477 d
.chunk
= LOCAL
->buf
; /* initial buffer chunk */
1478 d
.chunksize
= CHUNKSIZE
; /* file chunk size */
1479 INIT (&bs
,fd_string
,&d
,i
); /* initialize stringstruct */
1480 /* skip leading whitespace for broken MTAs */
1481 while (((c
= CHR (&bs
)) == '\n') || (c
== '\r') ||
1482 (c
== ' ') || (c
== '\t')) SNX (&bs
);
1483 if (SIZE (&bs
)) { /* read new data */
1484 /* remember internal header position */
1485 j
= LOCAL
->filesize
+ GETPOS (&bs
);
1486 s
= unix_mbxline (stream
,&bs
,&i
);
1488 if (i
) VALID (s
,t
,ti
,zn
); /* see if valid From line */
1489 if (!ti
) { /* someone pulled the rug from under us */
1490 sprintf (tmp
,"Unexpected changes to mailbox (try restarting): %.20s",
1493 unix_unlock (LOCAL
->fd
,stream
,lock
);
1494 unix_abort (stream
);
1495 mail_unlock (stream
);
1496 /* done with critical */
1497 MM_NOCRITICAL (stream
);
1500 stream
->silent
= T
; /* quell main program new message events */
1501 do { /* found a message */
1502 /* instantiate first new message */
1503 mail_exists (stream
,++nmsgs
);
1504 (elt
= mail_elt (stream
,nmsgs
))->valid
= T
;
1505 recent
++; /* assume recent by default */
1507 /* note position/size of internal header */
1508 elt
->private.special
.offset
= j
;
1509 elt
->private.msg
.header
.offset
= elt
->private.special
.text
.size
= i
;
1511 /* generate plausible IMAPish date string */
1512 date
[2] = date
[6] = date
[20] = '-'; date
[11] = ' ';
1513 date
[14] = date
[17] = ':';
1515 date
[0] = t
[ti
- 2]; date
[1] = t
[ti
- 1];
1517 date
[3] = t
[ti
- 6]; date
[4] = t
[ti
- 5]; date
[5] = t
[ti
- 4];
1519 date
[12] = t
[ti
+ 1]; date
[13] = t
[ti
+ 2];
1521 date
[15] = t
[ti
+ 4]; date
[16] = t
[ti
+ 5];
1522 if (t
[ti
+= 6] == ':') {/* ss */
1523 date
[18] = t
[++ti
]; date
[19] = t
[++ti
];
1524 ti
++; /* move to space */
1526 else date
[18] = date
[19] = '0';
1527 /* yy -- advance over timezone if necessary */
1528 if (zn
== ti
) ti
+= (((t
[zn
+1] == '+') || (t
[zn
+1] == '-')) ? 6 : 4);
1529 date
[7] = t
[ti
+ 1]; date
[8] = t
[ti
+ 2];
1530 date
[9] = t
[ti
+ 3]; date
[10] = t
[ti
+ 4];
1532 t
= zn
? (t
+ zn
+ 1) : (unsigned char *) "LCL";
1533 date
[21] = *t
++; date
[22] = *t
++; date
[23] = *t
++;
1534 if ((date
[21] != '+') && (date
[21] != '-')) date
[24] = '\0';
1535 else { /* numeric time zone */
1536 date
[24] = *t
++; date
[25] = *t
++;
1537 date
[26] = '\0'; date
[20] = ' ';
1539 /* set internal date */
1540 if (!mail_parse_date (elt
,date
)) {
1541 sprintf (tmp
,"Unable to parse internal date: %s",(char *) date
);
1545 do { /* look for message body */
1546 s
= t
= unix_mbxline (stream
,&bs
,&i
);
1547 if (i
) switch (*s
) { /* check header lines */
1548 case 'X': /* possible X-???: line */
1549 if (s
[1] == '-') { /* must be immediately followed by hyphen */
1550 /* X-Status: becomes Status: in S case */
1551 if (s
[2] == 'S' && s
[3] == 't' && s
[4] == 'a' && s
[5] == 't' &&
1552 s
[6] == 'u' && s
[7] == 's' && s
[8] == ':') s
+= 2;
1553 /* possible X-Keywords */
1554 else if (s
[2] == 'K' && s
[3] == 'e' && s
[4] == 'y' &&
1555 s
[5] == 'w' && s
[6] == 'o' && s
[7] == 'r' &&
1556 s
[8] == 'd' && s
[9] == 's' && s
[10] == ':') {
1558 retain
= NIL
; /* don't retain continuation */
1559 s
+= 11; /* flush leading whitespace */
1560 while (*s
&& (*s
!= '\n') && ((*s
!= '\r') || (s
[1] != '\n'))){
1561 while (*s
== ' ') s
++;
1562 /* find end of keyword */
1563 if (!(u
= strpbrk (s
," \n\r"))) u
= s
+ strlen (s
);
1564 /* got a keyword? */
1565 if ((k
= (u
- s
)) && (k
<= MAXUSERFLAG
)) {
1566 uf
.data
= (unsigned char *) s
;
1568 for (j
= 0; (j
< NUSERFLAGS
) && stream
->user_flags
[j
]; ++j
)
1569 if (!compare_csizedtext (stream
->user_flags
[j
],&uf
)) {
1570 elt
->user_flags
|= ((long) 1) << j
;
1574 s
= u
; /* advance to next keyword */
1579 /* possible X-IMAP */
1580 else if ((s
[2] == 'I') && (s
[3] == 'M') && (s
[4] == 'A') &&
1581 (s
[5] == 'P') && ((m
= (s
[6] == ':')) ||
1582 ((s
[6] == 'b') && (s
[7] == 'a') &&
1583 (s
[8] == 's') && (s
[9] == 'e') &&
1585 retain
= NIL
; /* don't retain continuation */
1586 if ((nmsgs
== 1) && !stream
->uid_validity
) {
1587 /* advance to data */
1589 /* flush whitespace */
1590 while (*s
== ' ') s
++;
1591 j
= 0; /* slurp UID validity */
1592 /* found a digit? */
1593 while (isdigit (*s
)) {
1594 j
*= 10; /* yes, add it in */
1597 /* flush whitespace */
1598 while (*s
== ' ') s
++;
1599 /* must have valid UID validity and UID last */
1600 if (j
&& isdigit (*s
)) {
1601 /* pseudo-header seen if X-IMAP */
1602 if (m
) pseudoseen
= LOCAL
->pseudo
= T
;
1603 /* save UID validity */
1604 stream
->uid_validity
= j
;
1605 j
= 0; /* slurp UID last */
1606 while (isdigit (*s
)) {
1607 j
*= 10; /* yes, add it in */
1611 stream
->uid_last
= j
;
1612 /* process keywords */
1613 for (j
= 0; (*s
!= '\n') && ((*s
!= '\r')||(s
[1] != '\n'));
1615 /* flush leading whitespace */
1616 while (*s
== ' ') s
++;
1617 u
= strpbrk (s
," \n\r");
1618 /* got a keyword? */
1619 if ((j
< NUSERFLAGS
) && (k
= (u
- s
)) &&
1620 (k
<= MAXUSERFLAG
)) {
1621 if (stream
->user_flags
[j
])
1622 fs_give ((void **) &stream
->user_flags
[j
]);
1623 stream
->user_flags
[j
] = (char *) fs_get (k
+ 1);
1624 strncpy (stream
->user_flags
[j
],s
,k
);
1625 stream
->user_flags
[j
][k
] = '\0';
1633 /* possible X-UID */
1634 else if (s
[2] == 'U' && s
[3] == 'I' && s
[4] == 'D' &&
1636 retain
= NIL
; /* don't retain continuation */
1637 /* only believe if have a UID validity */
1638 if (stream
->uid_validity
&& ((nmsgs
> 1) || !pseudoseen
)) {
1639 s
+= 6; /* advance to UID value */
1640 /* flush whitespace */
1641 while (*s
== ' ') s
++;
1643 /* found a digit? */
1644 while (isdigit (*s
)) {
1645 j
*= 10; /* yes, add it in */
1648 /* flush remainder of line */
1649 while (*s
!= '\n') s
++;
1650 /* make sure not duplicated */
1651 if (elt
->private.uid
)
1652 sprintf (tmp
,"Message %lu UID %lu already has UID %lu",
1653 pseudoseen
? elt
->msgno
- 1 : elt
->msgno
,
1654 j
,elt
->private.uid
);
1655 /* make sure UID doesn't go backwards */
1656 else if (j
<= prevuid
)
1657 sprintf (tmp
,"Message %lu UID %lu less than %lu",
1658 pseudoseen
? elt
->msgno
- 1 : elt
->msgno
,
1660 #if 0 /* this is currently broken by UIDPLUS */
1661 /* or skip by mailbox's recorded last */
1662 else if (j
> stream
->uid_last
)
1663 sprintf (tmp
,"Message %lu UID %lu greater than last %lu",
1664 pseudoseen
? elt
->msgno
- 1 : elt
->msgno
,
1665 j
,stream
->uid_last
);
1667 else { /* normal UID case */
1668 prevuid
= elt
->private.uid
= j
;
1669 #if 1 /* temporary kludge for UIDPLUS */
1670 if (prevuid
> stream
->uid_last
) {
1671 stream
->uid_last
= prevuid
;
1672 LOCAL
->ddirty
= LOCAL
->dirty
= T
;
1675 break; /* exit this cruft */
1678 /* invalidate UID validity */
1679 stream
->uid_validity
= 0;
1680 elt
->private.uid
= 0;
1685 /* otherwise fall into S case */
1687 case 'S': /* possible Status: line */
1688 if (s
[0] == 'S' && s
[1] == 't' && s
[2] == 'a' && s
[3] == 't' &&
1689 s
[4] == 'u' && s
[5] == 's' && s
[6] == ':') {
1690 retain
= NIL
; /* don't retain continuation */
1691 s
+= 6; /* advance to status flags */
1692 do switch (*s
++) {/* parse flags */
1693 case 'R': /* message read */
1696 case 'O': /* message old */
1699 recent
--; /* it really wasn't recent */
1702 case 'D': /* message deleted */
1705 case 'F': /* message flagged */
1708 case 'A': /* message answered */
1711 case 'T': /* message is a draft */
1714 default: /* some other crap */
1716 } while (*s
&& (*s
!= '\n') && ((*s
!= '\r') || (s
[1] != '\n')));
1717 break; /* all done */
1719 /* otherwise fall into default case */
1721 default: /* ordinary header line */
1722 if ((*s
== 'S') || (*s
== 's') ||
1723 (((*s
== 'X') || (*s
== 'x')) && (s
[1] == '-'))) {
1724 unsigned char *e
,*v
;
1725 /* must match what mail_filter() does */
1726 for (u
= s
,v
= tmp
,e
= u
+ min (i
,MAILTMPLEN
- 1);
1727 (u
< e
) && ((c
= (*u
? *u
: (*u
= ' '))) != ':') &&
1728 ((c
> ' ') || ((c
!= ' ') && (c
!= '\t') &&
1729 (c
!= '\r') && (c
!= '\n')));
1731 *v
= '\0'; /* tie off */
1732 /* matches internal header? */
1733 if (!compare_cstring (tmp
,"STATUS") ||
1734 !compare_cstring (tmp
,"X-STATUS") ||
1735 !compare_cstring (tmp
,"X-KEYWORDS") ||
1736 !compare_cstring (tmp
,"X-UID") ||
1737 !compare_cstring (tmp
,"X-IMAP") ||
1738 !compare_cstring (tmp
,"X-IMAPBASE")) {
1739 char err
[MAILTMPLEN
];
1740 sprintf (err
,"Discarding bogus %.20s header in message %lu",
1741 (char *) tmp
,elt
->msgno
);
1743 retain
= NIL
; /* don't retain continuation */
1744 break; /* different case or something */
1747 /* retain or non-continuation? */
1748 if (retain
|| ((*s
!= ' ') && (*s
!= '\t'))) {
1749 retain
= T
; /* retaining continuation now */
1750 /* line length in LF format newline */
1751 for (j
= k
= 0; j
< i
; ++j
) if (s
[j
] != '\r') ++k
;
1752 /* "internal" header size */
1753 elt
->private.spare
.data
+= k
;
1755 elt
->rfc822_size
+= k
+ 1;
1758 char err
[MAILTMPLEN
];
1759 sprintf (err
,"Discarding bogus continuation in msg %lu: %.80s",
1760 elt
->msgno
,(char *) s
);
1761 if ((u
= strpbrk (err
,"\r\n")) != NULL
) *u
= '\0';
1763 break; /* different case or something */
1767 } while (i
&& (*t
!= '\n') && ((*t
!= '\r') || (t
[1] != '\n')));
1768 /* "internal" header sans trailing newline */
1769 if (i
) elt
->private.spare
.data
--;
1770 /* assign a UID if none found */
1771 if (((nmsgs
> 1) || !pseudoseen
) && !elt
->private.uid
) {
1772 prevuid
= elt
->private.uid
= ++stream
->uid_last
;
1773 elt
->private.dirty
= T
;
1774 LOCAL
->ddirty
= T
; /* force update */
1776 else elt
->private.dirty
= elt
->recent
;
1778 /* note size of header, location of text */
1779 elt
->private.msg
.header
.text
.size
=
1780 (elt
->private.msg
.text
.offset
=
1781 (LOCAL
->filesize
+ GETPOS (&bs
)) - elt
->private.special
.offset
) -
1782 elt
->private.special
.text
.size
;
1783 k
= m
= 0; /* no previous line size yet */
1784 /* note current position */
1785 j
= LOCAL
->filesize
+ GETPOS (&bs
);
1786 if (i
) do { /* look for next message */
1787 s
= unix_mbxline (stream
,&bs
,&i
);
1788 if (i
) { /* got new data? */
1789 VALID (s
,t
,ti
,zn
); /* yes, parse line */
1790 if (!ti
) { /* not a header line, add it to message */
1791 elt
->rfc822_size
+= i
;
1792 for (j
= 0; j
< i
; ++j
) switch (s
[j
]) {
1793 case '\r': /* squeeze out CRs */
1794 elt
->rfc822_size
-= 1;
1796 case '\n': /* LF becomes CRLF */
1797 elt
->rfc822_size
+= 1;
1802 if ((i
== 1) && (*s
== '\n')) {
1806 else if ((i
== 2) && (*s
== '\r') && (s
[1] == '\n'))
1808 else k
= m
= 0; /* file does not end with newline! */
1809 /* update current position */
1810 j
= LOCAL
->filesize
+ GETPOS (&bs
);
1813 } while (i
&& !ti
); /* until found a header */
1814 elt
->private.msg
.text
.text
.size
= j
-
1815 (elt
->private.special
.offset
+ elt
->private.msg
.text
.offset
);
1816 /* flush ending blank line */
1817 elt
->private.msg
.text
.text
.size
-= m
;
1818 elt
->rfc822_size
-= k
;
1819 /* until end of buffer */
1820 } while (!stream
->sniff
&& i
);
1821 if (pseudoseen
) { /* flush pseudo-message if present */
1822 /* decrement recent count */
1823 if (mail_elt (stream
,1)->recent
) recent
--;
1824 /* and the exists count */
1825 mail_exists (stream
,nmsgs
--);
1826 mail_expunged(stream
,1);/* fake an expunge of that message */
1828 /* need to start a new UID validity? */
1829 if (!stream
->uid_validity
) {
1830 stream
->uid_validity
= time (0);
1831 /* in case a whiner with no life */
1832 if (mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
))
1833 stream
->uid_nosticky
= T
;
1834 else if (nmsgs
) { /* don't bother if empty file */
1835 /* make dirty to restart UID epoch */
1836 LOCAL
->ddirty
= LOCAL
->dirty
= T
;
1837 /* need to rewrite msg 1 if not pseudo */
1838 if (!LOCAL
->pseudo
) mail_elt (stream
,1)->private.dirty
= T
;
1839 MM_LOG ("Assigning new unique identifiers to all messages",NIL
);
1842 stream
->nmsgs
= oldnmsgs
; /* whack it back down */
1843 stream
->silent
= silent
; /* restore old silent setting */
1844 /* notify upper level of new mailbox sizes */
1845 mail_exists (stream
,nmsgs
);
1846 mail_recent (stream
,recent
);
1847 /* mark dirty so O flags are set */
1848 if (recent
) LOCAL
->dirty
= T
;
1851 /* no change, don't babble if never got time */
1852 else if (LOCAL
->filetime
&& LOCAL
->filetime
!= sbuf
.st_mtime
)
1853 MM_LOG ("New mailbox modification time but apparently no changes",WARN
);
1854 /* update parsed file size and time */
1855 LOCAL
->filesize
= sbuf
.st_size
;
1856 LOCAL
->filetime
= sbuf
.st_mtime
;
1857 return T
; /* return the winnage */
1860 /* UNIX read line from mailbox
1861 * Accepts: mail stream
1863 * pointer to line size
1864 * Returns: pointer to input line
1867 char *unix_mbxline (MAILSTREAM
*stream
,STRING
*bs
,unsigned long *size
)
1869 unsigned long i
,j
,k
,m
;
1872 /* flush old buffer */
1873 if (LOCAL
->line
) fs_give ((void **) &LOCAL
->line
);
1874 /* if buffer needs refreshing */
1875 if (!bs
->cursize
) SETPOS (bs
,GETPOS (bs
));
1876 if (SIZE (bs
)) { /* find newline */
1877 /* end of fast scan */
1878 te
= (t
= (s
= bs
->curpos
) + bs
->cursize
) - 12;
1879 while (s
< te
) if ((*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
1880 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
1881 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
1882 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n')) {
1884 break; /* exit loop */
1886 /* final character-at-a-time scan */
1887 while ((s
< t
) && (*s
!= '\n')) ++s
;
1888 /* difficult case if line spans buffer */
1889 if ((i
= s
- bs
->curpos
) == bs
->cursize
) {
1890 /* have space in line buffer? */
1891 if (i
> LOCAL
->linebuflen
) {
1892 fs_give ((void **) &LOCAL
->linebuf
);
1893 LOCAL
->linebuf
= (char *) fs_get (LOCAL
->linebuflen
= i
);
1895 /* remember what we have so far */
1896 memcpy (LOCAL
->linebuf
,bs
->curpos
,i
);
1897 /* load next buffer */
1898 SETPOS (bs
,k
= GETPOS (bs
) + i
);
1899 /* end of fast scan */
1900 te
= (t
= (s
= bs
->curpos
) + bs
->cursize
) - 12;
1901 /* fast scan in overlap buffer */
1902 while (s
< te
) if ((*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
1903 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
1904 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n') ||
1905 (*s
++ == '\n') || (*s
++ == '\n') || (*s
++ == '\n')) {
1907 break; /* exit loop */
1910 /* final character-at-a-time scan */
1911 while ((s
< t
) && (*s
!= '\n')) ++s
;
1913 if ((j
= s
- bs
->curpos
) == bs
->cursize
) {
1914 SETPOS (bs
,GETPOS (bs
) + j
);
1915 /* look for end of line (s-l-o-w!!) */
1916 for (m
= SIZE (bs
); m
&& (SNX (bs
) != '\n'); --m
,++j
);
1917 SETPOS (bs
,k
); /* go back to where it started */
1919 /* got size of data, make buffer for return */
1920 ret
= LOCAL
->line
= (char *) fs_get (i
+ j
+ 2);
1921 /* copy first chunk */
1922 memcpy (ret
,LOCAL
->linebuf
,i
);
1923 while (j
) { /* copy remainder */
1924 if (!bs
->cursize
) SETPOS (bs
,GETPOS (bs
));
1925 memcpy (ret
+ i
,bs
->curpos
,k
= min (j
,bs
->cursize
));
1926 i
+= k
; /* account for this much read in */
1928 bs
->curpos
+= k
; /* increment new position */
1929 bs
->cursize
-= k
; /* eat that many bytes */
1931 if (!bs
->cursize
) SETPOS (bs
,GETPOS (bs
));
1932 /* read newline at end */
1933 if (SIZE (bs
)) ret
[i
++] = SNX (bs
);
1934 ret
[i
] = '\0'; /* makes debugging easier */
1936 else { /* this is easy */
1937 ret
= bs
->curpos
; /* string it at this position */
1938 bs
->curpos
+= ++i
; /* increment new position */
1939 bs
->cursize
-= i
; /* eat that many bytes */
1941 *size
= i
; /* return that to user */
1943 else *size
= 0; /* end of data, return empty */
1947 /* UNIX make pseudo-header
1948 * Accepts: MAIL stream
1949 * buffer to write pseudo-header
1950 * Returns: length of pseudo-header
1953 unsigned long unix_pseudo (MAILSTREAM
*stream
,char *hdr
)
1956 char *s
,tmp
[MAILTMPLEN
];
1957 time_t now
= time (0);
1958 rfc822_fixed_date (tmp
);
1959 sprintf (hdr
,"From %s %.24s\nDate: %s\nFrom: %s <%s@%.80s>\nSubject: %s\nMessage-ID: <%lu@%.80s>\nX-IMAP: %010lu %010lu",
1960 pseudo_from
,ctime (&now
),
1961 tmp
,pseudo_name
,pseudo_from
,mylocalhost (),pseudo_subject
,
1962 (unsigned long) now
,mylocalhost (),stream
->uid_validity
,
1964 for (s
= hdr
+ strlen (hdr
),i
= 0; i
< NUSERFLAGS
; ++i
)
1965 if (stream
->user_flags
[i
])
1966 sprintf (s
+= strlen (s
)," %s",stream
->user_flags
[i
]);
1967 sprintf (s
+= strlen (s
),"\nStatus: RO\n\n%s\n\n",pseudo_msg
);
1968 return strlen (hdr
); /* return header length */
1971 /* UNIX make status string
1972 * Accepts: MAIL stream
1973 * destination string to write
1974 * message cache entry
1975 * UID to write if non-zero (else use elt->private.uid)
1976 * non-zero flag to write UID (.LT. 0 to write UID base info too)
1977 * Returns: length of string
1980 unsigned long unix_xstatus (MAILSTREAM
*stream
,char *status
,MESSAGECACHE
*elt
,
1981 unsigned long uid
,long flag
)
1987 int sticky
= uid
? T
: !stream
->uid_nosticky
;
1988 /* This used to use sprintf(), but thanks to certain cretinous C libraries
1989 with horribly slow implementations of sprintf() I had to change it to this
1990 mess. At least it should be fast. */
1991 if ((flag
< 0) && sticky
) { /* need to write X-IMAPbase: header? */
1992 *s
++ = 'X'; *s
++ = '-'; *s
++ = 'I'; *s
++ = 'M'; *s
++ = 'A'; *s
++ = 'P';
1993 *s
++ = 'b'; *s
++ = 'a'; *s
++ = 's'; *s
++ = 'e'; *s
++ = ':'; *s
++ = ' ';
1995 n
= stream
->uid_validity
; /* push UID validity digits on the stack */
1996 do *t
++ = (char) (n
% 10) + '0';
1998 /* pop UID validity digits from stack */
1999 while (t
> stack
) *s
++ = *--t
;
2001 n
= stream
->uid_last
; /* push UID last digits on the stack */
2002 do *t
++ = (char) (n
% 10) + '0';
2004 /* pop UID last digits from stack */
2005 while (t
> stack
) *s
++ = *--t
;
2006 for (n
= 0; n
< NUSERFLAGS
; ++n
) if ((t
= stream
->user_flags
[n
]) != NULL
)
2007 for (*s
++ = ' '; *t
; *s
++ = *t
++);
2009 pad
+= 30; /* increased padding if have IMAPbase */
2011 *s
++ = 'S'; *s
++ = 't'; *s
++ = 'a'; *s
++ = 't'; *s
++ = 'u'; *s
++ = 's';
2012 *s
++ = ':'; *s
++ = ' ';
2013 if (elt
->seen
) *s
++ = 'R';
2014 /* only write O if have a UID */
2015 if (flag
&& (!elt
->recent
|| !LOCAL
->appending
)) *s
++ = 'O';
2017 *s
++ = 'X'; *s
++ = '-'; *s
++ = 'S'; *s
++ = 't'; *s
++ = 'a'; *s
++ = 't';
2018 *s
++ = 'u'; *s
++ = 's'; *s
++ = ':'; *s
++ = ' ';
2019 if (elt
->deleted
) *s
++ = 'D';
2020 if (elt
->flagged
) *s
++ = 'F';
2021 if (elt
->answered
) *s
++ = 'A';
2022 if (elt
->draft
) *s
++ = 'T';
2025 if (sticky
) { /* only do this if UIDs sticky */
2026 *s
++ = 'X'; *s
++ = '-'; *s
++ = 'K'; *s
++ = 'e'; *s
++ = 'y'; *s
++ = 'w';
2027 *s
++ = 'o'; *s
++ = 'r'; *s
++ = 'd'; *s
++ = 's'; *s
++ = ':';
2028 if ((n
= elt
->user_flags
) != 0L) do {
2030 for (t
= stream
->user_flags
[find_rightmost_bit (&n
)]; *t
; *s
++ = *t
++);
2032 n
= s
- status
; /* get size of stuff so far */
2033 /* pad X-Keywords to make size constant */
2034 if (n
< pad
) for (n
= pad
- n
; n
> 0; --n
) *s
++ = ' ';
2036 if (flag
) { /* want to include UID? */
2038 /* push UID digits on the stack */
2039 n
= uid
? uid
: elt
->private.uid
;
2040 do *t
++ = (char) (n
% 10) + '0';
2042 *s
++ = 'X'; *s
++ = '-'; *s
++ = 'U'; *s
++ = 'I'; *s
++ = 'D'; *s
++ = ':';
2044 /* pop UID from stack */
2045 while (t
> stack
) *s
++ = *--t
;
2049 *s
++ = '\n'; *s
= '\0'; /* end of extended message status */
2050 return s
- status
; /* return size of resulting string */
2053 /* Rewrite mailbox file
2054 * Accepts: MAIL stream, must be critical and locked
2055 * return pointer to number of expunged messages if want expunge
2057 * expunge sequence, not deleted flag
2058 * Returns: T if success and mailbox unlocked, NIL if failure
2061 #define OVERFLOWBUFLEN 8192 /* initial overflow buffer length */
2063 long unix_rewrite (MAILSTREAM
*stream
,unsigned long *nexp
,DOTLOCK
*lock
,
2072 unsigned long recent
= stream
->recent
;
2073 unsigned long size
= LOCAL
->pseudo
? unix_pseudo (stream
,LOCAL
->buf
) : 0;
2074 if (nexp
) *nexp
= 0; /* initially nothing expunged */
2075 /* calculate size of mailbox after rewrite */
2076 for (i
= 1,flag
= LOCAL
->pseudo
? 1 : -1; i
<= stream
->nmsgs
; i
++) {
2077 elt
= mail_elt (stream
,i
); /* get cache */
2078 if (!(nexp
&& elt
->deleted
&& (flags
? elt
->sequence
: T
))) {
2079 /* add RFC822 size of this message */
2080 size
+= elt
->private.special
.text
.size
+ elt
->private.spare
.data
+
2081 unix_xstatus (stream
,LOCAL
->buf
,elt
,NIL
,flag
) +
2082 elt
->private.msg
.text
.text
.size
+ 1;
2083 flag
= 1; /* only count X-IMAPbase once */
2086 /* no messages, has a life, and no pseudo */
2087 if (!size
&& !mail_parameters (NIL
,GET_USERHASNOLIFE
,NIL
)) {
2088 LOCAL
->pseudo
= T
; /* so make a pseudo-message now */
2089 size
= unix_pseudo (stream
,LOCAL
->buf
);
2091 /* extend the file as necessary */
2092 if ((ret
= unix_extend (stream
,size
)) != 0L) {
2093 /* Set up buffered I/O file structure
2094 * curpos current position being written through buffering
2095 * filepos current position being written physically to the disk
2096 * bufpos current position being written in the buffer
2097 * protect current maximum position that can be written to the disk
2098 * before buffering is forced
2099 * The code tries to buffer so that that disk is written in multiples of
2100 * OVERBLOWBUFLEN bytes.
2102 f
.stream
= stream
; /* note mail stream */
2103 f
.curpos
= f
.filepos
= 0; /* start of file */
2104 f
.protect
= stream
->nmsgs
? /* initial protection pointer */
2105 mail_elt (stream
,1)->private.special
.offset
: 8192;
2106 f
.bufpos
= f
.buf
= (char *) fs_get (f
.buflen
= OVERFLOWBUFLEN
);
2108 if (LOCAL
->pseudo
) /* update pseudo-header */
2109 unix_write (&f
,LOCAL
->buf
,unix_pseudo (stream
,LOCAL
->buf
));
2110 /* loop through all messages */
2111 for (i
= 1,flag
= LOCAL
->pseudo
? 1 : -1; i
<= stream
->nmsgs
;) {
2112 elt
= mail_elt (stream
,i
);/* get cache */
2113 /* expunge this message? */
2114 if (nexp
&& elt
->deleted
&& (flags
? elt
->sequence
: T
)) {
2115 /* one less recent message */
2116 if (elt
->recent
) --recent
;
2117 mail_expunged(stream
,i
);/* notify upper levels */
2118 ++*nexp
; /* count up one more expunged message */
2120 else { /* preserve this message */
2121 i
++; /* advance to next message */
2122 if ((flag
< 0) || /* need to rewrite message? */
2123 elt
->private.dirty
|| (f
.curpos
!= elt
->private.special
.offset
) ||
2124 (elt
->private.msg
.header
.text
.size
!=
2125 (elt
->private.spare
.data
+
2126 unix_xstatus (stream
,LOCAL
->buf
,elt
,NIL
,flag
)))) {
2127 unsigned long newoffset
= f
.curpos
;
2128 /* yes, seek to internal header */
2129 lseek (LOCAL
->fd
,elt
->private.special
.offset
,L_SET
);
2130 read (LOCAL
->fd
,LOCAL
->buf
,elt
->private.special
.text
.size
);
2131 /* see if need to squeeze out a CR */
2132 if (LOCAL
->buf
[elt
->private.special
.text
.size
- 2] == '\r') {
2133 LOCAL
->buf
[--elt
->private.special
.text
.size
- 1] = '\n';
2134 --size
; /* squeezed out a CR from PC */
2136 /* protection pointer moves to RFC822 header */
2137 f
.protect
= elt
->private.special
.offset
+
2138 elt
->private.msg
.header
.offset
;
2139 /* write internal header */
2140 unix_write (&f
,LOCAL
->buf
,elt
->private.special
.text
.size
);
2141 /* get RFC822 header */
2142 s
= unix_header (stream
,elt
->msgno
,&j
,FT_INTERNAL
);
2143 /* in case this got decremented */
2144 elt
->private.msg
.header
.offset
= elt
->private.special
.text
.size
;
2145 /* header size, sans trailing newline */
2146 if ((j
< 2) || (s
[j
- 2] == '\n')) j
--;
2147 /* this can happen if CRs were squeezed */
2148 if (j
< elt
->private.spare
.data
) {
2149 /* so fix up counts */
2150 size
-= elt
->private.spare
.data
- j
;
2151 elt
->private.spare
.data
= j
;
2153 else if (j
!= elt
->private.spare
.data
)
2154 fatal ("header size inconsistent");
2155 /* protection pointer moves to RFC822 text */
2156 f
.protect
= elt
->private.special
.offset
+
2157 elt
->private.msg
.text
.offset
;
2158 unix_write (&f
,s
,j
); /* write RFC822 header */
2159 /* write status and UID */
2160 unix_write (&f
,LOCAL
->buf
,
2161 j
= unix_xstatus (stream
,LOCAL
->buf
,elt
,NIL
,flag
));
2162 flag
= 1; /* only write X-IMAPbase once */
2163 /* new file header size */
2164 elt
->private.msg
.header
.text
.size
= elt
->private.spare
.data
+ j
;
2166 /* did text move? */
2167 if (f
.curpos
!= f
.protect
) {
2168 /* get message text */
2169 s
= unix_text_work (stream
,elt
,&j
,FT_INTERNAL
);
2170 /* this can happen if CRs were squeezed */
2171 if (j
< elt
->private.msg
.text
.text
.size
) {
2172 /* so fix up counts */
2173 size
-= elt
->private.msg
.text
.text
.size
- j
;
2174 elt
->private.msg
.text
.text
.size
= j
;
2176 /* can't happen it says here */
2177 else if (j
> elt
->private.msg
.text
.text
.size
)
2178 fatal ("text size inconsistent");
2179 /* new text offset, status/UID may change it */
2180 elt
->private.msg
.text
.offset
= f
.curpos
- newoffset
;
2181 /* protection pointer moves to next message */
2182 f
.protect
= (i
<= stream
->nmsgs
) ?
2183 mail_elt (stream
,i
)->private.special
.offset
: (f
.curpos
+ j
+ 1);
2184 unix_write (&f
,s
,j
);/* write text */
2185 /* write trailing newline */
2186 unix_write (&f
,"\n",1);
2188 else { /* tie off header and status */
2189 unix_write (&f
,NIL
,NIL
);
2190 /* protection pointer moves to next message */
2191 f
.protect
= (i
<= stream
->nmsgs
) ?
2192 mail_elt (stream
,i
)->private.special
.offset
: size
;
2193 /* locate end of message text */
2194 j
= f
.filepos
+ elt
->private.msg
.text
.text
.size
;
2195 /* trailing newline already there? */
2196 if (f
.protect
== (j
+ 1)) f
.curpos
= f
.filepos
= f
.protect
;
2197 else { /* trailing newline missing, write it */
2198 f
.curpos
= f
.filepos
= j
;
2199 unix_write (&f
,"\n",1);
2202 /* new internal header offset */
2203 elt
->private.special
.offset
= newoffset
;
2204 elt
->private.dirty
=NIL
;/* message is now clean */
2206 else { /* no need to rewrite this message */
2207 /* tie off previous message if needed */
2208 unix_write (&f
,NIL
,NIL
);
2209 /* protection pointer moves to next message */
2210 f
.protect
= (i
<= stream
->nmsgs
) ?
2211 mail_elt (stream
,i
)->private.special
.offset
: size
;
2212 /* locate end of message text */
2213 j
= f
.filepos
+ elt
->private.special
.text
.size
+
2214 elt
->private.msg
.header
.text
.size
+
2215 elt
->private.msg
.text
.text
.size
;
2216 /* trailing newline already there? */
2217 if (f
.protect
== (j
+ 1)) f
.curpos
= f
.filepos
= f
.protect
;
2218 else { /* trailing newline missing, write it */
2219 f
.curpos
= f
.filepos
= j
;
2220 unix_write (&f
,"\n",1);
2226 unix_write (&f
,NIL
,NIL
); /* tie off final message */
2227 if (size
!= f
.filepos
) fatal ("file size inconsistent");
2228 fs_give ((void **) &f
.buf
); /* free buffer */
2229 /* make sure tied off */
2230 ftruncate (LOCAL
->fd
,LOCAL
->filesize
= size
);
2231 fsync (LOCAL
->fd
); /* make sure the updates take */
2232 if (size
&& (flag
< 0)) fatal ("lost UID base information");
2233 /* no longer dirty */
2234 LOCAL
->ddirty
= LOCAL
->dirty
= NIL
;
2235 /* notify upper level of new mailbox sizes */
2236 mail_exists (stream
,stream
->nmsgs
);
2237 mail_recent (stream
,recent
);
2238 /* set atime to now, mtime a second earlier */
2239 tp
[1] = (tp
[0] = time (0)) - 1;
2240 /* set the times, note change */
2241 if (!utime (stream
->mailbox
,tp
)) LOCAL
->filetime
= tp
[1];
2242 close (LOCAL
->fd
); /* close and reopen file */
2243 if ((LOCAL
->fd
= open (stream
->mailbox
,O_RDWR
,
2244 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
)))
2246 sprintf (LOCAL
->buf
,"Mailbox open failed, aborted: %s",strerror (errno
));
2247 MM_LOG (LOCAL
->buf
,ERROR
);
2248 unix_abort (stream
);
2250 dotlock_unlock (lock
); /* flush the lock file */
2252 return ret
; /* return state from algorithm */
2255 /* Extend UNIX mailbox file
2256 * Accepts: MAIL stream
2258 * Return: T if success, else NIL
2261 long unix_extend (MAILSTREAM
*stream
,unsigned long size
)
2263 unsigned long i
= (size
> LOCAL
->filesize
) ? size
- LOCAL
->filesize
: 0;
2264 if (i
) { /* does the mailbox need to grow? */
2265 if (i
> LOCAL
->buflen
) { /* make sure have enough space */
2266 /* this user won the lottery all right */
2267 fs_give ((void **) &LOCAL
->buf
);
2268 LOCAL
->buf
= (char *) fs_get ((LOCAL
->buflen
= i
) + 1);
2270 memset (LOCAL
->buf
,'\0',i
); /* get a block of nulls */
2271 while (T
) { /* until write successful or punt */
2272 lseek (LOCAL
->fd
,LOCAL
->filesize
,L_SET
);
2273 if ((write (LOCAL
->fd
,LOCAL
->buf
,i
) >= 0) && !fsync (LOCAL
->fd
)) break;
2275 long e
= errno
; /* note error before doing ftruncate */
2276 ftruncate (LOCAL
->fd
,LOCAL
->filesize
);
2277 if (MM_DISKERROR (stream
,e
,NIL
)) {
2278 fsync (LOCAL
->fd
); /* user chose to punt */
2279 sprintf (LOCAL
->buf
,"Unable to extend mailbox: %s",strerror (e
));
2280 if (!stream
->silent
) MM_LOG (LOCAL
->buf
,ERROR
);
2289 /* Write data to buffered file
2290 * Accepts: buffered file pointer
2291 * file data or NIL to indicate "flush buffer"
2292 * date size (ignored for "flush buffer")
2293 * Does not return until success
2296 void unix_write (UNIXFILE
*f
,char *buf
,unsigned long size
)
2298 unsigned long i
,j
,k
;
2299 if (buf
) { /* doing buffered write? */
2300 i
= f
->bufpos
- f
->buf
; /* yes, get size of current buffer data */
2301 /* yes, have space in current buffer chunk? */
2302 if ((j
= i
? ((f
->buflen
- i
) % OVERFLOWBUFLEN
) : f
->buflen
) != 0L) {
2303 /* yes, fill up buffer as much as we can */
2304 memcpy (f
->bufpos
,buf
,k
= min (j
,size
));
2305 f
->bufpos
+= k
; /* new buffer position */
2306 f
->curpos
+= k
; /* new current position */
2307 if (j
-= k
) return; /* all done if still have buffer free space */
2308 buf
+= k
; /* full, get new unwritten data pointer */
2309 size
-= k
; /* new data size */
2310 i
+= k
; /* new buffer data size */
2312 /* This chunk of the buffer is full. See if can make some space by
2313 * writing to the disk, if there's enough unprotected space to do so.
2314 * Try to fill out any unaligned chunk, along with any subsequent full
2315 * chunks that will fit in unprotected space.
2317 /* any unprotected space we can write to? */
2318 if ((j
= min (i
,f
->protect
- f
->filepos
)) != 0L) {
2319 /* yes, filepos not at chunk boundary? */
2320 if ((k
= f
->filepos
% OVERFLOWBUFLEN
) && ((k
= OVERFLOWBUFLEN
- k
) < j
))
2321 j
-= k
; /* yes, and can write out partial chunk */
2322 else k
= 0; /* no partial chunk to write */
2323 /* if at least a chunk free, write that too */
2324 if (j
> OVERFLOWBUFLEN
) k
+= j
- (j
% OVERFLOWBUFLEN
);
2325 if (k
) { /* write data if there is anything we can */
2326 unix_phys_write (f
,f
->buf
,k
);
2328 if (i
-= k
) memmove (f
->buf
,f
->buf
+ k
,i
);
2329 f
->bufpos
= f
->buf
+ i
; /* new end of buffer */
2333 /* Have flushed the buffer as best as possible. All done if no more
2334 * data to write. Otherwise, if the buffer is empty AND if the unwritten
2335 * data is larger than a chunk AND the unprotected space is also larger
2336 * than a chunk, then write as many chunks as we can directly from the
2337 * data. Buffer the rest, expanding the buffer as needed.
2339 if (size
) { /* have more data that we need to buffer? */
2340 /* can write any of it to disk instead? */
2341 if ((f
->bufpos
== f
->buf
) &&
2342 ((j
= min (f
->protect
- f
->filepos
,size
)) > OVERFLOWBUFLEN
)) {
2343 /* write as much as we can right now */
2344 unix_phys_write (f
,buf
,j
-= (j
% OVERFLOWBUFLEN
));
2345 buf
+= j
; /* new data pointer */
2346 size
-= j
; /* new data size */
2347 f
->curpos
+= j
; /* advance current pointer */
2349 if (size
) { /* still have data that we need to buffer? */
2350 /* yes, need to expand the buffer? */
2351 if ((i
= ((f
->bufpos
+ size
) - f
->buf
)) > f
->buflen
) {
2352 /* note current position in buffer */
2353 j
= f
->bufpos
- f
->buf
;
2354 i
+= OVERFLOWBUFLEN
; /* yes, grow another chunk */
2355 fs_resize ((void **) &f
->buf
,f
->buflen
= i
- (i
% OVERFLOWBUFLEN
));
2356 /* in case buffer relocated */
2357 f
->bufpos
= f
->buf
+ j
;
2359 /* buffer remaining data */
2360 memcpy (f
->bufpos
,buf
,size
);
2361 f
->bufpos
+= size
; /* new end of buffer */
2362 f
->curpos
+= size
; /* advance current pointer */
2366 else { /* flush buffer to disk */
2367 unix_phys_write (f
,f
->buf
,i
= f
->bufpos
- f
->buf
);
2368 f
->bufpos
= f
->buf
; /* reset buffer */
2369 /* update positions */
2370 f
->curpos
= f
->protect
= f
->filepos
;
2374 /* Physical disk write
2375 * Accepts: buffered file pointer
2378 * Does not return until success
2381 void unix_phys_write (UNIXFILE
*f
,char *buf
,size_t size
)
2383 MAILSTREAM
*stream
= f
->stream
;
2384 /* write data at desired position */
2385 while (size
&& ((lseek (LOCAL
->fd
,f
->filepos
,L_SET
) < 0) ||
2386 (write (LOCAL
->fd
,buf
,size
) < 0))) {
2388 char tmp
[MAILTMPLEN
];
2389 sprintf (tmp
,"Unable to write to mailbox: %s",strerror (e
= errno
));
2391 MM_DISKERROR (NIL
,e
,T
); /* serious problem, must retry */
2393 f
->filepos
+= size
; /* update file position */
2396 /* MBOX mail routines */
2399 /* Driver dispatch used by MAIL */
2401 DRIVER mboxdriver
= {
2402 "mbox", /* driver name */
2404 DR_LOCAL
|DR_MAIL
|DR_LOCKING
|DR_NONEWMAILRONLY
,
2405 (DRIVER
*) NIL
, /* next driver */
2406 mbox_valid
, /* mailbox is valid for us */
2407 unix_parameters
, /* manipulate parameters */
2408 unix_scan
, /* scan mailboxes */
2409 unix_list
, /* find mailboxes */
2410 unix_lsub
, /* find subscribed mailboxes */
2411 NIL
, /* subscribe to mailbox */
2412 NIL
, /* unsubscribe from mailbox */
2413 mbox_create
, /* create mailbox */
2414 mbox_delete
, /* delete mailbox */
2415 mbox_rename
, /* rename mailbox */
2416 mbox_status
, /* status of mailbox */
2417 mbox_open
, /* open mailbox */
2418 unix_close
, /* close mailbox */
2419 NIL
, /* fetch message "fast" attributes */
2420 NIL
, /* fetch message flags */
2421 NIL
, /* fetch overview */
2422 NIL
, /* fetch message structure */
2423 unix_header
, /* fetch message header */
2424 unix_text
, /* fetch message body */
2425 NIL
, /* fetch partial message text */
2426 NIL
, /* unique identifier */
2427 NIL
, /* message number */
2428 NIL
, /* modify flags */
2429 unix_flagmsg
, /* per-message modify flags */
2430 NIL
, /* search for message based on criteria */
2431 NIL
, /* sort messages */
2432 NIL
, /* thread messages */
2433 mbox_ping
, /* ping mailbox to see if still alive */
2434 mbox_check
, /* check for new messages */
2435 mbox_expunge
, /* expunge deleted messages */
2436 unix_copy
, /* copy messages to another mailbox */
2437 mbox_append
, /* append string message to mailbox */
2438 NIL
/* garbage collect stream */
2441 /* prototype stream */
2442 MAILSTREAM mboxproto
= {&mboxdriver
};
2444 /* MBOX mail validate mailbox
2445 * Accepts: mailbox name
2446 * Returns: our driver if name is valid, NIL otherwise
2449 DRIVER
*mbox_valid (char *name
)
2451 /* only INBOX, mbox must exist */
2452 if (!compare_cstring (name
,"INBOX") && (unix_valid ("mbox") || !errno
) &&
2453 (unix_valid (sysinbox()) || !errno
|| (errno
== ENOENT
)))
2455 return NIL
; /* can't win (yet, anyway) */
2458 /* MBOX mail create mailbox
2459 * Accepts: MAIL stream
2460 * mailbox name to create
2461 * Returns: T on success, NIL on failure
2464 long mbox_create (MAILSTREAM
*stream
,char *mailbox
)
2466 char tmp
[MAILTMPLEN
];
2467 if (!compare_cstring (mailbox
,"INBOX")) return unix_create (NIL
,"mbox");
2468 sprintf (tmp
,"Can't create non-INBOX name as mbox: %.80s",mailbox
);
2474 /* MBOX mail delete mailbox
2475 * Accepts: MAIL stream
2476 * mailbox name to delete
2477 * Returns: T on success, NIL on failure
2480 long mbox_delete (MAILSTREAM
*stream
,char *mailbox
)
2482 return mbox_rename (stream
,mailbox
,NIL
);
2486 /* MBOX mail rename mailbox
2487 * Accepts: MAIL stream
2489 * new mailbox name (or NIL for delete)
2490 * Returns: T on success, NIL on failure
2493 long mbox_rename (MAILSTREAM
*stream
,char *old
,char *newname
)
2495 char tmp
[MAILTMPLEN
];
2496 long ret
= unix_rename (stream
,"~/mbox",newname
);
2497 /* recreate file if renamed INBOX */
2498 if (ret
) unix_create (NIL
,"mbox");
2499 else MM_LOG (tmp
,ERROR
); /* log error */
2500 return ret
; /* return success */
2504 * Accepts: mail stream
2507 * Returns: T on success, NIL on failure
2510 long mbox_status (MAILSTREAM
*stream
,char *mbx
,long flags
)
2514 MAILSTREAM
*tstream
= NIL
;
2515 MAILSTREAM
*systream
= NIL
;
2516 /* make temporary stream (unless this mbx) */
2517 if (!stream
&& !(stream
= tstream
=
2518 mail_open (NIL
,mbx
,OP_READONLY
|OP_SILENT
))) return NIL
;
2519 status
.flags
= flags
; /* return status values */
2520 status
.messages
= stream
->nmsgs
;
2521 status
.recent
= stream
->recent
;
2522 if (flags
& SA_UNSEEN
) /* must search to get unseen messages */
2523 for (i
= 1,status
.unseen
= 0; i
<= stream
->nmsgs
; i
++)
2524 if (!mail_elt (stream
,i
)->seen
) status
.unseen
++;
2525 status
.uidnext
= stream
->uid_last
+ 1;
2526 status
.uidvalidity
= stream
->uid_validity
;
2527 if (!status
.recent
&& /* calculate post-snarf results */
2528 (systream
= mail_open (NIL
,sysinbox (),OP_READONLY
|OP_SILENT
))) {
2529 status
.messages
+= systream
->nmsgs
;
2530 status
.recent
+= systream
->recent
;
2531 if (flags
& SA_UNSEEN
) /* must search to get unseen messages */
2532 for (i
= 1; i
<= systream
->nmsgs
; i
++)
2533 if (!mail_elt (systream
,i
)->seen
) status
.unseen
++;
2534 /* kludge but probably good enough */
2535 status
.uidnext
+= systream
->nmsgs
;
2537 MM_STATUS(stream
,mbx
,&status
);/* pass status to main program */
2538 if (tstream
) mail_close (tstream
);
2539 if (systream
) mail_close (systream
);
2540 return T
; /* success */
2544 * Accepts: stream to open
2545 * Returns: stream on success, NIL on failure
2548 MAILSTREAM
*mbox_open (MAILSTREAM
*stream
)
2550 unsigned long i
= 1;
2551 unsigned long recent
= 0;
2552 /* return prototype for OP_PROTOTYPE call */
2553 if (!stream
) return &mboxproto
;
2554 /* change mailbox file name */
2555 fs_give ((void **) &stream
->mailbox
);
2556 stream
->mailbox
= cpystr ("mbox");
2557 /* open mailbox, snarf new mail */
2558 if (!(unix_open (stream
) && mbox_ping (stream
))) return NIL
;
2559 stream
->inbox
= T
; /* mark that this is an INBOX */
2560 /* notify upper level of mailbox sizes */
2561 mail_exists (stream
,stream
->nmsgs
);
2562 while (i
<= stream
->nmsgs
) if (mail_elt (stream
,i
++)->recent
) ++recent
;
2563 mail_recent (stream
,recent
); /* including recent messages */
2567 /* MBOX mail ping mailbox
2568 * Accepts: MAIL stream
2569 * Returns: T if stream alive, else NIL
2570 * No-op for readonly files, since read/writer can expunge it from under us!
2573 static int snarfed
= 0; /* number of snarfs */
2575 long mbox_ping (MAILSTREAM
*stream
)
2582 /* time to try snarf and sysinbox non-empty? */
2583 if (LOCAL
&& !stream
->rdonly
&& !stream
->lock
&&
2584 (time (0) >= (LOCAL
->lastsnarf
+
2585 (long) mail_parameters (NIL
,GET_SNARFINTERVAL
,NIL
))) &&
2586 !stat (sysinbox (),&sbuf
) && sbuf
.st_size
) {
2587 MM_CRITICAL (stream
); /* yes, go critical */
2588 /* open and lock sysinbox */
2589 if ((sfd
= unix_lock (sysinbox (),O_RDWR
,
2590 (long) mail_parameters (NIL
,GET_MBXPROTECTION
,NIL
),
2591 &lockx
,LOCK_EX
)) >= 0) {
2592 /* locked sysinbox in good format? */
2593 if (fstat (sfd
,&sbuf
) || !(size
= sbuf
.st_size
) ||
2594 !unix_isvalid_fd (sfd
)) {
2595 sprintf (LOCAL
->buf
,"Mail drop %s is not in standard Unix format",
2597 MM_LOG (LOCAL
->buf
,ERROR
);
2599 /* sysinbox good, parse and excl-lock mbox */
2600 else if (unix_parse (stream
,&lock
,LOCK_EX
)) {
2601 lseek (sfd
,0,L_SET
); /* read entire sysinbox into memory */
2602 read (sfd
,s
= (char *) fs_get (size
+ 1),size
);
2603 s
[size
] = '\0'; /* tie it off */
2604 /* append to end of mbox */
2605 lseek (LOCAL
->fd
,LOCAL
->filesize
,L_SET
);
2608 if ((write (LOCAL
->fd
,s
,size
) < 0) || fsync (LOCAL
->fd
)) {
2609 sprintf (LOCAL
->buf
,"New mail move failed: %s",strerror (errno
));
2610 MM_LOG (LOCAL
->buf
,WARN
);
2611 /* revert mbox to previous size */
2612 ftruncate (LOCAL
->fd
,LOCAL
->filesize
);
2614 /* sysinbox better not have changed */
2615 else if (fstat (sfd
,&sbuf
) || (size
!= sbuf
.st_size
)) {
2616 sprintf (LOCAL
->buf
,"Mail drop %s lock failure, old=%lu now=%lu",
2617 sysinbox (),size
,(unsigned long) sbuf
.st_size
);
2618 MM_LOG (LOCAL
->buf
,ERROR
);
2619 /* revert mbox to previous size */
2620 ftruncate (LOCAL
->fd
,LOCAL
->filesize
);
2621 /* Believe it or not, a Singaporean government system actually had
2622 * symlinks from /var/mail/user to ~user/mbox. To compound this
2623 * error, they used an SVR4 system; BSD and OSF locks would have
2624 * prevented it but not SVR4 locks.
2626 if (!fstat (sfd
,&sbuf
) && (size
== sbuf
.st_size
))
2627 syslog (LOG_ALERT
,"File %s and %s are the same file!",
2628 sysinbox (),stream
->mailbox
);
2630 else { /* data copied OK */
2631 ftruncate (sfd
,0); /* truncate sysinbox to zero bytes */
2632 if (!snarfed
++) { /* have we snarfed before? */
2633 /* syslog if server, else user log */
2634 sprintf (LOCAL
->buf
,"Moved %lu bytes of new mail to %s from %s",
2635 size
,stream
->mailbox
,sysinbox ());
2636 if (strcmp ((char *) mail_parameters (NIL
,GET_SERVICENAME
,NIL
),
2638 syslog (LOG_INFO
,"%s host= %s",LOCAL
->buf
,tcp_clienthost ());
2639 else MM_LOG (LOCAL
->buf
,WARN
);
2642 /* done with sysinbox text */
2643 fs_give ((void **) &s
);
2644 /* all done with mbox */
2645 unix_unlock (LOCAL
->fd
,stream
,&lock
);
2646 mail_unlock (stream
); /* unlock the stream */
2647 MM_NOCRITICAL (stream
); /* done with critical */
2649 /* all done with sysinbox */
2650 unix_unlock (sfd
,NIL
,&lockx
);
2652 MM_NOCRITICAL (stream
); /* done with critical */
2653 LOCAL
->lastsnarf
= time (0);/* note time of last snarf */
2655 return unix_ping (stream
); /* do the unix routine now */
2658 /* MBOX mail check mailbox
2659 * Accepts: MAIL stream
2662 void mbox_check (MAILSTREAM
*stream
)
2664 /* do local ping, then do unix routine */
2665 if (mbox_ping (stream
)) unix_check (stream
);
2669 /* MBOX mail expunge mailbox
2670 * Accepts: MAIL stream
2671 * sequence to expunge if non-NIL
2673 * Returns: T, always
2676 long mbox_expunge (MAILSTREAM
*stream
,char *sequence
,long options
)
2678 long ret
= unix_expunge (stream
,sequence
,options
);
2679 mbox_ping (stream
); /* do local ping */
2684 /* MBOX mail append message from stringstruct
2685 * Accepts: MAIL stream
2686 * destination mailbox
2689 * Returns: T if append successful, else NIL
2692 long mbox_append (MAILSTREAM
*stream
,char *mailbox
,append_t af
,void *data
)
2694 char tmp
[MAILTMPLEN
];
2695 if (mbox_valid (mailbox
)) return unix_append (stream
,"mbox",af
,data
);
2696 sprintf (tmp
,"Can't append to that name: %.80s",mailbox
);