PGTS PGTS Pty. Ltd.   ACN: 007 008 568               Mobile Version Coming Soon

point Site Navigation







Valid HTML 4.01!






   Give Windows The Boot!
   And Say Goodbye To Viruses!

   Ubuntu

   If you own a netbook/laptop~
   Download Ubuntu Netbook!





mboxw - A perl script which monitors email in mbox format

By Gerry Patterson

Several months ago I constructed a perl script to create notifications when email arrived in an mbox format mailbox. This is because I use mutt to read emails and mutt has no pop-up notifications. I have configured the MTA to deliver mail to an /nfs/mount which can be read from various workstations.


Why would anyone want to use mutt?

There are number of reasons why I use mutt to read my email, apart from the fact that I have been doing it for a long time and I am reluctant to change:

Having said all that, I have to admit that I have become used to the convenience of using GUI MUAs. Kmail, Thunderbird, Apple Mac Mail, will create a pop-up window to inform you that mail has arrived, and most of them will integrate with other products (such as appointments, contacts etc) Microsoft's Exchange MUA although (like all Microsoft products) very poor in overall performance has an impressive array of pop-up functions (with or without sound) and good integration with Exchange Calender.

So why not write a script which monitors the mailbox and creates a pop-up window to notify me when a mail arrives?

Pre-requisite Packages.

I thought this might be an opportunity to acquaint myself with some new programming language. But as soon as I started to grapple with the complexity of GUI programming I went back to using perl. At least I know how to use perl's GUI API. To be more precise, this script uses Tk/perl. You can check if you have Tk/perl already with this command:

	perl -e 'use Tk::ROText'

If Tk/perl is not installed an error message like the following will appear on the console:

	Can't locate Tk/ROText.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at -e line 1.
	BEGIN failed--compilation aborted at -e line 1.

You can install Tk/perl using CPAN, however if you have Ubuntu, you will find it much easier to use this command:

	sudo apt-get install perl-tk

The mboxw script has been written assuming that you will use your sound system rather than the terminal "bell". In which case you will require a sound player that behaves well when called from the command line (e.g. mpg123).

	sudo apt-get install mpg123

I use numerous Linux workstations. So I had set the script up to check an NFS mount which had the mbox format mailbox on it. The mboxw script was initiated at startup whenever I logged into a GUI desktop. The MTA runs on a server (no GUI --- console only). There are numerous scripts running on the server which monitor mailboxes to see if mail has been redirected to specific mailbox. This is because spam assassin automatically re-routes email with a spam weight greater than five using procmail. Also occassionally some spam gets through to my inbox because the weight is less than five. I save these to a certain area where they are re-routed to spam assassin to learn new spam. All spam is examined by another script which takes note of unknown hosts and adds them to /etc/postfix/access to be issued a REJECT by the MTA. Time spent in the access banned list increases by one day for each transgression. The list is also correlated with a list zombies, rude-robots and hosts that seem to persist with firewall policy violations.

So this script also has a section of code that allows re-routing of any email to the spam suspects box. The key used for identification is the number of the mbox file combined with the From address in the From opening dialogue. Message-ID is not reliable, since some mail may having missing or non-unique Message-ID tags.

Calendar Option.

After using the new mboxw script for a little while, I decided to include my Gmail calender in the notifications. Gmail do have a pop-up window in Chrome. But it only works when Chrome is running and the the Gcal page is open. I decided to use a command line utility, called gcalcli, which fetches Gcal info using webservices, If you have Ubuntu, you can install it with this command:

	sudo apt-get install gcalcli

BTW I found gcalcli to be a handy little utility for getting information from Gcal. The command interface is as follows:

	gcalcli [options] command [command args]
You can put your username and password in the config file (~/.gcalclirc). Example:
	[gcalcli]
	user: fred.nurk@gmail.com
	pw: egglyoggly99
	cals: owner

One option you will always need is the --nc option (no colour). Unfortunately this option can't be set in the config file. It seems that only options which take an argument can be set in the config file.

