| line | stmt | bran | cond | sub | pod | time | code | 
| 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 |  |  | 1 |  | 41320 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 9 | 1 |  |  | 1 |  | 7 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 44 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 1 |  |  | 1 |  | 6 | use Future; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 20 |  | 
| 14 | 1 |  |  | 1 |  | 889 | use Net::Async::Matrix; | 
|  | 1 |  |  |  |  | 43871 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 15 | 1 |  |  | 1 |  | 8 | use curry; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 1036 |  | 
| 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 |  |  |  |  |  |  | { | 
| 53 | 0 |  |  | 0 | 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 ) ), | 
|  | 0 |  |  |  |  |  |  | 
| 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( | 
|  | 0 |  |  |  |  |  |  | 
| 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 |  |  |  |  |  |  | on_presence => sub { | 
| 92 | 0 |  |  | 0 |  |  | my ( undef, $user, %changes ) = @_; | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 | 0 |  |  |  |  | if( exists $changes{presence} ) { | 
|  |  | 0 |  |  |  |  |  | 
| 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 |  |  |  |  |  |  | on_room_new => sub { | 
| 102 | 0 |  |  | 0 |  |  | my ( undef, $room ) = @_; | 
| 103 | 0 |  |  |  |  |  | $dist->fire_sync( on_room_new => $room ); | 
| 104 |  |  |  |  |  |  | }, | 
| 105 |  |  |  |  |  |  | on_room_del => sub { | 
| 106 | 0 |  |  | 0 |  |  | my ( undef, $room ) = @_; | 
| 107 | 0 |  |  |  |  |  | $dist->fire_sync( on_room_del => $room ); | 
| 108 |  |  |  |  |  |  | }, | 
| 109 |  |  |  |  |  |  | on_error => sub { | 
| 110 | 0 |  |  | 0 |  |  | my ( undef, $failure, $name ) = @_; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  |  |  |  | $dist->fire_sync( logerr => "Error: $failure" ); | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 | 0 |  |  |  |  | if( $name eq "http" ) { | 
| 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 |  |  |  |  |  |  | on_invite => sub { | 
| 122 | 0 |  |  | 0 |  |  | my ( undef, $event ) = @_; | 
| 123 | 0 |  |  |  |  |  | $dist->fire_sync( on_invite => $event ); | 
| 124 |  |  |  |  |  |  | }, | 
| 125 | 0 | 0 |  |  |  |  | ); | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 0 |  |  |  |  |  | $loop->add( $self->{matrix} ); | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 |  |  |  |  |  | return $self; | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | # Signal handlers | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | sub start | 
| 135 |  |  |  |  |  |  | { | 
| 136 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | defined $self->{user_id} or | 
| 139 | 0 | 0 |  |  |  |  | return Future->done; | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 0 |  |  |  |  |  | print STDERR "Logging in as $self->{user_id}...\n"; | 
| 142 |  |  |  |  |  |  | $self->{matrix}->login( | 
| 143 | 0 |  |  |  |  |  | map { $_ => $self->{$_} } qw( user_id password ) | 
|  | 0 |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | ); | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | sub do_upload | 
| 148 |  |  |  |  |  |  | { | 
| 149 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | { | 
| 160 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | { | 
| 168 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | { | 
| 176 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | { | 
| 184 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | { | 
| 192 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | { | 
| 200 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | { | 
| 208 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | { | 
| 216 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | { | 
| 224 | 0 |  |  | 0 | 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 |  |  |  |  |  |  | 0x55AA; |