| 1 |
=head1 NAME |
| 2 |
|
| 3 |
AnyEvent::MP::Config - configuration handling |
| 4 |
|
| 5 |
=head1 SYNOPSIS |
| 6 |
|
| 7 |
# see the "aemp" command line utility |
| 8 |
|
| 9 |
=head1 DESCRIPTION |
| 10 |
|
| 11 |
Move along please, nothing to see here at the moment. |
| 12 |
|
| 13 |
=cut |
| 14 |
|
| 15 |
package AnyEvent::MP::Config; |
| 16 |
|
| 17 |
use common::sense; |
| 18 |
|
| 19 |
use Carp (); |
| 20 |
use AnyEvent (); |
| 21 |
use JSON::XS (); |
| 22 |
|
| 23 |
our $CONFIG_FILE = exists $ENV{PERL_ANYEVENT_MP_RC} ? $ENV{PERL_ANYEVENT_MP_RC} |
| 24 |
: exists $ENV{HOME} ? "$ENV{HOME}/.perl-anyevent-mp" |
| 25 |
: "$ENV{APPDATA}/perl-anyevent-mp"; |
| 26 |
|
| 27 |
our %CFG; |
| 28 |
|
| 29 |
sub load { |
| 30 |
if (open my $fh, "<:raw", $CONFIG_FILE) { |
| 31 |
return if eval { |
| 32 |
local $/; |
| 33 |
%CFG = %{ JSON::XS->new->utf8->relaxed->decode (scalar <$fh>) }; |
| 34 |
1 |
| 35 |
}; |
| 36 |
} |
| 37 |
|
| 38 |
%CFG = ( |
| 39 |
version => 1, |
| 40 |
dirty => 1, |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
sub save { |
| 45 |
return unless delete $CFG{dirty}; |
| 46 |
|
| 47 |
open my $fh, ">:raw", "$CONFIG_FILE~new~" |
| 48 |
or Carp::croak "$CONFIG_FILE~new~: $!"; |
| 49 |
|
| 50 |
syswrite $fh, JSON::XS->new->pretty->utf8->encode (\%CFG) . "\n" |
| 51 |
or Carp::croak "$CONFIG_FILE~new~: $!"; |
| 52 |
|
| 53 |
close $fh |
| 54 |
or Carp::croak "$CONFIG_FILE~new~: $!"; |
| 55 |
|
| 56 |
unlink "$CONFIG_FILE~"; |
| 57 |
link $CONFIG_FILE, "$CONFIG_FILE~"; |
| 58 |
rename "$CONFIG_FILE~new~", $CONFIG_FILE |
| 59 |
or Carp::croak "$CONFIG_FILE: $!"; |
| 60 |
} |
| 61 |
|
| 62 |
sub config { |
| 63 |
\%CFG |
| 64 |
} |
| 65 |
|
| 66 |
sub _find_profile($); |
| 67 |
sub _find_profile($) { |
| 68 |
my ($name) = @_; |
| 69 |
|
| 70 |
if (defined $name) { |
| 71 |
my $profile = $CFG{profile}{$name}; |
| 72 |
return _find_profile $profile->{parent}, %$profile; |
| 73 |
} else { |
| 74 |
return %CFG; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
sub find_profile($;%) { |
| 79 |
my ($name, %kv) = @_; |
| 80 |
|
| 81 |
+{ |
| 82 |
monitor_timeout => 30, |
| 83 |
connect_interval => 2, |
| 84 |
framing_format => [qw(json storable)], # framing types we offer and accept, in order of preference |
| 85 |
auth_offer => [qw(tls_md6_64_256 hmac_md6_64_256)], # what we will send |
| 86 |
auth_accept => [qw(tls_md6_64_256 hmac_md6_64_256 tls_anon cleartext)], # what we accept |
| 87 |
%kv, |
| 88 |
_find_profile $name, |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
load; |
| 93 |
END { save } |
| 94 |
|
| 95 |
=head1 SEE ALSO |
| 96 |
|
| 97 |
L<AnyEvent::MP>. |
| 98 |
|
| 99 |
=head1 AUTHOR |
| 100 |
|
| 101 |
Marc Lehmann <schmorp@schmorp.de> |
| 102 |
http://home.schmorp.de/ |
| 103 |
|
| 104 |
=cut |
| 105 |
|
| 106 |
1 |
| 107 |
|