summaryrefslogtreecommitdiff
path: root/fml/lib/FML/Article/Summary.pm
blob: 93cd5700b6690b0f8970c69378664ce78881a8d6 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#-*- perl -*-
#
#  Copyright (C) 2002,2003,2004,2005,2006 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: Summary.pm,v 1.27 2006/05/05 13:24:10 fukachan Exp $
#

package FML::Article::Summary;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
use Mail::Message::Date;


=head1 NAME

FML::Article::Summary - generate article summary.

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head2 new()

constructor.

=cut


# Descriptions: constructor.
#    Arguments: OBJ($self) OBJ($curproc)
# Side Effects: $self->{ _curproc } = $curproc;
# Return Value: OBJ
sub new
{
    my ($self, $curproc) = @_;
    my ($type) = ref($self) || $self;
    my $me     = {};

    $me->{ _curproc } = $curproc;

    return bless $me, $type;
}


# Descriptions: append summary to $article_summary_file.
#    Arguments: OBJ($self) HANDLE($wh) NUM($id)
# Side Effects: update $article_summary_file file.
# Return Value: none
sub print
{
    my ($self, $wh, $id) = @_;
    my $curproc = $self->{ _curproc };
    my $config  = $curproc->config();

    # XXX last resort == STDOUT.
    $wh ||= \*STDOUT;

    if (defined $wh) {
	my $info = $self->_prepare_info($id);
	if (defined $info) {
	    $self->print_one_line_summary($wh, $info);
	}
    }
}


# Descriptions: prepare information on article $id for later use.
#               such as $id, $address, $subject et.al.
#    Arguments: OBJ($self) NUM($id)
# Side Effects: none
# Return Value: HASH_REF
sub _prepare_info
{
    my ($self, $id) = @_;
    my $curproc = $self->{ _curproc };
    my $config  = $curproc->config();
    my $tag     = $config->{ article_subject_tag };
    my $addrlen = $config->{ article_summary_file_format_address_length };
    my $article = undef;

    if (defined $self->{ _article }) {
	$article = $self->{ _article };
    }
    else {
	# XXX we need article object to use $article->filepath() method.
	eval q{ use FML::Article;};
	$article = new FML::Article $curproc;
    }

    my $file = $article->filepath($id);
    if (-f $file) {
	use Mail::Message;
	my $msg      = new Mail::Message->parse( { file => $file } );
	my $header   = $msg->whole_message_header();
	my $address  = $header->get('from')    || '';
	my $date_str = $header->get('date')    || '';
	my $subject  = $header->get('subject') || '';

	# data -> unix time.
	use Mail::Message::Date;
	my $date     = new Mail::Message::Date $date_str;
	my $unixtime = $date->as_unixtime();

	# log the first 15 bytes of user@domain in From: header field.
	if ($address) {
	    use Mail::Message::Address;
	    my $addr = new Mail::Message::Address $address;
	    $addr->cleanup();
	    $address = $addr->substr(0, $addrlen) || '';
	}

	# de-tag, unfold, and charset conversion.
	# XXX as_external_form() returns the "printable" string
	# XXX which encoding is EUC by default but should be changed to UTF-8.
	# XXX The "printable" string is used for output to local files
	# XXX e.g. "summary", "log" NOT mail tranfer.
	if ($subject) {
	    use Mail::Message::Subject;
	    my $sbj = new Mail::Message::Subject $subject;
	    $sbj->mime_header_decode();
	    $sbj->unfold();
	    $sbj->delete_tag($tag);
	    $sbj->unfold();
	    $subject = $sbj->as_external_form();
	}

	my $info = {
	    id       => $id,
	    address  => $address,
	    subject  => $subject,
	    unixtime => $unixtime,
	};

	return $info;
    }
    else {
	return undef;
    }
}


# Descriptions: one line version of print().
#    Arguments: OBJ($self) HANDLE($wh) HASH_REF($info)
# Side Effects: none
# Return Value: none
sub print_one_line_summary
{
    my ($self, $wh, $info) = @_;
    my $curproc = $self->{ _curproc };
    my $config  = $curproc->config();
    my $style   = $config->{ 'article_summary_file_format_style' };

    if ($style eq 'fml4_compatible') {
	$self->_fml4_compatible_style_one_line_summary($wh, $info);
    }
    else {
	$curproc->logerror("unknown \$article_summary_file_style: $style");
    }
}


# Descriptions: write out formatted string into $article_summary_file.
#    Arguments: OBJ($self) HANDLE($wh) HASH_REF($info)
# Side Effects: update $article_summary_file.
# Return Value: none
sub _fml4_compatible_style_one_line_summary
{
    my ($self, $wh, $info) = @_;
    my $curproc = $self->{ _curproc };
    my $time    = $info->{ unixtime } || undef;
    my $rdate   = undef;

    if ($time) {
	use Mail::Message::Date;
	$rdate = new Mail::Message::Date $time;
    }
    else {
	$curproc->logerror("unix time undefined");
    }

    if (defined $rdate) {
	my $date   = $rdate->{ 'log_file_style' };
	my $format = "%s [%d:%s] %s\n";
	my $id     = $info->{ id };
	my $addr   = $info->{ address };
	my $subj   = $info->{ subject };

	printf $wh $format, $date, $id, $addr, $subj;
    }
    else {
	$curproc->logerror("date object undefined.");
    }
}


