Circle-Net-Matrix

 view release on metacpan or  search on metacpan

lib/Circle/Net/Matrix.pm  view on Meta::CPAN

#  You may distribute under the terms of the GNU General Public License
#
#  (C) Paul Evans, 2014-2017 -- leonerd@leonerd.org.uk

package Circle::Net::Matrix;

use strict;
use warnings;
use base qw( Circle::Net );

our $VERSION = '0.03';

require Circle;
Circle->VERSION( '0.142470' );  # require late-loading of Tangence::Class

use constant NETTYPE => 'matrix';

use Circle::Widget::Box;
use Circle::Widget::Label;

use Data::Dump qw( pp );
use Scalar::Util qw( weaken );

=head1 NAME

C<Circle::Net::Matrix> - use C<Circle> as a I<Matrix> client

=head1 SYNOPSIS

On the global tab:

 /networks add -type matrix Matrix

On the newly-added "Matrix" tab:

 /set homeserver example.com
 /set user_id @me:example.com
 /set access_token MDAxABCDE...

 /connect

(for now you'll have to log in and steal and access token from another Matrix
client; for example L<App::MatrixTool>).

=cut

# To allow for out-of-tree development, use an inline Tangence class
# declaration instead of a .tan file
#
# class Circle.Net.Matrix {
#   isa Circle.WindowItem;
# }

sub DECLARE_TANGENCE
{
   Tangence::Class->declare( __PACKAGE__,
      superclasses => [qw( Circle::WindowItem )],
   );

   # Also load the other classes
   require Circle::Net::Matrix::Room;
   Circle::Net::Matrix::Room->DECLARE_TANGENCE;

   require Net::Async::Matrix;
   Net::Async::Matrix->VERSION( '0.18003' );
}

sub WEAKSELF_EVAL
{
   my ( $self, $method ) = @_;
   my $code = $self->can( $method ) or return sub {};

   weaken( $self );
   return sub {
      my @args = @_;
      eval { $self->$code( @args ); 1 } or
         warn $@;
   };
}

sub new
{
   my $class = shift;
   my %args = @_;

   my $self = $class->SUPER::new( %args );

   $self->{root} = $args{root};
   my $loop = $self->{loop} = $args{loop};

   # For WindowItem
   $self->set_prop_tag( $args{tag} );

   weaken( my $weakself = $self );
   my $matrix = $self->{matrix} = Net::Async::Matrix->new(
      on_log => sub { }, # TODO
      on_presence => $self->WEAKSELF_EVAL( 'on_presence' ),
      on_room_new => $self->WEAKSELF_EVAL( 'on_room_new' ),
      on_room_del => $self->WEAKSELF_EVAL( 'on_room_del' ),

      on_error => $self->WEAKSELF_EVAL( 'on_error' ),
   );

   $loop->add( $matrix );

   $self->set_network_status( "disconnected" );

   return $self;
}

sub on_error
{
   my $self = shift; shift;
   my ( $message ) = @_;

   $self->push_displayevent( error => { text => $message } );
   $self->bump_level( 3 );
}

sub parent
{
   my $self = shift;
   return $self->{root};
}

sub enumerable_name
{
   my $self = shift;
   return $self->get_prop_tag;
}

sub commandable_parent
{
   my $self = shift;
   return $self->{root};
}

sub get_room_or_create
{
   my $self = shift;
   my ( $room ) = @_;

   my $room_id = $room->room_id;

   return $self->{rooms}{$room_id} if exists $self->{rooms}{$room_id};

   my $registry = $self->{registry};
   my $root = $self->{root};

   my $roomobj = $registry->construct(
      "Circle::Net::Matrix::Room",
      root => $root,
      net  => $self,
      room => $room,



( run in 2.135 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )