1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Iterating over, and over such housekeeping message user commands.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2018 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
36 #define n_FILE cmd_message
38 #ifndef HAVE_AMALGAMATION
42 /* Prepare and print "[Message: xy]:" intro */
43 static bool_t
a_cmsg_show_overview(FILE *obuf
, struct message
*mp
, int msg_no
);
45 /* Show the requested messages */
46 static int _type1(int *msgvec
, bool_t doign
, bool_t dopage
, bool_t dopipe
,
47 bool_t donotdecode
, char *cmd
, ui64_t
*tstats
);
49 /* Pipe the requested messages */
50 static int _pipe1(char *str
, int doign
);
53 static int a_cmsg_top(void *vp
, struct n_ignore
const *itp
);
55 /* Delete the indicated messages. Set dot to some nice place afterwards */
56 static int delm(int *msgvec
);
59 a_cmsg_show_overview(FILE *obuf
, struct message
*mp
, int msg_no
){
61 char const *cpre
, *csuf
;
64 cpre
= csuf
= n_empty
;
66 if(n_COLOUR_IS_ACTIVE()){
67 struct n_colour_pen
*cpen
;
69 if((cpen
= n_colour_pen_create(n_COLOUR_ID_VIEW_MSGINFO
, NULL
)) != NULL
){
72 if((sp
= n_colour_pen_to_str(cpen
)) != NULL
)
74 if((sp
= n_colour_reset_to_str()) != NULL
)
79 /* XXX Message info uses wire format for line count */
81 A_("%s[-- Message %2d -- %lu lines, %lu bytes --]:%s\n"),
82 cpre
, msg_no
, (ul_i
)mp
->m_lines
, (ul_i
)mp
->m_size
, csuf
) > 0);
88 _type1(int *msgvec
, bool_t doign
, bool_t dopage
, bool_t dopipe
,
89 bool_t donotdecode
, char *cmd
, ui64_t
*tstats
)
95 enum sendaction action
;
96 bool_t
volatile formfeed
;
103 formfeed
= (dopipe
&& ok_blook(page
));
104 action
= ((dopipe
&& ok_blook(piperaw
))
105 ? SEND_MBOX
: donotdecode
107 ? SEND_TODISP
: SEND_TODISP_ALL
);
110 if ((obuf
= Popen(cmd
, "w", ok_vlook(SHELL
), NULL
, 1)) == NULL
) {
114 } else if ((n_psonce
& n_PSO_TTYOUT
) && (dopage
||
115 ((n_psonce
& n_PSO_INTERACTIVE
) && (cp
= ok_vlook(crt
)) != NULL
))) {
121 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
122 mp
= message
+ *ip
- 1;
123 if (!(mp
->m_content_info
& CI_HAVE_BODY
))
124 if (get_body(mp
) != OKAY
)
126 nlines
+= mp
->m_lines
+ 1; /* TODO BUT wire format, not display! */
130 /* >= not <: we return to the prompt */
131 if(dopage
|| nlines
>= (*cp
!= '\0'
132 ? (n_idec_uiz_cp(&lib
, cp
, 0, NULL
), lib
)
133 : (uiz_t
)n_realscreenheight
)){
134 if((obuf
= n_pager_open()) == NULL
)
138 if(action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
)
139 n_colour_env_create(n_COLOUR_CTX_VIEW
, obuf
, obuf
!= n_stdout
);
143 else if(action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
)
144 n_colour_env_create(n_COLOUR_CTX_VIEW
, n_stdout
, FAL0
);
149 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
150 mp
= message
+ *ip
- 1;
153 n_pstate
|= n_PS_DID_PRINT_DOT
;
155 if(!dopipe
&& ip
!= msgvec
&& fprintf(obuf
, "\n") < 0){
159 if(action
!= SEND_MBOX
&& !a_cmsg_show_overview(obuf
, mp
, *ip
)){
163 if(sendmp(mp
, obuf
, (doign
? n_IGNORE_TYPE
: NULL
), NULL
, action
, mstats
169 if(formfeed
){ /* TODO a nicer way to separate piped messages! */
170 if(putc('\f', obuf
) == EOF
){
176 tstats
[0] += mstats
[0];
180 if(!dopipe
&& (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
))
184 if (obuf
!= n_stdout
)
191 _pipe1(char *str
, int doign
)
194 char const *cmd
, *cmdq
;
199 if ((cmd
= laststring(str
, &needs_list
, TRU1
)) == NULL
) {
201 if (cmd
== NULL
|| *cmd
== '\0') {
202 n_err(_("Variable *cmd* not set\n"));
207 msgvec
= n_autorec_alloc((msgCount
+ 2) * sizeof *msgvec
);
210 *msgvec
= first(0, MMNORM
);
212 if (n_pstate
& (n_PS_ROBOT
| n_PS_HOOK_MASK
)) {
216 fputs(_("No messages to pipe.\n"), n_stdout
);
220 } else if (getmsglist(str
, msgvec
, 0) < 0)
223 if (n_pstate
& (n_PS_ROBOT
| n_PS_HOOK_MASK
)) {
227 fprintf(n_stdout
, "No applicable messages.\n");
231 cmdq
= n_shexp_quote_cp(cmd
, FAL0
);
232 fprintf(n_stdout
, _("Pipe to: %s\n"), cmdq
);
234 if ((rv
= _type1(msgvec
, doign
, FAL0
, TRU1
, FAL0
, n_UNCONST(cmd
), stats
)
236 fprintf(n_stdout
, "%s %" PRIu64
" bytes\n", cmdq
, stats
[0]);
243 a_cmsg_top(void *vp
, struct n_ignore
const *itp
){
246 enum{a_NONE
, a_SQUEEZE
= 1u<<0,
247 a_EMPTY
= 1u<<8, a_STOP
= 1u<<9, a_WORKMASK
= 0xFF00u
} f
;
252 if((iobuf
= Ftmp(NULL
, "topio", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
253 n_perr(_("`top': I/O temporary file"), 0);
257 if((pbuf
= Ftmp(NULL
, "toppag", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
258 n_perr(_("`top': temporary pager file"), 0);
263 /* TODO In v15 we should query the m_message object, and directly send only
264 * TODO those parts, optionally over empty-line-squeeze and quote-strip
265 * TODO filters, in which we are interested in: only text content!
266 * TODO And: with *topsqueeze*, header/content separating empty line.. */
267 n_pstate
&= ~n_PS_MSGLIST_DIRECT
; /* TODO NO ATTACHMENTS */
270 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_VIEW
, iobuf
, FAL0
); )
271 n_string_creat_auto(&s
);
275 if((n_idec_siz_cp(&l
, ok_vlook(toplines
), 0, NULL
276 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
277 ) != n_IDEC_STATE_CONSUMED
)
280 tmax
= n_screensize();
288 f
= ok_blook(topsqueeze
) ? a_SQUEEZE
: a_NONE
;
290 for(ip
= msgvec
= vp
;
291 *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
){
294 mp
= &message
[*ip
- 1];
297 n_pstate
|= n_PS_DID_PRINT_DOT
;
301 if(ftruncate(fileno(iobuf
), 0)){
302 n_perr(_("`top': ftruncate(2)"), 0);
307 if(!a_cmsg_show_overview(iobuf
, mp
, *ip
) ||
308 sendmp(mp
, iobuf
, itp
, NULL
, SEND_TODISP_ALL
, NULL
) < 0){
309 n_err(_("`top': failed to prepare message %d\n"), *ip
);
313 fflush_rewind(iobuf
);
315 /* TODO Skip over the _msg_overview line -- this is a hack to make
316 * TODO colours work: colour contexts should be objects */
320 if((c
= getc(iobuf
)) == EOF
|| putc(c
, pbuf
) == EOF
){
333 n_string_trunc(&s
, 0);
334 for(l
= 0, f
&= ~a_WORKMASK
; !(f
& a_STOP
);){
337 if((c
= getc(iobuf
)) == EOF
){
343 n_string_push_c(&s
, c
);
344 else if((f
& a_SQUEEZE
) && s
.s_len
== 0){
345 if(!(f
& a_STOP
) && ((f
& a_EMPTY
) || tmax
- 1 <= l
))
347 if(putc('\n', pbuf
) == EOF
){
354 char const *cp
, *xcp
;
356 cp
= n_string_cp_const(&s
);
357 /* TODO Brute simple skip part overviews; see above.. */
360 else if(s
.s_len
> 8 &&
361 (xcp
= strstr(cp
, "[-- ")) != NULL
&&
362 strstr(&xcp
[1], " --]") != NULL
)
367 for(qcp
= ok_vlook(quote_chars
); (c
= *cp
) != '\0'; ++cp
){
370 if(!blankspacechar(c
)){
371 if(strchr(qcp
, c
) == NULL
)
380 if(fputs(n_string_cp_const(&s
), pbuf
) == EOF
||
381 putc('\n', pbuf
) == EOF
){
390 n_string_trunc(&s
, 0);
398 if(!(f
& a_EMPTY
) && putc('\n', pbuf
) == EOF
){
408 n_COLOUR( n_colour_env_gut(); )
411 page_or_print(pbuf
, plines
);
425 int rv
= -1, *ip
, last
;
429 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
430 mp
= message
+ *ip
- 1;
432 mp
->m_flag
|= MDELETED
| MTOUCH
;
433 mp
->m_flag
&= ~(MPRESERVE
| MSAVED
| MBOX
);
437 setdot(message
+ last
- 1);
438 last
= first(0, MDELETED
);
440 setdot(message
+ last
- 1);
456 rv
= _type1(msgvec
, TRU1
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
467 rv
= _type1(msgvec
, FAL0
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
478 rv
= _type1(msgvec
, TRU1
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
489 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
500 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, TRU1
, NULL
, NULL
);
506 c_mimeview(void *vp
){ /* TODO direct addressable parts, multiple such */
511 if((msgvec
= vp
)[1] != 0){
512 n_err(_("`mimeview': can yet only take one message, sorry!\n"));/* TODO */
513 n_pstate_err_no
= n_ERR_NOTSUP
;
518 mp
= &message
[*msgvec
- 1];
521 n_pstate
|= n_PS_DID_PRINT_DOT
;
525 n_colour_env_create(n_COLOUR_CTX_VIEW
, n_stdout
, FAL0
);
528 if(!a_cmsg_show_overview(n_stdout
, mp
, *msgvec
))
529 n_pstate_err_no
= n_ERR_IO
;
530 else if(sendmp(mp
, n_stdout
, n_IGNORE_TYPE
, NULL
, SEND_TODISP_PARTS
,
532 n_pstate_err_no
= n_ERR_IO
;
534 n_pstate_err_no
= n_ERR_NONE
;
540 rv
= (n_pstate_err_no
!= n_ERR_NONE
);
572 struct n_ignore
*itp
;
576 if(n_ignore_is_any(n_IGNORE_TOP
))
579 itp
= n_ignore_new(TRU1
);
580 n_ignore_insert(itp
, TRU1
, "from", sizeof("from") -1);
581 n_ignore_insert(itp
, TRU1
, "to", sizeof("to") -1);
582 n_ignore_insert(itp
, TRU1
, "cc", sizeof("cc") -1);
583 n_ignore_insert(itp
, TRU1
, "subject", sizeof("subject") -1);
586 rv
= !a_cmsg_top(v
, itp
);
596 rv
= !a_cmsg_top(v
, n_IGNORE_TYPE
);
604 int list
[2], *ip
, *ip2
, mdot
, *msgvec
= v
, rv
= 1;
609 /* If some messages were supplied, find the first applicable one
610 * following dot using wrap around */
611 mdot
= (int)PTR2SIZE(dot
- message
+ 1);
613 /* Find first message in supplied message list which follows dot */
614 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
615 if ((mb
.mb_threaded
? message
[*ip
- 1].m_threadpos
> dot
->m_threadpos
623 mp
= message
+ *ip2
- 1;
624 if (!(mp
->m_flag
& MMNDEL
)) {
633 fprintf(n_stdout
, _("No messages applicable\n"));
637 /* If this is the first command, select message 1. Note that this must
638 * exist for us to get here at all */
639 if (!(n_pstate
& n_PS_SAW_COMMAND
)) {
645 /* Just find the next good message after dot, no wraparound */
646 if (mb
.mb_threaded
== 0) {
647 for (mp
= dot
+ !!(n_pstate
& n_PS_DID_PRINT_DOT
);
648 PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
649 if (!(mp
->m_flag
& MMNORM
))
652 /* TODO The threading code had some bugs that caused crashes.
653 * TODO The last thing (before the deep look) happens here,
654 * TODO so let's not trust n_PS_DID_PRINT_DOT but check & hope it fixes */
655 if ((mp
= dot
) != NULL
&& (n_pstate
& n_PS_DID_PRINT_DOT
))
656 mp
= next_in_thread(mp
);
657 while (mp
!= NULL
&& (mp
->m_flag
& MMNORM
))
658 mp
= next_in_thread(mp
);
660 if (mp
== NULL
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
662 fprintf(n_stdout
, _("At EOF\n"));
670 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
683 fprintf(n_stdout
, "%d\n", (int)PTR2SIZE(dot
- message
+ 1));
691 int *msgvec
= v
, *ip
, mesg
;
695 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
697 mp
= message
+ mesg
- 1;
698 fprintf(n_stdout
, "%d: ", mesg
);
699 if (mp
->m_xlines
> 0)
700 fprintf(n_stdout
, "%ld", mp
->m_xlines
);
703 fprintf(n_stdout
, "/%lu\n", (ul_i
)mp
->m_xsize
);
723 int list
[2], rv
= 0, *msgvec
= v
, lastdot
;
726 lastdot
= (int)PTR2SIZE(dot
- message
+ 1);
727 if (delm(msgvec
) >= 0) {
728 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
729 if (list
[0] > lastdot
) {
735 fprintf(n_stdout
, _("At EOF\n"));
737 fprintf(n_stdout
, _("No more messages\n"));
746 int *msgvec
= v
, *ip
;
750 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
752 mp
= &message
[*ip
- 1];
755 if (mp
->m_flag
& (MDELETED
| MSAVED
))
756 mp
->m_flag
&= ~(MDELETED
| MSAVED
);
758 mp
->m_flag
&= ~MDELETED
;
760 if (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)
761 imap_undelete(mp
, *ip
);
771 int *msgvec
= v
, *ip
;
774 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
775 setdot(message
+ *ip
- 1);
776 dot
->m_flag
|= MTOUCH
;
777 dot
->m_flag
&= ~MPRESERVE
;
778 n_pstate
|= n_PS_DID_PRINT_DOT
;
787 int *msgvec
= v
, *ip
;
790 if (n_pstate
& n_PS_EDIT
) {
791 n_err(_("`mbox' can only be used in a system mailbox\n")); /* TODO */
795 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
796 setdot(message
+ *ip
- 1);
797 dot
->m_flag
|= MTOUCH
| MBOX
;
798 dot
->m_flag
&= ~MPRESERVE
;
799 n_pstate
|= n_PS_DID_PRINT_DOT
;
809 int *msgvec
= v
, *ip
, mesg
, rv
= 1;
813 if (n_pstate
& n_PS_EDIT
) {
814 fprintf(n_stdout
, _("Cannot `preserve' in a system mailbox\n"));
818 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
820 mp
= message
+ mesg
- 1;
821 mp
->m_flag
|= MPRESERVE
;
824 n_pstate
|= n_PS_DID_PRINT_DOT
;
836 int *msgvec
= v
, *ip
;
839 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
840 mp
= &message
[*ip
- 1];
842 dot
->m_flag
&= ~(MREAD
| MTOUCH
);
843 dot
->m_flag
|= MSTATUS
;
845 if (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)
846 imap_unread(mp
, *ip
); /* TODO return? */
848 n_pstate
|= n_PS_DID_PRINT_DOT
;
857 int *msgvec
= v
, *ip
;
860 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
861 struct message
*mp
= message
+ *ip
- 1;
873 int *msgvec
= v
, *ip
;
876 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
877 m
= message
+ *ip
- 1;
879 if (!(m
->m_flag
& (MFLAG
| MFLAGGED
)))
880 m
->m_flag
|= MFLAG
| MFLAGGED
;
890 int *msgvec
= v
, *ip
;
893 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
894 m
= message
+ *ip
- 1;
896 if (m
->m_flag
& (MFLAG
| MFLAGGED
)) {
897 m
->m_flag
&= ~(MFLAG
| MFLAGGED
);
898 m
->m_flag
|= MUNFLAG
;
909 int *msgvec
= v
, *ip
;
912 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
913 m
= message
+ *ip
- 1;
915 if (!(m
->m_flag
& (MANSWER
| MANSWERED
)))
916 m
->m_flag
|= MANSWER
| MANSWERED
;
923 c_unanswered(void *v
)
926 int *msgvec
= v
, *ip
;
929 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
930 m
= message
+ *ip
- 1;
932 if (m
->m_flag
& (MANSWER
| MANSWERED
)) {
933 m
->m_flag
&= ~(MANSWER
| MANSWERED
);
934 m
->m_flag
|= MUNANSWER
;
945 int *msgvec
= v
, *ip
;
948 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
949 m
= message
+ *ip
- 1;
951 if (!(m
->m_flag
& (MDRAFT
| MDRAFTED
)))
952 m
->m_flag
|= MDRAFT
| MDRAFTED
;
962 int *msgvec
= v
, *ip
;
965 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
966 m
= message
+ *ip
- 1;
968 if (m
->m_flag
& (MDRAFT
| MDRAFTED
)) {
969 m
->m_flag
&= ~(MDRAFT
| MDRAFTED
);
970 m
->m_flag
|= MUNDRAFT
;