From 8f781dae6c4058e0dd42b81eefdc592acf257a9a Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Mon, 18 Sep 2023 01:35:57 +0200 Subject: [PATCH] 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. --- plugins/regex_gtin | 34 ++++++++++++++++++++++++++++++++++ revbank.plugins | 3 ++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 plugins/regex_gtin diff --git a/plugins/regex_gtin b/plugins/regex_gtin new file mode 100644 index 0000000..2e07d1d --- /dev/null +++ b/plugins/regex_gtin @@ -0,0 +1,34 @@ +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; +} diff --git a/revbank.plugins b/revbank.plugins index 206c3ec..8b723a0 100644 --- a/revbank.plugins +++ b/revbank.plugins @@ -46,5 +46,6 @@ products # matches product IDs (barcodes) market # also matches product IDs users # matches usernames #regex_angel # matches SHA2017/MCH2022 angel badges -url # matches URLs +regex_gtin # matches GTINs in URLs and element strings +url # matches other URLs #deprecated_raw # matches amounts