Blame view

mail/print.c 3.1 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 2009, 2010 Free
   Software Foundation, Inc.
4

5
   GNU Mailutils is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 3, or (at your option)
8 9
   any later version.

10
   GNU Mailutils is distributed in the hope that it will be useful,
11 12 13 14 15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17 18 19 20 21 22

#include "mail.h"

/*
 * p[rint] [msglist]
 * t[ype] [msglist]
23 24
 * P[rint] [msglist]
 * T[ype] [msglist]
25 26
 */

27
static int
28
mail_print_msg (msgset_t *mspec, mu_message_t mesg, void *data)
29
{
30 31 32
  mu_header_t hdr;
  mu_body_t body;
  mu_stream_t stream;
33 34
  size_t lines = 0;
  mu_stream_t out;
35
  int pagelines = util_get_crt ();
36
  int status;
37
  
38
  mu_message_lines (mesg, &lines);
39 40 41
  if (mailvar_get (NULL, "showenvelope", mailvar_type_boolean, 0) == 0)
    lines++;
  
42 43 44 45 46
  /* If it is POP or IMAP the lines number is not known, so try
     to be smart about it.  */
  if (lines == 0)
    {
      if (pagelines)
47
	{
48 49
	  size_t col = (size_t)util_getcols ();
	  if (col)
50
	    {
51
	      size_t size = 0;
52
	      mu_message_size (mesg, &size);
53
	      lines =  size / col;
54 55
	    }
	}
56
    }
Alain Magloire authored
57

58
  out = open_pager (lines);
59

60 61 62
  if (mailvar_get (NULL, "showenvelope", mailvar_type_boolean, 0) == 0)
    print_envelope (mspec, mesg, "From");
  
63 64 65
  if (*(int *) data) /* print was called with a lowercase 'p' */
    {
      size_t i, num = 0;
66
      const char *sptr;
67 68
      char *tmp;
      
69
      mu_message_get_header (mesg, &hdr);
70
      mu_header_get_field_count (hdr, &num);
71

72 73
      for (i = 1; i <= num; i++)
	{
74 75 76
	  if (mu_header_sget_field_name (hdr, i, &sptr))
	    continue;
	  if (mail_header_is_visible (sptr))
77
	    {
78
	      mu_stream_printf (out, "%s: ", sptr);
79
	      mu_header_aget_field_value (hdr, i, &tmp);
80
	      if (mail_header_is_unfoldable (sptr))
81
		mu_string_unfold (tmp, NULL);
82
	      util_rfc2047_decode (&tmp);
83
	      mu_stream_printf (out, "%s\n", tmp);
84
	      free (tmp);
85
	    }
86
	}
87
      mu_stream_printf (out, "\n");
88
      mu_message_get_body (mesg, &body);
89
      status = mu_body_get_streamref (body, &stream);
90 91
    }
  else
92
    status = mu_message_get_streamref (mesg, &stream);
93

94 95 96
  if (status)
    {
      mu_error (_("get_stream error: %s"), mu_strerror (status));
97
      mu_stream_unref (out);
98 99 100
      return 0;
    }

101 102
  mu_stream_copy (out, stream, 0, NULL);
  /* FIXME:
103 104
      if (ml_got_interrupt())
	{
105
	  mu_error (_("\nInterrupt"));
106 107
	  break;
	}
108
  */
109
  mu_stream_destroy (&stream);
110
  mu_stream_destroy (&out);
111
  
112
  util_mark_read (mesg);
113

114
  set_cursor (mspec->msg_part[0]);
115 116 117
  
  return 0;
}
118

119 120 121
int
mail_print (int argc, char **argv)
{
122
  int lower = mu_islower (argv[0][0]);
123
  int rc = util_foreach_msg (argc, argv, MSG_NODELETED|MSG_SILENT,
124 125
			     mail_print_msg, &lower);
  return rc;
126
}
127