/[suikacvs]/messaging/suikawari/wari.pl
Suika

Contents of /messaging/suikawari/wari.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download)
Sun Jun 16 11:06:45 2002 UTC (21 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +15 -6 lines
File MIME type: text/plain
2002-06-16  wakaba <w@suika.fam.cx>

	* wari.pl: New script.
	* module/: New directory.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24