1 /* ========================================================================
2  * Copyright 1988-2006 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  */
13 
14 /*
15  * Program:	MTX mail routines
16  *
17  * Author:	Mark Crispin
18  *		Networks and Distributed Computing
19  *		Computing & Communications
20  *		University of Washington
21  *		Administration Building, AG-44
22  *		Seattle, WA  98195
23  *		Internet: MRC@CAC.Washington.EDU
24  *
25  * Date:	24 June 1992
26  * Last Edited:	30 August 2006
27  */
28 
29 
30 #include <ctype.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include "mail.h"
34 #include "osdep.h"
35 #include <time.h>
36 #include <sys\stat.h>
37 #include <dos.h>
38 #include "rfc822.h"
39 #include "dummy.h"
40 #include "misc.h"
41 #include "fdstring.h"
42 
43 /* MTX I/O stream local data */
44 
45 typedef struct mtx_local {
46   int fd;			/* file descriptor for I/O */
47   off_t filesize;		/* file size parsed */
48   unsigned char *buf;		/* temporary buffer */
49 } MTXLOCAL;
50 
51 
52 /* Drive-dependent data passed to init method */
53 
54 typedef struct mtx_data {
55   int fd;			/* file data */
56   unsigned long pos;		/* initial position */
57 } MTXDATA;
58 
59 
60 /* Convenient access to local data */
61 
62 #define LOCAL ((MTXLOCAL *) stream->local)
63 
64 
65 /* Function prototypes */
66 
67 DRIVER *mtx_valid (char *name);
68 long mtx_isvalid (char *name,char *tmp);
69 void *mtx_parameters (long function,void *value);
70 void mtx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
71 void mtx_list (MAILSTREAM *stream,char *ref,char *pat);
72 void mtx_lsub (MAILSTREAM *stream,char *ref,char *pat);
73 long mtx_create (MAILSTREAM *stream,char *mailbox);
74 long mtx_delete (MAILSTREAM *stream,char *mailbox);
75 long mtx_rename (MAILSTREAM *stream,char *old,char *newname);
76 MAILSTREAM *mtx_open (MAILSTREAM *stream);
77 void mtx_close (MAILSTREAM *stream,long options);
78 char *mtx_header (MAILSTREAM *stream,unsigned long msgno,
79 		  unsigned long *length,long flags);
80 long mtx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
81 void mtx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
82 long mtx_ping (MAILSTREAM *stream);
83 void mtx_check (MAILSTREAM *stream);
84 long mtx_expunge (MAILSTREAM *stream,char *sequence,long options);
85 long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
86 long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
87 
88 char *mtx_file (char *dst,char *name);
89 long mtx_badname (char *tmp,char *s);
90 long mtx_parse (MAILSTREAM *stream);
91 void mtx_update_status (MAILSTREAM *stream,unsigned long msgno);
92 unsigned long mtx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
93 			  unsigned long *size);
94 
95 /* MTX mail routines */
96 
97 
98 /* Driver dispatch used by MAIL */
99 
100 DRIVER mtxdriver = {
101   "mtx",			/* driver name */
102 				/* driver flags */
103   DR_LOCAL|DR_MAIL|DR_LOWMEM|DR_CRLF|DR_NOSTICKY,
104   (DRIVER *) NIL,		/* next driver */
105   mtx_valid,			/* mailbox is valid for us */
106   mtx_parameters,		/* manipulate parameters */
107   mtx_scan,			/* scan mailboxes */
108   mtx_list,			/* list mailboxes */
109   mtx_lsub,			/* list subscribed mailboxes */
110   NIL,				/* subscribe to mailbox */
111   NIL,				/* unsubscribe from mailbox */
112   mtx_create,			/* create mailbox */
113   mtx_delete,			/* delete mailbox */
114   mtx_rename,			/* rename mailbox */
115   mail_status_default,		/* status of mailbox */
116   mtx_open,			/* open mailbox */
117   mtx_close,			/* close mailbox */
118   NIL,				/* fetch message "fast" attributes */
119   NIL,				/* fetch message flags */
120   NIL,				/* fetch overview */
121   NIL,				/* fetch message envelopes */
122   mtx_header,			/* fetch message header */
123   mtx_text,			/* fetch message body */
124   NIL,				/* fetch partial message text */
125   NIL,				/* unique identifier */
126   NIL,				/* message number */
127   NIL,				/* modify flags */
128   mtx_flagmsg,			/* per-message modify flags */
129   NIL,				/* search for message based on criteria */
130   NIL,				/* sort messages */
131   NIL,				/* thread messages */
132   mtx_ping,			/* ping mailbox to see if still alive */
133   mtx_check,			/* check for new messages */
134   mtx_expunge,			/* expunge deleted messages */
135   mtx_copy,			/* copy messages to another mailbox */
136   mtx_append,			/* append string message to mailbox */
137   NIL				/* garbage collect stream */
138 };
139 
140 				/* prototype stream */
141 MAILSTREAM mtxproto = {&mtxdriver};
142 
143 /* MTX mail validate mailbox
144  * Accepts: mailbox name
145  * Returns: our driver if name is valid, NIL otherwise
146  */
147 
mtx_valid(char * name)148 DRIVER *mtx_valid (char *name)
149 {
150   char tmp[MAILTMPLEN];
151   return mtx_isvalid (name,tmp) ? &mtxdriver : (DRIVER *) NIL;
152 }
153 
154 
155 /* MTX mail test for valid mailbox
156  * Accepts: mailbox name
157  * Returns: T if valid, NIL otherwise
158  */
159 
mtx_isvalid(char * name,char * tmp)160 long mtx_isvalid (char *name,char *tmp)
161 {
162   int fd;
163   long ret = NIL;
164   char *s;
165   struct stat sbuf;
166   errno = EINVAL;		/* assume invalid argument */
167 				/* if file, get its status */
168   if ((*name != '{') && mailboxfile (tmp,name) && !stat (tmp,&sbuf)) {
169     if (!sbuf.st_size)errno = 0;/* empty file */
170     else if ((fd = open (tmp,O_BINARY|O_RDONLY,NIL)) >= 0) {
171       memset (tmp,'\0',MAILTMPLEN);
172       if ((read (fd,tmp,64) >= 0) && (s = strchr (tmp,'\015')) &&
173 	  (s[1] == '\012')) {	/* valid format? */
174 	*s = '\0';		/* tie off header */
175 				/* must begin with dd-mmm-yy" */
176 	ret = (((tmp[2] == '-' && tmp[6] == '-') ||
177 		(tmp[1] == '-' && tmp[5] == '-')) &&
178 	       (s = strchr (tmp+18,',')) && strchr (s+2,';')) ? T : NIL;
179       }
180       else errno = -1;		/* bogus format */
181       close (fd);		/* close the file */
182     }
183   }
184 				/* in case INBOX but not mtx format */
185   else if ((errno == ENOENT) && ((name[0] == 'I') || (name[0] == 'i')) &&
186 	   ((name[1] == 'N') || (name[1] == 'n')) &&
187 	   ((name[2] == 'B') || (name[2] == 'b')) &&
188 	   ((name[3] == 'O') || (name[3] == 'o')) &&
189 	   ((name[4] == 'X') || (name[4] == 'x')) && !name[5]) errno = -1;
190   return ret;			/* return what we should */
191 }
192 
193 
194 /* MTX manipulate driver parameters
195  * Accepts: function code
196  *	    function-dependent value
197  * Returns: function-dependent return value
198  */
199 
mtx_parameters(long function,void * value)200 void *mtx_parameters (long function,void *value)
201 {
202   return NIL;
203 }
204 
205 /* MTX mail scan mailboxes
206  * Accepts: mail stream
207  *	    reference
208  *	    pattern to search
209  *	    string to scan
210  */
211 
mtx_scan(MAILSTREAM * stream,char * ref,char * pat,char * contents)212 void mtx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
213 {
214   if (stream) dummy_scan (NIL,ref,pat,contents);
215 }
216 
217 
218 /* MTX mail list mailboxes
219  * Accepts: mail stream
220  *	    reference
221  *	    pattern to search
222  */
223 
mtx_list(MAILSTREAM * stream,char * ref,char * pat)224 void mtx_list (MAILSTREAM *stream,char *ref,char *pat)
225 {
226   if (stream) dummy_list (stream,ref,pat);
227 }
228 
229 
230 /* MTX mail list subscribed mailboxes
231  * Accepts: mail stream
232  *	    reference
233  *	    pattern to search
234  */
235 
mtx_lsub(MAILSTREAM * stream,char * ref,char * pat)236 void mtx_lsub (MAILSTREAM *stream,char *ref,char *pat)
237 {
238   if (stream) dummy_lsub (stream,ref,pat);
239 }
240 
241 /* MTX mail create mailbox
242  * Accepts: MAIL stream
243  *	    mailbox name to create
244  * Returns: T on success, NIL on failure
245  */
246 
mtx_create(MAILSTREAM * stream,char * mailbox)247 long mtx_create (MAILSTREAM *stream,char *mailbox)
248 {
249   return dummy_create (stream,mailbox);
250 }
251 
252 
253 /* MTX mail delete mailbox
254  * Accepts: MAIL stream
255  *	    mailbox name to delete
256  * Returns: T on success, NIL on failure
257  */
258 
mtx_delete(MAILSTREAM * stream,char * mailbox)259 long mtx_delete (MAILSTREAM *stream,char *mailbox)
260 {
261   return dummy_delete (stream,mailbox);
262 }
263 
264 
265 /* MTX mail rename mailbox
266  * Accepts: MAIL stream
267  *	    old mailbox name
268  *	    new mailbox name (or NIL for delete)
269  * Returns: T on success, NIL on failure
270  */
271 
mtx_rename(MAILSTREAM * stream,char * old,char * newname)272 long mtx_rename (MAILSTREAM *stream,char *old,char *newname)
273 {
274   return dummy_rename (stream,old,newname);
275 }
276 
277 /* MTX mail open
278  * Accepts: stream to open
279  * Returns: stream on success, NIL on failure
280  */
281 
mtx_open(MAILSTREAM * stream)282 MAILSTREAM *mtx_open (MAILSTREAM *stream)
283 {
284   long i;
285   int fd;
286   char *s;
287   char tmp[MAILTMPLEN];
288 				/* return prototype for OP_PROTOTYPE call */
289   if (!stream) return &mtxproto;
290   if (stream->local) fatal ("mtx recycle stream");
291   if (!mailboxfile (tmp,stream->mailbox))
292     return (MAILSTREAM *) mtx_badname (tmp,stream->mailbox);
293 				/* open, possibly creating INBOX */
294   if (((fd = open (tmp,O_BINARY|(stream->rdonly ? O_RDONLY:O_RDWR),NIL)) < 0)&&
295       (compare_cstring (stream->mailbox,"INBOX") ||
296        ((fd = open (tmp,O_BINARY|O_RDWR|O_CREAT|O_EXCL,S_IREAD|S_IWRITE))<0))){
297     sprintf (tmp,"Can't open mailbox: %s",strerror (errno));
298     mm_log (tmp,ERROR);
299     return NIL;
300   }
301   stream->local = fs_get (sizeof (MTXLOCAL));
302 				/* canonicalize the stream mailbox name */
303   fs_give ((void **) &stream->mailbox);
304   if (s = strchr ((s = strrchr (tmp,'\\')) ? s : tmp,'.')) *s = '\0';
305   stream->mailbox = cpystr (tmp);
306   LOCAL->fd = fd;		/* note the file */
307   LOCAL->filesize = 0;		/* initialize parsed file size */
308   LOCAL->buf = NIL;		/* initially no local buffer */
309   stream->sequence++;		/* bump sequence number */
310   stream->uid_validity = time (0);
311 				/* parse mailbox */
312   stream->nmsgs = stream->recent = 0;
313   if (!mtx_ping (stream)) return NIL;
314   if (!stream->nmsgs) mm_log ("Mailbox is empty",(long) NIL);
315   stream->perm_seen = stream->perm_deleted =
316     stream->perm_flagged = stream->perm_answered = stream->perm_draft =
317       stream->rdonly ? NIL : T;
318   stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
319   return stream;		/* return stream to caller */
320 }
321 
322 /* MTX mail close
323  * Accepts: MAIL stream
324  *	    close options
325  */
326 
mtx_close(MAILSTREAM * stream,long options)327 void mtx_close (MAILSTREAM *stream,long options)
328 {
329   if (stream && LOCAL) {	/* only if a file is open */
330     int silent = stream->silent;
331     stream->silent = T;
332     if (options & CL_EXPUNGE) mtx_expunge (stream,NIL,NIL);
333     close (LOCAL->fd);		/* close the local file */
334     if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
335 				/* nuke the local data */
336     fs_give ((void **) &stream->local);
337     stream->dtb = NIL;		/* log out the DTB */
338   }
339 }
340 
341 /* MTX mail fetch message header
342  * Accepts: MAIL stream
343  *	    message # to fetch
344  *	    pointer to returned header text length
345  *	    option flags
346  * Returns: message header in RFC822 format
347  */
348 
mtx_header(MAILSTREAM * stream,unsigned long msgno,unsigned long * length,long flags)349 char *mtx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
350 		  long flags)
351 {
352   *length = 0;			/* default to empty */
353   if (flags & FT_UID) return "";/* UID call "impossible" */
354 				/* get to header position */
355   lseek (LOCAL->fd,mtx_hdrpos (stream,msgno,length),L_SET);
356 				/* is buffer big enough? */
357   if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
358   LOCAL->buf = (char *) fs_get ((size_t) *length + 1);
359   LOCAL->buf[*length] = '\0';	/* tie off string */
360 				/* slurp the data */
361   read (LOCAL->fd,LOCAL->buf,(size_t) *length);
362   return LOCAL->buf;
363 }
364 
365 
366 /* MTX mail fetch message text (body only)
367  * Accepts: MAIL stream
368  *	    message # to fetch
369  *	    pointer to returned header text length
370  *	    option flags
371  * Returns: T, always
372  */
373 
mtx_text(MAILSTREAM * stream,unsigned long msgno,STRING * bs,long flags)374 long mtx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
375 {
376   MESSAGECACHE *elt;
377   FDDATA d;
378   unsigned long hdrsize,hdrpos;
379 				/* UID call "impossible" */
380   if (flags & FT_UID) return NIL;
381   elt = mail_elt (stream,msgno);/* if message not seen */
382   if (elt->seen && !(flags & FT_PEEK)) {
383     elt->seen = T;		/* mark message as seen */
384 				/* recalculate status */
385     mtx_update_status (stream,msgno);
386     mm_flags (stream,msgno);
387   }
388 				/* get location of text data */
389   hdrpos = mtx_hdrpos (stream,msgno,&hdrsize);
390   d.fd = LOCAL->fd;		/* set initial stringstruct */
391   d.pos = hdrpos + hdrsize;
392 				/* flush old buffer */
393   if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
394   d.chunk = LOCAL->buf = (char *) fs_get ((size_t) d.chunksize = CHUNKSIZE);
395   INIT (bs,fd_string,(void *) &d,elt->rfc822_size - hdrsize);
396   return T;			/* success */
397 }
398 
399 /* MTX mail per-message modify flags
400  * Accepts: MAIL stream
401  *	    message cache element
402  */
403 
mtx_flagmsg(MAILSTREAM * stream,MESSAGECACHE * elt)404 void mtx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
405 {
406 				/* recalculate status */
407   mtx_update_status (stream,elt->msgno);
408 }
409 
410 
411 /* MTX mail ping mailbox
412  * Accepts: MAIL stream
413  * Returns: T if stream still alive, NIL if not
414  */
415 
mtx_ping(MAILSTREAM * stream)416 long mtx_ping (MAILSTREAM *stream)
417 {
418 				/* punt if stream no longer alive */
419   if (!(stream && LOCAL)) return NIL;
420 				/* parse mailbox, punt if parse dies */
421   return (mtx_parse (stream)) ? T : NIL;
422 }
423 
424 
425 /* MTX mail check mailbox (reparses status too)
426  * Accepts: MAIL stream
427  */
428 
mtx_check(MAILSTREAM * stream)429 void mtx_check (MAILSTREAM *stream)
430 {
431   unsigned long i = 1;
432   if (mtx_ping (stream)) {	/* ping mailbox */
433 				/* get new message status */
434     while (i <= stream->nmsgs) mail_elt (stream,i++);
435     mm_log ("Check completed",(long) NIL);
436   }
437 }
438 
439 /* MTX mail expunge mailbox
440  * Accepts: MAIL stream
441  *	    sequence to expunge if non-NIL
442  *	    expunge options
443  * Returns: T, always
444  */
445 
mtx_expunge(MAILSTREAM * stream,char * sequence,long options)446 long mtx_expunge (MAILSTREAM *stream,char *sequence,long options)
447 {
448   long ret;
449   unsigned long i = 1;
450   unsigned long j,k,m,recent;
451   unsigned long n = 0;
452   unsigned long delta = 0;
453   MESSAGECACHE *elt;
454   char tmp[MAILTMPLEN];
455   if (!(ret = (sequence ? ((options & EX_UID) ?
456 			   mail_uid_sequence (stream,sequence) :
457 			   mail_sequence (stream,sequence)) : LONGT) &&
458 	mtx_ping (stream)));	/* parse sequence if given, ping stream */
459   else if (stream->rdonly) mm_log ("Expunge ignored on readonly mailbox",WARN);
460   else {
461     mm_critical (stream);	/* go critical */
462     recent = stream->recent;	/* get recent now that pinged */
463     while (i <= stream->nmsgs) {/* for each message */
464       elt = mail_elt (stream,i);/* get cache element */
465 				/* number of bytes to smash or preserve */
466       k = elt->private.special.text.size + elt->rfc822_size;
467 				/* if need to expunge this message */
468       if (elt->deleted && (sequence ? elt->sequence : T)) {
469 				/* if recent, note one less recent message */
470 	if (elt->recent) --recent;
471 	delta += k;		/* number of bytes to delete */
472 				/* notify upper levels */
473 	mail_expunged (stream,i);
474 	n++;			/* count up one more deleted message */
475       }
476       else if (i++ && delta) {	/* preserved message */
477 				/* first byte to preserve */
478 	j = elt->private.special.offset;
479 	do {			/* read from source position */
480 	  m = min (k,(unsigned long) MAILTMPLEN);
481 	  lseek (LOCAL->fd,j,SEEK_SET);
482 	  read (LOCAL->fd,tmp,(size_t) m);
483 				/* write to destination position */
484 	  lseek (LOCAL->fd,j - delta,SEEK_SET);
485 	  write (LOCAL->fd,tmp,(size_t) m);
486 	  j += m;		/* next chunk, perhaps */
487 	} while (k -= m);	/* until done */
488 	elt->private.special.offset -= delta;
489       }
490     }
491     if (n) {			/* truncate file after last message */
492       chsize (LOCAL->fd,LOCAL->filesize -= delta);
493       sprintf (tmp,"Expunged %ld messages",n);
494       mm_log (tmp,(long) NIL);	/* output the news */
495     }
496     else mm_log ("No messages deleted, so no update needed",(long) NIL);
497     mm_nocritical (stream);	/* release critical */
498 				/* notify upper level of new mailbox size */
499     mail_exists (stream,stream->nmsgs);
500     mail_recent (stream,recent);
501   }
502   return ret;
503 }
504 
505 /* MTX mail copy message(s)
506  * Accepts: MAIL stream
507  *	    sequence
508  *	    destination mailbox
509  *	    copy options
510  * Returns: T if success, NIL if failed
511  */
512 
mtx_copy(MAILSTREAM * stream,char * sequence,char * mailbox,long options)513 long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
514 {
515   char tmp[MAILTMPLEN];
516   struct stat sbuf;
517   MESSAGECACHE *elt;
518   unsigned long i,j,k;
519   long ret = LONGT;
520   int fd;
521   mailproxycopy_t pc =
522     (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
523   if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
524 	mail_sequence (stream,sequence))) return NIL;
525   if (!mtx_isvalid (mailbox,tmp)) switch (errno) {
526   case ENOENT:			/* no such file? */
527     mm_notify (stream,"[TRYCREATE] Must create mailbox before append",
528 	       (long) NIL);
529     return NIL;
530   case 0:			/* merely empty file? */
531     break;
532   case EINVAL:			/* name is bogus */
533     if (pc) return (*pc) (stream,sequence,mailbox,options);
534     return mtx_badname (tmp,mailbox);
535   default:			/* file exists, but not valid format */
536     if (pc) return (*pc) (stream,sequence,mailbox,options);
537     sprintf (tmp,"Not a MTX-format mailbox: %s",mailbox);
538     mm_log (tmp,ERROR);
539     return NIL;
540   }
541 				/* open the destination */
542   if (!mailboxfile (tmp,mailbox) ||
543       (fd = open (tmp,O_BINARY|O_WRONLY|O_APPEND|O_CREAT,
544 		  S_IREAD|S_IWRITE)) < 0) {
545     sprintf (tmp,"Unable to open copy mailbox: %s",strerror (errno));
546     mm_log (tmp,ERROR);
547     return NIL;
548   }
549 
550   mm_critical (stream);		/* go critical */
551   fstat (fd,&sbuf);		/* get current file size */
552 				/* for each requested message */
553   for (i = 1; ret && (i <= stream->nmsgs); i++)
554     if ((elt = mail_elt (stream,i))->sequence) {
555       lseek (LOCAL->fd,elt->private.special.offset,SEEK_SET);
556 				/* number of bytes to copy */
557       k = elt->private.special.text.size + elt->rfc822_size;
558       do {			/* read from source position */
559 	j = min (k,(long) MAILTMPLEN);
560 	read (LOCAL->fd,tmp,(size_t) j);
561 	if (write (fd,tmp,(size_t) j) < 0) {
562 	  sprintf (tmp,"Unable to write message: %s",strerror (errno));
563 	  mm_log (tmp,ERROR);
564 	  chsize (fd,sbuf.st_size);
565 	  j = k;
566 	  ret = NIL;		/* note error */
567 	  break;
568 	}
569       } while (k -= j);		/* until done */
570     }
571   close (fd);			/* close the file */
572   mm_nocritical (stream);	/* release critical */
573 				/* delete all requested messages */
574   if (ret && (options & CP_MOVE)) for (i = 1; i <= stream->nmsgs; i++)
575     if ((elt = mail_elt (stream,i))->sequence) {
576       elt->deleted = T;		/* mark message deleted */
577 				/* recalculate status */
578       mtx_update_status (stream,i);
579     }
580   if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
581     mm_log ("Can not return meaningful COPYUID with this mailbox format",WARN);
582   return ret;
583 }
584 
585 /* MTX mail append message from stringstruct
586  * Accepts: MAIL stream
587  *	    destination mailbox
588  *	    append callback
589  *	    data for callback
590  * Returns: T if append successful, else NIL
591  */
592 
mtx_append(MAILSTREAM * stream,char * mailbox,append_t af,void * data)593 long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
594 {
595   struct stat sbuf;
596   int fd,ld,c;
597   char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN];
598   FILE *df;
599   MESSAGECACHE elt;
600   long f;
601   unsigned long i,uf;
602   STRING *message;
603   long ret = LONGT;
604 				/* default stream to prototype */
605   if (!stream) stream = &mtxproto;
606 				/* make sure valid mailbox */
607   if (!mtx_isvalid (mailbox,tmp)) switch (errno) {
608   case ENOENT:			/* no such file? */
609     if (((mailbox[0] == 'I') || (mailbox[0] == 'i')) &&
610 	((mailbox[1] == 'N') || (mailbox[1] == 'n')) &&
611 	((mailbox[2] == 'B') || (mailbox[2] == 'b')) &&
612 	((mailbox[3] == 'O') || (mailbox[3] == 'o')) &&
613 	((mailbox[4] == 'X') || (mailbox[4] == 'x')) && !mailbox[5])
614       dummy_create (NIL,"INBOX.MTX");
615     else {
616       mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
617       return NIL;
618     }
619 				/* falls through */
620   case 0:			/* merely empty file? */
621     break;
622   case EINVAL:
623     return mtx_badname (tmp,mailbox);
624   default:
625     sprintf (tmp,"Not a MTX-format mailbox: %.80s",mailbox);
626     mm_log (tmp,ERROR);
627     return NIL;
628   }
629 				/* get first message */
630   if (!(*af) (stream,data,&flags,&date,&message)) return NIL;
631 				/* open destination mailbox */
632   if (!mailboxfile (file,mailbox) ||
633       ((fd = open (file,O_BINARY|O_WRONLY|O_APPEND|O_CREAT,
634 		   S_IREAD|S_IWRITE)) < 0) || !(df = fdopen (fd,"ab"))) {
635     sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
636     mm_log (tmp,ERROR);
637     return NIL;
638   }
639   mm_critical (stream);		/* go critical */
640   fstat (fd,&sbuf);		/* get current file size */
641 
642   errno = 0;
643   do {				/* parse flags */
644     if (!SIZE (message)) {	/* guard against zero-length */
645       mm_log ("Append of zero-length message",ERROR);
646       ret = NIL;
647       break;
648     }
649     f = mail_parse_flags (stream,flags,&i);
650 				/* reverse bits (dontcha wish we had CIRC?) */
651     for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));
652     if (date) {			/* parse date if given */
653       if (!mail_parse_date (&elt,date)) {
654 	sprintf (tmp,"Bad date in append: %.80s",date);
655 	mm_log (tmp,ERROR);
656 	ret = NIL;		/* mark failure */
657 	break;
658       }
659       mail_date (tmp,&elt);	/* write preseved date */
660     }
661     else internal_date (tmp);	/* get current date in IMAP format */
662 				/* write header */
663     if (fprintf (df,"%s,%lu;%010lo%02lo\015\012",tmp,i = SIZE (message),uf,
664 		 (unsigned long) f) < 0) ret = NIL;
665     else {			/* write message */
666       if (i) do c = 0xff & SNX (message);
667       while ((putc (c,df) != EOF) && --i);
668 				/* get next message */
669       if (i || !(*af) (stream,data,&flags,&date,&message)) ret = NIL;
670     }
671   } while (ret && message);
672 				/* revert file if failure */
673   if (!ret || (fflush (df) == EOF)) {
674     chsize (fd,sbuf.st_size);	/* revert file */
675     close (fd);			/* make sure fclose() doesn't corrupt us */
676     if (errno) {
677       sprintf (tmp,"Message append failed: %s",strerror (errno));
678       mm_log (tmp,ERROR);
679     }
680     ret = NIL;
681   }
682   fclose (df);			/* close the file */
683   mm_nocritical (stream);	/* release critical */
684   if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))
685     mm_log ("Can not return meaningful APPENDUID with this mailbox format",
686 	    WARN);
687   return ret;
688 }
689 
690 
691 /* Return bad file name error message
692  * Accepts: temporary buffer
693  *	    file name
694  * Returns: long NIL always
695  */
696 
mtx_badname(char * tmp,char * s)697 long mtx_badname (char *tmp,char *s)
698 {
699   sprintf (tmp,"Invalid mailbox name: %s",s);
700   mm_log (tmp,ERROR);
701   return (long) NIL;
702 }
703 
704 /* Parse mailbox
705  * Accepts: MAIL stream
706  * Returns: T if parse OK
707  *	    NIL if failure, stream aborted
708  */
709 
mtx_parse(MAILSTREAM * stream)710 long mtx_parse (MAILSTREAM *stream)
711 {
712   struct stat sbuf;
713   MESSAGECACHE *elt = NIL;
714   unsigned char *s,*t,*x,lbuf[65];
715   char tmp[MAILTMPLEN];
716   long i;
717   long curpos = LOCAL->filesize;
718   long nmsgs = stream->nmsgs;
719   long recent = stream->recent;
720   fstat (LOCAL->fd,&sbuf);	/* get status */
721   if (sbuf.st_size < curpos) {	/* sanity check */
722     sprintf (tmp,"Mailbox shrank from %ld to %ld!",curpos,sbuf.st_size);
723     mm_log (tmp,ERROR);
724     mtx_close (stream,NIL);
725     return NIL;
726   }
727 				/* while there is stuff to parse */
728   while (i = sbuf.st_size - curpos) {
729 				/* get to that position in the file */
730     lseek (LOCAL->fd,curpos,SEEK_SET);
731     if ((i = read (LOCAL->fd,lbuf,64)) <= 0) {
732       sprintf (tmp,"Unable to read internal header at %ld, size = %ld: %s",
733 	       curpos,sbuf.st_size,i ? strerror (errno) : "no data read");
734       mm_log (tmp,ERROR);
735       mtx_close (stream,NIL);
736       return NIL;
737     }
738     lbuf[i] = '\0';		/* tie off buffer just in case */
739     if (!((s = strchr (lbuf,'\015')) && (s[1] == '\012'))) {
740       sprintf (tmp,"Unable to find end of line at %ld in %ld bytes, text: %s",
741 	       curpos,i,(char *) lbuf);
742       mm_log (tmp,ERROR);
743       mtx_close (stream,NIL);
744       return NIL;
745     }
746     *s = '\0';			/* tie off header line */
747     i = (s + 2) - lbuf;		/* note start of text offset */
748     if (!((s = strchr (lbuf,',')) && (t = strchr (s+1,';')))) {
749       sprintf (tmp,"Unable to parse internal header at %ld: %s",curpos,
750 	       (char *) lbuf);
751       mm_log (tmp,ERROR);
752       mtx_close (stream,NIL);
753       return NIL;
754     }
755 
756     *s++ = '\0'; *t++ = '\0';	/* tie off fields */
757 				/* intantiate an elt for this message */
758     (elt = mail_elt (stream,++nmsgs))->valid = T;
759     elt->private.uid = ++stream->uid_last;
760 				/* note file offset of header */
761     elt->private.special.offset = curpos;
762 				/* as well as offset from header of message */
763     elt->private.special.text.size = i;
764 				/* header size not known yet */
765     elt->private.msg.header.text.size = 0;
766 				/* parse the header components */
767     if (!(mail_parse_date (elt,lbuf) &&
768 	  (elt->rfc822_size = strtol (x = s,(char **) &s,10)) && (!(s && *s))&&
769 	  isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&
770 	  isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&
771 	  isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&
772 	  isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])) {
773       sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",
774 	       curpos,(char *) lbuf,(char *) x,(char *) t);
775       mtx_close (stream,NIL);
776       return NIL;
777     }
778 				/* update current position to next header */
779     curpos += i + elt->rfc822_size;
780 				/* calculate system flags */
781     if ((i = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;
782     if (i & fDELETED) elt->deleted = T;
783     if (i & fFLAGGED) elt->flagged = T;
784     if (i & fANSWERED) elt->answered = T;
785     if (i & fDRAFT) elt->draft = T;
786     if (curpos > sbuf.st_size) {
787       sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
788 	       elt->private.special.offset,curpos,sbuf.st_size);
789       mm_log (tmp,ERROR);
790       mtx_close (stream,NIL);
791       return NIL;
792     }
793   }
794 				/* update parsed file size */
795   LOCAL->filesize = sbuf.st_size;
796   mail_exists (stream,nmsgs);	/* notify upper level of new mailbox size */
797   mail_recent (stream,recent);	/* and of change in recent messages */
798   return T;			/* return the winnage */
799 }
800 
801 /* Update status string
802  * Accepts: MAIL stream
803  *	    message number
804  */
805 
mtx_update_status(MAILSTREAM * stream,unsigned long msgno)806 void mtx_update_status (MAILSTREAM *stream,unsigned long msgno)
807 {
808   char tmp[MAILTMPLEN];
809   MESSAGECACHE *elt = mail_elt (stream,msgno);
810   unsigned long j,k = 0;
811 				/* not if readonly you don't */
812   if (stream->rdonly || !elt->valid) return;
813   j = elt->user_flags;		/* get user flags */
814 				/* reverse bits (dontcha wish we had CIRC?) */
815   while (j) k |= 1 << 29 - find_rightmost_bit (&j);
816   sprintf (tmp,"%010lo%02o",k,	/* print new flag string */
817 	   fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
818 	   (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
819 	   (fDRAFT * elt->draft));
820 				/* get to that place in the file */
821   lseek (LOCAL->fd,(off_t) elt->private.special.offset +
822 	 elt->private.special.text.size - 14,SEEK_SET);
823   write (LOCAL->fd,tmp,12);	/* write new flags */
824 }
825 
826 /* MTX locate header for a message
827  * Accepts: MAIL stream
828  *	    message number
829  *	    pointer to returned header size
830  * Returns: position of header in file
831  */
832 
mtx_hdrpos(MAILSTREAM * stream,unsigned long msgno,unsigned long * size)833 unsigned long mtx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
834 			  unsigned long *size)
835 {
836   unsigned long siz;
837   size_t i = 0;
838   int q = 0;
839   char *s,tmp[MAILTMPLEN];
840   MESSAGECACHE *elt = mail_elt (stream,msgno);
841   long pos = elt->private.special.offset + elt->private.special.text.size;
842 				/* is header size known? */
843   if (!(*size = elt->private.msg.header.text.size)) {
844 				/* get to header position */
845     lseek (LOCAL->fd,pos,SEEK_SET);
846 				/* search message for CRLF CRLF */
847     for (siz = 1; siz <= elt->rfc822_size; siz++) {
848       if (!i &&			/* buffer empty? */
849 	  (read (LOCAL->fd,s = tmp,
850 		 i = (size_t) min(elt->rfc822_size-siz,(long)MAILTMPLEN))<= 0))
851 	return pos;
852       else i--;
853       switch (q) {		/* sniff at buffer */
854       case 0:			/* first character */
855 	q = (*s++ == '\015') ? 1 : 0;
856 	break;
857       case 1:			/* second character */
858 	q = (*s++ == '\012') ? 2 : 0;
859 	break;
860       case 2:			/* third character */
861 	q = (*s++ == '\015') ? 3 : 0;
862 	break;
863       case 3:			/* fourth character */
864 	if (*s++ == '\012') {	/* have the sequence? */
865 				/* yes, note for later */
866 	  elt->private.msg.header.text.size = (*size = siz);
867 	  return pos;		/* return to caller */
868 	}
869 	q = 0;			/* lost... */
870 	break;
871       }
872     }
873   }
874   return pos;			/* have position */
875 }
876