[ Log In | Register ]       
» Resource Database Index -> Source Code -> A Simple Perl OOP IRC Bot
Description // Info

An OOP IRC bot with an easy to use interface to add commands and server reply hooks.
Source Code

  1. #FILE - IRC/Socket.pm
  2.  
  3. package IRC::Socket;
  4.  
  5. use strict;
  6.  
  7. use IO::Socket;
  8. use IRC::Handler;
  9. use Data::Dumper;
  10.  
  11. sub new {
  12.     my ($class, $server, $port) = @_;
  13.     $port ||= 6667;
  14.  
  15.     my $self = {
  16.         server => $server,
  17.         port => $port,
  18.         count => {
  19.             sent => 0,
  20.             received => 0,
  21.         }
  22.     };
  23.    
  24.     $self->{connected} = 0;
  25.     $self->{handler} = IRC::Handler->new($class);
  26.     $self->{commands} = IRC::Handler->new($class);
  27.    
  28.     bless $self, $class;
  29.     return $self;
  30. }
  31.  
  32. sub is_connected {
  33.     my $self = shift;
  34.     return $self->{connected};
  35. }
  36.  
  37. sub add_handle {
  38.     my $self = shift;
  39.     my ($handle, $subref) = @_;
  40.     $self->{handler}->add_handle($handle, $subref);
  41. }
  42.  
  43. sub add_command {
  44.     my $self = shift;
  45.     my ($command, $subref) = @_;
  46.     $self->{commands}->add_handle($command, $subref);
  47. }
  48.  
  49. sub connect {
  50.     my $self = shift;
  51.     return if $self->is_connected();
  52.    
  53.     my ($server, $port) = ($self->{server}, $self->{port});
  54.    
  55.     if ($server && $port) {
  56.         $self->{sock} = IO::Socket::INET->new(
  57.             PeerAddr => $server,
  58.             PeerPort => $port,
  59.             Proto => 'tcp'
  60.         ) || die "Unable to connect";
  61.        
  62.         print "Connecting to $server:$port...n";
  63.         $self->{connected} = 1;
  64.     } else {
  65.         die("Server or port is undefined, cannot connect");
  66.     }
  67. }
  68.  
  69. sub read {
  70.     my $self = shift;
  71.     return if !$self->is_connected();
  72.     my $line = $self->{sock}->getline;
  73.     chomp($line);
  74.    
  75.     my @data = split(' ', $line, 4);
  76.    
  77.     $self->write("PONG $data[1]") if ($data[0] eq 'PING');
  78.    
  79.     if ($self->{handler}->has_handle($data[1])) {
  80.         $self->{handler}->exec_handle($data[1], $self);
  81.     }
  82.    
  83.     if ($data[1] eq 'PRIVMSG') {
  84.         my $nick;
  85.         if ($data[0] =~ /^:(S+?)!/) { $nick = $1; }
  86.         my $chan = $data[2];
  87.         my $msg = $data[3];
  88.         $msg =~ s/^://;
  89.         my @msgargs = split(' ', $msg);
  90.        
  91.         if ($self->{commands}->has_handle($msgargs[0])) {
  92.             my $command = $msgargs[0];
  93.             $msg =~ s/^$commands*//;
  94.             $self->{commands}->exec_handle($command, $self, $nick, $chan, $msg);
  95.         }
  96.     }
  97.    
  98.     $self->{count}->{received}++;
  99.     return $line;
  100. }
  101.  
  102. sub write {
  103.     my $self = shift;
  104.     my ($str) = @_;
  105.     return if !$self->is_connected();
  106.     $self->{count}->{sent}++;
  107.     $self->{sock}->print($str."rn");
  108. }
  109.  
  110. # Login to our IRC server
  111. sub login {
  112.     my $self = shift;
  113.     my ($nickname, $username, $realname, $serverpass) = @_;
  114.     $self->write("PASS $serverpass") if $serverpass;
  115.     $self->write("NICK $nickname");
  116.     $self->write("USER $username $nickname * :$realname");
  117. }
  118.  
  119. sub privmsg {
  120.     my $self = shift;
  121.     my ($channel, $message) = @_;
  122.     $self->write("PRIVMSG $channel :$message");
  123. }
  124.  
  125. sub notice {
  126.     my $self = shift;
  127.     my ($channel, $message) = @_;
  128.     $self->write("NOTICE $channel :$message");
  129. }
  130.  
  131. sub identify {
  132.     my $self = shift;
  133.     my ($pass) = @_;
  134.     $self->privmsg('NickServ', "IDENTIFY $pass");
  135. }
  136.  
  137. # Join a list of channels, if using a channel password
  138. # use #channel:password
  139. sub join {
  140.     my $self = shift;
  141.     for (@_) {
  142.         if (my $pass = $_ =~ /^#S+:(S+)$/) {
  143.             $self->write("JOIN $_ $pass");
  144.         } else {
  145.             $self->write("JOIN $_");
  146.         }
  147.     }
  148. }
  149.  
  150. # Leave a list of channels
  151. sub part {
  152.     my $self = shift;
  153.     my $reason = shift || 'Leaving..';
  154.     for (@_) {
  155.         $self->write("PART $_");
  156.     }
  157. }
  158.  
  159. # Send our QUIT message and close our IRC connection
  160. sub close {
  161.     my $self = shift;
  162.     return unless $self->is_connected();
  163.     $self->write("QUIT");
  164.     # close our IRC socket $self->{sock}
  165.     exit;
  166. }
  167.  
  168.  
  169. 1;
  170.  
  171. #END FILE - IRC/Socket.pm
  172.  
  173. #FILE - IRC/Handler.pm
  174.  
  175. package IRC::Handler;
  176.  
  177. use strict;
  178.  
  179. sub new {
  180.     my $class = shift;
  181.     my $irc = shift;
  182.    
  183.     my $self = {
  184.         hooks => {}
  185.     };
  186.    
  187.     bless $self, $class;
  188.     return $self;
  189. }
  190.  
  191. sub add_handle {
  192.     my $self = shift;
  193.     my ($key, $subref) = @_;
  194.     $self->{hooks}->{$key} = $subref;
  195. }
  196.  
  197. sub exec_handle {
  198.     my $self = shift;
  199.     my $key = shift;
  200.     my ($irc, $nick, $chan, $msg) = @_;
  201.    
  202.     my $subref = $self->{hooks}->{$key};
  203.     $subref->($irc, $nick, $chan, $msg);
  204. }
  205.  
  206. sub has_handle {
  207.     my $self = shift;
  208.     my ($handle) = @_;
  209.     if (exists($self->{hooks}->{$handle})) {
  210.         return 1;
  211.     }
  212.     return;
  213. }
  214.  
  215. 1;
  216.  
  217. #END FILE - IRC/Handler.pm
  218.  
  219. #FILE - run.pl
  220.  
  221. #!/usr/bin/perl -w
  222.  
  223. use strict;
  224. use warnings;
  225.  
  226. use IRC::Socket;
  227.  
  228. my $irc = IRC::Socket->new('irc.2600.net');
  229. $irc->connect();
  230. $irc->login('Sponge', 'Spongey', 'Sponge Bawb');
  231. #$irc->identify('somepassword');
  232.  
  233. # adds a handle for end of MOTD
  234. # and executes our 'join' subroutine
  235. $irc->add_handle('376', &join);
  236.  
  237. $irc->add_command('!say', &say);
  238. $irc->add_command('!die', &quit);
  239.  
  240. while (1) {
  241.     while ($irc->is_connected()) {
  242.         my $line = $irc->read();
  243.        
  244.         print $line."n";
  245.     }
  246. }
  247.  
  248. sub join {
  249.     my $irc = shift;
  250.     $irc->join('#sponge');
  251. }
  252.  
  253. # !say foo bar baz
  254. # bot replys back in channel with the text 'foo bar baz'
  255. sub say {
  256.     my ($irc, $nick, $chan, $msg) = @_;
  257.     $irc->privmsg($chan, $msg);
  258. }
  259.  
  260. # !quit
  261. # bot quits the irc server
  262. sub quit {
  263.     my $irc = shift;
  264.     $irc->quit();
  265. }


Comments

Add Comment
Please login to leave a comment!


 Network Access...
USER ID
PASSWORD

 Share Exploit
Share






 Code Information
Code Name:
A Simple Perl OOP IRC Bot

Author:
Author Name:
H4z3
Author's Website:

Author's Email:


Code Details:
Code Version: 1.0
Code Submission Date:
Code Submission Time 20:25:33
Submitted By: agri
Language: Perl

Code Ranking: 0 (you must be logged in to rank and vote on source code!)

Tags:


[ Download | Report Issue ]

 Code Search




 

LOL LEGAL STUFF #
By continuing past this page, and by your continued use of this site, you agree to be bound by and abide by the User Agreement.

© 2008-2011 r00tsecurity network. All rights reserved.
About Us | Contact Us | Support Us | Legal | Advertise | Privacy Policy

r00tsecurity's network is powered and running off MeowPower Enterprise Software and is professionally monitored and managed by the Kittens In Suits™.

COMMONLY ACCESSED #
INSPIRE ME #
" For you have been my hope, Sovereign LORD, my confidence since my youth. "
~ Psalm 71:5