Support an object lifetime for memory_pool_{push,pop}()
[s-mailx.git] / folder.c
blobd38abe9c41fc674110b50f45199c9f2e9d83f923
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 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8 /*
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 #undef n_FILE
37 #define n_FILE folder
39 #ifndef HAVE_AMALGAMATION
40 # include "nail.h"
41 #endif
43 #include <pwd.h>
45 /* Update mailname (if name != NULL) and displayname, return whether displayname
46 * was large enough to swallow mailname */
47 static bool_t _update_mailname(char const *name);
48 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
49 n_INLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
50 #endif
52 /**/
53 static void a_folder_info(void);
55 /* Set up the input pointers while copying the mail file into /tmp */
56 static void a_folder_mbox_setptr(FILE *ibuf, off_t offset);
58 static bool_t
59 _update_mailname(char const *name) /* TODO 2MUCH work, cache, prop of Object! */
61 char const *foldp;
62 char *mailp, *dispp;
63 size_t i, j, foldlen;
64 bool_t rv;
65 NYD_ENTER;
67 /* Don't realpath(3) if it's only an update request */
68 if(name != NULL){
69 #ifdef HAVE_REALPATH
70 char const *adjname;
71 enum protocol p;
73 p = which_protocol(name, TRU1, TRU1, &adjname);
75 if(p == PROTO_FILE || p == PROTO_MAILDIR){
76 name = adjname;
77 if (realpath(name, mailname) == NULL && n_err_no != n_ERR_NOENT) {
78 n_err(_("Can't canonicalize %s\n"), n_shexp_quote_cp(name, FAL0));
79 goto jdocopy;
81 }else
82 jdocopy:
83 #endif
84 n_strscpy(mailname, name, sizeof(mailname));
87 mailp = mailname;
88 dispp = displayname;
90 /* Don't display an absolute path but "+FOLDER" if under *folder* */
91 if(*(foldp = n_folder_query()) != '\0'){
92 foldlen = strlen(foldp);
93 if(strncmp(foldp, mailp, foldlen))
94 foldlen = 0;
95 }else
96 foldlen = 0;
98 /* We want to see the name of the folder .. on the screen */
99 i = strlen(mailp);
100 if(i < sizeof(displayname) - 3 -1){
101 if(foldlen > 0){
102 *dispp++ = '+';
103 *dispp++ = '[';
104 memcpy(dispp, mailp, foldlen);
105 dispp += foldlen;
106 mailp += foldlen;
107 *dispp++ = ']';
108 memcpy(dispp, mailp, i -= foldlen);
109 dispp[i] = '\0';
110 }else
111 memcpy(dispp, mailp, i +1);
112 rv = TRU1;
113 }else{
114 /* Avoid disrupting multibyte sequences (if possible) */
115 #ifndef HAVE_C90AMEND1
116 j = sizeof(displayname) / 3 - 3;
117 i -= sizeof(displayname) - (1/* + */ + 3) - j;
118 #else
119 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
120 i = j + __narrow_suffix(mailp + j, i - j,
121 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
122 #endif
123 snprintf(dispp, sizeof(displayname), "%s%.*s...%s",
124 (foldlen > 0 ? "[+]" : ""), (int)j, mailp, mailp + i);
125 rv = FAL0;
128 n_PS_ROOT_BLOCK((ok_vset(mailbox_resolved, mailname),
129 ok_vset(mailbox_display, displayname)));
130 NYD_LEAVE;
131 return rv;
134 #ifdef HAVE_C90AMEND1
135 n_INLINE size_t
136 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
138 int err;
139 size_t i, ok;
140 NYD_ENTER;
142 for (err = ok = i = 0; cpl > maxl || err;) {
143 int ml = mblen(cp, cpl);
144 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
145 (void)mblen(NULL, 0);
146 err = 1;
147 ml = 1;
148 } else {
149 if (!err)
150 ok = i;
151 err = 0;
152 if (ml == 0)
153 break;
155 cp += ml;
156 i += ml;
157 cpl -= ml;
159 NYD_LEAVE;
160 return ok;
162 #endif /* HAVE_C90AMEND1 */
164 static void
165 a_folder_info(void){
166 struct message *mp;
167 int u, n, d, s, hidden, moved;
168 NYD2_ENTER;
170 if(mb.mb_type == MB_VOID){
171 fprintf(n_stdout, _("(Currently no active mailbox)"));
172 goto jleave;
175 s = d = hidden = moved = 0;
176 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
177 if (mp->m_flag & MNEW)
178 ++n;
179 if ((mp->m_flag & MREAD) == 0)
180 ++u;
181 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
182 ++moved;
183 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
184 ++d;
185 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
186 ++s;
187 if (mp->m_flag & MHIDDEN)
188 ++hidden;
191 /* If displayname gets truncated the user effectively has no option to see
192 * the full pathname of the mailbox, so print it at least for '? fi' */
193 fprintf(n_stdout, "%s: ", n_shexp_quote_cp(
194 (_update_mailname(NULL) ? displayname : mailname), FAL0));
195 if (msgCount == 1)
196 fprintf(n_stdout, _("1 message"));
197 else
198 fprintf(n_stdout, _("%d messages"), msgCount);
199 if (n > 0)
200 fprintf(n_stdout, _(" %d new"), n);
201 if (u-n > 0)
202 fprintf(n_stdout, _(" %d unread"), u);
203 if (d > 0)
204 fprintf(n_stdout, _(" %d deleted"), d);
205 if (s > 0)
206 fprintf(n_stdout, _(" %d saved"), s);
207 if (moved > 0)
208 fprintf(n_stdout, _(" %d moved"), moved);
209 if (hidden > 0)
210 fprintf(n_stdout, _(" %d hidden"), hidden);
211 if (mb.mb_perm == 0)
212 fprintf(n_stdout, _(" [Read-only]"));
213 #ifdef HAVE_IMAP
214 if (mb.mb_type == MB_CACHE)
215 fprintf(n_stdout, _(" [Disconnected]"));
216 #endif
218 jleave:
219 putc('\n', n_stdout);
220 NYD2_LEAVE;
223 static void
224 a_folder_mbox_setptr(FILE *ibuf, off_t offset) /* TODO Mailbox->setptr() */
226 struct message self;
227 char const *cp2;
228 char *linebuf, *cp;
229 int selfcnt, c;
230 bool_t need_rfc4155, maybe, inhead, from_;
231 size_t filesize, linesize, cnt;
232 NYD_ENTER;
234 memset(&self, 0, sizeof self);
235 self.m_flag = MUSED | MNEW | MNEWEST;
236 filesize = mailsize - offset;
237 offset = ftell(mb.mb_otf);
238 need_rfc4155 = ok_blook(mbox_rfc4155);
239 maybe = TRU1;
240 inhead = FAL0;
241 selfcnt = 0;
242 linebuf = NULL, linesize = 0; /* TODO string pool */
244 for (;;) {
245 if (fgetline(&linebuf, &linesize, &filesize, &cnt, ibuf, 0) == NULL) {
246 self.m_xsize = self.m_size;
247 self.m_xlines = self.m_lines;
248 self.m_content_info = CI_HAVE_HEADER | CI_HAVE_BODY;
249 if (selfcnt > 0)
250 message_append(&self);
251 message_append_null();
252 if (linebuf != NULL)
253 n_free(linebuf);
254 break;
257 #ifdef notdef
258 if (linebuf[0] == '\0')
259 linebuf[0] = '.';
260 #endif
261 /* XXX Convert CRLF to LF; this should be rethought in that
262 * XXX CRLF input should possibly end as CRLF output? */
263 if (cnt >= 2 && linebuf[cnt - 1] == '\n' && linebuf[cnt - 2] == '\r')
264 linebuf[--cnt - 1] = '\n';
265 fwrite(linebuf, sizeof *linebuf, cnt, mb.mb_otf);
266 if (ferror(mb.mb_otf)) {
267 n_perr(_("/tmp"), 0);
268 exit(n_EXIT_ERR);
270 if (linebuf[cnt - 1] == '\n')
271 linebuf[cnt - 1] = '\0';
272 /* TODO In v15 this should use a/the flat MIME parser in order to ignore
273 * TODO "From " when MIME boundaries are active -- whereas this opens
274 * TODO another can of worms, it very likely is better than messing up
275 * TODO MIME because of a "From " line! */
276 if (maybe && linebuf[0] == 'F' &&
277 (from_ = is_head(linebuf, cnt, TRU1)) &&
278 (from_ == TRU1 || !need_rfc4155)) {
279 /* TODO char date[n_FROM_DATEBUF];
280 * TODO extract_date_from_from_(linebuf, cnt, date);
281 * TODO self.m_time = 10000; */
282 if (from_ == TRUM1) {
283 if (n_poption & n_PO_D_V)
284 n_err(_("Invalid MBOX \"From_ line\": %.*s\n"),
285 (int)cnt, linebuf);
286 else if (!(mb.mb_active & MB_FROM__WARNED))
287 n_err(_("MBOX mailbox contains non-conforming From_ line(s)!\n"
288 " Message boundaries may have been falsely detected!\n"
289 " Setting variable *mbox-rfc4155* and reopen should improve "
290 "the result.\n"
291 " If so, make changes permanent: \"copy * SOME-FILE\". "
292 "Then unset *mbox-rfc4155*\n"));
293 mb.mb_active |= MB_FROM__WARNED;
295 self.m_xsize = self.m_size;
296 self.m_xlines = self.m_lines;
297 self.m_content_info = CI_HAVE_HEADER | CI_HAVE_BODY;
298 if (selfcnt++ > 0)
299 message_append(&self);
300 msgCount++;
301 self.m_flag = MUSED | MNEW | MNEWEST;
302 self.m_size = 0;
303 self.m_lines = 0;
304 self.m_block = mailx_blockof(offset);
305 self.m_offset = mailx_offsetof(offset);
306 inhead = TRU1;
307 } else if (linebuf[0] == 0) {
308 inhead = FAL0;
309 } else if (inhead) {
310 for (cp = linebuf, cp2 = "status";; ++cp) {
311 if ((c = *cp2++) == 0) {
312 while (c = *cp++, whitechar(c))
314 if (cp[-1] != ':')
315 break;
316 while ((c = *cp++) != '\0')
317 if (c == 'R')
318 self.m_flag |= MREAD;
319 else if (c == 'O')
320 self.m_flag &= ~MNEW;
321 break;
323 if (*cp != c && *cp != upperconv(c))
324 break;
326 for (cp = linebuf, cp2 = "x-status";; ++cp) {
327 if ((c = *cp2++) == 0) {
328 while ((c = *cp++, whitechar(c)))
330 if (cp[-1] != ':')
331 break;
332 while ((c = *cp++) != '\0')
333 if (c == 'F')
334 self.m_flag |= MFLAGGED;
335 else if (c == 'A')
336 self.m_flag |= MANSWERED;
337 else if (c == 'T')
338 self.m_flag |= MDRAFTED;
339 break;
341 if (*cp != c && *cp != upperconv(c))
342 break;
345 offset += cnt;
346 self.m_size += cnt;
347 ++self.m_lines;
348 maybe = (linebuf[0] == 0);
350 NYD_LEAVE;
353 FL int
354 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
356 /* TODO This all needs to be converted to URL:: and Mailbox:: */
357 static int shudclob;
359 struct stat stb;
360 size_t offset;
361 char const *who, *orig_name;
362 int rv, omsgCount = 0;
363 FILE *ibuf = NULL, *lckfp = NULL;
364 bool_t isdevnull = FAL0;
365 NYD_ENTER;
367 n_pstate &= ~n_PS_SETFILE_OPENED;
369 /* C99 */{
370 enum fexp_mode fexpm;
372 if((who = shortcut_expand(name)) != NULL){
373 fexpm = FEXP_NSHORTCUT/* XXX | FEXP_NSHELL*/;
374 name = who;
375 }else
376 fexpm = FEXP_NSHELL;
378 if(name[0] == '%'){
379 char const *cp;
381 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
382 if(*(who = &name[1]) == ':')
383 ++who;
384 if((cp = strrchr(who, '/')) != NULL)
385 who = &cp[1];
386 if(*who == '\0')
387 goto jlogname;
388 }else{
389 jlogname:
390 if(fm & FEDIT_ACCOUNT){
391 if((who = ok_vlook(account)) == NULL)
392 who = ACCOUNT_NULL;
393 who = savecatsep(_("account"), ' ', who);
394 }else
395 who = ok_vlook(LOGNAME);
398 if ((name = fexpand(name, fexpm)) == NULL)
399 goto jem1;
402 /* For at least substdate() users TODO -> eventloop tick */
403 time_current_update(&time_current, FAL0);
405 switch (which_protocol(orig_name = name, TRU1, TRU1, &name)) {
406 case PROTO_FILE:
407 isdevnull = ((n_poption & n_PO_BATCH_FLAG) &&
408 !strcmp(name, n_path_devnull));
409 #ifdef HAVE_REALPATH
410 do { /* TODO we need objects, goes away then */
411 # ifdef HAVE_REALPATH_NULL
412 char *cp;
414 if ((cp = realpath(name, NULL)) != NULL) {
415 name = savestr(cp);
416 (free)(cp);
418 # else
419 char cbuf[PATH_MAX];
421 if (realpath(name, cbuf) != NULL)
422 name = savestr(cbuf);
423 # endif
424 } while (0);
425 #endif
426 rv = 1;
427 break;
428 #ifdef HAVE_MAILDIR
429 case PROTO_MAILDIR:
430 shudclob = 1;
431 rv = maildir_setfile(who, name, fm);
432 goto jleave;
433 #endif
434 #ifdef HAVE_POP3
435 case PROTO_POP3:
436 shudclob = 1;
437 rv = pop3_setfile(who, orig_name, fm);
438 goto jleave;
439 #endif
440 #ifdef HAVE_IMAP
441 case PROTO_IMAP:
442 shudclob = 1;
443 if((fm & FEDIT_NEWMAIL) && mb.mb_type == MB_CACHE)
444 rv = 1;
445 else
446 rv = imap_setfile(who, orig_name, fm);
447 goto jleave;
448 #endif
449 default:
450 n_err(_("Cannot handle protocol: %s\n"), orig_name);
451 goto jem1;
454 if ((ibuf = n_fopen_any(savecat("file://", name), "r", NULL)) == NULL) {
455 int e = n_err_no;
457 if ((fm & FEDIT_SYSBOX) && e == n_ERR_NOENT) {
458 if (!(fm & FEDIT_ACCOUNT) && strcmp(who, ok_vlook(LOGNAME)) &&
459 getpwnam(who) == NULL) {
460 n_err(_("%s is not a user of this system\n"),
461 n_shexp_quote_cp(who, FAL0));
462 goto jem2;
464 if (!(n_poption & n_PO_QUICKRUN_MASK) && ok_blook(bsdcompat))
465 n_err(_("No mail for %s at %s\n"),
466 who, n_shexp_quote_cp(name, FAL0));
468 if (fm & FEDIT_NEWMAIL)
469 goto jleave;
471 mb.mb_type = MB_VOID;
472 if (ok_blook(emptystart)) {
473 if (!(n_poption & n_PO_QUICKRUN_MASK) && !ok_blook(bsdcompat))
474 n_perr(name, e);
475 /* We must avoid returning -1 and causing program exit */
476 rv = 1;
477 goto jleave;
479 n_perr(name, e);
480 goto jem1;
483 if (fstat(fileno(ibuf), &stb) == -1) {
484 if (fm & FEDIT_NEWMAIL)
485 goto jleave;
486 n_perr(_("fstat"), 0);
487 goto jem1;
490 if (S_ISREG(stb.st_mode) || isdevnull) {
491 /* EMPTY */
492 } else {
493 if (fm & FEDIT_NEWMAIL)
494 goto jleave;
495 n_err_no = S_ISDIR(stb.st_mode) ? n_ERR_ISDIR : n_ERR_INVAL;
496 n_perr(name, 0);
497 goto jem1;
500 if (shudclob && !(fm & FEDIT_NEWMAIL) && !quit(FAL0))
501 goto jem2;
503 hold_sigs();
505 #ifdef HAVE_SOCKETS
506 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
507 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
508 #endif
510 /* TODO There is no intermediate VOID box we've switched to: name may
511 * TODO point to the same box that we just have written, so any updates
512 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
513 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
514 /* TODO We may uselessly open twice but quit() doesn't say whether we were
515 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
516 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
517 name = mailname;
518 Fclose(ibuf);
520 if ((ibuf = n_fopen_any(name, "r", NULL)) == NULL ||
521 fstat(fileno(ibuf), &stb) == -1 ||
522 (!S_ISREG(stb.st_mode) && !isdevnull)) {
523 n_perr(name, 0);
524 rele_sigs();
525 goto jem2;
529 /* Copy the messages into /tmp and set pointers */
530 if (!(fm & FEDIT_NEWMAIL)) {
531 if (isdevnull) {
532 mb.mb_type = MB_VOID;
533 mb.mb_perm = 0;
534 } else {
535 mb.mb_type = MB_FILE;
536 mb.mb_perm = (((n_poption & n_PO_R_FLAG) || (fm & FEDIT_RDONLY) ||
537 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
538 if (shudclob) {
539 if (mb.mb_itf) {
540 fclose(mb.mb_itf);
541 mb.mb_itf = NULL;
543 if (mb.mb_otf) {
544 fclose(mb.mb_otf);
545 mb.mb_otf = NULL;
549 shudclob = 1;
550 if (fm & FEDIT_SYSBOX)
551 n_pstate &= ~n_PS_EDIT;
552 else
553 n_pstate |= n_PS_EDIT;
554 initbox(name);
555 offset = 0;
556 } else {
557 fseek(mb.mb_otf, 0L, SEEK_END);
558 /* TODO Doing this without holding a lock is.. And not err checking.. */
559 fseek(ibuf, mailsize, SEEK_SET);
560 offset = mailsize;
561 omsgCount = msgCount;
564 if (isdevnull)
565 lckfp = (FILE*)-1;
566 else if (!(n_pstate & n_PS_EDIT))
567 lckfp = n_dotlock(name, fileno(ibuf), FLT_READ, offset,0,
568 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX));
569 else if (n_file_lock(fileno(ibuf), FLT_READ, offset,0,
570 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX)))
571 lckfp = (FILE*)-1;
573 if (lckfp == NULL) {
574 if (!(fm & FEDIT_NEWMAIL)) {
575 char const *emsg = (n_pstate & n_PS_EDIT)
576 ? N_("Unable to lock mailbox, aborting operation")
577 : N_("Unable to (dot) lock mailbox, aborting operation");
578 n_perr(V_(emsg), 0);
580 rele_sigs();
581 if (!(fm & FEDIT_NEWMAIL))
582 goto jem1;
583 goto jleave;
586 mailsize = fsize(ibuf);
588 /* TODO This is too simple minded? We should regenerate an index file
589 * TODO to be able to truly tell whether *anything* has changed! */
590 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
591 rele_sigs();
592 goto jleave;
594 a_folder_mbox_setptr(ibuf, offset);
595 setmsize(msgCount);
596 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
597 mb.mb_threaded = 0;
598 c_sort((void*)-1);
601 Fclose(ibuf);
602 ibuf = NULL;
603 if (lckfp != NULL && lckfp != (FILE*)-1) {
604 Pclose(lckfp, FAL0);
605 /*lckfp = NULL;*/
608 if (!(fm & FEDIT_NEWMAIL)) {
609 n_pstate &= ~n_PS_SAW_COMMAND;
610 n_pstate |= n_PS_SETFILE_OPENED;
613 rele_sigs();
615 rv = (msgCount == 0);
616 if(rv)
617 n_err_no = n_ERR_NODATA;
619 if(n_poption & n_PO_EXISTONLY)
620 goto jleave;
622 if(rv){
623 if(!(n_pstate & n_PS_EDIT) || (fm & FEDIT_NEWMAIL)){
624 if(!(fm & FEDIT_NEWMAIL)){
625 if (!ok_blook(emptystart))
626 n_err(_("No mail for %s at %s\n"),
627 who, n_shexp_quote_cp(name, FAL0));
629 goto jleave;
633 if(fm & FEDIT_NEWMAIL)
634 newmailinfo(omsgCount);
635 jleave:
636 if (ibuf != NULL) {
637 Fclose(ibuf);
638 if (lckfp != NULL && lckfp != (FILE*)-1)
639 Pclose(lckfp, FAL0);
641 NYD_LEAVE;
642 return rv;
643 jem2:
644 mb.mb_type = MB_VOID;
645 jem1:
646 n_err_no = n_ERR_NOTOBACCO;
647 rv = -1;
648 goto jleave;
651 FL int
652 newmailinfo(int omsgCount)
654 int mdot, i;
655 NYD_ENTER;
657 for (i = 0; i < omsgCount; ++i)
658 message[i].m_flag &= ~MNEWEST;
660 if (msgCount > omsgCount) {
661 for (i = omsgCount; i < msgCount; ++i)
662 message[i].m_flag |= MNEWEST;
663 fprintf(n_stdout, _("New mail has arrived.\n"));
664 if ((i = msgCount - omsgCount) == 1)
665 fprintf(n_stdout, _("Loaded 1 new message.\n"));
666 else
667 fprintf(n_stdout, _("Loaded %d new messages.\n"), i);
668 } else
669 fprintf(n_stdout, _("Loaded %d messages.\n"), msgCount);
671 temporary_folder_hook_check(TRU1);
673 mdot = getmdot(1);
675 if(ok_blook(header) && (i = omsgCount + 1) <= msgCount){
676 #ifdef HAVE_IMAP
677 if(mb.mb_type == MB_IMAP)
678 imap_getheaders(i, msgCount); /* TODO not here */
679 #endif
680 for(omsgCount = 0; i <= msgCount; ++omsgCount, ++i)
681 n_msgvec[omsgCount] = i;
682 n_msgvec[omsgCount] = 0;
683 print_headers(n_msgvec, FAL0, FAL0);
685 NYD_LEAVE;
686 return mdot;
689 FL void
690 setmsize(int sz)
692 NYD_ENTER;
693 if (n_msgvec != NULL)
694 n_free(n_msgvec);
695 n_msgvec = n_calloc(sz +1, sizeof *n_msgvec);
696 NYD_LEAVE;
699 FL void
700 print_header_summary(char const *Larg)
702 size_t i;
703 NYD_ENTER;
705 getmdot(0);
706 #ifdef HAVE_IMAP
707 if(mb.mb_type == MB_IMAP)
708 imap_getheaders(0, msgCount); /* TODO not here */
709 #endif
710 assert(n_msgvec != NULL);
712 if (Larg != NULL) {
713 /* Avoid any messages XXX add a make_mua_silent() and use it? */
714 if ((n_poption & (n_PO_VERB | n_PO_EXISTONLY)) == n_PO_EXISTONLY) {
715 n_stdout = freopen(n_path_devnull, "w", stdout);
716 n_stderr = freopen(n_path_devnull, "w", stderr);
718 i = (n_getmsglist(n_shexp_quote_cp(Larg, FAL0), n_msgvec, 0, NULL) <= 0);
719 if (n_poption & n_PO_EXISTONLY)
720 n_exit_status = (int)i;
721 else if(i == 0)
722 print_headers(n_msgvec, TRU1, FAL0); /* TODO should be iterator! */
723 } else {
724 i = 0;
725 if(!mb.mb_threaded){
726 for(; UICMP(z, i, <, msgCount); ++i)
727 n_msgvec[i] = i + 1;
728 }else{
729 struct message *mp;
731 for(mp = threadroot; mp; ++i, mp = next_in_thread(mp))
732 n_msgvec[i] = (int)PTR2SIZE(mp - message + 1);
734 print_headers(n_msgvec, FAL0, TRU1); /* TODO should be iterator! */
736 NYD_LEAVE;
739 FL void
740 n_folder_announce(enum n_announce_flags af){
741 int vec[2], mdot;
742 NYD_ENTER;
744 mdot = (mb.mb_type == MB_VOID) ? 1 : getmdot(0);
745 dot = &message[mdot - 1];
747 if(af != n_ANNOUNCE_NONE && ok_blook(header) &&
748 ((af & n_ANNOUNCE_MAIN_CALL) ||
749 ((af & n_ANNOUNCE_CHANGE) && !ok_blook(posix))))
750 af |= n_ANNOUNCE_STATUS | n__ANNOUNCE_HEADER;
752 if(af & n_ANNOUNCE_STATUS){
753 a_folder_info();
754 af |= n__ANNOUNCE_ANY;
757 if(af & n__ANNOUNCE_HEADER){
758 if(!(af & n_ANNOUNCE_MAIN_CALL) && ok_blook(bsdannounce))
759 n_OBSOLETE(_("*bsdannounce* is now default behaviour"));
760 vec[0] = mdot;
761 vec[1] = 0;
762 print_header_group(vec); /* XXX errors? */
763 af |= n__ANNOUNCE_ANY;
766 if(af & n__ANNOUNCE_ANY)
767 fflush(n_stdout);
768 NYD_LEAVE;
771 FL int
772 getmdot(int nmail)
774 struct message *mp;
775 char *cp;
776 int mdot;
777 enum mflag avoid = MHIDDEN | MDELETED;
778 NYD_ENTER;
780 if (!nmail) {
781 if (ok_blook(autothread)) {
782 n_OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
783 c_thread(NULL);
784 } else if ((cp = ok_vlook(autosort)) != NULL) {
785 if (mb.mb_sorted != NULL)
786 n_free(mb.mb_sorted);
787 mb.mb_sorted = sstrdup(cp);
788 c_sort(NULL);
791 if (mb.mb_type == MB_VOID) {
792 mdot = 1;
793 goto jleave;
796 if (nmail)
797 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
798 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
799 break;
801 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
802 if (mb.mb_threaded) {
803 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
804 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
805 break;
806 } else {
807 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
808 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
809 break;
813 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
814 if (mb.mb_threaded) {
815 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
816 if (mp->m_flag & MFLAGGED)
817 break;
818 } else {
819 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
820 if (mp->m_flag & MFLAGGED)
821 break;
825 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
826 if (mb.mb_threaded) {
827 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
828 if (!(mp->m_flag & (MREAD | avoid)))
829 break;
830 } else {
831 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
832 if (!(mp->m_flag & (MREAD | avoid)))
833 break;
837 if (nmail &&
838 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
839 mdot = (int)PTR2SIZE(mp - message + 1);
840 else if (ok_blook(showlast)) {
841 if (mb.mb_threaded) {
842 for (mp = this_in_thread(threadroot, -1); mp;
843 mp = prev_in_thread(mp))
844 if (!(mp->m_flag & avoid))
845 break;
846 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
847 } else {
848 for (mp = message + msgCount - 1; mp >= message; --mp)
849 if (!(mp->m_flag & avoid))
850 break;
851 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
853 } else if (!nmail &&
854 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
855 mdot = (int)PTR2SIZE(mp - message + 1);
856 else if (mb.mb_threaded) {
857 for (mp = threadroot; mp; mp = next_in_thread(mp))
858 if (!(mp->m_flag & avoid))
859 break;
860 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
861 } else {
862 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
863 if (!(mp->m_flag & avoid))
864 break;
865 mdot = PTRCMP(mp, <, message + msgCount)
866 ? (int)PTR2SIZE(mp - message + 1) : 1;
868 jleave:
869 NYD_LEAVE;
870 return mdot;
873 FL void
874 initbox(char const *name)
876 bool_t err;
877 char *tempMesg;
878 NYD_ENTER;
880 if (mb.mb_type != MB_VOID)
881 n_strscpy(prevfile, mailname, PATH_MAX);
883 /* TODO name always NE mailname (but goes away for objects anyway)
884 * TODO Well, not true no more except that in parens */
885 _update_mailname((name != mailname) ? name : NULL);
887 err = FAL0;
888 if((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
889 NULL){
890 n_perr(_("initbox: temporary mail message file, writer"), 0);
891 err = TRU1;
892 }else if((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
893 n_perr(_("initbox: temporary mail message file, reader"), 0);
894 err = TRU1;
896 Ftmp_release(&tempMesg);
897 if(err)
898 exit(n_EXIT_ERR);
900 message_reset();
901 mb.mb_active = MB_NONE;
902 mb.mb_threaded = 0;
903 #ifdef HAVE_IMAP
904 mb.mb_flags = MB_NOFLAGS;
905 #endif
906 if (mb.mb_sorted != NULL) {
907 n_free(mb.mb_sorted);
908 mb.mb_sorted = NULL;
910 dot = prevdot = threadroot = NULL;
911 n_pstate &= ~n_PS_DID_PRINT_DOT;
912 NYD_LEAVE;
915 FL char const *
916 n_folder_query(void){
917 struct n_string s, *sp = &s;
918 enum protocol proto;
919 char *cp;
920 char const *rv, *adjcp;
921 bool_t err;
922 NYD_ENTER;
924 sp = n_string_creat_auto(sp);
926 /* *folder* is linked with *folder_resolved*: we only use the latter */
927 for(err = FAL0;;){
928 if((rv = ok_vlook(folder_resolved)) != NULL)
929 break;
931 /* POSIX says:
932 * If directory does not start with a <slash> ('/'), the contents
933 * of HOME shall be prefixed to it.
934 * And:
935 * If folder is unset or set to null, [.] filenames beginning with
936 * '+' shall refer to files in the current directory.
937 * We may have the result already */
938 rv = n_empty;
939 err = FAL0;
941 if((cp = ok_vlook(folder)) == NULL)
942 goto jset;
944 /* Expand the *folder*; skip %: prefix for simplicity of use */
945 if(cp[0] == '%' && cp[1] == ':')
946 cp += 2;
947 if((err = (cp = fexpand(cp, FEXP_NSPECIAL | FEXP_NFOLDER | FEXP_NSHELL)
948 ) == NULL) || *cp == '\0')
949 goto jset;
950 else{
951 size_t i;
953 for(i = strlen(cp);;){
954 if(--i == 0)
955 goto jset;
956 if(cp[i] != '/'){
957 cp[++i] = '\0';
958 break;
963 switch((proto = which_protocol(cp, FAL0, FAL0, &adjcp))){
964 case PROTO_POP3:
965 n_err(_("*folder* can't be set to a flat, read-only POP3 account\n"));
966 err = TRU1;
967 goto jset;
968 case PROTO_IMAP:
969 #ifdef HAVE_IMAP
970 rv = cp;
971 if(!strcmp(rv, protbase(rv)))
972 rv = savecatsep(rv, '/', n_empty);
973 #else
974 n_err(_("*folder*: IMAP support not compiled in\n"));
975 err = TRU1;
976 #endif
977 goto jset;
978 default:
979 /* Further expansion desired */
980 break;
983 /* Prefix HOME as necessary */
984 if(*adjcp != '/'){ /* XXX path_is_absolute() */
985 size_t l1, l2;
986 char const *home;
988 home = ok_vlook(HOME);
989 l1 = strlen(home);
990 l2 = strlen(cp);
992 sp = n_string_reserve(sp, l1 + 1 + l2 +1);
993 if(cp != adjcp){
994 size_t i;
996 sp = n_string_push_buf(sp, cp, i = PTR2SIZE(adjcp - cp));
997 cp += i;
998 l2 -= i;
1000 sp = n_string_push_buf(sp, home, l1);
1001 sp = n_string_push_c(sp, '/');
1002 sp = n_string_push_buf(sp, cp, l2);
1003 cp = n_string_cp(sp);
1004 sp = n_string_drop_ownership(sp);
1007 /* TODO Since our visual mailname is resolved via realpath(3) if available
1008 * TODO to avoid that we loose track of our currently open folder in case
1009 * TODO we chdir away, but still checks the leading path portion against
1010 * TODO n_folder_query() to be able to abbreviate to the +FOLDER syntax if
1011 * TODO possible, we need to realpath(3) the folder, too */
1012 #ifndef HAVE_REALPATH
1013 rv = cp;
1014 #else
1015 assert(sp->s_len == 0 && sp->s_dat == NULL);
1016 # ifndef HAVE_REALPATH_NULL
1017 sp = n_string_reserve(sp, PATH_MAX +1);
1018 # endif
1020 if((sp->s_dat = realpath(cp, sp->s_dat)) != NULL){
1021 # ifdef HAVE_REALPATH_NULL
1022 n_string_cp(sp = n_string_assign_cp(sp, cp = sp->s_dat));
1023 (free)(cp);
1024 # endif
1025 rv = sp->s_dat;
1026 }else if(n_err_no == n_ERR_NOENT)
1027 rv = cp;
1028 else{
1029 n_err(_("Can't canonicalize *folder*: %s\n"),
1030 n_shexp_quote_cp(cp, FAL0));
1031 err = TRU1;
1032 rv = n_empty;
1034 sp = n_string_drop_ownership(sp);
1035 #endif /* HAVE_REALPATH */
1037 /* Always append a solidus to our result path upon success */
1038 if(!err){
1039 size_t i;
1041 if(rv[(i = strlen(rv)) - 1] != '/'){
1042 sp = n_string_reserve(sp, i + 1 +1);
1043 sp = n_string_push_buf(sp, rv, i);
1044 sp = n_string_push_c(sp, '/');
1045 rv = n_string_cp(sp);
1046 sp = n_string_drop_ownership(sp);
1050 jset:
1051 n_PS_ROOT_BLOCK(ok_vset(folder_resolved, rv));
1054 if(err){
1055 n_err(_("*folder* is not resolvable, using CWD\n"));
1056 assert(rv != NULL && *rv == '\0');
1058 NYD_LEAVE;
1059 return rv;
1062 FL int
1063 n_folder_mbox_prepare_append(FILE *fout, struct stat *st_or_null){
1064 /* TODO n_folder_mbox_prepare_append -> Mailbox->append() */
1065 struct stat stb;
1066 char buf[2];
1067 int rv;
1068 bool_t needsep;
1069 NYD2_ENTER;
1071 if(!fseek(fout, -2L, SEEK_END)){
1072 if(fread(buf, sizeof *buf, 2, fout) != 2)
1073 goto jerrno;
1074 needsep = (buf[0] != '\n' || buf[1] != '\n');
1075 }else{
1076 rv = n_err_no;
1078 if(st_or_null == NULL){
1079 st_or_null = &stb;
1080 if(fstat(fileno(fout), st_or_null))
1081 goto jerrno;
1084 if(st_or_null->st_size >= 2)
1085 goto jleave;
1086 if(st_or_null->st_size == 0){
1087 rv = n_ERR_NONE;
1088 goto jleave;
1091 if(fseek(fout, -1L, SEEK_END))
1092 goto jerrno;
1093 if(fread(buf, sizeof *buf, 1, fout) != 1)
1094 goto jerrno;
1095 needsep = (buf[0] != '\n');
1098 rv = n_ERR_NONE;
1099 if(fflush(fout) || (needsep && putc('\n', fout) == EOF))
1100 jerrno:
1101 rv = n_err_no;
1102 jleave:
1103 NYD2_LEAVE;
1104 return rv;
1107 /* s-it-mode */