Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Network Programming with Perl    (ISBN: 0201615711)


 

 List Price: $44.95
 Our Price: $44.95
 Used Price: $22.49

 Release Date: 27 December, 2000
 Manufacturer: Addison-Wesley Pub Co (Paperback)
 Sales Rank: 63,530

 Author: Lincoln D. Stein









More Info

 Perl IRC Bot 2002-03-14 00:57:47
  perl 
Category: source:perl:internet
Description: Example of how to use POE::Component::IRC to create an irc bot.
Platform: unix
Author: detour
Viewed: 16338
Rating: 4.3/5 (88 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
ircbot.pl
#!/usr/bin/perl
#
# ircbot.pl written by detour@metalshell.com
#
# This irc bot will handle and print the motd, users on a channel
# joins, parts, quits, nick changes, and public msgs's.
#
# You can bind a sub to any numeric reply by using irc_<num>.
# For example 376 is the RPL_ENDOFMOTD, so to catch this response
# you add irc_376 => \&your_sub to your session. 
#
# This requires that you have POE and POE::Component::IRC
# installed.  A simple way to do this is using the cpan module.
# perl -MCPAN -eshell
# cpan> install POE
# cpan> install POE::Component::IRC
#
# http://www.metalshell.com


use strict;

use POE;
use POE::Component::IRC;

POE::Component::IRC->new("irc_client");

POE::Session->new ( _start     => \&irc_start,
                    irc_join   => \&irc_join,
                    irc_part   => \&irc_part,
                    irc_nick   => \&irc_nick,
                    irc_quit   => \&irc_quit,
                    irc_376    => \&irc_connect,  #end of motd
                    irc_372    => \&irc_motd,
                    irc_353    => \&irc_names,
                    irc_public => \&irc_pub_msg, );

sub irc_start {
  # KERNEL, HEAP, and SESSION are constants exported by POE
  my $kernel = $_[KERNEL];
  my $heap = $_[HEAP];
  my $session = $_[SESSION];

  $kernel->refcount_increment( $session->ID(), "my bot");
  $kernel->post(irc_client=> register=> "all");

  $kernel->post(irc_client=>connect=> {  Nick     => 'ircbot024',
                                         Username => 'username',
                                         Ircname  => 'Metal Shell',
                                         Server   => 'irc.homelien.no',
                                         Port     => '6667',
                                      }
                );
}

sub irc_connect {
  my $kernel = $_[KERNEL];

  $kernel->post(irc_client=>join=>'#metalshell');
}

sub irc_motd {
  my $msg = $_[ARG1];

  print "MOTD: $msg\n";
}

sub irc_names {
  my $names = (split /:/, $_[ARG1])[1];
  my $channel = (split /:/, $_[ARG1])[0];

  $channel =~ s/[@|=] (.*?) /$1/;

  print "#-> Users on $channel [ $names ]\n";
}

#nick change
sub irc_nick {
  my $oldnick = (split /!/, $_[ARG0])[0];
  my $newnick = $_[ARG1];

  print "#-> $oldnick is now known as $newnick\n";
}

#user parted
sub irc_part {
  my $nick = (split /!/, $_[ARG0])[0];
  my $channel = $_[ARG1];

  print "#-> $nick has parted $channel\n";
}

#user joined
sub irc_join {
  my $nick = (split /!/, $_[ARG0])[0];
  my $channel = $_[ARG1];

  print "#-> $nick has joined $channel\n";
}

#user quit
sub irc_quit {
  my $nick = $_[ARG0];
  my $reason = $_[ARG1];

  print "#-> $nick has quit ($reason)\n";
}

sub irc_pub_msg{
  my $kernel = $_[KERNEL];
  my $nick = (split /!/, $_[ARG0])[0];
  my $channel = $_[ARG1]->[0];
  my $msg = $_[ARG2];

  print "$channel: <$nick> $msg\n";
}

#start everything
$poe_kernel->run();
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?