Article View: news.software.readers
Article #16568Re: Fortune Cookies
From: Tom Christiansen
Date: Wed, 03 May 1995 00:00
Date: Wed, 03 May 1995 00:00
209 lines
6552 bytes
6552 bytes
Submitted-by: tchrist@mox.perl.com
Archive-name: sigrand
In comp.lang.perl, tinsel@uiuc.edu writes:
:rscott@gate.net (Ryan Scott) writes:
:> Hi, I'm looking for a short program that simply serves a random quote
:> from a flat text file, basically just a fortune cookie server. It just
:> serves one quote, I'll do a server side include to run it.
:
:I have a simple C program that could be adapted to this use. It is
:designed as a CGI program, but it should trivial to change that.
:
: http://jaka.cerl.uiuc.edu/~tinsel/files/src/tomquote2.c
Given a sigfile like
%%
150 years ago everybody was a Christian Scientist. --dmr
%%
1776 - 1984
There Is No Middle Ground
--a button from the Libertarian Party
%%
: 1. What is the possibility of this being added in the future?
In the near future, the probability is close to zero. In the distant
future, I'll be dead, and posterity can do whatever they like... :-) --lwall
%%
"36 percent of the American Public believes that boiling radioactive milk
makes it safe to drink."
--results of a survey by Jon Miller at Northern Illinois University
%%
Adding manpower to a late software project makes it later
%%
You can use this program to generate them. Sample quotes are in
ftp://ftp.perl.com/pub/quotes/*
Including a program to sort/merge/trimdup them.
You should be able to easily adapt this to a qotd server for inetd or html.
In fact, here's one:
#!/usr/bin/perl
use TCP::Server;
Daemon "qotd", sub {
exec qw{sed -e 1d -e /^$/d .signature};
};
(which is just to prove that i do know how to use sed :-).
I'll leave it up for a while on my qotd port (telnet mox.perl.com 17)
for a demo. Be apprised that it will be "weighted" to select sigs
based on which hever newsgroup I'm currently reading. :-) [Somewhere I
have the shards of a Markov chain program to do the similarity
comparison too! ]
--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
Sorry. My testing organization is either too small, or too large, depending
on how you look at it. :-)
--Larry Wall in <1991Apr22.175438.8564@jpl-devvax.jpl.nasa.gov>
Technical Details
Message-ID:
<3o8229$nr9@csnews.cs.colorado.edu>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.infosystems.www.providers.mbox.zip!not-for-mailReferences:
<3nvc1u$11k4@news.gate.net> <3o0nbh$poe@vixen.cso.uiuc.edu>Show Full Headers
Complete Article Headers
Message-ID: <3o8229$nr9@csnews.cs.colorado.edu> Subject: Re: Fortune Cookies Date: Wed, 03 May 1995 00:00:00 +0000 From: Tom Christiansen <tchrist@mox.perl.com> References: <3nvc1u$11k4@news.gate.net> <3o0nbh$poe@vixen.cso.uiuc.edu> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.infosystems.www.providers.mbox.zip!not-for-mail X-Deja-AN: 101987070 Followup-to: comp.infosystems.www.providers,comp.lang.perl,news.software.readers,alt.sources.d Organization: Perl Consulting and Training Reply-to: tchrist@mox.perl.com (Tom Christiansen) Newsgroups: comp.infosystems.www.providers,comp.lang.perl,news.software.readers,alt.sources Originator: tchrist@mox