#!/usr/bin/perl -w

## This script can be used to post to your movabletype weblog using e-mail with your
## mobile or e-mail software.
## 
## hints to start with
## 1. add username to /etc/mail/aliases (or /etc/aliases) ie. ireceivethemail: "|sms.pl"
##
## 2. create with ln -s a symbolic link from the location of the sms.pl 
## to /usr/adm/sm.bin/ otherwise sendmail cannot use this script when receiving mail.
##
## this program is based largely on perl code from 
## http://www.bitwaste.com/wasted-bits/archives/000114.html
## by Raffi Krikorian <r@bitwaste.com>
##
## Many thanks to Marcus Mauller http://home.earthlink.net/~mmauller/
##
## Marcus added and changed code.
## Now it is is possible to post from a mobile and using  mailpackages 
## like Eudora Outlook or Mozilla's Thunderbird.
##
## I needed changes in the script cause the The mobile modified 
## the from address so that it was not possible to
## use authentication correctly.
## 
## little code was added and changed by myself (a wonder cause I do not speak Perl)
## $myself = Dennis Slagers http://www.aroundmyroom.com/blog
## 
## This script can be found at http://www.aroundmyroom.com/scripts/sms.txt
##
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.

use Mail::Header; # use this to parse out the e-mail header
use Mail::Internet; # and to parse out the entire mail message
use XMLRPC::Lite; # use this to call the blogger API
use strict;

################################################################################

## use the URL to use mt-xmlrpc.cgi ##
my $blogURL = "http://www.aroundmyroom.com/mt/";

## Check the weblogID with ie. rsd.xml (MetaWeblog, Blogger)
my $blogID = 0; # the ID of the blog to post to

## the username of the person to post with
my $blogUsername = "username to post to weblog";

## the password to the blog [you need to enter the password in the subject!!]
my $blogPassword; 

## just define the firstname, lastname and e-mail if exist when sending mail
my $firstname;
my $lastname;
my $email;

## what goes before and after the post
my $prepost = "";
my $postpost = "<p/>";

## the addresses that are allowed to post to this blog
my @authAddress = ( "e-mail1\@domain.com", "e-mail2\@domain2.com" );

################################################################################

## parse the mail and pull out the header
my $mail = new Mail::Internet \*STDIN;
my $header = $mail->head();


## Special code to check stuff from e-mail pkg or mobile phone
my $from = $header->get( "From", 0 );
chomp $from;

## mobile flavor: "=?ISO-8859-1?Q?firstname_lastname?=" <e-mail2@domain2.com>
if ($from =~ m/ISO-8859/) {
    ($lastname, $email) = split / /, $from;
} else { # regular email flavor: firstname lastname <e-mail1@domain.com>
    ($firstname, $lastname, $email) = split / /, $from;
}
$email =~ s/[<>]//g;
$from = $email;
my %is_authAddress;
for( @authAddress ) { $is_authAddress{$_} = 1; }
die "$from not in list" if not exists $is_authAddress{$from}; 

for( @authAddress ) { $is_authAddress{$_} = 1; }
exit 0 if( !$is_authAddress{$from} ); 

# # pull out the password, which is in the subject
$blogPassword = $header->get( "Subject", 0 );
chomp $blogPassword;

## from my mobile the password is messed up with ?ISO-8859-1? stuff remove it
$blogPassword =~ s/^=\?ISO-8859-1\?Q\?//;
$blogPassword =~ s/\?=$//;
$blogPassword =~ s/\s//;

## Content part ##
## Start with password in subject 
## Start Content with subject followed by a . to start content
##
## ie.
## subject: password
## content: 
## this is the subject.
## after the subject the content is entered as in the Entry Body
##
##
my $content = $mail->body();
$content = join " \n", @$content;
( my $subject, $content ) = ( $content =~ /^(.*?)\.\s*(.*?)$/s );
$content = $prepost . $content . $postpost;

## Post the message to the weblog using mt-xmlrpc.cgi script

## form the XML-RPC message
$blogURL .= "mt-xmlrpc.cgi";

## the response mail
my $mail_header_response = new Mail::Header;

eval {
## post the blog
    my $xml = new XMLRPC::Lite;
    my $status = $xml->proxy( $blogURL )->call( "metaWeblog.newPost", $blogID, $blogUsername, $blogPassword, { "title" => $subject, "description" => $content }, 1 );
    
## compose a response e-mail to say that we posted the stuff
    if( $status ) {
	$mail_header_response->add( "Subject", "blog entry posted" );

    } else {
	$mail_header_response->add( "Subject", "blog entry failed" );
	
    }

};
if( $@ ) {
    $mail_header_response->add( "Subject", "problem posting blog entry" );

}
my $mail_response = new Mail::Internet( "Header" => $mail_header_response );
$mail_response->smtpsend( "To", $from );