|
|
@ -159,6 +159,9 @@ 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 })); |
|
|
@ -208,7 +211,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?access_token=' . $opt->{access_token}; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/logout'; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri |
|
|
|
uri => $uri |
|
|
|
}); |
|
|
|
}); |
|
|
@ -218,8 +221,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. if not and room is an alias, it'll start with # so the access_token won't be sent |
|
|
|
# Room must be escaped. |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/join/' . uri_escape( $opt->{room} ) . '?access_token=' . $opt->{access_token}; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/join/' . uri_escape( $opt->{room} ); |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri |
|
|
|
uri => $uri |
|
|
|
}); |
|
|
|
}); |
|
|
@ -229,7 +232,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?access_token=' . $opt->{access_token}; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels'; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
method => 'GET', |
|
|
|
method => 'GET', |
|
|
|
uri => $uri |
|
|
|
uri => $uri |
|
|
@ -250,7 +253,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?access_token=' . $opt->{access_token}; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message'; |
|
|
|
# Ignore --message if reading from stdin |
|
|
|
# Ignore --message if reading from stdin |
|
|
|
if ($stdin){ |
|
|
|
if ($stdin){ |
|
|
|
$opt->{message} = ''; |
|
|
|
$opt->{message} = ''; |
|
|
@ -282,7 +285,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?access_token=' . $opt->{access_token} . '&filename=' . basename($file); |
|
|
|
my $uri = $opt->{server} . '/_matrix/media/v1/upload?filename=' . basename($file); |
|
|
|
my $mime = mimetype($file); |
|
|
|
my $mime = mimetype($file); |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri, |
|
|
|
uri => $uri, |
|
|
@ -296,7 +299,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?access_token=' . $opt->{access_token}; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message'; |
|
|
|
my $json = { |
|
|
|
my $json = { |
|
|
|
msgtype => 'm.file', |
|
|
|
msgtype => 'm.file', |
|
|
|
body => basename($file), |
|
|
|
body => basename($file), |
|
|
@ -328,7 +331,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?access_token=' . $opt->{access_token}; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/publicRooms'; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri, |
|
|
|
uri => $uri, |
|
|
|
}); |
|
|
|
}); |
|
|
@ -346,7 +349,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?access_token=' . $opt->{access_token}; |
|
|
|
my $uri = $opt->{server} . '/_matrix/client/r0/createRoom'; |
|
|
|
my $json = {}; |
|
|
|
my $json = {}; |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
uri => $uri, |
|
|
|
uri => $uri, |
|
|
@ -366,7 +369,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}) . '?access_token=' . $opt->{access_token}; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/directory/room/' . uri_escape($opt->{alias}); |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
room_id => $opt->{room} |
|
|
|
room_id => $opt->{room} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -381,7 +384,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?access_token=' . $opt->{access_token}; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.name'; |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
name => $opt->{name} |
|
|
|
name => $opt->{name} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -396,7 +399,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?access_token=' . $opt->{access_token}; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.topic'; |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
topic => $opt->{topic} |
|
|
|
topic => $opt->{topic} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -411,7 +414,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?access_token=' . $opt->{access_token}; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.join_rules'; |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
join_rules => $opt->{join_rules} |
|
|
|
join_rules => $opt->{join_rules} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -475,7 +478,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?access_token=' . $opt->{access_token}; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels'; |
|
|
|
$resp = send_request({ |
|
|
|
$resp = send_request({ |
|
|
|
method => 'PUT', |
|
|
|
method => 'PUT', |
|
|
|
uri => $uri, |
|
|
|
uri => $uri, |
|
|
@ -487,7 +490,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?access_token=' . $opt->{access_token}; |
|
|
|
$uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/invite'; |
|
|
|
foreach my $invite (@{$opt->{invite}}){ |
|
|
|
foreach my $invite (@{$opt->{invite}}){ |
|
|
|
$json = { |
|
|
|
$json = { |
|
|
|
user_id => $invite |
|
|
|
user_id => $invite |
|
|
@ -511,7 +514,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}) . '?access_token=' . $opt->{access_token}; |
|
|
|
my $uri = $opt->{server} . "/_matrix/client/r0/directory/room/" . uri_escape($opt->{alias}); |
|
|
|
my $resp = send_request({ |
|
|
|
my $resp = send_request({ |
|
|
|
method => 'DELETE', |
|
|
|
method => 'DELETE', |
|
|
|
uri => $uri |
|
|
|
uri => $uri |
|
|
|