AnyEvent-XMPP

 view release on metacpan or  search on metacpan

lib/AnyEvent/XMPP/Ext/MUC.pm  view on Meta::CPAN

package AnyEvent::XMPP::Ext::MUC;
use strict;
no warnings;
use AnyEvent::XMPP::Util qw/prep_bare_jid bare_jid stringprep_jid cmp_jid/;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
use AnyEvent::XMPP::Ext;
use AnyEvent::XMPP::Ext::MUC::Room;
use AnyEvent::XMPP::Ext::MUC::RoomInfo;

our @ISA = qw/AnyEvent::XMPP::Ext/;

=head1 NAME

AnyEvent::XMPP::Ext::MUC - Implements XEP-0045: Multi-User Chat

=head1 SYNOPSIS

   my $con = AnyEvent::XMPP::Connection->new (...);
   $con->add_extension (my $disco = AnyEvent::XMPP::Ext::Disco->new);
   $con->add_extension (my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco));
   ...

=head1 DESCRIPTION

This module handles multi user chats and provides new events to catch
multi user chat messages. It intercepts messages from the connection
so they don't interfere with your other callbacks on the connection.

This extension requires the L<AnyEvent::XMPP::Ext::Disco> extension for service
discovery.

=cut

=head1 METHODS

=over 4

=item B<new>

This is the constructor for a MUC extension object.
It takes no further arguments.

=cut

sub new {
   my $this = shift;
   my $class = ref($this) || $this;
   my $self = bless { join_timeout => 60, @_ }, $class;
   $self->{inhibit_forward} = { map { ($_ => 1) } qw/message presence/ };
   $self->init;
   $self
}

sub disco_feature { xmpp_ns ('muc') }

sub init {
   my ($self) = @_;

   my $proxy = sub {
      my ($self, @args) = @_;
      $self->event (@args);
   };

   $self->{disco}->enable_feature ($self->disco_feature);

   $self->reg_cb (
      join_error           => $proxy,
      subject_change_error => $proxy,
      message_error        => $proxy,
   );

   $self->reg_cb (
      ext_before_presence_xml => sub {
         my ($self, $con, $node) = @_;

         if (my $room = $self->get_room ($con, $node->attr ('from'))) {
            $self->stop_event;
            $room->handle_presence ($node);
         }
      },
      ext_before_message_xml => sub {
         my ($self, $con, $node) = @_;

         if (my $room = $self->get_room ($con, $node->attr ('from'))) {
            $self->stop_event;
            $room->handle_message ($node);
         }



( run in 0.598 second using v1.01-cache-2.11-cpan-39bf76dae61 )