summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
33edf1f)
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.
defined (my $op = shift @ARGV) or die_usage;
if ($op eq "get") {
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;
- 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) {
my ($root, $list) = grobble_root $sk;
for my $f (sort @$list) {
- 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 $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) {
my ($root, $list) = grobble_root $sk;
FILE: for my $f (sort @$list) {
+my $CONF = undef;
+
+sub configured_connection (;$) {
+ my ($quietp) = @_;
+ $CONF //= load_config $C{config};
+ return connect_to_server %$CONF, $quietp // 0;
+}
+
- my $sk = connect_to_server $C{config};
+ my $sk = configured_connection;
send_command0 $sk, "log";
my $st = get_state0 $sk;
close $sk;
send_command0 $sk, "log";
my $st = get_state0 $sk;
close $sk;
sub watch_and_notify0 ($) {
my ($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;
send_command0 $sk_log, "log";
my $st = get_state0 $sk_log;
sub { run_discard_output "amixer", "sset", $C{mixer}, "5\%-"; };
$OP{"scratch"} = sub {
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;
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;
if ($st->{play}) { send_command $sk, "disable"; }
else { send_command $sk, "enable"; }
close $sk;
$OP{"play/pause"} = sub {
my $st = get_state;
$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"; }
if (!$st->{play}) {
send_command $sk, "enable";
if ($st->{pause}) { send_command $sk, "resume"; }
};
$OP{"now-playing"} = 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;
my $info = get_now_playing $sk;
close $sk;
print format_now_playing %$info;
};
$OP{"notify-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;
my $info = get_now_playing $sk;
close $sk;
notify "$TITLE: Now playing", format_now_playing %$info;
our @EXPORT_OK = qw{get_response0 decode_response get_response
send_command0 send_command
split_fields
our @EXPORT_OK = qw{get_response0 decode_response get_response
send_command0 send_command
split_fields
+ load_config connect_to_server};
-sub connect_to_server ($;$) {
- my ($conf, $quietp) = @_;
+sub load_config ($) {
+ my ($conf) = @_;
my %conf = (connect => ["-unix", "/var/lib/disorder/socket"]);
my %conf = (connect => ["-unix", "/var/lib/disorder/socket"]);
open my $fh, "<", $conf;
LINE: while (<$fh>) {
open my $fh, "<", $conf;
LINE: while (<$fh>) {
close $fh;
for my $i (qw{ username password })
{ die "missing configuration keyword `$i'" unless exists $conf{$i}; }
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 @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; }
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; }
@f = split_fields get_response $sk;
die "expected version 2" unless $f[0] eq "2";
my $h = Digest::SHA->new($f[1]);
@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]);
- send_command $sk, "user", $conf{username}[0], $d;
+ send_command $sk, "user", $conf->{username}[0], $d;