gecko-dev/webtools/mozbot/BotModules/Sheriff.bm
2004-01-25 13:04:23 +00:00

141 lines
5.2 KiB
Plaintext

################################
# Sheriff Module #
################################
package BotModules::Sheriff;
use vars qw(@ISA);
@ISA = qw(BotModules);
1;
# RegisterConfig - Called when initialised, should call registerVariables
sub RegisterConfig {
my $self = shift;
$self->SUPER::RegisterConfig(@_);
$self->registerVariables(
# [ name, save?, settable? ]
['tree', 1, 1, 'SeaMonkey'],
['baseURI', 1, 1, 'http://tinderbox.mozilla.org/'],
['_sheriff', 1, 0, undef], # the undef actually means "don't touch", of course
['updateDelay', 1, 1, 360],
# XXX implement per-channel muting of the update notification
);
}
# Schedule - called when bot connects to a server, to install any schedulers
# use $self->schedule($event, $delay, $times, $data)
# where $times is 1 for a single event, -1 for recurring events,
# and a +ve number for an event that occurs that many times.
sub Schedule {
my $self = shift;
my ($event) = @_;
$self->schedule($event, \$self->{'updateDelay'}, -1, 'sheriff');
$self->SUPER::Schedule($event);
}
sub Help {
my $self = shift;
my ($event) = @_;
return {
'' => 'The Sheriff module keeps track of the current sheriff.',
'sheriff' => 'Display the current sheriff. Syntax: sheriff [tree]',
};
}
sub Told {
my $self = shift;
my ($event, $message) = @_;
if ($message =~ /^\s*(?:who's\s+|whose\s+|whos\s+|who\s+is\s+the\s+|who\s+is\s+|who\s+)?sheriff(?:\s+(?:of\s+)?(.*?))?(?:[\s,]+today)?[.?!1]*\s*$/osi) {
$self->GetSheriff($event, $1 || $self->{'tree'}, 'requested');
} else {
return $self->SUPER::Told(@_);
}
return 0; # dealt with it...
}
sub GetSheriff {
my $self = shift;
my ($event, $tree, $requested) = @_;
my $url = "$self->{'baseURI'}$tree/sheriff.pl";
$self->getURI($event, $url, $tree, $requested);
}
sub GotURI {
my $self = shift;
my ($event, $uri, $output, $tree, $requested) = @_;
# someone please pretty up the logic here... XXX
if ($output) {
# magicness
{ no warnings; # this can go _very_ wrong easily
# sheriff.pl is created using the following lines:
# $m =~ s/\'/\\\'/g;
# print SHERIFF "\$current_sheriff = '$m';\n1;";
$output =~ s/^\$current_sheriff = '//gosi; # strip front
$output =~ s/';\n1;$//gosi; # strip back
$output =~ s/\\\'/\'/gosi; # dequote quotes
# heuristics
$output =~ s/<!--.*?-->//gos;
$output =~ s/\n|\r|<a\s+href="|<\/a>//gosi;
$output =~ s/">/, /gosi;
$output =~ s/<br>|<\/?p><\/?div>/ /gosi;
$output =~ s/<\/?(?:b|strong)>/*/gosi;
$output =~ s/<\/?(?:u|em)>/_/gosi;
$output =~ s/<\/?(?:q)>/"/gosi;
$output =~ s/<\/?(?:i|dfn|cite)>/\//gosi;
}
if (defined($output)) {
if ($tree eq $self->{'tree'}) {
if ((defined($self->{'_sheriff'})) and ($self->{'_sheriff'} ne '')) { # not first time
if ($output ne $self->{'_sheriff'}) { # changed.
$self->announce($event, "Sheriff change: $output");
if (($requested) and (not ($event->{'channel'}))) {
$self->directSay($event, "$output");
}
} elsif ($requested) {
$self->say($event, "$event->{'from'}: $output");
}
} else { # first time
$self->say($event, "$event->{'from'}: $output") if ($requested);
}
$self->{'_sheriff'} = $output; # update internal cache
} else { # not default tree
if ($requested) {
$self->say($event, "$event->{'from'}: $output");
} # else EH!?
}
} else {
# something went very wrong
$self->say($event, "$event->{'from'}: I have no idea -- the '$tree' tree probably doesn't have a sheriff.") if ($requested);
if ($tree eq $self->{'tree'}) {
if (defined($self->{'_sheriff'})) {
# only do it once
$self->tellAdmin($event, "Oh dear lord what happened to the '$tree' sheriff line on the tinderbox page!!");
$self->{'_sheriff'} = undef;
}
}
}
} else {
if ($tree eq $self->{'tree'}) {
$self->say($event, "$event->{'from'}: Call an admin, I couldn't find the Sheriff page. Sorry!") if ($requested);
if (defined($self->{'_sheriff'})) {
# only do it once
$self->tellAdmin($event, "Looks like either I am badly configured or tinderbox is down - '$tree' came up blank when I went looking for the Sheriff.");
$self->{'_sheriff'} = undef;
}
} else {
if ($requested) {
$self->say($event, "$event->{'from'}: Are you sure there is a tree called '$tree'? I couldn't find one...");
} # else EH!?
}
}
}
sub Scheduled {
my $self = shift;
my ($event, @data) = @_;
if ($data[0] eq 'sheriff') {
$self->GetSheriff($event, $self->{'tree'}, 0);
} else {
$self->SUPER::Scheduled($event, @data);
}
}