/[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.3 - (hide annotations) (download)
Thu Mar 21 04:21:28 2002 UTC (24 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.2: +47 -15 lines
2002-03-21  wakaba <w@suika.fam.cx>

	* Body/: New directory.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24