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

Contents of /messaging/suikawari/wari.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Thu Jun 20 11:39:46 2002 UTC (21 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.2: +24 -9 lines
File MIME type: text/plain
2002-06-20  wakaba <w@suika.fam.cx>

	* wari.pl:
	- Don't set 'meta_info' when it is not defined.
	- (load_module): New procedure.  Inheriting is supported.
	- (send_msg): Don't die even if posting is failed.

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.3 $VERSION=do{my @r=(q$Revision: 1.2 $=~/\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     GetOptions (
24     debug => \$Bunshin::DEBUG,
25     'module-dir=s' => \$directory,
26     'nntp-server=s' => \$NNTP_SERVER,
27     'posted-log=s' => \$posted_log,
28 wakaba 1.2 verbose => \$VERBOSE,
29 wakaba 1.1 ) or die;
30    
31     sub dprint (@);
32 wakaba 1.2 sub vprint (@);
33 wakaba 1.1 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 wakaba 1.3 dprint $_;
56 wakaba 1.1 my $module = "Suikawari::$_";
57 wakaba 1.3 load_module ($_);
58 wakaba 1.1 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 wakaba 1.3 $b->set_regex (metainfo => shift (@metareg)) if @metareg > 0;
64     $b->set_elements (metainfo => @metareg) if @metareg > 0;
65 wakaba 1.1 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 wakaba 1.3 vprint 'Date: '.$t;
82     #dprint 'Subject: '.$_->header->field ('subject');
83 wakaba 1.1 send_msg ($_ => $nntp);
84     $time = $t if $t > $time || !$time;
85     }
86     close_nntp ($nntp);
87     $plog->replace ($_ => $time) if $time > $latest_time;
88     }
89    
90     open LOG, '> '.$posted_log or die "$0: $posted_log: $!";
91     binmode LOG;
92     print LOG $plog;
93     close LOG;
94    
95     sub send_msg ($$) {
96     my $msg = shift;
97     my $nntp = shift;
98     dprint "Posting Message...";
99     my @m = map {$_."\n"} split /\x0D\x0A/, $msg;
100     my $r = $nntp->post (@m);
101 wakaba 1.2 vprint ${*$nntp}{'net_cmd_code'}, @{${*$nntp}{'net_cmd_resp'}};
102 wakaba 1.1 unless ($r) {
103 wakaba 1.3 #close_nntp ($nntp);
104     #die;
105     vprint "send_msg: Can't post the message. Skiped";
106 wakaba 1.1 }
107     }
108    
109     sub open_nntp () {
110     require Net::NNTP;
111 wakaba 1.2 vprint "Connecting to $NNTP_SERVER...";
112 wakaba 1.1 my $nntp = Net::NNTP->new ($NNTP_SERVER) or die "$0: open_nntp: $!";
113 wakaba 1.2 vprint ${*$nntp}{'net_cmd_code'}, @{${*$nntp}{'net_cmd_resp'}};
114 wakaba 1.1 $nntp;
115     }
116    
117     sub close_nntp ($) {
118     my $nntp = shift;
119     return unless ref $nntp;
120     $nntp->quit;
121 wakaba 1.2 vprint ${*$nntp}{'net_cmd_code'}, @{${*$nntp}{'net_cmd_resp'}};
122 wakaba 1.1 }
123    
124 wakaba 1.3 sub load_module ($) {
125     no strict 'refs';
126     my $m = shift;
127     dprint qq{require "$m.sb"};
128     require "$m.sb";
129     if (defined &{ "Suikawari::${m}::require" }) {
130     for (&{ "Suikawari::${m}::require" }) {
131     load_module ($_) unless ${ "Suikawari::${_}::VERSION" };
132     push @{ "Suikawari::${m}::ISA" }, "Suikawari::${_}";
133     }
134     }
135     }
136    
137 wakaba 1.1 sub dprint (@) {
138     print shift, ' ' if $Bunshin::DEBUG && @_ > 1;
139     print map {/\n$/s? $_: $_."\n"} @_ if $Bunshin::DEBUG;
140     }
141    
142 wakaba 1.2 sub vprint (@) {
143     print shift, ' ' if ($VERBOSE || $Bunshin::DEBUG) && @_ > 1;
144     print map {/\n$/s? $_: $_."\n"} @_ if $VERBOSE || $Bunshin::DEBUG;
145     }
146    
147 wakaba 1.1 package posted_log_ns;
148     use vars qw/%OPTION/;
149    
150     sub import () {
151     require Message::Header::Default;
152     %OPTION = %Message::Header::Default::OPTION;
153     $OPTION{namespace_uri} = 'urn:x-temp:x-posted-log';
154     $OPTION{namespace_phname} = 'posted';
155     $OPTION{namespace_phname_goodcase} = 'Posted';
156     $OPTION{case_sensible} = 1;
157     $OPTION{value_type} = {
158     ':default' => ['Message::Field::Date'],
159     };
160    
161     $Message::Header::NS_phname2uri{$OPTION{namespace_phname}} = $OPTION{namespace_uri};
162     $Message::Header::NS_uri2phpackage{$OPTION{namespace_uri}} = __PACKAGE__;
163     }
164    
165     =head1 SEE ALSO
166    
167     Bunshin L<Shimbun>
168    
169     =head1 LICENSE
170    
171     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
172    
173     This program is free software; you can redistribute it and/or modify
174     it under the terms of the GNU General Public License as published by
175     the Free Software Foundation; either version 2 of the License, or
176     (at your option) any later version.
177    
178     This program is distributed in the hope that it will be useful,
179     but WITHOUT ANY WARRANTY; without even the implied warranty of
180     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
181     GNU General Public License for more details.
182    
183     You should have received a copy of the GNU General Public License
184     along with this program; see the file COPYING. If not, write to
185     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
186     Boston, MA 02111-1307, USA.
187    
188     =head1 CHANGE
189    
190     See F<ChangeLog>.
191 wakaba 1.3 $Date: 2002/06/16 11:06:45 $
192 wakaba 1.1
193     =cut
194    
195     1;
196     ### wari.pl ends here

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24