/[suikacvs]/messaging/manakai/lib/Message/Field/Structured.pm
Suika

Contents of /messaging/manakai/lib/Message/Field/Structured.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Wed Mar 20 09:56:26 2002 UTC (22 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +2 -2 lines
2002-03-20  wakaba <w@suika.fam.cx>

	* MsgID.pm, Received.pm, Subject.pm: New modules.
	* MsgID/: New directory.

1 wakaba 1.1
2     =head1 NAME
3    
4     Message::Field::Structured Perl module
5    
6     =head1 DESCRIPTION
7    
8     Perl module for RFC 822/2822 structured C<field>s.
9    
10     =cut
11    
12     package Message::Field::Structured;
13     require 5.6.0;
14     use strict;
15     use re 'eval';
16     use vars qw(%REG $VERSION);
17     $VERSION = '1.00';
18    
19     use overload '""' => sub {shift->stringify};
20    
21     $REG{comment} = qr/\x28(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x27\x2A-\x5B\x5D-\xFF]|(??{$REG{comment}}))*\x29/;
22     $REG{quoted_string} = qr/\x22(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\xFF])*\x22/;
23     $REG{domain_literal} = qr/\x5B(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x5A\x5E-\xFF])*\x5D/;
24    
25     $REG{WSP} = qr/[\x20\x09]+/;
26     $REG{FWS} = qr/[\x20\x09]*/;
27     $REG{M_quoted_string} = qr/\x22((?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\xFF])*)\x22/;
28     $REG{M_comment} = qr/\x28((?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x27\x2A-\x5B\x5D-\xFF]|(??{$REG{comment}}))*)\x29/;
29    
30     $REG{NON_atom} = qr/[^\x09\x20\x21\x23-\x27\x2A\x2B\x2D\x2F\x30-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]/;
31    
32     =head2 Message::Field::Structured->new ()
33    
34     Return empty Message::Field::Structured object.
35    
36     =cut
37    
38 wakaba 1.2 sub new ($;%) {
39 wakaba 1.1 bless {}, shift;
40     }
41    
42     =head2 Message::Field::Structured->parse ($unfolded_field_body)
43    
44     Parse structured C<field-body>.
45    
46     =cut
47    
48 wakaba 1.2 sub parse ($$;%) {
49 wakaba 1.1 my $self = bless {}, shift;
50     my $field_body = shift;
51     $self->{field_body} = $field_body;
52     $self;
53     }
54    
55     =head2 $self->stringify ()
56    
57     Returns C<field-body> as a string.
58    
59     =cut
60    
61     sub stringify ($) {
62     my $self = shift;
63     $self->{field_body};
64     }
65    
66     =head2 $self->as_plain_string ()
67    
68     Returns C<field-body> contents as a plain text fragment.
69     C<quoted-string> and C<quoted-pair> in C<comment> are
70     unquoted, so return value of this method can be invalid
71     as a part of the C<field>.
72    
73     =cut
74    
75     sub as_plain_string ($) {
76     my $self = shift;
77     $self->unquote_quoted_string ($self->unquote_comment ($self->{field_body}));
78     }
79    
80     sub quote_unsafe_string ($$) {
81     my $self = shift;
82     my $string = shift;
83     if ($string =~ /$REG{NON_atom}/ || $string =~ /$REG{WSP}$REG{WSP}+/) {
84     $string =~ s/([\x22\x5C])/\x5C$1/g;
85     $string = '"'.$string.'"';
86     }
87     $string;
88     }
89    
90     =head2 $self->unquote_quoted_string ($string)
91    
92     Unquote C<quoted-string>. Get rid of C<DQUOTE>s and
93     C<REVERSED SOLIDUS> included in C<quoted-pair>.
94     This method is intended for internal use.
95    
96     =cut
97    
98     sub unquote_quoted_string ($$) {
99     my $self = shift;
100     my $quoted_string = shift;
101     $quoted_string =~ s{$REG{M_quoted_string}}{
102     my $qtext = $1;
103     $qtext =~ s/\x5C([\x00-\xFF])/$1/g;
104     $qtext;
105     }goex;
106     $quoted_string;
107     }
108    
109     sub unquote_comment ($$) {
110     my $self = shift;
111     my $quoted_string = shift;
112     $quoted_string =~ s{$REG{M_comment}}{
113     my $qtext = $1;
114     $qtext =~ s/\x5C([\x00-\xFF])/$1/g;
115     '('.$qtext.')';
116     }goex;
117     $quoted_string;
118     }
119    
120     =head2 $self->delete_comment ($field_body)
121    
122     Remove all C<comment> in given strictured C<field-body>.
123     This method is intended for internal use.
124    
125     =cut
126    
127     sub delete_comment ($$) {
128     my $self = shift;
129     my $body = shift;
130     $body =~ s{($REG{quoted_string}|$REG{domain_literal})|$REG{comment}}{
131     my $o = $1; $o? $o : ' ';
132     }gex;
133     $body;
134     }
135    
136     =head1 EXAMPLE
137    
138     use Message::Field::Structured;
139    
140     my $field_body = '"This is an example of <\"> (quotation mark)."
141     (Comment within \q\u\o\t\e\d\-\p\a\i\r\(\s\))';
142     my $field = Message::Field::Structured->parse ($field_body);
143    
144     print $field->as_plain_string;
145    
146     =head1 LICENSE
147    
148     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
149    
150     This program is free software; you can redistribute it and/or modify
151     it under the terms of the GNU General Public License as published by
152     the Free Software Foundation; either version 2 of the License, or
153     (at your option) any later version.
154    
155     This program is distributed in the hope that it will be useful,
156     but WITHOUT ANY WARRANTY; without even the implied warranty of
157     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
158     GNU General Public License for more details.
159    
160     You should have received a copy of the GNU General Public License
161     along with this program; see the file COPYING. If not, write to
162     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
163     Boston, MA 02111-1307, USA.
164    
165     =head1 CHANGE
166    
167     See F<ChangeLog>.
168    
169     =cut
170    
171     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24