home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s053 / 7.ddi / usr / bin / notify < prev    next >
Encoding:
Text File  |  1990-12-08  |  4.9 KB  |  217 lines

  1. #!/sbin/sh
  2. #    Copyright (c) 1990 UNIX System Laboratories, Inc.
  3. #    Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T
  4. #      All Rights Reserved
  5.  
  6. #    THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
  7. #    UNIX System Laboratories, Inc.
  8. #    The copyright notice above does not evidence any
  9. #    actual or intended publication of such source code.
  10.  
  11. #ident    "@(#)/usr/bin/notify.sl 1.1 4.0 12/08/90 28808 AT&T-USL"
  12. #
  13. # Set up asynchronous notification of new incoming mail
  14. # by doing a 'mail -F "|NOTIFYPROG -m mailfile -o %R -s %S"
  15. #
  16. # notify -n ==> turn it off.
  17. # notify -y ==> turn it on. use default of $HOME/.mailfile for new mailbox.
  18. # notify -m mailfile ==> turn it on. use mailfile for new mailbox.
  19. # notify (no args) ==> Just check if currently activated and report findings.
  20. #
  21.  
  22. PATH=/usr/bin
  23. TMP=/tmp/notif$$
  24. trap "rm -f $TMP" 0
  25. NOTIFYPROG=/usr/lib/mail/notify2
  26. USAGE="\07USAGE: ${0} [[-]y/[-]n] [-m mailfile]\07"
  27. MSG1="${0}: Mail notification"
  28. MSG2="${0}: Notification cannot be installed unless "
  29. NEWMAILFILE=${HOME}/.mailfile
  30. YFLAG=0;
  31. NFLAG=0;
  32. SILENT=NO
  33. if [ ! -t 1 ]; then
  34.     # stdout not a tty. Be as silent as possible.....
  35.     SILENT=YES
  36. fi
  37.  
  38. set -- `getopt ynm: $*`
  39. if [ ${?} -ne 0 ]
  40. then
  41.     if [ ${SILENT} = NO ]
  42.     then
  43.         echo ${USAGE} 1>&2
  44.     fi
  45.     exit 2
  46. fi
  47. for arg in ${*}
  48. do
  49.     case ${arg} in
  50.     -n)    NFLAG=1; shift;;
  51.     -y)    YFLAG=1; shift;;
  52.     -m)    NEWMAILFILE=${2}; YFLAG=1; shift 2;;
  53.     --)    shift; break;;
  54.     esac
  55. done
  56. #
  57. # If any args left, assume they 'forgot' the leading dash...:-)
  58. #
  59. if [ ${#} -gt 0 ]
  60. then
  61.     case "$1" in
  62.         [Yy]* ) YFLAG=1 ;;
  63.         [Nn]* ) NFLAG=1 ;;
  64.         * )        
  65.         if [ ${SILENT} = NO ]
  66.         then
  67.             echo ${USAGE} 1>&2
  68.         fi
  69.         exit 2
  70.     esac
  71. fi
  72.  
  73. # check file name
  74. case "$NEWMAILFILE" in
  75.     /* ) ;;
  76.     * ) echo "$0: must use full path for $NEWMAILFILE" 1>&2; exit 1;;
  77. esac
  78.  
  79. # Get the name of the person from their id.
  80. # id returns: uid=unum(uname) gid=gnum(gname) ...
  81. set -- `id`
  82. case "$1" in
  83.     # If running as root, allow $LOGNAME to override.
  84.     uid=0'('*')' )    MYNAME=${LOGNAME:-`expr "$1" : ".*(\(.*\))"`} ;;
  85.     # root is not in /etc/passwd? This should never happen.
  86.     uid=0 )        MYNAME=${LOGNAME:-root} ;;
  87.     # Extract the name.
  88.     uid=*'('*')' )    MYNAME=`expr "$1" : ".*(\(.*\))"` ;;
  89.     # The user is no longer in the database? Exit if $LOGNAME isn't there.
  90.     * )            MYNAME=${LOGNAME:?No user name?} ;;
  91. esac
  92.  
  93. if [ ${NFLAG} -eq 1 -o ${YFLAG} -eq 0 ]
  94. then
  95.     if [ ! -f "/var/mail/${MYNAME}" ]
  96.     then
  97.         if [ ${SILENT} = NO ]
  98.         then
  99.             echo "${MSG1} not active"
  100.         fi
  101.         exit 0
  102.     fi
  103.     read rest < /var/mail/${MYNAME}
  104.     MFILE=`expr "x${rest}" : \
  105.         "xForward to |${NOTIFYPROG} -m \(.*\) -o %R -s %S"`
  106.     if [ "x${MFILE}" = x ]
  107.     then
  108.         if [ ${SILENT} = NO ]
  109.         then
  110.             echo "${MSG1} not active"
  111.         fi
  112.         exit 0
  113.     fi
  114.     if [ ${NFLAG} -eq 1 ]
  115.     then
  116.         #
  117.         # Turn notification facility off
  118.         #
  119.         MAIL=/var/mail/${MYNAME} mail -F "" > /dev/null 2>&1
  120.         if [ ${SILENT} = NO ]
  121.         then
  122.             echo "${MSG1} deactivated. \c"
  123.             echo "New mail will go into /var/mail/${MYNAME}"
  124.             echo "${0}: Old messages may be in '${MFILE}'"
  125.             echo "${0}: Be sure to redefine \${MAIL} appropriately"
  126.         fi
  127.         exit 0
  128.     fi
  129.     #
  130.     # Just report finding
  131.     #
  132.     if [ ${SILENT} = NO ]
  133.     then
  134.         echo "${MSG1} active."
  135.         echo "${0}: New mail messages will go to '${MFILE}'"
  136.     fi
  137.     exit 0
  138. fi
  139. #
  140. # Set up for notification. If already there and 'mailfile' is the same,
  141. # do nothing. If different, prompt user for confirmation before changing
  142. #
  143. if [ -f "/var/mail/${MYNAME}" -a -s "/var/mail/${MYNAME}" ]
  144. then
  145.     read rest < /var/mail/${MYNAME}
  146.     MFILE=`expr "x${rest}" : \
  147.         "xForward to |${NOTIFYPROG} -m \(.*\) -o%R %S"`
  148.     if [ "x${MFILE}" = x ]
  149.     then
  150.         if [ ${SILENT} = NO ]
  151.         then
  152.             echo "${MSG2} /var/mail/${MYNAME} is empty"
  153.         fi
  154.         exit 0
  155.     fi
  156.     if [ "${MFILE}" != "${NEWMAILFILE}" ]
  157.     then
  158.         if [ ${SILENT} = NO ]
  159.         then
  160.             echo "${0}: Notification already installed."
  161.             echo "${0}: Current alternate mailbox is '${MFILE}'"
  162.             echo "${0}: Do you want to change it to '${NEWMAILFILE}' \c"
  163.             echo "(y or n): \c"
  164.             read ANS
  165.             case "$ANS" in
  166.                 [Nn]* )
  167.                 echo "${0}: No change"
  168.                 exit 0
  169.                 ;;
  170.             esac
  171.         else
  172.             # Assume no change, since we can't ask the question...
  173.             exit 0
  174.         fi
  175.     fi
  176. fi
  177.  
  178. # check the new mail file
  179. if [ ! -f ${NEWMAILFILE} ]
  180. then
  181.     if  : > ${NEWMAILFILE} &&
  182.         chmod 600 ${NEWMAILFILE}
  183.     then :
  184.     else
  185.         echo "$0: cannot properly access ${NEWMAILFILE}" 1>&2
  186.         exit 1
  187.     fi
  188. elif [ ! -w ${NEWMAILFILE} ]
  189. then
  190.     echo "$0: ${NEWMAILFILE} is not writable!" 1>&2
  191.     exit 1
  192. fi
  193.  
  194. #
  195. # Just to be safe
  196. #
  197. if    MAIL=/var/mail/${MYNAME} mail -F "" > /dev/null 2>&1
  198. then    :
  199. else
  200.     echo "$0: Cannot install notification" 1>&2
  201.     exit 2
  202. fi
  203. if  MAIL=/var/mail/${MYNAME} \
  204.     mail -F "|${NOTIFYPROG} -m ${NEWMAILFILE} -o %R -s %S" > /dev/null 2>&1
  205. then    :
  206. else
  207.     echo "$0: Cannot install notification" 1>&2
  208.     exit 2
  209. fi
  210. if [ ${SILENT} = NO ]
  211. then
  212.     echo "${0}: Asynchronous 'new mail' notification installed"
  213.     echo "${0}: New mail messages will go to '${NEWMAILFILE}'"
  214.     echo "${0}: Be sure to re-define \${MAIL} appropriately"
  215. fi
  216. exit 0
  217.