diff --git a/README.md b/README.md index bdb2ad6..87e6719 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,15 @@ line (space separated), but that's an unintended feature... # PLUGINS -Refer to [RevBank::Plugins](https://metacpan.org/pod/RevBank::Plugins) for documentation about writing plugins. +Refer to [RevBank::Plugins](https://metacpan.org/pod/RevBank::Plugins) for +documentation about writing plugins. Plugins themselves may have some documentation in the respective plugin files. +Note that plugins that begin with `revspace_` are revspace specific hacks, and +were not written with reusability in mind. They will probably not work for your +setup. + # AUTHOR Juerd Waalboer <#####@juerd.nl> diff --git a/plugins/revspace_barcode b/plugins/revspace_barcode new file mode 100644 index 0000000..108b4a9 --- /dev/null +++ b/plugins/revspace_barcode @@ -0,0 +1,56 @@ +#!perl + + +# Ja, het is lelijk. Even snel in elkaar geklust en daarna niet meer naar +# gekeken. Pech :) -- Juerd + + + + +sub command :Tab(barcode) { + my ($self, $cart, $command) = @_; + + return NEXT if $command ne "barcode"; + + return "Barcode data", \&data; +} + +sub data { + my ($self, $cart, $input) = @_; + + $cart->add( + undef, + -0.07, + "Barcode <$input>", + { is_barcode => 1, barcode_data => $input } + ); + + return ACCEPT; +} + +sub hook_checkout { + my ($class, $cart, $username, $transaction_id) = @_; + + my @barcodes; + for my $item ($cart->select_items('is_barcode')) { + push @barcodes, $item->{barcode_data}; + } + if (@barcodes) { + print "\nCheck the following:\n 1. label tape is 12 mm\n 2. printer is on\n 3. wifi is enabled and connected\n\nPress enter to continue."; + readline STDIN; + + my $printjob = ""; + + open my $bcgen, "-|", "/home/bar/revlabel/barcode.pl", @barcodes + or warn "Could not open script 1"; + + local $/; + my $filenames = readline $bcgen; + close $bcgen; + + open my $fh, "| /home/bar/revlabel/ptouch-770-write 12 $filenames | nc -N 10.42.42.222 9100" + or warn "Couldn't open script 2\n"; + print $fh $printjob; + close $fh; + } +} diff --git a/plugins/revspace_bounties b/plugins/revspace_bounties new file mode 100644 index 0000000..89496cc --- /dev/null +++ b/plugins/revspace_bounties @@ -0,0 +1,20 @@ +#!perl + +my %bounties = ( + 1 => [ 10, "Bedankt voor het vegen/stofzuigen" ], + 2 => [ 10, "Bedankt voor het afvoeren van het afval" ], + 3 => [ 25, "Bedankt voor het dweilen" ], + 4 => [ 15, "Bedankt voor 't poetsen van alle tafels" ], +); + +sub command :Tab(BOUNTY1,BOUNTY2,BOUNTY3,BOUNTY4) { + my ($self, $cart, $command) = @_; + + if ($command =~ /BOUNTY(\d+)/) { + $cart->add(undef, +$bounties{$1}[0], $bounties{$1}[1]); + return ACCEPT; + } + + return NEXT; +} + diff --git a/plugins/revspace_git b/plugins/revspace_git new file mode 100644 index 0000000..4807dc5 --- /dev/null +++ b/plugins/revspace_git @@ -0,0 +1,15 @@ +#!perl + +sub command { NEXT } + +sub hook_user_balance { + my ($class, $username, $old, $delta, $new, $transaction_id) = @_; + my $msg = "$transaction_id ($username)"; + $msg =~ s/[^\x20-\x7E]//g; + $msg =~ s/'//g; + + system("(git commit -am '$msg') > /dev/null 2>&1") + == 0 or warn "Meh, gitfaal"; + system("git gc --auto"); +} + diff --git a/plugins/revspace_mqtt b/plugins/revspace_mqtt new file mode 100644 index 0000000..15adfea --- /dev/null +++ b/plugins/revspace_mqtt @@ -0,0 +1,36 @@ +#!perl + +use Net::MQTT::Simple "mosquitto.space.revspace.nl"; + +sub command { NEXT } + +sub hook_checkout { + my ($class, $cart, $user, $transaction_id) = @_; + my $filename = "revbank.sales"; + my @items = $cart->select_items('product_id') or return; + my %already_retained; + + my %stats = do { + my $in; + open($in, '<', $filename) + ? map { split " ", $_, 2 } readline $in + : () + }; + + $stats{ $_->{product_id} }++ for @items; + + for (@items) { + my $product = $_->{product_id}; + + publish "revspace/bank/sale" => $product; + next if $already_retained{ $product }; + + retain "revspace/bank/$product" => $stats{$_->{product_id}}; + $already_retained{ $product } = 1; + } + + open my $out, '>', "$filename.$$" or warn "$filename.$$: $!"; + printf {$out} "%-16s %9d\n", $_, $stats{$_} for sort keys %stats; + close $out or die "$filename.$$: $!"; + rename "$filename.$$", $filename or die $!; +} diff --git a/plugins/revspace_saldo b/plugins/revspace_saldo new file mode 100644 index 0000000..e8a644b --- /dev/null +++ b/plugins/revspace_saldo @@ -0,0 +1,46 @@ +#!perl + +use POSIX qw(strftime); + +sub command { NEXT } + +sub _box { + print( + "#" x 79, "\n", + (map { sprintf("## %-73s ##\n", $_) } @_), + "#" x 79, "\n" + ); +} + +sub hook_checkout_done { + my ($class, $cart, $user, $transaction_id) = @_; + + my $balance = RevBank::Users::balance($user) or return; + my $since = RevBank::Users::since($user); + + if ($balance < -13.37) { + _box( + "Hoi $user,", + "", + "Je saldo is $balance en dus lager dan toegestaan. Graag meteen aanvullen!", + "Zodra je een positief saldo hebt, mag je weer producten kopen.", + "", + "Bedankt!", + "-- Het bestuur", + ); + } elsif ( + defined $since + and $since =~ /^-\@(.*)/ + and $1 lt strftime('%Y-%m-%d_%H:%M:%S', localtime(time() - 14 * 86400)) + ) { + _box( + "Hoi $user,", + "", + "Je staat al sinds $1 negatief, dus meer dan 2 weken.", + "Je mag rood staan, maar niet langdurig. Wil je je saldo even aanvullen?", + "", + "Bedankt!", + "-- Het bestuur", + ); + } +} diff --git a/plugins/revspace_terminal b/plugins/revspace_terminal new file mode 100644 index 0000000..a695435 --- /dev/null +++ b/plugins/revspace_terminal @@ -0,0 +1,15 @@ +sub command { NEXT } + +# Terminal hacks + +# Reset terminal on startup +system 'reset'; + +# Select special characters in G1 on IBM 3151 for utf8 qr code, to counteract +# brokenness caused by squeezen's linedrawing. +sub hook_checkout { + my $select_charset = "\e>A"; # IBM 3151 + my $device_control = "\eP" . $select_charset . "\e\\"; # ANSI + my $clear_line = "\r \r"; + print $device_control, $clear_line; +} diff --git a/plugins/revspace_window b/plugins/revspace_window new file mode 100644 index 0000000..c3562d2 --- /dev/null +++ b/plugins/revspace_window @@ -0,0 +1,16 @@ +sub command { + # Bij elke invoer (bijv. barcode-scan) terug naar window 0. + system qw(screen -S bar -X at % select 0); + + # Iemand zet steeds vbell aan, en dus het piepje uit. Daardoor werkt de + # idle-plugin niet zinnig en dat kost geld. + system qw(screen -S bar -X vbell off); + + return NEXT; +} + +sub hook_abort { + command(); + return undef; +} +