Advertisement
swarley

IRC.pm IRC Bot Framework

Sep 5th, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.42 KB | None | 0 0
  1. use warnings;
  2. use strict;
  3. use conf;
  4. use extras;
  5. use IO::Socket;
  6. use feature 'switch';
  7. package IRC;
  8.  
  9. sub new {
  10.     my $class  = shift;                                                    
  11.     my $config = conf->new(shift);                                        
  12.     my %s_conf = $config->allocate('server');                              
  13.     my %u_conf = $config->allocate('user');                              
  14.     my %o_conf = $config->allocate('owner');                              
  15.     my $conf   = {server => \%s_conf, owner => \%o_conf, user =>\%u_conf};
  16.     my $socket = IO::Socket::INET->new(
  17.                       PeerAddr => $s_conf{network},      
  18.                       PeerPort => $s_conf{port},           
  19.                       Proto    => 'tcp')
  20.     or die "Can't connect to $s_conf{network}:$s_conf{port}\n :$!";      
  21.     my %hash = (
  22.             Socket   => $socket,                       
  23.             Config   => $conf,                 
  24.             numeric  => sub {},                      
  25.             privmsg  => sub {},                
  26.             notice   => sub {},                      
  27.             chan_msg => sub {});                                      
  28.     bless \%hash, $class;                                  
  29.    
  30. }
  31.  
  32. sub send_raw {
  33.     my $self = shift;
  34.     my $data = shift;
  35.     $self->{Socket}->send($data."\n");
  36.     puts(">> $data");
  37. }
  38.  
  39. sub puts($) {
  40.     my $text = shift;
  41.     print "$text\n";
  42. }
  43.  
  44. sub connect {
  45.     my $self = shift;
  46.     my $user_conf = $self->{Config}->{user};
  47.     $self->send_raw("USER $$user_conf{user} 0 0 :$$user_conf{real}");
  48.     $self->send_raw("NICK $$user_conf{nick1}");
  49. }
  50.  
  51. sub register {
  52.     my $self = shift;
  53.     my ($command, $ref) = @_;
  54.     $self->{lc $command} = $ref;
  55. }
  56.  
  57. sub breakdown {
  58.     my ($type,$data) = @_;
  59.     $data =~ s/.//;
  60.     given($type) {
  61.                 when('privmsg') {
  62.                         return {
  63.                                 nick => (split /\!/, $data)[0],
  64.                                 user => (split /\@/, (split /\!/, $data)[1])[1],
  65.                                 host => (split /\s/, (split /\@/, $data)[1])[0],
  66.                                 msg  => (split /\:/, $data, 2)[1]};
  67.                 }
  68.                 when('chan_msg') {
  69.             return {
  70.                                 nick => (split /\!/, $data)[0],
  71.                                 user => (split /\@/, (split /\!/, $data)[1])[1],
  72.                                 host => (split /\s/, (split /\@/, $data)[1])[0],
  73.                                 chan => (split /\s/, $data)[2],
  74.                                 msg  => (split /\:/, $data, 2)[1]};
  75.             }
  76.         when('notice') {
  77.             return {
  78.                                 nick => (split /\!/, $data)[0],
  79.                                 user => (split /\@/, (split /\!/, $data)[1])[1],
  80.                                 host => (split /\s/, (split /\@/, $data)[1])[0],
  81.                                 msg  => (split /\:/, $data, 2)[1]};
  82.         }
  83.     }
  84. }
  85.  
  86. sub main {
  87.     my $self   = shift;
  88.     my $socket = $self->{Socket};
  89.     while (my $data = <$socket>) {
  90.         print "<< $data";
  91.         $data =~ s/(\n|\r|\0)//g;
  92.         my $priv_val = (split /\s+/, $data)[2];
  93.         my $type  = (split /\s+/, $data)[1];
  94.         given ($type) {
  95.             when(/\d\d\d/) {
  96.                 $self->{numeric}->($type, $data);
  97.             }
  98.             when(/PRIVMSG/i) {
  99.                 if ($priv_val =~ m/^\#/) {
  100.                     $self->{last_message} = breakdown('chan_msg', $data); #make sure it returns a hash reference
  101.                     $self->{chan_msg}->($data);
  102.                 }
  103.                 else {
  104.                     $self->{last_message} = breakdown('privmsg', $data);
  105.                     $self->{privmsg}->($data);
  106.                 }
  107.             }
  108.             when(/NOTICE/i) {
  109.                 $self->{notice}->($data);
  110.             }
  111.         }
  112.         if ($data =~ m/^PING/i) {
  113.             my $pong = (split(':',$data,2))[1];
  114.             $self->send_raw("PONG :$pong");
  115.         }      
  116.     }
  117. }
  118. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement