[savepoint] ng
authorHeiko Schlittermann <hs@schlittermann.de>
Mon, 19 Jul 2010 23:42:14 +0200
branchng
changeset 6 31f1e0c6344d
parent 5 2407e371ec7a
child 7 2492cea31acd
[savepoint]
lib/Exim.pm
run
--- a/lib/Exim.pm	Fri Jul 16 17:12:22 2010 +0200
+++ b/lib/Exim.pm	Mon Jul 19 23:42:14 2010 +0200
@@ -1,5 +1,22 @@
 package Exim;
 
+=head1 NAME
+
+    Exim - perl interface to the exim binary
+
+=head1 SYNOPSIS
+
+    use Exim;
+    my $exim = new Exim;
+    
+    print $exim->get("spool_directory");
+
+=head1 DESCRIPTION
+
+This package is an interface (wrapper) for calling the Exim binary.
+
+=cut
+
 use strict;
 use warnings;
 use Carp;
@@ -7,6 +24,28 @@
 my %basename;
 my %binary;
 my %config;
+my %cmdline;
+
+=head1 METHODS
+
+=head2 Constructor new([options])
+
+Creates a new Exim object. The options are
+
+=over
+
+=item basename => I<basename>
+
+The basename of the Exim binary. (default: exim4)
+
+=item config => I<config>
+
+The name and location of the configuration file. (default:
+I<basename>.conf)
+
+=back
+
+=cut
 
 sub new {
     my $class = ref $_[0] ? ref shift : shift;
@@ -16,26 +55,94 @@
 
     $basename{$self} = $arg{basename} ? $arg{basename} : "exim4";
     $config{$self}   = $arg{config}   ? $arg{config}   : "$arg{basename}.conf";
-    $binary{$self} = $self->_find_exim;
+    $binary{$self}  = $self->_find_exim;
+    $cmdline{$self} = {};
 
     return $self;
 }
 
+=head2 binary(I<>)
+
+Returns the path of the Exim binary.
+
+=cut
+
 sub binary {
     my $self = shift;
     return $binary{$self};
 }
 
+=head2 run(I<list>)
+
+Runs Exim and returns the result a C<qx//> does. (Thus depending on
+the context as one huge scalar or as a list of scalars.)
+
+=cut
+
 sub run {
     my $self = shift;
-    return qx/$binary{$self} -C $config{$self} @_/;
+    my @cmd  = (
+        $binary{$self},
+        -C => $config{$self},
+        %{ $cmdline{$self} },
+        @_
+    );
+
+    warn join ":", @cmd, "\n";
+    open(EXIM, "-|") or do {
+        exec @cmd;
+        die "Can't exec: $!\n";
+    };
+
+    my @rc = <EXIM>;
+
+    close(EXIM);
+    return @rc if wantarray;
+    return $rc[0];
+
 }
 
-sub get {
+=head2 get_option(I<name>)
+
+Returns the value of the named configuration parameter/option.
+(The may be passed with or without the leading dollar sign.)
+
+=cut
+
+sub get_option {
     my $self = shift;
-    chomp(local $_ = $self->run(-bP => shift));
+    chomp(local $_ = $self->run(-bP => ($_[0] =~ /^\$?(.*)/)));
     croak $_ unless /=/;
-    return /=\s*(.*)/;
+    return /[=]\s*(.*)/;    # the [] to get the syntax hiliter right
+}
+
+=head2 set_cmdline(I<arg> => I<value>[, ...])
+
+This sets a command line option to be used for all following invocations
+of Exim. To remove one option, just use C<undef> as value, to remove all
+set command line options, use an emptry list.
+
+=cut
+
+sub set_cmdline {
+    my $self = shift;
+    
+
+    if (defined $_[1]) { $cmdline{$self}->{ $_[0] } = $_[1] }
+    elsif (defined $_[0]) { delete $cmdline{$self}->{ $_[0] } }
+    else                  { $cmdline{$self} = {} }
+
+}
+
+=head2 expand(I<string>)
+
+Expands the given string (interface to C<-be>).
+
+=cut
+
+sub expand {
+    my $self = shift;
+    return $self->run(-be => shift);
 }
 
 sub DESTROY {
@@ -43,6 +150,7 @@
     delete $basename{$self};
     delete $binary{$self};
     delete $config{$self};
+    delete $cmdline{$self};
 }
 
 sub _find_exim {
@@ -56,3 +164,11 @@
 }
 
 1;
+
+=head1 AUTHOR
+
+Heiko Schlittermann L<<hs@schlittermann.de>>
+
+=cut
+
+__END__
--- a/run	Fri Jul 16 17:12:22 2010 +0200
+++ b/run	Mon Jul 19 23:42:14 2010 +0200
@@ -8,4 +8,5 @@
 
 my $exim = new Exim(basename => "exim4", config => "/etc/exim4/exim4.conf");
 
-print $exim->get("spool_directory");
+$exim->set_cmdline(-oMi => "1.1.1.1.22");
+print $exim->expand("\$received_port");