All pastes #275772 Raw Edit

XChat Script

public text v1 · immutable
·published 2006-12-11 20:10 UTC
rendered paste body
#! /usr/bin/perl

## Using
use strict;
use warnings;

## Modules
use Time::HiRes qw(usleep);
use YAML::Syck;

## Init
my $title = 'Bot';
my $version = '1.0';
my $desc = 'Robot';

Xchat::register($title, $version, $desc);
Xchat::print('Loading ' . $title . '... ');

Xchat::hook_server("RAW LINE", \&OnIncoming);
Xchat::hook_command("", \&OnOutgoing);

## Subs
## Events
sub OnIncoming
{
	my @parts = @{$_[0]};
	my $message = join(' ', @parts);
	
	my $mynick = Xchat::get_info('nick');
	my $server = Xchat::get_info('server');
	
	&ParseIncoming(\@parts);
	
	return Xchat::EAT_ALL;
}

sub OnOutgoing
{
	my @parts = @{$_[0]};
	my $message = join(' ', @parts);
	
	my $mynick = Xchat::get_info('nick');
	my $server = Xchat::get_info('server');
	my $xchatdir = Xchat::get_info('xchatdir');
	
	my $configfile = $xchatdir . '/bot.conf';
	my $file = LoadFile($configfile);
	my %config = %{$file};
	
	return &ParseOutgoing(\@parts, \%config);
}

## Parsing Subs
sub ParseIncoming
{
	my @parts = @{$_[0]};
	my $sender = shift @parts;
	my $command = shift @parts;
	my $recipient = shift @parts;
	my $message = join(' ', @parts);
	my $wholemessage = join(' ', $sender, $command, $recipient, $message);
	
	my $mynick = Xchat::get_info('nick');
	my $server = Xchat::get_info('server');
	
	if ($sender =~ /^:(.+)!(.+)@(.+)$/) {
		my $snick = $1;
		my $suser = $2;
		my $shost = $3;
		
		if ($snick eq $mynick) {
			if ($command eq 'PRIVMSG') {
				if ($message =~ /^:!(\S+)( (.+))?$/) {
					my $command = $1;
					my $text = $3;
					
					if ($command =~ /^testfunc$/) {
						my $str = '54(0) F';
						my $find = '(\d+)\(0\)';
						my $repl = '$1\x{00B0}';
						$str =~ s/(^| )(\d+)\(0\)( |$)/$1$2\x{00B0}$3/g;
						&OutputEmit($str);
					} elsif ($command =~ /^testfunc2$/) {
					} elsif ($command =~ /^testfunc3$/) {
					} elsif ($command =~ /^testfunc4$/) {
					} elsif ($command =~ /^testfunc5$/) {
					} elsif ($command =~ /^testfunc6$/) {
					} elsif ($command =~ /^testfunc7$/) {
						my $str = '54(0) F';
						my $find = '(\d+)\(0\)';
						my $repl = '$1\x{00B0}';
						$str =~ s/^$find /$1\x{00B0} /g;
						&OutputEmit($str);
					} else {
						&OutputEmit('Unknown Command');
					}
				} elsif ($message =~ /^:(.+)$/) {
					&ReceiveRaw($wholemessage);
					&Output($wholemessage);
				} else {
					&ReceiveRaw($wholemessage);
					&Output($wholemessage);
				}
			} elsif ($command eq 'PING') {
				&ReceiveRaw($wholemessage);
				&Output('Ping');
			} elsif ($command eq 'PONG') {
				&ReceiveRaw($wholemessage);
				&Output('Pong');
			} else {
				&ReceiveRaw($wholemessage);
				&Output($wholemessage);
			}
		} else {
			&ReceiveRaw($wholemessage);
			&Output($wholemessage);
		}
	} elsif ($sender =~ /^:$server$/) {
		if ($command eq 'PRIVMSG') {
			&ReceiveRaw($wholemessage);
			&Output($wholemessage);
		} elsif ($command eq 'PING') {
			&ReceiveRaw($wholemessage);
			&Output('Server Ping');
		} elsif ($command eq 'PONG') {
			&ReceiveRaw($wholemessage);
			&Output('Server Pong');
		} else {
			&ReceiveRaw($wholemessage);
			&Output($wholemessage);
		}
	} else {
		&ReceiveRaw($wholemessage);
		&Output($wholemessage);
	}
}

