Commit 5c0eb492 5c0eb492308b309e85128ffb806e623e3f012feb by Sam Roberts

Made so it now saves all the attachments from all the messages in

a folder. Also GNU indented.
1 parent d609eea4
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
......@@ -11,21 +12,31 @@
#include <unistd.h>
#include <mailutils/mailbox.h>
#include <mailutils/address.h>
#include <mailutils/registrar.h>
#include <mailutils/parse822.h>
int
main(int argc, char **argv)
main (int argc, char **argv)
{
mailbox_t mbox;
size_t i;
size_t msgno;
size_t count = 0;
char *mailbox_name;
char *mbox_name = 0;
char *dir_name = 0;
int status;
mailbox_name = (argc > 1) ? argv[0] : "+dbuild.details";
if (argc != 3)
{
printf ("usage: mbox-explode <mbox> <directory>\n");
exit (0);
}
mbox_name = argv[1];
dir_name = argv[2];
if (mkdir (dir_name, 0777) != 0)
{
fprintf (stderr, "mkdir(%s) failed: %s\n", dir_name, strerror (errno));
exit (1);
}
/* Register the desire formats. */
{
list_t bookie;
......@@ -33,136 +44,72 @@ main(int argc, char **argv)
list_append (bookie, path_record);
}
if ((status = mailbox_create_default (&mbox, mailbox_name)) != 0)
{
fprintf (stderr, "could not create <%s>: %s\n",
mailbox_name, strerror (status));
exit (1);
}
if ((status = mailbox_create_default (&mbox, mbox_name)) != 0)
{
fprintf (stderr, "could not create <%s>: %s\n",
mbox_name, strerror (status));
exit (1);
}
{
debug_t debug;
mailbox_get_debug (mbox, &debug);
// debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT);
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",
mailbox_name, strerror (status));
exit (1);
}
mailbox_messages_count (mbox, &count);
for (i = 1; i <= count; ++i)
{
message_t msg;
header_t hdr;
char subj[128];
char date[128];
size_t len = 0;
if (
(status = mailbox_get_message (mbox, i, &msg)) != 0 ||
(status = message_get_header (msg, &hdr)) != 0)
{
fprintf (stderr, "msg %d : %s\n", i, strerror(status));
exit(2);
}
if (
(status = header_get_value (
hdr, MU_HEADER_SUBJECT, subj, sizeof (subj), &len)) != 0 ||
(status = header_get_value (
hdr, MU_HEADER_DATE, date, sizeof (date), &len)) != 0)
{
fprintf (stderr, "msg %d : No Subject|Date\n", i);
continue;
fprintf (stderr, "could not open <%s>: %s\n",
mbox_name, strerror (status));
exit (1);
}
mailbox_messages_count (mbox, &count);
if (strcasecmp(subj, "WTLS 1.0 Daily Build-details") == 0)
for (msgno = 1; msgno <= count; ++msgno)
{
const char* s = date;
struct tm tm;
char dir[] = "yyyy.mm.dd";
size_t nparts = 0;
size_t partno;
if((status = parse822_date_time(&s, s + strlen(s), &tm)))
{
fprintf (stderr, "parsing <%s> failed: %s\n", date, strerror(status));
exit(1);
}
printf ("%d Processing for: year %d month %d day %d\n",
i, tm.tm_year + 1900, tm.tm_mon, tm.tm_mday);
snprintf(dir, sizeof(dir), "%d.%02d.%02d",
tm.tm_year + 1900, tm.tm_mon, tm.tm_mday);
status = mkdir(dir, 0777);
if(status != 0)
{
switch(errno)
{
case EEXIST: /* we've already done this message */
continue;
case 0:
break;
default:
fprintf (stderr, "mkdir %s failed: %s\n", dir, strerror(errno));
status = 1;
goto END;
break;
}
}
if((status = message_get_num_parts(msg, &nparts))) {
fprintf (stderr, "get num parts failed: %s\n", strerror(status));
break;
}
for(partno = 1; partno <= nparts; partno++)
{
message_t part = NULL;
char content[128] = "<not found>";
char* fname = NULL;
char path[PATH_MAX];
if((status = message_get_part(msg, partno, &part))) {
fprintf (stderr, "get part failed: %s\n", strerror(status));
break;
}
message_get_header (part, &hdr);
header_get_value (hdr, MU_HEADER_CONTENT_DISPOSITION,
content, sizeof (content), &len);
fname = strrchr(content, '"');
if(fname)
{
*fname = 0;
fname = strchr(content, '"') + 1;
snprintf(path, sizeof(path), "%s/%s", dir, fname);
printf(" filename %s\n", path);
if((status = message_save_attachment(part, path, NULL))) {
fprintf (stderr, "save attachment failed: %s\n", strerror(status));
break;
}
}
}
status = 0;
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, strerror (status));
exit (2);
}
if ((status = message_get_num_parts (msg, &nparts)))
{
fprintf (stderr, "msg %d: get num parts failed: %s\n",
msgno, strerror (status));
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, strerror (status));
exit (1);
}
if ((status = message_save_attachment (part, path, 0)))
{
fprintf (stderr, "msg %d part %d: save failed: %s\n",
msgno, partno, strerror (status));
break;
}
}
}
}
END:
mailbox_close (mbox);
mailbox_destroy (&mbox);
return status;
}
......