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

Revision 867, 2.6 kB (checked in by mart, 7 months ago)

Don't rebless VCard IQs onto the special VCard IQ class, since it causes issues as described here:
http://lists.danga.com/pipermail/djabberd/2009-April/000734.html

Patch from Piers Harding.

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