Move debug to its own function

tags/patrix-0.1.4-1
Daniel Berteaud 7 years ago
parent 299fd790fe
commit 4632749605
  1. 118
      scripts/patrix

@ -35,9 +35,7 @@ GetOptions(
if (-e File::HomeDir->my_home . "/.patrixrc" && !$opt->{conf}){ if (-e File::HomeDir->my_home . "/.patrixrc" && !$opt->{conf}){
$opt->{conf} = File::HomeDir->my_home . "/.patrixrc"; $opt->{conf} = File::HomeDir->my_home . "/.patrixrc";
if ($opt->{debug}){ debug("Using default config file $opt->{conf}");
print "Using default config file $opt->{conf}\n\n";
}
} }
if ($opt->{conf} && -e $opt->{conf}){ if ($opt->{conf} && -e $opt->{conf}){
read_conf(); read_conf();
@ -45,9 +43,7 @@ if ($opt->{conf} && -e $opt->{conf}){
my $stdin = 0; my $stdin = 0;
if (!-t STDIN){ if (!-t STDIN){
if ($opt->{debug}){ debug("Reading data from stdin");
print "Reading data from stdin\n\n";
}
$stdin = 1; $stdin = 1;
} }
@ -77,6 +73,11 @@ $opt->{server} = 'https://' . $opt->{server} unless ($opt->{server} =~ m|https?:
my $lwp = LWP::UserAgent->new; my $lwp = LWP::UserAgent->new;
sub debug {
my $msg = shift;
print "$msg\n\n" if $opt->{debug};
}
# Load values from the config file if it exists # Load values from the config file if it exists
sub read_conf { sub read_conf {
my $cfg = Config::Simple->new; my $cfg = Config::Simple->new;
@ -90,9 +91,7 @@ sub read_conf {
# Submit user and password the the HS and obtain an access_token # Submit user and password the the HS and obtain an access_token
sub login { sub login {
if ( $opt->{debug} ){ debug("Trying to login on $opt->{server} as $opt->{user}");
print "Trying to login on $opt->{server} as $opt->{user}\n";
}
my $uri = $opt->{server} . '/_matrix/client/r0/login'; my $uri = $opt->{server} . '/_matrix/client/r0/login';
my $req = HTTP::Request->new( 'POST', $uri ); my $req = HTTP::Request->new( 'POST', $uri );
my $json = { my $json = {
@ -103,11 +102,7 @@ sub login {
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
my $resp = $lwp->request( $req ); my $resp = $lwp->request( $req );
if ( $opt->{debug} ){ debug("Login response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "Login response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error login in, please check your credentials\n"; die "Error login in, please check your credentials\n";
} }
@ -117,20 +112,14 @@ sub login {
# Invalidate the access_token # Invalidate the access_token
sub logout { sub logout {
if ( $opt->{debug} ){ debug("Trying to logout");
print "Trying to logout\n";
}
my $uri = $opt->{server} . '/_matrix/client/r0/logout?access_token=' . $opt->{access_token}; my $uri = $opt->{server} . '/_matrix/client/r0/logout?access_token=' . $opt->{access_token};
my $req = HTTP::Request->new( 'POST', $uri ); my $req = HTTP::Request->new( 'POST', $uri );
my $json = {}; my $json = {};
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
my $resp = $lwp->request( $req ); my $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("Logout response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "Logout response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error login out\n"; die "Error login out\n";
} }
@ -138,9 +127,7 @@ 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 {
if ($opt->{debug}){ debug("Trying to join room $opt->{room}");
print "Trying to join room $opt->{room}\n";
}
# 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. if not and room is an alias, it'll start with # so the access_token won't be sent
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} ) . '?access_token=' . $opt->{access_token};
my $req = HTTP::Request->new( 'POST', $uri ); my $req = HTTP::Request->new( 'POST', $uri );
@ -148,11 +135,7 @@ sub join_room {
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
my $resp = $lwp->request( $req ); my $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("Joining room response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "Joining room response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error joining room $opt->{room}\n"; die "Error joining room $opt->{room}\n";
} }
@ -179,11 +162,7 @@ sub send_msg {
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
my $resp = $lwp->request( $req ); my $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("Sending message response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "Sending message response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error sending message to $opt->{room}\n"; die "Error sending message to $opt->{room}\n";
} }
@ -193,20 +172,14 @@ sub send_msg {
sub send_file { 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
if ($opt->{debug}){ debug("Uploading file $opt->{file} to the media store");
print "Uploading file $opt->{file} to the media store\n\n";
}
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($opt->{file});
my $req = HTTP::Request->new( 'POST', $uri ); my $req = HTTP::Request->new( 'POST', $uri );
$req->header( 'Content-Type' => mimetype $opt->{file} ); $req->header( 'Content-Type' => mimetype $opt->{file} );
# Not ideal as it won't work for huge files but... # Not ideal as it won't work for huge files but...
$req->content( path( $opt->{file} )->slurp_raw ); $req->content( path( $opt->{file} )->slurp_raw );
my $resp = $lwp->request( $req ); my $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("File upload response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "File upload response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error uploading file\n"; die "Error uploading file\n";
} }
@ -216,10 +189,7 @@ sub send_file {
unless ($file_uri){ unless ($file_uri){
die "Server did not sent the file URI\n"; die "Server did not sent the file URI\n";
} }
if ($opt->{debug}){ debug("File uploaded, with the URI $file_uri\nNow Sending the file link to the room $opt->{room}");
print "File uploaded, with the URI $file_uri\n";
print "Now Sending the file link to the room $opt->{room}\n\n";
}
# 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?access_token=' . $opt->{access_token};
$req = HTTP::Request->new( 'POST', $uri ); $req = HTTP::Request->new( 'POST', $uri );
@ -236,11 +206,7 @@ sub send_file {
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
$resp = $lwp->request( $req ); $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("Posting file link to the room reseponse is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "Posting file link to the room reseponse is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error posting file link on room $opt->{room}\n"; die "Error posting file link on room $opt->{room}\n";
} }
@ -249,9 +215,7 @@ sub send_file {
# List public rooms # List public rooms
# 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 {
if ($opt->{debug}){ debug("Fetching list of public rooms on $opt->{server}");
print "Fetching list of public rooms on $opt->{server}\n";
}
my $uri = $opt->{server} . '/_matrix/client/r0/publicRooms?access_token=' . $opt->{access_token}; my $uri = $opt->{server} . '/_matrix/client/r0/publicRooms?access_token=' . $opt->{access_token};
my $req = HTTP::Request->new( 'POST', $uri ); my $req = HTTP::Request->new( 'POST', $uri );
my $json = {}; my $json = {};
@ -261,11 +225,7 @@ sub list_room {
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error joining room $opt->{room}\n"; die "Error joining room $opt->{room}\n";
} }
if ($opt->{debug}){ debug("List rooms response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "List rooms response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
print "Existing Rooms:\n"; print "Existing Rooms:\n";
foreach (@{from_json($resp->decoded_content)->{chunk}}){ foreach (@{from_json($resp->decoded_content)->{chunk}}){
print " * " . $_->{room_id}; print " * " . $_->{room_id};
@ -276,9 +236,7 @@ sub list_room {
# Create a new room # Create a new room
sub create_room { sub create_room {
if ($opt->{debug}){ debug("Creating a new room on $opt->{server}");
print "Creating a new room on $opt->{server}\n";
}
my $uri = $opt->{server} . '/_matrix/client/r0/createRoom?access_token=' . $opt->{access_token}; my $uri = $opt->{server} . '/_matrix/client/r0/createRoom?access_token=' . $opt->{access_token};
my $req = HTTP::Request->new( 'POST', $uri ); my $req = HTTP::Request->new( 'POST', $uri );
my $json = {}; my $json = {};
@ -290,11 +248,7 @@ sub create_room {
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
my $resp = $lwp->request( $req ); my $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("Room creation response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "Room creation response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error creating room on $opt->{server}\n"; die "Error creating room on $opt->{server}\n";
} }
@ -304,9 +258,7 @@ sub create_room {
# Modify an existing room # Modify an existing room
sub modify_room { sub modify_room {
if ($opt->{debug}){ debug("Modifying room $opt->{room} on $opt->{server}");
print "Modifying room $opt->{room} on $opt->{server}\n";
}
my ($uri,$req,$json,$resp); my ($uri,$req,$json,$resp);
# A new alias should be added # A new alias should be added
if ($opt->{alias}){ if ($opt->{alias}){
@ -318,11 +270,7 @@ sub modify_room {
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
$resp = $lwp->request( $req ); $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("New alias response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "New alias response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error adding new alias $opt->{alias} for room $opt->{room} on server $opt->{server}\n"; die "Error adding new alias $opt->{alias} for room $opt->{room} on server $opt->{server}\n";
} }
@ -337,11 +285,7 @@ sub modify_room {
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
$resp = $lwp->request( $req ); $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("modifying room name response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "modifying room name response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error changing name of room $opt->{room}\n"; die "Error changing name of room $opt->{room}\n";
} }
@ -356,11 +300,7 @@ sub modify_room {
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
$resp = $lwp->request( $req ); $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("modifying room topic response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "modifying room topic response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error changing topic of room $opt->{room}\n"; die "Error changing topic of room $opt->{room}\n";
} }
@ -376,11 +316,7 @@ sub modify_room {
$req->header( 'Content-Type' => 'application/json' ); $req->header( 'Content-Type' => 'application/json' );
$req->content( to_json($json) ); $req->content( to_json($json) );
$resp = $lwp->request( $req ); $resp = $lwp->request( $req );
if ($opt->{debug}){ debug("modifying room topic response is\n" . to_json(from_json($resp->decoded_content), { pretty => 1 }));
print "modifying room topic response is\n" .
to_json(from_json($resp->decoded_content), { pretty => 1 }) .
"\n\n";
}
# TODO: just warn if already invited # TODO: just warn if already invited
unless ( $resp->is_success ){ unless ( $resp->is_success ){
die "Error inviting user $invite in room $opt->{room}\n"; die "Error inviting user $invite in room $opt->{room}\n";

Loading…
Cancel
Save