pl/DisOrder.pm, etc.: Split `load_config' from `connect_to_server'.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 10 Jun 2020 15:03:56 +0000 (16:03 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 10 Jun 2020 15:03:56 +0000 (16:03 +0100)
Now we can read the configuration once and use it throughout the
program.  This is most useful for `disorder-notify watch', which no
longer unexpectedly tracks changes to the password-file symlink when it
reconnects.

bin/disorder-autoplay
bin/disorder-notify
pl/DisOrder.pm

index 0ab0051..7b0eca3 100755 (executable)
@@ -59,9 +59,10 @@ EOF
 
 defined (my $op = shift @ARGV) or die_usage;
 if ($op eq "get") {
-  defined (my $conf = shift @ARGV) or die_usage;
+  defined (my $cf = shift @ARGV) or die_usage;
   !@ARGV or die_usage;
-  my $sk = connect_to_server $conf;
+  my $conf = load_config $cf;
+  my $sk = connect_to_server %$conf;
   my ($root, $list) = grobble_root $sk;
 
   for my $f (sort @$list) {
@@ -70,13 +71,14 @@ if ($op eq "get") {
   }
   close $sk;
 } else {
-  defined (my $conf = shift @ARGV) or die_usage;
+  defined (my $cf = shift @ARGV) or die_usage;
   my $fh;
   if (defined (my $list = shift @ARGV)) { open $fh, "<", $list; }
   else { $fh = \*STDIN; }
   my %black = ();
   while (<$fh>) { chomp; $black{$_} = 1; }
-  my $sk = connect_to_server $conf;
+  my $conf = load_config $cf;
+  my $sk = connect_to_server %$conf;
   my ($root, $list) = grobble_root $sk;
 
   FILE: for my $f (sort @$list) {
index 092d255..4e0b354 100755 (executable)
@@ -124,8 +124,16 @@ sub get_state0 ($) {
   return \%st;
 }
 
+my $CONF = undef;
+
+sub configured_connection (;$) {
+  my ($quietp) = @_;
+  $CONF //= load_config $C{config};
+  return connect_to_server %$CONF, $quietp // 0;
+}
+
 sub get_state () {
-  my $sk = connect_to_server $C{config};
+  my $sk = configured_connection;
   send_command0 $sk, "log";
   my $st = get_state0 $sk;
   close $sk;
@@ -173,8 +181,8 @@ sub get_now_playing ($) {
 sub watch_and_notify0 ($) {
   my ($now_playing) = @_;
 
-  my $sk = connect_to_server $C{config}, 1;
-  my $sk_log = connect_to_server $C{config}, 1;
+  my $sk = configured_connection 1;
+  my $sk_log = configured_connection 1;
 
   send_command0 $sk_log, "log";
   my $st = get_state0 $sk_log;
@@ -269,14 +277,14 @@ $OP{"volume-down"} =
   sub { run_discard_output "amixer", "sset", $C{mixer}, "5\%-"; };
 
 $OP{"scratch"} = sub {
-  my $sk = connect_to_server $C{config};
+  my $sk = configured_connection;
   send_command $sk, "scratch";
   close $sk;
 };
 
 $OP{"enable/disable"} = sub {
   my $st = get_state;
-  my $sk = connect_to_server $C{config};
+  my $sk =configured_connection;
   if ($st->{play}) { send_command $sk, "disable"; }
   else { send_command $sk, "enable"; }
   close $sk;
@@ -284,7 +292,7 @@ $OP{"enable/disable"} = sub {
 
 $OP{"play/pause"} = sub {
   my $st = get_state;
-  my $sk = connect_to_server $C{config};
+  my $sk = configured_connection;
   if (!$st->{play}) {
     send_command $sk, "enable";
     if ($st->{pause}) { send_command $sk, "resume"; }
@@ -304,7 +312,7 @@ $OP{"watch"} = sub {
 };
 
 $OP{"now-playing"} = sub {
-  my $sk = connect_to_server $C{config};
+  my $sk = configured_connection;
   my $info = get_now_playing $sk;
   close $sk;
   print format_now_playing %$info;
@@ -312,7 +320,7 @@ $OP{"now-playing"} = sub {
 };
 
 $OP{"notify-now-playing"} = sub {
-  my $sk = connect_to_server $C{config};
+  my $sk = configured_connection;
   my $info = get_now_playing $sk;
   close $sk;
   notify "$TITLE: Now playing", format_now_playing %$info;
index 5f72131..ab609b3 100644 (file)
@@ -10,7 +10,7 @@ use Socket qw{:DEFAULT :addrinfo};
 our @EXPORT_OK = qw{get_response0 decode_response get_response
                    send_command0 send_command
                    split_fields
-                   connect_to_server};
+                   load_config connect_to_server};
 
 use Data::Dumper;
 
@@ -102,10 +102,9 @@ sub split_fields ($) {
   return @f;
 }
 
-sub connect_to_server ($;$) {
-  my ($conf, $quietp) = @_;
+sub load_config ($) {
+  my ($conf) = @_;
   my %conf = (connect => ["-unix", "/var/lib/disorder/socket"]);
-  my @f;
 
   open my $fh, "<", $conf;
   LINE: while (<$fh>) {
@@ -117,9 +116,15 @@ sub connect_to_server ($;$) {
   close $fh;
   for my $i (qw{ username password })
     { die "missing configuration keyword `$i'" unless exists $conf{$i}; }
+  return \%conf;
+}
+
+sub connect_to_server (\%;$) {
+  my ($conf, $quietp) = @_;
+  my @f;
 
   my $af = AF_UNSPEC;
-  my @a = $conf{connect}->@*;
+  my @a = $conf->{connect}->@*;
   die "empty address" unless @a;
   if ($a[0] eq "-unix") { $af = AF_UNIX; shift @a; }
   elsif ($a[0] eq "-4") { $af = AF_INET; shift @a; }
@@ -175,9 +180,9 @@ sub connect_to_server ($;$) {
   @f = split_fields get_response $sk;
   die "expected version 2" unless $f[0] eq "2";
   my $h = Digest::SHA->new($f[1]);
-  $h->add($conf{password}[0], pack "H*", $f[2]);
+  $h->add($conf->{password}[0], pack "H*", $f[2]);
   my $d = $h->hexdigest;
-  send_command $sk, "user", $conf{username}[0], $d;
+  send_command $sk, "user", $conf->{username}[0], $d;
 
   return $sk;
 }