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>.
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
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
38 #ifndef HAVE_AMALGAMATION
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
);
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
);
58 _update_mailname(char const *name
) /* TODO 2MUCH work, cache, prop of Object! */
66 /* Don't realpath(3) if it's only an update request */
72 p
= which_protocol(name
, TRU1
, TRU1
, &adjname
);
74 if(p
== PROTO_FILE
|| p
== PROTO_MAILDIR
){
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
));
83 n_strscpy(mailname
, name
, sizeof(mailname
));
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
))
97 /* We want to see the name of the folder .. on the screen */
99 if(i
< sizeof(displayname
) - 3 -1){
103 memcpy(dispp
, mailp
, foldlen
);
107 memcpy(dispp
, mailp
, i
-= foldlen
);
110 memcpy(dispp
, mailp
, i
+1);
113 /* Avoid disrupting multibyte sequences (if possible) */
114 #ifndef HAVE_C90AMEND1
115 j
= sizeof(displayname
) / 3 - 3;
116 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
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
);
122 snprintf(dispp
, sizeof(displayname
), "%s%.*s...%s",
123 (foldlen
> 0 ? "[+]" : ""), (int)j
, mailp
, mailp
+ i
);
127 n_PS_ROOT_BLOCK((ok_vset(mailbox_resolved
, mailname
),
128 ok_vset(mailbox_display
, displayname
)));
133 #ifdef HAVE_C90AMEND1
135 __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
)
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);
161 #endif /* HAVE_C90AMEND1 */
166 int u
, n
, d
, s
, hidden
, moved
;
169 if(mb
.mb_type
== MB_VOID
){
170 fprintf(n_stdout
, _("(Currently no active mailbox)"));
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
)
178 if ((mp
->m_flag
& MREAD
) == 0)
180 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
182 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
184 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
186 if (mp
->m_flag
& MHIDDEN
)
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
));
195 fprintf(n_stdout
, _("1 message"));
197 fprintf(n_stdout
, _("%d messages"), msgCount
);
199 fprintf(n_stdout
, _(" %d new"), n
);
201 fprintf(n_stdout
, _(" %d unread"), u
);
203 fprintf(n_stdout
, _(" %d deleted"), d
);
205 fprintf(n_stdout
, _(" %d saved"), s
);
207 fprintf(n_stdout
, _(" %d moved"), moved
);
209 fprintf(n_stdout
, _(" %d hidden"), hidden
);
211 fprintf(n_stdout
, _(" [Read-only]"));
213 if (mb
.mb_type
== MB_CACHE
)
214 fprintf(n_stdout
, _(" [Disconnected]"));
218 putc('\n', n_stdout
);
223 a_folder_mbox_setptr(FILE *ibuf
, off_t offset
) /* TODO Mailbox->setptr() */
229 bool_t need_rfc4155
, maybe
, inhead
, from_
;
230 size_t filesize
, linesize
, cnt
;
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
);
241 linebuf
= NULL
, linesize
= 0; /* TODO string pool */
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
;
249 message_append(&self
);
250 message_append_null();
257 if (linebuf
[0] == '\0')
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);
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"),
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 "
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
;
298 message_append(&self
);
300 self
.m_flag
= MUSED
| MNEW
| MNEWEST
;
303 self
.m_block
= mailx_blockof(offset
);
304 self
.m_offset
= mailx_offsetof(offset
);
306 } else if (linebuf
[0] == 0) {
309 for (cp
= linebuf
, cp2
= "status";; ++cp
) {
310 if ((c
= *cp2
++) == 0) {
311 while (c
= *cp
++, whitechar(c
))
315 while ((c
= *cp
++) != '\0')
317 self
.m_flag
|= MREAD
;
319 self
.m_flag
&= ~MNEW
;
322 if (*cp
!= c
&& *cp
!= upperconv(c
))
325 for (cp
= linebuf
, cp2
= "x-status";; ++cp
) {
326 if ((c
= *cp2
++) == 0) {
327 while ((c
= *cp
++, whitechar(c
)))
331 while ((c
= *cp
++) != '\0')
333 self
.m_flag
|= MFLAGGED
;
335 self
.m_flag
|= MANSWERED
;
337 self
.m_flag
|= MDRAFTED
;
340 if (*cp
!= c
&& *cp
!= upperconv(c
))
347 maybe
= (linebuf
[0] == 0);
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:: */
360 char const *who
, *orig_name
;
361 int rv
, omsgCount
= 0;
362 FILE *ibuf
= NULL
, *lckfp
= NULL
;
363 bool_t isdevnull
= FAL0
;
366 n_pstate
&= ~n_PS_SETFILE_OPENED
;
369 enum fexp_mode fexpm
;
371 if((who
= shortcut_expand(name
)) != NULL
){
372 fexpm
= FEXP_NSHORTCUT
/* XXX | FEXP_NSHELL*/;
380 fm
|= FEDIT_SYSBOX
; /* TODO fexpand() needs to tell is-valid-user! */
381 if(*(who
= &name
[1]) == ':')
383 if((cp
= strrchr(who
, '/')) != NULL
)
389 who
= ok_vlook(LOGNAME
);
391 if ((name
= fexpand(name
, fexpm
)) == NULL
)
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
)) {
400 isdevnull
= ((n_poption
& n_PO_BATCH_FLAG
) &&
401 !strcmp(name
, n_path_devnull
));
403 do { /* TODO we need objects, goes away then */
404 # ifdef HAVE_REALPATH_NULL
407 if ((cp
= realpath(name
, NULL
)) != NULL
) {
414 if (realpath(name
, cbuf
) != NULL
)
415 name
= savestr(cbuf
);
423 rv
= maildir_setfile(name
, fm
);
428 rv
= pop3_setfile(orig_name
, fm
);
434 if((fm
& FEDIT_NEWMAIL
) && mb
.mb_type
== MB_CACHE
)
437 rv
= imap_setfile(orig_name
, fm
);
441 n_err(_("Cannot handle protocol: %s\n"), orig_name
);
445 if ((ibuf
= n_fopen_any(savecat("file://", name
), "r", NULL
)) == NULL
) {
448 if ((fm
& FEDIT_SYSBOX
) && e
== n_ERR_NOENT
) {
449 if (strcmp(who
, ok_vlook(LOGNAME
)) && getpwnam(who
) == NULL
) {
450 n_err(_("%s is not a user of this system\n"),
451 n_shexp_quote_cp(who
, FAL0
));
454 if (!(n_poption
& n_PO_QUICKRUN_MASK
) && ok_blook(bsdcompat
))
455 n_err(_("No mail for %s\n"), who
);
457 if (fm
& FEDIT_NEWMAIL
)
460 mb
.mb_type
= MB_VOID
;
461 if (ok_blook(emptystart
)) {
462 if (!(n_poption
& n_PO_QUICKRUN_MASK
) && !ok_blook(bsdcompat
))
464 /* We must avoid returning -1 and causing program exit */
472 if (fstat(fileno(ibuf
), &stb
) == -1) {
473 if (fm
& FEDIT_NEWMAIL
)
475 n_perr(_("fstat"), 0);
479 if (S_ISREG(stb
.st_mode
) || isdevnull
) {
482 if (fm
& FEDIT_NEWMAIL
)
484 n_err_no
= S_ISDIR(stb
.st_mode
) ? n_ERR_ISDIR
: n_ERR_INVAL
;
489 if (shudclob
&& !(fm
& FEDIT_NEWMAIL
) && !quit(FAL0
))
495 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
496 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
499 /* TODO There is no intermediate VOID box we've switched to: name may
500 * TODO point to the same box that we just have written, so any updates
501 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
502 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
503 /* TODO We may uselessly open twice but quit() doesn't say whether we were
504 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
505 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name
, mailname
)) {
509 if ((ibuf
= n_fopen_any(name
, "r", NULL
)) == NULL
||
510 fstat(fileno(ibuf
), &stb
) == -1 ||
511 (!S_ISREG(stb
.st_mode
) && !isdevnull
)) {
518 /* Copy the messages into /tmp and set pointers */
519 if (!(fm
& FEDIT_NEWMAIL
)) {
521 mb
.mb_type
= MB_VOID
;
524 mb
.mb_type
= MB_FILE
;
525 mb
.mb_perm
= (((n_poption
& n_PO_R_FLAG
) || (fm
& FEDIT_RDONLY
) ||
526 access(name
, W_OK
) < 0) ? 0 : MB_DELE
| MB_EDIT
);
539 if (fm
& FEDIT_SYSBOX
)
540 n_pstate
&= ~n_PS_EDIT
;
542 n_pstate
|= n_PS_EDIT
;
546 fseek(mb
.mb_otf
, 0L, SEEK_END
);
547 /* TODO Doing this without holding a lock is.. And not err checking.. */
548 fseek(ibuf
, mailsize
, SEEK_SET
);
550 omsgCount
= msgCount
;
555 else if (!(n_pstate
& n_PS_EDIT
))
556 lckfp
= n_dotlock(name
, fileno(ibuf
), FLT_READ
, offset
,0,
557 (fm
& FEDIT_NEWMAIL
? 0 : UIZ_MAX
));
558 else if (n_file_lock(fileno(ibuf
), FLT_READ
, offset
,0,
559 (fm
& FEDIT_NEWMAIL
? 0 : UIZ_MAX
)))
563 if (!(fm
& FEDIT_NEWMAIL
)) {
564 char const *emsg
= (n_pstate
& n_PS_EDIT
)
565 ? N_("Unable to lock mailbox, aborting operation")
566 : N_("Unable to (dot) lock mailbox, aborting operation");
570 if (!(fm
& FEDIT_NEWMAIL
))
575 mailsize
= fsize(ibuf
);
577 /* TODO This is too simple minded? We should regenerate an index file
578 * TODO to be able to truly tell whether *anything* has changed! */
579 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
583 a_folder_mbox_setptr(ibuf
, offset
);
585 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
592 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1) {
597 if (!(fm
& FEDIT_NEWMAIL
)) {
598 n_pstate
&= ~n_PS_SAW_COMMAND
;
599 n_pstate
|= n_PS_SETFILE_OPENED
;
604 rv
= (msgCount
== 0);
606 n_err_no
= n_ERR_NODATA
;
608 if(n_poption
& n_PO_EXISTONLY
)
612 if(!(n_pstate
& n_PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)){
613 if(!(fm
& FEDIT_NEWMAIL
)){
614 if (!ok_blook(emptystart
))
615 n_err(_("No mail for %s\n"), who
);
621 if(fm
& FEDIT_NEWMAIL
)
622 newmailinfo(omsgCount
);
626 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
632 mb
.mb_type
= MB_VOID
;
634 n_err_no
= n_ERR_NOTOBACCO
;
640 newmailinfo(int omsgCount
)
645 for (i
= 0; i
< omsgCount
; ++i
)
646 message
[i
].m_flag
&= ~MNEWEST
;
648 if (msgCount
> omsgCount
) {
649 for (i
= omsgCount
; i
< msgCount
; ++i
)
650 message
[i
].m_flag
|= MNEWEST
;
651 fprintf(n_stdout
, _("New mail has arrived.\n"));
652 if ((i
= msgCount
- omsgCount
) == 1)
653 fprintf(n_stdout
, _("Loaded 1 new message.\n"));
655 fprintf(n_stdout
, _("Loaded %d new messages.\n"), i
);
657 fprintf(n_stdout
, _("Loaded %d messages.\n"), msgCount
);
659 temporary_folder_hook_check(TRU1
);
663 if (ok_blook(header
))
664 print_headers(omsgCount
+ 1, msgCount
, FAL0
);
673 if (n_msgvec
!= NULL
)
675 n_msgvec
= n_calloc(sz
+ 1, sizeof *n_msgvec
);
680 print_header_summary(char const *Larg
)
682 size_t bot
, top
, i
, j
;
686 /* Avoid any messages XXX add a make_mua_silent() and use it? */
687 if ((n_poption
& (n_PO_VERB
| n_PO_EXISTONLY
)) == n_PO_EXISTONLY
) {
688 n_stdout
= freopen(n_path_devnull
, "w", stdout
);
689 n_stderr
= freopen(n_path_devnull
, "w", stderr
);
691 assert(n_msgvec
!= NULL
);
692 i
= (getmsglist(/*TODO make const */n_UNCONST(Larg
), n_msgvec
, 0) <= 0);
693 if (n_poption
& n_PO_EXISTONLY
) {
694 n_exit_status
= (int)i
;
699 for (bot
= msgCount
, top
= 0, i
= 0; (j
= n_msgvec
[i
]) != 0; ++i
) {
706 bot
= 1, top
= msgCount
;
707 print_headers(bot
, top
, (Larg
!= NULL
)); /* TODO should take iterator!! */
713 n_folder_announce(enum n_announce_flags af
){
717 mdot
= (mb
.mb_type
== MB_VOID
) ? 1 : getmdot(0);
718 dot
= &message
[mdot
- 1];
720 if(af
!= n_ANNOUNCE_NONE
&& ok_blook(header
) &&
721 ((af
& n_ANNOUNCE_MAIN_CALL
) ||
722 ((af
& n_ANNOUNCE_CHANGE
) && !ok_blook(posix
))))
723 af
|= n_ANNOUNCE_STATUS
| n__ANNOUNCE_HEADER
;
725 if(af
& n_ANNOUNCE_STATUS
){
727 af
|= n__ANNOUNCE_ANY
;
730 if(af
& n__ANNOUNCE_HEADER
){
731 if(!(af
& n_ANNOUNCE_MAIN_CALL
) && ok_blook(bsdannounce
))
732 n_OBSOLETE(_("*bsdannounce* is now default behaviour"));
735 print_header_group(vec
); /* XXX errors? */
736 af
|= n__ANNOUNCE_ANY
;
739 if(af
& n__ANNOUNCE_ANY
)
750 enum mflag avoid
= MHIDDEN
| MDELETED
;
754 if (ok_blook(autothread
)) {
755 n_OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
757 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
758 if (mb
.mb_sorted
!= NULL
)
759 n_free(mb
.mb_sorted
);
760 mb
.mb_sorted
= sstrdup(cp
);
764 if (mb
.mb_type
== MB_VOID
) {
770 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
771 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
774 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
775 if (mb
.mb_threaded
) {
776 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
777 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
780 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
781 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
786 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
787 if (mb
.mb_threaded
) {
788 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
789 if (mp
->m_flag
& MFLAGGED
)
792 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
793 if (mp
->m_flag
& MFLAGGED
)
798 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
799 if (mb
.mb_threaded
) {
800 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
801 if (!(mp
->m_flag
& (MREAD
| avoid
)))
804 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
805 if (!(mp
->m_flag
& (MREAD
| avoid
)))
811 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
812 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
813 else if (ok_blook(showlast
)) {
814 if (mb
.mb_threaded
) {
815 for (mp
= this_in_thread(threadroot
, -1); mp
;
816 mp
= prev_in_thread(mp
))
817 if (!(mp
->m_flag
& avoid
))
819 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
821 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
822 if (!(mp
->m_flag
& avoid
))
824 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
827 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
828 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
829 else if (mb
.mb_threaded
) {
830 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
831 if (!(mp
->m_flag
& avoid
))
833 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
835 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
836 if (!(mp
->m_flag
& avoid
))
838 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
839 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
847 initbox(char const *name
)
853 if (mb
.mb_type
!= MB_VOID
)
854 n_strscpy(prevfile
, mailname
, PATH_MAX
);
856 /* TODO name always NE mailname (but goes away for objects anyway)
857 * TODO Well, not true no more except that in parens */
858 _update_mailname((name
!= mailname
) ? name
: NULL
);
861 if((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpmbox", OF_WRONLY
| OF_HOLDSIGS
)) ==
863 n_perr(_("initbox: temporary mail message file, writer"), 0);
865 }else if((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
866 n_perr(_("initbox: temporary mail message file, reader"), 0);
869 Ftmp_release(&tempMesg
);
874 mb
.mb_active
= MB_NONE
;
877 mb
.mb_flags
= MB_NOFLAGS
;
879 if (mb
.mb_sorted
!= NULL
) {
880 n_free(mb
.mb_sorted
);
883 dot
= prevdot
= threadroot
= NULL
;
884 n_pstate
&= ~n_PS_DID_PRINT_DOT
;
889 n_folder_query(void){
890 struct n_string s
, *sp
= &s
;
893 char const *rv
, *adjcp
;
897 sp
= n_string_creat_auto(sp
);
899 /* *folder* is linked with *folder_resolved*: we only use the latter */
901 if((rv
= ok_vlook(folder_resolved
)) != NULL
)
905 * If directory does not start with a <slash> ('/'), the contents
906 * of HOME shall be prefixed to it.
908 * If folder is unset or set to null, [.] filenames beginning with
909 * '+' shall refer to files in the current directory.
910 * We may have the result already */
914 if((cp
= ok_vlook(folder
)) == NULL
)
917 /* Expand the *folder*; skip %: prefix for simplicity of use */
918 if(cp
[0] == '%' && cp
[1] == ':')
920 if((err
= (cp
= fexpand(cp
, FEXP_NSPECIAL
| FEXP_NFOLDER
| FEXP_NSHELL
)
921 ) == NULL
) || *cp
== '\0')
926 for(i
= strlen(cp
);;){
936 switch((proto
= which_protocol(cp
, FAL0
, FAL0
, &adjcp
))){
938 n_err(_("*folder* can't be set to a flat, read-only POP3 account\n"));
944 if(!strcmp(rv
, protbase(rv
)))
945 rv
= savecatsep(rv
, '/', n_empty
);
947 n_err(_("*folder*: IMAP support not compiled in\n"));
952 /* Further expansion desired */
956 /* Prefix HOME as necessary */
957 if(*adjcp
!= '/'){ /* XXX path_is_absolute() */
961 home
= ok_vlook(HOME
);
965 sp
= n_string_reserve(sp
, l1
+ 1 + l2
+1);
969 sp
= n_string_push_buf(sp
, cp
, i
= PTR2SIZE(adjcp
- cp
));
973 sp
= n_string_push_buf(sp
, home
, l1
);
974 sp
= n_string_push_c(sp
, '/');
975 sp
= n_string_push_buf(sp
, cp
, l2
);
976 cp
= n_string_cp(sp
);
977 sp
= n_string_drop_ownership(sp
);
980 /* TODO Since our visual mailname is resolved via realpath(3) if available
981 * TODO to avoid that we loose track of our currently open folder in case
982 * TODO we chdir away, but still checks the leading path portion against
983 * TODO n_folder_query() to be able to abbreviate to the +FOLDER syntax if
984 * TODO possible, we need to realpath(3) the folder, too */
985 #ifndef HAVE_REALPATH
988 assert(sp
->s_len
== 0 && sp
->s_dat
== NULL
);
989 # ifndef HAVE_REALPATH_NULL
990 sp
= n_string_reserve(sp
, PATH_MAX
+1);
993 if((sp
->s_dat
= realpath(cp
, sp
->s_dat
)) != NULL
){
994 # ifdef HAVE_REALPATH_NULL
995 n_string_cp(sp
= n_string_assign_cp(sp
, cp
= sp
->s_dat
));
999 }else if(n_err_no
== n_ERR_NOENT
)
1002 n_err(_("Can't canonicalize *folder*: %s\n"),
1003 n_shexp_quote_cp(cp
, FAL0
));
1007 sp
= n_string_drop_ownership(sp
);
1008 #endif /* HAVE_REALPATH */
1010 /* Always append a solidus to our result path upon success */
1014 if(rv
[(i
= strlen(rv
)) - 1] != '/'){
1015 sp
= n_string_reserve(sp
, i
+ 1 +1);
1016 sp
= n_string_push_buf(sp
, rv
, i
);
1017 sp
= n_string_push_c(sp
, '/');
1018 rv
= n_string_cp(sp
);
1019 sp
= n_string_drop_ownership(sp
);
1024 n_PS_ROOT_BLOCK(ok_vset(folder_resolved
, rv
));
1028 n_err(_("*folder* is not resolvable, using CWD\n"));
1029 assert(rv
!= NULL
&& *rv
== '\0');
1036 n_folder_mbox_prepare_append(FILE *fout
, struct stat
*st_or_null
){
1037 /* TODO n_folder_mbox_prepare_append -> Mailbox->append() */
1044 if(!fseek(fout
, -2L, SEEK_END
)){
1045 if(fread(buf
, sizeof *buf
, 2, fout
) != 2)
1047 needsep
= (buf
[0] != '\n' || buf
[1] != '\n');
1051 if(st_or_null
== NULL
){
1053 if(fstat(fileno(fout
), st_or_null
))
1057 if(st_or_null
->st_size
>= 2)
1059 if(st_or_null
->st_size
== 0){
1064 if(fseek(fout
, -1L, SEEK_END
))
1066 if(fread(buf
, sizeof *buf
, 1, fout
) != 1)
1068 needsep
= (buf
[0] != '\n');
1072 if(fflush(fout
) || (needsep
&& putc('\n', fout
) == EOF
))