summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ip/imap4d/mbox.c
blob: 8eb57dfde5b530c799d9558ac3d3a2f9141cc879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <auth.h>
#include "imap4d.h"

static NamedInt	flagChars[NFlags] =
{
	{"s",	MSeen},
	{"a",	MAnswered},
	{"f",	MFlagged},
	{"D",	MDeleted},
	{"d",	MDraft},
	{"r",	MRecent},
};

static	int	fsCtl = -1;

static	void	boxFlags(Box *box);
static	int	createImp(Box *box, Qid *qid);
static	void	fsInit(void);
static	void	mboxGone(Box *box);
static	MbLock	*openImp(Box *box, int new);
static	int	parseImp(Biobuf *b, Box *box);
static	int	readBox(Box *box);
static	ulong	uidRenumber(Msg *m, ulong uid, int force);
static	int	impFlags(Box *box, Msg *m, char *flags);

/*
 * strategy:
 * every mailbox file has an associated .imp file
 * which maps upas/fs message digests to uids & message flags.
 *
 * the .imp files are locked by /mail/fs/usename/L.mbox.
 * whenever the flags can be modified, the lock file
 * should be opened, thereby locking the uid & flag state.
 * for example, whenever new uids are assigned to messages,
 * and whenever flags are changed internally, the lock file
 * should be open and locked.  this means the file must be
 * opened during store command, and when changing the \seen
 * flag for the fetch command.
 *
 * if no .imp file exists, a null one must be created before
 * assigning uids.
 *
 * the .imp file has the following format
 * imp		: "imap internal mailbox description\n"
 * 			uidvalidity " " uidnext "\n"
 *			messageLines
 *
 * messageLines	:
 *		| messageLines digest " " uid " " flags "\n"
 *
 * uid, uidnext, and uidvalidity are 32 bit decimal numbers
 * printed right justified in a field NUid characters long.
 * the 0 uid implies that no uid has been assigned to the message,
 * but the flags are valid. note that message lines are in mailbox
 * order, except possibly for 0 uid messages.
 *
 * digest is an ascii hex string NDigest characters long.
 *
 * flags has a character for each of NFlag flag fields.
 * if the flag is clear, it is represented by a "-".
 * set flags are represented as a unique single ascii character.
 * the currently assigned flags are, in order:
 *	MSeen		s
 *	MAnswered	a
 *	MFlagged	f
 *	MDeleted	D
 *	MDraft		d
 */
Box*
openBox(char *name, char *fsname, int writable)
{
	Box *box;
	MbLock *ml;
	int n, new;

	if(cistrcmp(name, "inbox") == 0)
		if(access("msgs", AEXIST) == 0)
			name = "msgs";
		else
			name = "mbox";
	fsInit();
	debuglog("imap4d open %s %s\n", name, fsname);

	if(fprint(fsCtl, "open '/mail/box/%s/%s' %s", username, name, fsname) < 0){
//ZZZ
		char err[ERRMAX];

		rerrstr(err, sizeof err);
		if(strstr(err, "file does not exist") == nil)
			fprint(2,
		"imap4d at %lud: upas/fs open %s/%s as %s failed: '%s' %s",
			time(nil), username, name, fsname, err,
			ctime(time(nil)));  /* NB: ctime result ends with \n */
		fprint(fsCtl, "close %s", fsname);
		return nil;
	}

	/*
	 * read box to find all messages
	 * each one has a directory, and is in numerical order
	 */
	box = MKZ(Box);
	box->writable = writable;

	n = strlen(name) + 1;
	box->name = emalloc(n);
	strcpy(box->name, name);

	n += STRLEN(".imp");
	box->imp = emalloc(n);
	snprint(box->imp, n, "%s.imp", name);

	n = strlen(fsname) + 1;
	box->fs = emalloc(n);
	strcpy(box->fs, fsname);

	n = STRLEN("/mail/fs/") + strlen(fsname) + 1;
	box->fsDir = emalloc(n);
	snprint(box->fsDir, n, "/mail/fs/%s", fsname);

	box->uidnext = 1;
	new = readBox(box);
	if(new >= 0){
		ml = openImp(box, new);
		if(ml != nil){
			closeImp(box, ml);
			return box;
		}
	}
	closeBox(box, 0);
	return nil;
}

/*
 * check mailbox
 * returns fd of open .imp file if imped.
 * otherwise, return value is insignificant
 *
 * careful: called by idle polling proc
 */
MbLock*
checkBox(Box *box, int imped)
{
	MbLock *ml;
	Dir *d;
	int new;

	if(box == nil)
		return nil;

	/*
	 * if stat fails, mailbox must be gone
	 */
	d = cdDirstat(box->fsDir, ".");
	if(d == nil){
		mboxGone(box);
		return nil;
	}
	new = 0;
	if(box->qid.path != d->qid.path || box->qid.vers != d->qid.vers
	|| box->mtime != d->mtime){
		new = readBox(box);
		if(new < 0){
			free(d);
			return nil;
		}
	}
	free(d);
	ml = openImp(box, new);
	if(ml == nil)
		box->writable = 0;
	else if(!imped){
		closeImp(box, ml);
		ml = nil;
	}
	return ml;
}

/*
 * mailbox is unreachable, so mark all messages expunged
 * clean up .imp files as well.
 */
static void
mboxGone(Box *box)
{
	Msg *m;

	if(cdExists(mboxDir, box->name) < 0)
		cdRemove(mboxDir, box->imp);
	for(m = box->msgs; m != nil; m = m->next)
		m->expunged = 1;
	box->writable = 0;
}

/*
 * read messages in the mailbox
 * mark message that no longer exist as expunged
 * returns -1 for failure, 0 if no new messages, 1 if new messages.
 */
static int
readBox(Box *box)
{
	Msg *msgs, *m, *last;
	Dir *d;
	char *s;
	long max, id;
	int i, nd, fd, new;

	fd = cdOpen(box->fsDir, ".", OREAD);
	if(fd < 0){
		syslog(0, "mail",
		    "imap4d at %lud: upas/fs stat of %s/%s aka %s failed: %r",
			time(nil), username, box->name, box->fsDir);
		mboxGone(box);
		return -1;
	}

	/*
	 * read box to find all messages
	 * each one has a directory, and is in numerical order
	 */
	d = dirfstat(fd);
	if(d == nil){
		close(fd);
		return -1;
	}
	box->mtime = d->mtime;
	box->qid = d->qid;
	last = nil;
	msgs = box->msgs;
	max = 0;
	new = 0;
	free(d);
	while((nd = dirread(fd, &d)) > 0){
		for(i = 0; i < nd; i++){
			s = d[i].name;
			id = strtol(s, &s, 10);
			if(id <= max || *s != '\0'
			|| (d[i].mode & DMDIR) != DMDIR)
				continue;

			max = id;

			while(msgs != nil){
				last = msgs;
				msgs = msgs->next;
				if(last->id == id)
					goto continueDir;
				last->expunged = 1;
			}

			new = 1;
			m = MKZ(Msg);
			m->id = id;
			m->fsDir = box->fsDir;
			m->fs = emalloc(2 * (MsgNameLen + 1));
			m->efs = seprint(m->fs, m->fs + (MsgNameLen + 1), "%lud/", id);
			m->size = ~0UL;
			m->lines = ~0UL;
			m->prev = last;
			m->flags = MRecent;
			if(!msgInfo(m))
				freeMsg(m);
			else{
				if(last == nil)
					box->msgs = m;
				else
					last->next = m;
				last = m;
			}
	continueDir:;
		}
		free(d);
	}
	close(fd);
	for(; msgs != nil; msgs = msgs->next)
		msgs->expunged = 1;

	/*
	 * make up the imap message sequence numbers
	 */
	id = 1;
	for(m = box->msgs; m != nil; m = m->next){
		if(m->seq && m->seq != id)
			bye("internal error assigning message numbers");
		m->seq = id++;
	}
	box->max = id - 1;

	return new;
}

/*
 * read in the .imp file, or make one if it doesn't exist.
 * make sure all flags and uids are consistent.
 * return the mailbox lock.
 */
#define IMPMAGIC	"imap internal mailbox description\n"
static MbLock*
openImp(Box *box, int new)
{
	Qid qid;
	Biobuf b;
	MbLock *ml;
	int fd;
//ZZZZ
	int once;

	ml = mbLock();
	if(ml == nil)
		return nil;
	fd = cdOpen(mboxDir, box->imp, OREAD);
	once = 0;
ZZZhack:
	if(fd < 0 || fqid(fd, &qid) < 0){
		if(fd < 0){
			char buf[ERRMAX];

			errstr(buf, sizeof buf);
			if(cistrstr(buf, "does not exist") == nil)
				fprint(2, "imap4d at %lud: imp open failed: %s\n", time(nil), buf);
			if(!once && cistrstr(buf, "locked") != nil){
				once = 1;
				fprint(2, "imap4d at %lud: imp %s/%s %s locked when it shouldn't be; spinning\n", time(nil), username, box->name, box->imp);
				fd = openLocked(mboxDir, box->imp, OREAD);
				goto ZZZhack;
			}
		}
		if(fd >= 0)
			close(fd);
		fd = createImp(box, &qid);
		if(fd < 0){
			mbUnlock(ml);
			return nil;
		}
		box->dirtyImp = 1;
		if(box->uidvalidity == 0)
			box->uidvalidity = box->mtime;
		box->impQid = qid;
		new = 1;
	}else if(qid.path != box->impQid.path || qid.vers != box->impQid.vers){
		Binit(&b, fd, OREAD);
		if(!parseImp(&b, box)){
			box->dirtyImp = 1;
			if(box->uidvalidity == 0)
				box->uidvalidity = box->mtime;
		}
		Bterm(&b);
		box->impQid = qid;
		new = 1;
	}
	if(new)
		boxFlags(box);
	close(fd);
	return ml;
}

/*
 * close the .imp file, after writing out any changes
 */
void
closeImp(Box *box, MbLock *ml)
{
	Msg *m;
	Qid qid;
	Biobuf b;
	char buf[NFlags+1];
	int fd;

	if(ml == nil)
		return;
	if(!box->dirtyImp){
		mbUnlock(ml);
		return;
	}

	fd = cdCreate(mboxDir, box->imp, OWRITE, 0664);
	if(fd < 0){
		mbUnlock(ml);
		return;
	}
	Binit(&b, fd, OWRITE);

	box->dirtyImp = 0;
	Bprint(&b, "%s", IMPMAGIC);
	Bprint(&b, "%.*lud %.*lud\n", NUid, box->uidvalidity, NUid, box->uidnext);
	for(m = box->msgs; m != nil; m = m->next){
		if(m->expunged)
			continue;
		wrImpFlags(buf, m->flags, strcmp(box->fs, "imap") == 0);
		Bprint(&b, "%.*s %.*lud %s\n", NDigest, m->info[IDigest], NUid, m->uid, buf);
	}
	Bterm(&b);

	if(fqid(fd, &qid) >= 0)
		box->impQid = qid;
	close(fd);
	mbUnlock(ml);
}

void
wrImpFlags(char *buf, int flags, int killRecent)
{
	int i;

	for(i = 0; i < NFlags; i++){
		if((flags & flagChars[i].v)
		&& (flagChars[i].v != MRecent || !killRecent))
			buf[i] = flagChars[i].name[0];
		else
			buf[i] = '-';
	}
	buf[i] = '\0';
}

int
emptyImp(char *mbox)
{
	Dir *d;
	long mode;
	int fd;

	fd = cdCreate(mboxDir, impName(mbox), OWRITE, 0664);
	if(fd < 0)
		return -1;
	d = cdDirstat(mboxDir, mbox);
	if(d == nil){
		close(fd);
		return -1;
	}
	fprint(fd, "%s%.*lud %.*lud\n", IMPMAGIC, NUid, d->mtime, NUid, 1UL);
	mode = d->mode & 0777;
	nulldir(d);
	d->mode = mode;
	dirfwstat(fd, d);
	free(d);
	return fd;
}

/*
 * try to match permissions with mbox
 */
static int
createImp(Box *box, Qid *qid)
{
	Dir *d;
	long mode;
	int fd;

	fd = cdCreate(mboxDir, box->imp, OREAD, 0664);
	if(fd < 0)
		return -1;
	d = cdDirstat(mboxDir, box->name);
	if(d != nil){
		mode = d->mode & 0777;
		nulldir(d);
		d->mode = mode;
		dirfwstat(fd, d);
		free(d);
	}
	if(fqid(fd, qid) < 0){
		close(fd);
		return -1;
	}

	return fd;
}

/*
 * read or re-read a .imp file.
 * this is tricky:
 *	messages can be deleted by another agent
 *	we might still have a Msg for an expunged message,
 *		because we haven't told the client yet.
 *	we can have a Msg without a .imp entry.
 *	flag information is added at the end of the .imp by copy & append
 *	there can be duplicate messages (same digests).
 *
 * look up existing messages based on uid.
 * look up new messages based on in order digest matching.
 *
 * note: in the face of duplicate messages, one of which is deleted,
 * two active servers may decide different ones are valid, and so return
 * different uids for the messages.  this situation will stablize when the servers exit.
 */
static int
parseImp(Biobuf *b, Box *box)
{
	Msg *m, *mm;
	char *s, *t, *toks[3];
	ulong uid, u;
	int match, n;

	m = box->msgs;
	s = Brdline(b, '\n');
	if(s == nil || Blinelen(b) != STRLEN(IMPMAGIC)
	|| strncmp(s, IMPMAGIC, STRLEN(IMPMAGIC)) != 0)
		return 0;

	s = Brdline(b, '\n');
	if(s == nil || Blinelen(b) != 2*NUid + 2)
		return 0;
	s[2*NUid + 1] = '\0';
	u = strtoul(s, &t, 10);
	if(u != box->uidvalidity && box->uidvalidity != 0)
		return 0;
	box->uidvalidity = u;
	if(*t != ' ' || t != s + NUid)
		return 0;
	t++;
	u = strtoul(t, &t, 10);
	if(box->uidnext > u)
		return 0;
	box->uidnext = u;
	if(t != s + 2*NUid+1 || box->uidnext == 0)
		return 0;

	uid = ~0;
	while(m != nil){
		s = Brdline(b, '\n');
		if(s == nil)
			break;
		n = Blinelen(b) - 1;
		if(n != NDigest + NUid + NFlags + 2
		|| s[NDigest] != ' ' || s[NDigest + NUid + 1] != ' ')
			return 0;
		toks[0] = s;
		s[NDigest] = '\0';
		toks[1] = s + NDigest + 1;
		s[NDigest + NUid + 1] = '\0';
		toks[2] = s + NDigest + NUid + 2;
		s[n] = '\0';
		t = toks[1];
		u = strtoul(t, &t, 10);
		if(*t != '\0' || uid != ~0 && (uid >= u && u || u && !uid))
			return 0;
		uid = u;

		/*
		 * zero uid => added by append or copy, only flags valid
		 * can only match messages without uids, but this message
		 * may not be the next one, and may have been deleted.
		 */
		if(!uid){
			for(; m != nil && m->uid; m = m->next)
				;
			for(mm = m; mm != nil; mm = mm->next){
				if(mm->info[IDigest] != nil &&
				    strcmp(mm->info[IDigest], toks[0]) == 0){
					if(!mm->uid)
						mm->flags = 0;
					if(!impFlags(box, mm, toks[2]))
						return 0;
					m = mm->next;
					break;
				}
			}
			continue;
		}

		/*
		 * ignore expunged messages,
		 * and messages already assigned uids which don't match this uid.
		 * such messages must have been deleted by another imap server,
		 * which updated the mailbox and .imp file since we read the mailbox,
		 * or because upas/fs got confused by consecutive duplicate messages,
		 * the first of which was deleted by another imap server.
		 */
		for(; m != nil && (m->expunged || m->uid && m->uid < uid); m = m->next)
			;
		if(m == nil)
			break;

		/*
		 * only check for digest match on the next message,
		 * since it comes before all other messages, and therefore
		 * must be in the .imp file if they should be.
		 */
		match = m->info[IDigest] != nil &&
			strcmp(m->info[IDigest], toks[0]) == 0;
		if(uid && (m->uid == uid || !m->uid && match)){
			if(!match)
				bye("inconsistent uid");

			/*
			 * wipe out recent flag if some other server saw this new message.
			 * it will be read from the .imp file if is really should be set,
			 * ie the message was only seen by a status command.
			 */
			if(!m->uid)
				m->flags = 0;

			if(!impFlags(box, m, toks[2]))
				return 0;
			m->uid = uid;
			m = m->next;
		}
	}
	return 1;
}

/*
 * parse .imp flags
 */
static int
impFlags(Box *box, Msg *m, char *flags)
{
	int i, f;

	f = 0;
	for(i = 0; i < NFlags; i++){
		if(flags[i] == '-')
			continue;
		if(flags[i] != flagChars[i].name[0])
			return 0;
		f |= flagChars[i].v;
	}

	/*
	 * recent flags are set until the first time message's box is selected or examined.
	 * it may be stored in the file as a side effect of a status or subscribe command;
	 * if so, clear it out.
	 */
	if((f & MRecent) && strcmp(box->fs, "imap") == 0)
		box->dirtyImp = 1;
	f |= m->flags & MRecent;

	/*
	 * all old messages with changed flags should be reported to the client
	 */
	if(m->uid && m->flags != f){
		box->sendFlags = 1;
		m->sendFlags = 1;
	}
	m->flags = f;
	return 1;
}

/*
 * assign uids to any new messages
 * which aren't already in the .imp file.
 * sum up totals for flag values.
 */
static void
boxFlags(Box *box)
{
	Msg *m;

	box->recent = 0;
	for(m = box->msgs; m != nil; m = m->next){
		if(m->uid == 0){
			box->dirtyImp = 1;
			box->uidnext = uidRenumber(m, box->uidnext, 0);
		}
		if(m->flags & MRecent)
			box->recent++;
	}
}

static ulong
uidRenumber(Msg *m, ulong uid, int force)
{
	for(; m != nil; m = m->next){
		if(!force && m->uid != 0)
			bye("uid renumbering with a valid uid");
		m->uid = uid++;
	}
	return uid;
}

void
closeBox(Box *box, int opened)
{
	Msg *m, *next;

	/*
	 * make sure to leave the mailbox directory so upas/fs can close the mailbox
	 */
	myChdir(mboxDir);

	if(box->writable){
		deleteMsgs(box);
		if(expungeMsgs(box, 0))
			closeImp(box, checkBox(box, 1));
	}

	if(fprint(fsCtl, "close %s", box->fs) < 0 && opened)
		bye("can't talk to mail server");
	for(m = box->msgs; m != nil; m = next){
		next = m->next;
		freeMsg(m);
	}
	free(box->name);
	free(box->fs);
	free(box->fsDir);
	free(box->imp);
	free(box);
}

int
deleteMsgs(Box *box)
{
	Msg *m;
	char buf[BufSize], *p, *start;
	int ok;

	if(!box->writable)
		return 0;

	/*
	 * first pass: delete messages; gang the writes together for speed.
	 */
	ok = 1;
	start = seprint(buf, buf + sizeof(buf), "delete %s", box->fs);
	p = start;
	for(m = box->msgs; m != nil; m = m->next){
		if((m->flags & MDeleted) && !m->expunged){
			m->expunged = 1;
			p = seprint(p, buf + sizeof(buf), " %lud", m->id);
			if(p + 32 >= buf + sizeof(buf)){
				if(write(fsCtl, buf, p - buf) < 0)
					bye("can't talk to mail server");
				p = start;
			}
		}
	}
	if(p != start && write(fsCtl, buf, p - buf) < 0)
		bye("can't talk to mail server");

	return ok;
}

/*
 * second pass: remove the message structure,
 * and renumber message sequence numbers.
 * update messages counts in mailbox.
 * returns true if anything changed.
 */
int
expungeMsgs(Box *box, int send)
{
	Msg *m, *next, *last;
	ulong n;

	n = 0;
	last = nil;
	for(m = box->msgs; m != nil; m = next){
		m->seq -= n;
		next = m->next;
		if(m->expunged){
			if(send)
				Bprint(&bout, "* %lud expunge\r\n", m->seq);
			if(m->flags & MRecent)
				box->recent--;
			n++;
			if(last == nil)
				box->msgs = next;
			else
				last->next = next;
			freeMsg(m);
		}else
			last = m;
	}
	if(n){
		box->max -= n;
		box->dirtyImp = 1;
	}
	return n;
}

static void
fsInit(void)
{
	if(fsCtl >= 0)
		return;
	fsCtl = open("/mail/fs/ctl", ORDWR);
	if(fsCtl < 0)
		bye("can't open mail file system");
	if(fprint(fsCtl, "close mbox") < 0)
		bye("can't initialize mail file system");
}

static char *stoplist[] =
{
	"mbox",
	"pipeto",
	"forward",
	"names",
	"pipefrom",
	"headers",
	"imap.ok",
	0
};

enum {
	Maxokbytes	= 4096,
	Maxfolders	= Maxokbytes / 4,
};

static char *folders[Maxfolders];
static char *folderbuff;

static void
readokfolders(void)
{
	int fd, nr;

	fd = open("imap.ok", OREAD);
	if(fd < 0)
		return;
	folderbuff = malloc(Maxokbytes);
	if(folderbuff == nil) {
		close(fd);
		return;
	}
	nr = read(fd, folderbuff, Maxokbytes-1);	/* once is ok */
	close(fd);
	if(nr < 0){
		free(folderbuff);
		folderbuff = nil;
		return;
	}
	folderbuff[nr] = 0;
	tokenize(folderbuff, folders, nelem(folders));
}

/*
 * reject bad mailboxes based on mailbox name
 */
int
okMbox(char *path)
{
	char *name;
	int i;

	if(folderbuff == nil && access("imap.ok", AREAD) == 0)
		readokfolders();
	name = strrchr(path, '/');
	if(name == nil)
		name = path;
	else
		name++;
	if(folderbuff != nil){
		for(i = 0; i < nelem(folders) && folders[i] != nil; i++)
			if(cistrcmp(folders[i], name) == 0)
				return 1;
		return 0;
	}
	if(strlen(name) + STRLEN(".imp") >= MboxNameLen)
		return 0;
	for(i = 0; stoplist[i]; i++)
		if(strcmp(name, stoplist[i]) == 0)
			return 0;
	if(isprefix("L.", name) || isprefix("imap-tmp.", name)
	|| issuffix(".imp", name)
	|| strcmp("imap.subscribed", name) == 0
	|| isdotdot(name) || name[0] == '/')
		return 0;
	return 1;
}