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 - 2017 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 SINLINE
size_t __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
);
52 static void a_folder_info(void);
55 _update_mailname(char const *name
) /* TODO 2MUCH work, cache, prop of Object! */
63 /* Don't realpath(3) if it's only an update request */
67 enum protocol p
= which_protocol(name
, TRU1
, TRU1
, &adjname
);
69 if(p
== PROTO_FILE
|| p
== PROTO_MAILDIR
){
71 if (realpath(name
, mailname
) == NULL
&& n_err_no
!= n_ERR_NOENT
) {
72 n_err(_("Can't canonicalize %s\n"), n_shexp_quote_cp(name
, FAL0
));
79 n_strscpy(mailname
, name
, sizeof(mailname
));
85 /* Don't display an absolute path but "+FOLDER" if under *folder* */
86 if(*(foldp
= n_folder_query()) != '\0'){
87 foldlen
= strlen(foldp
);
88 if(strncmp(foldp
, mailp
, foldlen
))
93 /* We want to see the name of the folder .. on the screen */
95 if(i
< sizeof(displayname
) - 3 -1){
99 memcpy(dispp
, mailp
, foldlen
);
103 memcpy(dispp
, mailp
, i
-= foldlen
);
106 memcpy(dispp
, mailp
, i
+1);
109 /* Avoid disrupting multibyte sequences (if possible) */
110 #ifndef HAVE_C90AMEND1
111 j
= sizeof(displayname
) / 3 - 3;
112 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
114 j
= field_detect_clip(sizeof(displayname
) / 3, mailp
, i
);
115 i
= j
+ __narrow_suffix(mailp
+ j
, i
- j
,
116 sizeof(displayname
) - (1/* + */ + 3 + 1) - j
);
118 snprintf(dispp
, sizeof(displayname
), "%s%.*s...%s",
119 (foldlen
> 0 ? "[+]" : ""), (int)j
, mailp
, mailp
+ i
);
123 n_PS_ROOT_BLOCK((ok_vset(mailbox_resolved
, mailname
),
124 ok_vset(mailbox_display
, displayname
)));
129 #ifdef HAVE_C90AMEND1
131 __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
)
137 for (err
= ok
= i
= 0; cpl
> maxl
|| err
;) {
138 int ml
= mblen(cp
, cpl
);
139 if (ml
< 0) { /* XXX _narrow_suffix(): mblen() error; action? */
140 (void)mblen(NULL
, 0);
157 #endif /* HAVE_C90AMEND1 */
162 int u
, n
, d
, s
, hidden
, moved
;
165 if(mb
.mb_type
== MB_VOID
){
166 fprintf(n_stdout
, _("(Currently no active mailbox)"));
170 s
= d
= hidden
= moved
= 0;
171 for (mp
= message
, n
= 0, u
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
172 if (mp
->m_flag
& MNEW
)
174 if ((mp
->m_flag
& MREAD
) == 0)
176 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
178 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
180 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
182 if (mp
->m_flag
& MHIDDEN
)
186 /* If displayname gets truncated the user effectively has no option to see
187 * the full pathname of the mailbox, so print it at least for '? fi' */
188 fprintf(n_stdout
, "%s: ", n_shexp_quote_cp(
189 (_update_mailname(NULL
) ? displayname
: mailname
), FAL0
));
191 fprintf(n_stdout
, _("1 message"));
193 fprintf(n_stdout
, _("%d messages"), msgCount
);
195 fprintf(n_stdout
, _(" %d new"), n
);
197 fprintf(n_stdout
, _(" %d unread"), u
);
199 fprintf(n_stdout
, _(" %d deleted"), d
);
201 fprintf(n_stdout
, _(" %d saved"), s
);
203 fprintf(n_stdout
, _(" %d moved"), moved
);
205 fprintf(n_stdout
, _(" %d hidden"), hidden
);
207 fprintf(n_stdout
, _(" [Read-only]"));
209 if (mb
.mb_type
== MB_CACHE
)
210 fprintf(n_stdout
, _(" [Disconnected]"));
214 putc('\n', n_stdout
);
219 setfile(char const *name
, enum fedit_mode fm
) /* TODO oh my god */
221 /* TODO This all needs to be converted to URL:: and Mailbox:: */
226 char const *who
, *orig_name
;
227 int rv
, omsgCount
= 0;
228 FILE *ibuf
= NULL
, *lckfp
= NULL
;
229 bool_t isdevnull
= FAL0
;
232 n_pstate
&= ~n_PS_SETFILE_OPENED
;
235 enum fexp_mode fexpm
;
237 if((who
= shortcut_expand(name
)) != NULL
){
238 fexpm
= FEXP_NSHORTCUT
/* XXX | FEXP_NSHELL*/;
241 fexpm
= FEXP_FULL
/* XXX FEXP_NSHELL*/;
246 fm
|= FEDIT_SYSBOX
; /* TODO fexpand() needs to tell is-valid-user! */
247 if(*(who
= &name
[1]) == ':')
249 if((cp
= strrchr(who
, '/')) != NULL
)
255 who
= ok_vlook(LOGNAME
);
257 if ((name
= fexpand(name
, fexpm
)) == NULL
)
261 /* For at least substdate() users TODO -> eventloop tick */
262 time_current_update(&time_current
, FAL0
);
264 switch (which_protocol(orig_name
= name
, TRU1
, TRU1
, &name
)) {
266 isdevnull
= ((n_poption
& n_PO_BATCH_FLAG
) &&
267 !strcmp(name
, n_path_devnull
));
269 do { /* TODO we need objects, goes away then */
270 # ifdef HAVE_REALPATH_NULL
273 if ((cp
= realpath(name
, NULL
)) != NULL
) {
280 if (realpath(name
, cbuf
) != NULL
)
281 name
= savestr(cbuf
);
289 rv
= maildir_setfile(name
, fm
);
294 rv
= pop3_setfile(orig_name
, fm
);
300 if((fm
& FEDIT_NEWMAIL
) && mb
.mb_type
== MB_CACHE
)
303 rv
= imap_setfile(orig_name
, fm
);
307 n_err(_("Cannot handle protocol: %s\n"), orig_name
);
311 if ((ibuf
= n_fopen_any(savecat("file://", name
), "r", NULL
)) == NULL
) {
314 if ((fm
& FEDIT_SYSBOX
) && e
== n_ERR_NOENT
) {
315 if (strcmp(who
, ok_vlook(LOGNAME
)) && getpwnam(who
) == NULL
) {
316 n_err(_("%s is not a user of this system\n"),
317 n_shexp_quote_cp(who
, FAL0
));
320 if (!(n_poption
& n_PO_QUICKRUN_MASK
) && ok_blook(bsdcompat
))
321 n_err(_("No mail for %s\n"), who
);
323 if (fm
& FEDIT_NEWMAIL
)
326 mb
.mb_type
= MB_VOID
;
327 if (ok_blook(emptystart
)) {
328 if (!(n_poption
& n_PO_QUICKRUN_MASK
) && !ok_blook(bsdcompat
))
330 /* We must avoid returning -1 and causing program exit */
338 if (fstat(fileno(ibuf
), &stb
) == -1) {
339 if (fm
& FEDIT_NEWMAIL
)
341 n_perr(_("fstat"), 0);
345 if (S_ISREG(stb
.st_mode
) || isdevnull
) {
348 if (fm
& FEDIT_NEWMAIL
)
350 n_err_no
= S_ISDIR(stb
.st_mode
) ? n_ERR_ISDIR
: n_ERR_INVAL
;
355 if (shudclob
&& !(fm
& FEDIT_NEWMAIL
) && !quit(FAL0
))
361 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
362 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
365 /* TODO There is no intermediate VOID box we've switched to: name may
366 * TODO point to the same box that we just have written, so any updates
367 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
368 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
369 /* TODO We may uselessly open twice but quit() doesn't say whether we were
370 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
371 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name
, mailname
)) {
375 if ((ibuf
= n_fopen_any(name
, "r", NULL
)) == NULL
||
376 fstat(fileno(ibuf
), &stb
) == -1 ||
377 (!S_ISREG(stb
.st_mode
) && !isdevnull
)) {
384 /* Copy the messages into /tmp and set pointers */
385 if (!(fm
& FEDIT_NEWMAIL
)) {
387 mb
.mb_type
= MB_VOID
;
390 mb
.mb_type
= MB_FILE
;
391 mb
.mb_perm
= (((n_poption
& n_PO_R_FLAG
) || (fm
& FEDIT_RDONLY
) ||
392 access(name
, W_OK
) < 0) ? 0 : MB_DELE
| MB_EDIT
);
405 if (fm
& FEDIT_SYSBOX
)
406 n_pstate
&= ~n_PS_EDIT
;
408 n_pstate
|= n_PS_EDIT
;
412 fseek(mb
.mb_otf
, 0L, SEEK_END
);
413 /* TODO Doing this without holding a lock is.. And not err checking.. */
414 fseek(ibuf
, mailsize
, SEEK_SET
);
416 omsgCount
= msgCount
;
421 else if (!(n_pstate
& n_PS_EDIT
))
422 lckfp
= n_dotlock(name
, fileno(ibuf
), FLT_READ
, offset
,0,
423 (fm
& FEDIT_NEWMAIL
? 0 : UIZ_MAX
));
424 else if (n_file_lock(fileno(ibuf
), FLT_READ
, offset
,0,
425 (fm
& FEDIT_NEWMAIL
? 0 : UIZ_MAX
)))
429 if (!(fm
& FEDIT_NEWMAIL
)) {
430 char const *emsg
= (n_pstate
& n_PS_EDIT
)
431 ? N_("Unable to lock mailbox, aborting operation")
432 : N_("Unable to (dot) lock mailbox, aborting operation");
436 if (!(fm
& FEDIT_NEWMAIL
))
441 mailsize
= fsize(ibuf
);
443 /* TODO This is too simple minded? We should regenerate an index file
444 * TODO to be able to truly tell whether *anything* has changed! */
445 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
449 setptr(ibuf
, offset
);
451 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
458 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1) {
463 if (!(fm
& FEDIT_NEWMAIL
)) {
464 n_pstate
&= ~n_PS_SAW_COMMAND
;
465 n_pstate
|= n_PS_SETFILE_OPENED
;
470 rv
= (msgCount
== 0);
472 n_err_no
= n_ERR_NODATA
;
474 if(n_poption
& n_PO_EXISTONLY
)
478 if(!(n_pstate
& n_PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)){
479 if(!(fm
& FEDIT_NEWMAIL
)){
480 if (!ok_blook(emptystart
))
481 n_err(_("No mail for %s\n"), who
);
487 if(fm
& FEDIT_NEWMAIL
)
488 newmailinfo(omsgCount
);
492 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
498 mb
.mb_type
= MB_VOID
;
500 n_err_no
= n_ERR_NOTOBACCO
;
506 newmailinfo(int omsgCount
)
511 for (i
= 0; i
< omsgCount
; ++i
)
512 message
[i
].m_flag
&= ~MNEWEST
;
514 if (msgCount
> omsgCount
) {
515 for (i
= omsgCount
; i
< msgCount
; ++i
)
516 message
[i
].m_flag
|= MNEWEST
;
517 fprintf(n_stdout
, _("New mail has arrived.\n"));
518 if ((i
= msgCount
- omsgCount
) == 1)
519 fprintf(n_stdout
, _("Loaded 1 new message.\n"));
521 fprintf(n_stdout
, _("Loaded %d new messages.\n"), i
);
523 fprintf(n_stdout
, _("Loaded %d messages.\n"), msgCount
);
525 temporary_folder_hook_check(TRU1
);
529 if (ok_blook(header
))
530 print_headers(omsgCount
+ 1, msgCount
, FAL0
);
539 if (n_msgvec
!= NULL
)
541 n_msgvec
= scalloc(sz
+ 1, sizeof *n_msgvec
);
546 print_header_summary(char const *Larg
)
548 size_t bot
, top
, i
, j
;
552 /* Avoid any messages XXX add a make_mua_silent() and use it? */
553 if ((n_poption
& (n_PO_VERB
| n_PO_EXISTONLY
)) == n_PO_EXISTONLY
) {
554 n_stdout
= freopen(n_path_devnull
, "w", stdout
);
555 n_stderr
= freopen(n_path_devnull
, "w", stderr
);
557 assert(n_msgvec
!= NULL
);
558 i
= (getmsglist(/*TODO make const */n_UNCONST(Larg
), n_msgvec
, 0) <= 0);
559 if (n_poption
& n_PO_EXISTONLY
) {
560 n_exit_status
= (int)i
;
565 for (bot
= msgCount
, top
= 0, i
= 0; (j
= n_msgvec
[i
]) != 0; ++i
) {
572 bot
= 1, top
= msgCount
;
573 print_headers(bot
, top
, (Larg
!= NULL
)); /* TODO should take iterator!! */
579 n_folder_announce(enum n_announce_flags af
){
583 mdot
= (mb
.mb_type
== MB_VOID
) ? 1 : getmdot(0);
584 dot
= &message
[mdot
- 1];
586 if(af
!= n_ANNOUNCE_NONE
&& ok_blook(header
) &&
587 ((af
& n_ANNOUNCE_MAIN_CALL
) ||
588 ((af
& n_ANNOUNCE_CHANGE
) && !ok_blook(posix
))))
589 af
|= n_ANNOUNCE_STATUS
| n__ANNOUNCE_HEADER
;
591 if(af
& n_ANNOUNCE_STATUS
){
593 af
|= n__ANNOUNCE_ANY
;
596 if(af
& n__ANNOUNCE_HEADER
){
597 if(!(af
& n_ANNOUNCE_MAIN_CALL
) && ok_blook(bsdannounce
))
598 n_OBSOLETE(_("*bsdannounce* is now default behaviour"));
601 print_header_group(vec
); /* XXX errors? */
602 af
|= n__ANNOUNCE_ANY
;
605 if(af
& n__ANNOUNCE_ANY
)
616 enum mflag avoid
= MHIDDEN
| MDELETED
;
620 if (ok_blook(autothread
)) {
621 n_OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
623 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
624 if (mb
.mb_sorted
!= NULL
)
626 mb
.mb_sorted
= sstrdup(cp
);
630 if (mb
.mb_type
== MB_VOID
) {
636 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
637 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
640 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
641 if (mb
.mb_threaded
) {
642 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
643 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
646 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
647 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
652 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
653 if (mb
.mb_threaded
) {
654 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
655 if (mp
->m_flag
& MFLAGGED
)
658 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
659 if (mp
->m_flag
& MFLAGGED
)
664 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
665 if (mb
.mb_threaded
) {
666 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
667 if (!(mp
->m_flag
& (MREAD
| avoid
)))
670 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
671 if (!(mp
->m_flag
& (MREAD
| avoid
)))
677 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
678 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
679 else if (ok_blook(showlast
)) {
680 if (mb
.mb_threaded
) {
681 for (mp
= this_in_thread(threadroot
, -1); mp
;
682 mp
= prev_in_thread(mp
))
683 if (!(mp
->m_flag
& avoid
))
685 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
687 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
688 if (!(mp
->m_flag
& avoid
))
690 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
693 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
694 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
695 else if (mb
.mb_threaded
) {
696 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
697 if (!(mp
->m_flag
& avoid
))
699 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
701 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
702 if (!(mp
->m_flag
& avoid
))
704 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
705 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
713 initbox(char const *name
)
718 if (mb
.mb_type
!= MB_VOID
)
719 n_strscpy(prevfile
, mailname
, PATH_MAX
);
721 /* TODO name always NE mailname (but goes away for objects anyway)
722 * TODO Well, not true no more except that in parens */
723 _update_mailname((name
!= mailname
) ? name
: NULL
);
725 if ((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpmbox", OF_WRONLY
| OF_HOLDSIGS
)) ==
727 n_perr(_("temporary mail message file"), 0);
730 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
731 n_perr(_("temporary mail message file"), 0);
734 Ftmp_release(&tempMesg
);
737 mb
.mb_active
= MB_NONE
;
740 mb
.mb_flags
= MB_NOFLAGS
;
742 if (mb
.mb_sorted
!= NULL
) {
746 dot
= prevdot
= threadroot
= NULL
;
747 n_pstate
&= ~n_PS_DID_PRINT_DOT
;
752 n_folder_query(void){
753 struct n_string s
, *sp
= &s
;
756 char const *rv
, *adjcp
;
760 sp
= n_string_creat_auto(sp
);
762 /* *folder* is linked with *folder_resolved*: we only use the latter */
764 if((rv
= ok_vlook(folder_resolved
)) != NULL
)
768 * If directory does not start with a <slash> ('/'), the contents
769 * of HOME shall be prefixed to it.
771 * If folder is unset or set to null, [.] filenames beginning with
772 * '+' shall refer to files in the current directory.
773 * We may have the result already */
777 if((cp
= ok_vlook(folder
)) == NULL
)
780 /* Expand the *folder*; skip %: prefix for simplicity of use */
781 if(cp
[0] == '%' && cp
[1] == ':')
783 if((err
= (cp
= fexpand(cp
, FEXP_NSPECIAL
| FEXP_NFOLDER
| FEXP_NSHELL
)
784 ) == NULL
) || *cp
== '\0')
789 for(i
= strlen(cp
);;){
799 switch((proto
= which_protocol(cp
, FAL0
, FAL0
, &adjcp
))){
801 n_err(_("*folder* can't be set to a flat, read-only POP3 account\n"));
807 if(!strcmp(rv
, protbase(rv
)))
808 rv
= savecatsep(rv
, '/', n_empty
);
810 n_err(_("*folder*: IMAP support not compiled in\n"));
815 /* Further expansion desired */
819 /* Prefix HOME as necessary */
820 if(*adjcp
!= '/'){ /* XXX path_is_absolute() */
824 home
= ok_vlook(HOME
);
828 sp
= n_string_reserve(sp
, l1
+ 1 + l2
+1);
832 sp
= n_string_push_buf(sp
, cp
, i
= PTR2SIZE(adjcp
- cp
));
836 sp
= n_string_push_buf(sp
, home
, l1
);
837 sp
= n_string_push_c(sp
, '/');
838 sp
= n_string_push_buf(sp
, cp
, l2
);
839 cp
= n_string_cp(sp
);
840 sp
= n_string_drop_ownership(sp
);
845 /* TODO Since our visual mailname is resolved via realpath(3) if available
846 * TODO to avoid that we loose track of our currently open folder in case
847 * TODO we chdir away, but still checks the leading path portion against
848 * TODO n_folder_query() to be able to abbreviate to the +FOLDER syntax if
849 * TODO possible, we need to realpath(3) the folder, too */
851 assert(sp
->s_len
== 0 && sp
->s_dat
== NULL
);
852 # ifndef HAVE_REALPATH_NULL
853 sp
= n_string_reserve(sp
, PATH_MAX
+1);
856 if((sp
->s_dat
= realpath(cp
, sp
->s_dat
)) != NULL
){
857 # ifdef HAVE_REALPATH_NULL
858 n_string_cp(sp
= n_string_assign_cp(sp
, cp
= sp
->s_dat
));
862 }else if(n_err_no
== n_ERR_NOENT
)
865 n_err(_("Can't canonicalize *folder*: %s\n"),
866 n_shexp_quote_cp(cp
, FAL0
));
870 sp
= n_string_drop_ownership(sp
);
871 #endif /* HAVE_REALPATH */
873 /* Always append a solidus to our result path upon success */
877 if(rv
[(i
= strlen(rv
)) - 1] != '/'){
878 sp
= n_string_reserve(sp
, i
+ 1 +1);
879 sp
= n_string_push_buf(sp
, rv
, i
);
880 sp
= n_string_push_c(sp
, '/');
881 rv
= n_string_cp(sp
);
882 sp
= n_string_drop_ownership(sp
);
887 n_PS_ROOT_BLOCK(ok_vset(folder_resolved
, rv
));
891 n_err(_("*folder* is not resolvable, using CWD\n"));
892 assert(rv
!= NULL
&& *rv
== '\0');
899 n_folder_mbox_prepare_append(FILE *fout
, struct stat
*st_or_null
){
900 /* TODO n_folder_mbox_prepare_append -> Mailbox->append() */
907 if(fseek(fout
, -2L, SEEK_END
)){
910 if(st_or_null
== NULL
){
912 if(fstat(fileno(fout
), st_or_null
))
916 if(st_or_null
->st_size
>= 2)
918 if(st_or_null
->st_size
== 0){
922 if(fseek(fout
, -1L, SEEK_END
))
929 switch(fread(buf
, sizeof *buf
, 2, fout
)){
944 if(fflush(fout
) || (needsep
&& putc('\n', fout
) == EOF
))