mirror of
https://github.com/dani/patrix.git
synced 2024-06-01 21:41:41 +02:00
Support sending several files
This commit is contained in:
parent
340efc0a0a
commit
e86c3e98d3
|
@ -27,7 +27,7 @@ Here're the vailable options:
|
||||||
* --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
|
* --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
|
* --debug: if present, will be verbose
|
||||||
* --conf: path to a conf file. Default conf file is ~/.patrixrc
|
* --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
|
* --file: if action is send-file, specify the path of the file to send. You can send several files at once by using multiple --file args
|
||||||
* --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
|
* --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
|
* --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
|
* --topic: set the topic of a room. Valid for create-room and modify-room
|
||||||
|
|
|
@ -26,7 +26,7 @@ GetOptions(
|
||||||
"server=s" => \$opt->{server},
|
"server=s" => \$opt->{server},
|
||||||
"room=s" => \$opt->{room},
|
"room=s" => \$opt->{room},
|
||||||
"message=s" => \$opt->{message},
|
"message=s" => \$opt->{message},
|
||||||
"files=s" => \$opt->{file},
|
"files=s@" => \$opt->{file},
|
||||||
"debug" => \$opt->{debug},
|
"debug" => \$opt->{debug},
|
||||||
"action=s" => \$opt->{action},
|
"action=s" => \$opt->{action},
|
||||||
"send-msg" => \$opt->{'send-msg'},
|
"send-msg" => \$opt->{'send-msg'},
|
||||||
|
@ -89,10 +89,12 @@ if ($opt->{room} && $opt->{room} =~ m/^#/){
|
||||||
debug('Room ID is ' . $opt->{room});
|
debug('Room ID is ' . $opt->{room});
|
||||||
}
|
}
|
||||||
|
|
||||||
# Handle ~
|
foreach (@{$opt->{file}}){
|
||||||
$opt->{file} =~ s/^~(\w*)/(getpwnam( $1 || $ENV{USER}))[7]/e;
|
# Handle ~
|
||||||
# Convert to absolute path
|
$_ =~ s/^~(\w*)/(getpwnam( $1 || $ENV{USER}))[7]/e;
|
||||||
$opt->{file} = File::Spec->rel2abs($opt->{file});
|
# Convert to absolute path
|
||||||
|
$_ = File::Spec->rel2abs($_);
|
||||||
|
}
|
||||||
|
|
||||||
# Check we have all the options we need
|
# Check we have all the options we need
|
||||||
if (!$opt->{access_token} && !$opt->{user}){
|
if (!$opt->{access_token} && !$opt->{user}){
|
||||||
|
@ -107,8 +109,10 @@ if ($opt->{action} eq 'send-msg' && (!$opt->{room} || (!$opt->{message} && !$std
|
||||||
if ($opt->{action} eq 'send-file' && (!$opt->{room} || !$opt->{file})){
|
if ($opt->{action} eq 'send-file' && (!$opt->{room} || !$opt->{file})){
|
||||||
die "You need to provide a room ID and a file to send\n\n";
|
die "You need to provide a room ID and a file to send\n\n";
|
||||||
}
|
}
|
||||||
if ($opt->{action} eq 'send-file' && $opt->{file} && !-e $opt->{file}){
|
if ($opt->{action} eq 'send-file' && $opt->{file}){
|
||||||
die "File $opt->{file} not found\n\n";
|
foreach (@{$opt->{file}}){
|
||||||
|
die "File $_ not found\n\n" unless (-e $_);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($opt->{action} eq 'modify-room' && !$opt->{room}){
|
if ($opt->{action} eq 'modify-room' && !$opt->{room}){
|
||||||
die "You need to specify the room to modify\n\n";
|
die "You need to specify the room to modify\n\n";
|
||||||
|
@ -244,14 +248,15 @@ sub send_msg {
|
||||||
|
|
||||||
# Send a file to the room
|
# Send a file to the room
|
||||||
sub send_file {
|
sub send_file {
|
||||||
|
my $file = shift;
|
||||||
# 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 $opt->{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($opt->{file});
|
my $uri = $opt->{server} . '/_matrix/media/v1/upload?access_token=' . $opt->{access_token} . '&filename=' . basename($file);
|
||||||
my $resp = send_request({
|
my $resp = send_request({
|
||||||
uri => $uri,
|
uri => $uri,
|
||||||
content_type => mimetype($opt->{file}),
|
content_type => mimetype($file),
|
||||||
content => path($opt->{file})->slurp_raw
|
content => path($file)->slurp_raw
|
||||||
});
|
});
|
||||||
debug("File upload response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
|
debug("File upload response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
|
||||||
die "Error uploading file\n" unless ($resp->is_success);
|
die "Error uploading file\n" unless ($resp->is_success);
|
||||||
|
@ -264,11 +269,11 @@ sub send_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?access_token=' . $opt->{access_token};
|
||||||
my $json = {
|
my $json = {
|
||||||
msgtype => 'm.file',
|
msgtype => 'm.file',
|
||||||
body => basename($opt->{file}),
|
body => basename($file),
|
||||||
filename => basename($opt->{file}),
|
filename => basename($file),
|
||||||
info => {
|
info => {
|
||||||
mimetype => mimetype $opt->{file},
|
mimetype => mimetype $file,
|
||||||
size => (stat $opt->{file})[7]
|
size => (stat $file)[7]
|
||||||
},
|
},
|
||||||
url => $file_uri
|
url => $file_uri
|
||||||
};
|
};
|
||||||
|
@ -516,7 +521,7 @@ elsif ($opt->{action} =~ m/^send\-(msg|message|notice)$/){
|
||||||
}
|
}
|
||||||
elsif ($opt->{action} eq 'send-file'){
|
elsif ($opt->{action} eq 'send-file'){
|
||||||
join_room();
|
join_room();
|
||||||
send_file();
|
send_file($_) foreach (@{$opt->{file}});
|
||||||
}
|
}
|
||||||
elsif ($opt->{action} eq 'create-room'){
|
elsif ($opt->{action} eq 'create-room'){
|
||||||
create_room();
|
create_room();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user