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 $VERSION = '2.02'; |
24 |
|
25 |
our $CONFIG_FILE = exists $ENV{PERL_ANYEVENT_MP_RC} ? $ENV{PERL_ANYEVENT_MP_RC} |
26 |
: exists $ENV{HOME} ? "$ENV{HOME}/.perl-anyevent-mp" |
27 |
: "$ENV{APPDATA}/perl-anyevent-mp"; |
28 |
|
29 |
our %CFG; |
30 |
|
31 |
sub load { |
32 |
if (open my $fh, "<:raw", $CONFIG_FILE) { |
33 |
return if eval { |
34 |
local $/; |
35 |
%CFG = %{ JSON::XS->new->utf8->relaxed->decode (scalar <$fh>) }; |
36 |
1 |
37 |
}; |
38 |
} |
39 |
|
40 |
%CFG = ( |
41 |
version => 1, |
42 |
); |
43 |
} |
44 |
|
45 |
sub save { |
46 |
return unless delete $CFG{dirty}; |
47 |
|
48 |
open my $fh, ">:raw", "$CONFIG_FILE~new~" |
49 |
or Carp::croak "$CONFIG_FILE~new~: $!"; |
50 |
|
51 |
syswrite $fh, JSON::XS->new->pretty->utf8->encode (\%CFG) . "\n" |
52 |
or Carp::croak "$CONFIG_FILE~new~: $!"; |
53 |
|
54 |
close $fh |
55 |
or Carp::croak "$CONFIG_FILE~new~: $!"; |
56 |
|
57 |
unlink "$CONFIG_FILE~"; |
58 |
link $CONFIG_FILE, "$CONFIG_FILE~"; |
59 |
rename "$CONFIG_FILE~new~", $CONFIG_FILE |
60 |
or Carp::croak "$CONFIG_FILE: $!"; |
61 |
} |
62 |
|
63 |
sub config { |
64 |
\%CFG |
65 |
} |
66 |
|
67 |
sub _find_profile($); |
68 |
sub _find_profile($) { |
69 |
my ($name) = @_; |
70 |
|
71 |
if (defined $name) { |
72 |
my $profile = $CFG{profile}{$name}; |
73 |
return _find_profile $profile->{parent}, %$profile; |
74 |
} else { |
75 |
return %CFG; |
76 |
} |
77 |
} |
78 |
|
79 |
sub find_profile($;%) { |
80 |
my ($name, %kv) = @_; |
81 |
|
82 |
my $norc = delete $kv{norc}; |
83 |
my $force = delete $kv{force}; |
84 |
|
85 |
%kv = ( |
86 |
monitor_timeout => 30, |
87 |
connect_interval => 2, |
88 |
framing_format => [qw(cbor json storable)], # framing types we offer and accept, in order of preference |
89 |
auth_offer => [qw(tls_sha3_512 hmac_sha3_512)], # what we will send |
90 |
auth_accept => [qw(tls_sha3_512 hmac_sha3_512 tls_anon cleartext)], # what we accept |
91 |
%kv, |
92 |
); |
93 |
|
94 |
unless ($norc) { |
95 |
if ($force) { |
96 |
%kv = (_find_profile $name, %kv); |
97 |
} else { |
98 |
%kv = (%kv, _find_profile $name); |
99 |
} |
100 |
} |
101 |
|
102 |
\%kv |
103 |
} |
104 |
|
105 |
load; |
106 |
END { save } |
107 |
|
108 |
=head1 SEE ALSO |
109 |
|
110 |
L<AnyEvent::MP>. |
111 |
|
112 |
=head1 AUTHOR |
113 |
|
114 |
Marc Lehmann <schmorp@schmorp.de> |
115 |
http://home.schmorp.de/ |
116 |
|
117 |
=cut |
118 |
|
119 |
1 |
120 |
|