1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3 
4    GNU Mailutils is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Mailutils is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include "mail.h"
18 
19 /*
20  * p[rint] [msglist]
21  * t[ype] [msglist]
22  * P[rint] [msglist]
23  * T[ype] [msglist]
24  */
25 
26 static int
mail_print_msg(msgset_t * mspec,mu_message_t mesg,void * data)27 mail_print_msg (msgset_t *mspec, mu_message_t mesg, void *data)
28 {
29   mu_header_t hdr;
30   mu_body_t body;
31   mu_stream_t stream;
32   size_t lines = 0;
33   mu_stream_t out;
34   int pagelines = util_get_crt ();
35   int status;
36 
37   mu_message_lines (mesg, &lines);
38   if (mailvar_is_true (mailvar_name_showenvelope))
39     lines++;
40 
41   /* If it is POP or IMAP the lines number is not known, so try
42      to be smart about it.  */
43   if (lines == 0)
44     {
45       if (pagelines)
46 	{
47 	  size_t col = (size_t) util_screen_columns ();
48 	  if (col)
49 	    {
50 	      size_t size = 0;
51 	      mu_message_size (mesg, &size);
52 	      lines =  size / col;
53 	    }
54 	}
55     }
56 
57   out = open_pager (lines);
58 
59   if (mailvar_is_true (mailvar_name_showenvelope))
60     print_stream_envelope (out, mspec, mesg, "From");
61 
62   if (*(int *) data) /* print was called with a lowercase 'p' */
63     {
64       size_t i, num = 0;
65       const char *sptr;
66       char *tmp;
67 
68       mu_message_get_header (mesg, &hdr);
69       mu_header_get_field_count (hdr, &num);
70 
71       for (i = 1; i <= num; i++)
72 	{
73 	  if (mu_header_sget_field_name (hdr, i, &sptr))
74 	    continue;
75 	  if (mail_header_is_visible (sptr))
76 	    {
77 	      mu_stream_printf (out, "%s: ", sptr);
78 	      mu_header_aget_field_value (hdr, i, &tmp);
79 	      if (mail_header_is_unfoldable (sptr))
80 		mu_string_unfold (tmp, NULL);
81 	      util_rfc2047_decode (&tmp);
82 	      mu_stream_printf (out, "%s\n", tmp);
83 	      free (tmp);
84 	    }
85 	}
86       mu_stream_printf (out, "\n");
87       mu_message_get_body (mesg, &body);
88       status = mu_body_get_streamref (body, &stream);
89     }
90   else
91     status = mu_message_get_streamref (mesg, &stream);
92 
93   if (status)
94     {
95       mu_error (_("get_stream error: %s"), mu_strerror (status));
96       mu_stream_unref (out);
97       return 0;
98     }
99 
100   mu_stream_copy (out, stream, 0, NULL);
101   /* FIXME:
102       if (ml_got_interrupt())
103 	{
104 	  mu_error (_("\nInterrupt"));
105 	  break;
106 	}
107   */
108   mu_stream_destroy (&stream);
109   mu_stream_destroy (&out);
110 
111   util_mark_read (mesg);
112 
113   set_cursor (msgset_msgno (mspec));
114 
115   return 0;
116 }
117 
118 int
mail_print(int argc,char ** argv)119 mail_print (int argc, char **argv)
120 {
121   int lower = mu_islower (argv[0][0]);
122   int rc = util_foreach_msg (argc, argv, MSG_NODELETED|MSG_SILENT,
123 			     mail_print_msg, &lower);
124   return rc;
125 }
126 
127