1 /* ========================================================================
2  * Copyright 2008-2011 Mark Crispin
3  * ========================================================================
4  */
5 
6 /*
7  * Program:	Dummy routines for DOS
8  *
9  * Author:	Mark Crispin
10  *
11  * Date:	24 May 1993
12  * Last Edited:	8 April 2011
13  *
14  * Previous versions of this file were
15  *
16  * Copyright 1988-2006 University of Washington
17  *
18  * Licensed under the Apache License, Version 2.0 (the "License");
19  * you may not use this file except in compliance with the License.
20  * You may obtain a copy of the License at
21  *
22  *     http://www.apache.org/licenses/LICENSE-2.0
23  *
24  */
25 
26 
27 #include <ctype.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include "mail.h"
31 #include "osdep.h"
32 #include <sys\stat.h>
33 #include <dos.h>
34 #include "dummy.h"
35 #include "misc.h"
36 
37 /* Function prototypes */
38 
39 DRIVER *dummy_valid (char *name);
40 void *dummy_parameters (long function,void *value);
41 void dummy_list_work (MAILSTREAM *stream,char *dir,char *pat,char *contents,
42 		      long level);
43 long dummy_listed (MAILSTREAM *stream,char delimiter,char *name,
44 		   long attributes,char *contents);
45 long dummy_subscribe (MAILSTREAM *stream,char *mailbox);
46 MAILSTREAM *dummy_open (MAILSTREAM *stream);
47 void dummy_close (MAILSTREAM *stream,long options);
48 long dummy_ping (MAILSTREAM *stream);
49 void dummy_check (MAILSTREAM *stream);
50 long dummy_expunge (MAILSTREAM *stream,char *sequence,long options);
51 long dummy_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
52 long dummy_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
53 long dummy_badname (char *tmp,char *s);
54 
55 /* Dummy routines */
56 
57 
58 /* Driver dispatch used by MAIL */
59 
60 DRIVER dummydriver = {
61   "dummy",			/* driver name */
62   DR_LOCAL|DR_MAIL,		/* driver flags */
63   (DRIVER *) NIL,		/* next driver */
64   dummy_valid,			/* mailbox is valid for us */
65   dummy_parameters,		/* manipulate parameters */
66   dummy_scan,			/* scan mailboxes */
67   dummy_list,			/* list mailboxes */
68   dummy_lsub,			/* list subscribed mailboxes */
69   dummy_subscribe,		/* subscribe to mailbox */
70   NIL,				/* unsubscribe from mailbox */
71   dummy_create,			/* create mailbox */
72   dummy_delete,			/* delete mailbox */
73   dummy_rename,			/* rename mailbox */
74   mail_status_default,		/* status of mailbox */
75   dummy_open,			/* open mailbox */
76   dummy_close,			/* close mailbox */
77   NIL,				/* fetch message "fast" attributes */
78   NIL,				/* fetch message flags */
79   NIL,				/* fetch overview */
80   NIL,				/* fetch message structure */
81   NIL,				/* fetch header */
82   NIL,				/* fetch text */
83   NIL,				/* fetch message data */
84   NIL,				/* unique identifier */
85   NIL,				/* message number from UID */
86   NIL,				/* modify flags */
87   NIL,				/* per-message modify flags */
88   NIL,				/* search for message based on criteria */
89   NIL,				/* sort messages */
90   NIL,				/* thread messages */
91   dummy_ping,			/* ping mailbox to see if still alive */
92   dummy_check,			/* check for new messages */
93   dummy_expunge,		/* expunge deleted messages */
94   dummy_copy,			/* copy messages to another mailbox */
95   dummy_append,			/* append string message to mailbox */
96   NIL				/* garbage collect stream */
97 };
98 
99 
100 				/* prototype stream */
101 MAILSTREAM dummyproto = {&dummydriver};
102 
103 				/* driver parameters */
104 static char *file_extension = NIL;
105 
106 /* Dummy validate mailbox
107  * Accepts: mailbox name
108  * Returns: our driver if name is valid, NIL otherwise
109  */
110 
dummy_valid(char * name)111 DRIVER *dummy_valid (char *name)
112 {
113   char *s,tmp[MAILTMPLEN];
114   struct stat sbuf;
115 				/* must be valid local mailbox */
116   return (name && *name && (*name != '{') &&
117 	  (s = mailboxfile (tmp,name)) && (!*s || !stat (s,&sbuf))) ?
118 	    &dummydriver : NIL;
119 }
120 
121 
122 /* Dummy manipulate driver parameters
123  * Accepts: function code
124  *	    function-dependent value
125  * Returns: function-dependent return value
126  */
127 
dummy_parameters(long function,void * value)128 void *dummy_parameters (long function,void *value)
129 {
130   void *ret = NIL;
131   switch ((int) function) {
132   case SET_EXTENSION:
133     if (file_extension) fs_give ((void **) &file_extension);
134     if (*(char *) value) file_extension = cpystr ((char *) value);
135   case GET_EXTENSION:
136     ret = (void *) file_extension;
137   }
138   return ret;
139 }
140 
141 /* Dummy scan mailboxes
142  * Accepts: mail stream
143  *	    reference
144  *	    pattern to search
145  *	    string to scan
146  */
147 
148 #define LISTTMPLEN 128
149 
dummy_scan(MAILSTREAM * stream,char * ref,char * pat,char * contents)150 void dummy_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
151 {
152   char *s,test[LISTTMPLEN],file[LISTTMPLEN];
153   long i = 0;
154   if (!pat || !*pat) {		/* empty pattern? */
155     if (dummy_canonicalize (test,ref,"*")) {
156 				/* tie off name at root */
157       if (s = strchr (test,'\\')) *++s = '\0';
158       else test[0] = '\0';
159       dummy_listed (stream,'\\',test,LATT_NOINFERIORS,NIL);
160     }
161   }
162 				/* get canonical form of name */
163   else if (dummy_canonicalize (test,ref,pat)) {
164 				/* found any wildcards? */
165     if (s = strpbrk (test,"%*")) {
166 				/* yes, copy name up to that point */
167       strncpy (file,test,(size_t) (i = s - test));
168       file[i] = '\0';		/* tie off */
169     }
170     else strcpy (file,test);	/* use just that name then */
171 				/* find directory name */
172     if (s = strrchr (file,'\\')) {
173       *++s = '\0';		/* found, tie off at that point */
174       s = file;
175     }
176 				/* silly case */
177     else if (file[0] == '#') s = file;
178 				/* do the work */
179     dummy_list_work (stream,s,test,contents,0);
180     if (pmatch ("INBOX",test))	/* always an INBOX */
181       dummy_listed (stream,NIL,"INBOX",LATT_NOINFERIORS,contents);
182   }
183 }
184 
185 /* Dummy list mailboxes
186  * Accepts: mail stream
187  *	    reference
188  *	    pattern to search
189  */
190 
dummy_list(MAILSTREAM * stream,char * ref,char * pat)191 void dummy_list (MAILSTREAM *stream,char *ref,char *pat)
192 {
193   dummy_scan (stream,ref,pat,NIL);
194 }
195 
196 
197 /* Dummy list subscribed mailboxes
198  * Accepts: mail stream
199  *	    pattern to search
200  */
201 
dummy_lsub(MAILSTREAM * stream,char * ref,char * pat)202 void dummy_lsub (MAILSTREAM *stream,char *ref,char *pat)
203 {
204   void *sdb = NIL;
205   char *s,*t,test[MAILTMPLEN],tmp[MAILTMPLEN];
206   int showuppers = pat[strlen (pat) - 1] == '%';
207 				/* get canonical form of name */
208   if (dummy_canonicalize (test,ref,pat) && (s = sm_read (tmp,&sdb))) do
209     if (*s != '{') {
210       if (pmatch_full (s,test,'\\')) {
211 	if (pmatch (s,"INBOX")) mm_lsub (stream,NIL,s,LATT_NOINFERIORS);
212 	else mm_lsub (stream,'\\',s,NIL);
213       }
214       else while (showuppers && (t = strrchr (s,'\\'))) {
215 	*t = '\0';		/* tie off the name */
216 	if (pmatch_full (s,test,'\\')) mm_lsub (stream,'\\',s,LATT_NOSELECT);
217       }
218     }
219 				/* until no more subscriptions */
220   while (s = sm_read (tmp,&sdb));
221 }
222 
223 
224 /* Dummy subscribe to mailbox
225  * Accepts: mail stream
226  *	    mailbox to add to subscription list
227  * Returns: T on success, NIL on failure
228  */
229 
dummy_subscribe(MAILSTREAM * stream,char * mailbox)230 long dummy_subscribe (MAILSTREAM *stream,char *mailbox)
231 {
232   char *s,tmp[MAILTMPLEN];
233   struct stat sbuf;
234 				/* must be valid local mailbox */
235   if ((s = mailboxfile (tmp,mailbox)) && *s && !stat (s,&sbuf) &&
236       ((sbuf.st_mode & S_IFMT) == S_IFREG)) return sm_subscribe (mailbox);
237   sprintf (tmp,"Can't subscribe %s: not a mailbox",mailbox);
238   mm_log (tmp,ERROR);
239   return NIL;
240 }
241 
242 /* Dummy list mailboxes worker routine
243  * Accepts: mail stream
244  *	    directory name to search
245  *	    search pattern
246  *	    string to scan
247  *	    search level
248  */
249 
dummy_list_work(MAILSTREAM * stream,char * dir,char * pat,char * contents,long level)250 void dummy_list_work (MAILSTREAM *stream,char *dir,char *pat,char *contents,
251 		      long level)
252 {
253   struct find_t f;
254   struct stat sbuf;
255   char *s,tmp[LISTTMPLEN],tmpx[LISTTMPLEN];
256   char *base = (dir && (dir[0] == '\\')) ? NIL : myhomedir ();
257 				/* build name */
258   if (base) sprintf (tmpx,"%s\\",base);
259   else tmpx[0] = '\0';
260   if (dir) strcat (tmpx,dir);
261 				/* punt if bogus name */
262   if (!mailboxfile (tmp,tmpx)) return;
263 				/* make directory wildcard */
264   strcat (tmp,(tmp[strlen (tmp) -1] == '\\') ? "*." : "\\*.");
265   strcat (tmp,file_extension ? file_extension : "*");
266 				/* do nothing if can't open directory */
267   if (!_dos_findfirst (tmp,_A_NORMAL|_A_SUBDIR,&f)) {
268 				/* list it if at top-level */
269     if (!level && dir && pmatch_full (dir,pat,'\\'))
270       dummy_listed (stream,'\\',dir,LATT_NOSELECT,contents);
271 				/* scan directory */
272     if (tmpx[strlen (tmpx) - 1] == '\\') do if (*f.name != '.') {
273       if (base) sprintf (tmpx,"%s\\",base);
274       else tmpx[0] = '\0';
275       if (dir) sprintf (tmpx + strlen (tmpx),"%s%s",dir,f.name);
276       else strcat (tmpx,f.name);
277       if (mailboxfile (tmp,tmpx) && !stat (tmp,&sbuf)) {
278 				/* suppress extension */
279 	if (file_extension && (s = strchr (f.name,'.'))) *s = '\0';
280 				/* now make name we'd return */
281 	if (dir) sprintf (tmp,"%s%s",dir,f.name);
282 	else strcpy (tmp,f.name);
283 				/* only interested in file type */
284 	switch (sbuf.st_mode & S_IFMT) {
285 	case S_IFDIR:		/* directory? */
286 	  if (pmatch_full (tmp,pat,'\\')) {
287 	    dummy_listed (stream,'\\',tmp,LATT_NOSELECT,contents);
288 	    strcat (tmp,"\\");	/* set up for dmatch call */
289 	  }
290 				/* try again with trailing / */
291 	  else if (pmatch_full (strcat (tmp,"\\"),pat,'\\'))
292 	    dummy_listed (stream,'\\',tmp,LATT_NOSELECT,contents);
293 	  if (dmatch (tmp,pat,'\\') &&
294 	      (level < (long) mail_parameters (NIL,GET_LISTMAXLEVEL,NIL)))
295 	    dummy_list_work (stream,tmp,pat,contents,level+1);
296 	  break;
297 	case S_IFREG:		/* ordinary name */
298 	  if (pmatch_full (tmp,pat,'\\') && !pmatch ("INBOX",tmp))
299 	    dummy_listed (stream,'\\',tmp,LATT_NOINFERIORS,contents);
300 	  break;
301 	}
302       }
303     }
304     while (!_dos_findnext (&f));
305   }
306 }
307 
308 /* Mailbox found
309  * Accepts: hierarchy delimiter
310  *	    mailbox name
311  *	    attributes
312  *	    contents to search before calling mm_list()
313  * Returns: T, always
314  */
315 
316 #define BUFSIZE MAILTMPLEN
317 
dummy_listed(MAILSTREAM * stream,char delimiter,char * name,long attributes,char * contents)318 long dummy_listed (MAILSTREAM *stream,char delimiter,char *name,
319 		   long attributes,char *contents)
320 {
321   struct stat sbuf;
322   int fd;
323   size_t csiz,ssiz,bsiz;
324   char *buf,tmp[MAILTMPLEN];
325   if (contents) {		/* want to search contents? */
326 				/* forget it if can't select or open */
327     if ((attributes & LATT_NOSELECT) || !(csiz = strlen (contents)) ||
328 	!mailboxfile (tmp,name) || stat (tmp,&sbuf) || (csiz > sbuf.st_size) ||
329 	((fd = open (tmp,O_RDONLY,NIL)) < 0)) return T;
330 				/* get buffer including slop */
331     buf = (char *) fs_get (BUFSIZE + (ssiz = 4 * ((csiz / 4) + 1)) + 1);
332     memset (buf,'\0',ssiz);	/* no slop area the first time */
333     while (sbuf.st_size) {	/* until end of file */
334       read (fd,buf+ssiz,bsiz = min (sbuf.st_size,BUFSIZE));
335       if (search ((unsigned char *) buf,bsiz+ssiz,
336 		  (unsigned char *) contents,csiz)) break;
337       memcpy (buf,buf+BUFSIZE,ssiz);
338       sbuf.st_size -= bsiz;	/* note that we read that much */
339     }
340     fs_give ((void **) &buf);	/* flush buffer */
341     close (fd);			/* finished with file */
342     if (!sbuf.st_size) return T;/* not found */
343   }
344 				/* notify main program */
345   mm_list (stream,delimiter,name,attributes);
346   return T;
347 }
348 
349 /* Dummy create mailbox
350  * Accepts: mail stream
351  *	    mailbox name to create
352  * Returns: T on success, NIL on failure
353  */
354 
dummy_create(MAILSTREAM * stream,char * mailbox)355 long dummy_create (MAILSTREAM *stream,char *mailbox)
356 {
357   char tmp[MAILTMPLEN];
358   return (compare_cstring (mailbox,"INBOX") && mailboxfile (tmp,mailbox)) ?
359     dummy_create_path (stream,tmp,NIL) : dummy_badname (tmp,mailbox);
360 }
361 
362 
363 /* Dummy create path
364  * Accepts: mail stream
365  *	    path name to create
366  *	    directory mode
367  * Returns: T on success, NIL on failure
368  */
369 
dummy_create_path(MAILSTREAM * stream,char * path,long dirmode)370 long dummy_create_path (MAILSTREAM *stream,char *path,long dirmode)
371 {
372   struct stat sbuf;
373   char c,*s,tmp[MAILTMPLEN];
374   int fd;
375   long ret = NIL;
376   char *t = strrchr (path,'\\');
377   char *pt = (path[1] == ':') ? path + 2 : path;
378   int wantdir = t && !t[1];
379   if (wantdir) *t = '\0';	/* flush trailing delimiter for directory */
380 				/* found superior to this name? */
381   if ((s = strrchr (pt,'\\')) && (s != pt)) {
382     strncpy (tmp,path,(size_t) (s - path));
383     tmp[s - path] = '\0';	/* make directory name for stat */
384     c = *++s;			/* tie off in case need to recurse */
385     *s = '\0';
386 				/* name doesn't exist, create it */
387     if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
388 	!dummy_create_path (stream,path,dirmode)) return NIL;
389     *s = c;			/* restore full name */
390   }
391   if (wantdir) {		/* want to create directory? */
392     ret = !mkdir (path);
393     *t = '\\';			/* restore directory delimiter */
394   }
395 				/* create file */
396   else if ((fd = open (path,O_WRONLY|O_CREAT|O_EXCL,S_IREAD|S_IWRITE)) >= 0)
397     ret = !close (fd);		/* close file */
398   if (!ret) {			/* error? */
399     sprintf (tmp,"Can't create mailbox node %s: %s",path,strerror (errno));
400     mm_log (tmp,ERROR);
401   }
402   return ret;			/* return status */
403 }
404 
405 /* Dummy delete mailbox
406  * Accepts: mail stream
407  *	    mailbox name to delete
408  * Returns: T on success, NIL on failure
409  */
410 
dummy_delete(MAILSTREAM * stream,char * mailbox)411 long dummy_delete (MAILSTREAM *stream,char *mailbox)
412 {
413   struct stat sbuf;
414   char *s,tmp[MAILTMPLEN];
415   if (!mailboxfile (tmp,mailbox)) return dummy_badname (tmp,mailbox);
416 				/* no trailing \ */
417   if ((s = strrchr (tmp,'\\')) && !s[1]) *s = '\0';
418   if (stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) == S_IFDIR) ?
419       rmdir (tmp) : unlink (tmp)) {
420     sprintf (tmp,"Can't delete mailbox %s: %s",mailbox,strerror (errno));
421     mm_log (tmp,ERROR);
422     return NIL;
423   }
424   return T;			/* return success */
425 }
426 
427 
428 /* Mail rename mailbox
429  * Accepts: mail stream
430  *	    old mailbox name
431  *	    new mailbox name
432  * Returns: T on success, NIL on failure
433  */
434 
dummy_rename(MAILSTREAM * stream,char * old,char * newname)435 long dummy_rename (MAILSTREAM *stream,char *old,char *newname)
436 {
437   struct stat sbuf;
438   char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN];
439 				/* make file name */
440   if (!mailboxfile (file,old)) return dummy_badname (tmp,old);
441 				/* no trailing \ allowed */
442   if (!(s = mailboxfile (tmp,newname)) || ((s = strrchr (s,'\\')) && !s[1]))
443     return dummy_badname (tmp,newname);
444   if (s) {			/* found superior to destination name? */
445     c = *++s;			/* remember first character of inferior */
446     *s = '\0';			/* tie off to get just superior */
447 				/* name doesn't exist, create it */
448     if ((stat (file,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
449 	!dummy_create (stream,file)) return NIL;
450     *s = c;			/* restore full name */
451   }
452   if (rename (file,tmp)) {
453     sprintf (tmp,"Can't rename mailbox %s to %s: %s",old,newname,
454 	     strerror (errno));
455     mm_log (tmp,ERROR);
456     return NIL;
457   }
458   return LONGT;			/* return success */
459 }
460 
461 /* Dummy open
462  * Accepts: stream to open
463  * Returns: stream on success, NIL on failure
464  */
465 
dummy_open(MAILSTREAM * stream)466 MAILSTREAM *dummy_open (MAILSTREAM *stream)
467 {
468   char tmp[MAILTMPLEN];
469   struct stat sbuf;
470   int fd = -1;
471 				/* OP_PROTOTYPE call or silence */
472   if (!stream || stream->silent) return NIL;
473   if (!mailboxfile (tmp,stream->mailbox))
474     sprintf (tmp,"Can't open this name: %.80s",stream->mailbox);
475   else if (compare_cstring (stream->mailbox,"INBOX") &&
476       ((fd = open (tmp,O_RDONLY,NIL)) < 0))
477     sprintf (tmp,"%s: %s",strerror (errno),stream->mailbox);
478   else {
479     if (fd >= 0) {		/* if got a file */
480       fstat (fd,&sbuf);		/* sniff at its size */
481       close (fd);
482       if (sbuf.st_size) sprintf (tmp,"Not a mailbox: %s",stream->mailbox);
483       else fd = -1;		/* a-OK */
484     }
485     if (fd < 0) {		/* no file, right? */
486       if (!stream->silent) {	/* only if silence not requested */
487 				/* say there are 0 messages */
488 	mail_exists (stream,(long) 0);
489 	mail_recent (stream,(long) 0);
490 	stream->uid_validity = time (0);
491       }
492       stream->inbox = T;	/* note that it's an INBOX */
493       return stream;		/* return success */
494     }
495   }
496   mm_log (tmp,stream->silent ? WARN: ERROR);
497   return NIL;			/* always fails */
498 }
499 
500 
501 /* Dummy close
502  * Accepts: MAIL stream
503  *	    options
504  */
505 
dummy_close(MAILSTREAM * stream,long options)506 void dummy_close (MAILSTREAM *stream,long options)
507 {
508 				/* return silently */
509 }
510 
511 /* Dummy ping mailbox
512  * Accepts: MAIL stream
513  * Returns: T if stream alive, else NIL
514  * No-op for readonly files, since read/writer can expunge it from under us!
515  */
516 
dummy_ping(MAILSTREAM * stream)517 long dummy_ping (MAILSTREAM *stream)
518 {
519   MAILSTREAM *test;
520 				/* time to do another test? */
521   if (time (0) >= ((time_t) (stream->gensym + 30))) {
522 				/* has mailbox format changed? */
523     if ((test = mail_open (NIL,stream->mailbox,OP_PROTOTYPE)) &&
524 	(test->dtb != stream->dtb) &&
525 	(test = mail_open (NIL,stream->mailbox,NIL))) {
526 				/* preserve some resources */
527       test->original_mailbox = stream->original_mailbox;
528       stream->original_mailbox = NIL;
529       test->sparep = stream->sparep;
530       stream->sparep = NIL;
531       test->sequence = stream->sequence;
532       mail_close ((MAILSTREAM *) /* flush resources used by dummy stream */
533 		  memcpy (fs_get (sizeof (MAILSTREAM)),stream,
534 			  sizeof (MAILSTREAM)));
535 				/* swap the streams */
536       memcpy (stream,test,sizeof (MAILSTREAM));
537       fs_give ((void **) &test);/* flush test now that copied */
538 				/* make sure application knows */
539       mail_exists (stream,stream->recent = stream->nmsgs);
540     }
541 				/* still hasn't changed */
542     else stream->gensym = time (0);
543   }
544   return T;
545 }
546 
547 
548 /* Dummy check mailbox
549  * Accepts: MAIL stream
550  * No-op for readonly files, since read/writer can expunge it from under us!
551  */
552 
dummy_check(MAILSTREAM * stream)553 void dummy_check (MAILSTREAM *stream)
554 {
555   dummy_ping (stream);		/* invoke ping */
556 }
557 
558 
559 /* Dummy expunge mailbox
560  * Accepts: MAIL stream
561  *	    sequence to expunge if non-NIL
562  *	    expunge options
563  * Returns: T, always
564  */
565 
dummy_expunge(MAILSTREAM * stream,char * sequence,long options)566 long dummy_expunge (MAILSTREAM *stream,char *sequence,long options)
567 {
568   return LONGT;
569 }
570 
571 /* Dummy copy message(s)
572  * Accepts: MAIL stream
573  *	    sequence
574  *	    destination mailbox
575  *	    options
576  * Returns: T if copy successful, else NIL
577  */
578 
dummy_copy(MAILSTREAM * stream,char * sequence,char * mailbox,long options)579 long dummy_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
580 {
581   if ((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
582       mail_sequence (stream,sequence)) fatal ("Impossible dummy_copy");
583   return NIL;
584 }
585 
586 
587 /* Dummy append message string
588  * Accepts: mail stream
589  *	    destination mailbox
590  *	    append callback function
591  *	    data for callback
592  * Returns: T on success, NIL on failure
593  */
594 
dummy_append(MAILSTREAM * stream,char * mailbox,append_t af,void * data)595 long dummy_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
596 {
597   struct stat sbuf;
598   int fd = -1;
599   int e;
600   char tmp[MAILTMPLEN];
601   MAILSTREAM *ts = default_proto (T);
602   if (compare_cstring (mailbox,"INBOX") && mailboxfile (tmp,mailbox) &&
603       ((fd = open (tmp,O_RDONLY,NIL)) < 0)) {
604     if ((e = errno) == ENOENT)	/* failed, was it no such file? */
605       mm_notify (stream,"[TRYCREATE] Must create mailbox before append",
606 		 (long) NIL);
607     sprintf (tmp,"%s: %s",strerror (e),mailbox);
608     mm_log (tmp,ERROR);		/* pass up error */
609     return NIL;			/* always fails */
610   }
611   if (fd >= 0) {		/* found file? */
612     fstat (fd,&sbuf);		/* get its size */
613     close (fd);			/* toss out the fd */
614     if (sbuf.st_size) ts = NIL;	/* non-empty file? */
615   }
616   if (ts) return (*ts->dtb->append) (stream,mailbox,af,data);
617   sprintf (tmp,"Indeterminate mailbox format: %s",mailbox);
618   mm_log (tmp,ERROR);
619   return NIL;
620 }
621 
622 /* Return bad file name error message
623  * Accepts: temporary buffer
624  *	    file name
625  * Returns: long NIL always
626  */
627 
dummy_badname(char * tmp,char * s)628 long dummy_badname (char *tmp,char *s)
629 {
630   sprintf (tmp,"Invalid mailbox name: %s",s);
631   mm_log (tmp,ERROR);
632   return (long) NIL;
633 }
634 
635 
636 /* Dummy canonicalize name
637  * Accepts: buffer to write name
638  *	    reference
639  *	    pattern
640  * Returns: T if success, NIL if failure
641  */
642 
dummy_canonicalize(char * tmp,char * ref,char * pat)643 long dummy_canonicalize (char *tmp,char *ref,char *pat)
644 {
645   unsigned long i;
646   char *s,dev[4];
647 				/* initially no device */
648   dev[0] = dev[1] = dev[2] = dev[3] = '\0';
649   if (ref) switch (*ref) {	/* preliminary reference check */
650   case '{':			/* remote names not allowed */
651     return NIL;			/* disallowed */
652   case '\0':			/* empty reference string */
653     break;
654   default:			/* all other names */
655     if (ref[1] == ':') {	/* start with device name? */
656       dev[0] = *ref++; dev[1] = *ref++;
657     }
658     break;
659   }
660   if (pat[1] == ':') {		/* device name in pattern? */
661     dev[0] = *pat++; dev[1] = *pat++;
662     ref = NIL;			/* ignore reference */
663   }
664   switch (*pat) {
665   case '#':			/* namespace names */
666     if (mailboxfile (tmp,pat)) strcpy (tmp,pat);
667     else return NIL;		/* unknown namespace */
668     break;
669   case '{':			/* remote names not allowed */
670     return NIL;
671   case '\\':			/* rooted name */
672     ref = NIL;			/* ignore reference */
673     break;
674   }
675 				/* make sure device names are rooted */
676   if (dev[0] && (*(ref ? ref : pat) != '\\')) dev[2] = '\\';
677 				/* build name */
678   sprintf (tmp,"%s%s%s",dev,ref ? ref : "",pat);
679   ucase (tmp);			/* force upper case */
680 				/* count wildcards */
681   for (i = 0, s = tmp; *s; *s++) if ((*s == '*') || (*s == '%')) ++i;
682   if (i > MAXWILDCARDS) {	/* ridiculous wildcarding? */
683     MM_LOG ("Excessive wildcards in LIST/LSUB",ERROR);
684     return NIL;
685   }
686   return T;
687 }
688