mbox-explode.c
3.02 KB
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
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <mailutils/debug.h>
#include <mailutils/errno.h>
#include <mailutils/list.h>
#include <mailutils/mailbox.h>
#include <mailutils/registrar.h>
#include <mailutils/stream.h>
#include <mailutils/message.h>
int
main (int argc, char **argv)
{
mailbox_t mbox;
size_t msgno;
size_t count = 0;
char *mbox_name = 0;
char *dir_name = 0;
int status;
int debug = 0;
if (strcmp("-d", argv[1]) == 0 && argc == 4)
{
debug = 1;
argc--;
argv[1] = argv[2];
argv[2] = argv[3];
}
if (argc != 3)
{
printf ("usage: mbox-explode <mbox> <directory>\n");
exit (0);
}
mbox_name = argv[1];
dir_name = argv[2];
/* Register the desire formats. */
{
list_t bookie;
registrar_get_list (&bookie);
list_append (bookie, path_record);
list_append (bookie, imap_record);
}
if ((status = mailbox_create_default (&mbox, mbox_name)) != 0)
{
fprintf (stderr, "could not create <%s>: %s\n",
mbox_name, mu_errstring (status));
exit (1);
}
if(debug)
{
mu_debug_t debug;
mailbox_get_debug (mbox, &debug);
mu_debug_set_level (debug, MU_DEBUG_TRACE | MU_DEBUG_PROT);
}
if ((status = mailbox_open (mbox, MU_STREAM_READ)) != 0)
{
fprintf (stderr, "could not open <%s>: %s\n",
mbox_name, mu_errstring (status));
exit (1);
}
mailbox_messages_count (mbox, &count);
if (mkdir (dir_name, 0777) != 0)
{
fprintf (stderr, "mkdir(%s) failed: %s\n", dir_name, strerror (errno));
mailbox_close (mbox);
exit (1);
}
for (msgno = 1; msgno <= count; ++msgno)
{
message_t msg = 0;
size_t nparts = 0;
size_t partno;
if ((status = mailbox_get_message (mbox, msgno, &msg)) != 0)
{
fprintf (stderr, "msg %d: get message failed: %s\n",
msgno, mu_errstring (status));
mailbox_close (mbox);
exit (1);
}
if ((status = message_get_num_parts (msg, &nparts)))
{
fprintf (stderr, "msg %d: get num parts failed: %s\n",
msgno, mu_errstring (status));
mailbox_close (mbox);
exit (1);
}
printf ("msg %03d: %02d parts\n", msgno, nparts);
for (partno = 1; partno <= nparts; partno++)
{
message_t part = 0;
char path[128];
sprintf (path, "%s/m%03d.p%02d", dir_name, msgno, partno);
printf ("msg %03d: part %02d > %s\n", msgno, partno, path);
if ((status = message_get_part (msg, partno, &part)))
{
fprintf (stderr, "msg %d: get part %d failed: %s\n",
msgno, partno, mu_errstring (status));
mailbox_close (mbox);
exit (1);
}
if ((status = message_save_attachment (part, path, 0)))
{
fprintf (stderr, "msg %d part %d: save failed: %s\n",
msgno, partno, mu_errstring (status));
break;
}
}
}
mailbox_close (mbox);
mailbox_destroy (&mbox);
return status;
}