Add n_hy[] ("-"), use it
[s-mailx.git] / folder.c
blob7accf9a11c8c5abd6dff370d774cd8ea485c3a5a
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 */
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 n_INLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
49 #endif
51 /**/
52 static void a_folder_info(void);
54 /* Set up the input pointers while copying the mail file into /tmp */
55 static void a_folder_mbox_setptr(FILE *ibuf, off_t offset);
57 static bool_t
58 _update_mailname(char const *name) /* TODO 2MUCH work, cache, prop of Object! */
60 char const *foldp;
61 char *mailp, *dispp;
62 size_t i, j, foldlen;
63 bool_t rv;
64 NYD_ENTER;
66 /* Don't realpath(3) if it's only an update request */
67 if(name != NULL){
68 #ifdef HAVE_REALPATH
69 char const *adjname;
70 enum protocol p;
72 p = which_protocol(name, TRU1, TRU1, &adjname);
74 if(p == PROTO_FILE || p == PROTO_MAILDIR){
75 name = adjname;
76 if (realpath(name, mailname) == NULL && n_err_no != n_ERR_NOENT) {
77 n_err(_("Can't canonicalize %s\n"), n_shexp_quote_cp(name, FAL0));
78 goto jdocopy;
80 }else
81 jdocopy:
82 #endif
83 n_strscpy(mailname, name, sizeof(mailname));
86 mailp = mailname;
87 dispp = displayname;
89 /* Don't display an absolute path but "+FOLDER" if under *folder* */
90 if(*(foldp = n_folder_query()) != '\0'){
91 foldlen = strlen(foldp);
92 if(strncmp(foldp, mailp, foldlen))
93 foldlen = 0;
94 }else
95 foldlen = 0;
97 /* We want to see the name of the folder .. on the screen */
98 i = strlen(mailp);
99 if(i < sizeof(displayname) - 3 -1){
100 if(foldlen > 0){
101 *dispp++ = '+';
102 *dispp++ = '[';
103 memcpy(dispp, mailp, foldlen);
104 dispp += foldlen;
105 mailp += foldlen;
106 *dispp++ = ']';
107 memcpy(dispp, mailp, i -= foldlen);
108 dispp[i] = '\0';
109 }else
110 memcpy(dispp, mailp, i +1);
111 rv = TRU1;
112 }else{
113 /* Avoid disrupting multibyte sequences (if possible) */
114 #ifndef HAVE_C90AMEND1
115 j = sizeof(displayname) / 3 - 3;
116 i -= sizeof(displayname) - (1/* + */ + 3) - j;
117 #else
118 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
119 i = j + __narrow_suffix(mailp + j, i - j,
120 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
121 #endif
122 snprintf(dispp, sizeof(displayname), "%s%.*s...%s",
123 (foldlen > 0 ? "[+]" : ""), (int)j, mailp, mailp + i);
124 rv = FAL0;
127 n_PS_ROOT_BLOCK((ok_vset(mailbox_resolved, mailname),
128 ok_vset(mailbox_display, displayname)));
129 NYD_LEAVE;
130 return rv;
133 #ifdef HAVE_C90AMEND1
134 n_INLINE size_t
135 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
137 int err;
138 size_t i, ok;
139 NYD_ENTER;
141 for (err = ok = i = 0; cpl > maxl || err;) {
142 int ml = mblen(cp, cpl);
143 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
144 (void)mblen(NULL, 0);
145 err = 1;
146 ml = 1;
147 } else {
148 if (!err)
149 ok = i;
150 err = 0;
151 if (ml == 0)
152 break;
154 cp += ml;
155 i += ml;
156 cpl -= ml;
158 NYD_LEAVE;
159 return ok;
161 #endif /* HAVE_C90AMEND1 */
163 static void
164 a_folder_info(void){
165 struct message *mp;
166 int u, n, d, s, hidden, moved;
167 NYD2_ENTER;
169 if(mb.mb_type == MB_VOID){
170 fprintf(n_stdout, _("(Currently no active mailbox)"));
171 goto jleave;
174 s = d = hidden = moved = 0;
175 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
176 if (mp->m_flag & MNEW)
177 ++n;
178 if ((mp->m_flag & MREAD) == 0)
179 ++u;
180 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
181 ++moved;
182 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
183 ++d;
184 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
185 ++s;
186 if (mp->m_flag & MHIDDEN)
187 ++hidden;
190 /* If displayname gets truncated the user effectively has no option to see
191 * the full pathname of the mailbox, so print it at least for '? fi' */
192 fprintf(n_stdout, "%s: ", n_shexp_quote_cp(
193 (_update_mailname(NULL) ? displayname : mailname), FAL0));
194 if (msgCount == 1)
195 fprintf(n_stdout, _("1 message"));
196 else
197 fprintf(n_stdout, _("%d messages"), msgCount);
198 if (n > 0)
199 fprintf(n_stdout, _(" %d new"), n);
200 if (u-n > 0)
201 fprintf(n_stdout, _(" %d unread"), u);
202 if (d > 0)
203 fprintf(n_stdout, _(" %d deleted"), d);
204 if (s > 0)
205 fprintf(n_stdout, _(" %d saved"), s);
206 if (moved > 0)
207 fprintf(n_stdout, _(" %d moved"), moved);
208 if (hidden > 0)
209 fprintf(n_stdout, _(" %d hidden"), hidden);
210 if (mb.mb_perm == 0)
211 fprintf(n_stdout, _(" [Read-only]"));
212 #ifdef HAVE_IMAP
213 if (mb.mb_type == MB_CACHE)
214 fprintf(n_stdout, _(" [Disconnected]"));
215 #endif
217 jleave:
218 putc('\n', n_stdout);
219 NYD2_LEAVE;
222 static void
223 a_folder_mbox_setptr(FILE *ibuf, off_t offset) /* TODO Mailbox->setptr() */
225 struct message self;
226 char const *cp2;
227 char *linebuf, *cp;
228 int selfcnt, c;
229 bool_t need_rfc4155, maybe, inhead, from_;
230 size_t filesize, linesize, cnt;
231 NYD_ENTER;
233 memset(&self, 0, sizeof self);
234 self.m_flag = MUSED | MNEW | MNEWEST;
235 filesize = mailsize - offset;
236 offset = ftell(mb.mb_otf);
237 need_rfc4155 = ok_blook(mbox_rfc4155);
238 maybe = TRU1;
239 inhead = FAL0;
240 selfcnt = 0;
241 linebuf = NULL, linesize = 0; /* TODO string pool */
243 for (;;) {
244 if (fgetline(&linebuf, &linesize, &filesize, &cnt, ibuf, 0) == NULL) {
245 self.m_xsize = self.m_size;
246 self.m_xlines = self.m_lines;
247 self.m_content_info = CI_HAVE_HEADER | CI_HAVE_BODY;
248 if (selfcnt > 0)
249 message_append(&self);
250 message_append_null();
251 if (linebuf != NULL)
252 n_free(linebuf);
253 break;
256 #ifdef notdef
257 if (linebuf[0] == '\0')
258 linebuf[0] = '.';
259 #endif
260 /* XXX Convert CRLF to LF; this should be rethought in that
261 * XXX CRLF input should possibly end as CRLF output? */
262 if (cnt >= 2 && linebuf[cnt - 1] == '\n' && linebuf[cnt - 2] == '\r')
263 linebuf[--cnt - 1] = '\n';
264 fwrite(linebuf, sizeof *linebuf, cnt, mb.mb_otf);
265 if (ferror(mb.mb_otf)) {
266 n_perr(_("/tmp"), 0);
267 exit(n_EXIT_ERR);
269 if (linebuf[cnt - 1] == '\n')
270 linebuf[cnt - 1] = '\0';
271 /* TODO In v15 this should use a/the flat MIME parser in order to ignore
272 * TODO "From " when MIME boundaries are active -- whereas this opens
273 * TODO another can of worms, it very likely is better than messing up
274 * TODO MIME because of a "From " line! */
275 if (maybe && linebuf[0] == 'F' &&
276 (from_ = is_head(linebuf, cnt, TRU1)) &&
277 (from_ == TRU1 || !need_rfc4155)) {
278 /* TODO char date[n_FROM_DATEBUF];
279 * TODO extract_date_from_from_(linebuf, cnt, date);
280 * TODO self.m_time = 10000; */
281 if (from_ == TRUM1) {
282 if (n_poption & n_PO_D_V)
283 n_err(_("Invalid MBOX \"From_ line\": %.*s\n"),
284 (int)cnt, linebuf);
285 else if (!(mb.mb_active & MB_FROM__WARNED))
286 n_err(_("MBOX mailbox contains non-conforming From_ line(s)!\n"
287 " Message boundaries may have been falsely detected!\n"
288 " Setting variable *mbox-rfc4155* and reopen should improve "
289 "the result.\n"
290 " If so, make changes permanent: \"copy * SOME-FILE\". "
291 "Then unset *mbox-rfc4155*\n"));
292 mb.mb_active |= MB_FROM__WARNED;
294 self.m_xsize = self.m_size;
295 self.m_xlines = self.m_lines;
296 self.m_content_info = CI_HAVE_HEADER | CI_HAVE_BODY;
297 if (selfcnt++ > 0)
298 message_append(&self);
299 msgCount++;
300 self.m_flag = MUSED | MNEW | MNEWEST;
301 self.m_size = 0;
302 self.m_lines = 0;
303 self.m_block = mailx_blockof(offset);
304 self.m_offset = mailx_offsetof(offset);
305 inhead = TRU1;
306 } else if (linebuf[0] == 0) {
307 inhead = FAL0;
308 } else if (inhead) {
309 for (cp = linebuf, cp2 = "status";; ++cp) {
310 if ((c = *cp2++) == 0) {
311 while (c = *cp++, whitechar(c))
313 if (cp[-1] != ':')
314 break;
315 while ((c = *cp++) != '\0')
316 if (c == 'R')
317 self.m_flag |= MREAD;
318 else if (c == 'O')
319 self.m_flag &= ~MNEW;
320 break;
322 if (*cp != c && *cp != upperconv(c))
323 break;
325 for (cp = linebuf, cp2 = "x-status";; ++cp) {
326 if ((c = *cp2++) == 0) {
327 while ((c = *cp++, whitechar(c)))
329 if (cp[-1] != ':')
330 break;
331 while ((c = *cp++) != '\0')
332 if (c == 'F')
333 self.m_flag |= MFLAGGED;
334 else if (c == 'A')
335 self.m_flag |= MANSWERED;
336 else if (c == 'T')
337 self.m_flag |= MDRAFTED;
338 break;
340 if (*cp != c && *cp != upperconv(c))
341 break;
344 offset += cnt;
345 self.m_size += cnt;
346 ++self.m_lines;
347 maybe = (linebuf[0] == 0);
349 NYD_LEAVE;
352 FL int
353 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
355 /* TODO This all needs to be converted to URL:: and Mailbox:: */
356 static int shudclob;
358 struct stat stb;
359 size_t offset;
360 char const *who, *orig_name;
361 int rv, omsgCount = 0;
362 FILE *ibuf = NULL, *lckfp = NULL;
363 bool_t isdevnull = FAL0;
364 NYD_ENTER;
366 n_pstate &= ~n_PS_SETFILE_OPENED;
368 /* C99 */{
369 enum fexp_mode fexpm;
371 if((who = shortcut_expand(name)) != NULL){
372 fexpm = FEXP_NSHORTCUT/* XXX | FEXP_NSHELL*/;
373 name = who;
374 }else
375 fexpm = FEXP_NSHELL;
377 if(name[0] == '%'){
378 char const *cp;
380 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
381 if(*(who = &name[1]) == ':')
382 ++who;
383 if((cp = strrchr(who, '/')) != NULL)
384 who = &cp[1];
385 if(*who == '\0')
386 goto jlogname;
387 }else
388 jlogname:
389 who = ok_vlook(LOGNAME);
391 if ((name = fexpand(name, fexpm)) == NULL)
392 goto jem1;
395 /* For at least substdate() users TODO -> eventloop tick */
396 time_current_update(&time_current, FAL0);
398 switch (which_protocol(orig_name = name, TRU1, TRU1, &name)) {
399 case PROTO_FILE:
400 isdevnull = ((n_poption & n_PO_BATCH_FLAG) &&
401 !strcmp(name, n_path_devnull));
402 #ifdef HAVE_REALPATH
403 do { /* TODO we need objects, goes away then */
404 # ifdef HAVE_REALPATH_NULL
405 char *cp;
407 if ((cp = realpath(name, NULL)) != NULL) {
408 name = savestr(cp);
409 (free)(cp);
411 # else
412 char cbuf[PATH_MAX];
414 if (realpath(name, cbuf) != NULL)
415 name = savestr(cbuf);
416 # endif
417 } while (0);
418 #endif
419 rv = 1;
420 break;
421 #ifdef HAVE_MAILDIR
422 case PROTO_MAILDIR:
423 shudclob = 1;
424 rv = maildir_setfile(name, fm);
425 goto jleave;
426 #endif
427 #ifdef HAVE_POP3
428 case PROTO_POP3:
429 shudclob = 1;
430 rv = pop3_setfile(orig_name, fm);
431 goto jleave;
432 #endif
433 #ifdef HAVE_IMAP
434 case PROTO_IMAP:
435 shudclob = 1;
436 if((fm & FEDIT_NEWMAIL) && mb.mb_type == MB_CACHE)
437 rv = 1;
438 else
439 rv = imap_setfile(orig_name, fm);
440 goto jleave;
441 #endif
442 default:
443 n_err(_("Cannot handle protocol: %s\n"), orig_name);
444 goto jem1;
447 if ((ibuf = n_fopen_any(savecat("file://", name), "r", NULL)) == NULL) {
448 int e = n_err_no;
450 if ((fm & FEDIT_SYSBOX) && e == n_ERR_NOENT) {
451 if (strcmp(who, ok_vlook(LOGNAME)) && getpwnam(who) == NULL) {
452 n_err(_("%s is not a user of this system\n"),
453 n_shexp_quote_cp(who, FAL0));
454 goto jem2;
456 if (!(n_poption & n_PO_QUICKRUN_MASK) && ok_blook(bsdcompat))
457 n_err(_("No mail for %s\n"), who);
459 if (fm & FEDIT_NEWMAIL)
460 goto jleave;
462 mb.mb_type = MB_VOID;
463 if (ok_blook(emptystart)) {
464 if (!(n_poption & n_PO_QUICKRUN_MASK) && !ok_blook(bsdcompat))
465 n_perr(name, e);
466 /* We must avoid returning -1 and causing program exit */
467 rv = 1;
468 goto jleave;
470 n_perr(name, e);
471 goto jem1;
474 if (fstat(fileno(ibuf), &stb) == -1) {
475 if (fm & FEDIT_NEWMAIL)
476 goto jleave;
477 n_perr(_("fstat"), 0);
478 goto jem1;
481 if (S_ISREG(stb.st_mode) || isdevnull) {
482 /* EMPTY */
483 } else {
484 if (fm & FEDIT_NEWMAIL)
485 goto jleave;
486 n_err_no = S_ISDIR(stb.st_mode) ? n_ERR_ISDIR : n_ERR_INVAL;
487 n_perr(name, 0);
488 goto jem1;
491 if (shudclob && !(fm & FEDIT_NEWMAIL) && !quit(FAL0))
492 goto jem2;
494 hold_sigs();
496 #ifdef HAVE_SOCKETS
497 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
498 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
499 #endif
501 /* TODO There is no intermediate VOID box we've switched to: name may
502 * TODO point to the same box that we just have written, so any updates
503 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
504 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
505 /* TODO We may uselessly open twice but quit() doesn't say whether we were
506 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
507 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
508 name = mailname;
509 Fclose(ibuf);
511 if ((ibuf = n_fopen_any(name, "r", NULL)) == NULL ||
512 fstat(fileno(ibuf), &stb) == -1 ||
513 (!S_ISREG(stb.st_mode) && !isdevnull)) {
514 n_perr(name, 0);
515 rele_sigs();
516 goto jem2;
520 /* Copy the messages into /tmp and set pointers */
521 if (!(fm & FEDIT_NEWMAIL)) {
522 if (isdevnull) {
523 mb.mb_type = MB_VOID;
524 mb.mb_perm = 0;
525 } else {
526 mb.mb_type = MB_FILE;
527 mb.mb_perm = (((n_poption & n_PO_R_FLAG) || (fm & FEDIT_RDONLY) ||
528 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
529 if (shudclob) {
530 if (mb.mb_itf) {
531 fclose(mb.mb_itf);
532 mb.mb_itf = NULL;
534 if (mb.mb_otf) {
535 fclose(mb.mb_otf);
536 mb.mb_otf = NULL;
540 shudclob = 1;
541 if (fm & FEDIT_SYSBOX)
542 n_pstate &= ~n_PS_EDIT;
543 else
544 n_pstate |= n_PS_EDIT;
545 initbox(name);
546 offset = 0;
547 } else {
548 fseek(mb.mb_otf, 0L, SEEK_END);
549 /* TODO Doing this without holding a lock is.. And not err checking.. */
550 fseek(ibuf, mailsize, SEEK_SET);
551 offset = mailsize;
552 omsgCount = msgCount;
555 if (isdevnull)
556 lckfp = (FILE*)-1;
557 else if (!(n_pstate & n_PS_EDIT))
558 lckfp = n_dotlock(name, fileno(ibuf), FLT_READ, offset,0,
559 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX));
560 else if (n_file_lock(fileno(ibuf), FLT_READ, offset,0,
561 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX)))
562 lckfp = (FILE*)-1;
564 if (lckfp == NULL) {
565 if (!(fm & FEDIT_NEWMAIL)) {
566 char const *emsg = (n_pstate & n_PS_EDIT)
567 ? N_("Unable to lock mailbox, aborting operation")
568 : N_("Unable to (dot) lock mailbox, aborting operation");
569 n_perr(V_(emsg), 0);
571 rele_sigs();
572 if (!(fm & FEDIT_NEWMAIL))
573 goto jem1;
574 goto jleave;
577 mailsize = fsize(ibuf);
579 /* TODO This is too simple minded? We should regenerate an index file
580 * TODO to be able to truly tell whether *anything* has changed! */
581 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
582 rele_sigs();
583 goto jleave;
585 a_folder_mbox_setptr(ibuf, offset);
586 setmsize(msgCount);
587 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
588 mb.mb_threaded = 0;
589 c_sort((void*)-1);
592 Fclose(ibuf);
593 ibuf = NULL;
594 if (lckfp != NULL && lckfp != (FILE*)-1) {
595 Pclose(lckfp, FAL0);
596 /*lckfp = NULL;*/
599 if (!(fm & FEDIT_NEWMAIL)) {
600 n_pstate &= ~n_PS_SAW_COMMAND;
601 n_pstate |= n_PS_SETFILE_OPENED;
604 rele_sigs();
606 rv = (msgCount == 0);
607 if(rv)
608 n_err_no = n_ERR_NODATA;
610 if(n_poption & n_PO_EXISTONLY)
611 goto jleave;
613 if(rv){
614 if(!(n_pstate & n_PS_EDIT) || (fm & FEDIT_NEWMAIL)){
615 if(!(fm & FEDIT_NEWMAIL)){
616 if (!ok_blook(emptystart))
617 n_err(_("No mail for %s\n"), who);
619 goto jleave;
623 if(fm & FEDIT_NEWMAIL)
624 newmailinfo(omsgCount);
625 jleave:
626 if (ibuf != NULL) {
627 Fclose(ibuf);
628 if (lckfp != NULL && lckfp != (FILE*)-1)
629 Pclose(lckfp, FAL0);
631 NYD_LEAVE;
632 return rv;
633 jem2:
634 mb.mb_type = MB_VOID;
635 jem1:
636 n_err_no = n_ERR_NOTOBACCO;
637 rv = -1;
638 goto jleave;
641 FL int
642 newmailinfo(int omsgCount)
644 int mdot, i;
645 NYD_ENTER;
647 for (i = 0; i < omsgCount; ++i)
648 message[i].m_flag &= ~MNEWEST;
650 if (msgCount > omsgCount) {
651 for (i = omsgCount; i < msgCount; ++i)
652 message[i].m_flag |= MNEWEST;
653 fprintf(n_stdout, _("New mail has arrived.\n"));
654 if ((i = msgCount - omsgCount) == 1)
655 fprintf(n_stdout, _("Loaded 1 new message.\n"));
656 else
657 fprintf(n_stdout, _("Loaded %d new messages.\n"), i);
658 } else
659 fprintf(n_stdout, _("Loaded %d messages.\n"), msgCount);
661 temporary_folder_hook_check(TRU1);
663 mdot = getmdot(1);
665 if(ok_blook(header))
666 print_headers(omsgCount + 1, msgCount, FAL0, FAL0);
667 NYD_LEAVE;
668 return mdot;
671 FL void
672 setmsize(int sz)
674 NYD_ENTER;
675 if (n_msgvec != NULL)
676 n_free(n_msgvec);
677 n_msgvec = n_calloc(sz + 1, sizeof *n_msgvec);
678 NYD_LEAVE;
681 FL void
682 print_header_summary(char const *Larg)
684 size_t bot, top, i, j;
685 NYD_ENTER;
687 if (Larg != NULL) {
688 /* Avoid any messages XXX add a make_mua_silent() and use it? */
689 if ((n_poption & (n_PO_VERB | n_PO_EXISTONLY)) == n_PO_EXISTONLY) {
690 n_stdout = freopen(n_path_devnull, "w", stdout);
691 n_stderr = freopen(n_path_devnull, "w", stderr);
693 assert(n_msgvec != NULL);
694 i = (getmsglist(/*TODO make const */n_UNCONST(Larg), n_msgvec, 0) <= 0);
695 if (n_poption & n_PO_EXISTONLY) {
696 n_exit_status = (int)i;
697 goto jleave;
699 if (i)
700 goto jleave;
701 for (bot = msgCount, top = 0, i = 0; (j = n_msgvec[i]) != 0; ++i) {
702 if (bot > j)
703 bot = j;
704 if (top < j)
705 top = j;
707 } else
708 bot = 1, top = msgCount;
710 /* C99 */{
711 bool_t isLarg;
713 isLarg = (Larg != NULL);
714 print_headers(bot, top, isLarg, !isLarg); /* TODO should be iterator! */
716 jleave:
717 NYD_LEAVE;
720 FL void
721 n_folder_announce(enum n_announce_flags af){
722 int vec[2], mdot;
723 NYD_ENTER;
725 mdot = (mb.mb_type == MB_VOID) ? 1 : getmdot(0);
726 dot = &message[mdot - 1];
728 if(af != n_ANNOUNCE_NONE && ok_blook(header) &&
729 ((af & n_ANNOUNCE_MAIN_CALL) ||
730 ((af & n_ANNOUNCE_CHANGE) && !ok_blook(posix))))
731 af |= n_ANNOUNCE_STATUS | n__ANNOUNCE_HEADER;
733 if(af & n_ANNOUNCE_STATUS){
734 a_folder_info();
735 af |= n__ANNOUNCE_ANY;
738 if(af & n__ANNOUNCE_HEADER){
739 if(!(af & n_ANNOUNCE_MAIN_CALL) && ok_blook(bsdannounce))
740 n_OBSOLETE(_("*bsdannounce* is now default behaviour"));
741 vec[0] = mdot;
742 vec[1] = 0;
743 print_header_group(vec); /* XXX errors? */
744 af |= n__ANNOUNCE_ANY;
747 if(af & n__ANNOUNCE_ANY)
748 fflush(n_stdout);
749 NYD_LEAVE;
752 FL int
753 getmdot(int nmail)
755 struct message *mp;
756 char *cp;
757 int mdot;
758 enum mflag avoid = MHIDDEN | MDELETED;
759 NYD_ENTER;
761 if (!nmail) {
762 if (ok_blook(autothread)) {
763 n_OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
764 c_thread(NULL);
765 } else if ((cp = ok_vlook(autosort)) != NULL) {
766 if (mb.mb_sorted != NULL)
767 n_free(mb.mb_sorted);
768 mb.mb_sorted = sstrdup(cp);
769 c_sort(NULL);
772 if (mb.mb_type == MB_VOID) {
773 mdot = 1;
774 goto jleave;
777 if (nmail)
778 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
779 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
780 break;
782 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
783 if (mb.mb_threaded) {
784 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
785 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
786 break;
787 } else {
788 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
789 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
790 break;
794 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
795 if (mb.mb_threaded) {
796 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
797 if (mp->m_flag & MFLAGGED)
798 break;
799 } else {
800 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
801 if (mp->m_flag & MFLAGGED)
802 break;
806 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
807 if (mb.mb_threaded) {
808 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
809 if (!(mp->m_flag & (MREAD | avoid)))
810 break;
811 } else {
812 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
813 if (!(mp->m_flag & (MREAD | avoid)))
814 break;
818 if (nmail &&
819 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
820 mdot = (int)PTR2SIZE(mp - message + 1);
821 else if (ok_blook(showlast)) {
822 if (mb.mb_threaded) {
823 for (mp = this_in_thread(threadroot, -1); mp;
824 mp = prev_in_thread(mp))
825 if (!(mp->m_flag & avoid))
826 break;
827 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
828 } else {
829 for (mp = message + msgCount - 1; mp >= message; --mp)
830 if (!(mp->m_flag & avoid))
831 break;
832 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
834 } else if (!nmail &&
835 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
836 mdot = (int)PTR2SIZE(mp - message + 1);
837 else if (mb.mb_threaded) {
838 for (mp = threadroot; mp; mp = next_in_thread(mp))
839 if (!(mp->m_flag & avoid))
840 break;
841 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
842 } else {
843 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
844 if (!(mp->m_flag & avoid))
845 break;
846 mdot = PTRCMP(mp, <, message + msgCount)
847 ? (int)PTR2SIZE(mp - message + 1) : 1;
849 jleave:
850 NYD_LEAVE;
851 return mdot;
854 FL void
855 initbox(char const *name)
857 bool_t err;
858 char *tempMesg;
859 NYD_ENTER;
861 if (mb.mb_type != MB_VOID)
862 n_strscpy(prevfile, mailname, PATH_MAX);
864 /* TODO name always NE mailname (but goes away for objects anyway)
865 * TODO Well, not true no more except that in parens */
866 _update_mailname((name != mailname) ? name : NULL);
868 err = FAL0;
869 if((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
870 NULL){
871 n_perr(_("initbox: temporary mail message file, writer"), 0);
872 err = TRU1;
873 }else if((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
874 n_perr(_("initbox: temporary mail message file, reader"), 0);
875 err = TRU1;
877 Ftmp_release(&tempMesg);
878 if(err)
879 exit(n_EXIT_ERR);
881 message_reset();
882 mb.mb_active = MB_NONE;
883 mb.mb_threaded = 0;
884 #ifdef HAVE_IMAP
885 mb.mb_flags = MB_NOFLAGS;
886 #endif
887 if (mb.mb_sorted != NULL) {
888 n_free(mb.mb_sorted);
889 mb.mb_sorted = NULL;
891 dot = prevdot = threadroot = NULL;
892 n_pstate &= ~n_PS_DID_PRINT_DOT;
893 NYD_LEAVE;
896 FL char const *
897 n_folder_query(void){
898 struct n_string s, *sp = &s;
899 enum protocol proto;
900 char *cp;
901 char const *rv, *adjcp;
902 bool_t err;
903 NYD_ENTER;
905 sp = n_string_creat_auto(sp);
907 /* *folder* is linked with *folder_resolved*: we only use the latter */
908 for(err = FAL0;;){
909 if((rv = ok_vlook(folder_resolved)) != NULL)
910 break;
912 /* POSIX says:
913 * If directory does not start with a <slash> ('/'), the contents
914 * of HOME shall be prefixed to it.
915 * And:
916 * If folder is unset or set to null, [.] filenames beginning with
917 * '+' shall refer to files in the current directory.
918 * We may have the result already */
919 rv = n_empty;
920 err = FAL0;
922 if((cp = ok_vlook(folder)) == NULL)
923 goto jset;
925 /* Expand the *folder*; skip %: prefix for simplicity of use */
926 if(cp[0] == '%' && cp[1] == ':')
927 cp += 2;
928 if((err = (cp = fexpand(cp, FEXP_NSPECIAL | FEXP_NFOLDER | FEXP_NSHELL)
929 ) == NULL) || *cp == '\0')
930 goto jset;
931 else{
932 size_t i;
934 for(i = strlen(cp);;){
935 if(--i == 0)
936 goto jset;
937 if(cp[i] != '/'){
938 cp[++i] = '\0';
939 break;
944 switch((proto = which_protocol(cp, FAL0, FAL0, &adjcp))){
945 case PROTO_POP3:
946 n_err(_("*folder* can't be set to a flat, read-only POP3 account\n"));
947 err = TRU1;
948 goto jset;
949 case PROTO_IMAP:
950 #ifdef HAVE_IMAP
951 rv = cp;
952 if(!strcmp(rv, protbase(rv)))
953 rv = savecatsep(rv, '/', n_empty);
954 #else
955 n_err(_("*folder*: IMAP support not compiled in\n"));
956 err = TRU1;
957 #endif
958 goto jset;
959 default:
960 /* Further expansion desired */
961 break;
964 /* Prefix HOME as necessary */
965 if(*adjcp != '/'){ /* XXX path_is_absolute() */
966 size_t l1, l2;
967 char const *home;
969 home = ok_vlook(HOME);
970 l1 = strlen(home);
971 l2 = strlen(cp);
973 sp = n_string_reserve(sp, l1 + 1 + l2 +1);
974 if(cp != adjcp){
975 size_t i;
977 sp = n_string_push_buf(sp, cp, i = PTR2SIZE(adjcp - cp));
978 cp += i;
979 l2 -= i;
981 sp = n_string_push_buf(sp, home, l1);
982 sp = n_string_push_c(sp, '/');
983 sp = n_string_push_buf(sp, cp, l2);
984 cp = n_string_cp(sp);
985 sp = n_string_drop_ownership(sp);
988 /* TODO Since our visual mailname is resolved via realpath(3) if available
989 * TODO to avoid that we loose track of our currently open folder in case
990 * TODO we chdir away, but still checks the leading path portion against
991 * TODO n_folder_query() to be able to abbreviate to the +FOLDER syntax if
992 * TODO possible, we need to realpath(3) the folder, too */
993 #ifndef HAVE_REALPATH
994 rv = cp;
995 #else
996 assert(sp->s_len == 0 && sp->s_dat == NULL);
997 # ifndef HAVE_REALPATH_NULL
998 sp = n_string_reserve(sp, PATH_MAX +1);
999 # endif
1001 if((sp->s_dat = realpath(cp, sp->s_dat)) != NULL){
1002 # ifdef HAVE_REALPATH_NULL
1003 n_string_cp(sp = n_string_assign_cp(sp, cp = sp->s_dat));
1004 (free)(cp);
1005 # endif
1006 rv = sp->s_dat;
1007 }else if(n_err_no == n_ERR_NOENT)
1008 rv = cp;
1009 else{
1010 n_err(_("Can't canonicalize *folder*: %s\n"),
1011 n_shexp_quote_cp(cp, FAL0));
1012 err = TRU1;
1013 rv = n_empty;
1015 sp = n_string_drop_ownership(sp);
1016 #endif /* HAVE_REALPATH */
1018 /* Always append a solidus to our result path upon success */
1019 if(!err){
1020 size_t i;
1022 if(rv[(i = strlen(rv)) - 1] != '/'){
1023 sp = n_string_reserve(sp, i + 1 +1);
1024 sp = n_string_push_buf(sp, rv, i);
1025 sp = n_string_push_c(sp, '/');
1026 rv = n_string_cp(sp);
1027 sp = n_string_drop_ownership(sp);
1031 jset:
1032 n_PS_ROOT_BLOCK(ok_vset(folder_resolved, rv));
1035 if(err){
1036 n_err(_("*folder* is not resolvable, using CWD\n"));
1037 assert(rv != NULL && *rv == '\0');
1039 NYD_LEAVE;
1040 return rv;
1043 FL int
1044 n_folder_mbox_prepare_append(FILE *fout, struct stat *st_or_null){
1045 /* TODO n_folder_mbox_prepare_append -> Mailbox->append() */
1046 struct stat stb;
1047 char buf[2];
1048 int rv;
1049 bool_t needsep;
1050 NYD2_ENTER;
1052 if(!fseek(fout, -2L, SEEK_END)){
1053 if(fread(buf, sizeof *buf, 2, fout) != 2)
1054 goto jerrno;
1055 needsep = (buf[0] != '\n' || buf[1] != '\n');
1056 }else{
1057 rv = n_err_no;
1059 if(st_or_null == NULL){
1060 st_or_null = &stb;
1061 if(fstat(fileno(fout), st_or_null))
1062 goto jerrno;
1065 if(st_or_null->st_size >= 2)
1066 goto jleave;
1067 if(st_or_null->st_size == 0){
1068 rv = n_ERR_NONE;
1069 goto jleave;
1072 if(fseek(fout, -1L, SEEK_END))
1073 goto jerrno;
1074 if(fread(buf, sizeof *buf, 1, fout) != 1)
1075 goto jerrno;
1076 needsep = (buf[0] != '\n');
1079 rv = n_ERR_NONE;
1080 if(fflush(fout) || (needsep && putc('\n', fout) == EOF))
1081 jerrno:
1082 rv = n_err_no;
1083 jleave:
1084 NYD2_LEAVE;
1085 return rv;
1088 /* s-it-mode */