imapext-2007

view src/osdep/unix/dummy.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line source
1 /* ========================================================================
2 * Copyright 1988-2007 University of Washington
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: Dummy 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: 9 May 1991
26 * Last Edited: 1 June 2007
27 */
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <errno.h>
33 extern int errno; /* just in case */
34 #include "mail.h"
35 #include "osdep.h"
36 #include <pwd.h>
37 #include <sys/stat.h>
38 #include "dummy.h"
39 #include "misc.h"
41 /* Function prototypes */
43 DRIVER *dummy_valid (char *name);
44 void *dummy_parameters (long function,void *value);
45 void dummy_list_work (MAILSTREAM *stream,char *dir,char *pat,char *contents,
46 long level);
47 long dummy_listed (MAILSTREAM *stream,char delimiter,char *name,
48 long attributes,char *contents);
49 long dummy_subscribe (MAILSTREAM *stream,char *mailbox);
50 MAILSTREAM *dummy_open (MAILSTREAM *stream);
51 void dummy_close (MAILSTREAM *stream,long options);
52 long dummy_ping (MAILSTREAM *stream);
53 void dummy_check (MAILSTREAM *stream);
54 long dummy_expunge (MAILSTREAM *stream,char *sequence,long options);
55 long dummy_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
56 long dummy_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
58 /* Dummy routines */
61 /* Driver dispatch used by MAIL */
63 DRIVER dummydriver = {
64 "dummy", /* driver name */
65 DR_LOCAL|DR_MAIL, /* driver flags */
66 (DRIVER *) NIL, /* next driver */
67 dummy_valid, /* mailbox is valid for us */
68 dummy_parameters, /* manipulate parameters */
69 dummy_scan, /* scan mailboxes */
70 dummy_list, /* list mailboxes */
71 dummy_lsub, /* list subscribed mailboxes */
72 dummy_subscribe, /* subscribe to mailbox */
73 NIL, /* unsubscribe from mailbox */
74 dummy_create, /* create mailbox */
75 dummy_delete, /* delete mailbox */
76 dummy_rename, /* rename mailbox */
77 mail_status_default, /* status of mailbox */
78 dummy_open, /* open mailbox */
79 dummy_close, /* close mailbox */
80 NIL, /* fetch message "fast" attributes */
81 NIL, /* fetch message flags */
82 NIL, /* fetch overview */
83 NIL, /* fetch message structure */
84 NIL, /* fetch header */
85 NIL, /* fetch text */
86 NIL, /* fetch message data */
87 NIL, /* unique identifier */
88 NIL, /* message number from UID */
89 NIL, /* modify flags */
90 NIL, /* per-message modify flags */
91 NIL, /* search for message based on criteria */
92 NIL, /* sort messages */
93 NIL, /* thread messages */
94 dummy_ping, /* ping mailbox to see if still alive */
95 dummy_check, /* check for new messages */
96 dummy_expunge, /* expunge deleted messages */
97 dummy_copy, /* copy messages to another mailbox */
98 dummy_append, /* append string message to mailbox */
99 NIL /* garbage collect stream */
100 };
102 /* prototype stream */
103 MAILSTREAM dummyproto = {&dummydriver};
105 /* Dummy validate mailbox
106 * Accepts: mailbox name
107 * Returns: our driver if name is valid, NIL otherwise
108 */
110 DRIVER *dummy_valid (char *name)
111 {
112 char *s,tmp[MAILTMPLEN];
113 struct stat sbuf;
114 /* must be valid local mailbox */
115 if (name && *name && (*name != '{') && (s = mailboxfile (tmp,name))) {
116 /* indeterminate clearbox INBOX */
117 if (!*s) return &dummydriver;
118 else if (!stat (s,&sbuf)) switch (sbuf.st_mode & S_IFMT) {
119 case S_IFREG:
120 case S_IFDIR:
121 return &dummydriver;
122 }
123 /* blackbox INBOX does not exist yet */
124 else if (!compare_cstring (name,"INBOX")) return &dummydriver;
125 }
126 return NIL;
127 }
130 /* Dummy manipulate driver parameters
131 * Accepts: function code
132 * function-dependent value
133 * Returns: function-dependent return value
134 */
136 void *dummy_parameters (long function,void *value)
137 {
138 void *ret = NIL;
139 switch ((int) function) {
140 case GET_INBOXPATH:
141 if (value) ret = dummy_file ((char *) value,"INBOX");
142 break;
143 }
144 return ret;
145 }
147 /* Dummy scan mailboxes
148 * Accepts: mail stream
149 * reference
150 * pattern to search
151 * string to scan
152 */
154 void dummy_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
155 {
156 DRIVER *drivers;
157 char *s,test[MAILTMPLEN],file[MAILTMPLEN];
158 long i;
159 if (!pat || !*pat) { /* empty pattern? */
160 if (dummy_canonicalize (test,ref,"*")) {
161 /* tie off name at root */
162 if (s = strchr (test,'/')) *++s = '\0';
163 else test[0] = '\0';
164 dummy_listed (stream,'/',test,LATT_NOSELECT,NIL);
165 }
166 }
167 /* get canonical form of name */
168 else if (dummy_canonicalize (test,ref,pat)) {
169 /* found any wildcards? */
170 if (s = strpbrk (test,"%*")) {
171 /* yes, copy name up to that point */
172 strncpy (file,test,i = s - test);
173 file[i] = '\0'; /* tie off */
174 }
175 else strcpy (file,test); /* use just that name then */
176 if (s = strrchr (file,'/')){/* find directory name */
177 *++s = '\0'; /* found, tie off at that point */
178 s = file;
179 }
180 /* silly case */
181 else if ((file[0] == '~') || (file[0] == '#')) s = file;
182 /* do the work */
183 dummy_list_work (stream,s,test,contents,0);
184 /* always an INBOX */
185 if (pmatch ("INBOX",ucase (test))) {
186 /* done if have a dirfmt INBOX */
187 for (drivers = (DRIVER *) mail_parameters (NIL,GET_DRIVERS,NIL);
188 drivers && !(!(drivers->flags & DR_DISABLE) &&
189 (drivers->flags & DR_DIRFMT) &&
190 (*drivers->valid) ("INBOX")); drivers = drivers->next);
191 /* list INBOX appropriately */
192 dummy_listed (stream,drivers ? '/' : NIL,"INBOX",
193 drivers ? NIL : LATT_NOINFERIORS,contents);
194 }
195 }
196 }
199 /* Dummy list mailboxes
200 * Accepts: mail stream
201 * reference
202 * pattern to search
203 */
205 void dummy_list (MAILSTREAM *stream,char *ref,char *pat)
206 {
207 dummy_scan (stream,ref,pat,NIL);
208 }
210 /* Dummy list subscribed mailboxes
211 * Accepts: mail stream
212 * reference
213 * pattern to search
214 */
216 void dummy_lsub (MAILSTREAM *stream,char *ref,char *pat)
217 {
218 void *sdb = NIL;
219 char *s,*t,test[MAILTMPLEN],tmp[MAILTMPLEN];
220 int showuppers = pat[strlen (pat) - 1] == '%';
221 /* get canonical form of name */
222 if (dummy_canonicalize (test,ref,pat) && (s = sm_read (&sdb))) do
223 if (*s != '{') {
224 if (!compare_cstring (s,"INBOX") &&
225 pmatch ("INBOX",ucase (strcpy (tmp,test))))
226 mm_lsub (stream,NIL,s,LATT_NOINFERIORS);
227 else if (pmatch_full (s,test,'/')) mm_lsub (stream,'/',s,NIL);
228 else while (showuppers && (t = strrchr (s,'/'))) {
229 *t = '\0'; /* tie off the name */
230 if (pmatch_full (s,test,'/')) mm_lsub (stream,'/',s,LATT_NOSELECT);
231 }
232 }
233 while (s = sm_read (&sdb)); /* until no more subscriptions */
234 }
237 /* Dummy subscribe to mailbox
238 * Accepts: mail stream
239 * mailbox to add to subscription list
240 * Returns: T on success, NIL on failure
241 */
243 long dummy_subscribe (MAILSTREAM *stream,char *mailbox)
244 {
245 char *s,tmp[MAILTMPLEN];
246 struct stat sbuf;
247 /* must be valid local mailbox */
248 if ((s = mailboxfile (tmp,mailbox)) && *s && !stat (s,&sbuf))
249 switch (sbuf.st_mode & S_IFMT) {
250 case S_IFDIR: /* allow but snarl */
251 sprintf (tmp,"CLIENT BUG DETECTED: subscribe of non-mailbox directory %.80s",
252 mailbox);
253 MM_NOTIFY (stream,tmp,WARN);
254 case S_IFREG:
255 return sm_subscribe (mailbox);
256 }
257 sprintf (tmp,"Can't subscribe %.80s: not a mailbox",mailbox);
258 MM_LOG (tmp,ERROR);
259 return NIL;
260 }
262 /* Dummy list mailboxes worker routine
263 * Accepts: mail stream
264 * directory name to search
265 * search pattern
266 * string to scan
267 * search level
268 */
270 void dummy_list_work (MAILSTREAM *stream,char *dir,char *pat,char *contents,
271 long level)
272 {
273 DRIVER *drivers;
274 dirfmttest_t dt;
275 DIR *dp;
276 struct direct *d;
277 struct stat sbuf;
278 char tmp[MAILTMPLEN],path[MAILTMPLEN];
279 size_t len = 0;
280 /* punt if bogus name */
281 if (!mailboxdir (tmp,dir,NIL)) return;
282 if (dp = opendir (tmp)) { /* do nothing if can't open directory */
283 /* see if a non-namespace directory format */
284 for (drivers = (DRIVER *) mail_parameters (NIL,GET_DRIVERS,NIL), dt = NIL;
285 dir && !dt && drivers; drivers = drivers->next)
286 if (!(drivers->flags & DR_DISABLE) && (drivers->flags & DR_DIRFMT) &&
287 (*drivers->valid) (dir))
288 dt = mail_parameters ((*drivers->open) (NIL),GET_DIRFMTTEST,NIL);
289 /* list it if at top-level */
290 if (!level && dir && pmatch_full (dir,pat,'/') && !pmatch (dir,"INBOX"))
291 dummy_listed (stream,'/',dir,dt ? NIL : LATT_NOSELECT,contents);
293 /* scan directory, ignore . and .. */
294 if (!dir || dir[(len = strlen (dir)) - 1] == '/') while (d = readdir (dp))
295 if ((!(dt && (*dt) (d->d_name))) &&
296 ((d->d_name[0] != '.') ||
297 (((long) mail_parameters (NIL,GET_HIDEDOTFILES,NIL)) ? NIL :
298 (d->d_name[1] && (((d->d_name[1] != '.') || d->d_name[2]))))) &&
299 ((len + strlen (d->d_name)) <= NETMAXMBX)) {
300 /* see if name is useful */
301 if (dir) sprintf (tmp,"%s%s",dir,d->d_name);
302 else strcpy (tmp,d->d_name);
303 /* make sure useful and can get info */
304 if ((pmatch_full (strcpy (path,tmp),pat,'/') ||
305 pmatch_full (strcat (path,"/"),pat,'/') ||
306 dmatch (path,pat,'/')) &&
307 mailboxdir (path,dir,"x") && (len = strlen (path)) &&
308 strcpy (path+len-1,d->d_name) && !stat (path,&sbuf)) {
309 /* only interested in file type */
310 switch (sbuf.st_mode & S_IFMT) {
311 case S_IFDIR: /* directory? */
312 /* form with trailing / */
313 sprintf (path,"%s/",tmp);
314 /* skip listing if INBOX */
315 if (!pmatch (tmp,"INBOX")) {
316 if (pmatch_full (tmp,pat,'/')) {
317 if (!dummy_listed (stream,'/',tmp,LATT_NOSELECT,contents))
318 break;
319 }
320 /* try again with trailing / */
321 else if (pmatch_full (path,pat,'/') &&
322 !dummy_listed (stream,'/',path,LATT_NOSELECT,contents))
323 break;
324 }
325 if (dmatch (path,pat,'/') &&
326 (level < (long) mail_parameters (NIL,GET_LISTMAXLEVEL,NIL)))
327 dummy_list_work (stream,path,pat,contents,level+1);
328 break;
329 case S_IFREG: /* ordinary name */
330 /* Must use ctime for systems that don't update mtime properly */
331 if (pmatch_full (tmp,pat,'/') && compare_cstring (tmp,"INBOX"))
332 dummy_listed (stream,'/',tmp,LATT_NOINFERIORS +
333 ((sbuf.st_size && (sbuf.st_atime < sbuf.st_ctime))?
334 LATT_MARKED : LATT_UNMARKED),contents);
335 break;
336 }
337 }
338 }
339 closedir (dp); /* all done, flush directory */
340 }
341 }
343 /* Scan file for contents
344 * Accepts: driver to use
345 * file name
346 * desired contents
347 * length of contents
348 * size of file
349 * Returns: NIL if contents not found, T if found
350 */
352 long scan_contents (DRIVER *dtb,char *name,char *contents,
353 unsigned long csiz,unsigned long fsiz)
354 {
355 scancontents_t sc = dtb ?
356 (scancontents_t) (*dtb->parameters) (GET_SCANCONTENTS,NIL) : NIL;
357 return (*(sc ? sc : dummy_scan_contents)) (name,contents,csiz,fsiz);
358 }
361 /* Scan file for contents
362 * Accepts: file name
363 * desired contents
364 * length of contents
365 * size of file
366 * Returns: NIL if contents not found, T if found
367 */
369 #define BUFSIZE 4*MAILTMPLEN
371 long dummy_scan_contents (char *name,char *contents,unsigned long csiz,
372 unsigned long fsiz)
373 {
374 int fd;
375 unsigned long ssiz,bsiz;
376 char *buf;
377 /* forget it if can't select or open */
378 if ((fd = open (name,O_RDONLY,NIL)) >= 0) {
379 /* get buffer including slop */
380 buf = (char *) fs_get (BUFSIZE + (ssiz = 4 * ((csiz / 4) + 1)) + 1);
381 memset (buf,'\0',ssiz); /* no slop area the first time */
382 while (fsiz) { /* until end of file */
383 read (fd,buf+ssiz,bsiz = min (fsiz,BUFSIZE));
384 if (search ((unsigned char *) buf,bsiz+ssiz,
385 (unsigned char *) contents,csiz)) break;
386 memcpy (buf,buf+BUFSIZE,ssiz);
387 fsiz -= bsiz; /* note that we read that much */
388 }
389 fs_give ((void **) &buf); /* flush buffer */
390 close (fd); /* finished with file */
391 if (fsiz) return T; /* found */
392 }
393 return NIL; /* not found */
394 }
396 /* Mailbox found
397 * Accepts: MAIL stream
398 * hierarchy delimiter
399 * mailbox name
400 * attributes
401 * contents to search before calling mm_list()
402 * Returns: NIL if should abort hierarchy search, else T (currently always)
403 */
405 long dummy_listed (MAILSTREAM *stream,char delimiter,char *name,
406 long attributes,char *contents)
407 {
408 DRIVER *d;
409 DIR *dp;
410 struct direct *dr;
411 dirfmttest_t dt;
412 unsigned long csiz;
413 struct stat sbuf;
414 int nochild;
415 char *s,tmp[MAILTMPLEN];
416 if (!(attributes & LATT_NOINFERIORS) && mailboxdir (tmp,name,NIL) &&
417 (dp = opendir (tmp))) { /* if not \NoInferiors */
418 /* locate dirfmttest if any */
419 for (d = (DRIVER *) mail_parameters (NIL,GET_DRIVERS,NIL), dt = NIL;
420 !dt && d; d = d->next)
421 if (!(d->flags & DR_DISABLE) && (d->flags & DR_DIRFMT) &&
422 (*d->valid) (name))
423 dt = mail_parameters ((*d->open) (NIL),GET_DIRFMTTEST,NIL);
424 /* scan directory for children */
425 for (nochild = T; nochild && (dr = readdir (dp)); )
426 if ((!(dt && (*dt) (dr->d_name))) &&
427 ((dr->d_name[0] != '.') ||
428 (((long) mail_parameters (NIL,GET_HIDEDOTFILES,NIL)) ? NIL :
429 (dr->d_name[1] && ((dr->d_name[1] != '.') || dr->d_name[2])))))
430 nochild = NIL;
431 attributes |= nochild ? LATT_HASNOCHILDREN : LATT_HASCHILDREN;
432 closedir (dp); /* all done, flush directory */
433 }
434 d = NIL; /* don't \NoSelect dir if it has a driver */
435 if ((attributes & LATT_NOSELECT) && (d = mail_valid (NIL,name,NIL)) &&
436 (d != &dummydriver)) attributes &= ~LATT_NOSELECT;
437 if (!contents || /* notify main program */
438 (!(attributes & LATT_NOSELECT) && (csiz = strlen (contents)) &&
439 (s = mailboxfile (tmp,name)) &&
440 (*s || (s = mail_parameters (NIL,GET_INBOXPATH,tmp))) &&
441 !stat (s,&sbuf) && (d || (csiz <= sbuf.st_size)) &&
442 SAFE_SCAN_CONTENTS (d,tmp,contents,csiz,sbuf.st_size)))
443 mm_list (stream,delimiter,name,attributes);
444 return T;
445 }
447 /* Dummy create mailbox
448 * Accepts: mail stream
449 * mailbox name to create
450 * Returns: T on success, NIL on failure
451 */
453 long dummy_create (MAILSTREAM *stream,char *mailbox)
454 {
455 char *s,tmp[MAILTMPLEN];
456 long ret = NIL;
457 /* validate name */
458 if (!(compare_cstring (mailbox,"INBOX") && (s = dummy_file (tmp,mailbox)))) {
459 sprintf (tmp,"Can't create %.80s: invalid name",mailbox);
460 MM_LOG (tmp,ERROR);
461 }
462 /* create the name, done if made directory */
463 else if ((ret = dummy_create_path (stream,tmp,get_dir_protection(mailbox)))&&
464 (s = strrchr (s,'/')) && !s[1]) return T;
465 return ret ? set_mbx_protections (mailbox,tmp) : NIL;
466 }
468 /* Dummy create path
469 * Accepts: mail stream
470 * path name to create
471 * directory mode
472 * Returns: T on success, NIL on failure
473 */
475 long dummy_create_path (MAILSTREAM *stream,char *path,long dirmode)
476 {
477 struct stat sbuf;
478 char c,*s,tmp[MAILTMPLEN];
479 int fd;
480 long ret = NIL;
481 char *t = strrchr (path,'/');
482 int wantdir = t && !t[1];
483 int mask = umask (0);
484 if (wantdir) *t = '\0'; /* flush trailing delimiter for directory */
485 if (s = strrchr (path,'/')) { /* found superior to this name? */
486 c = *++s; /* remember first character of inferior */
487 *s = '\0'; /* tie off to get just superior */
488 /* name doesn't exist, create it */
489 if ((stat (path,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
490 !dummy_create_path (stream,path,dirmode)) {
491 umask (mask); /* restore mask */
492 return NIL;
493 }
494 *s = c; /* restore full name */
495 }
496 if (wantdir) { /* want to create directory? */
497 ret = !mkdir (path,(int) dirmode);
498 *t = '/'; /* restore directory delimiter */
499 }
500 /* create file */
501 else if ((fd = open (path,O_WRONLY|O_CREAT|O_EXCL,
502 (long) mail_parameters(NIL,GET_MBXPROTECTION,NIL))) >=0)
503 ret = !close (fd);
504 if (!ret) { /* error? */
505 sprintf (tmp,"Can't create mailbox node %.80s: %.80s",path,strerror (errno));
506 MM_LOG (tmp,ERROR);
507 }
508 umask (mask); /* restore mask */
509 return ret; /* return status */
510 }
512 /* Dummy delete mailbox
513 * Accepts: mail stream
514 * mailbox name to delete
515 * Returns: T on success, NIL on failure
516 */
518 long dummy_delete (MAILSTREAM *stream,char *mailbox)
519 {
520 struct stat sbuf;
521 char *s,tmp[MAILTMPLEN];
522 if (!(s = dummy_file (tmp,mailbox))) {
523 sprintf (tmp,"Can't delete - invalid name: %.80s",s);
524 MM_LOG (tmp,ERROR);
525 }
526 /* no trailing / (workaround BSD kernel bug) */
527 if ((s = strrchr (tmp,'/')) && !s[1]) *s = '\0';
528 if (stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) == S_IFDIR) ?
529 rmdir (tmp) : unlink (tmp)) {
530 sprintf (tmp,"Can't delete mailbox %.80s: %.80s",mailbox,strerror (errno));
531 MM_LOG (tmp,ERROR);
532 return NIL;
533 }
534 return T; /* return success */
535 }
537 /* Mail rename mailbox
538 * Accepts: mail stream
539 * old mailbox name
540 * new mailbox name
541 * Returns: T on success, NIL on failure
542 */
544 long dummy_rename (MAILSTREAM *stream,char *old,char *newname)
545 {
546 struct stat sbuf;
547 char c,*s,tmp[MAILTMPLEN],mbx[MAILTMPLEN],oldname[MAILTMPLEN];
548 /* no trailing / allowed */
549 if (!dummy_file (oldname,old) || !(s = dummy_file (mbx,newname)) ||
550 stat (oldname,&sbuf) || ((s = strrchr (s,'/')) && !s[1] &&
551 ((sbuf.st_mode & S_IFMT) != S_IFDIR))) {
552 sprintf (mbx,"Can't rename %.80s to %.80s: invalid name",old,newname);
553 MM_LOG (mbx,ERROR);
554 return NIL;
555 }
556 if (s) { /* found a directory delimiter? */
557 if (!s[1]) *s = '\0'; /* ignore trailing delimiter */
558 else { /* found superior to destination name? */
559 c = *++s; /* remember first character of inferior */
560 *s = '\0'; /* tie off to get just superior */
561 /* name doesn't exist, create it */
562 if ((stat (mbx,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
563 !dummy_create (stream,mbx)) return NIL;
564 *s = c; /* restore full name */
565 }
566 }
567 /* rename of non-ex INBOX creates dest */
568 if (!compare_cstring (old,"INBOX") && stat (oldname,&sbuf))
569 return dummy_create (NIL,mbx);
570 if (rename (oldname,mbx)) {
571 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %.80s",old,newname,
572 strerror (errno));
573 MM_LOG (tmp,ERROR);
574 return NIL;
575 }
576 return T; /* return success */
577 }
579 /* Dummy open
580 * Accepts: stream to open
581 * Returns: stream on success, NIL on failure
582 */
584 MAILSTREAM *dummy_open (MAILSTREAM *stream)
585 {
586 int fd;
587 char err[MAILTMPLEN],tmp[MAILTMPLEN];
588 struct stat sbuf;
589 /* OP_PROTOTYPE call */
590 if (!stream) return &dummyproto;
591 err[0] = '\0'; /* no error message yet */
592 /* can we open the file? */
593 if (!dummy_file (tmp,stream->mailbox))
594 sprintf (err,"Can't open this name: %.80s",stream->mailbox);
595 else if ((fd = open (tmp,O_RDONLY,NIL)) < 0) {
596 /* no, error unless INBOX */
597 if (compare_cstring (stream->mailbox,"INBOX"))
598 sprintf (err,"%.80s: %.80s",strerror (errno),stream->mailbox);
599 }
600 else { /* file had better be empty then */
601 fstat (fd,&sbuf); /* sniff at its size */
602 close (fd);
603 if ((sbuf.st_mode & S_IFMT) != S_IFREG)
604 sprintf (err,"Can't open %.80s: not a selectable mailbox",
605 stream->mailbox);
606 else if (sbuf.st_size) /* bogus format if non-empty */
607 sprintf (err,"Can't open %.80s (file %.80s): not in valid mailbox format",
608 stream->mailbox,tmp);
609 }
610 if (err[0]) { /* if an error happened */
611 MM_LOG (err,stream->silent ? WARN : ERROR);
612 return NIL;
613 }
614 else if (!stream->silent) { /* only if silence not requested */
615 mail_exists (stream,0); /* say there are 0 messages */
616 mail_recent (stream,0); /* and certainly no recent ones! */
617 stream->uid_validity = time (0);
618 }
619 stream->inbox = T; /* note that it's an INBOX */
620 return stream; /* return success */
621 }
624 /* Dummy close
625 * Accepts: MAIL stream
626 * options
627 */
629 void dummy_close (MAILSTREAM *stream,long options)
630 {
631 /* return silently */
632 }
634 /* Dummy ping mailbox
635 * Accepts: MAIL stream
636 * Returns: T if stream alive, else NIL
637 */
639 long dummy_ping (MAILSTREAM *stream)
640 {
641 MAILSTREAM *test;
642 if (time (0) >= /* time to do another test? */
643 ((time_t) (stream->gensym +
644 (long) mail_parameters (NIL,GET_SNARFINTERVAL,NIL)))) {
645 /* has mailbox format changed? */
646 if ((test = mail_open (NIL,stream->mailbox,OP_PROTOTYPE)) &&
647 (test->dtb != stream->dtb) &&
648 (test = mail_open (NIL,stream->mailbox,NIL))) {
649 /* preserve some resources */
650 test->original_mailbox = stream->original_mailbox;
651 stream->original_mailbox = NIL;
652 test->sparep = stream->sparep;
653 stream->sparep = NIL;
654 test->sequence = stream->sequence;
655 mail_close ((MAILSTREAM *) /* flush resources used by dummy stream */
656 memcpy (fs_get (sizeof (MAILSTREAM)),stream,
657 sizeof (MAILSTREAM)));
658 /* swap the streams */
659 memcpy (stream,test,sizeof (MAILSTREAM));
660 fs_give ((void **) &test);/* flush test now that copied */
661 /* make sure application knows */
662 mail_exists (stream,stream->recent = stream->nmsgs);
663 }
664 /* still hasn't changed */
665 else stream->gensym = time (0);
666 }
667 return T;
668 }
671 /* Dummy check mailbox
672 * Accepts: MAIL stream
673 * No-op for readonly files, since read/writer can expunge it from under us!
674 */
676 void dummy_check (MAILSTREAM *stream)
677 {
678 dummy_ping (stream); /* invoke ping */
679 }
682 /* Dummy expunge mailbox
683 * Accepts: MAIL stream
684 * sequence to expunge if non-NIL
685 * expunge options
686 * Returns: T, always
687 */
689 long dummy_expunge (MAILSTREAM *stream,char *sequence,long options)
690 {
691 return LONGT;
692 }
694 /* Dummy copy message(s)
695 * Accepts: MAIL stream
696 * sequence
697 * destination mailbox
698 * options
699 * Returns: T if copy successful, else NIL
700 */
702 long dummy_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
703 {
704 if ((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
705 mail_sequence (stream,sequence)) fatal ("Impossible dummy_copy");
706 return NIL;
707 }
710 /* Dummy append message string
711 * Accepts: mail stream
712 * destination mailbox
713 * append callback function
714 * data for callback
715 * Returns: T on success, NIL on failure
716 */
718 long dummy_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
719 {
720 struct stat sbuf;
721 int fd = -1;
722 int e;
723 char tmp[MAILTMPLEN];
724 MAILSTREAM *ts = default_proto (T);
725 /* append to INBOX? */
726 if (!compare_cstring (mailbox,"INBOX")) {
727 /* yes, if no empty proto try creating */
728 if (!ts && !(*(ts = default_proto (NIL))->dtb->create) (ts,"INBOX"))
729 ts = NIL;
730 }
731 else if (dummy_file (tmp,mailbox) && ((fd = open (tmp,O_RDONLY,NIL)) < 0)) {
732 if ((e = errno) == ENOENT) /* failed, was it no such file? */
733 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
734 sprintf (tmp,"%.80s: %.80s",strerror (e),mailbox);
735 MM_LOG (tmp,ERROR); /* pass up error */
736 return NIL; /* always fails */
737 }
738 else if (fd >= 0) { /* found file? */
739 fstat (fd,&sbuf); /* get its size */
740 close (fd); /* toss out the fd */
741 if (sbuf.st_size) ts = NIL; /* non-empty file? */
742 }
743 if (ts) return (*ts->dtb->append) (stream,mailbox,af,data);
744 sprintf (tmp,"Indeterminate mailbox format: %.80s",mailbox);
745 MM_LOG (tmp,ERROR);
746 return NIL;
747 }
749 /* Dummy mail generate file string
750 * Accepts: temporary buffer to write into
751 * mailbox name string
752 * Returns: local file string or NIL if failure
753 */
755 char *dummy_file (char *dst,char *name)
756 {
757 char *s = mailboxfile (dst,name);
758 /* return our standard inbox */
759 return (s && !*s) ? strcpy (dst,sysinbox ()) : s;
760 }
763 /* Dummy canonicalize name
764 * Accepts: buffer to write name
765 * reference
766 * pattern
767 * Returns: T if success, NIL if failure
768 */
770 long dummy_canonicalize (char *tmp,char *ref,char *pat)
771 {
772 unsigned long i;
773 char *s;
774 if (ref) { /* preliminary reference check */
775 if (*ref == '{') return NIL;/* remote reference not allowed */
776 else if (!*ref) ref = NIL; /* treat empty reference as no reference */
777 }
778 switch (*pat) {
779 case '#': /* namespace name */
780 if (mailboxfile (tmp,pat)) strcpy (tmp,pat);
781 else return NIL; /* unknown namespace */
782 break;
783 case '{': /* remote names not allowed */
784 return NIL;
785 case '/': /* rooted name */
786 case '~': /* home directory name */
787 if (!ref || (*ref != '#')) {/* non-namespace reference? */
788 strcpy (tmp,pat); /* yes, ignore */
789 break;
790 }
791 /* fall through */
792 default: /* apply reference for all other names */
793 if (!ref) strcpy (tmp,pat); /* just copy if no namespace */
794 else if ((*ref != '#') || mailboxfile (tmp,ref)) {
795 /* wants root of name? */
796 if (*pat == '/') strcpy (strchr (strcpy (tmp,ref),'/'),pat);
797 /* otherwise just append */
798 else sprintf (tmp,"%s%s",ref,pat);
799 }
800 else return NIL; /* unknown namespace */
801 }
802 /* count wildcards */
803 for (i = 0, s = tmp; *s; *s++) if ((*s == '*') || (*s == '%')) ++i;
804 if (i > MAXWILDCARDS) { /* ridiculous wildcarding? */
805 MM_LOG ("Excessive wildcards in LIST/LSUB",ERROR);
806 return NIL;
807 }
808 return T;
809 }

UW-IMAP'd extensions by yuuji