source: client/trunk/lib/Sophie/Client.pm @ 288

Last change on this file since 288 was 288, checked in by nanardon, 13 years ago
  • add tools to analyse local rpms
File size: 3.2 KB
Line 
1package Sophie::Client;
2
3use 5.010000;
4use strict;
5use warnings;
6use RPC::XML;
7use base qw(RPC::XML::Client);
8$RPC::XML::FORCE_STRING_ENCODING = 1;
9
10our $VERSION = '0.01';
11
12sub 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
38sub 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
56sub 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
68sub 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
79sub 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
93sub 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
1051;
106
107__END__
108# Below is stub documentation for your module. You'd better edit it!
109
110=head1 NAME
111
112Sophie::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
121Stub documentation for Sophie::Client, created by h2xs. It looks like the
122author of the extension was negligent enough to leave the stub
123unedited.
124
125Blah blah blah.
126
127
128=head1 SEE ALSO
129
130Mention other useful documentation such as the documentation of
131related modules or operating system documentation (such as man pages
132in UNIX), or any relevant external documentation such as RFCs or
133standards.
134
135If you have a mailing list set up for your module, mention it here.
136
137If you have a web site set up for your module, mention it here.
138
139=head1 AUTHOR
140
141Olivier Thauvin, E<lt>olivier@localdomainE<gt>
142
143=head1 COPYRIGHT AND LICENSE
144
145Copyright (C) 2010 by Olivier Thauvin
146
147This library is free software; you can redistribute it and/or modify
148it under the same terms as Perl itself, either Perl version 5.12.2 or,
149at your option, any later version of Perl 5 you may have available.
150
151
152=cut
Note: See TracBrowser for help on using the repository browser.