home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / question / 9336 < prev    next >
Encoding:
Text File  |  1992-07-23  |  4.3 KB  |  170 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!cs.utexas.edu!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Fortunes
  5. Message-ID: <1992Jul23.143441.11923@news.eng.convex.com>
  6. Originator: tchrist@pixel.convex.com
  7. Sender: usenet@news.eng.convex.com (news access account)
  8. Nntp-Posting-Host: pixel.convex.com
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. Organization: CONVEX Realtime Development, Colorado Springs, CO
  11. References: <1992Jul23.023102.11675@polari>
  12. Date: Thu, 23 Jul 1992 14:34:41 GMT
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 152
  17.  
  18. From the keyboard of sparky@polari.online.com:
  19. :Don't know if this is the correct place to post this question, but...
  20. :
  21. :I know there is a way to create one's own "fortune" file for use in
  22. :mail and messages.  How is this done, and how can I make mush, for
  23. :example, use *my* fortune file instead of the default one?
  24.  
  25. This way:
  26.  
  27. --tom
  28.  
  29.  
  30. #!/usr/bin/perl
  31. #
  32. # sigrand -- supply random fortunes for .signature file
  33.  
  34. ################################################################
  35. # begin configuration section 
  36.  
  37. $MKNOD = "/etc/mknod";
  38.  
  39. &gethome;
  40.  
  41. # for rec/humor/funny instead of rec.humor.funny
  42. $NG_IS_DIR     = 1;     
  43.  
  44. $FULLNAME     = "$HOME/.fullname";
  45. $FIFO         = "$HOME/.signature";
  46. $ART         = "$HOME/.article";
  47. $NEWS         = "$HOME/News";
  48. $SIGS         = "$NEWS/SIGNATURES";
  49. $SEMA        = "$HOME/.sigrandpid";
  50.  
  51. # end configuration section -- HOME and FORTUNE get autoconf'd
  52. ################################################################
  53.  
  54. &setup;
  55.  
  56. &justme;
  57.  
  58. fork && exit;     # be a daemon 
  59.  
  60. open (SEMA, ">$SEMA") || die "can't write $SEMA: $!";
  61. print SEMA "$$\n";
  62. close SEMA || die "can't close $SEMA: $!";
  63.  
  64. MAIN: while (1) {
  65.     open (FIFO, "> $FIFO") || die "can't write $FIFO: $!";
  66.     &load_sigs;
  67.     $sig = $sig[rand @sig];
  68.     print FIFO $name, "\n" x (3 - ($sig =~ tr/\n//)), $sig;
  69.     close FIFO;
  70.     sleep 2; # to avoid dup sigs
  71. }
  72.  
  73. ################################################################
  74.  
  75. sub setup {
  76.     if (-e $FULLNAME) {
  77.     $name = `cat $FULLNAME`;
  78.     die "$FULLNAME should contain only 1 line, aborting" 
  79.         if $name =~ tr/\n// > 1;
  80.     } else {
  81.     chop($host = `hostname`);
  82.     ($host) = gethostbyname($host) unless $host =~ /\./;
  83.     $user = $ENV{USER} || $ENV{LOGNAME} || $pw[0] || die "intruder alert";
  84.     ($name = $pw[6]) =~ s/,.*//;
  85.     $name =~ s/&/$user/g; # can't believe some folks still do this
  86.     $name = "\t$name\t$user\@$host\n";
  87.     } 
  88.  
  89.  
  90.     &check_fortunes if !-e $SIGS;
  91.  
  92.     unless (-p $FIFO) {
  93.     if (!-e _) {
  94.         system("$MKNOD $FIFO p") && die "can't mknod $FIFO";
  95.         warn "created $FIFO as a named pipe\n";
  96.     } else {
  97.         die "$0: won't overwrite file .signature\n";
  98.     } 
  99.     }
  100.  
  101.  
  102.     srand(time|$$);
  103. }
  104.  
  105. sub load_sigs {
  106.     local($sigfile) = &ng;
  107.     if (!-e $sigfile) {
  108.     @sig = (&fortune);
  109.     return;
  110.     } 
  111.     open (SIGS,$sigfile) || die "can't open $sigfile";
  112.     local($/) = "%%\n";
  113.     undef @sig;
  114.     while (<SIGS>) {
  115.     s!$/$!!o;
  116.     push(@sig,$_);
  117.     } 
  118.     close SIGS;
  119.  
  120. sub ng {
  121.      open ART || return $SIGS;   
  122.      local($/) = '';
  123.      $_ = <ART>;
  124.      ($ng) = /Newsgroups:\s*([^,\s]*)/;
  125.      $ng =~ s!\.!/!g if $NG_IS_DIR;     # if rn -/,  or SAVEDIR=%p/%c
  126.      $ng = "$NEWS/$ng/SIGNATURES";
  127.      -f $ng ? $ng : $SIGS;
  128.  
  129. sub fortune {
  130.    local($_, $tries);
  131.    do { 
  132.        $_ = `$FORTUNE -s`; 
  133.    } until tr/\n// < 4 || $tries++ > 20;
  134.    local($*) = 1;
  135.    s/^/ /g;
  136.    $_ || " SIGRAND: deliver random signals to all processes.\n";
  137.  
  138. sub check_fortunes {
  139.     return if $FORTUNE;
  140.     foreach $dir (split(/:/, $ENV{PATH}), '/usr/games') {
  141.     return if -x ($FORTUNE = "$dir/fortune");
  142.     } 
  143.     die "Need either $SIGS or a fortune program, bailing out";
  144.  
  145. sub gethome {
  146.     @pw = getpwuid($<);
  147.     $HOME = $ENV{HOME} || $ENV{LOGDIR} || $pw[7]
  148.                || die "no home directory for user $<";
  149. }
  150.  
  151. # "There can be only one."  --the Highlander
  152. sub justme {
  153.     if (open SEMA) {
  154.     chop($pid = <SEMA>);
  155.     kill(0, $pid) && die "$0 already running (pid $pid)\n";
  156.     #warn "COOL: no $pid: $!";
  157.     close SEMA;
  158.     } 
  159. __END__
  160. -- 
  161.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  162.  
  163. One word sums up probably the responsibility of any vice president,
  164. and that one word is 'to be prepared'.  --Vice President Dan Quayle
  165.