Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use File::Basename;
- use Net::IMAP;
- # Set this to the hostname of your IMAP server
- $IMAPSERVER = "skidsr.us";
- $mbox = "$ARGV[0]";
- if (!$mbox) { die "Usage: $0 mbox\n"; }
- chop ($whoami = `/usr/ucb/whoami`);
- if ($whoami eq "root" || $whoami eq "cyrus") {
- die "fkoff\n";
- }
- #
- # Main Code
- #
- # Log in to Cyrus IMAP server
- ($user, $pass) = GetLogin( );
- $imap = new Net::IMAP($IMAPSERVER, Synchronous => 1);
- $response = $imap->login($user, $pass);
- print "Login: ", $response->status, "-",
- $response->status_text, "\n";
- # cyrmailbox is the mailbox name on the Cyrus server
- $cyrmailbox = "user." . "$user." . basename($mbox);
- # Create the new mailbox. If the mailbox already exists, do not
- # allow its contents to be overwritten!
- $response = $imap->create($cyrmailbox);
- print "Create: ", $response->status, "-",
- $response->status_text, "\n";
- if ($response->status eq "NO") {
- print "Mailbox $cyrmailbox already exists on Cyrus server!\n";
- print "Rename your file and try again.\n";
- $response = $imap->logout( );
- print "Logout: ", $response->status, "-",
- $response->status_text, "\n";
- exit;
- }
- # Copy the mbox
- if (-s $mbox) {
- TransferMbox($imap, $cyrmailbox, $mbox);
- }
- # Disconnect from IMAP server
- $response = $imap->logout( );
- print "Logout: ", $response->status, "-",
- $response->status_text, "\n";
- #
- # Get username and password information
- #
- sub GetLogin {
- my ($username, $password);
- print "Enter your IMAP username: ";
- chop ($username = <STDIN>);
- system "stty -echo";
- print "Enter your IMAP password: ";
- chop ($password = <STDIN>);
- system "stty echo";
- print "\n";
- return ($username, $password);
- }
- #
- # Dump a Unix-style mbox file into a Cyrus folder
- #
- sub TransferMbox {
- my ($imap, $mailbox, $mboxfile) = @_;
- my $blank = 1;
- my $count = 0;
- my $message = "";
- my $response;
- print "Transferring $mboxfile...\n";
- open(MBOX, $mboxfile);
- while (<MBOX>) {
- if ($blank && /^From /) {
- if ($message) {
- chop $message; # Remove extra blank line before next From
- $response = $imap->append($mailbox, $message) if $count;
- $count++;
- }
- $message = "";
- }
- else {
- chop;
- s/$/\r\n/; # IMAP requires CR/LF on each line
- $message .= $_;
- }
- $blank = /^\r$/ ? 1 : 0;
- }
- $response = $imap->append($mailbox, $message) if $count;
- $count++;
- close(MBOX);
- print "Transferred $count messages from $mboxfile to $mailbox.\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement