Matrix.pm

filter: Statementstmt 29.9% 38 / 127 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 10 / 30 Podpod 0.0% 0 / 15 totaltotal 25.6% 48 / 187 CC 7 SCAR 11.1
CC
7
Cov
27%
WEAKSELF_EVAL
6.0
__ANON__
6.0
get_room_or_create
6.0
CRAP 3.0
« Matrix/Utils.pm ↑ Summary
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2014-2017 -- leonerd@leonerd.org.uk
4  
5 package Circle::Net::Matrix;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use base qw( Circle::Net );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.03';
12  
13 1 require Circle;
14 1 Circle->VERSION( '0.142470' ); # require late-loading of Tangence::Class
15  
16 1 ▶use constant NETTYPE => 'matrix';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Circle::Widget::Box;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Circle::Widget::Label;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Data::Dump qw( pp );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 NAME
25  
26 C<Circle::Net::Matrix> - use C<Circle> as a I<Matrix> client
27  
28 =head1 SYNOPSIS
29  
30 On the global tab:
31  
32 /networks add -type matrix Matrix
33  
34 On the newly-added "Matrix" tab:
35  
36 /set homeserver example.com
37 /set user_id @me:example.com
38 /set access_token MDAxABCDE...
39  
40 /connect
41  
42 (for now you'll have to log in and steal and access token from another Matrix
43 client; for example L<App::MatrixTool>).
44  
45 =cut
46  
47 # To allow for out-of-tree development, use an inline Tangence class
48 # declaration instead of a .tan file
49 #
50 # class Circle.Net.Matrix {
51 # isa Circle.WindowItem;
52 # }
53  
54 sub DECLARE_TANGENCE
55 0 ▶{
Subroutine DECLARE_TANGENCE: not called CC=1 SCAR=6.9
Pod undocumented
56 0 Tangence::Class->declare( __PACKAGE__,
57 superclasses => [qw( Circle::WindowItem )],
58 );
59  
60 # Also load the other classes
61 0 require Circle::Net::Matrix::Room;
62 0 Circle::Net::Matrix::Room->DECLARE_TANGENCE;
63  
64 0 require Net::Async::Matrix;
65 0 Net::Async::Matrix->VERSION( '0.18003' );
66 }
67  
68 sub WEAKSELF_EVAL
69 0 ▶{
Subroutine WEAKSELF_EVAL: not called CC=2 SCAR=17.9
Pod undocumented
70 0 my ( $self, $method ) = @_;
71 0 ▶ my $code = $self->can( $method ) or return sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can($method) or return sub { } 0 0 0
72  
73 0 weaken( $self );
74 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
75 0 my @args = @_;
76 0 ▶ eval { $self->$code( @args ); 1 } or
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->$code(@args); 1 } } or warn $@ 0 0 0
77 warn $@;
78 0 };
79 }
80  
81 sub new
82 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my $class = shift;
84 0 my %args = @_;
85  
86 0 my $self = $class->SUPER::new( %args );
87  
88 0 $self->{root} = $args{root};
89 0 my $loop = $self->{loop} = $args{loop};
90  
91 # For WindowItem
92 0 $self->set_prop_tag( $args{tag} );
93  
94 0 weaken( my $weakself = $self );
95 my $matrix = $self->{matrix} = Net::Async::Matrix->new(
96 0 ▶ on_log => sub { }, # TODO
Subroutine __ANON__: not called CC=1 SCAR=0.0
97 0 on_presence => $self->WEAKSELF_EVAL( 'on_presence' ),
98 on_room_new => $self->WEAKSELF_EVAL( 'on_room_new' ),
99 on_room_del => $self->WEAKSELF_EVAL( 'on_room_del' ),
100  
101 on_error => $self->WEAKSELF_EVAL( 'on_error' ),
102 );
103  
104 0 $loop->add( $matrix );
105  
106 0 $self->set_network_status( "disconnected" );
107  
108 0 return $self;
109 }
110  
111 sub on_error
112 0 ▶{
Subroutine on_error: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $self = shift; shift;
114 0 my ( $message ) = @_;
115  
116 0 $self->push_displayevent( error => { text => $message } );
117 0 $self->bump_level( 3 );
118 }
119  
120 sub parent
121 0 ▶{
Subroutine parent: not called CC=1 SCAR=6.9
Pod undocumented
122 0 my $self = shift;
123 0 return $self->{root};
124 }
125  
126 sub enumerable_name
127 0 ▶{
Subroutine enumerable_name: not called CC=1 SCAR=6.9
Pod undocumented
128 0 my $self = shift;
129 0 return $self->get_prop_tag;
130 }
131  
132 sub commandable_parent
133 0 ▶{
Subroutine commandable_parent: not called CC=1 SCAR=6.9
Pod undocumented
134 0 my $self = shift;
135 0 return $self->{root};
136 }
137  
138 sub get_room_or_create
139 0 ▶{
Subroutine get_room_or_create: not called CC=2 SCAR=17.9
Pod undocumented
140 0 my $self = shift;
141 0 my ( $room ) = @_;
142  
143 0 my $room_id = $room->room_id;
144  
145 0 ▶ return $self->{rooms}{$room_id} if exists $self->{rooms}{$room_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"rooms"}{$room_id} 0 0 0
146  
147 0 my $registry = $self->{registry};
148 0 my $root = $self->{root};
149  
150 0 my $roomobj = $registry->construct(
151 "Circle::Net::Matrix::Room",
152 root => $root,
153 net => $self,
154 room => $room,
155 );
156 0 $self->{rooms}{$room_id} = $roomobj;
157  
158 0 $root->broadcast_sessions( new_item => $roomobj );
159  
160 0 return $roomobj;
161 }
162  
163 sub on_room_new
164 0 ▶{
Subroutine on_room_new: not called CC=1 SCAR=6.9
Pod undocumented
165 0 my $self = shift; shift;
166 0 my ( $room ) = @_;
167  
168 0 $self->get_room_or_create( $room );
169 }
170  
171 sub on_room_del
172 0 ▶{
Subroutine on_room_del: not called CC=2 SCAR=17.9
Pod undocumented
173 0 my $self = shift; shift;
174 0 my ( $room ) = @_;
175  
176 0 ▶ my $roomobj = delete $self->{rooms}{$room->room_id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $roomobj = delete $self->{"rooms"}{$room->room_id} or return 0 0 0
177
178 0 $self->{root}->broadcast_sessions( delete_item => $roomobj );
179 0 $roomobj->destroy;
180 }
181  
182 sub on_presence
183 0 ▶{
Subroutine on_presence: not called CC=1 SCAR=6.9
Pod undocumented
184 0 my $self = shift; shift;
185 0 my ( $user, %changes ) = @_;
186  
187 # Ignore for now
188 #$self->push_displayevent( "text", {
189 # text => "User ${\$user->user_id} presence change " . pp(\%changes),
190 #});
191 }
192  
193 1 __PACKAGE__->APPLY_Setting( homeserver =>
194 description => "Hostname of the homeserver",
195 type => 'str',
196 );
197  
198 1 __PACKAGE__->APPLY_Setting( user_id =>
199 description => "User ID to use",
200 type => 'str',
201 );
202  
203 1 __PACKAGE__->APPLY_Setting( access_token =>
204 description => "Access Token of the user",
205 type => 'str',
206 );
207  
208 1 __PACKAGE__->APPLY_Setting( initial_backlog =>
209 description => "Initial size of backlog to request per room",
210 type => "int",
211 default => 100,
212 );
213  
214 sub command_connect
215 0 ▶ : Command_description("Connect to the homeserver")
Subroutine command_connect: not called CC=2 SCAR=17.9
Pod undocumented
216 : Command_arg('homeserver?')
217 {
218 0 my $self = shift;
219 0 my ( $homeserver, $cinv ) = @_;
220  
221 0 ▶ $homeserver = $self->{homeserver} unless defined $homeserver;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $homeserver 0 0 0
222  
223 0 my $matrix = $self->{matrix};
224 $matrix->configure(
225 server => $homeserver,
226 first_sync_limit => $self->{initial_backlog},
227 0 );
228  
229 # TODO: would be nice if Circle could cope with Future-returning
230 # command subs...
231  
232 0 $self->push_displayevent( "status", { text => "logging in" } );
233 0 $self->set_network_status( "logging in" );
234  
235 my $f = $matrix->login(
236 user_id => $self->{user_id},
237 access_token => $self->{access_token},
238 0 ▶ )->on_done( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
239 0 $self->push_displayevent( "status", { text => "syncing..." } );
240 0 $self->set_network_status( "syncing" );
241  
242 0 ▶ $matrix->start->on_done( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
243 0 $self->set_network_status( "" );
244 0 });
245 0 });
246  
247 0 $matrix->adopt_future( $f );
248  
249 0 return ();
250 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 sub command_join
253 0 ▶ : Command_description("Join a named room")
Subroutine command_join: not called CC=1 SCAR=6.9
Pod undocumented
254 : Command_arg('roomname')
255 {
256 0 my $self = shift;
257 0 my ( $roomname, $cinv ) = @_;
258  
259 0 my $matrix = $self->{matrix};
260  
261 0 $matrix->join_room( $roomname );
262  
263 0 return;
264 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
265  
266 ###
267 # Widgets
268 ###
269  
270 sub get_widget_my_displayname
271 0 ▶{
Subroutine get_widget_my_displayname: not called CC=2 SCAR=17.9
Pod undocumented
272 0 my $self = shift;
273  
274 0 ▶ return $self->{widget_displayname} ||= do {
Condition0%0/3
$self->{"widget_displayname"} ||= do { my $registry = $self->{"registry"}; my $widget = $registry->construct("Circle::Widget::Label", "classes", ["nick"]); $widget->set_prop_text($self->{"matrix"}->myself->displayname); $widget }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"widget_displayname"} ||= do { my $registry = $self->{"registry"}; my $widget = $registry->construct("Circle::Widget::Label", "classes", ["nick"]); $widget->set_prop_text($self->{"matrix"}->myself->displayname); $widget }
$self->{"widget_displayname"} do { my $registry = $self->{"registry"}; my $widget = $registry->construct("Circle::Widget::Label", "classes", ["nick"]); $widget->set_prop_text($self->{"matrix"}->myself->displayname); $widget }
Truth table0%0/3
A ||= B
A= $self->{"widget_displayname"}B= do { my $registry = $self->{"registry"}; my $widget = $registry->construct("Circle::Widget::Label", "classes", ["nick"]); $widget->set_prop_text($self->{"matrix"}->myself->displayname); $widget }
ABresult
1X1
011
000
275 0 my $registry = $self->{registry};
276  
277 0 my $widget = $registry->construct(
278 "Circle::Widget::Label",
279 classes => [qw( nick )],
280 );
281  
282 0 $widget->set_prop_text( $self->{matrix}->myself->displayname );
283  
284 0 $widget;
285 };
286 }
287  
288 sub get_widget_statusbar
289 0 ▶{
Subroutine get_widget_statusbar: not called CC=1 SCAR=6.9
Pod undocumented
290 0 my $self = shift;
291  
292 0 my $registry = $self->{registry};
293  
294 0 my $statusbar = $registry->construct(
295 "Circle::Widget::Box",
296 classes => [qw( status )],
297 orientation => "horizontal",
298 );
299  
300 0 $statusbar->add( $self->get_widget_netname );
301  
302 0 $statusbar->add( $self->get_widget_my_displayname );
303  
304 # $statusbar->add( $self->get_widget_presence );
305  
306 0 return $statusbar;
307 }
308  
309 =head1 AUTHOR
310  
311 Paul Evans <leonerd@leonerd.org.uk>
312  
313 =cut
314  
315 1 0x55AA;
« Matrix/Utils.pm ↑ Summary