1 |
#!/usr/bin/perl |
2 |
|
3 |
=head1 NAME |
4 |
|
5 |
wari.pl --- A shimbun implemention to post messages to NNTP server |
6 |
|
7 |
=cut |
8 |
|
9 |
use strict; |
10 |
use lib qw#./lib/#; |
11 |
use vars qw/$MYNAME $NNTP_SERVER $VERSION/; |
12 |
$VERSION=do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
13 |
$MYNAME = 'Suikawari'; |
14 |
use Bunshin; |
15 |
use Message::Header; |
16 |
|
17 |
use Getopt::Long; |
18 |
$NNTP_SERVER = 'localhost'; |
19 |
$Bunshin::DEBUG = 0; |
20 |
my $VERBOSE; |
21 |
my $directory = './module/'; |
22 |
my $posted_log = '.posted'; |
23 |
GetOptions ( |
24 |
debug => \$Bunshin::DEBUG, |
25 |
'module-dir=s' => \$directory, |
26 |
'nntp-server=s' => \$NNTP_SERVER, |
27 |
'posted-log=s' => \$posted_log, |
28 |
verbose => \$VERBOSE, |
29 |
) or die; |
30 |
|
31 |
sub eprint (@); |
32 |
sub dprint (@); |
33 |
sub vprint (@); |
34 |
binmode STDOUT; |
35 |
|
36 |
opendir DIR, $directory; |
37 |
my @module = sort map {s/\.sb$//; $_} grep /^[A-Za-z0-9_]+\.sb$/, readdir DIR; |
38 |
close DIR; |
39 |
die "$0: $directory: No suikawari definition" if @module == 0; |
40 |
push @main::INC, $directory; |
41 |
|
42 |
my $plog; |
43 |
{ |
44 |
&posted_log_ns::import; |
45 |
open LOG, $posted_log; |
46 |
binmode LOG; |
47 |
local $/ = undef; |
48 |
$plog = parse Message::Header scalar <LOG>, |
49 |
-format => 'x-internal-logfile', |
50 |
-ns_default_phuri => $posted_log_ns::OPTION{namespace_uri}, |
51 |
; |
52 |
close LOG; |
53 |
} |
54 |
|
55 |
for (@module) { |
56 |
vprint $_; |
57 |
my $module = "Suikawari::$_"; |
58 |
load_module ($_); |
59 |
my $b = new Bunshin; |
60 |
my @msgreg = $module->msg_regex; |
61 |
my @metareg = $module->meta_regex; |
62 |
$b->set_regex (message => shift (@msgreg)); |
63 |
$b->set_elements (message => @msgreg); |
64 |
$b->set_regex (metainfo => shift (@metareg)) if @metareg > 0; |
65 |
$b->set_elements (metainfo => @metareg) if @metareg > 0; |
66 |
my %face = $module->face; |
67 |
for (keys %face) { |
68 |
$b->default_parameter ($_ => $face{$_}); |
69 |
} |
70 |
$module->on_load_source ($b); |
71 |
$b->set_source ($module->source); |
72 |
$module->on_make ($b); |
73 |
my ($nntp, $time); |
74 |
my $latest_time = $plog->field ($_, -new_item_unless_exist => 0); |
75 |
dprint 'Latest-Posted-Date: '.$latest_time; |
76 |
for ($b->make_msgs) { |
77 |
$_->option (format => 'news-usefor', -recursive => 1); |
78 |
$_->header->field ('x-shimbun-agent')->add ($MYNAME => $VERSION); |
79 |
my $t = $_->header->field ('date'); |
80 |
next if $latest_time >= $t; |
81 |
$nntp = open_nntp () unless ref $nntp; |
82 |
vprint 'Date: '.$t; |
83 |
#dprint 'Subject: '.$_->header->field ('subject'); |
84 |
send_msg ($_ => $nntp); |
85 |
$time = $t if $t > $time || !$time; |
86 |
} |
87 |
close_nntp ($nntp); |
88 |
$plog->replace ($_ => $time) if $time > $latest_time; |
89 |
} |
90 |
|
91 |
open LOG, '> '.$posted_log or die "$0: $posted_log: $!"; |
92 |
binmode LOG; |
93 |
print LOG $plog; |
94 |
close LOG; |
95 |
|
96 |
sub send_msg ($$) { |
97 |
my $msg = shift; |
98 |
my $nntp = shift; |
99 |
dprint "Posting Message..."; |
100 |
my @m = map {$_."\n"} split /\x0D\x0A/, $msg; |
101 |
my $r = $nntp->post (@m); |
102 |
if ($r) { |
103 |
vprint ${*$nntp}{'net_cmd_code'}, @{${*$nntp}{'net_cmd_resp'}}; |
104 |
} else { |
105 |
eprint ${*$nntp}{'net_cmd_code'}, @{${*$nntp}{'net_cmd_resp'}}; |
106 |
#close_nntp ($nntp); |
107 |
#die; |
108 |
eprint "send_msg: Can't post the message. Skiped"; |
109 |
} |
110 |
} |
111 |
|
112 |
sub open_nntp () { |
113 |
require Net::NNTP; |
114 |
vprint "Connecting to $NNTP_SERVER..."; |
115 |
my $nntp = Net::NNTP->new ($NNTP_SERVER) or die "$0: open_nntp: $!"; |
116 |
vprint ${*$nntp}{'net_cmd_code'}, @{${*$nntp}{'net_cmd_resp'}}; |
117 |
$nntp; |
118 |
} |
119 |
|
120 |
sub close_nntp ($) { |
121 |
my $nntp = shift; |
122 |
return unless ref $nntp; |
123 |
$nntp->quit; |
124 |
vprint ${*$nntp}{'net_cmd_code'}, @{${*$nntp}{'net_cmd_resp'}}; |
125 |
} |
126 |
|
127 |
sub load_module ($) { |
128 |
no strict 'refs'; |
129 |
my $m = shift; |
130 |
dprint qq{require "$m.sb"}; |
131 |
require "$m.sb"; |
132 |
if (defined &{ "Suikawari::${m}::require" }) { |
133 |
for (&{ "Suikawari::${m}::require" }) { |
134 |
load_module ($_) unless ${ "Suikawari::${_}::VERSION" }; |
135 |
push @{ "Suikawari::${m}::ISA" }, "Suikawari::${_}"; |
136 |
} |
137 |
} |
138 |
} |
139 |
|
140 |
sub eprint (@) { |
141 |
print shift, ' ' if @_ > 1; |
142 |
print map {/\n$/s? $_: $_."\n"} @_; |
143 |
} |
144 |
|
145 |
sub dprint (@) { |
146 |
print shift, ' ' if $Bunshin::DEBUG && @_ > 1; |
147 |
print map {/\n$/s? $_: $_."\n"} @_ if $Bunshin::DEBUG; |
148 |
} |
149 |
|
150 |
sub vprint (@) { |
151 |
print shift, ' ' if ($VERBOSE || $Bunshin::DEBUG) && @_ > 1; |
152 |
print map {/\n$/s? $_: $_."\n"} @_ if $VERBOSE || $Bunshin::DEBUG; |
153 |
} |
154 |
|
155 |
package posted_log_ns; |
156 |
use vars qw/%OPTION/; |
157 |
|
158 |
sub import () { |
159 |
require Message::Header::Default; |
160 |
%OPTION = %Message::Header::Default::OPTION; |
161 |
$OPTION{namespace_uri} = 'urn:x-temp:x-posted-log'; |
162 |
$OPTION{namespace_phname} = 'posted'; |
163 |
$OPTION{namespace_phname_goodcase} = 'Posted'; |
164 |
$OPTION{case_sensible} = 1; |
165 |
$OPTION{value_type} = { |
166 |
':default' => ['Message::Field::Date'], |
167 |
}; |
168 |
|
169 |
$Message::Header::NS_phname2uri{$OPTION{namespace_phname}} = $OPTION{namespace_uri}; |
170 |
$Message::Header::NS_uri2phpackage{$OPTION{namespace_uri}} = __PACKAGE__; |
171 |
} |
172 |
|
173 |
=head1 SEE ALSO |
174 |
|
175 |
Bunshin L<Shimbun> |
176 |
|
177 |
=head1 LICENSE |
178 |
|
179 |
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>. |
180 |
|
181 |
This program is free software; you can redistribute it and/or modify |
182 |
it under the terms of the GNU General Public License as published by |
183 |
the Free Software Foundation; either version 2 of the License, or |
184 |
(at your option) any later version. |
185 |
|
186 |
This program is distributed in the hope that it will be useful, |
187 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
188 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
189 |
GNU General Public License for more details. |
190 |
|
191 |
You should have received a copy of the GNU General Public License |
192 |
along with this program; see the file COPYING. If not, write to |
193 |
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
194 |
Boston, MA 02111-1307, USA. |
195 |
|
196 |
=head1 CHANGE |
197 |
|
198 |
See F<ChangeLog>. |
199 |
$Date: 2002/06/20 11:39:46 $ |
200 |
|
201 |
=cut |
202 |
|
203 |
1; |
204 |
### wari.pl ends here |