Re: Anyway to randomize sig files?
From Tom Christiansen <tch...@mox.perl.com>
· alt.ascii-art
· 19 Dec 1994 15:16 · View full thread
· report
#!/usr/bin/perl
#
# sigrand -- supply random fortunes for .signature file
# Tom Christiansen -- tch...@perl.com
################################################################
# begin configuration section
$MKNOD = "/etc/mknod";
$SIG{'PIPE'} = 'IGNORE';
&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 tch...@mox.perl.com
stab_val(stab)->str_nok = 1; /* what a wonderful hack! */
--Larry Wall in stab.c from the perl source code
Original message headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1012a6,29a923fbbd8ba89c X-Google-Attributes: gid1012a6,public X-Google-Thread: f996b,1bf4e46a52a72b7b X-Google-Attributes: gidf996b,public X-Google-ArrivalTime: 1994-12-19 07:16:21 PST Path: nntp.gmd.de!dearn!blekul11!idefix.CS.kuleuven.ac.be!ub4b!EU.net!Germany.EU.net! howland.reston.ans.net!news2.near.net!bloom-beacon.mit.edu!boulder!csnews!mox!tchrist From: Tom Christiansen <tch...@mox.perl.com> Newsgroups: alt.ascii-art,comp.unix.shell Subject: Re: Anyway to randomize sig files? Date: 19 Dec 1994 15:16:21 GMT Organization: Perl Consulting and Training Lines: 144 Message-ID: <3d4845$8c...@csnews.cs.Colorado.EDU> References: <345...@student.msu.edu> <3cppfl$mc...@cronkite.seas.gwu.edu> <MER...@linda.teleport.com> <3d0c28$62...@cronkite.seas.gwu.edu> Reply-To: tch...@mox.perl.com (Tom Christiansen) NNTP-Posting-Host: perl.com Originator: tchrist@mox Xref: nntp.gmd.de alt.ascii-art:18268 comp.unix.shell:17898
Related ASCII art in the Gallery
Explore these categories from our collection of 11,000+ artworks:
Make your own ASCII art
Turn images, text or 3D into ASCII, or draw your own – right in your browser.
About this message.
This message was posted publicly to the newsgroup
alt.ascii-art
in 1994 and is mirrored here unchanged as part of the Historic Archives – only email addresses are masked.
The artwork and text belong to their original authors: if you copy a piece, keep the artist's initials or signature intact and credit them where you can.
Are you the author? Contact us to get your posts attributed, connected to your artist profile, or removed.
Report this message
Help us keep the archive clean and accurate. Reports are reviewed by a person – nothing is changed automatically.