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
)
84 char tbuf
[PATH_MAX
], *mailp
, *dispp
;
89 /* Don't realpath(3) if it's only an update request */
92 enum protocol p
= which_protocol(name
);
93 if (p
== PROTO_FILE
|| p
== PROTO_MAILDIR
) {
94 if (realpath(name
, mailname
) == NULL
) {
95 n_err(_("Can't canonicalize \"%s\"\n"), name
);
102 n_strscpy(mailname
, name
, sizeof(mailname
));
108 /* Don't display an absolute path but "+FOLDER" if under *folder* */
109 if (getfold(tbuf
, sizeof tbuf
)) {
111 if (i
< sizeof(tbuf
) -1)
113 if (!strncmp(tbuf
, mailp
, i
)) {
119 /* We want to see the name of the folder .. on the screen */
121 if (i
< sizeof(displayname
) -1)
122 memcpy(dispp
, mailp
, i
+1);
125 /* Avoid disrupting multibyte sequences (if possible) */
126 #ifndef HAVE_C90AMEND1
127 j
= sizeof(displayname
) / 3 - 1;
128 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
130 j
= field_detect_clip(sizeof(displayname
) / 3, mailp
, i
);
131 i
= j
+ __narrow_suffix(mailp
+ j
, i
- j
,
132 sizeof(displayname
) - (1/* + */ + 3 + 1) - j
);
134 snprintf(dispp
, sizeof(displayname
), "%.*s...%s",
135 (int)j
, mailp
, mailp
+ i
);
142 setfile(char const *name
, enum fedit_mode fm
) /* TODO oh my god */
144 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
145 * necessary to make a mailbox accessible by a different user, and if that
146 * has happened, let's just let the usual file perms decide */
152 int rv
, omsgCount
= 0;
153 FILE *ibuf
= NULL
, *lckfp
= NULL
;
154 bool_t isdevnull
= FAL0
;
157 pstate
&= ~PS_SETFILE_OPENED
;
159 if (name
[0] == '%' || ((who
= shortcut_expand(name
)) != NULL
&& *who
== '%'))
160 fm
|= FEDIT_SYSBOX
; /* TODO fexpand() needs to tell is-valid-user! */
162 who
= (name
[1] != '\0') ? name
+ 1 : myname
;
164 if ((name
= expand(name
)) == NULL
)
167 switch (which_protocol(name
)) {
169 if (temporary_protocol_ext
!= NULL
)
170 name
= savecat(name
, temporary_protocol_ext
);
171 isdevnull
= ((options
& OPT_BATCH_FLAG
) && !strcmp(name
, "/dev/null"));
173 do { /* TODO we need objects, goes away then */
175 if (realpath(name
, ebuf
) != NULL
)
176 name
= savestr(ebuf
);
183 rv
= maildir_setfile(name
, fm
);
188 rv
= pop3_setfile(name
, fm
);
192 n_err(_("Cannot handle protocol: %s\n"), name
);
196 if ((ibuf
= Zopen(name
, "r")) == NULL
) {
197 if ((fm
& FEDIT_SYSBOX
) && errno
== ENOENT
) {
198 if (strcmp(who
, myname
) && getpwnam(who
) == NULL
) {
199 n_err(_("\"%s\" is not a user of this system\n"), who
);
202 if (!(options
& OPT_QUICKRUN_MASK
) && ok_blook(bsdcompat
))
203 n_err(_("No mail for %s\n"), who
);
204 if (fm
& FEDIT_NEWMAIL
)
206 if (ok_blook(emptystart
)) {
207 if (!(options
& OPT_QUICKRUN_MASK
) && !ok_blook(bsdcompat
))
209 /* We must avoid returning -1 and causing program exit */
210 mb
.mb_type
= MB_VOID
;
215 } else if (fm
& FEDIT_NEWMAIL
)
221 if (fstat(fileno(ibuf
), &stb
) == -1) {
222 if (fm
& FEDIT_NEWMAIL
)
224 n_perr(_("fstat"), 0);
228 if (S_ISREG(stb
.st_mode
) || isdevnull
) {
231 if (fm
& FEDIT_NEWMAIL
)
233 errno
= S_ISDIR(stb
.st_mode
) ? EISDIR
: EINVAL
;
238 /* Looks like all will be well. We must now relinquish our hold on the
239 * current set of stuff. Must hold signals while we are reading the new
240 * file, else we will ruin the message[] data structure */
242 hold_sigs(); /* TODO note on this one in quit.c:quit() */
243 if (shudclob
&& !(fm
& FEDIT_NEWMAIL
) && !quit()) {
249 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
250 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
253 /* TODO There is no intermediate VOID box we've switched to: name may
254 * TODO point to the same box that we just have written, so any updates
255 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
256 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
257 /* TODO We may uselessly open twice but quit() doesn't say wether we were
258 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
259 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name
, mailname
)) {
263 if ((ibuf
= Zopen(name
, "r")) == NULL
||
264 fstat(fileno(ibuf
), &stb
) == -1 ||
265 (!S_ISREG(stb
.st_mode
) && !isdevnull
)) {
272 /* Copy the messages into /tmp and set pointers */
273 if (!(fm
& FEDIT_NEWMAIL
)) {
274 mb
.mb_type
= MB_FILE
;
275 mb
.mb_perm
= (((options
& OPT_R_FLAG
) || (fm
& FEDIT_RDONLY
) ||
276 access(name
, W_OK
) < 0) ? 0 : MB_DELE
| MB_EDIT
);
288 if (fm
& FEDIT_SYSBOX
)
295 fseek(mb
.mb_otf
, 0L, SEEK_END
);
296 /* TODO Doing this without holding a lock is.. And not err checking.. */
297 fseek(ibuf
, mailsize
, SEEK_SET
);
299 omsgCount
= msgCount
;
304 else if (!(pstate
& PS_EDIT
))
305 lckfp
= n_dotlock(name
, fileno(ibuf
), FLT_READ
, offset
,0,
306 (fm
& FEDIT_NEWMAIL
? 0 : 1));
307 else if (n_file_lock(fileno(ibuf
), FLT_READ
, offset
,0,
308 (fm
& FEDIT_NEWMAIL
? 0 : 1)))
312 if (!(fm
& FEDIT_NEWMAIL
)) {
313 char const *emsg
= (pstate
& PS_EDIT
)
314 ? N_("Unable to lock mailbox, aborting operation")
315 : N_("Unable to (dot) lock mailbox, aborting operation");
319 if (!(fm
& FEDIT_NEWMAIL
))
324 mailsize
= fsize(ibuf
);
326 /* TODO This is too simple minded? We should regenerate an index file
327 * TODO to be able to truly tell wether *anything* has changed! */
328 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
332 setptr(ibuf
, offset
);
334 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
341 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1) {
347 if (!(fm
& FEDIT_NEWMAIL
)) {
348 pstate
&= ~PS_SAW_COMMAND
;
349 pstate
|= PS_SETFILE_OPENED
;
352 if ((options
& OPT_EXISTONLY
) && !(options
& OPT_HEADERLIST
)) {
353 rv
= (msgCount
== 0);
357 if ((!(pstate
& PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)) && msgCount
== 0) {
358 if (!(fm
& FEDIT_NEWMAIL
)) {
359 if (!ok_blook(emptystart
))
360 n_err(_("No mail for %s\n"), who
);
365 if (fm
& FEDIT_NEWMAIL
)
366 newmailinfo(omsgCount
);
372 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
378 mb
.mb_type
= MB_VOID
;
385 newmailinfo(int omsgCount
)
390 for (i
= 0; i
< omsgCount
; ++i
)
391 message
[i
].m_flag
&= ~MNEWEST
;
393 if (msgCount
> omsgCount
) {
394 for (i
= omsgCount
; i
< msgCount
; ++i
)
395 message
[i
].m_flag
|= MNEWEST
;
396 printf(_("New mail has arrived.\n"));
397 if ((i
= msgCount
- omsgCount
) == 1)
398 printf(_("Loaded 1 new message.\n"));
400 printf(_("Loaded %d new messages.\n"), i
);
402 printf(_("Loaded %d messages.\n"), msgCount
);
404 check_folder_hook(TRU1
);
408 if (ok_blook(header
))
409 print_headers(omsgCount
+ 1, msgCount
, FAL0
);
418 if (n_msgvec
!= NULL
)
420 n_msgvec
= scalloc(sz
+ 1, sizeof *n_msgvec
);
425 print_header_summary(char const *Larg
)
427 size_t bot
, top
, i
, j
;
431 /* Avoid any messages XXX add a make_mua_silent() and use it? */
432 if ((options
& (OPT_VERB
| OPT_EXISTONLY
)) == OPT_EXISTONLY
) {
433 freopen("/dev/null", "w", stdout
);
434 freopen("/dev/null", "w", stderr
);
436 assert(n_msgvec
!= NULL
);
437 i
= (getmsglist(/*TODO make arg const */UNCONST(Larg
), n_msgvec
, 0) <= 0);
438 if (options
& OPT_EXISTONLY
) {
439 exit_status
= (int)i
;
444 for (bot
= msgCount
, top
= 0, i
= 0; (j
= n_msgvec
[i
]) != 0; ++i
) {
451 bot
= 1, top
= msgCount
;
452 print_headers(bot
, top
, (Larg
!= NULL
)); /* TODO should take iterator!! */
458 announce(int printheaders
)
463 mdot
= newfileinfo();
466 dot
= message
+ mdot
- 1;
467 if (printheaders
&& msgCount
> 0 && ok_blook(header
)) {
468 print_header_group(vec
); /* XXX errors? */
477 int u
, n
, mdot
, d
, s
, hidden
, moved
;
480 if (mb
.mb_type
== MB_VOID
) {
486 s
= d
= hidden
= moved
=0;
487 for (mp
= message
, n
= 0, u
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
488 if (mp
->m_flag
& MNEW
)
490 if ((mp
->m_flag
& MREAD
) == 0)
492 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
494 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
496 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
498 if (mp
->m_flag
& MHIDDEN
)
502 /* If displayname gets truncated the user effectively has no option to see
503 * the full pathname of the mailbox, so print it at least for '? fi' */
504 printf(_("\"%s\": "),
505 (_update_mailname(NULL
) ? displayname
: mailname
));
507 printf(_("1 message"));
509 printf(_("%d messages"), msgCount
);
511 printf(_(" %d new"), n
);
513 printf(_(" %d unread"), u
);
515 printf(_(" %d deleted"), d
);
517 printf(_(" %d saved"), s
);
519 printf(_(" %d moved"), moved
);
521 printf(_(" %d hidden"), hidden
);
522 else if (mb
.mb_perm
== 0)
523 printf(_(" [Read only]"));
536 enum mflag avoid
= MHIDDEN
| MDELETED
;
540 if (ok_blook(autothread
)) {
541 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
543 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
544 if (mb
.mb_sorted
!= NULL
)
546 mb
.mb_sorted
= sstrdup(cp
);
550 if (mb
.mb_type
== MB_VOID
) {
556 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
557 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
560 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
561 if (mb
.mb_threaded
) {
562 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
563 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
566 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
567 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
572 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
573 if (mb
.mb_threaded
) {
574 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
575 if (mp
->m_flag
& MFLAGGED
)
578 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
579 if (mp
->m_flag
& MFLAGGED
)
584 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
585 if (mb
.mb_threaded
) {
586 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
587 if (!(mp
->m_flag
& (MREAD
| avoid
)))
590 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
591 if (!(mp
->m_flag
& (MREAD
| avoid
)))
597 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
598 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
599 else if (ok_blook(showlast
)) {
600 if (mb
.mb_threaded
) {
601 for (mp
= this_in_thread(threadroot
, -1); mp
;
602 mp
= prev_in_thread(mp
))
603 if (!(mp
->m_flag
& avoid
))
605 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
607 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
608 if (!(mp
->m_flag
& avoid
))
610 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
613 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
614 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
615 else if (mb
.mb_threaded
) {
616 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
617 if (!(mp
->m_flag
& avoid
))
619 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
621 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
622 if (!(mp
->m_flag
& avoid
))
624 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
625 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
633 initbox(char const *name
)
638 if (mb
.mb_type
!= MB_VOID
)
639 n_strscpy(prevfile
, mailname
, PATH_MAX
);
641 /* TODO name always NE mailname (but goes away for objects anyway)
642 * TODO Well, not true no more except that in parens */
643 _update_mailname((name
!= mailname
) ? name
: NULL
);
645 if ((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpmbox", OF_WRONLY
| OF_HOLDSIGS
)) ==
647 n_perr(_("temporary mail message file"), 0);
650 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
651 n_perr(_("temporary mail message file"), 0);
654 Ftmp_release(&tempMesg
);
658 if (mb
.mb_sorted
!= NULL
) {
662 dot
= prevdot
= threadroot
= NULL
;
663 pstate
&= ~PS_DID_PRINT_DOT
;
668 getfold(char *name
, size_t size
)
673 if ((folder
= ok_vlook(folder
)) != NULL
)
674 n_strscpy(name
, folder
, size
);
676 return (folder
!= NULL
);