Author Topic: Scripts To Check For New Mail Regularly [long]  (Read 2535 times)

clofland

  • Full Member
  • ***
  • Posts: 247
    • View Profile
    • http://www.lofland.net/
Scripts To Check For New Mail Regularly [long]
« on: November 01, 2005, 12:13:47 pm »
I've spent some time building scripts up so that my Z will check for new mail for me regularly, over either my cell phone w/ bluetooth or any network (WLAN/LAN) it happens to be on.

I thought I would share them with the masses, and solicit any ideas. I consider these very simple, but I know a lot of people either don't have the time, or the knowhow to write even simple scripts, so here they are.

I welcome any criticism or ideas. I don't consider my scripting skills to be anything wonderful, just enough to get by.

So, without further nonsense here goes:

Features:
-Z will check for new mail every 30 minutes via cron.
-The Z will check mail at every wake up.
-The Z will wake up every 30 minutes.
-The Z will not check mail more often than every 15 minutes (mostly).
-By "new mail" what it is checking is
--A. is there MORE mail in the box than last time it checked? If so, then say "new mail" and play a sound.
--B. If not A then is any mail in the box marked as UNREAD? If so, then say "UNread mail" and play a different sound.

Requirements:
-Must install hc-cron from the unstable feed if you want to have it check regularly, even when awake, otherwise it will only check at wakeup time.
-Fetchmail is just a binary, I put it in /usr/sbin/ I'll attach it to this message. I didn't compile it, I just ripped it out of an IPK meant for some other ROM.
-I also put the two wav files into /home/root/sounds/ which I will attach Of course you can use your own and alter the script to find them anywhere

Scripts and commands: (I've tried to comment them heavily)

This is the core of the "system":
(be sure to make this executable)
(FIND REFERENCES TO "domain" and change the hostname and IP's to your mail server!)
(Same for PASSWORD and USERID!)
/home/root/checkmail
(please be aware of line wraps your browser may put in)
Code: [Select]
#! /bin/bash
echo `date` checkmail run>>/home/root/checkmail.log
#It does keep a small log for debugging I find this useful to tell if it is waking up regularly to check mail
if [ "$1" != LIVE ]; then sleep 15; fi
#I find that if it starts too soon after wakeup it often fails Thus the sleep
#If I want it to not sleep I can put LIVE on the command line to make it hurry up
if ! grep -q mail\.domain\.net /etc/hosts; then echo 127.0.0.1 mail.lofland.net>>/etc/hosts;fi
#I find that DNS resolution often fails the first time over my cell phone connection
#Plus that eats up time and bits if you are charged either way
#So I just make sure the IP is in the hosts file and that elliminates DNS resolution
if ! grep -q mail\.domain\.net /home/root/.fetchmailrc; then echo poll mail.domain.net protocol IMAP user YOURUSERID with password YOURPASSWORD is root here>>/home/root/.fetchmailrc;fi
#These are the settings fetchmail needs I just have them set up automatically
#so that we do not have to worry about making sure we copy the file over
#NOTE if you do not use IMAP or need some other changes to how fetchmail works
#here is where to put them
#Google for fetchmail to find all of the options It is very powerful
chmod 710 /home/root/.fetchmailrc
#fetchmail complains if the perms are not just so on this file so I just set
#them each time to ellinate any possibility of fetchmail failing for this reason
touch /home/root/chdialing
#I had trouble with running over myself by trying to dial out when the script
#had already dialed out or it trying to dial out when I was already online
#This little system tries to overcome that
#it requires I use the script dial to connect and hangup to disconnect
#the GUI in pdaXrom does not work for me so I use these scripts
if [ ! -f /home/root/mandialing ] && [ ! -f /home/root/stonline ]
then    
    echo ch dialing>>/home/root/checkmail.log
    /usr/sbin/pppd -d
fi
#pppd actually dials the connection
#notice that if there is no bluetooth card is in the CF slot then pppd has no affect
#and the script continues on and tries with whatever connection it may or
#may not have
#which is just what I want
cd /home/root
if [ ! -f savenum ]
then
echo "0">savenum
fi
#savenum is where we save the results of the last mail check
oldnum=`cat savenum`
nums=`/usr/sbin/fetchmail -f /home/root/.fetchmailrc -k -c 2>/dev/null | tr -d '(' | cut -f1,3 -d' '`
#there is the meat of the script where fetchmail does the actual work
#again GOOGLE for the fetchmail man page and see the options
#it is very powerful and can work with almost any mail account POP or IMAP
echo `date` $nums>>/home/root/checkmail.log
currentnum=`echo $nums | cut -f1 -d' '`
readnum=`echo $nums | cut -f2 -d' '`
if [ "X$currentnum" != X ]; then
echo $currentnum>savenum
if [ $currentnum -gt $oldnum ]
then
 echo "New mail!"
 /usr/X11R6/bin/xmessage -display :0.0 -default okay -c New Mail &
 /usr/bin/esdplay /home/root/sounds/tinkalink2.wav
 #mplayer /opt/kdepimpi/kdepim/korganizer/koalarm.wav >/dev/null 2>&1
elif [ $currentnum -gt $readnum ]
 then
 /usr/bin/esdplay /home/root/sounds/reminder.wav
 /usr/X11R6/bin/xmessage -display :0.0 -timeout 30 -default okay -c UNREAD Mail &
 echo "UNREAD Mail"
else
 echo "No new mail . . ."
fi
else
 echo "Fetchmail ERROR"
fi
if [ ! -f /home/root/mandialing ] && [ ! -f /home/root/stonline ]
then
    echo ch hanging up>>/home/root/checkmail.log
    /etc/ppp/ppp-off
fi
rm -f /home/root/chdialing

That is the main script, and you can actually use that to check mail manually and test if it all works.

If you want your Z to wake up ever 30 minutes and do this, add these:

/etc/apm/wakeup.d/five
(make sure to make it executeable)
(you can really name it anything)
Code: [Select]
echo -n 30Of course, just change the number to make it wake up more or less often

/etc/apm/resume.d/300checkmail
(make it executable)
Code: [Select]
#! /bin/bash
echo `date` 300checkmail run>>/home/root/checkmail.log
if [ ! -f /home/root/savenum ]
    then
    /home/root/checkmail &
elif [ "`find /home/root -maxdepth 1 -name savenum -mmin +15`" = "/home/root/savenum" ]
    then
    /home/root/checkmail &
fi
Notice that it is going to use the timestamp on savenum to avoid checking more often than every 15 minutes. That way if you wake it up 3 times in 20 minutes, you don't get a mail check 3 times.

Now, if you have installed hc-cron from the mix, then you can also add a cron job so that it will do this every 30 minutes (or what you like) when it is not asleep (for instance while you are actually working on the Z)

First, hc-cron looks for vi in /usr/bin/ but it is in /bin so to make cron tab happy I just do this:
Code: [Select]
ln -s /bin/vi /usr/bin/viNow, to set up the cron job just type:
Code: [Select]
crontab -eAnd put in this line (change the number for different interval):
Code: [Select]
*/30 * * * * /etc/apm/resume.d/300checkmail
All set!

Finally, three helper scripts I use.

/home/root/dial
(I use this to dial out, and it keeps checkmail from running into my connections)
(make it executable)
Code: [Select]
touch /home/root/mandialing
if [ ! -f /home/root/chdialing ] && [ ! -f /home/root/stonline ]; then pppd -d;fi
touch /home/root/stonline
rm -f /home/root/mandialing

/home/root/hangup
(this is used to end my connection, again, without running into checkmail)
(make it executable)
Code: [Select]
if [ ! -f /home/root/chdialing ] && [ ! -f /home/root/mandialing ]; then /etc/ppp/ppp-off;fi
rm -f /home/root/stonline
rm -f /home/root/mandialing

Finally I have linked this script to my mail key on the Z. It connects for me, opens sylpheed, and then when it is done, it runs checkmail, so that it is "in sync" with what I just saw, and will give an accurate reading next time, and then it hangs up.

/home/root/mailkey
(make it executable)
Code: [Select]
/home/root/dial
sylpheed
/home/root/checkmail LIVE
/home/root/hangup

NOTES:
I had to put a lot of direct paths in the scripts, b/c cron runs with a very limitted path. I found things worked great everywhere except for under cron, so I just put full paths on everything and then cron is happy.

The system does NOT put the Z back to sleep. I just rely on the self timers to do that for 3 reasons:
-1. I don't know how to let the script know if IT waked up the machine (via apmsleep) or if I woke up the Z. I don't wantit shutting it back down every time I turn it on.
-2. The auto sleep timers work well. I have not had any excess battery drain from this.
-3. The Z gets really funny if you wake and sleep cycle it too fast, too often. So I preferr to let it stay awake until the auto sleep timer hits it.

I'm impressed if you have read this far. Please add any feedback you may have. I know this isn't pdaXrom specific, but it only works on pdaXrom (I tried for a long time on SHARP ROM and could not make it go.)
C-760 Last Running pdaXrom 1.1.0beta3
- Sandisk UltraII 1GB SD card - Socket LP CF LAN card - Socket LP CF WLAN card - Socket CF Bluetooth Card Rev H -

klatt

  • Newbie
  • *
  • Posts: 47
    • View Profile
Scripts To Check For New Mail Regularly [long]
« Reply #1 on: November 02, 2006, 06:10:22 pm »
Does anyone know where to get fetchmail? I actually can't seem to find an ipk of it ANYWHERE....I'm just trying to throw a simple mail checking script into torsmo...

Thanks,
Frank

zi99y

  • Sr. Member
  • ****
  • Posts: 282
    • View Profile
Scripts To Check For New Mail Regularly [long]
« Reply #2 on: November 02, 2006, 07:04:20 pm »
its in the zip on the previous post

klatt

  • Newbie
  • *
  • Posts: 47
    • View Profile
Scripts To Check For New Mail Regularly [long]
« Reply #3 on: November 05, 2006, 07:17:32 pm »
ahhh ok thanks!