This bot is not ready to be used. It is in development. Version 0.0
package HBCPlugins::CommandParser; use strict; use Digest::MD5 qw(md5_hex); use MediaWiki::API; use Data::Dumper; our $self; sub new { shift; $self = shift; bless($self); $self->{params}{internal} = {}; $self->{params}{methods} = { authorize => \&handleAuthorize, test => [\&setFlag, 'test'], go => [\&setFlag, 'go' ] }; $self->{params}{panel} = {}; $self->{params}{flags} = {}; &{$self->{shared}{add_job}}(\&init,0); &{$self->{shared}{add_job}}(\&login,0); &{$self->{shared}{add_job}}(\&parse_panel,5); return $self; } sub methodHandler { # called from the outside my $self = shift; my $method = shift; my $rc_function = shift; return undef unless (ref($rc_function) eq 'CODE' || ref($rc_function) eq 'ARRAY'); if ($self->{params}{methods}{$method}) { print "'$method' is already a reserved method name\n"; return undef; } print "Method registered: $method\n"; $self->{params}{methods}{lc($method)} = $rc_function; } sub init { $self->{params}{mw} = MediaWiki::API->new(); } sub login { print "Logging in...\n"; $self->{params}{mw}->{config}->{api_url} = 'http://en.wikipedia.org/w/api.php'; $self->{params}{mw}->login({lgname => $self->{params}{username}, lgpassword => $self->{params}{password}}) || die $self->{params}{mw}->{error}->{code}.': '.$self->{params}{mw}->{error}->{details}; print "Done...\n"; } sub parse_panel { print "Parsing panel...\n"; $self->{params}{pages}{controlPanel} = $self->{params}{mw}->get_page({title => $self->{params}{controlPanel}}); refuse_to_operate($self->{params}{pages}{controlPanel}->{'user'}) unless ($self->{params}{pages}{controlPanel}->{'user'} eq $self->{params}{username}); if ($self->{params}{signature_password}) { my $signature_target = $self->{params}{pages}{controlPanel}->{'*'}; $signature_target =~ s|\n$||; $signature_target = $self->{params}{signature_password}.$signature_target; my $signature_needed = lc(md5_hex($signature_target)); if ($self->{params}{pages}{controlPanel}->{'comment'} =~ m|([0-9a-f]{32})|i) { my $signature_given = lc($1); print "$signature_needed vs $signature_given\n"; print Dumper($self->{params}{pages}{controlPanel}); } else { return undef; } } my @lines = split("\n", $self->{params}{pages}{controlPanel}->{'*'}); LINE: foreach my $line (@lines) { next LINE if ($line =~ m|\{\{/done\}\}|); next LINE unless ($line =~ m|^;([^:]+):\s*(.*)|); my($method,$parameters) = ($1, $2); next LINE unless ($self->{params}{flags}{go} || lc($method) eq 'go'); my $ra_parameters = parse_parameters($parameters); $self->{params}{panel}{$method} ||= []; my $ref = $self->{params}{methods}{lc($method)}; print "Running method: $method...\n"; if (ref($ref) eq 'CODE') { &{$ref}({'mw' => $self->{params}{mw}}, $ra_parameters); } elsif (ref($ref) eq 'ARRAY') { my @cmd = @{$ref}; my $cmd = shift(@cmd); &{$cmd}({'mw' => $self->{params}{mw}},$ra_parameters,@cmd); } push(@{$self->{params}{panel}{$method}}, $ra_parameters); } if ($self->{params}{flags}{go}) { my $new_content = $self->{params}{pages}{controlPanel}->{'*'}; $new_content =~ s|(\n)?;Go|$1 ;Go|i || die; print "Marking page done.\n"; $self->{params}{mw}->edit( { action => 'edit', title => $self->{params}{controlPanel}, basetimestamp => $self->{params}{pages}{controlPanel}->{'timestamp'}, text => $new_content, summary => 'Plugins have been triggered, disabling "Go" command.', }) || die $self->{params}{mw}->{error}->{code} . ': ' . $self->{params}{mw}->{error}->{details}; print "Done...\n"; } else { print "No go...\n"; } print "\n"; } sub parse_parameters { my $string = shift; my @params; while ($string =~ m'\{(.*?)\}'g) { my @options = split('\|',$1); push (@params,\@options) } return \@params; } sub refuse_to_operate { my $user = shift; die("I refuse to act because the last person who edited was: $user\n") } ##### Methods ##### sub setFlag { my $rh_extras = shift; my $ra_params = shift; my $flag = shift; my $state = ${${$ra_params}[0]}[0]; if ($state && $state !~ m/no|off|false|0/i) { print "Set '$flag' on\n"; $self->{params}{flags}{$flag} = 1; } else { print "Set $flag off\n"; $self->{params}{flags}{$flag} = undef; } } sub handleAuthorize { my $rh_extras = shift; my $ra_params = shift; foreach my $ra_authorization (@{$ra_params}) { my $auth = lc(shift(@{$ra_authorization})); $self->{params}{internal}{authorizations}{$auth} = {'authorized' => 1}; foreach my $auth_setting (@{$ra_authorization}) { $auth_setting =~ m|(.*?)\s(.*)|; my ($key, $value) = ($1, $2); $self->{params}{internal}{authorizations}{$auth}{$key} = $value; } } } ################### 1;