1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Module::Jabber::ConnectionManager |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | A class to keep track of all the active connection in the barnowl |
---|
11 | jabber module |
---|
12 | |
---|
13 | =cut |
---|
14 | |
---|
15 | package BarnOwl::Module::Jabber::ConnectionManager; |
---|
16 | |
---|
17 | sub new { |
---|
18 | my $class = shift; |
---|
19 | return bless { }, $class; |
---|
20 | } |
---|
21 | |
---|
22 | sub addConnection { |
---|
23 | my $self = shift; |
---|
24 | my $jidStr = shift; |
---|
25 | |
---|
26 | my $client = BarnOwl::Module::Jabber::Connection->new; |
---|
27 | |
---|
28 | $self->{$jidStr}->{Client} = $client; |
---|
29 | $self->{$jidStr}->{Roster} = $client->Roster(); |
---|
30 | $self->{$jidStr}->{Status} = "available"; |
---|
31 | return $client; |
---|
32 | } |
---|
33 | |
---|
34 | sub removeConnection { |
---|
35 | my $self = shift; |
---|
36 | my $jidStr = shift; |
---|
37 | return 0 unless exists $self->{$jidStr}; |
---|
38 | |
---|
39 | $self->{$jidStr}->{Client}->Disconnect() |
---|
40 | if $self->{$jidStr}->{Client}; |
---|
41 | delete $self->{$jidStr}; |
---|
42 | |
---|
43 | return 1; |
---|
44 | } |
---|
45 | |
---|
46 | sub renameConnection { |
---|
47 | my $self = shift; |
---|
48 | my $oldJidStr = shift; |
---|
49 | my $newJidStr = shift; |
---|
50 | return 0 unless exists $self->{$oldJidStr}; |
---|
51 | return 0 if $oldJidStr eq $newJidStr; |
---|
52 | |
---|
53 | $self->{$newJidStr} = $self->{$oldJidStr}; |
---|
54 | delete $self->{$oldJidStr}; |
---|
55 | return 1; |
---|
56 | } |
---|
57 | |
---|
58 | sub connected { |
---|
59 | my $self = shift; |
---|
60 | return scalar keys %{ $self }; |
---|
61 | } |
---|
62 | |
---|
63 | sub getJIDs { |
---|
64 | my $self = shift; |
---|
65 | return keys %{ $self }; |
---|
66 | } |
---|
67 | |
---|
68 | sub jidExists { |
---|
69 | my $self = shift; |
---|
70 | my $jidStr = shift; |
---|
71 | return exists $self->{$jidStr}; |
---|
72 | } |
---|
73 | |
---|
74 | sub sidExists { |
---|
75 | my $self = shift; |
---|
76 | my $sid = shift || ""; |
---|
77 | foreach my $c ( values %{ $self } ) { |
---|
78 | return 1 if ($c->{Client}->{SESSION}->{id} eq $sid); |
---|
79 | } |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | sub getConnectionFromSid { |
---|
84 | my $self = shift; |
---|
85 | my $sid = shift; |
---|
86 | foreach my $c (values %{ $self }) { |
---|
87 | return $c->{Client} if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
88 | } |
---|
89 | return undef; |
---|
90 | } |
---|
91 | |
---|
92 | sub getConnectionFromJID { |
---|
93 | my $self = shift; |
---|
94 | my $jid = shift; |
---|
95 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
96 | return $self->{$jid}->{Client} if exists $self->{$jid}; |
---|
97 | } |
---|
98 | |
---|
99 | sub getRosterFromSid { |
---|
100 | my $self = shift; |
---|
101 | my $sid = shift; |
---|
102 | foreach my $c (values %{ $self }) { |
---|
103 | return $c->{Roster} |
---|
104 | if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
105 | } |
---|
106 | return undef; |
---|
107 | } |
---|
108 | |
---|
109 | sub getRosterFromJID { |
---|
110 | my $self = shift; |
---|
111 | my $jid = shift; |
---|
112 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
113 | return $self->{$jid}->{Roster} if exists $self->{$jid}; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | 1; |
---|