/[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.1 - (hide annotations) (download)
Wed Mar 13 14:47:07 2002 UTC (24 years, 5 months ago) by wakaba
Branch: MAIN
2002-03-13  wakaba <w@suika.fam.cx>

	* Header.pm: New module.
	* Entity.pm: Likewise.
	
	* ChangeLog: New file.

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     use vars qw($VERSION);
16     $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     $self->{header};
80     }
81    
82     =head2 $self->body ([$new_body])
83    
84     Returns C<body> as string unless $new_body.
85     Set $new_body instead of current C<body>.
86    
87     =cut
88    
89     sub body ($;$) {
90     my $self = shift;
91     my $new_body = shift;
92     if ($new_body) {
93     $self->{body} = $new_body;
94     }
95     $self->{body};
96     }
97    
98     =head2 $self->stringify ([%option])
99    
100     Returns the C<message> as a string.
101    
102     =cut
103    
104     sub stringify ($;%) {
105     my $self = shift;
106     my %OPT = @_;
107     my ($header, $body) = ($self->{header}, $self->{body});
108     $header .= "\n" if $header && $header !~ /\n$/;
109     $header."\n".$body;
110     }
111    
112     =head2 $self->get_option ($option_name)
113    
114     Returns value of the option.
115    
116     =head2 $self->set_option ($option_name, $option_value)
117    
118     Set new value of the option.
119    
120     =cut
121    
122     sub get_option ($$) {
123     my $self = shift;
124     my ($name) = @_;
125     $self->{option}->{$name};
126     }
127     sub set_option ($$$) {
128     my $self = shift;
129     my ($name, $value) = @_;
130     $self->{option}->{$name} = $value;
131     $self;
132     }
133    
134     =head1 EXAMPLE
135    
136     use Message::Entity;
137     my $msg = new Message::Entity;
138     $msg->header ($header);
139     $msg->body ($body);
140     print $msg;
141    
142     =head1 LICENSE
143    
144     Copyright 2002 wakaba E<lt>[email protected]<gt>.
145    
146     This program is free software; you can redistribute it and/or modify
147     it under the terms of the GNU General Public License as published by
148     the Free Software Foundation; either version 2 of the License, or
149     (at your option) any later version.
150    
151     This program is distributed in the hope that it will be useful,
152     but WITHOUT ANY WARRANTY; without even the implied warranty of
153     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
154     GNU General Public License for more details.
155    
156     You should have received a copy of the GNU General Public License
157     along with this program; see the file COPYING. If not, write to
158     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
159     Boston, MA 02111-1307, USA.
160    
161     =head1 CHANGE
162    
163     See F<ChangeLog>.
164     $Date: $
165    
166     =cut
167    
168     1;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24