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}->getSocket()->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 sidExists { |
---|
76 | my $self = shift; |
---|
77 | my $sid = shift || ""; |
---|
78 | foreach my $c ( values %{ $self } ) { |
---|
79 | return 1 if ($c->{Client}->{SESSION}->{id} eq $sid); |
---|
80 | } |
---|
81 | return 0; |
---|
82 | } |
---|
83 | |
---|
84 | sub getConnectionFromSid { |
---|
85 | my $self = shift; |
---|
86 | my $sid = shift; |
---|
87 | foreach my $c (values %{ $self }) { |
---|
88 | return $c->{Client} if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
89 | } |
---|
90 | return undef; |
---|
91 | } |
---|
92 | |
---|
93 | sub getConnectionFromJID { |
---|
94 | my $self = shift; |
---|
95 | my $jid = shift; |
---|
96 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
97 | return $self->{$jid}->{Client} if exists $self->{$jid}; |
---|
98 | } |
---|
99 | |
---|
100 | sub getRosterFromSid { |
---|
101 | my $self = shift; |
---|
102 | my $sid = shift; |
---|
103 | foreach my $c (values %{ $self }) { |
---|
104 | return $c->{Roster} |
---|
105 | if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
106 | } |
---|
107 | return undef; |
---|
108 | } |
---|
109 | |
---|
110 | sub getRosterFromJID { |
---|
111 | my $self = shift; |
---|
112 | my $jid = shift; |
---|
113 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
114 | return $self->{$jid}->{Roster} if exists $self->{$jid}; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | 1; |
---|