Whitespace and indentation cleanup

This commit is contained in:
thomas.figg 2009-03-10 23:47:06 +00:00
parent 10e7d2c630
commit 2195db8c1b
1 changed files with 149 additions and 162 deletions

View File

@ -12,11 +12,11 @@ my %CONF;
&readconf(@ARGV);
my $socket = new IO::Socket::INET(
PeerAddr => $CONF{server},
PeerPort => $CONF{port},
proto => 'tcp',
Type => SOCK_STREAM,
Timeout => 10
PeerAddr => $CONF{server},
PeerPort => $CONF{port},
proto => 'tcp',
Type => SOCK_STREAM,
Timeout => 10
) or die "socket: $!";
_send("NICK $CONF{nick}");
@ -45,16 +45,16 @@ $CONF{channel} =~ s/\s+//;
# add friends from conf file
%friends = map {
(my $friend = $_) =~ s/^\s+|\s+$//g;
(my $friend = $_) =~ s/^\s+|\s+$//g;
$friend, 1;
$friend, 1;
} split /,/, $CONF{friends} if $CONF{friends};
# add enemies from conf file
%enemies = map {
(my $enemy = $_) =~ s/^\s+|\s+$//g;
(my $enemy = $_) =~ s/^\s+|\s+$//g;
$enemy, 1;
$enemy, 1;
} split /,/, $CONF{enemies} if $CONF{enemies};
#== forever butting... ========================================================
@ -64,165 +64,154 @@ process() while 1;
#== subroutines ===============================================================
sub process {
die "main: $!" if $!;
die "main: $!" if $!;
process_line($_) for split /\n/, gets();
process_line($_) for split /\n/, gets();
}
sub process_line {
my $line = shift;
print "$line\n";
my $line = shift;
print "$line\n";
my ($from, $command, @data) = split /\s+/, $line;
my ($from, $command, @data) = split /\s+/, $line;
$from = defined $from ? $from : '';
$command = defined $command ? $command : '';
$from = defined $from ? $from : '';
$command = defined $command ? $command : '';
die "from server: @data" if $from eq 'ERROR';
die "from server: @data" if $from eq 'ERROR';
# if server pings, ping back.
pong($command =~ /^:\d+$/ ? $command : ":$CONF{nick}") if $from eq 'PING';
# if server pings, ping back.
pong($command =~ /^:\d+$/ ? $command : ":$CONF{nick}") if $from eq 'PING';
# If buttbot has successfully connected to the server, join a channel.
if ($command eq '001') {
cmd_connect();
# If buttbot has successfully connected to the server, join a channel.
if ($command eq '001') {
cmd_connect();
# otherwise, if it's a message
} elsif ($command eq 'PRIVMSG') {
cmd_privmsg($from, @data);
# otherwise, if it's a message
} elsif ($command eq 'PRIVMSG') {
cmd_privmsg($from, @data);
}
}
}
sub cmd_connect {
_send("MODE $CONF{nick} -x"); # hiding hostnames is for wimps.
if (defined $CONF{channel})
{
_send("JOIN $CONF{channel}") ;
}
_send("MODE $CONF{nick} -x"); # hiding hostnames is for wimps.
if (defined $CONF{channel}) {
_send("JOIN $CONF{channel}") ;
}
if (defined $CONF{nickpass})
{
_send("NICKSERV :identify $CONF{nickpass}");
}
if (defined $CONF{nickpass})
{
_send("NICKSERV :identify $CONF{nickpass}");
}
}
sub cmd_privmsg {
my($from, @data) = @_;
my($from, @data) = @_;
# get destination of message
my $to = shift @data;
# get destination of message
my $to = shift @data;
# get first word of message (might be command)
my $sub = shift @data;
# get first word of message (might be command)
my $sub = shift @data;
## remove preceding ':'
$sub =~ s/^://;
## remove preceding ':'
$sub =~ s/^://;
# if a user private messages the bot...
if ($to eq $CONF{nick}) {
pm_bot($from, $sub, @data);
# if a user private messages the bot...
if ($to eq $CONF{nick}) {
pm_bot($from, $sub, @data);
#if messages come from channel, start buttifying
} elsif ($to =~ /^\#/) {
pm_channel($from, $to, $sub, @data);
}
#if messages come from channel, start buttifying
} elsif ($to =~ /^\#/) {
pm_channel($from, $to, $sub, @data);
}
}
sub pm_bot {
my ($from, $sub, @data) = @_;
my ($from, $sub, @data) = @_;
my $to = $from;
my $to = $from;
$to =~ s/^:(.*)!.*$/$1/;
#If the command is !butt, buttify message.
##if the first word in the string is equal to the password, set the user to be the admin
if ($sub eq $CONF{pass}) {
$auth=$from;
}
$to =~ s/^:(.*)!.*$/$1/;
#If the command is !butt, buttify message.
##if the first word in the string is equal to the password, set the user to be the admin
if ($sub eq $CONF{pass}) {
$auth=$from;
}
##ADMIN FUNCTIONS
if ($auth eq $from) {
if ($sub eq "!join" and @data > 0)
{
$CONF{channel} = $CONF{channel}.",";
$CONF{channel} = $CONF{channel}.$data[0];
_send("JOIN $data[0]");
}
elsif ($sub eq "!leave" and @data > 0)
{
$CONF{channel} =~ s/$data[0]//;
_send("PART $data[0]");
}
}
##ADMIN FUNCTIONS
if ($auth eq $from) {
if ($sub eq "!join" and @data > 0) {
$CONF{channel} = $CONF{channel}.",";
$CONF{channel} = $CONF{channel}.$data[0];
_send("JOIN $data[0]");
} elsif ($sub eq "!leave" and @data > 0) {
$CONF{channel} =~ s/$data[0]//;
_send("PART $data[0]");
}
}
}
sub pm_channel {
my ($from, $to, $sub, @data) = @_;
my ($from, $to, $sub, @data) = @_;
my $sender = $from;
my $sender = $from;
$sender =~ s/^:(.*)!.*$/$1/;
if (exists $linestotal{$to})
{
$linestotal{$to}++;
}
else
{
$linestotal{$to} = 1;
}
##ignores statements from cout and users containing the word "bot"
if (($from !~/^:cout/) && ($from !~/^:[^!]*bot[^!]*!/i)) {
if ($sub !~ /^!/) {
my $rnd = 1;
unshift (@data,$sub);
if (@data > 1) {
#if it's a enemy, don't buttify message. If friend, buttify message more often.
$rnd = tobuttornottobutt($sender);
}
#if the random number is 0, buttify that data
if ($rnd ==0) {
$timeoflastbutting{$to} = time;
sleep(@data*0.2 + 1);
# if the message is a CTCP line, avoid replacing
# the CTCP command in the first word
if (substr($data[0], 0, 1) eq "\1") {
# only butt if the command is not the only word
if (@data > 1 && $data[1] ne "\1") {
my $first = shift(@data);
my @butted = &buttify(@data);
unshift(@butted, $first);
$sender =~ s/^:(.*)!.*$/$1/;
if (exists $linestotal{$to}) {
$linestotal{$to}++;
} else {
$linestotal{$to} = 1;
}
my $jam = join(" ", @data);
my $cock = join(" ", @butted);
_send("PRIVMSG $to :$cock") if ($jam ne $cock);
}
} else {
my @bread_and = &buttify(@data);
# comparing lists is piss easy in python :(
my $jam = join(" ", @data);
my $cock = join(" ", @bread_and);
_send("PRIVMSG $to :$cock") if ($jam ne $cock);
}
}
} elsif ($sub eq "!butt" and @data >0 ) {
if (($data[0] !~ /^!/) && ($data[0] !~ /^cout/)) {
my @bread_and = &buttify(@data);
# comparing lists is piss easy in python :(
my $jam = join(" ", @data);
my $cock = join(" ", @bread_and);
_send("PRIVMSG $to :$cock") if ($jam ne $cock);
}
}
}
##ignores statements from cout and users containing the word "bot"
if (($from !~/^:cout/) && ($from !~/^:[^!]*bot[^!]*!/i)) {
if ($sub !~ /^!/) {
my $rnd = 1;
unshift (@data,$sub);
if (@data > 1) {
#if it's a enemy, don't buttify message. If friend, buttify message more often.
$rnd = tobuttornottobutt($sender);
}
#if the random number is 0, buttify that data
if ($rnd ==0) {
$timeoflastbutting{$to} = time;
sleep(@data*0.2 + 1);
# if the message is a CTCP line, avoid replacing
# the CTCP command in the first word
if (substr($data[0], 0, 1) eq "\1") {
# only butt if the command is not the only word
if (@data > 1 && $data[1] ne "\1") {
my $first = shift(@data);
my @butted = &buttify(@data);
unshift(@butted, $first);
my $jam = join(" ", @data);
my $cock = join(" ", @butted);
_send("PRIVMSG $to :$cock") if ($jam ne $cock);
}
} else {
my @bread_and = &buttify(@data);
# comparing lists is piss easy in python :(
my $jam = join(" ", @data);
my $cock = join(" ", @bread_and);
_send("PRIVMSG $to :$cock") if ($jam ne $cock);
}
}
} elsif ($sub eq "!butt" and @data >0 ) {
if (($data[0] !~ /^!/) && ($data[0] !~ /^cout/)) {
my @bread_and = &buttify(@data);
# comparing lists is piss easy in python :(
my $jam = join(" ", @data);
my $cock = join(" ", @bread_and);
_send("PRIVMSG $to :$cock") if ($jam ne $cock);
}
}
}
}
#for future determining of butting
@ -231,14 +220,12 @@ sub tobuttornottobutt
my($rnd, $sender);
$sender = shift;
if (exists $enemies{$sender}) {
$rnd = 1;
}
elsif (exists $friends{$sender}) {
$rnd = int(rand(int($friendfrequency)));
}
else {
$rnd = int(rand(int($normalfrequency)));
}
$rnd = 1;
} elsif (exists $friends{$sender}) {
$rnd = int(rand(int($friendfrequency)));
} else {
$rnd = int(rand(int($normalfrequency)));
}
return $rnd;
}
@ -250,7 +237,7 @@ sub gets {
}
sub pong {
_send("PONG $_[0]");
_send("PONG $_[0]");
}
sub _send {
@ -258,31 +245,31 @@ sub _send {
}
sub _fork {
my $spoon = fork;
my $spoon = fork;
if (defined $spoon) {
if ($spoon == 0) { # is child process
return;
} else {
print "exiting, child pid = $spoon\n";
exit;
}
} else {
die "fork: $!";
}
if (defined $spoon) {
if ($spoon == 0) { # is child process
return;
} else {
print "exiting, child pid = $spoon\n";
exit;
}
} else {
die "fork: $!";
}
}
sub readconf {
my $file = shift;
($file = $0) =~ s/\.pl$/\.conf/i unless defined $file;
my $file = shift;
($file = $0) =~ s/\.pl$/\.conf/i unless defined $file;
open my($fh), $file or die "readconf: cannot open $file";
open my($fh), $file or die "readconf: cannot open $file";
while (<$fh>) {
next if /^\#/;
while (<$fh>) {
next if /^\#/;
if (/^\s*([^\s]+)\s*=\s*(.+)$/) {
$CONF{lc($1)} = $2;
}
}
if (/^\s*([^\s]+)\s*=\s*(.+)$/) {
$CONF{lc($1)} = $2;
}
}
}