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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 wether 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"), name
);
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
)){
116 if(folderp
[i
-1] != '/' && mailp
[i
] == '/') /* XXX DIRSEP magic */
124 /* We want to see the name of the folder .. on the screen */
126 if (i
< sizeof(displayname
) -1)
127 memcpy(dispp
, mailp
, i
+1);
130 /* Avoid disrupting multibyte sequences (if possible) */
131 #ifndef HAVE_C90AMEND1
132 j
= sizeof(displayname
) / 3 - 1;
133 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
135 j
= field_detect_clip(sizeof(displayname
) / 3, mailp
, i
);
136 i
= j
+ __narrow_suffix(mailp
+ j
, i
- j
,
137 sizeof(displayname
) - (1/* + */ + 3 + 1) - j
);
139 snprintf(dispp
, sizeof(displayname
), "%.*s...%s",
140 (int)j
, mailp
, mailp
+ i
);
147 setfile(char const *name
, enum fedit_mode fm
) /* TODO oh my god */
149 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
150 * necessary to make a mailbox accessible by a different user, and if that
151 * has happened, let's just let the usual file perms decide */
157 int rv
, omsgCount
= 0;
158 FILE *ibuf
= NULL
, *lckfp
= NULL
;
159 bool_t isdevnull
= FAL0
;
162 pstate
&= ~PS_SETFILE_OPENED
;
165 enum fexp_mode fexpm
;
167 if((who
= shortcut_expand(name
)) != NULL
){
168 fexpm
= FEXP_NSHORTCUT
| FEXP_NSHELL
;
174 fm
|= FEDIT_SYSBOX
; /* TODO fexpand() needs to tell is-valid-user! */
175 if(*(who
= &name
[1]) == ':')
182 if ((name
= fexpand(name
, fexpm
)) == NULL
)
186 switch (which_protocol(name
)) {
188 if (temporary_protocol_ext
!= NULL
)
189 name
= savecat(name
, temporary_protocol_ext
);
190 isdevnull
= ((options
& OPT_BATCH_FLAG
) && !strcmp(name
, "/dev/null"));
192 do { /* TODO we need objects, goes away then */
193 # ifdef HAVE_REALPATH_NULL
196 if ((cp
= realpath(name
, NULL
)) != NULL
) {
203 if (realpath(name
, cbuf
) != NULL
)
204 name
= savestr(cbuf
);
212 rv
= maildir_setfile(name
, fm
);
217 rv
= pop3_setfile(name
, fm
);
221 n_err(_("Cannot handle protocol: %s\n"), name
);
225 if ((ibuf
= Zopen(name
, "r")) == NULL
) {
226 if ((fm
& FEDIT_SYSBOX
) && errno
== ENOENT
) {
227 if (strcmp(who
, myname
) && getpwnam(who
) == NULL
) {
228 n_err(_("\"%s\" is not a user of this system\n"), who
);
231 if (!(options
& OPT_QUICKRUN_MASK
) && ok_blook(bsdcompat
))
232 n_err(_("No mail for %s\n"), who
);
233 if (fm
& FEDIT_NEWMAIL
)
235 if (ok_blook(emptystart
)) {
236 if (!(options
& OPT_QUICKRUN_MASK
) && !ok_blook(bsdcompat
))
238 /* We must avoid returning -1 and causing program exit */
239 mb
.mb_type
= MB_VOID
;
244 } else if (fm
& FEDIT_NEWMAIL
)
250 if (fstat(fileno(ibuf
), &stb
) == -1) {
251 if (fm
& FEDIT_NEWMAIL
)
253 n_perr(_("fstat"), 0);
257 if (S_ISREG(stb
.st_mode
) || isdevnull
) {
260 if (fm
& FEDIT_NEWMAIL
)
262 errno
= S_ISDIR(stb
.st_mode
) ? EISDIR
: EINVAL
;
267 /* Looks like all will be well. We must now relinquish our hold on the
268 * current set of stuff. Must hold signals while we are reading the new
269 * file, else we will ruin the message[] data structure */
271 hold_sigs(); /* TODO note on this one in quit.c:quit() */
272 if (shudclob
&& !(fm
& FEDIT_NEWMAIL
) && !quit()) {
278 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
279 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
282 /* TODO There is no intermediate VOID box we've switched to: name may
283 * TODO point to the same box that we just have written, so any updates
284 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
285 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
286 /* TODO We may uselessly open twice but quit() doesn't say wether we were
287 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
288 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name
, mailname
)) {
292 if ((ibuf
= Zopen(name
, "r")) == NULL
||
293 fstat(fileno(ibuf
), &stb
) == -1 ||
294 (!S_ISREG(stb
.st_mode
) && !isdevnull
)) {
301 /* Copy the messages into /tmp and set pointers */
302 if (!(fm
& FEDIT_NEWMAIL
)) {
303 mb
.mb_type
= MB_FILE
;
304 mb
.mb_perm
= (((options
& OPT_R_FLAG
) || (fm
& FEDIT_RDONLY
) ||
305 access(name
, W_OK
) < 0) ? 0 : MB_DELE
| MB_EDIT
);
317 if (fm
& FEDIT_SYSBOX
)
324 fseek(mb
.mb_otf
, 0L, SEEK_END
);
325 /* TODO Doing this without holding a lock is.. And not err checking.. */
326 fseek(ibuf
, mailsize
, SEEK_SET
);
328 omsgCount
= msgCount
;
333 else if (!(pstate
& PS_EDIT
))
334 lckfp
= n_dotlock(name
, fileno(ibuf
), FLT_READ
, offset
,0,
335 (fm
& FEDIT_NEWMAIL
? 0 : 1));
336 else if (n_file_lock(fileno(ibuf
), FLT_READ
, offset
,0,
337 (fm
& FEDIT_NEWMAIL
? 0 : 1)))
341 if (!(fm
& FEDIT_NEWMAIL
)) {
342 char const *emsg
= (pstate
& PS_EDIT
)
343 ? N_("Unable to lock mailbox, aborting operation")
344 : N_("Unable to (dot) lock mailbox, aborting operation");
348 if (!(fm
& FEDIT_NEWMAIL
))
353 mailsize
= fsize(ibuf
);
355 /* TODO This is too simple minded? We should regenerate an index file
356 * TODO to be able to truly tell wether *anything* has changed! */
357 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
361 setptr(ibuf
, offset
);
363 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
370 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1) {
376 if (!(fm
& FEDIT_NEWMAIL
)) {
377 pstate
&= ~PS_SAW_COMMAND
;
378 pstate
|= PS_SETFILE_OPENED
;
381 if ((options
& OPT_EXISTONLY
) && !(options
& OPT_HEADERLIST
)) {
382 rv
= (msgCount
== 0);
386 if ((!(pstate
& PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)) && msgCount
== 0) {
387 if (!(fm
& FEDIT_NEWMAIL
)) {
388 if (!ok_blook(emptystart
))
389 n_err(_("No mail for %s\n"), who
);
394 if (fm
& FEDIT_NEWMAIL
)
395 newmailinfo(omsgCount
);
401 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
407 mb
.mb_type
= MB_VOID
;
414 newmailinfo(int omsgCount
)
419 for (i
= 0; i
< omsgCount
; ++i
)
420 message
[i
].m_flag
&= ~MNEWEST
;
422 if (msgCount
> omsgCount
) {
423 for (i
= omsgCount
; i
< msgCount
; ++i
)
424 message
[i
].m_flag
|= MNEWEST
;
425 printf(_("New mail has arrived.\n"));
426 if ((i
= msgCount
- omsgCount
) == 1)
427 printf(_("Loaded 1 new message.\n"));
429 printf(_("Loaded %d new messages.\n"), i
);
431 printf(_("Loaded %d messages.\n"), msgCount
);
433 check_folder_hook(TRU1
);
437 if (ok_blook(header
))
438 print_headers(omsgCount
+ 1, msgCount
, FAL0
);
447 if (n_msgvec
!= NULL
)
449 n_msgvec
= scalloc(sz
+ 1, sizeof *n_msgvec
);
454 print_header_summary(char const *Larg
)
456 size_t bot
, top
, i
, j
;
460 /* Avoid any messages XXX add a make_mua_silent() and use it? */
461 if ((options
& (OPT_VERB
| OPT_EXISTONLY
)) == OPT_EXISTONLY
) {
462 freopen("/dev/null", "w", stdout
);
463 freopen("/dev/null", "w", stderr
);
465 assert(n_msgvec
!= NULL
);
466 i
= (getmsglist(/*TODO make arg const */UNCONST(Larg
), n_msgvec
, 0) <= 0);
467 if (options
& OPT_EXISTONLY
) {
468 exit_status
= (int)i
;
473 for (bot
= msgCount
, top
= 0, i
= 0; (j
= n_msgvec
[i
]) != 0; ++i
) {
480 bot
= 1, top
= msgCount
;
481 print_headers(bot
, top
, (Larg
!= NULL
)); /* TODO should take iterator!! */
487 announce(int printheaders
)
492 mdot
= newfileinfo();
495 dot
= message
+ mdot
- 1;
496 if (printheaders
&& msgCount
> 0 && ok_blook(header
)) {
497 print_header_group(vec
); /* XXX errors? */
506 int u
, n
, mdot
, d
, s
, hidden
, moved
;
509 if (mb
.mb_type
== MB_VOID
) {
515 s
= d
= hidden
= moved
=0;
516 for (mp
= message
, n
= 0, u
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
517 if (mp
->m_flag
& MNEW
)
519 if ((mp
->m_flag
& MREAD
) == 0)
521 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
523 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
525 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
527 if (mp
->m_flag
& MHIDDEN
)
531 /* If displayname gets truncated the user effectively has no option to see
532 * the full pathname of the mailbox, so print it at least for '? fi' */
533 printf(_("\"%s\": "),
534 (_update_mailname(NULL
) ? displayname
: mailname
));
536 printf(_("1 message"));
538 printf(_("%d messages"), msgCount
);
540 printf(_(" %d new"), n
);
542 printf(_(" %d unread"), u
);
544 printf(_(" %d deleted"), d
);
546 printf(_(" %d saved"), s
);
548 printf(_(" %d moved"), moved
);
550 printf(_(" %d hidden"), hidden
);
551 else if (mb
.mb_perm
== 0)
552 printf(_(" [Read only]"));
565 enum mflag avoid
= MHIDDEN
| MDELETED
;
569 if (ok_blook(autothread
)) {
570 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
572 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
573 if (mb
.mb_sorted
!= NULL
)
575 mb
.mb_sorted
= sstrdup(cp
);
579 if (mb
.mb_type
== MB_VOID
) {
585 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
586 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
589 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
590 if (mb
.mb_threaded
) {
591 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
592 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
595 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
596 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
601 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
602 if (mb
.mb_threaded
) {
603 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
604 if (mp
->m_flag
& MFLAGGED
)
607 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
608 if (mp
->m_flag
& MFLAGGED
)
613 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
614 if (mb
.mb_threaded
) {
615 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
616 if (!(mp
->m_flag
& (MREAD
| avoid
)))
619 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
620 if (!(mp
->m_flag
& (MREAD
| avoid
)))
626 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
627 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
628 else if (ok_blook(showlast
)) {
629 if (mb
.mb_threaded
) {
630 for (mp
= this_in_thread(threadroot
, -1); mp
;
631 mp
= prev_in_thread(mp
))
632 if (!(mp
->m_flag
& avoid
))
634 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
636 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
637 if (!(mp
->m_flag
& avoid
))
639 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
642 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
643 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
644 else if (mb
.mb_threaded
) {
645 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
646 if (!(mp
->m_flag
& avoid
))
648 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
650 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
651 if (!(mp
->m_flag
& avoid
))
653 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
654 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
662 initbox(char const *name
)
667 if (mb
.mb_type
!= MB_VOID
)
668 n_strscpy(prevfile
, mailname
, PATH_MAX
);
670 /* TODO name always NE mailname (but goes away for objects anyway)
671 * TODO Well, not true no more except that in parens */
672 _update_mailname((name
!= mailname
) ? name
: NULL
);
674 if ((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpmbox", OF_WRONLY
| OF_HOLDSIGS
)) ==
676 n_perr(_("temporary mail message file"), 0);
679 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
680 n_perr(_("temporary mail message file"), 0);
683 Ftmp_release(&tempMesg
);
687 if (mb
.mb_sorted
!= NULL
) {
691 dot
= prevdot
= threadroot
= NULL
;
692 pstate
&= ~PS_DID_PRINT_DOT
;
698 struct n_string s
, *sp
= &s
;
704 /* *folder* is linked with *_folder_resolved*: we only use the latter */
706 if((rv
= ok_vlook(_folder_resolved
)) != NULL
)
710 * If directory does not start with a <slash> ('/'), the contents
711 * of HOME shall be prefixed to it.
713 * If folder is unset or set to null, [.] filenames beginning with
714 * '+' shall refer to files in the current directory.
715 * We may have the result already */
719 if((cp
= ok_vlook(folder
)) == NULL
)
722 /* Expand the *folder*; skip %: prefix for simplicity of use */
723 if(cp
[0] == '%' && cp
[1] == ':')
725 if((err
= (cp
= fexpand(cp
, FEXP_NSHELL
)) == NULL
) || *cp
== '\0')
728 switch(which_protocol(cp
)){
730 n_err(_("*folder* can't be set to a flat, readonly POP3 account\n"));
734 /* Further expansion desired */
738 /* Prefix HOME as necessary */
740 char const *home
= ok_vlook(HOME
);
741 size_t l1
= strlen(home
), l2
= strlen(cp
);
743 sp
= n_string_creat_auto(sp
);
744 sp
= n_string_reserve(sp
, l1
+ 1 + l2
+1);
745 sp
= n_string_push_buf(sp
, home
, l1
);
746 sp
= n_string_push_c(sp
, '/');
747 sp
= n_string_push_buf(sp
, cp
, l2
);
748 cp
= n_string_cp(sp
);
753 /* TODO Since our visual mailname is resolved via realpath(3) if available
754 * TODO to avoid that we loose track of our currently open folder in case
755 * TODO we chdir away, but still checks the leading path portion against
756 * TODO fold_query() to be able to abbreviate to the +FOLDER syntax if
757 * TODO possible, we need to realpath(3) the folder, too */
760 # ifndef HAVE_REALPATH_NULL
762 sp
= n_string_drop_ownership(sp
);
763 sp
= n_string_reserve(sp
, PATH_MAX
+1);
768 if((sp
->s_dat
= realpath(cp
, sp
->s_dat
)) != NULL
){
769 # ifdef HAVE_REALPATH_NULL
770 sp
->s_dat
= savestr(cp
= sp
->s_dat
);
774 }else if(errno
== ENOENT
)
777 n_err(_("Can't canonicalize *folder*: \"%s\"\n"), cp
);
782 #endif /* HAVE_REALPATH */
786 bool_t reset
= !(pstate
& PS_ROOT
);
789 ok_vset(_folder_resolved
, rv
);
796 n_err(_("*folder* is not resolvable, using CWD\n"));
797 assert(rv
!= NULL
&& *rv
== '\0');