/[suikacvs]/test/cvs
Suika

Diff of /test/cvs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.15 by wakaba, Fri Apr 5 14:56:26 2002 UTC revision 1.38 by wakaba, Sun Jul 21 03:26:02 2002 UTC
# Line 1  Line 1 
1    
2  =head1 NAME  =head1 NAME
3    
4  Message::Header Perl module  Message::Header --- A Perl Module for Internet Message Headers
   
 =head1 DESCRIPTION  
   
 Perl module for RFC 822/2822 message C<header>.  
5    
6  =cut  =cut
7    
8  package Message::Header;  package Message::Header;
9  use strict;  use strict;
10  use vars qw($VERSION %REG);  use vars qw(%DEFAULT @ISA %REG $VERSION);
11  $VERSION = '1.00';  $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
12  use Carp ();  require Message::Field::Structured;     ## This may seem silly:-)
13  use overload '@{}' => sub { shift->_delete_empty_field->{field} },  push @ISA, qw(Message::Field::Structured);
14               '""' => sub { shift->stringify },  
15               fallback => 1;  %REG = %Message::Util::REG;
16            $REG{M_field} = qr/^([^\x3A]+):$REG{FWS}([\x00-\xFF]*)$/;
17  $REG{WSP}     = qr/[\x09\x20]/;          $REG{M_fromline} = qr/^\x3E?From$REG{WSP}+([\x00-\xFF]*)$/;
18  $REG{FWS}     = qr/[\x09\x20]*/;          $REG{ftext} = qr/[\x21-\x39\x3B-\x7E]+/;        ## [2]822
19  $REG{M_field} = qr/^([^\x3A]+):$REG{FWS}([\x00-\xFF]*)$/;          $REG{NON_ftext} = qr/[^\x21-\x39\x3B-\x7E]/;    ## [2]822
20  $REG{M_fromline} = qr/^\x3E?From$REG{WSP}+([\x00-\xFF]*)$/;          $REG{NON_ftext_usefor} = qr/[^0-9A-Za-z-]/;     ## name-character
21  $REG{UNSAFE_field_name} = qr/[\x00-\x20\x3A\x7F-\xFF]/;          $REG{NON_ftext_http} = $REG{NON_http_token};
22    
23  =head2 options  ## Namespace support
24            our %NS_phname2uri;     ## PH-namespace name -> namespace URI
25  These options can be getten/set by C<get_option>/C<set_option>          our %NS_uri2package;    ## namespace URI -> Package name
26  method.          our %NS_uri2phpackage;  ## namespace URI -> PH-Package name
27            require Message::Header::Default;       ## Default namespace
28  =head3 capitalize = 0/1  
29    ## Initialize of this class -- called by constructors
30  (First character of) C<field-name> is capitalized  %DEFAULT = (
31  when C<stringify>.  (Default = 1)      -_HASH_NAME => 'value',
32        -_METHODS   => [qw|field field_exist field_type add replace count delete subject id is|],
33  =head3 fold_length = numeric value      -_MEMBERS   => [qw|value|],
34        -_VALTYPE_DEFAULT   => ':default',
35  Length of line used to fold.  (Default = 70)      -by => 'name',
36        -field_format_pattern       => '%s: %s',
37  =head3 mail_from = 0/1      -field_name_case_sensible   => 0,
38        -field_name_unsafe_rule     => 'NON_ftext',
39  Outputs "From " line (known as Un*x From, Mail-From, and so on)      -field_name_validation      => 0,
40  when C<stringify>.  (Default = 0)      -field_sort => 0,
41        -format     => 'mail-rfc2822',
42  =cut      -header_default_charset     => 'iso-2022-int-1',
43        -header_default_charset_input       => 'iso-2022-int-1',
44  =head1 CONSTRUCTORS      -hook_init_fill_options     => sub {},
45        -hook_stringify_fill_fields => sub {},
46  The following methods construct new C<Message::Header> objects:      -linebreak_strict   => 0,
47        -line_length_max    => 60,  ## For folding
48  =over 4      #ns_default_phuri
49        -output_bcc => 0,
50  ## Initialize      -output_folding     => 1,
51  my %DEFAULT = (      -output_mail_from   => 0,
52    capitalize    => 1,      #parse_all
53    fold_length   => 70,      -translate_underscore       => 1,
54    #field_type   => {},      #uri_mailto_safe
55    format        => 'mail-rfc2822',      -uri_mailto_safe_level      => 4,
56    mail_from     => 0,      -use_folding        => 1,
57    output_bcc    => 0,      #value_type
   parse_all     => 0,  
   sort  => 'none',  
   translate_underscore  => 1,  
   validate      => 1,  
58  );  );
 $DEFAULT{field_type} = {  
         ':DEFAULT'      => 'Message::Field::Unstructured',  
           
         received        => 'Message::Field::Received',  
         'x-received'    => 'Message::Field::Received',  
           
         'content-type'  => 'Message::Field::ContentType',  
         'content-disposition'   => 'Message::Field::ContentDisposition',  
         'auto-submitted'        => 'Message::Field::ValueParams',  
         link    => 'Message::Field::ValueParams',  
         archive => 'Message::Field::ValueParams',  
         'x-face-type'   => 'Message::Field::ValueParams',  
           
         subject => 'Message::Field::Subject',  
         'x-nsubject'    => 'Message::Field::Subject',  
           
         'list-software' => 'Message::Field::UA',  
         'user-agent'    => 'Message::Field::UA',  
         server  => 'Message::Field::UA',  
           
         ## Numeric value  
         'content-length'        => 'Message::Field::Numval',  
         lines   => 'Message::Field::Numval',  
         'max-forwards'  => 'Message::Field::Numval',  
         'mime-version'  => 'Message::Field::Numval',  
         'x-jsmail-priority'     => 'Message::Field::Numval',  
         'x-priority'    => 'Message::Field::Numval',  
           
         path    => 'Message::Field::Path',  
 };  
 for (qw(cancel-lock importance   precedence list-id  
   x-face x-mail-count x-msmail-priority x-priority xref))  
   {$DEFAULT{field_type}->{$_} = 'Message::Field::Structured'}  
 for (qw(approved bcc cc complaints-to  
   delivered-to disposition-notification-to envelope-to  
   errors-to fcc from mail-followup-to mail-followup-cc  
   mail-reply-to  
   notice-requested-upon-delivery-to reply-to resent-bcc  
   resent-cc resent-to resent-from resent-sender return-path  
   return-receipt-to sender to x-approved x-beenthere  
   x-complaints-to x-envelope-from x-envelope-sender  
   x-envelope-to x-ml-address x-ml-command x-ml-to x-nfrom x-nto))  
   {$DEFAULT{field_type}->{$_} = 'Message::Field::Address'}  
 for (qw(date date-received delivery-date expires  
   expire-date nntp-posting-date posted reply-by resent-date x-tcup-date))  
   {$DEFAULT{field_type}->{$_} = 'Message::Field::Date'}  
 for (qw(article-updates client-date content-id in-reply-to message-id  
   references resent-message-id see-also supersedes))  
   {$DEFAULT{field_type}->{$_} = 'Message::Field::MsgID'}  
 for (qw(accept accept-charset accept-encoding accept-language  
   content-language  
   content-transfer-encoding encrypted followup-to keywords  
   list-archive list-digest list-help list-owner  
   list-post list-subscribe list-unsubscribe list-url uri newsgroups  
   x-brother x-daughter x-respect x-moe x-syster x-wife))  
   {$DEFAULT{field_type}->{$_} = 'Message::Field::CSV'}  
 for (qw(content-alias content-base content-location location referer  
   url x-home-page x-http_referer  
   x-info x-pgp-key x-ml-url x-uri x-url x-web))  
   {$DEFAULT{field_type}->{$_} = 'Message::Field::URI'}  
