1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#!/usr/bin/perl -w
#
# tinyurl.pl listens on a channel for URLs longer than a certain length,
# and then makes a tinyurl shortcut to them for the convenience of the
# poor bastards using terminal-based IRC clients.
#
# -- dennis taylor, <dennis@funkplanet.com>
use strict;
use POE;
use POE::Component::IRC;
use LWP::UserAgent;
use HTTP::Response;
use HTTP::Request::Common;
use URI::Find; # but... but... who'd want to?
use constant MIN_URL_LENGTH => 60;
my @urls;
my $finder = URI::Find->new( sub { push @urls, $_[1]; } );
my $ua = LWP::UserAgent->new();
my $chan = '#tempura';
my $nick = 'ebi';
my %services = ( tinyurl => \&_get_tinyurl,
shorl => \&_get_shorl,
masl => \&_get_masl,
shorter => \&_get_shorter, );
my $current = 'tinyurl';
sub _start {
my ($kernel) = $_[KERNEL];
$ua->agent( 'Mozilla/5.0 (X11; U; Linux i386; en-US; rv:1.0.0) Gecko/20020529' );
$kernel->post( 'urlbot', 'register', 'all');
$kernel->post( 'urlbot', 'connect', { Debug => 1,
Nick => $nick,
Server => $ARGV[0] ||
'scissorman.phreeow.net',
Port => $ARGV[1] || 6667,
Username => 'neenio',
Ircname => "tinyurl.pl", }
);
}
sub irc_001 {
my ($kernel) = $_[KERNEL];
$kernel->post( 'urlbot', 'mode', $nick, '+i' );
$kernel->post( 'urlbot', 'join', $chan );
}
sub irc_disconnected {
my ($server) = $_[ARG0];
print "Lost connection to server $server.\n";
}
sub irc_error {
my $err = $_[ARG0];
print "Server error occurred! $err\n";
}
sub irc_socketerr {
my $err = $_[ARG0];
print "Couldn't connect to server: $err\n";
}
sub _stop {
my ($kernel) = $_[KERNEL];
print "Control session stopped.\n";
$kernel->call( 'urlbot', 'quit', 'Neenios on ice!' );
}
sub irc_public {
my ($kernel, $who, $chan, $msg) = @_[KERNEL, ARG0 .. ARG2];
make_tiny( $kernel, $who, $chan, $msg, 0 );
}
sub irc_msg {
my ($kernel, $who, $chan, $msg) = @_[KERNEL, ARG0 .. ARG2];
make_tiny( $kernel, $who, $chan, $msg, 1 );
}
sub make_tiny {
my ($kernel, $who, $chan, $msg, $private) = @_;
$who =~ s/^(.*)!.*$/$1/ or die "Weird-ass who: $who";
# IGNORE INFOBOTS. ALL MUST DIE.
return if $who eq "pea";
if ($msg =~ /^\s*$nick[:,\-!](?:\s*please)?\s*switch to (\w+)/i
and not $private) {
my $new_service = $1;
if (exists $services{$new_service}) {
$current = $new_service;
$kernel->post( 'urlbot', 'privmsg', $chan, 'Done.' );
} else {
my $known = join ', ', keys %services;
$kernel->post( 'urlbot', 'privmsg', $chan,
"Sorry, $who, I don't know that service. Here are the ones I do know about: $known." );
}
} else {
$finder->find( \$msg );
while (@urls) {
my $url = shift @urls;
next if length $url < MIN_URL_LENGTH;
$kernel->post( 'urlbot', 'privmsg', $private ? $who : $chan,
$services{$current}->( $who, $url ) );
}
}
}
sub _get_tinyurl {
my ($who, $url) = @_;
my $re = '<blockquote>(http://tinyurl\.com/.*?)</blockquote>';
my $response = $ua->request( POST 'http://tinyurl.com/create.php',
[ url => $url ] );
if ($response->is_success and $response->content =~ /$re/) {
return "$who\'s url is at $1";
} else {
return 'tinyurl.com sucks.';
}
}
sub _get_shorl {
my ($who, $url) = @_;
my $re = 'Shorl: <a href=".*?">(http://shorl\.com/.*?)</a><br>';
my $response = $ua->request( POST 'http://shorl.com/create.php',
[ url => $url ] );
if ($response->is_success and $response->content =~ /$re/) {
return "$who\'s url is at $1";
} else {
return 'shorl.com sucks.';
}
}
sub _get_masl {
my ($who, $url) = @_;
my $re = 'Your shorter link is: <a href="(http://makeashorterlink\.com/.*?)">';
my $response = $ua->request( POST 'http://makeashorterlink.com/index.php',
[ url => $url ] );
if ($response->is_success and $response->content =~ /$re/) {
return "$who\'s url is at $1";
} else {
return 'makeashorterlink.com sucks.';
}
}
sub _get_shorter {
my ($who, $url) = @_;
my $re = 'is:<br><br><a href="(http://shorterlink\.com/.*?)">';
my $response = $ua->request( GET 'http://shorterlink.com/add_url.html',
[ url => $url ] );
if ($response->is_success and $response->content =~ /$re/) {
return "$who\'s url is at $1";
} else {
return 'makeashorterlink.com sucks.';
}
}
POE::Component::IRC->new( 'urlbot' ) or
die "Can't instantiate new IRC component!\n";
POE::Session->create( package_states => [ 'main' => [qw(_start _stop irc_001 irc_disconnected
irc_socketerr irc_error irc_public irc_msg)],], );
$poe_kernel->run();
exit 0;
|