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
|
#-*- perl -*-
#
# Copyright (C) 2002,2003,2004,2005,2006,2008 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: Cache.pm,v 1.25 2006/11/19 03:41:45 fukachan Exp $
#
package FML::Error::Cache;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
=head1 NAME
FML::Error::Cache - manipulate error/bounce information database.
=head1 SYNOPSIS
use FML::Error::Cache;
my $db = new FML::Error::Cache $curproc;
$db->add( $bounce_info );
where C<$bounce_info> follows:
$bounce_info = [
{
address => 'rudo@nuinui.net',
status => '5.x.y',
reason => '... reason ... ',
},
...
];
=head1 DESCRIPTION
This class provides database manipluation.
=head1 METHODS
=head2 new()
constructor.
=cut
# Descriptions: constructor.
# Arguments: OBJ($self) HASH_REF($curproc)
# Side Effects: create a cache directory.
# Return Value: OBJ
sub new
{
my ($self, $curproc) = @_;
my ($type) = ref($self) || $self;
my $me = { _curproc => $curproc };
my $config = $curproc->config();
my $db_dir = $config->{ error_mail_analyzer_cache_dir };
unless (-d $db_dir) {
$curproc->mkdir($db_dir, "mode=private");
}
use Tie::JournaledDir;
my $obj = new Tie::JournaledDir { dir => $db_dir };
$obj->expire();
return bless $me, $type;
}
=head2 open()
dummy.
=head2 close()
dummy.
=head2 touch()
dummy.
=cut
# Descriptions: dummy.
# Arguments: none
# Side Effects: none
# Return Value: none
sub open { 1;}
# Descriptions: dummy.
# Arguments: none
# Side Effects: none
# Return Value: none
sub close { 1;}
# Descriptions: dummy.
# Arguments: none
# Side Effects: none
# Return Value: none
sub touch { 1;}
=head2 add($address, $argv)
add data given as hash reference $argv into the database.
$argv = {
address => STR,
reason => STR,
status => STR,
};
C<Tie::JournaledDir> is a simple hash, so $argv is converted to the
following a set of key ($address) and value.
$address => "$unixtime status=$status reason=$reason"
=cut
# Descriptions: add bounce info into cache.
# Arguments: OBJ($self) STR($address) HASH_REF($argv)
# Side Effects: update cache
# Return Value: none
sub add
{
my ($self, $address, $argv) = @_;
my $curproc = $self->{ _curproc };
$self->_open_cache();
my $db = $self->{ _db };
if (defined $db) {
my ($reason, $status);
my $unixtime = time;
if (ref($argv) eq 'HASH') {
$reason = $argv->{ reason } || 'unknown';
$status = $argv->{ status } || 'unknown';
$status =~ s/\s+/_/g;
$reason =~ s/\s+/_/g;
}
else {
my $s = "FML::Error::Cache: unknown type: \$argv";
$curproc->logerror($s);
return undef;
}
if ($address) {
if ($self->_is_valid_address($address)) {
$db->{ $address } = "$unixtime status=$status reason=$reason";
}
else {
$curproc->logwarn("FML::Error::Cache: add: invalid address");
}
}
else {
$curproc->logwarn("FML::Error::Cache: add: invalid data");
}
$self->_close_cache();
}
else {
croak("FML::Error::Cache: add: unknown data input type");
}
}
=head2 delete($address)
delete entry for the specified $address.
=cut
# Descriptions: delete the entry for $address.
# Arguments: OBJ($self) STR($address)
# Side Effects: update cache
# Return Value: none
sub delete
{
my ($self, $address) = @_;
my $curproc = $self->{ _curproc };
$self->_open_cache();
my $db = $self->{ _db };
if (defined $db) {
if ($address) {
if ($self->_is_valid_address($address)) {
delete $db->{ $address };
}
else {
croak("FML::Error::Cache: delete: invalid address");
}
}
else {
$curproc->logwarn("FML::Error::Cache: delete: invalid data");
}
$self->_close_cache();
}
else {
croak("FML::Error::Cache: delete: unknown data input type");
}
}
=head1 CACHE IO MANIPULATION
You need to use primitive methods this class provides for IO into/from
error database (data cache).
=cut
# Descriptions: open the cache database for Tie::Journaled*.
# Arguments: OBJ($self)
# Side Effects: set up $self->{ _db }.
# Return Value: none
sub _open_cache
{
my ($self) = @_;
my $curproc = $self->{ _curproc };
my $config = $curproc->config();
my $dir = $config->{ error_mail_analyzer_cache_dir };
# parameters.
my %db = ();
my $type = $config->{ error_mail_analyzer_cache_type };
my $mode = $config->{ error_mail_analyzer_cache_mode } || 'temporal';
my $days = $config->{ error_mail_analyzer_cache_size } || 14;
my $args = {
dir => $dir,
unit => 'day',
limit => $days,
};
use Tie::JournaledDir;
tie %db, 'Tie::JournaledDir', $args;
$self->{ _db } = \%db;
}
# Descriptions: destruct tied hash.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub _close_cache
{
my ($self) = @_;
my $db = $self->{ _db };
if (defined $db) {
untie %$db;
}
}
=head1 UTILITY FUNCTIONS
=head2 get_primary_keys()
return primary keys in cache as ARRAY_REF.
=cut
# Descriptions: return (primary) key list in cache database.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: ARRAY_REF
sub get_primary_keys
{
my ($self) = @_;
$self->_open_cache();
my $db = $self->{ _db };
my @addr = keys %$db;
$self->_close_cache();
return \@addr;
}
# Descriptions: get all values as HASH_REF.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: HASH_REF
sub get_all_values_as_hash_ref
{
my ($self) = @_;
my $curproc = $self->{ _curproc };
my $config = $curproc->config();
my $dir = $config->{ error_mail_analyzer_cache_dir };
use Tie::JournaledDir;
my $cache = new Tie::JournaledDir { dir => $dir };
return $cache->get_all_values_as_hash_ref();
}
# Descriptions: check if the address is a valid string?
# Arguments: OBJ($self) STR($address)
# Side Effects: none
# Return Value: NUM
sub _is_valid_address
{
my ($self, $address) = @_;
use FML::Restriction::Base;
my $safe = new FML::Restriction::Base;
if ($safe->regexp_match('address', $address)) {
return 1;
}
else {
return 0;
}
}
=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,2008 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::Error::Cache first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|