"""
This module holds a basic mail check based on a mail file in a 
Linux/Unix environment.

Currently implemented:
  - checks the size and modtime of the file specified in your
    MAIL environment variable and tells you if you've got mail!
  - if you don't have a MAIL environment variable, it'll nicely 
    shut itself down
  - prints out the size of your mail file.  sometimes this is nice,
    but it tends to irritate me because ...  well, why does it have
    to be so big?!

Currently not implemented:
  - pop3, imap, or any other kind of protocol or mail file checking....


Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Copyright 2002, 2003, 2004 Will Guaraldi
"""

__author__ = "Will Guaraldi <willg@bluesock.org>"
__version__ = "1.1 (15 January, 2004)"
__description__ = "Keeps track of a mail file and alerts you if you have new mail."

import commands, os
from time import strftime, localtime
from lyntin import exported, ansi

lasttime = 0
lastsize = 0

def mailcheck(args):
  """
  Checks the size and last modify time of the file as specified in your
  MAIL environment variable (if you have one).  It does it automatically
  on the timer.
  """
  global lasttime, lastsize
  mail = os.environ.get("MAIL", None)
  if mail:
    try:
      newsize, z, newtime = os.stat(mail)[6:9]
    except:
      exported.hook_unregister("timer_hook", mailcheck)
      exported.write_error("MAIL environment points to non-existing file.")
      return

    if newtime > lasttime and newsize > lastsize and lasttime != 0:
      exported.write_message("%sYOU HAVE NEW MAIL%s (%dK, %s)" % 
                             (ansi.get_color("light green"),
                             ansi.get_color("default"),
                             newsize/1000,
                             strftime("%H:%M", localtime(newtime))))

      # FIXME - change this line if you want it to display other
      # information or if you don't have egrep available.
      subj = commands.getoutput("egrep \"^(From|Subject):\" %s | tail -n 2" % mail)
      exported.write_message(subj)
    lasttime = newtime
    lastsize = newsize
  else:
    exported.write_error("MAIL environment variable does not exist.")
    exported.hook_unregister("timer_hook", mailcheck)

def load():
  exported.hook_register("timer_hook", mailcheck)

def unload():
  exported.hook_unregister("timer_hook", mailcheck)