| 1 | package Bot::Cobalt::Plugin::Extras::CPAN; | ||
| 2 | $Bot::Cobalt::Plugin::Extras::CPAN::VERSION = '0.021001'; | ||
| 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 | 1 | ▶ | 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 unless $cmd eq "belongs"; | ||
| 96 | my $url = "/release/$dist"; | ||
| 97 | |||
| 98 | my $hints = +{ | ||
| 99 | Context => $msg->context, | ||
| 100 | Channel => $msg->channel, | ||
| 101 | Nick => $msg->src_nick, | ||
| 102 | Dist => $dist, | ||
| 103 | Link => "http://www.metacpan.org${url}", | ||
| 104 | }; | ||
| 105 | |||
| 106 | CMD: { | ||
| 107 | if ($cmd eq 'latest' || $cmd eq 'release') { | ||
| 108 | $hints->{Type} = 'latest'; | ||
| 109 | last CMD | ||
| 110 | } | ||
| 111 | |||
| 112 | if ($cmd eq 'dist') { | ||
| 113 | $hints->{Type} = 'dist'; | ||
| 114 | last CMD | ||
| 115 | } | ||
| 116 | |||
| 117 | if ($cmd eq 'test' || $cmd eq 'tests') { | ||
| 118 | $hints->{Type} = 'tests'; | ||
| 119 | last CMD | ||
| 120 | } | ||
| 121 | |||
| 122 | if ($cmd eq 'info' || $cmd eq 'abstract') { | ||
| 123 | $hints->{Type} = 'abstract'; | ||
| 124 | last CMD | ||
| 125 | } | ||
| 126 | |||
| 127 | if ($cmd eq 'license') { | ||
| 128 | $hints->{Type} = 'license'; | ||
| 129 | last CMD | ||
| 130 | } | ||
| 131 | |||
| 132 | if ($cmd eq 'belongs') { | ||
| 133 | $hints->{Type} = 'belongs'; | ||
| 134 | $url = "/module/$dist"; | ||
| 135 | last CMD | ||
| 136 | } | ||
| 137 | |||
| 138 | if ($cmd eq 'changes' || $cmd eq 'changelog') { | ||
| 139 | $hints->{Type} = 'changes'; | ||
| 140 | $url = "/module/$dist"; | ||
| 141 | last CMD | ||
| 142 | } | ||
| 143 | |||
| 144 | broadcast( 'message', $msg->context, $msg->channel, | ||
| 145 | "Unknown query; $HelpText" | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | |||
| 149 | $self->_request($url, $hints) if defined $hints->{Type}; | ||
| 150 | |||
| 151 | PLUGIN_EAT_ALL | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _request { | ||
| 155 | my ($self, $url, $hints) = @_; | ||
| 156 | |||
| 157 | my $base_url = 'http://api.metacpan.org'; | ||
| 158 | my $this_url = $base_url . $url; | ||
| 159 | |||
| 160 | logger->debug("metacpan request: $this_url"); | ||
| 161 | |||
| 162 | my $request = HTTP::Request->new(GET => $this_url); | ||
| 163 | |||
| 164 | broadcast( 'www_request', | ||
| 165 | $request, | ||
| 166 | 'mcpan_plug_resp_recv', | ||
| 167 | $hints | ||
| 168 | ); | ||
| 169 | } | ||
| 170 | |||
| 171 | sub Bot_mcpan_plug_resp_recv { | ||
| 172 | my ($self, $core) = splice @_, 0, 2; | ||
| 173 | my $response = ${ $_[1] }; | ||
| 174 | my $hints = ${ $_[2] }; | ||
| 175 | |||
| 176 | my $dist = $hints->{Dist}; | ||
| 177 | my $type = $hints->{Type}; | ||
| 178 | my $link = $hints->{Link}; | ||
| 179 | |||
| 180 | unless ($response->is_success) { | ||
| 181 | my $status = $response->code; | ||
| 182 | if ($status == 404) { | ||
| 183 | broadcast( 'message', | ||
| 184 | $hints->{Context}, $hints->{Channel}, | ||
| 185 | "No such distribution: $dist" | ||
| 186 | ); | ||
| 187 | } else { | ||
| 188 | broadcast( 'message', | ||
| 189 | $hints->{Context}, $hints->{Channel}, | ||
| 190 | "Could not get release info for $dist ($status)" | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | return PLUGIN_EAT_ALL | ||
| 194 | } | ||
| 195 | |||
| 196 | my $json = $response->content; | ||
| 197 | unless ($json) { | ||
| 198 | broadcast('message', | ||
| 199 | $hints->{Context}, $hints->{Channel}, | ||
| 200 | "Unknown failure -- no data received for $dist", | ||
| 201 | ); | ||
| 202 | return PLUGIN_EAT_ALL | ||
| 203 | } | ||
| 204 | |||
| 205 | my $d_hash = | ||
| 206 | try { $Serializer->thaw($json) } | ||
| 207 | catch { | ||
| 208 | broadcast( 'message', | ||
| 209 | $hints->{Context}, $hints->{Channel}, | ||
| 210 | "thaw failure; err: $_", | ||
| 211 | ); | ||
| 212 | undef | ||
| 213 | } or return PLUGIN_EAT_ALL; | ||
| 214 | |||
| 215 | unless ($d_hash && ref $d_hash eq 'HASH') { | ||
| 216 | broadcast( 'message', | ||
| 217 | $hints->{Context}, $hints->{Channel}, | ||
| 218 | "thaw failure for $dist; expected a HASH but got '$d_hash'" | ||
| 219 | ); | ||
| 220 | return PLUGIN_EAT_ALL | ||
| 221 | } | ||
| 222 | |||
| 223 | my $resp; | ||
| 224 | my $prefix = color bold => 'mCPAN'; | ||
| 225 | |||
| 226 | TYPE: { | ||
| 227 | |||
| 228 | if ($type eq 'abstract') { | ||
| 229 | my $abs = $d_hash->{abstract} || 'No abstract available.'; | ||
| 230 | my $vers = $d_hash->{version}; | ||
| 231 | $resp = "$prefix: ($dist $vers) $abs ; $link"; | ||
| 232 | last TYPE | ||
| 233 | } | ||
| 234 | |||
| 235 | if ($type eq 'dist') { | ||
| 236 | my $dl = $d_hash->{download_url} || 'No download link available.'; | ||
| 237 | $resp = "$prefix: ($dist) $dl"; | ||
| 238 | last TYPE | ||
| 239 | } | ||
| 240 | |||
| 241 | if ($type eq 'latest') { | ||
| 242 | my $vers = $d_hash->{version}; | ||
| 243 | my $arc = $d_hash->{archive}; | ||
| 244 | $resp = "$prefix: ($dist) Latest is $vers ($arc) ; $link"; | ||
| 245 | last TYPE | ||
| 246 | } | ||
| 247 | |||
| 248 | if ($type eq 'license') { | ||
| 249 | my $name = $d_hash->{name}; | ||
| 250 | my $lic = join ' ', @{ $d_hash->{license}||['undef'] }; | ||
| 251 | $resp = "$prefix: License terms for $name: $lic"; | ||
| 252 | last TYPE | ||
| 253 | } | ||
| 254 | |||
| 255 | if ($type eq 'tests') { | ||
| 256 | my %tests = %{ | ||
| 257 | keys %{$d_hash->{tests}||{}} ? | ||
| 258 | $d_hash->{tests} | ||
| 259 | : { pass => 0, fail => 0, na => 0, unknown => 0 } | ||
| 260 | }; | ||
| 261 | |||
| 262 | my $vers = $d_hash->{version}; | ||
| 263 | |||
| 264 | $resp = sprintf("%s: (%s %s) %d PASS, %d FAIL, %d NA, %d UNKNOWN", | ||
| 265 | $prefix, $dist, $vers, | ||
| 266 | $tests{pass}, $tests{fail}, $tests{na}, $tests{unknown} | ||
| 267 | ); | ||
| 268 | |||
| 269 | last TYPE | ||
| 270 | } | ||
| 271 | |||
| 272 | if ($type eq 'belongs') { | ||
| 273 | my $release = $d_hash->{release}; | ||
| 274 | $resp = "$prefix: $dist belongs to release $release"; | ||
| 275 | last TYPE | ||
| 276 | } | ||
| 277 | |||
| 278 | if ($type eq 'changes') { | ||
| 279 | my $release = $d_hash->{release}; | ||
| 280 | my $actuald = substr $release, 0, rindex $release, '-'; | ||
| 281 | my $link = "https://www.metacpan.org/changes/distribution/$actuald"; | ||
| 282 | $resp = "$prefix: Changes for $release: $link"; | ||
| 283 | last TYPE | ||
| 284 | } | ||
| 285 | |||
| 286 | logger->error("BUG; fell through in response handler"); | ||
| 287 | } | ||
| 288 | |||
| 289 | broadcast( 'message', | ||
| 290 | $hints->{Context}, $hints->{Channel}, | ||
| 291 | $resp | ||
| 292 | ) if $resp; | ||
| 293 | |||
| 294 | PLUGIN_EAT_ALL | ||
| 295 | } | ||
| 296 | |||
| 297 | 1; | ||
| 298 | __END__ | ||