| 1 | package Mail::DMARC::ConfigData; | ||
| 2 | use strict; | ||
| 3 | my $arrayref = eval do {local $/; <DATA>} | ||
| 4 | or die "Couldn't load ConfigData data: $@"; | ||
| 5 | close DATA; | ||
| 6 | my ($config, $features, $auto_features) = @$arrayref; | ||
| 7 | |||
| 8 | sub config { $config->{$_[1]} } | ||
| 9 | |||
| 10 | sub set_config { $config->{$_[1]} = $_[2] } | ||
| 11 | sub set_feature { $features->{$_[1]} = 0+!!$_[2] } # Constrain to 1 or 0 | ||
| 12 | |||
| 13 | sub auto_feature_names { sort grep !exists $features->{$_}, keys %$auto_features } | ||
| 14 | |||
| 15 | sub feature_names { | ||
| 16 | my @features = (sort keys %$features, auto_feature_names()); | ||
| 17 | @features; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub config_names { sort keys %$config } | ||
| 21 | |||
| 22 | sub write { | ||
| 23 | my $me = __FILE__; | ||
| 24 | |||
| 25 | # Can't use Module::Build::Dumper here because M::B is only a | ||
| 26 | # build-time prereq of this module | ||
| 27 | require Data::Dumper; | ||
| 28 | |||
| 29 | my $mode_orig = (stat $me)[2] & 07777; | ||
| 30 | chmod($mode_orig | 0222, $me); # Make it writeable | ||
| 31 | open(my $fh, '+<', $me) or die "Can't rewrite $me: $!"; | ||
| 32 | seek($fh, 0, 0); | ||
| 33 | while (<$fh>) { | ||
| 34 | last if /^__DATA__$/; | ||
| 35 | } | ||
| 36 | die "Couldn't find __DATA__ token in $me" if eof($fh); | ||
| 37 | |||
| 38 | seek($fh, tell($fh), 0); | ||
| 39 | my $data = [$config, $features, $auto_features]; | ||
| 40 | print($fh 'do{ my ' | ||
| 41 | . Data::Dumper->new([$data],['x'])->Purity(1)->Dump() | ||
| 42 | . '$x; }' ); | ||
| 43 | truncate($fh, tell($fh)); | ||
| 44 | close $fh; | ||
| 45 | |||
| 46 | chmod($mode_orig, $me) | ||
| 47 | or warn "Couldn't restore permissions on $me: $!"; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub feature { | ||
| 51 | my ($package, $key) = @_; | ||
| 52 | return $features->{$key} if exists $features->{$key}; | ||
| 53 | |||
| 54 | my $info = $auto_features->{$key} or return 0; | ||
| 55 | |||
| 56 | require Module::Build; # XXX should get rid of this | ||
| 57 | foreach my $type (sort keys %$info) { | ||
| 58 | my $prereqs = $info->{$type}; | ||
| 59 | next if $type eq 'description' || $type eq 'recommends'; | ||
| 60 | |||
| 61 | foreach my $modname (sort keys %$prereqs) { | ||
| 62 | my $status = Module::Build->check_installed_status($modname, $prereqs->{$modname}); | ||
| 63 | if ((!$status->{ok}) xor ($type =~ /conflicts$/)) { return 0; } | ||
| 64 | if ( ! eval "require $modname; 1" ) { return 0; } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | return 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | =head1 NAME | ||
| 72 | |||
| 73 | Mail::DMARC::ConfigData - Configuration for Mail::DMARC | ||
| 74 | |||
| 75 | =head1 SYNOPSIS | ||
| 76 | |||
| 77 | use Mail::DMARC::ConfigData; | ||
| 78 | $value = Mail::DMARC::ConfigData->config('foo'); | ||
| 79 | $value = Mail::DMARC::ConfigData->feature('bar'); | ||
| 80 | |||
| 81 | @names = Mail::DMARC::ConfigData->config_names; | ||
| 82 | @names = Mail::DMARC::ConfigData->feature_names; | ||
| 83 | |||
| 84 | Mail::DMARC::ConfigData->set_config(foo => $new_value); | ||
| 85 | Mail::DMARC::ConfigData->set_feature(bar => $new_value); | ||
| 86 | Mail::DMARC::ConfigData->write; # Save changes | ||
| 87 | |||
| 88 | |||
| 89 | =head1 DESCRIPTION | ||
| 90 | |||
| 91 | This module holds the configuration data for the C<Mail::DMARC> | ||
| 92 | module. It also provides a programmatic interface for getting or | ||
| 93 | setting that configuration data. Note that in order to actually make | ||
| 94 | changes, you'll have to have write access to the C<Mail::DMARC::ConfigData> | ||
| 95 | module, and you should attempt to understand the repercussions of your | ||
| 96 | actions. | ||
| 97 | |||
| 98 | |||
| 99 | =head1 METHODS | ||
| 100 | |||
| 101 | =over 4 | ||
| 102 | |||
| 103 | =item config($name) | ||
| 104 | |||
| 105 | Given a string argument, returns the value of the configuration item | ||
| 106 | by that name, or C<undef> if no such item exists. | ||
| 107 | |||
| 108 | =item feature($name) | ||
| 109 | |||
| 110 | Given a string argument, returns the value of the feature by that | ||
| 111 | name, or C<undef> if no such feature exists. | ||
| 112 | |||
| 113 | =item set_config($name, $value) | ||
| 114 | |||
| 115 | Sets the configuration item with the given name to the given value. | ||
| 116 | The value may be any Perl scalar that will serialize correctly using | ||
| 117 | C<Data::Dumper>. This includes references, objects (usually), and | ||
| 118 | complex data structures. It probably does not include transient | ||
| 119 | things like filehandles or sockets. | ||
| 120 | |||
| 121 | =item set_feature($name, $value) | ||
| 122 | |||
| 123 | Sets the feature with the given name to the given boolean value. The | ||
| 124 | value will be converted to 0 or 1 automatically. | ||
| 125 | |||
| 126 | =item config_names() | ||
| 127 | |||
| 128 | Returns a list of all the names of config items currently defined in | ||
| 129 | C<Mail::DMARC::ConfigData>, or in scalar context the number of items. | ||
| 130 | |||
| 131 | =item feature_names() | ||
| 132 | |||
| 133 | Returns a list of all the names of features currently defined in | ||
| 134 | C<Mail::DMARC::ConfigData>, or in scalar context the number of features. | ||
| 135 | |||
| 136 | =item auto_feature_names() | ||
| 137 | |||
| 138 | Returns a list of all the names of features whose availability is | ||
| 139 | dynamically determined, or in scalar context the number of such | ||
| 140 | features. Does not include such features that have later been set to | ||
| 141 | a fixed value. | ||
| 142 | |||
| 143 | =item write() | ||
| 144 | |||
| 145 | Commits any changes from C<set_config()> and C<set_feature()> to disk. | ||
| 146 | Requires write access to the C<Mail::DMARC::ConfigData> module. | ||
| 147 | |||
| 148 | =back | ||
| 149 | |||
| 150 | |||
| 151 | =head1 AUTHOR | ||
| 152 | |||
| 153 | C<Mail::DMARC::ConfigData> was automatically created using C<Module::Build>. | ||
| 154 | C<Module::Build> was written by Ken Williams, but he holds no | ||
| 155 | authorship claim or copyright claim to the contents of C<Mail::DMARC::ConfigData>. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | |||
| 160 | __DATA__ |