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 | BarnOwl::remove_dispatch($self->{$jidStr}->{Client}->{fileno}) if $self->{$jidStr}->{Client}->{fileno}; |
---|
40 | $self->{$jidStr}->{Client}->Disconnect() |
---|
41 | if $self->{$jidStr}->{Client}; |
---|
42 | delete $self->{$jidStr}; |
---|
43 | |
---|
44 | return 1; |
---|
45 | } |
---|
46 | |
---|
47 | sub renameConnection { |
---|
48 | my $self = shift; |
---|
49 | my $oldJidStr = shift; |
---|
50 | my $newJidStr = shift; |
---|
51 | return 0 unless exists $self->{$oldJidStr}; |
---|
52 | return 0 if $oldJidStr eq $newJidStr; |
---|
53 | |
---|
54 | $self->{$newJidStr} = $self->{$oldJidStr}; |
---|
55 | delete $self->{$oldJidStr}; |
---|
56 | return 1; |
---|
57 | } |
---|
58 | |
---|
59 | sub connected { |
---|
60 | my $self = shift; |
---|
61 | return scalar keys %{ $self }; |
---|
62 | } |
---|
63 | |
---|
64 | sub getJIDs { |
---|
65 | my $self = shift; |
---|
66 | return keys %{ $self }; |
---|
67 | } |
---|
68 | |
---|
69 | sub jidExists { |
---|
70 | my $self = shift; |
---|
71 | my $jidStr = shift; |
---|
72 | return exists $self->{$jidStr}; |
---|
73 | } |
---|
74 | |
---|
75 | sub baseJIDExists { |
---|
76 | my $self = shift; |
---|
77 | my $jidStr = shift; |
---|
78 | my $jid = new Net::Jabber::JID; |
---|
79 | $jid->SetJID($jidStr); |
---|
80 | my $baseJID = $jid->GetJID('base'); |
---|
81 | |
---|
82 | foreach my $cjidStr ( keys %{ $self } ) { |
---|
83 | my $cJID = new Net::Jabber::JID; |
---|
84 | $cJID->SetJID($cjidStr); |
---|
85 | return $cjidStr if ($cJID->GetJID('base') eq $baseJID); |
---|
86 | } |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | sub sidExists { |
---|
91 | my $self = shift; |
---|
92 | my $sid = shift || ""; |
---|
93 | foreach my $c ( values %{ $self } ) { |
---|
94 | return 1 if ($c->{Client}->{SESSION}->{id} eq $sid); |
---|
95 | } |
---|
96 | return 0; |
---|
97 | } |
---|
98 | |
---|
99 | sub getConnectionFromSid { |
---|
100 | my $self = shift; |
---|
101 | my $sid = shift; |
---|
102 | foreach my $c (values %{ $self }) { |
---|
103 | return $c->{Client} if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
104 | } |
---|
105 | return undef; |
---|
106 | } |
---|
107 | |
---|
108 | sub getConnectionFromJID { |
---|
109 | my $self = shift; |
---|
110 | my $jid = shift; |
---|
111 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
112 | return $self->{$jid}->{Client} if exists $self->{$jid}; |
---|
113 | } |
---|
114 | |
---|
115 | sub getRosterFromSid { |
---|
116 | my $self = shift; |
---|
117 | my $sid = shift; |
---|
118 | foreach my $c (values %{ $self }) { |
---|
119 | return $c->{Roster} |
---|
120 | if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
121 | } |
---|
122 | return undef; |
---|
123 | } |
---|
124 | |
---|
125 | sub getRosterFromJID { |
---|
126 | my $self = shift; |
---|
127 | my $jid = shift; |
---|
128 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
129 | return $self->{$jid}->{Roster} if exists $self->{$jid}; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | 1; |
---|