Perl IRC bot

By supafreky on Jan 24, 2009

Connects to an IRC server you specify and idles. Basically this is a frame for an IRC bot.

use IO::Socket;

fork;

$server = "irc.kingskrown.com";
$port = 6667;
$nick = "pl";
$user = "svs";
$name = "perl";

$fd = new IO::Socket::INET (PeerAddr => $server,
                           PeerPort => $port,
                           Proto => "tcp");

print $fd "NICK $nick\r\n";
print $fd "USER $user \"\" \"\" :$name\r\n";
while ($in = <$fd>) {
   if ($in =~ /^PING (:[^ ]+)$/i) {
      print $fd "PONG :$1\r\n";
   }
}

Comments

Sign in to comment.
dimecadmium   -  Jul 10, 2009

k0pp: (1) check the last line of that first while: "last;" - so no, it's NOT an infinite loop.
(2) hmm, perhaps you should look again: use IO::Socket; use warnings; use strict;
(3) perhaps not everyone is advanced enough to PoCo or simply doesn't want to use it. i know that was the case for me when i wrote old bots.

 Respond  
k0pp   -  Jul 08, 2009

Also, watch those 'bad habits' yourself...

You forgot

#!/usr/bin/perl
use warnings;
use strict;

Don't wanna get into bad habits now ;)
use IO::Socket;
use warnings;
use strict;

$sock = IO::Socket::INET->new(
PeerAddr => 'server_here',
PeerPort => 6667,
Proto => 'tcp' ) or die "could not make the connection";

You forgot to actuall `use' strict.

Not to mention... youre re-inventing the wheel. look into PoCo::IRC

 Respond  
k0pp   -  Jul 08, 2009

sorry to bust your bubble kid, but the first loop is all that will be engaged since its infinite. also, why not the newlines off...

#!/usr/bin/perl

use warnings;
use strict;

use IO::Socket::INET;

my ($host,$port,$nick,$ident,$real,$chan) = @ARGV;
die("usage: $0 <irc.server.net> <6667> <nick> <ident> <realname> <#channel>") unless $chan;

my $socket = IO::Socket::INET->new( PeerAddr => $chan,
                                    PeerPort => $port,
                                    Proto    => 'tcp',
                                    Timeout  => 2,
) or die("Cant create socket: $!");

sub smsg { print $socket "PRIVMSG $_[0] :$_[1]\r\n"; }
sub sraw { print $socket "$_[0]\r\n"; }

sraw("NICK $nick");
sraw("USER $ident 8 * :$real");
sraw("JOIN $chan");

while(chomp(my $body = <$socket>)){
  if($body =~ /PING(.+)/){ sraw("PONG $1"); }
  if($body =~ /$chan :ohai $nick/i){ smsg($chan,"ohai back!"); }
}
 Respond  
Tea   -  Jul 07, 2009

Also you could do this:-

!/usr/bin/perl

use IO::Socket;
use warnings;
use strict;

$sock = IO::Socket::INET->new(
PeerAddr => 'server_here',
PeerPort => 6667,
Proto => 'tcp' ) or die "could not make the connection";

while($line = <$sock>){
print $line;
if($line =~ /(NOTICE AUTH).*(checking ident)/i){
print $sock "NICK nick_here\nHelpBot :just a bot\n";
last;
}
}

while($line = <$sock>){
print $line;

use next line if the server asks for a ping

if($line =~ /^PING/){
    print $sock "PONG :" . (split(/ :/, $line))[1];
}
if($line =~ /(376|422)/i){
    print $sock "NICKSERV :identify nick_password\n";
    last;
}

}

 Respond  
Tea   -  Jul 07, 2009

You forgot

!/usr/bin/perl

use warnings;
use strict;

Don't wanna get into bad habits now ;)

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.