[2cedb7a] | 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 | |
---|
[f1a2736] | 39 | BarnOwl::remove_io_dispatch($self->{$jidStr}->{Client}->{fileno}) if $self->{$jidStr}->{Client}->{fileno}; |
---|
[2cedb7a] | 40 | $self->{$jidStr}->{Client}->Disconnect() |
---|
| 41 | if $self->{$jidStr}->{Client}; |
---|
| 42 | delete $self->{$jidStr}; |
---|
| 43 | |
---|
| 44 | return 1; |
---|
| 45 | } |
---|
| 46 | |
---|
[a957e92] | 47 | sub scheduleReconnect { |
---|
| 48 | my $self = shift; |
---|
| 49 | my $jidStr = shift; |
---|
| 50 | return 0 unless exists $self->{$jidStr}; |
---|
| 51 | BarnOwl::admin_message(Jabber => "Disconnected from jabber account $jidStr"); |
---|
| 52 | |
---|
[f3678c3] | 53 | unless (BarnOwl::getvar('jabber:reconnect') eq 'on') { |
---|
| 54 | return $self->removeConnection($jidStr); |
---|
| 55 | } |
---|
| 56 | |
---|
[f1a2736] | 57 | BarnOwl::remove_io_dispatch($self->{$jidStr}->{Client}->{fileno}) if $self->{$jidStr}->{Client}->{fileno}; |
---|
[a957e92] | 58 | $self->{$jidStr}->{Client}->Disconnect() |
---|
| 59 | if $self->{$jidStr}->{Client}; |
---|
| 60 | |
---|
| 61 | $self->{$jidStr}->{Status} = "reconnecting"; |
---|
| 62 | $self->{$jidStr}->{ReconnectBackoff} = 5; |
---|
| 63 | $self->{$jidStr}->{ReconnectAt} = time + $self->{$jidStr}->{ReconnectBackoff}; |
---|
| 64 | return 1; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | sub setAuth { |
---|
| 68 | my $self = shift; |
---|
| 69 | my $jidStr = shift; |
---|
| 70 | $self->{$jidStr}->{Auth} = shift; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | sub tryReconnect { |
---|
| 74 | my $self = shift; |
---|
| 75 | my $jidStr = shift; |
---|
| 76 | my $force = shift; |
---|
| 77 | |
---|
| 78 | return 0 unless exists $self->{$jidStr}; |
---|
| 79 | return 0 unless $self->{$jidStr}{Status} eq "reconnecting"; |
---|
| 80 | return 0 unless $force or (time > $self->{$jidStr}{ReconnectAt}); |
---|
| 81 | |
---|
| 82 | $self->{$jidStr}->{ReconnectBackoff} *= 2; |
---|
| 83 | $self->{$jidStr}->{ReconnectBackoff} = 60*5 |
---|
| 84 | if $self->{$jidStr}->{ReconnectBackoff} > 60*5; |
---|
| 85 | $self->{$jidStr}->{ReconnectAt} = time + $self->{$jidStr}->{ReconnectBackoff}; |
---|
| 86 | |
---|
[0dbb7d2] | 87 | my $client = $self->{$jidStr}->{Client}; |
---|
| 88 | my $status = $client->Connect; |
---|
[a957e92] | 89 | return 0 unless $status; |
---|
| 90 | |
---|
[0dbb7d2] | 91 | my @result = $client->AuthSend( %{ $self->{$jidStr}->{Auth} } ); |
---|
[a957e92] | 92 | if ( !@result || $result[0] ne 'ok' ) { |
---|
| 93 | $self->removeConnection($jidStr); |
---|
| 94 | BarnOwl::error( "Error in jabber reconnect: " . join( " ", @result ) ); |
---|
| 95 | return 0; |
---|
| 96 | } |
---|
[0dbb7d2] | 97 | $self->{$jidStr}->{Status} = "available"; |
---|
| 98 | $client->onConnect($self, $jidStr); |
---|
| 99 | foreach my $muc ($client->MUCs()) { |
---|
[8590774] | 100 | $muc->Join($muc->{ARGS}); |
---|
| 101 | } |
---|
| 102 | |
---|
[a957e92] | 103 | return 1; |
---|
| 104 | } |
---|
| 105 | |
---|
[7f33c18] | 106 | sub renameConnection { |
---|
| 107 | my $self = shift; |
---|
| 108 | my $oldJidStr = shift; |
---|
| 109 | my $newJidStr = shift; |
---|
| 110 | return 0 unless exists $self->{$oldJidStr}; |
---|
| 111 | return 0 if $oldJidStr eq $newJidStr; |
---|
| 112 | |
---|
[9c7a701] | 113 | $self->{$newJidStr} = $self->{$oldJidStr}; |
---|
[7f33c18] | 114 | delete $self->{$oldJidStr}; |
---|
| 115 | return 1; |
---|
| 116 | } |
---|
| 117 | |
---|
[2cedb7a] | 118 | sub connected { |
---|
| 119 | my $self = shift; |
---|
| 120 | return scalar keys %{ $self }; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | sub getJIDs { |
---|
| 124 | my $self = shift; |
---|
| 125 | return keys %{ $self }; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | sub jidExists { |
---|
| 129 | my $self = shift; |
---|
| 130 | my $jidStr = shift; |
---|
| 131 | return exists $self->{$jidStr}; |
---|
| 132 | } |
---|
| 133 | |
---|
[8ff511d] | 134 | sub baseJIDExists { |
---|
| 135 | my $self = shift; |
---|
| 136 | my $jidStr = shift; |
---|
| 137 | my $jid = new Net::Jabber::JID; |
---|
| 138 | $jid->SetJID($jidStr); |
---|
| 139 | my $baseJID = $jid->GetJID('base'); |
---|
| 140 | |
---|
| 141 | foreach my $cjidStr ( keys %{ $self } ) { |
---|
| 142 | my $cJID = new Net::Jabber::JID; |
---|
| 143 | $cJID->SetJID($cjidStr); |
---|
| 144 | return $cjidStr if ($cJID->GetJID('base') eq $baseJID); |
---|
| 145 | } |
---|
| 146 | return 0; |
---|
| 147 | } |
---|
| 148 | |
---|
[a957e92] | 149 | sub jidActive { |
---|
| 150 | my $self = shift; |
---|
| 151 | my $jidStr = shift; |
---|
| 152 | return(exists $self->{$jidStr} and $self->{$jidStr}{Status} eq "available"); |
---|
| 153 | } |
---|
| 154 | |
---|
[2cedb7a] | 155 | sub sidExists { |
---|
| 156 | my $self = shift; |
---|
| 157 | my $sid = shift || ""; |
---|
| 158 | foreach my $c ( values %{ $self } ) { |
---|
| 159 | return 1 if ($c->{Client}->{SESSION}->{id} eq $sid); |
---|
| 160 | } |
---|
| 161 | return 0; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | sub getConnectionFromSid { |
---|
| 165 | my $self = shift; |
---|
| 166 | my $sid = shift; |
---|
| 167 | foreach my $c (values %{ $self }) { |
---|
| 168 | return $c->{Client} if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
| 169 | } |
---|
| 170 | return undef; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | sub getConnectionFromJID { |
---|
| 174 | my $self = shift; |
---|
| 175 | my $jid = shift; |
---|
| 176 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
[a957e92] | 177 | return $self->{$jid}->{Client} if $self->jidActive($jid); |
---|
[2cedb7a] | 178 | } |
---|
| 179 | |
---|
| 180 | sub getRosterFromSid { |
---|
| 181 | my $self = shift; |
---|
| 182 | my $sid = shift; |
---|
| 183 | foreach my $c (values %{ $self }) { |
---|
| 184 | return $c->{Roster} |
---|
| 185 | if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
| 186 | } |
---|
| 187 | return undef; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | sub getRosterFromJID { |
---|
| 191 | my $self = shift; |
---|
| 192 | my $jid = shift; |
---|
| 193 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
[a957e92] | 194 | return $self->{$jid}->{Roster} if $self->jidExists($jid); |
---|
[2cedb7a] | 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | 1; |
---|