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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations) (download)
Sun Jun 16 10:42:06 2002 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.12: +11 -9 lines
2002-06-16  wakaba <w@suika.fam.cx>

	* MsgID.pm: Check '.test' reserved TLD if '-validate'.
	* UA.pm (add_our_name): New method (moved from 
	Message::Entity).

1 wakaba 1.1
2     =head1 NAME
3    
4 wakaba 1.7 Message::Field::Params --- Perl module for Internet message
5     field body consist of parameters, such as C<Content-Type:> field
6 wakaba 1.1
7     =cut
8    
9     package Message::Field::Params;
10     use strict;
11     require 5.6.0;
12     use re 'eval';
13 wakaba 1.7 use vars qw(@ISA %REG $VERSION);
14 wakaba 1.13 $VERSION=do{my @r=(q$Revision: 1.12 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
15 wakaba 1.3 require Message::Util;
16 wakaba 1.7 require Message::Field::Structured;
17     push @ISA, qw(Message::Field::Structured);
18    
19     use overload '""' => sub { $_[0]->stringify },
20     '0+' => sub { $_[0]->count },
21     '.=' => sub { $_[0]->add ($_[1], ['', value => 1]); $_[0] },
22     fallback => 1;
23    
24     *REG = \%Message::Util::REG;
25     ## Inherited: comment, quoted_string, domain_literal, angle_quoted
26     ## WSP, FWS, atext, atext_dot, token, attribute_char
27     ## S_encoded_word
28     ## M_quoted_string
29 wakaba 1.1
30 wakaba 1.7 $REG{param} = qr/(?:$REG{atext_dot}|$REG{quoted_string}|$REG{angle_quoted})(?:$REG{atext_dot}|$REG{quoted_string}|$REG{WSP}|,)*/;
31 wakaba 1.8 $REG{param_nocomma} = qr/(?:$REG{atext_dot}|$REG{quoted_string}|$REG{angle_quoted})(?:$REG{atext_dot}|$REG{quoted_string}|$REG{WSP})*/;
32 wakaba 1.1 ## more naive C<parameter>. (Comma is allowed for RFC 1049)
33 wakaba 1.9 $REG{param_free} = qr/(?:[^\x09\x20\x22\x3B\x3C]|$REG{quoted_string}|$REG{angle_quoted})(?:[^\x22\x3B\x3C]|$REG{quoted_string})*/;
34 wakaba 1.1 $REG{parameter} = qr/$REG{token}=(?:$REG{token}|$REG{quoted_string})?/;
35     ## as defined by RFC 2045, not RFC 2231.
36    
37 wakaba 1.9 #$REG{M_parameter} = qr/($REG{token})=($REG{token}|$REG{quoted_string})?/;
38     $REG{M_parameter} = qr/($REG{token})=($REG{quoted_string}|[^\x22]*)/;
39 wakaba 1.1 ## as defined by RFC 2045, not RFC 2231.
40     $REG{M_parameter_name} = qr/($REG{attribute_char}+)(?:\*([0-9]+)(\*)?|(\*))/;
41     ## as defined by RFC 2231.
42     $REG{M_parameter_extended_value} = qr/([^']*)'([^']*)'($REG{token}*)/;
43     ## as defined by RFC 2231, but more naive.
44    
45    
46 wakaba 1.7 =head1 CONSTRUCTORS
47 wakaba 1.5
48 wakaba 1.7 The following methods construct new objects:
49 wakaba 1.1
50 wakaba 1.7 =over 4
51 wakaba 1.1
52     =cut
53    
54 wakaba 1.7 ## Initialize of this class -- called by constructors
55     sub _init ($;%) {
56     my $self = shift;
57     my %options = @_;
58     my %DEFAULT = (
59 wakaba 1.9 -delete_fws => 1,## BUG: this option MUST be '1'.
60     ## parameter parser cannot procede CFWS.
61 wakaba 1.10 #encoding_after_encode
62     #encoding_before_decode
63     #field_param_name
64     #field_name
65     #format
66     #hook_encode_string
67     #hook_decode_string
68 wakaba 1.8 -parameter_rule => 'param',
69 wakaba 1.7 -parameter_name_case_sensible => 0,
70 wakaba 1.13 -parameter_value_max_length => 60,
71     -parameter_value_split_length => 35,
72 wakaba 1.7 -parameter_value_unsafe_rule => {'*default' => 'NON_http_attribute_char'},
73     -parse_all => 0,
74 wakaba 1.8 -separator => '; ',
75     -separator_regex => qr/$REG{FWS};$REG{FWS}/,
76 wakaba 1.7 -use_parameter_extension => 0,
77 wakaba 1.10 #value_type
78 wakaba 1.7 );
79     $self->SUPER::_init (%DEFAULT, %options);
80     $self->{param} = [];
81 wakaba 1.11 my $fname = $self->{option}->{field_name};
82 wakaba 1.8 if ($fname eq 'p3p') {
83     $self->{option}->{parameter_rule} = 'param_nocomma';
84     $self->{option}->{separator} = ', ';
85     $self->{option}->{separator_regex} = qr/$REG{FWS},$REG{FWS}/;
86     }
87 wakaba 1.7 if ($self->{option}->{format} =~ /^http-sip/) {
88     $self->{option}->{encoding_before_decode} = 'utf-8';
89     $self->{option}->{encoding_after_decode} = 'utf-8';
90     } elsif ($self->{option}->{format} =~ /^http/) {
91     $self->{option}->{encoding_before_decode} = 'iso-8859-1';
92     $self->{option}->{encoding_after_decode} = 'iso-8859-1';
93     }
94 wakaba 1.1 }
95    
96     ## Initialization for new () method.
97     sub _initialize_new ($;%) {
98 wakaba 1.7 ## Nothing to do
99     }
100    
101     ## Initialization for parse () method.
102     sub _initialize_parse ($;%) {
103     ## Nothing to do
104     }
105    
106     =item $p = Message::Field::Params->new ([%options])
107    
108     Constructs a new object. You might pass some options as parameters
109     to the constructor.
110    
111     =cut
112    
113     sub new ($;%) {
114     my $self = shift->SUPER::new (@_);
115     $self->_initialize_new (@_);
116     $self;
117 wakaba 1.1 }
118    
119 wakaba 1.7 =item $p = Message::Field::Params->parse ($field-body, [%options])
120 wakaba 1.1
121 wakaba 1.7 Constructs a new object with given field body. You might pass
122     some options as parameters to the constructor.
123 wakaba 1.1
124     =cut
125    
126     sub parse ($$;%) {
127     my $class = shift;
128 wakaba 1.7 my $self = bless {}, $class;
129 wakaba 1.1 my $body = shift;
130 wakaba 1.7 $self->_init (@_);
131     $self->_initialize_parse (@_);
132     $body = Message::Util::delete_comment ($body);
133     $body = $self->_delete_fws ($body) if $self->{option}->{delete_fws};
134 wakaba 1.1 my @b = ();
135 wakaba 1.8 $body =~ s{$REG{FWS}($REG{$self->{option}->{parameter_rule}})
136     (?:$self->{option}->{separator_regex}|$)}{
137 wakaba 1.1 my $param = $1;
138     push @b, $self->_parse_param ($param);
139     }goex;
140     @b = $self->_restore_param (@b);
141     $self->_save_param (@b);
142     $self;
143     }
144    
145     sub _parse_param ($$) {
146     my $self = shift;
147     my $param = shift;
148     if ($param =~ /^$REG{M_parameter}$/) {
149 wakaba 1.7 my ($name, $value) = ($self->_n11n_param_name ($1), $2);
150 wakaba 1.1 my ($seq, $isencoded, $charset, $lang) = (-1, 0, '', '');
151     if ($name =~ /^$REG{M_parameter_name}$/) {
152     ($name, $seq, $isencoded) = ($1, $4?-1:$2, ($3||$4)?1:0);
153     }
154     if ($isencoded && $value =~ /^$REG{M_parameter_extended_value}$/) {
155     ($charset, $lang, $value) = ($1, $2, $3);
156     }
157     return [$name, {value => $value, seq => $seq, is_encoded => $isencoded,
158     charset => $charset, language => $lang, is_parameter => 1}];
159     } else {
160     return [$param, {is_parameter => 0}];
161     }
162     }
163    
164     sub _restore_param ($@) {
165     my $self = shift;
166     my @p = @_;
167     my @ret;
168     my %part;
169     for my $i (@p) {
170     if ($i->[1]->{is_parameter}) {
171     my $p = $i->[1];
172     if ($p->{seq}<0) {
173     my $s = $p->{value};
174     if ($p->{is_encoded}) {
175     $s =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr(hex($1))/eg;
176 wakaba 1.3 my %s = &{$self->{option}->{hook_decode_string}} ($self, $s,
177     language => $p->{language}, charset => $p->{charset},
178     type => 'parameter/encoded');
179     ($s, $p->{charset}, $p->{language}) = (@s{qw(value charset language)});
180 wakaba 1.9 } elsif ($p->{is_internal}) {
181     $s = $p->{value};
182 wakaba 1.1 } else {
183 wakaba 1.3 my $q = 0;
184 wakaba 1.7 ($s,$q) = Message::Util::unquote_if_quoted_string ($p->{value});
185 wakaba 1.3 my %s = &{$self->{option}->{hook_decode_string}} ($self, $s,
186     type => ($q?'parameter/quoted':'parameter'));
187     ($s, $p->{charset}, $p->{language}) = (@s{qw(value charset language)});
188 wakaba 1.1 }
189     push @ret, [$i->[0], {value => $s, language => $p->{language},
190     charset => $p->{charset}, is_parameter => 1}];
191     } else {
192     $part{$i->[0]}->[$p->{seq}] = {
193 wakaba 1.7 value => scalar Message::Util::unquote_if_quoted_string ($p->{value}),
194 wakaba 1.1 language => $p->{language}, charset => $p->{charset},
195     is_encoded => $p->{is_encoded}};
196     }
197 wakaba 1.3 } else {
198 wakaba 1.9 #my $q = 0;
199     #($i->[0], $q) = Message::Util::unquote_if_quoted_string ($i->[0]);
200     #my %s = &{$self->{option}->{hook_decode_string}} ($self, $i->[0],
201     # type => ($q?'phrase/quoted':'phrase'));
202     push @ret, [Message::Util::decode_quoted_string ($self, $i->[0]),
203     {is_parameter => 0}];
204 wakaba 1.3 }
205 wakaba 1.1 }
206     for my $name (keys %part) {
207     my $t = join '', map {
208     my $v = $_;
209     my $s = $v->{value};
210     $s =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr(hex($1))/eg if $v->{is_encoded};
211     $s;
212     } @{$part{$name}};
213 wakaba 1.3 my %s = &{$self->{option}->{hook_decode_string}} ($self, $t,
214     type => 'parameter/encoded');
215     ($t,@part{$name}->[0]->{qw(charset language)})=(@s{qw(value charset language)});
216 wakaba 1.1 push @ret, [$name, {value => $t, charset => $part{$name}->[0]->{charset},
217     language => $part{$name}->[0]->{language},
218     is_parameter => 1}];
219     }
220     @ret;
221     }
222    
223 wakaba 1.11 ## $self->_save_param (@parameters)
224     ## --- Save parameter values to $self
225 wakaba 1.1 sub _save_param ($@) {
226     my $self = shift;
227 wakaba 1.11 my @p = @_;
228     $self->_parse_param_value (\@p) if $self->{option}->{parse_all};
229     $self->{param} = \@p;
230     $self;
231     }
232     *__save_param = \&_save_param; ## SHOULD NOT BE OVERRIDDEN!
233    
234     ## $self->_parse_param_value (\@parameters)
235     ## --- Parse each values of parameters
236     sub _parse_param_value ($\@) {
237     my $self = shift;
238     my $p = shift;
239     @$p = map {
240     if ($_->[1]->{is_parameter}) {
241 wakaba 1.9 $_->[1]->{value} = $self->_parse_value ($_->[0] => $_->[1]->{value});
242     }
243     $_;
244 wakaba 1.11 } @$p;
245     #$p;
246 wakaba 1.1 }
247    
248 wakaba 1.7 =back
249    
250     =head1 METHODS
251    
252     =over 4
253    
254     =item $p->add ($name => [$value], [$name => $value,...])
255 wakaba 1.1
256     Adds parameter name=value pair.
257    
258     Example:
259 wakaba 1.7 $p->add (title => 'foo of bar'); ## title="foo of bar"
260     $p->add (subject => 'hogehoge, foo'); ## subject*=''hogehoge%2C%20foo
261     $p->add (foo => ['bar', language => 'en']) ## foo*='en'bar
262     $p->add ('text/plain', ['', value => 1]) ## text/plain
263 wakaba 1.1
264     This method returns array reference of (name, {value => value, attribute...}).
265    
266     Available options: charset (charset name), language (language tag),
267     value (1/0, see example above).
268    
269     =cut
270    
271     sub add ($$;$%) {
272     my $self = shift;
273 wakaba 1.7 my %gp = @_;
274     my %option = %{$self->{option}};
275     for (grep {/^-/} keys %gp) {$option{substr ($_, 1)} = $gp{$_}}
276     $option{parse} = 1 if defined wantarray;
277     my $p;
278     for (grep {/^[^-]/} keys %gp) {
279     my ($name, $value, %po) = ($self->_n11n_param_name ($_));
280     if (ref $gp{$_}) {($value, %po) = @{$gp{$_}}} else {$value = $gp{$_}}
281     $p = [$name, {value => $value, charset => $po{charset},
282     is_parameter => 1, language => $po{language}}];
283     $p->[1]->{is_parameter} = 0 if !$value && $po{value};
284     Carp::croak "add: \$name contains of non-attribute-char: $name"
285     if $p->[1]->{is_parameter} && $name =~ /$REG{NON_http_attribute_char}/;
286 wakaba 1.9 $p->[1]->{value} = $self->_parse_value ($name => $p->[1]->{value})
287 wakaba 1.7 if $option{parse};
288     if ($option{prepend}) {
289     unshift @{$self->{param}}, $p;
290     } else {
291     push @{$self->{param}}, $p;
292     }
293 wakaba 1.1 }
294 wakaba 1.7 $p->[1]->{is_parameter}? $p->[1]->{value}: $p->[0];
295 wakaba 1.1 }
296 wakaba 1.7
297     sub replace ($%) {
298 wakaba 1.1 my $self = shift;
299 wakaba 1.7 my %gp = @_;
300     my %option = %{$self->{option}};
301     for (grep {/^-/} keys %gp) {$option{substr ($_, 1)} = $gp{$_}}
302     $option{parse} = 1 if defined wantarray;
303     my $p;
304     for (grep {/^[^-]/} keys %gp) {
305     my ($name, $value, %po) = ($self->_n11n_param_name ($_));
306 wakaba 1.12 if (ref $gp{$_} eq 'ARRAY') {($value, %po) = @{$gp{$_}}} else {$value = $gp{$_}}
307 wakaba 1.7 $p = [$name, {value => $value, charset => $po{charset},
308     is_parameter => 1, language => $po{language}}];
309 wakaba 1.8 $p->[1]->{is_parameter} = 0 if !defined ($value) && $po{value};
310 wakaba 1.7 Carp::croak "replace: \$name contains of non-attribute-char: $name"
311     if $p->[1]->{is_parameter} && $name =~ /$REG{NON_http_attribute_char}/;
312 wakaba 1.9 $p->[1]->{value} = $self->_parse_value ($name => $p->[1]->{value})
313 wakaba 1.7 if $option{parse};
314     my $f = 0;
315     for my $param (@{$self->{param}}) {
316     if ($param->[0] eq $name) {$param = $p; $f = 1}
317 wakaba 1.1 }
318 wakaba 1.7 push @{$self->{param}}, $p unless $f == 1;
319 wakaba 1.1 }
320 wakaba 1.7 $p->[1]->{is_parameter}? $p->[1]->{value}: $p->[0];
321 wakaba 1.1 }
322    
323 wakaba 1.8 ## TODO: multiple parameters support
324 wakaba 1.1 sub delete ($$;%) {
325     my $self = shift;
326 wakaba 1.7 my ($name, $index) = ($self->_n11n_param_name (shift), shift);
327 wakaba 1.1 my $i = 0;
328     for my $param (@{$self->{param}}) {
329     if ($param->[0] eq $name) {
330     $i++;
331     if ($index == 0 || $i == $index) {
332     undef $param;
333     return $self if $i == $index;
334     }
335     }
336     }
337     $self;
338     }
339    
340     sub count ($;$%) {
341     my $self = shift;
342 wakaba 1.7 my $name = $self->_n11n_param_name (shift);
343 wakaba 1.1 unless ($name) {
344     $self->_delete_empty ();
345     return $#{$self->{param}}+1;
346     }
347     my $count = 0;
348     for my $param (@{$self->{param}}) {
349     if ($param->[0] eq $name) {
350     $count++;
351     }
352     }
353     $count;
354     }
355    
356    
357 wakaba 1.7 sub parameter ($$;$%) {
358 wakaba 1.1 my $self = shift;
359 wakaba 1.7 my $name = $self->_n11n_param_name (shift);
360 wakaba 1.1 my $newvalue = shift;
361 wakaba 1.7 return $self->replace ($name => $newvalue,@_) if defined $newvalue;
362 wakaba 1.1 my @ret;
363     for my $param (@{$self->{param}}) {
364     if ($param->[0] eq $name) {
365     unless (wantarray) {
366 wakaba 1.3 $param->[1]->{value}
367 wakaba 1.9 = $self->_parse_value ($name => $param->[1]->{value});
368 wakaba 1.1 return $param->[1]->{value};
369     } else {
370 wakaba 1.3 $param->[1]->{value}
371 wakaba 1.9 = $self->_parse_value ($name => $param->[1]->{value});
372 wakaba 1.1 push @ret, $param->[1]->{value};
373     }
374     }
375     }
376     @ret;
377     }
378    
379     sub parameter_name ($$;$) {
380     my $self = shift;
381     my $i = shift;
382     my $newname = shift;
383     if ($newname) {
384 wakaba 1.4 return 0 if $newname =~ /$REG{NON_http_attribute_char}/;
385 wakaba 1.1 $self->{param}->[$i]->[0] = $newname;
386     }
387     $self->{param}->[$i]->[0];
388     }
389     sub parameter_value ($$;$) {
390     my $self = shift;
391     my $i = shift;
392     my $newvalue = shift;
393     if ($newvalue) {
394 wakaba 1.9 $newvalue = $self->_parse_value ($self->{param}->[$i]->[0] => $newvalue);
395 wakaba 1.1 $self->{param}->[$i]->[1]->{value} = $newvalue;
396     }
397     $self->{param}->[$i]->[1]->{value}
398 wakaba 1.9 = $self->_parse_value
399 wakaba 1.1 ($self->{param}->[$i]->[0] => $self->{param}->[$i]->[1]->{value});
400     $self->{param}->[$i]->[1]->{value};
401     }
402    
403    
404     sub _delete_empty ($) {
405     my $self = shift;
406 wakaba 1.11 $self->{param} = [grep {ref $_} @{$self->{param}}];
407 wakaba 1.1 }
408    
409    
410 wakaba 1.7 =item $field-body = $p->stringify ()
411 wakaba 1.1
412 wakaba 1.7 Returns C<field-body> as a string.
413 wakaba 1.1
414     =cut
415    
416     sub stringify ($;%) {
417     my $self = shift;
418 wakaba 1.7 my %o = @_;
419     my %option = %{$self->{option}};
420     for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}}
421     $self->_delete_empty;
422 wakaba 1.9 my @ret = ();
423     $self->scan (sub {
424     my $self = shift;
425     my ($n => $v) = @_[0 => 2]; #$_->[1];
426     return unless $self->_stringify_params_check (@_[0 => 2]);
427 wakaba 1.1 my $new = '';
428     if ($v->{is_parameter}) {
429     my ($encoded, @value) = (0, '');
430 wakaba 1.3 my (%e) = &{$self->{option}->{hook_encode_string}} ($self,
431     $v->{value}, current_charset => $v->{charset}, language => $v->{language},
432     type => 'parameter');
433 wakaba 1.11 if (!defined $e{value}) {
434 wakaba 1.8 $value[0] = undef;
435     } elsif ($option{use_parameter_extension} && ($e{charset} || $e{language}
436 wakaba 1.3 || $e{value} =~ /[\x00\x0D\x0A\x80-\xFF]/)) {
437 wakaba 1.1 my ($charset, $lang);
438     $encoded = 1;
439 wakaba 1.3 ($charset, $lang) = ($e{charset}, $e{language});
440 wakaba 1.1 ## Note: %-quoting for charset and for language is not allowed.
441     ## But charset name can be included non-sttribute-char such as "'".
442     ## How can we treat this?
443 wakaba 1.4 $charset =~ s/($REG{NON_http_attribute_char})/sprintf('%%%02X', ord $1)/ge;
444     $lang =~ s/($REG{NON_http_attribute_char})/sprintf('%%%02X', ord $1)/ge;
445 wakaba 1.7 if (length $e{value} > $option{parameter_value_max_length}) {
446 wakaba 1.13 for my $i (0..length ($e{value})/$option{parameter_value_split_length}) {
447     $value[$i] = substr ($e{value},
448     $i*$option{parameter_value_split_length},
449     $option{parameter_value_split_length});
450 wakaba 1.1 }
451 wakaba 1.3 } else {$value[0] = $e{value}}
452 wakaba 1.1 for my $i (0..$#value) {
453 wakaba 1.4 $value[$i] =~
454     s/($REG{NON_http_attribute_char})/sprintf('%%%02X', ord $1)/ge;
455 wakaba 1.1 }
456     $value[0] = "${charset}'${lang}'".$value[0];
457 wakaba 1.3 } elsif (length $e{value} == 0) {
458 wakaba 1.1 $value[0] = '""';
459     } else {
460 wakaba 1.7 if ($option{use_parameter_extension}
461     && length $e{value} > $option{parameter_value_max_length}) {
462 wakaba 1.13 for my $i (0..length ($e{value})/$option{parameter_value_split_length}) {
463 wakaba 1.7 $value[$i] = Message::Util::quote_unsafe_string
464 wakaba 1.13 (substr ($e{value}, $i*$option{parameter_value_split_length},
465     $option{parameter_value_split_length}),
466 wakaba 1.4 unsafe => 'NON_http_attribute_char');
467 wakaba 1.1 }
468     } else {
469 wakaba 1.9 my $unsafe = $self->{option}->{parameter_value_unsafe_rule}->{$n}
470 wakaba 1.7 || $self->{option}->{parameter_value_unsafe_rule}->{'*default'};
471     $value[0] = Message::Util::quote_unsafe_string
472     ($e{value}, unsafe => $unsafe);
473 wakaba 1.1 }
474     }
475     ## Note: quoted-string for parameter name is not allowed.
476     ## But it is better than output bare non-atext.
477     if ($#value == 0) {
478     $new =
479 wakaba 1.9 Message::Util::quote_unsafe_string ($n,
480 wakaba 1.7 unsafe => 'NON_attribute_char')
481 wakaba 1.8 .($encoded?'*':'').'='.$value[0]
482     if defined $value[0];
483 wakaba 1.1 } else {
484     my @new;
485 wakaba 1.7 my $name = Message::Util::quote_unsafe_string
486 wakaba 1.9 ($n, unsafe => 'NON_http_attribute_char');
487 wakaba 1.1 for my $i (0..$#value) {
488     push @new, $name.'*'.$i.($encoded?'*':'').'='.$value[$i];
489     }
490 wakaba 1.11 $new = join $self->{option}->{separator}, @new;
491 wakaba 1.1 }
492     } else {
493 wakaba 1.3 my %e = &{$self->{option}->{hook_encode_string}} ($self,
494 wakaba 1.9 $n, type => 'phrase');
495 wakaba 1.7 $new = Message::Util::quote_unsafe_string ($e{value},
496 wakaba 1.4 unsafe => 'NON_http_token_wsp');
497 wakaba 1.1 }
498 wakaba 1.11 push @ret, $new if length $new;
499 wakaba 1.9 });
500     join $self->{option}->{separator}, @ret;
501 wakaba 1.1 }
502 wakaba 1.7 *as_string = \&stringify;
503 wakaba 1.1
504 wakaba 1.9 sub _stringify_params_check ($$$) {
505     my $self = shift;
506     my ($name, $value) = @_;
507     1;
508     }
509    
510 wakaba 1.7 sub scan ($&) {
511     my ($self, $sub) = @_;
512     #my $sort;
513     #$sort = \&_header_cmp if $self->{option}->{sort} eq 'good-practice';
514     #$sort = {$a cmp $b} if $self->{option}->{sort} eq 'alphabetic';
515     my @param = @{$self->{param}};
516     #if (ref $sort) {
517     # @field = sort $sort @{$self->{param}};
518     #}
519     for my $param (@param) {
520 wakaba 1.9 &$sub($self, $param->[0] => $param->[1]->{value}, $param->[1]);
521 wakaba 1.7 }
522     }
523    
524 wakaba 1.8 =item $option-value = $p->option ($option-name)
525 wakaba 1.1
526 wakaba 1.7 Gets option value.
527    
528 wakaba 1.8 =item $p->option ($option-name, $option-value, ...)
529 wakaba 1.7
530     Set option value(s). You can pass multiple option name-value pair
531     as parameter when setting.
532 wakaba 1.1
533     =cut
534    
535 wakaba 1.7 sub option ($;$%) {
536 wakaba 1.1 my $self = shift;
537 wakaba 1.7 my $format = $self->{option}->{format};
538     $self->SUPER::option (@_);
539     if ($format ne $self->{option}->{format}) {
540     $self->scan (sub {
541     if (ref $_[1]) {
542     $_[1]->option (-format => $self->{option}->{format});
543     }
544     });
545 wakaba 1.1 }
546     }
547    
548 wakaba 1.9 ## value_type: Inherited
549 wakaba 1.1
550 wakaba 1.7 sub value_unsafe_rule ($$;$%) {
551 wakaba 1.1 my $self = shift;
552 wakaba 1.7 if (@_ == 1) {
553     return $self->{option}->{parameter_value_unsafe_rule}->{ $_[0] };
554     }
555     while (my ($name, $value) = splice (@_, 0, 2)) {
556     $name = $self->_n11n_param_name ($name);
557     $self->{option}->{parameter_value_unsafe_rule}->{$name} = $value;
558     }
559 wakaba 1.1 }
560    
561 wakaba 1.8 =item $clone = $p->clone ()
562 wakaba 1.1
563 wakaba 1.7 Returns a copy of the object.
564 wakaba 1.1
565     =cut
566    
567 wakaba 1.7 sub clone ($) {
568 wakaba 1.1 my $self = shift;
569 wakaba 1.7 $self->_delete_empty;
570     my $clone = $self->SUPER::clone;
571     $clone->{param} = Message::Util::make_clone ($self->{param});
572     $clone->{value_type} = Message::Util::make_clone ($self->{value_type});
573     $clone;
574 wakaba 1.1 }
575    
576     sub _delete_fws ($$) {
577     my $self = shift;
578     my $body = shift;
579 wakaba 1.3 $body =~ s{($REG{quoted_string}|$REG{domain_literal})|((?:$REG{token}|$REG{S_encoded_word})(?:$REG{WSP}+(?:$REG{token}|$REG{S_encoded_word}))+)|$REG{WSP}+}{
580     my ($o,$p) = ($1,$2);
581     if ($o) {$o}
582     elsif ($p) {$p=~s/$REG{WSP}+/\x20/g;$p}
583     else {''}
584 wakaba 1.1 }gex;
585     $body;
586     }
587    
588 wakaba 1.7 sub _n11n_param_name ($$) {
589     my $self = shift;
590     my $s = shift;
591     $s = lc $s unless $self->{option}->{parameter_name_case_sensible};
592     $s;
593     }
594    
595 wakaba 1.1 =head1 LICENSE
596    
597     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
598    
599     This program is free software; you can redistribute it and/or modify
600     it under the terms of the GNU General Public License as published by
601     the Free Software Foundation; either version 2 of the License, or
602     (at your option) any later version.
603    
604     This program is distributed in the hope that it will be useful,
605     but WITHOUT ANY WARRANTY; without even the implied warranty of
606     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
607     GNU General Public License for more details.
608    
609     You should have received a copy of the GNU General Public License
610     along with this program; see the file COPYING. If not, write to
611     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
612     Boston, MA 02111-1307, USA.
613    
614     =head1 CHANGE
615    
616     See F<ChangeLog>.
617 wakaba 1.13 $Date: 2002/06/15 07:15:59 $
618 wakaba 1.1
619     =cut
620    
621     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24