source: perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm @ f798d02

release-1.10release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since f798d02 was 8ff511d, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 14 years ago
Tweak jabberlogin. * Disallow logging into the same jabber account twice unless you are explicitly specifying a resource.
  • Property mode set to 100644
File size: 2.8 KB
Line 
1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Module::Jabber::ConnectionManager
7
8=head1 DESCRIPTION
9
10A class to keep track of all the active connection in the barnowl
11jabber module
12
13=cut
14
15package BarnOwl::Module::Jabber::ConnectionManager;
16
17sub new {
18    my $class = shift;
19    return bless { }, $class;
20}
21
22sub 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
34sub 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
47sub 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
59sub connected {
60    my $self = shift;
61    return scalar keys %{ $self };
62}
63
64sub getJIDs {
65    my $self = shift;
66    return keys %{ $self };
67}
68
69sub jidExists {
70    my $self = shift;
71    my $jidStr = shift;
72    return exists $self->{$jidStr};
73}
74
75sub 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
90sub 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
99sub 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
108sub 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
115sub 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
125sub 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
1331;
Note: See TracBrowser for help on using the repository browser.