#!/usr/bin/perl use IO::Socket; # Bot info my ($nick, $user, $real) = ("PDUrls", "coder", "PD URL bot"); my (%urls); $debug++; load_urls(); connect_to_irc(); ### Parse loop ### while(<$sock>) { $now = time(); print ("IN: $_") if ($debug); s[\r\n]//g; @in = split(/\s+/); os ("PONG $1") if (/PING :(.+)/); on_376() if ($in[1] == 376); on_privmsg($_) if ($in[1] eq "PRIVMSG"); os("JOIN #partydome") if ($in[1] eq "KICK"); } ### Events ### # End of MOTD sub on_376 { os("JOIN #partydome"); } # We get msg! { my ($lasturl, $lastfrom, $urltime); sub on_privmsg { my ($from, $cmd, $to, $msg) = split(/\s+/, $_[0], 4); my $now = time(); # Undef URL if older than 60 seconds undef ($lasturl) if ($now - $urltime > 60); $msg =~ s/^://; # Strip that pesky : print("privmsg: f: $from t: $to m: $msg\n") if ($debug); if ($msg =~ /#lasturls/) { my @list = lasturls(); foreach (@list) { chomp; os("PRIVMSG \#partydome $_\n"); } } # We got a fully qualified URL if ($msg =~ /http:\/\/(.+)/) { $lasturl = "http://$1"; return if ($lasturl =~ /[\<|\>]/); $lasturl = (split(/\s+/, $lasturl))[0]; $lastfrom = $from; $urltime = $now; logurl($lasturl); return; } # www.foo.com type URL. if ($msg =~ /www\.(.+)/) { my $url = $1; $lasturl = "http://www.$url"; $lasturl = (split(/\s+/, $lasturl))[0]; return if ($lasturl =~ /[\<|\>]/); $lastfrom = $from; $urltime = $now; logurl($lasturl); return; } # Someone confirmed the URL. if ($lasturl && !($lastfrom eq $from)) { if ($msg =~ /^\+\+/ || $msg =~ /^gg/) { logggurl($lasturl); $urltime = $now; } } } } # Athletic Supporters sub connect_to_irc { $sock = IO::Socket::INET->new( PeerHost => "eu.undernet.org", PeerPort => 6667) or return(1); os("USER $user . . :$real"); os("NICK $nick"); } sub os { print $sock ("@_\n"); print("OUT: @_\n") if ($debug); } # Could use a real database here but I'm lazy :) sub load_urls { open(URLS, "urls") or die("Can't open 'urls' for read: $!\n"); while() { chomp; my ($url, $count) = split(/\t/); $urls{$url} = $count; } close(URLS); } # Log a "gg" URL to the list file sub logggurl { my $url = $_[0]; $urls{$url}++; print("logggurl: $url $urls{$url}\n") if ($debug); open(URLS, ">urls") or die("Can't write urls: $!\n"); foreach (keys %urls) { print URLS ("$_\t$urls{$_}\n"); } close(URLS); } # Log any ol URL to the tail file sub logurl { print("logurl: $_[0]\n") if ($debug); open(OUT, ">>tail") or die("Can't write tail: $!\n"); print OUT ("$_[0]\n"); close(OUT); } sub lasturls { my @list; open(TAIL, "tail tail |") or die("Can't open tail: $!\n"); @list = ; @list = reverse(@list); return(@list); } END { print $sock ("QUIT :CLANG! CLINK! damn mechanic's ripping my balls off again..\n"); sleep(1); exit(1); }