|
|
@ -159,9 +159,6 @@ sub send_request { |
|
|
|
die "Missing an URI" unless $param->{uri}; |
|
|
|
die "Missing an URI" unless $param->{uri}; |
|
|
|
my $req = HTTP::Request->new( $param->{method}, $param->{uri} ); |
|
|
|
my $req = HTTP::Request->new( $param->{method}, $param->{uri} ); |
|
|
|
$req->header('Content-Type' => $param->{content_type}); |
|
|
|
$req->header('Content-Type' => $param->{content_type}); |
|
|
|
if (defined $opt->{access_token}){ |
|
|
|
|
|
|
|
$req->header('Authorization' => "Bearer $opt->{access_token}"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$req->content($param->{content}); |
|
|
|
$req->content($param->{content}); |
|
|
|
my $resp = $lwp->request( $req ); |
|
|
|
my $resp = $lwp->request( $req ); |
|
|
|
debug("Server responded:\n" . to_json(from_json($resp->decoded_content), { pretty => 1 })); |
|
|
|
debug("Server responded:\n" . to_json(from_json($resp->decoded_content), { pretty => 1 })); |
|
|
@ -211,7 +208,7 @@ sub login { |
|
|
|
# Invalidate the access_token |
|
|
|
# Invalidate the access_token |
|
|
|
sub logout { |
|
|
|
sub logout { |
|
|
|
debug("Trying to logout"); |
|
|
|
debug("Trying to logout"); |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/logout'; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/logout?access_token=' . $opt->{access_token}; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri |
|
|
|
uri => $uri |
|
|
|
}); |
|
|
|
}); |
|
|
@ -221,8 +218,8 @@ sub logout { |
|
|
|
# Join the specified room, before we can send anything |
|
|
|
# Join the specified room, before we can send anything |
|
|
|
sub join_room { |
|
|
|
sub join_room { |
|
|
|
debug("Trying to join room $opt->{room}"); |
|
|
|
debug("Trying to join room $opt->{room}"); |
|
|
|
# Room must be escaped. |
|
|
|
# Room must be escaped. if not and room is an alias, it'll start with # so the access_token won't be sent |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/join/' . uri_escape( $opt->{room} ); |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/join/' . uri_escape( $opt->{room} ) . '?access_token=' . $opt->{access_token}; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri |
|
|
|
uri => $uri |
|
|
|
}); |
|
|
|
}); |
|
|
@ -232,7 +229,7 @@ sub join_room { |
|
|
|
# Retrieve the actual permissions for a room |
|
|
|
# Retrieve the actual permissions for a room |
|
|
|
sub get_room_permissions { |
|
|
|
sub get_room_permissions { |
|
|
|
debug('Getting actual room state'); |
|
|
|
debug('Getting actual room state'); |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels'; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels?access_token=' . $opt->{access_token}; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
method => 'GET', |
|
|
|
method => 'GET', |
|
|
|
uri => $uri |
|
|
|
uri => $uri |
|
|
@ -253,7 +250,7 @@ sub who_am_i { |
|
|
|
|
|
|
|
|
|
|
|
# Send a text message (either message or notice as both are similar) |
|
|
|
# Send a text message (either message or notice as both are similar) |
|
|
|
sub send_msg { |
|
|
|
sub send_msg { |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message'; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message?access_token=' . $opt->{access_token}; |
|
|
|
# Ignore --message if reading from stdin |
|
|
|
# Ignore --message if reading from stdin |
|
|
|
if ($stdin){ |
|
|
|
if ($stdin){ |
|
|
|
$opt->{message} = ''; |
|
|
|
$opt->{message} = ''; |
|
|
@ -285,7 +282,7 @@ sub send_file { |
|
|
|
# Sending a file is a 2 steps operation. First we need to upload the file to the media store |
|
|
|
# Sending a file is a 2 steps operation. First we need to upload the file to the media store |
|
|
|
# And then we post the uri on the room |
|
|
|
# And then we post the uri on the room |
|
|
|
debug("Uploading file $file to the media store"); |
|
|
|
debug("Uploading file $file to the media store"); |
|
|
|
my $uri = $opt->{server} . '/_matrix/media/v1/upload?filename=' . basename($file); |
|
|
|
my $uri = $opt->{server} . '/_matrix/media/v1/upload?access_token=' . $opt->{access_token} . '&filename=' . basename($file); |
|
|
|
my $mime = mimetype($file); |
|
|
|
my $mime = mimetype($file); |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri, |
|
|
|
uri => $uri, |
|
|
@ -299,7 +296,7 @@ sub send_file { |
|
|
|
die "Server did not sent the file URI\n" unless ($file_uri); |
|
|
|
die "Server did not sent the file URI\n" unless ($file_uri); |
|
|
|
debug("File uploaded, with the URI $file_uri\nNow Sending the file link to the room $opt->{room}"); |
|
|
|
debug("File uploaded, with the URI $file_uri\nNow Sending the file link to the room $opt->{room}"); |
|
|
|
# Now lets post a new message with the URI of the file |
|
|
|
# Now lets post a new message with the URI of the file |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message'; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message?access_token=' . $opt->{access_token}; |
|
|
|
my $json = { |
|
|
|
my $json = { |
|
|
|
msgtype => 'm.file', |
|
|
|
msgtype => 'm.file', |
|
|
|
body => basename($file), |
|
|
|
body => basename($file), |
|
|
@ -331,7 +328,7 @@ sub send_file { |
|
|
|
# Note that there's no pagination handling yet, so you might not have all the results |
|
|
|
# Note that there's no pagination handling yet, so you might not have all the results |
|
|
|
sub list_room { |
|
|
|
sub list_room { |
|
|
|
debug("Fetching list of public rooms on $opt->{server}"); |
|
|
|
debug("Fetching list of public rooms on $opt->{server}"); |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/publicRooms'; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/publicRooms?access_token=' . $opt->{access_token}; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri, |
|
|
|
uri => $uri, |
|
|
|
}); |
|
|
|
}); |
|
|
@ -349,7 +346,7 @@ sub list_room { |
|
|
|
# Create a new room |
|
|
|
# Create a new room |
|
|
|
sub create_room { |
|
|
|
sub create_room { |
|
|
|
debug("Creating a new room on $opt->{server}"); |
|
|
|
debug("Creating a new room on $opt->{server}"); |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/createRoom'; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/createRoom?access_token=' . $opt->{access_token}; |
|
|
|
my $json = {}; |
|
|
|
my $json = {}; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri, |
|
|
|
uri => $uri, |
|
|
@ -369,7 +366,7 @@ sub modify_room { |
|
|
|
# A new alias should be added |
|
|
|
# A new alias should be added |
|
|
|
if ($opt->{alias}){ |
|
|
|
if ($opt->{alias}){ |
|
|
|
debug('Adding ' . $opt->{alias} . ' as a room alias'); |
|
|
|
debug('Adding ' . $opt->{alias} . ' as a room alias'); |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/directory/room/' . uri_escape($opt->{alias}); |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/directory/room/' . uri_escape($opt->{alias}) . '?access_token=' . $opt->{access_token}; |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
room_id => $opt->{room} |
|
|
|
room_id => $opt->{room} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -384,7 +381,7 @@ sub modify_room { |
|
|
|
# The name of the room is being updated |
|
|
|
# The name of the room is being updated |
|
|
|
if ($opt->{name}){ |
|
|
|
if ($opt->{name}){ |
|
|
|
debug('Changing the room name to ' . $opt->{name}); |
|
|
|
debug('Changing the room name to ' . $opt->{name}); |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.name'; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.name?access_token=' . $opt->{access_token}; |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
name => $opt->{name} |
|
|
|
name => $opt->{name} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -399,7 +396,7 @@ sub modify_room { |
|
|
|
# The topic is being updated |
|
|
|
# The topic is being updated |
|
|
|
if ($opt->{topic}){ |
|
|
|
if ($opt->{topic}){ |
|
|
|
debug('Changing the room topic to ' . $opt->{topic}); |
|
|
|
debug('Changing the room topic to ' . $opt->{topic}); |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.topic'; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.topic?access_token=' . $opt->{access_token}; |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
topic => $opt->{topic} |
|
|
|
topic => $opt->{topic} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -414,7 +411,7 @@ sub modify_room { |
|
|
|
# Changing joining rules |
|
|
|
# Changing joining rules |
|
|
|
if ($opt->{join_rules}){ |
|
|
|
if ($opt->{join_rules}){ |
|
|
|
debug('Changing the joining rules to '. $opt->{join_rules}); |
|
|
|
debug('Changing the joining rules to '. $opt->{join_rules}); |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.join_rules'; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.join_rules?access_token=' . $opt->{access_token}; |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
join_rules => $opt->{join_rules} |
|
|
|
join_rules => $opt->{join_rules} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -478,7 +475,7 @@ sub modify_room { |
|
|
|
} |
|
|
|
} |
|
|
|
my $perm = merge($current_perm, $new_perm); |
|
|
|
my $perm = merge($current_perm, $new_perm); |
|
|
|
debug("New permissions for this room will be:\n" . to_json($perm, { pretty => 1 })); |
|
|
|
debug("New permissions for this room will be:\n" . to_json($perm, { pretty => 1 })); |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels'; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels?access_token=' . $opt->{access_token}; |
|
|
|
$resp = send_request({ |
|
|
|
$resp = send_request({ |
|
|
|
method => 'PUT', |
|
|
|
method => 'PUT', |
|
|
|
uri => $uri, |
|
|
|
uri => $uri, |
|
|
@ -490,7 +487,7 @@ sub modify_room { |
|
|
|
# New invitees should be added |
|
|
|
# New invitees should be added |
|
|
|
if ($opt->{invite}){ |
|
|
|
if ($opt->{invite}){ |
|
|
|
debug('Inviting ' . join(',', @{$opt->{invite}}) . ' to join the room'); |
|
|
|
debug('Inviting ' . join(',', @{$opt->{invite}}) . ' to join the room'); |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/invite'; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/invite?access_token=' . $opt->{access_token}; |
|
|
|
foreach my $invite (@{$opt->{invite}}){ |
|
|
|
foreach my $invite (@{$opt->{invite}}){ |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
user_id => $invite |
|
|
|
user_id => $invite |
|
|
@ -514,7 +511,7 @@ sub modify_room { |
|
|
|
|
|
|
|
|
|
|
|
sub del_room_alias { |
|
|
|
sub del_room_alias { |
|
|
|
debug("Removing room alias $opt->{alias}"); |
|
|
|
debug("Removing room alias $opt->{alias}"); |
|
|
|
my $uri = $opt->{server} . "/_matrix/client/r0/directory/room/" . uri_escape($opt->{alias}); |
|
|
|
my $uri = $opt->{server} . "/_matrix/client/r0/directory/room/" . uri_escape($opt->{alias}) . '?access_token=' . $opt->{access_token}; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
method => 'DELETE', |
|
|
|
method => 'DELETE', |
|
|
|
uri => $uri |
|
|
|
uri => $uri |
|
|
|