=head1 UTILITIES

=head2 append($article, $id)

append summary information for article $id into the article summary file.

=cut


# Descriptions: append summary information for article $id.
#    Arguments: OBJ($self) OBJ($article) NUM($id)
# Side Effects: update summary
# Return Value: none
sub append
{
    my ($self, $article, $id) = @_;
    my $curproc = $self->{ _curproc };
    my $config  = $curproc->config();
    my $file    = $config->{ 'article_summary_file' };

    if (defined $article) {
	$self->{ _article } = $article;
    }

    use FileHandle;
    my $wh = new FileHandle ">> $file";
    if (defined $wh) {
	$wh->autoflush(1);
	$self->print($wh, $id);
	$wh->close();
    }
}


# Descriptions: re-genearete summary from $min to $max.
#    Arguments: OBJ($self) NUM($min) NUM($max)
# Side Effects: re-create $summary file.
# Return Value: none
sub rebuild
{
    my ($self, $min, $max) = @_;
    my $curproc = $self->{ _curproc };
    my $config  = $curproc->config();
    my $file    = $config->{ 'article_summary_file' };
    my $tmp     = "$file.new.$$";

    use FileHandle;
    my $wh = new FileHandle ">> $tmp";
    if (defined $wh) {
	$wh->autoflush(1);

	# speculate boundary if not specified.
	$min ||= 1;
	$max ||= $curproc->article_get_max_id();

	for my $id ($min .. $max) {
	    $self->print($wh, $id);
	}
	$wh->close();
    }

    if (-s $tmp) {
	rename($tmp, $file);
    }
    else {
	$curproc->logerror("fail to write $tmp");
    }
}


# Descriptions: print all lines in summary file into file handle $wh.
#    Arguments: OBJ($self) HANDLE($wh)
# Side Effects: none
# Return Value: none
sub dump
{
    my ($self, $wh) = @_;
    my $curproc     = $self->{ _curproc };
    my $config      = $curproc->config();
    my $article_summary_file = $config->{ "article_summary_file" };

    if (-f $article_summary_file) {
	my $rh = new FileHandle $article_summary_file;
	if (defined $rh && defined $wh) {
	    my $buf;
	    while ($buf = <$rh>) { print $wh $buf;}
	    $rh->close();
	}
    }
}


# Descriptions: remove content corresponding with expired articles.
#    Arguments: OBJ($self) NUM($first_seq) NUM($last_seq)
# Side Effects: summary rebuilt.
# Return Value: none
sub expire
{
    my ($self, $first_seq, $last_seq)  = @_;
    my $curproc = $self->{ _curproc };
    my $config  = $curproc->config();

    my $summary_style = $config->{ article_summary_file_format_style };
    if ($summary_style eq 'fml4_compatible') {
        $self->_expire_fml4_compatible_style_summary($first_seq, $last_seq);
    }
    else {
	my $errmsg = "unknown \$article_summary_file_style: $summary_style";
        $curproc->logerror($errmsg);
    }

}


# Descriptions: remove content corresponding with expired articles.
#    Arguments: OBJ($self) NUM($first_seq) NUM($last_seq)
# Side Effects: summary rebuilt.
# Return Value: none
sub _expire_fml4_compatible_style_summary
{
    my ($self, $first_seq, $last_seq)  = @_;
    my $curproc = $self->{ _curproc };
    my $config  = $curproc->config();

    # 06/05/03 16:31:16 [1268:fukachan@home.f] SUMMARY
    my $summary_file = $config->{ article_summary_file };
    my $tmp_file     = $curproc->tmp_file_path();
    if (-f $summary_file) {
	use FileHandle;
	my $rh = new FileHandle $summary_file;
	my $wh = new FileHandle "> $tmp_file";
	if (defined $rh && defined $wh) {
	    my $buf;
	  LINE:
	    while ($buf = <$rh>) {
		if ($buf =~ /\[(\d+):/) {
		    my $seq = $1;
		    next LINE if $seq <= $last_seq;
		}
		print $wh $buf;
	    }
	    $wh->close();
	    $rh->close();
	}
	else {
	    $curproc->logerror("cannot open summary file") unless defined $rh;
	    $curproc->logerror("cannot open tmp file")     unless defined $wh;
	}

	unless (-s $tmp_file) {
	    $curproc->logerror("expire: tmporary file creation fail");
	    return;
	}

	if (rename($tmp_file, $summary_file)) {
	    $curproc->logdebug("expire: summary file rebuilt");
	}
	else {
	    $curproc->logerror("expire: summary file rebuilding fail");
	}
    }
}


=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,2005,2006 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::Article::Summary appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;