mirror of https://github.com/dani/patrix.git
Add possibility to create a room
This commit is contained in:
parent
3d3b3f91d5
commit
dd85f18c18
|
@ -23,11 +23,16 @@ 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
|
||||
* --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
|
||||
* create-room: create a new room
|
||||
* get-access-token: just login and print the access token
|
||||
* get-room-list: prints the list of public rooms of this server
|
||||
|
||||
|
|
|
@ -25,7 +25,11 @@ GetOptions(
|
|||
"files=s" => \$opt->{file},
|
||||
"debug" => \$opt->{debug},
|
||||
"action=s" => \$opt->{action},
|
||||
"conf=s" => \$opt->{conf}
|
||||
"conf=s" => \$opt->{conf},
|
||||
"invite=s@" => \$opt->{invite},
|
||||
"name=s" => \$opt->{name},
|
||||
"alias=s" => \$opt->{alias},
|
||||
"topic=s" => \$opt->{topic}
|
||||
);
|
||||
|
||||
if (-e File::HomeDir->my_home . "/.patrixrc" && !$opt->{conf}){
|
||||
|
@ -55,7 +59,7 @@ if ($opt->{action} eq 'get-access-token' && (!$opt->{user} || !$opt->{password})
|
|||
die "You need to provide a valid user and password to get an access token\n\n";
|
||||
}
|
||||
elsif (!$opt->{access_token} && (!$opt->{user} || !$opt->{password})){
|
||||
die "You need to provide either an access token or a valid user and password\n\n";
|
||||
die "Test: You need to provide either an access token or a valid user and password\n\n";
|
||||
}
|
||||
if ($opt->{action} eq 'send-msg' && (!$opt->{room} || (!$opt->{message} && !$stdin))){
|
||||
die "You need to provide a room ID and a message\n\n";
|
||||
|
@ -268,6 +272,33 @@ sub list_room {
|
|||
}
|
||||
}
|
||||
|
||||
# Create a new room
|
||||
sub create_room {
|
||||
if ($opt->{debug}){
|
||||
print "Creating a new room on $opt->{server}\n";
|
||||
}
|
||||
my $uri = $opt->{server} . '/_matrix/client/r0/createRoom?access_token=' . $opt->{access_token};
|
||||
my $req = HTTP::Request->new( 'POST', $uri );
|
||||
my $json = {};
|
||||
$json->{room_alias_name} = $opt->{alias} if $opt->{alias};
|
||||
$json->{topic} = $opt->{topic} if $opt->{topic};
|
||||
$json->{name} = $opt->{name} if $opt->{name};
|
||||
$json->{invite} = $opt->{invite} if $opt->{invite};
|
||||
$req->header( 'Content-Type' => 'application/json' );
|
||||
$req->content( to_json($json) );
|
||||
my $resp = $lwp->request( $req );
|
||||
if ($opt->{debug}){
|
||||
print "Room creation response is\n" .
|
||||
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
|
||||
"\n\n";
|
||||
}
|
||||
unless ( $resp->is_success ){
|
||||
die "Error creating room on $opt->{server}\n";
|
||||
}
|
||||
my $room_id = from_json($resp->decoded_content)->{room_id};
|
||||
print "Room created with ID $room_id\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;
|
||||
|
@ -290,6 +321,9 @@ elsif ($opt->{action} eq 'send-file'){
|
|||
join_room();
|
||||
send_file();
|
||||
}
|
||||
elsif ($opt->{action} eq 'create-room'){
|
||||
create_room();
|
||||
}
|
||||
|
||||
logout() if $must_logout;
|
||||
|
||||
|
|
Loading…
Reference in New Issue