#!/usr/bin/env python2.7 """ Config file is ~/.screen_hooks and should contain: [email] server = server.hostname user = username password = password ssl = true or false """ import re import sys import socket import os.path import imaplib import subprocess import ConfigParser try: cfg = ConfigParser.SafeConfigParser() cfg.readfp(open(os.path.expanduser('~/.screen_hooks'))) except IOError: sys.exit(0) try: if cfg.getboolean('email', 'ssl'): imap_class = imaplib.IMAP4_SSL imap_port = imaplib.IMAP4_SSL_PORT except ConfigParser.NoOptionError: imap_class = imaplib.IMAP4 imap_port = imaplib.IMAP4_PORT try: timeout = cfg.getint('email', 'timeout') except ConfigParser.NoOptionError: timeout = 5 try: imap_port = cfg.getint('email', 'port') except ConfigParser.NoOptionError: pass password = cfg.get('email', 'password') if password.startswith("`") and password.endswith("`"): password = subprocess.check_output(password[1:-1], shell=True).strip() try: socket.setdefaulttimeout(timeout) conn = imap_class(cfg.get('email', 'server'), imap_port) conn.login(cfg.get('email', 'user'), password) unread = int(re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)) except: unread = "#[fg=red]E" if unread: print("#[fg=green][#[fg=white]{}#[fg=green]]".format(unread))