modules/CONFIG: use configuration file (-c)
authorSebastian <basti@notizbuch>
Tue, 10 Jun 2014 22:49:58 +0000 (00:49 +0200)
committerSebastian <basti@notizbuch>
Tue, 10 Jun 2014 22:49:58 +0000 (00:49 +0200)
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

index 73bd38c662536ee8e9132654ea6d7c2092871d0e..13d990bd6edd3dba64acbc97da9a3d92feb00144 100644 (file)
@@ -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(<CONF>) {
+               # 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;