mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

commit 45ab6ea8f8d57e7d79f90fb06c7f00848605225d
parent 539a057a17aa44ba64a3cb3de9d3aad14628d2e3
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sun, 27 Jul 2014 18:48:23 +0200

+mail_summary

Diffstat:
mail_summary | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+), 0 deletions(-)

diff --git a/mail_summary b/mail_summary @@ -0,0 +1,63 @@ +#!/usr/bin/perl +# http://louis.jachiet.com/blog/?p=228 +# by Louis Jachiet, public domain + +# read mail on stdin, produce summary on stdout + +use MIME::Parser; +use URI::Escape; +use Encode qw(decode); + +# http://stackoverflow.com/a/4597964/414272 +sub trim { + (my $s = $_[0]) =~ s/^\s+|\s+$//g; + return $s; +} + +my $parser = new MIME::Parser; +$parser->output_under("/tmp"); +$entity = $parser->parse(\*STDIN) or die "parse failed\n"; +$subject = decode("MIME-Header",$entity->get('Subject')); +$from = decode("MIME-Header",$entity->get('From')); +$body = ""; + +sub explore_mail +{ + my $mpart = $_[0] ; + if( defined($mpart->parts()) && $mpart->parts() > 0) + { + foreach $int ($mpart->parts()) + { + if($int->head->mime_type eq"text/plain") + { + $path = $int->bodyhandle->path ; + open RUTF8, ("<".$path) or die $! ; + while (my $line = <RUTF8>) + { + $body = $body." ".$line ; + } + if($int->head->mime_type eq"multipart/alternative" ) + { + explore_mail ($int); + } + } + } + } + else + { + $body = $body.(join " ", @{$mpart->body}); + } +} + +explore_mail $entity ; + +if(!($body eq "")) +{ + $sms = "[". (trim($from)) ."] ". (trim($subject)) ." xxx ".$body ; + $sms =~ s/\n/ /g; + $sms =~ s/ / /g; + $sms =~ s/xxx/\n/g; + $sms = substr($sms,0,400); + print ($sms); +} +$entity->purge;