| 1 | package Bot::Cobalt::Plugin::Extras::CPAN; | ||
| 2 | $Bot::Cobalt::Plugin::Extras::CPAN::VERSION = '0.021003'; | ||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use Bot::Cobalt; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Bot::Cobalt::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | use Bot::Cobalt::Serializer; | ||
| 9 | our $Serializer = Bot::Cobalt::Serializer->new('JSON'); | ||
| 10 | |||
| 11 | use HTTP::Request; | ||
| 12 | |||
| 13 | use Module::CoreList; | ||
| 14 | |||
| 15 | use Try::Tiny; | ||
| 16 | |||
| 17 | our $HelpText | ||
| 18 | = 'try: dist, latest, tests, abstract, changes, belongs, license'; | ||
| 19 | |||
| 20 | ## FIXME cachedb? | ||
| 21 | |||
| 22 | sub new { bless [], shift } | ||
| 23 | |||
| 24 | sub Cobalt_register { | ||
| 25 | my ($self, $core) = splice @_, 0, 2; | ||
| 26 | |||
| 27 | register $self, SERVER => qw/ | ||
| 28 | public_cmd_cpan | ||
| 29 | public_cmd_corelist | ||
| 30 | mcpan_plug_resp_recv | ||
| 31 | /; | ||
| 32 | |||
| 33 | logger->info("Loaded: !cpan"); | ||
| 34 | |||
| 35 | PLUGIN_EAT_NONE | ||
| 36 | } | ||
| 37 | |||
| 38 | sub Cobalt_unregister { | ||
| 39 | my ($self, $core) = splice @_, 0, 2; | ||
| 40 | logger->info("Bye!"); | ||
| 41 | PLUGIN_EAT_NONE | ||
| 42 | } | ||
| 43 | |||
| 44 | sub Bot_public_cmd_corelist { | ||
| 45 | my ($self, $core) = splice @_, 0, 2; | ||
| 46 | my $msg = ${ $_[0] }; | ||
| 47 | |||
| 48 | my $dist = $msg->message_array->[0]; | ||
| 49 | |||
| 50 | unless ($dist) { | ||
| 51 | broadcast( 'message', | ||
| 52 | $msg->context, $msg->channel, | ||
| 53 | "corelist needs a module name." | ||
| 54 | ); | ||
| 55 | return PLUGIN_EAT_ALL | ||
| 56 | } | ||
| 57 | |||
| 58 | my $resp; | ||
| 59 | my $vers = $msg->message_array->[1]; | ||
| 60 | if (my $first = Module::CoreList->first_release($dist, $vers)) { | ||
| 61 | $resp = $vers ? | ||
| 62 | "$dist ($vers) was released with $first" | ||
| 63 | : "$dist was released with $first" | ||
| 64 | } else { | ||
| 65 | $resp = "Module not found in core." | ||
| 66 | } | ||
| 67 | |||
| 68 | broadcast( 'message', | ||
| 69 | $msg->context, $msg->channel, | ||
| 70 | join(', ', $msg->src_nick, $resp) | ||
| 71 | ); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub Bot_public_cmd_cpan { | ||
| 75 | my ($self, $core) = splice @_, 0, 2; | ||
| 76 | my $msg = ${ $_[0] }; | ||
| 77 | |||
| 78 | my ($cmd, $dist) = @{ $msg->message_array }; | ||
| 79 | |||
| 80 | unless ($cmd) { | ||
| 81 | broadcast( 'message', | ||
| 82 | $msg->context, $msg->channel, | ||
| 83 | "No command; $HelpText" | ||
| 84 | ); | ||
| 85 | return PLUGIN_EAT_ALL | ||
| 86 | } | ||
| 87 | |||
| 88 | unless ($dist) { | ||
| 89 | # assume 'abstract' if only one arg | ||
| 90 | $dist = $cmd; | ||
| 91 | $cmd = 'abstract'; | ||
| 92 | } | ||
| 93 | |||
| 94 | $cmd = lc $cmd; | ||
| 95 | $dist =~ s/::/-/g | ||
| 96 | unless $cmd eq "belongs" | ||
| 97 | or $cmd eq "changes"; | ||
| 98 | my $url = "/release/$dist"; | ||
| 99 | |||
| 100 | my $hints = +{ | ||
| 101 | Context => $msg->context, | ||
| 102 | Channel => $msg->channel, | ||
| 103 | Nick => $msg->src_nick, | ||
| 104 | Dist => $dist, | ||
| 105 | Link => "http://www.metacpan.org${url}", | ||
| 106 | }; | ||
| 107 | |||
| 108 | CMD: { | ||
| 109 | if ($cmd eq 'latest' || $cmd eq 'release') { | ||
| 110 | $hints->{Type} = 'latest'; | ||
| 111 | last CMD | ||
| 112 | } | ||
| 113 | |||
| 114 | if ($cmd eq 'dist') { | ||
| 115 | $hints->{Type} = 'dist'; | ||
| 116 | last CMD | ||
| 117 | } | ||
| 118 | |||
| 119 | if ($cmd eq 'test' || $cmd eq 'tests') { | ||
| 120 | $hints->{Type} = 'tests'; | ||
| 121 | last CMD | ||
| 122 | } | ||
| 123 | |||
| 124 | if ($cmd eq 'info' || $cmd eq 'abstract') { | ||
| 125 | $hints->{Type} = 'abstract'; | ||
| 126 | last CMD | ||
| 127 | } | ||
| 128 | |||
| 129 | if ($cmd eq 'license') { | ||
| 130 | $hints->{Type} = 'license'; | ||
| 131 | last CMD | ||
| 132 | } | ||
| 133 | |||
| 134 | if ($cmd eq 'belongs') { | ||
| 135 | $hints->{Type} = 'belongs'; | ||
| 136 | $url = "/module/$dist"; | ||
| 137 | last CMD | ||
| 138 | } | ||
| 139 | |||
| 140 | if ($cmd eq 'changes') { | ||
| 141 | $hints->{Type} = 'changes'; | ||
| 142 | $url = "/module/$dist"; | ||
| 143 | last CMD | ||
| 144 | } | ||
| 145 | |||
| 146 | broadcast( 'message', $msg->context, $msg->channel, | ||
| 147 | "Unknown query; $HelpText" | ||
| 148 | ); | ||
| 149 | } | ||
| 150 | |||
| 151 | $self->_request($url, $hints) if defined $hints->{Type}; | ||
| 152 | |||
| 153 | PLUGIN_EAT_ALL | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _request { | ||
| 157 | my ($self, $url, $hints) = @_; | ||
| 158 | |||
| 159 | my $base_url = 'http://api.metacpan.org'; | ||
| 160 | my $this_url = $base_url . $url; | ||
| 161 | |||
| 162 | logger->debug("metacpan request: $this_url"); | ||
| 163 | |||
| 164 | my $request = HTTP::Request->new(GET => $this_url); | ||
| 165 | |||
| 166 | broadcast( 'www_request', | ||
| 167 | $request, | ||
| 168 | 'mcpan_plug_resp_recv', | ||
| 169 | $hints | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub Bot_mcpan_plug_resp_recv { | ||
| 174 | my ($self, $core) = splice @_, 0, 2; | ||
| 175 | my $response = ${ $_[1] }; | ||
| 176 | my $hints = ${ $_[2] }; | ||
| 177 | |||
| 178 | my $dist = $hints->{Dist}; | ||
| 179 | my $type = $hints->{Type}; | ||
| 180 | my $link = $hints->{Link}; | ||
| 181 | |||
| 182 | unless ($response->is_success) { | ||
| 183 | my $status = $response->code; | ||
| 184 | if ($status == 404) { | ||
| 185 | broadcast( 'message', | ||
| 186 | $hints->{Context}, $hints->{Channel}, | ||
| 187 | "No such distribution: $dist" | ||
| 188 | ); | ||
| 189 | } else { | ||
| 190 | broadcast( 'message', | ||
| 191 | $hints->{Context}, $hints->{Channel}, | ||
| 192 | "Could not get release info for $dist ($status)" | ||
| 193 | ); | ||
| 194 | } | ||
| 195 | return PLUGIN_EAT_ALL | ||
| 196 | } | ||
| 197 | |||
| 198 | my $json = $response->content; | ||
| 199 | unless ($json) { | ||
| 200 | broadcast('message', | ||
| 201 | $hints->{Context}, $hints->{Channel}, | ||
| 202 | "Unknown failure -- no data received for $dist", | ||
| 203 | ); | ||
| 204 | return PLUGIN_EAT_ALL | ||
| 205 | } | ||
| 206 | |||
| 207 | my $d_hash = | ||
| 208 | try { $Serializer->thaw($json) } | ||
| 209 | catch { | ||
| 210 | broadcast( 'message', | ||
| 211 | $hints->{Context}, $hints->{Channel}, | ||
| 212 | "thaw failure; err: $_", | ||
| 213 | ); | ||
| 214 | undef | ||
| 215 | } or return PLUGIN_EAT_ALL; | ||
| 216 | |||
| 217 | unless ($d_hash && ref $d_hash eq 'HASH') { | ||
| 218 | broadcast( 'message', | ||
| 219 | $hints->{Context}, $hints->{Channel}, | ||
| 220 | "thaw failure for $dist; expected a HASH but got '$d_hash'" | ||
| 221 | ); | ||
| 222 | return PLUGIN_EAT_ALL | ||
| 223 | } | ||
| 224 | |||
| 225 | my $resp; | ||
| 226 | my $prefix = color bold => 'mCPAN'; | ||
| 227 | |||
| 228 | TYPE: { | ||
| 229 | |||
| 230 | if ($type eq 'abstract') { | ||
| 231 | my $abs = $d_hash->{abstract} || 'No abstract available.'; | ||
| 232 | my $vers = $d_hash->{version}; | ||
| 233 | $resp = "$prefix: ($dist $vers) $abs ; $link"; | ||
| 234 | last TYPE | ||
| 235 | } | ||
| 236 | |||
| 237 | if ($type eq 'dist') { | ||
| 238 | my $dl = $d_hash->{download_url} || 'No download link available.'; | ||
| 239 | $resp = "$prefix: ($dist) $dl"; | ||
| 240 | last TYPE | ||
| 241 | } | ||
| 242 | |||
| 243 | if ($type eq 'latest') { | ||
| 244 | my $vers = $d_hash->{version}; | ||
| 245 | my $arc = $d_hash->{archive}; | ||
| 246 | $resp = "$prefix: ($dist) Latest is $vers ($arc) ; $link"; | ||
| 247 | last TYPE | ||
| 248 | } | ||
| 249 | |||
| 250 | if ($type eq 'license') { | ||
| 251 | my $name = $d_hash->{name}; | ||
| 252 | my $lic = join ' ', @{ $d_hash->{license}||['undef'] }; | ||
| 253 | $resp = "$prefix: License terms for $name: $lic"; | ||
| 254 | last TYPE | ||
| 255 | } | ||
| 256 | |||
| 257 | if ($type eq 'tests') { | ||
| 258 | my %tests = %{ | ||
| 259 | keys %{$d_hash->{tests}||{}} ? | ||
| 260 | $d_hash->{tests} | ||
| 261 | : { pass => 0, fail => 0, na => 0, unknown => 0 } | ||
| 262 | }; | ||
| 263 | |||
| 264 | my $vers = $d_hash->{version}; | ||
| 265 | |||
| 266 | $resp = sprintf("%s: (%s %s) %d PASS, %d FAIL, %d NA, %d UNKNOWN", | ||
| 267 | $prefix, $dist, $vers, | ||
| 268 | $tests{pass}, $tests{fail}, $tests{na}, $tests{unknown} | ||
| 269 | ); | ||
| 270 | |||
| 271 | last TYPE | ||
| 272 | } | ||
| 273 | |||
| 274 | if ($type eq 'belongs') { | ||
| 275 | my $release = $d_hash->{release}; | ||
| 276 | $resp = "$prefix: $dist belongs to release $release"; | ||
| 277 | last TYPE | ||
| 278 | } | ||
| 279 | |||
| 280 | if ($type eq 'changes') { | ||
| 281 | my $release = $d_hash->{release}; | ||
| 282 | my $actuald = substr $release, 0, rindex $release, '-'; | ||
| 283 | my $link = "https://www.metacpan.org/changes/distribution/$actuald"; | ||
| 284 | $resp = "$prefix: Changes for $release: $link"; | ||
| 285 | last TYPE | ||
| 286 | } | ||
| 287 | |||
| 288 | logger->error("BUG; fell through in response handler"); | ||
| 289 | } | ||
| 290 | |||
| 291 | broadcast( 'message', | ||
| 292 | $hints->{Context}, $hints->{Channel}, | ||
| 293 | $resp | ||
| 294 | ) if $resp; | ||
| 295 | |||
| 296 | PLUGIN_EAT_ALL | ||
| 297 | } | ||
| 298 | |||
| 299 | 1; | ||
| 300 | __END__ | ||