1#!/usr/bin/perl -w
2use HTTP::Proxy;
3use HTTP::Proxy::HeaderFilter::simple;
4use HTTP::Proxy::BodyFilter::simple;
5use HTTP::Proxy::BodyFilter::complete;
6use MIME::Base64;
7use Fcntl ':flock';
8use strict;
9
10# the proxy
11my $proxy = HTTP::Proxy->new( @ARGV );
12
13# the status page:
14# - auto-refresh (quickly at first, then more slowly)
15# - count the number of games and modify the title
16my $seen_title;
17$proxy->push_filter(
18    host     => 'www.dragongoserver.net',
19    path     => '^/status.php',
20    # auto-refresh
21    response => HTTP::Proxy::HeaderFilter::simple->new(
22        sub {
23            my ( $self, $headers, $response ) = @_;
24            ($response->request->uri->query || '') =~ /reload=(\d+)/;
25            my $n = ($1 || 0) + 1;
26            my $delay = $n < 5 ? 30 : $n < 15 ? 60 : $n < 25 ? 300 : 3600;
27            $headers->push_header( Refresh => "$delay;url="
28                  . $response->request->uri->path
29                  . "?reload=$n" );
30        }
31    ),
32    # count games
33    response => HTTP::Proxy::BodyFilter::complete->new(),
34    response => HTTP::Proxy::BodyFilter::simple->new(
35        filter => sub {
36             my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
37             next if ! $$dataref;
38
39             # count the games and change the title
40             my $n = 0; $n++ while $$dataref =~ /game\.php\?gid=\d+/g;
41             my $s = $n > 1 ? "s" : ""; $n ||= "No";
42             $$dataref =~ s!<TITLE>.*?</TITLE>!<TITLE>$n go game$s pending</TITLE>!s;
43        },
44    ),
45);
46
47# the game page:
48# - remove the Message: textarea
49# - add a link to make it appear when needed
50$proxy->push_filter(
51    host     => 'www.dragongoserver.net',
52    path     => '^/game.php',
53    response => HTTP::Proxy::BodyFilter::complete->new(),
54    response => HTTP::Proxy::BodyFilter::simple->new(
55      sub {
56          my $msg = '&msg=yes';
57          my $uri = $_[2]->request->uri;
58          if( $uri =~ s/$msg//o ) { $msg = ''; }
59          else { ${$_[1]} =~ s|(</?textarea.*>)|<!-- $1 -->|; }
60          ${$_[1]} =~ s|(Message:)|<a href="$uri$msg">$1</a>|;
61      }
62    )
63);
64
65$proxy->start;
66
67