Difference between revisions of "Irc.pl"

From Organic Design wiki
m
(change main loop to use IO:Select so no wait state, and multi-server support)
Line 4: Line 4:
  
 
use IO::Socket;
 
use IO::Socket;
 +
use IO::Select;
  
 
$user    = 'odserver';
 
$user    = 'odserver';
Line 39: Line 40:
  
 
# Main channel activity oop
 
# Main channel activity oop
while (<$sock>) {
+
my %streams = ();
 +
my $buffer = '';
 +
my $select = new IO::Select $sock;
 +
while (1) {
 +
for my $handle ($select->can_read(1)) {
 +
my $stream = fileno $handle;
 +
 
 +
if (sysread $handle, my $data, 100) {
 +
 
 +
# Append the data to the appropriate stream
 +
$streams{$stream} = exists($streams{$stream}) ? $streams{$stream}.$data : $data;
 +
 
 +
# Remove and process any complete messages in the stream
 +
if ($streams{$stream} =~ s/^(.*\r?\n)//s) {
 +
for (split /\r?\n/, $1) {
  
($command, $text) = split(/ :/, $_);
+
($command, $text) = split(/ :/, $_);
  
# Respond to pings if any
+
# Respond to pings if any
if ($command eq 'PING') {
+
if ($command eq 'PING') {
$text =~ s/[\r\n]//g;
+
$text =~ s/[\r\n]//g;
print $sock "PONG $text\n";
+
print $handle "PONG $text\n";
next;
+
next;
}
+
}
  
# Extract info and tidy
+
# Extract info and tidy
($nick, $type, $chan) = split(/ /, $_);
+
($nick, $type, $chan) = split(/ /, $_);
($nick, $host) = split(/!/, $nick);
+
($nick, $host) = split(/!/, $nick);
$nick =~ s/://;
+
$nick =~ s/://;
$text =~ s/[:\r\n]+//;
+
$text =~ s/[:\r\n]+//;
  
# Process if the line is a message in the channel
+
# Process if the line is a message in the channel
if ($chan eq $channel) {
+
if ($chan eq $channel) {
$ts = localtime();
+
$ts = localtime();
$ts = $1 if $ts =~ /(\d\d:\d\d:\d\d)/;
+
$ts = $1 if $ts =~ /(\d\d:\d\d:\d\d)/;
print "($ts) $nick: $text\n";
+
print "($ts) $nick: $text\n";
  
# Respond if message is known
+
# Respond if message is known
if ($text =~ /(^|\W)$user(\W|$)/i) {
+
if ($text =~ /(^|\W)$user(\W|$)/i) {
$msg = "Yo $nick, you talking to me?";
+
$msg = "Yo $nick, you talking to me?";
print $sock "PRIVMSG $channel :$msg\n";
+
print $handle "PRIVMSG $channel :$msg\n";
print "($ts) $user: $msg\n";
+
print "($ts) $user: $msg\n";
 +
}
 +
}
 +
}
 +
}
 
}
 
}
 
}
 
}
 
 
}
 
}

Revision as of 23:45, 21 June 2008

  1. !/usr/bin/perl
  2. Code to connect to an IRC channelOur Perl scripts.
  3. Started: 2008-06-20

use IO::Socket; use IO::Select;

$user = 'odserver'; $pass = '********'; $channel = '#organicdesign';

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

while (<$sock>) { if (/(NOTICE AUTH).*(checking ident)/i) { print $sock "NICK $user\nUSER $user 0 0 :odbot.pl\n"; last; } }

while (<$sock>) {

# if the server asks for a ping print $sock "PONG :".(split(/ :/, $_))[1] if /^PING/;

# end of MOTD section if (/ (376|422) /) { print $sock "NICKSERV :identify $user $pass\n"; last; } }

  1. Wait for a few secs and join the channel

sleep 3; print $sock "JOIN $channel\n";

  1. Main channel activity oop

my %streams = (); my $buffer = ; my $select = new IO::Select $sock; while (1) { for my $handle ($select->can_read(1)) { my $stream = fileno $handle;

if (sysread $handle, my $data, 100) {

# Append the data to the appropriate stream $streams{$stream} = exists($streams{$stream}) ? $streams{$stream}.$data : $data;

# Remove and process any complete messages in the stream if ($streams{$stream} =~ s/^(.*\r?\n)//s) { for (split /\r?\n/, $1) {

($command, $text) = split(/ :/, $_);

# Respond to pings if any if ($command eq 'PING') { $text =~ s/[\r\n]//g; print $handle "PONG $text\n"; next; }

# Extract info and tidy ($nick, $type, $chan) = split(/ /, $_); ($nick, $host) = split(/!/, $nick); $nick =~ s/://; $text =~ s/[:\r\n]+//;

# Process if the line is a message in the channel if ($chan eq $channel) { $ts = localtime(); $ts = $1 if $ts =~ /(\d\d:\d\d:\d\d)/; print "($ts) $nick: $text\n";

# Respond if message is known if ($text =~ /(^|\W)$user(\W|$)/i) { $msg = "Yo $nick, you talking to me?"; print $handle "PRIVMSG $channel :$msg\n"; print "($ts) $user: $msg\n"; } } } } } } }