`write'++: !interactive: urlxenc() attachment paths (Ralph Corderoy)..
[s-mailx.git] / folder.c
blob1c3aba796a1c53819b6a80da6866b459b5645519
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_shexp_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_shexp_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);
238 if (fm & FEDIT_NEWMAIL)
239 goto jleave;
241 mb.mb_type = MB_VOID;
242 if (ok_blook(emptystart)) {
243 if (!(options & OPT_QUICKRUN_MASK) && !ok_blook(bsdcompat))
244 n_perr(name, 0);
245 /* We must avoid returning -1 and causing program exit */
246 rv = 1;
247 goto jleave;
249 n_perr(name, 0);
250 goto jem1;
253 if (fstat(fileno(ibuf), &stb) == -1) {
254 if (fm & FEDIT_NEWMAIL)
255 goto jleave;
256 n_perr(_("fstat"), 0);
257 goto jem1;
260 if (S_ISREG(stb.st_mode) || isdevnull) {
261 /* EMPTY */
262 } else {
263 if (fm & FEDIT_NEWMAIL)
264 goto jleave;
265 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
266 n_perr(name, 0);
267 goto jem1;
270 /* Looks like all will be well. We must now relinquish our hold on the
271 * current set of stuff. Must hold signals while we are reading the new
272 * file, else we will ruin the message[] data structure */
274 hold_sigs(); /* TODO note on this one in quit.c:quit() */
275 if (shudclob && !(fm & FEDIT_NEWMAIL) && !quit()) {
276 rele_sigs();
277 goto jem2;
280 #ifdef HAVE_SOCKETS
281 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
282 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
283 #endif
285 /* TODO There is no intermediate VOID box we've switched to: name may
286 * TODO point to the same box that we just have written, so any updates
287 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
288 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
289 /* TODO We may uselessly open twice but quit() doesn't say whether we were
290 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
291 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
292 name = mailname;
293 Fclose(ibuf);
295 if ((ibuf = Zopen(name, "r")) == NULL ||
296 fstat(fileno(ibuf), &stb) == -1 ||
297 (!S_ISREG(stb.st_mode) && !isdevnull)) {
298 n_perr(name, 0);
299 rele_sigs();
300 goto jem2;
304 /* Copy the messages into /tmp and set pointers */
305 if (!(fm & FEDIT_NEWMAIL)) {
306 if (isdevnull) {
307 mb.mb_type = MB_VOID;
308 mb.mb_perm = 0;
309 } else {
310 mb.mb_type = MB_FILE;
311 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
312 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
313 if (shudclob) {
314 if (mb.mb_itf) {
315 fclose(mb.mb_itf);
316 mb.mb_itf = NULL;
318 if (mb.mb_otf) {
319 fclose(mb.mb_otf);
320 mb.mb_otf = NULL;
324 shudclob = 1;
325 if (fm & FEDIT_SYSBOX)
326 pstate &= ~PS_EDIT;
327 else
328 pstate |= PS_EDIT;
329 initbox(name);
330 offset = 0;
331 } else {
332 fseek(mb.mb_otf, 0L, SEEK_END);
333 /* TODO Doing this without holding a lock is.. And not err checking.. */
334 fseek(ibuf, mailsize, SEEK_SET);
335 offset = mailsize;
336 omsgCount = msgCount;
339 if (isdevnull)
340 lckfp = (FILE*)-1;
341 else if (!(pstate & PS_EDIT))
342 lckfp = n_dotlock(name, fileno(ibuf), FLT_READ, offset,0,
343 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX));
344 else if (n_file_lock(fileno(ibuf), FLT_READ, offset,0,
345 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX)))
346 lckfp = (FILE*)-1;
348 if (lckfp == NULL) {
349 if (!(fm & FEDIT_NEWMAIL)) {
350 char const *emsg = (pstate & PS_EDIT)
351 ? N_("Unable to lock mailbox, aborting operation")
352 : N_("Unable to (dot) lock mailbox, aborting operation");
353 n_perr(V_(emsg), 0);
355 rele_sigs();
356 if (!(fm & FEDIT_NEWMAIL))
357 goto jem1;
358 goto jleave;
361 mailsize = fsize(ibuf);
363 /* TODO This is too simple minded? We should regenerate an index file
364 * TODO to be able to truly tell whether *anything* has changed! */
365 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
366 rele_sigs();
367 goto jleave;
369 setptr(ibuf, offset);
370 setmsize(msgCount);
371 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
372 mb.mb_threaded = 0;
373 c_sort((void*)-1);
376 Fclose(ibuf);
377 ibuf = NULL;
378 if (lckfp != NULL && lckfp != (FILE*)-1) {
379 Pclose(lckfp, FAL0);
380 /*lckfp = NULL;*/
382 rele_sigs();
384 if (!(fm & FEDIT_NEWMAIL)) {
385 pstate &= ~PS_SAW_COMMAND;
386 pstate |= PS_SETFILE_OPENED;
389 if ((options & OPT_EXISTONLY) && !(options & OPT_HEADERLIST)) {
390 rv = (msgCount == 0);
391 goto jleave;
394 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
395 if (!(fm & FEDIT_NEWMAIL)) {
396 if (!ok_blook(emptystart))
397 n_err(_("No mail for %s\n"), who);
399 goto jleave;
402 if (fm & FEDIT_NEWMAIL)
403 newmailinfo(omsgCount);
405 rv = 0;
406 jleave:
407 if (ibuf != NULL) {
408 Fclose(ibuf);
409 if (lckfp != NULL && lckfp != (FILE*)-1)
410 Pclose(lckfp, FAL0);
412 NYD_LEAVE;
413 return rv;
414 jem2:
415 mb.mb_type = MB_VOID;
416 jem1:
417 rv = -1;
418 goto jleave;
421 FL int
422 newmailinfo(int omsgCount)
424 int mdot, i;
425 NYD_ENTER;
427 for (i = 0; i < omsgCount; ++i)
428 message[i].m_flag &= ~MNEWEST;
430 if (msgCount > omsgCount) {
431 for (i = omsgCount; i < msgCount; ++i)
432 message[i].m_flag |= MNEWEST;
433 printf(_("New mail has arrived.\n"));
434 if ((i = msgCount - omsgCount) == 1)
435 printf(_("Loaded 1 new message.\n"));
436 else
437 printf(_("Loaded %d new messages.\n"), i);
438 } else
439 printf(_("Loaded %d messages.\n"), msgCount);
441 check_folder_hook(TRU1);
443 mdot = getmdot(1);
445 if (ok_blook(header))
446 print_headers(omsgCount + 1, msgCount, FAL0);
447 NYD_LEAVE;
448 return mdot;
451 FL void
452 setmsize(int sz)
454 NYD_ENTER;
455 if (n_msgvec != NULL)
456 free(n_msgvec);
457 n_msgvec = scalloc(sz + 1, sizeof *n_msgvec);
458 NYD_LEAVE;
461 FL void
462 print_header_summary(char const *Larg)
464 size_t bot, top, i, j;
465 NYD_ENTER;
467 if (Larg != NULL) {
468 /* Avoid any messages XXX add a make_mua_silent() and use it? */
469 if ((options & (OPT_VERB | OPT_EXISTONLY)) == OPT_EXISTONLY) {
470 freopen("/dev/null", "w", stdout);
471 freopen("/dev/null", "w", stderr);
473 assert(n_msgvec != NULL);
474 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), n_msgvec, 0) <= 0);
475 if (options & OPT_EXISTONLY) {
476 exit_status = (int)i;
477 goto jleave;
479 if (i)
480 goto jleave;
481 for (bot = msgCount, top = 0, i = 0; (j = n_msgvec[i]) != 0; ++i) {
482 if (bot > j)
483 bot = j;
484 if (top < j)
485 top = j;
487 } else
488 bot = 1, top = msgCount;
489 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
490 jleave:
491 NYD_LEAVE;
494 FL void
495 announce(int printheaders)
497 int vec[2], mdot;
498 NYD_ENTER;
500 mdot = newfileinfo();
501 vec[0] = mdot;
502 vec[1] = 0;
503 dot = message + mdot - 1;
504 if (printheaders && msgCount > 0 && ok_blook(header)) {
505 print_header_group(vec); /* XXX errors? */
507 NYD_LEAVE;
510 FL int
511 newfileinfo(void)
513 struct message *mp;
514 int u, n, mdot, d, s, hidden, moved;
515 NYD_ENTER;
517 if (mb.mb_type == MB_VOID) {
518 mdot = 1;
519 goto jleave;
522 mdot = getmdot(0);
523 s = d = hidden = moved =0;
524 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
525 if (mp->m_flag & MNEW)
526 ++n;
527 if ((mp->m_flag & MREAD) == 0)
528 ++u;
529 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
530 ++moved;
531 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
532 ++d;
533 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
534 ++s;
535 if (mp->m_flag & MHIDDEN)
536 ++hidden;
539 /* If displayname gets truncated the user effectively has no option to see
540 * the full pathname of the mailbox, so print it at least for '? fi' */
541 printf(_("%s: "), n_shexp_quote_cp(
542 (_update_mailname(NULL) ? displayname : mailname), FAL0));
543 if (msgCount == 1)
544 printf(_("1 message"));
545 else
546 printf(_("%d messages"), msgCount);
547 if (n > 0)
548 printf(_(" %d new"), n);
549 if (u-n > 0)
550 printf(_(" %d unread"), u);
551 if (d > 0)
552 printf(_(" %d deleted"), d);
553 if (s > 0)
554 printf(_(" %d saved"), s);
555 if (moved > 0)
556 printf(_(" %d moved"), moved);
557 if (hidden > 0)
558 printf(_(" %d hidden"), hidden);
559 else if (mb.mb_perm == 0)
560 printf(_(" [Read only]"));
561 printf("\n");
562 jleave:
563 NYD_LEAVE;
564 return mdot;
567 FL int
568 getmdot(int nmail)
570 struct message *mp;
571 char *cp;
572 int mdot;
573 enum mflag avoid = MHIDDEN | MDELETED;
574 NYD_ENTER;
576 if (!nmail) {
577 if (ok_blook(autothread)) {
578 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
579 c_thread(NULL);
580 } else if ((cp = ok_vlook(autosort)) != NULL) {
581 if (mb.mb_sorted != NULL)
582 free(mb.mb_sorted);
583 mb.mb_sorted = sstrdup(cp);
584 c_sort(NULL);
587 if (mb.mb_type == MB_VOID) {
588 mdot = 1;
589 goto jleave;
592 if (nmail)
593 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
594 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
595 break;
597 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
598 if (mb.mb_threaded) {
599 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
600 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
601 break;
602 } else {
603 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
604 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
605 break;
609 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
610 if (mb.mb_threaded) {
611 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
612 if (mp->m_flag & MFLAGGED)
613 break;
614 } else {
615 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
616 if (mp->m_flag & MFLAGGED)
617 break;
621 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
622 if (mb.mb_threaded) {
623 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
624 if (!(mp->m_flag & (MREAD | avoid)))
625 break;
626 } else {
627 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
628 if (!(mp->m_flag & (MREAD | avoid)))
629 break;
633 if (nmail &&
634 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
635 mdot = (int)PTR2SIZE(mp - message + 1);
636 else if (ok_blook(showlast)) {
637 if (mb.mb_threaded) {
638 for (mp = this_in_thread(threadroot, -1); mp;
639 mp = prev_in_thread(mp))
640 if (!(mp->m_flag & avoid))
641 break;
642 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
643 } else {
644 for (mp = message + msgCount - 1; mp >= message; --mp)
645 if (!(mp->m_flag & avoid))
646 break;
647 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
649 } else if (!nmail &&
650 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
651 mdot = (int)PTR2SIZE(mp - message + 1);
652 else if (mb.mb_threaded) {
653 for (mp = threadroot; mp; mp = next_in_thread(mp))
654 if (!(mp->m_flag & avoid))
655 break;
656 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
657 } else {
658 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
659 if (!(mp->m_flag & avoid))
660 break;
661 mdot = PTRCMP(mp, <, message + msgCount)
662 ? (int)PTR2SIZE(mp - message + 1) : 1;
664 jleave:
665 NYD_LEAVE;
666 return mdot;
669 FL void
670 initbox(char const *name)
672 char *tempMesg;
673 NYD_ENTER;
675 if (mb.mb_type != MB_VOID)
676 n_strscpy(prevfile, mailname, PATH_MAX);
678 /* TODO name always NE mailname (but goes away for objects anyway)
679 * TODO Well, not true no more except that in parens */
680 _update_mailname((name != mailname) ? name : NULL);
682 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
683 NULL) {
684 n_perr(_("temporary mail message file"), 0);
685 exit(EXIT_ERR);
687 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
688 n_perr(_("temporary mail message file"), 0);
689 exit(EXIT_ERR);
691 Ftmp_release(&tempMesg);
693 message_reset();
694 mb.mb_threaded = 0;
695 if (mb.mb_sorted != NULL) {
696 free(mb.mb_sorted);
697 mb.mb_sorted = NULL;
699 dot = prevdot = threadroot = NULL;
700 pstate &= ~PS_DID_PRINT_DOT;
701 NYD_LEAVE;
704 FL char const *
705 folder_query(void){
706 struct n_string s, *sp = &s;
707 char *cp;
708 char const *rv;
709 bool_t err;
710 NYD_ENTER;
712 /* *folder* is linked with *_folder_resolved*: we only use the latter */
713 for(err = FAL0;;){
714 if((rv = ok_vlook(_folder_resolved)) != NULL)
715 break;
717 /* POSIX says:
718 * If directory does not start with a <slash> ('/'), the contents
719 * of HOME shall be prefixed to it.
720 * And:
721 * If folder is unset or set to null, [.] filenames beginning with
722 * '+' shall refer to files in the current directory.
723 * We may have the result already */
724 rv = "";
725 err = FAL0;
727 if((cp = ok_vlook(folder)) == NULL)
728 goto jset;
730 /* Expand the *folder*; skip %: prefix for simplicity of use */
731 if(cp[0] == '%' && cp[1] == ':')
732 cp += 2;
733 if((err = (cp = fexpand(cp, FEXP_NSHELL)) == NULL) || *cp == '\0')
734 goto jset;
736 switch(which_protocol(cp)){
737 case PROTO_POP3:
738 n_err(_("*folder* can't be set to a flat, readonly POP3 account\n"));
739 err = TRU1;
740 goto jset;
741 default:
742 /* Further expansion desired */
743 break;
746 /* Prefix HOME as necessary */
747 if(*cp != '/'){
748 char const *home = ok_vlook(HOME);
749 size_t l1 = strlen(home), l2 = strlen(cp);
751 sp = n_string_creat_auto(sp);
752 sp = n_string_reserve(sp, l1 + 1 + l2 +1);
753 sp = n_string_push_buf(sp, home, l1);
754 sp = n_string_push_c(sp, '/');
755 sp = n_string_push_buf(sp, cp, l2);
756 cp = n_string_cp(sp);
759 rv = cp;
761 /* TODO Since our visual mailname is resolved via realpath(3) if available
762 * TODO to avoid that we loose track of our currently open folder in case
763 * TODO we chdir away, but still checks the leading path portion against
764 * TODO fold_query() to be able to abbreviate to the +FOLDER syntax if
765 * TODO possible, we need to realpath(3) the folder, too */
766 #ifdef HAVE_REALPATH
767 /* C99 */{
768 # ifndef HAVE_REALPATH_NULL
769 if(cp == sp->s_dat)
770 sp = n_string_drop_ownership(sp);
771 sp = n_string_reserve(sp, PATH_MAX +1);
772 # else
773 sp->s_dat = NULL;
774 # endif
776 if((sp->s_dat = realpath(cp, sp->s_dat)) != NULL){
777 # ifdef HAVE_REALPATH_NULL
778 sp->s_dat = savestr(cp = sp->s_dat);
779 (free)(cp);
780 # endif
781 rv = sp->s_dat;
782 }else if(errno == ENOENT)
783 rv = cp;
784 else{
785 n_err(_("Can't canonicalize *folder*: %s\n"),
786 n_shexp_quote_cp(cp, FAL0));
787 err = TRU1;
788 rv = "";
791 #endif /* HAVE_REALPATH */
793 jset:
794 /* C99 */{
795 bool_t reset = !(pstate & PS_ROOT);
797 pstate |= PS_ROOT;
798 ok_vset(_folder_resolved, rv);
799 if(reset)
800 pstate &= ~PS_ROOT;
804 if(err){
805 n_err(_("*folder* is not resolvable, using CWD\n"));
806 assert(rv != NULL && *rv == '\0');
808 NYD_LEAVE;
809 return rv;
812 /* s-it-mode */