#!/usr/pkg/bin/perl # Copyright (c) 2009, Emile "iMil" Heitor # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the GCU-Squad nor the # names of its contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY Emile "iMil" Heitor ''AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL Emile "iMil" Heitor BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use strict; use Switch; use Config::IniFiles; use WordPress::XMLRPC; use utf8; my $title; my $categories; my $content; my $wp; my $postfile; my $postid; my $published = 0; my $use_utf8 = 1; sub usage { print "$0 \n"; exit 1; } sub postread { my $line; open POSTFILE, $postfile or die $!; while () { $line = $_; if ($use_utf8 eq 1) { utf8::encode($line); } if (!defined($content) && $line =~ /^[\n\#]/) { next; } if (!defined($title) && $line =~ /^title\:\s*(.+)/i) { $title = $1; next; } if (!defined($categories) && $line =~ /^categories\:\s*(.+)/i) { $categories = $1; next; } if (!defined($postid) && $line =~ /^postid\:\s*(.+)/i) { $postid = $1; next; } $content .= $line; } close POSTFILE; } sub add_post { my $wpost = {}; if (defined($postid)) { print "Already posted with id $postid\n"; exit 2; } $wpost->{title} = $title; $wpost->{categories} = eval($categories); $wpost->{description} = $content; $postid = $wp->newPost($wpost, $published) or die ($wp->errstr); print "Entry posted with id $postid\n"; open POSTFILE, ">>$postfile"; print POSTFILE "postid: $postid\n"; close POSTFILE; } sub del_post { if (defined($postid)) { $wp->deletePost($postid) or die ($wp->errstr); # remove postid, perlmonks receipe open IN, "<", $postfile or die; my @contents = ; close IN; @contents = grep { !/^postid\:/i } @contents; open OUT, ">", $postfile or die; print OUT @contents; close OUT; print "Entry $postid deleted\n"; } } sub mod_post { if (defined($postid)) { my $wpost = $wp->getPost($postid); $wpost->{title} = $title; $wpost->{categories} = eval($categories); $wpost->{description} = $content; $wp->editPost($postid, $wpost, $published) or die ($wp->errstr); print "Entry $postid modified\n"; } } if (@ARGV < 3) { usage; } my $blog = $ARGV[0]; my $action = $ARGV[1]; $postfile = $ARGV[2]; # read post from file postread; my $cfg = new Config::IniFiles( -file => $ENV{'HOME'}."/.wpostrc" ) || die; $wp = WordPress::XMLRPC->new({ username => $cfg->val($blog, "user"), password => $cfg->val($blog, "passwd"), proxy => $cfg->val($blog, "url"), }) or die; switch ($action) { case /^add/ { add_post } case /^del/ { del_post } case /^mod/ { mod_post } else { usage } }