| 1 | package Amethyst::Brain::Infobot::Module::Exchange; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw(@ISA | ||
| 5 | %CODE2CODE %TLD2CODE %CURR2CODE %COUNTRY2CODE | ||
| 6 | %CODE2CURR | ||
| 7 | $REFERER $CONVERTER); | ||
| 8 | use Data::Dumper; | ||
| 9 | use POE; | ||
| 10 | use POE::Component::Client::UserAgent; | ||
| 11 | use HTTP::Request::Common; | ||
| 12 | use HTTP::Response; | ||
| 13 | use LWP::UserAgent; # For init | ||
| 14 | use Amethyst::Message; | ||
| 15 | use Amethyst::Brain::Infobot; | ||
| 16 | use Amethyst::Brain::Infobot::Module; | ||
| 17 | |||
| 18 | @ISA = qw(Amethyst::Brain::Infobot::Module); | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $class = shift; | ||
| 22 | my $self = $class->SUPER::new( | ||
| 23 | Name => 'Exchange', | ||
| 24 | Regex => qr/^(?:ex)?change\s+(\d+)\s+(\w+)\s+(?:into|to|for)\s+(\w+)/x, | ||
| 25 | Usage => '(ex)?change 100 USD for DEM', | ||
| 26 | Description => "Convert currencies", | ||
| 27 | @_ | ||
| 28 | ); | ||
| 29 | |||
| 30 | return bless $self, $class; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub init { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | eval { spawn POE::Component::Client::UserAgent; }; | ||
| 37 | if ($@) { | ||
| 38 | die $@ unless $@ =~ /^alias is in use by another session/; | ||
| 39 | } | ||
| 40 | |||
| 41 | $REFERER = 'http://www.xe.net/ucc/full.shtml'; | ||
| 42 | $CONVERTER ='http://www.xe.net/ucc/convert.cgi'; | ||
| 43 | |||
| 44 | my $ua = new LWP::UserAgent; | ||
| 45 | my $uri = new URI($REFERER); | ||
| 46 | my $request = new HTTP::Request(GET => $uri); | ||
| 47 | |||
| 48 | print STDERR "Requesting $uri\n"; | ||
| 49 | |||
| 50 | my $response = $ua->request($request); | ||
| 51 | |||
| 52 | unless ($response->is_success) { | ||
| 53 | print STDERR $response->error_as_HTML; | ||
| 54 | die "Failed to contact currency converter"; | ||
| 55 | } | ||
| 56 | |||
| 57 | my $html = $response->content; | ||
| 58 | $html =~ s|.*<SELECT[^>]*>(.*?)</SELECT.*|$1|s; | ||
| 59 | |||
| 60 | my @data = | ||
| 61 | map { | ||
| 62 | [ $_ =~ /=\"([^\"]+)\">(.*?)(?:,\s*(.*?))? \(...\)</g ] | ||
| 63 | } | ||
| 64 | grep /\S/, | ||
| 65 | split /\n/, $html; | ||
| 66 | |||
| 67 | %CURR2CODE = map { defined $_->[2] | ||
| 68 | ? (lc $_->[2] => uc $_->[0]) | ||
| 69 | : () } @data; | ||
| 70 | %COUNTRY2CODE = map { lc $_->[1] => uc $_->[0] } @data; | ||
| 71 | %CODE2CODE = map { lc $_->[0] => uc $_->[0] } @data; | ||
| 72 | |||
| 73 | %CODE2CURR = reverse %CURR2CODE; | ||
| 74 | |||
| 75 | TLD: | ||
| 76 | while (<DATA>) { | ||
| 77 | chomp; | ||
| 78 | |||
| 79 | my ($tld, $country) = split /\s+/, $_, 2; | ||
| 80 | if (exists $COUNTRY2CODE{lc $country}) { | ||
| 81 | $TLD2CODE{lc $tld} = $COUNTRY2CODE{lc $country}; | ||
| 82 | next TLD; | ||
| 83 | } | ||
| 84 | |||
| 85 | ($tld, $country) = split /\s+/, $_, 3; | ||
| 86 | if (exists $COUNTRY2CODE{lc $country}) { | ||
| 87 | $TLD2CODE{lc $tld} = $COUNTRY2CODE{lc $country}; | ||
| 88 | next TLD; | ||
| 89 | } | ||
| 90 | |||
| 91 | # print STDERR "Unable to identify $_\n"; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub action { | ||
| 96 | my ($self, $message, $number, $from, $to) = @_; | ||
| 97 | |||
| 98 | $from = lc $from; | ||
| 99 | $to = lc $to; | ||
| 100 | |||
| 101 | my @all = (keys %CODE2CODE, keys %CURR2CODE, keys %COUNTRY2CODE, | ||
| 102 | keys %TLD2CODE); | ||
| 103 | |||
| 104 | print STDERR join(", ", sort @all); | ||
| 105 | |||
| 106 | my $fcode = $CODE2CODE{$from} | ||
| 107 | || $CURR2CODE{$from} | ||
| 108 | || $COUNTRY2CODE{$from} | ||
| 109 | || $TLD2CODE{$from}; | ||
| 110 | |||
| 111 | my $tcode = $CODE2CODE{$to} | ||
| 112 | || $CURR2CODE{$to} | ||
| 113 | || $COUNTRY2CODE{$to} | ||
| 114 | || $TLD2CODE{$to}; | ||
| 115 | |||
| 116 | unless (defined $fcode) { | ||
| 117 | my $reply = $self->reply_to($message, "Unknown currency $from"); | ||
| 118 | $reply->send; | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | |||
| 122 | unless (defined $tcode) { | ||
| 123 | my $reply = $self->reply_to($message, "Unknown currency $to"); | ||
| 124 | $reply->send; | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | my %states = map { $_ => "handler_$_" } qw( | ||
| 129 | _start response | ||
| 130 | ); | ||
| 131 | |||
| 132 | print STDERR "Creating child session for exchange\n"; | ||
| 133 | |||
| 134 | POE::Session->create( | ||
| 135 | package_states => [ ref($self) => \%states ], | ||
| 136 | args => [ $self, $message, $number, $fcode, $tcode ], | ||
| 137 | ); | ||
| 138 | |||
| 139 | return 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub handler_response { | ||
| 143 | my ($kernel, $heap, $session, $pbargs) = | ||
| 144 | @_[KERNEL, HEAP, SESSION, ARG1]; | ||
| 145 | my ($request, $response, $entry) = @$pbargs; | ||
| 146 | |||
| 147 | unless ($response->is_success) { | ||
| 148 | my $reply = $heap->{Module}->reply_to($heap->{Message}, | ||
| 149 | "HTTP Request failed"); | ||
| 150 | $reply->send; | ||
| 151 | print STDERR $response->error_as_HTML; | ||
| 152 | return; | ||
| 153 | } | ||
| 154 | |||
| 155 | my $html = $response->content; | ||
| 156 | |||
| 157 | unless ($html =~ /Live Rates.*\+1><B>([^\n]*)\n(?:.*)\+1><B>([^\n]*)/si) | ||
| 158 | { | ||
| 159 | my $reply = $heap->{Module}->reply_to($heap->{Message}, | ||
| 160 | "Failed to parse response"); | ||
| 161 | $reply->send; | ||
| 162 | print STDERR $response->content; | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | my $out = "$1 = $2"; | ||
| 167 | $out =~ s/<.*?>/ /g; | ||
| 168 | $out =~ s/\s+/ /g; | ||
| 169 | |||
| 170 | my $reply = $heap->{Module}->reply_to($heap->{Message}, $out); | ||
| 171 | $reply->send; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub handler__start { | ||
| 175 | my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; | ||
| 176 | my ($module, $message, $number, $fcode, $tcode) = @_[ARG0..$#_]; | ||
| 177 | |||
| 178 | $heap->{Module} = $module; | ||
| 179 | $heap->{Message} = $message; | ||
| 180 | |||
| 181 | my $uri = new URI($CONVERTER); | ||
| 182 | $uri->query_form( | ||
| 183 | From => $fcode, | ||
| 184 | To => $tcode, | ||
| 185 | Amount => $number, | ||
| 186 | ); | ||
| 187 | $uri = $uri->canonical; | ||
| 188 | my $request = new HTTP::Request(GET => $uri); | ||
| 189 | $request->referer($REFERER); | ||
| 190 | |||
| 191 | my $postback = $session->postback('response'); | ||
| 192 | |||
| 193 | $kernel->post('useragent', 'request', | ||
| 194 | request => $request, | ||
| 195 | response => $postback, | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | |||
| 199 | 1; | ||
| 200 | |||
| 201 | __DATA__ |