diff --git a/README.md b/README.md index b7fb049..9ae59fc 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,10 @@ For now it's very limited, and can only send text messages and files to a room. * --debug: if present, will be verbose * --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 - * --invite: a matrix ID (@user:server.domain.tld) to invite in a room. Can be specified several times - * --name: set the name of a room - * --topic: set the topic of a room - * --alias: set an alias for a room + * --invite: a matrix ID (@user:server.domain.tld) to invite in a room. Can be specified several times. Valid for create-room and modify-room + * --name: set the name of a room. Valid for create-room and modify-room + * --topic: set the topic of a room. Valid for create-room and modify-room + * --alias: set an alias for a room. Valid for create-room and modify-room * --federation: Enable the federation when creating a room. Default is enabled. Can be turned of with --no-federation * --action: what to do. Valid actions are * send-msg (default): send the text message @@ -34,6 +34,7 @@ For now it's very limited, and can only send text messages and files to a room. * 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 * create-room: create a new room + * modify-room: change an existing room (add an alias, set name, topic or invite) * 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 a61df82..1dc3598 100644 --- a/scripts/patrix +++ b/scripts/patrix @@ -306,6 +306,93 @@ sub create_room { print "$room_id\n"; } +# Modify an existing room +sub modify_room { + if ($opt->{debug}){ + print "Modifying room $opt->{room} on $opt->{server}\n"; + } + my ($uri,$req,$json,$resp); + # A new alias should be added + if ($opt->{alias}){ + $uri = $opt->{server} . '/_matrix/client/r0/directory/room/' . uri_escape($opt->{alias}) . '?access_token=' . $opt->{access_token}; + $req = HTTP::Request->new( 'PUT', $uri ); + $json = { + room_id => $opt->{room} + }; + $req->header( 'Content-Type' => 'application/json' ); + $req->content( to_json($json) ); + $resp = $lwp->request( $req ); + if ($opt->{debug}){ + print "New alias response is\n" . + to_json(from_json($resp->decoded_content), { pretty => 1 }) . + "\n\n"; + } + unless ( $resp->is_success ){ + die "Error adding new alias $opt->{alias} for room $opt->{room} on server $opt->{server}\n"; + } + } + # The name of the room is being updated + if ($opt->{name}){ + $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.name?access_token=' . $opt->{access_token}; + $req = HTTP::Request->new( 'PUT', $uri ); + $json = { + name => $opt->{name} + }; + $req->header( 'Content-Type' => 'application/json' ); + $req->content( to_json($json) ); + $resp = $lwp->request( $req ); + if ($opt->{debug}){ + print "modifying room name response is\n" . + to_json(from_json($resp->decoded_content), { pretty => 1 }) . + "\n\n"; + } + unless ( $resp->is_success ){ + die "Error changing name of room $opt->{room}\n"; + } + } + # The topic is being updated + if ($opt->{topic}){ + $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.topic?access_token=' . $opt->{access_token}; + $req = HTTP::Request->new( 'PUT', $uri ); + $json = { + topic => $opt->{topic} + }; + $req->header( 'Content-Type' => 'application/json' ); + $req->content( to_json($json) ); + $resp = $lwp->request( $req ); + if ($opt->{debug}){ + print "modifying room topic response is\n" . + to_json(from_json($resp->decoded_content), { pretty => 1 }) . + "\n\n"; + } + unless ( $resp->is_success ){ + die "Error changing topic of room $opt->{room}\n"; + } + } + # New invitees should be added + if ($opt->{invite}){ + $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/invite?access_token=' . $opt->{access_token}; + foreach my $invite (@{$opt->{invite}}){ + $req = HTTP::Request->new( 'POST', $uri ); + $json = { + user_id => $invite + }; + $req->header( 'Content-Type' => 'application/json' ); + $req->content( to_json($json) ); + $resp = $lwp->request( $req ); + if ($opt->{debug}){ + print "modifying room topic response is\n" . + to_json(from_json($resp->decoded_content), { pretty => 1 }) . + "\n\n"; + } + # TODO: just warn if already invited + unless ( $resp->is_success ){ + die "Error inviting user $invite in room $opt->{room}\n"; + } + } + } +} + # Should we logout at the end ? Only if we used login and pass # If we used an access_token, we don't want it to be invalidated my $must_logout = ($opt->{access_token}) ? 0 : 1; @@ -331,6 +418,9 @@ elsif ($opt->{action} eq 'send-file'){ elsif ($opt->{action} eq 'create-room'){ create_room(); } +elsif ($opt->{action} eq 'modify-room'){ + modify_room(); +} logout() if $must_logout;