Example of using gcalcli to get the week's agenda:

        gcalcli --nc agenda 2011-04-04 2011-04-05
        Mon Apr 04   9:00pm  FOOBAR

        Tue Apr 05   7:00pm  GTK - remember ecky

        Wed Apr 06  12:00am  Jim Nurk's Birthday
                     8:00am  Brekkie at the Hilton
                     3:00pm  payday
                     3:15pm  Gabby sport
                     9:00pm  foobar fun night
                    10:00pm  Pay Telstra account

        Thu Apr 07   5:00pm  Break Dancing
                     7:00pm  dinner with janet

        Fri Apr 08  12:00am  John Smith Birthday
                     1:00pm  schools close for term 1
                     5:35pm  Dinner with hockey team after game

The script sometimes gives unpredictable results with 24 hour clock formats It insists on calling midnight 12:00am. Whereas noon is called 12:00pm. But it does grok ISO dates (yyyy-mm-dd). If you can live with these foibles it is quite a handy little script. You can quick add an item to your default calendar with a command such as the following:

	gcalcli quick '2011-05-01 08:00 Breakfast with the user group'

However, you can't delete entries. For anything more complex than adding a simple appointment (such as the above) it would be far easier to start up a browser and use the Google Calendar interface. These days, most desktop users will have a browser running in at least one active window.

The mboxw script has been written to be installed in your $HOME/bin folder. Once it has been installed and made executable, you can start it with the following command:

        nohup ~/bin/mboxw & >/dev/null

If the folder $HOME/bin is in your path you can omit the path.

You may prefer to start the script automatically when you first login. In Kubuntu, you can do this by creating a link in ~/.kde/Autostart

In Ubuntu, you need to use the option System->Preferences->Sessions or System->Preferences->Startup Applications.

The new Ubuntu Unity interface does not have a System Menu. Startup scripts are kept in ~/.config/autostart/. However the files are special files (with a suffix .desktop). These files can be edited and created with the utility gnome-desktop-item-edit. The safest way to create them is with "Startup Applications Preferences". Go to the applications menu and type startup ... Unity will suggest the Startup Applications Preferences panel.

However, there is a problem with starting this script in Ubuntu. The program keeps running as a zombie process after you log off from the Gnome session There is a term command in mboxw. You can terminate the active session by running

    mboxw term

In order to get this command to run add this line to /etc/gdm/PostSession/Default:

        if [ -x $HOME/bin/mboxw ] ; then
                # terminate any active instances of mboxw
                $HOME/bin/mboxw term
        fi

Note: This problem does not occur in Kubuntu. The KDE session manager kills off any active mboxw process when you log off.

Configuration File

Each work station can have its own configuration file. The configuration file is written as a perl script ... This makes it easy to load into the main script (just use require). Of course if you make a mistake in your configuration file it means the script will crash. So if you use this approach you should always check your configuration file with perl -c immediately after making a change.

Some of the things that seem obvious to put in the configuration script are:

A sample configuration script is can be downloaded from this site (see link below).

Getting it to work with Mac OS X

The script will work in Mac OS X. However the difficult task is getting Tk/perl to work. I found there were two reasonably easy paths to get a workingversion of Tk/perl:

If you go down either of these routes, you may need to rename /usr/bin/perl. ... Either that or change the #! line in the perl scripts ... e.g.

#!/opt/local/bin/perl

The list of things need to get it working with Mac Os X:

# Install MacPorts
sudo port install perl5
# Prepend /opt/local/bin and /opt/local/sbin to PATH
# Rename /usr/bin/perl and created a logical link to /opt/local/bin/perl
# Modified .profile for root. (prepend /opt/local/bin:/opt/local/sbin)
sudo port install mpg123
sudo port install python26
sudo port select --set python python26
sudo port install gcalcli
sudo port install mpg123
sudo port install p5-tk
# Add to startup items folder:
# System Preferences->Accounts
# Select user then select "Login items"

Source Code

A Copy of the source code can be found here.

A copy of the configuration file (perl script) can be found here.