]> git.draconx.ca Git - irssi-scripts.git/commitdiff
Update mpdsplat to work with latest perl. master
authorNick Bowler <nbowler@draconx.ca>
Thu, 21 May 2020 02:59:37 +0000 (22:59 -0400)
committerNick Bowler <nbowler@draconx.ca>
Thu, 21 May 2020 02:59:37 +0000 (22:59 -0400)
And add support for UNIX socket connections too because why not.

mpdsplat.pl

index 3a46cbe19b1a6395cec783726e123fae4aba6437..6fad6576f516e110b518a1b4a83c9227c5c1a1c8 100644 (file)
@@ -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) {