File Coverage

blib/lib/Atheme.pm
Criterion Covered Total %
statement 18 50 36.0
branch 0 8 0.0
condition 0 13 0.0
subroutine 6 11 54.5
pod n/a
total 24 82 29.2


line stmt bran cond sub pod time code
1             package Atheme;
2             our $VERSION = '0.0001';
3              
4 1     1   46091 use strict;
  1         3  
  1         41  
5 1     1   6 use warnings;
  1         2  
  1         30  
6 1     1   1124 use utf8;
  1         138  
  1         7  
7 1     1   45 use vars qw($ERROR);
  1         2  
  1         47  
8              
9 1     1   6 use Carp;
  1         2  
  1         112  
10              
11             $ERROR = '';
12              
13             require RPC::XML;
14             require RPC::XML::Client;
15 1     1   596 use Atheme::Fault;
  1         3  
  1         1114  
16              
17             =head1 NAME
18              
19             Atheme - Perl interface to Atheme's XML-RPC methods
20              
21             =head1 VERSION
22              
23             version 0.0001
24              
25             =head1 DESCRIPTION
26              
27             This class provides an interface to the XML-RPC methods of the Atheme IRC Services.
28              
29             =head1 METHODS
30              
31             These are all either virtual or helper methods. They are being implemented in
32             service-specific classes.
33              
34             =head2 new
35              
36             Services constructor. Takes a hash as argument:
37             my $svs = new Atheme(url => "http://localhost:8000");
38              
39             url: URL to Atheme's XML-RPC server.
40              
41             lang: Language for result strings (en, ...).
42              
43             validate: If 1 then validation should be done in perl already, if 0
44             then validation is only done in atheme itself. In both cases, atheme
45             validates. If you choose to use the perl validation, you will get more verbose
46             fault messages containing an additional key 'subtype'.
47              
48             =cut
49              
50             sub new {
51 0     0     my ($self, %arg) = @_;
52              
53             # There is no default for url
54 0 0         my $url = delete $arg{url} or croak "Atheme: You need to provide an url";
55              
56             # Some default values
57 0           my $svs = bless {
58             rpc => RPC::XML::Client->new($url),
59             lang => 'en',
60             validate => 1,
61             %arg,
62             }, $self;
63              
64 0           $svs
65             }
66              
67             =head2 return_dispatch
68              
69             Handles results from RPC Calls!
70              
71             Here is the list of fault types and default strings (these are likely to be
72             at least partially overridden by Atheme::*Serv classes.
73              
74             fault_needmoreparams = "Insufficient parameters."
75             fault_badparams = "Invalid parameters."
76             fault_nosuch_source = "No such source."
77             fault_nosuch_target = "No such target."
78             fault_authfail = "Authentication failed."
79             fault_noprivs = "Insufficient privileges."
80             fault_nosuch_key = "No such key."
81             fault_alreadyexists = "Item already exists."
82             fault_toomany = "Too many items."
83             fault_emailfail = "Email verification failed."
84             fault_notverified = "Action not verified."
85             fault_nochange = "No change."
86             fault_already_authed = "You are already authenticated."
87             fault_unimplemented = "Method not implemented."
88             =cut
89              
90             sub return_dispatch {
91 0     0     my ($self, $return, @fault_strings_override) = @_;
92              
93 0 0         if(ref($return) eq "RPC::XML::fault")
    0          
94             {
95 0           my $faultcode = $return->code;
96              
97             # Default string table for the fault dispatch table (should be overridden per method)
98 0           my @fault_strings = (
99             [ "Insufficient parameters.", "fault_needmoreparams"],
100             [ "Invalid parameters.", "fault_badparams"],
101             [ "No such source.", "fault_nosuch_source"],
102             [ "No such target.", "fault_nosuch_target"],
103             [ "Authentication failed.", "fault_authfail" ],
104             [ "Insufficient privileges.", "fault_noprivs" ],
105             [ "No such key.", "fault_nosuch_key" ],
106             [ "Item already exists.", "fault_alreadyexists" ],
107             [ "Too many items.", "fault_toomany" ],
108             [ "Email verification failed.", "fault_emailfail" ],
109             [ "Action not verified.", "fault_notverified" ],
110             [ "No change.", "fault_nochange" ],
111             [ "You are already authenticated.", "fault_already_authed" ],
112             [ "Method not implemented.", "fault_unimplemented" ],
113             );
114              
115 0   0       @fault_strings = map { $fault_strings_override[$_] ||= $fault_strings[$_] } (0..$#fault_strings);
  0            
116              
117 0           return { type => $fault_strings[$faultcode-1][1], string => $fault_strings[$faultcode-1][0], code => $faultcode};
118             }
119             elsif(ref($return) eq "RPC::XML::string")
120             {
121 0           my $value = $return->value;
122 0           return { type => 'success', string => $value };
123             }
124             else
125             {
126 0           return { type => 'fault_http', string => "Connection refused." };
127             }
128             }
129              
130             =head2 call_svs
131              
132             Method call
133              
134             =cut
135              
136             sub call_svs {
137 0     0     my ($self, $args) = @_;
138              
139 0   0       my $result = $self->{rpc}->send_request('atheme.command', $args->{authcookie} || "x", $args->{nick} || "x", $args->{address} || "x", $args->{svs} || "x",$args->{cmd} || "x", @{$args->{params}});
  0   0        
      0        
      0        
      0        
140            
141 0 0         return $self->return_dispatch($result, ($args->{fault_overwrite} ? $args->{fault_overwrite} : {}));
142             }
143              
144             =head2 login
145              
146             A common method used to log into the services in order to execute other
147             methods. Every service inherits this, so you can load just Atheme::MemoServ
148             and log in through that.
149              
150             =cut
151              
152             sub login {
153 0     0     my ($self, $args) = @_;
154              
155             # This method is different from all others and so has to be called separately.
156 0           my $result = $self->{rpc}->send_request('atheme.login', $args->{nick}, $args->{pass}, $args->{address});
157              
158 0           my @overrides;
159              
160 0           $overrides[fault_needmoreparams-1] = ["Insufficient parameters.","fault_needmoreparams"];
161 0           $overrides[fault_nosuch_source-1] = ["The account is not registered.","fault_nosuch_source"];
162 0           $overrides[fault_authfail-1] = ["The password is not valid for this account.","fault_authfail"];
163 0           $overrides[fault_noprivs-1] = ["The account has been frozen.","fault_noprivs"];
164              
165 0           return $self->return_dispatch($result, @overrides);
166             }
167              
168             =head2 logout
169              
170             A common method used to log out and clean up your authcookie. This should be
171             done, but does not have to be done. This method is also inherited and
172             therefore usable in every *Serv.
173              
174             =cut
175              
176             sub logout {
177 0     0     my ($self, $args) = @_;
178              
179             # Set required variables
180              
181             # This method is different from all others and so has to be called separately.
182 0           my $result = $self->{rpc}->send_request('atheme.logout', $args->{authcookie}, $args->{nick});
183              
184 0           my @overrides;
185 0           $overrides[Atheme::Fault::fault_nosuch_source()-1] = ["Unknown user.","fault_nosuch_source"];
186 0           $overrides[Atheme::Fault::fault_authfail()-1] = ["Invalid authcookie for this account.","fault_authfail"];
187              
188 0           return $self->return_dispatch($result, @overrides);
189              
190             }
191              
192             =head1 AUTHORS
193              
194             Pippijn van Steenhoven
195             Stephan Jauernick
196              
197             =cut
198              
199             1;