You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

66 lines
1.3 KiB

# vim: set fileencoding=utf-8 :
# Minimail
# Copyleft 2019, p≡p foundation
# this file is under GNU General Public License 3.0
from secrets import token_urlsafe
from itertools import compress
from functools import partial
from time import sleep
def unlock(inbox):
lockfile = inbox / "lock"
try:
lockfile.unlink()
except:
pass
class Lock:
def __init__(self, inbox):
self.inbox = inbox
def __exit__(self, *exc):
unlock(self.inbox)
def __enter__(self):
lockfile = self.inbox / "lock"
while lockfile.is_file():
sleep(1)
lockfile.touch()
def send(inbox, msg):
with Lock(inbox):
name = token_urlsafe(16) + ".eml"
with open(inbox / name, "wb") as f:
f.write(str(msg).encode())
def newer(file1, file2):
if not file2.is_file():
return False
elif not file1.is_file():
return True
stat1 = file1.stat()
stat2 = file2.stat()
return stat2.st_mtime > stat1.st_mtime
def recv_all(inbox, marker):
with Lock(inbox):
r = []
while not r:
for f in compress(inbox.glob("*.eml"), partial(newer, file1=marker)):
t = f.readall()
r.append(t)
if not r:
sleep(1)
marker.touch(exist_ok=True)
return r