| 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->Join(@_); |
|---|
| 46 | |
|---|
| 47 | # Add MUC to list of MUCs, unless we're just changing nicks. |
|---|
| 48 | push @{$self->MUCs}, $muc unless grep {$_->BaseJID eq $muc->BaseJID} $self->MUCs; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | =head2 MUCLeave ARGS |
|---|
| 52 | |
|---|
| 53 | Leave a MUC. The MUC is specified in the same form as L</FindMUC> |
|---|
| 54 | |
|---|
| 55 | Returns true if successful, false if this connection was not in the |
|---|
| 56 | named MUC. |
|---|
| 57 | |
|---|
| 58 | =cut |
|---|
| 59 | |
|---|
| 60 | sub MUCLeave { |
|---|
| 61 | my $self = shift; |
|---|
| 62 | my $muc = $self->FindMUC(@_); |
|---|
| 63 | return unless $muc; |
|---|
| 64 | |
|---|
| 65 | $muc->Leave(); |
|---|
| 66 | $self->{_BARNOWL_MUCS} = [grep {$_->BaseJID ne $muc->BaseJID} $self->MUCs]; |
|---|
| 67 | return 1; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | =head2 FindMUC ARGS |
|---|
| 71 | |
|---|
| 72 | Return the Net::Jabber::MUC object representing a specific MUC we're |
|---|
| 73 | joined to, undef if it doesn't exists. ARGS can be either JID => $JID, |
|---|
| 74 | or Room => $room, Server => $server. |
|---|
| 75 | |
|---|
| 76 | =cut |
|---|
| 77 | |
|---|
| 78 | sub FindMUC { |
|---|
| 79 | my $self = shift; |
|---|
| 80 | |
|---|
| 81 | my %args; |
|---|
| 82 | while($#_ >= 0) { $args{ lc(pop(@_)) } = pop(@_); } |
|---|
| 83 | |
|---|
| 84 | my $jid; |
|---|
| 85 | if($args{jid}) { |
|---|
| 86 | $jid = $args{jid}; |
|---|
| 87 | } elsif($args{room} && $args{server}) { |
|---|
| 88 | $jid = Net::Jabber::JID->new(userid => $args{room}, |
|---|
| 89 | server => $args{server}); |
|---|
| 90 | } |
|---|
| 91 | $jid = $jid->GetJID('base') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
|---|
| 92 | |
|---|
| 93 | foreach my $muc ($self->MUCs) { |
|---|
| 94 | return $muc if $muc->BaseJID eq $jid; |
|---|
| 95 | } |
|---|
| 96 | return undef; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | =head2 MUCs |
|---|
| 100 | |
|---|
| 101 | Returns a list (or arrayref in scalar context) of Net::Jabber::MUC |
|---|
| 102 | objects we believe ourself to be connected to. |
|---|
| 103 | |
|---|
| 104 | =cut |
|---|
| 105 | |
|---|
| 106 | sub MUCs { |
|---|
| 107 | my $self = shift; |
|---|
| 108 | my $mucs = $self->{_BARNOWL_MUCS}; |
|---|
| 109 | return wantarray ? @$mucs : $mucs; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | =head2 getSID |
|---|
| 114 | |
|---|
| 115 | Returns the StreamID for this connection. |
|---|
| 116 | |
|---|
| 117 | =cut |
|---|
| 118 | |
|---|
| 119 | sub getStreamID { |
|---|
| 120 | my $self = shift; |
|---|
| 121 | return $self->{SESSION}->{id} || ""; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | =head2 getSocket |
|---|
| 125 | |
|---|
| 126 | Returns the IO::Socket for this connection. |
|---|
| 127 | |
|---|
| 128 | =cut |
|---|
| 129 | |
|---|
| 130 | sub getSocket { |
|---|
| 131 | my $self = shift; |
|---|
| 132 | my $sid = getStreamID($self); |
|---|
| 133 | return $self->{STREAM}->GetSock($sid) || -1; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | =head2 OwlProcess |
|---|
| 137 | |
|---|
| 138 | Non-blocking connection processing. For use in a select loop. |
|---|
| 139 | |
|---|
| 140 | =cut |
|---|
| 141 | |
|---|
| 142 | sub OwlProcess { |
|---|
| 143 | my $self = shift; |
|---|
| 144 | my $status = $self->Process(0); |
|---|
| 145 | if ( !defined($status) ) { |
|---|
| 146 | my $jid = $self->{SESSION}->{FULLJID}; |
|---|
| 147 | BarnOwl::error("Jabber account $jid disconnected!"); |
|---|
| 148 | BarnOwl::Module::Jabber::do_logout($jid); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | =head1 SEE ALSO |
|---|
| 153 | |
|---|
| 154 | L<Net::Jabber::Client>, L<BarnOwl::Module::Jabber> |
|---|
| 155 | |
|---|
| 156 | =cut |
|---|
| 157 | |
|---|
| 158 | 1; |
|---|