SEND_TODISP(_ALL)?: append newline if message does have no final one
[s-mailx.git] / folder.c
blob409ef7977d8b61e256ec19821b43aa692dcb3944
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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 switch (which_protocol(name)) {
187 case PROTO_FILE:
188 if (temporary_protocol_ext != NULL)
189 name = savecat(name, temporary_protocol_ext);
190 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
191 #ifdef HAVE_REALPATH
192 do { /* TODO we need objects, goes away then */
193 # ifdef HAVE_REALPATH_NULL
194 char *cp;
196 if ((cp = realpath(name, NULL)) != NULL) {
197 name = savestr(cp);
198 (free)(cp);
200 # else
201 char cbuf[PATH_MAX];
203 if (realpath(name, cbuf) != NULL)
204 name = savestr(cbuf);
205 # endif
206 } while (0);
207 #endif
208 rv = 1;
209 break;
210 case PROTO_MAILDIR:
211 shudclob = 1;
212 rv = maildir_setfile(name, fm);
213 goto jleave;
214 #ifdef HAVE_POP3
215 case PROTO_POP3:
216 shudclob = 1;
217 rv = pop3_setfile(name, fm);
218 goto jleave;
219 #endif
220 default:
221 n_err(_("Cannot handle protocol: %s\n"), name);
222 goto jem1;
225 if ((ibuf = Zopen(name, "r")) == NULL) {
226 if ((fm & FEDIT_SYSBOX) && errno == ENOENT) {
227 if (strcmp(who, myname) && getpwnam(who) == NULL) {
228 n_err(_("%s is not a user of this system\n"),
229 n_shell_quote_cp(who, FAL0));
230 goto jem2;
232 if (!(options & OPT_QUICKRUN_MASK) && ok_blook(bsdcompat))
233 n_err(_("No mail for %s\n"), who);
234 if (fm & FEDIT_NEWMAIL)
235 goto jleave;
236 if (ok_blook(emptystart)) {
237 if (!(options & OPT_QUICKRUN_MASK) && !ok_blook(bsdcompat))
238 n_perr(name, 0);
239 /* We must avoid returning -1 and causing program exit */
240 mb.mb_type = MB_VOID;
241 rv = 1;
242 goto jleave;
244 goto jem2;
245 } else if (fm & FEDIT_NEWMAIL)
246 goto jleave;
247 n_perr(name, 0);
248 goto jem1;
251 if (fstat(fileno(ibuf), &stb) == -1) {
252 if (fm & FEDIT_NEWMAIL)
253 goto jleave;
254 n_perr(_("fstat"), 0);
255 goto jem1;
258 if (S_ISREG(stb.st_mode) || isdevnull) {
259 /* EMPTY */
260 } else {
261 if (fm & FEDIT_NEWMAIL)
262 goto jleave;
263 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
264 n_perr(name, 0);
265 goto jem1;
268 /* Looks like all will be well. We must now relinquish our hold on the
269 * current set of stuff. Must hold signals while we are reading the new
270 * file, else we will ruin the message[] data structure */
272 hold_sigs(); /* TODO note on this one in quit.c:quit() */
273 if (shudclob && !(fm & FEDIT_NEWMAIL) && !quit()) {
274 rele_sigs();
275 goto jem2;
278 #ifdef HAVE_SOCKETS
279 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
280 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
281 #endif
283 /* TODO There is no intermediate VOID box we've switched to: name may
284 * TODO point to the same box that we just have written, so any updates
285 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
286 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
287 /* TODO We may uselessly open twice but quit() doesn't say whether we were
288 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
289 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
290 name = mailname;
291 Fclose(ibuf);
293 if ((ibuf = Zopen(name, "r")) == NULL ||
294 fstat(fileno(ibuf), &stb) == -1 ||
295 (!S_ISREG(stb.st_mode) && !isdevnull)) {
296 n_perr(name, 0);
297 rele_sigs();
298 goto jem2;
302 /* Copy the messages into /tmp and set pointers */
303 if (!(fm & FEDIT_NEWMAIL)) {
304 mb.mb_type = MB_FILE;
305 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
306 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
307 if (shudclob) {
308 if (mb.mb_itf) {
309 fclose(mb.mb_itf);
310 mb.mb_itf = NULL;
312 if (mb.mb_otf) {
313 fclose(mb.mb_otf);
314 mb.mb_otf = NULL;
317 shudclob = 1;
318 if (fm & FEDIT_SYSBOX)
319 pstate &= ~PS_EDIT;
320 else
321 pstate |= PS_EDIT;
322 initbox(name);
323 offset = 0;
324 } else {
325 fseek(mb.mb_otf, 0L, SEEK_END);
326 /* TODO Doing this without holding a lock is.. And not err checking.. */
327 fseek(ibuf, mailsize, SEEK_SET);
328 offset = mailsize;
329 omsgCount = msgCount;
332 if (isdevnull)
333 lckfp = (FILE*)-1;
334 else if (!(pstate & PS_EDIT))
335 lckfp = n_dotlock(name, fileno(ibuf), FLT_READ, offset,0,
336 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX));
337 else if (n_file_lock(fileno(ibuf), FLT_READ, offset,0,
338 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX)))
339 lckfp = (FILE*)-1;
341 if (lckfp == NULL) {
342 if (!(fm & FEDIT_NEWMAIL)) {
343 char const *emsg = (pstate & PS_EDIT)
344 ? N_("Unable to lock mailbox, aborting operation")
345 : N_("Unable to (dot) lock mailbox, aborting operation");
346 n_perr(V_(emsg), 0);
348 rele_sigs();
349 if (!(fm & FEDIT_NEWMAIL))
350 goto jem1;
351 goto jleave;
354 mailsize = fsize(ibuf);
356 /* TODO This is too simple minded? We should regenerate an index file
357 * TODO to be able to truly tell whether *anything* has changed! */
358 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
359 rele_sigs();
360 goto jleave;
362 setptr(ibuf, offset);
363 setmsize(msgCount);
364 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
365 mb.mb_threaded = 0;
366 c_sort((void*)-1);
369 Fclose(ibuf);
370 ibuf = NULL;
371 if (lckfp != NULL && lckfp != (FILE*)-1) {
372 Pclose(lckfp, FAL0);
373 /*lckfp = NULL;*/
375 rele_sigs();
377 if (!(fm & FEDIT_NEWMAIL)) {
378 pstate &= ~PS_SAW_COMMAND;
379 pstate |= PS_SETFILE_OPENED;
382 if ((options & OPT_EXISTONLY) && !(options & OPT_HEADERLIST)) {
383 rv = (msgCount == 0);
384 goto jleave;
387 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
388 if (!(fm & FEDIT_NEWMAIL)) {
389 if (!ok_blook(emptystart))
390 n_err(_("No mail for %s\n"), who);
392 goto jleave;
395 if (fm & FEDIT_NEWMAIL)
396 newmailinfo(omsgCount);
398 rv = 0;
399 jleave:
400 if (ibuf != NULL) {
401 Fclose(ibuf);
402 if (lckfp != NULL && lckfp != (FILE*)-1)
403 Pclose(lckfp, FAL0);
405 NYD_LEAVE;
406 return rv;
407 jem2:
408 mb.mb_type = MB_VOID;
409 jem1:
410 rv = -1;
411 goto jleave;
414 FL int
415 newmailinfo(int omsgCount)
417 int mdot, i;
418 NYD_ENTER;
420 for (i = 0; i < omsgCount; ++i)
421 message[i].m_flag &= ~MNEWEST;
423 if (msgCount > omsgCount) {
424 for (i = omsgCount; i < msgCount; ++i)
425 message[i].m_flag |= MNEWEST;
426 printf(_("New mail has arrived.\n"));
427 if ((i = msgCount - omsgCount) == 1)
428 printf(_("Loaded 1 new message.\n"));
429 else
430 printf(_("Loaded %d new messages.\n"), i);
431 } else
432 printf(_("Loaded %d messages.\n"), msgCount);
434 check_folder_hook(TRU1);
436 mdot = getmdot(1);
438 if (ok_blook(header))
439 print_headers(omsgCount + 1, msgCount, FAL0);
440 NYD_LEAVE;
441 return mdot;
444 FL void
445 setmsize(int sz)
447 NYD_ENTER;
448 if (n_msgvec != NULL)
449 free(n_msgvec);
450 n_msgvec = scalloc(sz + 1, sizeof *n_msgvec);
451 NYD_LEAVE;
454 FL void
455 print_header_summary(char const *Larg)
457 size_t bot, top, i, j;
458 NYD_ENTER;
460 if (Larg != NULL) {
461 /* Avoid any messages XXX add a make_mua_silent() and use it? */
462 if ((options & (OPT_VERB | OPT_EXISTONLY)) == OPT_EXISTONLY) {
463 freopen("/dev/null", "w", stdout);
464 freopen("/dev/null", "w", stderr);
466 assert(n_msgvec != NULL);
467 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), n_msgvec, 0) <= 0);
468 if (options & OPT_EXISTONLY) {
469 exit_status = (int)i;
470 goto jleave;
472 if (i)
473 goto jleave;
474 for (bot = msgCount, top = 0, i = 0; (j = n_msgvec[i]) != 0; ++i) {
475 if (bot > j)
476 bot = j;
477 if (top < j)
478 top = j;
480 } else
481 bot = 1, top = msgCount;
482 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
483 jleave:
484 NYD_LEAVE;
487 FL void
488 announce(int printheaders)
490 int vec[2], mdot;
491 NYD_ENTER;
493 mdot = newfileinfo();
494 vec[0] = mdot;
495 vec[1] = 0;
496 dot = message + mdot - 1;
497 if (printheaders && msgCount > 0 && ok_blook(header)) {
498 print_header_group(vec); /* XXX errors? */
500 NYD_LEAVE;
503 FL int
504 newfileinfo(void)
506 struct message *mp;
507 int u, n, mdot, d, s, hidden, moved;
508 NYD_ENTER;
510 if (mb.mb_type == MB_VOID) {
511 mdot = 1;
512 goto jleave;
515 mdot = getmdot(0);
516 s = d = hidden = moved =0;
517 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
518 if (mp->m_flag & MNEW)
519 ++n;
520 if ((mp->m_flag & MREAD) == 0)
521 ++u;
522 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
523 ++moved;
524 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
525 ++d;
526 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
527 ++s;
528 if (mp->m_flag & MHIDDEN)
529 ++hidden;
532 /* If displayname gets truncated the user effectively has no option to see
533 * the full pathname of the mailbox, so print it at least for '? fi' */
534 printf(_("%s: "), n_shell_quote_cp(
535 (_update_mailname(NULL) ? displayname : mailname), FAL0));
536 if (msgCount == 1)
537 printf(_("1 message"));
538 else
539 printf(_("%d messages"), msgCount);
540 if (n > 0)
541 printf(_(" %d new"), n);
542 if (u-n > 0)
543 printf(_(" %d unread"), u);
544 if (d > 0)
545 printf(_(" %d deleted"), d);
546 if (s > 0)
547 printf(_(" %d saved"), s);
548 if (moved > 0)
549 printf(_(" %d moved"), moved);
550 if (hidden > 0)
551 printf(_(" %d hidden"), hidden);
552 else if (mb.mb_perm == 0)
553 printf(_(" [Read only]"));
554 printf("\n");
555 jleave:
556 NYD_LEAVE;
557 return mdot;
560 FL int
561 getmdot(int nmail)
563 struct message *mp;
564 char *cp;
565 int mdot;
566 enum mflag avoid = MHIDDEN | MDELETED;
567 NYD_ENTER;
569 if (!nmail) {
570 if (ok_blook(autothread)) {
571 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
572 c_thread(NULL);
573 } else if ((cp = ok_vlook(autosort)) != NULL) {
574 if (mb.mb_sorted != NULL)
575 free(mb.mb_sorted);
576 mb.mb_sorted = sstrdup(cp);
577 c_sort(NULL);
580 if (mb.mb_type == MB_VOID) {
581 mdot = 1;
582 goto jleave;
585 if (nmail)
586 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
587 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
588 break;
590 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
591 if (mb.mb_threaded) {
592 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
593 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
594 break;
595 } else {
596 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
597 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
598 break;
602 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
603 if (mb.mb_threaded) {
604 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
605 if (mp->m_flag & MFLAGGED)
606 break;
607 } else {
608 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
609 if (mp->m_flag & MFLAGGED)
610 break;
614 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
615 if (mb.mb_threaded) {
616 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
617 if (!(mp->m_flag & (MREAD | avoid)))
618 break;
619 } else {
620 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
621 if (!(mp->m_flag & (MREAD | avoid)))
622 break;
626 if (nmail &&
627 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
628 mdot = (int)PTR2SIZE(mp - message + 1);
629 else if (ok_blook(showlast)) {
630 if (mb.mb_threaded) {
631 for (mp = this_in_thread(threadroot, -1); mp;
632 mp = prev_in_thread(mp))
633 if (!(mp->m_flag & avoid))
634 break;
635 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
636 } else {
637 for (mp = message + msgCount - 1; mp >= message; --mp)
638 if (!(mp->m_flag & avoid))
639 break;
640 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
642 } else if (!nmail &&
643 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
644 mdot = (int)PTR2SIZE(mp - message + 1);
645 else if (mb.mb_threaded) {
646 for (mp = threadroot; mp; mp = next_in_thread(mp))
647 if (!(mp->m_flag & avoid))
648 break;
649 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
650 } else {
651 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
652 if (!(mp->m_flag & avoid))
653 break;
654 mdot = PTRCMP(mp, <, message + msgCount)
655 ? (int)PTR2SIZE(mp - message + 1) : 1;
657 jleave:
658 NYD_LEAVE;
659 return mdot;
662 FL void
663 initbox(char const *name)
665 char *tempMesg;
666 NYD_ENTER;
668 if (mb.mb_type != MB_VOID)
669 n_strscpy(prevfile, mailname, PATH_MAX);
671 /* TODO name always NE mailname (but goes away for objects anyway)
672 * TODO Well, not true no more except that in parens */
673 _update_mailname((name != mailname) ? name : NULL);
675 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
676 NULL) {
677 n_perr(_("temporary mail message file"), 0);
678 exit(EXIT_ERR);
680 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
681 n_perr(_("temporary mail message file"), 0);
682 exit(EXIT_ERR);
684 Ftmp_release(&tempMesg);
686 message_reset();
687 mb.mb_threaded = 0;
688 if (mb.mb_sorted != NULL) {
689 free(mb.mb_sorted);
690 mb.mb_sorted = NULL;
692 dot = prevdot = threadroot = NULL;
693 pstate &= ~PS_DID_PRINT_DOT;
694 NYD_LEAVE;
697 FL char const *
698 folder_query(void){
699 struct n_string s, *sp = &s;
700 char *cp;
701 char const *rv;
702 bool_t err;
703 NYD_ENTER;
705 /* *folder* is linked with *_folder_resolved*: we only use the latter */
706 for(err = FAL0;;){
707 if((rv = ok_vlook(_folder_resolved)) != NULL)
708 break;
710 /* POSIX says:
711 * If directory does not start with a <slash> ('/'), the contents
712 * of HOME shall be prefixed to it.
713 * And:
714 * If folder is unset or set to null, [.] filenames beginning with
715 * '+' shall refer to files in the current directory.
716 * We may have the result already */
717 rv = "";
718 err = FAL0;
720 if((cp = ok_vlook(folder)) == NULL)
721 goto jset;
723 /* Expand the *folder*; skip %: prefix for simplicity of use */
724 if(cp[0] == '%' && cp[1] == ':')
725 cp += 2;
726 if((err = (cp = fexpand(cp, FEXP_NSHELL)) == NULL) || *cp == '\0')
727 goto jset;
729 switch(which_protocol(cp)){
730 case PROTO_POP3:
731 n_err(_("*folder* can't be set to a flat, readonly POP3 account\n"));
732 err = TRU1;
733 goto jset;
734 default:
735 /* Further expansion desired */
736 break;
739 /* Prefix HOME as necessary */
740 if(*cp != '/'){
741 char const *home = ok_vlook(HOME);
742 size_t l1 = strlen(home), l2 = strlen(cp);
744 sp = n_string_creat_auto(sp);
745 sp = n_string_reserve(sp, l1 + 1 + l2 +1);
746 sp = n_string_push_buf(sp, home, l1);
747 sp = n_string_push_c(sp, '/');
748 sp = n_string_push_buf(sp, cp, l2);
749 cp = n_string_cp(sp);
752 rv = cp;
754 /* TODO Since our visual mailname is resolved via realpath(3) if available
755 * TODO to avoid that we loose track of our currently open folder in case
756 * TODO we chdir away, but still checks the leading path portion against
757 * TODO fold_query() to be able to abbreviate to the +FOLDER syntax if
758 * TODO possible, we need to realpath(3) the folder, too */
759 #ifdef HAVE_REALPATH
760 /* C99 */{
761 # ifndef HAVE_REALPATH_NULL
762 if(cp == sp->s_dat)
763 sp = n_string_drop_ownership(sp);
764 sp = n_string_reserve(sp, PATH_MAX +1);
765 # else
766 sp->s_dat = NULL;
767 # endif
769 if((sp->s_dat = realpath(cp, sp->s_dat)) != NULL){
770 # ifdef HAVE_REALPATH_NULL
771 sp->s_dat = savestr(cp = sp->s_dat);
772 (free)(cp);
773 # endif
774 rv = sp->s_dat;
775 }else if(errno == ENOENT)
776 rv = cp;
777 else{
778 n_err(_("Can't canonicalize *folder*: %s\n"),
779 n_shell_quote_cp(cp, FAL0));
780 err = TRU1;
781 rv = "";
784 #endif /* HAVE_REALPATH */
786 jset:
787 /* C99 */{
788 bool_t reset = !(pstate & PS_ROOT);
790 pstate |= PS_ROOT;
791 ok_vset(_folder_resolved, rv);
792 if(reset)
793 pstate &= ~PS_ROOT;
797 if(err){
798 n_err(_("*folder* is not resolvable, using CWD\n"));
799 assert(rv != NULL && *rv == '\0');
801 NYD_LEAVE;
802 return rv;
805 /* s-it-mode */