From cd040238114c91f4942e0847448a84830fac4f7c Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Thu, 23 Jul 2015 21:15:01 +0200 Subject: Allow custom database path. --- lib/Net/IMAP/Sync.pm | 54 +++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 24 deletions(-) (limited to 'lib/Net/IMAP') diff --git a/lib/Net/IMAP/Sync.pm b/lib/Net/IMAP/Sync.pm index 2c2a434..2aff76c 100644 --- a/lib/Net/IMAP/Sync.pm +++ b/lib/Net/IMAP/Sync.pm @@ -57,13 +57,13 @@ my %OPTIONS = ( ############################################################################# # Utilities -# read_config($conffile, $section, %opts) -# Read $conffile's default section, then $section (which takes -# precedence). %opts extends %OPTIONS and maps each option to a -# regexp validating its values. +# read_config($conffile, $sections, %opts) +# Read $conffile's default section, then each section in the array +# reference $section (which takes precedence). %opts extends %OPTIONS +# and maps each option to a regexp validating its values. sub read_config($$%) { my $conffile = shift; - my $section = shift; + my $sections = shift; my %opts = (%OPTIONS, @_); $conffile = ($ENV{XDG_CONFIG_HOME} // "$ENV{HOME}/.config") .'/'. $conffile @@ -73,26 +73,32 @@ sub read_config($$%) { unless defined $conffile and -f $conffile and -r $conffile; my $h = Config::Tiny::->read($conffile); - die "No such section $section\n" unless defined $h->{$section}; - - my $conf = $h->{_}; # default section - $conf->{$_} = $h->{$section}->{$_} foreach keys %{$h->{$section}}; - - # default values - $conf->{type} //= 'imaps'; - $conf->{host} //= 'localhost'; - $conf->{port} //= $conf->{type} eq 'imaps' ? 993 : $conf->{type} eq 'imap' ? 143 : undef; - $conf->{auth} //= 'PLAIN LOGIN'; - $conf->{STARTTLS} //= 'TRUE'; - - # untaint and validate the config - foreach my $k (keys %$conf) { - die "Invalid option $k\n" unless defined $opts{$k}; - next unless defined $conf->{$k}; - die "Invalid option $k = $conf->{$k}\n" unless $conf->{$k} =~ $opts{$k}; - $conf->{$k} = $1; + + my %configs; + foreach my $section (@$sections) { + my $conf = { %{$h->{_}} }; # default section + $configs{$section} = $conf; + next unless defined $section and $section ne '_'; + + die "No such section $section\n" unless defined $h->{$section}; + $conf->{$_} = $h->{$section}->{$_} foreach keys %{$h->{$section}}; + + # default values + $conf->{type} //= 'imaps'; + $conf->{host} //= 'localhost'; + $conf->{port} //= $conf->{type} eq 'imaps' ? 993 : $conf->{type} eq 'imap' ? 143 : undef; + $conf->{auth} //= 'PLAIN LOGIN'; + $conf->{STARTTLS} //= 'TRUE'; + + # untaint and validate the config + foreach my $k (keys %$conf) { + die "Invalid option $k\n" unless defined $opts{$k}; + next unless defined $conf->{$k}; + die "Invalid option $k = $conf->{$k}\n" unless $conf->{$k} =~ $opts{$k}; + $conf->{$k} = $1; + } } - return %$conf; + return \%configs; } -- cgit v1.2.3