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
50 static struct mditem
{
51 struct message
*md_data
;
56 static sigjmp_buf maildirjmp
;
58 /* Do some cleanup in the tmp/ subdir */
59 static void _cleantmp(void);
61 static int maildir_setfile1(const char *name
, int nmail
, int omsgCount
);
62 static int mdcmp(const void *a
, const void *b
);
63 static int subdir(const char *name
, const char *sub
, int nmail
);
64 static void append(const char *name
, const char *sub
, const char *fn
);
65 static void readin(const char *name
, struct message
*m
);
66 static void maildir_update(void);
67 static void move(struct message
*m
);
68 static char *mkname(time_t t
, enum mflag f
, const char *pref
);
69 static void maildircatch(int s
);
70 static enum okay
maildir_append1(const char *name
, FILE *fp
, off_t off1
,
71 long size
, enum mflag flag
);
72 static enum okay
trycreate(const char *name
);
73 static enum okay
mkmaildir(const char *name
);
74 static struct message
*mdlook(const char *name
, struct message
*data
);
75 static void mktable(void);
76 static enum okay
subdir_remove(const char *name
, const char *sub
);
87 if ((dirp
= opendir("tmp")) == NULL
)
91 while ((dp
= readdir(dirp
)) != NULL
) {
92 if (dp
->d_name
[0] == '.')
94 sstpcpy(sstpcpy(dep
, "tmp/"), dp
->d_name
);
95 if (stat(dep
, &st
) < 0)
97 if (st
.st_atime
+ 36*3600 < now
)
105 maildir_setfile(const char *name
, int nmail
, int isedit
)
107 sighandler_type saveint
;
109 int i
= -1, omsgCount
;
113 omsgCount
= msgCount
;
114 if (cwget(&cw
) == STOP
) {
115 fprintf(stderr
, "Fatal: Cannot open current directory\n");
120 saveint
= safe_signal(SIGINT
, SIG_IGN
);
121 if (chdir(name
) < 0) {
122 fprintf(stderr
, "Cannot change directory to \"%s\".\n", name
);
127 edit
= (isedit
!= 0);
137 mb
.mb_type
= MB_MAILDIR
;
140 if (sigsetjmp(maildirjmp
, 1) == 0) {
143 if (saveint
!= SIG_IGN
)
144 safe_signal(SIGINT
, maildircatch
);
145 i
= maildir_setfile1(name
, nmail
, omsgCount
);
149 safe_signal(SIGINT
, saveint
);
151 mb
.mb_type
= MB_VOID
;
155 if (cwret(&cw
) == STOP
) {
156 fputs("Fatal: Cannot change back to current directory.\n",
162 if (nmail
&& mb
.mb_sorted
&& msgCount
> omsgCount
) {
168 if (!nmail
&& !edit
&& msgCount
== 0) {
169 if (mb
.mb_type
== MB_MAILDIR
&& value("emptystart") == NULL
)
170 fprintf(stderr
, "No mail at %s\n", name
);
173 if (nmail
&& msgCount
> omsgCount
)
174 newmailinfo(omsgCount
);
179 maildir_setfile1(const char *name
, int nmail
, int omsgCount
)
185 mb
.mb_perm
= (options
& OPT_R_FLAG
) ? 0 : MB_DELE
;
186 if ((i
= subdir(name
, "cur", nmail
)) != 0)
188 if ((i
= subdir(name
, "new", nmail
)) != 0)
190 append(name
, NULL
, NULL
);
191 for (i
= nmail
?omsgCount
:0; i
< msgCount
; i
++)
192 readin(name
, &message
[i
]);
194 if (msgCount
> omsgCount
)
195 qsort(&message
[omsgCount
],
196 msgCount
- omsgCount
,
197 sizeof *message
, mdcmp
);
200 qsort(message
, msgCount
, sizeof *message
, mdcmp
);
206 * In combination with the names from mkname(), this comparison function
207 * ensures that the order of messages in a maildir folder created by mailx
208 * remains always the same. In effect, if a mbox folder is transferred to
209 * a maildir folder by 'copy *', the order of the messages in mailx will
213 mdcmp(const void *a
, const void *b
)
217 if ((i
= ((struct message
const*)a
)->m_time
-
218 ((struct message
const*)b
)->m_time
) == 0)
219 i
= strcmp(&((struct message
const*)a
)->m_maildir_file
[4],
220 &((struct message
const*)b
)->m_maildir_file
[4]);
225 subdir(const char *name
, const char *sub
, int nmail
)
230 if ((dirp
= opendir(sub
)) == NULL
) {
231 fprintf(stderr
, "Cannot open directory \"%s/%s\".\n",
235 if (access(sub
, W_OK
) < 0)
237 while ((dp
= readdir(dirp
)) != NULL
) {
238 if (dp
->d_name
[0] == '.' &&
239 (dp
->d_name
[1] == '\0' ||
240 (dp
->d_name
[1] == '.' &&
241 dp
->d_name
[2] == '\0')))
243 if (dp
->d_name
[0] == '.')
245 if (!nmail
|| mdlook(dp
->d_name
, NULL
) == NULL
)
246 append(name
, sub
, dp
->d_name
);
253 append(const char *name
, const char *sub
, const char *fn
)
258 enum mflag f
= MUSED
|MNOFROM
|MNEWEST
;
264 if (strcmp(sub
, "new") == 0)
266 t
= strtol(fn
, &xp
, 10);
267 if ((cp
= strrchr(xp
, ',')) != NULL
&&
268 cp
> &xp
[2] && cp
[-1] == '2' && cp
[-2] == ':') {
290 if (msgCount
+ 1 >= msgspace
) {
291 const int chunk
= 64;
292 message
= srealloc(message
,
293 (msgspace
+= chunk
) * sizeof *message
);
294 memset(&message
[msgCount
], 0, chunk
* sizeof *message
);
296 if (fn
== NULL
|| sub
== NULL
)
298 m
= &message
[msgCount
++];
300 m
->m_maildir_file
= smalloc((sz
= strlen(sub
)) + i
+ 2);
301 memcpy(m
->m_maildir_file
, sub
, sz
);
302 m
->m_maildir_file
[sz
] = '/';
303 memcpy(m
->m_maildir_file
+ sz
+ 1, fn
, i
+ 1);
306 m
->m_maildir_hash
= ~pjw(fn
);
311 readin(const char *name
, struct message
*m
)
314 size_t bufsize
, buflen
, cnt
;
315 long size
= 0, lines
= 0;
320 if ((fp
= Fopen(m
->m_maildir_file
, "r")) == NULL
) {
321 fprintf(stderr
, "Cannot read \"%s/%s\" for message %d\n",
322 name
, m
->m_maildir_file
,
323 (int)(m
- &message
[0] + 1));
324 m
->m_flag
|= MHIDDEN
;
327 buf
= smalloc(bufsize
= LINESIZE
);
330 fseek(mb
.mb_otf
, 0L, SEEK_END
);
331 offset
= ftell(mb
.mb_otf
);
332 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
338 if (is_head(buf
, buflen
)) {
339 putc('>', mb
.mb_otf
);
342 size
+= fwrite(buf
, 1, buflen
, mb
.mb_otf
);/*XXX err hdling*/
343 emptyline
= (*buf
== '\n');
347 putc('\n', mb
.mb_otf
);
353 m
->m_size
= m
->m_xsize
= size
;
354 m
->m_lines
= m
->m_xlines
= lines
;
355 m
->m_block
= mailx_blockof(offset
);
356 m
->m_offset
= mailx_offsetof(offset
);
364 sighandler_type saveint
;
368 if (cwget(&cw
) == STOP
) {
369 fprintf(stderr
, "Fatal: Cannot open current directory\n");
372 saveint
= safe_signal(SIGINT
, SIG_IGN
);
373 if (chdir(mailname
) < 0) {
374 fprintf(stderr
, "Cannot change directory to \"%s\".\n",
379 if (sigsetjmp(maildirjmp
, 1) == 0) {
380 if (saveint
!= SIG_IGN
)
381 safe_signal(SIGINT
, maildircatch
);
384 safe_signal(SIGINT
, saveint
);
385 if (cwret(&cw
) == STOP
) {
386 fputs("Fatal: Cannot change back to current directory.\n",
397 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0;
403 for (m
= &message
[0], c
= 0; m
< &message
[msgCount
]; m
++) {
404 if (m
->m_flag
& MBOX
)
408 if (makembox() == STOP
)
411 for (m
= &message
[0], gotcha
=0, held
=0; m
< &message
[msgCount
]; m
++) {
413 dodel
= m
->m_flag
& MDELETED
;
415 dodel
= !((m
->m_flag
&MPRESERVE
) ||
416 (m
->m_flag
&MTOUCH
) == 0);
418 if (unlink(m
->m_maildir_file
) < 0)
419 fprintf(stderr
, "Cannot delete file \"%s/%s\" "
421 mailname
, m
->m_maildir_file
,
422 (int)(m
- &message
[0] + 1));
426 if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
) ||
427 m
->m_flag
& (MNEW
|MBOXED
|MSAVED
|MSTATUS
|
438 if ((gotcha
|| modflags
) && edit
) {
439 printf(tr(168, "\"%s\" "), displayname
);
440 printf(value("bsdcompat") || value("bsdmsgs") ?
441 catgets(catd
, CATSET
, 170, "complete\n") :
442 catgets(catd
, CATSET
, 212, "updated.\n"));
443 } else if (held
&& !edit
&& mb
.mb_perm
!= 0) {
445 printf(tr(155, "Held 1 message in %s\n"), displayname
);
447 printf(tr(156, "Held %d messages in %s\n"), held
,
451 free
: for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
452 free(m
->m_maildir_file
);
456 move(struct message
*m
)
460 fn
= mkname(0, m
->m_flag
, &m
->m_maildir_file
[4]);
461 new = savecat("cur/", fn
);
462 if (strcmp(m
->m_maildir_file
, new) == 0)
464 if (link(m
->m_maildir_file
, new) < 0) {
465 fprintf(stderr
, "Cannot link \"%s/%s\" to \"%s/%s\": "
466 "message %d not touched.\n",
467 mailname
, m
->m_maildir_file
,
469 (int)(m
- &message
[0] + 1));
472 if (unlink(m
->m_maildir_file
) < 0)
473 fprintf(stderr
, "Cannot unlink \"%s/%s\".\n",
474 mailname
, m
->m_maildir_file
);
478 mkname(time_t t
, enum mflag f
, const char *pref
)
480 static unsigned long cnt
;
494 node
= srealloc(node
, size
+= 20);
497 node
[n
++] = '\\', node
[n
++] = '0',
498 node
[n
++] = '5', node
[n
++] = '7';
501 node
[n
++] = '\\', node
[n
++] = '0',
502 node
[n
++] = '7', node
[n
++] = '2';
509 size
= 60 + strlen(node
);
511 n
= snprintf(cp
, size
, "%lu.%06lu_%06lu.%s:2,",
513 (unsigned long)mypid
, ++cnt
, node
);
515 size
= (n
= strlen(pref
)) + 13;
517 memcpy(cp
, pref
, n
+ 1);
518 for (i
= n
; i
> 3; i
--)
519 if (cp
[i
-1] == ',' && cp
[i
-2] == '2' &&
525 memcpy(cp
+ n
, ":2,", 4);
548 siglongjmp(maildirjmp
, s
);
552 maildir_append(const char *name
, FILE *fp
)
555 size_t bufsize
, buflen
, cnt
;
556 off_t off1
= -1, offs
;
558 int flag
= MNEW
|MNEWEST
;
562 if (mkmaildir(name
) != OKAY
)
564 buf
= smalloc(bufsize
= LINESIZE
);
569 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
570 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
571 if (off1
!= (off_t
)-1) {
572 ok
= maildir_append1(name
, fp
, off1
,
576 if (fseek(fp
, offs
+buflen
, SEEK_SET
) < 0)
579 off1
= offs
+ buflen
;
586 if (bp
&& buf
[0] == '\n')
588 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
590 while (whitechar(*lp
&0377))
593 while (*++lp
!= '\0')
602 } else if (bp
&& inhead
&&
603 ascncasecmp(buf
, "x-status", 8) == 0) {
605 while (whitechar(*lp
&0377))
608 while (*++lp
!= '\0')
621 } while (bp
!= NULL
);
627 maildir_append1(const char *name
, FILE *fp
, off_t off1
, long size
,
630 int const attempts
= 43200;
631 char buf
[4096], *fn
, *tmp
, *new;
638 /* Create a unique temporary file */
639 for (i
= 0;; sleep(1), ++i
) {
641 fprintf(stderr
, tr(198,
642 "Can't create an unique file name in "
643 "\"%s/tmp\".\n"), name
);
648 fn
= mkname(now
, flag
, NULL
);
649 tmp
= salloc(n
= strlen(name
) + strlen(fn
) + 6);
650 snprintf(tmp
, n
, "%s/tmp/%s", name
, fn
);
651 if (stat(tmp
, &st
) >= 0 || errno
!= ENOENT
)
654 /* Use "wx" for O_EXCL */
655 if ((op
= Fopen(tmp
, "wx")) != NULL
)
659 if (fseek(fp
, off1
, SEEK_SET
) < 0)
662 z
= size
> (long)sizeof buf
? (long)sizeof buf
: size
;
663 if ((n
= fread(buf
, 1, z
, fp
)) != z
||
664 (size_t)n
!= fwrite(buf
, 1, n
, op
)) {
666 fprintf(stderr
, "Error writing to \"%s\".\n", tmp
);
675 new = salloc(n
= strlen(name
) + strlen(fn
) + 6);
676 snprintf(new, n
, "%s/new/%s", name
, fn
);
677 if (link(tmp
, new) < 0) {
678 fprintf(stderr
, "Cannot link \"%s\" to \"%s\".\n", tmp
, new);
682 fprintf(stderr
, "Cannot unlink \"%s\".\n", tmp
);
687 trycreate(const char *name
)
691 if (stat(name
, &st
) == 0) {
692 if (!S_ISDIR(st
.st_mode
)) {
693 fprintf(stderr
, "\"%s\" is not a directory.\n", name
);
696 } else if (makedir(name
) != OKAY
) {
697 fprintf(stderr
, "Cannot create directory \"%s\".\n", name
);
700 imap_created_mailbox
++;
705 mkmaildir(const char *name
)
711 if (trycreate(name
) == OKAY
) {
712 np
= ac_alloc((sz
= strlen(name
)) + 5);
713 memcpy(np
, name
, sz
);
714 memcpy(np
+ sz
, "/tmp", 5);
715 if (trycreate(np
) == OKAY
) {
716 strcpy(&np
[sz
], "/new");
717 if (trycreate(np
) == OKAY
) {
718 strcpy(&np
[sz
], "/cur");
719 if (trycreate(np
) == OKAY
)
728 static struct message
*
729 mdlook(const char *name
, struct message
*data
)
732 unsigned c
, h
, n
= 0;
734 if (data
&& data
->m_maildir_hash
)
735 h
= ~data
->m_maildir_hash
;
739 md
= &mdtable
[c
= h
];
740 while (md
->md_data
!= NULL
) {
741 if (strcmp(&md
->md_data
->m_maildir_file
[4], name
) == 0)
743 c
+= n
&1 ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
745 while (c
>= (unsigned)mdprime
)
746 c
-= (unsigned)mdprime
;
749 if (data
!= NULL
&& md
->md_data
== NULL
)
751 return md
->md_data
? md
->md_data
: NULL
;
759 mdprime
= nextprime(msgCount
);
760 mdtable
= scalloc(mdprime
, sizeof *mdtable
);
761 for (i
= 0; i
< msgCount
; i
++)
762 mdlook(&message
[i
].m_maildir_file
[4], &message
[i
]);
766 subdir_remove(const char *name
, const char *sub
)
769 int pathsize
, pathend
, namelen
, sublen
, n
;
773 namelen
= strlen(name
);
774 sublen
= strlen(sub
);
775 path
= smalloc(pathsize
= namelen
+ sublen
+ 30);
776 memcpy(path
, name
, namelen
);
778 memcpy(path
+ namelen
+ 1, sub
, sublen
);
779 path
[namelen
+sublen
+1] = '/';
780 path
[pathend
= namelen
+ sublen
+ 2] = '\0';
781 if ((dirp
= opendir(path
)) == NULL
) {
786 while ((dp
= readdir(dirp
)) != NULL
) {
787 if (dp
->d_name
[0] == '.' &&
788 (dp
->d_name
[1] == '\0' ||
789 (dp
->d_name
[1] == '.' &&
790 dp
->d_name
[2] == '\0')))
792 if (dp
->d_name
[0] == '.')
794 n
= strlen(dp
->d_name
);
795 if (pathend
+ n
+ 1 > pathsize
)
796 path
= srealloc(path
, pathsize
= pathend
+ n
+ 30);
797 memcpy(path
+ pathend
, dp
->d_name
, n
+ 1);
798 if (unlink(path
) < 0) {
806 path
[pathend
] = '\0';
807 if (rmdir(path
) < 0) {
817 maildir_remove(const char *name
)
819 if (subdir_remove(name
, "tmp") == STOP
||
820 subdir_remove(name
, "new") == STOP
||
821 subdir_remove(name
, "cur") == STOP
)
823 if (rmdir(name
) < 0) {