Commit | Line | Data |
---|---|---|
dc88d139 TH |
1 | #! /usr/bin/perl -W |
2 | # | |
7aaba0e0 TH |
3 | # yapfaq Version 0.6 by Thomas Hochstein |
4 | # (Original author: Marc Brockschmidt) | |
dc88d139 | 5 | # |
7aaba0e0 | 6 | # This script posts any project described in its config-file. Most people |
dc88d139 TH |
7 | # will use it in combination with cron(8). |
8 | # | |
9 | # Copyright (C) 2003 Marc Brockschmidt <marc@marcbrockschmidt.de> | |
7aaba0e0 | 10 | # Copyright (c) 2010 Thomas Hochstein <thh@inter.net> |
dc88d139 TH |
11 | # |
12 | # It can be redistributed and/or modified under the same terms under | |
13 | # which Perl itself is published. | |
14 | ||
6b421553 | 15 | my $Version = "0.6.2"; |
dc88d139 | 16 | |
5f5909d2 | 17 | my $NNTPServer = "localhost"; |
dc88d139 TH |
18 | my $NNTPUser = ""; |
19 | my $NNTPPass = ""; | |
20 | my $Sender = ""; | |
21 | my $ConfigFile = "yapfaq.cfg"; | |
15dd764a | 22 | my $UsePGP = 0; |
dc88d139 TH |
23 | |
24 | ################################## PGP-Config ################################# | |
25 | ||
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 | |
28 | ||
29 | my $PGPSigner = ''; # sign as who? | |
30 | my $PGPPass = ''; # pgp2 only | |
31 | my $PathtoPGPPass = ''; # pgp2, pgp5 and gpg | |
32 | ||
33 | ||
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'; | |
38 | ||
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'); | |
43 | ||
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'); | |
50 | ||
51 | ############################# End of Configuration ############################# | |
52 | ||
53 | use strict; | |
54 | use Net::NNTP; | |
40847f71 | 55 | use Net::Domain qw(hostfqdn); |
dc88d139 TH |
56 | use Date::Calc qw(Add_Delta_YM Add_Delta_Days Delta_Days Today); |
57 | use Fcntl ':flock'; # import LOCK_* constants | |
4251e545 | 58 | use Getopt::Std; |
dc88d139 TH |
59 | my ($TDY, $TDM, $TDD) = Today(); #TD: Today's date |
60 | ||
b9550622 | 61 | # read commandline options |
4251e545 | 62 | my %Options; |
a052296f | 63 | getopts('Vhvpdt:f:', \%Options); |
b9550622 | 64 | # -V: print version / copyright information |
a052296f TH |
65 | if ($Options{'V'}) { |
66 | print "$0 v $Version\nCopyright (c) 2003 Marc Brockschmidt <marc\@marcbrockschmidt.de>\nCopyright (c) 2010 Thomas Hochstein <thh\@inter.net>\n"; | |
67 | print "This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.\n"; | |
68 | exit(0); | |
69 | } | |
b9550622 | 70 | # -h: feed myself to perldoc |
4251e545 | 71 | if ($Options{'h'}) { |
ae3b1b79 | 72 | exec ('perldoc', $0); |
4251e545 TH |
73 | exit(0); |
74 | }; | |
b9550622 | 75 | # -f: set $Faq |
4251e545 TH |
76 | my ($Faq) = $Options{'f'} if ($Options{'f'}); |
77 | ||
b9550622 | 78 | # read configuration (configured FAQs) |
dc88d139 | 79 | my @Config; |
4251e545 | 80 | readconfig (\$ConfigFile, \@Config, \$Faq); |
dc88d139 | 81 | |
b9550622 TH |
82 | # for each FAQ: |
83 | # - parse configuration | |
84 | # - read status data | |
85 | # - if FAQ is due: call postfaq() | |
dc88d139 TH |
86 | foreach (@Config) { |
87 | my ($LPD,$LPM,$LPY) = (01, 01, 0001); #LP: Last posting-date | |
88 | my ($NPY,$NPM,$NPD); #NP: Next posting-date | |
89 | my $SupersedeMID; | |
90 | ||
0c6ebe78 | 91 | my ($ActName,$File,$PFreq,$Expire) =($$_{'name'},$$_{'file'},$$_{'posting-frequency'},$$_{'expires'}); |
dc88d139 TH |
92 | my ($From,$Subject,$NG,$Fup2)=($$_{'from'},$$_{'subject'},$$_{'ngs'},$$_{'fup2'}); |
93 | my ($MIDF,$ReplyTo,$ExtHea)=($$_{'mid-format'},$$_{'reply-to'},$$_{'extraheader'}); | |
94 | my ($Supersede) =($$_{'supersede'}); | |
4251e545 | 95 | |
b9550622 | 96 | # -f: loop if not FAQ to post |
4251e545 TH |
97 | next if (defined($Faq) && $ActName ne $Faq); |
98 | ||
b9550622 | 99 | # read status data |
dc88d139 TH |
100 | if (open (FH, "<$File.cfg")) { |
101 | while(<FH>){ | |
102 | if (/##;; Lastpost:\s*(\d{1,2})\.(\d{1,2})\.(\d{2}(\d{2})?)/){ | |
103 | ($LPD, $LPM, $LPY) = ($1, $2, $3); | |
104 | } elsif (/^##;;\s*LastMID:\s*(<\S+@\S+>)\s*$/) { | |
105 | $SupersedeMID = $1; | |
106 | } | |
107 | } | |
108 | close FH; | |
109 | } else { | |
74407146 | 110 | warn "$0: W: Couldn't open $File.cfg: $!\n"; |
dc88d139 TH |
111 | } |
112 | ||
113 | $SupersedeMID = "" unless $Supersede; | |
114 | ||
0c6ebe78 TH |
115 | ($NPY,$NPM,$NPD) = calcdelta ($LPY,$LPM,$LPD,$PFreq); |
116 | ||
b9550622 | 117 | # if FAQ is due: get it out |
4251e545 TH |
118 | if (Delta_Days($NPY,$NPM,$NPD,$TDY,$TDM,$TDD) >= 0 or ($Options{'p'})) { |
119 | if($Options{'d'}) { | |
120 | print "$ActName: Would be posted now (but running in simulation mode [$0 -d]).\n" if $Options{'v'}; | |
121 | } else { | |
a0605478 | 122 | postfaq(\$ActName,\$File,\$From,\$Subject,\$NG,\$Fup2,\$MIDF,\$ExtHea,\$Sender,\$TDY,\$TDM,\$TDD,\$ReplyTo,\$SupersedeMID,\$Expire); |
4251e545 TH |
123 | } |
124 | } elsif($Options{'v'}) { | |
125 | print "$ActName: Nothing to do.\n"; | |
dc88d139 TH |
126 | } |
127 | } | |
128 | ||
129 | exit; | |
130 | ||
131 | ################################## readconfig ################################## | |
4251e545 TH |
132 | # Takes a filename, a reference to an array, which will hold hashes with |
133 | # the data from $File, and - optionally - the name of the (single) FAQ to post | |
dc88d139 TH |
134 | |
135 | sub readconfig{ | |
4251e545 | 136 | my ($File, $Config, $Faq) = @_; |
dc88d139 TH |
137 | my ($LastEntry, $Error, $i) = ('','',0); |
138 | ||
366322b2 | 139 | print "Reading configuration.\n" if($Options{'v'}); |
4251e545 | 140 | |
74407146 | 141 | open FH, "<$$File" or die "$0: E: Can't open $$File: $!"; |
dc88d139 | 142 | while (<FH>) { |
4251e545 | 143 | next if (defined($$Faq) && !/^\s*=====\s*$/ && defined($$Config[$i]{'name'}) && $$Config[$i]{'name'} ne $$Faq ); |
dc88d139 TH |
144 | if (/^(\s*(\S+)\s*=\s*'?(.*?)'?\s*(#.*$|$)|^(.*?)'?\s*(#.*$|$))/ && not /^\s*$/) { |
145 | $LastEntry = lc($2) if $2; | |
146 | $$Config[$i]{$LastEntry} .= $3 if $3; | |
147 | $$Config[$i]{$LastEntry} .= "\n$5" if $5 && $5; | |
148 | } | |
149 | if (/^\s*=====\s*$/) { | |
150 | $i++; | |
151 | } | |
152 | } | |
153 | close FH; | |
154 | ||
155 | #Check saved values: | |
156 | for $i (0..$i){ | |
4251e545 | 157 | next if (defined($$Faq) && defined($$Config[$i]{'name'}) && $$Config[$i]{'name'} ne $$Faq ); |
dbca4ad8 TH |
158 | unless(defined($$Config[$i]{'name'}) && $$Config[$i]{'name'} =~ /^\S+$/) { |
159 | $Error .= "E: The name of your project \"$$Config[$i]{'name'}\" is not defined or contains whitespaces.\n" | |
dc88d139 | 160 | } |
dbca4ad8 TH |
161 | unless(defined($$Config[$i]{'file'}) && -f $$Config[$i]{'file'}) { |
162 | $Error .= "E: The file to post for your project \"$$Config[$i]{'name'}\" is not defined or does not exist.\n" | |
163 | } | |
164 | unless(defined($$Config[$i]{'from'}) && $$Config[$i]{'from'} =~ /\S+\@(\S+\.)?\S{2,}\.\S{2,}/) { | |
165 | $Error .= "E: The From header for your project \"$$Config[$i]{'name'}\" seems to be incorrect.\n" | |
166 | } | |
167 | unless(defined($$Config[$i]{'ngs'}) && $$Config[$i]{'ngs'} =~ /^\S+$/) { | |
168 | $Error .= "E: The Newsgroups header for your project \"$$Config[$i]{'name'}\" is not defined or contains whitespaces.\n" | |
169 | } | |
170 | unless(defined($$Config[$i]{'subject'})) { | |
171 | $Error .= "E: The Subject header for your project \"$$Config[$i]{'name'}\" is not defined.\n" | |
dc88d139 TH |
172 | } |
173 | unless(!$$Config[$i]{'fup2'} || $$Config[$i]{'fup2'} =~ /^\S+$/) { | |
dbca4ad8 | 174 | $Error .= "E: The Followup-To header for your project \"$$Config[$i]{'name'}\" contains whitespaces.\n" |
dc88d139 | 175 | } |
dbca4ad8 | 176 | unless(defined($$Config[$i]{'posting-frequency'}) && $$Config[$i]{'posting-frequency'} =~ /^\s*\d+\s*[dwmy]\s*$/) { |
74407146 | 177 | $Error .= "E: The Posting-frequency for your project \"$$Config[$i]{'name'}\" is invalid.\n" |
dc88d139 | 178 | } |
5ddba442 | 179 | unless(!$$Config[$i]{'expires'} || $$Config[$i]{'expires'} =~ /^\s*\d+\s*[dwmy]\s*$/) { |
0c6ebe78 TH |
180 | warn "$0: W: The Expires for your project \"$$Config[$i]{'name'}\" is invalid - set to 3 month.\n"; |
181 | } | |
40847f71 TH |
182 | unless(defined($$Config[$i]{'mid-format'}) && $$Config[$i]{'mid-format'} =~ /^<\S+\@\S{2,}\.\S{2,}>$/) { |
183 | warn "$0: W: The Expires for your project \"$$Config[$i]{'name'}\" seems to be invalid - set to default.\n"; | |
184 | } | |
dc88d139 | 185 | } |
dbca4ad8 | 186 | $Error .= "-" x 25 . 'program terminated' . "-" x 25 . "\n" if $Error; |
dc88d139 TH |
187 | die $Error if $Error; |
188 | } | |
189 | ||
0c6ebe78 TH |
190 | ################################# calcdelta ################################# |
191 | # Takes a date (year, month and day) and a time period (1d, 1w, 1m, 1y, ...) | |
192 | # and adds the latter to the former | |
193 | ||
194 | sub calcdelta { | |
195 | my ($Year, $Month, $Day, $Period) = @_; | |
196 | my ($NYear, $NMonth, $NDay); | |
197 | ||
198 | if ($Period =~ /(\d+)\s*([dw])/) { # Is counted in days or weeks: Use Add_Delta_Days. | |
199 | ($NYear, $NMonth, $NDay) = Add_Delta_Days($Year, $Month, $Day, (($2 eq "w")?$1 * 7: $1 * 1)); | |
200 | } elsif ($Period =~ /(\d+)\s*([my])/) { #Is counted in months or years: Use Add_Delta_YM | |
201 | ($NYear, $NMonth, $NDay) = Add_Delta_YM($Year, $Month, $Day, (($2 eq "m")?(0,$1):($1,0))); | |
202 | } | |
203 | return ($NYear, $NMonth, $NDay); | |
204 | } | |
205 | ||
dc88d139 TH |
206 | ################################## postfaq ################################## |
207 | # Takes a filename and many other vars. | |
208 | # | |
209 | # It reads the data-file $File and then posts the article. | |
210 | ||
211 | sub postfaq { | |
0c6ebe78 | 212 | my ($ActName,$File,$From,$Subject,$NG,$Fup2,$MIDF,$ExtraHeaders,$Sender,$TDY,$TDM,$TDD,$ReplyTo,$Supersedes,$Expire) = @_; |
dc88d139 TH |
213 | my (@Header,@Body,$MID,$InRealBody,$LastModified); |
214 | ||
366322b2 | 215 | print "$$ActName: Preparing to post.\n" if($Options{'v'}); |
4251e545 | 216 | |
dc88d139 TH |
217 | #Prepare MID: |
218 | $$TDM = ($$TDM < 10 && $$TDM !~ /^0/) ? "0" . $$TDM : $$TDM; | |
219 | $$TDD = ($$TDD < 10 && $$TDD !~ /^0/) ? "0" . $$TDD : $$TDD; | |
220 | ||
221 | $MID = $$MIDF; | |
dbca4ad8 | 222 | $MID = '<%n-%d.%m.%y@'.hostfqdn.'>' if !defined($MID); |
dc88d139 TH |
223 | $MID =~ s/\%n/$$ActName/g; |
224 | $MID =~ s/\%d/$$TDD/g; | |
225 | $MID =~ s/\%m/$$TDM/g; | |
226 | $MID =~ s/\%y/$$TDY/g; | |
227 | ||
dc88d139 TH |
228 | #Now get the body: |
229 | open (FH, "<$$File"); | |
230 | while (<FH>){ | |
231 | s/\r//; | |
232 | push (@Body, $_), next if $InRealBody; | |
233 | $InRealBody++ if /^$/; | |
8e1cb154 | 234 | $LastModified = $1 if /^Last-modified: (\S+)$/i; |
dc88d139 TH |
235 | push @Body, $_; |
236 | } | |
237 | close FH; | |
238 | push @Body, "\n" if ($Body[-1] ne "\n"); | |
239 | ||
240 | #Create Date- and Expires-Header: | |
241 | my @time = localtime; | |
242 | my $ss = ($time[0]<10) ? "0" . $time[0] : $time[0]; | |
243 | my $mm = ($time[1]<10) ? "0" . $time[1] : $time[1]; | |
244 | my $hh = ($time[2]<10) ? "0" . $time[2] : $time[2]; | |
245 | my $day = $time[3]; | |
246 | my $month = ($time[4]+1<10) ? "0" . ($time[4]+1) : $time[4]+1; | |
247 | my $monthN = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$time[4]]; | |
248 | my $wday = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat")[$time[6]]; | |
249 | my $year = (1900 + $time[5]); | |
250 | my $tz = $time[8] ? " +0200" : " +0100"; | |
7823ece9 TH |
251 | |
252 | $$Expire = '3m' if !$$Expire; # set default if unset: 3 month | |
253 | ||
0c6ebe78 | 254 | my ($expY,$expM,$expD) = calcdelta ($year,$month,$day,$$Expire); |
dc88d139 TH |
255 | my $expmonthN = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$expM-1]; |
256 | ||
257 | my $date = "$day $monthN $year " . $hh . ":" . $mm . ":" . $ss . $tz; | |
258 | my $expdate = "$expD $expmonthN $expY $hh:$mm:$ss$tz"; | |
0c6ebe78 | 259 | |
dc88d139 TH |
260 | #Replace %LM by the content of the news.answer-pseudo-header Last-modified: |
261 | if ($LastModified) { | |
262 | $$Subject =~ s/\%LM/$LastModified/; | |
263 | } | |
264 | ||
4251e545 TH |
265 | # Test mode? |
266 | if($Options{'t'} and $Options{'t'} !~ /console/i) { | |
267 | $$NG = $Options{'t'}; | |
268 | } | |
269 | ||
dc88d139 TH |
270 | #Now create the complete Header: |
271 | push @Header, "From: $$From\n"; | |
272 | push @Header, "Newsgroups: $$NG\n"; | |
273 | push @Header, "Followup-To: $$Fup2\n" if $$Fup2; | |
274 | push @Header, "Subject: $$Subject\n"; | |
275 | push @Header, "Message-ID: $MID\n"; | |
276 | push @Header, "Supersedes: $$Supersedes\n" if $$Supersedes; | |
277 | push @Header, "Date: $date\n"; | |
278 | push @Header, "Expires: $expdate\n"; | |
279 | push @Header, "Sender: $$Sender\n" if $$Sender; | |
280 | push @Header, "Mime-Version: 1.0\n"; | |
281 | push @Header, "Reply-To: $$ReplyTo\n" if $$ReplyTo; | |
282 | push @Header, "Content-Type: text/plain; charset=ISO-8859-15\n"; | |
283 | push @Header, "Content-Transfer-Encoding: 8bit\n"; | |
284 | push @Header, "User-Agent: yapfaq/$Version\n"; | |
285 | if ($$ExtraHeaders) { | |
286 | push @Header, "$_\n" for (split /\n/, $$ExtraHeaders); | |
287 | } | |
288 | ||
b9550622 | 289 | # sign article if $UsePGP is true |
dc88d139 TH |
290 | my @Article = ($UsePGP)?@{signpgp(\@Header, \@Body)}:(@Header, "\n", @Body); |
291 | ||
b9550622 | 292 | # post article |
366322b2 | 293 | print "$$ActName: Posting article ...\n" if($Options{'v'}); |
dc88d139 TH |
294 | post(\@Article); |
295 | ||
5ddba442 TH |
296 | # Test mode? |
297 | return if($Options{'t'}); | |
298 | ||
b9550622 | 299 | # otherwise: update status data |
366322b2 | 300 | print "$$ActName: Save status information.\n" if($Options{'v'}); |
4251e545 | 301 | |
74407146 | 302 | open (FH, ">$$File.cfg") or die "$0: E: Can't open $$File.cfg: $!"; |
dc88d139 TH |
303 | print FH "##;; Lastpost: $day.$month.$year\n"; |
304 | print FH "##;; LastMID: $MID\n"; | |
305 | close FH; | |
306 | } | |
307 | ||
308 | ################################## post ################################## | |
309 | # Takes a complete article (Header and Body). | |
310 | # | |
311 | # It opens a connection to $NNTPServer and posts the message. | |
312 | ||
313 | sub post { | |
314 | my ($ArticleR) = @_; | |
315 | ||
4251e545 TH |
316 | # Test mode? |
317 | if(defined($Options{'t'}) and $Options{'t'} =~ /console/i) { | |
318 | print "\n-----BEGIN--------------------------------------------------\n"; | |
319 | print @$ArticleR; | |
320 | print "\n------END---------------------------------------------------\n"; | |
321 | return; | |
322 | } | |
323 | ||
dc88d139 | 324 | my $NewsConnection = Net::NNTP->new($NNTPServer, Reader => 1) |
74407146 | 325 | or die "$0: E: Can't connect to news server '$NNTPServer'!\n"; |
dc88d139 | 326 | |
d69ba0bb | 327 | $NewsConnection->authinfo ($NNTPUser, $NNTPPass) if (defined($NNTPUser)); |
dc88d139 TH |
328 | $NewsConnection->post(); |
329 | $NewsConnection->datasend (@$ArticleR); | |
330 | $NewsConnection->dataend(); | |
331 | ||
b9550622 | 332 | # Posting failed? Save to ERROR.dat |
dc88d139 TH |
333 | if (!$NewsConnection->ok()) { |
334 | open FH, ">>ERROR.dat"; | |
114be302 | 335 | print FH "\nPosting failed! Saving to ERROR.dat. Response from news server:\n"; |
dc88d139 TH |
336 | print FH $NewsConnection->code(); |
337 | print FH $NewsConnection->message(); | |
338 | print FH "\n"; | |
339 | print FH @$ArticleR; | |
340 | print FH "-" x 80, "\n"; | |
341 | close FH; | |
342 | } | |
343 | ||
344 | $NewsConnection->quit(); | |
345 | } | |
346 | ||
347 | #-------- sub getpgpcommand | |
348 | # getpgpcommand generates the command to sign the message and returns it. | |
349 | # | |
350 | # Receives: | |
351 | # - $PGPVersion: A scalar holding the PGPVersion | |
352 | sub getpgpcommand { | |
353 | my ($PGPVersion) = @_; | |
354 | my $PGPCommand; | |
355 | ||
356 | if ($PGPVersion eq '2') { | |
357 | if ($PathtoPGPPass && !$PGPPass) { | |
74407146 | 358 | open (PGPPW, $PathtoPGPPass) or die "$0: E: Can't open $PathtoPGPPass: $!"; |
dc88d139 TH |
359 | $PGPPass = <PGPPW>; |
360 | close PGPPW; | |
361 | } | |
362 | ||
363 | if ($PGPPass) { | |
364 | $PGPCommand = "PGPPASS=\"".$PGPPass."\" ".$pgp." -u \"".$PGPSigner."\" +verbose=0 language='en' -saft <".$pgptmpf.".txt >".$pgptmpf.".txt.asc"; | |
365 | } else { | |
74407146 | 366 | die "$0: E: PGP-Passphrase is unknown!\n"; |
dc88d139 TH |
367 | } |
368 | } elsif ($PGPVersion eq '5') { | |
369 | if ($PathtoPGPPass) { | |
370 | $PGPCommand = "PGPPASSFD=2 ".$pgp."s -u \"".$PGPSigner."\" -t --armor -o ".$pgptmpf.".txt.asc -z -f < ".$pgptmpf.".txt 2<".$PathtoPGPPass; | |
371 | } else { | |
74407146 | 372 | die "$0: E: PGP-Passphrase is unknown!\n"; |
dc88d139 TH |
373 | } |
374 | } elsif ($PGPVersion =~ m/GPG/io) { | |
375 | if ($PathtoPGPPass) { | |
376 | $PGPCommand = $pgp." --digest-algo MD5 -a -u \"".$PGPSigner."\" -o ".$pgptmpf.".txt.asc --no-tty --batch --passphrase-fd 2 2<".$PathtoPGPPass." --clearsign ".$pgptmpf.".txt"; | |
377 | } else { | |
74407146 | 378 | die "$0: E: Passphrase is unknown!\n"; |
dc88d139 TH |
379 | } |
380 | } else { | |
74407146 | 381 | die "$0: E: Unknown PGP-Version $PGPVersion!"; |
dc88d139 TH |
382 | } |
383 | return $PGPCommand; | |
384 | } | |
385 | ||
386 | ||
387 | #-------- sub signarticle | |
388 | # signarticle signs an articel and returns a reference to an array | |
389 | # containing the whole signed Message. | |
390 | # | |
391 | # Receives: | |
392 | # - $HeaderAR: A reference to a array containing the articles headers. | |
393 | # - $BodyR: A reference to an array containing the body. | |
394 | # | |
395 | # Returns: | |
396 | # - $MessageRef: A reference to an array containing the whole message. | |
397 | sub signpgp { | |
398 | my ($HeaderAR, $BodyR) = @_; | |
399 | my (@pgphead, @pgpbody, $pgphead, $pgpbody, $header, $signheaders, @signheaders, $currentheader, $HeaderR, $line); | |
400 | ||
401 | foreach my $line (@$HeaderAR) { | |
402 | if ($line =~ /^(\S+):\s+(.*)$/s) { | |
403 | $currentheader = $1; | |
404 | $$HeaderR{lc($currentheader)} = "$1: $2"; | |
405 | } else { | |
406 | $$HeaderR{lc($currentheader)} .= $line; | |
407 | } | |
408 | } | |
409 | ||
410 | foreach (@PGPSignHeaders) { | |
411 | if (defined($$HeaderR{lc($_)}) && $$HeaderR{lc($_)} =~ m/^[^\s:]+: .+/o) { | |
412 | push @signheaders, $_; | |
413 | } | |
414 | } | |
415 | ||
416 | $pgpbody = join ("", @$BodyR); | |
417 | ||
418 | # Delete and create the temporary pgp-Files | |
419 | unlink "$pgptmpf.txt"; | |
420 | unlink "$pgptmpf.txt.asc"; | |
421 | $signheaders = join(",", @signheaders); | |
422 | ||
423 | $pgphead = "X-Signed-Headers: $signheaders\n"; | |
424 | foreach $header (@signheaders) { | |
425 | if ($$HeaderR{lc($header)} =~ m/^[^\s:]+: (.+?)\n?$/so) { | |
426 | $pgphead .= $header.": ".$1."\n"; | |
427 | } | |
428 | } | |
429 | ||
74407146 | 430 | open(FH, ">" . $pgptmpf . ".txt") or die "$0: E: can't open $pgptmpf: $!\n"; |
dc88d139 TH |
431 | print FH $pgphead, "\n", $pgpbody; |
432 | print FH "\n" if ($PGPVersion =~ m/GPG/io); # workaround a pgp/gpg incompatibility - should IMHO be fixed in pgpverify | |
74407146 | 433 | close(FH) or warn "$0: W: Couldn't close TMP: $!\n"; |
dc88d139 TH |
434 | |
435 | # Start PGP, then read the signature; | |
436 | my $PGPCommand = getpgpcommand($PGPVersion); | |
437 | `$PGPCommand`; | |
438 | ||
74407146 | 439 | open (FH, "<" . $pgptmpf . ".txt.asc") or die "$0: E: can't open ".$pgptmpf.".txt.asc: $!\n"; |
dc88d139 TH |
440 | $/ = "$pgpbegin\n"; |
441 | $_ = <FH>; | |
442 | unless (m/\Q$pgpbegin\E$/o) { | |
443 | # unlink $pgptmpf . ".txt"; | |
444 | # unlink $pgptmpf . ".txt.asc"; | |
74407146 | 445 | die "$0: E: $pgpbegin not found in ".$pgptmpf.".txt.asc\n" |
dc88d139 | 446 | } |
74407146 | 447 | unlink($pgptmpf . ".txt") or warn "$0: W: Couldn't unlink $pgptmpf.txt: $!\n"; |
dc88d139 TH |
448 | |
449 | $/ = "\n"; | |
450 | $_ = <FH>; | |
451 | unless (m/^Version: (\S+)(?:\s(\S+))?/o) { | |
452 | unlink $pgptmpf . ".txt"; | |
453 | unlink $pgptmpf . ".txt.asc"; | |
74407146 | 454 | die "$0: E: didn't find PGP Version line where expected.\n"; |
dc88d139 TH |
455 | } |
456 | ||
457 | if (defined($2)) { | |
458 | $$HeaderR{$pgpheader} = $1."-".$2." ".$signheaders; | |
459 | } else { | |
460 | $$HeaderR{$pgpheader} = $1." ".$signheaders; | |
461 | } | |
462 | ||
463 | do { # skip other pgp headers like | |
464 | $_ = <FH>; # "charset:"||"comment:" until empty line | |
465 | } while ! /^$/; | |
466 | ||
467 | while (<FH>) { | |
468 | chomp; | |
469 | last if /^\Q$pgpend\E$/; | |
470 | $$HeaderR{$pgpheader} .= "\n\t$_"; | |
471 | } | |
472 | ||
473 | $$HeaderR{$pgpheader} .= "\n" unless ($$HeaderR{$pgpheader} =~ /\n$/s); | |
474 | ||
475 | $_ = <FH>; | |
476 | unless (eof(FH)) { | |
477 | unlink $pgptmpf . ".txt"; | |
478 | unlink $pgptmpf . ".txt.asc"; | |
74407146 | 479 | die "$0: E: unexpected data following $pgpend\n"; |
dc88d139 TH |
480 | } |
481 | close(FH); | |
482 | unlink "$pgptmpf.txt.asc"; | |
483 | ||
484 | my $tmppgpheader = $pgpheader . ": " . $$HeaderR{$pgpheader}; | |
485 | delete $$HeaderR{$pgpheader}; | |
486 | ||
487 | @pgphead = (); | |
488 | foreach $header (@PGPorderheaders) { | |
489 | if ($$HeaderR{$header} && $$HeaderR{$header} ne "\n") { | |
490 | push(@pgphead, "$$HeaderR{$header}"); | |
491 | delete $$HeaderR{$header}; | |
492 | } | |
493 | } | |
494 | ||
495 | foreach $header (keys %$HeaderR) { | |
496 | if ($$HeaderR{$header} && $$HeaderR{$header} ne "\n") { | |
497 | push(@pgphead, "$$HeaderR{$header}"); | |
498 | delete $$HeaderR{$header}; | |
499 | } | |
500 | } | |
501 | ||
502 | push @pgphead, ("X-PGP-Key: " . $PGPSigner . "\n"), $tmppgpheader; | |
503 | undef $tmppgpheader; | |
504 | ||
505 | @pgpbody = split /$/m, $pgpbody; | |
506 | my @pgpmessage = (@pgphead, "\n", @pgpbody); | |
507 | return \@pgpmessage; | |
508 | } | |
272b0243 TH |
509 | |
510 | __END__ | |
511 | ||
512 | ################################ Documentation ################################# | |
513 | ||
514 | =head1 NAME | |
515 | ||
516 | yapfaq - Post Usenet FAQs I<(yet another postfaq)> | |
517 | ||
518 | =head1 SYNOPSIS | |
519 | ||
520 | B<yapfaq> [B<-hvpd>] [B<-t> I<newsgroups> | CONSOLE] [B<-f> I<project name>] | |
521 | ||
522 | =head1 REQUIREMENTS | |
523 | ||
524 | =over 2 | |
525 | ||
526 | =item - | |
527 | ||
528 | Perl 5.8 or later | |
529 | ||
530 | =item - | |
531 | ||
532 | Net::NNTP | |
533 | ||
534 | =item - | |
535 | ||
536 | Date::Calc | |
537 | ||
538 | =item - | |
539 | ||
540 | Getopt::Std | |
541 | ||
542 | =back | |
543 | ||
544 | Furthermore you need access to a news server to actually post FAQs. | |
545 | ||
546 | =head1 DESCRIPTION | |
547 | ||
548 | B<yapfaq> posts (one or more) FAQs to Usenet with a certain posting | |
549 | frequency (every n days, weeks, months or years), adding all necessary | |
550 | headers as defined in its config file (by default F<yapfaq.cfg>). | |
551 | ||
552 | =head2 Configuration | |
553 | ||
554 | F<yapfaq.cfg> consists of one or more blocks, separated by C<=====> on | |
555 | a single line, each containing the configuration for one FAQ as a set | |
556 | of definitions in the form of I<param = value>. | |
557 | ||
558 | =over 4 | |
559 | ||
560 | =item B<Name> = I<project name> | |
561 | ||
562 | A name referring to your FAQ, also used for generation of a Message-ID. | |
563 | ||
564 | This value must be set. | |
565 | ||
566 | =item B<File> = I<file name> | |
567 | ||
568 | A file containing the message body of your FAQ and all pseudo headers | |
569 | (subheaders in the news.answers style). | |
570 | ||
571 | This value must be set. | |
572 | ||
573 | =item B<Posting-frequency> = I<time period> | |
574 | ||
575 | The posting frequency defines how often your FAQ will be posted. | |
576 | B<yapfaq> will only post your FAQ if this period of time has passed | |
577 | since the last posting. | |
578 | ||
579 | You can declare that time period either in I<B<d>ays> or I<B<w>weeks> | |
580 | or I<B<m>onths> or I<B<y>ears>. | |
581 | ||
582 | This value must be set. | |
583 | ||
584 | =item B<Expires> = I<time period> | |
585 | ||
586 | The period of time after which your message will expire. An Expires | |
587 | header will be calculated adding this time period to today's date. | |
588 | ||
589 | You can declare this time period either in I<B<d>ays> or I<B<w>weeks> | |
590 | or I<B<m>onths> or I<B<y>ears>. | |
591 | ||
592 | This setting is optional; the default is 3 months. | |
593 | ||
594 | =item B<From> = I<author> | |
595 | ||
596 | The author of your FAQ as it will appear in the From header of the | |
597 | message. | |
598 | ||
599 | This value must be set. | |
600 | ||
601 | =item B<Subject> = I<subject> | |
602 | ||
603 | The title of your FAQ as it will appear in the Subject header of the | |
604 | message. | |
605 | ||
606 | You may use the special string C<%LM> which will be replaced with | |
607 | the contents of the Last-Modified subheader in your I<File>. | |
608 | ||
609 | This value must be set. | |
610 | ||
611 | =item B<NGs> = I<newsgroups> | |
612 | ||
613 | A comma-separated list of newsgroup(s) to post your FAQ to as it will | |
614 | appear in the Newsgroups header of the message. | |
615 | ||
616 | This value must be set. | |
617 | ||
618 | =item B<Fup2> = I<newsgroup | poster> | |
619 | ||
620 | A comma-separated list of newsgroup(s) or the special string I<poster> | |
621 | as it will appear in the Followup-To header of the message. | |
622 | ||
623 | This setting is optional. | |
624 | ||
625 | =item B<MID-Format> = I<pattern> | |
626 | ||
627 | A pattern from which the message ID is generated as it will appear in | |
628 | the Message-ID header of the message. | |
629 | ||
630 | You may use the special strings C<%n> for the I<Name> of your project, | |
631 | C<%d> for the date the message is posted, C<%m> for the month and | |
632 | C<%y> for the year, respectively. | |
633 | ||
634 | This value must be set. | |
635 | ||
636 | =item B<Supersede> = I<yes> | |
637 | ||
638 | Add Supersedes header to the message containing the Message-ID header | |
639 | of the last posting. | |
640 | ||
641 | This setting is optional; you should set it to yes or leave it out. | |
642 | ||
643 | =item B<ExtraHeader> = I<additional headers> | |
644 | ||
645 | The contents of I<ExtraHeader> is added verbatim to the headers of | |
646 | your message so you can add custom headers like Approved. | |
647 | ||
648 | This setting is optional. | |
649 | ||
650 | =back | |
651 | ||
652 | =head2 Example configuration file | |
653 | ||
654 | # name of your project | |
655 | Name = 'testpost' | |
656 | ||
657 | # file to post (complete body and pseudo-headers) | |
658 | # ($File.cfg contains data on last posting and last MID) | |
659 | File = 'test.txt' | |
660 | ||
661 | # how often your project should be posted | |
662 | # use (d)ay OR (w)eek OR (m)onth OR (y)ear | |
663 | Posting-frequency = '1d' | |
664 | ||
665 | # time period after which the posting should expire | |
666 | # use (d)ay OR (w)eek OR (m)onth OR (y)ear | |
667 | Expires = '3m' | |
668 | ||
669 | # header "From:" | |
670 | From = 'test@domain.invalid' | |
671 | ||
672 | # header "Subject:" | |
673 | # (may contain "%LM" which will be replaced by the contents of the | |
674 | # Last-Modified pseudo header). | |
675 | Subject = 'test noreply ignore' | |
676 | ||
677 | # comma-separated list of newsgroup(s) to post to | |
678 | # (header "Newsgroups:") | |
679 | NGs = 'de.test' | |
680 | ||
681 | # header "Followup-To:" | |
682 | Fup2 = 'poster' | |
683 | ||
684 | # Message-ID ("%n" is $Name) | |
685 | MID-Format = '<%n-%d.%m.%y@domain.invalid>' | |
686 | ||
687 | # Supersede last posting? | |
688 | Supersede = yes | |
689 | ||
690 | # extra headers (appended verbatim) | |
691 | # use this for custom headers like "Approved:" | |
692 | ExtraHeader = 'Approved: moderator@domain.invalid | |
693 | X-Header: Some text' | |
694 | ||
695 | # other projects may follow separated with "=====" | |
696 | ===== | |
697 | ||
698 | Name = 'othertest' | |
699 | File = 'test.txt' | |
700 | Posting-frequency = '2m' | |
701 | From = 'My Name <my.name@domain.invalid>' | |
702 | Subject = 'Test of yapfag <%LM>' | |
703 | NGs = 'de.test,de.alt.test' | |
704 | Fup2 = 'de.test' | |
705 | MID-Format = '<%n-%m.%y@domain.invalid>' | |
706 | Supersede = yes | |
707 | ||
708 | Information about the last post and about how to form message IDs for | |
709 | posts is stored in a file named F<I<project name>.cfg> which will be | |
710 | generated if it does not exist. Each of those status files will | |
711 | contain two lines, the first being the date of the last time the FAQ | |
712 | was posted and the second being the message ID of that incarnation. | |
713 | ||
714 | =head1 OPTIONS | |
715 | ||
716 | =over 3 | |
717 | ||
a052296f TH |
718 | =item B<-V> (version) |
719 | ||
720 | Print out version and copyright information on B<yapfaq> and exit. | |
721 | ||
272b0243 TH |
722 | =item B<-h> (help) |
723 | ||
ae3b1b79 | 724 | Print this man page and exit. |
272b0243 TH |
725 | |
726 | =item B<-v> (verbose) | |
727 | ||
728 | Print out status information while running to STDOUT. | |
729 | ||
730 | =item B<-p> (post unconditionally) | |
731 | ||
732 | Post (all) FAQs unconditionally ignoring the posting frequency setting. | |
733 | ||
734 | You may want to use this with the B<-f> option (see below). | |
735 | ||
736 | =item B<-d> (dry run) | |
737 | ||
738 | Start B<yapfaq> in simulation mode, i.e. don't post anything and don't | |
739 | update any status information. | |
740 | ||
741 | =item B<-t> I<newsgroup(s) | CONSOLE> (test) | |
742 | ||
743 | Don't post to the newsgroups defined in F<yqpfaq.cfg>, but to the | |
744 | newsgroups given after B<-t> as a comma-separated list or print the | |
745 | FAQs to STDOUT separated by lines of dashes if the special string | |
746 | C<CONSOLE> is given. This can be used to preview what B<yapfaq> would | |
747 | do without embarassing yourself on Usenet. The status files are not | |
748 | updated when this option is given. | |
749 | ||
750 | You may want to use this with the B<-f> option (see below). | |
751 | ||
752 | =item B<-f> I<project name> | |
753 | ||
754 | Just deal with one FAQ only. | |
755 | ||
756 | By default B<yapfaq> will work on all FAQs that are defined in | |
757 | F<yapfaq.cfg>, check whether they are due for posting and - if they | |
758 | are - post them. Consequently when the B<-p> option is set all FAQs | |
759 | will be posted unconditionally. That may not be what you want to | |
760 | achieve, so you can limit the operation of B<yapfaq> to the named FAQ | |
761 | only. | |
762 | ||
763 | =back | |
764 | ||
765 | =head1 EXAMPLES | |
766 | ||
767 | Post all FAQs that are due for posting: | |
768 | ||
769 | yapfaq | |
770 | ||
771 | Do a dry run, showing which FAQs would be posted: | |
772 | ||
773 | yapfaq -dv | |
774 | ||
775 | Do a test run and print on STDOUT what the FAQ I<myfaq> would look | |
776 | like when posted, regardless whether it is due for posting or not: | |
777 | ||
778 | yapfaq -pt CONSOLE -f myfaq | |
779 | ||
780 | Do a "real" test run and post the FAQ I<myfaq> to I<de.test>, but only | |
781 | if it is due: | |
782 | ||
783 | yapfaq -t de.test -f myfaq | |
784 | ||
785 | =head1 ENVIRONMENT | |
786 | ||
787 | There are no special environment variables used by B<yapfaq>. | |
788 | ||
789 | =head1 FILES | |
790 | ||
791 | =over 4 | |
792 | ||
793 | =item F<yapfaq.pl> | |
794 | ||
795 | The script itself. | |
796 | ||
797 | =item F<yapfaq.cfg> | |
798 | ||
799 | Configuration file for B<yapfaq>. | |
800 | ||
801 | =item F<*.cfg> | |
802 | ||
803 | Status data on FAQs. | |
804 | ||
805 | The status files will be created on successful posting if they don't | |
806 | already exist. The first line of the file will be the date of the last | |
807 | time the FAQ was posted and the second line will be the message ID of | |
808 | the last post of that FAQ. | |
809 | ||
810 | =back | |
811 | ||
812 | =head1 BUGS | |
813 | ||
814 | Many, I'm sure. | |
815 | ||
816 | =head1 SEE ALSO | |
817 | ||
818 | L<http://th-h.de/download/scripts.php> will have the current | |
819 | version of this program. | |
820 | ||
821 | =head1 AUTHOR | |
822 | ||
823 | Thomas Hochstein <thh@inter.net> | |
824 | ||
825 | Original author (until version 0.5b from 2003): | |
826 | Marc Brockschmidt <marc@marcbrockschmidt.de> | |
827 | ||
828 | ||
829 | =head1 COPYRIGHT AND LICENSE | |
830 | ||
831 | Copyright (c) 2003 Marc Brockschmidt <marc@marcbrockschmidt.de> | |
832 | ||
833 | Copyright (c) 2010 Thomas Hochstein <thh@inter.net> | |
834 | ||
835 | This program is free software; you may redistribute it and/or modify it | |
836 | under the same terms as Perl itself. | |
837 | ||
838 | =cut |