1package DJabberd::RosterStorage;
2# abstract base class
3use strict;
4use warnings;
5use base 'DJabberd::Plugin';
6use DJabberd::Roster;
7use DJabberd::RosterItem;
8
9### store the object in a closure, so we can retrieve it later
10### this allows us to manipulate the roster storage from other
11### places, to for example pre-link users.
12{   my $singleton;
13    sub singleton { $singleton };
14
15    sub new {
16        my $self = shift;
17        $singleton = $self->SUPER::new( @_ );
18    }
19}
20
21# don't override, or at least call SUPER to this if you do.
22sub register {
23    my ($self, $vhost) = @_;
24    $vhost->register_hook("RosterGet", sub {
25        my ($vh, $cb, $jid) = @_;
26        # cb can '->set_roster(Roster)' or decline
27        $self->get_roster($cb, $jid);
28    });
29    $vhost->register_hook("RosterAddUpdateItem", sub {
30        my ($vh, $cb, $jid, $ritem) = @_;
31        # $cb takes ->done($ritem_final) or error
32        $self->addupdate_roster_item($cb, $jid, $ritem);
33    });
34    $vhost->register_hook("RosterRemoveItem", sub {
35        my ($vh, $cb, $jid, $ritem) = @_;
36        # $cb can ->done
37        $self->delete_roster_item($cb, $jid, $ritem);
38    });
39    $vhost->register_hook("RosterLoadItem", sub {
40        my ($vh, $cb, $user_jid, $contact_jid) = @_;
41        # cb can 'set($data|undef)' and 'error($reason)'
42        $self->load_roster_item($user_jid, $contact_jid, $cb);
43    });
44
45    $vhost->register_hook("RosterSetItem", sub {
46        my ($vh, $cb, $user_jid, $ritem) = @_;
47        # cb can ->error($reason) and ->done()
48        $self->set_roster_item($cb, $user_jid, $ritem);
49    });
50    $vhost->register_hook("RosterWipe", sub {
51        my ($vh, $cb, $user_jid) = @_;
52        # cb can ->error($reason) and ->done()
53        $self->wipe_roster($cb, $user_jid);
54    });
55
56}
57
58# override this.
59sub get_roster {
60    my ($self, $cb, $jid) = @_;
61    $cb->declined;
62}
63
64# override this.
65sub addupdate_roster_item {
66    my ($self, $cb, $jid, $ritem) = @_;
67    $cb->declined;
68}
69
70# override this.  unlike addupdate, you should respect the subscription level
71sub set_roster_item {
72    my ($self, $cb, $jid, $ritem) = @_;
73    warn "SET ROSTER ITEM FAILED\n";
74    $cb->error;
75}
76
77# override this.
78sub delete_roster_item {
79    my ($self, $cb, $jid, $ritem) = @_;
80    $cb->declined;
81}
82
83# override this.
84sub load_roster_item {
85    my ($self, $jid, $target_jid, $cb) = @_;
86    $cb->error("load_roster_item not implemented");
87}
88
89# override this.
90sub wipe_roster {
91    my ($self, $cb, $jid) = @_;
92    $cb->error("wipe_roster not implemented");
93}
94
951;
96