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 - 2020 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
37 #define su_FILE folder
40 #ifndef mx_HAVE_AMALGAMATION
49 #include "mx/cmd-shortcut.h"
50 #include "mx/dig-msg.h"
51 #include "mx/file-locks.h"
52 #include "mx/file-streams.h"
53 #include "mx/net-pop3.h"
54 #include "mx/net-socket.h"
56 #include "mx/ui-str.h"
59 #include "su/code-in.h"
61 /* Update mailname (if name != NIL) and displayname, return whether displayname
62 * was large enough to swallow mailname */
63 static boole
_update_mailname(char const *name
);
64 #ifdef mx_HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
65 su_SINLINE uz
__narrow_suffix(char const *cp
, uz cpl
, uz maxl
);
69 static void a_folder_info(void);
71 /* Set up the input pointers while copying the mail file into /tmp */
72 static void a_folder_mbox_setptr(FILE *ibuf
, off_t offset
, boole iseml
);
74 #ifdef mx_HAVE_C90AMEND1
76 __narrow_suffix(char const *cp
, uz cpl
, uz maxl
)
82 for (err
= ok
= i
= 0; cpl
> maxl
|| err
;) {
83 int ml
= mblen(cp
, cpl
);
84 if (ml
< 0) { /* XXX _narrow_suffix(): mblen() error; action? */
102 #endif /* mx_HAVE_C90AMEND1 */
105 _update_mailname(char const *name
) /* TODO 2MUCH work, cache, prop of Obj! */
113 /* Do not realpath(3) if it's only an update request */
115 #ifdef mx_HAVE_REALPATH
119 p
= which_protocol(name
, TRU1
, TRU1
, &adjname
);
121 if(p
== n_PROTO_FILE
|| p
== n_PROTO_MAILDIR
|| p
== n_PROTO_EML
){
123 if(realpath(name
, mailname
) == NIL
&& su_err_no() != su_ERR_NOENT
){
124 n_err(_("Cannot canonicalize %s\n"), n_shexp_quote_cp(name
, FAL0
));
130 su_cs_pcopy_n(mailname
, name
, sizeof(mailname
));
136 /* Don't display an absolute path but "+FOLDER" if under *folder* */
137 if(*(foldp
= n_folder_query()) != '\0'){
138 foldlen
= su_cs_len(foldp
);
139 if(strncmp(foldp
, mailp
, foldlen
))
144 /* We want to see the name of the folder .. on the screen */
145 i
= su_cs_len(mailp
);
146 if(i
< sizeof(displayname
) - 3 -1){
150 su_mem_copy(dispp
, mailp
, foldlen
);
154 su_mem_copy(dispp
, mailp
, i
-= foldlen
);
157 su_mem_copy(dispp
, mailp
, i
+1);
160 /* Avoid disrupting multibyte sequences (if possible) */
161 #ifndef mx_HAVE_C90AMEND1
162 j
= sizeof(displayname
) / 3 - 3;
163 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
165 j
= field_detect_clip(sizeof(displayname
) / 3, mailp
, i
);
166 i
= j
+ __narrow_suffix(mailp
+ j
, i
- j
,
167 sizeof(displayname
) - (1/* + */ + 3 + 1) - j
);
169 snprintf(dispp
, sizeof(displayname
), "%s%.*s...%s",
170 (foldlen
> 0 ? "[+]" : ""), (int)j
, mailp
, mailp
+ i
);
174 n_PS_ROOT_BLOCK((ok_vset(mailbox_resolved
, mailname
),
175 ok_vset(mailbox_display
, displayname
)));
183 int u
, n
, d
, s
, hidden
, moved
;
186 if(mb
.mb_type
== MB_VOID
){
187 fprintf(n_stdout
, _("(Currently no active mailbox)"));
191 s
= d
= hidden
= moved
= 0;
192 for (mp
= message
, n
= 0, u
= 0; PCMP(mp
, <, message
+ msgCount
); ++mp
) {
193 if (mp
->m_flag
& MNEW
)
195 if ((mp
->m_flag
& MREAD
) == 0)
197 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
199 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
201 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
203 if (mp
->m_flag
& MHIDDEN
)
207 /* If displayname gets truncated the user effectively has no option to see
208 * the full pathname of the mailbox, so print it at least for '? fi' */
209 fprintf(n_stdout
, "%s: ", n_shexp_quote_cp(
210 (_update_mailname(NULL
) ? displayname
: mailname
), FAL0
));
212 fprintf(n_stdout
, _("1 message"));
214 fprintf(n_stdout
, _("%d messages"), msgCount
);
216 fprintf(n_stdout
, _(" %d new"), n
);
218 fprintf(n_stdout
, _(" %d unread"), u
);
220 fprintf(n_stdout
, _(" %d deleted"), d
);
222 fprintf(n_stdout
, _(" %d saved"), s
);
224 fprintf(n_stdout
, _(" %d moved"), moved
);
226 fprintf(n_stdout
, _(" %d hidden"), hidden
);
228 fprintf(n_stdout
, _(" [Read-only]"));
230 if (mb
.mb_type
== MB_CACHE
)
231 fprintf(n_stdout
, _(" [Disconnected]"));
235 putc('\n', n_stdout
);
240 a_folder_mbox_setptr(FILE *ibuf
, off_t offset
, boole iseml
){
243 a_HAD_BAD_FROM_
= 1u<<1,
252 struct message self
, commit
;
256 uz filesize
, linesize
, cnt
;
259 filesize
= mailsize
- offset
;
261 su_mem_set(&self
, 0, sizeof self
);
262 self
.m_flag
= MUSED
| MNEW
| MNEWEST
;
264 offset
= ftell(mb
.mb_otf
);
265 f
= a_MAYBE
| (iseml
? a_ISEML
: (ok_blook(mbox_rfc4155
) ? a_RFC4155
: 0));
267 mx_fs_linepool_aquire(&linebuf
, &linesize
);
269 /* Ensure space for terminating LF, so do append it */
270 if(UNLIKELY(fgetline(&linebuf
, &linesize
, &filesize
, &cnt
, ibuf
, TRU1
272 /* TODO We are not prepared for error here */
275 commit
.m_size
+= self
.m_size
;
276 commit
.m_lines
+= self
.m_lines
;
279 commit
.m_xsize
= commit
.m_size
;
280 commit
.m_xlines
= commit
.m_lines
;
281 commit
.m_content_info
= CI_HAVE_HEADER
| CI_HAVE_BODY
;
282 message_append(&commit
);
284 message_append_null();
286 if(f
& a_HAD_BAD_FROM_
){
287 /*if(!(mb.mb_active & MB_BAD_FROM_))*/{
288 mb
.mb_active
|= MB_BAD_FROM_
;
289 /* TODO mbox-rfc4155 does NOT fix From_ line! */
290 n_err(_("MBOX contains non-conforming From_ line(s)!\n"
291 " Message boundaries may have been misdetected!\n"
292 " Setting *mbox-rfc4155* and reopening _may_ "
293 "improve the result.\n"
294 " Recreating the mailbox will perform MBOXO quoting: "
295 "\"copy * SOME-FILE\".\n"
296 " (Then unset *mbox-rfc4155* again.)\n"));
300 mx_fs_linepool_release(linebuf
, linesize
);
304 /* Normalize away line endings, we will place (readded) \n */
305 if(cnt
>= 2 && linebuf
[cnt
- 2] == '\r')
306 linebuf
[--cnt
] = '\0';
307 linebuf
[--cnt
] = '\0';
308 /* We cannot use this ASSERTion since it will trigger for example when
309 * the Linux kernel crashes and the log files (which may contain NULs)
310 * are sent out via email! (It has been active for quite some time..) */
311 /*ASSERT(linebuf[0] != '\0' || cnt == 0);*/
313 /* TODO In v15 this should use a/the flat MIME parser in order to ignore
314 * TODO "From " when MIME boundaries are active -- whereas this opens
315 * TODO another can of worms, it very likely is better than messing up
316 * TODO MIME because of a "From " line!.
317 * TODO That is: Mailbox superclass, MBOX:Mailbox, virtual load() which
318 * TODO creates collection of MessageHull objects which are able to load
319 * TODO their content, and normalize content, correct structural errs */
320 if(UNLIKELY(cnt
== 0)){
323 if(LIKELY(!(f
& a_CREATE
)))
326 commit
.m_size
+= self
.m_size
;
327 commit
.m_lines
+= self
.m_lines
;
329 f
&= ~(a_CREATE
| a_INHEAD
| a_COMMIT
);
334 if(UNLIKELY((f
& a_MAYBE
) && ((from_
= ((f
& a_ISEML
) != 0)) ||
335 ((linebuf
[0] == 'F') && (from_
= is_head(linebuf
, cnt
, TRU1
)) &&
336 (from_
== TRU1
|| !(f
& a_RFC4155
)))))){
337 /* TODO char date[n_FROM_DATEBUF];
338 * TODO extract_date_from_from_(linebuf, cnt, date);
339 * TODO self.m_time = 10000; */
340 self
.m_xsize
= self
.m_size
;
341 self
.m_xlines
= self
.m_lines
;
342 self
.m_content_info
= CI_HAVE_HEADER
| CI_HAVE_BODY
;
344 f
|= a_CREATE
| a_INHEAD
;
348 f
|= a_HAD_BAD_FROM_
;
349 /* TODO MBADFROM_ causes the From_ line to be replaced entirely
350 * TODO when the message is newly written via e.g. `copy'.
351 * TODO Instead this From_ should be an object which can fix
352 * TODO the parts which are missing or are faulty, such that
353 * TODO good info can be kept; this is possible after the main
354 * TODO message header has been fully parsed. For now we are stuck
355 * TODO and fail for example in a_header_extract_date_from_from_()
356 * TODO (which should not exist as such btw) */
357 self
.m_flag
= MUSED
| MNEW
| MNEWEST
| MBADFROM_
;
359 self
.m_flag
= MUSED
| MNEW
| MNEWEST
| (f
& a_ISEML
? MNOFROM
: 0);
362 self
.m_block
= mailx_blockof(offset
);
363 self
.m_offset
= mailx_offsetof(offset
);
368 if(LIKELY(!(f
& a_INHEAD
)))
371 if(LIKELY((cp
= su_mem_find(linebuf
, ':', cnt
)) != NULL
)){
377 for(cps
= linebuf
; su_cs_is_blank(*cps
); ++cps
)
379 for(cpe
= cp
; cpe
> cps
&& (--cpe
, su_cs_is_blank(*cpe
));)
381 switch(P2UZ(cpe
- cps
)){
383 if(!su_cs_cmp_case_n(cps
, "status", 5))
386 if((c
= *cp
) == '\0')
389 self
.m_flag
|= MREAD
;
391 self
.m_flag
&= ~MNEW
;
395 if(!su_cs_cmp_case_n(cps
, "x-status", 7))
398 if((c
= *cp
) == '\0')
401 self
.m_flag
|= MFLAGGED
;
403 self
.m_flag
|= MANSWERED
;
405 self
.m_flag
|= MDRAFTED
;
409 }else if(!su_cs_is_blank(linebuf
[0])){
410 /* So either this is a false detection (nothing but From_ line
411 * yet), or no separating empty line in between header/body!
412 * In the latter case, add one! */
414 if(putc('\n', mb
.mb_otf
) == EOF
){
415 n_perr(_("/tmp"), 0);
416 exit(n_EXIT_ERR
); /* TODO no! */
422 commit
.m_size
+= self
.m_size
;
423 commit
.m_lines
+= self
.m_lines
;
425 f
&= ~(a_CREATE
| a_INHEAD
| a_COMMIT
);
431 f
&= ~(a_CREATE
| a_COMMIT
);
433 message_append(&commit
);
437 linebuf
[cnt
++] = '\n';
438 ASSERT(linebuf
[cnt
] == '\0');
439 fwrite(linebuf
, sizeof *linebuf
, cnt
, mb
.mb_otf
);
441 if(ferror(mb
.mb_otf
)){
442 n_perr(_("/tmp"), 0);
443 exit(n_EXIT_ERR
); /* TODO no! */
453 setfile(char const *name
, enum fedit_mode fm
) /* TODO oh my god */
455 /* TODO This all needs to be converted to URL:: and Mailbox:: */
460 char const *who
, *orig_name
;
462 int rv
, omsgCount
= 0;
463 FILE *ibuf
= NULL
, *lckfp
= NULL
;
464 boole isdevnull
= FAL0
;
467 n_pstate
&= ~n_PS_SETFILE_OPENED
;
470 enum fexp_mode fexpm
;
472 if((who
= mx_shortcut_expand(name
)) != NIL
){
476 fexpm
= FEXP_SHORTCUT
| FEXP_NSHELL
;
481 fm
|= FEDIT_SYSBOX
; /* TODO fexpand() needs to tell is-valid-user! */
482 if(*(who
= &name
[1]) == ':')
484 if((cp
= su_cs_rfind_c(who
, '/')) != NULL
)
490 if(fm
& FEDIT_ACCOUNT
){
491 if((who
= ok_vlook(account
)) == NULL
)
493 who
= savecatsep(_("account"), ' ', who
);
495 who
= ok_vlook(LOGNAME
);
498 if(!(fm
& FEDIT_SYSBOX
)){
501 if((((cp
= ok_vlook(inbox
)) != NIL
&& *cp
!= '\0') ||
502 (cp
= ok_vlook(MAIL
)) != NIL
) &&
503 !su_cs_cmp(cp
, name
))
507 if((name
= fexpand(name
, fexpm
)) == NIL
)
511 /* For at least substdate() users TODO -> eventloop tick */
512 time_current_update(&time_current
, FAL0
);
514 switch((proto
= which_protocol(orig_name
= name
, TRU1
, TRU1
, &name
))){
516 if(fm
& ~FEDIT_RDONLY
){
517 n_err(_("Sorry, for now eml:// files cannot be used like this: %s\n"),
524 isdevnull
= ((n_poption
& n_PO_BATCH_FLAG
) &&
525 !su_cs_cmp(name
, n_path_devnull
));
526 #ifdef mx_HAVE_REALPATH
527 do { /* TODO we need objects, goes away then */
528 # ifdef mx_HAVE_REALPATH_NULL
531 if ((cp
= realpath(name
, NULL
)) != NULL
) {
538 if (realpath(name
, cbuf
) != NULL
)
539 name
= savestr(cbuf
);
545 #ifdef mx_HAVE_MAILDIR
548 rv
= maildir_setfile(who
, name
, fm
);
554 rv
= mx_pop3_setfile(who
, orig_name
, fm
);
560 if((fm
& FEDIT_NEWMAIL
) && mb
.mb_type
== MB_CACHE
)
563 rv
= imap_setfile(who
, orig_name
, fm
);
567 n_err(_("Cannot handle protocol: %s\n"), orig_name
);
571 if((ibuf
= mx_fs_open_any(savecat("file://", name
), "r", NIL
)) == NIL
){
574 if ((fm
& FEDIT_SYSBOX
) && e
== su_ERR_NOENT
) {
575 if (!(fm
& FEDIT_ACCOUNT
) && su_cs_cmp(who
, ok_vlook(LOGNAME
)) &&
576 getpwnam(who
) == NULL
) {
577 n_err(_("%s is not a user of this system\n"),
578 n_shexp_quote_cp(who
, FAL0
));
581 if (!(n_poption
& n_PO_QUICKRUN_MASK
) && ok_blook(bsdcompat
))
582 n_err(_("No mail for %s at %s\n"),
583 who
, n_shexp_quote_cp(name
, FAL0
));
585 if (fm
& FEDIT_NEWMAIL
)
588 if(mb
.mb_digmsg
!= NIL
)
589 mx_dig_msg_on_mailbox_close(&mb
);
590 mb
.mb_type
= MB_VOID
;
592 if (ok_blook(emptystart
)) {
593 if (!(n_poption
& n_PO_QUICKRUN_MASK
) && !ok_blook(bsdcompat
))
595 /* We must avoid returning -1 and causing program exit */
603 if (fstat(fileno(ibuf
), &stb
) == -1) {
604 if (fm
& FEDIT_NEWMAIL
)
606 n_perr(_("fstat"), 0);
610 if (S_ISREG(stb
.st_mode
) || isdevnull
) {
613 if (fm
& FEDIT_NEWMAIL
)
615 su_err_set_no(S_ISDIR(stb
.st_mode
) ? su_ERR_ISDIR
: su_ERR_INVAL
);
620 if (shudclob
&& !(fm
& FEDIT_NEWMAIL
) && !quit(FAL0
))
626 if(!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
!= NIL
){
627 if(mb
.mb_sock
->s_fd
>= 0)
628 mx_socket_close(mb
.mb_sock
); /* TODO VMAILFS->close() on open thing */
634 /* TODO There is no intermediate VOID box we've switched to: name may
635 * TODO point to the same box that we just have written, so any updates
636 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
637 /* TODO In addition: in case of compressed/hook boxes we lock a tmp file! */
638 /* TODO We may uselessly open twice but quit() doesn't say whether we were
639 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
640 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !su_cs_cmp(name
, mailname
)) {
644 if((ibuf
= mx_fs_open_any(name
, "r", NIL
)) == NIL
||
645 fstat(fileno(ibuf
), &stb
) == -1 ||
646 (!S_ISREG(stb
.st_mode
) && !isdevnull
)) {
653 /* Copy the messages into /tmp and set pointers */
654 if (!(fm
& FEDIT_NEWMAIL
)) {
656 mb
.mb_type
= MB_VOID
;
659 mb
.mb_type
= MB_FILE
;
660 mb
.mb_perm
= (((n_poption
& n_PO_R_FLAG
) || (fm
& FEDIT_RDONLY
) ||
661 access(name
, W_OK
) < 0) ? 0 : MB_DELE
| MB_EDIT
);
674 if (fm
& FEDIT_SYSBOX
)
675 n_pstate
&= ~n_PS_EDIT
;
677 n_pstate
|= n_PS_EDIT
;
681 fseek(mb
.mb_otf
, 0L, SEEK_END
);
682 /* TODO Doing this without holding a lock is.. And not err checking.. */
683 fseek(ibuf
, mailsize
, SEEK_SET
);
685 omsgCount
= msgCount
;
690 else if(!(n_pstate
& n_PS_EDIT
))
691 lckfp
= mx_file_dotlock(name
, fileno(ibuf
), mx_FILE_LOCK_TYPE_READ
,
692 offset
,0, (fm
& FEDIT_NEWMAIL
? 0 : UZ_MAX
));
693 else if(mx_file_lock(fileno(ibuf
), mx_FILE_LOCK_TYPE_READ
, offset
,0,
694 (fm
& FEDIT_NEWMAIL
? 0 : UZ_MAX
)))
698 if (!(fm
& FEDIT_NEWMAIL
)) {
699 #ifdef mx_HAVE_UISTRINGS
700 char const * const emsg
= (n_pstate
& n_PS_EDIT
)
701 ? N_("Unable to lock mailbox, aborting operation")
702 : N_("Unable to (dot) lock mailbox, aborting operation");
707 if (!(fm
& FEDIT_NEWMAIL
))
712 mailsize
= fsize(ibuf
);
714 /* TODO This is too simple minded? We should regenerate an index file
715 * TODO to be able to truly tell whether *anything* has changed! */
716 if ((fm
& FEDIT_NEWMAIL
) && UCMP(z
, mailsize
, <=, offset
)) {
720 a_folder_mbox_setptr(ibuf
, offset
, (proto
== n_PROTO_EML
));
722 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
729 if(lckfp
!= NIL
&& lckfp
!= R(FILE*,-1)){
730 mx_fs_pipe_close(lckfp
, FAL0
);
734 if (!(fm
& FEDIT_NEWMAIL
)) {
735 n_pstate
&= ~n_PS_SAW_COMMAND
;
736 n_pstate
|= n_PS_SETFILE_OPENED
;
741 rv
= (msgCount
== 0);
743 su_err_set_no(su_ERR_NODATA
);
745 if(n_poption
& n_PO_EXISTONLY
)
749 if(!(n_pstate
& n_PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)){
750 if(!(fm
& FEDIT_NEWMAIL
)){
751 if (!ok_blook(emptystart
))
752 n_err(_("No mail for %s at %s\n"),
753 who
, n_shexp_quote_cp(name
, FAL0
));
759 if(fm
& FEDIT_NEWMAIL
)
760 newmailinfo(omsgCount
);
764 if(lckfp
!= NIL
&& lckfp
!= R(FILE*,-1))
765 mx_fs_pipe_close(lckfp
, FAL0
);
772 if(mb
.mb_digmsg
!= NIL
)
773 mx_dig_msg_on_mailbox_close(&mb
);
774 mb
.mb_type
= MB_VOID
;
776 su_err_set_no(su_ERR_NOTOBACCO
);
782 newmailinfo(int omsgCount
)
787 for (i
= 0; i
< omsgCount
; ++i
)
788 message
[i
].m_flag
&= ~MNEWEST
;
790 if (msgCount
> omsgCount
) {
791 for (i
= omsgCount
; i
< msgCount
; ++i
)
792 message
[i
].m_flag
|= MNEWEST
;
793 fprintf(n_stdout
, _("New mail has arrived.\n"));
794 if ((i
= msgCount
- omsgCount
) == 1)
795 fprintf(n_stdout
, _("Loaded 1 new message.\n"));
797 fprintf(n_stdout
, _("Loaded %d new messages.\n"), i
);
799 fprintf(n_stdout
, _("Loaded %d messages.\n"), msgCount
);
801 temporary_folder_hook_check(TRU1
);
805 if(ok_blook(header
) && (i
= omsgCount
+ 1) <= msgCount
){
807 if(mb
.mb_type
== MB_IMAP
)
808 imap_getheaders(i
, msgCount
); /* TODO not here */
810 for(omsgCount
= 0; i
<= msgCount
; ++omsgCount
, ++i
)
811 n_msgvec
[omsgCount
] = i
;
812 n_msgvec
[omsgCount
] = 0;
813 print_headers(n_msgvec
, FAL0
, FAL0
);
825 n_msgvec
= n_calloc(size
+1, sizeof *n_msgvec
);
830 print_header_summary(char const *Larg
)
837 if(mb
.mb_type
== MB_IMAP
)
838 imap_getheaders(0, msgCount
); /* TODO not here */
840 ASSERT(n_msgvec
!= NULL
);
843 /* Avoid any messages XXX add a make_mua_silent() and use it? */
844 if ((n_poption
& (n_PO_V
| n_PO_EXISTONLY
)) == n_PO_EXISTONLY
) {
845 n_stdout
= freopen(n_path_devnull
, "w", stdout
);
846 n_stderr
= freopen(n_path_devnull
, "w", stderr
);
848 i
= (n_getmsglist(n_shexp_quote_cp(Larg
, FAL0
), n_msgvec
, 0, NULL
) <= 0);
849 if (n_poption
& n_PO_EXISTONLY
)
850 n_exit_status
= (int)i
;
852 print_headers(n_msgvec
, TRU1
, FAL0
); /* TODO should be iterator! */
856 for(; UCMP(z
, i
, <, msgCount
); ++i
)
861 for(mp
= threadroot
; mp
; ++i
, mp
= next_in_thread(mp
))
862 n_msgvec
[i
] = (int)P2UZ(mp
- message
+ 1);
864 print_headers(n_msgvec
, FAL0
, TRU1
); /* TODO should be iterator! */
870 n_folder_announce(enum n_announce_flags af
){
874 mdot
= (mb
.mb_type
== MB_VOID
) ? 1 : getmdot(0);
875 dot
= &message
[mdot
- 1];
877 if(af
!= n_ANNOUNCE_NONE
&& ok_blook(header
) &&
878 ((af
& n_ANNOUNCE_MAIN_CALL
) ||
879 ((af
& n_ANNOUNCE_CHANGE
) && !ok_blook(posix
))))
880 af
|= n_ANNOUNCE_STATUS
| n__ANNOUNCE_HEADER
;
882 if(af
& n_ANNOUNCE_STATUS
){
884 af
|= n__ANNOUNCE_ANY
;
887 if(af
& n__ANNOUNCE_HEADER
){
888 if(!(af
& n_ANNOUNCE_MAIN_CALL
) && ok_blook(bsdannounce
))
889 n_OBSOLETE(_("*bsdannounce* is now default behaviour"));
892 print_header_group(vec
); /* XXX errors? */
893 af
|= n__ANNOUNCE_ANY
;
896 if(af
& n__ANNOUNCE_ANY
)
907 enum mflag avoid
= MHIDDEN
| MDELETED
;
911 if (ok_blook(autothread
)) {
912 n_OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
914 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
915 if (mb
.mb_sorted
!= NULL
)
916 n_free(mb
.mb_sorted
);
917 mb
.mb_sorted
= su_cs_dup(cp
, 0);
921 if (mb
.mb_type
== MB_VOID
) {
927 for (mp
= message
; PCMP(mp
, <, message
+ msgCount
); ++mp
)
928 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
931 if (!nmail
|| PCMP(mp
, >=, message
+ msgCount
)) {
932 if (mb
.mb_threaded
) {
933 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
934 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
937 for (mp
= message
; PCMP(mp
, <, message
+ msgCount
); ++mp
)
938 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
943 if ((mb
.mb_threaded
? (mp
== NULL
) : PCMP(mp
, >=, message
+ msgCount
))) {
944 if (mb
.mb_threaded
) {
945 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
946 if (mp
->m_flag
& MFLAGGED
)
949 for (mp
= message
; PCMP(mp
, <, message
+ msgCount
); ++mp
)
950 if (mp
->m_flag
& MFLAGGED
)
955 if ((mb
.mb_threaded
? (mp
== NULL
) : PCMP(mp
, >=, message
+ msgCount
))) {
956 if (mb
.mb_threaded
) {
957 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
958 if (!(mp
->m_flag
& (MREAD
| avoid
)))
961 for (mp
= message
; PCMP(mp
, <, message
+ msgCount
); ++mp
)
962 if (!(mp
->m_flag
& (MREAD
| avoid
)))
968 (mb
.mb_threaded
? (mp
!= NULL
) : PCMP(mp
, <, message
+ msgCount
)))
969 mdot
= (int)P2UZ(mp
- message
+ 1);
970 else if (ok_blook(showlast
)) {
971 if (mb
.mb_threaded
) {
972 for (mp
= this_in_thread(threadroot
, -1); mp
;
973 mp
= prev_in_thread(mp
))
974 if (!(mp
->m_flag
& avoid
))
976 mdot
= (mp
!= NULL
) ? (int)P2UZ(mp
- message
+ 1) : msgCount
;
978 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
979 if (!(mp
->m_flag
& avoid
))
981 mdot
= (mp
>= message
) ? (int)P2UZ(mp
- message
+ 1) : msgCount
;
984 (mb
.mb_threaded
? (mp
!= NULL
) : PCMP(mp
, <, message
+ msgCount
)))
985 mdot
= (int)P2UZ(mp
- message
+ 1);
986 else if (mb
.mb_threaded
) {
987 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
988 if (!(mp
->m_flag
& avoid
))
990 mdot
= (mp
!= NULL
) ? (int)P2UZ(mp
- message
+ 1) : 1;
992 for (mp
= message
; PCMP(mp
, <, message
+ msgCount
); ++mp
)
993 if (!(mp
->m_flag
& avoid
))
995 mdot
= PCMP(mp
, <, message
+ msgCount
)
996 ? (int)P2UZ(mp
- message
+ 1) : 1;
1004 initbox(char const *name
)
1006 struct mx_fs_tmp_ctx
*fstcp
;
1010 if (mb
.mb_type
!= MB_VOID
)
1011 su_cs_pcopy_n(prevfile
, mailname
, PATH_MAX
);
1013 /* TODO name always NE mailname (but goes away for objects anyway)
1014 * TODO Well, not true no more except that in parens */
1015 _update_mailname((name
!= mailname
) ? name
: NULL
);
1018 if((mb
.mb_otf
= mx_fs_tmp_open("tmpmbox", (mx_FS_O_WRONLY
|
1019 mx_FS_O_HOLDSIGS
), &fstcp
)) == NIL
){
1020 n_perr(_("initbox: temporary mail message file, writer"), 0);
1022 }else if((mb
.mb_itf
= mx_fs_open(fstcp
->fstc_filename
, "&r")
1024 n_perr(_("initbox: temporary mail message file, reader"), 0);
1027 mx_fs_tmp_release(fstcp
);
1032 mb
.mb_active
= MB_NONE
;
1035 mb
.mb_flags
= MB_NOFLAGS
;
1037 if (mb
.mb_sorted
!= NULL
) {
1038 n_free(mb
.mb_sorted
);
1039 mb
.mb_sorted
= NULL
;
1041 dot
= prevdot
= threadroot
= NULL
;
1042 n_pstate
&= ~n_PS_DID_PRINT_DOT
;
1047 n_folder_query(void){
1048 struct n_string s_b
, *s
;
1049 enum protocol proto
;
1051 char const *rv
, *adjcp
;
1055 s
= n_string_creat_auto(&s_b
);
1057 /* *folder* is linked with *folder_resolved*: we only use the latter */
1059 if((rv
= ok_vlook(folder_resolved
)) != NIL
)
1063 * If directory does not start with a <slash> ('/'), the contents
1064 * of HOME shall be prefixed to it.
1066 * If folder is unset or set to null, [.] filenames beginning with
1067 * '+' shall refer to files in the current directory.
1068 * We may have the result already.
1069 * P.S.: that "or set to null" seems to be a POSIX bug, V10 mail and BSD
1070 * Mail since 1982 work differently, follow suit */
1074 if((cp
= ok_vlook(folder
)) == NIL
)
1077 /* Expand the *folder*; skip %: prefix for simplicity of use */
1078 if(cp
[0] == '%' && cp
[1] == ':')
1080 if((err
= (cp
= fexpand(cp
, FEXP_NSPECIAL
| FEXP_NFOLDER
| FEXP_NSHELL
)
1081 ) == NIL
) /*|| *cp == '\0'*/)
1086 for(i
= su_cs_len(cp
);;){
1096 switch((proto
= which_protocol(cp
, FAL0
, FAL0
, &adjcp
))){
1098 n_err(_("*folder*: cannot use the POP3 protocol\n"));
1104 if(!su_cs_cmp(rv
, protbase(rv
)))
1105 rv
= savecatsep(rv
, '/', n_empty
);
1107 n_err(_("*folder*: IMAP support not compiled in\n"));
1112 /* Further expansion desired */
1116 /* Prefix HOME as necessary */
1117 if(*adjcp
!= '/'){ /* XXX path_is_absolute() */
1121 home
= ok_vlook(HOME
);
1122 l1
= su_cs_len(home
);
1123 ASSERT(l1
> 0); /* (checked VIP variable) */
1126 s
= n_string_reserve(s
, l1
+ 1 + l2
+1);
1131 s
= n_string_push_buf(s
, cp
, i
= P2UZ(adjcp
- cp
));
1136 s
= n_string_push_buf(s
, home
, l1
);
1138 s
= n_string_push_c(s
, '/');
1139 s
= n_string_push_buf(s
, cp
, l2
);
1141 cp
= n_string_cp(s
);
1142 s
= n_string_drop_ownership(s
);
1145 /* TODO Since visual mailname is resolved via realpath(3) if available
1146 * TODO to avoid that we loose track of our currently open folder in case
1147 * TODO we chdir away, but still checks the leading path portion against
1148 * TODO folder_query() to be able to abbreviate to the +FOLDER syntax if
1149 * TODO possible, we need to realpath(3) the folder, too */
1150 #ifndef mx_HAVE_REALPATH
1153 ASSERT(s
->s_len
== 0 && s
->s_dat
== NIL
);
1154 # ifndef mx_HAVE_REALPATH_NULL
1155 s
= n_string_reserve(s
, PATH_MAX
+1);
1158 if((s
->s_dat
= realpath(cp
, s
->s_dat
)) != NIL
){
1159 # ifdef mx_HAVE_REALPATH_NULL
1160 n_string_cp(s
= n_string_assign_cp(s
, cp
= s
->s_dat
));
1164 }else if(su_err_no() == su_ERR_NOENT
)
1167 n_err(_("Cannot canonicalize *folder*: %s\n"),
1168 n_shexp_quote_cp(cp
, FAL0
));
1172 s
= n_string_drop_ownership(s
);
1173 #endif /* mx_HAVE_REALPATH */
1175 /* Always append a solidus to our result path upon success */
1179 if(rv
[(i
= su_cs_len(rv
)) - 1] != '/'){
1180 s
= n_string_reserve(s
, i
+ 1 +1);
1181 s
= n_string_push_buf(s
, rv
, i
);
1182 s
= n_string_push_c(s
, '/');
1183 rv
= n_string_cp(s
);
1184 s
= n_string_drop_ownership(s
);
1189 n_PS_ROOT_BLOCK(ok_vset(folder_resolved
, rv
));
1193 n_err(_("*folder* is not resolvable, using CWD\n"));
1194 ASSERT(rv
!= NIL
&& *rv
== '\0');
1202 n_folder_mbox_prepare_append(FILE *fout
, struct stat
*st_or_nil
){
1203 /* TODO n_folder_mbox_prepare_append -> Mailbox->append() */
1210 if(fseek(fout
, -2L, SEEK_END
) == 0 && fread(buf
, sizeof *buf
, 2, fout
) == 2)
1211 needsep
= (buf
[0] != '\n' || buf
[1] != '\n');
1214 if(st_or_nil
== NIL
){
1216 if(fstat(fileno(fout
), st_or_nil
))
1220 if(st_or_nil
->st_size
>= 2)
1222 if(st_or_nil
->st_size
== 0){
1228 if(fseek(fout
, -1L, SEEK_END
))
1230 if(fread(buf
, sizeof *buf
, 1, fout
) != 1)
1232 needsep
= (buf
[0] != '\n');
1236 if((needsep
&& (fseek(fout
, 0L, SEEK_END
) || putc('\n', fout
) == EOF
)) ||
1237 fflush(fout
) == EOF
)
1246 #include "su/code-ou.h"