root/lang/perl/Moxy/trunk/lib/Moxy/Plugin/GPS.pm @ 3964

Revision 3964, 4.6 kB (checked in by tokuhirom, 5 years ago)

Moxy: added documents.

Line 
1package Moxy::Plugin::GPS;
2use strict;
3use warnings;
4use base qw/Moxy::Plugin/;
5use URI::Escape;
6use Carp;
7use URI;
8use Path::Class;
9
10sub register {
11    my ($class, $context) = @_;
12
13    # au
14    # TODO: gpsone
15    $context->register_hook(
16        'response_filter_E' => sub {
17            my ( $context, $args ) = @_;
18
19            ${ $args->{content_ref} }
20                =~ s{device:location\?url=([^'"> ]+)}{"http://gps.moxy/au/?redirect_to=" . uri_escape($1)}ge;
21        },
22        request_filter_E => sub {
23            my ( $context, $args ) = @_;
24
25            if ( $args->{request}->uri =~ m{^http://gps\.moxy/au/\?redirect_to=(.+)} ) {
26                my $redirect_to = uri_unescape($1);
27
28                # XXX this is suck, but au ua works like this. orz.
29                $redirect_to .= '?datum=tokyo&unit=dms&lat=35.37.16.00&lon=139.43.38.25';
30
31                my $response = HTTP::Response->new( 302, 'Redirect by Moxy(GPS)' );
32                $context->log(debug => "Redirect GPS to : $redirect_to");
33                $response->header(Location => $redirect_to);
34                $args->{filter}->proxy->response($response);
35            }
36        }
37    );
38
39    # willcom
40    $context->register_hook(
41        request_filter_H => sub {
42            my ( $context, $args ) = @_;
43
44            if ($args->{request}->uri =~ m{^http://location\.request/dummy\.cgi\?my=(.+)&pos=\$location$}) {
45                my $redirect_to = $1;
46
47                $context->log(debug => "redirect uri is $redirect_to");
48
49                $redirect_to .= '?pos=N35.37.12.543E139.43.29.920';
50
51                my $response = HTTP::Response->new( 302, 'Redirect by Moxy(GPS willcom)' );
52                $response->header(Location => $redirect_to);
53                return $args->{filter}->proxy->response($response);
54            }
55        }
56    );
57
58    # docomo iarea
59    #  TODO: support navi_pos
60    #  TODO: support lcs
61    $context->register_hook(
62        request_filter_I => sub {
63            my ( $context, $args ) = @_;
64
65            if ($args->{request}->uri =~ m{^http://w1m\.docomo\.ne\.jp/cp/iarea}) {
66                # TODO: support post?
67                $context->log(debug => "request uri is @{[ $args->{request}->uri ]}");
68
69                my %queries = URI->new($args->{request}->uri)->query_form;
70
71                # validation
72                my $errstr;
73                if ($queries{ecode} ne 'OPENAREACODE') {
74                    $errstr = 'ecode should be OPENAREACODE';
75                }
76                if ($queries{msn} ne 'OPENAREAKEY') {
77                    $errstr = 'msn should be OPENAREAKEY';
78                }
79                if (not exists $queries{nl}) {
80                    $errstr = 'nl missing';
81                }
82                if ($queries{nl} !~ m[^http://]) {
83                    $errstr = 'nl should start with http://';
84                }
85                if (length($queries{nl})>=256) {
86                    $errstr = 'nl too long(256)';
87                }
88
89                my $output = $class->render_template(
90                    $context,
91                    'iarea.tt' => {
92                        errstr  => $errstr,
93                        queries => \%queries,
94                    }
95                );
96
97                my $response = HTTP::Response->new( 200, 'Moxy(GPS iarea)' );
98                $response->content($output);
99                return $args->{filter}->proxy->response($response);
100            }
101        }
102    );
103
104    # softbank
105    #   TODO: support vodafone(z attribute)
106    $context->register_hook(
107        response_filter_V => sub {
108            my ( $context, $args ) = @_;
109
110            ${ $args->{content_ref} }
111                =~ s{location:(?:cell|gps|auto)\?url=([^'"> ]+)}{"http://gps.moxy/softbank/?redirect_to=" . uri_escape($1)}ge;
112        },
113        request_filter_V => sub {
114            my ( $context, $args ) = @_;
115
116            if ( $args->{request}->uri =~ m{^http://gps\.moxy/softbank/\?redirect_to=(.+)} ) {
117                my $redirect_to = uri_unescape($1);
118
119                # XXX this is suck, but vodafone ua works like this. orz.
120                $redirect_to .= '?geo=wgs84&pos=N35.37.29.12E139.43.8.45';
121
122                my $response = HTTP::Response->new( 302, 'Redirect by Moxy(GPS)' );
123                $context->log(debug => "Redirect GPS to : $redirect_to");
124                $response->header(Location => $redirect_to);
125                $args->{filter}->proxy->response($response);
126            }
127        }
128    );
129}
130
1311;
132__END__
133
134=head1 NAME
135
136Moxy::Plugin::GPS - gps simulation for Moxy
137
138=head1 SYNOPSIS
139
140  - module: GPS
141
142=head1 DESCRIPTION
143
144GPS simulation feature for Moxy.
145
146=head1 TODO
147
148    support gpsone(au)
149    support posinfo(docomo)
150    support select pos
151
152=head1 SEE ALSO
153
154L<Moxy>
Note: See TracBrowser for help on using the browser.