Subject: Re: New mail script?

From: [email-masked] (Michael J Hannah)
Date: Mon, 11 Apr 1994 14:31:33 +0700

----------
X-Sun-Data-Type: text
X-Sun-Data-Description: text
X-Sun-Data-Name: text
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 43

++ Date: Mon, 11 Apr 1994 13:31 -0300 (BST)
++ From: Robert Nicholson <[email-masked]>
++ Subject: New mail script?
++ Resent-Sender: [email-masked]
++ 
++ I filter mail using Procmail so most of it is directed to folders
++ before I've read it.
++ 
++ Anybody written a script to point out the new mail in any folders?
++ 
++ Newmail could be anything that doesn't have a Status: header...
++ 

As a diehard Anglophile, always happy to help a Brit.  :-)

Yes, I do this all the time.  My design was to have a script that
could be invoked by cron with argument(s) being the name(s) of
mail folder(s) to be examined.  That way I can set up cron entries so
that a given folder is only examined as often as I wish.  Some I check
only once a week, others once or twice a day, etc.  The "notification"
that I use is to mail a message to myself whose text is the name(s)
of the folder(s) that have new or unread mail.  This works since my
file .procmailrc directs all mail NOT from me to folders, so when my
mailtool beeps in my window, I know it is a notice.

The actual implementation is one main script to do the work, and
a second short script invoked by formail on all the messages in the folder.
I also do the appropriate (and necessary) file locking on the folders.
It would have been cleaner to use mailx for the second action, but
there is currently a bug in SunOS mailx which prevents using it for this.
Your mail interface program may work as described in the comments.
The script creates a "flag" file that indicates when notification has
been sent.  If the modification date of the folder is older than the
"flag" file, then notice has been sent, and further notice is unnecessary.
Otherwise you would get "perpetual" notices.  Notice can be "forced"
with a special argument to the main script.  I do that once a week
on all my folders just to nag myself to read my unread mail.

Please be tolerant of the copyright notices, they are required by my
company on anything that we produce on company time.

Michael

----------
X-Sun-Data-Type: shell-script
X-Sun-Data-Description: shell-script
X-Sun-Data-Name: export.teststatus
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 134

#!/bin/sh
#
# Copyright (C) 1994 Sandia Corporation.
# This software may be used royalty-free, for research purposes only,
# so long as this copyright notice is retained in all copies.
# This software was developed at Sandia National Laboratories,
# which is managed and operated by Sandia Corporation,
# a subsidiary of Martin Marietta Corporation,
# as part of work sponsored by an agency of the United States Government.
# Neither the Unites States Government, nor any agency thereof,
# nor any of their employees, nor any of their contractors,
# subcontractors, or their employees, makes any warranty,
# express or implied, or assumes any legal liability or responsibility
# for the accuracy, completeness, or usefulness of this software.
#
# Michael J. Hannah, Scientific Computing Systems
# [email-masked]
#
# The following directory locations should be changed as appropriate.
# The directories identified work on my Solaris 2.2 system.
#
FORMAIL=/usr/local/bin/formail
TESTSTATUS=$HOME/test.status
MAILDIR=$HOME/maildir
MAILER=/usr/bin/mailx
# need SysV style echo
ECHO=/usr/bin/echo
CAT=/usr/bin/cat
FIND=/usr/bin/find
EGREP=/usr/bin/egrep
# need -f since lockfiles are mode 000
RM="/usr/bin/rm -f"
ME=$USER
LOCKEXT=".lock"
NEWEXT=".sent_new"
LOCKFILE="/usr/local/bin/lockfile -r2 -l300"
umask 077

FORCE="no"
if [ "$1" = "FORCE" ]; then
  shift
  FORCE="yes"
fi
# The FORCE flag will check and send a message
# regardless of the status of the NEWEXT files

# input folders are parameters so can have
# different cron checks for each folder
NEWMAIL=""

# should always have folders since invoked by cron,
# but check to be thorough, and let cron mail me the error
if [ -z "${1}" ]; then
	$ECHO $0 ERROR: no folders specified
	exit 1
fi

cd $MAILDIR
# Since I do this cd I can have just the folder names as arguments

for i in $* ; do
# only bother if folder exists and is non-null
  if [ -s $i ]; then
# first lock this folder, to prevent conflict from procmail
    if $LOCKFILE ${i}${LOCKEXT} 2>/dev/null
    then
     trap "$RM ${i}${LOCKEXT}" 1 2 3 15
