3 # yapfaq Version 0.6 by Thomas Hochstein
4 # (Original author: Marc Brockschmidt)
6 # This script posts any project described in its config-file. Most people
7 # will use it in combination with cron(8).
9 # Copyright (C) 2003 Marc Brockschmidt <marc@marcbrockschmidt.de>
10 # Copyright (c) 2010 Thomas Hochstein <thh@inter.net>
12 # It can be redistributed and/or modified under the same terms under
13 # which Perl itself is published.
15 my $Version = "0.6-unreleased";
21 my $ConfigFile = "yapfaq.cfg";
24 ################################## PGP-Config #################################
26 my $pgp = '/usr/bin/pgp'; # path to pgp
27 my $PGPVersion = '2'; # Use 2 for 2.X, 5 for PGP > 2.X and GPG for GPG
29 my $PGPSigner = ''; # sign as who?
30 my $PGPPass = ''; # pgp2 only
31 my $PathtoPGPPass = ''; # pgp2, pgp5 and gpg
34 my $pgpbegin ='-----BEGIN PGP SIGNATURE-----';# Begin of PGP-Signature
35 my $pgpend ='-----END PGP SIGNATURE-----'; # End of PGP-Signature
36 my $pgptmpf ='pgptmp'; # temporary file for PGP.
37 my $pgpheader ='X-PGP-Sig';
39 my @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');
44 my @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');
51 ############################# End of Configuration #############################
55 use Date::Calc qw(Add_Delta_YM Add_Delta_Days Delta_Days Today);
56 use Fcntl ':flock'; # import LOCK_* constants
57 my ($TDY, $TDM, $TDD) = Today(); #TD: Today's date
60 readconfig (\$ConfigFile, \@Config);
63 my ($LPD,$LPM,$LPY) = (01, 01, 0001); #LP: Last posting-date
64 my ($NPY,$NPM,$NPD); #NP: Next posting-date
67 my ($ActName,$File,$PFreq) =($$_{'name'},$$_{'file'},$$_{'posting-frequency'});
68 my ($From,$Subject,$NG,$Fup2)=($$_{'from'},$$_{'subject'},$$_{'ngs'},$$_{'fup2'});
69 my ($MIDF,$ReplyTo,$ExtHea)=($$_{'mid-format'},$$_{'reply-to'},$$_{'extraheader'});
70 my ($Supersede) =($$_{'supersede'});
72 if (open (FH, "<$File.cfg")) {
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*$/) {
82 warn "Couldn't open $File.cfg: $!\n";
85 $SupersedeMID = "" unless $Supersede;
87 if ($PFreq =~ /(\d+)\s*([dw])/) { # Is counted in days or weeks: Use Add_Delta_Days.
88 ($NPY,$NPM,$NPD) = Add_Delta_Days($LPY, $LPM, $LPD, (($2 eq "w")?$1 * 7: $1 * 1));
89 } elsif ($PFreq =~ /(\d+)\s*([my])/) { #Is counted in months or years: Use Add_Delta_YM
90 ($NPY,$NPM,$NPD) = Add_Delta_YM($LPY, $LPM, $LPD, (($2 eq "m")?(0,$1):($1,0)));
93 if (Delta_Days($NPY,$NPM,$NPD,$TDY,$TDM,$TDD) >= 0 ) {
94 postfaq(\$ActName,\$File,\$From,\$Subject,\$NG,\$Fup2,\$MIDF,\$ExtHea,\$Sender,\$TDY,\$TDM,\$TDD,\$ReplyTo,\$SupersedeMID);
100 ################################## readconfig ##################################
101 # Takes a filename and the reference to an array, which will hold hashes with
102 # the data from $File.
105 my ($File, $Config) = @_;
106 my ($LastEntry, $Error, $i) = ('','',0);
108 open FH, "<$$File" or die "$0: Can't open $$File: $!";
110 if (/^(\s*(\S+)\s*=\s*'?(.*?)'?\s*(#.*$|$)|^(.*?)'?\s*(#.*$|$))/ && not /^\s*$/) {
111 $LastEntry = lc($2) if $2;
112 $$Config[$i]{$LastEntry} .= $3 if $3;
113 $$Config[$i]{$LastEntry} .= "\n$5" if $5 && $5;
115 if (/^\s*=====\s*$/) {
123 unless($$Config[$i]{'from'} =~ /\S+\@(\S+\.)?\S{2,}\.\S{2,}/) {
124 $Error .= "The From-header for your project \"$$Config[$i]{'name'}\" seems to be incorrect.\n"
126 unless($$Config[$i]{'ngs'} =~ /^\S+$/) {
127 $Error .= "The Newsgroups-header for your project \"$$Config[$i]{'name'}\" contains whitespaces.\n"
129 unless(!$$Config[$i]{'fup2'} || $$Config[$i]{'fup2'} =~ /^\S+$/) {
130 $Error .= "The Followup-To-header for your project \"$$Config[$i]{'name'}\" contains whitespaces.\n"
132 unless($$Config[$i]{'posting-frequency'} =~ /^\s*\d+\s*[dwmy]\s*$/) {
133 $Error .= "The Posting-frequency for your project \"$$Config[$i]{'name'}\" is invalid.\n"
135 $Error .= "-" x 25 . "\n" if $Error;
137 die $Error if $Error;
140 ################################## postfaq ##################################
141 # Takes a filename and many other vars.
143 # It reads the data-file $File and then posts the article.
146 my ($ActName,$File,$From,$Subject,$NG,$Fup2,$MIDF,$ExtraHeaders,$Sender,$TDY,$TDM,$TDD,$ReplyTo,$Supersedes) = @_;
147 my (@Header,@Body,$MID,$InRealBody,$LastModified);
150 $$TDM = ($$TDM < 10 && $$TDM !~ /^0/) ? "0" . $$TDM : $$TDM;
151 $$TDD = ($$TDD < 10 && $$TDD !~ /^0/) ? "0" . $$TDD : $$TDD;
154 $MID =~ s/\%n/$$ActName/g;
155 $MID =~ s/\%d/$$TDD/g;
156 $MID =~ s/\%m/$$TDM/g;
157 $MID =~ s/\%y/$$TDY/g;
161 open (FH, "<$$File");
164 push (@Body, $_), next if $InRealBody;
165 $InRealBody++ if /^$/;
166 $LastModified = $1 if /^Last-modified: (\S+)$/i;
170 push @Body, "\n" if ($Body[-1] ne "\n");
172 #Create Date- and Expires-Header:
173 my @time = localtime;
174 my $ss = ($time[0]<10) ? "0" . $time[0] : $time[0];
175 my $mm = ($time[1]<10) ? "0" . $time[1] : $time[1];
176 my $hh = ($time[2]<10) ? "0" . $time[2] : $time[2];
178 my $month = ($time[4]+1<10) ? "0" . ($time[4]+1) : $time[4]+1;
179 my $monthN = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$time[4]];
180 my $wday = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat")[$time[6]];
181 my $year = (1900 + $time[5]);
182 my $tz = $time[8] ? " +0200" : " +0100";
184 my ($expY,$expM,$expD) = Add_Delta_YM($year, $month, $day, 0, 3);
185 my $expmonthN = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$expM-1];
187 my $date = "$day $monthN $year " . $hh . ":" . $mm . ":" . $ss . $tz;
188 my $expdate = "$expD $expmonthN $expY $hh:$mm:$ss$tz";
190 #Replace %LM by the content of the news.answer-pseudo-header Last-modified:
192 $$Subject =~ s/\%LM/$LastModified/;
195 #Now create the complete Header:
196 push @Header, "From: $$From\n";
197 push @Header, "Newsgroups: $$NG\n";
198 push @Header, "Followup-To: $$Fup2\n" if $$Fup2;
199 push @Header, "Subject: $$Subject\n";
200 push @Header, "Message-ID: $MID\n";
201 push @Header, "Supersedes: $$Supersedes\n" if $$Supersedes;
202 push @Header, "Date: $date\n";
203 push @Header, "Expires: $expdate\n";
204 push @Header, "Sender: $$Sender\n" if $$Sender;
205 push @Header, "Mime-Version: 1.0\n";
206 push @Header, "Reply-To: $$ReplyTo\n" if $$ReplyTo;
207 push @Header, "Content-Type: text/plain; charset=ISO-8859-15\n";
208 push @Header, "Content-Transfer-Encoding: 8bit\n";
209 push @Header, "User-Agent: yapfaq/$Version\n";
210 if ($$ExtraHeaders) {
211 push @Header, "$_\n" for (split /\n/, $$ExtraHeaders);
214 my @Article = ($UsePGP)?@{signpgp(\@Header, \@Body)}:(@Header, "\n", @Body);
218 open (FH, ">$$File.cfg") or die "$0: Can't open $$File.cfg: $!";
219 print FH "##;; Lastpost: $day.$month.$year\n";
220 print FH "##;; LastMID: $MID\n";
224 ################################## post ##################################
225 # Takes a complete article (Header and Body).
227 # It opens a connection to $NNTPServer and posts the message.
232 my $NewsConnection = Net::NNTP->new($NNTPServer, Reader => 1)
233 or die "Can't connect to news server $NNTPServer!\n";
235 $NewsConnection->authinfo ($NNTPUser, $NNTPPass);
236 $NewsConnection->post();
237 $NewsConnection->datasend (@$ArticleR);
238 $NewsConnection->dataend();
240 if (!$NewsConnection->ok()) {
241 open FH, ">>ERROR.dat";
242 print FH "\nPosting failed! Saving to ERROR.dat. Response from news server:\n";
243 print FH $NewsConnection->code();
244 print FH $NewsConnection->message();
247 print FH "-" x 80, "\n";
251 $NewsConnection->quit();
254 #-------- sub getpgpcommand
255 # getpgpcommand generates the command to sign the message and returns it.
258 # - $PGPVersion: A scalar holding the PGPVersion
260 my ($PGPVersion) = @_;
263 if ($PGPVersion eq '2') {
264 if ($PathtoPGPPass && !$PGPPass) {
265 open (PGPPW, $PathtoPGPPass) or die "Can't open $PathtoPGPPass: $!";
271 $PGPCommand = "PGPPASS=\"".$PGPPass."\" ".$pgp." -u \"".$PGPSigner."\" +verbose=0 language='en' -saft <".$pgptmpf.".txt >".$pgptmpf.".txt.asc";
273 die "$0: PGP-Passphrase is unknown!\n";
275 } elsif ($PGPVersion eq '5') {
276 if ($PathtoPGPPass) {
277 $PGPCommand = "PGPPASSFD=2 ".$pgp."s -u \"".$PGPSigner."\" -t --armor -o ".$pgptmpf.".txt.asc -z -f < ".$pgptmpf.".txt 2<".$PathtoPGPPass;
279 die "$0: PGP-Passphrase is unknown!\n";
281 } elsif ($PGPVersion =~ m/GPG/io) {
282 if ($PathtoPGPPass) {
283 $PGPCommand = $pgp." --digest-algo MD5 -a -u \"".$PGPSigner."\" -o ".$pgptmpf.".txt.asc --no-tty --batch --passphrase-fd 2 2<".$PathtoPGPPass." --clearsign ".$pgptmpf.".txt";
285 die "$0: Passphrase is unknown!\n";
288 die "$0: Unknown PGP-Version $PGPVersion!";
294 #-------- sub signarticle
295 # signarticle signs an articel and returns a reference to an array
296 # containing the whole signed Message.
299 # - $HeaderAR: A reference to a array containing the articles headers.
300 # - $BodyR: A reference to an array containing the body.
303 # - $MessageRef: A reference to an array containing the whole message.
305 my ($HeaderAR, $BodyR) = @_;
306 my (@pgphead, @pgpbody, $pgphead, $pgpbody, $header, $signheaders, @signheaders, $currentheader, $HeaderR, $line);
308 foreach my $line (@$HeaderAR) {
309 if ($line =~ /^(\S+):\s+(.*)$/s) {
311 $$HeaderR{lc($currentheader)} = "$1: $2";
313 $$HeaderR{lc($currentheader)} .= $line;
317 foreach (@PGPSignHeaders) {
318 if (defined($$HeaderR{lc($_)}) && $$HeaderR{lc($_)} =~ m/^[^\s:]+: .+/o) {
319 push @signheaders, $_;
323 $pgpbody = join ("", @$BodyR);
325 # Delete and create the temporary pgp-Files
326 unlink "$pgptmpf.txt";
327 unlink "$pgptmpf.txt.asc";
328 $signheaders = join(",", @signheaders);
330 $pgphead = "X-Signed-Headers: $signheaders\n";
331 foreach $header (@signheaders) {
332 if ($$HeaderR{lc($header)} =~ m/^[^\s:]+: (.+?)\n?$/so) {
333 $pgphead .= $header.": ".$1."\n";
337 open(FH, ">" . $pgptmpf . ".txt") or die "$0: can't open $pgptmpf: $!\n";
338 print FH $pgphead, "\n", $pgpbody;
339 print FH "\n" if ($PGPVersion =~ m/GPG/io); # workaround a pgp/gpg incompatibility - should IMHO be fixed in pgpverify
340 close(FH) or warn "$0: Couldn't close TMP: $!\n";
342 # Start PGP, then read the signature;
343 my $PGPCommand = getpgpcommand($PGPVersion);
346 open (FH, "<" . $pgptmpf . ".txt.asc") or die "$0: can't open ".$pgptmpf.".txt.asc: $!\n";
349 unless (m/\Q$pgpbegin\E$/o) {
350 # unlink $pgptmpf . ".txt";
351 # unlink $pgptmpf . ".txt.asc";
352 die "$0: $pgpbegin not found in ".$pgptmpf.".txt.asc\n"
354 unlink($pgptmpf . ".txt") or warn "$0: Couldn't unlink $pgptmpf.txt: $!\n";
358 unless (m/^Version: (\S+)(?:\s(\S+))?/o) {
359 unlink $pgptmpf . ".txt";
360 unlink $pgptmpf . ".txt.asc";
361 die "$0: didn't find PGP Version line where expected.\n";
365 $$HeaderR{$pgpheader} = $1."-".$2." ".$signheaders;
367 $$HeaderR{$pgpheader} = $1." ".$signheaders;
370 do { # skip other pgp headers like
371 $_ = <FH>; # "charset:"||"comment:" until empty line
376 last if /^\Q$pgpend\E$/;
377 $$HeaderR{$pgpheader} .= "\n\t$_";
380 $$HeaderR{$pgpheader} .= "\n" unless ($$HeaderR{$pgpheader} =~ /\n$/s);
384 unlink $pgptmpf . ".txt";
385 unlink $pgptmpf . ".txt.asc";
386 die "$0: unexpected data following $pgpend\n";
389 unlink "$pgptmpf.txt.asc";
391 my $tmppgpheader = $pgpheader . ": " . $$HeaderR{$pgpheader};
392 delete $$HeaderR{$pgpheader};
395 foreach $header (@PGPorderheaders) {
396 if ($$HeaderR{$header} && $$HeaderR{$header} ne "\n") {
397 push(@pgphead, "$$HeaderR{$header}");
398 delete $$HeaderR{$header};
402 foreach $header (keys %$HeaderR) {
403 if ($$HeaderR{$header} && $$HeaderR{$header} ne "\n") {
404 push(@pgphead, "$$HeaderR{$header}");
405 delete $$HeaderR{$header};
409 push @pgphead, ("X-PGP-Key: " . $PGPSigner . "\n"), $tmppgpheader;
412 @pgpbody = split /$/m, $pgpbody;
413 my @pgpmessage = (@pgphead, "\n", @pgpbody);