59    
60  ## taken from L<HTTP::Header>  ## taken from L<HTTP::Header>
61  # "Good Practice" order of HTTP message headers:  # "Good Practice" order of HTTP message headers:
# Line 155  my @header_order = qw( Line 87  my @header_order = qw(
87  );  );
88  my %header_order;  my %header_order;
89    
90    =head1 CONSTRUCTORS
91    
92    The following methods construct new C<Message::Header> objects:
93    
94    =over 4
95    
96    =cut
97    
98  sub _init ($;%) {  sub _init ($;%) {
99    my $self = shift;    my $self = shift;
100    my %options = @_;    my %options = @_;
101    $self->{field} = [];    my $DEFAULT = Message::Util::make_clone (\%DEFAULT);
102    $self->{option} = \%DEFAULT;    $self->SUPER::_init (%$DEFAULT, %options);
103      $self->{value} = [];
104      $self->_ns_load_ph ('default');
105      $self->_ns_load_ph ('x-rfc822');
106      $self->_ns_load_ph ('x-http');
107      $self->{option}->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-rfc822'}
108        unless $self->{option}->{ns_default_phuri};
109      
110      ## For text/rfc822-headers
111      if (ref $options{entity_header}) {
112        $self->{entity_header} = $options{entity_header};
113        delete $options{entity_header};
114      }
115    my @new_fields = ();    my @new_fields = ();
116    for my $name (keys %options) {    for my $name (keys %options) {
117      if (substr ($name, 0, 1) eq '-') {      unless (substr ($name, 0, 1) eq '-') {
       $self->{option}->{substr ($name, 1)} = $options{$name};  
     } else {  
118        push @new_fields, ($name => $options{$name});        push @new_fields, ($name => $options{$name});
119      }      }
120    }    }
121    $self->add (@new_fields, -parse => $self->{option}->{parse_all})    &{ $self->{option}->{hook_init_fill_options} } ($self, $self->{option});
122      if $#new_fields > -1;    $self->_init_by_format ($self->{option}->{format}, $self->{option});
     
   my $format = $self->{option}->{format};  
   if ($format =~ /^cgi/) {  
     unshift @header_order, qw(content-type location);  
     $self->{option}->{sort} = 'good-practice';  
   } elsif ($format =~ /^http/) {  
     $self->{option}->{sort} = 'good-practice';  
   }  
     
123    # Make alternative representations of @header_order.  This is used    # Make alternative representations of @header_order.  This is used
124    # for sorting.    # for sorting.
125    my $i = 1;    my $i = 1;
126    for (@header_order) {    for (@header_order) {
127        $header_order{$_} = $i++ unless $header_order{$_};        $header_order{$_} = $i++ unless $header_order{$_};
128    }    }
129      
130      $self->add (@new_fields, -parse => $self->{option}->{parse_all})
131        if $#new_fields > -1;
132  }  }
133    
134  =item Message::Header->new ([%initial-fields/options])  sub _init_by_format ($$\%) {
135      my $self = shift;
136      my ($format, $option) = @_;
137      if ($format =~ /http/) {
138        $option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-http'};
139        if ($format =~ /cgi/) {
140          unshift @header_order, qw(content-type location);
141          $option->{field_sort} = 'good-practice';
142          $option->{use_folding} = 0;
143        } else {
144          $option->{field_sort} = 'good-practice';
145        }
146      } elsif ($format =~ /mail|news/) {    ## RFC 822
147        $option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-rfc822'};
148      }
149      if ($format =~ /uri-url-mailto/) {
150        $option->{output_bcc} = 0;
151        $option->{field_format_pattern} = '%s=%s';
152        $option->{output_folding} = sub {
153          $_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge;
154          $_[1];
155        };  ## Yes, this is not folding!
156      }
157    }
158    
159    =item $msg = Message::Header->new ([%initial-fields/options])
160    
161  Constructs a new C<Message::Headers> object.  You might pass some initial  Constructs a new C<Message::Headers> object.  You might pass some initial
162  C<field-name>-C<field-body> pairs and/or options as parameters to the constructor.  C<field-name>-C<field-body> pairs and/or options as parameters to the constructor.
# Line 203  Example: Line 172  Example:
172    
173  =cut  =cut
174    
175  sub new ($;%) {  ## Inherited
   my $class = shift;  
   my $self = bless {}, $class;  
   $self->_init (@_);  
   $self;  
 }  
176    
177  =item Message::Header->parse ($header, [%initial-fields/options])  =item $msg = Message::Header->parse ($header, [%initial-fields/options])
178    
179  Parses given C<header> and constructs a new C<Message::Headers>  Parses given C<header> and constructs a new C<Message::Headers>
180  object.  You might pass some additional C<field-name>-C<field-body> pairs  object.  You might pass some additional C<field-name>-C<field-body> pairs
# Line 223  sub parse ($$;%) { Line 187  sub parse ($$;%) {
187    my $header = shift;    my $header = shift;
188    my $self = bless {}, $class;    my $self = bless {}, $class;
189    $self->_init (@_);    $self->_init (@_);
190    $header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos;     ## unfold    if ($self->{option}->{linebreak_strict}) {
191        $header =~ s/\x0D\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding};
192      } else {
193        $header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding};
194      }
195      my %option = (%{ $self->{option} });
196      $option{parse_all} = 0;
197    for my $field (split /\x0D?\x0A/, $header) {    for my $field (split /\x0D?\x0A/, $header) {
198      if ($field =~ /$REG{M_fromline}/) {      if ($field =~ /$REG{M_fromline}/) {
199        my $body = $1;        my ($s,undef,$value) = $self->_value_to_arrayitem
200        $body = $self->_field_body ($body, 'mail-from')          ('mail-from' => $1, \%option);
201          if $self->{option}->{parse_all};        push @{$self->{value}}, $value if $s;
       push @{$self->{field}}, {name => 'mail-from', body => $body};  
202      } elsif ($field =~ /$REG{M_field}/) {      } elsif ($field =~ /$REG{M_field}/) {
203        my ($name, $body) = (lc $1, $2);        my ($name, $body) = ($1, $2);
       $name =~ s/$REG{WSP}+$//;  
204        $body =~ s/$REG{WSP}+$//;        $body =~ s/$REG{WSP}+$//;
205        $body = $self->_field_body ($body, $name) if $self->{option}->{parse_all};        my ($s,undef,$value) = $self->_value_to_arrayitem
206        push @{$self->{field}}, {name => $name, body => $body};          ($name => $body, \%option);
207      }        push @{$self->{value}}, $value if $s;
208        } elsif (length $field) {
209          my ($s,undef,$value) = $self->_value_to_arrayitem
210            ('x-unknown' => $field, \%option);
211          push @{$self->{value}}, $value if $s;
212        }
213      }
214      $self->_ns_associate_numerical_prefix;        ## RFC 2774 namespace
215      for (@{ $self->{value} }) {
216        no strict 'refs';
217        $_->{name}
218          = &{ ${ &_NS_uri2package ($_->{ns}).'::OPTION' }{n11n_name} }
219          ($self, &_NS_uri2package ($_->{ns}), $_->{name});
220        $_->{body} = $self->_parse_value ($_->{name} => $_->{body}, ns => $_->{ns})
221          if $self->{option}->{parse_all};
222    }    }
223    $self;    $self;
224  }  }
225    
226  =item Message::Header->parse_array (\@header, [%initial-fields/options])  =item $msg = Message::Header->parse_array (\@header, [%initial-fields/options])
227    
228  Parses given C<header> and constructs a new C<Message::Headers>  Parses given C<header> and constructs a new C<Message::Headers>
229  object.  Same as C<Message::Header-E<lt>parse> but this method  object.  Same as C<Message::Header-E<lt>parse> but this method
# Line 260  sub parse_array ($\@;%) { Line 242  sub parse_array ($\@;%) {
242    $self->_init (@_);    $self->_init (@_);
243    while (1) {    while (1) {
244      my $field = shift @$header;      my $field = shift @$header;
245      while (1) {      if ($self->{option}->{use_folding}) {
246        if ($$header[0] =~ /^$REG{WSP}/) {        while (1) {
247          $field .= shift @$header;          if ($$header[0] =~ /^$REG{WSP}/) {
248        } else {last}            $field .= shift @$header;
249            } else {last}
250          }
251      }      }
252      $field =~ tr/\x0D\x0A//d;   ## BUG: not safe for bar CR/LF      if ($self->{option}->{linebreak_strict}) {
253          $field =~ s/\x0D\x0A//g;
254        } else {
255          $field =~ tr/\x0D\x0A//d;
256        }
257        local $self->{option}->{parse} = $self->{option}->{parse_all};
258      if ($field =~ /$REG{M_fromline}/) {      if ($field =~ /$REG{M_fromline}/) {
259        my $body = $1;        my ($s,undef,$value) = $self->_value_to_arrayitem
260        $body = $self->_field_body ($body, 'mail-from')          ('mail-from' => $1, $self->{option});
261          if $self->{option}->{parse_all};        push @{$self->{value}}, $value if $s;
       push @{$self->{field}}, {name => 'mail-from', body => $body};  
262      } elsif ($field =~ /$REG{M_field}/) {      } elsif ($field =~ /$REG{M_field}/) {
263        my ($name, $body) = (lc $1, $2);        my ($name, $body) = ($self->_n11n_field_name ($1), $2);
       $name =~ s/$REG{WSP}+$//;  
264        $body =~ s/$REG{WSP}+$//;        $body =~ s/$REG{WSP}+$//;
265        $body = $self->_field_body ($body, $name) if $self->{option}->{parse_all};        my ($s,undef,$value) = $self->_value_to_arrayitem
266        push @{$self->{field}}, {name => $name, body => $body};          ($name => $body, $self->{option});
267          push @{$self->{value}}, $value if $s;
268        } elsif (length $field) {
269          my ($s,undef,$value) = $self->_value_to_arrayitem
270            ('x-unknown' => $field, $self->{option});
271          push @{$self->{value}}, $value if $s;
272      }      }
273      last if $#$header < 0;      last if $#$header < 0;
274    }    }
275      $self->_ns_associate_numerical_prefix;        ## RFC 2774 namespace
276    $self;    $self;
277  }  }
278    
# Line 296  context, only first one is returned.) Line 289  context, only first one is returned.)
289    
290  =cut  =cut
291    
292  sub field ($$) {  sub field ($@) {shift->SUPER::item (@_)}
293    sub field_exist ($@) {shift->SUPER::item_exist (@_)}
294    
295    ## item-by?, \$checked-item, {item-key => 1}, \%option
296    sub _item_match ($$\$\%\%) {
297    my $self = shift;    my $self = shift;
298    my $name = lc shift;    my ($by, $i, $list, $option) = @_;
299    my @ret;    return 0 unless ref $$i;  ## Already removed
300    for my $field (@{$self->{field}}) {    if ($by eq 'name') {
301      if ($field->{name} eq $name) {      my %o = %$option; #$o{parse} = 0;
302        unless (wantarray) {      my %l;
303          $field->{body} = $self->_field_body ($field->{body}, $name);      for (keys %$list) {
304          return $field->{body};        my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', \%o);
305          if ($s) {
306            $l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1;
307        } else {        } else {
308          $field->{body} = $self->_field_body ($field->{body}, $name);          $l{$v->{name} .':'. ( $option->{ns} || $self->{option}->{ns_default_phuri} ) } = 1;
309          push @ret, $field->{body};        }
310        }
311        return 1 if $l{$$i->{name} . ':' . $$i->{ns}};
312      } elsif ($by eq 'ns') {
313        return 1 if $list->{ $$i->{ns} };
314      } elsif ($by eq 'http-ns-define') {
315        if ($$i->{ns} eq $self->{ns}->{phname2uri}->{'x-http'}
316         || $$i->{ns} eq $self->{ns}->{phname2uri}->{'x-http-c'}) {
317          my $n = $$i->{name};
318          if ($n eq 'opt' || $n eq 'c-opt' || $n eq 'man' || $n eq 'c-man') {
319            $option->{parse} = 0;
320            $$i->{body} = $self->_parse_value ($$i->{name} => $$i->{body}, ns => $$i->{ns});
321            for my $j (0..$$i->{body}->count-1) {
322              return 1 if $list->{ ($$i->{body}->value ($j))[0]->value };
323            }
324        }        }
325      }      }
326    }    }
327    if ($#ret < 0) {    0;
     return $self->add ($name);  
   }  
   @ret;  
328  }  }
329    *_delete_match = \&_item_match;
330    
331  sub field_exist ($$) {  ## Returns returned item value    \$item-value, \%option
332    my $self = shift;  sub _item_return_value ($\$\%) {
333    my $name = lc shift;    if (ref ${$_[1]}->{body}) {
334    my @ret;      ${$_[1]}->{body};
335    for my $field (@{$self->{field}}) {    } else {
336      return 1 if ($field->{name} eq $name);      ${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body},
337          ns => ${$_[1]}->{ns});
338        ${$_[1]}->{body};
339      }
340    }
341    *_add_return_value = \&_item_return_value;
342    *_replace_return_value = \&_item_return_value;
343    
344    ## Returns returned (new created) item value    $name, \%option
345    sub _item_new_value ($$\%) {
346      my $self = shift;
347      my ($name, $option) = @_;
348      if ($option->{by} eq 'http-ns-define') {
349        my $value = $self->_parse_value (opt => '', ns => $self->{ns}->{phname2uri}->{'x-http'});
350        ($value->value (0))[0]->value ($name);
351        {name => 'opt', body => $value, ns => $self->{ns}->{phname2uri}->{'x-http'}};
352      } else {
353        my ($s,undef,$value) = $self->_value_to_arrayitem
354            ($name => '', $option);
355        $s? $value: undef;
356    }    }
   0;  
357  }  }
358    
 =head2 $self->field_name ($index)  
   
 Returns C<field-name> of $index'th C<field>.  
   
 =head2 $self->field_body ($index)  
359    
 Returns C<field-body> of $index'th C<field>.  
360    
361  =cut  ## $self->_parse_value ($type, $value, %options);
362    sub _parse_value ($$$;%) {
363  sub field_name ($$) {    my $self = shift;
364    my $self = shift;    my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT};
365    $self->{field}->[shift]->{name};    my $value = shift;  return $value if ref $value;
366  }    my %option = @_;
367  sub field_body ($$) {    my $vtype; { no strict 'refs';
368    my $self = shift;      my $vt = ${&_NS_uri2package ($option{ns}).'::OPTION'}{value_type};
369    my $i = shift;      if (ref $vt) {
370    $self->{field}->[$i]->{body}        $vtype = $vt->{$name} || $vt->{$self->{option}->{_VALTYPE_DEFAULT}};
371     = $self->_field_body ($self->{field}->[$i]->{body}, $self->{field}->[$i]->{name});      }
372    $self->{field}->[$i]->{body};      ## For compatiblity.
373  }      unless (ref $vtype) { $vtype = $self->{option}->{value_type}->{$name}
374          || $self->{option}->{value_type}->{$self->{option}->{_VALTYPE_DEFAULT}} }
375  sub _field_body ($$$) {    }
376    my $self = shift;    my $vpackage = $vtype->[0];
377    my ($body, $name) = @_;    my %vopt = %{$vtype->[1]} if ref $vtype->[1];
378    unless (ref $body) {    if ($vpackage eq ':none:') {
379      my $type = $self->{option}->{field_type}->{$name}      return $value;
380              || $self->{option}->{field_type}->{':DEFAULT'};    } elsif (length $value) {
381      eval "require $type" or Carp::croak ("_field_body: $type: $@");      eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@};
382      unless ($body) {      return $vpackage->parse ($value,
383        $body = $type->new (-field_name => $name,        -format   => $self->{option}->{format},
384          -format => $self->{option}->{format});        -field_ns => $option{ns},
385      } else {        -field_name       => $name,
386        $body = $type->parse ($body, -field_name => $name,      -header_default_charset     => $self->{option}->{header_default_charset},
387          -format => $self->{option}->{format});      -header_default_charset_input       => $self->{option}->{header_default_charset_input},
388      }      -internal_charset_name      => $self->{option}->{internal_charset_name},
389          -parse_all        => $self->{option}->{parse_all},
390        %vopt);
391      } else {
392        eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@};
393        return $vpackage->new (
394          -format   => $self->{option}->{format},
395          -field_ns => $option{ns},
396          -field_name       => $name,
397        -header_default_charset     => $self->{option}->{header_default_charset},
398        -header_default_charset_input       => $self->{option}->{header_default_charset_input},
399        -internal_charset_name      => $self->{option}->{internal_charset_name},
400          -parse_all        => $self->{option}->{parse_all},
401        %vopt);
402      }
403    }
404    
405    ## Defined for text/rfc822-headers
406    sub entity_header ($;$) {
407      my $self = shift;
408      my $new_header = shift;
409      if (ref $new_header) {
410        $self->{header} = $new_header;
411    }    }
412    $body;    $self->{header};
413  }  }
414    
415  =head2 $self->field_name_list ()  =head2 $self->field_name_list ()
# Line 377  returns ALL names.) Line 422  returns ALL names.)
422    
423  sub field_name_list ($) {  sub field_name_list ($) {
424    my $self = shift;    my $self = shift;
425    $self->_delete_empty_field ();    $self->_delete_empty ();
426    map {$_->{name}} @{$self->{field}};    map { $_->{name} . ':' . $_->{ns} } @{$self->{value}};
427    }
428    
429    sub namespace_ph_default ($;$) {
430      my $self = shift;
431      if (defined $_[0]) {
432        no strict 'refs';
433        $self->{option}->{ns_default_phuri} = $_[0];
434        $self->_ns_load_ph (${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{namespace_phname});
435      }
436      $self->{option}->{ns_default_phuri};
437  }  }
438    
439  =head2 $self->add ($field-name, $field-body, [$name, $body, ...])  =item $hdr->add ($field-name, $field-body, [$name, $body, ...])
440    
441  Adds an new C<field>.  It is not checked whether  Adds some field name/body pairs.  Even if there are
442  the field which named $field_body is already exist or not.  one or more fields named given C<$field-name>,
443  If you don't want duplicated C<field>s, use C<replace> method.  given name/body pairs are ADDed.  Use C<replace>
444    to remove same-name-fields.
445    
446  Instead of field name-body pair, you might pass some options.  Instead of field name-body pair, you might pass some options.
447  Four options are available for this method.  Four options are available for this method.
# Line 403  C<-validate>: Checks whether C<field-nam Line 459  C<-validate>: Checks whether C<field-nam
459    
460  =cut  =cut
461    
462  sub add ($%) {  ## [Name: Value] pair -> internal array item
463    my $self = shift;  ## $self->_value_to_arrayitem ($name => $value, {%options})
464    my %fields = @_;  ## or
465    my %option = %{$self->{option}};  ## $self->_value_to_arrayitem ($name => [$value, %value_options], {%options})
466    $option{parse} = defined wantarray unless defined $option{parse};  ##
467    for (grep {/^-/} keys %fields) {$option{substr ($_, 1)} = $fields{$_}}  ## Return: ((1 = success / 0 = failue), $full_name, $arrayitem)
468    my $body;  sub _value_to_arrayitem ($$$\%) {
469    for (grep {/^[^-]/} keys %fields) {    my $self = shift;
470      my $name = lc $_;  $body = $fields{$_};    my ($name, $value, $option) = @_;
471      $name =~ tr/_/-/ if $option{translate_underscore};    my $value_option = {};
472      Carp::croak "add: $name: invalid field-name"    if (ref $value eq 'ARRAY') {
473        if $option{validate} && $name =~ /$REG{UNSAFE_field_name}/;      ($value, %$value_option) = @$value;
474      $body = $self->_field_body ($body, $name) if $option{parse};    }
475      if ($option{prepend}) {    my $default_ns = $option->{ns_default_phuri};
476        unshift @{$self->{field}}, {name => $name, body => $body};    my $nsuri = $default_ns;
477      } else {    $name =~ s/^$REG{WSP}+//;  $name =~ s/$REG{WSP}+$//;
478        push @{$self->{field}}, {name => $name, body => $body};    
479      no strict 'refs';
480      if ($value_option->{ns}) {
481        $nsuri = $value_option->{ns};
482      } elsif ($option->{ns}) {
483        $nsuri = $option->{ns};
484      } elsif (($default_ns eq $self->{ns}->{uri2phname}->{'x-http'}
485           && $name =~ s/^([0-9]+)-//)
486        || ($name =~ s/^x-http-([0-9]+)-//i)) {     ## Numric namespace prefix, RFC 2774
487        my $prefix = 0+$1;
488        $nsuri = $self->{ns}->{number2uri}->{ $prefix };
489        unless ($nsuri) {
490          $self->{ns}->{number2uri}->{ $prefix } = 'urn:x-suika-fam-cx:msgpm:header:x-temp:'.$prefix;
491          $nsuri = $self->{ns}->{number2uri}->{ $prefix };
492        }
493      } elsif (
494        ${ &_NS_uri2package ($default_ns).'::OPTION' }{use_ph_namespace}
495        && (
496           ($name =~ s/^([Xx]-[A-Za-z0-9]+|[A-Za-z]*[A-WYZa-wyz0-9][A-Za-z0-9]*)-
497                        ([Xx]-[A-Za-z0-9]+|[A-Za-z0-9]*[A-WYZa-wyz0-9][A-Za-z0-9]*)-//x)
498         || $name =~ s/^([Xx]-[A-Za-z0-9]+|[A-Za-z0-9]*[A-WYZa-wyz0-9][A-Za-z0-9]*)-//
499        )) {
500        my ($prefix1, $prefix2) = ($1, $2);
501        my $original_prefix = $&;  my $one_prefix = 0;
502        unless ($prefix2) {
503          $prefix2 = $prefix1;
504          $prefix1 = $self->{ns}->{uri2phname}->{ $default_ns };
505          $one_prefix = 1;
506        }
507        my $prefix
508          = &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} }
509            ($self, &_NS_uri2package ($nsuri), $prefix1.'-'.$prefix2);
510        $self->_ns_load_ph ($prefix);
511        $nsuri = $self->{ns}->{phname2uri}->{ $prefix };
512        unless ($nsuri) {
513          $nsuri = $default_ns;
514          $prefix
515            = &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} }
516              ($self, &_NS_uri2package ($nsuri), $one_prefix? $prefix2: $prefix1);
517          $self->_ns_load_ph ($prefix);
518          $nsuri = $self->{ns}->{phname2uri}->{ $prefix };
519          if ($nsuri) {
520            $name = $prefix2 . '-' . $name unless $one_prefix;
521          } else {
522            $name = $original_prefix . $name;
523            $nsuri = $default_ns;
524          }
525      }      }
526    }    }
527    $body if $option{parse};    $name
528        = &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_name} }
529          ($self, &_NS_uri2package ($nsuri), $name);
530      Carp::croak "$name: invalid field-name"
531        if $option->{field_name_validation}
532          && $name =~ /$REG{ $option->{field_name_unsafe_rule} }/;
533      $value = $self->_parse_value ($name => $value, ns => $nsuri)
534        if $option->{parse} || $option->{parse_all};
535      $option->{parse} = 0;
536      (1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri});
537  }  }
538    *_add_hash_check = \&_value_to_arrayitem;
539    *_replace_hash_check = \&_value_to_arrayitem;
540    
541  =head2 $self->relace ($field_name, $field_body)  =head2 $self->relace ($field_name, $field_body)
542    
# Line 435  first one is used and the others are not Line 548  first one is used and the others are not
548    
549  =cut  =cut
550    
551  sub replace ($%) {  sub _replace_hash_shift ($\%$\%) {
552    my $self = shift;    shift; my $r = shift;  my $n = $_[0]->{name} . ':' . $_[0]->{ns};
553    my %params = @_;    if ($$r{$n}) {
554    my %option = %{$self->{option}};      my $d = $$r{$n};
555    $option{parse} = defined wantarray unless defined $option{parse};      delete $$r{$n};
556    for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}      return $d;
   my (%new_field);  
   for (grep {/^[^-]/} keys %params) {  
     my $name = lc $_;  
     $name =~ tr/_/-/ if $option{translate_underscore};  
     Carp::croak "replace: $name: invalid field-name"  
       if $option{validate} && $name =~ /$REG{UNSAFE_field_name}/;  
     $params{$_} = $self->_field_body ($params{$_}, $name) if $option{parse};  
     $new_field{$name} = $params{$_};  
   }  
   my $body = (%new_field)[-1];  
   for my $field (@{$self->{field}}) {  
     if (defined $new_field{$field->{name}}) {  
       $field->{body} = $new_field {$field->{name}};  
       $new_field{$field->{name}} = undef;  
     }  
   }  
   for (keys %new_field) {  
     push @{$self->{field}}, {name => $_, body => $new_field{$_}};  
557    }    }
558    $body if $option{parse};    undef;
559  }  }
560    
561  =head2 $self->delete ($field-name, [$name, ...])  =head2 $self->delete ($field-name, [$name, ...])
# Line 469  Deletes C<field> named as $field_name. Line 564  Deletes C<field> named as $field_name.
564    
565  =cut  =cut
566    
 sub delete ($@) {  
   my $self = shift;  
   my %delete;  
   for (@_) {$delete{lc $_} = 1}  
   for my $field (@{$self->{field}}) {  
     undef $field if $delete{$field->{name}};  
   }  
 }  
