revbank/plugins/warnings
2018-09-04 23:17:03 +02:00

46 lines
1.2 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 ($regex, $products, $text) = m[^
(?:
/((?>[^/\\]++|\\.)*+)/ # /regex with support for \/escaped\/ slashes/
| (\S+) # products IDs, comma separated
)
\s+
(.*)
]x;
$regex
? sub {
my ($id, $desc) = @_;
$desc =~ /$regex/i ? $text : ();
}
: sub {
my ($id, $desc) = @_;
(grep { $_ eq $id } split /,/, $products) ? $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\n" for @warnings;
sleep 2;
}
}