Last active
April 6, 2017 07:45
-
-
Save Neo-Oli/50b3ef5f3895953f61f0864afb329b41 to your computer and use it in GitHub Desktop.
Mail notification script for termux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/data/data/com.termux/files/usr/bin/python3 | |
import os | |
# from subprocess import Popen | |
import subprocess | |
import email | |
import re | |
from email.header import decode_header | |
import json | |
def run(command): | |
output=subprocess.call(['bash', '-c', command]) | |
return output | |
# def run(command): | |
# output="" | |
# p = Popen(['bash', '-c', command]) | |
def clean(string): | |
if type(string)==list: | |
new="" | |
for value in string: | |
new="{}{}".format(new,value) | |
string=new | |
if string: | |
string=string.replace("'","") | |
string=string.replace("`","") | |
string=string.replace("\"","") | |
string=string.rstrip() | |
return string | |
def realdecode_header(string): | |
if not string: | |
string="" | |
d=decode_header(string) | |
header="" | |
for val,enc in d: | |
if not type(val) == str: | |
if not enc: | |
enc="utf-8" | |
val=val.decode(enc) | |
header="{}{}".format(header,val) | |
return header | |
def processmail(mail): | |
with open (os.path.expanduser("~/mail/new/{}".format(mail)),"r",errors="replace") as mailfile: | |
mailtext=mailfile.read() | |
msg=email.message_from_string(mailtext) | |
sender=msg["From"] | |
# sender=re.sub("[aeiou]+", "", sender) | |
sender=realdecode_header(sender) | |
sender=clean(sender) | |
if sender: | |
frommail=re.findall("(.*) <.*>",sender) | |
if len(frommail)>0: | |
sender=frommail[0] | |
else: | |
sender="Unknown" | |
subject=msg["Subject"] | |
if subject: | |
subject=realdecode_header(subject) | |
subject=clean(subject) | |
else: | |
subject="No Subject" | |
body=msg.get_payload() | |
for i in range(0,3): | |
if not type(body)==str: | |
body=body[i].get_payload() | |
body=clean(body) | |
title=sender | |
content=subject | |
if not content: | |
content="No Content" | |
command=" ".join([ | |
"termux-notification", | |
"--id '{}'".format(mail), | |
"--title '{}'".format(title), | |
"--content '{}'".format(content,body), | |
"--led-color 00ffff", | |
"--vibrate 300,150,300", | |
"--action '{}'".format( | |
";".join([ | |
# "if `tmux ls 2>/dev/null | grep -v attached`", | |
# "then setsid sh -c \"exec tmux a <> /dev/pts/0 >&0 2>&1\"", # this breaks all sorts of stuff | |
"am start --user 0 -n com.termux/com.termux.app.TermuxActivity", | |
# "fi", | |
"tmux new-window -n \"Mail\" \"{}\"".format( | |
";".join([ | |
"mutt -e \\\"macro index - 'l~i{}'; push \\n\\\"".format(mail.split(":")[0]) | |
]) | |
) | |
]) | |
), | |
"--button1 'Archive'", | |
"--button1-action '{}'".format( | |
";".join([ | |
"mv ~/mail/new/{}* ~/mail/.archive/cur".format(mail), | |
"mv ~/mail/cur/{}* ~/mail/.archive/cur".format(mail), | |
"termux-notification-remove {}".format(mail) | |
]) | |
), | |
# "--button2 'Reply'", | |
# "--button2-action '{}'".format( | |
# ";".join([ | |
# "am start --user 0 -n com.termux/com.termux.app.TermuxActivity", | |
# "tmux new-window -n \"Mail\" \"{}\"".format( | |
# ";".join([ | |
# "mutt -e \\\"macro index - 'l~i{}'; push \\nr\\n\\n\\n\\\"".format(mail.split(":")[0]) | |
# ]) | |
# ) | |
# ]) | |
# ) | |
"--button2 'Preview'", | |
"--button2-action \"{}\"".format( | |
";".join([ | |
"termux-toast '{}'".format(body) | |
]) | |
) | |
]) | |
# print(command) | |
run(command) | |
command="tmux display-message '✉ {}: {}'".format(title,content) | |
run(command) | |
cachefile=os.path.expanduser("~/.mail_notify_cache") | |
cachedata=None | |
if os.path.exists(cachefile): | |
with open (cachefile,"r") as cachefilefile: | |
cachedata=cachefilefile.read() | |
if cachedata: | |
cache=json.loads(cachedata) | |
else: | |
cache=[] | |
maildir=os.path.expanduser("~/mail/new") | |
files=os.listdir(maildir) | |
for mail in files: | |
if mail not in cache: | |
print("processing {}".format(mail)) | |
processmail(mail) | |
cache.append(mail) | |
if cache: | |
curdir=os.path.expanduser("~/mail/cur") | |
curfiles=os.listdir(curdir) | |
newcache=list(cache) | |
for mail in cache: | |
if mail not in files: | |
test="{}:2,".format(mail) | |
if mail not in curfiles and test not in curfiles: | |
print("processing removal of {}".format(mail)) | |
command="termux-notification-remove '{}'".format(mail) | |
run(command) | |
newcache.remove(mail) | |
with open (cachefile,"w") as cachefilefile: | |
cachefilefile.write(json.dumps(newcache)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment