Article View: news.software.readers
Article #15517Re: TRN: change .signature by newsgroup
From: Tom Christiansen
Date: Thu, 23 Feb 1995 21:32
Date: Thu, 23 Feb 1995 21:32
158 lines
4982 bytes
4982 bytes
In news.software.readers,
twpierce@midway.uchicago.edu (Tim Pierce) writes:
:Presented without warranty: you gets what you pays for. Any
:elegance, cleverness or beauty in this script is entirely
:the fault of Tom Christiansen, and it's a wonder it survived
:until the present day.
Hm.. hm...
Here's the current version in use as we speak. Somewhere there's
a version that build Markov chains to figure the best signature,
but that's a little slower. :-)
--tom
#!/usr/bin/perl
#
# sigrand -- supply random fortunes for .signature file
################################################################
# begin configuration section
$MKNOD = "/etc/mknod";
$SIG{'PIPE'} = 'IGNORE'; # idiot vi
&gethome;
# for rec/humor/funny instead of rec.humor.funny
$NG_IS_DIR = 1;
$FULLNAME = "$HOME/.fullname";
$FIFO = "$HOME/.signature";
$ART = "$HOME/.article";
$NEWS = "$HOME/News";
$SIGS = "$NEWS/SIGNATURES";
$SEMA = "$HOME/.sigrandpid";
$GLOBRAND = 1/4; # chance to use global sigs anyway
# end configuration section -- HOME and FORTUNE get autoconf'd
################################################################
&setup;
&justme;
#fork && exit; # be a daemon
open (SEMA, ">$SEMA") || die "can't write $SEMA: $!";
print SEMA "$$\n";
close SEMA || die "can't close $SEMA: $!";
MAIN: while (1) {
open (FIFO, "> $FIFO") || die "can't write $FIFO: $!";
&load_sigs;
$sig = $sig[rand @sig];
print FIFO $name, "\n" x (3 - ($sig =~ tr/\n//)), $sig;
close FIFO;
sleep 2; # to avoid dup sigs
}
die "NOT REACHED";
################################################################
sub setup {
if (-e $FULLNAME) {
$name = `cat $FULLNAME`;
die "$FULLNAME should contain only 1 line, aborting"
if $name =~ tr/\n// > 1;
} else {
chop($host = `hostname`);
($host) = gethostbyname($host) unless $host =~ /\./;
$user = $ENV{USER} || $ENV{LOGNAME} || $pw[0] || die "intruder alert";
($name = $pw[6]) =~ s/,.*//;
$name =~ s/&/\u\L$user/g; # can't believe some folks still do this
$name = "\t$name\t$user\@$host\n";
}
&check_fortunes if !-e $SIGS;
unless (-p $FIFO) {
if (!-e _) {
system("$MKNOD $FIFO p") && die "can't mknod $FIFO";
warn "created $FIFO as a named pipe\n";
} else {
die "$0: won't overwrite file .signature\n";
}
}
srand(time|$$);
}
sub load_sigs {
local($sigfile) = &ng;
if (!-e $sigfile) {
@sig = (&fortune);
return;
}
open (SIGS,$sigfile) || die "can't open $sigfile";
local($/) = "%%\n";
undef @sig;
while (<SIGS>) {
s!$/$!!o;
push(@sig,$_);
}
close SIGS;
}
sub ng {
(rand(1.0) > ($GLOBRAND) && open ART) || return $SIGS;
local($/) = '';
$_ = <ART>;
($ng) = /Newsgroups:\s*([^,\s]*)/;
$ng =~ s!\.!/!g if $NG_IS_DIR; # if rn -/, or SAVEDIR=%p/%c
$ng = "$NEWS/$ng/SIGNATURES";
-f $ng ? $ng : $SIGS;
}
sub fortune {
local($_, $tries);
do {
$_ = `$FORTUNE -s`;
} until tr/\n// < 4 || $tries++ > 20;
local($*) = 1;
s/^/ /g;
$_ || " SIGRAND: deliver random signals to all processes.\n";
}
sub check_fortunes {
return if $FORTUNE;
foreach $dir (split(/:/, $ENV{PATH}), '/usr/games') {
return if -x ($FORTUNE = "$dir/fortune");
}
die "Need either $SIGS or a fortune program, bailing out";
}
sub gethome {
@pw = getpwuid($<);
$HOME = $ENV{HOME} || $ENV{LOGDIR} || $pw[7]
|| die "no home directory for user $<";
}
# "There can be only one." --the Highlander
sub justme {
if (open SEMA) {
chop($pid = <SEMA>);
kill(0, $pid) && die "$0 already running (pid $pid)\n";
#warn "COOL: no $pid: $!";
close SEMA;
}
}
--
Tom Christiansen Perl Consultant, Gamer, Hiker tchrist@mox.perl.com
Ivy, privy, famous; clamour
And enamour rhyme with hammer.
Technical Details
Message-ID:
<3iiuts$b2i@csnews.cs.Colorado.EDU>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!nntp.gmd.de!news.rwth-aachen.de!news.rhrz.uni-bonn.de!news.uni-stuttgart.de!news.belwue.de!delos.stgt.sub.org!delos.stgt.sub.org!news.maz.net!pipex!oleane!jussieu.fr!math.ohio-state.edu!howland.reston.ans.net!news.sprintlink.net!uunet!boulder!csnews!mox!tchristReferences:
<3iearp$227@news.cais.com> <D4FC9H.1qG@midway.uchicago.edu>Show Full Headers
Complete Article Headers
Message-ID: <3iiuts$b2i@csnews.cs.Colorado.EDU> Subject: Re: TRN: change .signature by newsgroup Date: Thu, 23 Feb 1995 21:32:44 +0000 From: Tom Christiansen <tchrist@mox.perl.com> References: <3iearp$227@news.cais.com> <D4FC9H.1qG@midway.uchicago.edu> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!nntp.gmd.de!news.rwth-aachen.de!news.rhrz.uni-bonn.de!news.uni-stuttgart.de!news.belwue.de!delos.stgt.sub.org!delos.stgt.sub.org!news.maz.net!pipex!oleane!jussieu.fr!math.ohio-state.edu!howland.reston.ans.net!news.sprintlink.net!uunet!boulder!csnews!mox!tchrist Newsgroups: news.software.readers Organization: Perl Consulting and Training Lines: 158 Reply-To: tchrist@mox.perl.com (Tom Christiansen) NNTP-Posting-Host: perl.com Keywords: trn signature macro Originator: tchrist@mox