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; #Get the class name required for bless
- my $config = conf->new(shift); #Get the config file name
- my %s_conf = $config->allocate('server'); #Server Config
- my %u_conf = $config->allocate('user'); #User Info Config
- my %o_conf = $config->allocate('owner'); #Owner Config
- my $conf = {server => \%s_conf, owner => \%o_conf, user =>\%u_conf}; #Create a reference to the different configurations
- my $socket = new IO::Socket::INET(PeerAddr => $s_conf{network}, #Creating the socket connection
- PeerPort => $s_conf{port}, #....
- Proto => 'tcp') or die #Could the connection be made?
- "Can't connect to ".$s_conf{network}.":".$s_conf{port}."\n"; #If not then die and tell us that something went wrong.
- my %hash = (Socket => $socket, #Essentially IRC::init but I'm using it in init.
- Config => $conf, #.....
- numeric => sub {}, #.....
- privmsg => sub {}, #.....
- notice => sub {}, #.....
- chan_msg => sub {}); #.....
- bless \%hash, $class; #blessing the class
- }
- sub init {
- my $self = shift;
- $self->register('notice', sub { });
- $self->register('privmsg', sub { });
- $self->register('chan_msg', sub { });
- $self->register('numeric', sub { });
- }
- 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) = @_;
- given(type) {
- when('privmsg') {
- my @s = split ' ',$data;
- }
- }
- }
- sub main {
- my $self = shift;
- my $socket = $self->{Socket};
- while (my $data = <$socket>) {
- print "<< $data";
- my @split = split ' ', $data;
- my $type = $split[1];
- given ($type) {
- when(/\d\d\d/) {
- $self->{numeric}->($type, $data);
- }
- when(/PRIVMSG/i) {
- if ($split[2] =~ m/^\#/) {
- # $self->{last_message} = breakdown('chan_msg', $data); #make sure it returns a hash reference
- $self->{chan_msg}->($data);
- }
- else {
- # $self->{last_message} = breadown('privmsg', $data);
- $self->{privmsg}->($data);
- }
- }
- when(/NOTICE/i) {
- $self->{notice}->($data);
- }
- when(/PING/i) {
- my @line = split(':',$data,2);
- print @line;
- $self->send_raw("PONG :".$line[1]);
- }
- }
- }
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement