-
Notifications
You must be signed in to change notification settings - Fork 1
/
pine-maildir-4.33
1370 lines (1357 loc) · 44 KB
/
pine-maildir-4.33
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff -Nur pine4.33/README.maildir pine4.33-maildir/README.maildir
--- pine4.33/README.maildir Wed Dec 31 18:00:00 1969
+++ pine4.33-maildir/README.maildir Mon Jun 19 12:36:17 2000
@@ -0,0 +1,93 @@
+Pine 4.0x Maildir c-client module
+---------------------------------
+
+Written by Mattias Larsson <[email protected]> <[email protected]>
+
+This is the second release of my Maildir driver for Pine 4. It is based
+on Eric Greens IMAP2bis Maildir driver.
+
+PLEASE NOTE that this driver has only been tested in the IMAP4rev1 daemon
+before. It was just put into Pine 4.00, and it looks like it works, but it
+has not been extensively tested. It has been running for 8 months in
+production in our IMAP4 daemon though.
+
+PLEASE NOTE:
+
+This driver needs to store the UID's of the message in the file somehow. In
+the earlier versions of this driver this was done by adding a new maildir
+flag (the ,3 flag), however, this solution was not very good since most
+other clients could not handle it. Thus I had to find another solution. In
+this driver I use a pretty radical method. Any file in the maildir with the
+execute bit set has its UID set in the mtime of the file. So you should not
+edit the files in here, or in any otherway update the mtime, because then
+the UID gets changed. Most clients should not do this, so I think this
+solution is compatible with most other clients (if you find one that isn't,
+let me know). If you for some reason have to edit a file in the Maildir,
+delete the ".uidvalidity" file in the root of the Maildir (ie, the dir where
+you find cur/ new/ and tmp/). Alternatively, edit maildir.c and define the
+NO_UID_VALIDITY option, which will cause the Maildir to get set new UID's on
+every start up. Note that if you are running IMAP and Netscape as a client,
+you can't change the UID's, because Netscape do not support the UID validity
+flag in the IMAP protocol. So use this with care.
+
+Please edit the maildir.c file in any case. There are 3 options you can
+set yourself. The default configuration is not suitable for ISPs. If you are
+an ISP, or system with many users, you might want to consider setting some
+of the options different.
+
+Ohh, if there are problems compiling it, let me know, and please let me know
+what you did to fix it. This thing was developed on Solaris (using both GCC
+and SunCC, should work with both), but I haven't tried it on any other
+platform. It is also known to compile cleanly on Linux RH5.1
+
+CONFIGURATION
+-------------
+
+There are a few configurable options. You find these at the top of the
+maildir.c file (and it can be found in imap/src/osdep/unix if I'm not all
+mistaken). Right now, two options are configurable. By default it is
+configured for ISP use, something that you might want to change if you use
+it at home.
+
+HOW TO USE
+----------
+
+Use it as any other c-client driver. There is some option you want to change
+if you want all folders to be created as Maildirs (and I can't remember what
+the option is from the top of my head). Read the pine documentation.
+
+CHANGES
+-------
+
+Rel 4. Coredump problem fixed. In release 3 I decided to user the sparep
+ in the message cache so no patching of mail.h would be necessary,
+ however, PINE uses this pointer internally for other things, causing
+ coredumps when used with the Rel 3. patch.
+
+Rel 3. New way of storing UID's (compatible with ,2 clients).
+ Multiple inbox patches applied
+
+Rel 2. Pine 4 changes.
+
+Rel 1. Imap4rev 1 driver
+
+FINAL NOTES
+-----------
+
+I'll try to maintain and release new versions as soon as I have time over,
+which unfortunately does not happen very often in this business ;)
+
+You can (might) find newer versions of this driver at:
+
+http://www.freeit.com/mta/
+
+
+Regards,
+Daniel Mattias Larsson
+
+e-mail: [email protected]
+ph: +46-707-268785
+snail-mail:
+Industrivagen 4
+SE-194 77 Upplands Vasby
+SWEDEN
diff -Nur pine4.33/imap/src/c-client/mail.h pine4.33-maildir/imap/src/c-client/mail.h
--- pine4.33/imap/src/c-client/mail.h Mon Nov 1 20:18:28 1999
+++ pine4.33-maildir/imap/src/c-client/mail.h Mon Jun 19 12:36:17 2000
@@ -623,6 +623,7 @@
unsigned int spare2 : 1; /* second spare bit */
unsigned int spare3 : 1; /* third spare bit */
void *sparep; /* spare pointer */
+ void *maildirp; /* for the Maildir driver */
unsigned long user_flags; /* user-assignable flags */
} MESSAGECACHE;
diff -Nur pine4.33/imap/src/osdep/unix/Makefile pine4.33-maildir/imap/src/osdep/unix/Makefile
--- pine4.33/imap/src/osdep/unix/Makefile Tue Nov 16 20:26:06 1999
+++ pine4.33-maildir/imap/src/osdep/unix/Makefile Mon Jun 19 12:36:17 2000
@@ -35,7 +35,7 @@
EXTRAAUTHENTICATORS=
SPECIALAUTHENTICATORS=
-EXTRADRIVERS=mbox
+EXTRADRIVERS=maildir mbox
PASSWDTYPE=std
@@ -85,7 +85,7 @@
# Standard distribution build parameters
DEFAULTAUTHENTICATORS=md5 $(SPECIALAUTHENTICATORS) log
-DEFAULTDRIVERS=imap nntp pop3 mh mx mbx tenex mtx mmdf unix news phile
+DEFAULTDRIVERS=maildir imap nntp pop3 mh mx mbx tenex mtx mmdf unix news phile
# Normally no need to change any of these new diff!
@@ -90,7 +90,7 @@
ARCHIVE=c-client.a
BINARIES=mail.o misc.o newsrc.o smanager.o osdep.o utf8.o siglocal.o \
dummy.o pseudo.o netmsg.o flstring.o fdstring.o \
- rfc822.o nntp.o smtp.o imap4r1.o pop3.o \
+ rfc822.o nntp.o smtp.o imap4r1.o pop3.o maildir.o\
unix.o mbox.o mbx.o mmdf.o tenex.o mtx.o news.o phile.o mh.o mx.o
CFLAGS=-g
@@ -669,7 +669,7 @@
tenex.o: mail.h misc.h osdep.h tenex.h dummy.h
unix.o: mail.h misc.h osdep.h unix.h pseudo.h dummy.h
utf8.o: mail.h misc.h osdep.h utf8.h
-
+maildir.o: mail.h misc.h osdep.h maildir.h dummy.h
# OS-dependent
diff -Nur pine4.33/imap/src/osdep/unix/dummy.c pine4.33-maildir/imap/src/osdep/unix/dummy.c
--- pine4.33/imap/src/osdep/unix/dummy.c Thu Oct 14 16:18:57 1999
+++ pine4.33-maildir/imap/src/osdep/unix/dummy.c Mon Jun 19 12:36:17 2000
@@ -331,7 +331,10 @@
DRIVER *d;
/* don't \NoSelect if have a driver for it */
if ((attributes & LATT_NOSELECT) && (d = mail_valid (NIL,name,NIL)) &&
- (d != &dummydriver)) attributes &= ~LATT_NOSELECT;
+ (d != &dummydriver)) {
+ attributes &= ~LATT_NOSELECT;
+ attributes |= LATT_NOINFERIORS;
+ }
if (contents) { /* want to search contents? */
/* forget it if can't select or open */
if ((attributes & LATT_NOSELECT) || !(csiz = strlen (contents)) ||
@@ -353,7 +356,8 @@
}
/* notify main program */
mm_list (stream,delimiter,name,attributes);
- return T;
+ if (attributes & LATT_NOINFERIORS) return NIL;
+ else return T;
}
/* Dummy create mailbox
diff -Nur pine4.33/imap/src/osdep/unix/maildir.c pine4.33-maildir/imap/src/osdep/unix/maildir.c
--- pine4.33/imap/src/osdep/unix/maildir.c Wed Dec 31 18:00:00 1969
+++ pine4.33-maildir/imap/src/osdep/unix/maildir.c Mon Jun 19 12:38:10 2000
@@ -0,0 +1,1122 @@
+/*
+ * Maildir Module for PINE 4.0x - fourth release, use with CARE!
+ *
+ * Author: Mattias Larsson <[email protected]>
+ *
+ * Version: 21.07.98
+ *
+ * Please read the README.maildir file before using this module!
+ *
+ * If you have any questions, please e-mail [email protected]
+ *
+ * Multiple inboxes patch by Dean Gaudet <[email protected]>
+ *
+ * =================================================
+ *
+ * Based on the IMAP2 maildir routines by:
+ *
+ * Author: Eric Green
+ * Bloodhounds International Inc.
+ *
+ * Additional contributions from:
+ * Aidas Kasparas ([email protected])
+ *
+ * Date: 27 April 1997
+ * Last Edited: 13 June 1997
+ *
+ * Based (heavily) on mh.c and other c-client library files by Mark Crispin:
+ *
+ * Mark Crispin
+ * Networks and Distributed Computing
+ * Computing & Communications
+ * University of Washington
+ * Administration Building, AG-44
+ * Seattle, WA 98195
+ * Internet: [email protected]
+ *
+ * Copyright 1995 by the University of Washington
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose and without fee is hereby granted, provided
+ * that the above copyright notice appears in all copies and that both the
+ * above copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the University of Washington not be
+ * used in advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission. This software is made
+ * available "as is", and
+ * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
+ * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
+ * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
+ * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+/* CONFIGURABLE OPTIONS - PLEASE CHECK THESE OUT */
+
+#undef NO_MAILDIR_FIDDLE /* disallow Maildir with Maildir in the
+ name. This is useful in an ISP setup
+ using the IMAP daemon. #undef it if you
+ are running a normal pine and know what
+ you are doing */
+
+#undef NO_ABSOLUTE_PATHS /* if you define this, all paths
+ use your HOMEDIR is the root instead
+ of the actual root of the machine. This
+ is also useful in an ISP setup with
+ IMAP */
+
+#undef NO_UID_VALIDITIY /* define this if you want the UID's not
+ to be persistent over sessions. Use this
+ if you use another client to read the
+ maildir that screws up the special way
+ in which we store UIDs. Do not enable
+ unless you are sure you need it. */
+
+/* END CONFIGURATION */
+
+#define MTA_DEBUG /* debugging sent to stdout */
+#undef MTA_DEBUG
+
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+extern int errno; /* just in case */
+#include "mail.h"
+#include "osdep.h"
+#include <pwd.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <utime.h>
+#include "maildir.h"
+#include "misc.h"
+#include "dummy.h"
+
+/* Driver dispatch used by MAIL */
+
+DRIVER maildirdriver = {
+ "maildir", /* driver name */
+ /* driver flags */
+ DR_MAIL|DR_LOCAL|DR_NOFAST|DR_NAMESPACE,
+ (DRIVER *) NIL, /* next driver */
+ maildir_valid, /* mailbox is valid for us */
+ maildir_parameters, /* manipulate parameters */
+ NIL, /* scan mailboxes */
+ maildir_list, /* find mailboxes */
+ maildir_lsub, /* find subscribed mailboxes */
+ maildir_sub, /* subscribe to mailbox */
+ maildir_unsub, /* unsubscribe from mailbox */
+ maildir_create, /* create mailbox */
+ maildir_delete, /* delete mailbox */
+ maildir_rename, /* rename mailbox */
+ NIL, /* status of mailbox */
+ maildir_open, /* open mailbox */
+ maildir_close, /* close mailbox */
+ maildir_fast, /* fetch message "fast" attributes */
+ NIL, /* fetch message flags */
+ NIL, /* fetch overview */
+ NIL, /* fetch message envelopes */
+ maildir_fetchheader, /* fetch message header */
+ maildir_fetchtext, /* fetch message body */
+ NIL, /* fetch partial message text */
+ NIL, /* unique identifier */
+ NIL, /* message number */
+ NIL, /* modify flags */
+ maildir_flagmsg, /* per-message modify flags */
+ NIL, /* search for message based on criteria */
+ NIL, /* sort messages */
+ NIL, /* thread messages */
+ maildir_ping, /* ping mailbox to see if still alive */
+ maildir_check, /* check for new messages */
+ maildir_expunge, /* expunge deleted messages */
+ maildir_copy, /* copy messages to another mailbox */
+ maildir_append, /* append string message to mailbox */
+ maildir_gc /* garbage collect stream */
+};
+
+ /* prototype stream */
+MAILSTREAM maildirproto = {&maildirdriver};
+
+/* Check validity of mailbox
+ */
+
+DRIVER *maildir_valid (char *name)
+{
+ return maildir_isvalid(name,T) ? &maildirdriver : NIL;
+}
+
+int maildir_isvalid (char *name,long justname)
+{
+ char tmp[MAILTMPLEN];
+ struct stat sbuf;
+
+ if (!name || (!*name))
+ return NIL;
+
+ /* okay, anything containing the name Maildir will be ignored
+ this is to prevent anyone from fiddling with their incoming Maildir
+ directly, it should be accessed via the INBOX alias */
+
+ #ifdef NO_MAILDIR_FIDDLE
+ if (strstr(name, "Maildir")) {
+ return NIL;
+ }
+ #endif
+ /* If we are requested only to check
+ if the name is appropriate then we
+ have done! */
+ if (justname && *name == '#') return T;
+
+
+ /* must be valid local mailbox */
+ if ((*name != '*') && (*name != '{') &&
+ maildir_file (tmp,name) &&
+ /* assume its maildir if its a dir */
+ stat (tmp,&sbuf) == 0 && S_ISDIR (sbuf.st_mode))
+ return T;
+
+ /* INBOX is for default Maildir */
+ if (!strcmp (ucase (strcpy (tmp,name)), "INBOX") &&
+ (stat (maildir_file (tmp,name),&sbuf) == 0) &&
+ S_ISDIR (sbuf.st_mode))
+ return T;
+
+ return NIL;
+}
+
+/* Maildir mail generate file string
+ */
+
+char *maildir_file (char *dst,char *name)
+{
+ char tmp[MAILTMPLEN];
+
+ if (strlen (name) > 3 && /* safe do other comparisons */
+ (*name == '#') &&
+ (name[1] == 'm' || name[1] == 'M') &&
+ (name[2] == 'd' || name[2] == 'D') &&
+ (name[3] == '/'))
+ name += 4;
+
+#ifdef NO_ABSOLUTE_PATHS
+ if (*name == '/') {
+ /* we do not want to accept / absolute paths, so lets strip the first
+ / ... */
+ sprintf(dst,"%s/%s/cur", myhomedir(), name+1);
+
+/* strncpy (dst, name, MAILTMPLEN - 2);
+ strncat (dst, "/cur", MAILTMPLEN - 2);
+ dst[MAILTMPLEN - 1] = '\0'; */
+ }
+ else
+ sprintf (dst,"%s/%s/cur",myhomedir (),
+ strcmp (ucase (strcpy (tmp, name)), "INBOX") ? name : MAILDIRPATH);
+#else
+ if (*name == '/') {
+ strncpy (dst, name, MAILTMPLEN - 2);
+ strncat (dst, "/cur", MAILTMPLEN - 2);
+ dst[MAILTMPLEN - 1] = '\0';
+ }
+ else
+ sprintf (dst,"%s/%s/cur",myhomedir (),
+ strcmp (ucase (strcpy (tmp, name)), "INBOX") ? name : MAILDIRPATH);
+
+#endif
+
+ #ifdef MTA_DEBUG
+ printf("maildir_file '%s'\n", dst);
+ #endif
+ return dst;
+}
+
+/* Maildir open
+ */
+
+MAILSTREAM *maildir_open (MAILSTREAM *stream)
+{
+ char tmp[MAILTMPLEN],tmp2[MAILTMPLEN];
+
+ if (!stream) return &maildirproto;
+ if (LOCAL) { /* recycle stream */
+ maildir_close (stream, 0);
+ stream->dtb = &maildirdriver;
+ mail_free_cache (stream);
+ stream->uid_last = 0; /* default UID validity */
+ stream->uid_validity = time (0);
+ }
+
+ stream->uid_validity = 0; /* was time(0) */
+
+ if (stream->uid_last < time(0))
+ stream->uid_last = time (0);
+
+ stream->local = fs_get (sizeof (MAILDIRLOCAL));
+ LOCAL->inbox = !strcmp (ucase (strcpy (tmp,stream->mailbox)),"INBOX") ||
+ !strcmp (stream->mailbox,maildir_file (tmp2,"INBOX"));
+ LOCAL->dir = cpystr (maildir_file (tmp,stream->mailbox)); /* copy dir name */
+ /* make temporary buffer */
+ LOCAL->buf = (char *) fs_get ((LOCAL->buflen = MAXMESSAGESIZE) + 1);
+ LOCAL->scantime = 0; /* not scanned yet */
+ stream->sequence++;
+ stream->nmsgs = stream->recent = 0;
+
+ maildir_ping_core (stream);
+ maildir_ping (stream);
+/* if (maildir_ping (stream) && !(stream->nmsgs || stream->silent))
+ printf("Mailbox is empty\n");
+*/
+ return stream;
+
+}
+
+/* Maildir ping mailbox
+ */
+
+long maildir_ping_core (MAILSTREAM *stream)
+{
+ char tmp[MAILTMPLEN];
+ MESSAGECACHE *elt;
+ struct stat sbuf, sbuf2;
+ DIR *dir;
+ struct direct *d;
+ int reloadall = NIL;
+ int uidinvalid = NIL;
+ unsigned long old;
+ long i;
+ long nmsgs = stream->nmsgs;
+ long recent = stream->recent;
+ long nfiles = stream->nmsgs;
+ int silent = stream->silent;
+ char *s, *s2;
+ mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
+
+/* maildir_copynew (LOCAL->dir);
+ */
+
+ if (stat (LOCAL->dir,&sbuf) < 0) {
+ sprintf (tmp,"Unable to open maildir: %s",strerror (errno));
+ mm_log (tmp,ERROR);
+ return NIL;
+ }
+
+ /* okay, lets try to figure out the Maildir UID validity. This is done
+ by checking the last modification time of the file .uidvalidity
+ in the rootdir of the Maildir. Any program reordering the files
+ in the directory have to touch this file */
+
+ sprintf(tmp, "%s/../.uidvalidity", LOCAL->dir);
+
+ if (stat (tmp,&sbuf2) < 0) {
+ /* no uid validity file found, if uid_validity == 0, we have
+ to set it to something other than 0, and then create the
+ .uidvalidity file for future accesses */
+
+ if (stream->uid_validity == 0) {
+ FILE *fl;
+ struct utimbuf tbuf;
+
+ stream->uid_validity = time(0);
+ tbuf.actime = stream->uid_validity;
+ tbuf.modtime = stream->uid_validity;
+
+ if ((fl = fopen(tmp, "w"))) {
+ fclose(fl);
+ chmod (tmp, S_IRUSR|S_IWUSR);
+ utime(tmp, &tbuf);
+ }
+ }
+ uidinvalid = T; /* UID's are invalid, update them */
+ } else {
+ /* valid file, lets set UID if uid_validity = 0 */
+ if (stream->uid_validity == 0) {
+ stream->uid_validity = sbuf2.st_mtime;
+ }
+ }
+
+ #ifdef NO_UID_VALIDITY
+ uidinvalid = T; /* force the UIDs to be invalid and reset every time,
+ useful in an environment without imap servers and
+ clients that screw up the UIDs.. i'd leave it to
+ OFF until I really need it though... */
+ #endif
+
+ stream->silent = T;
+ if (sbuf.st_ctime != LOCAL->scantime) {
+ /* update the message list */
+ struct direct **names = NIL;
+ nfiles = scandir (LOCAL->dir,&names,maildir_select,maildir_namesort);
+
+ for (i = 0; i < nfiles; i++) {
+
+ /* check if file has executable bit set */
+ sprintf(tmp, "%s/%s", LOCAL->dir, names[i]->d_name);
+ stat (tmp,&sbuf2);
+ if (sbuf2.st_mode & S_IXUSR) {
+ /* executable bit set, modtime is uid */
+ if (sbuf2.st_mtime > stream->uid_last)
+ stream->uid_last = sbuf2.st_mtime+1;
+ }
+ /* this is kept for backwards compatibility */
+ if ((s = strstr (names[i]->d_name,":3,")))
+ s += 3;
+ if (s && (s2 = strstr (s, ",U"))) {
+ s2 += 2;
+ sscanf(s2, "%d", &old);
+ if (old > stream->uid_last) {
+ stream->uid_last = old+1;
+ }
+ }
+
+ }
+
+ mm_critical (stream); /* go critical */
+ old = stream->uid_last;
+ LOCAL->scantime = sbuf.st_ctime;
+
+ /* check if old files same */
+ for (i = 0; i < stream->nmsgs; i++) {
+
+ if (strcmp ((char *) mail_elt (stream, i + 1)->maildirp,
+ names[i]->d_name)) {
+ reloadall = T;
+ break;
+ }
+ }
+
+ if (reloadall) { /* files are out of order, rebuild cache */
+
+ i = 1;
+ while (i <= stream->nmsgs)
+ /* clean out cache */
+ if ((elt = (MESSAGECACHE *) (*mc) (stream,i,CH_ELT))) {
+ fs_give ((void **) &elt->maildirp);
+ mail_expunged (stream,i);
+ }
+ else
+ i++;
+
+ mm_log ("Warning: Mailbox has changed in an unexpected way. Reloading.",
+ WARN);
+ stream->nmsgs = 0;
+ }
+ nmsgs = stream->nmsgs;
+
+ stream->nmsgs = nfiles; /* hm? */
+
+ for (i = nmsgs; i < nfiles; i++) {
+
+ mail_exists(stream, i+1);
+ /* if newly seen, add to list */
+ (elt = mail_elt (stream, i + 1))->maildirp = (long) cpystr (names[i]->d_name);
+ elt->valid = T;
+
+ /* grab the flags */
+ if ((s = strstr (names[i]->d_name,":3,"))) {
+ s += 3;
+ if (strchr (s,'F'))
+ elt->flagged = T;
+ if (strchr (s,'R'))
+ elt->answered = T;
+ if (strchr (s,'S'))
+ elt->seen = T;
+ if (strchr (s,'T'))
+ elt->deleted = T;
+ } else if ((s = strstr (names[i]->d_name,":2,"))) {
+ /* this is the :2, id where all files go nowadays */
+ s += 3;
+ if (strchr (s,'F'))
+ elt->flagged = T;
+ if (strchr (s,'R'))
+ elt->answered = T;
+ if (strchr (s,'S'))
+ elt->seen = T;
+ if (strchr (s,'T'))
+ elt->deleted = T;
+ sprintf(tmp, "%s/%s", LOCAL->dir, names[i]->d_name);
+ stat (tmp,&sbuf2);
+ if (sbuf2.st_mode & S_IXUSR) {
+ /* executable bit set, modtime is uid */
+ elt->private.uid = sbuf2.st_mtime;
+ }
+ /* and if we could not retrieve UID from modtime, or if
+ UIDs are invalid, go here */
+ if (elt->private.uid == 0 || uidinvalid) {
+ stream->uid_last = (elt = mail_elt (stream,i+1))->private.uid = stream->uid_last+1;
+ maildir_flagmsg(stream, elt);
+ }
+ s = 0; /* make sure next if statement does not trigger */
+ }
+
+ if (s)
+ if ((s2 = strstr (s, ",U"))) {
+ s2 += 2;
+ sscanf(s2, "%d", &elt->private.uid);
+ if (elt->private.uid == 0 || uidinvalid) {
+ stream->uid_last = (elt = mail_elt (stream,i+1))->private.uid = stream->uid_last+1;
+ maildir_flagmsg(stream, elt);
+ }
+
+ } else { /* assign new UID */
+ stream->uid_last = (elt = mail_elt (stream,i+1))->private.uid = stream->uid_last+1;
+ elt->recent = T;
+ recent++;
+ maildir_flagmsg(stream, elt); /* store the UID that we assigned to it */
+ }
+
+
+
+ }
+
+ mm_nocritical (stream); /* release critical */
+ /* free the names stuff */
+ for (i = 0; i < nfiles; i++)
+ fs_give ((void **) &names[i]);
+ if (names)
+ fs_give ((void **) &names);
+ }
+ stream->silent = silent;
+ mail_exists(stream,nfiles);
+/* if (!reloadall) */
+ mail_recent (stream,recent);
+
+ return T; /* return that we are alive */
+}
+
+long maildir_ping (MAILSTREAM *stream)
+{
+ maildir_copynew (LOCAL->dir);
+ return maildir_ping_core (stream);
+}
+
+void maildir_copynew (const char *mailbox)
+{
+ char tmp[MAILTMPLEN],file[MAILTMPLEN],newfile[MAILTMPLEN];
+ DIR *dir;
+ struct dirent *d;
+ struct stat sbuf;
+
+ sprintf (tmp,"%s/../new",mailbox);
+ if (!(dir = opendir (tmp)))
+ return;
+
+ while (d = readdir (dir)) {
+ if (d->d_name[0] == '.')
+ continue; /* skip .files */
+
+ sprintf (file,"%s/%s",tmp,d->d_name);
+ /* make sure this is a normal file */
+ if (stat (file,&sbuf) == 0 && S_ISREG (sbuf.st_mode)) {
+
+ if (strstr (d->d_name,":3,")) /* this message already has flags */
+ sprintf (newfile,"%s/%s",mailbox,d->d_name);
+ else
+ sprintf (newfile,"%s/%s:3,",mailbox,d->d_name);
+
+ /* move the new mail to the cur dir */
+ if (link (file,newfile) == -1)
+ mm_log("Unable to read new mail!",WARN);
+ else
+ unlink (file);
+ }
+ }
+ closedir (dir);
+}
+
+int maildir_select (struct direct *name)
+{
+ if (name->d_name[0] != '.')
+ return T;
+
+ return NIL;
+}
+
+int maildir_namesort (struct direct **d1,struct direct **d2)
+{
+ return strcmp ((*d1)->d_name,(*d2)->d_name);
+}
+
+
+/* Maildir garbage collect stream
+ */
+
+void maildir_gc (MAILSTREAM *stream,long gcflags)
+{
+ unsigned long i;
+
+ if (gcflags & GC_TEXTS) { /* garbage collect texts? */
+ /* flush texts from cache */
+/* if (LOCAL->hdr) fs_give ((void **) &LOCAL->hdr);
+// if (stream->text) fs_give ((void **) &stream->text);
+// stream->msgno = 0; invalidate stream text
+*/
+ }
+}
+
+/* Maildir close
+ */
+
+void maildir_close (MAILSTREAM *stream, long options)
+{
+ MESSAGECACHE *elt;
+ int i;
+ mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
+
+/* CL_EXPUNGE OPTION SUPPORT HERE SOMEWHERE! */
+ /* clean out the cached paths */
+ for (i = 1; i <= stream->nmsgs; i++)
+ if ((elt = (MESSAGECACHE *) (*mc) (stream,i,CH_ELT)) && elt->maildirp) {
+ fs_give ((void **) &elt->maildirp);
+ elt->maildirp = 0; /* otherwise pine coredumps */
+ }
+
+ if (LOCAL) { /* only if a stream is open */
+ if (LOCAL->dir) fs_give ((void **) &LOCAL->dir);
+ maildir_gc (stream,GC_TEXTS); /* free local cache */
+ /* free local scratch buffer */
+ if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
+ /* nuke the local data */
+ fs_give ((void **) &stream->local);
+ stream->dtb = NIL; /* log out the DTB */
+ }
+}
+
+void maildir_check (MAILSTREAM *stream)
+{
+ /* Perhaps in the future this will preserve flags */
+ if (maildir_ping (stream)) mm_log ("Check completed",(long) NIL);
+}
+
+long maildir_fetchtext (MAILSTREAM *stream,unsigned long msgno,STRING *bs, long flags)
+{
+ unsigned long i;
+ MESSAGECACHE *elt;
+ /* UID call "impossible" */
+ if (flags & FT_UID) return NIL;
+ elt = mail_elt (stream,msgno);/* get elt */
+ /* snarf message if don't have it yet */
+ if (!elt->private.msg.text.text.data) {
+ maildir_fetchheader (stream,msgno,&i,flags);
+ if (!elt->private.msg.text.text.data) return NIL;
+ }
+ if (!(flags & FT_PEEK)) { /* mark as seen */
+ mail_elt (stream,msgno)->seen = T;
+ maildir_flagmsg (stream, mail_elt(stream,msgno));
+ mm_flags (stream,msgno);
+ }
+ if (!elt->private.msg.text.text.data) return NIL;
+ INIT (bs,mail_string,elt->private.msg.text.text.data,
+ elt->private.msg.text.text.size);
+ return T;
+}
+
+
+/* Maildir fetch message header
+ */
+
+char *maildir_fetchheader (MAILSTREAM *stream,unsigned long msgno,
+ unsigned long *length, long flags)
+{
+ unsigned long i,hdrsize;
+ int fd;
+ char *t;
+ char tmp[MAILTMPLEN];
+ char *s,*b;
+ struct stat sbuf;
+ struct tm *tm;
+ MESSAGECACHE *elt;
+ *length = 0; /* default to empty */
+ if (flags & FT_UID) return "";/* UID call "impossible" */
+ elt = mail_elt (stream,msgno);/* get elt */
+ if (!elt->private.msg.header.text.data) {
+
+/* maildir_gc (stream,GC_TEXTS); invalidate current cache */
+ /* build message file name */
+ sprintf (tmp,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
+ if ((fd = open (tmp,O_RDONLY,NIL)) >= 0) {
+ fstat (fd,&sbuf); /* get size of message */
+ /* make plausible IMAPish date string */
+ tm = gmtime (&sbuf.st_mtime);
+ elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
+ elt->year = tm->tm_year + 1900 - BASEYEAR;
+ elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
+ elt->seconds = tm->tm_sec;
+ elt->zhours = 0; elt->zminutes = 0;
+ /* slurp message */
+ read (fd,s = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
+ s[sbuf.st_size] = '\0'; /* tie off file */
+ close (fd); /* close file */
+
+ for (i = 0,b = s; *b && !(i && (*b == '\n')); i = (*b++ == '\n'));
+ hdrsize = (*b ? ++b:b)-s; /* number of header bytes */
+
+ elt->rfc822_size = /* size of entire message in CRLF form */
+ (elt->private.msg.header.text.size =
+ strcrlfcpy ((char **) &elt->private.msg.header.text.data,&i,s,
+ hdrsize)) +
+ (elt->private.msg.text.text.size =
+ strcrlfcpy ((char **) &elt->private.msg.text.text.data,&i,b,
+ sbuf.st_size - hdrsize));
+ fs_give ((void **) &s);
+ } else return "";
+
+ }
+
+ *length = elt->private.msg.header.text.size;
+ return (char *) elt->private.msg.header.text.data;
+}
+
+void maildir_fast (MAILSTREAM *stream,char *sequence,long flags)
+{
+ unsigned long i,j;
+ /* ugly and slow */
+ if (stream && LOCAL && ((flags & FT_UID) ?
+ mail_uid_sequence (stream,sequence) :
+ mail_sequence (stream,sequence)))
+ for (i = 1; i <= stream->nmsgs; i++)
+ if (mail_elt (stream,i)->sequence) maildir_fetchheader (stream,i,&j,NIL);
+}
+
+/* Maildir find list of subscribed mailboxes
+ * Accepts: mail stream
+ * pattern to search
+ */
+
+void maildir_list (MAILSTREAM *stream,char *ref, char *pat)
+{
+ return;
+}
+
+void *maildir_parameters (long function,void *value)
+{
+ return NIL;
+}
+
+long maildir_create (MAILSTREAM *stream,char *mailbox)
+{
+ char tmp[MAILTMPLEN];
+ char err[MAILTMPLEN];
+ char *s, *s2;
+ int fnlen, i;
+ char *subdir_names[] = {"/cur","/new","/tmp",NULL};
+
+ /* must not already exist */
+ if (access (maildir_file (tmp,mailbox),F_OK) == 0) {
+ sprintf (err,"Can't create mailbox %s: mailbox already exists",mailbox);
+ mm_log (err,ERROR);
+ return NIL;
+ }
+
+ maildir_file (tmp,mailbox); /* get file name */
+ fnlen = strlen (tmp);
+ /*syslog(LOG_INFO, "fname: '%s'", tmp);*/
+ tmp[fnlen - 4] = '\0'; /* making main directory's name */
+ fnlen -= 4;
+
+ /* okay, try to add support for adding hiearchys of directories, this
+ is done by scanning for /'s.... */
+
+ /*syslog(LOG_INFO, "tmp '%s'", tmp);*/
+ s = tmp;
+
+ while ((s = strstr(s, "/")) != 0) {
+ /*syslog(LOG_INFO, "Before make: '%s'", s);*/
+ *s = '\0';
+ /*syslog(LOG_INFO, "Trying to make: '%s'", tmp);*/
+ if (mkdir (tmp,0700) && *s != '\0') /* trying to make the dir */
+ if (errno != EEXIST) {
+ sprintf (err,"Can't create mailbox %s: %s %s",
+ mailbox,tmp,strerror (errno));
+ mm_log (err,ERROR);
+ return NIL;
+ }
+ *s = '/';
+ s++;
+ }
+
+ if (mkdir (tmp,0700)) { /* try to make new dir */
+ sprintf (err,"Can't create mailbox %s: %s %s",
+ mailbox,tmp,strerror (errno));
+ mm_log (err,ERROR);
+ return NIL;
+ }
+
+ /*syslog(LOG_INFO, "create maildir");*/
+ for (i = 0; subdir_names[i]; i++) {
+ strcpy (tmp + fnlen,subdir_names[i]);
+
+ if (mkdir (tmp,0700)) { /* try to make new dir */
+ sprintf (err,"Can't create mailbox %s: %s %s",
+ mailbox,tmp,strerror (errno));
+ mm_log (err,ERROR);
+ return NIL;
+ }
+ }
+
+ return T; /* return success */
+}
+
+void maildir_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
+{
+ char oldfile[MAILTMPLEN],newfile[MAILTMPLEN],fn[MAILTMPLEN];
+ struct utimbuf tbuf;
+ char *s;
+
+ /* build the new filename */
+ sprintf (oldfile,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
+ if ((s = strchr ((char *) elt->maildirp,':'))) *s = '\0';
+ sprintf (fn,"%s:2,%s%s%s%s",(char *) elt->maildirp,elt->flagged ? "F" : "",
+ elt->answered ? "R" : "",elt->seen ? "S" : "",
+ elt->deleted ? "T" : "");
+ sprintf (newfile,"%s/%s",LOCAL->dir,fn);
+ /* rename the file with new flags */
+ if (rename (oldfile,newfile) < 0) {
+ sprintf(oldfile,"Unable to write flags to disk: %s",strerror (errno));
+ mm_log(oldfile,ERROR);
+ return;
+ }
+ /* update the file name in cache */
+ fs_give ((void **) &elt->maildirp);
+ elt->maildirp = (long) cpystr (fn);
+
+ /* fix the UID on the file */
+ tbuf.actime = elt->private.uid;
+ tbuf.modtime = elt->private.uid;
+ chmod (newfile, S_IRUSR|S_IWUSR|S_IXUSR);
+ utime (newfile, &tbuf);
+
+}
+
+void maildir_expunge (MAILSTREAM *stream)
+{
+ MESSAGECACHE *elt;
+ unsigned long i = 1;
+ unsigned long n = 0;
+ unsigned long recent = stream->recent;
+
+ maildir_gc (stream,GC_TEXTS); /* invalidate texts */
+ mm_critical (stream); /* go critical */
+ while (i <= stream->nmsgs) { /* for each message */
+ /* if deleted, need to trash it */
+ if ((elt = mail_elt (stream,i))->deleted) {
+ sprintf (LOCAL->buf,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
+ if (unlink (LOCAL->buf)) {/* try to delete the message */
+ sprintf (LOCAL->buf,"Expunge of message %ld failed, aborted: %s",i,
+ strerror (errno));
+ mm_log (LOCAL->buf,WARN);
+ break;
+ }
+ /* free the cached filename */
+ if (elt->maildirp) {
+ fs_give ((void **) &elt->maildirp);
+ elt->maildirp = 0; /* otherwise pine coredumps */
+ }
+ if (elt->recent) --recent;/* if recent, note one less recent message */
+ mail_expunged (stream,i); /* notify upper levels */
+ n++; /* count up one more expunged message */
+ }
+ else i++; /* otherwise try next message */
+ }
+ if (n) { /* output the news if any expunged */