1 | package Sophie::Client; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | use RPC::XML; |
---|
7 | use base qw(RPC::XML::Client); |
---|
8 | $RPC::XML::FORCE_STRING_ENCODING = 1; |
---|
9 | |
---|
10 | our $VERSION = '0.01'; |
---|
11 | |
---|
12 | sub new { |
---|
13 | my ($class, %options) = @_; |
---|
14 | |
---|
15 | my $self = $class->SUPER::new( |
---|
16 | $options{server} || 'http://sophie2.aero.jussieu.fr/rpc' |
---|
17 | ); |
---|
18 | if ($options{proxy}) { |
---|
19 | $self->useragent->proxy([ 'http' ], $options{proxy}); |
---|
20 | } else { |
---|
21 | $self->useragent->env_proxy; |
---|
22 | } |
---|
23 | |
---|
24 | $self->{options} = { %options }; |
---|
25 | |
---|
26 | |
---|
27 | if ($options{login}) { |
---|
28 | login($self) or die "Can't login"; |
---|
29 | } |
---|
30 | |
---|
31 | my $realclass = $class . ($options{type} ? ('::' . $options{type}) : ''); |
---|
32 | no strict qw(refs); |
---|
33 | eval "require $realclass;"; |
---|
34 | return if($@); |
---|
35 | bless($self, $realclass); |
---|
36 | } |
---|
37 | |
---|
38 | sub login { |
---|
39 | my ($self) = @_; |
---|
40 | my %options = %{ $self->{options} }; |
---|
41 | if ($options{login}) { |
---|
42 | my $res = $self->send_request('login', |
---|
43 | $options{login}, |
---|
44 | $options{password}); |
---|
45 | if (ref $res) { |
---|
46 | $self->request->header('cookie', $$res); |
---|
47 | return 1; |
---|
48 | } else { |
---|
49 | return; |
---|
50 | } |
---|
51 | } else { |
---|
52 | return; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | sub get_var { |
---|
57 | my ($self, $varname) = @_; |
---|
58 | my $resp = $self->send_request('user.fetchdata', $varname); |
---|
59 | if (ref $resp) { |
---|
60 | if ($resp->value) { |
---|
61 | return $resp->value; |
---|
62 | } |
---|
63 | } else { |
---|
64 | return {}; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | sub set_var { |
---|
69 | my ($self, $varname, $data) = @_; |
---|
70 | |
---|
71 | my $resp = $self->send_request('user.update_data', $varname, $data); |
---|
72 | if (ref $resp) { |
---|
73 | return 1; |
---|
74 | } else { |
---|
75 | return; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | sub handle_message { |
---|
80 | my ($self, $heap, $context, $message) = @_; |
---|
81 | |
---|
82 | $self->login; |
---|
83 | if ($message =~ /^\s*set\s+(\w+)\s+(\S+)/) { |
---|
84 | warn "$1, $2"; |
---|
85 | $self->user_config($heap, $1, $2); |
---|
86 | } else { |
---|
87 | eval { |
---|
88 | $self->submit_query($heap, $context, $message); |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | sub submit_query { |
---|
94 | my ($self, $heap, $context, $message) = @_; |
---|
95 | |
---|
96 | my $resp = $self->send_request('chat.message', $context, $message); |
---|
97 | if (ref($resp)) { |
---|
98 | $self->show_reply($heap, $resp->value); |
---|
99 | } else { |
---|
100 | return; |
---|
101 | } |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | 1; |
---|
106 | |
---|
107 | __END__ |
---|
108 | # Below is stub documentation for your module. You'd better edit it! |
---|
109 | |
---|
110 | =head1 NAME |
---|
111 | |
---|
112 | Sophie::Client - Perl extension for blah blah blah |
---|
113 | |
---|
114 | =head1 SYNOPSIS |
---|
115 | |
---|
116 | use Sophie::Client; |
---|
117 | blah blah blah |
---|
118 | |
---|
119 | =head1 DESCRIPTION |
---|
120 | |
---|
121 | Stub documentation for Sophie::Client, created by h2xs. It looks like the |
---|
122 | author of the extension was negligent enough to leave the stub |
---|
123 | unedited. |
---|
124 | |
---|
125 | Blah blah blah. |
---|
126 | |
---|
127 | |
---|
128 | =head1 SEE ALSO |
---|
129 | |
---|
130 | Mention other useful documentation such as the documentation of |
---|
131 | related modules or operating system documentation (such as man pages |
---|
132 | in UNIX), or any relevant external documentation such as RFCs or |
---|
133 | standards. |
---|
134 | |
---|
135 | If you have a mailing list set up for your module, mention it here. |
---|
136 | |
---|
137 | If you have a web site set up for your module, mention it here. |
---|
138 | |
---|
139 | =head1 AUTHOR |
---|
140 | |
---|
141 | Olivier Thauvin, E<lt>olivier@localdomainE<gt> |
---|
142 | |
---|
143 | =head1 COPYRIGHT AND LICENSE |
---|
144 | |
---|
145 | Copyright (C) 2010 by Olivier Thauvin |
---|
146 | |
---|
147 | This library is free software; you can redistribute it and/or modify |
---|
148 | it under the same terms as Perl itself, either Perl version 5.12.2 or, |
---|
149 | at your option, any later version of Perl 5 you may have available. |
---|
150 | |
---|
151 | |
---|
152 | =cut |
---|