1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
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
38 #ifndef HAVE_AMALGAMATION
44 /* Prepare and print "[Message: xy]:" intro */
45 static void _show_msg_overview(FILE *obuf
, struct message
*mp
, int msg_no
);
47 /* ... And place the extracted date in `date' */
48 static void _parse_from_(struct message
*mp
, char date
[FROM_DATEBUF
]);
50 /* Print out the header of a specific message
51 * __hprf: handle *headline*
52 * __subject: return -1 if Subject: yet seen, otherwise smalloc()d Subject:
53 * __putindent: print out the indenting in threaded display */
54 static void _print_head(size_t yetprinted
, size_t msgno
, FILE *f
,
56 static void __hprf(size_t yetprinted
, char const *fmt
, size_t msgno
,
57 FILE *f
, bool_t threaded
, char const *attrlist
);
58 static char * __subject(struct message
*mp
, bool_t threaded
,
60 static int __putindent(FILE *fp
, struct message
*mp
, int maxwidth
);
62 static int _dispc(struct message
*mp
, char const *a
);
64 /* Shared `z' implementation */
65 static int a_cmd_scroll(char const *arg
, bool_t onlynew
);
67 /* Shared `headers' implementation */
68 static int _headers(int msgspec
);
70 /* Show the requested messages */
71 static int _type1(int *msgvec
, bool_t doign
, bool_t dopage
, bool_t dopipe
,
72 bool_t dodecode
, char *cmd
, ui64_t
*tstats
);
74 /* Pipe the requested messages */
75 static int _pipe1(char *str
, int doign
);
78 static int a_cmd_top(void *vp
, struct ignoretab
*itp
);
81 _show_msg_overview(FILE *obuf
, struct message
*mp
, int msg_no
)
83 char const *cpre
= "", *csuf
= "";
87 if (pstate
& PS_COLOUR_ACTIVE
) {
88 struct n_colour_pen
*cpen
;
90 if ((cpen
= n_colour_pen_create(n_COLOUR_ID_VIEW_MSGINFO
, NULL
)) != NULL
){
93 if ((sp
= n_colour_pen_to_str(cpen
)) != NULL
)
95 if ((sp
= n_colour_reset_to_str()) != NULL
)
100 fprintf(obuf
, _("%s[-- Message %2d -- %lu lines, %lu bytes --]:%s\n"),
101 cpre
, msg_no
, (ul_i
)mp
->m_lines
, (ul_i
)mp
->m_size
, csuf
);
106 _parse_from_(struct message
*mp
, char date
[FROM_DATEBUF
]) /* TODO line pool */
114 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) != NULL
&&
115 (hlen
= readline_restart(ibuf
, &hline
, &hsize
, 0)) > 0)
116 extract_date_from_from_(hline
, hlen
, date
);
123 _print_head(size_t yetprinted
, size_t msgno
, FILE *f
, bool_t threaded
)
126 char attrlist
[attrlen
+1], *cp
;
130 if ((cp
= ok_vlook(attrlist
)) != NULL
) {
131 if (strlen(cp
) == attrlen
) {
132 memcpy(attrlist
, cp
, attrlen
+1);
135 n_err(_("*attrlist* is not of the correct length, using builtin\n"));
138 if (ok_blook(bsdcompat
) || ok_blook(bsdflags
)) {
139 char const bsdattr
[attrlen
+1] = "NU *HMFAT+-$~";
140 memcpy(attrlist
, bsdattr
, sizeof bsdattr
);
141 } else if (ok_blook(SYSV3
)) {
142 char const bsdattr
[attrlen
+1] = "NU *HMFAT+-$~";
143 memcpy(attrlist
, bsdattr
, sizeof bsdattr
);
144 OBSOLETE(_("*SYSV3*: please use *bsdcompat* or *bsdflags*, "
145 "or set *attrlist*"));
147 char const pattr
[attrlen
+1] = "NUROSPMFAT+-$~";
148 memcpy(attrlist
, pattr
, sizeof pattr
);
152 if ((fmt
= ok_vlook(headline
)) == NULL
) {
153 fmt
= ((ok_blook(bsdcompat
) || ok_blook(bsdheadline
))
154 ? "%>%a%m %-20f %16d %3l/%-5o %i%-S"
155 : "%>%a%m %-18f %16d %4l/%-5o %i%-s");
158 __hprf(yetprinted
, fmt
, msgno
, f
, threaded
, attrlist
);
163 __hprf(size_t yetprinted
, char const *fmt
, size_t msgno
, FILE *f
,
164 bool_t threaded
, char const *attrlist
)
166 char buf
[16], datebuf
[FROM_DATEBUF
], cbuf
[8], *cp
, *subjline
;
167 char const *datefmt
, *date
, *name
, *fp
n_COLOUR( COMMA
*colo_tag
);
168 int i
, n
, s
, wleft
, subjlen
;
171 n_COLOUR( struct n_colour_pen
*cpen_new COMMA
*cpen_cur COMMA
*cpen_bas
; )
178 _LOOP_MASK
= (1<<4) - 1,
184 if ((mp
= message
+ msgno
- 1) == dot
)
188 n_COLOUR( colo_tag
= NULL
; )
190 datefmt
= ok_vlook(datefield
);
192 if (datefmt
!= NULL
) {
193 fp
= hfield1("date", mp
);/* TODO use m_date field! */
199 date
= fakedate(datet
);
200 fp
= ok_vlook(datefield_markout_older
);
201 i
= (*datefmt
!= '\0');
203 i
|= (*fp
!= '\0') ? 2 | 4 : 2; /* XXX no magics */
205 /* May we strftime(3)? */
207 memcpy(&time_current
.tc_local
, localtime(&datet
),
208 sizeof time_current
.tc_local
);
210 if ((i
& 2) && (datet
> time_current
.tc_time
+ DATE_SECSDAY
||
211 #define _6M ((DATE_DAYSYEAR / 2) * DATE_SECSDAY)
212 (datet
+ _6M
< time_current
.tc_time
))) {
214 if ((datefmt
= (i
& 4) ? fp
: NULL
) == NULL
) {
215 memset(datebuf
, ' ', FROM_DATEBUF
); /* xxx ur */
216 memcpy(datebuf
+ 4, date
+ 4, 7);
217 datebuf
[4 + 7] = ' ';
218 memcpy(datebuf
+ 4 + 7 + 1, date
+ 20, 4);
219 datebuf
[4 + 7 + 1 + 4] = '\0';
222 n_COLOUR( colo_tag
= n_COLOUR_TAG_SUM_OLDER
; )
223 } else if ((i
& 1) == 0)
225 } else if (datet
== (time_t)0 && !(mp
->m_flag
& MNOFROM
)) {
226 /* TODO eliminate this path, query the FROM_ date in setptr(),
227 * TODO all other codepaths do so by themselves ALREADY ?????
228 * TODO assert(mp->m_time != 0);, then
229 * TODO ALSO changes behaviour of datefield_markout_older */
230 _parse_from_(mp
, datebuf
);
233 date
= fakedate(datet
);
237 if (name
!= NULL
&& ok_blook(showto
) && is_myname(skin(name
))) {
238 if ((cp
= hfield1("to", mp
)) != NULL
) {
248 name
= ok_blook(showname
) ? realname(name
) : prstr(skin(name
));
252 /* Detect the width of the non-format characters in *headline*;
253 * like that we can simply use putc() in the next loop, since we have
254 * already calculated their column widths (TODO it's sick) */
255 wleft
= subjlen
= scrnwidth
;
257 for (fp
= fmt
; *fp
!= '\0'; ++fp
) {
263 if (digitchar(*fp
)) {
266 n
= 10*n
+ *fp
- '0';
267 while (++fp
, digitchar(*fp
));
277 if (mb_cur_max
> 1) {
279 if ((s
= mbtowc(&wc
, fp
, mb_cur_max
)) == -1)
281 else if ((n
= wcwidth(wc
)) == -1)
293 /* Walk *headline*, producing output TODO not (really) MB safe */
296 colo_tag
= n_COLOUR_TAG_SUM_DOT
;
297 cpen_bas
= n_colour_pen_create(n_COLOUR_ID_SUM_HEADER
, colo_tag
);
298 n_colour_pen_put(cpen_new
= cpen_cur
= cpen_bas
, f
);
301 for (fp
= fmt
; *fp
!= '\0'; ++fp
) {
304 if ((c
= *fp
& 0xFF) != '%') {
306 if ((cpen_new
= cpen_bas
) != cpen_cur
)
307 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
316 if ((c
= *++fp
) == '-') {
321 if (digitchar(*fp
)) {
323 n
= 10*n
+ *fp
- '0';
324 while (++fp
, digitchar(*fp
));
327 if ((c
= *fp
& 0xFF) == '\0')
337 if (flags
& _ISDOT
) {
338 n_COLOUR( cpen_new
= n_colour_pen_create(n_COLOUR_ID_SUM_DOTMARK
,
340 if (options
& OPT_UNICODE
) {
342 /* 25B8;BLACK RIGHT-POINTING SMALL TRIANGLE: â–¸ */
343 cbuf
[1] = (char)0x96, cbuf
[2] = (char)0xB8;
345 /* 25C2;BLACK LEFT-POINTING SMALL TRIANGLE: â—‚ */
346 cbuf
[1] = (char)0x97, cbuf
[2] = (char)0x82;
357 if (UICMP(32, ABS(n
), >, wleft
))
358 n
= (n
< 0) ? -wleft
: wleft
;
359 snprintf(buf
, sizeof buf
, "%u.%02u",
360 (mp
->m_spamscore
>> 8), (mp
->m_spamscore
& 0xFF));
361 n
= fprintf(f
, "%*s", n
, buf
);
362 wleft
= (n
>= 0) ? wleft
- n
: 0;
369 c
= _dispc(mp
, attrlist
);
372 if (cpen_new
== cpen_cur
)
374 if (cpen_new
!= cpen_cur
)
375 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
377 if (UICMP(32, ABS(n
), >, wleft
))
378 n
= (n
< 0) ? -wleft
: wleft
;
380 n
= fprintf(f
, "%*s", n
, cbuf
);
381 wleft
= (n
>= 0) ? wleft
- n
: 0;
383 if ((cpen_new
= cpen_bas
) != cpen_cur
)
384 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
388 if (datefmt
!= NULL
) {
389 i
= strftime(datebuf
, sizeof datebuf
, datefmt
,
390 &time_current
.tc_local
);
394 n_err(_("Ignored date format, it excesses the target "
395 "buffer (%lu bytes)\n"), (ul_i
)sizeof(datebuf
));
400 if (UICMP(32, ABS(n
), >, wleft
))
401 n
= (n
< 0) ? -wleft
: wleft
;
402 n
= fprintf(f
, "%*.*s", n
, n
, date
);
403 wleft
= (n
>= 0) ? wleft
- n
: 0;
408 if (UICMP(32, ABS(n
), >, wleft
))
409 n
= (n
< 0) ? -wleft
: wleft
;
410 n
= fprintf(f
, "%*u", n
, (threaded
== 1 ? mp
->m_level
: 0));
411 wleft
= (n
>= 0) ? wleft
- n
: 0;
422 n
= (n
< 0) ? -wleft
: wleft
;
424 if (flags
& _ISTO
) /* XXX tr()! */
426 n
= fprintf(f
, "%s%s", ((flags
& _ISTO
) ? "To " : ""),
427 colalign(name
, i
, n
, &wleft
));
430 else if (flags
& _ISTO
)
436 cpen_new
= n_colour_pen_create(n_COLOUR_ID_SUM_THREAD
, colo_tag
);
437 if (cpen_new
!= cpen_cur
)
438 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
440 n
= __putindent(f
, mp
, MIN(wleft
, scrnwidth
- 60));
441 wleft
= (n
>= 0) ? wleft
- n
: 0;
443 if ((cpen_new
= cpen_bas
) != cpen_cur
)
444 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
451 if (UICMP(32, ABS(n
), >, wleft
))
452 n
= (n
< 0) ? -wleft
: wleft
;
454 n
= fprintf(f
, "%*ld", n
, mp
->m_xlines
);
455 wleft
= (n
>= 0) ? wleft
- n
: 0;
467 for (i
= msgCount
; i
> 999; i
/= 10)
470 if (UICMP(32, ABS(n
), >, wleft
))
471 n
= (n
< 0) ? -wleft
: wleft
;
472 n
= fprintf(f
, "%*lu", n
, (ul_i
)msgno
);
473 wleft
= (n
>= 0) ? wleft
- n
: 0;
478 if (UICMP(32, ABS(n
), >, wleft
))
479 n
= (n
< 0) ? -wleft
: wleft
;
480 n
= fprintf(f
, "%*lu", n
, (ul_i
)mp
->m_xsize
);
481 wleft
= (n
>= 0) ? wleft
- n
: 0;
493 if (UICMP(32, ABS(n
), >, subjlen
))
494 n
= (n
< 0) ? -subjlen
: subjlen
;
496 n
-= (n
< 0) ? -2 : 2;
499 if (subjline
== NULL
)
500 subjline
= __subject(mp
, (threaded
&& (flags
& _IFMT
)), yetprinted
);
501 if (subjline
== (char*)-1) {
502 n
= fprintf(f
, "%*s", n
, "");
503 wleft
= (n
>= 0) ? wleft
- n
: 0;
505 n
= fprintf(f
, ((flags
& _SFMT
) ? "\"%s\"" : "%s"),
506 colalign(subjline
, ABS(n
), n
, &wleft
));
511 case 'T': { /* Message recipient flags */
512 /* We never can reuse "name" since it's the full name */
513 struct name
const *np
= lextract(hfield1("to", mp
), GTO
| GSKIN
);
517 for (; np
!= NULL
; np
= np
->n_flink
) {
518 switch (is_mlist(np
->n_name
, FAL0
)) {
519 case MLIST_SUBSCRIBED
: c
= 'S'; goto jputcb
;
520 case MLIST_KNOWN
: c
= 'L'; goto jputcb
;
528 np
= lextract(hfield1("cc", mp
), GCC
| GSKIN
);
535 for (i
= msgCount
; i
> 999; i
/= 10)
538 if (UICMP(32, ABS(n
), >, wleft
))
539 n
= (n
< 0) ? -wleft
: wleft
;
540 n
= fprintf(f
, "%*lu",
541 n
, (threaded
? (ul_i
)mp
->m_threadpos
: (ul_i
)msgno
));
542 wleft
= (n
>= 0) ? wleft
- n
: 0;
545 if (options
& OPT_D_V
)
546 n_err(_("Unkown *headline* format: %%%c\n"), c
);
560 if (subjline
!= NULL
&& subjline
!= (char*)-1)
566 __subject(struct message
*mp
, bool_t threaded
, size_t yetprinted
)
569 char *rv
= (char*)-1, *ms
;
572 if ((ms
= hfield1("subject", mp
)) == NULL
)
575 in
.l
= strlen(in
.s
= ms
);
576 mime_fromhdr(&in
, &out
, TD_ICONV
| TD_ISPR
);
579 if (!threaded
|| mp
->m_level
== 0)
582 /* In a display thread - check whether this message uses the same
583 * Subject: as it's parent or elder neighbour, suppress printing it if
584 * this is the case. To extend this a bit, ignore any leading Re: or
585 * Fwd: plus follow-up WS. Ignore invisible messages along the way */
586 ms
= subject_re_trim(ms
);
588 for (; (mp
= prev_in_thread(mp
)) != NULL
&& yetprinted
-- > 0;) {
591 if (visible(mp
) && (os
= hfield1("subject", mp
)) != NULL
) {
595 in
.l
= strlen(in
.s
= os
);
596 mime_fromhdr(&in
, &oout
, TD_ICONV
| TD_ISPR
);
597 x
= asccasecmp(ms
, subject_re_trim(oout
.s
));
613 __putindent(FILE *fp
, struct message
*mp
, int maxwidth
)/* XXX no magic consts */
616 int *us
, indlvl
, indw
, i
, important
= MNEW
| MFLAGGED
;
620 if (mp
->m_level
== 0 || maxwidth
== 0) {
625 cs
= ac_alloc(mp
->m_level
);
626 us
= ac_alloc(mp
->m_level
* sizeof *us
);
629 if (mp
->m_younger
&& UICMP(32, i
+ 1, ==, mp
->m_younger
->m_level
)) {
630 if (mp
->m_parent
&& mp
->m_parent
->m_flag
& important
)
631 us
[i
] = mp
->m_flag
& important
? 0x2523 : 0x2520;
633 us
[i
] = mp
->m_flag
& important
? 0x251D : 0x251C;
636 if (mp
->m_parent
&& mp
->m_parent
->m_flag
& important
)
637 us
[i
] = mp
->m_flag
& important
? 0x2517 : 0x2516;
639 us
[i
] = mp
->m_flag
& important
? 0x2515 : 0x2514;
644 for (i
= mp
->m_level
- 2; i
>= 0; --i
) {
646 if (UICMP(32, i
, >, mq
->m_level
- 1)) {
651 if (mq
->m_parent
&& (mq
->m_parent
->m_flag
& important
))
664 for (indlvl
= indw
= 0; (ui8_t
)indlvl
< mp
->m_level
&& indw
< maxwidth
;
666 if (indw
< maxwidth
- 1)
667 indw
+= (int)putuc(us
[indlvl
], cs
[indlvl
] & 0xFF, fp
);
669 indw
+= (int)putuc(0x21B8, '^', fp
);
671 indw
+= putuc(0x25B8, '>', fp
);
681 _dispc(struct message
*mp
, char const *a
)
686 if ((mp
->m_flag
& (MREAD
| MNEW
)) == MREAD
)
688 if ((mp
->m_flag
& (MREAD
| MNEW
)) == (MREAD
| MNEW
))
690 if (mp
->m_flag
& MANSWERED
)
692 if (mp
->m_flag
& MDRAFTED
)
694 if ((mp
->m_flag
& (MREAD
| MNEW
)) == MNEW
)
696 if (!(mp
->m_flag
& (MREAD
| MNEW
)))
698 if (mp
->m_flag
& MSPAM
)
700 if (mp
->m_flag
& MSPAMUNSURE
)
702 if (mp
->m_flag
& MSAVED
)
704 if (mp
->m_flag
& MPRESERVE
)
706 if (mp
->m_flag
& (MBOX
| MBOXED
))
708 if (mp
->m_flag
& MFLAGGED
)
710 if (mb
.mb_threaded
== 1 && mp
->m_collapsed
> 0)
712 if (mb
.mb_threaded
== 1 && mp
->m_collapsed
< 0)
719 a_cmd_scroll(char const *arg
, bool_t onlynew
){
723 int msgspec
, size
, maxs
;
726 /* TODO scroll problem: we do not know whether + and $ have already reached
727 * TODO the last screen in threaded mode */
728 msgspec
= onlynew
? -1 : 0;
730 if((maxs
= msgCount
/ size
) > 0 && msgCount
% size
== 0)
759 case '1': case '2': case '3': case '4': case '5':
760 case '6': case '7': case '8': case '9': case '0':
763 l
= strtol(arg
, &eptr
, 10);
766 if(l
> maxs
- (isabs
? 0 : _screen
))
768 _screen
= isabs
? (int)l
: _screen
+ l
;
774 printf(_("On last screenful of messages\n"));
783 l
= strtol(arg
, &eptr
, 10);
793 printf(_("On first screenful of messages\n"));
800 n_err(_("Unrecognized scrolling command: %s\n"), arg
);
805 size
= _headers(msgspec
);
812 _headers(int msgspec
) /* TODO rework v15 */
815 bool_t
volatile isrelax
;
816 ui32_t
volatile flag
;
817 int g
, k
, mesg
, size
;
818 int volatile lastg
= 1;
819 struct message
*mp
, *mq
, *lastmq
= NULL
;
820 enum mflag fl
= MNEW
| MFLAGGED
;
823 time_current_update(&time_current
, FAL0
);
827 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
835 if (options
& OPT_INTERACTIVE
)
836 n_colour_env_create(n_COLOUR_CTX_SUM
, FAL0
);
842 #if 0 /* FIXME original code path */
855 if (mb
.mb_threaded
== 0) {
858 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
862 if (mp
->m_flag
& fl
) {
866 if ((msgspec
> 0 && PTRCMP(mp
, ==, message
+ msgspec
- 1)) ||
867 (msgspec
== 0 && g
== k
) ||
868 (msgspec
== -2 && g
== k
+ size
&& lastmq
) ||
869 (msgspec
< 0 && g
>= k
&& (mp
->m_flag
& fl
) != 0))
873 if (lastmq
&& (msgspec
== -2 ||
874 (msgspec
== -1 && PTRCMP(mp
, ==, message
+ msgCount
)))) {
881 mesg
= (int)PTR2SIZE(mp
- message
);
882 if (PTRCMP(dot
, !=, message
+ msgspec
- 1)) { /* TODO really?? */
883 for (mq
= mp
; PTRCMP(mq
, <, message
+ msgCount
); ++mq
)
892 for (; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
896 if (UICMP(32, flag
++, >=, size
))
898 _print_head(0, mesg
, stdout
, 0);
903 } else { /* threaded */
906 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
908 (mp
->m_collapsed
<= 0 ||
909 PTRCMP(mp
, ==, message
+ msgspec
- 1))) {
912 if (mp
->m_flag
& fl
) {
916 if ((msgspec
> 0 && PTRCMP(mp
, ==, message
+ msgspec
- 1)) ||
917 (msgspec
== 0 && g
== k
) ||
918 (msgspec
== -2 && g
== k
+ size
&& lastmq
) ||
919 (msgspec
< 0 && g
>= k
&& (mp
->m_flag
& fl
) != 0))
923 if (lastmq
&& (msgspec
== -2 ||
924 (msgspec
== -1 && PTRCMP(mp
, ==, message
+ msgCount
)))) {
930 if (PTRCMP(dot
, !=, message
+ msgspec
- 1)) { /* TODO really?? */
931 for (mq
= mp
; mq
; mq
= next_in_thread(mq
))
932 if (visible(mq
) && mq
->m_collapsed
<= 0) {
942 (mp
->m_collapsed
<= 0 ||
943 PTRCMP(mp
, ==, message
+ msgspec
- 1))) {
944 if (UICMP(32, flag
++, >=, size
))
946 _print_head(flag
- 1, PTR2SIZE(mp
- message
+ 1), stdout
,
950 mp
= next_in_thread(mp
);
957 printf(_("No more mail.\n"));
958 if (pstate
& (PS_HOOK_MASK
| PS_ROBOT
))
962 n_sigman_cleanup_ping(&sm
);
966 n_COLOUR( n_colour_env_gut((sm
.sm_signo
!= SIGPIPE
) ? stdout
: NULL
); )
968 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
973 _type1(int *msgvec
, bool_t doign
, bool_t dopage
, bool_t dopipe
,
974 bool_t dodecode
, char *cmd
, ui64_t
*tstats
)
982 FILE * volatile obuf
;
983 bool_t
volatile isrelax
= FAL0
;
986 enum sendaction
const action
= ((dopipe
&& ok_blook(piperaw
))
987 ? SEND_MBOX
: dodecode
989 ? SEND_TODISP
: SEND_TODISP_ALL
);
990 bool_t
const volatile formfeed
= (dopipe
&& ok_blook(page
));
993 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
1001 if ((obuf
= Popen(cmd
, "w", ok_vlook(SHELL
), NULL
, 1)) == NULL
) {
1005 } else if ((options
& OPT_TTYOUT
) && (dopage
||
1006 ((options
& OPT_INTERACTIVE
) && (cp
= ok_vlook(crt
)) != NULL
))) {
1010 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
1011 mp
= message
+ *ip
- 1;
1012 if (!(mp
->m_have
& HAVE_BODY
))
1013 if (get_body(mp
) != OKAY
)
1014 goto jcleanup_leave
;
1015 nlines
+= mp
->m_lines
+ 1; /* Message info XXX and PARTS... */
1019 /* >= not <: we return to the prompt */
1020 if (dopage
|| UICMP(z
, nlines
, >=,
1021 (*cp
!= '\0' ? strtoul(cp
, NULL
, 0) : (size_t)realscreenheight
))) {
1022 if ((obuf
= n_pager_open()) == NULL
)
1026 if ((options
& OPT_INTERACTIVE
) &&
1027 (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
1028 action
== SEND_SHOW
))
1029 n_colour_env_create(n_COLOUR_CTX_VIEW
, obuf
!= stdout
);
1033 else if ((options
& OPT_INTERACTIVE
) &&
1034 (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
))
1035 n_colour_env_create(n_COLOUR_CTX_VIEW
, FAL0
);
1038 /*TODO unless we have our signal manager special care must be taken */
1041 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
1042 mp
= message
+ *ip
- 1;
1045 pstate
|= PS_DID_PRINT_DOT
;
1047 if (!dopipe
&& ip
!= msgvec
)
1048 fprintf(obuf
, "\n");
1049 if (action
!= SEND_MBOX
)
1050 _show_msg_overview(obuf
, mp
, *ip
);
1051 sendmp(mp
, obuf
, (doign
? ignore
: NULL
), NULL
, action
, mstats
);
1053 if (formfeed
) /* TODO a nicer way to separate piped messages! */
1056 tstats
[0] += mstats
[0];
1063 n_sigman_cleanup_ping(&sm
);
1067 n_COLOUR( n_colour_env_gut((sm
.sm_signo
!= SIGPIPE
) ? obuf
: NULL
); )
1069 n_pager_close(obuf
);
1072 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
1077 _pipe1(char *str
, int doign
)
1080 char const *cmd
, *cmdq
;
1081 int *msgvec
, rv
= 1;
1085 if ((cmd
= laststring(str
, &needs_list
, TRU1
)) == NULL
) {
1086 cmd
= ok_vlook(cmd
);
1087 if (cmd
== NULL
|| *cmd
== '\0') {
1088 n_err(_("Variable *cmd* not set\n"));
1093 msgvec
= salloc((msgCount
+ 2) * sizeof *msgvec
);
1096 *msgvec
= first(0, MMNORM
);
1098 if (pstate
& (PS_HOOK_MASK
| PS_ROBOT
)) {
1102 puts(_("No messages to pipe."));
1106 } else if (getmsglist(str
, msgvec
, 0) < 0)
1109 if (pstate
& (PS_HOOK_MASK
| PS_ROBOT
)) {
1113 printf("No applicable messages.\n");
1117 cmdq
= n_shexp_quote_cp(cmd
, FAL0
);
1118 printf(_("Pipe to: %s\n"), cmdq
);
1120 if ((rv
= _type1(msgvec
, doign
, FAL0
, TRU1
, FAL0
, UNCONST(cmd
), stats
)) == 0)
1121 printf("%s %" PRIu64
" bytes\n", cmdq
, stats
[0]);
1128 a_cmd_top(void *vp
, struct ignoretab
*itp
){
1131 enum{a_NONE
, a_SQUEEZE
= 1u<<0,
1132 a_EMPTY
= 1u<<8, a_STOP
= 1u<<9, a_WORKMASK
= 0xFF00u
} f
;
1133 size_t tmax
, plines
;
1137 if((iobuf
= Ftmp(NULL
, "topio", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1138 n_perr(_("`top': I/O temporary file"), 0);
1142 if((pbuf
= Ftmp(NULL
, "toppag", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1143 n_perr(_("`top': temporary pager file"), 0);
1148 /* TODO In v15 we should query the m_message object, and directly send only
1149 * TODO those parts, optionally over empty-line-squeeze and quote-strip
1150 * TODO filters, in which we are interested in: only text content!
1151 * TODO And: with *topsqueeze*, header/content separating empty line.. */
1152 pstate
&= ~PS_MSGLIST_DIRECT
; /* TODO NO ATTACHMENTS */
1156 if (options
& OPT_INTERACTIVE
)
1157 n_colour_env_create(n_COLOUR_CTX_VIEW
, TRU1
);
1159 n_string_creat_auto(&s
);
1163 if((l
= strtol(ok_vlook(toplines
), NULL
, 0)) <= 0){
1164 tmax
= (size_t)screensize();
1172 f
= ok_blook(topsqueeze
) ? a_SQUEEZE
: a_NONE
;
1174 for(ip
= msgvec
= vp
;
1175 *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
){
1178 mp
= &message
[*ip
- 1];
1181 pstate
|= PS_DID_PRINT_DOT
;
1185 if(ftruncate(fileno(iobuf
), 0)){
1186 n_perr(_("`top': ftruncate(2)"), 0);
1190 if(sendmp(mp
, iobuf
, itp
, NULL
, SEND_TODISP_ALL
, NULL
) < 0){
1191 n_err(_("`top': failed to prepare message %d\n"), *ip
);
1195 fflush_rewind(iobuf
);
1197 _show_msg_overview(pbuf
, mp
, *ip
);
1202 n_string_trunc(&s
, 0);
1203 for(l
= 0, f
&= ~a_WORKMASK
; !(f
& a_STOP
);){
1206 if((c
= getc(iobuf
)) == EOF
){
1212 n_string_push_c(&s
, c
);
1213 else if((f
& a_SQUEEZE
) && s
.s_len
== 0){
1214 if(!(f
& a_STOP
) && ((f
& a_EMPTY
) || tmax
- 1 <= l
))
1216 if(putc('\n', pbuf
) == EOF
){
1223 char const *cp
, *xcp
;
1225 cp
= n_string_cp_const(&s
);
1226 /* TODO Brute simple skip part overviews; see above.. */
1227 if(!(f
& a_SQUEEZE
))
1229 else if(s
.s_len
> 8 &&
1230 (xcp
= strstr(cp
, "[-- ")) != NULL
&&
1231 strstr(&xcp
[1], " --]") != NULL
)
1233 else for(; (c
= *cp
) != '\0'; ++cp
){
1236 if(!blankspacechar(c
)){
1245 if(fputs(n_string_cp_const(&s
), pbuf
) == EOF
||
1246 putc('\n', pbuf
) == EOF
){
1255 n_string_trunc(&s
, 0);
1263 if(!(f
& a_EMPTY
) && putc('\n', pbuf
) == EOF
){
1273 n_COLOUR( n_colour_env_gut(pbuf
); )
1276 page_or_print(pbuf
, plines
);
1283 return (vp
!= NULL
);
1287 c_cmdnotsupp(void *v
) /* TODO -> lex.c */
1291 n_err(_("The requested feature is not compiled in\n"));
1302 rv
= print_header_group((int*)v
);
1308 print_header_group(int *vector
)
1313 assert(vector
!= NULL
&& vector
!= (void*)-1);
1314 rv
= _headers(vector
[0]);
1325 rv
= a_cmd_scroll(v
, FAL0
);
1336 rv
= a_cmd_scroll(v
, TRU1
);
1345 int *msgvec
= v
, *ip
, n
;
1347 FILE * volatile obuf
;
1348 bool_t
volatile isrelax
;
1351 time_current_update(&time_current
, FAL0
);
1355 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
1362 if (options
& OPT_INTERACTIVE
) {
1363 if ((cp
= ok_vlook(crt
)) != NULL
) {
1364 for (n
= 0, ip
= msgvec
; *ip
!= 0; ++ip
)
1366 if (UICMP(z
, n
, >, (*cp
== '\0'
1367 ? (size_t)screensize() : strtoul(cp
, NULL
, 0)) + 3) &&
1368 (obuf
= n_pager_open()) == NULL
)
1371 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_SUM
, obuf
!= stdout
); )
1374 /* Update dot before display so that the dotmark etc. are correct */
1375 for (ip
= msgvec
; *ip
!= 0; ++ip
)
1378 setdot(message
+ *ip
- 1);
1382 for (n
= 0, ip
= msgvec
; *ip
!= 0; ++ip
) { /* TODO join into _print_head() */
1383 _print_head((size_t)n
++, (size_t)*ip
, obuf
, mb
.mb_threaded
);
1389 n_sigman_cleanup_ping(&sm
);
1393 n_COLOUR( n_colour_env_gut((sm
.sm_signo
!= SIGPIPE
) ? obuf
: NULL
); )
1395 n_pager_close(obuf
);
1397 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
1402 print_headers(size_t bottom
, size_t topx
, bool_t only_marked
)
1405 bool_t
volatile isrelax
;
1409 time_current_update(&time_current
, FAL0
);
1412 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
1420 if (options
& OPT_INTERACTIVE
)
1421 n_colour_env_create(n_COLOUR_CTX_SUM
, FAL0
);
1426 for (printed
= 0; bottom
<= topx
; ++bottom
) {
1427 struct message
*mp
= message
+ bottom
- 1;
1429 if (!(mp
->m_flag
& MMARK
))
1431 } else if (!visible(mp
))
1433 _print_head(printed
++, bottom
, stdout
, FAL0
);
1439 n_sigman_cleanup_ping(&sm
);
1443 n_COLOUR( n_colour_env_gut((sm
.sm_signo
!= SIGPIPE
) ? stdout
: NULL
); )
1445 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
1453 printf("%d\n", (int)PTR2SIZE(dot
- message
+ 1));
1461 int *msgvec
= v
, rv
;
1464 rv
= _type1(msgvec
, TRU1
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
1472 int *msgvec
= v
, rv
;
1475 rv
= _type1(msgvec
, FAL0
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
1483 int *msgvec
= v
, rv
;
1486 rv
= _type1(msgvec
, TRU1
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
1494 int *msgvec
= v
, rv
;
1497 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
1505 int *msgvec
= v
, rv
;
1508 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, TRU1
, NULL
, NULL
);
1520 rv
= _pipe1(str
, 1);
1532 rv
= _pipe1(str
, 0);
1539 struct ignoretab it
[2];
1543 n_ignoretab_creat(&it
[0], TRU1
);
1544 n_ignoretab_creat(&it
[1], TRU1
);
1545 n_ignoretab_insert(&it
[1], "from", sizeof("from") -1);
1546 n_ignoretab_insert(&it
[1], "to", sizeof("to") -1);
1547 n_ignoretab_insert(&it
[1], "cc", sizeof("cc") -1);
1548 n_ignoretab_insert(&it
[1], "subject", sizeof("subject") -1);
1550 rv
= !a_cmd_top(v
, it
);
1560 rv
= !a_cmd_top(v
, ignore
);
1575 if(*(argv
= v
) != NULL
){
1576 if((cp
= fexpand(*argv
, FEXP_NSHELL
| FEXP_LOCAL
)) == NULL
)
1579 cp
= folder_query();
1581 rv
= run_command(ok_vlook(LISTER
), 0, COMMAND_FD_PASS
, COMMAND_FD_PASS
, cp
,