Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use warnings;
- use strict;
- use conf;
- use extras;
- use IO::Socket;
- use feature 'switch';
- package IRC;
- sub new {
- my $class = shift;
- my $config = conf->new(shift);
- my %s_conf = $config->allocate('server');
- my %u_conf = $config->allocate('user');
- my %o_conf = $config->allocate('owner');
- my $conf = {server => \%s_conf, owner => \%o_conf, user =>\%u_conf};
- my $socket = IO::Socket::INET->new(
- PeerAddr => $s_conf{network},
- PeerPort => $s_conf{port},
- Proto => 'tcp')
- or die "Can't connect to $s_conf{network}:$s_conf{port}\n :$!";
- my %hash = (
- Socket => $socket,
- Config => $conf,
- numeric => sub {},
- privmsg => sub {},
- notice => sub {},
- chan_msg => sub {});
- bless \%hash, $class;
- }
- sub send_raw {
- my $self = shift;
- my $data = shift;
- $self->{Socket}->send($data."\n");
- puts(">> $data");
- }
- sub puts($) {
- my $text = shift;
- print "$text\n";
- }
- sub connect {
- my $self = shift;
- my $user_conf = $self->{Config}->{user};
- $self->send_raw("USER $$user_conf{user} 0 0 :$$user_conf{real}");
- $self->send_raw("NICK $$user_conf{nick1}");
- }
- sub register {
- my $self = shift;
- my ($command, $ref) = @_;
- $self->{lc $command} = $ref;
- }
- sub breakdown {
- my ($type,$data) = @_;
- $data =~ s/.//;
- given($type) {
- when('privmsg') {
- return {
- nick => (split /\!/, $data)[0],
- user => (split /\@/, (split /\!/, $data)[1])[1],
- host => (split /\s/, (split /\@/, $data)[1])[0],
- msg => (split /\:/, $data, 2)[1]};
- }
- when('chan_msg') {
- return {
- nick => (split /\!/, $data)[0],
- user => (split /\@/, (split /\!/, $data)[1])[1],
- host => (split /\s/, (split /\@/, $data)[1])[0],
- chan => (split /\s/, $data)[2],
- msg => (split /\:/, $data, 2)[1]};
- }
- when('notice') {
- return {
- nick => (split /\!/, $data)[0],
- user => (split /\@/, (split /\!/, $data)[1])[1],
- host => (split /\s/, (split /\@/, $data)[1])[0],
- msg => (split /\:/, $data, 2)[1]};
- }
- }
- }
- sub main {
- my $self = shift;
- my $socket = $self->{Socket};
- while (my $data = <$socket>) {
- print "<< $data";
- $data =~ s/(\n|\r|\0)//g;
- my $priv_val = (split /\s+/, $data)[2];
- my $type = (split /\s+/, $data)[1];
- given ($type) {
- when(/\d\d\d/) {
- $self->{numeric}->($type, $data);
- }
- when(/PRIVMSG/i) {
- if ($priv_val =~ m/^\#/) {
- $self->{last_message} = breakdown('chan_msg', $data); #make sure it returns a hash reference
- $self->{chan_msg}->($data);
- }
- else {
- $self->{last_message} = breakdown('privmsg', $data);
- $self->{privmsg}->($data);
- }
- }
- when(/NOTICE/i) {
- $self->{notice}->($data);
- }
- }
- if ($data =~ m/^PING/i) {
- my $pong = (split(':',$data,2))[1];
- $self->send_raw("PONG :$pong");
- }
- }
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement