line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
1
|
|
744
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
8
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
9
|
1
|
|
|
1
|
|
10
|
use base qw( Circle::Net ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
445
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
require Circle; |
14
|
|
|
|
|
|
|
Circle->VERSION( '0.142470' ); # require late-loading of Tangence::Class |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
73065
|
use constant NETTYPE => 'matrix'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
4
|
use Circle::Widget::Box; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
19
|
1
|
|
|
1
|
|
396
|
use Circle::Widget::Label; |
|
1
|
|
|
|
|
100
|
|
|
1
|
|
|
|
|
21
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
3
|
use Data::Dump qw( pp ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
22
|
1
|
|
|
1
|
|
4
|
use Scalar::Util qw( weaken ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
648
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 NAME |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
C - use C as a I 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). |
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
|
|
|
|
|
|
|
{ |
56
|
0
|
|
|
0
|
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.18002' ); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub WEAKSELF_EVAL |
69
|
|
|
|
|
|
|
{ |
70
|
0
|
|
|
0
|
0
|
|
my ( $self, $method ) = @_; |
71
|
0
|
0
|
|
0
|
|
|
my $code = $self->can( $method ) or return sub {}; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
weaken( $self ); |
74
|
|
|
|
|
|
|
return sub { |
75
|
0
|
|
|
0
|
|
|
my @args = @_; |
76
|
0
|
0
|
|
|
|
|
eval { $self->$code( @args ); 1 } or |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
warn $@; |
78
|
0
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub new |
82
|
|
|
|
|
|
|
{ |
83
|
0
|
|
|
0
|
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 |
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
|
|
|
|
|
|
|
{ |
113
|
0
|
|
|
0
|
0
|
|
my $self = shift; shift; |
|
0
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
{ |
122
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
123
|
0
|
|
|
|
|
|
return $self->{root}; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub enumerable_name |
127
|
|
|
|
|
|
|
{ |
128
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
129
|
0
|
|
|
|
|
|
return $self->get_prop_tag; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub commandable_parent |
133
|
|
|
|
|
|
|
{ |
134
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
135
|
0
|
|
|
|
|
|
return $self->{root}; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub get_room_or_create |
139
|
|
|
|
|
|
|
{ |
140
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
141
|
0
|
|
|
|
|
|
my ( $room ) = @_; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
my $room_id = $room->room_id; |
144
|
|
|
|
|
|
|
|
145
|
0
|
0
|
|
|
|
|
return $self->{rooms}{$room_id} if exists $self->{rooms}{$room_id}; |
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
|
|
|
|
|
|
|
{ |
165
|
0
|
|
|
0
|
0
|
|
my $self = shift; shift; |
|
0
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
my ( $room ) = @_; |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
$self->get_room_or_create( $room ); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub on_room_del |
172
|
|
|
|
|
|
|
{ |
173
|
0
|
|
|
0
|
0
|
|
my $self = shift; shift; |
|
0
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
my ( $room ) = @_; |
175
|
|
|
|
|
|
|
|
176
|
0
|
0
|
|
|
|
|
my $roomobj = delete $self->{rooms}{$room->room_id} or return; |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
$self->{root}->broadcast_sessions( delete_item => $roomobj ); |
179
|
0
|
|
|
|
|
|
$roomobj->destroy; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub on_presence |
183
|
|
|
|
|
|
|
{ |
184
|
0
|
|
|
0
|
0
|
|
my $self = shift; shift; |
|
0
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
__PACKAGE__->APPLY_Setting( homeserver => |
194
|
|
|
|
|
|
|
description => "Hostname of the homeserver", |
195
|
|
|
|
|
|
|
type => 'str', |
196
|
|
|
|
|
|
|
); |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
__PACKAGE__->APPLY_Setting( user_id => |
199
|
|
|
|
|
|
|
description => "User ID to use", |
200
|
|
|
|
|
|
|
type => 'str', |
201
|
|
|
|
|
|
|
); |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
__PACKAGE__->APPLY_Setting( access_token => |
204
|
|
|
|
|
|
|
description => "Access Token of the user", |
205
|
|
|
|
|
|
|
type => 'str', |
206
|
|
|
|
|
|
|
); |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub command_connect |
209
|
|
|
|
|
|
|
: Command_description("Connect to the homeserver") |
210
|
|
|
|
|
|
|
: Command_arg('homeserver?') |
211
|
|
|
|
|
|
|
{ |
212
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
213
|
0
|
|
|
|
|
0
|
my ( $homeserver, $cinv ) = @_; |
214
|
|
|
|
|
|
|
|
215
|
0
|
0
|
|
|
|
0
|
$homeserver = $self->{homeserver} unless defined $homeserver; |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
0
|
my $matrix = $self->{matrix}; |
218
|
0
|
|
|
|
|
0
|
$matrix->configure( |
219
|
|
|
|
|
|
|
server => $homeserver, |
220
|
|
|
|
|
|
|
); |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# TODO: would be nice if Circle could cope with Future-returning |
223
|
|
|
|
|
|
|
# command subs... |
224
|
|
|
|
|
|
|
|
225
|
0
|
|
|
|
|
0
|
$self->push_displayevent( "status", { text => "logging in" } ); |
226
|
0
|
|
|
|
|
0
|
$self->set_network_status( "logging in" ); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
my $f = $matrix->login( |
229
|
|
|
|
|
|
|
user_id => $self->{user_id}, |
230
|
|
|
|
|
|
|
access_token => $self->{access_token}, |
231
|
|
|
|
|
|
|
)->on_done( sub { |
232
|
0
|
|
|
0
|
|
0
|
$self->push_displayevent( "status", { text => "syncing..." } ); |
233
|
0
|
|
|
|
|
0
|
$self->set_network_status( "syncing" ); |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
$matrix->start->on_done( sub { |
236
|
0
|
|
|
|
|
0
|
$self->set_network_status( "" ); |
237
|
0
|
|
|
|
|
0
|
}); |
238
|
0
|
|
|
|
|
0
|
}); |
239
|
|
|
|
|
|
|
|
240
|
0
|
|
|
|
|
0
|
$matrix->adopt_future( $f ); |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
0
|
return (); |
243
|
1
|
|
|
1
|
|
7
|
} |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
sub command_join |
246
|
|
|
|
|
|
|
: Command_description("Join a named room") |
247
|
|
|
|
|
|
|
: Command_arg('roomname') |
248
|
|
|
|
|
|
|
{ |
249
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
250
|
0
|
|
|
|
|
|
my ( $roomname, $cinv ) = @_; |
251
|
|
|
|
|
|
|
|
252
|
0
|
|
|
|
|
|
my $matrix = $self->{matrix}; |
253
|
|
|
|
|
|
|
|
254
|
0
|
|
|
|
|
|
$matrix->join_room( $roomname ); |
255
|
|
|
|
|
|
|
|
256
|
0
|
|
|
|
|
|
return; |
257
|
1
|
|
|
1
|
|
349
|
} |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
### |
260
|
|
|
|
|
|
|
# Widgets |
261
|
|
|
|
|
|
|
### |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub get_widget_my_displayname |
264
|
|
|
|
|
|
|
{ |
265
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
266
|
|
|
|
|
|
|
|
267
|
0
|
|
0
|
|
|
|
return $self->{widget_displayname} ||= do { |
268
|
0
|
|
|
|
|
|
my $registry = $self->{registry}; |
269
|
|
|
|
|
|
|
|
270
|
0
|
|
|
|
|
|
my $widget = $registry->construct( |
271
|
|
|
|
|
|
|
"Circle::Widget::Label", |
272
|
|
|
|
|
|
|
classes => [qw( nick )], |
273
|
|
|
|
|
|
|
); |
274
|
|
|
|
|
|
|
|
275
|
0
|
|
|
|
|
|
$widget->set_prop_text( $self->{matrix}->myself->displayname ); |
276
|
|
|
|
|
|
|
|
277
|
0
|
|
|
|
|
|
$widget; |
278
|
|
|
|
|
|
|
}; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
sub get_widget_statusbar |
282
|
|
|
|
|
|
|
{ |
283
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
284
|
|
|
|
|
|
|
|
285
|
0
|
|
|
|
|
|
my $registry = $self->{registry}; |
286
|
|
|
|
|
|
|
|
287
|
0
|
|
|
|
|
|
my $statusbar = $registry->construct( |
288
|
|
|
|
|
|
|
"Circle::Widget::Box", |
289
|
|
|
|
|
|
|
classes => [qw( status )], |
290
|
|
|
|
|
|
|
orientation => "horizontal", |
291
|
|
|
|
|
|
|
); |
292
|
|
|
|
|
|
|
|
293
|
0
|
|
|
|
|
|
$statusbar->add( $self->get_widget_netname ); |
294
|
|
|
|
|
|
|
|
295
|
0
|
|
|
|
|
|
$statusbar->add( $self->get_widget_my_displayname ); |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
# $statusbar->add( $self->get_widget_presence ); |
298
|
|
|
|
|
|
|
|
299
|
0
|
|
|
|
|
|
return $statusbar; |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=head1 AUTHOR |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
Paul Evans |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=cut |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
0x55AA; |