567    
568  =head2 $self->count ([$field_name])  =head2 $self->count ([$field_name])
569    
# Line 486  of fields.  (Same as $#$self+1) Line 573  of fields.  (Same as $#$self+1)
573    
574  =cut  =cut
575    
576  sub count ($;$) {  sub _count_by_name ($$\%) {
577    my $self = shift;    my $self = shift;
578    my ($name) = (lc shift);    my ($array, $option) = @_;
579    unless ($name) {    my $name = $self->_n11n_field_name ($$option{-name});
580      $self->_delete_empty_field ();    my @a = grep {$_->{name} eq $name} @{$self->{$array}};
581      return $#{$self->{field}}+1;    $#a + 1;
582    }  }
583    my $count = 0;  
584    for my $field (@{$self->{field}}) {  ## Delete empty items
585      if ($field->{name} eq $name) {  sub _delete_empty ($) {
586        $count++;    my $self = shift;
587      }    my $array = $self->{option}->{_HASH_NAME};
588    }    $self->{$array} = [grep {ref $_ && length $_->{name}} @{$self->{$array}}];
   $count;  
589  }  }
590    
591  =head2 $self->rename ($field-name, $new-name, [$old, $new,...])  =head2 $self->rename ($field-name, $new-name, [$old, $new,...])
# Line 515  sub rename ($%) { Line 601  sub rename ($%) {
601    for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}    for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
602    my %new_name;    my %new_name;
603    for (grep {/^[^-]/} keys %params) {    for (grep {/^[^-]/} keys %params) {
604      my ($old => $new) = (lc $_ => lc $params{$_});      my ($old => $new)
605          = ($self->_n11n_field_name ($_) => $self->_n11n_field_name ($params{$_}));
606        $old =~ tr/_/-/ if $option{translate_underscore};
607      $new =~ tr/_/-/ if $option{translate_underscore};      $new =~ tr/_/-/ if $option{translate_underscore};
608      Carp::croak "rename: $new: invalid field-name"      Carp::croak "rename: $new: invalid field-name"
609        if $option{validate} && $new =~ /$REG{UNSAFE_field_name}/;        if $option{field_name_validation}
610            && $new =~ /$REG{$option{field_name_unsafe_rule}}/;
611      $new_name{$old} = $new;      $new_name{$old} = $new;
612    }    }
613    for my $field (@{$self->{field}}) {    for my $field (@{$self->{value}}) {
614      if (length $new_name{$field->{name}}) {      if (length $new_name{$field->{name}}) {
615        $field->{name} = $new_name{$field->{name}};        $field->{name} = $new_name{$field->{name}};
616      }      }
# Line 539  for each value. Line 628  for each value.
628    
629  =cut  =cut
630    
631  sub scan ($&) {  sub _scan_sort ($\@\%) {
632    my ($self, $sub) = @_;    my $self = shift;
633      my ($array, $option) = @_;
634    my $sort;    my $sort;
635    $sort = \&_header_cmp if $self->{option}->{sort} eq 'good-practice';    $sort = \&_header_cmp if $option->{field_sort} eq 'good-practice';
636    $sort = {$a cmp $b} if $self->{option}->{sort} eq 'alphabetic';    $sort = {$a cmp $b} if $option->{field_sort} eq 'alphabetic';
637    my @field = @{$self->{field}};    return ( sort $sort @$array ) if ref $sort;
638    if (ref $sort) {    @$array;
639      @field = sort $sort @{$self->{field}};  }
640    }  
641    for my $field (@field) {  sub _n11n_field_name ($$) {
642      next if $field->{name} =~ /^_/;    no strict 'refs';
643      &$sub($field->{name} => $field->{body});    my $self = shift;
644    }    my $s = shift;
645      $s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//;
646      $s = lc $s unless ${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{case_sensible};
647      $s;
648  }  }
649    
650  # Compare function which makes it easy to sort headers in the  # Compare function which makes it easy to sort headers in the
# Line 579  sub stringify ($;%) { Line 672  sub stringify ($;%) {
672    my $self = shift;    my $self = shift;
673    my %params = @_;    my %params = @_;
674    my %option = %{$self->{option}};    my %option = %{$self->{option}};
675      $option{format} = $params{-format} if $params{-format};
676      $self->_init_by_format ($option{format}, \%option)
677        if $self->{option}->{format} ne $option{format};
678    for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}    for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
679      ## Fill required fields
680        my %exist;
681        for ($self->field_name_list) {$exist{$_} = 1}
682        &{ $option{hook_stringify_fill_fields} } ($self, \%exist, \%option);
683    my @ret;    my @ret;
684    if ($option{mail_from}) {    ## RFC 2774 numerical field name prefix
685      my $fromline = $self->field ('mail-from');    my %nprefix;
686      push @ret, 'From '.$fromline if $fromline;    {no strict 'refs';
687    }      %nprefix = reverse %{ $self->{ns}->{number2uri} };
688    $self->scan (sub {      my $i = (sort { $a <=> $b } keys %{ $self->{ns}->{number2uri} })[-1] + 1;
689      my ($name, $body) = (@_);      $i = 10 if $i < 10;
690      return unless length $name;      my $hprefix = ${ &_NS_uri2package
691      return if $option{mail_from} && $name eq 'mail-from';                         ($self->{ns}->{phname2uri}->{'x-http'})
692      return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc');                         .'::OPTION' } {namespace_phname_goodcase};
693      my $fbody;      for my $uri (keys %nprefix) {
694      if (ref $body) {        if ($nprefix{ $uri } < 10) {
695        $fbody = $body->stringify (-format => $option{format});          $nprefix{ $uri } = $i++;
696      } else {        }
697        $fbody = $body;        my $nsfs = $self->item ($uri, -by => 'http-ns-define');
698          for my $i (0..$nsfs->count-1) {
699            my $nsf = ($nsfs->value ($i))[0];
700            if ($nsf->value eq $uri) {
701              $nsf->replace (ns => $nprefix{ $uri });
702              $nprefix{ $uri } = $hprefix . '-' . $nprefix{ $uri };
703              last;
704            }
705          }
706      }      }
707      return unless length $fbody;    }
708      $fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g;    my $_stringify = sub {
709      $fbody =~ s/\x0A(?=[^\x09\x20])/\x0A\x20/g;      no strict 'refs';
710      $name =~ s/((?:^|-)[a-z])/uc($1)/ge if $option{capitalize};        my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns});
711      push @ret, $name.': '.$self->fold ($fbody);        return unless length $name;
712    });        return if $option{output_mail_from} && $name eq 'mail-from';
713    my $ret = join ("\n", @ret);        $body = '' if !$option{output_bcc} && $name eq 'bcc';
714    $ret? $ret."\n": '';        my $nspackage = &_NS_uri2package ($nsuri);
715          my $oname;        ## Outputed field-name
716          my $prefix = $nprefix{ $nsuri }
717                    || ${$nspackage.'::OPTION'} {namespace_phname_goodcase}
718                    || $self->{ns}->{uri2phname}->{ $nsuri };
719          my $default_prefix = ${ &_NS_uri2package ($option{ns_default_phuri})
720                                  .'::OPTION'} {namespace_phname_goodcase};
721          $prefix = '' if $prefix eq $default_prefix;
722          $prefix =~ s/^\Q$default_prefix\E-//;
723          my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase};
724          if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) }
725          else { $oname = $name }
726          if ($prefix) { $oname = $prefix . '-' . $oname }
727          if ($option{format} =~ /uri-url-mailto/) {
728            return if (( ${$nspackage.'::OPTION'} {uri_mailto_safe}->{$name}
729                      || ${$nspackage.'::OPTION'} {uri_mailto_safe}->{':default'})
730                      < $option{uri_mailto_safe_level});
731            if ($name eq 'to') {
732              $body = $self->field ('to', -new_item_unless_exist => 0);
733              if (ref $body && $body->have_group) {
734                #
735              } elsif (ref $body && $body->count > 1) {
736                $body = $body->clone;
737                $body->delete ({-by => 'index'}, 0);
738              }
739            }
740          }
741          my $fbody;
742          if (ref $body) {
743            $fbody = $body->stringify (-format => $option{format});
744          } else {
745            $fbody = $body;
746          }
747          unless (${$nspackage.'::OPTION'} {field}->{$name}->{empty_body}) {
748            return unless length $fbody;
749          }
750          unless ($option{linebreak_strict}) {
751            ## bare \x0D and bare \x0A are unsafe
752            $fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g;
753            $fbody =~ s/\x0A(?=[^\x09\x20])/\x0A\x20/g;
754          } else {
755            $fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g;
756          }
757          if ($option{use_folding}) {
758            if (ref $option{output_folding}) {
759              $fbody = &{$option{output_folding}} ($self, $fbody,
760                -initial_length => length ($oname) +2);
761            } elsif ($option{output_folding}) {
762              $fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2);
763            }
764          }
765          push @ret, sprintf $option{field_format_pattern}, $oname, $fbody;
766        };
767      if ($option{format} =~ /uri-url-mailto/) {
768        if ($option{format} =~ /uri-url-mailto-to/) {
769          my $to = $self->field ('to', -new_item_unless_exist => 0);
770          if ($to) {
771            unless ($to->have_group) {
772              my $fbody = $to->stringify (-format => $option{format}, -max => 1);
773              return &{$option{output_folding}} ($self, $fbody);
774            }
775          }
776          '';
777        } elsif ($option{format} =~ /uri-url-mailto-rfc1738/) {
778          my $to = $self->field ('to', -new_item_unless_exist => 0);
779          if ($to) {
780            my $fbody = $to->addr_spec (-format => $option{format});
781            return &{$option{output_folding}} ($self, $fbody);
782          }
783          '';
784        } else {
785          $self->scan ($_stringify);
786          my $ret = join ('&', @ret);
787          $ret;
788        }
789      } else {
790        if ($option{output_mail_from}) {
791          my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0);
792          push @ret, 'From '.$fromline if $fromline;
793        }
794        $self->scan ($_stringify);
795        my $ret = join ("\x0D\x0A", @ret);
796        $ret? $ret."\x0D\x0A": '';
797      }
798  }  }
799  *as_string = \&stringify;  *as_string = \&stringify;
800    
# Line 619  sub option ($@) { Line 810  sub option ($@) {
810      return $self->{option}->{ shift (@_) };      return $self->{option}->{ shift (@_) };
811    }    }
812    while (my ($name, $value) = splice (@_, 0, 2)) {    while (my ($name, $value) = splice (@_, 0, 2)) {
     $name =~ s/^-//;  
813      $self->{option}->{$name} = $value;      $self->{option}->{$name} = $value;
814      if ($name eq 'format') {      if ($name eq 'format') {
815        for my $f (@{$self->{field}}) {        for my $f (@{$self->{field}}) {
# Line 631  sub option ($@) { Line 821  sub option ($@) {
821    }    }
822  }  }
823    
824  sub field_type ($$;$) {  sub field_type ($@) {shift->SUPER::value_type (@_)}
825    
826    ## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) )
827    sub _fold ($$;%) {
828    my $self = shift;    my $self = shift;
829    my $field_name = shift;    my $string = shift;
830    my $new_field_type = shift;    my %option = @_;
831    if ($new_field_type) {    my $max = $self->{option}->{line_length_max};
832      $self->{option}->{field_type}->{$field_name} = $new_field_type;    $max = 20 if $max < 20;
833    }    
834    $self->{option}->{field_type}->{$field_name}    my $l = $option{-initial_length} || 0;
835    || $self->{option}->{field_type}->{':DEFAULT'};    $l += length $1 if $string =~ /^([^\x09\x20]+)/;
836      $string =~ s{([\x09\x20][^\x09\x20]+)}{
837        my $s = $1;
838        if (($l + length $s) > $max) {
839          $s = "\x0D\x0A\x20" . $s;
840          $l = 1 + length $s;
841        } else { $l += length $s }
842        $s;
843      }gex;
844      $string;
845  }  }
846    
847  sub _delete_empty_field ($) {  sub _ns_load_ph ($$) {
848    my $self = shift;    my $self = shift;
849    my @ret;    my $name = shift;     ## normalized prefix (without HYPHEN-MINUS)
850    for my $field (@{$self->{field}}) {    return if $self->{ns}->{phname2uri}->{$name};
851      push @ret, $field if $field->{name};    $self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name};
852    }    return unless $self->{ns}->{phname2uri}->{$name};
853    $self->{field} = \@ret;    $self->{ns}->{uri2phname}->{$self->{ns}->{phname2uri}->{$name}} = $name;
854    $self;  }
855    
856    sub _ns_associate_numerical_prefix ($) {
857      my $self = shift;
858      $self->scan (sub {shift;
859        my $f = shift;  return unless $f->{name};
860        if ($f->{ns} eq $self->{ns}->{phname2uri}->{'x-http'}
861         || $f->{ns} eq $self->{ns}->{phname2uri}->{'x-http-c'}) {
862          my $fn = $f->{name};
863          if ($fn eq 'opt' || $fn eq 'man') {
864            $f->{body} = $self->_parse_value ($fn => $f->{body}, ns => $f->{ns});
865            for ($f->{body}->value (0..$f->{body}->count-1)) {
866              my ($nsuri, $number) = ($_->value, $_->item ('ns'));
867              if ($number && $nsuri) {
868                $self->{ns}->{number2uri}->{ $number } = $nsuri;
869              }
870            }
871          }
872        }
873      });
874      $self->scan (sub {shift;
875        my $f = shift;
876        if ($f->{ns} =~ /urn:x-suika-fam-cx:msgpm:header:x-temp:([0-9]+)$/ && $self->{ns}->{number2uri}->{ $1 }) {
877          $f->{ns} = $self->{ns}->{number2uri}->{ $1 };
878        }
879      });
880  }  }
881    
882  sub fold ($$;$) {  ## $package_name = Message::Header::_NS_uri2phpackage ($nsuri)
883    my $self = shift;  ## (For internal use of Message::* modules)
884    my $string = shift;  sub _NS_uri2phpackage ($) {
885    my $len = shift || $self->{option}->{fold_length};    $NS_uri2phpackage{$_[0]}
886    $len = 60 if $len < 60;    || $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}};
887      }
888    ## This code is taken from Mail::Header 1.43 in MailTools,  sub _NS_uri2package ($) {
889    ## by Graham Barr (Maintained by Mark Overmeer <mailtools@overmeer.net>).    $NS_uri2package{$_[0]}
890    my $max = int($len - 5);         # 4 for leading spcs + 1 for [\,\;]    || $NS_uri2phpackage{$_[0]}
891    my $min = int($len * 4 / 5) - 4;    || $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}};
   my $ml = $len;  
     
   if (length($string) > $ml) {  
      #Split the line up  
      # first bias towards splitting at a , or a ; >4/5 along the line  
      # next split a whitespace  
      # else we are looking at a single word and probably don't want to split  
      my $x = "";  
      $x .= "$1\n "  
        while($string =~ s/^$REG{WSP}*(  
                           [^"]{$min,$max}?[\,\;]  
                           |[^"]{1,$max}$REG{WSP}  
                           |[^\s"]*(?:"[^"]*"[^\s"]*)+$REG{WSP}  
                           |[^\s"]+$REG{WSP}  
                           )  
                         //x);  
      $x .= $string;  
      $string = $x;  
      $string =~ s/(\A$REG{WSP}+|$REG{WSP}+\Z)//sog;  
      $string =~ s/\s+\n/\n/sog;  
   }  
   $string;  
892  }  }
893    
894  =head2 $self->clone ()  =head2 $self->clone ()
# Line 692  Returns a copy of Message::Header object Line 897  Returns a copy of Message::Header object
897    
898  =cut  =cut
899    
900  sub clone ($) {  ## Inhreited
   my $self = shift;  
   my $clone = new Message::Header;  
   for my $name (%{$self->{option}}) {  
     if (ref $self->{option}->{$name} eq 'HASH') {  
       $clone->{option}->{$name} = {%{$self->{option}->{$name}}};  
     } elsif (ref $self->{option}->{$name} eq 'ARRAY') {  
       $clone->{option}->{$name} = [@{$self->{option}->{$name}}];  
     } else {  
       $clone->{option}->{$name} = $self->{option}->{$name};  
     }  
   }  
   for (@{$self->{field}}) {  
     $clone->add ($_->{name}, scalar $_->{body});  
   }  
   $clone;  
 }  
901    
902  =head1 NOTE  =head1 NOTE
903    

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.38

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24