summaryrefslogtreecommitdiff
path: root/examples/cpp/mimetest.cc
blob: d97ffe39dbb88afc6e5ce3482356fa0f971591d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2009-2012, 2014-2015 Free Software Foundation, Inc.

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

   GNU Mailutils is distributed in the hope that it will be useful,
   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
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <mailutils/cpp/mailutils.h>

using namespace std;
using namespace mailutils;

void message_display_parts (Message& msg, int indent);

bool print_attachments = false;
int indent_level = 4;

void
print_file (const std::string& fname, int indent)
{
  char buf[128];
  FILE *fp = fopen (fname.c_str (), "r");

  if (!fp)
    {
      cerr << "can't open file " << fname << ": " << strerror (errno) << endl;
      return;
    }

  while (fgets (buf, sizeof buf, fp))
    cout << setw (indent) << setfill (' ') << buf;
  fclose (fp);
  unlink (fname.c_str ());
}

int
main (int argc, char **argv)
{
  int i = 0;
  bool debug = false;

  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], "-d") == 0)
        debug = true;
      else if (strcmp (argv[i], "-p") == 0)
        print_attachments = true;
      else if (strcmp (argv[i], "-i") == 0)
        indent_level = strtoul (argv[++i], NULL, 0);
      else
        break;
    }

  /* Registration. */
  registrar_record (mu_imap_record);
  registrar_record (mu_pop_record);
  registrar_record (mu_mbox_record);
  registrar_set_default_record (mu_mbox_record);

  MailboxDefault mbox (argv[i]);
  
  /* Debugging trace. */
  if (debug)
    {
      mu_debug_set_category_level (MU_DEBCAT_MAILBOX, 
				   MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT));
    }

  /* Open the mailbox for reading only. */
  mbox.open ();

  /* Iterate through the entire message set. */
  size_t count = mbox.messages_count ();

  for (size_t i = 1; i <= count; ++i)
  {
    Message msg = mbox.get_message (i);
    Header hdr = msg.get_header ();

    cout << "Message: " << i << endl;
    cout << "From: " << hdr[MU_HEADER_FROM] << endl;
    cout << "Subject: " << hdr.get_value (MU_HEADER_SUBJECT, "[none]") << endl;
    cout << "Number of parts in message - " << msg.get_num_parts () << endl;
    cout << "Total message size - "
	 << msg.size () << "/" << msg.lines () << endl;

    try {
      message_display_parts (msg, 0);
    }
    catch (Exception& e)
    {
      cerr << e.method () << ": " << e.what () << endl;
    }
  }

  mbox.close ();
  return 0;
}

static void
print_message_part_sizes (Message& part, int indent)
{
  Header hdr = part.get_header ();
  Body body = part.get_body ();

  cout << setw (indent) << setfill (' ') << "Message part size - ";
  cout << part.size () << "/" << part.lines () << ": "
       << hdr.size  () << "/" << hdr.lines  () << ", "
       << body.size () << "/" << body.lines () << endl;
}
  
void
message_display_parts (Message& msg, int indent)
{
  /* How many parts does the message has? */
  size_t nparts = msg.get_num_parts ();

  /* Iterate through all the parts.  Treat type "message/rfc822"
     differently, since it is a message of its own that can have other
     subparts(recursive). */
  for (int j = 1; j <= nparts; j++)
    {
      Message part = msg.get_part (j);
      Header hdr = part.get_header ();

      string type ("text/plain");
      string encoding ("7bit");
      try {
	type = hdr[MU_HEADER_CONTENT_TYPE];
	encoding = hdr[MU_HEADER_CONTENT_TRANSFER_ENCODING];
      }
      catch (Exception& e)
      {
	if (e.status () != MU_ERR_NOENT) {
	  cerr << e.method () << ": " << e.what () << endl;
	  exit (1);
	}
      }

      cout << setw (indent) << setfill (' ')
	   << "Type of part " << j << " = " << type << endl;

      print_message_part_sizes (part, indent);

      bool ismulti = part.is_multipart ();
      if (type == "message/rfc822" || ismulti)
        {
          if (!ismulti)
	    part = part.unencapsulate ();

	  Header hdr = part.get_header ();
	  string from = hdr[MU_HEADER_FROM];
	  string subject = hdr.get_value (MU_HEADER_SUBJECT, "[none]");

	  cout << setw (indent) << setfill (' ')
	       << "Encapsulated message : " << from << "\t" << subject << endl;
	  cout << setw (indent) << setfill (' ') << "Begin" << endl;

	  size_t nsubparts = part.get_num_parts ();
          message_display_parts (part, indent + indent_level);
        }
      else if (strncasecmp (type.c_str (), "text/plain",
			    strlen ("text/plain")) == 0 ||
               strncasecmp (type.c_str (), "text/html",
			    strlen ("text/html")) == 0 ||
	       type.empty ())
        {
	  cout << setw (indent) << setfill (' ') << "Text Message" << endl;
	  cout << setw (indent) << setfill (' ') << "Begin" << endl;

	  Body body = part.get_body ();
	  Stream stream = body.get_stream ();

	  FilterStream filter (stream, encoding, 0, 0);
	  char buf[2048];

          while (filter.readline (buf, sizeof (buf)) == 0 &&
		 filter.get_read_count ())
            {
	      cout << setw (indent) << setfill (' ') << buf;
            }
        }
      else
        {
          /* Save the attachements. */
	  string fname;
	  try {
	    fname = part.get_attachment_name ();
	  }
	  catch (Exception& e) {
	    fname = mailutils::tempname ();
	  }
	  cout << setw (indent) << setfill (' ')
	       << "Attachment - saving " << "[" << fname << "]" << endl;
	  cout << setw (indent) << setfill (' ') << "Begin" << endl;

          part.save_attachment ();
          if (print_attachments)
            print_file (fname, indent);
        }
      cout << endl << setw (indent) << setfill (' ') << "End" << endl;
    }
}

Return to:

Send suggestions and report system problems to the System administrator.