From 03a854f576b99ca005755b43ccce82636ba5120e Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Thu, 7 Sep 2017 13:25:49 +0200 Subject: [PATCH] Fix joining room by alias And replace --notice with --action=send-notice --- README.md | 5 +++-- scripts/patrix | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6874f13..4a72da8 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,15 @@ For now it's very limited, and can only send text messages and files to a room. * --password: the password to auth against the HS * --server: the HS you want to connect to. Default is https://matrix.org * --access_token: can be used instead of --user and --password - * --room: the room to which the message must be sent + * --room: the room to which the message must be sent. Can be a room ID or a room alias * --message: the text message you want to send. If you send something on stdin, it's assumed to be the text to send and this option is ignored * --debug: if present, will be verbose - * --notice: send a notice instead of a message (more or less the same but the client can display it differently. Riot for example will not notify you for notices) * --conf: path to a conf file. Default conf file is ~/.patrixrc * --file: if action is send-file, specify the path of the file to send * --action: what to do. Valid actions are * send-msg (default): send the text message + * send-message: an alias for send-msg + * send-notice: send a notice. Very similar to send-msg but the client may display it differently. Eg Riot will not notify you for notices * send-file: send a binary file. --file must be set * get-access-token: just login and print the access token * get-room-list: prints the list of public rooms of this server diff --git a/scripts/patrix b/scripts/patrix index 843ce23..9377c06 100644 --- a/scripts/patrix +++ b/scripts/patrix @@ -10,6 +10,7 @@ use Config::Simple; use File::HomeDir; use File::MimeInfo; use File::Basename; +use URI::Escape; use Path::Tiny; our $opt; @@ -21,7 +22,6 @@ GetOptions( "server=s" => \$opt->{server}, "room=s" => \$opt->{room}, "message=s" => \$opt->{message}, - "notice" => \$opt->{notice}, "files=s" => \$opt->{file}, "debug" => \$opt->{debug}, "action=s" => \$opt->{action}, @@ -134,7 +134,7 @@ sub join_room(){ if ($opt->{debug}){ print "Trying to join room $opt->{room}\n"; } - my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/join?access_token=' . $opt->{access_token}; + my $uri = $opt->{server} . '/_matrix/client/r0/join/' . uri_escape( $opt->{room} ) . '?access_token=' . $opt->{access_token}; my $req = HTTP::Request->new( 'POST', $uri ); my $json = {}; $req->header( 'Content-Type' => 'application/json' ); @@ -150,14 +150,14 @@ sub join_room(){ } # Resolve room -> room_id if joined by alias my $room_id = from_json($resp->decoded_content)->{room_id}; - $opt->{roomt} = $room_id if $room_id; + $opt->{room} = $room_id if $room_id; } sub send_msg(){ my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message?access_token=' . $opt->{access_token}; my $req = HTTP::Request->new( 'POST', $uri ); my $json = { - msgtype => ($opt->{notice}) ? 'm.notice' : 'm.text', + msgtype => ($opt->{action} eq 'send-notice') ? 'm.notice' : 'm.text', body => $opt->{message} }; $req->header( 'Content-Type' => 'application/json' ); @@ -242,7 +242,7 @@ if ($opt->{action} eq 'get-access-token'){ elsif ($opt->{action} eq 'get-room-list'){ list_room(); } -elsif ($opt->{action} eq 'send-msg'){ +elsif ($opt->{action} =~ m/send\-(msg|message|notice)/){ join_room(); send_msg(); }