#!/usr/bin/perl -w # -*- perl -*- use strict; # sigrand: generates random signatures use vars qw( $SEMA $QUOTES ); my $DEBUGGING = 1; # constants -- eventually pull from ~/.sigrandrc my $HOME = get_home(); my $MKNOD = "/bin/mknod"; my $FIFO = "$HOME/.signature"; # my $NAME = "\techo \"Ojkk Ufhj (oufhj@jwtqx.htr)\" | perl -pe 'tr[f-za-eF-ZA-E][a-zA-Z]'"; my $NAME = " name\n"; $NAME .= " company\n"; $NAME .= " email \n"; # limit to the number of lines in the quote my $MAX_LINES = 5; $QUOTES = "$HOME/.quotes"; $SEMA = "$HOME/.sigrandpid"; setup(); justme(); # make sure program not already running fork && exit; # background ourself and go away open(SEMA, "> $SEMA") or die "couldn't write $SEMA: $!"; print SEMA "$$\n"; close(SEMA) or warn "couldn't close $SEMA: $!"; for (;;) { open(FIFO, "> $FIFO") or die "couldn't write $FIFO: $!\n"; my $sig = pick_quote(); if ($NAME) { print FIFO $NAME, "\n", $sig; } else { print FIFO $sig; } close FIFO; select(undef, undef, undef, 0.2); } # ------------------------- # returns home) sub get_home { my $home = $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($<))[7] or die "no home directory for user $<"; return $home; } # dumps a message if debugging sub _report { print @_ if $DEBUGGING; } # sets the program up sub setup { $SIG{PIPE} = 'IGNORE'; # ignore pipe opened, then closed without reading if (-p $FIFO) { warn "$0: using existing named pipe $FIFO\n"; } elsif (-e _) { die "$0: won't overwrite file .signature\n"; } else { system("$MKNOD $FIFO p") && die "couldn't $MKNOD $FIFO"; warn "created $FIFO as a named pipe\n"; } } sub justme { if (open(SEMA)) { my $pid = ; chop $pid; kill(0, $pid) && die "$0 already running (pid $pid), bailing out"; close SEMA; } else { print STDERR "no $SEMA file opened\n"; } } # returns a quote sub pick_quote { open QUOTES or die "couldn't open $QUOTES\n"; local $/ = "%%\n"; my @quotes = ; close QUOTES; my $q; while ($q = $quotes[int(rand($#quotes))]) { chomp $q; last if $q =~ tr/\n// <= $MAX_LINES; } return $q; }