sub ParseOutgoing
{
	my @parts = @{$_[0]};
	my %config = %{$_[1]};
	my $message = join(' ', @parts);
	
	my $mynick = Xchat::get_info('nick');
	my $server = Xchat::get_info('server');
	
#	$message =~ s/(\d+)\(0\)/$1 . "\x{00B0}"/eg;

#	$message = &ReplaceWord($message, '(\d+)\(0\)', '$1\x{00B0}');
#	$message = &ReplaceWord($message, '\(c\)', '\x{00A9}');
#	$message = &ReplaceWord($message, '\(r\)', '\x{00AE}');
#	$message = &ReplaceWord($message, '\(beep\)', '\x{0007}');
#	$message = &ReplaceWord($message, '[*\\]nix', 'linux');
#	$message = &ReplaceWord($message, '(\S+)\@gamil.com', '$1\@gmail.com');
	
	my @replace = @{$config{replace}};
	
	for (my $i = 0; $i < scalar @replace; $i++) {
		my %item = %{$replace[$i]};
		$message = &ReplaceWord($message, $item{find}, $item{replace});
	}
	
	Xchat::command('SAY ' . $message);
	return Xchat::EAT_ALL;
}

## Send/Receive
sub Send
{
	my $command = $_[0];
	my $recipient = $_[1];
	my $message = $_[2];
	
	my $server = Xchat::get_info('server');
	
	Xchat::command("QUOTE $command $recipient :$message", undef, $server);
}

sub SendRaw
{
	my $raw = $_[0];
	
	my $server = Xchat::get_info('server');
	
	Xchat::command("QUOTE $raw", undef, $server);
}

sub Receive
{
	my $sender = $_[0];
	my $command = $_[1];
	my $recipient = $_[2];
	my $message = $_[3];
	
	my $server = Xchat::get_info('server');
	
	Xchat::command("RECV :$sender $command $recipient :$message", undef, $server);
}

sub ReceiveRaw
{
	my $raw = $_[0];
	
	my $server = Xchat::get_info('server');
	
	Xchat::command("RECV $raw", undef, $server);
}

sub Output
{
	my $message = $_[0];
	
	my $mynick = Xchat::get_info('nick');
	my $server = Xchat::get_info('server');
	
	Xchat::command("RECV :RawLog!bot\@bot.andrewh.com PRIVMSG $mynick :$message", undef, $server);
}

sub OutputEmit
{
	my $message = $_[0];
	my $sender  = 'Bot';;
	
	if ($_[1]) {
		$sender = $_[1];
	}
	
	Xchat::emit_print('Channel Message', ($sender, $message, '', ''));
}

## General Subs
sub Sleep #### USE HOOK_TIMEOUT
{
	my $seconds = $_[0];
	my $slept = usleep(1000000 * $seconds) / 1000000;
	return $slept;
}

sub ReplaceWord
{
	my $text = $_[0];
	my $word = $_[1];
	my $replcement = $_[2];
	
	eval('$text =~ s/^$word /' . $replcement . ' /g');
	eval('$text =~ s/ $word / ' . $replcement . ' /g');
	eval('$text =~ s/ $word$/ ' . $replcement . '/g');
	eval('$text =~ s/^$word$/' . $replcement . '/g');
	
	return $text;
}

sub Rand
{
	my $min = $_[0];
	my $max = $_[1];
	my $num = $min + rand($max - $min);
	return $num;
}

## End
Xchat::print("Done\n");