#!/usr/bin/python # vim:foldmethod=indent """ Copyright (C) 2008 Devon Jones 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. This software is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA For questions regarding this application contact Devon Jones http://www.evilsoft.org/lightbiff/ """ import sys import os import imaplib import time import yaml from email.utils import parsedate from socket import socket, AF_INET, SOCK_DGRAM def get_imap_handle(config): try: imap = None if config['imap_ssl'] == 'true': imap = imaplib.IMAP4_SSL(config['imap_server']) else: imap = imaplib.IMAP4(config['imap_server']) imap.login(config['imap_username'], config['imap_password']) except Exception, e: print("IMAP on fire eh?") exit(-1) return imap def get_oldest_unseen_date(imap): typ, data = imap.search(None, 'UNSEEN') oldestmsg = None oldtime = None idlist = data[0].split() if len(idlist) > 0: num = idlist[0] typ, data = imap.fetch(num, '(RFC822)') msgdate = get_date(data[0][1]) return msgdate def get_date(message): lines = message.splitlines() datestr = None for line in lines: if line.startswith("Date: "): datestr = line.split(": ")[1] msgdate = time.mktime(parsedate(datestr)) return msgdate def check_time(config, msgdate): now = time.time() udp = socket(AF_INET, SOCK_DGRAM) port = config['orb_port'] host = config['orb_host'] if msgdate: if (now - msgdate) > (config['seconds'] + config['redseconds']): data = "%%FF0000" udp.sendto(data, (host, port)) elif (now - msgdate) > config['seconds']: data = "#FF0000" udp.sendto(data, (host, port)) else: percent = (now - msgdate) / config['seconds'] value = 512 * percent if value > 256: byte1 = 255 byte2 = 255 - int(value - 256) else: byte1 = int(value) byte2 = 255 color = hex_chars(byte1) + hex_chars(byte2) + "00" data = "#%s" % (color) udp.sendto(data, (host, port)) else: data = "roam" udp.sendto(data, (host, port)) def hex_chars(num): value = str(hex(num)).upper()[2:4] if len(value) == 1: return "0" + value return value def get_config(): if os.path.exists('lightbiff.yaml'): return yaml.load(file('lightbiff.yaml', 'r'))['lightbiff'] elif os.path.exists(os.path.expanduser('~/.lightbiff')): return yaml.load(file(os.path.expanduser('~/.lightbiff'), 'r'))['lightbiff'] elif os.path.exists('/etc/lightbiff.yaml'): return yaml.load(file('/etc/lightbiff.yaml', 'r'))['lightbiff'] else: raise Exception('No config file') def main(): config = get_config() while 0 == 0: imap = get_imap_handle(config) imap.select() msgdate = get_oldest_unseen_date(imap) check_time(config, msgdate) imap.close() imap.logout() time.sleep(config['update']) if __name__ == "__main__": main()