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 - 2016 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
);
53 __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
)
59 for (err
= ok
= i
= 0; cpl
> maxl
|| err
;) {
60 int ml
= mblen(cp
, cpl
);
61 if (ml
< 0) { /* XXX _narrow_suffix(): mblen() error; action? */
79 #endif /* HAVE_C90AMEND1 */
82 _update_mailname(char const *name
)
89 /* Don't realpath(3) if it's only an update request */
92 enum protocol p
= which_protocol(name
);
94 if (p
== PROTO_FILE
|| p
== PROTO_MAILDIR
) {
95 if (realpath(name
, mailname
) == NULL
&& errno
!= ENOENT
) {
96 n_err(_("Can't canonicalize %s\n"), n_shexp_quote_cp(name
, FAL0
));
103 n_strscpy(mailname
, name
, sizeof(mailname
));
109 /* Don't display an absolute path but "+FOLDER" if under *folder* */
113 if(*(folderp
= folder_query()) != '\0'){
115 if(!strncmp(folderp
, mailp
, i
)){
122 /* We want to see the name of the folder .. on the screen */
124 if (i
< sizeof(displayname
) -1)
125 memcpy(dispp
, mailp
, i
+1);
128 /* Avoid disrupting multibyte sequences (if possible) */
129 #ifndef HAVE_C90AMEND1
130 j
= sizeof(displayname
) / 3 - 1;
131 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
133 j
= field_detect_clip(sizeof(displayname
) / 3, mailp
, i
);
134 i
= j
+ __narrow_suffix(mailp
+ j
, i
- j
,
135 sizeof(displayname
) - (1/* + */ + 3 + 1) - j
);
137 snprintf(dispp
, sizeof(displayname
), "%.*s...%s",
138 (int)j
, mailp
, mailp
+ i
);
142 bool_t reset
= !(pstate
& PS_ROOT
);
145 ok_vset(_mailbox_resolved
, mailname
);
146 ok_vset(_mailbox_display
, displayname
);
155 setfile(char const *name
, enum fedit_mode fm
) /* TODO oh my god */
157 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
158 * necessary to make a mailbox accessible by a different user, and if that
159 * has happened, let's just let the usual file perms decide */
165 int rv
, omsgCount
= 0;
166 FILE *ibuf
= NULL
, *lckfp
= NULL
;
167 bool_t isdevnull
= FAL0
;
170 pstate
&= ~PS_SETFILE_OPENED
;
173 enum fexp_mode fexpm
;
175 if((who
= shortcut_expand(name
)) != NULL
){
176 fexpm
= FEXP_NSHORTCUT
| FEXP_NSHELL
;
182 fm
|= FEDIT_SYSBOX
; /* TODO fexpand() needs to tell is-valid-user! */
183 if(*(who
= &name
[1]) == ':')
190 if ((name
= fexpand(name
, fexpm
)) == NULL
)
194 /* For at least substdate() users */
195 time_current_update(&time_current
, FAL0
);
197 switch (which_protocol(name
)) {
199 if (temporary_protocol_ext
!= NULL
)
200 name
= savecat(name
, temporary_protocol_ext
);
201 isdevnull
= ((options
& OPT_BATCH_FLAG
) && !strcmp(name
, "/dev/null"));
203 do { /* TODO we need objects, goes away then */
204 # ifdef HAVE_REALPATH_NULL
207 if ((cp
= realpath(name
, NULL
)) != NULL
) {
214 if (realpath(name
, cbuf
) != NULL
)
215 name
= savestr(cbuf
);
223 rv
= maildir_setfile(name
, fm
);
228 rv
= pop3_setfile(name
, fm
);
232 n_err(_("Cannot handle protocol: %s\n"), name
);
236 if ((ibuf
= Zopen(name
, "r")) == NULL
) {
239 if ((fm
& FEDIT_SYSBOX
) && e
== ENOENT
) {
240 if (strcmp(who
, myname
) && getpwnam(who
) == NULL
) {
241 n_err(_("%s is not a user of this system\n"),
242 n_shexp_quote_cp(who
, FAL0
));
245 if (!(options
& OPT_QUICKRUN_MASK
) && ok_blook(bsdcompat
))
246 n_err(_("No mail for %s\n"), who
);
248 if (fm
& FEDIT_NEWMAIL
)
251 mb
.mb_type
= MB_VOID
;
252 if (ok_blook(emptystart
)) {
253 if (!(options
& OPT_QUICKRUN_MASK
) && !ok_blook(bsdcompat
))
255 /* We must avoid returning -1 and causing program exit */
263 if (fstat(fileno(ibuf
), &stb
) == -1) {
264 if (fm
& FEDIT_NEWMAIL
)
266 n_perr(_("fstat"), 0);
270 if (S_ISREG(stb
.st_mode
) || isdevnull
) {
273 if (fm
& FEDIT_NEWMAIL
)
275 errno
= S_ISDIR(stb
.st_mode
) ? EISDIR
: EINVAL
;
280 /* Looks like all will be well. We must now relinquish our hold on the
281 * current set of stuff. Must hold signals while we are reading the new
282 * file, else we will ruin the message[] data structure */
284 hold_sigs(); /* TODO note on this one in quit.c:quit() */
285 if (shudclob
&& !(fm
& FEDIT_NEWMAIL
) && !quit()) {
291 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
292 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
295 /* TODO There is no intermediate VOID box we've switched to: name may
296 * TODO point to the same box that we just have written, so any updates
297 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
298 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
299 /* TODO We may uselessly open twice but quit() doesn't say whether we were
300 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
301 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name
, mailname
)) {
305 if ((ibuf
= Zopen(name
, "r")) == NULL
||
306 fstat(fileno(ibuf
), &stb
) == -1 ||
307 (!S_ISREG(stb
.st_mode
) && !isdevnull
)) {
314 /* Copy the messages into /tmp and set pointers */
315 if (!(fm
& FEDIT_NEWMAIL
)) {
317 mb
.mb_type
= MB_VOID
;
320 mb
.mb_type
= MB_FILE
;
321 mb
.mb_perm
= (((options
& OPT_R_FLAG
) || (fm
& FEDIT_RDONLY
) ||
322 access(name
, W_OK
) < 0) ? 0 : MB_DELE
| MB_EDIT
);
335 if (fm
& FEDIT_SYSBOX
)
342 fseek(mb
.mb_otf
, 0L, SEEK_END
);
343 /* TODO Doing this without holding a lock is.. And not err checking.. */
344 fseek(ibuf
, mailsize
, SEEK_SET
);
346 omsgCount
= msgCount
;
351 else if (!(pstate
& PS_EDIT
))
352 lckfp
= n_dotlock(name
, fileno(ibuf
), FLT_READ
, offset
,0,
353 (fm
& FEDIT_NEWMAIL
? 0 : UIZ_MAX
));
354 else if (n_file_lock(fileno(ibuf
), FLT_READ
, offset
,0,
355 (fm
& FEDIT_NEWMAIL
? 0 : UIZ_MAX
)))
359 if (!(fm
& FEDIT_NEWMAIL
)) {
360 char const *emsg
= (pstate
& PS_EDIT
)
361 ? N_("Unable to lock mailbox, aborting operation")
362 : N_("Unable to (dot) lock mailbox, aborting operation");
366 if (!(fm
& FEDIT_NEWMAIL
))
371 mailsize
= fsize(ibuf
);
373 /* TODO This is too simple minded? We should regenerate an index file
374 * TODO to be able to truly tell whether *anything* has changed! */
375 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
379 setptr(ibuf
, offset
);
381 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
388 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1) {
394 if (!(fm
& FEDIT_NEWMAIL
)) {
395 pstate
&= ~PS_SAW_COMMAND
;
396 pstate
|= PS_SETFILE_OPENED
;
399 if ((options
& OPT_EXISTONLY
) && !(options
& OPT_HEADERLIST
)) {
400 rv
= (msgCount
== 0);
404 if ((!(pstate
& PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)) && msgCount
== 0) {
405 if (!(fm
& FEDIT_NEWMAIL
)) {
406 if (!ok_blook(emptystart
))
407 n_err(_("No mail for %s\n"), who
);
412 if (fm
& FEDIT_NEWMAIL
)
413 newmailinfo(omsgCount
);
419 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
425 mb
.mb_type
= MB_VOID
;
432 newmailinfo(int omsgCount
)
437 for (i
= 0; i
< omsgCount
; ++i
)
438 message
[i
].m_flag
&= ~MNEWEST
;
440 if (msgCount
> omsgCount
) {
441 for (i
= omsgCount
; i
< msgCount
; ++i
)
442 message
[i
].m_flag
|= MNEWEST
;
443 printf(_("New mail has arrived.\n"));
444 if ((i
= msgCount
- omsgCount
) == 1)
445 printf(_("Loaded 1 new message.\n"));
447 printf(_("Loaded %d new messages.\n"), i
);
449 printf(_("Loaded %d messages.\n"), msgCount
);
451 check_folder_hook(TRU1
);
455 if (ok_blook(header
))
456 print_headers(omsgCount
+ 1, msgCount
, FAL0
);
465 if (n_msgvec
!= NULL
)
467 n_msgvec
= scalloc(sz
+ 1, sizeof *n_msgvec
);
472 print_header_summary(char const *Larg
)
474 size_t bot
, top
, i
, j
;
478 /* Avoid any messages XXX add a make_mua_silent() and use it? */
479 if ((options
& (OPT_VERB
| OPT_EXISTONLY
)) == OPT_EXISTONLY
) {
480 freopen("/dev/null", "w", stdout
);
481 freopen("/dev/null", "w", stderr
);
483 assert(n_msgvec
!= NULL
);
484 i
= (getmsglist(/*TODO make const */n_UNCONST(Larg
), n_msgvec
, 0) <= 0);
485 if (options
& OPT_EXISTONLY
) {
486 exit_status
= (int)i
;
491 for (bot
= msgCount
, top
= 0, i
= 0; (j
= n_msgvec
[i
]) != 0; ++i
) {
498 bot
= 1, top
= msgCount
;
499 print_headers(bot
, top
, (Larg
!= NULL
)); /* TODO should take iterator!! */
505 announce(int printheaders
)
510 mdot
= newfileinfo();
513 dot
= message
+ mdot
- 1;
514 if (printheaders
&& msgCount
> 0 && ok_blook(header
)) {
515 print_header_group(vec
); /* XXX errors? */
524 int u
, n
, mdot
, d
, s
, hidden
, moved
;
527 if (mb
.mb_type
== MB_VOID
) {
533 s
= d
= hidden
= moved
=0;
534 for (mp
= message
, n
= 0, u
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
535 if (mp
->m_flag
& MNEW
)
537 if ((mp
->m_flag
& MREAD
) == 0)
539 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
541 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
543 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
545 if (mp
->m_flag
& MHIDDEN
)
549 /* If displayname gets truncated the user effectively has no option to see
550 * the full pathname of the mailbox, so print it at least for '? fi' */
551 printf(_("%s: "), n_shexp_quote_cp(
552 (_update_mailname(NULL
) ? displayname
: mailname
), FAL0
));
554 printf(_("1 message"));
556 printf(_("%d messages"), msgCount
);
558 printf(_(" %d new"), n
);
560 printf(_(" %d unread"), u
);
562 printf(_(" %d deleted"), d
);
564 printf(_(" %d saved"), s
);
566 printf(_(" %d moved"), moved
);
568 printf(_(" %d hidden"), hidden
);
569 else if (mb
.mb_perm
== 0)
570 printf(_(" [Read only]"));
583 enum mflag avoid
= MHIDDEN
| MDELETED
;
587 if (ok_blook(autothread
)) {
588 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
590 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
591 if (mb
.mb_sorted
!= NULL
)
593 mb
.mb_sorted
= sstrdup(cp
);
597 if (mb
.mb_type
== MB_VOID
) {
603 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
604 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
607 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
608 if (mb
.mb_threaded
) {
609 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
610 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
613 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
614 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
619 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
620 if (mb
.mb_threaded
) {
621 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
622 if (mp
->m_flag
& MFLAGGED
)
625 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
626 if (mp
->m_flag
& MFLAGGED
)
631 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
632 if (mb
.mb_threaded
) {
633 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
634 if (!(mp
->m_flag
& (MREAD
| avoid
)))
637 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
638 if (!(mp
->m_flag
& (MREAD
| avoid
)))
644 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
645 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
646 else if (ok_blook(showlast
)) {
647 if (mb
.mb_threaded
) {
648 for (mp
= this_in_thread(threadroot
, -1); mp
;
649 mp
= prev_in_thread(mp
))
650 if (!(mp
->m_flag
& avoid
))
652 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
654 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
655 if (!(mp
->m_flag
& avoid
))
657 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
660 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
661 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
662 else if (mb
.mb_threaded
) {
663 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
664 if (!(mp
->m_flag
& avoid
))
666 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
668 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
669 if (!(mp
->m_flag
& avoid
))
671 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
672 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
680 initbox(char const *name
)
685 if (mb
.mb_type
!= MB_VOID
)
686 n_strscpy(prevfile
, mailname
, PATH_MAX
);
688 /* TODO name always NE mailname (but goes away for objects anyway)
689 * TODO Well, not true no more except that in parens */
690 _update_mailname((name
!= mailname
) ? name
: NULL
);
692 if ((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpmbox", OF_WRONLY
| OF_HOLDSIGS
)) ==
694 n_perr(_("temporary mail message file"), 0);
697 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
698 n_perr(_("temporary mail message file"), 0);
701 Ftmp_release(&tempMesg
);
705 if (mb
.mb_sorted
!= NULL
) {
709 dot
= prevdot
= threadroot
= NULL
;
710 pstate
&= ~PS_DID_PRINT_DOT
;
716 struct n_string s
, *sp
= &s
;
722 sp
= n_string_creat_auto(sp
);
724 /* *folder* is linked with *_folder_resolved*: we only use the latter */
726 if((rv
= ok_vlook(_folder_resolved
)) != NULL
)
730 * If directory does not start with a <slash> ('/'), the contents
731 * of HOME shall be prefixed to it.
733 * If folder is unset or set to null, [.] filenames beginning with
734 * '+' shall refer to files in the current directory.
735 * We may have the result already */
739 if((cp
= ok_vlook(folder
)) == NULL
)
742 /* Expand the *folder*; skip %: prefix for simplicity of use */
743 if(cp
[0] == '%' && cp
[1] == ':')
745 if((err
= (cp
= fexpand(cp
, FEXP_NSPECIAL
| FEXP_NFOLDER
| FEXP_NSHELL
)
746 ) == NULL
) || *cp
== '\0')
749 switch(which_protocol(cp
)){
751 n_err(_("*folder* can't be set to a flat, readonly POP3 account\n"));
755 /* Further expansion desired */
759 /* Prefix HOME as necessary */
760 if(*cp
!= '/'){ /* XXX path_is_absolute() */
764 home
= ok_vlook(HOME
);
768 sp
= n_string_reserve(sp
, l1
+ 1 + l2
+1);
769 sp
= n_string_push_buf(sp
, home
, l1
);
770 sp
= n_string_push_c(sp
, '/');
771 sp
= n_string_push_buf(sp
, cp
, l2
);
772 cp
= n_string_cp(sp
);
773 sp
= n_string_drop_ownership(sp
);
778 /* TODO Since our visual mailname is resolved via realpath(3) if available
779 * TODO to avoid that we loose track of our currently open folder in case
780 * TODO we chdir away, but still checks the leading path portion against
781 * TODO folder_query() to be able to abbreviate to the +FOLDER syntax if
782 * TODO possible, we need to realpath(3) the folder, too */
784 assert(sp
->s_len
== 0 && sp
->s_dat
== NULL
);
785 # ifndef HAVE_REALPATH_NULL
786 sp
= n_string_reserve(sp
, PATH_MAX
+1);
789 if((sp
->s_dat
= realpath(cp
, sp
->s_dat
)) != NULL
){
790 # ifdef HAVE_REALPATH_NULL
791 n_string_cp(sp
= n_string_assign_cp(sp
, cp
= sp
->s_dat
));
795 }else if(errno
== ENOENT
)
798 n_err(_("Can't canonicalize *folder*: %s\n"),
799 n_shexp_quote_cp(cp
, FAL0
));
803 sp
= n_string_drop_ownership(sp
);
804 #endif /* HAVE_REALPATH */
806 /* Always append a solidus to our result path upon success */
810 if(rv
[(i
= strlen(rv
)) - 1] != '/'){
811 sp
= n_string_reserve(sp
, i
+ 1 +1);
812 sp
= n_string_push_buf(sp
, rv
, i
);
813 sp
= n_string_push_c(sp
, '/');
814 rv
= n_string_cp(sp
);
815 sp
= n_string_drop_ownership(sp
);
821 bool_t reset
= !(pstate
& PS_ROOT
);
824 ok_vset(_folder_resolved
, rv
);
831 n_err(_("*folder* is not resolvable, using CWD\n"));
832 assert(rv
!= NULL
&& *rv
== '\0');