#!/usr/bin/perl -w

# re-bounce message from mbox
#
# Dobrica Pavlinusic <dpavlin@rot13.org>

use strict;

use Email::Folder::Mbox;
use Email::MIME::Attachment::Stripper;
use Email::Send;
use Data::Dumper;

my $mbox = shift || die "usage: $0 mbox_with_bounces\n";
my $smtp_server = shift || 'dmz1.pliva.hr';

my $box = new Email::Folder::Mbox($mbox) || die "can't open folder $mbox: $!";

print "working on $mbox\n";

while ( my $mail = $box->next_message ) {

	my $stripper = Email::MIME::Attachment::Stripper->new($mail);

	if (! $stripper) {
	 	warn "WARNING: can't parse message:\n$mail\n";
		next;
	}

	foreach my $att ( $stripper->attachments ) {
		if ($att->{content_type} eq 'message/rfc822') {
			#print $att->{payload},"\n", '-' x 76, "\n";
			send SMTP => $att->{payload}, $smtp_server;
		}
	}

}
