[2cedb7a] | 1 | use warnings; |
---|
| 2 | use strict; |
---|
| 3 | |
---|
| 4 | =head1 NAME |
---|
| 5 | |
---|
| 6 | BarnOwl::Module::Jabber::Connection |
---|
| 7 | |
---|
| 8 | =head1 DESCRIPTION |
---|
| 9 | |
---|
| 10 | A subclass of L<Net::Jabber::Client> used in the BarnOwl jabber module |
---|
| 11 | |
---|
| 12 | =cut |
---|
| 13 | |
---|
| 14 | package BarnOwl::Module::Jabber::Connection; |
---|
| 15 | |
---|
| 16 | use base qw(Net::Jabber::Client); |
---|
| 17 | |
---|
| 18 | use Net::Jabber; |
---|
| 19 | |
---|
| 20 | sub new { |
---|
| 21 | my $class = shift; |
---|
| 22 | |
---|
| 23 | my %args = (); |
---|
| 24 | if(BarnOwl::getvar('debug') eq 'on') { |
---|
| 25 | $args{debuglevel} = 1; |
---|
| 26 | $args{debugfile} = 'jabber.log'; |
---|
| 27 | } |
---|
| 28 | my $self = $class->SUPER::new(%args); |
---|
| 29 | $self->{_BARNOWL_MUCS} = []; |
---|
| 30 | return $self; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | =head2 MUCJoin |
---|
| 34 | |
---|
| 35 | Extends MUCJoin to keep track of the MUCs we're joined to as |
---|
| 36 | Net::Jabber::MUC objects. Takes the same arguments as |
---|
| 37 | L<Net::Jabber::MUC/new> and L<Net::Jabber::MUC/Connect> |
---|
| 38 | |
---|
| 39 | =cut |
---|
| 40 | |
---|
| 41 | sub MUCJoin { |
---|
| 42 | my $self = shift; |
---|
| 43 | my $muc = Net::Jabber::MUC->new(connection => $self, @_); |
---|
| 44 | $muc->Join(@_); |
---|
[3c455b4] | 45 | |
---|
| 46 | # Add MUC to list of MUCs, unless we're just changing nicks. |
---|
| 47 | push @{$self->MUCs}, $muc unless grep {$_->BaseJID eq $muc->BaseJID} $self->MUCs; |
---|
[2cedb7a] | 48 | } |
---|
| 49 | |
---|
| 50 | =head2 MUCLeave ARGS |
---|
| 51 | |
---|
| 52 | Leave a MUC. The MUC is specified in the same form as L</FindMUC> |
---|
| 53 | |
---|
| 54 | =cut |
---|
| 55 | |
---|
| 56 | sub MUCLeave { |
---|
| 57 | my $self = shift; |
---|
| 58 | my $muc = $self->FindMUC(@_); |
---|
| 59 | return unless $muc; |
---|
| 60 | |
---|
| 61 | $muc->Leave(); |
---|
| 62 | $self->{_BARNOWL_MUCS} = [grep {$_->BaseJID ne $muc->BaseJID} $self->MUCs]; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | =head2 FindMUC ARGS |
---|
| 66 | |
---|
| 67 | Return the Net::Jabber::MUC object representing a specific MUC we're |
---|
| 68 | joined to, undef if it doesn't exists. ARGS can be either JID => $JID, |
---|
| 69 | or Room => $room, Server => $server. |
---|
| 70 | |
---|
| 71 | =cut |
---|
| 72 | |
---|
| 73 | sub FindMUC { |
---|
| 74 | my $self = shift; |
---|
| 75 | |
---|
| 76 | my %args; |
---|
| 77 | while($#_ >= 0) { $args{ lc(pop(@_)) } = pop(@_); } |
---|
| 78 | |
---|
| 79 | my $jid; |
---|
| 80 | if($args{jid}) { |
---|
| 81 | $jid = $args{jid}; |
---|
| 82 | } elsif($args{room} && $args{server}) { |
---|
| 83 | $jid = Net::Jabber::JID->new(userid => $args{room}, |
---|
| 84 | server => $args{server}); |
---|
| 85 | } |
---|
| 86 | $jid = $jid->GetJID('base') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
| 87 | |
---|
| 88 | foreach my $muc ($self->MUCs) { |
---|
| 89 | return $muc if $muc->BaseJID eq $jid; |
---|
| 90 | } |
---|
| 91 | return undef; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | =head2 MUCs |
---|
| 95 | |
---|
| 96 | Returns a list (or arrayref in scalar context) of Net::Jabber::MUC |
---|
| 97 | objects we believe ourself to be connected to. |
---|
| 98 | |
---|
| 99 | =cut |
---|
| 100 | |
---|
| 101 | sub MUCs { |
---|
| 102 | my $self = shift; |
---|
| 103 | my $mucs = $self->{_BARNOWL_MUCS}; |
---|
| 104 | return wantarray ? @$mucs : $mucs; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | =head1 SEE ALSO |
---|
| 109 | |
---|
| 110 | L<Net::Jabber::Client>, L<BarnOwl::Module::Jabber> |
---|
| 111 | |
---|
| 112 | =cut |
---|
| 113 | |
---|
| 114 | 1; |
---|