From ec840ce0832ff8ba9abfb79edf82838bc35c92f3 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 11 Jun 2014 00:49:58 +0200 Subject: [PATCH] modules/CONFIG: use configuration file (-c) All applications using the CONFIG module now require a "-c /path/to/config" parameter. Loading the module fails if this file is missing or incomplete. --- modules/CONFIG.pm | 49 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/modules/CONFIG.pm b/modules/CONFIG.pm index 73bd38c..13d990b 100644 --- a/modules/CONFIG.pm +++ b/modules/CONFIG.pm @@ -2,22 +2,45 @@ use strict; use v5.012; +use MISC; package CONFIG; -our %config = ( - 'address' => [ 2, 240, 8001, 7 ], # my address - 'in_charset' => 'cp437', # input charset if unknown +our %config; - # Message Base - 'dbase_driver' => 'sqlite', - 'dbase_path' => '/home/basti/fido/msgbase', - 'dbase_user' => '', - 'dbase_pass' => '', +# read and parse configuration file + my ($argidx) = grep $ARGV[$_] eq '-c', 0..$#ARGV; + if(!defined $argidx || !defined $ARGV[$argidx+1]) { + die("Please specify a configuration file (-c config)!\n\n"); + } + open(CONF, '<', $ARGV[$argidx+1]) or + die("Can't open '$ARGV[$argidx+1]': $!!\n\n"); + while() { + # sanitize input line + chomp; + next unless(/(\S)/); # empty lines + next if(/^[#;]/); # comment lines + s/^\s*//; s/\s*$//; # remove whitespace - # Special Areas - 'areafix' => 'AREAFIX', - 'netmail' => 'NETMAIL', - 'outbound' => 'OUTBOUND', -); + # save valid lines + my ($key, $value) = split(/\s*=\s*/, $_); + $value =~ /^\"(.*)\"$/; $value = $1; + $config{lc $key}=$value; + } + close(CONF); + +# check mandatory keywords + my @keywords = ( + 'address', 'in_charset', 'out_charset', + 'dbase_driver', 'dbase_path', 'dbase_user', 'dbase_pass', + 'netmail', 'outbound', 'areafix', + ); + foreach my $key (@keywords) { + if(!defined $config{$key}) { + die("Keyword '$key' missing in configuration!\n\n"); + } + } + +# fix non-strings + $config{address} = MISC::text2fido($config{address}); 1; -- 2.30.2