2 * S-nail - 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
51 * Mail -- a mail program
53 * Maildir folder support.
56 static struct mditem
{
57 struct message
*md_data
;
62 static sigjmp_buf maildirjmp
;
64 static int maildir_setfile1(const char *name
, int newmail
, int omsgCount
);
65 static int mdcmp(const void *a
, const void *b
);
66 static int subdir(const char *name
, const char *sub
, int newmail
);
67 static void cleantmp(const char *name
);
68 static void append(const char *name
, const char *sub
, const char *fn
);
69 static void readin(const char *name
, struct message
*m
);
70 static void maildir_update(void);
71 static void move(struct message
*m
);
72 static char *mkname(time_t t
, enum mflag f
, const char *pref
);
73 static void maildircatch(int s
);
74 static enum okay
maildir_append1(const char *name
, FILE *fp
, off_t off1
,
75 long size
, enum mflag flag
);
76 static enum okay
trycreate(const char *name
);
77 static enum okay
mkmaildir(const char *name
);
78 static struct message
*mdlook(const char *name
, struct message
*data
);
79 static void mktable(void);
80 static enum okay
subdir_remove(const char *name
, const char *sub
);
83 maildir_setfile(const char *name
, int newmail
, int isedit
)
85 sighandler_type saveint
;
87 int i
= -1, omsgCount
;
92 if (cwget(&cw
) == STOP
) {
93 fprintf(stderr
, "Fatal: Cannot open current directory\n");
98 saveint
= safe_signal(SIGINT
, SIG_IGN
);
99 if (chdir(name
) < 0) {
100 fprintf(stderr
, "Cannot change directory to \"%s\".\n", name
);
115 mb
.mb_type
= MB_MAILDIR
;
118 if (sigsetjmp(maildirjmp
, 1) == 0) {
121 if (saveint
!= SIG_IGN
)
122 safe_signal(SIGINT
, maildircatch
);
123 i
= maildir_setfile1(name
, newmail
, omsgCount
);
127 safe_signal(SIGINT
, saveint
);
129 mb
.mb_type
= MB_VOID
;
133 if (cwret(&cw
) == STOP
) {
134 fputs("Fatal: Cannot change back to current directory.\n",
140 if (newmail
&& mb
.mb_sorted
&& msgCount
> omsgCount
) {
146 if (!newmail
&& !edit
&& msgCount
== 0) {
147 if (mb
.mb_type
== MB_MAILDIR
&& value("emptystart") == NULL
)
148 fprintf(stderr
, "No mail at %s\n", name
);
151 if (newmail
&& msgCount
> omsgCount
)
152 newmailinfo(omsgCount
);
157 maildir_setfile1(const char *name
, int newmail
, int omsgCount
)
163 mb
.mb_perm
= Rflag
? 0 : MB_DELE
;
164 if ((i
= subdir(name
, "cur", newmail
)) != 0)
166 if ((i
= subdir(name
, "new", newmail
)) != 0)
168 append(name
, NULL
, NULL
);
169 for (i
= newmail
?omsgCount
:0; i
< msgCount
; i
++)
170 readin(name
, &message
[i
]);
172 if (msgCount
> omsgCount
)
173 qsort(&message
[omsgCount
],
174 msgCount
- omsgCount
,
175 sizeof *message
, mdcmp
);
178 qsort(message
, msgCount
, sizeof *message
, mdcmp
);
184 * In combination with the names from mkname(), this comparison function
185 * ensures that the order of messages in a maildir folder created by mailx
186 * remains always the same. In effect, if a mbox folder is transferred to
187 * a maildir folder by 'copy *', the order of the messages in mailx will
191 mdcmp(const void *a
, const void *b
)
195 if ((i
= ((struct message
*)a
)->m_time
-
196 ((struct message
*)b
)->m_time
) == 0)
197 i
= strcmp(&((struct message
*)a
)->m_maildir_file
[4],
198 &((struct message
*)b
)->m_maildir_file
[4]);
203 subdir(const char *name
, const char *sub
, int newmail
)
208 if ((dirfd
= opendir(sub
)) == NULL
) {
209 fprintf(stderr
, "Cannot open directory \"%s/%s\".\n",
213 if (access(sub
, W_OK
) < 0)
215 while ((dp
= readdir(dirfd
)) != NULL
) {
216 if (dp
->d_name
[0] == '.' &&
217 (dp
->d_name
[1] == '\0' ||
218 (dp
->d_name
[1] == '.' &&
219 dp
->d_name
[2] == '\0')))
221 if (dp
->d_name
[0] == '.')
223 if (!newmail
|| mdlook(dp
->d_name
, NULL
) == NULL
)
224 append(name
, sub
, dp
->d_name
);
231 cleantmp(const char *name
)
237 size_t fnsz
= 0, ssz
;
241 if ((dirfd
= opendir("tmp")) == NULL
)
244 while ((dp
= readdir(dirfd
)) != NULL
) {
245 if (dp
->d_name
[0] == '.' &&
246 (dp
->d_name
[1] == '\0' ||
247 (dp
->d_name
[1] == '.' &&
248 dp
->d_name
[2] == '\0')))
250 if (dp
->d_name
[0] == '.')
252 if ((ssz
= strlen(dp
->d_name
)) + 5 > fnsz
) {
254 fn
= smalloc(fnsz
= ssz
+ 40);
257 strcpy(&fn
[4], dp
->d_name
);
258 if (stat(fn
, &st
) < 0)
260 if (st
.st_atime
+ 36*3600 < now
)
268 append(const char *name
, const char *sub
, const char *fn
)
273 enum mflag f
= MUSED
|MNOFROM
|MNEWEST
;
279 if (strcmp(sub
, "new") == 0)
281 t
= strtol(fn
, &xp
, 10);
282 if ((cp
= strrchr(xp
, ',')) != NULL
&&
283 cp
> &xp
[2] && cp
[-1] == '2' && cp
[-2] == ':') {
305 if (msgCount
+ 1 >= msgspace
) {
306 const int chunk
= 64;
307 message
= srealloc(message
,
308 (msgspace
+= chunk
) * sizeof *message
);
309 memset(&message
[msgCount
], 0, chunk
* sizeof *message
);
311 if (fn
== NULL
|| sub
== NULL
)
313 m
= &message
[msgCount
++];
314 m
->m_maildir_file
= smalloc((sz
= strlen(sub
)) + strlen(fn
) + 2);
315 strcpy(m
->m_maildir_file
, sub
);
316 m
->m_maildir_file
[sz
] = '/';
317 strcpy(&m
->m_maildir_file
[sz
+1], fn
);
320 m
->m_maildir_hash
= ~pjw(fn
);
325 readin(const char *name
, struct message
*m
)
328 size_t bufsize
, buflen
, count
;
329 long size
= 0, lines
= 0;
334 if ((fp
= Fopen(m
->m_maildir_file
, "r")) == NULL
) {
335 fprintf(stderr
, "Cannot read \"%s/%s\" for message %d\n",
336 name
, m
->m_maildir_file
,
337 (int)(m
- &message
[0] + 1));
338 m
->m_flag
|= MHIDDEN
;
341 buf
= smalloc(bufsize
= LINESIZE
);
344 fseek(mb
.mb_otf
, 0L, SEEK_END
);
345 offset
= ftell(mb
.mb_otf
);
346 while (fgetline(&buf
, &bufsize
, &count
, &buflen
, fp
, 1) != NULL
) {
348 if (buf
[0] == 'F' && buf
[1] == 'r' && buf
[2] == 'o' &&
349 buf
[3] == 'm' && buf
[4] == ' ') {
350 putc('>', mb
.mb_otf
);
354 size
+= fwrite(bp
, 1, buflen
, mb
.mb_otf
);
355 emptyline
= *bp
== '\n';
358 putc('\n', mb
.mb_otf
);
364 m
->m_size
= m
->m_xsize
= size
;
365 m
->m_lines
= m
->m_xlines
= lines
;
366 m
->m_block
= mailx_blockof(offset
);
367 m
->m_offset
= mailx_offsetof(offset
);
375 sighandler_type saveint
;
379 if (cwget(&cw
) == STOP
) {
380 fprintf(stderr
, "Fatal: Cannot open current directory\n");
383 saveint
= safe_signal(SIGINT
, SIG_IGN
);
384 if (chdir(mailname
) < 0) {
385 fprintf(stderr
, "Cannot change directory to \"%s\".\n",
390 if (sigsetjmp(maildirjmp
, 1) == 0) {
391 if (saveint
!= SIG_IGN
)
392 safe_signal(SIGINT
, maildircatch
);
395 safe_signal(SIGINT
, saveint
);
396 if (cwret(&cw
) == STOP
) {
397 fputs("Fatal: Cannot change back to current directory.\n",
407 FILE *readstat
= NULL
;
409 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0;
414 if ((readstat
= Zopen(Tflag
, "w", NULL
)) == NULL
)
419 for (m
= &message
[0], c
= 0; m
< &message
[msgCount
]; m
++) {
420 if (m
->m_flag
& MBOX
)
424 if (makembox() == STOP
)
427 for (m
= &message
[0], gotcha
=0, held
=0; m
< &message
[msgCount
]; m
++) {
428 if (readstat
!= NULL
&& (m
->m_flag
& (MREAD
|MDELETED
)) != 0) {
430 if ((id
= hfield1("message-id", m
)) != NULL
||
431 (id
= hfieldX("article-id", m
)) != NULL
)
432 fprintf(readstat
, "%s\n", id
);
435 dodel
= m
->m_flag
& MDELETED
;
437 dodel
= !((m
->m_flag
&MPRESERVE
) ||
438 (m
->m_flag
&MTOUCH
) == 0);
440 if (unlink(m
->m_maildir_file
) < 0)
441 fprintf(stderr
, "Cannot delete file \"%s/%s\" "
443 mailname
, m
->m_maildir_file
,
444 (int)(m
- &message
[0] + 1));
448 if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
) ||
449 m
->m_flag
& (MNEW
|MBOXED
|MSAVED
|MSTATUS
|
459 bypass
: if (readstat
!= NULL
)
461 if ((gotcha
|| modflags
) && edit
) {
462 printf(catgets(catd
, CATSET
, 168, "\"%s\" "), mailname
);
463 printf(value("bsdcompat") || value("bsdmsgs") ?
464 catgets(catd
, CATSET
, 170, "complete\n") :
465 catgets(catd
, CATSET
, 212, "updated.\n"));
466 } else if (held
&& !edit
&& mb
.mb_perm
!= 0) {
468 printf(catgets(catd
, CATSET
, 155,
469 "Held 1 message in %s\n"), mailname
);
471 printf(catgets(catd
, CATSET
, 156,
472 "Held %d messages in %s\n"), held
, mailname
);
475 free
: for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
476 free(m
->m_maildir_file
);
480 move(struct message
*m
)
484 fn
= mkname(0, m
->m_flag
, &m
->m_maildir_file
[4]);
485 new = savecat("cur/", fn
);
486 if (strcmp(m
->m_maildir_file
, new) == 0)
488 if (link(m
->m_maildir_file
, new) < 0) {
489 fprintf(stderr
, "Cannot link \"%s/%s\" to \"%s/%s\": "
490 "message %d not touched.\n",
491 mailname
, m
->m_maildir_file
,
493 (int)(m
- &message
[0] + 1));
496 if (unlink(m
->m_maildir_file
) < 0)
497 fprintf(stderr
, "Cannot unlink \"%s/%s\".\n",
498 mailname
, m
->m_maildir_file
);
502 mkname(time_t t
, enum mflag f
, const char *pref
)
504 static unsigned long count
;
518 node
= srealloc(node
, size
+= 20);
521 node
[n
++] = '\\', node
[n
++] = '0',
522 node
[n
++] = '5', node
[n
++] = '7';
525 node
[n
++] = '\\', node
[n
++] = '0',
526 node
[n
++] = '7', node
[n
++] = '2';
533 size
= 60 + strlen(node
);
535 n
= snprintf(cp
, size
, "%lu.%06lu_%06lu.%s:2,",
537 (unsigned long)mypid
, ++count
, node
);
539 size
= (n
= strlen(pref
)) + 13;
542 for (i
= n
; i
> 3; i
--)
543 if (cp
[i
-1] == ',' && cp
[i
-2] == '2' &&
549 strcpy(&cp
[n
], ":2,");
572 siglongjmp(maildirjmp
, s
);
576 maildir_append(const char *name
, FILE *fp
)
579 size_t bufsize
, buflen
, count
;
580 off_t off1
= -1, offs
;
582 int flag
= MNEW
|MNEWEST
;
586 if (mkmaildir(name
) != OKAY
)
588 buf
= smalloc(bufsize
= LINESIZE
);
593 bp
= fgetline(&buf
, &bufsize
, &count
, &buflen
, fp
, 1);
594 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
595 if (off1
!= (off_t
)-1) {
596 ok
= maildir_append1(name
, fp
, off1
,
600 fseek(fp
, offs
+buflen
, SEEK_SET
);
602 off1
= offs
+ buflen
;
609 if (bp
&& buf
[0] == '\n')
611 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
613 while (whitechar(*lp
&0377))
616 while (*++lp
!= '\0')
625 } else if (bp
&& inhead
&&
626 ascncasecmp(buf
, "x-status", 8) == 0) {
628 while (whitechar(*lp
&0377))
631 while (*++lp
!= '\0')
644 } while (bp
!= NULL
);
650 maildir_append1(const char *name
, FILE *fp
, off_t off1
, long size
,
653 const int attempts
= 43200;
656 char *fn
, *tmp
, *new;
662 for (i
= 0; i
< attempts
; i
++) {
664 fn
= mkname(now
, flag
, NULL
);
665 tmp
= salloc(n
= strlen(name
) + strlen(fn
) + 6);
666 snprintf(tmp
, n
, "%s/tmp/%s", name
, fn
);
667 if (stat(tmp
, &st
) < 0 && errno
== ENOENT
)
673 "Cannot create unique file name in \"%s/tmp\".\n",
677 if ((op
= Fopen(tmp
, "w")) == NULL
) {
678 fprintf(stderr
, "Cannot write to \"%s\".\n", tmp
);
681 fseek(fp
, off1
, SEEK_SET
);
683 z
= size
> (long)sizeof buf
? (long)sizeof buf
: size
;
684 if ((n
= fread(buf
, 1, z
, fp
)) != z
||
685 (size_t)n
!= fwrite(buf
, 1, n
, op
)) {
686 fprintf(stderr
, "Error writing to \"%s\".\n", tmp
);
694 new = salloc(n
= strlen(name
) + strlen(fn
) + 6);
695 snprintf(new, n
, "%s/new/%s", name
, fn
);
696 if (link(tmp
, new) < 0) {
697 fprintf(stderr
, "Cannot link \"%s\" to \"%s\".\n", tmp
, new);
701 fprintf(stderr
, "Cannot unlink \"%s\".\n", tmp
);
706 trycreate(const char *name
)
710 if (stat(name
, &st
) == 0) {
711 if (!S_ISDIR(st
.st_mode
)) {
712 fprintf(stderr
, "\"%s\" is not a directory.\n", name
);
715 } else if (makedir(name
) != OKAY
) {
716 fprintf(stderr
, "Cannot create directory \"%s\".\n", name
);
719 imap_created_mailbox
++;
724 mkmaildir(const char *name
)
730 if (trycreate(name
) == OKAY
) {
731 np
= ac_alloc((sz
= strlen(name
)) + 5);
733 strcpy(&np
[sz
], "/tmp");
734 if (trycreate(np
) == OKAY
) {
735 strcpy(&np
[sz
], "/new");
736 if (trycreate(np
) == OKAY
) {
737 strcpy(&np
[sz
], "/cur");
738 if (trycreate(np
) == OKAY
)
747 static struct message
*
748 mdlook(const char *name
, struct message
*data
)
751 unsigned c
, h
, n
= 0;
753 if (data
&& data
->m_maildir_hash
)
754 h
= ~data
->m_maildir_hash
;
758 md
= &mdtable
[c
= h
];
759 while (md
->md_data
!= NULL
) {
760 if (strcmp(&md
->md_data
->m_maildir_file
[4], name
) == 0)
762 c
+= n
&1 ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
764 while (c
>= (unsigned)mdprime
)
765 c
-= (unsigned)mdprime
;
768 if (data
!= NULL
&& md
->md_data
== NULL
)
770 return md
->md_data
? md
->md_data
: NULL
;
778 mdprime
= nextprime(msgCount
);
779 mdtable
= scalloc(mdprime
, sizeof *mdtable
);
780 for (i
= 0; i
< msgCount
; i
++)
781 mdlook(&message
[i
].m_maildir_file
[4], &message
[i
]);
785 subdir_remove(const char *name
, const char *sub
)
788 int pathsize
, pathend
, namelen
, sublen
, n
;
792 namelen
= strlen(name
);
793 sublen
= strlen(sub
);
794 path
= smalloc(pathsize
= namelen
+ sublen
+ 30);
797 strcpy(&path
[namelen
+1], sub
);
798 path
[namelen
+sublen
+1] = '/';
799 path
[pathend
= namelen
+ sublen
+ 2] = '\0';
800 if ((dirfd
= opendir(path
)) == NULL
) {
805 while ((dp
= readdir(dirfd
)) != NULL
) {
806 if (dp
->d_name
[0] == '.' &&
807 (dp
->d_name
[1] == '\0' ||
808 (dp
->d_name
[1] == '.' &&
809 dp
->d_name
[2] == '\0')))
811 if (dp
->d_name
[0] == '.')
813 n
= strlen(dp
->d_name
);
814 if (pathend
+ n
+ 1 > pathsize
)
815 path
= srealloc(path
, pathsize
= pathend
+ n
+ 30);
816 strcpy(&path
[pathend
], dp
->d_name
);
817 if (unlink(path
) < 0) {
825 path
[pathend
] = '\0';
826 if (rmdir(path
) < 0) {
836 maildir_remove(const char *name
)
838 if (subdir_remove(name
, "tmp") == STOP
||
839 subdir_remove(name
, "new") == STOP
||
840 subdir_remove(name
, "cur") == STOP
)
842 if (rmdir(name
) < 0) {