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 */
80 rv
= (fprintf(obuf
, _("%s[-- Message %2d -- %lu lines, %lu bytes --]:%s\n"),
81 cpre
, msg_no
, (ul_i
)mp
->m_lines
, (ul_i
)mp
->m_size
, csuf
) > 0);
87 _type1(int *msgvec
, bool_t doign
, bool_t dopage
, bool_t dopipe
,
88 bool_t donotdecode
, char *cmd
, ui64_t
*tstats
)
94 enum sendaction action
;
95 bool_t
volatile formfeed
;
102 formfeed
= (dopipe
&& ok_blook(page
));
103 action
= ((dopipe
&& ok_blook(piperaw
))
104 ? SEND_MBOX
: donotdecode
106 ? SEND_TODISP
: SEND_TODISP_ALL
);
109 if ((obuf
= Popen(cmd
, "w", ok_vlook(SHELL
), NULL
, 1)) == NULL
) {
113 } else if ((n_psonce
& n_PSO_TTYOUT
) && (dopage
||
114 ((n_psonce
& n_PSO_INTERACTIVE
) && (cp
= ok_vlook(crt
)) != NULL
))) {
120 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
121 mp
= message
+ *ip
- 1;
122 if (!(mp
->m_content_info
& CI_HAVE_BODY
))
123 if (get_body(mp
) != OKAY
)
125 nlines
+= mp
->m_lines
+ 1; /* TODO BUT wire format, not display! */
129 /* >= not <: we return to the prompt */
130 if(dopage
|| nlines
>= (*cp
!= '\0'
131 ? (n_idec_uiz_cp(&lib
, cp
, 0, NULL
), lib
)
132 : (uiz_t
)n_realscreenheight
)){
133 if((obuf
= n_pager_open()) == NULL
)
137 if(action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
)
138 n_colour_env_create(n_COLOUR_CTX_VIEW
, obuf
, obuf
!= n_stdout
);
142 else if(action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
)
143 n_colour_env_create(n_COLOUR_CTX_VIEW
, n_stdout
, FAL0
);
148 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
149 mp
= message
+ *ip
- 1;
152 n_pstate
|= n_PS_DID_PRINT_DOT
;
154 if(!dopipe
&& ip
!= msgvec
&& fprintf(obuf
, "\n") < 0){
158 if(action
!= SEND_MBOX
&& !a_cmsg_show_overview(obuf
, mp
, *ip
)){
162 if(sendmp(mp
, obuf
, (doign
? n_IGNORE_TYPE
: NULL
), NULL
, action
, mstats
168 if(formfeed
){ /* TODO a nicer way to separate piped messages! */
169 if(putc('\f', obuf
) == EOF
){
175 tstats
[0] += mstats
[0];
179 if(!dopipe
&& (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
))
183 if (obuf
!= n_stdout
)
190 _pipe1(char *str
, int doign
)
193 char const *cmd
, *cmdq
;
198 if ((cmd
= laststring(str
, &needs_list
, TRU1
)) == NULL
) {
200 if (cmd
== NULL
|| *cmd
== '\0') {
201 n_err(_("Variable *cmd* not set\n"));
206 msgvec
= salloc((msgCount
+ 2) * sizeof *msgvec
);
209 *msgvec
= first(0, MMNORM
);
211 if (n_pstate
& (n_PS_ROBOT
| n_PS_HOOK_MASK
)) {
215 fputs(_("No messages to pipe.\n"), n_stdout
);
219 } else if (getmsglist(str
, msgvec
, 0) < 0)
222 if (n_pstate
& (n_PS_ROBOT
| n_PS_HOOK_MASK
)) {
226 fprintf(n_stdout
, "No applicable messages.\n");
230 cmdq
= n_shexp_quote_cp(cmd
, FAL0
);
231 fprintf(n_stdout
, _("Pipe to: %s\n"), cmdq
);
233 if ((rv
= _type1(msgvec
, doign
, FAL0
, TRU1
, FAL0
, n_UNCONST(cmd
), stats
)
235 fprintf(n_stdout
, "%s %" PRIu64
" bytes\n", cmdq
, stats
[0]);
242 a_cmsg_top(void *vp
, struct n_ignore
const *itp
){
245 enum{a_NONE
, a_SQUEEZE
= 1u<<0,
246 a_EMPTY
= 1u<<8, a_STOP
= 1u<<9, a_WORKMASK
= 0xFF00u
} f
;
251 if((iobuf
= Ftmp(NULL
, "topio", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
252 n_perr(_("`top': I/O temporary file"), 0);
256 if((pbuf
= Ftmp(NULL
, "toppag", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
257 n_perr(_("`top': temporary pager file"), 0);
262 /* TODO In v15 we should query the m_message object, and directly send only
263 * TODO those parts, optionally over empty-line-squeeze and quote-strip
264 * TODO filters, in which we are interested in: only text content!
265 * TODO And: with *topsqueeze*, header/content separating empty line.. */
266 n_pstate
&= ~n_PS_MSGLIST_DIRECT
; /* TODO NO ATTACHMENTS */
269 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_VIEW
, iobuf
, FAL0
); )
270 n_string_creat_auto(&s
);
274 if((n_idec_siz_cp(&l
, ok_vlook(toplines
), 0, NULL
275 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
276 ) != n_IDEC_STATE_CONSUMED
)
279 tmax
= n_screensize();
287 f
= ok_blook(topsqueeze
) ? a_SQUEEZE
: a_NONE
;
289 for(ip
= msgvec
= vp
;
290 *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
){
293 mp
= &message
[*ip
- 1];
296 n_pstate
|= n_PS_DID_PRINT_DOT
;
300 if(ftruncate(fileno(iobuf
), 0)){
301 n_perr(_("`top': ftruncate(2)"), 0);
306 if(!a_cmsg_show_overview(iobuf
, mp
, *ip
) ||
307 sendmp(mp
, iobuf
, itp
, NULL
, SEND_TODISP_ALL
, NULL
) < 0){
308 n_err(_("`top': failed to prepare message %d\n"), *ip
);
312 fflush_rewind(iobuf
);
314 /* TODO Skip over the _msg_overview line -- this is a hack to make
315 * TODO colours work: colour contexts should be objects */
319 if((c
= getc(iobuf
)) == EOF
|| putc(c
, pbuf
) == EOF
){
332 n_string_trunc(&s
, 0);
333 for(l
= 0, f
&= ~a_WORKMASK
; !(f
& a_STOP
);){
336 if((c
= getc(iobuf
)) == EOF
){
342 n_string_push_c(&s
, c
);
343 else if((f
& a_SQUEEZE
) && s
.s_len
== 0){
344 if(!(f
& a_STOP
) && ((f
& a_EMPTY
) || tmax
- 1 <= l
))
346 if(putc('\n', pbuf
) == EOF
){
353 char const *cp
, *xcp
;
355 cp
= n_string_cp_const(&s
);
356 /* TODO Brute simple skip part overviews; see above.. */
359 else if(s
.s_len
> 8 &&
360 (xcp
= strstr(cp
, "[-- ")) != NULL
&&
361 strstr(&xcp
[1], " --]") != NULL
)
366 for(qcp
= ok_vlook(quote_chars
); (c
= *cp
) != '\0'; ++cp
){
369 if(!blankspacechar(c
)){
370 if(strchr(qcp
, c
) == NULL
)
379 if(fputs(n_string_cp_const(&s
), pbuf
) == EOF
||
380 putc('\n', pbuf
) == EOF
){
389 n_string_trunc(&s
, 0);
397 if(!(f
& a_EMPTY
) && putc('\n', pbuf
) == EOF
){
407 n_COLOUR( n_colour_env_gut(); )
410 page_or_print(pbuf
, plines
);
424 int rv
= -1, *ip
, last
;
428 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
429 mp
= message
+ *ip
- 1;
431 mp
->m_flag
|= MDELETED
| MTOUCH
;
432 mp
->m_flag
&= ~(MPRESERVE
| MSAVED
| MBOX
);
436 setdot(message
+ last
- 1);
437 last
= first(0, MDELETED
);
439 setdot(message
+ last
- 1);
455 rv
= _type1(msgvec
, TRU1
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
466 rv
= _type1(msgvec
, FAL0
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
477 rv
= _type1(msgvec
, TRU1
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
488 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
499 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, TRU1
, NULL
, NULL
);
505 c_mimeview(void *vp
){ /* TODO direct addressable parts, multiple such */
510 if((msgvec
= vp
)[1] != 0){
511 n_err(_("`mimeview': can yet only take one message, sorry!\n"));/* TODO */
512 n_pstate_err_no
= n_ERR_NOTSUP
;
517 mp
= &message
[*msgvec
- 1];
520 n_pstate
|= n_PS_DID_PRINT_DOT
;
524 n_colour_env_create(n_COLOUR_CTX_VIEW
, n_stdout
, FAL0
);
527 if(!a_cmsg_show_overview(n_stdout
, mp
, *msgvec
))
528 n_pstate_err_no
= n_ERR_IO
;
529 else if(sendmp(mp
, n_stdout
, n_IGNORE_TYPE
, NULL
, SEND_TODISP_PARTS
,
531 n_pstate_err_no
= n_ERR_IO
;
533 n_pstate_err_no
= n_ERR_NONE
;
539 rv
= (n_pstate_err_no
!= n_ERR_NONE
);
571 struct n_ignore
*itp
;
575 if(n_ignore_is_any(n_IGNORE_TOP
))
578 itp
= n_ignore_new(TRU1
);
579 n_ignore_insert(itp
, TRU1
, "from", sizeof("from") -1);
580 n_ignore_insert(itp
, TRU1
, "to", sizeof("to") -1);
581 n_ignore_insert(itp
, TRU1
, "cc", sizeof("cc") -1);
582 n_ignore_insert(itp
, TRU1
, "subject", sizeof("subject") -1);
585 rv
= !a_cmsg_top(v
, itp
);
595 rv
= !a_cmsg_top(v
, n_IGNORE_TYPE
);
603 int list
[2], *ip
, *ip2
, mdot
, *msgvec
= v
, rv
= 1;
608 /* If some messages were supplied, find the first applicable one
609 * following dot using wrap around */
610 mdot
= (int)PTR2SIZE(dot
- message
+ 1);
612 /* Find first message in supplied message list which follows dot */
613 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
614 if ((mb
.mb_threaded
? message
[*ip
- 1].m_threadpos
> dot
->m_threadpos
622 mp
= message
+ *ip2
- 1;
623 if (!(mp
->m_flag
& MMNDEL
)) {
632 fprintf(n_stdout
, _("No messages applicable\n"));
636 /* If this is the first command, select message 1. Note that this must
637 * exist for us to get here at all */
638 if (!(n_pstate
& n_PS_SAW_COMMAND
)) {
644 /* Just find the next good message after dot, no wraparound */
645 if (mb
.mb_threaded
== 0) {
646 for (mp
= dot
+ !!(n_pstate
& n_PS_DID_PRINT_DOT
);
647 PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
648 if (!(mp
->m_flag
& MMNORM
))
651 /* TODO The threading code had some bugs that caused crashes.
652 * TODO The last thing (before the deep look) happens here,
653 * TODO so let's not trust n_PS_DID_PRINT_DOT but check & hope it fixes */
654 if ((mp
= dot
) != NULL
&& (n_pstate
& n_PS_DID_PRINT_DOT
))
655 mp
= next_in_thread(mp
);
656 while (mp
!= NULL
&& (mp
->m_flag
& MMNORM
))
657 mp
= next_in_thread(mp
);
659 if (mp
== NULL
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
661 fprintf(n_stdout
, _("At EOF\n"));
669 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
682 fprintf(n_stdout
, "%d\n", (int)PTR2SIZE(dot
- message
+ 1));
690 int *msgvec
= v
, *ip
, mesg
;
694 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
696 mp
= message
+ mesg
- 1;
697 fprintf(n_stdout
, "%d: ", mesg
);
698 if (mp
->m_xlines
> 0)
699 fprintf(n_stdout
, "%ld", mp
->m_xlines
);
702 fprintf(n_stdout
, "/%lu\n", (ul_i
)mp
->m_xsize
);
722 int list
[2], rv
= 0, *msgvec
= v
, lastdot
;
725 lastdot
= (int)PTR2SIZE(dot
- message
+ 1);
726 if (delm(msgvec
) >= 0) {
727 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
728 if (list
[0] > lastdot
) {
734 fprintf(n_stdout
, _("At EOF\n"));
736 fprintf(n_stdout
, _("No more messages\n"));
745 int *msgvec
= v
, *ip
;
749 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
751 mp
= &message
[*ip
- 1];
754 if (mp
->m_flag
& (MDELETED
| MSAVED
))
755 mp
->m_flag
&= ~(MDELETED
| MSAVED
);
757 mp
->m_flag
&= ~MDELETED
;
759 if (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)
760 imap_undelete(mp
, *ip
);
770 int *msgvec
= v
, *ip
;
773 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
774 setdot(message
+ *ip
- 1);
775 dot
->m_flag
|= MTOUCH
;
776 dot
->m_flag
&= ~MPRESERVE
;
777 n_pstate
|= n_PS_DID_PRINT_DOT
;
786 int *msgvec
= v
, *ip
;
789 if (n_pstate
& n_PS_EDIT
) {
790 n_err(_("`mbox' can only be used in a system mailbox\n")); /* TODO */
794 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
795 setdot(message
+ *ip
- 1);
796 dot
->m_flag
|= MTOUCH
| MBOX
;
797 dot
->m_flag
&= ~MPRESERVE
;
798 n_pstate
|= n_PS_DID_PRINT_DOT
;
808 int *msgvec
= v
, *ip
, mesg
, rv
= 1;
812 if (n_pstate
& n_PS_EDIT
) {
813 fprintf(n_stdout
, _("Cannot `preserve' in a system mailbox\n"));
817 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
819 mp
= message
+ mesg
- 1;
820 mp
->m_flag
|= MPRESERVE
;
823 n_pstate
|= n_PS_DID_PRINT_DOT
;
835 int *msgvec
= v
, *ip
;
838 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
839 mp
= &message
[*ip
- 1];
841 dot
->m_flag
&= ~(MREAD
| MTOUCH
);
842 dot
->m_flag
|= MSTATUS
;
844 if (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)
845 imap_unread(mp
, *ip
); /* TODO return? */
847 n_pstate
|= n_PS_DID_PRINT_DOT
;
856 int *msgvec
= v
, *ip
;
859 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
860 struct message
*mp
= message
+ *ip
- 1;
872 int *msgvec
= v
, *ip
;
875 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
876 m
= message
+ *ip
- 1;
878 if (!(m
->m_flag
& (MFLAG
| MFLAGGED
)))
879 m
->m_flag
|= MFLAG
| MFLAGGED
;
889 int *msgvec
= v
, *ip
;
892 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
893 m
= message
+ *ip
- 1;
895 if (m
->m_flag
& (MFLAG
| MFLAGGED
)) {
896 m
->m_flag
&= ~(MFLAG
| MFLAGGED
);
897 m
->m_flag
|= MUNFLAG
;
908 int *msgvec
= v
, *ip
;
911 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
912 m
= message
+ *ip
- 1;
914 if (!(m
->m_flag
& (MANSWER
| MANSWERED
)))
915 m
->m_flag
|= MANSWER
| MANSWERED
;
922 c_unanswered(void *v
)
925 int *msgvec
= v
, *ip
;
928 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
929 m
= message
+ *ip
- 1;
931 if (m
->m_flag
& (MANSWER
| MANSWERED
)) {
932 m
->m_flag
&= ~(MANSWER
| MANSWERED
);
933 m
->m_flag
|= MUNANSWER
;
944 int *msgvec
= v
, *ip
;
947 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
948 m
= message
+ *ip
- 1;
950 if (!(m
->m_flag
& (MDRAFT
| MDRAFTED
)))
951 m
->m_flag
|= MDRAFT
| MDRAFTED
;
961 int *msgvec
= v
, *ip
;
964 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
965 m
= message
+ *ip
- 1;
967 if (m
->m_flag
& (MDRAFT
| MDRAFTED
)) {
968 m
->m_flag
&= ~(MDRAFT
| MDRAFTED
);
969 m
->m_flag
|= MUNDRAFT
;