| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # (C) Paul Evans, 2015 -- leonerd@leonerd.org.uk | ||||||||||||||
| 5 | |||||||||||||||
| 6 | package App::MatrixClient::Matrix; | ||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | our $VERSION = '0.01'; | |||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | ▶ | use Future; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 1 | ▶ | use Net::Async::Matrix; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 1 | ▶ | use curry; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | =head1 EMITTED SIGNALS | ||||||||||||||
| 18 | |||||||||||||||
| 19 | log( $str ) | ||||||||||||||
| 20 | logerr( $str ) | ||||||||||||||
| 21 | |||||||||||||||
| 22 | * on_invite( $event ) | ||||||||||||||
| 23 | |||||||||||||||
| 24 | * on_user_displayname( $user, $oldname ) | ||||||||||||||
| 25 | * on_user_presence( $user ) | ||||||||||||||
| 26 | |||||||||||||||
| 27 | * on_room_new( $room ) | ||||||||||||||
| 28 | * on_room_del( $room ) | ||||||||||||||
| 29 | |||||||||||||||
| 30 | =head1 CONSUMED SIGNALS | ||||||||||||||
| 31 | |||||||||||||||
| 32 | start | ||||||||||||||
| 33 | |||||||||||||||
| 34 | * do_upload( file => $file, content_type => $type ) | ||||||||||||||
| 35 | |||||||||||||||
| 36 | * do_get_displayname( ?$user_id ) => $displayname | ||||||||||||||
| 37 | * do_set_displayname( $displayname ) | ||||||||||||||
| 38 | |||||||||||||||
| 39 | * do_set_presence( $state, ?$msg ) | ||||||||||||||
| 40 | |||||||||||||||
| 41 | * do_add_alias( $alias, $room_id ) | ||||||||||||||
| 42 | * do_del_alias( $alias ) | ||||||||||||||
| 43 | |||||||||||||||
| 44 | * do_room_create( $name ) => $event | ||||||||||||||
| 45 | * do_room_join( $name ) | ||||||||||||||
| 46 | * do_room_leave( $room_id ) | ||||||||||||||
| 47 | * do_room_msg( $room_id, $message ) | ||||||||||||||
| 48 | |||||||||||||||
| 49 | =cut | ||||||||||||||
| 50 | |||||||||||||||
| 51 | sub new | ||||||||||||||
| 52 | 0 | ▶ | { | ||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 53 | 0 | my $class = shift; | |||||||||||||
| 54 | 0 | my %args = @_; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | my $loop = $args{loop}; | |||||||||||||
| 57 | 0 | my $dist = $args{dist}; | |||||||||||||
| 58 | |||||||||||||||
| 59 | my $self = bless { | ||||||||||||||
| 60 | loop => $loop, | ||||||||||||||
| 61 | dist => $dist, | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | ( map { $_ => $args{$_} } qw( user_id password ) ), | |||||||||||||
| 64 | }, $class; | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 0 | $dist->declare_signal( $_ ) for | |||||||||||||
| 67 | qw( on_invite on_user_displayname on_user_presence on_room_new on_room_del ); | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 0 | $dist->declare_action( $_ ) for qw( | |||||||||||||
| 70 | do_upload do_get_displayname do_set_displayname do_set_presence | ||||||||||||||
| 71 | do_add_alias do_del_alias | ||||||||||||||
| 72 | do_room_create do_room_join do_room_leave do_room_msg | ||||||||||||||
| 73 | ); | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | $dist->subscribe_async( $_ => $self->${\"curry::$_"} ) for qw( | |||||||||||||
| 76 | start | ||||||||||||||
| 77 | do_upload do_get_displayname do_set_displayname do_set_presence | ||||||||||||||
| 78 | do_add_alias do_del_alias | ||||||||||||||
| 79 | do_room_create do_room_join do_room_leave do_room_msg | ||||||||||||||
| 80 | ); | ||||||||||||||
| 81 | |||||||||||||||
| 82 | $self->{matrix} = Net::Async::Matrix->new( | ||||||||||||||
| 83 | server => $args{server}, | ||||||||||||||
| 84 | ( $args{ssl} ? ( | ||||||||||||||
| 85 | SSL => 1, | ||||||||||||||
| 86 | SSL_verify_mode => 0, | ||||||||||||||
| 87 | ) : () ), | ||||||||||||||
| 88 | |||||||||||||||
| 89 | on_log => $dist->curry::fire_sync( 'log' ), | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 0 | ▶ | on_presence => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||
| 92 | 0 | my ( undef, $user, %changes ) = @_; | |||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | ▶ | if( exists $changes{presence} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 95 | 0 | $dist->fire_sync( on_user_presence => $user ); | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | elsif( exists $changes{displayname} ) { | ||||||||||||||
| 98 | 0 | $dist->fire_sync( on_user_displayname => $user, $changes{username}[0] ); | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | }, | ||||||||||||||
| 101 | 0 | ▶ | on_room_new => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 102 | 0 | my ( undef, $room ) = @_; | |||||||||||||
| 103 | 0 | $dist->fire_sync( on_room_new => $room ); | |||||||||||||
| 104 | }, | ||||||||||||||
| 105 | 0 | ▶ | on_room_del => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 106 | 0 | my ( undef, $room ) = @_; | |||||||||||||
| 107 | 0 | $dist->fire_sync( on_room_del => $room ); | |||||||||||||
| 108 | }, | ||||||||||||||
| 109 | 0 | ▶ | on_error => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 110 | 0 | my ( undef, $failure, $name ) = @_; | |||||||||||||
| 111 | |||||||||||||||
| 112 | 0 | $dist->fire_sync( logerr => "Error: $failure" ); | |||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | ▶ | if( $name eq "http" ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | 0 | my ( undef, undef, undef, $response, $request ) = @_; | |||||||||||||
| 116 | 0 | $dist->fire_sync( logerr => " ".$request->uri->path_query ); | |||||||||||||
| 117 | 0 | $dist->fire_sync( logerr => " ".$response->content ); | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | }, | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | ▶ | on_invite => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 122 | 0 | my ( undef, $event ) = @_; | |||||||||||||
| 123 | 0 | $dist->fire_sync( on_invite => $event ); | |||||||||||||
| 124 | }, | ||||||||||||||
| 125 | 0 | ▶ | ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | |||||||||||||||
| 127 | 0 | $loop->add( $self->{matrix} ); | |||||||||||||
| 128 | |||||||||||||||
| 129 | 0 | return $self; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | # Signal handlers | ||||||||||||||
| 133 | |||||||||||||||
| 134 | sub start | ||||||||||||||
| 135 | 0 | ▶ | { | ||||||||||||
|
Subroutine
start: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 136 | 0 | my $self = shift; | |||||||||||||
| 137 | |||||||||||||||
| 138 | defined $self->{user_id} or | ||||||||||||||
| 139 | 0 | ▶ | return Future->done; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 140 | |||||||||||||||
| 141 | 0 | print STDERR "Logging in as $self->{user_id}...\n"; | |||||||||||||
| 142 | $self->{matrix}->login( | ||||||||||||||
| 143 | 0 | map { $_ => $self->{$_} } qw( user_id password ) | |||||||||||||
| 144 | ); | ||||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | sub do_upload | ||||||||||||||
| 148 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_upload: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 149 | 0 | my $self = shift; | |||||||||||||
| 150 | 0 | my ( undef, %args ) = @_; | |||||||||||||
| 151 | |||||||||||||||
| 152 | $self->{matrix}->upload( | ||||||||||||||
| 153 | file => $args{file}, | ||||||||||||||
| 154 | content_type => $args{content_type}, | ||||||||||||||
| 155 | 0 | ); | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | sub do_get_displayname | ||||||||||||||
| 159 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_get_displayname: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 160 | 0 | my $self = shift; | |||||||||||||
| 161 | 0 | my ( undef, $user_id ) = @_; | |||||||||||||
| 162 | |||||||||||||||
| 163 | 0 | $self->{matrix}->get_displayname( $user_id ); | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | |||||||||||||||
| 166 | sub do_set_displayname | ||||||||||||||
| 167 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_set_displayname: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 168 | 0 | my $self = shift; | |||||||||||||
| 169 | 0 | my ( undef, $displayname ) = @_; | |||||||||||||
| 170 | |||||||||||||||
| 171 | 0 | $self->{matrix}->set_displayname( $displayname ); | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | |||||||||||||||
| 174 | sub do_set_presence | ||||||||||||||
| 175 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_set_presence: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 176 | 0 | my $self = shift; | |||||||||||||
| 177 | 0 | my ( undef, $state, $msg ) = @_; | |||||||||||||
| 178 | |||||||||||||||
| 179 | 0 | $self->{matrix}->set_presence( $state, $msg ); | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | sub do_add_alias | ||||||||||||||
| 183 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_add_alias: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 184 | 0 | my $self = shift; | |||||||||||||
| 185 | 0 | my ( undef, $alias, $room_id ) = @_; | |||||||||||||
| 186 | |||||||||||||||
| 187 | 0 | $self->{matrix}->add_alias( $alias, $room_id ); | |||||||||||||
| 188 | } | ||||||||||||||
| 189 | |||||||||||||||
| 190 | sub do_del_alias | ||||||||||||||
| 191 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_del_alias: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 192 | 0 | my $self = shift; | |||||||||||||
| 193 | 0 | my ( undef, $alias ) = @_; | |||||||||||||
| 194 | |||||||||||||||
| 195 | 0 | $self->{matrix}->delete_alias( $alias ); | |||||||||||||
| 196 | } | ||||||||||||||
| 197 | |||||||||||||||
| 198 | sub do_room_create | ||||||||||||||
| 199 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_room_create: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 200 | 0 | my $self = shift; | |||||||||||||
| 201 | 0 | my ( undef, $name ) = @_; | |||||||||||||
| 202 | |||||||||||||||
| 203 | 0 | $self->{matrix}->create_room( $name ); | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | sub do_room_join | ||||||||||||||
| 207 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_room_join: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 208 | 0 | my $self = shift; | |||||||||||||
| 209 | 0 | my ( undef, $name ) = @_; | |||||||||||||
| 210 | |||||||||||||||
| 211 | 0 | $self->{matrix}->join_room( $name ); | |||||||||||||
| 212 | } | ||||||||||||||
| 213 | |||||||||||||||
| 214 | sub do_room_leave | ||||||||||||||
| 215 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_room_leave: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 216 | 0 | my $self = shift; | |||||||||||||
| 217 | 0 | my ( undef, $room_id ) = @_; | |||||||||||||
| 218 | |||||||||||||||
| 219 | 0 | $self->{matrix}->leave_room( $room_id ); | |||||||||||||
| 220 | } | ||||||||||||||
| 221 | |||||||||||||||
| 222 | sub do_room_msg | ||||||||||||||
| 223 | 0 | ▶ | { | ||||||||||||
|
Subroutine
do_room_msg: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 224 | 0 | my $self = shift; | |||||||||||||
| 225 | 0 | my ( undef, $room_id, $msg ) = @_; | |||||||||||||
| 226 | |||||||||||||||
| 227 | 0 | $self->{matrix}->send_room_message( $room_id, $msg ); | |||||||||||||
| 228 | } | ||||||||||||||
| 229 | |||||||||||||||
| 230 | 1 | 0x55AA; | |||||||||||||