1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
package Atheme;
use strict;
use warnings;
use utf8;
use vars qw($ERROR);
use Carp;
our $VERSION = "0.01";
$ERROR = '';
require RPC::XML;
require RPC::XML::Client;
=head1 NAME
Atheme - Perl interface to Atheme's XML-RPC methods
=head1 DESCRIPTION
This class provides an interface to the XML-RPC methods of the Atheme IRC Services.
=head1 METHODS
These are all either virtual or helper methods. They are being implemented in
service-specific classes.
=head2 new
Services constructor. Takes a hash as argument:
my $svs = new Atheme(url => "http://localhost:8000");
url<string>: URL to Atheme's XML-RPC server.
lang<string>: Language for result strings (en, ...).
validate<boolean>: If 1 then validation should be done in perl already, if 0
then validation is only done in atheme itself. In both cases, atheme
validates. If you choose to use the perl validation, you will get more verbose
fault messages containing an additional key 'subtype'.
=cut
sub new {
my ($self, %arg) = @_;
# There is no default for url
my $url = delete $arg{url}
or croak "Atheme: You need to provide an url";
# Some default values
my $svs = bless {
rpc => RPC::XML::Client->new($url),
lang => 'en',
validate => 1,
%arg,
}, $self;
require FormValidator::Simple
if $svs->{validate};
# Get fault and success handling dispatch tables
$svs->init_dispatcher;
$svs
}
=head2 init_dispatcher
Initialises the dispatch tables handling faults or success
Here is the list of fault types and default strings (these are likely to be
at least partially overridden by Atheme::*Serv classes.
fault_needmoreparams = "Insufficient parameters."
fault_badparams = "Invalid parameters."
fault_nosuch_source = "No such source."
fault_nosuch_target = "No such target."
fault_authfail = "Authentication failed."
fault_noprivs = "Insufficient privileges."
fault_nosuch_key = "No such key."
fault_alreadyexists = "Item already exists."
fault_toomany = "Too many items."
fault_emailfail = "Email verification failed."
fault_notverified = "Action not verified."
fault_nochange = "No change."
fault_already_authed = "You are already authenticated."
fault_unimplemented = "Method not implemented."
=cut
sub init_dispatcher {
my ($self) = @_;
# Default string table for the fault dispatch table (should be overridden per method)
$self->{fault_strings} = {
fault_needmoreparams => "Insufficient parameters.",
fault_badparams => "Invalid parameters.",
fault_nosuch_source => "No such source.",
fault_nosuch_target => "No such target.",
fault_authfail => "Authentication failed.",
fault_noprivs => "Insufficient privileges.",
fault_nosuch_key => "No such key.",
fault_alreadyexists => "Item already exists.",
fault_toomany => "Too many items.",
fault_emailfail => "Email verification failed.",
fault_notverified => "Action not verified.",
fault_nochange => "No change.",
fault_already_authed => "You are already authenticated.",
fault_unimplemented => "Method not implemented."
};
# Fault dispatch table
$self->{fault_dispatch} = {
1 => sub { $self->{result} = { type => 'fault_needmoreparams', string => $self->{fault_strings}->{fault_needmoreparams} }; },
2 => sub { $self->{result} = { type => 'fault_badparams', string => $self->{fault_strings}->{fault_badparams} }; },
3 => sub { $self->{result} = { type => 'fault_nosuch_source', string => $self->{fault_strings}->{fault_nosuch_source} }; },
4 => sub { $self->{result} = { type => 'fault_nosuch_target', string => $self->{fault_strings}->{fault_nosuch_target} }; },
5 => sub { $self->{result} = { type => 'fault_authfail', string => $self->{fault_strings}->{fault_authfail} }; },
6 => sub { $self->{result} = { type => 'fault_noprivs', string => $self->{fault_strings}->{fault_noprivs} }; },
7 => sub { $self->{result} = { type => 'fault_nosuch_key', string => $self->{fault_strings}->{fault_nosuch_key} }; },
8 => sub { $self->{result} = { type => 'fault_alreadyexists', string => $self->{fault_strings}->{fault_alreadyexists} }; },
9 => sub { $self->{result} = { type => 'fault_toomany', string => $self->{fault_strings}->{fault_toomany} }; },
10 => sub { $self->{result} = { type => 'fault_emailfail', string => $self->{fault_strings}->{fault_emailfail} }; },
11 => sub { $self->{result} = { type => 'fault_notverified', string => $self->{fault_strings}->{fault_notverified} }; },
12 => sub { $self->{result} = { type => 'fault_nochange', string => $self->{fault_strings}->{fault_nochange} }; },
13 => sub { $self->{result} = { type => 'fault_already_authed', string => $self->{fault_strings}->{fault_already_authed} }; },
14 => sub { $self->{result} = { type => 'fault_unimplemented', string => $self->{fault_strings}->{fault_unimplemented} }; },
};
# Result dispatch table
$self->{dispatch} = {
'RPC::XML::fault' => sub { $self->{fault_dispatch}->{$self->{result}->code}->(); },
'RPC::XML::string' => sub { $self->{result} = { type => 'success', string => $self->{result}->value }; },
'other' => sub { $self->{result} = { type => 'fault_http', string => "Connection refused." }; }
};
()
}
=head2 call_rpc
Method call
=cut
sub call_rpc {
my ($self) = @_;
$self->{result} = $self->{rpc}->send_request('atheme.command',
$self->{authcookie} || "x",
$self->{nick} || "x",
$self->{address} || "x",
$self->{svs} || "x",
$self->{cmd} || "x",
$self->{params});
eval { print "--------------------------------------------------------\nOriginal | " . $self->{result}->string . "\n"; };
$self->{dispatch}{ref $self->{result} || 'other'}->()
}
=head2 call_svs
Call service command
Required variables:
$self->{authcookie}
$self->{nick}
$self->{address}
$self->{svs}
$self->{cmd}
$self->{params}
=cut
sub call_svs {
my ($self, $args) = @_;
# Set required variables
$self->{authcookie} = $args->{authcookie};
$self->{nick} = $args->{nick};
$self->{address} = $args->{address};
$self->{params} = $args->{params};
# Make the call
$self->call_rpc;
# Return the result, or the fault
$self->{result}
}
=head2 login
A common method used to log into the services in order to execute other
methods. Every service inherits this, so you can load just Atheme::MemoServ
and log in through that.
=cut
sub login {
my ($self, $args) = @_;
# Set required variables
$self->{nick} = $args->{nick};
$self->{pass} = $args->{pass};
$self->{address} = $args->{address};
# This method is different from all others and so has to be called separately.
$self->{result} = $self->{rpc}->send_request('atheme.login',
$self->{nick},
$self->{pass},
$self->{address});
# Dispatch table changes for login
$self->{fault_strings}->{fault_needmoreparams} = "Insufficient parameters.";
$self->{fault_strings}->{fault_nosuch_source} = "The account is not registered.",
$self->{fault_strings}->{fault_authfail} = "The password is not valid for this account.";
$self->{fault_strings}->{fault_noprivs} = "The account has been frozen.";
$self->{dispatch}{ref $self->{result} || 'other'}->()
}
=head2 logout
A common method used to log out and clean up your authcookie. This should be
done, but does not have to be done. This method is also inherited and
therefore usable in every *Serv.
=cut
sub logout {
my ($self, $args) = @_;
# Set required variables
$self->{authcookie} = $args->{authcookie};
$self->{nick} = $args->{nick};
# This method is different from all others and so has to be called separately.
$self->{result} = $self->{rpc}->send_request('atheme.logout',
$self->{authcookie},
$self->{nick});
# Dispatch table changes for login
$self->{fault_strings}->{fault_nosuch_source} = "Unknown user.";
$self->{fault_strings}->{fault_authfail} = "Invalid authcookie for this account.";
$self->{dispatch}{ref $self->{result} || 'other'}->()
}
=head1 AUTHORS
Author: Pippijn van Steenhoven <pip88nl@gmail.com>
This package is part of the Atheme IRC Services.
=cut
1
|