| 1 | use warnings; |
|---|
| 2 | use strict; |
|---|
| 3 | use utf8; |
|---|
| 4 | |
|---|
| 5 | =head1 NAME |
|---|
| 6 | |
|---|
| 7 | BarnOwl::Module::Jabber::Connection |
|---|
| 8 | |
|---|
| 9 | =head1 DESCRIPTION |
|---|
| 10 | |
|---|
| 11 | A subclass of L<Net::Jabber::Client> used in the BarnOwl jabber module |
|---|
| 12 | |
|---|
| 13 | =cut |
|---|
| 14 | |
|---|
| 15 | package BarnOwl::Module::Jabber::Connection; |
|---|
| 16 | |
|---|
| 17 | use base qw(Net::Jabber::Client); |
|---|
| 18 | |
|---|
| 19 | use Net::Jabber; |
|---|
| 20 | |
|---|
| 21 | sub new { |
|---|
| 22 | my $class = shift; |
|---|
| 23 | |
|---|
| 24 | my %args = (); |
|---|
| 25 | if(BarnOwl::getvar('debug') eq 'on') { |
|---|
| 26 | $args{debuglevel} = 1; |
|---|
| 27 | $args{debugfile} = 'jabber.log'; |
|---|
| 28 | } |
|---|
| 29 | my $self = $class->SUPER::new(%args); |
|---|
| 30 | $self->{_BARNOWL_MUCS} = []; |
|---|
| 31 | return $self; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | =head2 MUCJoin |
|---|
| 35 | |
|---|
| 36 | Extends MUCJoin to keep track of the MUCs we're joined to as |
|---|
| 37 | Net::Jabber::MUC objects. Takes the same arguments as |
|---|
| 38 | L<Net::Jabber::MUC/new> and L<Net::Jabber::MUC/Connect> |
|---|
| 39 | |
|---|
| 40 | =cut |
|---|
| 41 | |
|---|
| 42 | sub MUCJoin { |
|---|
| 43 | my $self = shift; |
|---|
| 44 | my $muc = Net::Jabber::MUC->new(connection => $self, @_); |
|---|
| 45 | $muc->{ARGS} = @_; # Save these for later |
|---|
| 46 | $muc->Join(@_); |
|---|
| 47 | |
|---|
| 48 | # Add MUC to list of MUCs, unless we're just changing nicks. |
|---|
| 49 | push @{$self->MUCs}, $muc unless grep {$_->BaseJID eq $muc->BaseJID} $self->MUCs; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | =head2 MUCLeave ARGS |
|---|
| 53 | |
|---|
| 54 | Leave a MUC. The MUC is specified in the same form as L</FindMUC> |
|---|
| 55 | |
|---|
| 56 | Returns true if successful, false if this connection was not in the |
|---|
| 57 | named MUC. |
|---|
| 58 | |
|---|
| 59 | =cut |
|---|
| 60 | |
|---|
| 61 | sub MUCLeave { |
|---|
| 62 | my $self = shift; |
|---|
| 63 | my $muc = $self->FindMUC(@_); |
|---|
| 64 | return unless $muc; |
|---|
| 65 | |
|---|
| 66 | $muc->Leave(); |
|---|
| 67 | $self->{_BARNOWL_MUCS} = [grep {$_->BaseJID ne $muc->BaseJID} $self->MUCs]; |
|---|
| 68 | return 1; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | =head2 FindMUC ARGS |
|---|
| 72 | |
|---|
| 73 | Return the Net::Jabber::MUC object representing a specific MUC we're |
|---|
| 74 | joined to, undef if it doesn't exists. ARGS can be either JID => $JID, |
|---|
| 75 | or Room => $room, Server => $server. |
|---|
| 76 | |
|---|
| 77 | =cut |
|---|
| 78 | |
|---|
| 79 | sub FindMUC { |
|---|
| 80 | my $self = shift; |
|---|
| 81 | |
|---|
| 82 | my %args; |
|---|
| 83 | while($#_ >= 0) { $args{ lc(pop(@_)) } = pop(@_); } |
|---|
| 84 | |
|---|
| 85 | my $jid; |
|---|
| 86 | if($args{jid}) { |
|---|
| 87 | $jid = $args{jid}; |
|---|
| 88 | } elsif($args{room} && $args{server}) { |
|---|
| 89 | $jid = Net::Jabber::JID->new(userid => $args{room}, |
|---|
| 90 | server => $args{server}); |
|---|
| 91 | } |
|---|
| 92 | $jid = $jid->GetJID('base') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
|---|
| 93 | |
|---|
| 94 | foreach my $muc ($self->MUCs) { |
|---|
| 95 | return $muc if $muc->BaseJID eq $jid; |
|---|
| 96 | } |
|---|
| 97 | return undef; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | =head2 MUCs |
|---|
| 101 | |
|---|
| 102 | Returns a list (or arrayref in scalar context) of Net::Jabber::MUC |
|---|
| 103 | objects we believe ourself to be connected to. |
|---|
| 104 | |
|---|
| 105 | =cut |
|---|
| 106 | |
|---|
| 107 | sub MUCs { |
|---|
| 108 | my $self = shift; |
|---|
| 109 | my $mucs = $self->{_BARNOWL_MUCS}; |
|---|
| 110 | return wantarray ? @$mucs : $mucs; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | =head2 getSID |
|---|
| 115 | |
|---|
| 116 | Returns the StreamID for this connection. |
|---|
| 117 | |
|---|
| 118 | =cut |
|---|
| 119 | |
|---|
| 120 | sub getStreamID { |
|---|
| 121 | my $self = shift; |
|---|
| 122 | return $self->{SESSION}->{id} || ""; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | =head2 getSocket |
|---|
| 126 | |
|---|
| 127 | Returns the IO::Socket for this connection. |
|---|
| 128 | |
|---|
| 129 | =cut |
|---|
| 130 | |
|---|
| 131 | sub getSocket { |
|---|
| 132 | my $self = shift; |
|---|
| 133 | my $sid = getStreamID($self); |
|---|
| 134 | return $self->{STREAM}->GetSock($sid) || -1; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | =head2 OwlProcess |
|---|
| 138 | |
|---|
| 139 | Non-blocking connection processing. For use in a select loop. |
|---|
| 140 | |
|---|
| 141 | =cut |
|---|
| 142 | |
|---|
| 143 | sub OwlProcess { |
|---|
| 144 | my $self = shift; |
|---|
| 145 | my $jid = shift || $self->{SESSION}->{FULLJID}; |
|---|
| 146 | my $status = $self->Process(0); |
|---|
| 147 | if ( !defined($status) ) { |
|---|
| 148 | $BarnOwl::Module::Jabber::conn->scheduleReconnect($jid); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | =head2 Disconnect |
|---|
| 153 | |
|---|
| 154 | Work around a bug in Net::Jabber::Client where Process' return status |
|---|
| 155 | is not cleared on disconnect. |
|---|
| 156 | |
|---|
| 157 | =cut |
|---|
| 158 | |
|---|
| 159 | sub Disconnect { |
|---|
| 160 | my $self = shift; |
|---|
| 161 | delete $self->{PROCESSERROR}; |
|---|
| 162 | return $self->SUPER::Disconnect(@_); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | =head2 OnConnect |
|---|
| 166 | |
|---|
| 167 | Actions to perform on connecting and reconnecting. |
|---|
| 168 | |
|---|
| 169 | =cut |
|---|
| 170 | |
|---|
| 171 | sub onConnect { |
|---|
| 172 | my $self = shift; |
|---|
| 173 | my $conn = shift; |
|---|
| 174 | my $jidStr = shift; |
|---|
| 175 | |
|---|
| 176 | my $fullJid = $self->{SESSION}->{FULLJID} || $jidStr; |
|---|
| 177 | my $roster = $conn->getRosterFromJID($jidStr); |
|---|
| 178 | |
|---|
| 179 | $roster->fetch(); |
|---|
| 180 | $self->PresenceSend( priority => 1 ); |
|---|
| 181 | |
|---|
| 182 | $conn->renameConnection($jidStr, $fullJid); |
|---|
| 183 | BarnOwl::admin_message('Jabber', "Connected to jabber as $fullJid"); |
|---|
| 184 | # The remove_io_dispatch() method is called from the |
|---|
| 185 | # ConnectionManager's removeConnection() method. |
|---|
| 186 | $self->{fileno} = $self->getSocket()->fileno(); |
|---|
| 187 | BarnOwl::add_io_dispatch($self->{fileno}, 'r', sub { $self->OwlProcess($fullJid) }); |
|---|
| 188 | |
|---|
| 189 | # populate completion from roster. |
|---|
| 190 | for my $buddy ( $roster->jids('all') ) { |
|---|
| 191 | my %jq = $roster->query($buddy); |
|---|
| 192 | my $name = $jq{name} || $buddy->GetUserID(); |
|---|
| 193 | $BarnOwl::Module::Jabber::completion_jids{$name} = 1; |
|---|
| 194 | $BarnOwl::Module::Jabber::completion_jids{$buddy->GetJID()} = 1; |
|---|
| 195 | } |
|---|
| 196 | $BarnOwl::Module::Jabber::vars{idletime} |= BarnOwl::getidletime(); |
|---|
| 197 | unless (exists $BarnOwl::Module::Jabber::vars{keepAliveTimer}) { |
|---|
| 198 | $BarnOwl::Module::Jabber::vars{keepAliveTimer} = |
|---|
| 199 | BarnOwl::Timer->new({ |
|---|
| 200 | 'after' => 5, |
|---|
| 201 | 'interval' => 5, |
|---|
| 202 | 'cb' => sub { BarnOwl::Module::Jabber::do_keep_alive_and_auto_away(@_) } |
|---|
| 203 | }); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | =head1 SEE ALSO |
|---|
| 208 | |
|---|
| 209 | L<Net::Jabber::Client>, L<BarnOwl::Module::Jabber> |
|---|
| 210 | |
|---|
| 211 | =cut |
|---|
| 212 | |
|---|
| 213 | 1; |
|---|