popexec 0.2

A program that executes commands when e-mail arrives at a POP account

by Bill Kendrick
bill@newbreedsoftware.com

New Breed Software
http://www.newbreedsoftware.com/popexec/

July 11, 1999 - November 1, 2000


WHAT DOES IT DO?
----------------
popexec occassionally checks the status of a POP e-mail account
and runs a command whenever new messages arrive.  It can also, optionally,
run a different comamnd whenever there are no more new messages (ie, after
you've read and/or deleted those e-mails).


WHY WOULD I WANT THIS?
----------------------
I realize there are literally oodles of POP-checking programs out there,
but I feel popexec is very useful too, since it can run arbitrary commands
when e-mail arrives.

The different things one could do with popexec include, but are far from
limited to:

  * Writing "You have new mail" to your terminal
  * Making the terminal beep
  * Playing a sound
  * Popping up an X window
  * Changing your X desktop
  * Blinking an LED on your keyboard

Later in this document I'll give you some "recipes" that will do each
of these.


INSTALLATION
------------
To compile popexec, all you should need to do is type:

  make


If you'd like it installed to a locally-accessible place, you can use while
logged-in as superuser (root):

  make install


By default, it is installed in "/usr/local/bin/".  You can change this
by specifying "DEST" when running "make install":

  make install DEST=/some/other/path


RUNNING popexec
---------------
You can provide popexec with a number of options, or it can try to figure
them out for itself:

  -s, or --server

    You can tell popexec what POP server to connect to.  If you don't
    specify, it will check with "localhost," which should be the machine
    you're running popexec on.

  -p, or --port

    If for some reason the POP server is running on a different port than
    the standard (110), you can specify that, too.

  -u, or --user

    Since your username on a different server may not be the same as the
    username you're running popexec under, telling popexec what name to
    log in with is useful.

    If you don't specify a username, popexec tries to guess your username
    two different ways.  First, it looks at your ".netrc" file.  (More on
    that later.)  Second, if it still hasn't figured it out, it will check
    the environment variables "USER" and then "LOGNAME".

    If it cannot figure it out whatsoever, then it will prompt you for
    the username.

  -w, or --wait

    With this option, you can tell popexec how often to check for new mail.
    The default time is 60 seconds.


THE .netrc FILE
---------------
Before I explain the "exec" part of "popexec," you might be wondering about
one last bit of "pop" information.  Where does popexec get your password!?

If popexec knows the server and username of the POP account you're trying
to connect to, it will try to look into your ".netrc" file to determine
the password to use.

What's a ".netrc" file?  It's a file stored in your "home directory"
(also known as "$HOME" or "~") used by a few network programs, most
notably, many FTP clients, to automate logging in.

If you ask your FTP client to connect to "ftp.myserver.net", and there's
a username and password specified for that machine in your ".netrc" file,
it won't bother asking you for your username and password... it already has
them!

popexec does exactly the same.  If you ask it to connect to the POP server
"pop.email.org", and there's a username and password specified for that
machine in your ".netrc" file, it won't bother asking you for your
username and password... it already has them!

You should check the "man" page for the "ftp" program for more details,
but the simplest example of a .netrc file is:

  machine ftp.server.net login johnd password abcd1234
  machine pop.email.org login jdoe password xyz5678

Note: For safety reasons, you should make sure that this file is not
readable by anyone but yourself!

  chmod 600 ~/.netrc

Since you may have more than one POP account on a server, you can even
list multiple "login"'s in your .netrc, and then run multiple popexec's,
one for each account.

In your .netrc, you could say:

  machine pop.email.org login jdoe password xyz5678
  machine pop.email.org login jane password lmnop$
  machine pop.email.org login frank password fr@NK

And then run popexec three times:

  popexec -s pop.email.org -u jdoe
  popexec -s pop.email.org -u jane
  popexec -s pop.email.org -u frank

Each popexec will determine the corrent password, based on the machine AND
login listed in your .netrc!

What happens if you don't have an entry in your .netrc, or don't even have
a .netrc file?  Since specifying your password on the commandline would be
downright stupid, popexec prompts you for your password.  (When you type it,
nothing is echoed back, for safety.)


EXECUTING PROGRAMS
------------------
popexec is meant to execute a program when new e-mail arrives.  You can also
have it execute a program when there is no more new e-mail.  You simply
specify these as options:

  -x, or --exec

    This program will run whenever more new e-mail arrives.  (Whenever the
    number of "new messages" is different than it was the last time we
    checked.)  If you do not specify this yourself, the command executed
    will be: "/bin/echo New Mail", which simply writes "New Mail" to the
    terminal where you started popexec.

  -y, or --execnone

    This program will run whenever there is no longer new e-mail.  (Whenever
    the number of "new messages" is zero, and it wasn't zero the last time
    we checked.)  This option is useful to "undo" things that happened when
    new e-mail came in.

Since the command you provide to "-x" ("--exec") or "-y" ("--execnone") has
to be one single argument, and most commands you would want to run will
take arguments themselves, you will need to quote the command, so your
shell will send it all as one argument:

  WRONG:  ... -x /bin/echo New mail
  RIGHT:  ... -x "/bin/echo New mail"


Most programs you would run will end within some short, finite amount of
time.  For example, "echo" will finish right after it's done writing text
to your terminal.  If you ran some command which played a sound through
your speaker, it would finish fairly immediately as well.

What if you want to run a program and let it keep running?  Use the fork
option:

  -f, or --fork

    When a program is executed, it is executed in the background.  The
    next time something happens (more new e-mail, no more new e-mail),
    the program is 'killed' (a "SIGINT" 'interrupt' signal is sent to it).

A good example of a program that you'd want to fork would be the LED-blinking
program by Anders Johansson.  For it to keep blinking an LED light on your
keyboard, the program must keep running.  When the number of e-mails changes,
you want to stop the old LED blinking process (and possibly start a new one).


COMMAND VARIABLES
-----------------
popexec sounds pretty useful so far, doesn't it?  Well, it gets better.
Instead of simply running some predetermined command whenever e-mail comes
in, you can use some "variables" to make the program behave differently
depending on, say, how many messages have arrived!

A simple example would be:

  popexec -s pop.server.com -u username -x "/bin/echo You have %m new messages"

popexec will replace the "%m" with the number of new messages on the server.

The variables that can be used are:

  %s    Name of the pop server being checked
  %p    Port number being checked
  %u    Username of the account being checked
  %m    Number of new messages
  %%    The "%" character

So, for example, you could say:

  ... -x "/bin/echo %%%% %u has %m new messages on port %p of %s %%%%"

This would come out as, for example:

  %% jdoe has 5 new messages on port 110 of pop.mail.com %%


You could even use variables to alter the way other commands work.  Say
you're checking two POP accounts and want each one to play a different
sound.  You could specify the sound on each popexec command, but it would
be much cooler to just create a sound file for each username:

  ... -x "cat %u.au > /dev/audio"


SOME RECIPES
------------
Here are some different commands I've tried out with popexec:

  ... -x "/bin/echo You have new mail"

    This says "You have new mail" on the terminal whenever new e-mail arrives.

  ... -x "/bin/echo %m new messages"

    This tells you how many new messages you have.

  ... -x "/bin/echo -n -e \a"

    This sends an "alert" (bell) to the terminal, making it beep.

  ... -x "/bin/cat you_have_mail.au > /dev/audio"

    This plays an AU audio file when new e-mail arrives.

  ... -x "xmessage You have mail"

    This pops up a simple X window telling you there's new e-mail.

  ... -x "xsetroot -solid white" -y "xsetroot -solid black"

    This changes your X sessions desktop to white whenever you have new
    mail.  Otherwise it changes it to black.

  ... -x "led 3 %m" -f

    This blinks the "Scroll Lock" LED on your keyboard when you have e-mail.
    It blinks as many times as there are messages.  (Note the "-f"!!!)

    (Anders Johansson's "led" program is available in the "led" subdirectory.)


CREDITS
-------
* Reason I wrote "popexec":         Donald J. Bindner's "mailblink"
* Reason Donald wrote "mailblink":  Jouni.Lohikoski@iki.fi's "tleds"
                                    Anders Johansson's "led"
* popexec code:                     Bill Kendrick <bill@newbreedsoftware.com>
* POP3 routines:                    Scott Holden <scotth@thezone.net>
* "--execnone" idea:                Remington Stone <rjstone@ucdavis.edu>'s dad
* "led" written by (and used with
  permission from):                 Anders Johansson <andersjo@lysator.liu.se>
