| 1 |
wakaba |
1.1 |
|
| 2 |
|
|
=head1 NAME
|
| 3 |
|
|
|
| 4 |
|
|
Message::Entity Perl module
|
| 5 |
|
|
|
| 6 |
|
|
=head1 DESCRIPTION
|
| 7 |
|
|
|
| 8 |
|
|
Perl module for RFC 822/2822 C<message>.
|
| 9 |
|
|
MIME multipart will be also supported (but not implemented yet).
|
| 10 |
|
|
|
| 11 |
|
|
=cut
|
| 12 |
|
|
|
| 13 |
|
|
package Message::Entity;
|
| 14 |
|
|
use strict;
|
| 15 |
wakaba |
1.2 |
use vars qw($VERSION %DEFAULT);
|
| 16 |
wakaba |
1.1 |
$VERSION = '1.00';
|
| 17 |
|
|
|
| 18 |
|
|
use Message::Header;
|
| 19 |
|
|
use overload '""' => sub {shift->stringify};
|
| 20 |
|
|
|
| 21 |
|
|
=head2 Message::Entity->new ([%option])
|
| 22 |
|
|
|
| 23 |
|
|
Returns new Message::Entity instance. Some options can be
|
| 24 |
|
|
specified as hash.
|
| 25 |
|
|
|
| 26 |
|
|
=cut
|
| 27 |
|
|
|
| 28 |
|
|
sub new ($;%) {
|
| 29 |
|
|
my $class = shift;
|
| 30 |
|
|
my $self = bless {option => {@_}}, $class;
|
| 31 |
|
|
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
|
| 32 |
|
|
$self;
|
| 33 |
|
|
}
|
| 34 |
|
|
|
| 35 |
|
|
=head2 Message::Entity->parse ($message, [%option])
|
| 36 |
|
|
|
| 37 |
|
|
Parses given C<message> and return a new Message::Entity
|
| 38 |
|
|
object. Some options can be specified as hash.
|
| 39 |
|
|
|
| 40 |
|
|
=cut
|
| 41 |
|
|
|
| 42 |
|
|
sub parse ($$;%) {
|
| 43 |
|
|
my $class = shift;
|
| 44 |
|
|
my $message = shift;
|
| 45 |
|
|
my $self = bless {option => {@_}}, $class;
|
| 46 |
|
|
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
|
| 47 |
|
|
my ($isheader, @header, @body) = 1;
|
| 48 |
|
|
for my $line (split /\x0D?\x0A/, $message) {
|
| 49 |
|
|
if ($isheader && !length($line)) {
|
| 50 |
|
|
$isheader = 0;
|
| 51 |
|
|
} elsif ($isheader) {
|
| 52 |
|
|
push @header, $line
|
| 53 |
|
|
} else {
|
| 54 |
|
|
push @body, $line;
|
| 55 |
|
|
}
|
| 56 |
|
|
}
|
| 57 |
|
|
$self->{header} = Message::Header->parse (join "\n", @header);
|
| 58 |
|
|
$self->{body} = join "\n", @body;
|
| 59 |
|
|
$self;
|
| 60 |
|
|
}
|
| 61 |
|
|
|
| 62 |
|
|
=head2 $self->header ([$new_header])
|
| 63 |
|
|
|
| 64 |
|
|
Returns Message::Header unless $new_header.
|
| 65 |
|
|
Set $new_header instead of current C<header>.
|
| 66 |
|
|
If !ref $new_header, Message::Header->parse is automatically
|
| 67 |
|
|
called.
|
| 68 |
|
|
|
| 69 |
|
|
=cut
|
| 70 |
|
|
|
| 71 |
|
|
sub header ($;$) {
|
| 72 |
|
|
my $self = shift;
|
| 73 |
|
|
my $new_header = shift;
|
| 74 |
|
|
if (ref $new_header) {
|
| 75 |
|
|
$self->{header} = $new_header;
|
| 76 |
|
|
} elsif ($new_header) {
|
| 77 |
|
|
$self->{header} = Message::Header->parse ($new_header);
|
| 78 |
|
|
}
|
| 79 |
wakaba |
1.2 |
unless ($self->{header}) {
|
| 80 |
|
|
$self->{header} = new Message::Header;
|
| 81 |
|
|
}
|
| 82 |
wakaba |
1.1 |
$self->{header};
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
=head2 $self->body ([$new_body])
|
| 86 |
|
|
|
| 87 |
|
|
Returns C<body> as string unless $new_body.
|
| 88 |
|
|
Set $new_body instead of current C<body>.
|
| 89 |
|
|
|
| 90 |
|
|
=cut
|
| 91 |
|
|
|
| 92 |
|
|
sub body ($;$) {
|
| 93 |
|
|
my $self = shift;
|
| 94 |
|
|
my $new_body = shift;
|
| 95 |
|
|
if ($new_body) {
|
| 96 |
|
|
$self->{body} = $new_body;
|
| 97 |
|
|
}
|
| 98 |
|
|
$self->{body};
|
| 99 |
|
|
}
|
| 100 |
|
|
|
| 101 |
|
|
=head2 $self->stringify ([%option])
|
| 102 |
|
|
|
| 103 |
|
|
Returns the C<message> as a string.
|
| 104 |
|
|
|
| 105 |
|
|
=cut
|
| 106 |
|
|
|
| 107 |
|
|
sub stringify ($;%) {
|
| 108 |
|
|
my $self = shift;
|
| 109 |
|
|
my %OPT = @_;
|
| 110 |
|
|
my ($header, $body) = ($self->{header}, $self->{body});
|
| 111 |
|
|
$header .= "\n" if $header && $header !~ /\n$/;
|
| 112 |
|
|
$header."\n".$body;
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
=head2 $self->get_option ($option_name)
|
| 116 |
|
|
|
| 117 |
|
|
Returns value of the option.
|
| 118 |
|
|
|
| 119 |
|
|
=head2 $self->set_option ($option_name, $option_value)
|
| 120 |
|
|
|
| 121 |
|
|
Set new value of the option.
|
| 122 |
|
|
|
| 123 |
|
|
=cut
|
| 124 |
|
|
|
| 125 |
|
|
sub get_option ($$) {
|
| 126 |
|
|
my $self = shift;
|
| 127 |
|
|
my ($name) = @_;
|
| 128 |
|
|
$self->{option}->{$name};
|
| 129 |
|
|
}
|
| 130 |
|
|
sub set_option ($$$) {
|
| 131 |
|
|
my $self = shift;
|
| 132 |
|
|
my ($name, $value) = @_;
|
| 133 |
|
|
$self->{option}->{$name} = $value;
|
| 134 |
|
|
$self;
|
| 135 |
|
|
}
|
| 136 |
|
|
|
| 137 |
|
|
=head1 EXAMPLE
|
| 138 |
|
|
|
| 139 |
|
|
use Message::Entity;
|
| 140 |
|
|
my $msg = new Message::Entity;
|
| 141 |
|
|
$msg->header ($header);
|
| 142 |
|
|
$msg->body ($body);
|
| 143 |
|
|
print $msg;
|
| 144 |
|
|
|
| 145 |
|
|
=head1 LICENSE
|
| 146 |
|
|
|
| 147 |
|
|
Copyright 2002 wakaba E<lt>[email protected]<gt>.
|
| 148 |
|
|
|
| 149 |
|
|
This program is free software; you can redistribute it and/or modify
|
| 150 |
|
|
it under the terms of the GNU General Public License as published by
|
| 151 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
| 152 |
|
|
(at your option) any later version.
|
| 153 |
|
|
|
| 154 |
|
|
This program is distributed in the hope that it will be useful,
|
| 155 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 156 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 157 |
|
|
GNU General Public License for more details.
|
| 158 |
|
|
|
| 159 |
|
|
You should have received a copy of the GNU General Public License
|
| 160 |
|
|
along with this program; see the file COPYING. If not, write to
|
| 161 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
| 162 |
|
|
Boston, MA 02111-1307, USA.
|
| 163 |
|
|
|
| 164 |
|
|
=head1 CHANGE
|
| 165 |
|
|
|
| 166 |
|
|
See F<ChangeLog>.
|
| 167 |
wakaba |
1.2 |
$Date: 2002/03/13 14:47:07 $
|
| 168 |
wakaba |
1.1 |
|
| 169 |
|
|
=cut
|
| 170 |
|
|
|
| 171 |
|
|
1;
|