1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Maildir folder support.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
9 * Gunnar Ritter. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Gunnar Ritter
22 * and his contributors.
23 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static struct mditem
{
45 struct message
*md_data
;
50 static sigjmp_buf maildirjmp
;
52 /* Do some cleanup in the tmp/ subdir */
53 static void _cleantmp(void);
55 static int maildir_setfile1(const char *name
, int nmail
, int omsgCount
);
56 static int mdcmp(const void *a
, const void *b
);
57 static int subdir(const char *name
, const char *sub
, int nmail
);
58 static void append(const char *name
, const char *sub
, const char *fn
);
59 static void readin(const char *name
, struct message
*m
);
60 static void maildir_update(void);
61 static void move(struct message
*m
);
62 static char *mkname(time_t t
, enum mflag f
, const char *pref
);
63 static void maildircatch(int s
);
64 static enum okay
maildir_append1(const char *name
, FILE *fp
, off_t off1
,
65 long size
, enum mflag flag
);
66 static enum okay
trycreate(const char *name
);
67 static enum okay
mkmaildir(const char *name
);
68 static struct message
*mdlook(const char *name
, struct message
*data
);
69 static void mktable(void);
70 static enum okay
subdir_remove(const char *name
, const char *sub
);
81 if ((dirp
= opendir("tmp")) == NULL
)
85 while ((dp
= readdir(dirp
)) != NULL
) {
86 if (dp
->d_name
[0] == '.')
88 sstpcpy(sstpcpy(dep
, "tmp/"), dp
->d_name
);
89 if (stat(dep
, &st
) < 0)
91 if (st
.st_atime
+ 36*3600 < now
)
99 maildir_setfile(char const * volatile name
, int nmail
, int isedit
)
101 sighandler_type
volatile saveint
;
103 int i
= -1, omsgCount
;
107 omsgCount
= msgCount
;
108 if (cwget(&cw
) == STOP
) {
109 fprintf(stderr
, "Fatal: Cannot open current directory\n");
114 saveint
= safe_signal(SIGINT
, SIG_IGN
);
115 if (chdir(name
) < 0) {
116 fprintf(stderr
, "Cannot change directory to \"%s\".\n", name
);
121 edit
= (isedit
!= 0);
131 mb
.mb_type
= MB_MAILDIR
;
134 if (sigsetjmp(maildirjmp
, 1) == 0) {
137 if (saveint
!= SIG_IGN
)
138 safe_signal(SIGINT
, maildircatch
);
139 i
= maildir_setfile1(name
, nmail
, omsgCount
);
141 if (nmail
&& mdtable
!= NULL
)
143 safe_signal(SIGINT
, saveint
);
145 mb
.mb_type
= MB_VOID
;
149 if (cwret(&cw
) == STOP
) {
150 fputs("Fatal: Cannot change back to current directory.\n",
156 if (nmail
&& mb
.mb_sorted
&& msgCount
> omsgCount
) {
162 if (!nmail
&& !edit
&& msgCount
== 0) {
163 if (mb
.mb_type
== MB_MAILDIR
&& value("emptystart") == NULL
)
164 fprintf(stderr
, "No mail at %s\n", name
);
167 if (nmail
&& msgCount
> omsgCount
)
168 newmailinfo(omsgCount
);
173 maildir_setfile1(const char *name
, int nmail
, int omsgCount
)
179 mb
.mb_perm
= (options
& OPT_R_FLAG
) ? 0 : MB_DELE
;
180 if ((i
= subdir(name
, "cur", nmail
)) != 0)
182 if ((i
= subdir(name
, "new", nmail
)) != 0)
184 append(name
, NULL
, NULL
);
185 for (i
= nmail
?omsgCount
:0; i
< msgCount
; i
++)
186 readin(name
, &message
[i
]);
188 if (msgCount
> omsgCount
)
189 qsort(&message
[omsgCount
],
190 msgCount
- omsgCount
,
191 sizeof *message
, mdcmp
);
194 qsort(message
, msgCount
, sizeof *message
, mdcmp
);
200 * In combination with the names from mkname(), this comparison function
201 * ensures that the order of messages in a maildir folder created by mailx
202 * remains always the same. In effect, if a mbox folder is transferred to
203 * a maildir folder by 'copy *', the order of the messages in mailx will
207 mdcmp(const void *a
, const void *b
)
211 if ((i
= ((struct message
const*)a
)->m_time
-
212 ((struct message
const*)b
)->m_time
) == 0)
213 i
= strcmp(&((struct message
const*)a
)->m_maildir_file
[4],
214 &((struct message
const*)b
)->m_maildir_file
[4]);
219 subdir(const char *name
, const char *sub
, int nmail
)
224 if ((dirp
= opendir(sub
)) == NULL
) {
225 fprintf(stderr
, "Cannot open directory \"%s/%s\".\n",
229 if (access(sub
, W_OK
) < 0)
231 while ((dp
= readdir(dirp
)) != NULL
) {
232 if (dp
->d_name
[0] == '.' &&
233 (dp
->d_name
[1] == '\0' ||
234 (dp
->d_name
[1] == '.' &&
235 dp
->d_name
[2] == '\0')))
237 if (dp
->d_name
[0] == '.')
239 if (!nmail
|| mdlook(dp
->d_name
, NULL
) == NULL
)
240 append(name
, sub
, dp
->d_name
);
247 append(const char *name
, const char *sub
, const char *fn
)
252 enum mflag f
= MUSED
|MNOFROM
|MNEWEST
;
258 if (strcmp(sub
, "new") == 0)
260 t
= strtol(fn
, &xp
, 10);
261 if ((cp
= strrchr(xp
, ',')) != NULL
&&
262 cp
> &xp
[2] && cp
[-1] == '2' && cp
[-2] == ':') {
284 if (msgCount
+ 1 >= msgspace
) {
285 const int chunk
= 64;
286 message
= srealloc(message
,
287 (msgspace
+= chunk
) * sizeof *message
);
288 memset(&message
[msgCount
], 0, chunk
* sizeof *message
);
290 if (fn
== NULL
|| sub
== NULL
)
292 m
= &message
[msgCount
++];
294 m
->m_maildir_file
= smalloc((sz
= strlen(sub
)) + i
+ 2);
295 memcpy(m
->m_maildir_file
, sub
, sz
);
296 m
->m_maildir_file
[sz
] = '/';
297 memcpy(m
->m_maildir_file
+ sz
+ 1, fn
, i
+ 1);
300 m
->m_maildir_hash
= ~pjw(fn
);
305 readin(const char *name
, struct message
*m
)
308 size_t bufsize
, buflen
, cnt
;
309 long size
= 0, lines
= 0;
314 if ((fp
= Fopen(m
->m_maildir_file
, "r")) == NULL
) {
315 fprintf(stderr
, "Cannot read \"%s/%s\" for message %d\n",
316 name
, m
->m_maildir_file
,
317 (int)(m
- &message
[0] + 1));
318 m
->m_flag
|= MHIDDEN
;
321 buf
= smalloc(bufsize
= LINESIZE
);
324 fseek(mb
.mb_otf
, 0L, SEEK_END
);
325 offset
= ftell(mb
.mb_otf
);
326 while (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1) != NULL
) {
328 * Since we simply copy over data without doing any transfer
329 * encoding reclassification/adjustment we *have* to perform
330 * RFC 4155 compliant From_ quoting here
332 if (is_head(buf
, buflen
)) {
333 putc('>', mb
.mb_otf
);
336 size
+= fwrite(buf
, 1, buflen
, mb
.mb_otf
);/*XXX err hdling*/
337 emptyline
= (*buf
== '\n');
341 putc('\n', mb
.mb_otf
);
347 m
->m_size
= m
->m_xsize
= size
;
348 m
->m_lines
= m
->m_xlines
= lines
;
349 m
->m_block
= mailx_blockof(offset
);
350 m
->m_offset
= mailx_offsetof(offset
);
358 sighandler_type saveint
;
362 if (cwget(&cw
) == STOP
) {
363 fprintf(stderr
, "Fatal: Cannot open current directory\n");
366 saveint
= safe_signal(SIGINT
, SIG_IGN
);
367 if (chdir(mailname
) < 0) {
368 fprintf(stderr
, "Cannot change directory to \"%s\".\n",
373 if (sigsetjmp(maildirjmp
, 1) == 0) {
374 if (saveint
!= SIG_IGN
)
375 safe_signal(SIGINT
, maildircatch
);
378 safe_signal(SIGINT
, saveint
);
379 if (cwret(&cw
) == STOP
) {
380 fputs("Fatal: Cannot change back to current directory.\n",
391 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0;
397 for (m
= &message
[0], c
= 0; m
< &message
[msgCount
]; m
++) {
398 if (m
->m_flag
& MBOX
)
402 if (makembox() == STOP
)
405 for (m
= &message
[0], gotcha
=0, held
=0; m
< &message
[msgCount
]; m
++) {
407 dodel
= m
->m_flag
& MDELETED
;
409 dodel
= !((m
->m_flag
&MPRESERVE
) ||
410 (m
->m_flag
&MTOUCH
) == 0);
412 if (unlink(m
->m_maildir_file
) < 0)
413 fprintf(stderr
, "Cannot delete file \"%s/%s\" "
415 mailname
, m
->m_maildir_file
,
416 (int)(m
- &message
[0] + 1));
420 if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
) ||
421 m
->m_flag
& (MNEW
|MBOXED
|MSAVED
|MSTATUS
|
432 if ((gotcha
|| modflags
) && edit
) {
433 printf(tr(168, "\"%s\" "), displayname
);
434 printf((value("bsdcompat") || value("bsdmsgs"))
435 ? tr(170, "complete\n") : tr(212, "updated.\n"));
436 } else if (held
&& !edit
&& mb
.mb_perm
!= 0) {
438 printf(tr(155, "Held 1 message in %s\n"), displayname
);
440 printf(tr(156, "Held %d messages in %s\n"), held
,
444 free
: for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
445 free(m
->m_maildir_file
);
449 move(struct message
*m
)
453 fn
= mkname(0, m
->m_flag
, &m
->m_maildir_file
[4]);
454 new = savecat("cur/", fn
);
455 if (strcmp(m
->m_maildir_file
, new) == 0)
457 if (link(m
->m_maildir_file
, new) < 0) {
458 fprintf(stderr
, "Cannot link \"%s/%s\" to \"%s/%s\": "
459 "message %d not touched.\n",
460 mailname
, m
->m_maildir_file
,
462 (int)(m
- &message
[0] + 1));
465 if (unlink(m
->m_maildir_file
) < 0)
466 fprintf(stderr
, "Cannot unlink \"%s/%s\".\n",
467 mailname
, m
->m_maildir_file
);
471 mkname(time_t t
, enum mflag f
, const char *pref
)
473 static unsigned long cnt
;
487 node
= srealloc(node
, size
+= 20);
490 node
[n
++] = '\\', node
[n
++] = '0',
491 node
[n
++] = '5', node
[n
++] = '7';
494 node
[n
++] = '\\', node
[n
++] = '0',
495 node
[n
++] = '7', node
[n
++] = '2';
502 size
= 60 + strlen(node
);
504 n
= snprintf(cp
, size
, "%lu.%06lu_%06lu.%s:2,",
506 (unsigned long)mypid
, ++cnt
, node
);
508 size
= (n
= strlen(pref
)) + 13;
510 memcpy(cp
, pref
, n
+ 1);
511 for (i
= n
; i
> 3; i
--)
512 if (cp
[i
-1] == ',' && cp
[i
-2] == '2' &&
518 memcpy(cp
+ n
, ":2,", 4);
541 siglongjmp(maildirjmp
, s
);
545 maildir_append(const char *name
, FILE *fp
)
548 size_t bufsize
, buflen
, cnt
;
549 off_t off1
= -1, offs
;
551 int flag
= MNEW
|MNEWEST
;
555 if (mkmaildir(name
) != OKAY
)
557 buf
= smalloc(bufsize
= LINESIZE
);
562 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
563 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
564 if (off1
!= (off_t
)-1) {
565 ok
= maildir_append1(name
, fp
, off1
,
569 if (fseek(fp
, offs
+buflen
, SEEK_SET
) < 0)
572 off1
= offs
+ buflen
;
579 if (bp
&& buf
[0] == '\n')
581 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
583 while (whitechar(*lp
&0377))
586 while (*++lp
!= '\0')
595 } else if (bp
&& inhead
&&
596 ascncasecmp(buf
, "x-status", 8) == 0) {
598 while (whitechar(*lp
&0377))
601 while (*++lp
!= '\0')
614 } while (bp
!= NULL
);
620 maildir_append1(const char *name
, FILE *fp
, off_t off1
, long size
,
623 int const attempts
= 43200;
624 char buf
[4096], *fn
, *tmp
, *new;
631 /* Create a unique temporary file */
632 for (i
= 0;; sleep(1), ++i
) {
634 fprintf(stderr
, tr(198,
635 "Can't create an unique file name in "
636 "\"%s/tmp\".\n"), name
);
641 fn
= mkname(now
, flag
, NULL
);
642 tmp
= salloc(n
= strlen(name
) + strlen(fn
) + 6);
643 snprintf(tmp
, n
, "%s/tmp/%s", name
, fn
);
644 if (stat(tmp
, &st
) >= 0 || errno
!= ENOENT
)
647 /* Use "wx" for O_EXCL */
648 if ((op
= Fopen(tmp
, "wx")) != NULL
)
652 if (fseek(fp
, off1
, SEEK_SET
) < 0)
655 z
= size
> (long)sizeof buf
? (long)sizeof buf
: size
;
656 if ((n
= fread(buf
, 1, z
, fp
)) != z
||
657 (size_t)n
!= fwrite(buf
, 1, n
, op
)) {
659 fprintf(stderr
, "Error writing to \"%s\".\n", tmp
);
668 new = salloc(n
= strlen(name
) + strlen(fn
) + 6);
669 snprintf(new, n
, "%s/new/%s", name
, fn
);
670 if (link(tmp
, new) < 0) {
671 fprintf(stderr
, "Cannot link \"%s\" to \"%s\".\n", tmp
, new);
675 fprintf(stderr
, "Cannot unlink \"%s\".\n", tmp
);
680 trycreate(const char *name
)
684 if (stat(name
, &st
) == 0) {
685 if (!S_ISDIR(st
.st_mode
)) {
686 fprintf(stderr
, "\"%s\" is not a directory.\n", name
);
689 } else if (makedir(name
) != OKAY
) {
690 fprintf(stderr
, "Cannot create directory \"%s\".\n", name
);
693 imap_created_mailbox
++;
698 mkmaildir(const char *name
)
704 if (trycreate(name
) == OKAY
) {
705 np
= ac_alloc((sz
= strlen(name
)) + 5);
706 memcpy(np
, name
, sz
);
707 memcpy(np
+ sz
, "/tmp", 5);
708 if (trycreate(np
) == OKAY
) {
709 strcpy(&np
[sz
], "/new");
710 if (trycreate(np
) == OKAY
) {
711 strcpy(&np
[sz
], "/cur");
712 if (trycreate(np
) == OKAY
)
721 static struct message
*
722 mdlook(const char *name
, struct message
*data
)
725 unsigned c
, h
, n
= 0;
727 if (data
&& data
->m_maildir_hash
)
728 h
= ~data
->m_maildir_hash
;
732 md
= &mdtable
[c
= h
];
733 while (md
->md_data
!= NULL
) {
734 if (strcmp(&md
->md_data
->m_maildir_file
[4], name
) == 0)
736 c
+= n
&1 ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
738 while (c
>= (unsigned)mdprime
)
739 c
-= (unsigned)mdprime
;
742 if (data
!= NULL
&& md
->md_data
== NULL
)
744 return md
->md_data
? md
->md_data
: NULL
;
752 mdprime
= nextprime(msgCount
);
753 mdtable
= scalloc(mdprime
, sizeof *mdtable
);
754 for (i
= 0; i
< msgCount
; i
++)
755 mdlook(&message
[i
].m_maildir_file
[4], &message
[i
]);
759 subdir_remove(const char *name
, const char *sub
)
762 int pathsize
, pathend
, namelen
, sublen
, n
;
766 namelen
= strlen(name
);
767 sublen
= strlen(sub
);
768 path
= smalloc(pathsize
= namelen
+ sublen
+ 30);
769 memcpy(path
, name
, namelen
);
771 memcpy(path
+ namelen
+ 1, sub
, sublen
);
772 path
[namelen
+sublen
+1] = '/';
773 path
[pathend
= namelen
+ sublen
+ 2] = '\0';
774 if ((dirp
= opendir(path
)) == NULL
) {
779 while ((dp
= readdir(dirp
)) != NULL
) {
780 if (dp
->d_name
[0] == '.' &&
781 (dp
->d_name
[1] == '\0' ||
782 (dp
->d_name
[1] == '.' &&
783 dp
->d_name
[2] == '\0')))
785 if (dp
->d_name
[0] == '.')
787 n
= strlen(dp
->d_name
);
788 if (pathend
+ n
+ 1 > pathsize
)
789 path
= srealloc(path
, pathsize
= pathend
+ n
+ 30);
790 memcpy(path
+ pathend
, dp
->d_name
, n
+ 1);
791 if (unlink(path
) < 0) {
799 path
[pathend
] = '\0';
800 if (rmdir(path
) < 0) {
810 maildir_remove(const char *name
)
812 if (subdir_remove(name
, "tmp") == STOP
||
813 subdir_remove(name
, "new") == STOP
||
814 subdir_remove(name
, "cur") == STOP
)
816 if (rmdir(name
) < 0) {