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
))
246 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
247 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
250 /* TODO There is no intermediate VOID box we've switched to: name may
251 * TODO point to the same box that we just have written, so any updates
252 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
253 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
254 /* TODO We may uselessly open twice but quit() doesn't say wether we were
255 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
256 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name
, mailname
)) {
260 if ((ibuf
= Zopen(name
, "r")) == NULL
||
261 fstat(fileno(ibuf
), &stb
) == -1 ||
262 (!S_ISREG(stb
.st_mode
) && !isdevnull
)) {
269 /* Copy the messages into /tmp and set pointers */
270 if (!(fm
& FEDIT_NEWMAIL
)) {
271 mb
.mb_type
= MB_FILE
;
272 mb
.mb_perm
= (((options
& OPT_R_FLAG
) || (fm
& FEDIT_RDONLY
) ||
273 access(name
, W_OK
) < 0) ? 0 : MB_DELE
| MB_EDIT
);
285 if (fm
& FEDIT_SYSBOX
)
292 fseek(mb
.mb_otf
, 0L, SEEK_END
);
293 /* TODO Doing this without holding a lock is.. And not err checking.. */
294 fseek(ibuf
, mailsize
, SEEK_SET
);
296 omsgCount
= msgCount
;
301 else if (!(pstate
& PS_EDIT
))
302 lckfp
= n_dotlock(name
, fileno(ibuf
), FLT_READ
, offset
,0,
303 (fm
& FEDIT_NEWMAIL
? 0 : 1));
304 else if (n_file_lock(fileno(ibuf
), FLT_READ
, offset
,0,
305 (fm
& FEDIT_NEWMAIL
? 0 : 1)))
309 if (!(fm
& FEDIT_NEWMAIL
)) {
310 char const *emsg
= (pstate
& PS_EDIT
)
311 ? N_("Unable to lock mailbox, aborting operation")
312 : N_("Unable to (dot) lock mailbox, aborting operation");
316 if (!(fm
& FEDIT_NEWMAIL
))
321 mailsize
= fsize(ibuf
);
323 /* TODO This is too simple minded? We should regenerate an index file
324 * TODO to be able to truly tell wether *anything* has changed! */
325 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
329 setptr(ibuf
, offset
);
331 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
338 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1) {
344 if (!(fm
& FEDIT_NEWMAIL
)) {
345 pstate
&= ~PS_SAW_COMMAND
;
346 pstate
|= PS_SETFILE_OPENED
;
349 if ((options
& OPT_EXISTONLY
) && !(options
& OPT_HEADERLIST
)) {
350 rv
= (msgCount
== 0);
354 if ((!(pstate
& PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)) && msgCount
== 0) {
355 if (!(fm
& FEDIT_NEWMAIL
)) {
356 if (!ok_blook(emptystart
))
357 n_err(_("No mail for %s\n"), who
);
362 if (fm
& FEDIT_NEWMAIL
)
363 newmailinfo(omsgCount
);
369 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
375 mb
.mb_type
= MB_VOID
;
382 newmailinfo(int omsgCount
)
387 for (i
= 0; i
< omsgCount
; ++i
)
388 message
[i
].m_flag
&= ~MNEWEST
;
390 if (msgCount
> omsgCount
) {
391 for (i
= omsgCount
; i
< msgCount
; ++i
)
392 message
[i
].m_flag
|= MNEWEST
;
393 printf(_("New mail has arrived.\n"));
394 if ((i
= msgCount
- omsgCount
) == 1)
395 printf(_("Loaded 1 new message.\n"));
397 printf(_("Loaded %d new messages.\n"), i
);
399 printf(_("Loaded %d messages.\n"), msgCount
);
401 check_folder_hook(TRU1
);
405 if (ok_blook(header
))
406 print_headers(omsgCount
+ 1, msgCount
, FAL0
);
415 if (n_msgvec
!= NULL
)
417 n_msgvec
= scalloc(sz
+ 1, sizeof *n_msgvec
);
422 print_header_summary(char const *Larg
)
424 size_t bot
, top
, i
, j
;
428 /* Avoid any messages XXX add a make_mua_silent() and use it? */
429 if ((options
& (OPT_VERB
| OPT_EXISTONLY
)) == OPT_EXISTONLY
) {
430 freopen("/dev/null", "w", stdout
);
431 freopen("/dev/null", "w", stderr
);
433 assert(n_msgvec
!= NULL
);
434 i
= (getmsglist(/*TODO make arg const */UNCONST(Larg
), n_msgvec
, 0) <= 0);
435 if (options
& OPT_EXISTONLY
) {
436 exit_status
= (int)i
;
441 for (bot
= msgCount
, top
= 0, i
= 0; (j
= n_msgvec
[i
]) != 0; ++i
) {
448 bot
= 1, top
= msgCount
;
449 print_headers(bot
, top
, (Larg
!= NULL
)); /* TODO should take iterator!! */
455 announce(int printheaders
)
460 mdot
= newfileinfo();
463 dot
= message
+ mdot
- 1;
464 if (printheaders
&& msgCount
> 0 && ok_blook(header
)) {
465 print_header_group(vec
); /* XXX errors? */
474 int u
, n
, mdot
, d
, s
, hidden
, moved
;
477 if (mb
.mb_type
== MB_VOID
) {
483 s
= d
= hidden
= moved
=0;
484 for (mp
= message
, n
= 0, u
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
485 if (mp
->m_flag
& MNEW
)
487 if ((mp
->m_flag
& MREAD
) == 0)
489 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
491 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
493 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
495 if (mp
->m_flag
& MHIDDEN
)
499 /* If displayname gets truncated the user effectively has no option to see
500 * the full pathname of the mailbox, so print it at least for '? fi' */
501 printf(_("\"%s\": "),
502 (_update_mailname(NULL
) ? displayname
: mailname
));
504 printf(_("1 message"));
506 printf(_("%d messages"), msgCount
);
508 printf(_(" %d new"), n
);
510 printf(_(" %d unread"), u
);
512 printf(_(" %d deleted"), d
);
514 printf(_(" %d saved"), s
);
516 printf(_(" %d moved"), moved
);
518 printf(_(" %d hidden"), hidden
);
519 else if (mb
.mb_perm
== 0)
520 printf(_(" [Read only]"));
533 enum mflag avoid
= MHIDDEN
| MDELETED
;
537 if (ok_blook(autothread
)) {
538 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
540 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
541 if (mb
.mb_sorted
!= NULL
)
543 mb
.mb_sorted
= sstrdup(cp
);
547 if (mb
.mb_type
== MB_VOID
) {
553 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
554 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
557 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
558 if (mb
.mb_threaded
) {
559 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
560 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
563 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
564 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
569 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
570 if (mb
.mb_threaded
) {
571 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
572 if (mp
->m_flag
& MFLAGGED
)
575 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
576 if (mp
->m_flag
& MFLAGGED
)
581 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
582 if (mb
.mb_threaded
) {
583 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
584 if (!(mp
->m_flag
& (MREAD
| avoid
)))
587 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
588 if (!(mp
->m_flag
& (MREAD
| avoid
)))
594 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
595 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
596 else if (ok_blook(showlast
)) {
597 if (mb
.mb_threaded
) {
598 for (mp
= this_in_thread(threadroot
, -1); mp
;
599 mp
= prev_in_thread(mp
))
600 if (!(mp
->m_flag
& avoid
))
602 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
604 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
605 if (!(mp
->m_flag
& avoid
))
607 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
610 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
611 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
612 else if (mb
.mb_threaded
) {
613 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
614 if (!(mp
->m_flag
& avoid
))
616 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
618 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
619 if (!(mp
->m_flag
& avoid
))
621 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
622 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
630 initbox(char const *name
)
635 if (mb
.mb_type
!= MB_VOID
)
636 n_strscpy(prevfile
, mailname
, PATH_MAX
);
638 /* TODO name always NE mailname (but goes away for objects anyway)
639 * TODO Well, not true no more except that in parens */
640 _update_mailname((name
!= mailname
) ? name
: NULL
);
642 if ((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpmbox", OF_WRONLY
| OF_HOLDSIGS
)) ==
644 n_perr(_("temporary mail message file"), 0);
647 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
648 n_perr(_("temporary mail message file"), 0);
651 Ftmp_release(&tempMesg
);
655 if (mb
.mb_sorted
!= NULL
) {
659 dot
= prevdot
= threadroot
= NULL
;
660 pstate
&= ~PS_DID_PRINT_DOT
;
665 getfold(char *name
, size_t size
)
670 if ((folder
= ok_vlook(folder
)) != NULL
)
671 n_strscpy(name
, folder
, size
);
673 return (folder
!= NULL
);