# now check for new and unread messages

# due to a current bug in SunOS mailx program
# since this is not the "system mailbox"
# mailx will not flag anything as "new" only as "unread"
# unfortunately, "new" is not flagged as "unread"
# so the following simpler attempt is unsuccessful
# FLAGS=`$ECHO "set screen=3 \n headers :u \n x" | $MAILER -N -n -f $i | $EGREP -c "^No messages satisfy :u" `

# So, use formail on the entire folder with a simple script
     FLAGS=`$CAT $i | $FORMAIL -s $TESTSTATUS`
     $RM ${i}${LOCKEXT}
     trap - 1 2 3 15
     if [ -n "${FLAGS}" ]; then
# if we have flags and FORCE is set,
#   force this folder to be flagged regardless of status of NEWEXT
	if [ "$FORCE" = "yes" ]; then
	   NEWMAIL="${NEWMAIL}   ${i}(forced)"
           $ECHO "forced" > ${i}${NEWEXT}
	else
		if [ ! -f ${i}${NEWEXT} ]; then
# if we have flags, and the "NEWEXT" file does not exist
# (which means we have not already sent a message)
# flag the folder as having new mail, and create the flag file
	           NEWMAIL="${NEWMAIL}   ${i}"
		   $ECHO "first" > ${i}${NEWEXT}
		else
		   NEWER=""
NEWER=`$FIND . -name ${i} -prune -newer ${i}${NEWEXT} -print `
		   if [ -n "${NEWER}" ]; then
# mail folder has been modified since the NEWEXT file was
# which implies a new message has arrived
# so send another message
		     NEWMAIL="${NEWMAIL}   ${i}(again)"
	             $ECHO "again" > ${i}${NEWEXT}
# note that an "else" here could check the contents of NEWEXT
# if it was "first" could send a "second" message, and reset contents, etc
		   fi
		fi # end if NEWEXT exists
	fi # end if FORCE
     else
# no flags in this folder, so delete any "NEWEXT" file hanging around
        if [ -f ${i}${NEWEXT} ]; then 
          $RM ${i}${NEWEXT} 2>/dev/null
        fi
     fi # end if flags discovered
    else
# send message about unsuccessful lockfile, but continue for other folders
# be sure to unset outfolder and record
# so we do not archive these notification messages
      $ECHO "~:unset outfolder \n~:unset record \n ${0} unable to create lockfile for $i" | $MAILER -s "lockfile ERROR" $ME >/dev/null 2>/dev/null
    fi # end if lockfile successful
  else
# folder does not exist, or is null
# so delete any "NEWEXT" file hanging around
    if [ -f ${i}${NEWEXT} ]; then 
      $RM ${i}${NEWEXT} 2>/dev/null
    fi
  fi # end if folder exists
done
# DEBUG
DEBUGFILE=/home/mjhanna/procmail/cron.debug.log
# be sure to unset outfolder and record
# so we do not archive these notification messages
if [ -n "${NEWMAIL}" ]; then
 $ECHO "~:unset outfolder \n~:unset record \n New or Unread mail in folder(s): \n ${NEWMAIL}" | $MAILER -s "new/unread mail" $ME >/dev/null 2>/dev/null
fi
----------
X-Sun-Data-Type: shell-script
X-Sun-Data-Description: shell-script
X-Sun-Data-Name: export.test.status
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 30

#!/bin/sh
#
# Copyright (C) 1994 Sandia Corporation.
# This software may be used royalty-free, for research purposes only,
# so long as this copyright notice is retained in all copies.
# This software was developed at Sandia National Laboratories,
# which is managed and operated by Sandia Corporation,
# a subsidiary of Martin Marietta Corporation,
# as part of work sponsored by an agency of the United States Government.
# Neither the Unites States Government, nor any agency thereof,
# nor any of their employees, nor any of their contractors,
# subcontractors, or their employees, makes any warranty,
# express or implied, or assumes any legal liability or responsibility
# for the accuracy, completeness, or usefulness of this software.
#
# Michael J. Hannah, Scientific Computing Systems
# [email-masked]
#
# This script is passed (at least) the headers of a mail message
# and echoes an "N" if it is new mail, or an "O" if unread

FORMAIL=/usr/local/bin/formail

Test_Status=`$FORMAIL -x Status:`
if [ -z "${Test_Status}" ]; then
  /usr/bin/echo " N\c"
fi
if [ "${Test_Status}" = " O" ]; then
  /usr/bin/echo " O\c"
fi