| 1 | package SpamMonkey::Config; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.01'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | SpamMonkey::Config - Read SpamMonkey configuration files | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use SpamMonkey::Config; | ||
| 16 | my $conf = SpamMonkey::Config->new(); | ||
| 17 | $conf->read("foo.cf"); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | SpamMonkey configuration files look suspiciously similar to SpamAssassin | ||
| 22 | configuration files; with the current exception of conditionals, meta | ||
| 23 | rules and certain DNSBL rules, which are not supported. | ||
| 24 | |||
| 25 | SpamMonkey however does require a C<00default.cf> to set defaults; they | ||
| 26 | are not hard-coded. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | sub new { my $class = shift; bless {}, $class; } | ||
| 31 | |||
| 32 | sub mylang { "en" } | ||
| 33 | |||
| 34 | sub read { | ||
| 35 | my ($self, $file) = @_; | ||
| 36 | open my $in, $file or die "Can't open $file: $!"; | ||
| 37 | $self->{file} = $file; | ||
| 38 | while (<$in>) { $self->parse_line($_) } | ||
| 39 | delete $self->{file}; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub parser_package { "SpamMonkey::Config::Parser" } | ||
| 43 | |||
| 44 | sub parse_line { | ||
| 45 | my ($self, $line) = @_; | ||
| 46 | $self->{pp} ||= $self->parser_package; | ||
| 47 | $line =~ s/(?<!\\)#.*$//; # remove comments | ||
| 48 | $line =~ s/^\s+//; # remove leading whitespace | ||
| 49 | $line =~ s/\s+$//; # remove tailing whitespace | ||
| 50 | return unless($line); # skip empty lines | ||
| 51 | #warn "Parsing $line"; | ||
| 52 | |||
| 53 | $self->{line} = $line; | ||
| 54 | my($key, $value) = split(/\s+/, $line, 2); | ||
| 55 | $key = lc $key; | ||
| 56 | # convert all dashes in setting name to underscores. | ||
| 57 | $key =~ s/-/_/g; | ||
| 58 | if ($key eq "ifplugin" or $key eq "if" or $key eq "endif") { | ||
| 59 | return; # XXX don't support conditionals | ||
| 60 | } elsif ($key eq "include") { | ||
| 61 | die "Include $value"; | ||
| 62 | } elsif ($key !~ /^_/ and my $code = $self->{pp}->can($key)) { | ||
| 63 | $code->($self->{pp},$self, ($value||"")); | ||
| 64 | } else { | ||
| 65 | if (exists $self->{settings}{$key}) { | ||
| 66 | # This may be unsound | ||
| 67 | if (ref $self->{settings}{$key}) { | ||
| 68 | push @{$self->{settings}{$key}}, $value; | ||
| 69 | } else { | ||
| 70 | $self->{settings}{$key} = [$self->{settings}{$key}, $value ]; | ||
| 71 | } | ||
| 72 | } else { | ||
| 73 | $self->{settings}{$key} = $value; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | delete $self->{line}; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub die { | ||
| 80 | my ($self, $mess) = @_; | ||
| 81 | die "Couldn't parse line $.\n<$self->{line}>\n in $self->{file}: $mess\n"; | ||
| 82 | } | ||
| 83 | |||
| 84 | package SpamMonkey::Config::Parser; | ||
| 85 | |||
| 86 | sub _split_params { | ||
| 87 | my ($class, $conf, $value, $params) = @_; | ||
| 88 | my ($key) = (caller(1))[3]; | ||
| 89 | $key =~ s/.*:://; | ||
| 90 | my @values = split(/\s+/, $value, $params); | ||
| 91 | if (@values != $params) { | ||
| 92 | $conf->die("Missing value for '$key' directive"); | ||
| 93 | } | ||
| 94 | @values; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub require_version {}; | ||
| 98 | |||
| 99 | sub compile_rule { | ||
| 100 | my ($class, $conf, $rule) = @_; | ||
| 101 | if ($rule =~ /^\// or $rule =~ s/^m(\W)/$1/) { | ||
| 102 | my $regexp = eval "qr${rule}o"; | ||
| 103 | $conf->die("Couldn't understand $rule: $@") if $@; | ||
| 104 | return $regexp; | ||
| 105 | } else { $conf->die("Malformed regexp $rule"); } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _body { | ||
| 109 | my ($class, $conf, $value) = @_; | ||
| 110 | my ($type) = (caller(1))[3]; | ||
| 111 | $type =~ s/.*:://; | ||
| 112 | #warn "Seen $type"; | ||
| 113 | if ($value =~ /^(\S+)\s+eval:(.*)$/) { | ||
| 114 | $conf->{rules}{$type}{$1} = { op => "eval", code => $2 } | ||
| 115 | } else { | ||
| 116 | my ($name, $rule) = $class->_split_params($conf, $value, 2); | ||
| 117 | my $regexp = $class->compile_rule($conf,$rule); | ||
| 118 | $conf->{rules}{$type}{$name} = $regexp; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | sub body { shift->_body(@_) } | ||
| 123 | sub rawbody { shift->_body(@_) } | ||
| 124 | sub full { shift->_body(@_) } | ||
| 125 | |||
| 126 | sub describe { | ||
| 127 | my ($class, $conf, $value) = @_; | ||
| 128 | my ($name, $description) = $class->_split_params($conf, $value, 2); | ||
| 129 | $conf->{descriptions}{$name} = $description; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub test { } # That's what SpamAssassin is for (Heh, heh) | ||
| 133 | |||
| 134 | sub uri { | ||
| 135 | my ($class, $conf, $value) = @_; | ||
| 136 | my ($name, $rule) = $class->_split_params($conf, $value, 2); | ||
| 137 | my $regexp = $class->compile_rule($conf,$rule); | ||
| 138 | $conf->{rules}{uri}{$name} = $regexp; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub header { | ||
| 142 | my ($class, $conf, $value) = @_; | ||
| 143 | my ($name, $rule) = $class->_split_params($conf, $value, 2); | ||
| 144 | my $unset; | ||
| 145 | if ($rule =~ s{(?:\s*\[if-unset:\s*(.*)\])?$}{}) { $unset = $1; } | ||
| 146 | if ($rule =~ /^\s*([\w:\-_]+)\s*(=~|!~)\s*(.*)/) { | ||
| 147 | my $regexp = $class->compile_rule($conf,$3); | ||
| 148 | $conf->{rules}{header}{$name} = { header => $1, op => $2, re => $regexp}; | ||
| 149 | $conf->{rules}{header}{$name}{unset} = $unset if $unset; | ||
| 150 | } elsif ($rule =~ /\s*exists:(\S+)/) { | ||
| 151 | $conf->{rules}{header}{$name} = { header => $1, op => "exists" } | ||
| 152 | } elsif ($rule =~ /\s*eval:(.*)/) { | ||
| 153 | $conf->{rules}{header}{$name} = { op => "eval", code => $1 } | ||
| 154 | } else { | ||
| 155 | $conf->die("Couldn't parse $rule"); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | sub clear_report_template { my ($class, $conf) = @_; $conf->{settings}{report_template} = ""; } | ||
| 160 | sub clear_unsafe_report_template { my ($class, $conf) = @_; $conf->{settings}{unsafe_report_template} = ""; } | ||
| 161 | sub clear_headers { my ($class, $conf) = @_; $conf->{settings}{headers} = {}; } | ||
| 162 | |||
| 163 | sub report { my ($class, $conf, $value) = @_; $conf->{settings}{report_template} .= $value."\n"; } | ||
| 164 | sub unsafe_report { my ($class, $conf, $value) = @_; $conf->{settings}{unsafe_report_template} .= $value."\n"; } | ||
| 165 | |||
| 166 | sub add_header { | ||
| 167 | my ($class, $conf, $value) = @_; | ||
| 168 | my ($reason,$name, $thing) = $class->_split_params($conf, $value, 3); | ||
| 169 | push @{$conf->{settings}{headers}{$reason}}, {$name => $thing}; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub meta { } # XXX We don't support meta rules | ||
| 173 | |||
| 174 | sub _splitter { | ||
| 175 | my ($class, $conf, $value) = @_; | ||
| 176 | my ($type) = (caller(1))[3]; | ||
| 177 | $type =~ s/.*:://; | ||
| 178 | my ($name, $thing) = $class->_split_params($conf, $value, 2); | ||
| 179 | $conf->{$type}{$name} = [ split /\s+/, $thing ]; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub tflags { shift->_splitter(@_) } | ||
| 183 | sub score { shift->_splitter(@_) } | ||
| 184 | |||
| 185 | sub lang { | ||
| 186 | my ($class, $conf, $value) = @_; | ||
| 187 | my ($lang, $thing) = $class->_split_params($conf, $value, 2); | ||
| 188 | if ($lang eq $conf->mylang) { $conf->parse_line($thing); } | ||
| 189 | } | ||
| 190 | |||
| 191 | sub priority { | ||
| 192 | my ($class, $conf, $value) = @_; | ||
| 193 | my ($name, $score) = $class->_split_params($conf, $value, 2); | ||
| 194 | $conf->{priority}{$name} = $score; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub whitelist_from_rcvd { | ||
| 198 | my ($class, $conf, $value) = @_; | ||
| 199 | my ($addr, $ip) = $class->_split_params($conf, $value, 2); | ||
| 200 | push @{$conf->{whitelist_from_rcvd}}, [ $addr, $ip ]; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub def_whitelist_from_rcvd { | ||
| 204 | my ($class, $conf, $value) = @_; | ||
| 205 | my ($addr, $ip) = $class->_split_params($conf, $value, 2); | ||
| 206 | push @{$conf->{def_whitelist_from_rcvd}}, [ $addr, $ip ]; | ||
| 207 | } | ||
| 208 | |||
| 209 | =head1 AUTHOR | ||
| 210 | |||
| 211 | simon, E<lt>simon@E<gt> | ||
| 212 | |||
| 213 | =head1 COPYRIGHT AND LICENSE | ||
| 214 | |||
| 215 | Copyright (C) 2005 by simon | ||
| 216 | |||
| 217 | This library is free software; you can redistribute it and/or modify | ||
| 218 | it under the same terms as Perl itself, either Perl version 5.8.7 or, | ||
| 219 | at your option, any later version of Perl 5 you may have available. | ||
| 220 | |||
| 221 | |||
| 222 | =cut |