#/bin/bash
#
# dips-post - a script to post my IP address to a DIPS server
# Date: 01 Sept 1999
#
# This script requires a program called NetCat (http://www.l0pht.com/~weld/netcat/)
# or if you are running the RedHat distribution, it is included on the CD (look for nc-*.rpm)
#
# My next DIPS project, if any, would be to write a dips-post daemon in C.
# 
#				-Dave <wormfood@pig.net>
#
# (if some of my lines appear long...that is because I use 132 column screen mode :)


###########################################################################################
# This is the section that the "average" user is most
# likely to change, but probably won't need to
###########################################################################################
#
# At the time that I wrote this script the postmodem.com DNS server
# is having problems, so you'll probably want to uncomment the following
# line with the domain name and comment the line with the IP address when
# things are fixed.
#DIPSERVER=postmodem.com
DIPSERVER=36.67.0.77

# SLEEPTIME is the amount of time in minutes between postings
# do NOT set SLEEPTIME to less than 30 (read the DIPS client specs)
SLEEPTIME=30

#KILLCOUNT is the number of tries/seconds to try to "kill" another running dips-post script
KILLCOUNT=5
#LOCKDIR is the directory put the PID lock file in
LOCKDIR=/var/run
###########################################################################################
# DO NOT CHANGE ANYTHING PAST THIS POINT (unless you know what you're doing)
###########################################################################################

trap TrapAndQuit SIGUSR1
trap TrapAndQuit EXIT
trap TrapAndQuit SIGHUP
trap TrapAndQuit SIGINT
trap TrapAndQuit SIGQUIT
trap TrapAndQuit SIGKILL

function main ()
{
  if [ $# != 4 ]; then
    Usage
  fi

  CheckAndClearPIDFile $*
  MakePIDFile $*

  while :; do
    if CheckPIDFile $*; then
      if DeviceConnected $*; then
        PostToDIPS $*
      else
        ClearPIDFile $*
        Quit
      fi
    else
      Quit
    fi
  done

#  stay in a loop until one of the 3 following conditions:
#  1) PID Lock file changes (means another copy of this script is running)
#  2) Interface is no longer active (usually ppp0)
#  3) We trap a SIGNAL

}

function CheckPIDFile ()
{
# Check to see if we created the current lock file

  if [ -f $LOCKDIR/dips-post.$1.pid ]; then
    if let `cat $LOCKDIR/dips-post.$1.pid`==$$; then
      return 0
    else
      return 1
    fi
  else
    return 1
  fi
}


function DeviceConnected ()
{
# Check to see if the device is still connected

  if ifconfig $1 &>/dev/null; then
    return 0
  else
    return 1
  fi
}


function TrapAndQuit
{
# Function to trap SIGUSR1 and other signals.
# A signal of SIGUSR1 is a signal to shutdown and delete
# our PIDFile, if it is not our PIDFile, then just exit.
# We use a SIGUSR1 so that if we send the signal to the
# wrong program, it shouldn't kill it. (but this should never happen)

  if [ CheckPIDFile ]; then
    ClearPIDFile
    Quit
  else
    Quit
  fi
}


function Quit ()
{
  exit 0
}


function Usage ()
{
  echo dips-post '{Interface} {IPAddress} {DIPSPassword} {DIPSTopic}'
  echo 
  echo Where:
  echo '{interface}' is the network interface '(usually ppp0)'
  echo '{IPAddress}' is your dotted quad IP address
  echo '{DIPSPassword}' is your DIPS password '(duh)'
  echo '{DIPSTopic}' is your topic to show on the DIPS list
  exit 1
}


function MakePIDFile ()
{
  if [ -f $LOCKDIR/dips-post.$1.pid ]; then
    return 1
  else
    echo $$>$LOCKDIR/dips-post.$1.pid
    return 0
  fi
}


function ClearPIDFile ()
{
# We want to make sure the lock file really exisit before we delete it
# because it may get deleted before we get here.

  if [ -f $LOCKDIR/dips-post.$1.pid ]; then
    rm -f $LOCKDIR/dips-post.$1.pid
  fi

  return 0
}

function CheckAndClearPIDFile ()
{
# It Should be safe to delete the lock file if we can't kill the
# program from here. There should only be one copy of this script
# running at any given time. When we start up we should be the only
# one running. The lock file may be left over after a crash.
# we can also make sure we remove any lock files from the startup
# scripts '(probably rc.local would be best)' but the technique
# I'm using will require the least modifications to any "system" files.

  if  CheckPIDFile $*; then
    ClearPIDFile $*
    return 0
  else
    while let $[$SECONDS<$[KILLCOUNT]]; do
      if [ -f $LOCKDIR/dips-post.$1.pid ]; then
        if kill -SIGUSR1 `cat $LOCKDIR/dips-post.$1.pid` &>/dev/null; then
          sleep 1s
        else
          break 2
        fi
      else
        return 0
      fi
    done
    ClearPIDFile $*
  fi
}

function PostToDIPS ()
{
#  date
  echo -e GET /dips-admin/update.cgi?pwd=$3"&"ip=$2"&"topic=$4"\n"HTTP/1.0"\n"Referer: dips-post"\n"Connection:Keep-Alive"\n"User-Agent: dips-post"\n"Host: $2"\n"Accept: */*|nc $DIPSERVER 80
  sleep $[$SLEEPTIME]m
}

main $*
