root/trunk/DJabberd-VCard/lib/DJabberd/Plugin/VCard.pm @ 710

Revision 710, 2.7 kB (checked in by bradfitz, 3 years ago)

move vcard stuff (which is currently only half-baked, anyway, doing
everything synchronously), into its own module. (especially since it
was called DJabberd::Plugin)

Line 
1package DJabberd::Plugin::VCard;
2use strict;
3use base 'DJabberd::Plugin';
4use warnings;
5use DJabberd::Plugin::VCard::IQ;
6
7our $logger = DJabberd::Log->get_logger();
8
9# this spec implements the jep--0054 vcard spec using an SQLite store
10# later it should be refactored to have pluggable stores
11# but I really just want to get it out there -- sky
12# This should be split into the following
13#  DJabberd::Plugin::VCard::IQ isa DJabberd::IQ
14#  DJabberd::Plugin::VCard::SQLite isa Djabberd::Plugin::VCard
15#  DJabberd::Plugin::VCard manages and loads everythign
16
17
18
19sub iq_class {
20    "DJabberd::Plugin::VCard::IQ";
21}
22
23
24sub register {
25    my ($self, $vhost) = @_;
26    my $vcard_cb = sub {
27        my ($vh, $cb, $iq) = @_;
28        unless ($iq->isa("DJabberd::IQ")) {
29            $cb->decline;
30            return;
31        }
32        if(my $to = $iq->to_jid) {
33            unless ($vh->handles_jid($to)) {
34                $cb->decline;
35                return;
36            }
37        }
38        if ($iq->signature eq 'get-{vcard-temp}vCard') {
39            bless $iq, $self->iq_class;
40            $self->get_vcard($vh, $iq);
41            $cb->stop_chain;
42            return;
43        } elsif ($iq->signature eq 'set-{vcard-temp}vCard') {
44            bless $iq, $self->iq_class;
45            $self->set_vcard($vh, $iq);
46            $cb->stop_chain;
47            return;
48        }
49        $cb->decline;
50    };
51    $vhost->register_hook("switch_incoming_client",$vcard_cb);
52    $vhost->register_hook("switch_incoming_server",$vcard_cb);
53    $vhost->add_feature("vcard-temp");
54
55}
56
57
58sub get_vcard {
59    my ($self, $vhost, $iq) = @_;
60
61
62    my $user;
63    if ($iq->to) {
64        $user = $iq->to_jid->as_bare_string;
65    } else {
66        # user is requesting their own vCard
67        $user = $iq->connection->bound_jid->as_bare_string;
68    }
69    $logger->info("Getting vcard for user '$user'");
70    my $reply;
71    if(my $vcard = $self->load_vcard($user)) {
72        $reply = $self->make_response($iq, $vcard);
73    } else {
74        $reply = $self->make_response($iq, "<vCard xmlns='vcard-temp'/>");
75    }
76    $reply->deliver($vhost);
77
78}
79
80
81sub make_response {
82    my ($self, $iq, $vcard) = @_;
83
84
85
86    my $response = $iq->clone;
87    my $from = $iq->from;
88    my $to   = $iq->to;
89    $response->set_raw($vcard);
90
91    $response->attrs->{"{}type"} = 'result';
92    $response->set_to($from);
93    $to ? $response->set_from($to) : delete($response->attrs->{"{}from"});
94
95    return $response;
96}
97
98sub set_vcard {
99    my ($self, $vhost, $iq) = @_;
100
101    my $vcard = $iq->first_element();
102    my $user  = $iq->connection->bound_jid->as_bare_string;
103    $self->store_vcard($user, $vcard);
104
105    $logger->info("Set vcard for user '$user'");
106    $self->make_response($iq)->deliver($vhost);
107}
108
109
1101;
Note: See TracBrowser for help on using the browser.