#!/usr/bin/perl
# Name: mail2mt-newpost.pl
# Version: 01-30
# Copyright: 2006.01.08 shibata@luky.org
# Usage: perl mail2mt-newpost.pl < entire-single-mail.file

use strict;
use warnings;

use MIME::Parser;
use HTTP::Date;
use Jcode;
use XMLRPC::Lite;

my $DEBUG = 0;
#-------------------------------------Blog configs
my $blogid   = "9";
my $username = "hoge";
my $password = "fuga";
my $proxyurl = "http://blog.uriuri.com/mt/mt-xmlrpc.cgi";
my $bodylines = 10;
#-------------------------------------Mailing list configs
my $MLID1     = "x-ml-name";
my $MLID2     = "list-id";
my $MLCOUNT1  = "x-mail-count";
#my $MLCOUNT2 = "x-ml-count";
#my $AddrExp  = '([^()<>@,;:\/\s"\'&|]+)@[^()<>@,;:\/\s"\'&|]+';
my $AddrExp  = '([-_\.a-zA-Z0-9]+)@([-_a-zA-Z0-9]*[-_\.a-zA-Z0-9]+)';
#main
#-------------------------------------Parsing Mail Header and Body
my $parser = new MIME::Parser;
my $entity = $parser->parse(\*STDIN);

#-------------------------------------Mail Header treatment
my $subject = $entity->head->get('subject',0);
$subject = Jcode->new($subject)->mime_decode->euc;
my $date = $entity->head->get('date',0);
$DEBUG && print "date=".$date;
my $mlname = $entity->head->get($MLID1,0);
if($mlname) {   #fml ML server
	chomp ($mlname);
}else{
	chomp ($mlname = $entity->head->get($MLID2,0));
	$mlname  =~ s/<(.*?)>/$1/;
}
my $mlcount = $entity->head->get($MLCOUNT1,0);
if($mlcount){   #fml ML server
	chomp ($mlcount);
}else{
	chomp ($mlcount = $subject);
	$mlcount  =~ s/^.*[: ]([0-9]+?)[ \]].*/$1/;
}
#$head->unfold;
#$head->decode;

$subject = Jcode->new($subject)->mime_decode->utf8;
my $time = HTTP::Date::time2isoz(str2time($date));
$time  =~ s/(^.*) (.*)/$1T$2/;
$DEBUG && print "time=".$time;

#-------------------------------------Mail Body treatment
my $body =  $entity->bodyhandle;
my $mailbody =  $body->as_string;
$mailbody = Jcode->new($mailbody)->euc;
$mailbody  =~ s/$AddrExp/"$1\&\#64\;xxxxx"/geo;
$mailbody  =~ s/</"\&lt;"/geo;
$mailbody  =~ s/>/"\&gt;"/geo;

$mailbody =~ /((?:.*\n){$bodylines})((?:.*\n){0,})/;

$body = Jcode->new($1)->utf8;
my $extend = Jcode->new($2)->utf8;

$DEBUG && print "Title: ",$subject;
$DEBUG && print "Category: ",$mlname;
$DEBUG && print "isoTime: ",$time,"\n";
$DEBUG && print "\n";
$DEBUG && print "[".$mlname."-".$mlcount."] ";
#-------------------------------------post new Blog from ML 
my $postid = XMLRPC::Lite
	-> proxy($proxyurl)
	-> call('metaWeblog.newPost', $blogid, $username, $password,
		{
		'title'             => $subject,
		'description'       => $body,
		'dateCreated'       => $time,
		'mt_allow_comments' => 1,
		'mt_allow_pings'    => 1,
#		'mt_convert_breaks' => "",
		'mt_text_more'      => $extend,
		'mt_excerpt'        => "",
		'mt_keywords'       => $mlcount,
#		'mt_tb_ping_urls'   => "", #array of strings, ping URLs
		},
		0 # 1 = publish
	)
	-> result;

#-------------------------------------get Category list to prepare category ID
if (defined ($postid) && $mlname)
{
	my $categories = XMLRPC::Lite
		-> proxy($proxyurl)
		-> call('mt.getCategoryList',
			$blogid,
			$username,
			$password
		)
		-> result;
       	my $i = 0;
       	my $catId = 0;
	do {
        	if( $mlname =~ /$categories->[$i]->{'categoryName'}/) {
$DEBUG && print $categories->[$i]->{'categoryId'}." = ";
$DEBUG && print $categories->[$i]->{'categoryName'}."\n";
			$catId = $categories->[$i]->{'categoryId'};
		}
	$i++;
	}
	until(!$categories->[$i]->{'categoryName'});

#-------------------------------------set Category of just posted Id
	my $res = XMLRPC::Lite
		-> proxy($proxyurl)
		-> call('mt.setPostCategories',
			$postid,
			$username,
			$password,
			[
				{
				'categoryId'   => $catId,
				'isPrimary'    => 1
				}
			]
		)
		-> result;
	print "set category $! ";
}
else
{
	die "failed $!";
}
#-------------------------------------publish post
my $res = XMLRPC::Lite
	-> proxy($proxyurl)
	-> call('mt.publishPost',
		$postid,
		$username,
		$password,
	)
	-> result;
print "publish post $!\n";
