From d2bfbb076a37395d0101d0edcc575a63b74bc458 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Sat, 1 Feb 2025 21:25:43 +0100 Subject: [PATCH] add function for getting early config without logging This supports reading early config parameters from environment variables or files. --- lib/Qpsmtpd.pm | 6 +++--- lib/Qpsmtpd/Config.pm | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/Qpsmtpd.pm b/lib/Qpsmtpd.pm index 14201c6..56932c1 100644 --- a/lib/Qpsmtpd.pm +++ b/lib/Qpsmtpd.pm @@ -59,19 +59,19 @@ sub load_logging { return if $LOGGING_LOADED; # already done return if $hooks->{'logging'}; # avoid triggering log activity - my @plugin_dirs = $self->conf->from_file('plugin_dirs'); + my @plugin_dirs = $self->conf->early_config('plugin_dirs'); if (!@plugin_dirs) { my ($name) = ($0 =~ m!(.*?)/([^/]+)$!); @plugin_dirs = ("$name/plugins"); } - my @loggers = $self->conf->from_file('logging'); + my @loggers = $self->conf->early_config('logging'); for my $logger (@loggers) { $self->_load_plugin($logger, @plugin_dirs); $self->log(LOGINFO, "Loaded $logger"); } - $TraceLevel = $self->conf->from_file('loglevel'); + $TraceLevel = $self->conf->early_config('loglevel'); unless (defined($TraceLevel) && $TraceLevel =~ /^\d+$/) { $TraceLevel = LOGWARN; # Default if no loglevel file found. diff --git a/lib/Qpsmtpd/Config.pm b/lib/Qpsmtpd/Config.pm index 4bf1b2c..4fed567 100644 --- a/lib/Qpsmtpd/Config.pm +++ b/lib/Qpsmtpd/Config.pm @@ -27,6 +27,17 @@ sub log { warn join(' ', $$, @log) . "\n"; } +sub early_config { + my ($self, $config, $file, $visited) = @_; + + my @config; + + @config = $self->from_environment($config); + return wantarray ? @config : $config[0] if @config; + + return $self->from_file($config, $file, $visited); +} + sub config { my ($self, $qp, $c, $type) = @_; @@ -34,6 +45,11 @@ sub config { # first run the user_config hooks my ($rc, @config); + + @config = $self->from_environment($c); + + return wantarray ? @config : $config[0] if @config; + if (ref $type && UNIVERSAL::can($type, 'address')) { ($rc, @config) = $qp->run_hooks_no_respond('user_config', $type, $c); if (defined $rc && $rc == OK) {