1 # This is part of ypp-sc-tools, a set of third-party tools for assisting
2 # players of Yohoho Puzzle Pirates.
4 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
20 # are used without permission. This program is not endorsed or
21 # sponsored by Three Rings.
25 use HTTP::Request::Common ();
33 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
36 @EXPORT = qw(&parse_info_clientside &fetch_with_rsync
37 &parse_info_serverside &parse_info_serverside_ocean
38 %oceans %commods %clients %routes %route_mysteries
39 &parse_pctb_commodmap %pctb_commodmap @pctb_commodmap
40 &get_our_version &check_tsv_line
41 &pipethrough_prep &pipethrough_run
42 &pipethrough_run_along &pipethrough_run_finish
44 &cgipostform &yarrgpostform &cgi_get_caller
51 our %oceans; # eg $oceans{'Midnight'}{'Ruby'}{'Eta Island'}= $sources;
52 our %clients; # eg $clients{'ypp-sc-tools'}= [ qw(last-page) ];
53 our %routes; # eg $routes{'Midnight'}{'Orca'}{'Tinga'}= $sources NB abbrevs!
54 our %route_mysteries; # eg $route_mysteries{'Midnight'}{'Norse'}= 3
56 # 's' = Special Circumstances; 'l' = local ; B = with Bleach
59 # eg $commods{'Fine black cloth'}{Srcs}= $sources;
60 # eg $commods{'Fine black cloth'}{Mass}= 700 [g]
61 # eg $commods{'Fine black cloth'}{Volume}= 1000 [ml]
63 our (%pctb_commodmap,@pctb_commodmap);
65 my %colours; # eg $colours{'c'}{'black'}= $sources
66 my @rawcm; # eg $rawcm[0]='fine rum'; $rawcm[1]='fine %c cloth'
68 sub parse_info1 ($$) {
70 my $mm= new IO::File $mmfn, 'r' or die "$mmfn $!";
78 @ctx= (sub { $colours{$colourkind}{lc $_} .= $src; });
79 } elsif (m/^commods$/) {
80 @ctx= (sub { push @rawcm, lc $_; });
81 } elsif (m/^ocean (\w+)$/) {
84 $ocean or die; # ref to $ocean needed to work
88 $oceans{$ocean}{$arch}{$_} .= $src;
91 } elsif (m/^routes (\w+)$/) {
94 m/^(\S[^\t]*\S),\s*(\S[^\t]*\S),\s*([1-9][0-9]{0,2})$/ or die;
95 $routes{$ocean}{$1}{$2}= $3;
97 } elsif (m/^client (\S+.*\S)$/) {
99 $clients{$client}= [ ];
102 push @{ $clients{$client} }, $bug;
105 my $indent= length $&;
106 die "wrong indent $indent" unless defined $ctx[$indent-1];
107 &{ $ctx[$indent-1] }();
112 $mm->error and die $!;
115 #print Dumper(\%oceans);
116 #print Dumper(\@rawcm);
123 if ($s !~ m/\%(\w+)/) {
124 my ($name, $props) = $s =~
125 /^(\S[^\t]*\S)\t+(\S[^\t]*\S)$/
126 or die "bad commodspec $s";
127 my $ucname= ucfirst $name;
128 $commods{$ucname}{Srcs} .= $ss;
129 my $c= $commods{$ucname};
131 foreach my $prop (split /\s+/, $props) {
132 if ($prop =~ m/^([1-9]\d*)(k?)g$/) {
133 $c->{Mass}= $1 * ($2 ? 1000 : 1);
134 } elsif ($prop =~m/^([1-9]\d*)l$/) {
135 $c->{Volume}= $1 * 1000;
137 die "unknown property $prop for $ucname";
140 die "no mass for $ucname" unless defined $c->{Mass};
143 die "unknown $&" unless defined $colours{$1};
144 my ($lhs,$pctlet,$rhs)= ($`,$1,$');
145 foreach my $c (keys %{ $colours{$pctlet} }) {
146 &$ca($lhs.$c.$rhs, $ss .'%'. $colours{$pctlet}{$c});
149 foreach (@rawcm) { &$ca($_,$src); }
151 foreach my $on (keys %routes) {
152 my $routes= $routes{$on};
153 my $ocean= $oceans{$on};
154 die unless defined $ocean;
157 foreach my $an (sort keys %$ocean) {
158 my $arch= $ocean->{$an};
159 push @allislands, sort keys %$arch;
161 parse_info_maproutes($on, \@allislands, $routes);
162 foreach my $route (values %$routes) {
163 parse_info_maproutes($on, \@allislands, $route);
168 sub parse_info_clientside () {
169 my $yarrg= $ENV{'YPPSC_YARRG_DICT_UPDATE'};
170 return unless $yarrg;
171 my $master= fetch_with_rsync('info');
172 parse_info1($master,'s');
173 my $local= '_local-info.txt';
175 parse_info1($local,'s');
177 die "$local $!" unless $! == &ENOENT;
181 sub fetch_with_rsync ($) {
184 my $rsync= $ENV{'YPPSC_YARRG_RSYNC'};
185 $rsync= 'rsync' if !defined $rsync;
187 my $local= "_master-$stem.txt";
188 my $src= $ENV{'YPPSC_YARRG_DICT_UPDATE'};
190 my $remote= "$src/master-$stem.txt";
191 $!=0; system 'rsync','-Lt','--',$remote,$local;
192 die "$? $!" if $! or $?;
197 sub parse_info_maproutes ($$$) {
198 my ($on, $allislands, $routemap) = @_;;
199 foreach my $k (sort keys %$routemap) {
200 my @ok= grep { index($_,$k) >= 0 } @$allislands;
201 die "ambiguous $k" if @ok>1;
203 $route_mysteries{$on}{$k}++;
204 delete $routemap->{$k};
205 } elsif ($ok[0] ne $k) {
206 $routemap->{$ok[0]}= $routemap->{$k};
207 delete $routemap->{$k};
212 sub parse_info_serverside () {
213 parse_info1('master-info.txt','s');
215 sub parse_info_serverside_ocean ($) {
216 my ($oceanname) = @_;
217 die "unknown ocean $oceanname ?" unless exists $oceans{$oceanname};
218 parse_info1("ocean-".(lc $oceanname).".txt",'s');
221 sub parse_pctb_commodmap () {
222 undef %pctb_commodmap;
223 foreach my $commod (keys %commods) { $commods{$commod}{Srcs} =~ s/b//; }
225 my $c= new IO::File '_commodmap.tsv';
226 if (!$c) { $!==&ENOENT or die $!; return 0; }
229 m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
230 die if defined $pctb_commodmap{$1}; $pctb_commodmap{$1}= $2;
231 die if defined $pctb_commodmap[$2]; $pctb_commodmap[$2]= $1;
232 $commods{$1}{Srcs} .= 'b';
234 $c->error and die $!;
239 sub get_our_version ($$) {
240 my ($aref,$prefix) = @_;
241 $aref->{"${prefix}name"}= 'ypp-sc-tools yarrg';
242 $aref->{"${prefix}fixes"}= 'lastpage';
244 my $version= `git-describe --tags HEAD || echo 0unknown`; $? and die $?;
246 $aref->{"${prefix}version"}= $version;
248 # clientname "ypp-sc-tools"
249 # clientversion 2.1-g2e06a26 [from git-describe --tags HEAD]
250 # clientfixes "lastpage" [space separated list]
253 sub pipethrough_prep () {
254 my $tf= IO::File::new_tmpfile() or die $!;
258 sub pipethrough_run_along ($$$@) {
259 my ($tf, $childprep, $cmd, @a) = @_;
260 $tf->flush or die $!;
261 $tf->seek(0,0) or die $!;
262 my $fh= new IO::File;
263 my $child= $fh->open("-|"); defined $child or die $!;
265 open STDIN, "<&", $tf;
266 &$childprep() if defined $childprep;
267 exec $cmd @a; die "@a $!";
271 sub pipethrough_run_finish ($$) {
273 $fh->error and die $!;
274 close $fh or die "$what $! $?"; die $? if $?;
277 sub pipethrough_run ($$$@) {
278 my ($tf, $childprep, $cmd, @a) = @_;
279 my $pt= pipethrough_run_along($tf,$childprep,$cmd,@a);
281 { undef $/; $!=0; $r= <$pt>; }
282 defined $r or die $!;
283 pipethrough_run_finish($pt, "@a");
286 sub pipethrough_run_gzip ($) {
287 pipethrough_run($_[0],undef,'gzip','gzip');
290 sub yarrgpostform ($$) {
291 my ($ua, $form) = @_;
292 my $dest= $ENV{'YPPSC_YARRG_YARRG'};
293 get_our_version($form, 'client');
294 die unless $dest =~ m,/$,;
295 return cgipostform($ua, "${dest}commod-update-receiver", $form);
298 sub cgipostform ($$$) {
299 my ($ua, $url, $form) = @_;
300 my $req= HTTP::Request::Common::POST($url,
302 Content_Type => 'form-data');
303 if ($url =~ m,^\.?/,) {
304 my $tf= pipethrough_prep();
305 print $tf $req->content() or die $!;
306 #print STDERR "[[[",$req->content(),"]]]";
307 my $out= pipethrough_run($tf, sub {
308 $ENV{'REQUEST_METHOD'}= 'POST';
309 $ENV{'QUERY_STRING'}= '';
310 $ENV{'PATH_TRANSLATED'}= $url;
311 $ENV{'PATH_INFO'}= '';
312 $ENV{'HTTP_HOST'}= 'localhost';
313 $ENV{'REMOTE_ADDR'}= '127.0.0.1';
314 $ENV{'GATEWAY_INTERFACE'}= 'CGI/1.1';
315 $ENV{'DOCUMENT_ROOT'}= '.';
316 $ENV{'SCRIPT_FILENAME'}= $url;
317 $ENV{'SCRIPT_NAME'}= $url;
318 $ENV{'HTTP_USER_AGENT'}= 'Commods.pm local test';
320 foreach my $f (qw(Content_Length Content_Type)) {
321 $ENV{uc $f}= $req->header($f);
323 #system 'printenv >&2';
326 $out =~ m,^Content-Type: text/plain.*\n\n, or die "$out ?";
329 my $resp= $ua->request($req);
330 die $resp->status_line."\n".$resp->content."\n "
331 unless $resp->is_success;
332 return $resp->content();
338 sub check_tsv_line ($$) {
339 my ($l, $bad_data_callback) = @_;
340 my $bad_data= sub { &$bad_data_callback("bad data: line $.: $_[0]"); };
342 chomp($l) or &$bad_data('missing end-of-line');
344 $l !~ m/\P{IsPrint}/ or &$bad_data('nonprinting char(s)');
345 my @v= split /\t/, $l, -1;
346 @v==6 or &$bad_data('wrong number of fields');
347 my ($commod,$stall) = @v;
350 defined $commods{$commod} or
351 &$bad_data("unknown commodity \`$commod'");
353 $stall =~ m/^\p{IsUpper}|^[0-9]/ or &$bad_data("stall not capitalised");
354 !exists $check_tsv_done{$commod,$stall} or &$bad_data("repeated data");
355 $check_tsv_done{$commod,$stall}= 1;
356 foreach my $i (2..5) {
358 $f =~ m/^(|0|[1-9][0-9]{0,5}|\>1000)$/ or &$bad_data("bad field $i");
359 ($i % 2) or ($f !~ m/\>/) or &$bad_data("> in field $i price");
362 foreach my $i (2,4) {
363 &$bad_data("price with no qty or vice versa (field $i)")
364 if length($v[$i]) xor length($v[$i+1]);
366 length($v[2]) or length($v[4]) or
367 &$bad_data("commodity entry with no buy or sell offer");
372 sub cgi_get_caller () {
373 my $caller= $ENV{'REMOTE_ADDR'};
374 $caller= 'LOCAL' unless defined $caller;
376 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
379 $fwdf =~ s/[^0-9.,]/?/g;
385 sub set_ctype_utf8 () {
386 setlocale(LC_CTYPE, "en.UTF-8");