chiark / gitweb /
pl/DisOrder.pm, etc.: Split `load_config' from `connect_to_server'.
[profile] / pl / DisOrder.pm
index 5f72131b8dd2f22641160fed0560b59d4f161079..ab609b32cc3bd81e0251ee067bb44919e6f2cfc6 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;
 }