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 ();
32 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
35 @EXPORT = qw(&parse_masters %oceans %commods %clients
36 &parse_pctb_commodmap %pctb_commodmap @pctb_commodmap
37 &get_our_version &check_tsv_line
38 &pipethrough_prep &pipethrough_run
39 &pipethrough_run_along &pipethrough_run_finish
47 our %oceans; # eg $oceans{'Midnight'}{'Ruby'}{'Eta Island'}= $sources;
48 our %commods; # eg $commods{'Fine black cloth'}= $sources;
49 our %clients; # eg $clients{'ypp-sc-tools'}= [ qw(last-page) ];
51 # 's' = Special Circumstances; 'l' = local ; B = with Bleach
53 our (%pctb_commodmap,@pctb_commodmap);
55 my %colours; # eg $colours{'c'}{'black'}= $sources
56 my @rawcm; # eg $rawcm[0]='fine rum'; $rawcm[1]='fine %c cloth'
58 sub parse_master_master1 ($$) {
60 my $mm= new IO::File $mmfn, 'r' or die "$mmfn $!";
68 @ctx= (sub { $colours{$colourkind}{lc $_} .= $src; });
69 } elsif (m/^commods$/) {
70 @ctx= (sub { push @rawcm, lc $_; });
71 } elsif (m/^ocean (\w+)$/) {
74 $ocean or die; # ref to $ocean needed to work
78 $oceans{$ocean}{$arch}{$_} .= $src;
81 } elsif (m/^client (\S+.*\S)$/) {
83 $clients{$client}= [ ];
86 push @{ $clients{$client} }, $bug;
89 my $indent= length $&;
90 die "wrong indent $indent" unless defined $ctx[$indent-1];
91 &{ $ctx[$indent-1] }();
96 $mm->error and die $!;
99 #print Dumper(\%oceans);
100 #print Dumper(\@rawcm);
107 if ($s !~ m/\%(\w+)/) { $commods{ucfirst $s} .= $ss; return; }
108 die "unknown $&" unless defined $colours{$1};
109 foreach my $c (keys %{ $colours{$1} }) {
110 &$ca($`.$c.$', $ss .'%'. $colours{$1}{$c});
113 foreach (@rawcm) { &$ca($_,$src); }
116 sub parse_masters () {
117 parse_master_master1('master-master.txt','s');
120 sub parse_pctb_commodmap () {
121 undef %pctb_commodmap;
122 foreach my $commod (keys %commods) { $commods{$commod} =~ s/b//; }
124 my $c= new IO::File '_commodmap.tsv' or die $!;
125 if (!$c) { $!==&ENOENT or die $!; return 0; }
128 m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
129 die if defined $pctb_commodmap{$1}; $pctb_commodmap{$1}= $2;
130 die if defined $pctb_commodmap[$2]; $pctb_commodmap[$2]= $1;
133 $c->error and die $!;
138 sub get_our_version ($$) {
139 my ($aref,$prefix) = @_;
140 $aref->{"${prefix}name"}= 'ypp-sc-tools yarrg';
141 $aref->{"${prefix}fixes"}= 'lastpage';
143 my $version= `git-describe --tags HEAD`; $? and die $?;
145 $aref->{"${prefix}version"}= $version;
149 sub pipethrough_prep () {
150 my $tf= IO::File::new_tmpfile() or die $!;
154 sub pipethrough_run_along ($$$@) {
155 my ($tf, $childprep, $cmd, @a) = @_;
156 $tf->flush or die $!;
157 $tf->seek(0,0) or die $!;
158 my $fh= new IO::File;
159 my $child= $fh->open("-|"); defined $child or die $!;
161 open STDIN, "<&", $tf;
162 &$childprep() if defined $childprep;
163 exec $cmd @a; die $!;
167 sub pipethrough_run_finish ($) {
169 $fh->error and die $!;
170 close $fh or die "$! $?"; die $? if $?;
173 sub pipethrough_run ($$$@) {
174 my ($tf, $childprep, $cmd, @a) = @_;
175 my $pt= pipethrough_run_along($tf,$childprep,$cmd,@a);
177 { undef $/; $!=0; $r= <$pt>; }
178 defined $r or die $!;
179 pipethrough_run_finish($pt);
182 sub pipethrough_run_gzip ($) {
183 pipethrough_run($_[0],undef,'gzip','gzip');
186 sub cgipostform ($$$) {
187 my ($ua, $url, $form) = @_;
188 my $req= HTTP::Request::Common::POST($url,
190 Content_Type => 'form-data');
191 if ($url =~ m,^\.?/,) {
192 my $tf= pipethrough_prep();
193 print $tf $req->content() or die $!;
194 #print STDERR "[[[",$req->content(),"]]]";
195 my $out= pipethrough_run($tf, sub {
196 $ENV{'REQUEST_METHOD'}= 'POST';
197 $ENV{'QUERY_STRING'}= '';
198 $ENV{'PATH_TRANSLATED'}= $url;
199 $ENV{'PATH_INFO'}= '';
200 $ENV{'HTTP_HOST'}= 'localhost';
201 $ENV{'REMOTE_ADDR'}= '127.0.0.1';
202 $ENV{'GATEWAY_INTERFACE'}= 'CGI/1.1';
203 $ENV{'DOCUMENT_ROOT'}= '.';
204 $ENV{'SCRIPT_FILENAME'}= $url;
205 $ENV{'SCRIPT_NAME'}= $url;
206 $ENV{'HTTP_USER_AGENT'}= 'Commods.pm local test';
208 foreach my $f (qw(Content_Length Content_Type)) {
209 $ENV{uc $f}= $req->header($f);
211 #system 'printenv >&2';
214 $out =~ m,^Content-Type: text/plain.*\n\n, or die "$out ?";
217 my $resp= $ua->request($url,$req);
218 die $resp->status_line unless $resp->is_success;
219 return $resp->content();
225 sub check_tsv_line ($$) {
226 my ($l, $bad_data_callback) = @_;
227 my $bad_data= sub { &$bad_data_callback("bad data: line $.: $_[0]"); };
229 chomp($l) or &$bad_data('missing end-of-line');
231 $l !~ m/\P{IsPrint}/ or &$bad_data('nonprinting char(s)');
232 $l !~ m/\\/ or &$bad_data('data contains backslashes');
233 my @v= split /\t/, $l, -1;
234 @v==6 or &$bad_data('wrong number of fields');
235 my ($commod,$stall) = @v;
238 defined $commods{$commod} or
239 &$bad_data("unknown commodity \`$commod'");
241 $stall =~ m/^\p{IsUpper}|^[0-9]/ or &$bad_data("stall not capitalised");
242 !exists $check_tsv_done{$commod,$stall} or &$bad_data("repeated data");
243 $check_tsv_done{$commod,$stall}= 1;
244 foreach my $i (2..5) {
246 $f =~ m/^(|0|[1-9][0-9]{0,5}|\>1000)$/ or &$bad_data("bad field $i");
247 ($i % 2) or ($f !~ m/\>/) or &$bad_data("> in field $i price");