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 - 2017 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
= ((n_poption
& n_PO_R_FLAG
) || (fm
& FEDIT_RDONLY
))
178 if ((i
= _maildir_subdir(name
, "cur", fm
)) != 0)
180 if ((i
= _maildir_subdir(name
, "new", fm
)) != 0)
182 _maildir_append(name
, NULL
, NULL
);
185 for (i
= ((fm
& FEDIT_NEWMAIL
) ? omsgCount
: 0); i
< msgCount
; ++i
) {
186 readin(name
, message
+ i
);
191 if (fm
& FEDIT_NEWMAIL
) {
192 if (msgCount
> omsgCount
)
193 qsort(&message
[omsgCount
], msgCount
- omsgCount
, sizeof *message
,
196 qsort(message
, msgCount
, sizeof *message
, &mdcmp
);
204 mdcmp(void const *a
, void const *b
)
206 struct message
const *mpa
= a
, *mpb
= b
;
210 if ((i
= mpa
->m_time
- mpb
->m_time
) == 0)
211 i
= strcmp(mpa
->m_maildir_file
+ 4, mpb
->m_maildir_file
+ 4);
217 _maildir_subdir(char const *name
, char const *sub
, enum fedit_mode fm
)
224 if ((dirp
= opendir(sub
)) == NULL
) {
225 n_err(_("Cannot open directory %s\n"),
226 n_shexp_quote_cp(savecatsep(name
, '/', sub
), FAL0
));
230 if (access(sub
, W_OK
) == -1)
232 while ((dp
= readdir(dirp
)) != NULL
) {
233 if (dp
->d_name
[0] == '.' && (dp
->d_name
[1] == '\0' ||
234 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
236 if (dp
->d_name
[0] == '.')
238 if (!(fm
& FEDIT_NEWMAIL
) || mdlook(dp
->d_name
, NULL
) == NULL
)
239 _maildir_append(name
, sub
, dp
->d_name
);
249 _maildir_append(char const *name
, char const *sub
, char const *fn
)
254 enum mflag f
= MUSED
| MNOFROM
| MNEWEST
;
260 if (fn
!= NULL
&& sub
!= NULL
) {
261 if (!strcmp(sub
, "new"))
263 t
= strtol(fn
, &xp
, 10);
264 if ((cp
= strrchr(xp
, ',')) != NULL
&& PTRCMP(cp
, >, xp
+ 2) &&
265 cp
[-1] == '2' && cp
[-2] == ':') {
266 while (*++cp
!= '\0') {
288 /* Ensure room (and a NULLified last entry) */
290 message_append(NULL
);
293 if (fn
== NULL
|| sub
== NULL
)
296 m
= message
+ msgCount
++;
298 m
->m_maildir_file
= smalloc((sz
= strlen(sub
)) + i
+ 1 +1);
299 memcpy(m
->m_maildir_file
, sub
, sz
);
300 m
->m_maildir_file
[sz
] = '/';
301 memcpy(m
->m_maildir_file
+ sz
+ 1, fn
, i
+1);
304 m
->m_maildir_hash
= ~a_maildir_hash(fn
);
311 readin(char const *name
, struct message
*m
)
314 size_t bufsize
, buflen
, cnt
;
315 long size
= 0, lines
= 0;
321 if ((fp
= Fopen(m
->m_maildir_file
, "r")) == NULL
) {
322 n_err(_("Cannot read %s for message %lu\n"),
323 n_shexp_quote_cp(savecatsep(name
, '/', m
->m_maildir_file
), FAL0
),
324 (ul_i
)PTR2SIZE(m
- message
+ 1));
325 m
->m_flag
|= MHIDDEN
;
330 fseek(mb
.mb_otf
, 0L, SEEK_END
);
331 offset
= ftell(mb
.mb_otf
);
332 buf
= smalloc(bufsize
= LINESIZE
);
334 while (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1) != NULL
) {
335 /* Since we simply copy over data without doing any transfer
336 * encoding reclassification/adjustment we *have* to perform
337 * RFC 4155 compliant From_ quoting here */
338 if (emptyline
&& is_head(buf
, buflen
, FAL0
)) {
339 putc('>', mb
.mb_otf
);
342 size
+= fwrite(buf
, 1, buflen
, mb
.mb_otf
);/*XXX err hdling*/
343 emptyline
= (*buf
== '\n');
348 putc('\n', mb
.mb_otf
);
355 m
->m_size
= m
->m_xsize
= size
;
356 m
->m_lines
= m
->m_xlines
= lines
;
357 m
->m_block
= mailx_blockof(offset
);
358 m
->m_offset
= mailx_offsetof(offset
);
368 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0;
374 if (!(n_pstate
& n_PS_EDIT
)) {
376 for (m
= message
, c
= 0; PTRCMP(m
, <, message
+ msgCount
); ++m
) {
377 if (m
->m_flag
& MBOX
)
381 if (makembox() == STOP
)
384 for (m
= message
, gotcha
= 0, held
= 0; PTRCMP(m
, <, message
+ msgCount
);
386 if (n_pstate
& n_PS_EDIT
)
387 dodel
= m
->m_flag
& MDELETED
;
389 dodel
= !((m
->m_flag
& MPRESERVE
) || !(m
->m_flag
& MTOUCH
));
391 if (unlink(m
->m_maildir_file
) < 0)
392 n_err(_("Cannot delete file %s for message %lu\n"),
393 n_shexp_quote_cp(savecatsep(mailname
, '/', m
->m_maildir_file
),
394 FAL0
), (ul_i
)PTR2SIZE(m
- message
+ 1));
398 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
) ||
399 (m
->m_flag
& (MNEW
| MBOXED
| MSAVED
| MSTATUS
| MFLAG
|
400 MUNFLAG
| MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
))) {
408 if ((gotcha
|| modflags
) && (n_pstate
& n_PS_EDIT
)) {
409 fprintf(n_stdout
, "%s %s\n",
410 n_shexp_quote_cp(displayname
, FAL0
),
411 ((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
412 ? _("complete") : _("updated.")));
413 } else if (held
&& !(n_pstate
& n_PS_EDIT
) && mb
.mb_perm
!= 0) {
415 fprintf(n_stdout
, _("Held 1 message in %s\n"), displayname
);
417 fprintf(n_stdout
, _("Held %d messages in %s\n"), held
, displayname
);
421 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
422 free(m
->m_maildir_file
);
427 _maildir_move(struct message
*m
)
432 fn
= mkname(0, m
->m_flag
, m
->m_maildir_file
+ 4);
433 new = savecat("cur/", fn
);
434 if (!strcmp(m
->m_maildir_file
, new))
436 if (link(m
->m_maildir_file
, new) == -1) {
437 n_err(_("Cannot link %s to %s: message %lu not touched\n"),
438 n_shexp_quote_cp(savecatsep(mailname
, '/', m
->m_maildir_file
), FAL0
),
439 n_shexp_quote_cp(savecatsep(mailname
, '/', new), FAL0
),
440 (ul_i
)PTR2SIZE(m
- message
+ 1));
443 if (unlink(m
->m_maildir_file
) == -1)
444 n_err(_("Cannot unlink %s\n"),
445 n_shexp_quote_cp(savecatsep(mailname
, '/', m
->m_maildir_file
), FAL0
));
451 mkname(time_t t
, enum mflag f
, char const *pref
)
453 static unsigned long cnt
;
454 static pid_t mypid
; /* XXX This should possibly be global, somehow */
468 if (UICMP(32, n
, <, size
+ 8))
469 node
= srealloc(node
, size
+= 20);
472 node
[n
++] = '\\', node
[n
++] = '0',
473 node
[n
++] = '5', node
[n
++] = '7';
476 node
[n
++] = '\\', node
[n
++] = '0',
477 node
[n
++] = '7', node
[n
++] = '2';
482 } while (*cp
++ != '\0');
484 size
= 60 + strlen(node
);
486 n
= snprintf(cp
, size
, "%lu.%06lu_%06lu.%s:2,",
487 (ul_i
)t
, (ul_i
)mypid
, ++cnt
, node
);
489 size
= (n
= strlen(pref
)) + 13;
491 memcpy(cp
, pref
, n
+1);
492 for (i
= n
; i
> 3; --i
)
493 if (cp
[i
- 1] == ',' && cp
[i
- 2] == '2' && cp
[i
- 3] == ':') {
498 memcpy(cp
+ n
, ":2,", 3 +1);
520 maildir_append1(char const *name
, FILE *fp
, off_t off1
, long size
,
523 char buf
[4096], *fn
, *tfn
, *nfn
;
527 size_t nlen
, flen
, n
;
533 /* Create a unique temporary file */
534 for (nfn
= (char*)0xA /* XXX no magic */;; n_msleep(500, FAL0
)) {
535 now
= n_time_epoch();
536 flen
= strlen(fn
= mkname(now
, flag
, NULL
));
537 tfn
= salloc(n
= nlen
+ flen
+ 6);
538 snprintf(tfn
, n
, "%s/tmp/%s", name
, fn
);
540 /* Use "wx" for O_EXCL XXX stat(2) rather redundant; coverity:TOCTOU */
541 if ((!stat(tfn
, &st
) || errno
== ENOENT
) &&
542 (op
= Fopen(tfn
, "wx")) != NULL
)
545 nfn
= (char*)(PTR2SIZE(nfn
) - 1);
547 n_err(_("Can't create an unique file name in %s\n"),
548 n_shexp_quote_cp(savecat(name
, "/tmp"), FAL0
));
553 if (fseek(fp
, off1
, SEEK_SET
) == -1)
556 size_t z
= UICMP(z
, size
, >, sizeof buf
) ? sizeof buf
: (size_t)size
;
558 if (z
!= (n
= fread(buf
, 1, z
, fp
)) || n
!= fwrite(buf
, 1, n
, op
)) {
560 n_err(_("Error writing to %s\n"), n_shexp_quote_cp(tfn
, FAL0
));
568 nfn
= salloc(n
= nlen
+ flen
+ 6);
569 snprintf(nfn
, n
, "%s/new/%s", name
, fn
);
570 if (link(tfn
, nfn
) == -1) {
571 n_err(_("Cannot link %s to %s\n"), n_shexp_quote_cp(tfn
, FAL0
),
572 n_shexp_quote_cp(nfn
, FAL0
));
577 if (unlink(tfn
) == -1)
578 n_err(_("Cannot unlink %s\n"), n_shexp_quote_cp(tfn
, FAL0
));
585 trycreate(char const *name
)
591 if (!stat(name
, &st
)) {
592 if (!S_ISDIR(st
.st_mode
)) {
593 n_err(_("%s is not a directory\n"), n_shexp_quote_cp(name
, FAL0
));
596 } else if (!n_path_mkdir(name
)) {
597 n_err(_("Cannot create directory %s\n"), n_shexp_quote_cp(name
, FAL0
));
607 mkmaildir(char const *name
) /* TODO proper cleanup on error; use path[] loop */
614 if (trycreate(name
) == OKAY
) {
615 np
= ac_alloc((sz
= strlen(name
)) + 4 +1);
616 memcpy(np
, name
, sz
);
617 memcpy(np
+ sz
, "/tmp", 4 +1);
618 if (trycreate(np
) == OKAY
) {
619 memcpy(np
+ sz
, "/new", 4 +1);
620 if (trycreate(np
) == OKAY
) {
621 memcpy(np
+ sz
, "/cur", 4 +1);
631 static struct message
*
632 mdlook(char const *name
, struct message
*data
)
638 if (data
&& data
->m_maildir_hash
)
639 h
= ~data
->m_maildir_hash
;
641 h
= a_maildir_hash(name
);
644 md
= _maildir_table
+ c
;
646 while (md
->md_data
!= NULL
) {
647 if (!strcmp(md
->md_data
->m_maildir_file
+ 4, name
))
649 c
+= (n
& 1) ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
651 while (c
>= _maildir_prime
)
653 md
= _maildir_table
+ c
;
655 if (data
!= NULL
&& md
->md_data
== NULL
)
668 _maildir_prime
= nextprime(msgCount
);
669 _maildir_table
= scalloc(_maildir_prime
, sizeof *_maildir_table
);
670 for (mp
= message
, i
= msgCount
; i
-- != 0; ++mp
)
671 mdlook(mp
->m_maildir_file
+ 4, mp
);
676 subdir_remove(char const *name
, char const *sub
)
679 int pathsize
, pathend
, namelen
, sublen
, n
;
685 namelen
= strlen(name
);
686 sublen
= strlen(sub
);
687 path
= smalloc(pathsize
= namelen
+ sublen
+ 30 +1);
688 memcpy(path
, name
, namelen
);
690 memcpy(path
+ namelen
+ 1, sub
, sublen
);
691 path
[namelen
+ sublen
+ 1] = '/';
692 path
[pathend
= namelen
+ sublen
+ 2] = '\0';
694 if ((dirp
= opendir(path
)) == NULL
) {
698 while ((dp
= readdir(dirp
)) != NULL
) {
699 if (dp
->d_name
[0] == '.' && (dp
->d_name
[1] == '\0' ||
700 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
702 if (dp
->d_name
[0] == '.')
704 n
= strlen(dp
->d_name
);
705 if (UICMP(32, pathend
+ n
+1, >, pathsize
))
706 path
= srealloc(path
, pathsize
= pathend
+ n
+ 30);
707 memcpy(path
+ pathend
, dp
->d_name
, n
+1);
708 if (unlink(path
) == -1) {
716 path
[pathend
] = '\0';
717 if (rmdir(path
) == -1) {
729 maildir_setfile(char const * volatile name
, enum fedit_mode fm
)
731 sighandler_type
volatile saveint
;
737 omsgCount
= msgCount
;
738 if (cwget(&cw
) == STOP
) {
739 n_alert(_("Cannot open current directory"));
743 if (!(fm
& FEDIT_NEWMAIL
) && !quit(FAL0
))
746 saveint
= safe_signal(SIGINT
, SIG_IGN
);
748 if (!(fm
& FEDIT_NEWMAIL
)) {
749 if (fm
& FEDIT_SYSBOX
)
750 n_pstate
&= ~n_PS_EDIT
;
752 n_pstate
|= n_PS_EDIT
;
762 mb
.mb_type
= MB_MAILDIR
;
765 if (chdir(name
) < 0) {
766 n_err(_("Cannot change directory to %s\n"), n_shexp_quote_cp(name
, FAL0
));
767 mb
.mb_type
= MB_VOID
;
771 safe_signal(SIGINT
, saveint
);
775 _maildir_table
= NULL
;
776 if (sigsetjmp(_maildir_jmp
, 1) == 0) {
777 if (fm
& FEDIT_NEWMAIL
)
779 if (saveint
!= SIG_IGN
)
780 safe_signal(SIGINT
, &__maildircatch
);
781 i
= _maildir_setfile1(name
, fm
, omsgCount
);
783 if ((fm
& FEDIT_NEWMAIL
) && _maildir_table
!= NULL
)
784 free(_maildir_table
);
786 safe_signal(SIGINT
, saveint
);
789 mb
.mb_type
= MB_VOID
;
794 if (cwret(&cw
) == STOP
)
795 n_panic(_("Cannot change back to current directory"));
799 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
&& msgCount
> omsgCount
) {
804 if (!(fm
& FEDIT_NEWMAIL
)) {
805 n_pstate
&= ~n_PS_SAW_COMMAND
;
806 n_pstate
|= n_PS_SETFILE_OPENED
;
809 if ((n_poption
& n_PO_EXISTONLY
) && !(n_poption
& n_PO_HEADERLIST
)) {
814 if (!(fm
& FEDIT_NEWMAIL
) && (fm
& FEDIT_SYSBOX
) && msgCount
== 0) {
815 if (mb
.mb_type
== MB_MAILDIR
/* XXX ?? */ && !ok_blook(emptystart
))
816 n_err(_("No mail at %s\n"), n_shexp_quote_cp(name
, FAL0
));
821 if ((fm
& FEDIT_NEWMAIL
) && msgCount
> omsgCount
)
822 newmailinfo(omsgCount
);
830 maildir_quit(bool_t hold_sigs_on
)
832 sighandler_type saveint
;
842 if (cwget(&cw
) == STOP
) {
843 n_alert(_("Cannot open current directory"));
847 saveint
= safe_signal(SIGINT
, SIG_IGN
);
849 if (chdir(mailname
) == -1) {
850 n_err(_("Cannot change directory to %s\n"),
851 n_shexp_quote_cp(mailname
, FAL0
));
853 safe_signal(SIGINT
, saveint
);
857 if (sigsetjmp(_maildir_jmp
, 1) == 0) {
858 if (saveint
!= SIG_IGN
)
859 safe_signal(SIGINT
, &__maildircatch_hold
);
863 safe_signal(SIGINT
, saveint
);
865 if (cwret(&cw
) == STOP
)
866 n_panic(_("Cannot change back to current directory"));
877 maildir_append(char const *name
, FILE *fp
, long offset
)
880 size_t bufsize
, buflen
, cnt
;
881 off_t off1
= -1, offs
;
884 enum {_NONE
= 0, _INHEAD
= 1<<0, _NLSEP
= 1<<1} state
;
888 if ((rv
= mkmaildir(name
)) != OKAY
)
891 buf
= smalloc(bufsize
= LINESIZE
); /* TODO line pool; signals */
894 offs
= offset
/* BSD will move due to O_APPEND! ftell(fp) */;
898 for (flag
= MNEW
, state
= _NLSEP
;;) {
899 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
902 ((state
& (_INHEAD
| _NLSEP
)) == _NLSEP
&&
903 is_head(buf
, buflen
, FAL0
))) {
904 if (off1
!= (off_t
)-1) {
905 if ((rv
= maildir_append1(name
, fp
, off1
, size
, flag
)) == STOP
)
908 if (fseek(fp
, offs
+ buflen
, SEEK_SET
) == -1) {
913 off1
= offs
+ buflen
;
925 if (buf
[0] == '\n') {
928 } else if (state
& _INHEAD
) {
929 if (!ascncasecmp(buf
, "status", 6)) {
931 while (whitechar(*lp
))
934 while (*++lp
!= '\0')
943 } else if (!ascncasecmp(buf
, "x-status", 8)) {
945 while (whitechar(*lp
))
948 while (*++lp
!= '\0')
974 maildir_remove(char const *name
)
979 if (subdir_remove(name
, "tmp") == STOP
||
980 subdir_remove(name
, "new") == STOP
||
981 subdir_remove(name
, "cur") == STOP
)
983 if (rmdir(name
) == -1) {