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
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
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
39 #ifndef HAVE_AMALGAMATION
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
);
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
);
59 _update_mailname(char const *name
) /* TODO 2MUCH work, cache, prop of Object! */
67 /* Don't realpath(3) if it's only an update request */
73 p
= which_protocol(name
, TRU1
, TRU1
, &adjname
);
75 if(p
== PROTO_FILE
|| p
== PROTO_MAILDIR
){
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
));
84 n_strscpy(mailname
, name
, sizeof(mailname
));
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
))
98 /* We want to see the name of the folder .. on the screen */
100 if(i
< sizeof(displayname
) - 3 -1){
104 memcpy(dispp
, mailp
, foldlen
);
108 memcpy(dispp
, mailp
, i
-= foldlen
);
111 memcpy(dispp
, mailp
, i
+1);
114 /* Avoid disrupting multibyte sequences (if possible) */
115 #ifndef HAVE_C90AMEND1
116 j
= sizeof(displayname
) / 3 - 3;
117 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
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
);
123 snprintf(dispp
, sizeof(displayname
), "%s%.*s...%s",
124 (foldlen
> 0 ? "[+]" : ""), (int)j
, mailp
, mailp
+ i
);
128 n_PS_ROOT_BLOCK((ok_vset(mailbox_resolved
, mailname
),
129 ok_vset(mailbox_display
, displayname
)));
134 #ifdef HAVE_C90AMEND1
136 __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
)
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);
162 #endif /* HAVE_C90AMEND1 */
167 int u
, n
, d
, s
, hidden
, moved
;
170 if(mb
.mb_type
== MB_VOID
){
171 fprintf(n_stdout
, _("(Currently no active mailbox)"));
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
)
179 if ((mp
->m_flag
& MREAD
) == 0)
181 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
183 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
185 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
187 if (mp
->m_flag
& MHIDDEN
)
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
));
196 fprintf(n_stdout
, _("1 message"));
198 fprintf(n_stdout
, _("%d messages"), msgCount
);
200 fprintf(n_stdout
, _(" %d new"), n
);
202 fprintf(n_stdout
, _(" %d unread"), u
);
204 fprintf(n_stdout
, _(" %d deleted"), d
);
206 fprintf(n_stdout
, _(" %d saved"), s
);
208 fprintf(n_stdout
, _(" %d moved"), moved
);
210 fprintf(n_stdout
, _(" %d hidden"), hidden
);
212 fprintf(n_stdout
, _(" [Read-only]"));
214 if (mb
.mb_type
== MB_CACHE
)
215 fprintf(n_stdout
, _(" [Disconnected]"));
219 putc('\n', n_stdout
);
224 a_folder_mbox_setptr(FILE *ibuf
, off_t offset
) /* TODO Mailbox->setptr() */
230 bool_t need_rfc4155
, maybe
, inhead
, from_
;
231 size_t filesize
, linesize
, cnt
;
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
);
242 linebuf
= NULL
, linesize
= 0; /* TODO string pool */
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
;
250 message_append(&self
);
251 message_append_null();
258 if (linebuf
[0] == '\0')
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);
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"),
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 "
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
;
299 message_append(&self
);
301 self
.m_flag
= MUSED
| MNEW
| MNEWEST
;
304 self
.m_block
= mailx_blockof(offset
);
305 self
.m_offset
= mailx_offsetof(offset
);
307 } else if (linebuf
[0] == 0) {
310 for (cp
= linebuf
, cp2
= "status";; ++cp
) {
311 if ((c
= *cp2
++) == 0) {
312 while (c
= *cp
++, whitechar(c
))
316 while ((c
= *cp
++) != '\0')
318 self
.m_flag
|= MREAD
;
320 self
.m_flag
&= ~MNEW
;
323 if (*cp
!= c
&& *cp
!= upperconv(c
))
326 for (cp
= linebuf
, cp2
= "x-status";; ++cp
) {
327 if ((c
= *cp2
++) == 0) {
328 while ((c
= *cp
++, whitechar(c
)))
332 while ((c
= *cp
++) != '\0')
334 self
.m_flag
|= MFLAGGED
;
336 self
.m_flag
|= MANSWERED
;
338 self
.m_flag
|= MDRAFTED
;
341 if (*cp
!= c
&& *cp
!= upperconv(c
))
348 maybe
= (linebuf
[0] == 0);
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:: */
361 char const *who
, *orig_name
;
362 int rv
, omsgCount
= 0;
363 FILE *ibuf
= NULL
, *lckfp
= NULL
;
364 bool_t isdevnull
= FAL0
;
367 n_pstate
&= ~n_PS_SETFILE_OPENED
;
370 enum fexp_mode fexpm
;
372 if((who
= shortcut_expand(name
)) != NULL
){
373 fexpm
= FEXP_NSHORTCUT
/* XXX | FEXP_NSHELL*/;
381 fm
|= FEDIT_SYSBOX
; /* TODO fexpand() needs to tell is-valid-user! */
382 if(*(who
= &name
[1]) == ':')
384 if((cp
= strrchr(who
, '/')) != NULL
)
390 if(fm
& FEDIT_ACCOUNT
){
391 if((who
= ok_vlook(account
)) == NULL
)
393 who
= savecatsep(_("account"), ' ', who
);
395 who
= ok_vlook(LOGNAME
);
398 if ((name
= fexpand(name
, fexpm
)) == NULL
)
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
)) {
407 isdevnull
= ((n_poption
& n_PO_BATCH_FLAG
) &&
408 !strcmp(name
, n_path_devnull
));
410 do { /* TODO we need objects, goes away then */
411 # ifdef HAVE_REALPATH_NULL
414 if ((cp
= realpath(name
, NULL
)) != NULL
) {
421 if (realpath(name
, cbuf
) != NULL
)
422 name
= savestr(cbuf
);
431 rv
= maildir_setfile(who
, name
, fm
);
437 rv
= pop3_setfile(who
, orig_name
, fm
);
443 if((fm
& FEDIT_NEWMAIL
) && mb
.mb_type
== MB_CACHE
)
446 rv
= imap_setfile(who
, orig_name
, fm
);
450 n_err(_("Cannot handle protocol: %s\n"), orig_name
);
454 if ((ibuf
= n_fopen_any(savecat("file://", name
), "r", NULL
)) == NULL
) {
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
));
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
)
471 if(mb
.mb_digmsg
!= NULL
)
472 n_dig_msg_on_mailbox_close(&mb
);
473 mb
.mb_type
= MB_VOID
;
475 if (ok_blook(emptystart
)) {
476 if (!(n_poption
& n_PO_QUICKRUN_MASK
) && !ok_blook(bsdcompat
))
478 /* We must avoid returning -1 and causing program exit */
486 if (fstat(fileno(ibuf
), &stb
) == -1) {
487 if (fm
& FEDIT_NEWMAIL
)
489 n_perr(_("fstat"), 0);
493 if (S_ISREG(stb
.st_mode
) || isdevnull
) {
496 if (fm
& FEDIT_NEWMAIL
)
498 n_err_no
= S_ISDIR(stb
.st_mode
) ? n_ERR_ISDIR
: n_ERR_INVAL
;
503 if (shudclob
&& !(fm
& FEDIT_NEWMAIL
) && !quit(FAL0
))
509 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
510 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
513 /* TODO There is no intermediate VOID box we've switched to: name may
514 * TODO point to the same box that we just have written, so any updates
515 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
516 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
517 /* TODO We may uselessly open twice but quit() doesn't say whether we were
518 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
519 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name
, mailname
)) {
523 if ((ibuf
= n_fopen_any(name
, "r", NULL
)) == NULL
||
524 fstat(fileno(ibuf
), &stb
) == -1 ||
525 (!S_ISREG(stb
.st_mode
) && !isdevnull
)) {
532 /* Copy the messages into /tmp and set pointers */
533 if (!(fm
& FEDIT_NEWMAIL
)) {
535 mb
.mb_type
= MB_VOID
;
538 mb
.mb_type
= MB_FILE
;
539 mb
.mb_perm
= (((n_poption
& n_PO_R_FLAG
) || (fm
& FEDIT_RDONLY
) ||
540 access(name
, W_OK
) < 0) ? 0 : MB_DELE
| MB_EDIT
);
553 if (fm
& FEDIT_SYSBOX
)
554 n_pstate
&= ~n_PS_EDIT
;
556 n_pstate
|= n_PS_EDIT
;
560 fseek(mb
.mb_otf
, 0L, SEEK_END
);
561 /* TODO Doing this without holding a lock is.. And not err checking.. */
562 fseek(ibuf
, mailsize
, SEEK_SET
);
564 omsgCount
= msgCount
;
569 else if (!(n_pstate
& n_PS_EDIT
))
570 lckfp
= n_dotlock(name
, fileno(ibuf
), FLT_READ
, offset
,0,
571 (fm
& FEDIT_NEWMAIL
? 0 : UIZ_MAX
));
572 else if (n_file_lock(fileno(ibuf
), FLT_READ
, offset
,0,
573 (fm
& FEDIT_NEWMAIL
? 0 : UIZ_MAX
)))
577 if (!(fm
& FEDIT_NEWMAIL
)) {
578 #ifdef HAVE_UISTRINGS
579 char const * const emsg
= (n_pstate
& n_PS_EDIT
)
580 ? N_("Unable to lock mailbox, aborting operation")
581 : N_("Unable to (dot) lock mailbox, aborting operation");
586 if (!(fm
& FEDIT_NEWMAIL
))
591 mailsize
= fsize(ibuf
);
593 /* TODO This is too simple minded? We should regenerate an index file
594 * TODO to be able to truly tell whether *anything* has changed! */
595 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
599 a_folder_mbox_setptr(ibuf
, offset
);
601 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
608 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1) {
613 if (!(fm
& FEDIT_NEWMAIL
)) {
614 n_pstate
&= ~n_PS_SAW_COMMAND
;
615 n_pstate
|= n_PS_SETFILE_OPENED
;
620 rv
= (msgCount
== 0);
622 n_err_no
= n_ERR_NODATA
;
624 if(n_poption
& n_PO_EXISTONLY
)
628 if(!(n_pstate
& n_PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)){
629 if(!(fm
& FEDIT_NEWMAIL
)){
630 if (!ok_blook(emptystart
))
631 n_err(_("No mail for %s at %s\n"),
632 who
, n_shexp_quote_cp(name
, FAL0
));
638 if(fm
& FEDIT_NEWMAIL
)
639 newmailinfo(omsgCount
);
643 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
649 if(mb
.mb_digmsg
!= NULL
)
650 n_dig_msg_on_mailbox_close(&mb
);
651 mb
.mb_type
= MB_VOID
;
653 n_err_no
= n_ERR_NOTOBACCO
;
659 newmailinfo(int omsgCount
)
664 for (i
= 0; i
< omsgCount
; ++i
)
665 message
[i
].m_flag
&= ~MNEWEST
;
667 if (msgCount
> omsgCount
) {
668 for (i
= omsgCount
; i
< msgCount
; ++i
)
669 message
[i
].m_flag
|= MNEWEST
;
670 fprintf(n_stdout
, _("New mail has arrived.\n"));
671 if ((i
= msgCount
- omsgCount
) == 1)
672 fprintf(n_stdout
, _("Loaded 1 new message.\n"));
674 fprintf(n_stdout
, _("Loaded %d new messages.\n"), i
);
676 fprintf(n_stdout
, _("Loaded %d messages.\n"), msgCount
);
678 temporary_folder_hook_check(TRU1
);
682 if(ok_blook(header
) && (i
= omsgCount
+ 1) <= msgCount
){
684 if(mb
.mb_type
== MB_IMAP
)
685 imap_getheaders(i
, msgCount
); /* TODO not here */
687 for(omsgCount
= 0; i
<= msgCount
; ++omsgCount
, ++i
)
688 n_msgvec
[omsgCount
] = i
;
689 n_msgvec
[omsgCount
] = 0;
690 print_headers(n_msgvec
, FAL0
, FAL0
);
700 if (n_msgvec
!= NULL
)
702 n_msgvec
= n_calloc(sz
+1, sizeof *n_msgvec
);
707 print_header_summary(char const *Larg
)
714 if(mb
.mb_type
== MB_IMAP
)
715 imap_getheaders(0, msgCount
); /* TODO not here */
717 assert(n_msgvec
!= NULL
);
720 /* Avoid any messages XXX add a make_mua_silent() and use it? */
721 if ((n_poption
& (n_PO_VERB
| n_PO_EXISTONLY
)) == n_PO_EXISTONLY
) {
722 n_stdout
= freopen(n_path_devnull
, "w", stdout
);
723 n_stderr
= freopen(n_path_devnull
, "w", stderr
);
725 i
= (n_getmsglist(n_shexp_quote_cp(Larg
, FAL0
), n_msgvec
, 0, NULL
) <= 0);
726 if (n_poption
& n_PO_EXISTONLY
)
727 n_exit_status
= (int)i
;
729 print_headers(n_msgvec
, TRU1
, FAL0
); /* TODO should be iterator! */
733 for(; UICMP(z
, i
, <, msgCount
); ++i
)
738 for(mp
= threadroot
; mp
; ++i
, mp
= next_in_thread(mp
))
739 n_msgvec
[i
] = (int)PTR2SIZE(mp
- message
+ 1);
741 print_headers(n_msgvec
, FAL0
, TRU1
); /* TODO should be iterator! */
747 n_folder_announce(enum n_announce_flags af
){
751 mdot
= (mb
.mb_type
== MB_VOID
) ? 1 : getmdot(0);
752 dot
= &message
[mdot
- 1];
754 if(af
!= n_ANNOUNCE_NONE
&& ok_blook(header
) &&
755 ((af
& n_ANNOUNCE_MAIN_CALL
) ||
756 ((af
& n_ANNOUNCE_CHANGE
) && !ok_blook(posix
))))
757 af
|= n_ANNOUNCE_STATUS
| n__ANNOUNCE_HEADER
;
759 if(af
& n_ANNOUNCE_STATUS
){
761 af
|= n__ANNOUNCE_ANY
;
764 if(af
& n__ANNOUNCE_HEADER
){
765 if(!(af
& n_ANNOUNCE_MAIN_CALL
) && ok_blook(bsdannounce
))
766 n_OBSOLETE(_("*bsdannounce* is now default behaviour"));
769 print_header_group(vec
); /* XXX errors? */
770 af
|= n__ANNOUNCE_ANY
;
773 if(af
& n__ANNOUNCE_ANY
)
784 enum mflag avoid
= MHIDDEN
| MDELETED
;
788 if (ok_blook(autothread
)) {
789 n_OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
791 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
792 if (mb
.mb_sorted
!= NULL
)
793 n_free(mb
.mb_sorted
);
794 mb
.mb_sorted
= sstrdup(cp
);
798 if (mb
.mb_type
== MB_VOID
) {
804 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
805 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
808 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
809 if (mb
.mb_threaded
) {
810 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
811 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
814 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
815 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
820 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
821 if (mb
.mb_threaded
) {
822 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
823 if (mp
->m_flag
& MFLAGGED
)
826 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
827 if (mp
->m_flag
& MFLAGGED
)
832 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
833 if (mb
.mb_threaded
) {
834 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
835 if (!(mp
->m_flag
& (MREAD
| avoid
)))
838 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
839 if (!(mp
->m_flag
& (MREAD
| avoid
)))
845 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
846 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
847 else if (ok_blook(showlast
)) {
848 if (mb
.mb_threaded
) {
849 for (mp
= this_in_thread(threadroot
, -1); mp
;
850 mp
= prev_in_thread(mp
))
851 if (!(mp
->m_flag
& avoid
))
853 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
855 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
856 if (!(mp
->m_flag
& avoid
))
858 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
861 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
862 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
863 else if (mb
.mb_threaded
) {
864 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
865 if (!(mp
->m_flag
& avoid
))
867 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
869 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
870 if (!(mp
->m_flag
& avoid
))
872 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
873 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
881 initbox(char const *name
)
887 if (mb
.mb_type
!= MB_VOID
)
888 n_strscpy(prevfile
, mailname
, PATH_MAX
);
890 /* TODO name always NE mailname (but goes away for objects anyway)
891 * TODO Well, not true no more except that in parens */
892 _update_mailname((name
!= mailname
) ? name
: NULL
);
895 if((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpmbox", OF_WRONLY
| OF_HOLDSIGS
)) ==
897 n_perr(_("initbox: temporary mail message file, writer"), 0);
899 }else if((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
900 n_perr(_("initbox: temporary mail message file, reader"), 0);
903 Ftmp_release(&tempMesg
);
908 mb
.mb_active
= MB_NONE
;
911 mb
.mb_flags
= MB_NOFLAGS
;
913 if (mb
.mb_sorted
!= NULL
) {
914 n_free(mb
.mb_sorted
);
917 dot
= prevdot
= threadroot
= NULL
;
918 n_pstate
&= ~n_PS_DID_PRINT_DOT
;
923 n_folder_query(void){
924 struct n_string s
, *sp
= &s
;
927 char const *rv
, *adjcp
;
931 sp
= n_string_creat_auto(sp
);
933 /* *folder* is linked with *folder_resolved*: we only use the latter */
935 if((rv
= ok_vlook(folder_resolved
)) != NULL
)
939 * If directory does not start with a <slash> ('/'), the contents
940 * of HOME shall be prefixed to it.
942 * If folder is unset or set to null, [.] filenames beginning with
943 * '+' shall refer to files in the current directory.
944 * We may have the result already */
948 if((cp
= ok_vlook(folder
)) == NULL
)
951 /* Expand the *folder*; skip %: prefix for simplicity of use */
952 if(cp
[0] == '%' && cp
[1] == ':')
954 if((err
= (cp
= fexpand(cp
, FEXP_NSPECIAL
| FEXP_NFOLDER
| FEXP_NSHELL
)
955 ) == NULL
) || *cp
== '\0')
960 for(i
= strlen(cp
);;){
970 switch((proto
= which_protocol(cp
, FAL0
, FAL0
, &adjcp
))){
972 n_err(_("*folder* can't be set to a flat, read-only POP3 account\n"));
978 if(!strcmp(rv
, protbase(rv
)))
979 rv
= savecatsep(rv
, '/', n_empty
);
981 n_err(_("*folder*: IMAP support not compiled in\n"));
986 /* Further expansion desired */
990 /* Prefix HOME as necessary */
991 if(*adjcp
!= '/'){ /* XXX path_is_absolute() */
995 home
= ok_vlook(HOME
);
999 sp
= n_string_reserve(sp
, l1
+ 1 + l2
+1);
1003 sp
= n_string_push_buf(sp
, cp
, i
= PTR2SIZE(adjcp
- cp
));
1007 sp
= n_string_push_buf(sp
, home
, l1
);
1008 sp
= n_string_push_c(sp
, '/');
1009 sp
= n_string_push_buf(sp
, cp
, l2
);
1010 cp
= n_string_cp(sp
);
1011 sp
= n_string_drop_ownership(sp
);
1014 /* TODO Since our visual mailname is resolved via realpath(3) if available
1015 * TODO to avoid that we loose track of our currently open folder in case
1016 * TODO we chdir away, but still checks the leading path portion against
1017 * TODO n_folder_query() to be able to abbreviate to the +FOLDER syntax if
1018 * TODO possible, we need to realpath(3) the folder, too */
1019 #ifndef HAVE_REALPATH
1022 assert(sp
->s_len
== 0 && sp
->s_dat
== NULL
);
1023 # ifndef HAVE_REALPATH_NULL
1024 sp
= n_string_reserve(sp
, PATH_MAX
+1);
1027 if((sp
->s_dat
= realpath(cp
, sp
->s_dat
)) != NULL
){
1028 # ifdef HAVE_REALPATH_NULL
1029 n_string_cp(sp
= n_string_assign_cp(sp
, cp
= sp
->s_dat
));
1033 }else if(n_err_no
== n_ERR_NOENT
)
1036 n_err(_("Can't canonicalize *folder*: %s\n"),
1037 n_shexp_quote_cp(cp
, FAL0
));
1041 sp
= n_string_drop_ownership(sp
);
1042 #endif /* HAVE_REALPATH */
1044 /* Always append a solidus to our result path upon success */
1048 if(rv
[(i
= strlen(rv
)) - 1] != '/'){
1049 sp
= n_string_reserve(sp
, i
+ 1 +1);
1050 sp
= n_string_push_buf(sp
, rv
, i
);
1051 sp
= n_string_push_c(sp
, '/');
1052 rv
= n_string_cp(sp
);
1053 sp
= n_string_drop_ownership(sp
);
1058 n_PS_ROOT_BLOCK(ok_vset(folder_resolved
, rv
));
1062 n_err(_("*folder* is not resolvable, using CWD\n"));
1063 assert(rv
!= NULL
&& *rv
== '\0');
1070 n_folder_mbox_prepare_append(FILE *fout
, struct stat
*st_or_null
){
1071 /* TODO n_folder_mbox_prepare_append -> Mailbox->append() */
1078 if(!fseek(fout
, -2L, SEEK_END
)){
1079 if(fread(buf
, sizeof *buf
, 2, fout
) != 2)
1081 needsep
= (buf
[0] != '\n' || buf
[1] != '\n');
1085 if(st_or_null
== NULL
){
1087 if(fstat(fileno(fout
), st_or_null
))
1091 if(st_or_null
->st_size
>= 2)
1093 if(st_or_null
->st_size
== 0){
1098 if(fseek(fout
, -1L, SEEK_END
))
1100 if(fread(buf
, sizeof *buf
, 1, fout
) != 1)
1102 needsep
= (buf
[0] != '\n');
1106 if(fflush(fout
) || (needsep
&& putc('\n', fout
) == EOF
))