Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] Re: .forward file



Sven Simon wrote:

> I, therefore, would like to strip off the attachment and forward only 
> the text
> message to my mobile. I know we can pipe stuff to external commands from
> the .forward file, but how do I get rid of the attachments?

Try the following perl script:

------- begin MULTIPART-strip.pl ---------
#!/usr/bin/perl -w

use strict;
use MIME::Parser;
use MIME::Entity;
use MIME::QuotedPrint;

$|++;

my $envelope = <STDIN>;
my $parser = MIME::Parser->new;
$parser->output_to_core('ALL');

my $ent = $parser->read(\*STDIN);

sub getplainpart {
	my $ent = shift;
	if ($ent->is_multipart) {
		for (my $part = 0; $part < $ent->parts; $part++) {
			if ($ent->parts($part)->effective_type eq "text/plain") {
				return $ent->parts($part);
			}
			if ($ent->parts($part)->is_multipart) {
				return getplainpart($ent->parts($part));
			}
		}
	} else {
		if ($ent->effective_type eq "text/plain") {
			return $ent;
		} else {
			return MIME::Entity->build(
				Data => "[[ text/plain part is missing ]]\n",
				Encoding => '7bit',
				Type => 'text/plain');
		}
	}
}

my $plainpart = getplainpart($ent);

my $content_transfer_encoding = $plainpart->head->get('Content-Transfer-Encoding') || '7bit';
my $content_type = $plainpart->head->get('Content-Type') || 'text/plain';
my $body = $plainpart->body_as_string;

if ($content_transfer_encoding =~ /^quoted-printable/i) {
	$body = decode_qp($body);
	$content_transfer_encoding = '8bit';
}
my $newent;

if ($ent->is_multipart) {
	$newent = MIME::Entity->build(
		Data => $body . "\n\n[[ Multipart mail stripped ]]\n",
		Encoding => $content_transfer_encoding,
		Type => $content_type);
	$ent->parts([$newent]);
	$ent->head->add('X-Mailfilter', 'Multipart mail stripped');
	$ent->make_singlepart;
	$ent->sync_headers(Length => 'COMPUTE', Nonstandard => 'ERASE');
} else {
	$newent = MIME::Entity->build(
		Data => $body,
		Encoding => $content_transfer_encoding,
		Type => $content_type);
	$ent->make_multipart;
	$ent->parts([$newent]);
	$ent->make_singlepart;
	$ent->sync_headers(Length => 'COMPUTE', Nonstandard => 'ERASE');
}


# Netscape does not understand the "In-Reply-To" header when parsing.
# Add to References
my $inreplyto = $ent->head->get('In-Reply-To');
if(defined($inreplyto)) {
	my $reference = $ent->head->get('References');
	if(defined($reference)) {
		my @example.com = split(/ /, $reference);
		my $lastref = pop(@example.com);
		if ($lastref ne $inreplyto) {
			$ent->head->replace('References', $reference . ' ' . $inreplyto);
			$ent->head->add('X-References', 'Modified');
		}
	} else {
		$ent->head->add('References', $inreplyto);
		$ent->head->add('X-References', 'Modified');
	}
}

# print $envelope;
$ent->print;
------- end MULTIPART-strip.pl ---------

-- 
Tobias						PGP: http://9ac7e0bc.2ya.com
This mail is made of 100% recycled bits.
np: Yoko Kanno: Ghost in The Shell : Stand Alone Complex - be Human (VICL-61217 09 - spotter []


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links