#!/bin/bash

# Written by Joshua Koplik <jkoplik@bu.edu>
# ------------------------------------------------------------------------
# This program will use a serial modem to call any pager, alpa-numeric or 
# just plain numeric and it will leave the message  A-B-C-D-E-F
# Where A is the total number of e-mail messages you have waiting.  
# And B-F are the numbers of messages from specific people you chose
# If you have any questions or comments or changes/additions to this code 
# please mail them to the author.
 
# To make this work: read and edit up to the line that says STOP

# this is the directory where lock files can be found
LOCKDIR=/var/spool/uucp
# your mail directory including your folder
MAILDIR=/usr/spool/mail/root
# your modem goes here, with no leading /dev/
DEVICE=cua3

# this is your beeper number
PHONE=6695673
# this is the account that this is going to be run from
USER=root

# UNCOMMENT THE FOLLOWING LINES IF YOU WANT THIS PROGRAM TO GRAB POPMAIL
# ACCOUNT= jkoplik
# PASSWORD= SelfEvident
# MAILSERV= acs-mail.bu.edu
# popclient -3 -u $ACCOUNT -p $PASSWORD -c $MAILSERV >> $MAILDIR$USER.tmp

# OK..here comes the hard part... these 5 variables are strings that will 
# occur ONCE AND ONLY ONCE in every mail message from a particular user, 
# for example, if I get mail from Jim Smith <jsmith@smith.com> I would 
# probably want the string to be "<jsmith@" because if you look at the 
# header in the mail file you will see that this string only occurs once.  
# I could also probably do "Jim Smith" safely, because many systems stick 
# the user's name in the header.  This part may require some 
# experimentation to get the numbers exactly right, but "<user@" has given 
# me the best results. (these are NOT case-sensitive)

S1="<foo@"
S2="<jkoplik@"
S3="<oscar.meyer@"
S3="Usernames May Work"
S4="But Don't forget the quotes"
S5="these aren't case-sensitive, EiThEr"

# This is just like the above section, except this is a line that occurs 
# ONCE and ONLY ONCE in __EVERY__ mail message...these are somewhat hard to 
# come by, as there are fre things that fit this criteria, but "From:" has 
# worked for me, although this gets screwed up sometimes when messages are 
# forwarded, but even if it's not exactly right, the worst thing that 
# happes is you get the wrong number of messages. (this IS case-sensitive)

S0="From:"

# -----------------------STOP-----STOP-----STOP-----------------------------
# you shouldn't need to edit anything below this line (but feel free to)



# this tests if the modem is locked, and will quit the program if the 
# modem is locked.

if [ -f $LOCKDIR/LCK..$DEVICE ]
    then
    exit 1
    fi

# uncomment these lines if you want to override file locking.
# if [ -f $LOCKDIR/LCK..$DEVICE ]
#    then
#    rm -rf $LOCKDIR/LCK..$DEVICE
#    fi

# if you don't have mail we don't need to run this now do we :)
# if you don't allow fingers you need to remove this section

if finger root |grep -q -i "no mail"
then
exit 1
fi


# check the number of messages and dial the modem
MAIL=$MAILDIR.tmp

CODE=`grep $S0 $MAIL |wc -l |cut -c7`*`grep -i -e $S1 $MAIL |wc -l |cut -c7`*`grep -i -e $S2 $MAIL |wc -l |cut -c7`*`grep -i -e $S3 $MAIL |wc -l |cut -c7`*`grep -i -e $S4 $MAIL |wc -l |cut -c7`*`grep -i -e $S5 $MAIL |wc -l |cut -c7`

( stty 38400 -tostop
  chat ABORT "NO CARRIER" "" ATM0 OK "ATDT$PHONE ,,, DT$CODE" 
) < /dev/$DEVICE > /dev/$DEVICE 

# this is how long to wait before hanging up--change this value as needed

sleep 16

    rm -rf $LOCKDIR/LCK..$DEVICE
cat $MAIL >> $MAILDIR
rm $MAIL
chmod 660 $MAILDIR
chmod 660 $MAIL
chgrp mail $MAIL
chgrp mail $MAILDIR

