Created
November 2, 2012 12:50
-
-
Save managementboy/4001198 to your computer and use it in GitHub Desktop.
This program converts cyrus style mail directories into mbox files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# cyrus2mbox.pl | |
################## | |
# This program converts cyrus style mail directories into mbox files. | |
# | |
# Written by Jason Burgess of HolosTek, Inc. | |
# | |
# This program is provided under the "I don't really care what you do with it, | |
# but I'm not going to support it" license. | |
# | |
# Usage: cyrus2mbox.pl username | |
# Note: Be sure to change the $cyruspath variable to fit your system. | |
use Date::Format; | |
my $user = shift; | |
##### | |
# Change this line to reflect your path to the cryus imap mailbox directory | |
my $cyruspath = "/var/spool/imap/user/$user/"; | |
my $outfile = "$user"; | |
opendir(DIR, $cyruspath); | |
my @filelist = readdir(DIR); | |
closedir(DIR); | |
open(OUTFILE, "> $outfile"); | |
foreach $infile (@filelist) | |
{ | |
my $foundit = true; | |
if (($infile =~ /.*?\.$/) && ($infile ne '..') && ($infile ne '.')) | |
{ | |
print "\nChecking $infile..."; | |
my $from = ""; | |
# Get From: address from file | |
open(INFILE, "<$cyruspath$infile"); | |
while () | |
{ | |
chomp; | |
s/[\x0D\x0A]//g; | |
if (/^From\:\ (.*)$/) | |
{ | |
if ($foundit) | |
{ | |
print $_ . "\t"; | |
$from = $1; | |
$foundit = 0; | |
} | |
} | |
} | |
close(INFILE); | |
# Get Date from file information. | |
my @fileinfo = stat($cyruspath . $infile); | |
my $date = time2str("%a %b %e %T %Y", $fileinfo[10]); | |
# Write header. | |
print OUTFILE sprintf("From %s %s\n", $from, $date); | |
# Write email to file | |
open(INFILE, "<$cyruspath$infile"); | |
while () | |
{ | |
s/[\x0D\x0A]//g; | |
print OUTFILE "$_\n"; | |
} | |
close(INFILE); | |
# Print a blank line between messages | |
print OUTFILE "\n"; | |
} | |
} | |
close(OUTFILE); | |
print "\n\nDone\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment