ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Config.pm
Revision: 1.11
Committed: Sun Feb 26 11:12:54 2012 UTC (12 years, 3 months ago) by root
Branch: MAIN
Changes since 1.10: +14 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.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 root 1.2 Move along please, nothing to see here at the moment.
12 root 1.1
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 root 1.8 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 root 1.1
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     );
41     }
42    
43     sub save {
44     return unless delete $CFG{dirty};
45    
46     open my $fh, ">:raw", "$CONFIG_FILE~new~"
47     or Carp::croak "$CONFIG_FILE~new~: $!";
48    
49     syswrite $fh, JSON::XS->new->pretty->utf8->encode (\%CFG) . "\n"
50     or Carp::croak "$CONFIG_FILE~new~: $!";
51    
52     close $fh
53     or Carp::croak "$CONFIG_FILE~new~: $!";
54    
55     unlink "$CONFIG_FILE~";
56     link $CONFIG_FILE, "$CONFIG_FILE~";
57     rename "$CONFIG_FILE~new~", $CONFIG_FILE
58     or Carp::croak "$CONFIG_FILE: $!";
59     }
60    
61 root 1.3 sub config {
62 root 1.2 \%CFG
63     }
64    
65 root 1.5 sub _find_profile($);
66 root 1.4 sub _find_profile($) {
67     my ($name) = @_;
68    
69     if (defined $name) {
70     my $profile = $CFG{profile}{$name};
71 root 1.5 return _find_profile $profile->{parent}, %$profile;
72 root 1.4 } else {
73     return %CFG;
74     }
75     }
76    
77     sub find_profile($;%) {
78     my ($name, %kv) = @_;
79    
80 root 1.11 my $norc = delete $kv{norc};
81     my $force = delete $kv{force};
82    
83     %kv = (
84 root 1.6 monitor_timeout => 30,
85     connect_interval => 2,
86 root 1.9 framing_format => [qw(json storable)], # framing types we offer and accept, in order of preference
87 root 1.7 auth_offer => [qw(tls_md6_64_256 hmac_md6_64_256)], # what we will send
88     auth_accept => [qw(tls_md6_64_256 hmac_md6_64_256 tls_anon cleartext)], # what we accept
89 root 1.4 %kv,
90 root 1.11 );
91    
92     unless ($norc) {
93     if ($force) {
94     %kv = (_find_profile $name, %kv);
95     } else {
96     %kv = (%kv, _find_profile $name);
97     }
98 root 1.3 }
99 root 1.11
100     \%kv
101 root 1.3 }
102    
103 root 1.1 load;
104     END { save }
105    
106     =head1 SEE ALSO
107    
108     L<AnyEvent::MP>.
109    
110     =head1 AUTHOR
111    
112     Marc Lehmann <schmorp@schmorp.de>
113     http://home.schmorp.de/
114    
115     =cut
116    
117     1
118