summaryrefslogtreecommitdiff
path: root/fml/lib/FML/MTA/Control.pm
blob: 9d52742bd60d81bc984639bc17862e4ff07b0be2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#-*- perl -*-
#
#  Copyright (C) 2002,2003,2004 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Control.pm,v 1.4 2004/01/22 15:01:01 fukachan Exp $
#

package FML::MTA::Control;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;

use FML::MTA::Control::Postfix;
use FML::MTA::Control::Qmail;
use FML::MTA::Control::Procmail;
use FML::MTA::Control::Sendmail;
use FML::MTA::Control::Utils;
@ISA = qw(FML::MTA::Control::Postfix
	  FML::MTA::Control::Qmail
	  FML::MTA::Control::Procmail
	  FML::MTA::Control::Sendmail
	  FML::MTA::Control::Utils
	  );

my $debug = 0;


=head1 NAME

FML::MTA::Control - utilities to handle MTA specific configurations

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head2 new($args)

    $args = {
	mta_type => 'postfix',
    };

C<mta_type> is one of
    C<postfix>,
    C<qmail>
and
    C<procmail>.

=cut


my $default_mta = 'postfix';


# Descriptions: ordinary constructor.
#    Arguments: OBJ($self) HASH_REF($ctl_args)
# Side Effects: none
# Return Value: OBJ
sub new
{
    my ($self, $ctl_args) = @_;
    my ($type) = ref($self) || $self;
    my $me     = {};

    # default mta is postfix.
    $me->{ mta_type } = $ctl_args->{ mta_type } || $default_mta;

    return bless $me, $type;
}


# Descriptions: $mta_type is valid or not.
#    Arguments: OBJ($self) STR($mta_type)
# Side Effects: none
# Return Value: NUM(1 or 0)
sub is_valid_mta_type
{
    my ($self, $mta_type) = @_;

    if ($mta_type eq 'postfix'  ||
	$mta_type eq 'qmail'    ||
	$mta_type eq 'sendmail' ||
	$mta_type eq 'procmail') {
	return 1;
    }
    else {
	return 0;
    }
}


# Descriptions: install configuration template files.
#    Arguments: OBJ($self)
#               HASH_REF($curproc) HASH_REF($params) HASH_REF($optargs)
# Side Effects: update aliases
# Return Value: none
sub setup
{
    my ($self, $curproc, $params, $optargs) = @_;
    my $mta_type = $optargs->{ mta_type } || $self->{ mta_type };

    if ($self->is_valid_mta_type($mta_type)) {
	my $method = "${mta_type}_setup";
	$self->$method($curproc, $params, $optargs);
    }
    else {
	croak("unknown MTA");
    }
}


# Descriptions: update alias.db from alias file
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) HASH_REF($optargs)
# Side Effects: update aliases
# Return Value: none
sub update_alias
{
    my ($self, $curproc, $params, $optargs) = @_;
    my $mta_type = $optargs->{ mta_type } || $self->{ mta_type };

    if ($self->is_valid_mta_type($mta_type)) {
	my $method = "${mta_type}_update_alias";
	$self->$method($curproc, $params, $optargs);
    }
    else {
	croak("unknown MTA");
    }
}


# Descriptions: find key in alias maps
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) HASH_REF($optargs)
# Side Effects: none
# Return Value: none
sub find_key_in_alias_maps
{
    my ($self, $curproc, $params, $optargs) = @_;
    my $mta_type = $optargs->{ mta_type } || $self->{ mta_type };
    my $key      = $optargs->{ key };

    if ($self->is_valid_mta_type($mta_type)) {
	my $method = "${mta_type}_find_key_in_alias_maps";
	$self->$method($curproc, $params, $optargs);
    }
    else {
	croak("unknown MTA");
    }
}


# Descriptions: return aliases as HASH_REF
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) HASH_REF($optargs)
# Side Effects: none
# Return Value: HASH_REF
sub get_aliases_as_hash_ref
{
    my ($self, $curproc, $params, $optargs) = @_;
    my $mta_type = $optargs->{ mta_type } || $self->{ mta_type };

    if ($self->is_valid_mta_type($mta_type)) {
	my $method = "${mta_type}_get_aliases_as_hash_ref";
	$self->$method($curproc, $params, $optargs);
    }
    else {
	croak("unknown MTA");
    }
}


