1 package Exim; |
1 package Exim; |
|
2 |
|
3 =head1 NAME |
|
4 |
|
5 Exim - perl interface to the exim binary |
|
6 |
|
7 =head1 SYNOPSIS |
|
8 |
|
9 use Exim; |
|
10 my $exim = new Exim; |
|
11 |
|
12 print $exim->get("spool_directory"); |
|
13 |
|
14 =head1 DESCRIPTION |
|
15 |
|
16 This package is an interface (wrapper) for calling the Exim binary. |
|
17 |
|
18 =cut |
2 |
19 |
3 use strict; |
20 use strict; |
4 use warnings; |
21 use warnings; |
5 use Carp; |
22 use Carp; |
6 |
23 |
7 my %basename; |
24 my %basename; |
8 my %binary; |
25 my %binary; |
9 my %config; |
26 my %config; |
|
27 my %cmdline; |
|
28 |
|
29 =head1 METHODS |
|
30 |
|
31 =head2 Constructor new([options]) |
|
32 |
|
33 Creates a new Exim object. The options are |
|
34 |
|
35 =over |
|
36 |
|
37 =item basename => I<basename> |
|
38 |
|
39 The basename of the Exim binary. (default: exim4) |
|
40 |
|
41 =item config => I<config> |
|
42 |
|
43 The name and location of the configuration file. (default: |
|
44 I<basename>.conf) |
|
45 |
|
46 =back |
|
47 |
|
48 =cut |
10 |
49 |
11 sub new { |
50 sub new { |
12 my $class = ref $_[0] ? ref shift : shift; |
51 my $class = ref $_[0] ? ref shift : shift; |
13 |
52 |
14 bless my $self = \my $anonymous => $class; |
53 bless my $self = \my $anonymous => $class; |
15 my %arg = @_; |
54 my %arg = @_; |
16 |
55 |
17 $basename{$self} = $arg{basename} ? $arg{basename} : "exim4"; |
56 $basename{$self} = $arg{basename} ? $arg{basename} : "exim4"; |
18 $config{$self} = $arg{config} ? $arg{config} : "$arg{basename}.conf"; |
57 $config{$self} = $arg{config} ? $arg{config} : "$arg{basename}.conf"; |
19 $binary{$self} = $self->_find_exim; |
58 $binary{$self} = $self->_find_exim; |
|
59 $cmdline{$self} = {}; |
20 |
60 |
21 return $self; |
61 return $self; |
22 } |
62 } |
|
63 |
|
64 =head2 binary(I<>) |
|
65 |
|
66 Returns the path of the Exim binary. |
|
67 |
|
68 =cut |
23 |
69 |
24 sub binary { |
70 sub binary { |
25 my $self = shift; |
71 my $self = shift; |
26 return $binary{$self}; |
72 return $binary{$self}; |
27 } |
73 } |
28 |
74 |
|
75 =head2 run(I<list>) |
|
76 |
|
77 Runs Exim and returns the result a C<qx//> does. (Thus depending on |
|
78 the context as one huge scalar or as a list of scalars.) |
|
79 |
|
80 =cut |
|
81 |
29 sub run { |
82 sub run { |
30 my $self = shift; |
83 my $self = shift; |
31 return qx/$binary{$self} -C $config{$self} @_/; |
84 my @cmd = ( |
|
85 $binary{$self}, |
|
86 -C => $config{$self}, |
|
87 %{ $cmdline{$self} }, |
|
88 @_ |
|
89 ); |
|
90 |
|
91 warn join ":", @cmd, "\n"; |
|
92 open(EXIM, "-|") or do { |
|
93 exec @cmd; |
|
94 die "Can't exec: $!\n"; |
|
95 }; |
|
96 |
|
97 my @rc = <EXIM>; |
|
98 |
|
99 close(EXIM); |
|
100 return @rc if wantarray; |
|
101 return $rc[0]; |
|
102 |
32 } |
103 } |
33 |
104 |
34 sub get { |
105 =head2 get_option(I<name>) |
|
106 |
|
107 Returns the value of the named configuration parameter/option. |
|
108 (The may be passed with or without the leading dollar sign.) |
|
109 |
|
110 =cut |
|
111 |
|
112 sub get_option { |
35 my $self = shift; |
113 my $self = shift; |
36 chomp(local $_ = $self->run(-bP => shift)); |
114 chomp(local $_ = $self->run(-bP => ($_[0] =~ /^\$?(.*)/))); |
37 croak $_ unless /=/; |
115 croak $_ unless /=/; |
38 return /=\s*(.*)/; |
116 return /[=]\s*(.*)/; # the [] to get the syntax hiliter right |
|
117 } |
|
118 |
|
119 =head2 set_cmdline(I<arg> => I<value>[, ...]) |
|
120 |
|
121 This sets a command line option to be used for all following invocations |
|
122 of Exim. To remove one option, just use C<undef> as value, to remove all |
|
123 set command line options, use an emptry list. |
|
124 |
|
125 =cut |
|
126 |
|
127 sub set_cmdline { |
|
128 my $self = shift; |
|
129 |
|
130 |
|
131 if (defined $_[1]) { $cmdline{$self}->{ $_[0] } = $_[1] } |
|
132 elsif (defined $_[0]) { delete $cmdline{$self}->{ $_[0] } } |
|
133 else { $cmdline{$self} = {} } |
|
134 |
|
135 } |
|
136 |
|
137 =head2 expand(I<string>) |
|
138 |
|
139 Expands the given string (interface to C<-be>). |
|
140 |
|
141 =cut |
|
142 |
|
143 sub expand { |
|
144 my $self = shift; |
|
145 return $self->run(-be => shift); |
39 } |
146 } |
40 |
147 |
41 sub DESTROY { |
148 sub DESTROY { |
42 my $self = shift; |
149 my $self = shift; |
43 delete $basename{$self}; |
150 delete $basename{$self}; |
44 delete $binary{$self}; |
151 delete $binary{$self}; |
45 delete $config{$self}; |
152 delete $config{$self}; |
|
153 delete $cmdline{$self}; |
46 } |
154 } |
47 |
155 |
48 sub _find_exim { |
156 sub _find_exim { |
49 my $self = shift; |
157 my $self = shift; |
50 my %seen; |
158 my %seen; |