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

Contents of /messaging/suikawari/wari.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations) (download)
Wed Jul 24 12:14:48 2002 UTC (21 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.4: +8 -5 lines
File MIME type: text/plain
2002-07-24  Wakaba <w@suika.fam.cx>

	* wari.pl (--module-name): New option.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24