Advertisement
swarley

Homemade IRC Perl On Event Style pm

Aug 30th, 2011
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.99 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;                                                    #Get the class name required for bless
  11.     my $config = conf->new(shift);                                         #Get the config file name
  12.     my %s_conf = $config->allocate('server');                              #Server Config
  13.     my %u_conf = $config->allocate('user');                                #User Info Config
  14.     my %o_conf = $config->allocate('owner');                               #Owner Config
  15.     my $conf   = {server => \%s_conf, owner => \%o_conf, user =>\%u_conf}; #Create a reference to the different configurations
  16.     my $socket = new IO::Socket::INET(PeerAddr => $s_conf{network},        #Creating the socket connection
  17.                       PeerPort => $s_conf{port},           #....
  18.                       Proto    => 'tcp') or die        #Could the connection be made?
  19.     "Can't connect to ".$s_conf{network}.":".$s_conf{port}."\n";           #If not then die and tell us that something went wrong.
  20.     my %hash = (Socket   => $socket,                       #Essentially IRC::init but I'm using it in init.
  21.             Config   => $conf,                         #.....
  22.             numeric  => sub {},                        #.....
  23.             privmsg  => sub {},                        #.....
  24.             notice   => sub {},                        #.....
  25.             chan_msg => sub {});                                       #.....
  26.     bless \%hash, $class;                                  #blessing the class
  27.    
  28. }
  29.  
  30. sub init {
  31.     my $self = shift;
  32.     $self->register('notice', sub { });
  33.     $self->register('privmsg', sub { });
  34.     $self->register('chan_msg', sub { });
  35.     $self->register('numeric', sub { });
  36. }
  37.  
  38. sub send_raw {
  39.     my $self = shift;
  40.     my $data = shift;
  41.     $self->{Socket}->send($data."\n");
  42.     puts(">> $data");
  43. }
  44.  
  45. sub puts($) {
  46.     my $text = shift;
  47.     print "$text\n";
  48. }
  49.  
  50. sub connect {
  51.     my $self = shift;
  52.     my $user_conf = $self->{Config}->{user};
  53.     $self->send_raw("USER ".$user_conf->{user}." 0 0 :".$user_conf->{real});
  54.     $self->send_raw("NICK ".$user_conf->{nick1});
  55. }
  56.  
  57. sub register {
  58.     my $self = shift;
  59.     my ($command, $ref) = @_;
  60.     $self->{lc $command} = $ref;
  61. }
  62.  
  63. sub breakdown {
  64.     my ($type,$data) = @_;
  65.     given(type) {
  66.         when('privmsg') {
  67.             my @s = split ' ',$data;
  68.         }
  69.     }
  70. }
  71.  
  72. sub main {
  73.     my $self   = shift;
  74.     my $socket = $self->{Socket};
  75.     while (my $data = <$socket>) {
  76.         print "<< $data";
  77.         my @split = split ' ', $data;
  78.         my $type  = $split[1];
  79.         given ($type) {
  80.             when(/\d\d\d/) {
  81.                 $self->{numeric}->($type, $data);
  82.             }
  83.             when(/PRIVMSG/i) {
  84.                 if ($split[2] =~ m/^\#/) {
  85. #                   $self->{last_message} = breakdown('chan_msg', $data); #make sure it returns a hash reference
  86.                     $self->{chan_msg}->($data);
  87.                 }
  88.                 else {
  89. #                   $self->{last_message} = breadown('privmsg', $data);
  90.                     $self->{privmsg}->($data);
  91.                 }
  92.             }
  93.             when(/NOTICE/i) {
  94.                 $self->{notice}->($data);
  95.             }
  96.             when(/PING/i) {
  97.                 my @line = split(':',$data,2);
  98.                 print @line;
  99.                 $self->send_raw("PONG :".$line[1]);
  100.             }
  101.         }
  102.                
  103.     }
  104. }
  105. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement