2017-05-11 00:32:02 -07:00
|
|
|
#!/usr/bin/env python3
|
2017-04-26 15:52:37 -07:00
|
|
|
|
|
|
|
import imaplib
|
2017-05-11 00:32:02 -07:00
|
|
|
import email
|
2017-04-26 15:52:37 -07:00
|
|
|
import os
|
|
|
|
import sys
|
2017-05-11 00:32:02 -07:00
|
|
|
import re
|
2017-05-25 02:46:13 -07:00
|
|
|
import html
|
2017-08-06 07:10:40 -07:00
|
|
|
import socket
|
2017-05-11 00:32:02 -07:00
|
|
|
from shlex import quote
|
2017-04-26 15:52:37 -07:00
|
|
|
|
|
|
|
ICON = ""
|
|
|
|
CONFIG_FILE = os.path.expanduser("~/.private/config/email")
|
2017-05-11 00:32:02 -07:00
|
|
|
CACHE_FILE = "/tmp/polybar-mail"
|
2017-04-26 15:52:37 -07:00
|
|
|
|
|
|
|
COLOR_HL = "B48EAD"
|
|
|
|
COLOR_NONE = "65737E"
|
|
|
|
COLOR_ERR = "D08770"
|
|
|
|
|
2017-05-11 00:32:02 -07:00
|
|
|
|
|
|
|
#
|
2017-08-06 07:10:40 -07:00
|
|
|
def internet_is_on():
|
|
|
|
try:
|
|
|
|
host = socket.gethostbyname("www.google.com")
|
|
|
|
s = socket.create_connection((host, 80), 2)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return False
|
|
|
|
|
2017-05-11 00:32:02 -07:00
|
|
|
def recent_messages(address, password):
|
|
|
|
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)
|
|
|
|
obj.login(address, password)
|
|
|
|
try:
|
|
|
|
obj.select('Inbox')
|
|
|
|
ret, resp = obj.search(None, '(UNSEEN)')
|
|
|
|
if ret == 'OK':
|
|
|
|
uids = resp[0].split()
|
|
|
|
if uids:
|
|
|
|
ret, data = obj.fetch(b",".join(uids), '(BODY.PEEK[])')
|
|
|
|
if ret == 'OK':
|
|
|
|
for m in filter(lambda x: len(x) > 1, data):
|
|
|
|
yield email.message_from_string(m[1].decode('utf-8'))
|
|
|
|
else:
|
|
|
|
print("Error: fetch")
|
|
|
|
else:
|
|
|
|
print("Error: lookup")
|
|
|
|
finally:
|
|
|
|
obj.close()
|
|
|
|
obj.logout()
|
|
|
|
|
|
|
|
def notify(sender, subject):
|
2017-05-11 12:41:04 -07:00
|
|
|
if not sender[0].isalnum() and "<" in sender:
|
2017-05-11 00:32:02 -07:00
|
|
|
sender = sender.split("<")[1].split(">")[0]
|
2017-05-25 02:46:13 -07:00
|
|
|
os.system("/usr/bin/notify-send {} {}".format(quote("<b>{}</b>".format(html.escape(sender))), quote(html.escape(subject))))
|
2017-05-11 00:32:02 -07:00
|
|
|
|
|
|
|
def ding():
|
|
|
|
os.system("/usr/bin/aplay -N -q '{}'".format(os.path.expanduser("~/.config/polybar/sounds/robinhood76-music-box.wav")))
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if os.path.isfile(CONFIG_FILE):
|
2017-08-06 07:10:40 -07:00
|
|
|
if not internet_is_on():
|
|
|
|
print("%{{F#{}}}{} !".format(COLOR_ERR, ICON))
|
|
|
|
sys.exit(2)
|
|
|
|
|
2017-05-11 00:32:02 -07:00
|
|
|
cache = []
|
|
|
|
if os.path.isfile(CACHE_FILE):
|
|
|
|
with open(CACHE_FILE, 'r') as fp:
|
|
|
|
cache = fp.read().strip().split("\n")
|
|
|
|
|
|
|
|
with open(CONFIG_FILE, 'r') as fp:
|
|
|
|
accounts = [line.strip().split(",") for line in fp if line]
|
|
|
|
|
|
|
|
total = 0
|
|
|
|
unique = 0
|
|
|
|
for addr, pwd in accounts:
|
|
|
|
for msg in recent_messages(addr, pwd):
|
|
|
|
total += 1
|
|
|
|
if msg['Message-Id'] not in cache:
|
|
|
|
notify(msg['From'], msg['Subject'])
|
|
|
|
cache.append(msg['Message-Id'].strip())
|
|
|
|
unique += 1
|
|
|
|
|
|
|
|
color = COLOR_HL if total > 0 else COLOR_NONE
|
|
|
|
print("%{{u#{}}}%{{F#{}}}{} {}".format(color, color, ICON, total))
|
|
|
|
if unique > 0:
|
|
|
|
ding()
|
|
|
|
with open(CACHE_FILE, 'w') as fp:
|
|
|
|
fp.write("\n".join(cache[-75:]))
|
|
|
|
else:
|
|
|
|
print("%{{F#{}}}{} -".format(COLOR_ERR, ICON))
|
|
|
|
sys.exit(1)
|