revbank/plugins/warnings
2018-09-03 03:50:12 +02:00

43 lines
1.1 KiB
Perl
Executable file

#!perl
# The file format for 'revbank.warnings' is simply two whitespace separated
# columns: product id (or /regex/ for descriptions) and the warning text.
sub _read_warnings {
open my $fh, 'revbank.warnings' or die $!;
return map {
my ($matcher, $text) = split " ", $_, 2;
chomp $text;
my ($regex) = $matcher =~ m[^/([^/]+)/$];
$regex
? sub {
my ($id, $desc) = @_;
$desc =~ /$regex/i ? $text : ();
}
: sub {
my ($id, $desc) = @_;
(grep { $_ eq $id } split /,/, $matcher) ? $text : ();
}
} grep /\S/, grep !/^\s*#/, readline $fh;
}
sub command { NEXT }
sub hook_add {
my ($class, $cart, $user, $item) = @_;
return if defined $user; # skip market items
return if not exists $item->{product_id}; # skip unlisted, deposit, give, take
my @warnings = map {
$_->( $item->{product_id}, $item->{description} )
} _read_warnings;
if (@warnings) {
print "\e[7m>> $_ <<\e[0m " for @warnings;
sleep 2;
print "\n";
}
}