Advertisement
Guest User

Untitled

a guest
Aug 5th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.78 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use utf8;
  5. use MediaWiki::API;
  6. use Irssi;
  7. use WikiCommon;
  8.  
  9. our $VERSION = "20091123";
  10. our %IRSSI   = (
  11.     authors     => 'pl:User:Beau',
  12.     contact     => 'beau@adres.pl',
  13.     name        => 'wiki-admins',
  14.     description => 'Utility for wikipedians\' channel',
  15.     license     => 'GPL',
  16.     url         => 'http://tools.wikimedia.pl/~masti',
  17. );
  18.  
  19. my %wiki_channels;
  20.  
  21. $wiki_channels{'#mst'} = {
  22.         'url' => 'https://pl.wikipedia.org/w/api.php',    #
  23. };
  24.  
  25. $wiki_channels{'#wikipedia-pl'} = {
  26.     'url' => 'https://pl.wikipedia.org/w/api.php',    #
  27. };
  28.  
  29. $wiki_channels{'#wiktionary-pl'} = {
  30.     'url' => 'https://pl.wiktionary.org/w/api.php',    #
  31. };
  32.  
  33. $wiki_channels{'#wikisource-pl'} = {
  34.     'url' => 'https://pl.wikisource.org/w/api.php',    #
  35. };
  36.  
  37. $wiki_channels{'#wikibooks-pl'} = {
  38.     'url' => 'https://pl.wikibooks.org/w/api.php',    #
  39. };
  40.  
  41. foreach my $settings ( values %wiki_channels ) {
  42.     $settings->{admins}         = {};
  43.     $settings->{admins_fetched} = 0;
  44. }
  45.  
  46. # -------------------------------------------------------------------
  47.  
  48. sub rcadmins($) {
  49.     my $data = shift;
  50.  
  51.     my $api = MediaWiki::API->new( 'url' => $data->{url}, );
  52.  
  53.     if ( time() - $data->{admins_fetched} > 3600 ) {
  54.         my $response = $api->query(
  55.             'action'  => 'query',
  56.             'list'    => 'allusers',
  57.             'augroup' => 'sysop',
  58.             'aulimit' => 'max',
  59.         );
  60.         my @list = values %{ $response->{query}->{allusers} };
  61.         $data->{admins} = { map { $_->{name} => 1 } @list };
  62.     }
  63.  
  64.     my $response = $api->query(
  65.         'action'  => 'query',
  66.         'list'    => 'recentchanges',
  67.         'rcprop'  => 'user',
  68.         'rclimit' => 100,
  69.         'rcshow'  => '!bot',
  70.     );
  71.  
  72.     my %nicks;
  73.     foreach my $item ( values %{ $response->{query}->{recentchanges} } ) {
  74.         my $nick = $item->{user};
  75.         next unless exists $data->{admins}->{$nick};
  76.         $nicks{$nick}++;
  77.     }
  78.     return sort keys %nicks;
  79. }
  80.  
  81. sub message {
  82.     my ( $server, $args, $sender, $address ) = @_;
  83.     my ( $target, $msg ) = $args =~ /^(\S+) :(.+)$/;
  84.  
  85.     my $settings = $wiki_channels{$target};
  86.     return
  87.       unless defined $settings;
  88.  
  89.     my $channel = $server->channel_find($target);
  90.     return unless $channel;
  91.  
  92.     return unless isActive($server, $channel);
  93.  
  94.     if ( $msg =~ /^!help$/ ) {
  95.         $server->command("notice $sender !admini - wyświetla listę aktywnych administratorów");
  96.         return;
  97.     }
  98.  
  99.     return
  100.       unless $msg =~ /^!admin[yi]$/i;
  101.  
  102.     eval {
  103.         my $list = join( ', ', rcadmins($settings) );
  104.         if ( $list ne '' ) {
  105.             $server->command("msg $target $sender: admini na OZ (100 ostatnich edycji): $list");
  106.         }
  107.         else {
  108.             $server->command("msg $target $sender: Uuups! Chyba nie ma adminów na OZ!");
  109.         }
  110.     };
  111.     if ($@) {
  112.         $server->command("msg $target $sender: wystąpił błąd: $@");
  113.     }
  114. }
  115.  
  116. Irssi::signal_add_last( "event privmsg", "message" );
  117.  
  118. # perltidy -et=8 -l=0 -i=8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement