File Coverage

blib/lib/Bot/Cobalt/Plugin/Extras/Money.pm
Criterion Covered Total %
statement 13 88 14.7
branch 0 20 0.0
condition 0 16 0.0
subroutine 5 13 38.4
pod 0 8 0.0
total 18 145 12.4


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