Drop requirement of BYTE_ORDER knowledge ++ for OPT_CROSS_BUILD..
[s-mailx.git] / maildir.c
blob4369cd45972c4fed3f2abf1834b5f99ff328b28f
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Maildir folder support. FIXME rewrite - why do we chdir(2)??
3 *@ FIXME indeed - my S-Postman Python (!) is faster dealing with maildir!!
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
7 */
8 /*
9 * Copyright (c) 2004
10 * Gunnar Ritter. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Gunnar Ritter
23 * and his contributors.
24 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
40 #undef n_FILE
41 #define n_FILE maildir
43 #ifndef HAVE_AMALGAMATION
44 # include "nail.h"
45 #endif
47 #include <dirent.h>
49 struct mditem {
50 struct message *md_data;
51 unsigned md_hash;
54 static struct mditem *_maildir_table;
55 static ui32_t _maildir_prime;
56 static sigjmp_buf _maildir_jmp;
58 static void __maildircatch(int s);
59 static void __maildircatch_hold(int s);
61 static unsigned a_maildir_hash(char const *cp);
63 /* Do some cleanup in the tmp/ subdir */
64 static void _cleantmp(void);
66 static int _maildir_setfile1(char const *name, enum fedit_mode fm,
67 int omsgCount);
69 /* In combination with the names from mkname(), this comparison function
70 * ensures that the order of messages in a maildir folder created by mailx
71 * remains always the same. In effect, if a mbox folder is transferred to
72 * a maildir folder by 'copy *', the message order wont' change */
73 static int mdcmp(void const *a, void const *b);
75 static int _maildir_subdir(char const *name, char const *sub,
76 enum fedit_mode fm);
78 static void _maildir_append(char const *name, char const *sub,
79 char const *fn);
81 static void readin(char const *name, struct message *m);
83 static void maildir_update(void);
85 static void _maildir_move(struct message *m);
87 static char * mkname(time_t t, enum mflag f, char const *pref);
89 static enum okay maildir_append1(char const *name, FILE *fp, off_t off1,
90 long size, enum mflag flag);
92 static enum okay trycreate(char const *name);
94 static enum okay mkmaildir(char const *name);
96 static struct message * mdlook(char const *name, struct message *data);
98 static void mktable(void);
100 static enum okay subdir_remove(char const *name, char const *sub);
102 static void
103 __maildircatch(int s)
105 NYD_X; /* Signal handler */
106 siglongjmp(_maildir_jmp, s);
109 static void
110 __maildircatch_hold(int s)
112 NYD_X; /* Signal handler */
113 n_UNUSED(s);
114 /* TODO no STDIO in signal handler, no _() tr's -- pre-translate interrupt
115 * TODO globally; */
116 n_err_sighdl(_("\nImportant operation in progress: "
117 "interrupt again to forcefully abort\n"));
118 safe_signal(SIGINT, &__maildircatch);
121 static unsigned
122 a_maildir_hash(char const *cp) /* TODO obsolete that -> torek_hash */
124 unsigned h = 0, g;
125 NYD_ENTER;
127 cp--;
128 while (*++cp) {
129 h = (h << 4 & 0xffffffff) + (*cp&0377);
130 if ((g = h & 0xf0000000) != 0) {
131 h = h ^ g >> 24;
132 h = h ^ g;
135 NYD_LEAVE;
136 return h;
139 static void
140 _cleantmp(void)
142 char dep[PATH_MAX];
143 struct stat st;
144 time_t now;
145 DIR *dirp;
146 struct dirent *dp;
147 NYD_ENTER;
149 if ((dirp = opendir("tmp")) == NULL)
150 goto jleave;
152 now = n_time_epoch();
153 while ((dp = readdir(dirp)) != NULL) {
154 if (dp->d_name[0] == '.')
155 continue;
156 sstpcpy(sstpcpy(dep, "tmp/"), dp->d_name);
157 if (stat(dep, &st) == -1)
158 continue;
159 if (st.st_atime + 36*3600 < now)
160 unlink(dep);
162 closedir(dirp);
163 jleave:
164 NYD_LEAVE;
167 static int
168 _maildir_setfile1(char const *name, enum fedit_mode fm, int omsgCount)
170 int i;
171 NYD_ENTER;
173 if (!(fm & FEDIT_NEWMAIL))
174 _cleantmp();
176 mb.mb_perm = ((n_poption & n_PO_R_FLAG) || (fm & FEDIT_RDONLY))
177 ? 0 : MB_DELE;
178 if ((i = _maildir_subdir(name, "cur", fm)) != 0)
179 goto jleave;
180 if ((i = _maildir_subdir(name, "new", fm)) != 0)
181 goto jleave;
182 _maildir_append(name, NULL, NULL);
184 srelax_hold();
185 for (i = ((fm & FEDIT_NEWMAIL) ? omsgCount : 0); i < msgCount; ++i) {
186 readin(name, message + i);
187 srelax();
189 srelax_rele();
191 if (fm & FEDIT_NEWMAIL) {
192 if (msgCount > omsgCount)
193 qsort(&message[omsgCount], msgCount - omsgCount, sizeof *message,
194 &mdcmp);
195 } else if (msgCount)
196 qsort(message, msgCount, sizeof *message, &mdcmp);
197 i = msgCount;
198 jleave:
199 NYD_LEAVE;
200 return i;
203 static int
204 mdcmp(void const *a, void const *b)
206 struct message const *mpa = a, *mpb = b;
207 long i;
208 NYD_ENTER;
210 if ((i = mpa->m_time - mpb->m_time) == 0)
211 i = strcmp(mpa->m_maildir_file + 4, mpb->m_maildir_file + 4);
212 NYD_LEAVE;
213 return i;
216 static int
217 _maildir_subdir(char const *name, char const *sub, enum fedit_mode fm)
219 DIR *dirp;
220 struct dirent *dp;
221 int rv;
222 NYD_ENTER;
224 if ((dirp = opendir(sub)) == NULL) {
225 n_err(_("Cannot open directory %s\n"),
226 n_shexp_quote_cp(savecatsep(name, '/', sub), FAL0));
227 rv = -1;
228 goto jleave;
230 if (access(sub, W_OK) == -1)
231 mb.mb_perm = 0;
232 while ((dp = readdir(dirp)) != NULL) {
233 if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
234 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
235 continue;
236 if (dp->d_name[0] == '.')
237 continue;
238 if (!(fm & FEDIT_NEWMAIL) || mdlook(dp->d_name, NULL) == NULL)
239 _maildir_append(name, sub, dp->d_name);
241 closedir(dirp);
242 rv = 0;
243 jleave:
244 NYD_LEAVE;
245 return rv;
248 static void
249 _maildir_append(char const *name, char const *sub, char const *fn)
251 struct message *m;
252 size_t sz, i;
253 time_t t = 0;
254 enum mflag f = MUSED | MNOFROM | MNEWEST;
255 char const *cp;
256 char *xp;
257 NYD_ENTER;
258 n_UNUSED(name);
260 if (fn != NULL && sub != NULL) {
261 if (!strcmp(sub, "new"))
262 f |= MNEW;
263 t = strtol(fn, &xp, 10);
264 if ((cp = strrchr(xp, ',')) != NULL && PTRCMP(cp, >, xp + 2) &&
265 cp[-1] == '2' && cp[-2] == ':') {
266 while (*++cp != '\0') {
267 switch (*cp) {
268 case 'F':
269 f |= MFLAGGED;
270 break;
271 case 'R':
272 f |= MANSWERED;
273 break;
274 case 'S':
275 f |= MREAD;
276 break;
277 case 'T':
278 f |= MDELETED;
279 break;
280 case 'D':
281 f |= MDRAFT;
282 break;
288 /* Ensure room (and a NULLified last entry) */
289 ++msgCount;
290 message_append(NULL);
291 --msgCount;
293 if (fn == NULL || sub == NULL)
294 goto jleave;
296 m = message + msgCount++;
297 i = strlen(fn);
298 m->m_maildir_file = smalloc((sz = strlen(sub)) + i + 1 +1);
299 memcpy(m->m_maildir_file, sub, sz);
300 m->m_maildir_file[sz] = '/';
301 memcpy(m->m_maildir_file + sz + 1, fn, i +1);
302 m->m_time = t;
303 m->m_flag = f;
304 m->m_maildir_hash = ~a_maildir_hash(fn);
305 jleave:
306 NYD_LEAVE;
307 return;
310 static void
311 readin(char const *name, struct message *m)
313 char *buf;
314 size_t bufsize, buflen, cnt;
315 long size = 0, lines = 0;
316 off_t offset;
317 FILE *fp;
318 int emptyline = 0;
319 NYD_ENTER;
321 if ((fp = Fopen(m->m_maildir_file, "r")) == NULL) {
322 n_err(_("Cannot read %s for message %lu\n"),
323 n_shexp_quote_cp(savecatsep(name, '/', m->m_maildir_file), FAL0),
324 (ul_i)PTR2SIZE(m - message + 1));
325 m->m_flag |= MHIDDEN;
326 goto jleave;
329 cnt = fsize(fp);
330 fseek(mb.mb_otf, 0L, SEEK_END);
331 offset = ftell(mb.mb_otf);
332 buf = smalloc(bufsize = LINESIZE);
333 buflen = 0;
334 while (fgetline(&buf, &bufsize, &cnt, &buflen, fp, 1) != NULL) {
335 /* Since we simply copy over data without doing any transfer
336 * encoding reclassification/adjustment we *have* to perform
337 * RFC 4155 compliant From_ quoting here */
338 if (emptyline && is_head(buf, buflen, FAL0)) {
339 putc('>', mb.mb_otf);
340 ++size;
342 size += fwrite(buf, 1, buflen, mb.mb_otf);/*XXX err hdling*/
343 emptyline = (*buf == '\n');
344 ++lines;
346 free(buf);
347 if (!emptyline) {
348 putc('\n', mb.mb_otf);
349 ++lines;
350 ++size;
353 Fclose(fp);
354 fflush(mb.mb_otf);
355 m->m_size = m->m_xsize = size;
356 m->m_lines = m->m_xlines = lines;
357 m->m_block = mailx_blockof(offset);
358 m->m_offset = mailx_offsetof(offset);
359 substdate(m);
360 jleave:
361 NYD_LEAVE;
364 static void
365 maildir_update(void)
367 struct message *m;
368 int dodel, c, gotcha = 0, held = 0, modflags = 0;
369 NYD_ENTER;
371 if (mb.mb_perm == 0)
372 goto jfree;
374 if (!(n_pstate & n_PS_EDIT)) {
375 holdbits();
376 for (m = message, c = 0; PTRCMP(m, <, message + msgCount); ++m) {
377 if (m->m_flag & MBOX)
378 c++;
380 if (c > 0)
381 if (makembox() == STOP)
382 goto jbypass;
384 for (m = message, gotcha = 0, held = 0; PTRCMP(m, <, message + msgCount);
385 ++m) {
386 if (n_pstate & n_PS_EDIT)
387 dodel = m->m_flag & MDELETED;
388 else
389 dodel = !((m->m_flag & MPRESERVE) || !(m->m_flag & MTOUCH));
390 if (dodel) {
391 if (unlink(m->m_maildir_file) < 0)
392 n_err(_("Cannot delete file %s for message %lu\n"),
393 n_shexp_quote_cp(savecatsep(mailname, '/', m->m_maildir_file),
394 FAL0), (ul_i)PTR2SIZE(m - message + 1));
395 else
396 ++gotcha;
397 } else {
398 if ((m->m_flag & (MREAD | MSTATUS)) == (MREAD | MSTATUS) ||
399 (m->m_flag & (MNEW | MBOXED | MSAVED | MSTATUS | MFLAG |
400 MUNFLAG | MANSWER | MUNANSWER | MDRAFT | MUNDRAFT))) {
401 _maildir_move(m);
402 ++modflags;
404 ++held;
407 jbypass:
408 if ((gotcha || modflags) && (n_pstate & n_PS_EDIT)) {
409 fprintf(n_stdout, "%s %s\n",
410 n_shexp_quote_cp(displayname, FAL0),
411 ((ok_blook(bsdcompat) || ok_blook(bsdmsgs))
412 ? _("complete") : _("updated.")));
413 } else if (held && !(n_pstate & n_PS_EDIT) && mb.mb_perm != 0) {
414 if (held == 1)
415 fprintf(n_stdout, _("Held 1 message in %s\n"), displayname);
416 else
417 fprintf(n_stdout, _("Held %d messages in %s\n"), held, displayname);
419 fflush(n_stdout);
420 jfree:
421 for (m = message; PTRCMP(m, <, message + msgCount); ++m)
422 free(m->m_maildir_file);
423 NYD_LEAVE;
426 static void
427 _maildir_move(struct message *m)
429 char *fn, *new;
430 NYD_ENTER;
432 fn = mkname(0, m->m_flag, m->m_maildir_file + 4);
433 new = savecat("cur/", fn);
434 if (!strcmp(m->m_maildir_file, new))
435 goto jleave;
436 if (link(m->m_maildir_file, new) == -1) {
437 n_err(_("Cannot link %s to %s: message %lu not touched\n"),
438 n_shexp_quote_cp(savecatsep(mailname, '/', m->m_maildir_file), FAL0),
439 n_shexp_quote_cp(savecatsep(mailname, '/', new), FAL0),
440 (ul_i)PTR2SIZE(m - message + 1));
441 goto jleave;
443 if (unlink(m->m_maildir_file) == -1)
444 n_err(_("Cannot unlink %s\n"),
445 n_shexp_quote_cp(savecatsep(mailname, '/', m->m_maildir_file), FAL0));
446 jleave:
447 NYD_LEAVE;
450 static char *
451 mkname(time_t t, enum mflag f, char const *pref)
453 static unsigned long cnt;
454 static pid_t mypid; /* XXX This should possibly be global, somehow */
455 static char *node;
457 char *cp;
458 int size, n, i;
459 NYD_ENTER;
461 if (pref == NULL) {
462 if (mypid == 0)
463 mypid = getpid();
464 if (node == NULL) {
465 cp = nodename(0);
466 n = size = 0;
467 do {
468 if (UICMP(32, n, <, size + 8))
469 node = srealloc(node, size += 20);
470 switch (*cp) {
471 case '/':
472 node[n++] = '\\', node[n++] = '0',
473 node[n++] = '5', node[n++] = '7';
474 break;
475 case ':':
476 node[n++] = '\\', node[n++] = '0',
477 node[n++] = '7', node[n++] = '2';
478 break;
479 default:
480 node[n++] = *cp;
482 } while (*cp++ != '\0');
484 size = 60 + strlen(node);
485 cp = salloc(size);
486 n = snprintf(cp, size, "%lu.%06lu_%06lu.%s:2,",
487 (ul_i)t, (ul_i)mypid, ++cnt, node);
488 } else {
489 size = (n = strlen(pref)) + 13;
490 cp = salloc(size);
491 memcpy(cp, pref, n +1);
492 for (i = n; i > 3; --i)
493 if (cp[i - 1] == ',' && cp[i - 2] == '2' && cp[i - 3] == ':') {
494 n = i;
495 break;
497 if (i <= 3) {
498 memcpy(cp + n, ":2,", 3 +1);
499 n += 3;
502 if (n < size - 7) {
503 if (f & MDRAFTED)
504 cp[n++] = 'D';
505 if (f & MFLAGGED)
506 cp[n++] = 'F';
507 if (f & MANSWERED)
508 cp[n++] = 'R';
509 if (f & MREAD)
510 cp[n++] = 'S';
511 if (f & MDELETED)
512 cp[n++] = 'T';
513 cp[n] = '\0';
515 NYD_LEAVE;
516 return cp;
519 static enum okay
520 maildir_append1(char const *name, FILE *fp, off_t off1, long size,
521 enum mflag flag)
523 char buf[4096], *fn, *tfn, *nfn;
524 struct stat st;
525 FILE *op;
526 time_t now;
527 size_t nlen, flen, n;
528 enum okay rv = STOP;
529 NYD_ENTER;
531 nlen = strlen(name);
533 /* Create a unique temporary file */
534 for (nfn = (char*)0xA /* XXX no magic */;; n_msleep(500, FAL0)) {
535 now = n_time_epoch();
536 flen = strlen(fn = mkname(now, flag, NULL));
537 tfn = salloc(n = nlen + flen + 6);
538 snprintf(tfn, n, "%s/tmp/%s", name, fn);
540 /* Use "wx" for O_EXCL XXX stat(2) rather redundant; coverity:TOCTOU */
541 if ((!stat(tfn, &st) || errno == ENOENT) &&
542 (op = Fopen(tfn, "wx")) != NULL)
543 break;
545 nfn = (char*)(PTR2SIZE(nfn) - 1);
546 if (nfn == NULL) {
547 n_err(_("Can't create an unique file name in %s\n"),
548 n_shexp_quote_cp(savecat(name, "/tmp"), FAL0));
549 goto jleave;
553 if (fseek(fp, off1, SEEK_SET) == -1)
554 goto jtmperr;
555 while (size > 0) {
556 size_t z = UICMP(z, size, >, sizeof buf) ? sizeof buf : (size_t)size;
558 if (z != (n = fread(buf, 1, z, fp)) || n != fwrite(buf, 1, n, op)) {
559 jtmperr:
560 n_err(_("Error writing to %s\n"), n_shexp_quote_cp(tfn, FAL0));
561 Fclose(op);
562 goto jerr;
564 size -= n;
566 Fclose(op);
568 nfn = salloc(n = nlen + flen + 6);
569 snprintf(nfn, n, "%s/new/%s", name, fn);
570 if (link(tfn, nfn) == -1) {
571 n_err(_("Cannot link %s to %s\n"), n_shexp_quote_cp(tfn, FAL0),
572 n_shexp_quote_cp(nfn, FAL0));
573 goto jerr;
575 rv = OKAY;
576 jerr:
577 if (unlink(tfn) == -1)
578 n_err(_("Cannot unlink %s\n"), n_shexp_quote_cp(tfn, FAL0));
579 jleave:
580 NYD_LEAVE;
581 return rv;
584 static enum okay
585 trycreate(char const *name)
587 struct stat st;
588 enum okay rv = STOP;
589 NYD_ENTER;
591 if (!stat(name, &st)) {
592 if (!S_ISDIR(st.st_mode)) {
593 n_err(_("%s is not a directory\n"), n_shexp_quote_cp(name, FAL0));
594 goto jleave;
596 } else if (!n_path_mkdir(name)) {
597 n_err(_("Cannot create directory %s\n"), n_shexp_quote_cp(name, FAL0));
598 goto jleave;
600 rv = OKAY;
601 jleave:
602 NYD_LEAVE;
603 return rv;
606 static enum okay
607 mkmaildir(char const *name) /* TODO proper cleanup on error; use path[] loop */
609 char *np;
610 size_t sz;
611 enum okay rv = STOP;
612 NYD_ENTER;
614 if (trycreate(name) == OKAY) {
615 np = ac_alloc((sz = strlen(name)) + 4 +1);
616 memcpy(np, name, sz);
617 memcpy(np + sz, "/tmp", 4 +1);
618 if (trycreate(np) == OKAY) {
619 memcpy(np + sz, "/new", 4 +1);
620 if (trycreate(np) == OKAY) {
621 memcpy(np + sz, "/cur", 4 +1);
622 rv = trycreate(np);
625 ac_free(np);
627 NYD_LEAVE;
628 return rv;
631 static struct message *
632 mdlook(char const *name, struct message *data)
634 struct mditem *md;
635 ui32_t c, h, n = 0;
636 NYD_ENTER;
638 if (data && data->m_maildir_hash)
639 h = ~data->m_maildir_hash;
640 else
641 h = a_maildir_hash(name);
642 h %= _maildir_prime;
643 c = h;
644 md = _maildir_table + c;
646 while (md->md_data != NULL) {
647 if (!strcmp(md->md_data->m_maildir_file + 4, name))
648 break;
649 c += (n & 1) ? -((n+1)/2) * ((n+1)/2) : ((n+1)/2) * ((n+1)/2);
650 n++;
651 while (c >= _maildir_prime)
652 c -= _maildir_prime;
653 md = _maildir_table + c;
655 if (data != NULL && md->md_data == NULL)
656 md->md_data = data;
657 NYD_LEAVE;
658 return md->md_data;
661 static void
662 mktable(void)
664 struct message *mp;
665 size_t i;
666 NYD_ENTER;
668 _maildir_prime = nextprime(msgCount);
669 _maildir_table = scalloc(_maildir_prime, sizeof *_maildir_table);
670 for (mp = message, i = msgCount; i-- != 0; ++mp)
671 mdlook(mp->m_maildir_file + 4, mp);
672 NYD_LEAVE;
675 static enum okay
676 subdir_remove(char const *name, char const *sub)
678 char *path;
679 int pathsize, pathend, namelen, sublen, n;
680 DIR *dirp;
681 struct dirent *dp;
682 enum okay rv = STOP;
683 NYD_ENTER;
685 namelen = strlen(name);
686 sublen = strlen(sub);
687 path = smalloc(pathsize = namelen + sublen + 30 +1);
688 memcpy(path, name, namelen);
689 path[namelen] = '/';
690 memcpy(path + namelen + 1, sub, sublen);
691 path[namelen + sublen + 1] = '/';
692 path[pathend = namelen + sublen + 2] = '\0';
694 if ((dirp = opendir(path)) == NULL) {
695 n_perr(path, 0);
696 goto jleave;
698 while ((dp = readdir(dirp)) != NULL) {
699 if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
700 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
701 continue;
702 if (dp->d_name[0] == '.')
703 continue;
704 n = strlen(dp->d_name);
705 if (UICMP(32, pathend + n +1, >, pathsize))
706 path = srealloc(path, pathsize = pathend + n + 30);
707 memcpy(path + pathend, dp->d_name, n +1);
708 if (unlink(path) == -1) {
709 n_perr(path, 0);
710 closedir(dirp);
711 goto jleave;
714 closedir(dirp);
716 path[pathend] = '\0';
717 if (rmdir(path) == -1) {
718 n_perr(path, 0);
719 goto jleave;
721 rv = OKAY;
722 jleave:
723 free(path);
724 NYD_LEAVE;
725 return rv;
728 FL int
729 maildir_setfile(char const * volatile name, enum fedit_mode fm)
731 sighandler_type volatile saveint;
732 struct cw cw;
733 int omsgCount;
734 int volatile i = -1;
735 NYD_ENTER;
737 omsgCount = msgCount;
738 if (cwget(&cw) == STOP) {
739 n_alert(_("Cannot open current directory"));
740 goto jleave;
743 if (!(fm & FEDIT_NEWMAIL) && !quit(FAL0))
744 goto jleave;
746 saveint = safe_signal(SIGINT, SIG_IGN);
748 if (!(fm & FEDIT_NEWMAIL)) {
749 if (fm & FEDIT_SYSBOX)
750 n_pstate &= ~n_PS_EDIT;
751 else
752 n_pstate |= n_PS_EDIT;
753 if (mb.mb_itf) {
754 fclose(mb.mb_itf);
755 mb.mb_itf = NULL;
757 if (mb.mb_otf) {
758 fclose(mb.mb_otf);
759 mb.mb_otf = NULL;
761 initbox(name);
762 mb.mb_type = MB_MAILDIR;
765 if (chdir(name) < 0) {
766 n_err(_("Cannot change directory to %s\n"), n_shexp_quote_cp(name, FAL0));
767 mb.mb_type = MB_VOID;
768 *mailname = '\0';
769 msgCount = 0;
770 cwrelse(&cw);
771 safe_signal(SIGINT, saveint);
772 goto jleave;
775 _maildir_table = NULL;
776 if (sigsetjmp(_maildir_jmp, 1) == 0) {
777 if (fm & FEDIT_NEWMAIL)
778 mktable();
779 if (saveint != SIG_IGN)
780 safe_signal(SIGINT, &__maildircatch);
781 i = _maildir_setfile1(name, fm, omsgCount);
783 if ((fm & FEDIT_NEWMAIL) && _maildir_table != NULL)
784 free(_maildir_table);
786 safe_signal(SIGINT, saveint);
788 if (i < 0) {
789 mb.mb_type = MB_VOID;
790 *mailname = '\0';
791 msgCount = 0;
794 if (cwret(&cw) == STOP)
795 n_panic(_("Cannot change back to current directory"));
796 cwrelse(&cw);
798 setmsize(msgCount);
799 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted && msgCount > omsgCount) {
800 mb.mb_threaded = 0;
801 c_sort((void*)-1);
804 if (!(fm & FEDIT_NEWMAIL)) {
805 n_pstate &= ~n_PS_SAW_COMMAND;
806 n_pstate |= n_PS_SETFILE_OPENED;
809 if ((n_poption & n_PO_EXISTONLY) && !(n_poption & n_PO_HEADERLIST)) {
810 i = (msgCount == 0);
811 goto jleave;
814 if (!(fm & FEDIT_NEWMAIL) && (fm & FEDIT_SYSBOX) && msgCount == 0) {
815 if (mb.mb_type == MB_MAILDIR /* XXX ?? */ && !ok_blook(emptystart))
816 n_err(_("No mail at %s\n"), n_shexp_quote_cp(name, FAL0));
817 i = 1;
818 goto jleave;
821 if ((fm & FEDIT_NEWMAIL) && msgCount > omsgCount)
822 newmailinfo(omsgCount);
823 i = 0;
824 jleave:
825 NYD_LEAVE;
826 return i;
829 FL bool_t
830 maildir_quit(bool_t hold_sigs_on)
832 sighandler_type saveint;
833 struct cw cw;
834 bool_t rv;
835 NYD_ENTER;
837 if(hold_sigs_on)
838 rele_sigs();
840 rv = FAL0;
842 if (cwget(&cw) == STOP) {
843 n_alert(_("Cannot open current directory"));
844 goto jleave;
847 saveint = safe_signal(SIGINT, SIG_IGN);
849 if (chdir(mailname) == -1) {
850 n_err(_("Cannot change directory to %s\n"),
851 n_shexp_quote_cp(mailname, FAL0));
852 cwrelse(&cw);
853 safe_signal(SIGINT, saveint);
854 goto jleave;
857 if (sigsetjmp(_maildir_jmp, 1) == 0) {
858 if (saveint != SIG_IGN)
859 safe_signal(SIGINT, &__maildircatch_hold);
860 maildir_update();
863 safe_signal(SIGINT, saveint);
865 if (cwret(&cw) == STOP)
866 n_panic(_("Cannot change back to current directory"));
867 cwrelse(&cw);
868 rv = TRU1;
869 jleave:
870 if(hold_sigs_on)
871 hold_sigs();
872 NYD_LEAVE;
873 return rv;
876 FL enum okay
877 maildir_append(char const *name, FILE *fp, long offset)
879 char *buf, *bp, *lp;
880 size_t bufsize, buflen, cnt;
881 off_t off1 = -1, offs;
882 long size;
883 int flag;
884 enum {_NONE = 0, _INHEAD = 1<<0, _NLSEP = 1<<1} state;
885 enum okay rv;
886 NYD_ENTER;
888 if ((rv = mkmaildir(name)) != OKAY)
889 goto jleave;
891 buf = smalloc(bufsize = LINESIZE); /* TODO line pool; signals */
892 buflen = 0;
893 cnt = fsize(fp);
894 offs = offset /* BSD will move due to O_APPEND! ftell(fp) */;
895 size = 0;
897 srelax_hold();
898 for (flag = MNEW, state = _NLSEP;;) {
899 bp = fgetline(&buf, &bufsize, &cnt, &buflen, fp, 1);
901 if (bp == NULL ||
902 ((state & (_INHEAD | _NLSEP)) == _NLSEP &&
903 is_head(buf, buflen, FAL0))) {
904 if (off1 != (off_t)-1) {
905 if ((rv = maildir_append1(name, fp, off1, size, flag)) == STOP)
906 goto jfree;
907 srelax();
908 if (fseek(fp, offs + buflen, SEEK_SET) == -1) {
909 rv = STOP;
910 goto jfree;
913 off1 = offs + buflen;
914 size = 0;
915 state = _INHEAD;
916 flag = MNEW;
918 if (bp == NULL)
919 break;
920 } else
921 size += buflen;
922 offs += buflen;
924 state &= ~_NLSEP;
925 if (buf[0] == '\n') {
926 state &= ~_INHEAD;
927 state |= _NLSEP;
928 } else if (state & _INHEAD) {
929 if (!ascncasecmp(buf, "status", 6)) {
930 lp = buf + 6;
931 while (whitechar(*lp))
932 ++lp;
933 if (*lp == ':')
934 while (*++lp != '\0')
935 switch (*lp) {
936 case 'R':
937 flag |= MREAD;
938 break;
939 case 'O':
940 flag &= ~MNEW;
941 break;
943 } else if (!ascncasecmp(buf, "x-status", 8)) {
944 lp = buf + 8;
945 while (whitechar(*lp))
946 ++lp;
947 if (*lp == ':') {
948 while (*++lp != '\0')
949 switch (*lp) {
950 case 'F':
951 flag |= MFLAGGED;
952 break;
953 case 'A':
954 flag |= MANSWERED;
955 break;
956 case 'T':
957 flag |= MDRAFTED;
958 break;
964 assert(rv == OKAY);
965 jfree:
966 srelax_rele();
967 free(buf);
968 jleave:
969 NYD_LEAVE;
970 return rv;
973 FL enum okay
974 maildir_remove(char const *name)
976 enum okay rv = STOP;
977 NYD_ENTER;
979 if (subdir_remove(name, "tmp") == STOP ||
980 subdir_remove(name, "new") == STOP ||
981 subdir_remove(name, "cur") == STOP)
982 goto jleave;
983 if (rmdir(name) == -1) {
984 n_perr(name, 0);
985 goto jleave;
987 rv = OKAY;
988 jleave:
989 NYD_LEAVE;
990 return rv;
993 /* s-it-mode */