#!/usr/bin/perl # Blosxom Helper: moblog.pl # Author(s): Brandt Kurowski # Version: 2004-01-18 # Documentation: See the bottom of this file or type: perldoc moblog.pl # $Id: moblog.pl,v 1.3 2004/08/18 20:45:59 viv Exp $ use warnings; use strict; use File::Copy; use File::Spec; use File::Temp qw/ tempdir tempfile /; use MIME::Parser; # --- Configurable variables ----- # this is the place where you want the blog entry to go my $text = "/home/you/blosxom/entries/moblog"; # this is a tag in the subject line of the message as delivered, # to be stripped before changing it to the story title -- # leave blank if not using a subject line tag my $blog_tag = ""; # the suffix appended to blosxom story files, e.g. 'txt' my $blog_suffix = "txt"; # line-break tag to use, e.g.
,
,
,

,

, or

my $brtag = "

"; # this is the place where you want the image files to go # (you may want to use the "binary" plugin and make this the same as $text my $images = $text; # this is the url to the directory where you're putting the images my $uri_prefix = "/blog/moblog"; # this is the pattern to be used for each image. be sure to include "$uri" # where you want the uri to the image to be. (note that this will be eval-ed) my $uri_pattern = q|$blog_title$brtag|; # server to rsync local data with (blank if rsync not needed) my $remote = "www.example.com"; # path on remote server that corresponds to local $text path my $remote_text = "/var/www/blosxom/entries/moblog"; # path on remote server that corresponds to local $images path # (not needed if $text is the same as $images) my $remote_images = undef; # fetchmail sets umask to 077, which might cause problems for you my $umask = 022; # -------------------------------- umask $umask; my $parser = new MIME::Parser; $parser->output_under(tempdir(CLEANUP => 1)); my $entity = $parser->parse(\*STDIN); my $blog_title = $entity->head->get('subject') || "Moblog"; my $blog_entry = ""; # the text of this blog entry my $text_part = ""; # the text/plain portion of the message # first, strip any (optional) subject-line moblog-entry identifier $blog_title =~ s/(${blog_tag}[ ])(.*)/$2/; # then strip any trailing newlines to avoid polluting IMG ALT tag chomp($blog_title); # recursively handles parts of a mime message sub handle { my ($part) = @_; my $type = $part->effective_type; if ($type eq 'multipart/mixed') { handle($_) for $part->parts; } elsif ($type eq 'image/jpeg' or $type eq 'application/octet-stream') { # make up a unique name for this image in our $images directory my (undef, $name) = tempfile(DIR => $images, SUFFIX => '.jpg', OPEN => 0); # and move it there move($part->bodyhandle->path, $name) or die "couldn't move: $!"; # and spit out the appropriate html for it my $relative = File::Spec->abs2rel($name, $images); my $uri = "$uri_prefix/$relative"; # FIXME probably broken on win32 $uri_pattern =~ s/\$uri/$uri/e; $uri_pattern =~ s/\$blog_title/$blog_title/e; $uri_pattern =~ s/\$brtag/$brtag/e; } elsif ($type eq 'text/plain') { my $io = $part->open("r") or die "error opening body: $!"; while (defined(my $line = $io->getline)) { $text_part .= "$line\n"; } $text_part .= "$brtag\n"; $io->close or die "error closing body: $!"; unlink($part->bodyhandle->path); } else { die "i don't handle mime type $_ yet!"; } # Print parts of the story entry in the order you choose $blog_entry = ""; $blog_entry .= "$uri_pattern\n"; $blog_entry .= "$text_part\n"; } handle($entity); # don't let File::Temp open this for us, because it'll ignore our umask my (undef, $file) = tempfile(DIR => $text, SUFFIX => ".$blog_suffix", OPEN => 0); open OUT, ">$file" or die "can't write to $file ($!)"; print OUT "$blog_title\n\n$blog_entry"; close OUT; if ($remote) { # do images first or the blog entry may be visible before the images are if ($text ne $images) { # only do the images if they aren't in the text dir system "rsync -e ssh -a --delete $images/ $remote:$remote_images"; } system "rsync -e ssh -a --delete $text/ $remote:$remote_text"; } __END__ =head1 NAME Blosxom Helper: moblog.pl =head1 SYNOPSIS Use this script to take pictures emailed from a mobile phone and insert them into your Blosxom blog. =head1 INSTALLATION AND CONFIGURATION These instructions assume you want to use fetchmail to retreive moblogged pictures. If you want to use this with procmail, .qmail, .forward, etc, the details aren't here but things should work similarly. Basic steps: =over =item 1. Setup an email account to use with this script. With a pure fetchmail setup, it's easiest to dedicate an email account to your moblog. If you want to use a general account, and filter out only specific messages to be moblogged, then you may want to investigate procmail. =item 2. Configure fetchmail to poll that account. You probably want to put your fetchmail settings (host to poll, username, password) into a C<.fetchmailrc> file in your home directory, and either run fetchmail as a daemon or in your crontab. =item 3. Tell fetchmail to use this script as an MDA. Just make sure that this script is an executable, and indicate it's path in the fetchmail C configuration option. =item 4. Edit the configuration variables at the top of this script appropriately. Make special note of the C<$remote> variable, which is used if you want to run fetchmail (and this script) on a server other than the server that your blog is on. If that is the case, then you should also configure public-key authentication for SSH-ing into your blog server. This script will invoke rsync-over-SSH to update your blog. =back =head1 VERSION 2004-01-18 =head1 AUTHOR Brandt Kurowski , http://brandt.kurowski.net/ =head1 SEE ALSO Moblog Script Home/Docs: http://brandt.kurowski.net/blog/blog/moblog/ Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/ =head1 BUGS Address bug reports and comments to the author or to the Blosxom mailing list [http://www.yahoogroups.com/group/blosxom]. =head1 LICENSE Copyright 2003, Brandt Kurowski Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =cut # vim: ts=4 sw=4 cin