From: Nick Bowler Date: Thu, 21 May 2020 02:59:37 +0000 (-0400) Subject: Update mpdsplat to work with latest perl. X-Git-Url: https://git.draconx.ca/gitweb/irssi-scripts.git/commitdiff_plain/HEAD Update mpdsplat to work with latest perl. And add support for UNIX socket connections too because why not. --- diff --git a/mpdsplat.pl b/mpdsplat.pl index 3a46cbe..6fad657 100644 --- a/mpdsplat.pl +++ b/mpdsplat.pl @@ -1,4 +1,4 @@ -# Copyright © 2008, 2010 Nick Bowler +# Copyright © 2008, 2010, 2020 Nick Bowler # # License WTFPL2: Do What The Fuck You Want To Public License, version 2. # This is free software: you are free to do what the fuck you want to. @@ -8,13 +8,14 @@ # posting to public channels, you can piss off a wider audience. use strict; +use utf8; + use vars qw($VERSION %IRSSI); -use utf8; -use encoding 'utf8'; use IO::Socket::INET6; +use IO::Socket::UNIX; -$VERSION = '0.2'; +$VERSION = '1.0'; %IRSSI = ( authors => 'Nick Bowler', contact => 'nbowler@draconx.ca', @@ -24,27 +25,39 @@ $VERSION = '0.2'; ); # MPD Connection Settings -$ENV{"MPD_HOST"} = "localhost" unless ($ENV{"MPD_HOST"}); -$ENV{"MPD_PORT"} = 6600 unless ($ENV{"MPD_PORT"}); +$ENV{"MPD_HOST"} //= "localhost"; +$ENV{"MPD_PORT"} //= 6600; Irssi::settings_add_str("mpd", "mpd_host", $ENV{"MPD_HOST"}); Irssi::settings_add_int("mpd", "mpd_port", $ENV{"MPD_PORT"}); # Open a connection to MPD. sub mpd_open { - my $sock = new IO::Socket::INET6( - PeerAddr => Irssi::settings_get_str("mpd_host"), - PeerPort => Irssi::settings_get_int("mpd_port"), - Proto => 'tcp', - ) or do { - print CLIENTERROR "failed to open MPD socket: $!."; - return undef; - }; + my $host = Irssi::settings_get_str("mpd_host"); + my $port = Irssi::settings_get_int("mpd_port"); + my $sock; + + if ($host =~ /^[@\/]/) { + $host =~ s/^@/\0/; + $sock = new IO::Socket::UNIX(Type => SOCK_STREAM(), + Peer => $host) + } else { + $sock = new IO::Socket::INET6(PeerAddr => $host, + PeerPort => $port, + Proto => 'tcp', + Timeout => 2) + } + + unless ($sock) { + print CLIENTERROR "failed to open MPD socket: $!"; + return; + } + binmode($sock, ":utf8"); # Grab the MPD version. - if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) { - print CLIENTERROR "failed MPD handshake: $!."; - return undef; + unless (<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/) { + print CLIENTERROR "failed MPD handshake: $!"; + return; } return $sock; @@ -116,7 +129,7 @@ sub cmd_current { # If the active window is not a channel or query, display locally. $local = 1 unless ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")); - + if ($local) { $window->print($text); } elsif ($data) {