#!/usr/bin/perl # Snippetsd.pl: A Robot to allow posting of conversations on a blog. # # Author: David L. Sifry david-snippets@sifry.com # Copyright (c) 2002 # All Rights Reserved # # This program is licenced under the GNU General Public License Version 2 # http://www.gnu.org/licenses/gpl.txt # # If you like this program, please drop me a line and let me know! # use Net::Jabber qw(Client); use Net::Jabber::Protocol; require LWP::UserAgent; use HTTP::Cookies; use Net::Blogger; my $username = "My blog username"; my $password = "My blog password"; my $blogid = 4; #Change this to your blogID. my $jabberserver = "MY-JABBER-SERVER"; my $robotusername = "My Robot's JabberID"; my $robotpassword = "My Robot's Jabber Password"; $con = new Net::Jabber::Client(debuglevel=>0); $con->Connect(hostname=>$jabberserver); die if (!$con->Connected()); my $b = Net::Blogger->new(engine=>"movabletype"); $b->Proxy("http://MY-DOMAN-NAME/PATH/cgi-bin/mt-xmlrpc.cgi"); $b->Username($username); $b->Password($password); $b->BlogId($blogid); @result = $con->AuthSend(username=>$robotusername, password=>$robotpassword, resource=>"conversationsd"); if ($result[0] ne "ok") { die "Ident/Auth with server failed: $result[0] - $result[1]\n"; } $con->SetCallBacks( presence => \&handle_presence, message => \&handle_message); $con->RosterGet(); $con->PresenceSend(); # Wait for data # ------------- while(defined($con->Process())) { } $con->Disconnect(); exit; sub handle_message { my ($sid,$msg) = @_; # Drop it if we're not waiting for our message ID $id = $msg->GetID; if ($msg->DefinedBody()) { $mfrom = $msg->GetFrom(); $mfrom =~ s|^(.+?)/.*$|$1|; $mdate = $msg->GetTimeStamp(); $date = `date +"%X"`; chomp $date; $msubject = $msg->GetSubject(); $mbody = ''; $mbody .= '

[' . $mfrom . ' ' . $date . "] " . $msg->GetBody() . "

\n"; errlog(3,$msg->GetXML()); print "RECEIVED: $id FROM: $mfrom $mdate:\n$msubject\n$mbody\n\n"; blogit($msubject,$mbody); print "Posting Complete.\n"; } } sub handle_presence { my ($sid,$presence) = @_; my $jid = $presence->GetFrom(); my $show = $presence->GetShow(); my $type = $presence->GetType(); $jid =~ s!\/.*$!!; # remove any resource suffix from JID print STDERR "Presence from $jid:\n".$presence->GetXML(); #$present{$jid} = $presence->GetShow() || 'normal'; if ($type eq "subscribe") { print STDERR "$jid requests subscription"; $con->Send($presence->Reply(type => 'subscribed')); $con->Send($presence->Reply(type => 'subscribe')); } if ($type eq "unsubscribe") { print STDERR "$jid requests unsubscription"; $con->Send($presence->Reply(type => 'unsubscribed')); $con->Send($presence->Reply(type => 'unsubscribe')); delete $present{$jid}; } if ($type eq "unavailable") { print STDERR "$jid is unavailable"; delete $present{$jid}; } if ($type eq "") { # We'll count normal, chat and away as valid # present stati if ($show =~ /^(available|chat|away|)$/i) { $show = 'available' if ($show =~ /^$/); print STDERR "$jid is $show\n"; $present{$jid} = 1; } else { delete $present{$jid}; } } } sub errlog { my ($level,@string) = @_; if ($level <= $verbose) { print STDERR @string; } } sub blogit { my ($title,$msg) = @_; if (length($title) > 0) { # OK, new post $b->metaWeblog()->newPost(title=>$title, description=>$msg, publish=>1); } else { my ($a,$href) = $b->metaWeblog()->getRecentPosts(numposts => 1); if ($a) { my $content = $href->{'content'}; $content .= $msg; print "CONTENT: $content\n\nPOSTID: ". $href->{postid} ."\n"; $b->editPost(postbody => \$content, postid => $href->{'postid'}, publish => 1); } else { print "ERROR! Could not get the most recent post.\n\n"; } } }