/[suikacvs]/messaging/bunshin/Bunshin.pm
Suika

Diff of /messaging/bunshin/Bunshin.pm

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

revision 1.1 by wakaba, Sun Jun 16 02:50:54 2002 UTC revision 1.5 by wakaba, Thu Aug 29 12:10:59 2002 UTC
# Line 1  Line 1 
1    
2  =head1 NAME  =head1 NAME
3    
4  Bunshin  Bunshin --- A shimbun implemrntion written in Perl
5    
6  =cut  =cut
7    
# Line 11  use vars qw($DEBUG $MYNAME $VERSION); Line 11  use vars qw($DEBUG $MYNAME $VERSION);
11  $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
12  $MYNAME = 'Bunshin';  $MYNAME = 'Bunshin';
13  $DEBUG = 0;  $DEBUG = 0;
 use Time::Local;  
14  use FileHandle;  use FileHandle;
15  require Message::Entity;  require Message::Entity;
16  require Message::Util;  require Message::Util;
# Line 21  sub new ($;%) { Line 20  sub new ($;%) {
20    my $class = shift;    my $class = shift;
21    my $self = bless {}, $class;    my $self = bless {}, $class;
22    $self->{fmt2str} = Message::Util::make_clone    $self->{fmt2str} = Message::Util::make_clone
23      ($Message::Field::Date::DEFAULT{-fmt2str});      (\%Message::Field::Date::FMT2STR);
24    $self;    $self;
25  }  }
26    
# Line 68  sub set_elements ($$@) { Line 67  sub set_elements ($$@) {
67  sub set_source ($%) {  sub set_source ($%) {
68    my $self = shift;    my $self = shift;
69    my %option = @_;    my %option = @_;
70      ($self->{source}, $self->{meta_info}) = $self->_get_resource (%option);
71      $self->default_parameter (base_uri => $option{uri}) if $option{uri};
72            ## BUG: Doesn't support redirection
73      if ($option{uri} || $option{file}) {
74        my $c = $self->{hook_code_conversion} || \&_code_conversion;
75        $self->{source} = &$c ($self, $self->{source}, \%option, $self->{meta_info});
76      }
77      $self->{source} =~ s/\x0D(?!\x0A)/\x0D\x0A/g;
78      $self->{source} =~ s/(?<!\x0D)\x0A/\x0D\x0A/g;
79      $self;
80    }
81    
82    sub _get_resource ($%) {
83      my $self = shift;
84      my %option = @_;
85      my ($resource, $meta);
86    if (defined $option{value}) {    if (defined $option{value}) {
87      $self->{source} = $option{value};      $resource = $option{value};
88    } elsif ($option{uri}) {    } elsif ($option{uri}) {
89      require Message::Field::UA;      require Message::Field::UA;
90      require LWP::UserAgent;      require LWP::UserAgent;
# Line 80  sub set_source ($%) { Line 95  sub set_source ($%) {
95      $lwp->agent ($ua->stringify);      $lwp->agent ($ua->stringify);
96      my $req = HTTP::Request->new (GET => $option{uri});      my $req = HTTP::Request->new (GET => $option{uri});
97      my $res = $lwp->request ($req);      my $res = $lwp->request ($req);
98      my $c = $self->{hook_code_conversion} || \&_code_conversion;      $resource = $res->content;
99      $self->{source} = &$c ($self, $res->content, \%option);      $meta = parse Message::Header $res->headers_as_string,
100      $self->default_parameter (base_uri => $option{uri});        -parse_all => 0, -format => 'http-response',
101        ;
102    } elsif ($option{file}) {    } elsif ($option{file}) {
103      my $f = new FileHandle $option{file} => 'r';      my $f = new FileHandle $option{file} => 'r';
104      Carp::croak "set_source: $option{file}: $!" unless defined $f;      Carp::croak "set_source: $option{file}: $!" unless defined $f;
105      my $c = $self->{hook_code_conversion} || \&_code_conversion;      my $c = $self->{hook_code_conversion} || \&_code_conversion;
106      local $/ = undef;      local $/ = undef;
107      $self->{source} = &$c ($self, $f->getline, \%option);      $resource = $f->getline;
     close SRC;  
108    } else {    } else {
109      Carp::croak "set_source: $_[0]: Unsupported data source type";      Carp::croak "_get_resource: $_[0]: Unsupported data source type";
110    }    }
111    $self->{source} =~ s/\x0D(?!\x0A)/\x0D\x0A/g;    ($resource, $meta);
   $self->{source} =~ s/(?<!\x0D)\x0A/\x0D\x0A/g;  
   $self;  
112  }  }
113    
114  ## $self->_code_conversion ($string, \%option)  ## $self->_code_conversion ($string, \%option, $meta_info)
115  sub _code_conversion ($$\%) {  sub _code_conversion ($$\%$) {
116    $_[1];    $_[1];
117  }  }
118    
# Line 138  sub _make_a_msg ($@) { Line 151  sub _make_a_msg ($@) {
151      -fill_date  => 0,      -fill_date  => 0,
152      -fill_msgid => 0,      -fill_msgid => 0,
153      -fill_ua_name       => 'x-shimbun-agent',      -fill_ua_name       => 'x-shimbun-agent',
154        -format     => 'news-usefor',
155      -parse_all  => 1,      -parse_all  => 1,
156    ;    ;
157    my $hdr = $msg->header;    my $hdr = $msg->header;
158    ## Originator and date    ## Originator and date
159      my $from = $hdr->field ('from')->add ($p{from_mail});      my $from = $hdr->field ('from')->add ($p{from_mail} || 'foo@bar.invalid');
160      $from->display_name ($p{from_name}) if length $p{from_name};      $from->display_name ($p{from_name}) if length $p{from_name};
161      my $date = $hdr->field ('date');      my $date = $hdr->field ('date');
162      $p{date_year} ||= (gmtime)[5];      $p{date_year} ||= (gmtime)[5];
163      $date->set_datetime (@p{qw/date_year date_month      $date->set_datetime (@p{qw/date_year date_month
164        date_day date_hour date_minute date_second/},        date_day date_hour date_minute date_second/},
165        zone => $p{date_zone});        zone => $p{date_zone});
166      $hdr->add ('x-uri' => $p{from_uri}) if $p{from_uri};      $hdr->add (x_uri => $p{from_uri}) if $p{from_uri};
167      if ($p{from_face}) {      if ($p{from_face}) {
168        $msg->header->field ('x-face')->value ($p{from_face});        $msg->header->field ('x-face')->value ($p{from_face});
169      } elsif ($p{faces}->{$p{from_mail}}) {      } elsif ($p{faces}->{$p{from_mail}}) {
# Line 160  sub _make_a_msg ($@) { Line 174  sub _make_a_msg ($@) {
174    ## Message attribute    ## Message attribute
175      if (length $p{msg_id}) {      if (length $p{msg_id}) {
176        $hdr->add ('message-id' => $p{msg_id});        $hdr->add ('message-id' => $p{msg_id});
177      } else {      } elsif ($p{msg_id_from} || $p{msg_id_right} || $p{list_id}) {
       my $id_right = $p{msg_id_right} || $p{list_id};  
178        my $c = $p{msg_count};        my $c = $p{msg_count};
179        $c = '.d'.(0+$date) unless defined $p{msg_count};        $c = '.d'.(0+$date) unless defined $p{msg_count};
180        my $mid = sprintf '<msg%s.BS%%list@%s>', $c, $id_right;        my $mid;
181        $mid = sprintf '<msg%s.BS%%%s>', $c, $p{msg_id_from} if $p{msg_id_from};        if ($p{msg_id_from}) {
182            $mid = sprintf '<msg%s.BS%%%s%%%s>', $c, $p{list_id}, $p{msg_id_from};
183          } elsif ($p{msg_id_right}) {
184            $mid = sprintf '<msg%s.BS%%%s%%list@%s>', $c, $p{list_id}, $p{msg_id_right};
185          } else { #if ($p{list_id}) {
186            $mid = sprintf '<msg%s.BS%%list@%s>', $c, $p{list_id};
187          }
188        $hdr->add (($DEBUG?'x-':'').'message-id' => $mid);        $hdr->add (($DEBUG?'x-':'').'message-id' => $mid);
189      }      }
190      $hdr->add (subject => $p{subject}) if length $p{subject};      if (length $p{subject}) {
191          $hdr->add (subject => $p{subject});
192        } elsif (length $p{DEFAULT_subject}) {
193          $hdr->add (subject => $p{DEFAULT_subject});
194        }
195      my $a;      my $a;
196      for (grep {/^misc_/} keys %p) {      for (grep {/^misc_/} keys %p) {
197        $a = $hdr->field ('content-x-properties') unless ref $a;        $a = $hdr->field ('content-x-properties') unless ref $a;
# Line 176  sub _make_a_msg ($@) { Line 199  sub _make_a_msg ($@) {
199        $name =~ tr/_/-/;        $name =~ tr/_/-/;
200        if ($p{base_uri} && /uri/ && length $p{$_}) {        if ($p{base_uri} && /uri/ && length $p{$_}) {
201          require URI::WithBase;          require URI::WithBase;
202          $a->add ($name => URI::WithBase->new ($_, $p{base_uri})->abs);          $a->add ($name => URI::WithBase->new ($p{$_}, $p{base_uri})->abs);
203          } elsif (/color/) {
204            $p{$_} = '#'.$p{$_} if $p{$_} =~ /^[A-Za-z0-9]{6}$/;
205            $a->add ($name => $p{$_}) if length $p{$_};
206        } else {        } else {
207          $a->add ($name => $p{$_}) if length $p{$_};          $a->add ($name => $p{$_}) if length $p{$_};
208        }        }
# Line 190  sub _make_a_msg ($@) { Line 216  sub _make_a_msg ($@) {
216        $lid->value ($p{list_id});        $lid->value ($p{list_id});
217        $lid->display_name ($p{list_name}) if length $p{list_name};        $lid->display_name ($p{list_name}) if length $p{list_name};
218      }      }
219      $hdr->add ('x-mail-count' => $p{msg_count}) if defined $p{msg_count};      $hdr->add (x_mail_count => $p{msg_count}) if defined $p{msg_count};
220      $hdr->add ('x-ml-info' => $p{list_info}) if defined $p{list_info};      $hdr->add (x_ml_info => $p{list_info}) if defined $p{list_info};
221      if ($p{base_uri}) {      if ($p{base_uri}) {
222        my $uri = $hdr->add (x_uri => '');        my $uri = $hdr->add (x_uri => '');
223        $uri->value ($p{base_uri});        $uri->value ($p{base_uri});
224        $uri->display_name ($p{list_name}) if length $p{list_name};        $uri->display_name ($p{list_name}) if length $p{list_name};
225      }      }
226      if ($p{urn_template}) {      if ($p{urn_template}) {
227        my $urn = $self->Message::Field::Date::_date2str ({        my $urn = $date->stringify (
228          format_template => $p{urn_template},          -format_macros  => $self->{fmt2str},
229          date_time       => $date->unix_time,          -format_template        => $p{urn_template},
230          zone    => $date->zone,          -format_parameters      => \%p,
231          fmt2str => $self->{fmt2str},        );
232        });        #my $urn = $self->Message::Field::Date::_date2str ({
233          #  format_template        => $p{urn_template},
234          #  date_time      => $date->unix_time,
235          #  zone   => $date->zone,
236          #  fmt2str        => $self->{fmt2str},
237          #}, \%p);
238        $hdr->add ('x-uri')->value ($urn);        $hdr->add ('x-uri')->value ($urn);
239      }      }
240    ## Additional information    ## Additional information
# Line 233  sub default_parameter ($@) { Line 264  sub default_parameter ($@) {
264    $self;    $self;
265  }  }
266    
267    =head1 $meta = $b->meta_information
268    
269    Gets meta information from resource requesting protocol.
270    Usually returned value is Message::Header object.
271    When resource is getten via HTTP, its content is HTTP response
272    header.
273    
274    =cut
275    
276    sub meta_information ($) {
277      my $self = shift;
278      $self->{meta_info};
279    }
280    
281  =head1 LICENSE  =head1 LICENSE
282    
283  Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.  Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.5

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24