Add variable expiry.
[usenet/yapfaq.git] / yapfaq.pl
... / ...
CommitLineData
1#! /usr/bin/perl -W
2#
3# yapfaq Version 0.6 by Thomas Hochstein
4# (Original author: Marc Brockschmidt)
5#
6# This script posts any project described in its config-file. Most people
7# will use it in combination with cron(8).
8#
9# Copyright (C) 2003 Marc Brockschmidt <marc@marcbrockschmidt.de>
10# Copyright (c) 2010 Thomas Hochstein <thh@inter.net>
11#
12# It can be redistributed and/or modified under the same terms under
13# which Perl itself is published.
14
15my $Version = "0.6-unreleased";
16
17my $NNTPServer = "localhost";
18my $NNTPUser = "";
19my $NNTPPass = "";
20my $Sender = "";
21my $ConfigFile = "yapfaq.cfg";
22my $UsePGP = 0;
23
24################################## PGP-Config #################################
25
26my $pgp = '/usr/bin/pgp'; # path to pgp
27my $PGPVersion = '2'; # Use 2 for 2.X, 5 for PGP > 2.X and GPG for GPG
28
29my $PGPSigner = ''; # sign as who?
30my $PGPPass = ''; # pgp2 only
31my $PathtoPGPPass = ''; # pgp2, pgp5 and gpg
32
33
34my $pgpbegin ='-----BEGIN PGP SIGNATURE-----';# Begin of PGP-Signature
35my $pgpend ='-----END PGP SIGNATURE-----'; # End of PGP-Signature
36my $pgptmpf ='pgptmp'; # temporary file for PGP.
37my $pgpheader ='X-PGP-Sig';
38
39my @PGPSignHeaders = ('From', 'Newsgroups', 'Subject', 'Control',
40 'Supersedes', 'Followup-To', 'Date', 'Sender', 'Approved',
41 'Message-ID', 'Reply-To', 'Cancel-Lock', 'Cancel-Key',
42 'Also-Control', 'Distribution');
43
44my @PGPorderheaders = ('from', 'newsgroups', 'subject', 'control',
45 'supersedes', 'followup-To', 'date', 'organization', 'lines',
46 'sender', 'approved', 'distribution', 'message-id',
47 'references', 'reply-to', 'mime-version', 'content-type',
48 'content-transfer-encoding', 'summary', 'keywords', 'cancel-lock',
49 'cancel-key', 'also-control', 'x-pgp', 'user-agent');
50
51############################# End of Configuration #############################
52
53use strict;
54use Net::NNTP;
55use Date::Calc qw(Add_Delta_YM Add_Delta_Days Delta_Days Today);
56use Fcntl ':flock'; # import LOCK_* constants
57my ($TDY, $TDM, $TDD) = Today(); #TD: Today's date
58
59my @Config;
60readconfig (\$ConfigFile, \@Config);
61
62foreach (@Config) {
63 my ($LPD,$LPM,$LPY) = (01, 01, 0001); #LP: Last posting-date
64 my ($NPY,$NPM,$NPD); #NP: Next posting-date
65 my $SupersedeMID;
66
67 my ($ActName,$File,$PFreq,$Expire) =($$_{'name'},$$_{'file'},$$_{'posting-frequency'},$$_{'expires'});
68 my ($From,$Subject,$NG,$Fup2)=($$_{'from'},$$_{'subject'},$$_{'ngs'},$$_{'fup2'});
69 my ($MIDF,$ReplyTo,$ExtHea)=($$_{'mid-format'},$$_{'reply-to'},$$_{'extraheader'});
70 my ($Supersede) =($$_{'supersede'});
71
72 if (open (FH, "<$File.cfg")) {
73 while(<FH>){
74 if (/##;; Lastpost:\s*(\d{1,2})\.(\d{1,2})\.(\d{2}(\d{2})?)/){
75 ($LPD, $LPM, $LPY) = ($1, $2, $3);
76 } elsif (/^##;;\s*LastMID:\s*(<\S+@\S+>)\s*$/) {
77 $SupersedeMID = $1;
78 }
79 }
80 close FH;
81 } else {
82 warn "$0: W: Couldn't open $File.cfg: $!\n";
83 }
84
85 $SupersedeMID = "" unless $Supersede;
86
87 ($NPY,$NPM,$NPD) = calcdelta ($LPY,$LPM,$LPD,$PFreq);
88
89 if (Delta_Days($NPY,$NPM,$NPD,$TDY,$TDM,$TDD) >= 0 ) {
90 postfaq(\$ActName,\$File,\$From,\$Subject,\$NG,\$Fup2,\$MIDF,\$ExtHea,\$Sender,\$TDY,\$TDM,\$TDD,\$ReplyTo,\$SupersedeMID,\$Expire);
91 }
92}
93
94exit;
95
96################################## readconfig ##################################
97# Takes a filename and the reference to an array, which will hold hashes with
98# the data from $File.
99
100sub readconfig{
101 my ($File, $Config) = @_;
102 my ($LastEntry, $Error, $i) = ('','',0);
103
104 open FH, "<$$File" or die "$0: E: Can't open $$File: $!";
105 while (<FH>) {
106 if (/^(\s*(\S+)\s*=\s*'?(.*?)'?\s*(#.*$|$)|^(.*?)'?\s*(#.*$|$))/ && not /^\s*$/) {
107 $LastEntry = lc($2) if $2;
108 $$Config[$i]{$LastEntry} .= $3 if $3;
109 $$Config[$i]{$LastEntry} .= "\n$5" if $5 && $5;
110 }
111 if (/^\s*=====\s*$/) {
112 $i++;
113 }
114 }
115 close FH;
116
117 #Check saved values:
118 for $i (0..$i){
119 unless($$Config[$i]{'from'} =~ /\S+\@(\S+\.)?\S{2,}\.\S{2,}/) {
120 $Error .= "E: The From-header for your project \"$$Config[$i]{'name'}\" seems to be incorrect.\n"
121 }
122 unless($$Config[$i]{'ngs'} =~ /^\S+$/) {
123 $Error .= "E: The Newsgroups-header for your project \"$$Config[$i]{'name'}\" contains whitespaces.\n"
124 }
125 unless(!$$Config[$i]{'fup2'} || $$Config[$i]{'fup2'} =~ /^\S+$/) {
126 $Error .= "E: The Followup-To-header for your project \"$$Config[$i]{'name'}\" contains whitespaces.\n"
127 }
128 unless($$Config[$i]{'posting-frequency'} =~ /^\s*\d+\s*[dwmy]\s*$/) {
129 $Error .= "E: The Posting-frequency for your project \"$$Config[$i]{'name'}\" is invalid.\n"
130 }
131 unless($$Config[$i]{'expires'} =~ /^\s*\d+\s*[dwmy]\s*$/) {
132 $$Config[$i]{'expires'} = '3m'; # set default: 3 month
133 warn "$0: W: The Expires for your project \"$$Config[$i]{'name'}\" is invalid - set to 3 month.\n";
134 }
135 $Error .= "-" x 25 . "\n" if $Error;
136 }
137 die $Error if $Error;
138}
139
140################################# calcdelta #################################
141# Takes a date (year, month and day) and a time period (1d, 1w, 1m, 1y, ...)
142# and adds the latter to the former
143
144sub calcdelta {
145 my ($Year, $Month, $Day, $Period) = @_;
146 my ($NYear, $NMonth, $NDay);
147
148 if ($Period =~ /(\d+)\s*([dw])/) { # Is counted in days or weeks: Use Add_Delta_Days.
149 ($NYear, $NMonth, $NDay) = Add_Delta_Days($Year, $Month, $Day, (($2 eq "w")?$1 * 7: $1 * 1));
150 } elsif ($Period =~ /(\d+)\s*([my])/) { #Is counted in months or years: Use Add_Delta_YM
151 ($NYear, $NMonth, $NDay) = Add_Delta_YM($Year, $Month, $Day, (($2 eq "m")?(0,$1):($1,0)));
152 }
153 return ($NYear, $NMonth, $NDay);
154}
155
156################################## postfaq ##################################
157# Takes a filename and many other vars.
158#
159# It reads the data-file $File and then posts the article.
160
161sub postfaq {
162 my ($ActName,$File,$From,$Subject,$NG,$Fup2,$MIDF,$ExtraHeaders,$Sender,$TDY,$TDM,$TDD,$ReplyTo,$Supersedes,$Expire) = @_;
163 my (@Header,@Body,$MID,$InRealBody,$LastModified);
164
165 #Prepare MID:
166 $$TDM = ($$TDM < 10 && $$TDM !~ /^0/) ? "0" . $$TDM : $$TDM;
167 $$TDD = ($$TDD < 10 && $$TDD !~ /^0/) ? "0" . $$TDD : $$TDD;
168
169 $MID = $$MIDF;
170 $MID =~ s/\%n/$$ActName/g;
171 $MID =~ s/\%d/$$TDD/g;
172 $MID =~ s/\%m/$$TDM/g;
173 $MID =~ s/\%y/$$TDY/g;
174
175
176 #Now get the body:
177 open (FH, "<$$File");
178 while (<FH>){
179 s/\r//;
180 push (@Body, $_), next if $InRealBody;
181 $InRealBody++ if /^$/;
182 $LastModified = $1 if /^Last-modified: (\S+)$/i;
183 push @Body, $_;
184 }
185 close FH;
186 push @Body, "\n" if ($Body[-1] ne "\n");
187
188 #Create Date- and Expires-Header:
189 my @time = localtime;
190 my $ss = ($time[0]<10) ? "0" . $time[0] : $time[0];
191 my $mm = ($time[1]<10) ? "0" . $time[1] : $time[1];
192 my $hh = ($time[2]<10) ? "0" . $time[2] : $time[2];
193 my $day = $time[3];
194 my $month = ($time[4]+1<10) ? "0" . ($time[4]+1) : $time[4]+1;
195 my $monthN = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$time[4]];
196 my $wday = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat")[$time[6]];
197 my $year = (1900 + $time[5]);
198 my $tz = $time[8] ? " +0200" : " +0100";
199
200 my ($expY,$expM,$expD) = calcdelta ($year,$month,$day,$$Expire);
201 my $expmonthN = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$expM-1];
202
203 my $date = "$day $monthN $year " . $hh . ":" . $mm . ":" . $ss . $tz;
204 my $expdate = "$expD $expmonthN $expY $hh:$mm:$ss$tz";
205
206 #Replace %LM by the content of the news.answer-pseudo-header Last-modified:
207 if ($LastModified) {
208 $$Subject =~ s/\%LM/$LastModified/;
209 }
210
211 #Now create the complete Header:
212 push @Header, "From: $$From\n";
213 push @Header, "Newsgroups: $$NG\n";
214 push @Header, "Followup-To: $$Fup2\n" if $$Fup2;
215 push @Header, "Subject: $$Subject\n";
216 push @Header, "Message-ID: $MID\n";
217 push @Header, "Supersedes: $$Supersedes\n" if $$Supersedes;
218 push @Header, "Date: $date\n";
219 push @Header, "Expires: $expdate\n";
220 push @Header, "Sender: $$Sender\n" if $$Sender;
221 push @Header, "Mime-Version: 1.0\n";
222 push @Header, "Reply-To: $$ReplyTo\n" if $$ReplyTo;
223 push @Header, "Content-Type: text/plain; charset=ISO-8859-15\n";
224 push @Header, "Content-Transfer-Encoding: 8bit\n";
225 push @Header, "User-Agent: yapfaq/$Version\n";
226 if ($$ExtraHeaders) {
227 push @Header, "$_\n" for (split /\n/, $$ExtraHeaders);
228 }
229
230 my @Article = ($UsePGP)?@{signpgp(\@Header, \@Body)}:(@Header, "\n", @Body);
231
232 post(\@Article);
233
234 open (FH, ">$$File.cfg") or die "$0: E: Can't open $$File.cfg: $!";
235 print FH "##;; Lastpost: $day.$month.$year\n";
236 print FH "##;; LastMID: $MID\n";
237 close FH;
238}
239
240################################## post ##################################
241# Takes a complete article (Header and Body).
242#
243# It opens a connection to $NNTPServer and posts the message.
244
245sub post {
246 my ($ArticleR) = @_;
247
248 my $NewsConnection = Net::NNTP->new($NNTPServer, Reader => 1)
249 or die "$0: E: Can't connect to news server '$NNTPServer'!\n";
250
251 $NewsConnection->authinfo ($NNTPUser, $NNTPPass);
252 $NewsConnection->post();
253 $NewsConnection->datasend (@$ArticleR);
254 $NewsConnection->dataend();
255
256 if (!$NewsConnection->ok()) {
257 open FH, ">>ERROR.dat";
258 print FH "\nPosting failed! Saving to ERROR.dat. Response from news server:\n";
259 print FH $NewsConnection->code();
260 print FH $NewsConnection->message();
261 print FH "\n";
262 print FH @$ArticleR;
263 print FH "-" x 80, "\n";
264 close FH;
265 }
266
267 $NewsConnection->quit();
268}
269
270#-------- sub getpgpcommand
271# getpgpcommand generates the command to sign the message and returns it.
272#
273# Receives:
274# - $PGPVersion: A scalar holding the PGPVersion
275sub getpgpcommand {
276 my ($PGPVersion) = @_;
277 my $PGPCommand;
278
279 if ($PGPVersion eq '2') {
280 if ($PathtoPGPPass && !$PGPPass) {
281 open (PGPPW, $PathtoPGPPass) or die "$0: E: Can't open $PathtoPGPPass: $!";
282 $PGPPass = <PGPPW>;
283 close PGPPW;
284 }
285
286 if ($PGPPass) {
287 $PGPCommand = "PGPPASS=\"".$PGPPass."\" ".$pgp." -u \"".$PGPSigner."\" +verbose=0 language='en' -saft <".$pgptmpf.".txt >".$pgptmpf.".txt.asc";
288 } else {
289 die "$0: E: PGP-Passphrase is unknown!\n";
290 }
291 } elsif ($PGPVersion eq '5') {
292 if ($PathtoPGPPass) {
293 $PGPCommand = "PGPPASSFD=2 ".$pgp."s -u \"".$PGPSigner."\" -t --armor -o ".$pgptmpf.".txt.asc -z -f < ".$pgptmpf.".txt 2<".$PathtoPGPPass;
294 } else {
295 die "$0: E: PGP-Passphrase is unknown!\n";
296 }
297 } elsif ($PGPVersion =~ m/GPG/io) {
298 if ($PathtoPGPPass) {
299 $PGPCommand = $pgp." --digest-algo MD5 -a -u \"".$PGPSigner."\" -o ".$pgptmpf.".txt.asc --no-tty --batch --passphrase-fd 2 2<".$PathtoPGPPass." --clearsign ".$pgptmpf.".txt";
300 } else {
301 die "$0: E: Passphrase is unknown!\n";
302 }
303 } else {
304 die "$0: E: Unknown PGP-Version $PGPVersion!";
305 }
306 return $PGPCommand;
307}
308
309
310#-------- sub signarticle
311# signarticle signs an articel and returns a reference to an array
312# containing the whole signed Message.
313#
314# Receives:
315# - $HeaderAR: A reference to a array containing the articles headers.
316# - $BodyR: A reference to an array containing the body.
317#
318# Returns:
319# - $MessageRef: A reference to an array containing the whole message.
320sub signpgp {
321 my ($HeaderAR, $BodyR) = @_;
322 my (@pgphead, @pgpbody, $pgphead, $pgpbody, $header, $signheaders, @signheaders, $currentheader, $HeaderR, $line);
323
324 foreach my $line (@$HeaderAR) {
325 if ($line =~ /^(\S+):\s+(.*)$/s) {
326 $currentheader = $1;
327 $$HeaderR{lc($currentheader)} = "$1: $2";
328 } else {
329 $$HeaderR{lc($currentheader)} .= $line;
330 }
331 }
332
333 foreach (@PGPSignHeaders) {
334 if (defined($$HeaderR{lc($_)}) && $$HeaderR{lc($_)} =~ m/^[^\s:]+: .+/o) {
335 push @signheaders, $_;
336 }
337 }
338
339 $pgpbody = join ("", @$BodyR);
340
341 # Delete and create the temporary pgp-Files
342 unlink "$pgptmpf.txt";
343 unlink "$pgptmpf.txt.asc";
344 $signheaders = join(",", @signheaders);
345
346 $pgphead = "X-Signed-Headers: $signheaders\n";
347 foreach $header (@signheaders) {
348 if ($$HeaderR{lc($header)} =~ m/^[^\s:]+: (.+?)\n?$/so) {
349 $pgphead .= $header.": ".$1."\n";
350 }
351 }
352
353 open(FH, ">" . $pgptmpf . ".txt") or die "$0: E: can't open $pgptmpf: $!\n";
354 print FH $pgphead, "\n", $pgpbody;
355 print FH "\n" if ($PGPVersion =~ m/GPG/io); # workaround a pgp/gpg incompatibility - should IMHO be fixed in pgpverify
356 close(FH) or warn "$0: W: Couldn't close TMP: $!\n";
357
358 # Start PGP, then read the signature;
359 my $PGPCommand = getpgpcommand($PGPVersion);
360 `$PGPCommand`;
361
362 open (FH, "<" . $pgptmpf . ".txt.asc") or die "$0: E: can't open ".$pgptmpf.".txt.asc: $!\n";
363 $/ = "$pgpbegin\n";
364 $_ = <FH>;
365 unless (m/\Q$pgpbegin\E$/o) {
366# unlink $pgptmpf . ".txt";
367# unlink $pgptmpf . ".txt.asc";
368 die "$0: E: $pgpbegin not found in ".$pgptmpf.".txt.asc\n"
369 }
370 unlink($pgptmpf . ".txt") or warn "$0: W: Couldn't unlink $pgptmpf.txt: $!\n";
371
372 $/ = "\n";
373 $_ = <FH>;
374 unless (m/^Version: (\S+)(?:\s(\S+))?/o) {
375 unlink $pgptmpf . ".txt";
376 unlink $pgptmpf . ".txt.asc";
377 die "$0: E: didn't find PGP Version line where expected.\n";
378 }
379
380 if (defined($2)) {
381 $$HeaderR{$pgpheader} = $1."-".$2." ".$signheaders;
382 } else {
383 $$HeaderR{$pgpheader} = $1." ".$signheaders;
384 }
385
386 do { # skip other pgp headers like
387 $_ = <FH>; # "charset:"||"comment:" until empty line
388 } while ! /^$/;
389
390 while (<FH>) {
391 chomp;
392 last if /^\Q$pgpend\E$/;
393 $$HeaderR{$pgpheader} .= "\n\t$_";
394 }
395
396 $$HeaderR{$pgpheader} .= "\n" unless ($$HeaderR{$pgpheader} =~ /\n$/s);
397
398 $_ = <FH>;
399 unless (eof(FH)) {
400 unlink $pgptmpf . ".txt";
401 unlink $pgptmpf . ".txt.asc";
402 die "$0: E: unexpected data following $pgpend\n";
403 }
404 close(FH);
405 unlink "$pgptmpf.txt.asc";
406
407 my $tmppgpheader = $pgpheader . ": " . $$HeaderR{$pgpheader};
408 delete $$HeaderR{$pgpheader};
409
410 @pgphead = ();
411 foreach $header (@PGPorderheaders) {
412 if ($$HeaderR{$header} && $$HeaderR{$header} ne "\n") {
413 push(@pgphead, "$$HeaderR{$header}");
414 delete $$HeaderR{$header};
415 }
416 }
417
418 foreach $header (keys %$HeaderR) {
419 if ($$HeaderR{$header} && $$HeaderR{$header} ne "\n") {
420 push(@pgphead, "$$HeaderR{$header}");
421 delete $$HeaderR{$header};
422 }
423 }
424
425 push @pgphead, ("X-PGP-Key: " . $PGPSigner . "\n"), $tmppgpheader;
426 undef $tmppgpheader;
427
428 @pgpbody = split /$/m, $pgpbody;
429 my @pgpmessage = (@pgphead, "\n", @pgpbody);
430 return \@pgpmessage;
431}
This page took 0.011594 seconds and 4 git commands to generate.