#!/usr/bin/perl # $Id$ # TODO: Map URLs to temp 'AA' handles for further manipulation # TODO: Way to represent tags? # TODO: Allow/Deny for port 12345 on benchbot use warnings; use strict; our $USER; our $PASSWD; # Ask deusx for a copy of this to access THE chomp account # Otherwise, comment this out and fill in your details above. require "chomp_passwd.perl"; our $API_URL = 'http://del.icio.us/api'; our $DEL_URL = "http://del.icio.us/$USER"; our $DEL_BASE_URL = "http://del.icio.us/"; our $DEBUG = 1; $|=1; use Util::Backend; use Util::Common qw(try_uptime); use Regexp::Common 'RE_URI'; use LWP::UserAgent; use HTTP::Request; use URI::Escape; my $noisy = -1; svc_run("127.0.0.1", 12345); exit 0; sub delicious { my ($user, $passwd, $op, %params) = @_; my $api_url = "$API_URL/$op?"; $api_url .= join('&', map { $_.'='.uri_escape($params{$_}) } keys %params); my $ua = LWP::UserAgent->new(); my $req = HTTP::Request->new(GET=>$api_url); $req->authorization_basic($user, $passwd); my $res = $ua->request($req); } sub on_public { goto &read_irc; } sub on_msg { goto &read_irc; } sub read_irc { my $msg = shift; # Support uptime request my $ut_response = try_uptime( $msg->message(), (defined $msg->{channel}) ? 'public' : 'private', $msg->nick(), $msg->addressed(), ); if (defined $ut_response) { $msg->say($ut_response); msg_send($msg); } # Respond only when requested, or when msg'ed return unless $msg->addressed() or !defined $msg->channel(); my $CURR_USER = $USER; my $channel = $msg->cschannel(); $channel =~ s/\#//; $CURR_USER .= '_'.$channel if (defined $channel); my $out = ($msg->channel() ne $msg->nick()) ? $msg->csnick().': ' : ''; # TODO: Per-channel setting for noisiness. # TODO: Better table-based command processing (see infobot?) if ($msg->message() =~ /^help/i) { $out .= "I am a simple minded URL muncher who posts to ". "del.icio.us at $DEL_BASE_URL/$CURR_USER. Address me or message me with ". "URLs. Add a comment after a url using '#'. Tell me to ". "speak or shut up. I may have other talents."; $msg->say($out); msg_send($msg); } elsif ($msg->message() =~ /^shut up/i) { if ($noisy) { $out .= 'Shutting up now.'; $msg->say($out); msg_send($msg); } $noisy = undef; } elsif ($msg->message() =~ /^speak/i) { if (!$noisy) { $out .= 'Removing the gag.'; $msg->say($out); msg_send($msg); } $noisy = -1; } else { # Extract the URL and description my $body = $msg->message(); my $url_re = RE_URI; my ($url, $description) = ("",""); ($url,undef, undef, undef, $description) = ($body=~/^($url_re(#?[^\s#])+)((\s+#)?\s*)(.*)/); print "USER: $CURR_USER URL: $url DESC: $description\n"; # Do nothing if there's no URL return if !$url; # Use the URL as description by default. $description = $url if !$description; # Build a list of tags for the posting, using channel and nick #my @tags = (); #push @tags, $msg->csnick(); # Extract tags from description (ie. :fun :weird :huh) #while ($description=~s/\s*\:(\w+)\s*/ /) { # push @tags, $1; #} # Construct attribution in extended text my $extended = "posted by ".$msg->csnick(); # Post to del.icio.us my $res = delicious( $CURR_USER, $PASSWD, 'posts/add', url => $url, description => $description, extended => $extended, ); if ($res->is_success) { if ($res->content =~ /done/) { $out .= 'URL posted.'; } else { $out .= 'Blech. Something went wrong.'; warn("PROBLEM: ".$res->content); } } else { $out .= 'Posting failed: '.$res->status_line; } if ($noisy) { $msg->say($out); msg_send($msg); } } }