commit b582282c367e07bef7c65df7c02c375963ebf31c parent 58eef9946c7713b2d6ef21c52b1987f58c93b41c Author: Alex Balgavy <alex@balgavy.eu> Date: Wed, 4 Jan 2023 16:54:12 +0100 linkhandler: first steps to untrack Diffstat:
M | scripts/linkhandler | | | 30 | +++++++++++++++++++++++++++--- |
1 file changed, 27 insertions(+), 3 deletions(-)
diff --git a/scripts/linkhandler b/scripts/linkhandler @@ -8,10 +8,28 @@ use constant EXIT_USER_CANCELLED => 130; use constant MPD_HOST => 'localhost'; use constant MPD_PORT => 6600; -if ( @ARGV != 1 ) { - die 'Link necessary.'; +sub urlize { + my ($rv) = @_; + $rv =~ s/([^A-Za-z0-9])/sprintf("%%%2.2X", ord($1))/ge; + return $rv; } -my ($LINK) = @ARGV; + +sub un_urlize { + my ($rv) = @_; + $rv =~ s/\+/ /g; + $rv =~ s/%(..)/pack("c",hex($1))/ge; + return $rv; +} + +sub untrack { + my ($orig_link) = @_; + if ($orig_link =~ /(https?(?::|%3A)%2F%2F.*)/) { + my $new_link = $1; + return un_urlize($new_link); + } + return $orig_link; +} + my $CHOOSER = $ENV{'CHOOSER'}; unless (length $CHOOSER) { die 'Must set CHOOSER in environment.'; @@ -20,6 +38,12 @@ unless (length $CHOOSER) { my $HOME = $ENV{'HOME'}; my $TERM = $ENV{'TERM'}; +if ( @ARGV != 1 ) { + die 'Link necessary.'; +} +my ($LINK) = @ARGV; +$LINK = untrack($LINK); + =begin This subroutine takes a subroutine reference and executes the dereferenced subroutine in a fork. Basically a way to handle things async by just writing `detach sub { .... }` in code.