line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Matrix::Webhook; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: A http->matrix webhook |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
672
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
7
|
1
|
|
|
1
|
|
19
|
use 5.010; |
|
1
|
|
|
|
|
4
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = "0.900"; |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
568
|
use Net::Async::HTTP::Server::PSGI; |
|
1
|
|
|
|
|
110429
|
|
|
1
|
|
|
|
|
47
|
|
12
|
1
|
|
|
1
|
|
601
|
use Net::Async::Matrix; |
|
1
|
|
|
|
|
35553
|
|
|
1
|
|
|
|
|
31
|
|
13
|
1
|
|
|
1
|
|
831
|
use IO::Async::Loop; |
|
1
|
|
|
|
|
9951
|
|
|
1
|
|
|
|
|
36
|
|
14
|
1
|
|
|
1
|
|
532
|
use IO::Async::Timer::Countdown; |
|
1
|
|
|
|
|
1792
|
|
|
1
|
|
|
|
|
49
|
|
15
|
1
|
|
|
1
|
|
533
|
use Plack::Request; |
|
1
|
|
|
|
|
47979
|
|
|
1
|
|
|
|
|
34
|
|
16
|
1
|
|
|
1
|
|
455
|
use Plack::Response; |
|
1
|
|
|
|
|
1837
|
|
|
1
|
|
|
|
|
44
|
|
17
|
1
|
|
|
1
|
|
466
|
use Digest::SHA1 qw(sha1_hex); |
|
1
|
|
|
|
|
689
|
|
|
1
|
|
|
|
|
71
|
|
18
|
1
|
|
|
1
|
|
8
|
use Encode; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
83
|
|
19
|
1
|
|
|
1
|
|
552
|
use Log::Any qw($log); |
|
1
|
|
|
|
|
8612
|
|
|
1
|
|
|
|
|
7
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
2175
|
use base qw(Class::Accessor::Fast); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
512
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors( qw( |
24
|
|
|
|
|
|
|
matrix_home_server matrix_room matrix_user matrix_password |
25
|
|
|
|
|
|
|
http_port |
26
|
|
|
|
|
|
|
secret |
27
|
|
|
|
|
|
|
)); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
binmode( STDOUT, ':utf8' ); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub run { |
32
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my $loop = IO::Async::Loop->new; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my $matrix = Net::Async::Matrix->new( |
37
|
|
|
|
|
|
|
server => $self->matrix_home_server, |
38
|
|
|
|
|
|
|
SSL => 1, |
39
|
|
|
|
|
|
|
); |
40
|
0
|
|
|
|
|
|
$loop->add($matrix); |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
$matrix->login( |
43
|
|
|
|
|
|
|
user_id => $self->matrix_user, |
44
|
|
|
|
|
|
|
password => $self->matrix_password, |
45
|
|
|
|
|
|
|
)->get; |
46
|
0
|
|
|
|
|
|
$log->infof( "Logged in as %s at %s", $self->matrix_user, $self->matrix_home_server ); |
47
|
0
|
|
|
|
|
|
my $room = $matrix->join_room( $self->matrix_room )->get; |
48
|
0
|
|
|
|
|
|
$log->infof( "Joined room %s", $room->name ); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $httpserver = Net::Async::HTTP::Server::PSGI->new( |
51
|
|
|
|
|
|
|
app => sub { |
52
|
0
|
|
|
0
|
|
|
my $env = shift; |
53
|
0
|
|
|
|
|
|
my $req = Plack::Request->new($env); |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
my $params = $req->parameters; |
56
|
0
|
|
|
|
|
|
my $msg = decode_utf8( $params->{message} ); |
57
|
0
|
0
|
|
|
|
|
if ( $self->secret ) { |
58
|
0
|
|
|
|
|
|
my $token = $params->{token}; |
59
|
0
|
|
|
|
|
|
my $check = sha1_hex( encode_utf8($msg), $self->secret ); |
60
|
0
|
0
|
0
|
|
|
|
if ( !$token || ( $token ne $check ) ) { |
61
|
0
|
|
|
|
|
|
return Plack::Response->new( 401, undef, 'bad token' )->finalize; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
0
|
|
|
|
|
|
$log->debugf( "got message >%s<", $msg ); |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
eval { $room->send_message($msg)->get }; |
|
0
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
|
if ($@) { |
68
|
0
|
|
|
|
|
|
return Plack::Response->new( 500, undef, $@ )->finalize; |
69
|
|
|
|
|
|
|
} |
70
|
0
|
|
|
|
|
|
return Plack::Response->new( 200, undef, 'message posted to matrix' )->finalize; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
); |
73
|
0
|
|
|
|
|
|
$loop->add($httpserver); |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
$httpserver->listen( |
76
|
|
|
|
|
|
|
addr => { family => "inet", socktype => "stream", port => $self->http_port }, )->get; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
$log->infof( "Started http server at http://localhost:%s", $self->http_port ); |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
$loop->run; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
q{ listening to: Antibalas - Fu Chronicles }; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
__END__ |