2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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
42 static char sccsid
[] = "@(#)maildir.c 1.20 (gritter) 12/28/06";
57 * Mail -- a mail program
59 * Maildir folder support.
62 static struct mditem
{
63 struct message
*md_data
;
68 static sigjmp_buf maildirjmp
;
70 static int maildir_setfile1(const char *name
, int newmail
, int omsgCount
);
71 static int mdcmp(const void *a
, const void *b
);
72 static int subdir(const char *name
, const char *sub
, int newmail
);
73 static void cleantmp(const char *name
);
74 static void append(const char *name
, const char *sub
, const char *fn
);
75 static void readin(const char *name
, struct message
*m
);
76 static void maildir_update(void);
77 static void move(struct message
*m
);
78 static char *mkname(time_t t
, enum mflag f
, const char *pref
);
79 static void maildircatch(int s
);
80 static enum okay
maildir_append1(const char *name
, FILE *fp
, off_t off1
,
81 long size
, enum mflag flag
);
82 static enum okay
trycreate(const char *name
);
83 static enum okay
mkmaildir(const char *name
);
84 static struct message
*mdlook(const char *name
, struct message
*data
);
85 static void mktable(void);
86 static enum okay
subdir_remove(const char *name
, const char *sub
);
89 maildir_setfile(const char *name
, int newmail
, int isedit
)
91 sighandler_type saveint
;
93 int i
= -1, omsgCount
;
98 if (cwget(&cw
) == STOP
) {
99 fprintf(stderr
, "Fatal: Cannot open current directory\n");
104 saveint
= safe_signal(SIGINT
, SIG_IGN
);
105 if (chdir(name
) < 0) {
106 fprintf(stderr
, "Cannot change directory to \"%s\".\n", name
);
121 mb
.mb_type
= MB_MAILDIR
;
124 if (sigsetjmp(maildirjmp
, 1) == 0) {
127 if (saveint
!= SIG_IGN
)
128 safe_signal(SIGINT
, maildircatch
);
129 i
= maildir_setfile1(name
, newmail
, omsgCount
);
133 safe_signal(SIGINT
, saveint
);
135 mb
.mb_type
= MB_VOID
;
139 if (cwret(&cw
) == STOP
) {
140 fputs("Fatal: Cannot change back to current directory.\n",
146 if (newmail
&& mb
.mb_sorted
&& msgCount
> omsgCount
) {
152 if (!newmail
&& !edit
&& msgCount
== 0) {
153 if (mb
.mb_type
== MB_MAILDIR
&& value("emptystart") == NULL
)
154 fprintf(stderr
, "No mail at %s\n", name
);
157 if (newmail
&& msgCount
> omsgCount
)
158 newmailinfo(omsgCount
);
163 maildir_setfile1(const char *name
, int newmail
, int omsgCount
)
169 mb
.mb_perm
= Rflag
? 0 : MB_DELE
;
170 if ((i
= subdir(name
, "cur", newmail
)) != 0)
172 if ((i
= subdir(name
, "new", newmail
)) != 0)
174 append(name
, NULL
, NULL
);
175 for (i
= newmail
?omsgCount
:0; i
< msgCount
; i
++)
176 readin(name
, &message
[i
]);
178 if (msgCount
> omsgCount
)
179 qsort(&message
[omsgCount
],
180 msgCount
- omsgCount
,
181 sizeof *message
, mdcmp
);
184 qsort(message
, msgCount
, sizeof *message
, mdcmp
);
190 * In combination with the names from mkname(), this comparison function
191 * ensures that the order of messages in a maildir folder created by mailx
192 * remains always the same. In effect, if a mbox folder is transferred to
193 * a maildir folder by 'copy *', the order of the messages in mailx will
197 mdcmp(const void *a
, const void *b
)
201 if ((i
= ((struct message
*)a
)->m_time
-
202 ((struct message
*)b
)->m_time
) == 0)
203 i
= strcmp(&((struct message
*)a
)->m_maildir_file
[4],
204 &((struct message
*)b
)->m_maildir_file
[4]);
209 subdir(const char *name
, const char *sub
, int newmail
)
214 if ((dirfd
= opendir(sub
)) == NULL
) {
215 fprintf(stderr
, "Cannot open directory \"%s/%s\".\n",
219 if (access(sub
, W_OK
) < 0)
221 while ((dp
= readdir(dirfd
)) != NULL
) {
222 if (dp
->d_name
[0] == '.' &&
223 (dp
->d_name
[1] == '\0' ||
224 (dp
->d_name
[1] == '.' &&
225 dp
->d_name
[2] == '\0')))
227 if (dp
->d_name
[0] == '.')
229 if (!newmail
|| mdlook(dp
->d_name
, NULL
) == NULL
)
230 append(name
, sub
, dp
->d_name
);
237 cleantmp(const char *name
)
243 size_t fnsz
= 0, ssz
;
247 if ((dirfd
= opendir("tmp")) == NULL
)
250 while ((dp
= readdir(dirfd
)) != NULL
) {
251 if (dp
->d_name
[0] == '.' &&
252 (dp
->d_name
[1] == '\0' ||
253 (dp
->d_name
[1] == '.' &&
254 dp
->d_name
[2] == '\0')))
256 if (dp
->d_name
[0] == '.')
258 if ((ssz
= strlen(dp
->d_name
)) + 5 > fnsz
) {
260 fn
= smalloc(fnsz
= ssz
+ 40);
263 strcpy(&fn
[4], dp
->d_name
);
264 if (stat(fn
, &st
) < 0)
266 if (st
.st_atime
+ 36*3600 < now
)
274 append(const char *name
, const char *sub
, const char *fn
)
279 enum mflag f
= MUSED
|MNOFROM
|MNEWEST
;
285 if (strcmp(sub
, "new") == 0)
287 t
= strtol(fn
, &xp
, 10);
288 if ((cp
= strrchr(xp
, ',')) != NULL
&&
289 cp
> &xp
[2] && cp
[-1] == '2' && cp
[-2] == ':') {
311 if (msgCount
+ 1 >= msgspace
) {
312 const int chunk
= 64;
313 message
= srealloc(message
,
314 (msgspace
+= chunk
) * sizeof *message
);
315 memset(&message
[msgCount
], 0, chunk
* sizeof *message
);
317 if (fn
== NULL
|| sub
== NULL
)
319 m
= &message
[msgCount
++];
320 m
->m_maildir_file
= smalloc((sz
= strlen(sub
)) + strlen(fn
) + 2);
321 strcpy(m
->m_maildir_file
, sub
);
322 m
->m_maildir_file
[sz
] = '/';
323 strcpy(&m
->m_maildir_file
[sz
+1], fn
);
326 m
->m_maildir_hash
= ~pjw(fn
);
331 readin(const char *name
, struct message
*m
)
334 size_t bufsize
, buflen
, count
;
335 long size
= 0, lines
= 0;
340 if ((fp
= Fopen(m
->m_maildir_file
, "r")) == NULL
) {
341 fprintf(stderr
, "Cannot read \"%s/%s\" for message %d\n",
342 name
, m
->m_maildir_file
,
343 (int)(m
- &message
[0] + 1));
344 m
->m_flag
|= MHIDDEN
;
347 buf
= smalloc(bufsize
= LINESIZE
);
350 fseek(mb
.mb_otf
, 0L, SEEK_END
);
351 offset
= ftell(mb
.mb_otf
);
352 while (fgetline(&buf
, &bufsize
, &count
, &buflen
, fp
, 1) != NULL
) {
354 if (buf
[0] == 'F' && buf
[1] == 'r' && buf
[2] == 'o' &&
355 buf
[3] == 'm' && buf
[4] == ' ') {
356 putc('>', mb
.mb_otf
);
360 size
+= fwrite(bp
, 1, buflen
, mb
.mb_otf
);
361 emptyline
= *bp
== '\n';
364 putc('\n', mb
.mb_otf
);
370 m
->m_size
= m
->m_xsize
= size
;
371 m
->m_lines
= m
->m_xlines
= lines
;
372 m
->m_block
= mailx_blockof(offset
);
373 m
->m_offset
= mailx_offsetof(offset
);
381 sighandler_type saveint
;
385 if (cwget(&cw
) == STOP
) {
386 fprintf(stderr
, "Fatal: Cannot open current directory\n");
389 saveint
= safe_signal(SIGINT
, SIG_IGN
);
390 if (chdir(mailname
) < 0) {
391 fprintf(stderr
, "Cannot change directory to \"%s\".\n",
396 if (sigsetjmp(maildirjmp
, 1) == 0) {
397 if (saveint
!= SIG_IGN
)
398 safe_signal(SIGINT
, maildircatch
);
401 safe_signal(SIGINT
, saveint
);
402 if (cwret(&cw
) == STOP
) {
403 fputs("Fatal: Cannot change back to current directory.\n",
413 FILE *readstat
= NULL
;
415 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0;
420 if ((readstat
= Zopen(Tflag
, "w", NULL
)) == NULL
)
425 for (m
= &message
[0], c
= 0; m
< &message
[msgCount
]; m
++) {
426 if (m
->m_flag
& MBOX
)
430 if (makembox() == STOP
)
433 for (m
= &message
[0], gotcha
=0, held
=0; m
< &message
[msgCount
]; m
++) {
434 if (readstat
!= NULL
&& (m
->m_flag
& (MREAD
|MDELETED
)) != 0) {
436 if ((id
= hfield("message-id", m
)) != NULL
||
437 (id
= hfield("article-id", m
)) != NULL
)
438 fprintf(readstat
, "%s\n", id
);
441 dodel
= m
->m_flag
& MDELETED
;
443 dodel
= !((m
->m_flag
&MPRESERVE
) ||
444 (m
->m_flag
&MTOUCH
) == 0);
446 if (unlink(m
->m_maildir_file
) < 0)
447 fprintf(stderr
, "Cannot delete file \"%s/%s\" "
449 mailname
, m
->m_maildir_file
,
450 (int)(m
- &message
[0] + 1));
454 if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
) ||
455 m
->m_flag
& (MNEW
|MBOXED
|MSAVED
|MSTATUS
|
465 bypass
: if (readstat
!= NULL
)
467 if ((gotcha
|| modflags
) && edit
) {
468 printf(catgets(catd
, CATSET
, 168, "\"%s\" "), mailname
);
469 printf(value("bsdcompat") || value("bsdmsgs") ?
470 catgets(catd
, CATSET
, 170, "complete\n") :
471 catgets(catd
, CATSET
, 212, "updated.\n"));
472 } else if (held
&& !edit
&& mb
.mb_perm
!= 0) {
474 printf(catgets(catd
, CATSET
, 155,
475 "Held 1 message in %s\n"), mailname
);
477 printf(catgets(catd
, CATSET
, 156,
478 "Held %d messages in %s\n"), held
, mailname
);
481 free
: for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
482 free(m
->m_maildir_file
);
486 move(struct message
*m
)
490 fn
= mkname(0, m
->m_flag
, &m
->m_maildir_file
[4]);
491 new = savecat("cur/", fn
);
492 if (strcmp(m
->m_maildir_file
, new) == 0)
494 if (link(m
->m_maildir_file
, new) < 0) {
495 fprintf(stderr
, "Cannot link \"%s/%s\" to \"%s/%s\": "
496 "message %d not touched.\n",
497 mailname
, m
->m_maildir_file
,
499 (int)(m
- &message
[0] + 1));
502 if (unlink(m
->m_maildir_file
) < 0)
503 fprintf(stderr
, "Cannot unlink \"%s/%s\".\n",
504 mailname
, m
->m_maildir_file
);
508 mkname(time_t t
, enum mflag f
, const char *pref
)
510 static unsigned long count
;
524 node
= srealloc(node
, size
+= 20);
527 node
[n
++] = '\\', node
[n
++] = '0',
528 node
[n
++] = '5', node
[n
++] = '7';
531 node
[n
++] = '\\', node
[n
++] = '0',
532 node
[n
++] = '7', node
[n
++] = '2';
539 size
= 60 + strlen(node
);
541 n
= snprintf(cp
, size
, "%lu.%06lu_%06lu.%s:2,",
543 (unsigned long)mypid
, ++count
, node
);
545 size
= (n
= strlen(pref
)) + 13;
548 for (i
= n
; i
> 3; i
--)
549 if (cp
[i
-1] == ',' && cp
[i
-2] == '2' &&
555 strcpy(&cp
[n
], ":2,");
578 siglongjmp(maildirjmp
, s
);
582 maildir_append(const char *name
, FILE *fp
)
585 size_t bufsize
, buflen
, count
;
586 off_t off1
= -1, offs
;
588 int flag
= MNEW
|MNEWEST
;
592 if (mkmaildir(name
) != OKAY
)
594 buf
= smalloc(bufsize
= LINESIZE
);
599 bp
= fgetline(&buf
, &bufsize
, &count
, &buflen
, fp
, 1);
600 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
601 if (off1
!= (off_t
)-1) {
602 ok
= maildir_append1(name
, fp
, off1
,
606 fseek(fp
, offs
+buflen
, SEEK_SET
);
608 off1
= offs
+ buflen
;
615 if (bp
&& buf
[0] == '\n')
617 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
619 while (whitechar(*lp
&0377))
622 while (*++lp
!= '\0')
631 } else if (bp
&& inhead
&&
632 ascncasecmp(buf
, "x-status", 8) == 0) {
634 while (whitechar(*lp
&0377))
637 while (*++lp
!= '\0')
650 } while (bp
!= NULL
);
656 maildir_append1(const char *name
, FILE *fp
, off_t off1
, long size
,
659 const int attempts
= 43200;
662 char *fn
, *tmp
, *new;
668 for (i
= 0; i
< attempts
; i
++) {
670 fn
= mkname(now
, flag
, NULL
);
671 tmp
= salloc(n
= strlen(name
) + strlen(fn
) + 6);
672 snprintf(tmp
, n
, "%s/tmp/%s", name
, fn
);
673 if (stat(tmp
, &st
) < 0 && errno
== ENOENT
)
679 "Cannot create unique file name in \"%s/tmp\".\n",
683 if ((op
= Fopen(tmp
, "w")) == NULL
) {
684 fprintf(stderr
, "Cannot write to \"%s\".\n", tmp
);
687 fseek(fp
, off1
, SEEK_SET
);
689 z
= size
> (long)sizeof buf
? (long)sizeof buf
: size
;
690 if ((n
= fread(buf
, 1, z
, fp
)) != z
||
691 (size_t)n
!= fwrite(buf
, 1, n
, op
)) {
692 fprintf(stderr
, "Error writing to \"%s\".\n", tmp
);
700 new = salloc(n
= strlen(name
) + strlen(fn
) + 6);
701 snprintf(new, n
, "%s/new/%s", name
, fn
);
702 if (link(tmp
, new) < 0) {
703 fprintf(stderr
, "Cannot link \"%s\" to \"%s\".\n", tmp
, new);
707 fprintf(stderr
, "Cannot unlink \"%s\".\n", tmp
);
712 trycreate(const char *name
)
716 if (stat(name
, &st
) == 0) {
717 if (!S_ISDIR(st
.st_mode
)) {
718 fprintf(stderr
, "\"%s\" is not a directory.\n", name
);
721 } else if (makedir(name
) != OKAY
) {
722 fprintf(stderr
, "Cannot create directory \"%s\".\n", name
);
725 imap_created_mailbox
++;
730 mkmaildir(const char *name
)
736 if (trycreate(name
) == OKAY
) {
737 np
= ac_alloc((sz
= strlen(name
)) + 5);
739 strcpy(&np
[sz
], "/tmp");
740 if (trycreate(np
) == OKAY
) {
741 strcpy(&np
[sz
], "/new");
742 if (trycreate(np
) == OKAY
) {
743 strcpy(&np
[sz
], "/cur");
744 if (trycreate(np
) == OKAY
)
753 static struct message
*
754 mdlook(const char *name
, struct message
*data
)
757 unsigned c
, h
, n
= 0;
759 if (data
&& data
->m_maildir_hash
)
760 h
= ~data
->m_maildir_hash
;
764 md
= &mdtable
[c
= h
];
765 while (md
->md_data
!= NULL
) {
766 if (strcmp(&md
->md_data
->m_maildir_file
[4], name
) == 0)
768 c
+= n
&1 ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
770 while (c
>= (unsigned)mdprime
)
771 c
-= (unsigned)mdprime
;
774 if (data
!= NULL
&& md
->md_data
== NULL
)
776 return md
->md_data
? md
->md_data
: NULL
;
784 mdprime
= nextprime(msgCount
);
785 mdtable
= scalloc(mdprime
, sizeof *mdtable
);
786 for (i
= 0; i
< msgCount
; i
++)
787 mdlook(&message
[i
].m_maildir_file
[4], &message
[i
]);
791 subdir_remove(const char *name
, const char *sub
)
794 int pathsize
, pathend
, namelen
, sublen
, n
;
798 namelen
= strlen(name
);
799 sublen
= strlen(sub
);
800 path
= smalloc(pathsize
= namelen
+ sublen
+ 30);
803 strcpy(&path
[namelen
+1], sub
);
804 path
[namelen
+sublen
+1] = '/';
805 path
[pathend
= namelen
+ sublen
+ 2] = '\0';
806 if ((dirfd
= opendir(path
)) == NULL
) {
811 while ((dp
= readdir(dirfd
)) != NULL
) {
812 if (dp
->d_name
[0] == '.' &&
813 (dp
->d_name
[1] == '\0' ||
814 (dp
->d_name
[1] == '.' &&
815 dp
->d_name
[2] == '\0')))
817 if (dp
->d_name
[0] == '.')
819 n
= strlen(dp
->d_name
);
820 if (pathend
+ n
+ 1 > pathsize
)
821 path
= srealloc(path
, pathsize
= pathend
+ n
+ 30);
822 strcpy(&path
[pathend
], dp
->d_name
);
823 if (unlink(path
) < 0) {
831 path
[pathend
] = '\0';
832 if (rmdir(path
) < 0) {
842 maildir_remove(const char *name
)
844 if (subdir_remove(name
, "tmp") == STOP
||
845 subdir_remove(name
, "new") == STOP
||
846 subdir_remove(name
, "cur") == STOP
)
848 if (rmdir(name
) < 0) {