not parse [[]]s with rubbish in them
[gpy.git] / WikiLinker.pm
blob53afb9e5c2330d3577dd1d0b478b030e4f00b7f5
1 #!/usr/local/bin/perl
3 package Bot::BasicBot::Pluggable::Module::WikiLinker;
4 use Bot::BasicBot::Pluggable::Module;
5 use base qw(Bot::BasicBot::Pluggable::Module);
7 use strict;
8 use warnings;
9 use MediaWiki::API;
10 use Data::Dumper;
11 use WikiLinkParser;
13 my %channels_urls = (
14 '#wikinews-spam' => 'https://en.wikinews.org/w/api.php',
15 '#wikipedia-ru-spam' => 'https://ru.wikipedia.org/w/api.php',
17 my %mw;
19 # Secondary variables.
20 for my $channel_name (keys %channels_urls) {
21 $mw{$channel_name} = MediaWiki::API->new();
22 $mw{$channel_name}->{config}->{api_url} = $channels_urls{$channel_name};
25 sub told{
26 shift->process_message(@_);
29 sub emoted {
30 shift->process_message(@_);
33 sub process_message{
34 my ($self, $msg, $pri) = @_;
35 my $body = $msg->{body};
36 my $who = $msg->{who};
37 my $channel = $msg->{'channel'};
38 if (!grep { $_ eq $channel } keys %channels_urls){
39 return;
41 if ($body =~ m{\[\[(.*?)\]\]} or $body =~ m{\{\{(.*?)\}\}}g){
42 return join " ", @{WikiLinkParser->get_urls_by_text($body, $mw{$channel})};
44 return;
47 sub help{
48 return "parser for [[*]] in messages";
53 #_ _END_ _