aboutsummaryrefslogtreecommitdiffstats
path: root/lib/SpamCat/Conf.pm
blob: ab6a3ffbf7fc6e623f641b1f1efb4dc05e55ed19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package SpamCat::Conf;

use IO::File;

use strict;
use warnings;

sub read {
  my ($filen) = @_;
  my %rc;

  my $fh = IO::File->new($filen) ||
    die "Couldn't open $filen for reading: $!\n";
  while (<$fh>) {
    my ($key, $val) = parse_line($_);
    if (defined $key && defined $val) {
      $rc{$key} = $val;
    }
  }
  $fh->close;

  %rc;
}

sub parse_line {
  my ($line) = @_;

  chomp $line;
  $line =~ s/(.*)#.*/$1/;
  $line =~ s/\s+$//;

  if ($line =~ /\s*([^\s]*)\s*=\s*(.*)$/) {
    my $key = lc $1;
    my $val = $2;

    if ($key eq 'domains') {
      $val =~ s/,/ /g;
      my @vals = split /\s+/, $val;
      $val = \@vals;
    }
    return ($key, $val);
  }
}

1;