revbank/plugins/regex_gtin
Juerd Waalboer 8f781dae6c Add simple GS1 "Digital Link" and "Element String" support
Also has a regex for some known promotional URLs that don't adhere to
the Digital Link standard.
2023-09-18 01:37:35 +02:00

34 lines
1.1 KiB
Text

use List::Util qw(sum);
my @regexes = (
qr[^https?://.*?/01/(\d{14})\b], # GS1 Digital Link with GTIN-14
qr[^https?://.*?/01/0(\d{13})\b], # GS1 Digital Link with GTIN-13
qr[^https?://.*?/01/00(\d{12})\b], # GS1 Digital Link with GTIN-12
qr[^https?://.*?/01/0{6}(\d{8})\b], # GS1 Digital Link with GTIN-8
qr[^\(01\)(\d{14})\b], # GS1 Element String with GTIN-14
qr[^\(01\)0(\d{13})\b], # GS1 Element String with GTIN-13
qr[^\(01\)00(\d{12})\b], # GS1 Element String with GTIN-12
qr[^\(01\)0{6}(\d{8})\b], # GS1 Element String with GTIN-8
qr[^https://\w+url\.com/(?:q/|q/srn|srn)(\d{13})], # spam with GTIN-13
);
sub command ($self, $cart, $command, @) {
$self->{orig_command} //= $command;
$self->{regexes} //= [ @regexes ];
while (my $regex = shift @{ $self->{regexes} }) {
if ($self->{orig_command} =~ $regex) {
my $gtin = $1;
my @digits = reverse split //, $gtin;
my $checksum = (10 - sum(map $digits[$_] * ($_ % 2 ? 3 : 1), 1..$#digits) % 10) % 10;
$digits[0] == $checksum or next;
return REDO, $gtin;
}
}
return NEXT;
}