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

Contents of /messaging/bunshin/Bunshin.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download)
Sun Jun 16 10:46:29 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +3 -5 lines
2002-06-16  wakaba <w@suika.fam.cx>

	* Bunshin.pm: New module.
	* ChangeLog: New file.

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.1 $=~/\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 if (defined $option{value}) {
71 $self->{source} = $option{value};
72 } elsif ($option{uri}) {
73 require Message::Field::UA;
74 require LWP::UserAgent;
75 my $ua = Message::Field::UA->new;
76 $ua->add_our_name;
77 $ua->add ('libwww-perl' => $LWP::VERSION);
78 my $lwp = LWP::UserAgent->new;
79 $lwp->agent ($ua->stringify);
80 my $req = HTTP::Request->new (GET => $option{uri});
81 my $res = $lwp->request ($req);
82 my $c = $self->{hook_code_conversion} || \&_code_conversion;
83 $self->{source} = &$c ($self, $res->content, \%option);
84 $self->default_parameter (base_uri => $option{uri});
85 } elsif ($option{file}) {
86 my $f = new FileHandle $option{file} => 'r';
87 Carp::croak "set_source: $option{file}: $!" unless defined $f;
88 my $c = $self->{hook_code_conversion} || \&_code_conversion;
89 local $/ = undef;
90 $self->{source} = &$c ($self, $f->getline, \%option);
91 } else {
92 Carp::croak "set_source: $_[0]: Unsupported data source type";
93 }
94 $self->{source} =~ s/\x0D(?!\x0A)/\x0D\x0A/g;
95 $self->{source} =~ s/(?<!\x0D)\x0A/\x0D\x0A/g;
96 $self;
97 }
98
99 ## $self->_code_conversion ($string, \%option)
100 sub _code_conversion ($$\%) {
101 $_[1];
102 }
103
104 sub make_msgs ($) {
105 my $self = shift;
106 my $s = $self->{source};
107 my $f = $self->{hook_make_msg} || \&_make_a_msg;
108 my @msg;
109 my %param = %{$self->{default_parameter}};
110 if ($self->{regex_metainfo} && ref $self->{elements_metainfo}) {
111 $s =~ s{ $self->{regex_metainfo} }{
112 no strict 'refs';
113 for my $i (0..$#{$self->{elements_metainfo}}) {
114 $param{$self->{elements_metainfo}->[$i]} = ${$i+1};
115 }
116 $&;
117 }esx;
118 }
119 $s =~ s{ $self->{regex_message} }{
120 no strict 'refs';
121 my %p = %param;
122 for my $i (0..$#{$self->{elements_message}}) {
123 $p{$self->{elements_message}->[$i]} = ${$i+1};
124 }
125 my $msg = &$f ($self, %p);
126 push @msg, $msg;
127 }gesx;
128 @msg;
129 }
130
131 ## Default function for "make_msg"
132 sub _make_a_msg ($@) {
133 my $self = shift;
134 my %p = @_;
135 my $msg = new Message::Entity
136 -fill_date => 0,
137 -fill_msgid => 0,
138 -fill_ua_name => 'x-shimbun-agent',
139 -parse_all => 1,
140 ;
141 my $hdr = $msg->header;
142 ## Originator and date
143 my $from = $hdr->field ('from')->add ($p{from_mail});
144 $from->display_name ($p{from_name}) if length $p{from_name};
145 my $date = $hdr->field ('date');
146 $p{date_year} ||= (gmtime)[5];
147 $date->set_datetime (@p{qw/date_year date_month
148 date_day date_hour date_minute date_second/},
149 zone => $p{date_zone});
150 $hdr->add ('x-uri' => $p{from_uri}) if $p{from_uri};
151 if ($p{from_face}) {
152 $msg->header->field ('x-face')->value ($p{from_face});
153 } elsif ($p{faces}->{$p{from_mail}}) {
154 $msg->header->field ('x-face')->value ($p{faces}->{$p{from_mail}});
155 } elsif ($p{list_face}) {
156 $msg->header->field ('x-face')->value ($p{list_face});
157 }
158 ## Message attribute
159 if (length $p{msg_id}) {
160 $hdr->add ('message-id' => $p{msg_id});
161 } else {
162 my $id_right = $p{msg_id_right} || $p{list_id};
163 my $c = $p{msg_count};
164 $c = '.d'.(0+$date) unless defined $p{msg_count};
165 my $mid = sprintf '<msg%s.BS%%list@%s>', $c, $id_right;
166 $mid = sprintf '<msg%s.BS%%%s>', $c, $p{msg_id_from} if $p{msg_id_from};
167 $hdr->add (($DEBUG?'x-':'').'message-id' => $mid);
168 }
169 $hdr->add (subject => $p{subject}) if length $p{subject};
170 my $a;
171 for (grep {/^misc_/} keys %p) {
172 $a = $hdr->field ('content-x-properties') unless ref $a;
173 my $name = substr ($_, 5);
174 $name =~ tr/_/-/;
175 if ($p{base_uri} && /uri/ && length $p{$_}) {
176 require URI::WithBase;
177 $a->add ($name => URI::WithBase->new ($p{$_}, $p{base_uri})->abs);
178 } else {
179 $a->add ($name => $p{$_}) if length $p{$_};
180 }
181 }
182 ## Body and body information
183 my $b = $self->{hook_msg_body} || \&_make_a_msg_body;
184 &$b ($self, $msg, $p{body}, \%p);
185 ## List information
186 if (length $p{list_id}) {
187 my $lid = $hdr->field ('list-id');
188 $lid->value ($p{list_id});
189 $lid->display_name ($p{list_name}) if length $p{list_name};
190 }
191 $hdr->add ('x-mail-count' => $p{msg_count}) if defined $p{msg_count};
192 $hdr->add ('x-ml-info' => $p{list_info}) if defined $p{list_info};
193 if ($p{base_uri}) {
194 my $uri = $hdr->add (x_uri => '');
195 $uri->value ($p{base_uri});
196 $uri->display_name ($p{list_name}) if length $p{list_name};
197 }
198 if ($p{urn_template}) {
199 my $urn = $self->Message::Field::Date::_date2str ({
200 format_template => $p{urn_template},
201 date_time => $date->unix_time,
202 zone => $date->zone,
203 fmt2str => $self->{fmt2str},
204 });
205 $hdr->add ('x-uri')->value ($urn);
206 }
207 ## Additional information
208 my $u = $self->{hook_msg_header_add};
209 &$u ($self, $msg, \%p) if ref $u;
210 $hdr->field ('x-shimbun-agent')->add ($MYNAME => $VERSION);
211 $msg;
212 }
213
214 sub _make_a_msg_body ($$$\%) {
215 my $self = shift;
216 my ($msg, $body, $param) = @_;
217 if (length $body) {
218 $body =~ s/(?<!\x0D\x0A)\z/\x0D\x0A/s;
219 $msg->body ($body);
220 }
221 }
222
223 sub default_parameter ($@) {
224 my $self = shift;
225 if (@_ == 1) {
226 return $self->{default_parameter}->{ $_[0] };
227 }
228 while (my ($name, $value) = splice (@_, 0, 2)) {
229 $self->{default_parameter}->{$name} = $value;
230 }
231 $self;
232 }
233
234 =head1 LICENSE
235
236 Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
237
238 This program is free software; you can redistribute it and/or modify
239 it under the terms of the GNU General Public License as published by
240 the Free Software Foundation; either version 2 of the License, or
241 (at your option) any later version.
242
243 This program is distributed in the hope that it will be useful,
244 but WITHOUT ANY WARRANTY; without even the implied warranty of
245 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
246 GNU General Public License for more details.
247
248 You should have received a copy of the GNU General Public License
249 along with this program; see the file COPYING. If not, write to
250 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
251 Boston, MA 02111-1307, USA.
252
253 =head1 CHANGE
254
255 See F<ChangeLog>.
256 $Date: 2002/06/16 02:50:54 $
257
258 =cut
259
260 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24