#!/usr/bin/env python
#
# Requires a python-zephyr from at least March 14, 2009.
# See http://github.com/ebroder/python-zephyr

import zephyr, _zephyr
import select
import pynotify

zephyr.init()
pynotify.init('Zephyr')
zephyr.Subscriptions().add(('message', 'personal', '%me%'))
zephyr.Subscriptions().add(('operations', 'message', '*'))
zephyr.Subscriptions().add(('filsrv', '*', '*'))
zephyr.Subscriptions().add(('geofft', '*', '*'))
zephyr.Subscriptions().add(('geofft-test', '*', '*'))
zephyr.Subscriptions().add(('sipb', '*', '*'))
zephyr.Subscriptions().add(('scripts', '*', '*'))
zephyr.Subscriptions().add(('debathena', '*', '*'))
zephyr.Subscriptions().add(('consult', '*', '*'))
zephyr.Subscriptions().add(('mail', '*', '*'))
zephyr.Subscriptions().add(('message', 'consult', '*'))

while True:
  try:
    m = zephyr.receive(True)
  except IOError, e:
    if e.errno != 110:
      raise
    continue
  if m.opcode.lower() == "ping":
    continue
  f = m.message.split("\x00")
  if len(f) < 2:
    continue
  try:
    body = f[1].decode('UTF-8')
  except:
    body = f[1].decode('ISO-8859-1')
  if m.recipient == '*':
    n = pynotify.Notification("Zephyr from %s" % m.sender.replace("@ATHENA.MIT.EDU", ""), body)
  else:
    n = pynotify.Notification("%s / %s / %s" % (m.cls, m.instance, m.sender.replace("@ATHENA.MIT.EDU", "")), body)
  #n.set_timeout(pynotify.EXPIRES_NEVER)
  n.show()
