summaryrefslogtreecommitdiff
path: root/fml/lib/FML/Process/Emulate.pm
blob: 72e5425c85b056d70d3920f5fa2ac4e259d2a99c (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
#-*- perl -*-
#
# Copyright (C) 2004,2005 Ken'ichi Fukamachi
#          All rights reserved.
#
# $FML: Emulate.pm,v 1.8 2005/08/10 11:28:44 fukachan Exp $
#

package FML::Process::Emulate;

use vars qw($debug @ISA @EXPORT @EXPORT_OK);
use strict;
use Carp;

use FML::Config;
use FML::Process::Kernel;
@ISA = qw(FML::Process::Kernel);


=head1 NAME

FML::Process::Emulate -- fml version 4's fml.pl emulator.

=head1 SYNOPSIS

   use FML::Process::Emulate;
   ...

See L<FML::Process::Flow> for details of the fml flow.

=head1 DESCRIPTION

C<FML::Process::Flow::ProcessStart($obj, $args)> drives the fml flow
where C<$obj> is the object C<FML::Process::$module::new()> returns.

=head1 METHOD

=head2 new($args)

create C<FML::Process::Emulate> object.
C<$curproc> is the object C<FML::Process::Kernel> returns but
we bless it as C<FML::Process::Emulate> object again.

=cut


# Descriptions: standard constructor.
#               sub class of FML::Process::Kernel
#    Arguments: OBJ($self) HASH_REF($args)
# Side Effects: none
# Return Value: OBJ(FML::Process::Emulate)
sub new
{
    my ($self, $args) = @_;
    my $type    = ref($self) || $self;
    my $curproc = new FML::Process::Kernel $args;
    return bless $curproc, $type;
}


=head2 prepare($args)

forward the request to the base class.
adjust ml_* and load configuration files.

=cut


# Descriptions: prepare miscellaneous work before the main routine starts.
#               adjust ml_* and load configuration files.
#    Arguments: OBJ($curproc) HASH_REF($args)
# Side Effects: none
# Return Value: none
sub prepare
{
    my ($curproc, $args) = @_;
    my $config           = $curproc->config();
    my $resolver_args    = {
	fallback => "config_cf_generate_from_config_ph",
    };

    # load only default configuration.
    my $default_config_cf = $curproc->default_config_cf_filepath();
    $config->load_file($default_config_cf);

    # go !
    $curproc->ml_variables_resolve($resolver_args);
    $curproc->config_cf_files_load();
    $curproc->env_fix_perl_include_path();
    $curproc->scheduler_init();
    $curproc->log_message_init();
    $curproc->_fml4_emulate();
}


# Descriptions: emulate fml4 (fml.pl in fact) process.
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub _fml4_emulate
{
    my ($curproc) = @_;
    my $option    = $curproc->command_line_options || {};
    my $myname    = $curproc->myname();

    if ($myname eq 'mead.pl') {
	$curproc->_fml4_emulate_error_process();
    }
    elsif ($myname eq 'msend.pl') {
	$curproc->_fml4_emulate_digest_process();
    }
    elsif (defined $option->{ ctladdr } && $option->{ ctladdr }) {
	$curproc->_fml4_emulate_command_mail_process();
    }
    else {
	$curproc->_fml4_emulate_post_article_process();
    }
}


# Descriptions: emulate fml4 (fml.pl in fact) process.
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub _fml4_emulate_post_article_process
{
    my ($curproc) = @_;
    my $config    = $curproc->config();

    $curproc->log("start as article_post mode");

    eval q{
	use FML::Process::Distribute;
	unshift(@ISA, "FML::Process::Distribute");
    };
    if ($@) {
	$curproc->logerror($@);
	croak("failed to initialize article_post process.");
    }

    if ($config->yes('use_article_post_function')) {
	$curproc->incoming_message_parse();
    }
    else {
	$curproc->logerror("use of distribute program prohibited");
	exit(0);
    }
}


# Descriptions: emulate fml4 (fml.pl in fact) process.
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub _fml4_emulate_command_mail_process
{
    my ($curproc) = @_;
    my $config    = $curproc->config();

    $curproc->log("start as command_mail mode");

    eval q{
	use FML::Process::Command;
	unshift(@ISA, "FML::Process::Command");
    };
    if ($@) {
	$curproc->logerror($@);
	croak("failed to initialize command_mail process.");
    }

    if ($config->yes('use_command_mail_function')) {
	$curproc->incoming_message_parse();
    }
    else {
	$curproc->logerror("use of command_mail program prohibited");
	exit(0);
    }
}


# Descriptions: emulate error (mead.pl in fact) process.
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub _fml4_emulate_error_process
{
    my ($curproc) = @_;
    my $config    = $curproc->config();

    # |/usr/local/fml/libexec/mead.pl \
    #  -E /usr/local/fml -S /var/spool/ml -D /var/spool/ml/elena

    # XXX-TODO: disabled for test ?
    $curproc->log("start as error mail analyzer mode");

    exit(0);

    eval q{
	use FML::Process::Error;
	unshift(@ISA, "FML::Process::Error");
    };
    if ($@) {
	$curproc->logerror($@);
	croak("failed to initialize error_mail process.");
    }

    if ($config->yes('use_error_mail_analyzer_function')) {
	$curproc->incoming_message_parse();
    }
    else {
	$curproc->logerror("use of error_mail program prohibited");
	exit(0);
    }
}


# Descriptions: emulate digest (msend.pl in fact) process.
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub _fml4_emulate_digest_process
{
    my ($curproc) = @_;
    my $config    = $curproc->config();

    # /usr/local/fml/msend.pl /var/spool/ml/elena
    $curproc->log("start as article digest mode");

    # XXX-TODO: disabled for test ?
    exit(0);

    eval q{
	use FML::Process::Digest;
	unshift(@ISA, "FML::Process::Digest");
    };
    if ($@) {
	$curproc->logerror($@);
	croak("failed to initialize digest_mail process.");
    }

    if ($config->yes('use_article_digest_function')) {
	;
    }
    else {
	$curproc->logerror("use of article digest program prohibited");
	exit(0);
    }
}


=head1 FALLBACK FOR ERROR RECOVORY

=head2 config_cf_generate_from_config_ph($fallback_args)

When fml.pl runs, it generates config.cf if config.cf does not exist.
This code is a subset of "fml $ml mergeml" command.

=cut


# Descriptions: generate config.cf if it does not exist.
#    Arguments: OBJ($curproc) HASH_REF($fallback_args)
# Side Effects: none
# Return Value: none
sub config_cf_generate_from_config_ph
{
    my ($curproc, $fallback_args) = @_;
    my $ml_home_dir    = '';
    my $config_cf_path = $fallback_args->{ config_cf_path };
    my $config_ph_path = $fallback_args->{ config_cf_path };
    $config_ph_path    =~ s/config.cf/config.ph/;

    use File::stat;
    my $stat_ph = stat($config_ph_path);
    my $stat_cf = stat($config_cf_path);
    if (! -f $config_ph_path && -f $config_cf_path) {
	# fml8 normal case.
	# OK, DO NOTHING.
    }
    elsif (($stat_ph->mtime > $stat_cf->mtime) ||
	   (-f $config_ph_path && ! -f $config_cf_path)) {
	# fml4 -> fml8 case (config.ph -> config.cf).
	$curproc->log("generate config.cf from config.ph");
	use File::Basename;
	my $ml_home_dir = dirname($config_cf_path);
	my $params = {
	    ml_home_dir   => $ml_home_dir,
	    src_dir       => $ml_home_dir,
	    target_system => "fml4",
	};
	$curproc->_fml4_merge($params);
    }
    elsif (-f $config_ph_path && -f $config_cf_path) {
	# OK, DO NOTHING.
    }
    else {
	# ? abnormal ???
    }
}


# Descriptions: merge ML configurations.
#    Arguments: OBJ($curproc) HASH_REF($params)
# Side Effects: none
# Return Value: none
sub _fml4_merge
{
    my ($curproc, $params) = @_;
    my $src_dir = $params->{ src_dir }       || undef;
    my $system  = $params->{ target_system } || undef;

    use FML::Merge;
    my $merge = new FML::Merge $curproc, $params;
    $merge->set_target_system($system);

    # 1. back up .
    $merge->backup_old_config_files();

    # XXX mergeml do this, but emulator not need this.
    #   2. fix include*.
    #   3. run newml --force.
    use FML::ML::Control;
    my $control = new FML::ML::Control;
    $control->install_config_cf($curproc, {}, $params);

    # 4. convert files if needed.
    $merge->convert_list_files();

    # XXX mergeml do this, but emulator not need this.
    #   5. analyze fml4 configuration and build diff only.
    #   6. translate diff into fml8 cf.
    $merge->merge_into_config_cf();

    # XXX mergeml do this, but emulator not need this.
    # 7. warning.
}


=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) 2004,2005 Ken'ichi Fukamachi

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

=head1 HISTORY

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

=cut


1;