summaryrefslogtreecommitdiff
path: root/fml/lib/Mail/Bounce/Qmail.pm
blob: afb0de2230a5031ad3292e84a96646b5a379f973 (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
#-*- 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: Qmail.pm,v 1.9 2003/01/07 08:38:35 fukachan Exp $
#


package Mail::Bounce::Qmail;

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

=head1 NAME

Mail::Bounce::Qmail - Qmail error message format parser

=head1 SYNOPSIS

=head1 DESCRIPTION

Parse bounce messages generated by qmail.

Qmail actually has a standard, called QSBMF (qmail-send bounce message
format), as describbed in

    http://cr.yp.to/proto/qsbmf.txt

=cut


# Descriptions: parse qmail error message.
#    Arguments: OBJ($self) OBJ($msg) HASH_REF($result)
# Side Effects: update $result
# Return Value: none
sub analyze
{
    my ($self, $msg, $result) = @_;
    my $state       = 0;
    my $pattern     = 'Hi. This is the';
    my $end_pattern = '--- Undelivered message follows ---';

    # search data
    my ($addr, $reason);
    my $m = $msg->{ next };
    do {
	if (defined $m) {
	    my $num = $m->num_paragraph;
	    for ( my $i = 0; $i < $num ; $i++ ) {
		my $data = $m->nth_paragraph( $i + 1 );

		if ($data =~ /$pattern/o)     { $state = 1;}
		if ($data =~ /$end_pattern/o) { $state = 0;}

		if ($state == 1) {
		    $data =~ s/\n/ /go;
		    if ($data =~ /\<(\S+\@\S+)\>:\s*(.*)/) {
			($addr, $reason) = ($1, $2);

			# XXX-TODO: we should use $self->address_clean_up() ?

			my $status = '5.x.y';
			if ($data =~ /\#(\d+\.\d+\.\d+)/) {
			    $status = $1;
			}
			elsif ($data =~ /\s+(\d{3})\s+/) {
			    my $code = $1;
			    $status  = '5.x.y' if $code =~ /^5/o;
			    $status  = '4.x.y' if $code =~ /^4/o;
			}

			$result->{ $addr }->{ 'Diagnostic-Code' } = $reason;
			$result->{ $addr }->{ 'Status' }          = $status;
			$result->{ $addr }->{ 'hints' }           = 'qmail';
		    }
		}
	    }
	}

	$m = $m->{ next };
    } while (defined $m);

    $result;
}


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

=cut


1;