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
|
#!/usr/local/bin/perl -w
#-*- perl -*-
#
# Copyright (C) 2000 Ken'ichi Fukamachi
# All rights reserved.
#
# $FML$
#
package FML::Process::Distribute;
use vars qw($debug @ISA @EXPORT @EXPORT_OK);
use strict;
use Carp;
use FML::Process::Kernel;
use FML::Log qw(Log);
use FML::Config;
=head1 NAME
FML::Process::Distribute -- fml5 article distributer library.
=head1 SYNOPSIS
use FML::Process::Command;
...
See L<FML::Process::Flow> for details of flow.
=head1 DESCRIPTION
=cut
require Exporter;
@ISA = qw(FML::Process::Kernel Exporter);
sub new
{
my ($self, $args) = @_;
my $type = ref($self) || $self;
my $curproc = new FML::Process::Kernel $args;
return bless $curproc, $type;
}
sub prepare
{
my ($self, $args) = @_;
$self->SUPER::prepare($args);
}
sub verify_request
{
my ($curproc, $args) = @_;
$curproc->verify_sender_credential();
$curproc->simple_loop_check();
}
sub run
{
my ($curproc, $args) = @_;
$curproc->lock();
{
# user credential
my $cred = $curproc->{ credential };
# Q: the mail sender is a ML member?
if ($cred->is_member) {
# A: If so, we try to distribute this article.
_distribute( $curproc );
}
}
$curproc->unlock();
}
sub finish
{
my ($curproc, $args) = @_;
$curproc->inform_reply_messages();
}
# $article->header_rewrite;
# $article->increment_id;
# $article->spool;
# distribute( $article );
sub _distribute
{
my ($curproc, $args) = @_;
my $config = $curproc->{ config };
# XXX $ah is "article handler" object.
# XXX $ah != $curproc->{ article } (which is just a key)
# XXX $curproc->{ article } is prepared as a side effect.
my $ah = $curproc->_prepare_article($args);
# get sequence number
my $id = $ah->increment_id;
# XXX debug, remove here in the future
{
my $ha_msg = $curproc->{ article }->{ body }->get_content_type_list;
for (@$ha_msg) { Log($_);}
}
# ticket system checks the message before header rewritings.
$curproc->_ticket_check($args) if $config->yes('use_ticket');
# header operations
# XXX we need $curproc->{ article }, which is prepared above.
$curproc->_header_rewrite({ id => $id });
# spool in the article before delivery
$ah->spool_in($id);
# delivery starts !
$curproc->_deliver_article($args);
}
sub _prepare_article
{
my ($curproc, $args) = @_;
# create aritcle to distribute
use FML::Article;
# Side Effects: $article->{ curproc } = $curproc;
return new FML::Article $curproc;
}
sub _header_rewrite
{
my ($curproc, $args) = @_;
my $config = $curproc->{ config };
my $header = $curproc->{ article }->{ header };
my $rules = $curproc->{ config }->{ article_header_rewrite_rules };
my $id = $args->{ id };
for my $rule (split(/\s+/, $rules)) {
Log("_header_rewrite( $rule )") if $config->yes('debug');
if ($header->can($rule)) { # See FML::Header and FML::Header::*
$header->$rule($config, { # for methods themself
mode => 'distribute',
id => $id,
});
}
else {
Log("Error: header->$rule is undefined");
}
}
}
sub _deliver_article
{
my ($curproc, $args) = @_;
my $config = $curproc->{ config }; # FML::Config object;
my $body = $curproc->{ article }->{ body }; # MailingList::Messages object;
my $header = $curproc->{ article }->{ header };# FML::Header object;
unless ( $config->yes( 'use_article_delivery' ) ) {
return;
}
# SMTP-FROM is a must!
unless ( $config->{'maintainer'} ) {
Log("not delivery for undefined \$maintainer");
return;
}
# distribute article
use MailingList::Delivery;
my $fp = sub { Log(@_);}; # pointer to the log function
my $sfp = sub { my ($s) = @_; print $s; print "\n" if $s !~ /\n$/o;};
{ # debug
my $dir = $config->{ smtp_log_dir };
use File::Utils qw(mkdirhier);
mkdirhier($dir) unless -d $dir;
my $f = $config->{ smtp_log_file };
unlink $f if -f $f;
if ($f) {
use FileHandle;
my $fh = new FileHandle "> $f";
if (defined $fh) {
$fh->autoflush(1);
$sfp = sub { print $fh @_;};
}
}
}
my $service = new MailingList::Delivery {
log_function => $fp,
smtp_log_function => $sfp,
};
if ($service->error) { Log($service->error); return;}
$service->deliver(
{
'smtp_servers' => $config->{'smtp_servers'},
'smtp_sender' => $config->{'smtp_sender'},
'recipient_maps' => $config->{recipient_maps},
'recipient_limit' => $config->{recipient_limit},
'header' => $header,
'body' => $body,
});
if ($service->error) { Log($service->error); return;}
}
sub _ticket_check
{
my ($curproc, $args) = @_;
my $config = $curproc->{ config };
my $model = $config->{ ticket_model };
my $pkg = "FML::Ticket::Model::";
if ($model eq 'toymodel') {
$pkg .= $model;
}
else {
Log("ticket: unknown model");
return;
}
# fake use() to do "use FML::Ticket::$model;"
eval qq{ require $pkg; $pkg->import();};
unless ($@) {
my $ticket = $pkg->new($curproc, $args);
if (defined $ticket) {
$ticket->assign($curproc, $args);
$ticket->update_status($curproc, $args);
$ticket->update_db($curproc, $args);
}
else {
Log("creating ticket object failed");
}
}
else {
Log($@);
}
}
1;
|