123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- use strict;
- use warnings;
- use 5.010;
- use IO::Socket::SSL qw(SSL_VERIFY_NONE);
- use IO::Async::Loop;
- use Net::Async::WebSocket::Client;
- use Net::Async::HTTP;
- use Net::Async::HTTP::Server;
- use JSON;
- use YAML;
- use Data::UUID;
- use Getopt::Long;
- use Data::Dumper;
- use URI::Encode qw(uri_encode uri_decode);
-
- binmode STDOUT, ":encoding(UTF-8)";
- binmode STDERR, ":encoding(UTF-8)";
- my $msisdn_to_matrix = {
- '447417892400' => '@matthew:matrix.org',
- };
- my $matrix_to_msisdn = {};
- foreach (keys %$msisdn_to_matrix) {
- $matrix_to_msisdn->{$msisdn_to_matrix->{$_}} = $_;
- }
- my $loop = IO::Async::Loop->new;
- GetOptions(
- 'C|config=s' => \my $CONFIG,
- 'eval-from=s' => \my $EVAL_FROM,
- ) or exit 1;
- if( defined $EVAL_FROM ) {
-
- $SIG{HUP} = sub {
- my $code = do {
- open my $fh, "<", $EVAL_FROM or warn( "Cannot read - $!" ), return;
- local $/; <$fh>
- };
- eval $code or warn "Cannot eval() - $@";
- };
- }
- defined $CONFIG or die "Must supply --config\n";
- my %CONFIG = %{ YAML::LoadFile( $CONFIG ) };
- my %MATRIX_CONFIG = %{ $CONFIG{matrix} };
- $MATRIX_CONFIG{SSL_verify_mode} = SSL_VERIFY_NONE;
- my $bridgestate = {};
- my $roomid_by_callid = {};
-
- my $sessid = lc new Data::UUID->create_str();
- my $as_token = $CONFIG{"matrix-bot"}->{as_token};
- my $hs_domain = $CONFIG{"matrix-bot"}->{domain};
- my $http = Net::Async::HTTP->new();
- $loop->add( $http );
- sub create_virtual_user
- {
- my ($localpart) = @_;
- my ( $response ) = $http->do_request(
- method => "POST",
- uri => URI->new(
- $CONFIG{"matrix"}->{server}.
- "/_matrix/client/api/v1/register?".
- "access_token=$as_token&user_id=$localpart"
- ),
- content_type => "application/json",
- content => <<EOT
- {
- "type": "m.login.application_service",
- "user": "$localpart"
- }
- EOT
- )->get;
- warn $response->as_string if ($response->code != 200);
- }
-
- my $http_server = Net::Async::HTTP::Server->new(
- on_request => sub {
- my $self = shift;
- my ( $req ) = @_;
- my $response;
- my $path = uri_decode($req->path);
- warn("request: $path");
- if ($path =~ m
-
- my $localpart = $1;
- create_virtual_user($localpart);
- $response = HTTP::Response->new( 200 );
- $response->add_content('{}');
- $response->content_type( "application/json" );
- }
- elsif ($path =~ m
- my $event = JSON->new->decode($req->body);
- print Dumper($event);
- my $room_id = $event->{room_id};
- my %dp = %{$CONFIG{'verto-dialog-params'}};
- $dp{callID} = $bridgestate->{$room_id}->{callid};
- if ($event->{type} eq 'm.room.membership') {
- my $membership = $event->{content}->{membership};
- my $state_key = $event->{state_key};
- my $room_id = $event->{state_id};
-
- if ($membership eq 'invite') {
-
- my ( $response ) = $http->do_request(
- method => "POST",
- uri => URI->new(
- $CONFIG{"matrix"}->{server}.
- "/_matrix/client/api/v1/rooms/$room_id/join?".
- "access_token=$as_token&user_id=$state_key"
- ),
- content_type => "application/json",
- content => "{}",
- )->get;
- warn $response->as_string if ($response->code != 200);
- }
- }
- elsif ($event->{type} eq 'm.call.invite') {
- my $room_id = $event->{room_id};
- $bridgestate->{$room_id}->{matrix_callid} = $event->{content}->{call_id};
- $bridgestate->{$room_id}->{callid} = lc new Data::UUID->create_str();
- $bridgestate->{$room_id}->{sessid} = $sessid;
-
- my $offer = $event->{content}->{offer}->{sdp};
-
- $roomid_by_callid->{ $bridgestate->{$room_id}->{callid} } = $room_id;
-
- my $f = send_verto_json_request("verto.invite", {
- "sdp" => $offer,
- "dialogParams" => \%dp,
- "sessid" => $bridgestate->{$room_id}->{sessid},
- });
- $self->adopt_future($f);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- elsif ($event->{type} eq 'm.call.answer') {
-
- my $room_id = $event->{room_id};
-
- my $answer = $event->{content}->{answer}->{sdp};
- my $f = send_verto_json_request("verto.answer", {
- "sdp" => $answer,
- "dialogParams" => \%dp,
- "sessid" => $bridgestate->{$room_id}->{sessid},
- });
- $self->adopt_future($f);
- }
- elsif ($event->{type} eq 'm.call.hangup') {
- my $room_id = $event->{room_id};
- if ($bridgestate->{$room_id}->{matrix_callid} eq $event->{content}->{call_id}) {
- my $f = send_verto_json_request("verto.bye", {
- "dialogParams" => \%dp,
- "sessid" => $bridgestate->{$room_id}->{sessid},
- });
- $self->adopt_future($f);
- }
- else {
- warn "Ignoring unrecognised callid: ".$event->{content}->{call_id};
- }
- }
- else {
- warn "Unhandled event: $event->{type}";
- }
-
- $response = HTTP::Response->new( 200 );
- $response->add_content('{}');
- $response->content_type( "application/json" );
- }
- else {
- warn "Unhandled path: $path";
- $response = HTTP::Response->new( 404 );
- }
- $req->respond( $response );
- },
- );
- $loop->add( $http_server );
- $http_server->listen(
- addr => { family => "inet", socktype => "stream", port => 8009 },
- on_listen_error => sub { die "Cannot listen - $_[-1]\n" },
- );
- my $bot_verto = Net::Async::WebSocket::Client->new(
- on_frame => sub {
- my ( $self, $frame ) = @_;
- warn "[Verto] receiving $frame";
- on_verto_json($frame);
- },
- );
- $loop->add( $bot_verto );
- my $verto_connecting = $loop->new_future;
- $bot_verto->connect(
- %{ $CONFIG{"verto-bot"} },
- on_connected => sub {
- warn("[Verto] connected to websocket");
- if (not $verto_connecting->is_done) {
- $verto_connecting->done($bot_verto);
- send_verto_json_request("login", {
- 'login' => $CONFIG{'verto-dialog-params'}{'login'},
- 'passwd' => $CONFIG{'verto-config'}{'passwd'},
- 'sessid' => $sessid,
- });
- }
- },
- on_connect_error => sub { die "Cannot connect to verto - $_[-1]" },
- on_resolve_error => sub { die "Cannot resolve to verto - $_[-1]" },
- );
- my $as_url = $CONFIG{"matrix-bot"}->{as_url};
- Future->needs_all(
- $http->do_request(
- method => "POST",
- uri => URI->new( $CONFIG{"matrix"}->{server}."/_matrix/appservice/v1/register" ),
- content_type => "application/json",
- content => <<EOT
- {
- "as_token": "$as_token",
- "url": "$as_url",
- "namespaces": { "users": [ { "regex": "\@\\\\+.*", "exclusive": false } ] }
- }
- EOT
- )->then( sub{
- my ($response) = (@_);
- warn $response->as_string if ($response->code != 200);
- return Future->done;
- }),
- $verto_connecting,
- )->get;
- $loop->attach_signal(
- PIPE => sub { warn "pipe\n" }
- );
- $loop->attach_signal(
- INT => sub { $loop->stop },
- );
- $loop->attach_signal(
- TERM => sub { $loop->stop },
- );
- eval {
- $loop->run;
- } or my $e = $@;
- die $e if $e;
- exit 0;
- {
- my $json_id;
- my $requests;
- sub send_verto_json_request
- {
- $json_id ||= 1;
-
- my ($method, $params) = @_;
- my $json = {
- jsonrpc => "2.0",
- method => $method,
- params => $params,
- id => $json_id,
- };
- my $text = JSON->new->encode( $json );
- warn "[Verto] sending $text";
- $bot_verto->send_frame ( $text );
- my $request = $loop->new_future;
- $requests->{$json_id} = $request;
- $json_id++;
- return $request;
- }
-
- sub send_verto_json_response
- {
- my ($result, $id) = @_;
- my $json = {
- jsonrpc => "2.0",
- result => $result,
- id => $id,
- };
- my $text = JSON->new->encode( $json );
- warn "[Verto] sending $text";
- $bot_verto->send_frame ( $text );
- }
-
- sub on_verto_json
- {
- my $json = JSON->new->decode( $_[0] );
- if ($json->{method}) {
- if (($json->{method} eq 'verto.answer' && $json->{params}->{sdp}) ||
- $json->{method} eq 'verto.media') {
- my $caller = $json->{dialogParams}->{caller_id_number};
- my $callee = $json->{dialogParams}->{destination_number};
- my $caller_user = '@+' . $caller . ':' . $hs_domain;
- my $callee_user = $msisdn_to_matrix->{$callee} || warn "unrecogised callee: $callee";
- my $room_id = $roomid_by_callid->{$json->{params}->{callID}};
- if ($json->{params}->{sdp}) {
- $http->do_request(
- method => "POST",
- uri => URI->new(
- $CONFIG{"matrix"}->{server}.
- "/_matrix/client/api/v1/send/m.call.answer?".
- "access_token=$as_token&user_id=$caller_user"
- ),
- content_type => "application/json",
- content => JSON->new->encode({
- call_id => $bridgestate->{$room_id}->{matrix_callid},
- version => 0,
- answer => {
- sdp => $json->{params}->{sdp},
- type => "answer",
- },
- }),
- )->then( sub {
- send_verto_json_response( {
- method => $json->{method},
- }, $json->{id});
- })->get;
- }
- }
- elsif ($json->{method} eq 'verto.invite') {
- my $caller = $json->{dialogParams}->{caller_id_number};
- my $callee = $json->{dialogParams}->{destination_number};
- my $caller_user = '@+' . $caller . ':' . $hs_domain;
- my $callee_user = $msisdn_to_matrix->{$callee} || warn "unrecogised callee: $callee";
-
- my $alias = ($caller lt $callee) ? ($caller.'-'.$callee) : ($callee.'-'.$caller);
- my $room_id;
-
- create_virtual_user($caller);
-
-
- $http->do_request(
- method => "POST",
- uri => URI->new(
- $CONFIG{"matrix"}->{server}.
- "/_matrix/client/api/v1/createRoom?".
- "access_token=$as_token&user_id=$caller_user"
- ),
- content_type => "application/json",
- content => JSON->new->encode({
- room_alias_name => $alias,
- invite => [ $callee_user ],
- }),
- )->then( sub {
- my ( $response ) = @_;
- my $resp = JSON->new->decode($response->content);
- $room_id = $resp->{room_id};
- $roomid_by_callid->{$json->{params}->{callID}} = $room_id;
- })->get;
-
- my ($response) = $http->do_request(
- method => "POST",
- uri => URI->new(
- $CONFIG{"matrix"}->{server}.
- "/_matrix/client/api/v1/join/$room_id?".
- "access_token=$as_token&user_id=$caller_user"
- ),
- content_type => "application/json",
- content => '{}',
- )->get;
- $bridgestate->{$room_id}->{matrix_callid} = lc new Data::UUID->create_str();
- $bridgestate->{$room_id}->{callid} = $json->{dialogParams}->{callID};
- $bridgestate->{$room_id}->{sessid} = $sessid;
-
- $http->do_request(
- method => "POST",
- uri => URI->new(
- $CONFIG{"matrix"}->{server}.
- "/_matrix/client/api/v1/send/m.call.invite?".
- "access_token=$as_token&user_id=$caller_user"
- ),
- content_type => "application/json",
- content => JSON->new->encode({
- call_id => $bridgestate->{$room_id}->{matrix_callid},
- version => 0,
- answer => {
- sdp => $json->{params}->{sdp},
- type => "offer",
- },
- }),
- )->then( sub {
-
- send_verto_json_response( {
- method => $json->{method},
- }, $json->{id});
- })->get;
- }
- elsif ($json->{method} eq 'verto.bye') {
- my $caller = $json->{dialogParams}->{caller_id_number};
- my $callee = $json->{dialogParams}->{destination_number};
- my $caller_user = '@+' . $caller . ':' . $hs_domain;
- my $callee_user = $msisdn_to_matrix->{$callee} || warn "unrecogised callee: $callee";
- my $room_id = $roomid_by_callid->{$json->{params}->{callID}};
-
-
- $http->do_request(
- method => "POST",
- uri => URI->new(
- $CONFIG{"matrix"}->{server}.
- "/_matrix/client/api/v1/send/m.call.hangup?".
- "access_token=$as_token&user_id=$caller_user"
- ),
- content_type => "application/json",
- content => JSON->new->encode({
- call_id => $bridgestate->{$room_id}->{matrix_callid},
- version => 0,
- }),
- )->then( sub {
-
- send_verto_json_response( {
- method => $json->{method},
- }, $json->{id});
- })->get;
- }
- else {
- warn ("[Verto] unhandled method: " . $json->{method});
- send_verto_json_response( {
- method => $json->{method},
- }, $json->{id});
- }
- }
- elsif ($json->{result}) {
- $requests->{$json->{id}}->done($json->{result});
- }
- elsif ($json->{error}) {
- $requests->{$json->{id}}->fail($json->{error}->{message}, $json->{error});
- }
- }
- }
|