| 1 | package DJabberd::Plugin::VCard; |
|---|
| 2 | use strict; |
|---|
| 3 | use base 'DJabberd::Plugin'; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | our $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 | |
|---|
| 18 | sub iq_class { |
|---|
| 19 | "DJabberd::Plugin::VCard::IQ"; |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | sub 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 | |
|---|
| 55 | sub 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 | |
|---|
| 78 | sub 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 | |
|---|
| 95 | sub 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 | |
|---|
| 107 | 1; |
|---|