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