/[suikacvs]/messaging/manakai/lib/Message/Entity.pm
Suika

Contents of /messaging/manakai/lib/Message/Entity.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download)
Mon Mar 25 10:18:35 2002 UTC (24 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +13 -6 lines
2002-03-25  wakaba <w@suika.fam.cx>

	* MIME/: New directory.
	* Util.pm: New module.
	* Entity.pm, Header.pm (parse_all): New option.
	(hook_encode_string, hook_decode_string): Likewise.

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 use vars qw($VERSION %DEFAULT);
16 $VERSION=do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
17
18 use Message::Header;
19 use overload '""' => sub {shift->stringify};
20
21 %DEFAULT = (
22 body_class => {'/DEFAULT' => 'Message::Body::TextPlain'},
23 parse_all => -1,
24 );
25
26 =head2 Message::Entity->new ([%option])
27
28 Returns new Message::Entity instance. Some options can be
29 specified as hash.
30
31 =cut
32
33 sub new ($;%) {
34 my $class = shift;
35 my $self = bless {option => {@_}}, $class;
36 for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
37 $self;
38 }
39
40 =head2 Message::Entity->parse ($message, [%option])
41
42 Parses given C<message> and return a new Message::Entity
43 object. Some options can be specified as hash.
44
45 =cut
46
47 sub parse ($$;%) {
48 my $class = shift;
49 my $message = shift;
50 my $self = bless {option => {@_}}, $class;
51 for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
52 my ($isheader, @header, @body) = 1;
53 for my $line (split /\x0D?\x0A/, $message) {
54 if ($isheader && !length($line)) {
55 $isheader = 0;
56 } elsif ($isheader) {
57 push @header, $line
58 } else {
59 push @body, $line;
60 }
61 }
62 $self->{header} = Message::Header->parse (join ("\n", @header),
63 parse_all => $self->{option}->{parse_all});
64 $self->{body} = join "\n", @body;
65 $self->{body} = $self->_body ($self->{body}, $self->content_type)
66 if $self->{option}->{parse_all}>0;
67 $self;
68 }
69
70 =head2 $self->header ([$new_header])
71
72 Returns Message::Header unless $new_header.
73 Set $new_header instead of current C<header>.
74 If !ref $new_header, Message::Header->parse is automatically
75 called.
76
77 =cut
78
79 sub header ($;$) {
80 my $self = shift;
81 my $new_header = shift;
82 if (ref $new_header) {
83 $self->{header} = $new_header;
84 } elsif ($new_header) {
85 $self->{header} = Message::Header->parse ($new_header,
86 parse_all => $self->{option}->{parse_all});
87 }
88 unless ($self->{header}) {
89 $self->{header} = new Message::Header ();
90 }
91 $self->{header};
92 }
93
94 =head2 $self->body ([$new_body])
95
96 Returns C<body> as string unless $new_body.
97 Set $new_body instead of current C<body>.
98
99 =cut
100
101 sub body ($;$) {
102 my $self = shift;
103 my $new_body = shift;
104 if ($new_body) {
105 $self->{body} = $new_body;
106 }
107 $self->{body} = $self->_body ($self->{body}, $self->content_type)
108 unless ref $self->{body};
109 $self->{body};
110 }
111
112 sub _body ($;$$) {
113 my $self = shift;
114 my $body = shift;
115 my $ct = shift;
116 $ct = $self->{option}->{body_class}->{$ct}
117 || $self->{option}->{body_class}->{'/DEFAULT'};
118 eval "require $ct";
119 if (ref $body) {
120 return $body;
121 } elsif ($body) {
122 return $ct->parse ($body,
123 parse_all => $self->{option}->{parse_all});
124 } else {
125 return $ct->new ($body);
126 }
127 }
128
129 =head2 $self->stringify ([%option])
130
131 Returns the C<message> as a string.
132
133 =cut
134
135 sub stringify ($;%) {
136 my $self = shift;
137 my %OPT = @_;
138 my ($header, $body) = ($self->{header}, $self->{body});
139 $header .= "\n" if $header && $header !~ /\n$/;
140 $header."\n".$body;
141 }
142 sub as_string ($;%) {shift->stringify}
143
144 =head2 $self->option ($option_name)
145
146 Returns/set (new) value of the option.
147
148 =cut
149
150 sub option ($$;$) {
151 my $self = shift;
152 my ($name, $newval) = @_;
153 if ($newval) {
154 $self->{option}->{$name} = $newval;
155 }
156 $self->{option}->{$name};
157 }
158
159 =head2 $self->content_type ([%options])
160
161 Returns C<body>'s content-type (Internet Media Type).
162 This method is not implemented yet so always returns
163 C<text/plain>.
164
165 =cut
166
167 sub content_type ($;%) {
168 'text/plain';
169 }
170
171 =head1 EXAMPLE
172
173 use Message::Entity;
174 my $msg = new Message::Entity;
175 $msg->header ($header);
176 $msg->body ($body);
177 print $msg;
178
179 =head1 SEE ALSO
180
181 Message::* Perl modules
182 <http://suika.fam.cx/~wakaba/Message-pm/>
183
184 =head1 LICENSE
185
186 Copyright 2002 wakaba E<lt>[email protected]<gt>.
187
188 This program is free software; you can redistribute it and/or modify
189 it under the terms of the GNU General Public License as published by
190 the Free Software Foundation; either version 2 of the License, or
191 (at your option) any later version.
192
193 This program is distributed in the hope that it will be useful,
194 but WITHOUT ANY WARRANTY; without even the implied warranty of
195 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
196 GNU General Public License for more details.
197
198 You should have received a copy of the GNU General Public License
199 along with this program; see the file COPYING. If not, write to
200 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
201 Boston, MA 02111-1307, USA.
202
203 =head1 CHANGE
204
205 See F<ChangeLog>.
206 $Date: 2002/03/21 04:21:28 $
207
208 =cut
209
210 1;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24