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
|
#-*- perl -*-
#
# Copyright (C) 2001,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: Bounce.pm,v 1.26 2003/08/23 04:35:44 fukachan Exp $
#
package Mail::Bounce;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
my $debug = 0;
=head1 NAME
Mail::Bounce - analyze error messages
=head1 SYNOPSIS
use Mail::Message;
my $msg = Mail::Message->parse( { fd => \*STDIN } );
use Mail::Bounce;
my $bouncer = new Mail::Bounce;
$bouncer->analyze( $msg );
# show results
for my $a ( $bouncer->address_list ) {
print "address: $a\n";
print " status: ";
print $bouncer->status( $a );
print "\n";
print " reason: ";
print $bouncer->reason( $a );
print "\n";
print "\n";
}
=head1 DESCRIPTION
try to analyze the given error message, which is a Mail::Message
object.
For non DSN pattern,
try to analyze it by using modules in C<Mail::Bounce::>
which can recognize MTA specific and domian specific patterns.
For example,
Mail::Bounce
$msg
--------> Mail::Bounce::DSN::analyze()
<--------
$result
returned C<$result> provides the following information:
$result = {
address1 => {
Original-Recipient => 'rfc822; addr',
Final-Recipient => 'rfc822; addr',
Diagnostic-Code => 'reason ...',
Action => 'failed',
Status => '4.0.0',
Reporting-MTA => 'dns; server.fml.org',
Received-From-MTA => 'DNS; server.fml.org',
hints => ... which module matches ...,
},
address2 => {
... snip ...
},
...
};
=head1 METHODS
=head2 new()
standard new() method.
=cut
# Descriptions: constructor.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: OBJ
sub new
{
my ($self) = @_;
my ($type) = ref($self) || $self;
my $me = {};
return bless $me, $type;
}
=head2 analyze($msg)
C<$msg> is a C<Mail::Message> object.
This routine is a top level switch which provides the entrance
for C<Mail::Bounce::> modules, for example, C<Mail::Bounce::DSN>.
C<Mail::Bounce::$model::analyze( \$msg, \$result )>
method in each module is the actual model specific analyzer.
C<$result> has an answer of analyze if the error message pattern is
already known.
=cut
# Descriptions: top level dispatcher.
# Arguments: OBJ($self) OBJ($msg)
# Side Effects: update $self->{ _result }, which holds several info
# Return Value: none
sub analyze
{
my ($self, $msg) = @_;
my $result = {};
if ($debug) {
my $h = $msg->data_type_list( { debug => 1 } );
print STDERR " ----- dump msg -----\n";
for my $hdr (@$h) { print STDERR " ", $hdr, "\n";}
print STDERR " ----- dump msg end -----\n";
}
MODEL:
for my $pkg (qw(
DSN
Postfix19991231
Qmail
Exim
GOO
Freeserve
SimpleMatch
)) {
my $module = "Mail::Bounce::$pkg";
print STDERR "\n --- module: $module\n" if $debug;
eval qq {
use $module;
$module->analyze( \$msg , \$result );
};
croak($@) if $@;
if (keys %$result) {
print STDERR "\n match $module\n" if $debug;
last MODEL;
}
if ($debug) {
print STDERR " * not match $module\n" unless %$result;
}
}
$self->{ _result } = $result;
}
=head2 address_list()
return ARRAY of addresses found in the error message.
=cut
# Descriptions: return ARRAY of addresses found in the error message.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: ARRAY
sub address_list
{
my ($self) = @_;
my $result = $self->{ _result };
return keys %$result;
}
=head2 status($addr)
return status (string) for C<$addr>.
The status can be extracted from C<result> analyze() method gives.
=head2 reason($addr)
return error reason (string) for C<$addr>.
It can be extracted from C<result> analyze() method gives.
=cut
# XXX-TODO: hmm, we should prepare $addr->status() and $addr->reason() ?
# Descriptions: return status (string) for $addr.
# Arguments: OBJ($self) STR($addr)
# Side Effects: none
# Return Value: STR
sub status
{
my ($self, $addr) = @_;
my $status = $self->{ _result }->{ $addr }->{ 'Status' };
$status =~ s/\s+/ /go;
$status =~ s/\s*$//o;
$status;
}
# Descriptions: return reason (string) for $addr.
# Arguments: OBJ($self) STR($addr)
# Side Effects: none
# Return Value: STR
sub reason
{
my ($self, $addr) = @_;
my $reason = $self->{ _result }->{ $addr }->{ 'Diagnostic-Code' };
$reason =~ s/\s+/ /go;
$reason =~ s/\s*$//o;
$reason;
}
# Descriptions: return hints (string) for $addr.
# Arguments: OBJ($self) STR($addr)
# Side Effects: none
# Return Value: STR
sub hints
{
my ($self, $addr) = @_;
$self->{ _result }->{ $addr }->{ 'hints' };
}
=head2 look_like_japanese(string)
return 1 if C<string> looks Japanese one in JIS/SJIS/EUC code.
return 0 unless.
=cut
my $RE_SJIS_C = '[\201-\237\340-\374][\100-\176\200-\374]';
my $RE_SJIS_S = "($RE_SJIS_C)+";
my $RE_EUC_C = '[\241-\376][\241-\376]';
my $RE_EUC_S = "($RE_EUC_C)+";
my $RE_JIN = '\033\$[\@B]';
my $RE_JOUT = '\033\([BJ]';
my @REGEXP = (
$RE_SJIS_C,
$RE_SJIS_S,
$RE_EUC_C,
$RE_EUC_S,
$RE_JIN,
$RE_JOUT,
);
# Descriptions: $buf looks like Japanese ?
# Arguments: OBJ($self) STR($buf)
# Side Effects: none
# Return Value: 1 or 0
sub look_like_japanese
{
my ($self, $buf) = @_;
for my $regexp (@REGEXP) {
return 1 if $buf =~ /$regexp/;
}
0;
}
=head2 address_clean_up(hint, addr)
clean up C<addr> and return it.
C<hint> gives a special hint for some specific MTA or domain.
It is rarely used.
=cut
# Descriptions: clean up address for further use
# Arguments: OBJ($self) STR($hint) STR($addr)
# Side Effects: none
# Return Value: STR
sub address_clean_up
{
my ($self, $hint, $addr) = @_;
if ($debug) { print STDERR "address_clean_up($hint, $addr)\n";}
# nuke predecing and trailing strings around user@domain pattern
my $prev_addr = $addr;
do {
$prev_addr = $addr;
print STDERR " address_clean_up.in: $prev_addr\n" if $debug;
$addr =~ s/\.$//o;
$addr =~ s/^\<//o;
$addr =~ s/\>$//o;
$addr =~ s/^\"//o;
$addr =~ s/\"$//o;
print STDERR " address_clean_up.out: $addr\n" if $debug;
} while ($addr ne $prev_addr);
# Mail::Bounce::FixBrokenAddress class provides irrgular
# address handlings, so handles domain/MTA specific addresses.
# For example, nifty.ne.jp, webtv.ne.jp, ...
# XXX-TODO: Mail::Bounce::FixBrokenAddress::FixIt is ugly.
use Mail::Bounce::FixBrokenAddress;
return Mail::Bounce::FixBrokenAddress::FixIt($hint, $addr);
}
=head1 SECURITY CONSIDERATION
The address returned by Mail::Bounce class may be unsafe. Please
validate it by some other class such as FML::Restriction class before
use it at some other place.
=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) 2001,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
Mail::Bounce first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|