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
|
#-*- perl -*-
#
# Copyright (C) 2000 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.
#
# $Id$
# $FML$
#
package FML::Parse;
=head1 NAME
FML::Parse - parse the incoming message/mail to the header and body.
=head1 SYNOPSIS
($r_header, $r_body) = new FML::Parse \*STDIN;
=head1 DESCRIPTION
FML::Parse parses the incoming mail. The target to parse is given the
argument os new() constructor.
$r_header is the reference to the header object, which is returned by
Mail::Header class. $r_body is reference to the scalar mail body
variable, which is alloced in FML::Parse name space.
=head1 METHOD
=item new( fd )
C<fd> is the file handle.
Normally C<fd> is the handle for STDIN channel.
=head1 SEE ALSO
L<Mail::Header>,
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2001 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::Parse.pm appeared in fml5.
=cut
use lib qw(./lib/fml5 ./lib/CPAN ./lib/3RDPARTY ./lib);
use vars qw($InComingMessage);
use strict;
use Carp;
use FML::Header;
use FML::Body;
use FML::Config;
use FML::Log qw(Log);
sub new
{
my ($self, $fd) = @_;
my $me = {};
bless $me, $self;
# return ( $ref_to_mail_header, $ref_to_mail_body, $error_code);
return $me->_parse($fd);
}
# return ( $ref_to_mail_header, $ref_to_mail_body, $error_code);
sub _parse
{
my ($self, $fd) = @_;
my ($header, $header_size);
my $body_size;
my $total_buffer_size;
my ($p, $buf);
# extract header and put it to $header
while ($p = sysread($fd, $_, 1024)) {
$total_buffer_size += $p;
$buf .= $_;
if (($p = index($buf, "\n\n", 0)) > 0) {
$header = substr($buf, 0, $p + 1);
$header_size = $p + 1;
$InComingMessage = substr($buf, $p + 2);
last;
}
}
# extract mail body and put it to $FML::Parse::InComingMessage
while ($p = sysread($fd, $_, 1024)) {
$total_buffer_size += $p;
$InComingMessage .= $_;
}
# read the message (mail body) from the incoming mail
$body_size = length($InComingMessage);
Log("read total=$total_buffer_size header=$header_size body=$body_size");
my @h = split(/\n/, $header);
my $x;
for $x (@h) { $x .= "\n";}
# extract each field from the header array
my $r_header = new FML::Header \@h, Modify => 0;
my $r_body = new FML::Body \$InComingMessage;
# return ( $ref_to_mail_header, $ref_to_mail_body, $error_code);
return ($r_header, $r_body, 0);
}
1;
|