Friday, March 17, 2006

Check your balance from your phone!

So I'd been thinking about this one for a while and when I finally got around to doing it, it was easier than I thought.

Being the linux head that I am, I already had the scripts to accomplish most of what I needed, so a short script later I could check my bank account from my phone!

1. The basic idea is that you notify your computer via e-mail that you would like your bank balance.
2. Your computer checks its e-mail every minute or so, and catches your request.
3. Your computer logs into your banking site, and finds your balance.
4. Your computer SMS's your balance to your phone, and goes back to sleep.

Hokai! So! Here's the earth... First you need to get a free pop3 mailbox for your linux box. I used fetchmail because it's fucking easy to set up. If you get a pop3 account that doesn't require SSL encryption it's even easier to set up. But be mindful of what data you send over your phone and e-mail... The other easy part of this is that you don't have to set up sendmail or postfix, since you're only picking up the mail from your pop3 account, not sending. We're using google's SMS engine for that!

I'm using washington mutual who has a pretty good (and VERY up-to-date) website which displays my account info on the first page after logging in (good cuz I'm lazy!).

First lets make the mailwatcher:
$diff=`diff /var/spool/mail/username mailtemp.dat`;
@diff=split(/n/, $diff);
if(@diff[3]!~/^>/ && $diff=~/e-mailaddy/ && $diff=~/wamu/i){
print "$datetsending balance!n";
`cat /var/spool/mail/username > mailtemp.dat`;
`sleep 1m`;

Since I don't know an easy way to read mail besides reading it off of the /var/spool/mail/user I just make a new copy of it each minute so I can only look at the newest e-mails, and it doesn't get activated off of old e-mails. It compairs the new mail to the mail it just gathered from the same place a minute before (~/mailtemp.dat). The diff command will show only the different lines which will show either how the new mail file has more than the old mail file, how the new mail file has been emptied and the old one has more, or that they're the same. I check the beginning of one of the lines to make sure the diff output doesn't have a ">" in the beginning to keep from picking up the old requests when the new mail is emptied. It also makes sure the output has your cell phone's e-mail address in it (represented by e-mailaddy), and looks for the keyword in the output (in my case the keyword is wamu (case-insensitive)).

If those conditions are met, then it runs
curl -s --connect-timeout 15 -m 45 --insecure -L -c cook(cont)
ies -v -d "txtUserID=username&pwdPas(cont)
2.y=0" "
ntityManagement/postlogon.hdlr" 2> /dev/n(cont)
ull > balance.out;
/home/username/ balance.out;
shred balance.out cookies;

Please note that the first 7 lines are all one line. There are no trailing spaces, so you can just delete "(cont)" until the next line rejoins the former. Right, so we're using curl to send post data that logs you into the site, then follows the redirects, and spits out the webpage to balance.out. The website is then parsed by
my $file=shift;
my $html=`cat $file`;
@html=split('<tr', $html);
`curl -s '

Hokai, so you pick up the file, and split it by the table row tags, then grab the portion you want (in this case the third section (remember that's number 2)), then locate the unique tags around your account balance (in this case the "nobr" tags), then I use a simple http GET request sent to google's SMS function to send the balance to your phone (phonenumber) via SMS.

No comments: