/* $Id: split_mangled_mailbox.cc 2845 2007-12-02 13:43:57Z flaterco $ */ // g++ -Wall -Wextra -pedantic -O2 -s -o split_mangled_mailbox split_mangled_mailbox.cc -ldstr // For reasons unknown, mail processing system does this to the // envelope From: // // >From dave@fubar.com Sun Dec 02 00:11:06 2007 // Causing getmail to do this: // // From unknown Sat Dec 1 19:11:23 2007 // Return-Path: // Received: from mail.fubar.com by endpoint.nowhere.net // with IMAP4-SSL; 02 Dec 2007 00:11:23 -0000 // // >From dave@fubar.com Sun Dec 02 00:11:06 2007 // Mutt then makes it worse: // // From unknown Sat Dec 1 19:06:33 2007 // Return-Path: // Status: O // Content-Length: 1720 // Lines: 40 // // >From dave@fubar.com Sun Dec 02 00:01:18 2007 // This program recovers the original e-mail from a "standard" mbox // mailbox that has been the victim of such mangling. // Based on split_std_mailbox.cc 2442 2007-04-19 01:26:54Z flaterco #include #include #include #include #include #include #include FILE *newmsg (char *dest_dir) { static const unsigned bufsize = 1000; static unsigned long serialnum = 0; size_t l; char msgfilename[bufsize]; FILE *fp; struct stat stat_struct; l = strlen(dest_dir); assert (l > 0); assert (l + 80 < bufsize); strcpy (msgfilename, dest_dir); if (msgfilename[l-1] != '/') msgfilename[l++] = '/'; do sprintf (msgfilename+l, "msg%lu", serialnum++); while (stat (msgfilename, &stat_struct) == 0); if (!(fp = fopen (msgfilename, "w"))) { perror (msgfilename); fprintf (stderr, "Aborting run\n"); exit (-1); } return fp; } int main (int argc, char **argv) { FILE *mbox, *msg=NULL; Dstr inbuf; if (argc != 3) { fprintf (stderr, "Usage: split_mangled_mailbox mangled-mailbox-filename destination-directory\n"); exit (-1); } if (!(mbox = fopen (argv[1], "r"))) { perror (argv[1]); exit (-1); } while (!inbuf.getline(mbox).isNull()) { if (!strncmp (inbuf.aschar(), "From unknown ", 13)) { if (msg) fclose (msg); msg = newmsg (argv[2]); do { inbuf.getline(mbox); if (inbuf.isNull()) { fprintf (stderr, "Error: unexpected end of mbox file\n"); exit (-1); } } while (strncmp (inbuf.aschar(), ">From ", 6)); inbuf /= 1; } if (!msg) { fprintf (stderr, "Error: mbox file does not begin as expected.\n"); exit (-1); } fprintf (msg, "%s\n", inbuf.aschar()); } fclose (mbox); if (msg) fclose (msg); return 0; }