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 %commods; # eg $commods{'Fine black cloth'}= $sources;
53 our %clients; # eg $clients{'ypp-sc-tools'}= [ qw(last-page) ];
54 our %routes; # eg $routes{'Midnight'}{'Orca'}{'Tinga'}= $sources NB abbrevs!
55 our %route_mysteries; # eg $route_mysteries{'Midnight'}{'Norse'}= 3
57 # 's' = Special Circumstances; 'l' = local ; B = with Bleach
59 our (%pctb_commodmap,@pctb_commodmap);
61 my %colours; # eg $colours{'c'}{'black'}= $sources
62 my @rawcm; # eg $rawcm[0]='fine rum'; $rawcm[1]='fine %c cloth'
64 sub parse_info1 ($$) {
66 my $mm= new IO::File $mmfn, 'r' or die "$mmfn $!";
74 @ctx= (sub { $colours{$colourkind}{lc $_} .= $src; });
75 } elsif (m/^commods$/) {
76 @ctx= (sub { push @rawcm, lc $_; });
77 } elsif (m/^ocean (\w+)$/) {
80 $ocean or die; # ref to $ocean needed to work
84 $oceans{$ocean}{$arch}{$_} .= $src;
87 } elsif (m/^routes (\w+)$/) {
90 m/^(\S[^\t]*\S),\s*(\S[^\t]*\S),\s*([1-9][0-9]{0,2})$/ or die;
91 $routes{$ocean}{$1}{$2}= $3;
93 } elsif (m/^client (\S+.*\S)$/) {
95 $clients{$client}= [ ];
98 push @{ $clients{$client} }, $bug;
101 my $indent= length $&;
102 die "wrong indent $indent" unless defined $ctx[$indent-1];
103 &{ $ctx[$indent-1] }();
108 $mm->error and die $!;
111 #print Dumper(\%oceans);
112 #print Dumper(\@rawcm);
119 if ($s !~ m/\%(\w+)/) { $commods{ucfirst $s} .= $ss; return; }
120 die "unknown $&" unless defined $colours{$1};
121 foreach my $c (keys %{ $colours{$1} }) {
122 &$ca($`.$c.$', $ss .'%'. $colours{$1}{$c});
125 foreach (@rawcm) { &$ca($_,$src); }
127 foreach my $on (keys %routes) {
128 my $routes= $routes{$on};
129 my $ocean= $oceans{$on};
130 die unless defined $ocean;
133 foreach my $an (sort keys %$ocean) {
134 my $arch= $ocean->{$an};
135 push @allislands, sort keys %$arch;
137 parse_info_maproutes($on, \@allislands, $routes);
138 foreach my $route (values %$routes) {
139 parse_info_maproutes($on, \@allislands, $route);
144 sub parse_info_clientside () {
145 my $yarrg= $ENV{'YPPSC_YARRG_DICT_UPDATE'};
146 return unless $yarrg;
147 my $master= fetch_with_rsync('info');
148 parse_info1($master,'s');
149 my $local= '_local-info.txt';
151 parse_info1($local,'s');
153 die "$local $!" unless $! == &ENOENT;
157 sub fetch_with_rsync ($) {
160 my $rsync= $ENV{'YPPSC_YARRG_RSYNC'};
161 $rsync= 'rsync' if !defined $rsync;
163 my $local= "_master-$stem.txt";
164 my $src= $ENV{'YPPSC_YARRG_DICT_UPDATE'};
166 my $remote= "$src/master-$stem.txt";
167 $!=0; system 'rsync','-Lt','--',$remote,$local;
168 die "$? $!" if $! or $?;
173 sub parse_info_maproutes ($$$) {
174 my ($on, $allislands, $routemap) = @_;;
175 foreach my $k (sort keys %$routemap) {
176 my @ok= grep { index($_,$k) >= 0 } @$allislands;
177 die "ambiguous $k" if @ok>1;
179 $route_mysteries{$on}{$k}++;
180 delete $routemap->{$k};
181 } elsif ($ok[0] ne $k) {
182 $routemap->{$ok[0]}= $routemap->{$k};
183 delete $routemap->{$k};
188 sub parse_info_serverside () {
189 parse_info1('master-info.txt','s');
191 sub parse_info_serverside_ocean ($) {
192 my ($oceanname) = @_;
193 die "unknown ocean $oceanname ?" unless exists $oceans{$oceanname};
194 parse_info1("ocean-".(lc $oceanname).".txt",'s');
197 sub parse_pctb_commodmap () {
198 undef %pctb_commodmap;
199 foreach my $commod (keys %commods) { $commods{$commod} =~ s/b//; }
201 my $c= new IO::File '_commodmap.tsv' or die $!;
202 if (!$c) { $!==&ENOENT or die $!; return 0; }
205 m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
206 die if defined $pctb_commodmap{$1}; $pctb_commodmap{$1}= $2;
207 die if defined $pctb_commodmap[$2]; $pctb_commodmap[$2]= $1;
210 $c->error and die $!;
215 sub get_our_version ($$) {
216 my ($aref,$prefix) = @_;
217 $aref->{"${prefix}name"}= 'ypp-sc-tools yarrg';
218 $aref->{"${prefix}fixes"}= 'lastpage';
220 my $version= `git-describe --tags HEAD`; $? and die $?;
222 $aref->{"${prefix}version"}= $version;
224 # clientname "ypp-sc-tools"
225 # clientversion 2.1-g2e06a26 [from git-describe --tags HEAD]
226 # clientfixes "lastpage" [space separated list]
229 sub pipethrough_prep () {
230 my $tf= IO::File::new_tmpfile() or die $!;
234 sub pipethrough_run_along ($$$@) {
235 my ($tf, $childprep, $cmd, @a) = @_;
236 $tf->flush or die $!;
237 $tf->seek(0,0) or die $!;
238 my $fh= new IO::File;
239 my $child= $fh->open("-|"); defined $child or die $!;
241 open STDIN, "<&", $tf;
242 &$childprep() if defined $childprep;
243 exec $cmd @a; die "@a $!";
247 sub pipethrough_run_finish ($$) {
249 $fh->error and die $!;
250 close $fh or die "$what $! $?"; die $? if $?;
253 sub pipethrough_run ($$$@) {
254 my ($tf, $childprep, $cmd, @a) = @_;
255 my $pt= pipethrough_run_along($tf,$childprep,$cmd,@a);
257 { undef $/; $!=0; $r= <$pt>; }
258 defined $r or die $!;
259 pipethrough_run_finish($pt, "@a");
262 sub pipethrough_run_gzip ($) {
263 pipethrough_run($_[0],undef,'gzip','gzip');
266 sub yarrgpostform ($$) {
267 my ($ua, $form) = @_;
268 my $dest= $ENV{'YPPSC_YARRG_YARRG'};
269 get_our_version($form, 'client');
270 die unless $dest =~ m,/$,;
271 return cgipostform($ua, "${dest}commod-update-receiver", $form);
274 sub cgipostform ($$$) {
275 my ($ua, $url, $form) = @_;
276 my $req= HTTP::Request::Common::POST($url,
278 Content_Type => 'form-data');
279 if ($url =~ m,^\.?/,) {
280 my $tf= pipethrough_prep();
281 print $tf $req->content() or die $!;
282 #print STDERR "[[[",$req->content(),"]]]";
283 my $out= pipethrough_run($tf, sub {
284 $ENV{'REQUEST_METHOD'}= 'POST';
285 $ENV{'QUERY_STRING'}= '';
286 $ENV{'PATH_TRANSLATED'}= $url;
287 $ENV{'PATH_INFO'}= '';
288 $ENV{'HTTP_HOST'}= 'localhost';
289 $ENV{'REMOTE_ADDR'}= '127.0.0.1';
290 $ENV{'GATEWAY_INTERFACE'}= 'CGI/1.1';
291 $ENV{'DOCUMENT_ROOT'}= '.';
292 $ENV{'SCRIPT_FILENAME'}= $url;
293 $ENV{'SCRIPT_NAME'}= $url;
294 $ENV{'HTTP_USER_AGENT'}= 'Commods.pm local test';
296 foreach my $f (qw(Content_Length Content_Type)) {
297 $ENV{uc $f}= $req->header($f);
299 #system 'printenv >&2';
302 $out =~ m,^Content-Type: text/plain.*\n\n, or die "$out ?";
305 my $resp= $ua->request($req);
306 die $resp->status_line unless $resp->is_success;
307 return $resp->content();
313 sub check_tsv_line ($$) {
314 my ($l, $bad_data_callback) = @_;
315 my $bad_data= sub { &$bad_data_callback("bad data: line $.: $_[0]"); };
317 chomp($l) or &$bad_data('missing end-of-line');
319 $l !~ m/\P{IsPrint}/ or &$bad_data('nonprinting char(s)');
320 my @v= split /\t/, $l, -1;
321 @v==6 or &$bad_data('wrong number of fields');
322 my ($commod,$stall) = @v;
325 defined $commods{$commod} or
326 &$bad_data("unknown commodity \`$commod'");
328 $stall =~ m/^\p{IsUpper}|^[0-9]/ or &$bad_data("stall not capitalised");
329 !exists $check_tsv_done{$commod,$stall} or &$bad_data("repeated data");
330 $check_tsv_done{$commod,$stall}= 1;
331 foreach my $i (2..5) {
333 $f =~ m/^(|0|[1-9][0-9]{0,5}|\>1000)$/ or &$bad_data("bad field $i");
334 ($i % 2) or ($f !~ m/\>/) or &$bad_data("> in field $i price");
337 foreach my $i (2,4) {
338 &$bad_data("price with no qty or vice versa (field $i)")
339 if length($v[$i]) xor length($v[$i+1]);
341 length($v[2]) or length($v[4]) or
342 &$bad_data("commodity entry with no buy or sell offer");
347 sub cgi_get_caller () {
348 my $caller= $ENV{'REMOTE_ADDR'};
349 $caller= 'LOCAL' unless defined $caller;
351 my $fwdf= $ENV{'HTTP_X_FORWARDED_FOR'};
354 $fwdf =~ s/[^0-9.,]/?/g;
360 sub set_ctype_utf8 () {
361 setlocale(LC_CTYPE, "en.UTF-8");