Commits

User Eax  committed 785645a

initial commit

  • Participants

Comments (0)

Files changed (1)

+#!/usr/bin/perl
+
+# Googler IRC Bot
+# (c) 2010 Alexandr A Alexeev
+# http://eax.me/
+
+use IO::Socket::INET;
+use URI::Escape;
+use Text::Iconv;
+use strict;
+
+my ($server, $port, $botnick, $charset) = 
+   qw/irc.dalnet.ru 6667 googler cp1251/;
+my $pass = "";
+my @channels = qw/googlertest/;
+
+my $opt  = "--timeout=10 --no-check-certificate --user-agent='Mozilla/5.0 (X11; U; FreeBSD i386; ru-RU; rv:1.9.1.10) Gecko/20100625 Firefox/3.5.10'";
+my $conv = Text::Iconv->new($charset, "utf-8");
+
+while(1) {
+   print "CONNECTING: server = $server, port = $port...\n";
+   my $irc = IO::Socket::INET->new(
+     PeerAddr => $server,
+     PeerPort => $port,
+     Proto => 'tcp',
+     Type => SOCK_STREAM,
+     Timeout => 10
+   );
+
+   if(defined $irc) {
+     print $irc "USER $botnick localhost localhost $botnick\r\n";
+     print $irc "NICK $botnick\r\n";
+     print $irc "PASS $pass\r\n" if($pass);
+     print $irc "JOIN #$_\r\n" for(@channels);
+
+     while(my $line = <$irc>) {
+       $line = $conv->convert($line);
+       if($line =~ /^PING \:([a-zA-Z\-\.0-9]+)/){ 
+         print $irc "PONG $1\r\n";
+       } elsif($line =~ /^\:([^\!\ ]+)\![^\ ]+\ ([^\r\n]+)\r\n$/) {
+         my $nick = $1; $line = $2;
+
+         if(my ($channel, $message) = $line =~ /^PRIVMSG #([^\ ]+) \:(.+)$/) {
+           # см http://ru.wikipedia.org/wiki/IRC#CTCP
+           if(my ($action) = $message =~ /^\x01ACTION ([^\x01]+)\x01?$/){
+              print irc_msg_encode("[$channel] *** $nick $action\n");
+           } else {
+              print irc_msg_encode("[$channel] $nick: $message\n");
+              if($message =~ /\!google (.*)/) {
+                my $result = do_google($1);
+                print $irc "PRIVMSG #$channel :$nick, $result\r\n";
+              }
+           }
+         } elsif(my ($chan, $nick) = $line =~ /^KICK #([^\ ]+) ([^\ ]+)/) {
+           if($nick eq $botnick) {
+             print "DEBUG: i'm kicked from #$chan\n";
+             last;
+           }
+         }
+       } # if($line =~ /^PONG ...
+     } # while($line = <$irc> ...
+   } else { # if(defined $irc) ...
+     print "CONNECTION FAILED!\n";
+   }
+   sleep 300;
+}
+
+# поиск в Google
+sub do_google {
+   my $query = $_[0];
+   my $url = "http://google.com/search?q=".uri_escape($query);
+   my $data = `wget $opt -q '$url' -O -`;
+   return "query failed - error $?" if($?);
+   return $1 if($data =~ /<h3 class="r"><a href="([^"]+)"/i);
+   return "nothing found";
+}
+
+# вырезаем выделения цветом и тп
+sub irc_msg_encode {
+   my $text = $_[0];
+   $text =~ s/\x0F//g; 
+   $text =~ s/\x02//g; $text =~ s/\x1F//g;
+   $text =~ s/\x03([0-9]{1,2}(\,[0-9]{1,2})?)?//g;
+   $text;
+}
+