#!/usr/local/bin/perl
#
#                           beeper.pl
#
#              written by Ken Stevens <children@empire.net>
#
#                          31 May 1995
#
# DESCRIPTION
# This perl script logs into a game using pei and checks to see if you have
# any telegrams.  If you do, it reads the telegrams and then mails them to
# you using the standard unix mailer.  If you don't have any telegrams,
# then it waits $minutes minutes and then tries again.  beeper.pl does
# not delete your telegrams.  (It answers 'n' to the prompt.)
#
# You can run beeper.pl in the background while you're connected.  It will
# just try to connect, see that you're using it, and politely exit and try
# again later.
#
# INSTALLATION
# STEP I.
# Set the following five variables:

$minutes = 10;			# How many minutes to wait between tries
				# WARNING: Do not set this too small.
$game = "chi";			# In pei, the name of your game
$pei = "/home/children/bin/pei";# The location of pei on your system
$email = "children";            # Your email address
$mailx = "/bin/mailx";		# The location of mailx on your system
				# type "which mailx" to find it
#
# STEP II.
# In unix, type:
#   chmod +x beeper.pl
# where beeper.pl is the name of this file
#
# HOW TO RUN IT:
# In unix, type:
#   beeper.pl &
#
# BUG REPORTS:
# mail your bug-reports and comments to:
# children@empire.net

# FURTHER CONFIGURATION
# If you're running beeper.pl in the foreground, you may wish to set
# $no_mail = 1;
# This will prevent beeper.pl from sending you mail and exiting when it
# finds a telegram.  This will also cause beeper.pl to print a one line
# status message every time it checks your telegrams, and will "beep" your
# terminal when it sees that you have a telegram.
# If you wish all of the features of $no_mail = 1 PLUS you'd like your
# telegram printed on your terminal, then you should set:
# $no_mail = 2;
#
# To print pei's login session, set
# $debug = 1;
#
# --- YOU SHOULD NOT NEED TO CHANGE ANYTHING BELOW THIS LINE ---

die "No pei\n" unless -x $pei;
die "No mailx\n" unless -x $mailx;

for (;;) {
  &check_telegrams;
}

sub check_telegrams {
  local ($status, $connected, $unconnected);

  open(PEI,"echo 'read; ?n
bye' | $pei -n $game 2>&1 |") || die "Can't run pei\n";
  while (<PEI>) {
    print if $debug;
    &status;
    last if /Perl Empire Interface/;
  }
  while (<PEI>) {
    print if $debug;
    &status;
    &forward_mail_and_exit if /^> BULLETIN/;

# Delete the following line if you're only interested in bulletins.
    &forward_mail_and_exit if /^> Telegram/;

    last if /^No telegrams for you at the moment\.\.\.\n$/
  }
  if ($no_mail) {
    if (defined $status) {
      print ">>\u$status<<\n";
    } elsif ($unconnected || !$connected) {
      print ">>Unable to connect<<\n";
    } else {
      print;
    }
  }
  close PEI;
  sleep ($minutes * 60);
}

sub forward_mail_and_exit {
  local ($subject, $tele);

  if (/> BULLETIN\s+from\s+(\S+),\s+\(\#0\)\s+dated\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d\d:\d\d):\d\d\s+\d\d\d\d/) {
    $subject = "BULL $5 $2 $3 $4";
  } elsif (/> Telegram\s+from\s+(\S+),\s+\(\#\d+\)\s+dated\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d\d:\d\d):\d\d\s+\d\d\d\d/) {
    $subject = "Tele $1 $5 $2 $3 $4";
  } else {
    $subject = "$game telegram";
  }
  if ($no_mail == 1) {
    print "$subject\07\n";
    return;
  }
  $tele = $_;
  while (<PEI>) {
    print if $debug;
    $tele .= $_;
    last if /^Into the shredder, boss? n\n$/ ||
            /Can I throw away these old love letters? n\n$/;
  }
  $tele =~ s/\s+$//;
  if ($no_mail == 2) {
    print "$tele\07\n";
    return;
  }
  close PEI;
  open(MAILX, "| $mailx -s '$subject' $email") ||
    die "Can't run mailx\n";
  print MAILX "$tele\n";
  close MAILX;
  exit 0;
}

sub status {
  if (/Empire hours restriction in force/) {
    $status = "closed";
    next;
  }
  if (/Game is probably not up\./) {
    $status = "down";
    next;
  }
  if (/Network problems\?/) {
    $status = "linkdead";
    next;
  }
  if (/Good luck\./) {
    $status = "unreachable";
    next;
  }
  if (/^Connected!/) {
    $connected = 1;
    next;
  }
  if (/Not a local command, and not connected to a game\./) {
    $unconnected = 1;
    next;
  }
  if (/^country in use by/) {
    $status = "in use";
    next;
  }
}
