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
40 #ifndef HAVE_AMALGAMATION
47 struct message
*md_data
;
51 static struct mditem
*_maildir_table
;
52 static long _maildir_prime
;
53 static sigjmp_buf _maildir_jmp
;
55 /* Do some cleanup in the tmp/ subdir */
56 static void _cleantmp(void);
58 static int maildir_setfile1(const char *name
, int nmail
, int omsgCount
);
59 static int mdcmp(const void *a
, const void *b
);
60 static int subdir(const char *name
, const char *sub
, int nmail
);
61 static void _maildir_append(const char *name
, const char *sub
, const char *fn
);
62 static void readin(const char *name
, struct message
*m
);
63 static void maildir_update(void);
64 static void move(struct message
*m
);
65 static char *mkname(time_t t
, enum mflag f
, const char *pref
);
66 static void maildircatch(int s
);
67 static enum okay
maildir_append1(const char *name
, FILE *fp
, off_t off1
,
68 long size
, enum mflag flag
);
69 static enum okay
trycreate(const char *name
);
70 static enum okay
mkmaildir(const char *name
);
71 static struct message
*mdlook(const char *name
, struct message
*data
);
72 static void mktable(void);
73 static enum okay
subdir_remove(const char *name
, const char *sub
);
84 if ((dirp
= opendir("tmp")) == NULL
)
88 while ((dp
= readdir(dirp
)) != NULL
) {
89 if (dp
->d_name
[0] == '.')
91 sstpcpy(sstpcpy(dep
, "tmp/"), dp
->d_name
);
92 if (stat(dep
, &st
) < 0)
94 if (st
.st_atime
+ 36*3600 < now
)
102 maildir_setfile(char const * volatile name
, int nmail
, int isedit
)
104 sighandler_type
volatile saveint
;
106 int i
= -1, omsgCount
;
110 omsgCount
= msgCount
;
111 if (cwget(&cw
) == STOP
) {
112 fprintf(stderr
, "Fatal: Cannot open current directory\n");
117 saveint
= safe_signal(SIGINT
, SIG_IGN
);
118 if (chdir(name
) < 0) {
119 fprintf(stderr
, "Cannot change directory to \"%s\".\n", name
);
124 edit
= (isedit
!= 0);
134 mb
.mb_type
= MB_MAILDIR
;
136 _maildir_table
= NULL
;
137 if (sigsetjmp(_maildir_jmp
, 1) == 0) {
140 if (saveint
!= SIG_IGN
)
141 safe_signal(SIGINT
, maildircatch
);
142 i
= maildir_setfile1(name
, nmail
, omsgCount
);
144 if (nmail
&& _maildir_table
!= NULL
)
145 free(_maildir_table
);
146 safe_signal(SIGINT
, saveint
);
148 mb
.mb_type
= MB_VOID
;
152 if (cwret(&cw
) == STOP
) {
153 fputs("Fatal: Cannot change back to current directory.\n",
159 if (nmail
&& mb
.mb_sorted
&& msgCount
> omsgCount
) {
165 if (!nmail
&& !edit
&& msgCount
== 0) {
166 if (mb
.mb_type
== MB_MAILDIR
&& value("emptystart") == NULL
)
167 fprintf(stderr
, "No mail at %s\n", name
);
170 if (nmail
&& msgCount
> omsgCount
)
171 newmailinfo(omsgCount
);
176 maildir_setfile1(const char *name
, int nmail
, int omsgCount
)
182 mb
.mb_perm
= (options
& OPT_R_FLAG
) ? 0 : MB_DELE
;
183 if ((i
= subdir(name
, "cur", nmail
)) != 0)
185 if ((i
= subdir(name
, "new", nmail
)) != 0)
187 _maildir_append(name
, NULL
, NULL
);
188 for (i
= nmail
?omsgCount
:0; i
< msgCount
; i
++)
189 readin(name
, &message
[i
]);
191 if (msgCount
> omsgCount
)
192 qsort(&message
[omsgCount
],
193 msgCount
- omsgCount
,
194 sizeof *message
, mdcmp
);
197 qsort(message
, msgCount
, sizeof *message
, mdcmp
);
203 * In combination with the names from mkname(), this comparison function
204 * ensures that the order of messages in a maildir folder created by mailx
205 * remains always the same. In effect, if a mbox folder is transferred to
206 * a maildir folder by 'copy *', the order of the messages in mailx will
210 mdcmp(const void *a
, const void *b
)
214 if ((i
= ((struct message
const*)a
)->m_time
-
215 ((struct message
const*)b
)->m_time
) == 0)
216 i
= strcmp(&((struct message
const*)a
)->m_maildir_file
[4],
217 &((struct message
const*)b
)->m_maildir_file
[4]);
222 subdir(const char *name
, const char *sub
, int nmail
)
227 if ((dirp
= opendir(sub
)) == NULL
) {
228 fprintf(stderr
, "Cannot open directory \"%s/%s\".\n",
232 if (access(sub
, W_OK
) < 0)
234 while ((dp
= readdir(dirp
)) != NULL
) {
235 if (dp
->d_name
[0] == '.' &&
236 (dp
->d_name
[1] == '\0' ||
237 (dp
->d_name
[1] == '.' &&
238 dp
->d_name
[2] == '\0')))
240 if (dp
->d_name
[0] == '.')
242 if (!nmail
|| mdlook(dp
->d_name
, NULL
) == NULL
)
243 _maildir_append(name
, sub
, dp
->d_name
);
250 _maildir_append(const char *name
, const char *sub
, const char *fn
)
255 enum mflag f
= MUSED
|MNOFROM
|MNEWEST
;
261 if (strcmp(sub
, "new") == 0)
263 t
= strtol(fn
, &xp
, 10);
264 if ((cp
= strrchr(xp
, ',')) != NULL
&&
265 cp
> &xp
[2] && cp
[-1] == '2' && cp
[-2] == ':') {
287 if (msgCount
+ 1 >= msgspace
) {
288 const int chunk
= 64;
289 message
= srealloc(message
,
290 (msgspace
+= chunk
) * sizeof *message
);
291 memset(&message
[msgCount
], 0, chunk
* sizeof *message
);
293 if (fn
== NULL
|| sub
== NULL
)
295 m
= &message
[msgCount
++];
297 m
->m_maildir_file
= smalloc((sz
= strlen(sub
)) + i
+ 2);
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
= ~pjw(fn
);
308 readin(const char *name
, struct message
*m
)
311 size_t bufsize
, buflen
, cnt
;
312 long size
= 0, lines
= 0;
317 if ((fp
= Fopen(m
->m_maildir_file
, "r")) == NULL
) {
318 fprintf(stderr
, "Cannot read \"%s/%s\" for message %d\n",
319 name
, m
->m_maildir_file
,
320 (int)(m
- &message
[0] + 1));
321 m
->m_flag
|= MHIDDEN
;
324 buf
= smalloc(bufsize
= LINESIZE
);
327 fseek(mb
.mb_otf
, 0L, SEEK_END
);
328 offset
= ftell(mb
.mb_otf
);
329 while (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1) != NULL
) {
331 * Since we simply copy over data without doing any transfer
332 * encoding reclassification/adjustment we *have* to perform
333 * RFC 4155 compliant From_ quoting here
335 if (is_head(buf
, buflen
)) {
336 putc('>', mb
.mb_otf
);
339 size
+= fwrite(buf
, 1, buflen
, mb
.mb_otf
);/*XXX err hdling*/
340 emptyline
= (*buf
== '\n');
344 putc('\n', mb
.mb_otf
);
350 m
->m_size
= m
->m_xsize
= size
;
351 m
->m_lines
= m
->m_xlines
= lines
;
352 m
->m_block
= mailx_blockof(offset
);
353 m
->m_offset
= mailx_offsetof(offset
);
361 sighandler_type saveint
;
365 if (cwget(&cw
) == STOP
) {
366 fprintf(stderr
, "Fatal: Cannot open current directory\n");
369 saveint
= safe_signal(SIGINT
, SIG_IGN
);
370 if (chdir(mailname
) < 0) {
371 fprintf(stderr
, "Cannot change directory to \"%s\".\n",
376 if (sigsetjmp(_maildir_jmp
, 1) == 0) {
377 if (saveint
!= SIG_IGN
)
378 safe_signal(SIGINT
, maildircatch
);
381 safe_signal(SIGINT
, saveint
);
382 if (cwret(&cw
) == STOP
) {
383 fputs("Fatal: Cannot change back to current directory.\n",
394 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0;
400 for (m
= &message
[0], c
= 0; m
< &message
[msgCount
]; m
++) {
401 if (m
->m_flag
& MBOX
)
405 if (makembox() == STOP
)
408 for (m
= &message
[0], gotcha
=0, held
=0; m
< &message
[msgCount
]; m
++) {
410 dodel
= m
->m_flag
& MDELETED
;
412 dodel
= !((m
->m_flag
&MPRESERVE
) ||
413 (m
->m_flag
&MTOUCH
) == 0);
415 if (unlink(m
->m_maildir_file
) < 0)
416 fprintf(stderr
, "Cannot delete file \"%s/%s\" "
418 mailname
, m
->m_maildir_file
,
419 (int)(m
- &message
[0] + 1));
423 if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
) ||
424 m
->m_flag
& (MNEW
|MBOXED
|MSAVED
|MSTATUS
|
435 if ((gotcha
|| modflags
) && edit
) {
436 printf(tr(168, "\"%s\" "), displayname
);
437 printf((value("bsdcompat") || value("bsdmsgs"))
438 ? tr(170, "complete\n") : tr(212, "updated.\n"));
439 } else if (held
&& !edit
&& mb
.mb_perm
!= 0) {
441 printf(tr(155, "Held 1 message in %s\n"), displayname
);
443 printf(tr(156, "Held %d messages in %s\n"), held
,
447 free
: for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
448 free(m
->m_maildir_file
);
452 move(struct message
*m
)
456 fn
= mkname(0, m
->m_flag
, &m
->m_maildir_file
[4]);
457 new = savecat("cur/", fn
);
458 if (strcmp(m
->m_maildir_file
, new) == 0)
460 if (link(m
->m_maildir_file
, new) < 0) {
461 fprintf(stderr
, "Cannot link \"%s/%s\" to \"%s/%s\": "
462 "message %d not touched.\n",
463 mailname
, m
->m_maildir_file
,
465 (int)(m
- &message
[0] + 1));
468 if (unlink(m
->m_maildir_file
) < 0)
469 fprintf(stderr
, "Cannot unlink \"%s/%s\".\n",
470 mailname
, m
->m_maildir_file
);
474 mkname(time_t t
, enum mflag f
, const char *pref
)
476 static unsigned long cnt
;
489 if (UICMP(32, n
, <, size
+ 8))
490 node
= srealloc(node
, size
+= 20);
493 node
[n
++] = '\\', node
[n
++] = '0',
494 node
[n
++] = '5', node
[n
++] = '7';
497 node
[n
++] = '\\', node
[n
++] = '0',
498 node
[n
++] = '7', node
[n
++] = '2';
505 size
= 60 + strlen(node
);
507 n
= snprintf(cp
, size
, "%lu.%06lu_%06lu.%s:2,",
509 (unsigned long)mypid
, ++cnt
, node
);
511 size
= (n
= strlen(pref
)) + 13;
513 memcpy(cp
, pref
, n
+ 1);
514 for (i
= n
; i
> 3; i
--)
515 if (cp
[i
-1] == ',' && cp
[i
-2] == '2' &&
521 memcpy(cp
+ n
, ":2,", 4);
544 siglongjmp(_maildir_jmp
, s
);
548 maildir_append(const char *name
, FILE *fp
)
551 size_t bufsize
, buflen
, cnt
;
552 off_t off1
= -1, offs
;
554 int flag
= MNEW
|MNEWEST
;
558 if (mkmaildir(name
) != OKAY
)
560 buf
= smalloc(bufsize
= LINESIZE
);
565 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
566 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
567 if (off1
!= (off_t
)-1) {
568 ok
= maildir_append1(name
, fp
, off1
,
572 if (fseek(fp
, offs
+buflen
, SEEK_SET
) < 0)
575 off1
= offs
+ buflen
;
582 if (bp
&& buf
[0] == '\n')
584 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
586 while (whitechar(*lp
&0377))
589 while (*++lp
!= '\0')
598 } else if (bp
&& inhead
&&
599 ascncasecmp(buf
, "x-status", 8) == 0) {
601 while (whitechar(*lp
&0377))
604 while (*++lp
!= '\0')
617 } while (bp
!= NULL
);
623 maildir_append1(const char *name
, FILE *fp
, off_t off1
, long size
,
626 int const attempts
= 43200;
627 char buf
[4096], *fn
, *tmp
, *new;
634 /* Create a unique temporary file */
635 for (i
= 0;; sleep(1), ++i
) {
637 fprintf(stderr
, tr(198,
638 "Can't create an unique file name in "
639 "\"%s/tmp\".\n"), name
);
644 fn
= mkname(now
, flag
, NULL
);
645 tmp
= salloc(n
= strlen(name
) + strlen(fn
) + 6);
646 snprintf(tmp
, n
, "%s/tmp/%s", name
, fn
);
647 if (stat(tmp
, &st
) >= 0 || errno
!= ENOENT
)
650 /* Use "wx" for O_EXCL */
651 if ((op
= Fopen(tmp
, "wx")) != NULL
)
655 if (fseek(fp
, off1
, SEEK_SET
) < 0)
658 z
= size
> (long)sizeof buf
? (long)sizeof buf
: size
;
659 if ((n
= fread(buf
, 1, z
, fp
)) != z
||
660 (size_t)n
!= fwrite(buf
, 1, n
, op
)) {
662 fprintf(stderr
, "Error writing to \"%s\".\n", tmp
);
671 new = salloc(n
= strlen(name
) + strlen(fn
) + 6);
672 snprintf(new, n
, "%s/new/%s", name
, fn
);
673 if (link(tmp
, new) < 0) {
674 fprintf(stderr
, "Cannot link \"%s\" to \"%s\".\n", tmp
, new);
678 fprintf(stderr
, "Cannot unlink \"%s\".\n", tmp
);
683 trycreate(const char *name
)
687 if (stat(name
, &st
) == 0) {
688 if (!S_ISDIR(st
.st_mode
)) {
689 fprintf(stderr
, "\"%s\" is not a directory.\n", name
);
692 } else if (makedir(name
) != OKAY
) {
693 fprintf(stderr
, "Cannot create directory \"%s\".\n", name
);
696 imap_created_mailbox
++;
701 mkmaildir(const char *name
)
707 if (trycreate(name
) == OKAY
) {
708 np
= ac_alloc((sz
= strlen(name
)) + 5);
709 memcpy(np
, name
, sz
);
710 memcpy(np
+ sz
, "/tmp", 5);
711 if (trycreate(np
) == OKAY
) {
712 strcpy(&np
[sz
], "/new");
713 if (trycreate(np
) == OKAY
) {
714 strcpy(&np
[sz
], "/cur");
715 if (trycreate(np
) == OKAY
)
724 static struct message
*
725 mdlook(const char *name
, struct message
*data
)
728 unsigned c
, h
, n
= 0;
730 if (data
&& data
->m_maildir_hash
)
731 h
= ~data
->m_maildir_hash
;
735 md
= &_maildir_table
[c
= h
];
736 while (md
->md_data
!= NULL
) {
737 if (strcmp(&md
->md_data
->m_maildir_file
[4], name
) == 0)
739 c
+= n
&1 ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
741 while (c
>= (unsigned)_maildir_prime
)
742 c
-= (unsigned)_maildir_prime
;
743 md
= &_maildir_table
[c
];
745 if (data
!= NULL
&& md
->md_data
== NULL
)
747 return md
->md_data
? md
->md_data
: NULL
;
755 _maildir_prime
= nextprime(msgCount
);
756 _maildir_table
= scalloc(_maildir_prime
, sizeof *_maildir_table
);
757 for (i
= 0; i
< msgCount
; i
++)
758 mdlook(&message
[i
].m_maildir_file
[4], &message
[i
]);
762 subdir_remove(const char *name
, const char *sub
)
765 int pathsize
, pathend
, namelen
, sublen
, n
;
769 namelen
= strlen(name
);
770 sublen
= strlen(sub
);
771 path
= smalloc(pathsize
= namelen
+ sublen
+ 30);
772 memcpy(path
, name
, namelen
);
774 memcpy(path
+ namelen
+ 1, sub
, sublen
);
775 path
[namelen
+sublen
+1] = '/';
776 path
[pathend
= namelen
+ sublen
+ 2] = '\0';
777 if ((dirp
= opendir(path
)) == NULL
) {
782 while ((dp
= readdir(dirp
)) != NULL
) {
783 if (dp
->d_name
[0] == '.' &&
784 (dp
->d_name
[1] == '\0' ||
785 (dp
->d_name
[1] == '.' &&
786 dp
->d_name
[2] == '\0')))
788 if (dp
->d_name
[0] == '.')
790 n
= strlen(dp
->d_name
);
791 if (UICMP(32, pathend
+ n
+ 1, >, pathsize
))
792 path
= srealloc(path
, pathsize
= pathend
+ n
+ 30);
793 memcpy(path
+ pathend
, dp
->d_name
, n
+ 1);
794 if (unlink(path
) < 0) {
802 path
[pathend
] = '\0';
803 if (rmdir(path
) < 0) {
813 maildir_remove(const char *name
)
815 if (subdir_remove(name
, "tmp") == STOP
||
816 subdir_remove(name
, "new") == STOP
||
817 subdir_remove(name
, "cur") == STOP
)
819 if (rmdir(name
) < 0) {