chiark / gitweb /
WIP update processor, generating outgoing email
[ypp-sc-tools.web-live.git] / pctb / commod-update-receiver
index 531ff1a2e5ac05e17f7bff891ddc07c7f416d08d..2c409f48d4b4ce7557d85a9380a25a6ead30a24a 100755 (executable)
 #  clientname                  "ypp-sc-tools"
 #  clientversion               2.1-g2e06a26  [from git-describe --tags HEAD]
 #  clientfixes                 "lastpage"  [space separated list]
-#  deduped.tsv.gz              output of ypp-commodities --tsv
+#  data filename=deduped.tsv.gz        output of ypp-commodities --tsv
 
 use strict (qw(vars));
 use POSIX;
 
+use Commods;
+
 $CGI::POST_MAX= 3*1024*1024;
 $CGI::DISABLE_UPLOADS= 1;
 
@@ -44,3 +46,93 @@ use CGI qw/:standard -private_tempfiles/;
 
 setlocale(LC_CTYPE, "en_GB.UTF-8");
 
+my $re_any= "^(.*)\$";
+
+parse_masters();
+
+sub fail ($) {
+    my ($msg) = @_;
+    print header(-status=>'400 Bad commodity update',
+                -type=>'text/plain',
+                -charset=>'us-ascii');
+    print "Error: $msg\n";
+    exit 0;
+}
+
+sub must_param ($$) {
+    my ($n,$re)= @_;
+    my $v= param($n);
+    fail("missing form parameter $n") unless defined $v;
+    fail("invalid form parameter $n") unless $v =~ m/$re/;
+    return $1;
+}
+
+my $clientname= must_param('clientname',$re_any);
+my $clientinfo= $clients{$clientname};
+fail('unknown client') unless defined $client;
+
+my $clientfixes= must_param('clientfixes', "^([-0-9a-z ]*)\$");
+my @clientfixes= sort grep { m/./ } split /\s+/, $clientfixes;
+$clientfixes= "@clientfixes";
+foreach my $bug (@$clientinfo) {
+    fail("client out of date - missing bugfix \`$bug'")
+       unless grep { $_ eq $bug } @clientfixes;
+}
+
+my $cversion= must_param('clientversion', "^(\\d[-+._0-9a-zA-Z]+)\$");
+
+my $ocean= must_param('ocean', $re_any);
+my $island= must_param('island', $re_any);
+
+my $arches= $oceans{$ocean};
+fail("unknown ocean") unless $arches;
+
+my $island_found= 0;
+foreach my $islands (values %$arches) {
+    my $sources= $islands->{$island};
+    next unless $sources;
+    die if $island_found;
+    $island_found= $sources;
+}
+fail("unknown island") unless $island_found;
+
+die if $ocean =~ m/\=/;
+die if $island =~ m/\=/;
+
+$datafile= must_param('data',"^(deduped\\.tsv\\.gz)\$");
+$indatafh= upload('data');  fail("data is not a file") unless defined $datafh;
+
+our %done;
+
+my $content= MIME::Entity->build(Type => 'multipart/mixed',
+                                Boundary => '=',
+                                Charset => 'utf-8');
+
+my $clientspec= "$clientname $clientversion $clientfixes";
+foreach $vn (qw(ocean island
+               clientspec
+               clientname clientversion clientfixes)) {
+
+while (<$datafh>) {
+    !m/\P{IsPrint}/ or die bad_data('nonprinting char(s)');
+    my @v= split /\t/;
+    @v==6 or bad_data('wrong number of fields');
+    my ($commod,$stall) = @v;
+    defined $commods{$commod} or bad_data("unknown commodity \`$commod'");
+    $stall =~ m/^\p{IsUpper}/ or bad_data("stall not capitalised");
+    !exists $done{$commod,$stall} or bad_data("repeated data");
+    $done{$commod,$stall}= 1;
+    foreach my $f (@v[2..5]) {
+       $f =~ m/^(0|[1-9][0-9]{0,5}|\>1000)$/ or bad_data("bad field");
+       ($v % 1) or ($v !~ m/\>/) or bad_data("> in price");
+    }
+}
+
+
+
+
+#foreach my $commod (sort keys %commods) {
+#    print "$commod\n";
+#}
+#STDOUT->error and die $!;
+#close STDOUT or die $!;