Category: Email
Author/Contact Info Robert Rendler <rendler at iinet dot net dot au>
Description: Another one of those pesky things that randomizes your ~/.signature file.
#!/usr/bin/perl -w
#
# Signatures should be seperated by a % on it's own line, eg:
#
#   I like pies in the sky.
#   %
#   Milk and cookies are also nice.
#   
#   Especially cold milk.
#   %
#   JAPH
#   %
#   <% `uptime` %>
#

use strict;
use Fcntl qw(:DEFAULT :flock);

my $signature = "$ENV{HOME}/.signature";   # The FIFO.
my $sigs_data = "$ENV{HOME}/.randsig.dat"; # Sigs go in here.
my $pid_file  = "$ENV{HOME}/.randsig.pid";
my $seperator = "%";


# See if the randsig.pid file exists, and whether or not
# the file has a lock on it (just to check to see if randsig
# is already running. If it is locked then exit gracefully.
if (-e $pid_file) {
    sysopen PID_F, $pid_file, O_RDWR|O_CREAT or die "Couldn't open '$p
+id_file': $!\n";
    exit unless flock PID_F, LOCK_EX|LOCK_NB;
    print PID_F $$ + 1; # I know... but I'm just too lazy.
}


$0 =~ s#.*/##;
print "$0: going into background.\n";
fork && exit;


my ($l_mod, @sigs) = 0;
while (1) {
    # Compare last modified timestamps of the sigs data file.
    my $n_mod = (stat($sigs_data))[9] || $l_mod;
    if ($n_mod != $l_mod) {
    $l_mod  = $n_mod;

    @sigs   = get_sigs();
    }


    my $sig = $sigs[rand @sigs];
    $sig =~ s#<%\s+(.*?)\s+%>#eval $1#eg;

    sysopen SIG_F, $signature, O_WRONLY|O_TRUNC or die "Couldn't open 
+'$signature': $!\n";
    print SIG_F $@ ? $@ : $sig;
    close SIG_F;

    sleep 1;
}


sub get_sigs {
    sysopen SIGS_F, $sigs_data, O_RDONLY or die "Couldn't open '$sigs_
+data': $!\n";
    local $/ = "\n$seperator\n";

    return map { s#^\n##; s#%$##; s#\n$##g; $_ } <SIGS_F>;
}