sendmp(), setfile(): FIX: ensure time_current is actualized
[s-mailx.git] / folder.c
blobd97276e968fa2edeef2de8d5f88ce03fadd759fe
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Folder (mailbox) initialization, newmail announcement and related.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE folder
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 #include <pwd.h>
44 /* Update mailname (if name != NULL) and displayname, return whether displayname
45 * was large enough to swallow mailname */
46 static bool_t _update_mailname(char const *name);
47 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
48 SINLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
49 #endif
51 #ifdef HAVE_C90AMEND1
52 SINLINE size_t
53 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
55 int err;
56 size_t i, ok;
57 NYD_ENTER;
59 for (err = ok = i = 0; cpl > maxl || err;) {
60 int ml = mblen(cp, cpl);
61 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
62 (void)mblen(NULL, 0);
63 err = 1;
64 ml = 1;
65 } else {
66 if (!err)
67 ok = i;
68 err = 0;
69 if (ml == 0)
70 break;
72 cp += ml;
73 i += ml;
74 cpl -= ml;
76 NYD_LEAVE;
77 return ok;
79 #endif /* HAVE_C90AMEND1 */
81 static bool_t
82 _update_mailname(char const *name)
84 char *mailp, *dispp;
85 size_t i, j;
86 bool_t rv = TRU1;
87 NYD_ENTER;
89 /* Don't realpath(3) if it's only an update request */
90 if (name != NULL) {
91 #ifdef HAVE_REALPATH
92 enum protocol p = which_protocol(name);
94 if (p == PROTO_FILE || p == PROTO_MAILDIR) {
95 if (realpath(name, mailname) == NULL && errno != ENOENT) {
96 n_err(_("Can't canonicalize %s\n"), n_shell_quote_cp(name, FAL0));
97 rv = FAL0;
98 goto jdocopy;
100 } else
101 jdocopy:
102 #endif
103 n_strscpy(mailname, name, sizeof(mailname));
106 mailp = mailname;
107 dispp = displayname;
109 /* Don't display an absolute path but "+FOLDER" if under *folder* */
110 /* C99 */{
111 char const *folderp;
113 if(*(folderp = folder_query()) != '\0'){
114 i = strlen(folderp);
115 if(!strncmp(folderp, mailp, i)){
116 if(folderp[i -1] != '/' && mailp[i] == '/') /* XXX DIRSEP magic */
117 ++i;
118 mailp += i;
119 *dispp++ = '+';
124 /* We want to see the name of the folder .. on the screen */
125 i = strlen(mailp);
126 if (i < sizeof(displayname) -1)
127 memcpy(dispp, mailp, i +1);
128 else {
129 rv = FAL0;
130 /* Avoid disrupting multibyte sequences (if possible) */
131 #ifndef HAVE_C90AMEND1
132 j = sizeof(displayname) / 3 - 1;
133 i -= sizeof(displayname) - (1/* + */ + 3) - j;
134 #else
135 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
136 i = j + __narrow_suffix(mailp + j, i - j,
137 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
138 #endif
139 snprintf(dispp, sizeof(displayname), "%.*s...%s",
140 (int)j, mailp, mailp + i);
142 NYD_LEAVE;
143 return rv;
146 FL int
147 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
149 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
150 * necessary to make a mailbox accessible by a different user, and if that
151 * has happened, let's just let the usual file perms decide */
152 static int shudclob;
154 struct stat stb;
155 size_t offset;
156 char const *who;
157 int rv, omsgCount = 0;
158 FILE *ibuf = NULL, *lckfp = NULL;
159 bool_t isdevnull = FAL0;
160 NYD_ENTER;
162 pstate &= ~PS_SETFILE_OPENED;
164 /* C99 */{
165 enum fexp_mode fexpm;
167 if((who = shortcut_expand(name)) != NULL){
168 fexpm = FEXP_NSHORTCUT | FEXP_NSHELL;
169 name = who;
170 }else
171 fexpm = FEXP_NSHELL;
173 if(name[0] == '%'){
174 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
175 if(*(who = &name[1]) == ':')
176 ++who;
177 if(*who == '\0')
178 who = myname;
179 }else
180 who = myname;
182 if ((name = fexpand(name, fexpm)) == NULL)
183 goto jem1;
186 /* For at least substdate() users */
187 time_current_update(&time_current, FAL0);
189 switch (which_protocol(name)) {
190 case PROTO_FILE:
191 if (temporary_protocol_ext != NULL)
192 name = savecat(name, temporary_protocol_ext);
193 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
194 #ifdef HAVE_REALPATH
195 do { /* TODO we need objects, goes away then */
196 # ifdef HAVE_REALPATH_NULL
197 char *cp;
199 if ((cp = realpath(name, NULL)) != NULL) {
200 name = savestr(cp);
201 (free)(cp);
203 # else
204 char cbuf[PATH_MAX];
206 if (realpath(name, cbuf) != NULL)
207 name = savestr(cbuf);
208 # endif
209 } while (0);
210 #endif
211 rv = 1;
212 break;
213 case PROTO_MAILDIR:
214 shudclob = 1;
215 rv = maildir_setfile(name, fm);
216 goto jleave;
217 #ifdef HAVE_POP3
218 case PROTO_POP3:
219 shudclob = 1;
220 rv = pop3_setfile(name, fm);
221 goto jleave;
222 #endif
223 default:
224 n_err(_("Cannot handle protocol: %s\n"), name);
225 goto jem1;
228 if ((ibuf = Zopen(name, "r")) == NULL) {
229 if ((fm & FEDIT_SYSBOX) && errno == ENOENT) {
230 if (strcmp(who, myname) && getpwnam(who) == NULL) {
231 n_err(_("%s is not a user of this system\n"),
232 n_shell_quote_cp(who, FAL0));
233 goto jem2;
235 if (!(options & OPT_QUICKRUN_MASK) && ok_blook(bsdcompat))
236 n_err(_("No mail for %s\n"), who);
237 if (fm & FEDIT_NEWMAIL)
238 goto jleave;
239 if (ok_blook(emptystart)) {
240 if (!(options & OPT_QUICKRUN_MASK) && !ok_blook(bsdcompat))
241 n_perr(name, 0);
242 /* We must avoid returning -1 and causing program exit */
243 mb.mb_type = MB_VOID;
244 rv = 1;
245 goto jleave;
247 goto jem2;
248 } else if (fm & FEDIT_NEWMAIL)
249 goto jleave;
250 n_perr(name, 0);
251 goto jem1;
254 if (fstat(fileno(ibuf), &stb) == -1) {
255 if (fm & FEDIT_NEWMAIL)
256 goto jleave;
257 n_perr(_("fstat"), 0);
258 goto jem1;
261 if (S_ISREG(stb.st_mode) || isdevnull) {
262 /* EMPTY */
263 } else {
264 if (fm & FEDIT_NEWMAIL)
265 goto jleave;
266 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
267 n_perr(name, 0);
268 goto jem1;
271 /* Looks like all will be well. We must now relinquish our hold on the
272 * current set of stuff. Must hold signals while we are reading the new
273 * file, else we will ruin the message[] data structure */
275 hold_sigs(); /* TODO note on this one in quit.c:quit() */
276 if (shudclob && !(fm & FEDIT_NEWMAIL) && !quit()) {
277 rele_sigs();
278 goto jem2;
281 #ifdef HAVE_SOCKETS
282 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
283 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
284 #endif
286 /* TODO There is no intermediate VOID box we've switched to: name may
287 * TODO point to the same box that we just have written, so any updates
288 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
289 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
290 /* TODO We may uselessly open twice but quit() doesn't say whether we were
291 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
292 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
293 name = mailname;
294 Fclose(ibuf);
296 if ((ibuf = Zopen(name, "r")) == NULL ||
297 fstat(fileno(ibuf), &stb) == -1 ||
298 (!S_ISREG(stb.st_mode) && !isdevnull)) {
299 n_perr(name, 0);
300 rele_sigs();
301 goto jem2;
305 /* Copy the messages into /tmp and set pointers */
306 if (!(fm & FEDIT_NEWMAIL)) {
307 if (isdevnull) {
308 mb.mb_type = MB_VOID;
309 mb.mb_perm = 0;
310 } else {
311 mb.mb_type = MB_FILE;
312 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
313 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
314 if (shudclob) {
315 if (mb.mb_itf) {
316 fclose(mb.mb_itf);
317 mb.mb_itf = NULL;
319 if (mb.mb_otf) {
320 fclose(mb.mb_otf);
321 mb.mb_otf = NULL;
325 shudclob = 1;
326 if (fm & FEDIT_SYSBOX)
327 pstate &= ~PS_EDIT;
328 else
329 pstate |= PS_EDIT;
330 initbox(name);
331 offset = 0;
332 } else {
333 fseek(mb.mb_otf, 0L, SEEK_END);
334 /* TODO Doing this without holding a lock is.. And not err checking.. */
335 fseek(ibuf, mailsize, SEEK_SET);
336 offset = mailsize;
337 omsgCount = msgCount;
340 if (isdevnull)
341 lckfp = (FILE*)-1;
342 else if (!(pstate & PS_EDIT))
343 lckfp = n_dotlock(name, fileno(ibuf), FLT_READ, offset,0,
344 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX));
345 else if (n_file_lock(fileno(ibuf), FLT_READ, offset,0,
346 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX)))
347 lckfp = (FILE*)-1;
349 if (lckfp == NULL) {
350 if (!(fm & FEDIT_NEWMAIL)) {
351 char const *emsg = (pstate & PS_EDIT)
352 ? N_("Unable to lock mailbox, aborting operation")
353 : N_("Unable to (dot) lock mailbox, aborting operation");
354 n_perr(V_(emsg), 0);
356 rele_sigs();
357 if (!(fm & FEDIT_NEWMAIL))
358 goto jem1;
359 goto jleave;
362 mailsize = fsize(ibuf);
364 /* TODO This is too simple minded? We should regenerate an index file
365 * TODO to be able to truly tell whether *anything* has changed! */
366 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
367 rele_sigs();
368 goto jleave;
370 setptr(ibuf, offset);
371 setmsize(msgCount);
372 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
373 mb.mb_threaded = 0;
374 c_sort((void*)-1);
377 Fclose(ibuf);
378 ibuf = NULL;
379 if (lckfp != NULL && lckfp != (FILE*)-1) {
380 Pclose(lckfp, FAL0);
381 /*lckfp = NULL;*/
383 rele_sigs();
385 if (!(fm & FEDIT_NEWMAIL)) {
386 pstate &= ~PS_SAW_COMMAND;
387 pstate |= PS_SETFILE_OPENED;
390 if ((options & OPT_EXISTONLY) && !(options & OPT_HEADERLIST)) {
391 rv = (msgCount == 0);
392 goto jleave;
395 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
396 if (!(fm & FEDIT_NEWMAIL)) {
397 if (!ok_blook(emptystart))
398 n_err(_("No mail for %s\n"), who);
400 goto jleave;
403 if (fm & FEDIT_NEWMAIL)
404 newmailinfo(omsgCount);
406 rv = 0;
407 jleave:
408 if (ibuf != NULL) {
409 Fclose(ibuf);
410 if (lckfp != NULL && lckfp != (FILE*)-1)
411 Pclose(lckfp, FAL0);
413 NYD_LEAVE;
414 return rv;
415 jem2:
416 mb.mb_type = MB_VOID;
417 jem1:
418 rv = -1;
419 goto jleave;
422 FL int
423 newmailinfo(int omsgCount)
425 int mdot, i;
426 NYD_ENTER;
428 for (i = 0; i < omsgCount; ++i)
429 message[i].m_flag &= ~MNEWEST;
431 if (msgCount > omsgCount) {
432 for (i = omsgCount; i < msgCount; ++i)
433 message[i].m_flag |= MNEWEST;
434 printf(_("New mail has arrived.\n"));
435 if ((i = msgCount - omsgCount) == 1)
436 printf(_("Loaded 1 new message.\n"));
437 else
438 printf(_("Loaded %d new messages.\n"), i);
439 } else
440 printf(_("Loaded %d messages.\n"), msgCount);
442 check_folder_hook(TRU1);
444 mdot = getmdot(1);
446 if (ok_blook(header))
447 print_headers(omsgCount + 1, msgCount, FAL0);
448 NYD_LEAVE;
449 return mdot;
452 FL void
453 setmsize(int sz)
455 NYD_ENTER;
456 if (n_msgvec != NULL)
457 free(n_msgvec);
458 n_msgvec = scalloc(sz + 1, sizeof *n_msgvec);
459 NYD_LEAVE;
462 FL void
463 print_header_summary(char const *Larg)
465 size_t bot, top, i, j;
466 NYD_ENTER;
468 if (Larg != NULL) {
469 /* Avoid any messages XXX add a make_mua_silent() and use it? */
470 if ((options & (OPT_VERB | OPT_EXISTONLY)) == OPT_EXISTONLY) {
471 freopen("/dev/null", "w", stdout);
472 freopen("/dev/null", "w", stderr);
474 assert(n_msgvec != NULL);
475 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), n_msgvec, 0) <= 0);
476 if (options & OPT_EXISTONLY) {
477 exit_status = (int)i;
478 goto jleave;
480 if (i)
481 goto jleave;
482 for (bot = msgCount, top = 0, i = 0; (j = n_msgvec[i]) != 0; ++i) {
483 if (bot > j)
484 bot = j;
485 if (top < j)
486 top = j;
488 } else
489 bot = 1, top = msgCount;
490 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
491 jleave:
492 NYD_LEAVE;
495 FL void
496 announce(int printheaders)
498 int vec[2], mdot;
499 NYD_ENTER;
501 mdot = newfileinfo();
502 vec[0] = mdot;
503 vec[1] = 0;
504 dot = message + mdot - 1;
505 if (printheaders && msgCount > 0 && ok_blook(header)) {
506 print_header_group(vec); /* XXX errors? */
508 NYD_LEAVE;
511 FL int
512 newfileinfo(void)
514 struct message *mp;
515 int u, n, mdot, d, s, hidden, moved;
516 NYD_ENTER;
518 if (mb.mb_type == MB_VOID) {
519 mdot = 1;
520 goto jleave;
523 mdot = getmdot(0);
524 s = d = hidden = moved =0;
525 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
526 if (mp->m_flag & MNEW)
527 ++n;
528 if ((mp->m_flag & MREAD) == 0)
529 ++u;
530 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
531 ++moved;
532 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
533 ++d;
534 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
535 ++s;
536 if (mp->m_flag & MHIDDEN)
537 ++hidden;
540 /* If displayname gets truncated the user effectively has no option to see
541 * the full pathname of the mailbox, so print it at least for '? fi' */
542 printf(_("%s: "), n_shell_quote_cp(
543 (_update_mailname(NULL) ? displayname : mailname), FAL0));
544 if (msgCount == 1)
545 printf(_("1 message"));
546 else
547 printf(_("%d messages"), msgCount);
548 if (n > 0)
549 printf(_(" %d new"), n);
550 if (u-n > 0)
551 printf(_(" %d unread"), u);
552 if (d > 0)
553 printf(_(" %d deleted"), d);
554 if (s > 0)
555 printf(_(" %d saved"), s);
556 if (moved > 0)
557 printf(_(" %d moved"), moved);
558 if (hidden > 0)
559 printf(_(" %d hidden"), hidden);
560 else if (mb.mb_perm == 0)
561 printf(_(" [Read only]"));
562 printf("\n");
563 jleave:
564 NYD_LEAVE;
565 return mdot;
568 FL int
569 getmdot(int nmail)
571 struct message *mp;
572 char *cp;
573 int mdot;
574 enum mflag avoid = MHIDDEN | MDELETED;
575 NYD_ENTER;
577 if (!nmail) {
578 if (ok_blook(autothread)) {
579 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
580 c_thread(NULL);
581 } else if ((cp = ok_vlook(autosort)) != NULL) {
582 if (mb.mb_sorted != NULL)
583 free(mb.mb_sorted);
584 mb.mb_sorted = sstrdup(cp);
585 c_sort(NULL);
588 if (mb.mb_type == MB_VOID) {
589 mdot = 1;
590 goto jleave;
593 if (nmail)
594 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
595 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
596 break;
598 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
599 if (mb.mb_threaded) {
600 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
601 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
602 break;
603 } else {
604 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
605 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
606 break;
610 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
611 if (mb.mb_threaded) {
612 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
613 if (mp->m_flag & MFLAGGED)
614 break;
615 } else {
616 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
617 if (mp->m_flag & MFLAGGED)
618 break;
622 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
623 if (mb.mb_threaded) {
624 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
625 if (!(mp->m_flag & (MREAD | avoid)))
626 break;
627 } else {
628 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
629 if (!(mp->m_flag & (MREAD | avoid)))
630 break;
634 if (nmail &&
635 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
636 mdot = (int)PTR2SIZE(mp - message + 1);
637 else if (ok_blook(showlast)) {
638 if (mb.mb_threaded) {
639 for (mp = this_in_thread(threadroot, -1); mp;
640 mp = prev_in_thread(mp))
641 if (!(mp->m_flag & avoid))
642 break;
643 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
644 } else {
645 for (mp = message + msgCount - 1; mp >= message; --mp)
646 if (!(mp->m_flag & avoid))
647 break;
648 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
650 } else if (!nmail &&
651 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
652 mdot = (int)PTR2SIZE(mp - message + 1);
653 else if (mb.mb_threaded) {
654 for (mp = threadroot; mp; mp = next_in_thread(mp))
655 if (!(mp->m_flag & avoid))
656 break;
657 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
658 } else {
659 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
660 if (!(mp->m_flag & avoid))
661 break;
662 mdot = PTRCMP(mp, <, message + msgCount)
663 ? (int)PTR2SIZE(mp - message + 1) : 1;
665 jleave:
666 NYD_LEAVE;
667 return mdot;
670 FL void
671 initbox(char const *name)
673 char *tempMesg;
674 NYD_ENTER;
676 if (mb.mb_type != MB_VOID)
677 n_strscpy(prevfile, mailname, PATH_MAX);
679 /* TODO name always NE mailname (but goes away for objects anyway)
680 * TODO Well, not true no more except that in parens */
681 _update_mailname((name != mailname) ? name : NULL);
683 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
684 NULL) {
685 n_perr(_("temporary mail message file"), 0);
686 exit(EXIT_ERR);
688 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
689 n_perr(_("temporary mail message file"), 0);
690 exit(EXIT_ERR);
692 Ftmp_release(&tempMesg);
694 message_reset();
695 mb.mb_threaded = 0;
696 if (mb.mb_sorted != NULL) {
697 free(mb.mb_sorted);
698 mb.mb_sorted = NULL;
700 dot = prevdot = threadroot = NULL;
701 pstate &= ~PS_DID_PRINT_DOT;
702 NYD_LEAVE;
705 FL char const *
706 folder_query(void){
707 struct n_string s, *sp = &s;
708 char *cp;
709 char const *rv;
710 bool_t err;
711 NYD_ENTER;
713 /* *folder* is linked with *_folder_resolved*: we only use the latter */
714 for(err = FAL0;;){
715 if((rv = ok_vlook(_folder_resolved)) != NULL)
716 break;
718 /* POSIX says:
719 * If directory does not start with a <slash> ('/'), the contents
720 * of HOME shall be prefixed to it.
721 * And:
722 * If folder is unset or set to null, [.] filenames beginning with
723 * '+' shall refer to files in the current directory.
724 * We may have the result already */
725 rv = "";
726 err = FAL0;
728 if((cp = ok_vlook(folder)) == NULL)
729 goto jset;
731 /* Expand the *folder*; skip %: prefix for simplicity of use */
732 if(cp[0] == '%' && cp[1] == ':')
733 cp += 2;
734 if((err = (cp = fexpand(cp, FEXP_NSHELL)) == NULL) || *cp == '\0')
735 goto jset;
737 switch(which_protocol(cp)){
738 case PROTO_POP3:
739 n_err(_("*folder* can't be set to a flat, readonly POP3 account\n"));
740 err = TRU1;
741 goto jset;
742 default:
743 /* Further expansion desired */
744 break;
747 /* Prefix HOME as necessary */
748 if(*cp != '/'){
749 char const *home = ok_vlook(HOME);
750 size_t l1 = strlen(home), l2 = strlen(cp);
752 sp = n_string_creat_auto(sp);
753 sp = n_string_reserve(sp, l1 + 1 + l2 +1);
754 sp = n_string_push_buf(sp, home, l1);
755 sp = n_string_push_c(sp, '/');
756 sp = n_string_push_buf(sp, cp, l2);
757 cp = n_string_cp(sp);
760 rv = cp;
762 /* TODO Since our visual mailname is resolved via realpath(3) if available
763 * TODO to avoid that we loose track of our currently open folder in case
764 * TODO we chdir away, but still checks the leading path portion against
765 * TODO fold_query() to be able to abbreviate to the +FOLDER syntax if
766 * TODO possible, we need to realpath(3) the folder, too */
767 #ifdef HAVE_REALPATH
768 /* C99 */{
769 # ifndef HAVE_REALPATH_NULL
770 if(cp == sp->s_dat)
771 sp = n_string_drop_ownership(sp);
772 sp = n_string_reserve(sp, PATH_MAX +1);
773 # else
774 sp->s_dat = NULL;
775 # endif
777 if((sp->s_dat = realpath(cp, sp->s_dat)) != NULL){
778 # ifdef HAVE_REALPATH_NULL
779 sp->s_dat = savestr(cp = sp->s_dat);
780 (free)(cp);
781 # endif
782 rv = sp->s_dat;
783 }else if(errno == ENOENT)
784 rv = cp;
785 else{
786 n_err(_("Can't canonicalize *folder*: %s\n"),
787 n_shell_quote_cp(cp, FAL0));
788 err = TRU1;
789 rv = "";
792 #endif /* HAVE_REALPATH */
794 jset:
795 /* C99 */{
796 bool_t reset = !(pstate & PS_ROOT);
798 pstate |= PS_ROOT;
799 ok_vset(_folder_resolved, rv);
800 if(reset)
801 pstate &= ~PS_ROOT;
805 if(err){
806 n_err(_("*folder* is not resolvable, using CWD\n"));
807 assert(rv != NULL && *rv == '\0');
809 NYD_LEAVE;
810 return rv;
813 /* s-it-mode */