[2cedb7a] | 1 | use warnings; |
---|
| 2 | use strict; |
---|
[e0ffe77] | 3 | use utf8; |
---|
[2cedb7a] | 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(@_); |
---|
[3c455b4] | 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; |
---|
[2cedb7a] | 49 | } |
---|
| 50 | |
---|
| 51 | =head2 MUCLeave ARGS |
---|
| 52 | |
---|
| 53 | Leave a MUC. The MUC is specified in the same form as L</FindMUC> |
---|
| 54 | |
---|
[892568b] | 55 | Returns true if successful, false if this connection was not in the |
---|
| 56 | named MUC. |
---|
| 57 | |
---|
[2cedb7a] | 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]; |
---|
[892568b] | 67 | return 1; |
---|
[2cedb7a] | 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 | =head1 SEE ALSO |
---|
| 114 | |
---|
| 115 | L<Net::Jabber::Client>, L<BarnOwl::Module::Jabber> |
---|
| 116 | |
---|
| 117 | =cut |
---|
| 118 | |
---|
| 119 | 1; |
---|