# Descriptions: install alias file.
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) HASH_REF($optargs)
# Side Effects: update aliases
# Return Value: none
sub install_alias
{
    my ($self, $curproc, $params, $optargs) = @_;
    my $mta_type = $optargs->{ mta_type } || $self->{ mta_type };

    if ($self->is_valid_mta_type($mta_type)) {
	my $method = "${mta_type}_install_alias";
	$self->$method($curproc, $params, $optargs);
    }
    else {
	croak("unknown MTA");
    }
}


# Descriptions: remove entry in alias maps.
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) HASH_REF($optargs)
# Side Effects: update aliases
# Return Value: none
sub remove_alias
{
    my ($self, $curproc, $params, $optargs) = @_;
    my $mta_type = $optargs->{ mta_type } || $self->{ mta_type };

    if ($self->is_valid_mta_type($mta_type)) {
	my $method = "${mta_type}_remove_alias";
	$self->$method($curproc, $params, $optargs);
    }
    else {
	croak("unknown MTA");
    }
}


# Descriptions: install/update virtual map file.
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) HASH_REF($optargs)
# Side Effects: update aliases
# Return Value: none
sub install_virtual_map
{
    my ($self, $curproc, $params, $optargs) = @_;
    my $mta_type = $optargs->{ mta_type } || $self->{ mta_type };

    if ($self->is_valid_mta_type($mta_type)) {
	my $method = "${mta_type}_install_virtual_map";
	$self->$method($curproc, $params, $optargs);
    }
    else {
	croak("unknown MTA");
    }
}


# Descriptions: remove entry in virtual maps.
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) HASH_REF($optargs)
# Side Effects: update aliases
# Return Value: none
sub remove_virtual_map
{
    my ($self, $curproc, $params, $optargs) = @_;
    my $mta_type = $optargs->{ mta_type } || $self->{ mta_type };

    if ($self->is_valid_mta_type($mta_type)) {
	my $method = "${mta_type}_remove_virtual_map";
	$self->$method($curproc, $params, $optargs);
    }
    else {
	croak("unknown MTA");
    }
}


# Descriptions: update virtual_map.
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) HASH_REF($optargs)
# Side Effects: update virtual_map
# Return Value: none
sub update_virtual_map
{
    my ($self, $curproc, $params, $optargs) = @_;
    my $mta_type = $optargs->{ mta_type } || $self->{ mta_type };

    if ($self->is_valid_mta_type($mta_type)) {
	my $method = "${mta_type}_update_virtual_map";
	$self->$method($curproc, $params, $optargs);
    }
    else {
	croak("unknown MTA");
    }
}


# Descriptions: install file $to dst with variable expansion of $src.
#    Arguments: OBJ($self) STR($src) STR($dst) HASH_REF($config)
# Side Effects: create $dst
# Return Value: none
sub _install
{
    my ($self, $src, $dst, $config) = @_;

    # XXX-TODO: method-ify ?
    eval q{
	use FML::Config::Convert;
	&FML::Config::Convert::convert_file($src, $dst, $config);
    };
    croak($@) if $@;
}


# Descriptions: remove the specified entry in the postfix style map.
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params) HASH_REF($optargs)
#               HASH_REF($p)
# Side Effects: update virtual_map
# Return Value: none
sub _remove_postfix_style_virtual
{
    my ($self, $curproc, $params, $optargs, $p) = @_;
    my $removed = 0;
    my $key     = $p->{ key };

    #
    # XXX-TODO: ERROR: this module is private but used over modules.
    #

    use File::Spec;
    my $virtual     = $p->{ map };
    my $virtual_new = $virtual . 'new'. $$;

    if (-f $virtual) {
	$curproc->ui_message("removing $key in $virtual");
    }
    else {
	return;
    }

    use FileHandle;
    my $rh = new FileHandle $virtual;
    my $wh = new FileHandle "> $virtual_new";
    if (defined $rh && defined $wh) {
	my $buf;

      LINE:
	while ($buf = <$rh>) {
	    if ($buf =~ /\<VIRTUAL\s+$key\@/
		   ..
		$buf =~ /\<\/VIRTUAL\s+$key\@/) {
		$removed++;
		next LINE;
	    }

	    print $wh $buf;
	}
	$wh->close;
	$rh->close;

	if ($removed > 3) {
	    if (rename($virtual_new, $virtual)) {
		$curproc->ui_message("removed");
	    }
	    else {
		my $s = "fail to rename virtual files";
		$curproc->ui_message("error: $s");
		$curproc->logerror($s);
	    }
	}
    }
    else {
	warn("cannot open $virtual")     unless defined $rh;
	warn("cannot open $virtual_new") unless defined $wh;
    }
}


=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2002,2003,2004 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

FML::MTA::Control first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;