Re: Mailbox check utility

Paul Lankow <[email protected]>
Newsgroups gmane.org.user-groups.linux.pdxlug
Message-ID <[email protected]>
On Thu, 10 Jun 2004, Charlie Schluting wrote:

> Excellent, that's a great idea. I think I may use this too... thanks :)

Well, I wasn't satisfied so I played with it some more. The user
script is "cm". It will take the arguments start, stop and check.  
"Start" will start the mail-check utility in the background, "Stop"  
will kill the background job, and "Check" will take a look, then quit.  
Note that you can do a quick check even while it is already running in
the background. The is handy if you think you might have missed a
notification.

"cm" calls cm.scr to do the actual work.

---

# cm
#!/bin/sh

COMMAND="$1"

if [ "$COMMAND" = "check" ]; then
 cm.scr check
 exit
fi

if [ "$COMMAND" = "start" ]; then
 if [ -f $HOME/.cmpid ]; then
  echo "`basename $0` is already running."
  exit 1
 fi
 cm.scr start &
 exit
fi

if [ "$COMMAND" = "stop" ]; then
 if ! [ -f $HOME/.cmpid ]; then
  echo "`basename $0` is not running."
  exit 1
 fi
 kill `cat $HOME/.cmpid`
 rm $HOME/.cmpid
 exit
fi

echo "Usage: `basename $0` start|stop|check"
exit

---

# cm.scr
#!/bin/sh

COMMAND="$1"

if [ "$COMMAND" = "start" ]; then 
 echo $$ >$HOME/.cmpid
fi

MAILDIR=$HOME/mail
GREP=/bin/grep
TOTAL_COUNT=0
MAILBOXES="[INBOX] Musicians OurPlace [BLUE] [PDXLUG] [SYSTEM]"

while [ 0 = 0 ]; do
cd $MAILDIR
MAILIN=""
for each in /var/mail/$USER $MAILBOXES
do
  if [ -f $each ]; then
    totalCount=`$GREP "^From " $each | wc -l`
    readCount=`$GREP "^Status: RO" $each | wc -l`
    unreadCount=`expr $totalCount - $readCount`
      if [ $TOTAL_COUNT -eq 0 ]; then
        TOTAL_COUNT=`expr $TOTAL_COUNT + $unreadCount`
      fi
      if ! [ $unreadCount -eq 0 ]; then
        if [ "$each" = "/var/mail/$USER" ]; then each="INBOX"; fi
        MAILIN="$MAILIN $each"
      fi
  fi
done

if ! [ "$MAILIN" = "" ]; then
 if ! [ "$MAILIN" = "$OLDMAILIN" ]; then
  OLDMAILIN=$MAILIN
  echo "You have new mail in:$MAILIN"
 fi
fi

if [ "$COMMAND" = "check" ]; then
 break
fi

sleep 60
done

---

You're right ... I need a life. :-)

Regards,
Paul

---                  _
Paul F. Lankow      -o)
Linux enthusiast    /\\
www.fedora.com     _\_v
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.