| 1 | package Bot::Cobalt::Plugin::Extras::Money; | ||
| 2 | $Bot::Cobalt::Plugin::Extras::Money::VERSION = '0.021001'; | ||
| 3 | 1 | ▶ | use Bot::Cobalt; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Bot::Cobalt::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | |||
| 6 | use URI::Escape; | ||
| 7 | use HTTP::Request; | ||
| 8 | |||
| 9 | sub new { bless {}, shift } | ||
| 10 | |||
| 11 | sub Cobalt_register { | ||
| 12 | my ($self, $core) = splice @_, 0, 2; | ||
| 13 | |||
| 14 | $self->{Cached} = {}; | ||
| 15 | |||
| 16 | register( $self, 'SERVER', | ||
| 17 | qw/ | ||
| 18 | public_cmd_currency | ||
| 19 | public_cmd_cc | ||
| 20 | public_cmd_money | ||
| 21 | |||
| 22 | currencyconv_rate_recv | ||
| 23 | currencyconv_expire_cache | ||
| 24 | / | ||
| 25 | ); | ||
| 26 | |||
| 27 | logger->info("Loaded: cc money currency"); | ||
| 28 | |||
| 29 | $core->timer_set( 1200, | ||
| 30 | { | ||
| 31 | Event => 'currencyconv_expire_cache', | ||
| 32 | Alias => $core->get_plugin_alias($self), | ||
| 33 | }, | ||
| 34 | 'CURRENCYCONV_CACHE' | ||
| 35 | ); | ||
| 36 | |||
| 37 | return PLUGIN_EAT_NONE | ||
| 38 | } | ||
| 39 | |||
| 40 | sub Cobalt_unregister { | ||
| 41 | my ($self, $core) = splice @_, 0, 2; | ||
| 42 | |||
| 43 | logger->info("Unloaded"); | ||
| 44 | |||
| 45 | return PLUGIN_EAT_NONE | ||
| 46 | } | ||
| 47 | |||
| 48 | sub Bot_currencyconv_expire_cache { | ||
| 49 | my ($self, $core) = splice @_, 0, 2; | ||
| 50 | |||
| 51 | for my $fromto (keys %{ $self->{Cached} }) { | ||
| 52 | my $delta = time - $self->{Cached}->{$fromto}->{TS}; | ||
| 53 | |||
| 54 | if ($delta >= 1200) { | ||
| 55 | logger->debug("expired cached: $fromto"); | ||
| 56 | |||
| 57 | delete $self->{Cached}->{$fromto}; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | $core->timer_set( 1200, | ||
| 62 | { | ||
| 63 | Event => 'currencyconv_expire_cache', | ||
| 64 | Alias => $core->get_plugin_alias($self), | ||
| 65 | }, | ||
| 66 | 'CURRENCYCONV_CACHE' | ||
| 67 | ); | ||
| 68 | |||
| 69 | return PLUGIN_EAT_ALL; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub Bot_public_cmd_currency { | ||
| 73 | my ($self, $core) = splice @_, 0, 2; | ||
| 74 | my $msg = ${ $_[0] }; | ||
| 75 | my $context = $msg->context; | ||
| 76 | |||
| 77 | my $channel = $msg->channel; | ||
| 78 | |||
| 79 | my $message = $msg->message_array; | ||
| 80 | my ($value, $from, undef, $to) = @$message; | ||
| 81 | |||
| 82 | unless ($value && $from && $to) { | ||
| 83 | broadcast( 'message', $context, $channel, | ||
| 84 | "Syntax: !cc <value> <abbrev> TO <abbrev>" | ||
| 85 | ); | ||
| 86 | return PLUGIN_EAT_ALL | ||
| 87 | } | ||
| 88 | |||
| 89 | my $valid_val = qr/^(\d+)?\.?(\d+)?$/; | ||
| 90 | my $valid_abbrev = qr/^[a-zA-Z]{3}$/; | ||
| 91 | |||
| 92 | unless ($value =~ $valid_val) { | ||
| 93 | broadcast( 'message', $context, $channel, | ||
| 94 | "$value is not a valid quantity." | ||
| 95 | ); | ||
| 96 | return PLUGIN_EAT_ALL | ||
| 97 | } | ||
| 98 | |||
| 99 | unless ($from =~ $valid_abbrev && $to =~ $valid_abbrev) { | ||
| 100 | broadcast( 'message', $context, $channel, | ||
| 101 | "Currency codes must be three-letter abbreviations." | ||
| 102 | ); | ||
| 103 | return PLUGIN_EAT_ALL | ||
| 104 | } | ||
| 105 | |||
| 106 | $self->_request_conversion_rate( | ||
| 107 | uc($from), uc($to), $value, $context, $channel | ||
| 108 | ); | ||
| 109 | |||
| 110 | return PLUGIN_EAT_ALL | ||
| 111 | } | ||
| 112 | |||
| 113 | sub Bot_public_cmd_cc { Bot_public_cmd_currency(@_) } | ||
| 114 | sub Bot_public_cmd_money { Bot_public_cmd_currency(@_) } | ||
| 115 | |||
| 116 | sub Bot_currencyconv_rate_recv { | ||
| 117 | my ($self, $core) = splice @_, 0, 2; | ||
| 118 | my $response = ${ $_[1] }; | ||
| 119 | my $args = ${ $_[2] }; | ||
| 120 | my ($value, $context, $channel, $from, $to) = @$args; | ||
| 121 | |||
| 122 | unless ($response->is_success) { | ||
| 123 | if ($response->code == 500) { | ||
| 124 | broadcast( 'message', $context, $channel, | ||
| 125 | "Received error 500; is your currency code valid?" | ||
| 126 | ); | ||
| 127 | } else { | ||
| 128 | broadcast( 'message', $context, $channel, | ||
| 129 | "HTTP failed: ".$response->code | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | return PLUGIN_EAT_ALL | ||
| 133 | } | ||
| 134 | |||
| 135 | my $content = $response->decoded_content; | ||
| 136 | |||
| 137 | my($rate,$converted); | ||
| 138 | if ( $content =~ /<double.*>(.*)<\/double>/i ) { | ||
| 139 | $rate = $1||1; | ||
| 140 | $converted = sprintf("%.2f", $value * $rate); | ||
| 141 | } else { | ||
| 142 | broadcast( 'message', $context, $channel, | ||
| 143 | "Failed to retrieve currency conversion ($from -> $to)" | ||
| 144 | ); | ||
| 145 | return PLUGIN_EAT_ALL | ||
| 146 | } | ||
| 147 | |||
| 148 | my $cachekey = "${from}-${to}"; | ||
| 149 | $self->{Cached}->{$cachekey} = { | ||
| 150 | Rate => $rate, | ||
| 151 | TS => time, | ||
| 152 | }; | ||
| 153 | |||
| 154 | broadcast( 'message', $context, $channel, | ||
| 155 | "$value $from == $converted $to" | ||
| 156 | ); | ||
| 157 | |||
| 158 | return PLUGIN_EAT_ALL | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _request_conversion_rate { | ||
| 162 | my ($self, $from, $to, $value, $context, $channel) = @_; | ||
| 163 | return unless $from and $to; | ||
| 164 | |||
| 165 | ## maybe cached | ||
| 166 | my $cachekey = "${from}-${to}"; | ||
| 167 | if ($self->{Cached}->{$cachekey}) { | ||
| 168 | my $cachedrate = $self->{Cached}->{$cachekey}->{Rate}; | ||
| 169 | my $converted = $value * $cachedrate; | ||
| 170 | |||
| 171 | broadcast( 'message', $context, $channel, | ||
| 172 | "$value $from == $converted $to" | ||
| 173 | ); | ||
| 174 | |||
| 175 | return 1 | ||
| 176 | } | ||
| 177 | |||
| 178 | my $uri = | ||
| 179 | "http://www.webservicex.net/CurrencyConvertor.asmx" | ||
| 180 | ."/ConversionRate?FromCurrency=${from}&ToCurrency=${to}"; | ||
| 181 | |||
| 182 | if ( core()->Provided->{www_request} ) { | ||
| 183 | my $req = HTTP::Request->new( 'GET', $uri ) || return undef; | ||
| 184 | |||
| 185 | broadcast( 'www_request', | ||
| 186 | $req, | ||
| 187 | 'currencyconv_rate_recv', | ||
| 188 | [ $value, $context, $channel, $from, $to ], | ||
| 189 | ); | ||
| 190 | } else { | ||
| 191 | broadcast( 'message', $context, $channel, | ||
| 192 | "No async HTTP available; try loading Bot::Cobalt::Plugin::WWW" | ||
| 193 | ); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | __END__ | ||