3 # Exchange.pl - currency exchange "module"
5 # Last update: 990818 08:30:10, bobby@bofh.dk
6 # 20021111 Tim Riker <Tim@Rikers.org>
17 use HTTP::Request::Common qw(POST GET);
20 $no_exchange++ if ($@);
24 my($LookFor,%Hash) = @_;
26 my $Found = (grep /$LookFor/i, keys %Hash)[0];
27 $Found =~ m/\((\w\w\w)\)/;
36 "AS", "AMERICAN SAMOA",
41 "AG", "ANTIGUA AND BARBUDA",
59 "BA", "BOSNIA AND HERZEGOWINA",
61 "BV", "BOUVET ISLAND",
63 "IO", "BRITISH INDIAN OCEAN TERRITORY",
64 "BN", "BRUNEI DARUSSALAM",
72 "KY", "CAYMAN ISLANDS",
73 "CF", "CENTRAL AFRICAN REPUBLIC",
77 "CX", "CHRISTMAS ISLAND",
78 "CC", "COCOS (KEELING) ISLANDS",
82 "CD", "CONGO, THE DEMOCRATIC REPUBLIC OF THE",
85 "CI", "COTE D'IVOIRE",
86 "HR", "CROATIA (local name: Hrvatska)",
89 "CZ", "CZECH REPUBLIC",
93 "DO", "DOMINICAN REPUBLIC",
98 "GQ", "EQUATORIAL GUINEA",
102 "FK", "FALKLAND ISLANDS (MALVINAS)",
103 "FO", "FAROE ISLANDS",
107 "FX", "FRANCE, METROPOLITAN",
108 "GF", "FRENCH GUIANA",
109 "PF", "FRENCH POLYNESIA",
110 "TF", "FRENCH SOUTHERN TERRITORIES",
124 "GW", "GUINEA-BISSAU",
127 "HM", "HEARD AND MC DONALD ISLANDS",
128 "VA", "HOLY SEE (VATICAN CITY STATE)",
135 "IR", "IRAN (ISLAMIC REPUBLIC OF)",
146 "KP", "KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF",
147 "KR", "KOREA, REPUBLIC OF",
150 "LA", "LAO PEOPLE'S DEMOCRATIC REPUBLIC",
155 "LY", "LIBYAN ARAB JAMAHIRIYA",
156 "LI", "LIECHTENSTEIN",
160 "MK", "MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF",
167 "MH", "MARSHALL ISLANDS",
173 "FM", "MICRONESIA, FEDERATED STATES OF",
174 "MD", "MOLDOVA, REPUBLIC OF",
185 "AN", "NETHERLANDS ANTILLES",
186 "NC", "NEW CALEDONIA",
192 "NF", "NORFOLK ISLAND",
193 "MP", "NORTHERN MARIANA ISLANDS",
199 "PG", "PAPUA NEW GUINEA",
210 "RU", "RUSSIAN FEDERATION",
212 "KN", "SAINT KITTS AND NEVIS",
214 "VC", "SAINT VINCENT AND THE GRENADINES",
217 "ST", "SAO TOME AND PRINCIPE",
218 "SA", "SAUDI ARABIA",
221 "SL", "SIERRA LEONE",
223 "SK", "SLOVAKIA (Slovak Republic)",
225 "SB", "SOLOMON ISLANDS",
227 "ZA", "SOUTH AFRICA",
228 "GS", "SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS",
232 "PM", "ST. PIERRE AND MIQUELON",
235 "SJ", "SVALBARD AND JAN MAYEN ISLANDS",
239 "SY", "SYRIAN ARAB REPUBLIC",
240 "TW", "TAIWAN, PROVINCE OF CHINA",
242 "TZ", "TANZANIA, UNITED REPUBLIC OF",
247 "TT", "TRINIDAD AND TOBAGO",
250 "TM", "TURKMENISTAN",
251 "TC", "TURKS AND CAICOS ISLANDS",
255 "AE", "UNITED ARAB EMIRATES",
256 "GB", "UNITED KINGDOM",
257 "US", "UNITED STATES",
258 "UM", "UNITED STATES MINOR OUTLYING ISLANDS",
264 "VG", "VIRGIN ISLANDS (BRITISH)",
265 "VI", "VIRGIN ISLANDS (U.S.)",
266 "WF", "WALLIS AND FUTUNA ISLANDS",
267 "EH", "WESTERN SAHARA",
278 &::DEBUG("exchange(@_)");
280 return "Exchange.pl needs LWP::UserAgent and HTTP::Request::Common"
283 my ($From, $To, $Amount, $Country);
285 if ($message =~ /^([\d\.\,]+)\s+(\S+)\s+(?:into|to|for)\s+(\S+)/i) {
286 ($Amount,$From,$To) = ($1,$2,$3);
287 $From = uc $From; $To = uc $To;
288 } elsif ($message =~ /^for\s(?:the\s)?([\w\s]+)/i) {
289 # looking up the currency for a country
292 return "that doesn't look right";
295 my $ua = new LWP::UserAgent;
296 #$ua->agent("Mozilla/5.0 " . $ua->agent); # Let's pretend
297 $ua->agent("Mozilla/5.0"); # Let's pretend
298 $ua->proxy('http', $::param{'httpProxy'}) if (&::IsParam("httpProxy"));
301 my $Referer = 'http://www.xe.net/ucc/full.shtml';
302 my $Converter='http://www.xe.net/ucc/convert.cgi';
304 # Get a list of currency abbreviations...
305 my $grab = GET $Referer;
306 my $reply = $ua->request($grab);
307 if (!$reply->is_success) {
308 return "EXCHANGE: ".$reply->status_line;
310 my $html = $reply->as_string;
311 my %Currencies = (grep /\S+/,
312 ($html =~ /option value="([^"]+)">.*?,\s*([^<]+)</gi)
315 my %CurrLookup = reverse ($html =~ /option value="([^"]+)">([^<]+)</gi);
319 # crysflame++ for the space fix.
321 foreach my $Found (grep /$Country/i, keys %CurrLookup){
322 $Found =~ s/,/ uses/g;
323 $retval .= "$Found, ";
325 $retval =~ s/(?:, )?\|?$//;
326 return substr($retval, 0, 510);
328 my %tld2country = &GetTlds;
329 if ($From =~ /^\.(\w\w)$/) { # Probably a tld
330 $From = $tld2country{uc $1};
332 if ($To =~ /^\.(\w\w)$/) { # Probably a tld
333 $To = $tld2country{uc $1};
336 # Make sure that $Amount is of the form \d+(\.\d\d)?
337 $Amount = sprintf("%.2f",$Amount);
339 # Get the exact currency abbreviations
340 my $newFrom = &GetAbb($From, %CurrLookup);
341 my $newTo = &GetAbb($To, %CurrLookup);
343 $From = $newFrom if $newFrom;
344 $To = $newTo if $newTo;
346 if (exists $Currencies{$From} and exists $Currencies{$To}) {
348 my $req = POST $Converter,
355 # Falsify where we came from
356 $req->referer($Referer);
358 my $res = $ua->request($req); # Submit request
360 if ($res->is_success) { # Went through ok
361 my $html = $res->as_string;
362 # parse each one to avoid undefined warnings
363 my ($When) = ($html =~ m/ as of (\d{4}\.\d\d.\d\d\s\d\d:\d\d:\d\d\s\S+)/gi);
364 my ($Cfrom) = ($html =~ m/(\d[\d,.]+)\s*$From/gi);
365 my ($Cto) = ($html =~ m/(\d[\d,.]+)\s*$To/gi);
366 #my ($When, $Cfrom, $Cto) =
367 # grep /\S+/, ($html =~ m/ as of (\d{4}\.\d\d.\d\d\s\d\d:\d\d:\d\d\s\S+)|(\d[\d,.]+)\s*$From|(\d[\d,.]+)\s* $To/gi);
370 return "$Cfrom $Currencies{$From} makes ".
371 "$Cto $Currencies{$To} (from http://www.xe.com/)"; # ." ($When)\n";
373 return "i got some error trying that";
376 return "EXCHANGE: ". $res->status_line;
379 return "Don't know about \"$From\" as a currency" if (!exists $Currencies{$From});
380 return "Don't know about \"$To\" as a currency" if (!exists $Currencies{$To});
387 &::performStrictReply(&exchange($args));
398 Exchange.pl - Exchange between currencies
403 HTTP::Request::Common
409 =head1 PUBLIC INTERFACE
411 Exchange <amount> <currency> for|[in]to <currency>
415 Contacts C<www.xe.net> and grabs the exchange rates; warning - the
416 currency code is a bit cranky.
420 Bobby <bobby@bofh.dk>