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

Contents of /messaging/bunshin/Bunshin.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download)
Wed Jul 24 12:12:34 2002 UTC (21 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +42 -13 lines
2002-07-24  Wakaba <w@suika.fam.cx>

	* Bunshin.pm:
	- (_get_resource): New.
	- (set_source): Moved main process to _get_resource.
	- (_get_resource, set_source): Set protocol's meta information
	to $self->{meta_info}.
	- (meta_information): New method.

1
2 =head1 NAME
3
4 Bunshin --- A shimbun implemrntion written in Perl
5
6 =cut
7
8 package Bunshin;
9 use strict;
10 use vars qw($DEBUG $MYNAME $VERSION);
11 $VERSION=do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
12 $MYNAME = 'Bunshin';
13 $DEBUG = 0;
14 use FileHandle;
15 require Message::Entity;
16 require Message::Util;
17 require Message::Field::Date;
18
19 sub new ($;%) {
20 my $class = shift;
21 my $self = bless {}, $class;
22 $self->{fmt2str} = Message::Util::make_clone
23 ($Message::Field::Date::DEFAULT{-fmt2str});
24 $self;
25 }
26
27 =item $b->msg_regex ($regex)
28
29 Set regex used to cut a message.
30
31 =cut
32
33 sub set_regex ($$$) {
34 my $self = shift;
35 my $name = shift;
36 my $regex = shift;
37 $regex =~ s/\x20/\\x20/g;
38 $regex =~ s/\x09/\\x09/g;
39 $regex =~ s/\x0D(?!\x0A)/\x0D\x0A/g;
40 $regex =~ s/(?<!\x0D)\x0A/\x0D\x0A/g;
41 $regex =~ s/\x0D/\\x0D/g;
42 $regex =~ s/\x0A/\\x0A/g;
43 $regex =~ s/\x23/\\x23/g;
44 $self->{'regex_'.$name} = $regex;
45 }
46
47 sub set_hook_function ($$\&) {
48 my $self = shift;
49 my $name = shift;
50 my $function = shift;
51 $self->{'hook_'.$name} = $function;
52 }
53
54 sub set_format ($$\&) {
55 my $self = shift;
56 my $name = shift;
57 my $function = shift;
58 $self->{fmt2str}->{$name} = $function;
59 }
60
61 sub set_elements ($$@) {
62 my $self = shift;
63 my $name = shift;
64 $self->{'elements_'.$name} = \@_;
65 }
66
67 sub set_source ($%) {
68 my $self = shift;
69 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}) {
87 $resource = $option{value};
88 } elsif ($option{uri}) {
89 require Message::Field::UA;
90 require LWP::UserAgent;
91 my $ua = Message::Field::UA->new;
92 $ua->add_our_name;
93 $ua->add ('libwww-perl' => $LWP::VERSION);
94 my $lwp = LWP::UserAgent->new;
95 $lwp->agent ($ua->stringify);
96 my $req = HTTP::Request->new (GET => $option{uri});
97 my $res = $lwp->request ($req);
98 $resource = $res->content;
99 $meta = parse Message::Header $res->headers_as_string,
100 -parse_all => 0, -format => 'http-response',
101 ;
102 } elsif ($option{file}) {
103 my $f = new FileHandle $option{file} => 'r';
104 Carp::croak "set_source: $option{file}: $!" unless defined $f;
105 my $c = $self->{hook_code_conversion} || \&_code_conversion;
106 local $/ = undef;
107 $resource = $f->getline;
108 } else {
109 Carp::croak "_get_resource: $_[0]: Unsupported data source type";
110 }
111 ($resource, $meta);
112 }
113
114 ## $self->_code_conversion ($string, \%option, $meta_info)
115 sub _code_conversion ($$\%$) {
116 $_[1];
117 }
118
119 sub make_msgs ($) {
120 my $self = shift;
121 my $s = $self->{source};
122 my $f = $self->{hook_make_msg} || \&_make_a_msg;
123 my @msg;
124 my %param = %{$self->{default_parameter}};
125 if ($self->{regex_metainfo} && ref $self->{elements_metainfo}) {
126 $s =~ s{ $self->{regex_metainfo} }{
127 no strict 'refs';
128 for my $i (0..$#{$self->{elements_metainfo}}) {
129 $param{$self->{elements_metainfo}->[$i]} = ${$i+1};
130 }
131 $&;
132 }esx;
133 }
134 $s =~ s{ $self->{regex_message} }{
135 no strict 'refs';
136 my %p = %param;
137 for my $i (0..$#{$self->{elements_message}}) {
138 $p{$self->{elements_message}->[$i]} = ${$i+1};
139 }
140 my $msg = &$f ($self, %p);
141 push @msg, $msg;
142 }gesx;
143 @msg;
144 }
145
146 ## Default function for "make_msg"
147 sub _make_a_msg ($@) {
148 my $self = shift;
149 my %p = @_;
150 my $msg = new Message::Entity
151 -fill_date => 0,
152 -fill_msgid => 0,
153 -fill_ua_name => 'x-shimbun-agent',
154 -parse_all => 1,
155 ;
156 my $hdr = $msg->header;
157 ## Originator and date
158 my $from = $hdr->field ('from')->add ($p{from_mail} || 'foo@bar.invalid');
159 $from->display_name ($p{from_name}) if length $p{from_name};
160 my $date = $hdr->field ('date');
161 $p{date_year} ||= (gmtime)[5];
162 $date->set_datetime (@p{qw/date_year date_month
163 date_day date_hour date_minute date_second/},
164 zone => $p{date_zone});
165 $hdr->add (x_uri => $p{from_uri}) if $p{from_uri};
166 if ($p{from_face}) {
167 $msg->header->field ('x-face')->value ($p{from_face});
168 } elsif ($p{faces}->{$p{from_mail}}) {
169 $msg->header->field ('x-face')->value ($p{faces}->{$p{from_mail}});
170 } elsif ($p{list_face}) {
171 $msg->header->field ('x-face')->value ($p{list_face});
172 }
173 ## Message attribute
174 if (length $p{msg_id}) {
175 $hdr->add ('message-id' => $p{msg_id});
176 } elsif ($p{msg_id_from} || $p{msg_id_right} || $p{list_id}) {
177 my $c = $p{msg_count};
178 $c = '.d'.(0+$date) unless defined $p{msg_count};
179 my $mid;
180 if ($p{msg_id_from}) {
181 $mid = sprintf '<msg%s.BS%%%s%%%s>', $c, $p{list_id}, $p{msg_id_from};
182 } elsif ($p{msg_id_right}) {
183 $mid = sprintf '<msg%s.BS%%%s%%list@%s>', $c, $p{list_id}, $p{msg_id_right};
184 } else { #if ($p{list_id}) {
185 $mid = sprintf '<msg%s.BS%%list@%s>', $c, $p{list_id};
186 }
187 $hdr->add (($DEBUG?'x-':'').'message-id' => $mid);
188 }
189 if (length $p{subject}) {
190 $hdr->add (subject => $p{subject});
191 } elsif (length $p{DEFAULT_subject}) {
192 $hdr->add (subject => $p{DEFAULT_subject});
193 }
194 my $a;
195 for (grep {/^misc_/} keys %p) {
196 $a = $hdr->field ('content-x-properties') unless ref $a;
197 my $name = substr ($_, 5);
198 $name =~ tr/_/-/;
199 if ($p{base_uri} && /uri/ && length $p{$_}) {
200 require URI::WithBase;
201 $a->add ($name => URI::WithBase->new ($p{$_}, $p{base_uri})->abs);
202 } else {
203 $a->add ($name => $p{$_}) if length $p{$_};
204 }
205 }
206 ## Body and body information
207 my $b = $self->{hook_msg_body} || \&_make_a_msg_body;
208 &$b ($self, $msg, $p{body}, \%p);
209 ## List information
210 if (length $p{list_id}) {
211 my $lid = $hdr->field ('list-id');
212 $lid->value ($p{list_id});
213 $lid->display_name ($p{list_name}) if length $p{list_name};
214 }
215 $hdr->add (x_mail_count => $p{msg_count}) if defined $p{msg_count};
216 $hdr->add (x_ml_info => $p{list_info}) if defined $p{list_info};
217 if ($p{base_uri}) {
218 my $uri = $hdr->add (x_uri => '');
219 $uri->value ($p{base_uri});
220 $uri->display_name ($p{list_name}) if length $p{list_name};
221 }
222 if ($p{urn_template}) {
223 my $urn = $self->Message::Field::Date::_date2str ({
224 format_template => $p{urn_template},
225 date_time => $date->unix_time,
226 zone => $date->zone,
227 fmt2str => $self->{fmt2str},
228 }, \%p);
229 $hdr->add ('x-uri')->value ($urn);
230 }
231 ## Additional information
232 my $u = $self->{hook_msg_header_add};
233 &$u ($self, $msg, \%p) if ref $u;
234 $hdr->field ('x-shimbun-agent')->add ($MYNAME => $VERSION);
235 $msg;
236 }
237
238 sub _make_a_msg_body ($$$\%) {
239 my $self = shift;
240 my ($msg, $body, $param) = @_;
241 if (length $body) {
242 $body =~ s/(?<!\x0D\x0A)\z/\x0D\x0A/s;
243 $msg->body ($body);
244 }
245 }
246
247 sub default_parameter ($@) {
248 my $self = shift;
249 if (@_ == 1) {
250 return $self->{default_parameter}->{ $_[0] };
251 }
252 while (my ($name, $value) = splice (@_, 0, 2)) {
253 $self->{default_parameter}->{$name} = $value;
254 }
255 $self;
256 }
257
258 =head1 $meta = $b->meta_information
259
260 Gets meta information from resource requesting protocol.
261 Usually returned value is Message::Header object.
262 When resource is getten via HTTP, its content is HTTP response
263 header.
264
265 =cut
266
267 sub meta_information ($) {
268 my $self = shift;
269 $self->{meta_info};
270 }
271
272 =head1 LICENSE
273
274 Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
275
276 This program is free software; you can redistribute it and/or modify
277 it under the terms of the GNU General Public License as published by
278 the Free Software Foundation; either version 2 of the License, or
279 (at your option) any later version.
280
281 This program is distributed in the hope that it will be useful,
282 but WITHOUT ANY WARRANTY; without even the implied warranty of
283 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
284 GNU General Public License for more details.
285
286 You should have received a copy of the GNU General Public License
287 along with this program; see the file COPYING. If not, write to
288 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
289 Boston, MA 02111-1307, USA.
290
291 =head1 CHANGE
292
293 See F<ChangeLog>.
294 $Date: 2002/06/20 11:36:32 $
295
296 =cut
297
298 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24