summaryrefslogtreecommitdiff
path: root/examples/mimetest.c
blob: fba093ab73994dbc7ab09adb1f1acc9129c4c7c2 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   This program 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 2, or (at your option)
   any later version.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#include <sys/types.h>

#include <mailutils/mailutils.h>

void message_display_parts(message_t msg, int indent);

char from[256];
char subject[256];
int print_attachments;
int indent_level = 4;

void
print_file (const char *fname, int indent)
{
  char buf[128];
  FILE *fp = fopen (fname, "r");

  if (!fp)
    {
      fprintf (stderr, "can't open file %s: %s", fname, strerror (errno));
      return;
    }

  while (fgets (buf, sizeof buf, fp))
    printf ("%*.*s%s", indent, indent, "", buf);
  fclose (fp);
  unlink (fname);
}

int
main (int argc, char **argv)
{
  mailbox_t mbox = NULL;
  int ret;
  size_t i;
  size_t count = 0;
  char *mailbox_name;
  int debug = 0;

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

  mailbox_name = argv[i];

  /* Registration.  */
  {
    list_t bookie;
    registrar_get_list (&bookie);
    list_append (bookie, file_record);
    list_append (bookie, imap_record);
    list_append (bookie, mbox_record);
    list_append (bookie, path_record);
    list_append (bookie, pop_record);
  }

  if ((ret = mailbox_create_default (&mbox, mailbox_name)) != 0)
    {
      fprintf (stderr, "could not create - %s\n", mu_errstring (ret));
      exit (2);
    }

  /* Debugging trace. */
  if (debug)
    {
      mu_debug_t debug;
      mailbox_get_debug (mbox, &debug);
      mu_debug_set_level (debug, MU_DEBUG_TRACE | MU_DEBUG_PROT);
    }

  /* Open the mailbox for reading only.  */
  if ((ret = mailbox_open (mbox, MU_STREAM_RDWR)) != 0)
    {
      fprintf (stderr, "mailbox open - %s\n", mu_errstring (ret));
      exit (2);
    }

  /* Iterate through the entire message set.  */
  mailbox_messages_count (mbox, &count);

  for (i = 1; i <= count; ++i)
    {
      message_t msg;
      header_t hdr;
      size_t nparts;
      size_t msize;

      if ((ret = mailbox_get_message (mbox, i, &msg)) != 0)
	{
	  fprintf (stderr, "mailbox_get_message - %s\n", mu_errstring (ret));
	  exit (2);
	}
      if ((ret = message_size (msg, &msize)) != 0)
	{
	  fprintf (stderr, "message_size - %s\n", mu_errstring (ret));
	  exit (2);
	}
      if ((ret = message_get_header (msg, &hdr)) != 0)
	{
	  fprintf (stderr, "message_get_header - %s\n", mu_errstring (ret));
	  exit (2);
	}
      header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL);
      header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject),
			NULL);
      printf ("Message: %d\n", i);
      printf ("From: %s\n", from);
      printf ("Subject: %s\n", subject);

      if ((ret = message_get_num_parts (msg, &nparts)) != 0)
	{
	  fprintf (stderr, "message_get_num_parts - %s\n", mu_errstring (ret));
	  exit (2);
	}
      printf ("Number of parts in message - %d\n", nparts);
      printf ("Total message size - %d\n", msize);
      message_display_parts (msg, 0);
    }
  mailbox_close (mbox);
  mailbox_destroy (&mbox);
  return 0;
}

char buf[2048];

void
message_display_parts (message_t msg, int indent)
{
  int ret, j;
  size_t msize, nparts, nsubparts;
  message_t part;
  header_t hdr;
  char type[256];
  char encoding[256];
  stream_t str;
  body_t body;
  int offset, ismulti;
  size_t nbytes;

  /* How many parts does the message has? */
  if ((ret = message_get_num_parts (msg, &nparts)) != 0)
    {
      fprintf (stderr, "message_get_num_parts - %s\n", mu_errstring (ret));
      exit (2);
    }

  /* 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 (j = 1; j <= nparts; j++)
    {
      if ((ret = message_get_part (msg, j, &part)) != 0)
	{
	  fprintf (stderr, "mime_get_part - %s\n", mu_errstring (ret));
	  exit (2);
	}
      if ((ret = message_size (part, &msize)) != 0)
	{
	  fprintf (stderr, "message_size - %s\n", mu_errstring (ret));
	  exit (2);
	}
      if ((ret = message_get_header (part, &hdr)) != 0)
	{
	  fprintf (stderr, "message_get_header - %s\n", mu_errstring (ret));
	  exit (2);
	}
      header_get_value (hdr, MU_HEADER_CONTENT_TYPE, type, sizeof (type),
			NULL);
      printf ("%*.*sType of part %d = %s\n", indent, indent, "", j, type);
      printf ("%*.*sMessage part size - %d\n", indent, indent, "", msize);
      encoding[0] = '\0';
      header_get_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, encoding,
			sizeof (encoding), NULL);
      ismulti = 0;
      if ((type[0]
	   && strncasecmp (type, "message/rfc822", strlen (type)) == 0)
	  || (message_is_multipart (part, &ismulti) == 0 && ismulti))
	{
	  if (!ismulti)
	    {
	      ret = message_unencapsulate (part, &part, NULL);
	      if (ret != 0)
		fprintf (stderr, "message_unencapsulate - %s\n",
			 mu_errstring (ret));
	      break;
	    }
	  if ((ret = message_get_header (part, &hdr)) != 0)
	    {
	      fprintf (stderr, "message_get_header - %s\n",
		       mu_errstring (ret));
	      exit (2);
	    }
	  header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL);
	  header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject),
			    NULL);
	  printf ("%*.*sEncapsulated message : %s\t%s\n",
		  indent, indent, "", from, subject);
	  printf ("%*.*sBegin\n", indent, indent, "");
	  if ((ret = message_get_num_parts (part, &nsubparts)) != 0)
	    {
	      fprintf (stderr, "mime_get_num_parts - %s\n",
		       mu_errstring (ret));
	      exit (2);
	    }
	  message_display_parts (part, indent+indent_level);
	  message_destroy (&part, NULL);
	}
      else if (type[0] == '\0'
	       || (strncasecmp (type, "text/plain", strlen ("text/plain")) ==
		   0)
	       || (strncasecmp (type, "text/html", strlen ("text/html")) ==
		   0))
	{
	  printf ("%*.*sText Message\n", indent, indent, "");
	  printf ("%*.*sBegin\n", indent, indent, "");
	  message_get_body (part, &body);
	  body_get_stream (body, &str);
	  filter_create (&str, str, encoding, 0, 0);
	  offset = 0;
	  while (stream_readline (str, buf, sizeof (buf), offset, &nbytes) ==
		 0 && nbytes)
	    {
	      printf ("%*.*s%s", indent, indent, "", buf);
	      offset += nbytes;
	    }
	  stream_destroy (&str, NULL);
	}
      else
	{
	  /* Save the attachements.  */
	  char *fname = NULL;
	  message_aget_attachment_name (part, &fname);
	  if (fname == NULL)
	    fname = mu_tempname (NULL);

	  printf ("%*.*sAttachment - saving [%s]\n", indent, indent, "",
		  fname);
	  printf ("%*.*sBegin\n", indent, indent, "");
	  /*FIXME: What is the 'data' argument for? */
	  message_save_attachment (part, NULL, NULL);
	  if (print_attachments)
	    print_file (fname, indent);
	  free (fname);
	}
      printf ("\n%*.*sEnd\n", indent, indent, "");
    }
}

Return to:

Send suggestions and report system problems to the System administrator.