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";
}
}
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.
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
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!"); }
}
Also you could do this:-
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;
if($line =~ /^PING/){
print $sock "PONG :" . (split(/ :/, $line))[1];
}
if($line =~ /(376|422)/i){
print $sock "NICKSERV :identify nick_password\n";
last;
}
}