1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Maildir folder support. FIXME rewrite - why do we chdir(2)??
3 *@ FIXME indeed - my S-Postman Python (!) is faster dealing with maildir!!
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
10 * Gunnar Ritter. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Gunnar Ritter
23 * and his contributors.
24 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #define n_FILE maildir
43 #ifndef HAVE_AMALGAMATION
50 struct message
*md_data
;
54 static struct mditem
*_maildir_table
;
55 static ui32_t _maildir_prime
;
56 static sigjmp_buf _maildir_jmp
;
58 static void __maildircatch(int s
);
59 static void __maildircatch_hold(int s
);
61 static unsigned a_maildir_hash(char const *cp
);
63 /* Do some cleanup in the tmp/ subdir */
64 static void _cleantmp(void);
66 static int _maildir_setfile1(char const *name
, enum fedit_mode fm
,
69 /* In combination with the names from mkname(), this comparison function
70 * ensures that the order of messages in a maildir folder created by mailx
71 * remains always the same. In effect, if a mbox folder is transferred to
72 * a maildir folder by 'copy *', the message order wont' change */
73 static int mdcmp(void const *a
, void const *b
);
75 static int _maildir_subdir(char const *name
, char const *sub
,
78 static void _maildir_append(char const *name
, char const *sub
,
81 static void readin(char const *name
, struct message
*m
);
83 static void maildir_update(void);
85 static void _maildir_move(struct message
*m
);
87 static char * mkname(time_t t
, enum mflag f
, char const *pref
);
89 static enum okay
maildir_append1(char const *name
, FILE *fp
, off_t off1
,
90 long size
, enum mflag flag
);
92 static enum okay
trycreate(char const *name
);
94 static enum okay
mkmaildir(char const *name
);
96 static struct message
* mdlook(char const *name
, struct message
*data
);
98 static void mktable(void);
100 static enum okay
subdir_remove(char const *name
, char const *sub
);
103 __maildircatch(int s
)
105 NYD_X
; /* Signal handler */
106 siglongjmp(_maildir_jmp
, s
);
110 __maildircatch_hold(int s
)
112 NYD_X
; /* Signal handler */
114 /* TODO no STDIO in signal handler, no _() tr's -- pre-translate interrupt
116 n_err_sighdl(_("\nImportant operation in progress: "
117 "interrupt again to forcefully abort\n"));
118 safe_signal(SIGINT
, &__maildircatch
);
122 a_maildir_hash(char const *cp
) /* TODO obsolete that -> torek_hash */
129 h
= (h
<< 4 & 0xffffffff) + (*cp
&0377);
130 if ((g
= h
& 0xf0000000) != 0) {
149 if ((dirp
= opendir("tmp")) == NULL
)
152 now
= n_time_epoch();
153 while ((dp
= readdir(dirp
)) != NULL
) {
154 if (dp
->d_name
[0] == '.')
156 sstpcpy(sstpcpy(dep
, "tmp/"), dp
->d_name
);
157 if (stat(dep
, &st
) == -1)
159 if (st
.st_atime
+ 36*3600 < now
)
168 _maildir_setfile1(char const *name
, enum fedit_mode fm
, int omsgCount
)
173 if (!(fm
& FEDIT_NEWMAIL
))
176 mb
.mb_perm
= ((options
& OPT_R_FLAG
) || (fm
& FEDIT_RDONLY
)) ? 0 : MB_DELE
;
177 if ((i
= _maildir_subdir(name
, "cur", fm
)) != 0)
179 if ((i
= _maildir_subdir(name
, "new", fm
)) != 0)
181 _maildir_append(name
, NULL
, NULL
);
184 for (i
= ((fm
& FEDIT_NEWMAIL
) ? omsgCount
: 0); i
< msgCount
; ++i
) {
185 readin(name
, message
+ i
);
190 if (fm
& FEDIT_NEWMAIL
) {
191 if (msgCount
> omsgCount
)
192 qsort(&message
[omsgCount
], msgCount
- omsgCount
, sizeof *message
,
195 qsort(message
, msgCount
, sizeof *message
, &mdcmp
);
203 mdcmp(void const *a
, void const *b
)
205 struct message
const *mpa
= a
, *mpb
= b
;
209 if ((i
= mpa
->m_time
- mpb
->m_time
) == 0)
210 i
= strcmp(mpa
->m_maildir_file
+ 4, mpb
->m_maildir_file
+ 4);
216 _maildir_subdir(char const *name
, char const *sub
, enum fedit_mode fm
)
223 if ((dirp
= opendir(sub
)) == NULL
) {
224 n_err(_("Cannot open directory %s\n"),
225 n_shexp_quote_cp(savecatsep(name
, '/', sub
), FAL0
));
229 if (access(sub
, W_OK
) == -1)
231 while ((dp
= readdir(dirp
)) != NULL
) {
232 if (dp
->d_name
[0] == '.' && (dp
->d_name
[1] == '\0' ||
233 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
235 if (dp
->d_name
[0] == '.')
237 if (!(fm
& FEDIT_NEWMAIL
) || mdlook(dp
->d_name
, NULL
) == NULL
)
238 _maildir_append(name
, sub
, dp
->d_name
);
248 _maildir_append(char const *name
, char const *sub
, char const *fn
)
253 enum mflag f
= MUSED
| MNOFROM
| MNEWEST
;
259 if (fn
!= NULL
&& sub
!= NULL
) {
260 if (!strcmp(sub
, "new"))
262 t
= strtol(fn
, &xp
, 10);
263 if ((cp
= strrchr(xp
, ',')) != NULL
&& PTRCMP(cp
, >, xp
+ 2) &&
264 cp
[-1] == '2' && cp
[-2] == ':') {
265 while (*++cp
!= '\0') {
287 /* Ensure room (and a NULLified last entry) */
289 message_append(NULL
);
292 if (fn
== NULL
|| sub
== NULL
)
295 m
= message
+ msgCount
++;
297 m
->m_maildir_file
= smalloc((sz
= strlen(sub
)) + i
+ 1 +1);
298 memcpy(m
->m_maildir_file
, sub
, sz
);
299 m
->m_maildir_file
[sz
] = '/';
300 memcpy(m
->m_maildir_file
+ sz
+ 1, fn
, i
+1);
303 m
->m_maildir_hash
= ~a_maildir_hash(fn
);
310 readin(char const *name
, struct message
*m
)
313 size_t bufsize
, buflen
, cnt
;
314 long size
= 0, lines
= 0;
320 if ((fp
= Fopen(m
->m_maildir_file
, "r")) == NULL
) {
321 n_err(_("Cannot read %s for message %lu\n"),
322 n_shexp_quote_cp(savecatsep(name
, '/', m
->m_maildir_file
), FAL0
),
323 (ul_i
)PTR2SIZE(m
- message
+ 1));
324 m
->m_flag
|= MHIDDEN
;
329 fseek(mb
.mb_otf
, 0L, SEEK_END
);
330 offset
= ftell(mb
.mb_otf
);
331 buf
= smalloc(bufsize
= LINESIZE
);
333 while (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1) != NULL
) {
334 /* Since we simply copy over data without doing any transfer
335 * encoding reclassification/adjustment we *have* to perform
336 * RFC 4155 compliant From_ quoting here */
337 if (emptyline
&& is_head(buf
, buflen
, FAL0
)) {
338 putc('>', mb
.mb_otf
);
341 size
+= fwrite(buf
, 1, buflen
, mb
.mb_otf
);/*XXX err hdling*/
342 emptyline
= (*buf
== '\n');
347 putc('\n', mb
.mb_otf
);
354 m
->m_size
= m
->m_xsize
= size
;
355 m
->m_lines
= m
->m_xlines
= lines
;
356 m
->m_block
= mailx_blockof(offset
);
357 m
->m_offset
= mailx_offsetof(offset
);
367 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0;
373 if (!(pstate
& PS_EDIT
)) {
375 for (m
= message
, c
= 0; PTRCMP(m
, <, message
+ msgCount
); ++m
) {
376 if (m
->m_flag
& MBOX
)
380 if (makembox() == STOP
)
383 for (m
= message
, gotcha
= 0, held
= 0; PTRCMP(m
, <, message
+ msgCount
);
385 if (pstate
& PS_EDIT
)
386 dodel
= m
->m_flag
& MDELETED
;
388 dodel
= !((m
->m_flag
& MPRESERVE
) || !(m
->m_flag
& MTOUCH
));
390 if (unlink(m
->m_maildir_file
) < 0)
391 n_err(_("Cannot delete file %s for message %lu\n"),
392 n_shexp_quote_cp(savecatsep(mailname
, '/', m
->m_maildir_file
),
393 FAL0
), (ul_i
)PTR2SIZE(m
- message
+ 1));
397 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
) ||
398 (m
->m_flag
& (MNEW
| MBOXED
| MSAVED
| MSTATUS
| MFLAG
|
399 MUNFLAG
| MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
))) {
407 if ((gotcha
|| modflags
) && (pstate
& PS_EDIT
)) {
408 printf(_("%s "), n_shexp_quote_cp(displayname
, FAL0
));
409 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
410 ? _("complete\n") : _("updated.\n"));
411 } else if (held
&& !(pstate
& PS_EDIT
) && mb
.mb_perm
!= 0) {
413 printf(_("Held 1 message in %s\n"), displayname
);
415 printf(_("Held %d messages in %s\n"), held
, displayname
);
419 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
420 free(m
->m_maildir_file
);
425 _maildir_move(struct message
*m
)
430 fn
= mkname(0, m
->m_flag
, m
->m_maildir_file
+ 4);
431 new = savecat("cur/", fn
);
432 if (!strcmp(m
->m_maildir_file
, new))
434 if (link(m
->m_maildir_file
, new) == -1) {
435 n_err(_("Cannot link %s to %s: message %lu not touched\n"),
436 n_shexp_quote_cp(savecatsep(mailname
, '/', m
->m_maildir_file
), FAL0
),
437 n_shexp_quote_cp(savecatsep(mailname
, '/', new), FAL0
),
438 (ul_i
)PTR2SIZE(m
- message
+ 1));
441 if (unlink(m
->m_maildir_file
) == -1)
442 n_err(_("Cannot unlink %s\n"),
443 n_shexp_quote_cp(savecatsep(mailname
, '/', m
->m_maildir_file
), FAL0
));
449 mkname(time_t t
, enum mflag f
, char const *pref
)
451 static unsigned long cnt
;
452 static pid_t mypid
; /* XXX This should possibly be global, somehow */
466 if (UICMP(32, n
, <, size
+ 8))
467 node
= srealloc(node
, size
+= 20);
470 node
[n
++] = '\\', node
[n
++] = '0',
471 node
[n
++] = '5', node
[n
++] = '7';
474 node
[n
++] = '\\', node
[n
++] = '0',
475 node
[n
++] = '7', node
[n
++] = '2';
480 } while (*cp
++ != '\0');
482 size
= 60 + strlen(node
);
484 n
= snprintf(cp
, size
, "%lu.%06lu_%06lu.%s:2,",
485 (ul_i
)t
, (ul_i
)mypid
, ++cnt
, node
);
487 size
= (n
= strlen(pref
)) + 13;
489 memcpy(cp
, pref
, n
+1);
490 for (i
= n
; i
> 3; --i
)
491 if (cp
[i
- 1] == ',' && cp
[i
- 2] == '2' && cp
[i
- 3] == ':') {
496 memcpy(cp
+ n
, ":2,", 3 +1);
518 maildir_append1(char const *name
, FILE *fp
, off_t off1
, long size
,
521 char buf
[4096], *fn
, *tfn
, *nfn
;
525 size_t nlen
, flen
, n
;
531 /* Create a unique temporary file */
532 for (nfn
= (char*)0xA /* XXX no magic */;; n_msleep(500, FAL0
)) {
533 now
= n_time_epoch();
534 flen
= strlen(fn
= mkname(now
, flag
, NULL
));
535 tfn
= salloc(n
= nlen
+ flen
+ 6);
536 snprintf(tfn
, n
, "%s/tmp/%s", name
, fn
);
538 /* Use "wx" for O_EXCL XXX stat(2) rather redundant; coverity:TOCTOU */
539 if ((!stat(tfn
, &st
) || errno
== ENOENT
) &&
540 (op
= Fopen(tfn
, "wx")) != NULL
)
543 nfn
= (char*)(PTR2SIZE(nfn
) - 1);
545 n_err(_("Can't create an unique file name in %s\n"),
546 n_shexp_quote_cp(savecat(name
, "/tmp"), FAL0
));
551 if (fseek(fp
, off1
, SEEK_SET
) == -1)
554 size_t z
= UICMP(z
, size
, >, sizeof buf
) ? sizeof buf
: (size_t)size
;
556 if (z
!= (n
= fread(buf
, 1, z
, fp
)) || n
!= fwrite(buf
, 1, n
, op
)) {
558 n_err(_("Error writing to %s\n"), n_shexp_quote_cp(tfn
, FAL0
));
566 nfn
= salloc(n
= nlen
+ flen
+ 6);
567 snprintf(nfn
, n
, "%s/new/%s", name
, fn
);
568 if (link(tfn
, nfn
) == -1) {
569 n_err(_("Cannot link %s to %s\n"), n_shexp_quote_cp(tfn
, FAL0
),
570 n_shexp_quote_cp(nfn
, FAL0
));
575 if (unlink(tfn
) == -1)
576 n_err(_("Cannot unlink %s\n"), n_shexp_quote_cp(tfn
, FAL0
));
583 trycreate(char const *name
)
589 if (!stat(name
, &st
)) {
590 if (!S_ISDIR(st
.st_mode
)) {
591 n_err(_("%s is not a directory\n"), n_shexp_quote_cp(name
, FAL0
));
594 } else if (!n_path_mkdir(name
)) {
595 n_err(_("Cannot create directory %s\n"), n_shexp_quote_cp(name
, FAL0
));
605 mkmaildir(char const *name
) /* TODO proper cleanup on error; use path[] loop */
612 if (trycreate(name
) == OKAY
) {
613 np
= ac_alloc((sz
= strlen(name
)) + 4 +1);
614 memcpy(np
, name
, sz
);
615 memcpy(np
+ sz
, "/tmp", 4 +1);
616 if (trycreate(np
) == OKAY
) {
617 memcpy(np
+ sz
, "/new", 4 +1);
618 if (trycreate(np
) == OKAY
) {
619 memcpy(np
+ sz
, "/cur", 4 +1);
629 static struct message
*
630 mdlook(char const *name
, struct message
*data
)
636 if (data
&& data
->m_maildir_hash
)
637 h
= ~data
->m_maildir_hash
;
639 h
= a_maildir_hash(name
);
642 md
= _maildir_table
+ c
;
644 while (md
->md_data
!= NULL
) {
645 if (!strcmp(md
->md_data
->m_maildir_file
+ 4, name
))
647 c
+= (n
& 1) ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
649 while (c
>= _maildir_prime
)
651 md
= _maildir_table
+ c
;
653 if (data
!= NULL
&& md
->md_data
== NULL
)
666 _maildir_prime
= nextprime(msgCount
);
667 _maildir_table
= scalloc(_maildir_prime
, sizeof *_maildir_table
);
668 for (mp
= message
, i
= msgCount
; i
-- != 0; ++mp
)
669 mdlook(mp
->m_maildir_file
+ 4, mp
);
674 subdir_remove(char const *name
, char const *sub
)
677 int pathsize
, pathend
, namelen
, sublen
, n
;
683 namelen
= strlen(name
);
684 sublen
= strlen(sub
);
685 path
= smalloc(pathsize
= namelen
+ sublen
+ 30 +1);
686 memcpy(path
, name
, namelen
);
688 memcpy(path
+ namelen
+ 1, sub
, sublen
);
689 path
[namelen
+ sublen
+ 1] = '/';
690 path
[pathend
= namelen
+ sublen
+ 2] = '\0';
692 if ((dirp
= opendir(path
)) == NULL
) {
696 while ((dp
= readdir(dirp
)) != NULL
) {
697 if (dp
->d_name
[0] == '.' && (dp
->d_name
[1] == '\0' ||
698 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
700 if (dp
->d_name
[0] == '.')
702 n
= strlen(dp
->d_name
);
703 if (UICMP(32, pathend
+ n
+1, >, pathsize
))
704 path
= srealloc(path
, pathsize
= pathend
+ n
+ 30);
705 memcpy(path
+ pathend
, dp
->d_name
, n
+1);
706 if (unlink(path
) == -1) {
714 path
[pathend
] = '\0';
715 if (rmdir(path
) == -1) {
727 maildir_setfile(char const * volatile name
, enum fedit_mode fm
)
729 sighandler_type
volatile saveint
;
735 omsgCount
= msgCount
;
736 if (cwget(&cw
) == STOP
) {
737 n_alert(_("Cannot open current directory"));
741 if (!(fm
& FEDIT_NEWMAIL
) && !quit(FAL0
))
744 saveint
= safe_signal(SIGINT
, SIG_IGN
);
746 if (!(fm
& FEDIT_NEWMAIL
)) {
747 if (fm
& FEDIT_SYSBOX
)
760 mb
.mb_type
= MB_MAILDIR
;
763 if (chdir(name
) < 0) {
764 n_err(_("Cannot change directory to %s\n"), n_shexp_quote_cp(name
, FAL0
));
765 mb
.mb_type
= MB_VOID
;
769 safe_signal(SIGINT
, saveint
);
773 _maildir_table
= NULL
;
774 if (sigsetjmp(_maildir_jmp
, 1) == 0) {
775 if (fm
& FEDIT_NEWMAIL
)
777 if (saveint
!= SIG_IGN
)
778 safe_signal(SIGINT
, &__maildircatch
);
779 i
= _maildir_setfile1(name
, fm
, omsgCount
);
781 if ((fm
& FEDIT_NEWMAIL
) && _maildir_table
!= NULL
)
782 free(_maildir_table
);
784 safe_signal(SIGINT
, saveint
);
787 mb
.mb_type
= MB_VOID
;
792 if (cwret(&cw
) == STOP
)
793 n_panic(_("Cannot change back to current directory"));
797 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
&& msgCount
> omsgCount
) {
802 if (!(fm
& FEDIT_NEWMAIL
)) {
803 pstate
&= ~PS_SAW_COMMAND
;
804 pstate
|= PS_SETFILE_OPENED
;
807 if ((options
& OPT_EXISTONLY
) && !(options
& OPT_HEADERLIST
)) {
812 if (!(fm
& FEDIT_NEWMAIL
) && (fm
& FEDIT_SYSBOX
) && msgCount
== 0) {
813 if (mb
.mb_type
== MB_MAILDIR
/* XXX ?? */ && !ok_blook(emptystart
))
814 n_err(_("No mail at %s\n"), n_shexp_quote_cp(name
, FAL0
));
819 if ((fm
& FEDIT_NEWMAIL
) && msgCount
> omsgCount
)
820 newmailinfo(omsgCount
);
828 maildir_quit(bool_t hold_sigs_on
)
830 sighandler_type saveint
;
840 if (cwget(&cw
) == STOP
) {
841 n_alert(_("Cannot open current directory"));
845 saveint
= safe_signal(SIGINT
, SIG_IGN
);
847 if (chdir(mailname
) == -1) {
848 n_err(_("Cannot change directory to %s\n"),
849 n_shexp_quote_cp(mailname
, FAL0
));
851 safe_signal(SIGINT
, saveint
);
855 if (sigsetjmp(_maildir_jmp
, 1) == 0) {
856 if (saveint
!= SIG_IGN
)
857 safe_signal(SIGINT
, &__maildircatch_hold
);
861 safe_signal(SIGINT
, saveint
);
863 if (cwret(&cw
) == STOP
)
864 n_panic(_("Cannot change back to current directory"));
875 maildir_append(char const *name
, FILE *fp
, long offset
)
878 size_t bufsize
, buflen
, cnt
;
879 off_t off1
= -1, offs
;
882 enum {_NONE
= 0, _INHEAD
= 1<<0, _NLSEP
= 1<<1} state
;
886 if ((rv
= mkmaildir(name
)) != OKAY
)
889 buf
= smalloc(bufsize
= LINESIZE
); /* TODO line pool; signals */
892 offs
= offset
/* BSD will move due to O_APPEND! ftell(fp) */;
896 for (flag
= MNEW
, state
= _NLSEP
;;) {
897 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
900 ((state
& (_INHEAD
| _NLSEP
)) == _NLSEP
&&
901 is_head(buf
, buflen
, FAL0
))) {
902 if (off1
!= (off_t
)-1) {
903 if ((rv
= maildir_append1(name
, fp
, off1
, size
, flag
)) == STOP
)
906 if (fseek(fp
, offs
+ buflen
, SEEK_SET
) == -1) {
911 off1
= offs
+ buflen
;
923 if (buf
[0] == '\n') {
926 } else if (state
& _INHEAD
) {
927 if (!ascncasecmp(buf
, "status", 6)) {
929 while (whitechar(*lp
))
932 while (*++lp
!= '\0')
941 } else if (!ascncasecmp(buf
, "x-status", 8)) {
943 while (whitechar(*lp
))
946 while (*++lp
!= '\0')
972 maildir_remove(char const *name
)
977 if (subdir_remove(name
, "tmp") == STOP
||
978 subdir_remove(name
, "new") == STOP
||
979 subdir_remove(name
, "cur") == STOP
)
981 if (rmdir(name
) == -1) {