#!/usr/local/bin/perl # Copyright (c) 2005-2008 Frédéric Senault. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of the author or any contributors may be used to # endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AHTOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. use strict; #Commande rpost my($rpost)='/news/tools/rpost localhost -M'; #Répertoire temporaire my($tmpdir)='/news/tmp'; #Répertoire de datas pour autopost my($path)='/news/autopost'; #From par défaut my($from)='"F. Senault" '; #Proprio du robot my($sender)='"F. Senault" '; #Subject par défaut my($subject)='Autopost'; #Domaine local (pour la seconde partie du message-id) my($domaine)='talisker.lacave.net'; #Fichier log my($log)='/var/log/news/autopost'; ############################################################################## my($autopostv)="1.0"; my($fch)=shift; my($debug)=defined(shift); my($ng,$org,$expire); my($doexp)=0; my($now); my($followup)="poster"; my($version)=""; my($die,@msg,$s); my($nump,$mid,$oldmid); my(@head,$h); $now=localtime; open(FCH,">> $log") or die("Erreur d'ouverture du log. ($!)"); print FCH "********** $$ - $now : $fch, $subject$version\n"; &mydie(\*FCH,"Fichier $path/msg/$fch inexistant.") unless (-e "$path/msg/$fch"); if(-e "$path/msginfo/$fch.mid") { open(MID,"$path/msginfo/$fch.mid") or &mydie(\*FCH,"Erreur d'ouverture du $path/msginfo/$fch.mid."); $nump=; close(MID); chomp($nump); $oldmid="<$fch.$nump\@$domaine>"; $nump++; $mid="<$fch.$nump\@$domaine>"; } else { $oldmid=""; $nump=1; $mid="<$fch.$nump\@$domaine>"; } if(-e "$path/edito/$fch.edito") { open(EDI,"$path/edito/$fch.edito") or &mydie(\*FCH,"Erreur d'ouverture du fichier $path/edito/$fch.edito."); @msg=; close(EDI); my($jour,$mois,$an); (undef,undef,undef,$jour,$mois,$an)=localtime; $mois++; $an+=1900; rename("$path/edito/$fch.edito","$path/edito/old/$fch.edito.$an$mois$jour"); } open(IN,"$path/msg/$fch") or &mydie(\*FCH,"Erreur d'ouverture du fichier $path/msg/$fch."); $s=0; while($_=){ if($s==1) { if(/^$/) { open(INC,$1) or &mydie(\*FCH,"Erreur d'ouverture du fichier lié $1."); push(@msg,); close(INC); } else { push(@msg,$_); } } else { if(/^\n/) { $s=1; } elsif(/^N(ewsgoups)*: (.*)/) { $ng=$2; } elsif(/^F(rom)*: (.*)/) { $from=$2; } elsif(/^S(ubject)*: (.*)/) { $subject=$2; } elsif(/^Followup-To: (.*)/ || /^FU2: (.*)/) { $followup=$1; } elsif(/^V(ersion)*: (.*)/) { $version=" ($2)"; } elsif(/^O(rganization)*: (.*)/) { $org=$2; } elsif(/^H(eader)*: (.*)/) { push(@head,$2); } elsif(/^E(xpires)*/) { $doexp=1; } } } close(IN); if($doexp) { if(-e "$path/msginfo/$fch.exp") { open(EXP,"$path/msginfo/$fch.exp") or &mydie(\*FCH,"Erreur d'ouverture du $path/msginfo/$fch.exp."); $expire=; close(EXP); chomp($expire); } else { $doexp=0; } } open(OUT,"> $tmpdir/autopost$$") or &mydie(\*FCH,"Erreur d'ouverture du fichier temporaire."); print OUT "From: $from\n"; print OUT "Sender: $sender\n" unless ($sender eq $from); print OUT "Organization: $org\n" if(defined($org)); print OUT "Subject: $subject$version\n"; if($debug) { print OUT "Newsgroups: lacave.test\n"; } else { print OUT "Newsgroups: $ng\n"; } print OUT "Followup-To: $followup\n"; print OUT "Message-ID: $mid\n"; print OUT "Supersedes: $oldmid\n" unless ($nump==1); print OUT "Expires: $expire\n" if ($doexp); print OUT "User-Agent: Autopost v$autopostv\n"; print OUT join("\n",@head)."\n" unless (scalar @head==0); print OUT "\n"; print OUT join("",@msg); close(OUT); close(FCH); $s=system("$rpost >>$log 2>&1 <$tmpdir/autopost$$"); open(FCH,">> $log") or die("Erreur d'ouverture du log. ($!)"); if($s==-1) { print FCH "* Erreur -1 : erreur fatale.\n"; } elsif($s==1) { print FCH "* Erreur 1 : erreur en postant.\n"; } elsif($s==2) { print FCH "* Erreur 2 : erreur d'authentification.\n"; } elsif($s==3) { print FCH "* Erreur 3 : réponse inattendue.\n"; } unlink("$tmpdir/autopost$$"); if($s==0) { open(MID,"> $path/msginfo/$fch.mid") or &mydie(\*FCH,"Erreur lors de la sauvegarde dans $path/msginfo/$fch.mid."); print MID "$nump"; close(MID); } print FCH "********** $$ - fin\n"; close(FCH); sub mydie { my($fh)=shift; my($msg)=shift; print $fh "* $msg ($!)\n"; print $fh "********** $$ - fin anormale\n"; close($fh); die("$msg ($!)"); }