-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmane-fetch.pl
executable file
·235 lines (201 loc) · 5.52 KB
/
gmane-fetch.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env perl
require 5.008_001;
#@ gmane-fetch.pl: connect to any hostnames in %HOSTS and query/update all
#@ groups that are mentioned for the corresponding name in the configuration
#@ file $RCFILE (are named NAME.), store fetched articles of updated groups in
#@ $MBOX (appended), finally update $RCFILE.
#@ $RCFILE can contain comments: lines that start with # (like this one).
#@ To init a new group, simply place its name alone on a line, as in:
#@ gwene.mail.s-mailx
#@ then run this script.
#@ TODO Primitive yet: no command line, no help, no file locking.
my %HOSTS = ("gmane" => "news.gmane.io");#, "gwene" => "news.gwene.org");
my $RCFILE = "${ENV{HOME}}/sec.arena/mail/.gmane.rc";
my $MBOX = "${ENV{HOME}}/sec.arena/mail/gmane";
my $SAFE_FSYNC = 1; # fsync(3) after each message (etc.)?
#
# Copyright (c) 2014 - 2022 Steffen Nurpmeso <[email protected]>.
#
# Based on the script nntp-to-mbox.pl that is:
#
# Copyright © 1999, 2000 Jamie Zawinski <[email protected]>
#
# Permission to use, copy, modify, distribute, and sell this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation. No representations are made about the suitability of this
# software for any purpose. It is provided "as is" without express or
# implied warranty.
#use diagnostics;# -verbose;
use warnings;
use strict;
use Encode;
use IO::Handle;
use POSIX;
use Socket;
my (@OGRPS, @COMMENTS, @NGRPS);
my ($RCFILE_SAVE, $UPDATE, $QUERY) = (0, 0, 0);
sub rcfile_parse{
print STDERR ". Reading resource file ${RCFILE}..\n";
open RC, '<:bytes', $RCFILE or die $^E;
@OGRPS = <RC>;
close RC or die $^E;
while(@OGRPS){
my $g = shift @OGRPS;
chomp $g;
if($g =~ /^\s*#/){
push @COMMENTS, $g;
next
}
$g =~ /^\s*([^\s]+)(?:\s+(\d+)\s+(\d+)\s+(\d+))?\s*$/;
die "Error parsing <$g>" unless $1;
if(!defined $2 || !defined $3 || !defined $4){
print STDERR ".. Group <$1> seems to be new: will only query status\n";
++$QUERY;
push @NGRPS, [$1, 0, 0, -1]
}else{
++$UPDATE;
push @NGRPS, [$1, $2, $3, $4]
}
}
}
sub rcfile_save{
return unless $RCFILE_SAVE;
print STDERR ". Writing resource file ${RCFILE}..\n";
open RC, '>:bytes', $RCFILE or die $^E;
while(@COMMENTS){
my $c = shift @COMMENTS;
print RC $c, "\n"
}
while(@NGRPS){
my $gr = shift @NGRPS;
print RC "$gr->[0] $gr->[1] $gr->[2] $gr->[3]\n"
}
RC->flush;
RC->sync if $SAFE_FSYNC;
close RC or die $^E
}
sub nntp_command{
my ($cmd) = @_;
$cmd =~ s/[\r\n]+$//; # canonicalize linebreaks.
#print STDERR ">> $cmd\n";
print NNTP "$cmd\r\n"
}
sub nntp_response{
my ($no_error) = @_;
$_ = <NNTP>;
die "Got no NNTP response" unless defined $_;
s/[\r\n]+$//; # canonicalize linebreaks.
#print STDERR "<< $_\n";
die "Malformed NNTP response: $_" if !m/^[0-9][0-9][0-9] /;
die "NNTP error: $_" if !$no_error && !m/^2[0-9][0-9] /;
$_
}
sub nntp_open{
my ($hostname, $port) = @_;
$port = 119 unless $port;
my $iaddr = gethostbyname($hostname);
die "$0: Cannot resolve hostname \"$hostname\". $!" unless $iaddr;
# Open a socket and get the data
die "$0: Cannot create socket: $!\n" if !socket NNTP, AF_INET, SOCK_STREAM, (getprotobyname('tcp'))[2];
die "$0: Cannot connect: $!\n" unless connect NNTP, pack_sockaddr_in($port, $iaddr);
binmode NNTP, ":bytes";
select(NNTP);$|=1;
nntp_response;
}
sub nntp_close{
nntp_command "QUIT";
close NNTP
}
sub nntp_group{
my ($group) = @_;
nntp_command "GROUP $group";
$_ = nntp_response 1;
return (undef, undef) if /^411 /; # No such newsgroup
my ($from, $to) = m/^[0-9]+ [0-9]+ ([0-9]+) ([0-9]+) .*/;
($from, $to)
}
sub nntp_article{
my ($fh, $group, $art) = @_;
nntp_command "ARTICLE $art";
$_ = nntp_response 1;
if(m/^423 /){
print STDERR "\n! Article $art expired or cancelled?\n";
return 0
}
if(!m/^2[0-9][0-9] /){
print STDERR "\n! NNTP error: $_\n";
return 0
}
print $fh "From ${group}-${art} ", scalar gmtime, "\n";
while(<NNTP>){
s/[\r\n]+$//; # canonicalize linebreaks.
last if m/^\.$/; # lone dot terminates
s/^\.//; # de-dottify.
s/^(From )/>$1/; # de-Fromify.
print $fh "$_\n"
}
print $fh "\n";
$fh->flush;
$fh->sync if $SAFE_FSYNC;
1
}
sub main {
my ($o);
rcfile_parse;
if($UPDATE > 0){
open MBOX, '>>:bytes', $MBOX or die $^E;
select MBOX;$|=1;
}
foreach my $name (keys %HOSTS){
my $hostname = $HOSTS{$name};
print STDERR ". Connecting to ${hostname}..\n";
nntp_open $hostname;
for(my $i = 0; $i < @NGRPS; ++$i){
my $gr = $NGRPS[$i];
next unless $gr->[0] =~ /^$name\./;
if($gr->[3] < 0){
print STDERR ". Query $gr->[0] .. "
}else{
print STDERR ". Update $gr->[0] #$gr->[3] ($gr->[1]/$gr->[2]) .. "
}
my ($f, $t) = nntp_group($gr->[0]);
if(!defined $f){
print STDERR "GROUP INACCESSABLE, skipping entry\n";
next
}
print STDERR "$f/$t\n";
($gr->[1], $gr->[2]) = ($f, $t);
if(!defined $gr->[3] || $gr->[3] < 0 || $gr->[3] > $t){
$gr->[3] = $t;
$RCFILE_SAVE = 1
}else{
my $j = 0;
while($gr->[3] < $t){
++$gr->[3];
$RCFILE_SAVE = 1;
if($j++ == 0){
print STDERR " $gr->[3]"
}elsif ($j % 8 == 0){
print STDERR "\n $gr->[3]"
}else{
print STDERR " $gr->[3]"
}
last unless nntp_article(($UPDATE > 0 ? *MBOX : *STDOUT), $gr->[0], $gr->[3])
}
print STDERR "\n" if $j > 0
}
}
nntp_close
}
if($UPDATE > 0){
close MBOX or die $^E
}
}
END{
rcfile_save
}
main;
exit 0;
# s-itt-mode