#!/usr/bin/perl
#
# =[ Comments ]===============================================================
# 05/23/2005 njc It appears that Blogspot has made a few changes that change
# the BLOG's appearance. First
is now
# and \n's are now thrown in and are interpreted. So now I need
# to strip the \n out of the file. Currently I'm unsure if I
# need to turn this into a full HTML document or not.
# 05/30/2005 njc Found out why Blogspot doesn't get my Blog entry. It seems that
# Comcast isn't sending them! I don't know why but I can now send
# directly from my machine to linuxha.com (modified sendmail, so
# I'll be using Comcast a lot less).
# 07/12/2005 njc I ran the validator on my Blog web page and it caught a few
# errors. One of those errors was the & I need to change the to
# & this will be interesting.
# http://validator.w3.org/check?uri=http://linuxha.blogspot.com/
# 01/20/2007 njc 1and1 is, again, having problems getting to blogger.com (the
# blog entries don't show up (grrr!). So I've instead decided to
# use comcast (2008 - I now use gmail to forward my mail, problem
# solved).
# 07/18/2008 njc The '!\r\n' or '!\n' seems to caused by sendmail. What is
# happening si that sendmail adds the '!' in the middle of words
# or sentences. It occurs around right after position 2048 (hmm).
# I'm thinking that I may need to read in 2048 bytes then add a
# or somewhere before but near that point so I can get a
# word break. I don't know if this will cause Google blog to add
# a new line (I hope not). FIXED!: see the sub word_break.
# 07/22/2008 njc I need a way to backup the file such that once I've sent the
# file I back it up (-20080721.gz)
#
# ============================================================================
# Word_break
# This solves a rather interesting problem. Sendmail sends out a "!\n" if it
# finds more than 2048 (2040?) characters. So what this does is every 2040
# characters search back to the previous space and insert a '\r\n'
# this keeps Sendmail from sending the "!\n"
sub word_break($) {
my ($str) = @_;
#####
$tmp = "";
$offset = 0;
$total_len = length($str);
$rd_bytes = 2040;
do {
$i++;
$t = substr($str, $offset, $rd_bytes); # Read in $rd_bytes
$r_offset = rindex($t, " "); # Find the last space
$offset = $offset + $r_offset; # Last space is the in the next read
$t = substr($t, 0, $r_offset); # save up to the last space
$tmp = $tmp . "\r\n" . $t; # Toss it on the end of the string
# Figure out if we're trying to read past the end
if($offset + $rd_bytes > $total_len) {
$rd_bytes = $total_len - $offset;
# Read the rest of the time and throw it on the end of tmp
$tmp = $tmp . "\r\n" . substr($str, $offset, $rd_bytes);
$offset = 9999999; # I doubt I can right a blog entry that large
break; # Does this even work?
}
} while($offset <= $total_len);
#####
return $tmp;
}
# ============================================================================
sub backup_file($$) {
my ($filename, $date) = @_;
print "\bzip2 <$filename >$filename.$date.bz\n";
`bzip2 <$filename >$filename.$date.bz`;
}
# ============================================================================
sub HelpMessage($) {
my ($date) = @_;
print "Sends current blog to the user.\@blogger.com blog via example.com\n";
print "sendBlog.pl \n";
print " -t | --to send to this address\n";
print " -f | --from from address\n";
print " -c | --cc cc address\n";
print " --bcc blind carbon copy address\n";
print " --backup Backup file to \'filename.${date}\'\n";
print " -s | --secret secret password (replaces )\n";
print " -v | --verbose print out status messages\n";
print " -d | --debug stop before actually sending mail\n";
print " -h | -? | --help This help message\n";
exit 1;
}
# ============================================================================
use vars qw( $verbose=0 $testing=0 $options $backup); # Note no commas needed
my $header;
my $footer;
my $secret = `cat .secret`; # Yes the file must be in the immediate directory
chomp($secret);
my $from ="userid\@example.com"; # you need to change this
my $to;# = "userid.${secret}\@blogger.com";
my $cc = "";
my $bcc = "";
my $testing = 0;
my $verbose = 0;
my $version = "Sendmail";
#y $date = `date +"%Y%m%d"`;
my ($sec, $min, $hr, $mday, $mon, $yr) = localtime(time);
my $date = sprintf "%04d%02d%02d", 1900+$yr, $mon+1, $mday;
use Getopt::Long;
# this is important for sending html formatted mail
# mail -a options
# -a \"Content-Type: text/html; charset=ISO-8859-1" -a \"Content-Transfer-Encoding: 7bit\"
$options = "-a \"Content-Type: text/html; charset=ISO-8859-1\" -a \"Content-Transfer-Encoding: 7bit\"";
# Some GetOpt options
# xxx! no arg, allows for --foo or --nofoo
# xxx+ no arg, increments xxx by one each time it is seen
# xxx=i int
# xxx=s string
# xxx=o extended int
# xxx=f float
GetOptions('help|?' => sub { HelpMessage($date) },
'debug!' => \$testing,
'b!' => \$backup,
'backup!' => \$backup,
'to=s' => \$address,
"from=s" => \$from,
"cc=s" => \$cc,
"bss=s" => \$bcc,
"secret=s" => \$secret,
'verbose!' => \$verbose );
($file) = @ARGV;
if(!defined($file)) {
HelpMessage($date);
}
if(defined($address)) {
$to = $address;
} else {
$to = "userid.${secret}\@blogger.com";
}
print "Address to send to: $address\n" if ($verbose);
print "Address to send to: $to\n" if ($verbose);
#my ($sec, $min, $hr, $mday, $mon, $yr, $wday) = localtime(time);
#$date = sprintf "%s, %02d %s %d %02d:%02d:%02d -0500",
# (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$wday],
# $mday,
# (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon],
# 1900+$yr,
# $hr, $min, $sec;
# check to see that the file exists
# =[ Oops! ]==================================================================
# OK, we have 2 problems here. We need to escape any CLI characters so they
# don't get interpreted as something they're not ( -s " Something & Something")
# and we need to keep the original intact for the HTML subject!
# ============================================================================
# Cut the title out of the title header and use it as the subject
#
# Tivo HME SDK
$upd_str =''; # delimiting string
@title = (($body = qx(cat $file)) =~ m|$upd_str(.*)$upd_str|s); # match update
$subject = join(" ", @title);
$subject =~ s/\n//g;
# OK here is where we need to filter the & to & We should probably work
# on < & > also.
#From - Mon May 23 11:33:57 2005
#Date: $date
#From: userid\@example.net
#To: $address
#Subject: $subject
#User-Agent: Sendblog Perl script
#X-Accept-Language: en-us, en
#MIME-Version: 1.0
#Content-Type: text/html; charset=ISO-8859-1
#Content-Transfer-Encoding: 7bit
#
#
$eheader ="To:$to
";
if($cc) {
$eheader .="CC:$cc
";
}
$eheader .= "From:$from
Subject:$subject
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
";
$header = "
$subject
";
$footer = '
';
#
if($verbose) {
print "Adding Subject: $subject\n";
print "Removing comments\n";
}
# ============================================================================
# OK, we now need to properly send html mail!
# I think this means putting together the proper headers and building the
# whole html message and not just a partial one like I've been building. Also
# I need to remove all the extra spaces and properly quote any extranious
# &'s
# ============================================================================
# ============================================================================
# From - Mon May 23 11:33:57 2005
# X-Mozilla-Status: 0001
# X-Mozilla-Status2: 00000000
# FCC: mailbox://root@pop.example.net/Sent
# X-Identity-Key: id1
# X-Account-Key: account1
# Date: Mon, 23 May 2005 11:33:57 -0400
# From: none <""root\"@(none)">
# X-Mozilla-Draft-Info: internal/draft; vcard=0; receipt=0; uuencode=0
# User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317)
# X-Accept-Language: en-us, en
# MIME-Version: 1.0
# To: userid@example.net
# Subject: Test
# Content-Type: text/html; charset=ISO-8859-1
# Content-Transfer-Encoding: 7bit
#
#
#
#
#
#
#
#
# I need to build an HTML message to be sent Blogspot.com
#
#
#
# ============================================================================
# Sample blog entry
#
#
#
#
# Tivo HME SDK
#
# Sorry for ignoring the BLOG, I had to study for mid-terms. I think I did
$body =~ s|||gs; # grab the body only
$body =~ s|\s+| |gs; # remove duplicate spaces & replace newlines with a space
# Okay now the real fun. We have to add a new line to this at a word break at a
# word break before 2048 characters.
$body = word_break($body);
if($verbose) {
print $eheader;
print "Title: $subject\n";
print "$header\n";
print "$body\n";
print "$footer\n";
print "#end\n";
}
# send file with sendmail
# Using sendmail on cookie.uucp sending via example.net
my $cmd = "|/usr/sbin/sendmail -t -n";
print "Executing: $cmd\n";
if($testing) {
backup_file($file, $date) if($backup);
exit 0;
}
#open(MESSAGE, "|mutt -s \"$subject\" $address");
open(MESSAGE, $cmd);
print "Sending the message\n";
# Can we check for an error here?
print MESSAGE $eheader;
print MESSAGE $header;
print MESSAGE $body;
print MESSAGE $footer;
print MESSAGE "\n#end\n"; # This makes sure nothing get tagged to the end of the blog entry
close(MESSAGE);
backup_file($file, $date) if($backup);
print "Done\n\n";