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 - 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
36 #define n_FILE cmd_message
38 #ifndef HAVE_AMALGAMATION
42 /* Prepare and print "[Message: xy]:" intro */
43 static void _show_msg_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 _show_msg_overview(FILE *obuf
, struct message
*mp
, int msg_no
)
61 char const *cpre
, *csuf
;
64 cpre
= csuf
= n_empty
;
66 if (pstate
& PS_COLOUR_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 fprintf(obuf
, _("%s[-- Message %2d -- %lu lines, %lu bytes --]:%s\n"),
80 cpre
, msg_no
, (ul_i
)mp
->m_lines
, (ul_i
)mp
->m_size
, csuf
);
85 _type1(int *msgvec
, bool_t doign
, bool_t dopage
, bool_t dopipe
,
86 bool_t donotdecode
, char *cmd
, ui64_t
*tstats
)
95 bool_t
volatile isrelax
= FAL0
;
98 enum sendaction
const action
= ((dopipe
&& ok_blook(piperaw
))
99 ? SEND_MBOX
: donotdecode
101 ? SEND_TODISP
: SEND_TODISP_ALL
);
102 bool_t
const volatile formfeed
= (dopipe
&& ok_blook(page
));
105 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
113 if ((obuf
= Popen(cmd
, "w", ok_vlook(SHELL
), NULL
, 1)) == NULL
) {
117 } else if ((options
& OPT_TTYOUT
) && (dopage
||
118 ((options
& OPT_INTERACTIVE
) && (cp
= ok_vlook(crt
)) != NULL
))) {
122 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
123 mp
= message
+ *ip
- 1;
124 if (!(mp
->m_content_info
& CI_HAVE_BODY
))
125 if (get_body(mp
) != OKAY
)
127 nlines
+= mp
->m_lines
+ 1; /* Message info XXX and PARTS... */
131 /* >= not <: we return to the prompt */
132 if (dopage
|| UICMP(z
, nlines
, >=,
133 (*cp
!= '\0' ? strtoul(cp
, NULL
, 0) : (size_t)realscreenheight
))) {
134 if ((obuf
= n_pager_open()) == NULL
)
138 if ((options
& OPT_INTERACTIVE
) &&
139 (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
140 action
== SEND_SHOW
))
141 n_colour_env_create(n_COLOUR_CTX_VIEW
, obuf
!= stdout
);
145 else if ((options
& OPT_INTERACTIVE
) &&
146 (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
))
147 n_colour_env_create(n_COLOUR_CTX_VIEW
, FAL0
);
150 /*TODO unless we have our signal manager special care must be taken */
153 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
154 mp
= message
+ *ip
- 1;
157 pstate
|= PS_DID_PRINT_DOT
;
159 if (!dopipe
&& ip
!= msgvec
)
161 if (action
!= SEND_MBOX
)
162 _show_msg_overview(obuf
, mp
, *ip
);
163 sendmp(mp
, obuf
, (doign
? n_IGNORE_TYPE
: NULL
), NULL
, action
, mstats
);
165 if (formfeed
) /* TODO a nicer way to separate piped messages! */
168 tstats
[0] += mstats
[0];
175 n_sigman_cleanup_ping(&sm
);
179 n_COLOUR( n_colour_env_gut((sm
.sm_signo
!= SIGPIPE
) ? obuf
: NULL
); )
184 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
189 _pipe1(char *str
, int doign
)
192 char const *cmd
, *cmdq
;
197 if ((cmd
= laststring(str
, &needs_list
, TRU1
)) == NULL
) {
199 if (cmd
== NULL
|| *cmd
== '\0') {
200 n_err(_("Variable *cmd* not set\n"));
205 msgvec
= salloc((msgCount
+ 2) * sizeof *msgvec
);
208 *msgvec
= first(0, MMNORM
);
210 if (pstate
& (PS_HOOK_MASK
| PS_ROBOT
)) {
214 puts(_("No messages to pipe."));
218 } else if (getmsglist(str
, msgvec
, 0) < 0)
221 if (pstate
& (PS_HOOK_MASK
| PS_ROBOT
)) {
225 printf("No applicable messages.\n");
229 cmdq
= n_shexp_quote_cp(cmd
, FAL0
);
230 printf(_("Pipe to: %s\n"), cmdq
);
232 if ((rv
= _type1(msgvec
, doign
, FAL0
, TRU1
, FAL0
, n_UNCONST(cmd
), stats
)
234 printf("%s %" PRIu64
" bytes\n", cmdq
, stats
[0]);
241 a_cmsg_top(void *vp
, struct n_ignore
const *itp
){
244 enum{a_NONE
, a_SQUEEZE
= 1u<<0,
245 a_EMPTY
= 1u<<8, a_STOP
= 1u<<9, a_WORKMASK
= 0xFF00u
} f
;
250 if((iobuf
= Ftmp(NULL
, "topio", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
251 n_perr(_("`top': I/O temporary file"), 0);
255 if((pbuf
= Ftmp(NULL
, "toppag", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
256 n_perr(_("`top': temporary pager file"), 0);
261 /* TODO In v15 we should query the m_message object, and directly send only
262 * TODO those parts, optionally over empty-line-squeeze and quote-strip
263 * TODO filters, in which we are interested in: only text content!
264 * TODO And: with *topsqueeze*, header/content separating empty line.. */
265 pstate
&= ~PS_MSGLIST_DIRECT
; /* TODO NO ATTACHMENTS */
269 if (options
& OPT_INTERACTIVE
)
270 n_colour_env_create(n_COLOUR_CTX_VIEW
, TRU1
);
272 n_string_creat_auto(&s
);
276 if((l
= strtol(ok_vlook(toplines
), NULL
, 0)) <= 0){
277 tmax
= (size_t)screensize();
285 f
= ok_blook(topsqueeze
) ? a_SQUEEZE
: a_NONE
;
287 for(ip
= msgvec
= vp
;
288 *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
){
291 mp
= &message
[*ip
- 1];
294 pstate
|= PS_DID_PRINT_DOT
;
298 if(ftruncate(fileno(iobuf
), 0)){
299 n_perr(_("`top': ftruncate(2)"), 0);
303 if(sendmp(mp
, iobuf
, itp
, NULL
, SEND_TODISP_ALL
, NULL
) < 0){
304 n_err(_("`top': failed to prepare message %d\n"), *ip
);
308 fflush_rewind(iobuf
);
310 _show_msg_overview(pbuf
, mp
, *ip
);
315 n_string_trunc(&s
, 0);
316 for(l
= 0, f
&= ~a_WORKMASK
; !(f
& a_STOP
);){
319 if((c
= getc(iobuf
)) == EOF
){
325 n_string_push_c(&s
, c
);
326 else if((f
& a_SQUEEZE
) && s
.s_len
== 0){
327 if(!(f
& a_STOP
) && ((f
& a_EMPTY
) || tmax
- 1 <= l
))
329 if(putc('\n', pbuf
) == EOF
){
336 char const *cp
, *xcp
;
338 cp
= n_string_cp_const(&s
);
339 /* TODO Brute simple skip part overviews; see above.. */
342 else if(s
.s_len
> 8 &&
343 (xcp
= strstr(cp
, "[-- ")) != NULL
&&
344 strstr(&xcp
[1], " --]") != NULL
)
346 else for(; (c
= *cp
) != '\0'; ++cp
){
349 if(!blankspacechar(c
)){
358 if(fputs(n_string_cp_const(&s
), pbuf
) == EOF
||
359 putc('\n', pbuf
) == EOF
){
368 n_string_trunc(&s
, 0);
376 if(!(f
& a_EMPTY
) && putc('\n', pbuf
) == EOF
){
386 n_COLOUR( n_colour_env_gut(pbuf
); )
389 page_or_print(pbuf
, plines
);
403 int rv
= -1, *ip
, last
;
407 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
408 mp
= message
+ *ip
- 1;
410 mp
->m_flag
|= MDELETED
| MTOUCH
;
411 mp
->m_flag
&= ~(MPRESERVE
| MSAVED
| MBOX
);
415 setdot(message
+ last
- 1);
416 last
= first(0, MDELETED
);
418 setdot(message
+ last
- 1);
434 rv
= _type1(msgvec
, TRU1
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
445 rv
= _type1(msgvec
, FAL0
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
456 rv
= _type1(msgvec
, TRU1
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
467 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
478 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, TRU1
, NULL
, NULL
);
509 struct n_ignore
*itp
;
513 if(n_ignore_is_any(n_IGNORE_TOP
))
516 itp
= n_ignore_new(TRU1
);
517 n_ignore_insert(itp
, TRU1
, "from", sizeof("from") -1);
518 n_ignore_insert(itp
, TRU1
, "to", sizeof("to") -1);
519 n_ignore_insert(itp
, TRU1
, "cc", sizeof("cc") -1);
520 n_ignore_insert(itp
, TRU1
, "subject", sizeof("subject") -1);
523 rv
= !a_cmsg_top(v
, itp
);
533 rv
= !a_cmsg_top(v
, n_IGNORE_TYPE
);
541 int list
[2], *ip
, *ip2
, mdot
, *msgvec
= v
, rv
= 1;
546 /* If some messages were supplied, find the first applicable one
547 * following dot using wrap around */
548 mdot
= (int)PTR2SIZE(dot
- message
+ 1);
550 /* Find first message in supplied message list which follows dot */
551 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
552 if ((mb
.mb_threaded
? message
[*ip
- 1].m_threadpos
> dot
->m_threadpos
560 mp
= message
+ *ip2
- 1;
561 if (!(mp
->m_flag
& MMNDEL
)) {
570 printf(_("No messages applicable\n"));
574 /* If this is the first command, select message 1. Note that this must
575 * exist for us to get here at all */
576 if (!(pstate
& PS_SAW_COMMAND
)) {
582 /* Just find the next good message after dot, no wraparound */
583 if (mb
.mb_threaded
== 0) {
584 for (mp
= dot
+ !!(pstate
& PS_DID_PRINT_DOT
);
585 PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
586 if (!(mp
->m_flag
& MMNORM
))
589 /* TODO The threading code had some bugs that caused crashes.
590 * TODO The last thing (before the deep look) happens here,
591 * TODO so let's not trust PS_DID_PRINT_DOT but check & hope it fixes */
592 if ((mp
= dot
) != NULL
&& (pstate
& PS_DID_PRINT_DOT
))
593 mp
= next_in_thread(mp
);
594 while (mp
!= NULL
&& (mp
->m_flag
& MMNORM
))
595 mp
= next_in_thread(mp
);
597 if (mp
== NULL
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
599 printf(_("At EOF\n"));
607 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
620 printf("%d\n", (int)PTR2SIZE(dot
- message
+ 1));
628 int *msgvec
= v
, *ip
, mesg
;
632 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
634 mp
= message
+ mesg
- 1;
635 printf("%d: ", mesg
);
636 if (mp
->m_xlines
> 0)
637 printf("%ld", mp
->m_xlines
);
640 printf("/%lu\n", (ul_i
)mp
->m_xsize
);
660 int list
[2], rv
= 0, *msgvec
= v
, lastdot
;
663 lastdot
= (int)PTR2SIZE(dot
- message
+ 1);
664 if (delm(msgvec
) >= 0) {
665 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
666 if (list
[0] > lastdot
) {
672 printf(_("At EOF\n"));
674 printf(_("No more messages\n"));
683 int *msgvec
= v
, *ip
;
687 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
689 mp
= message
+ *ip
- 1;
692 if (mp
->m_flag
& (MDELETED
| MSAVED
))
693 mp
->m_flag
&= ~(MDELETED
| MSAVED
);
695 mp
->m_flag
&= ~MDELETED
;
704 int *msgvec
= v
, *ip
;
707 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
708 setdot(message
+ *ip
- 1);
709 dot
->m_flag
|= MTOUCH
;
710 dot
->m_flag
&= ~MPRESERVE
;
711 pstate
|= PS_DID_PRINT_DOT
;
720 int *msgvec
= v
, *ip
;
723 if (pstate
& PS_EDIT
) {
724 n_err(_("`mbox' can only be used in a system mailbox\n")); /* TODO */
728 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
729 setdot(message
+ *ip
- 1);
730 dot
->m_flag
|= MTOUCH
| MBOX
;
731 dot
->m_flag
&= ~MPRESERVE
;
732 pstate
|= PS_DID_PRINT_DOT
;
742 int *msgvec
= v
, *ip
, mesg
, rv
= 1;
746 if (pstate
& PS_EDIT
) {
747 printf(_("Cannot `preserve' in a system mailbox\n"));
751 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
753 mp
= message
+ mesg
- 1;
754 mp
->m_flag
|= MPRESERVE
;
757 pstate
|= PS_DID_PRINT_DOT
;
768 int *msgvec
= v
, *ip
;
771 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
772 setdot(message
+ *ip
- 1);
773 dot
->m_flag
&= ~(MREAD
| MTOUCH
);
774 dot
->m_flag
|= MSTATUS
;
775 pstate
|= PS_DID_PRINT_DOT
;
784 int *msgvec
= v
, *ip
;
787 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
788 struct message
*mp
= message
+ *ip
- 1;
800 int *msgvec
= v
, *ip
;
803 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
804 m
= message
+ *ip
- 1;
806 if (!(m
->m_flag
& (MFLAG
| MFLAGGED
)))
807 m
->m_flag
|= MFLAG
| MFLAGGED
;
817 int *msgvec
= v
, *ip
;
820 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
821 m
= message
+ *ip
- 1;
823 if (m
->m_flag
& (MFLAG
| MFLAGGED
)) {
824 m
->m_flag
&= ~(MFLAG
| MFLAGGED
);
825 m
->m_flag
|= MUNFLAG
;
836 int *msgvec
= v
, *ip
;
839 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
840 m
= message
+ *ip
- 1;
842 if (!(m
->m_flag
& (MANSWER
| MANSWERED
)))
843 m
->m_flag
|= MANSWER
| MANSWERED
;
850 c_unanswered(void *v
)
853 int *msgvec
= v
, *ip
;
856 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
857 m
= message
+ *ip
- 1;
859 if (m
->m_flag
& (MANSWER
| MANSWERED
)) {
860 m
->m_flag
&= ~(MANSWER
| MANSWERED
);
861 m
->m_flag
|= MUNANSWER
;
872 int *msgvec
= v
, *ip
;
875 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
876 m
= message
+ *ip
- 1;
878 if (!(m
->m_flag
& (MDRAFT
| MDRAFTED
)))
879 m
->m_flag
|= MDRAFT
| MDRAFTED
;
889 int *msgvec
= v
, *ip
;
892 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
893 m
= message
+ *ip
- 1;
895 if (m
->m_flag
& (MDRAFT
| MDRAFTED
)) {
896 m
->m_flag
&= ~(MDRAFT
| MDRAFTED
);
897 m
->m_flag
|= MUNDRAFT
;