diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-10-24 16:28:32 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-10-24 16:28:32 +0200 |
commit | 5a042eb3c64960b79ed1de3dfb735c79e51d8894 (patch) | |
tree | 6dcea5c19ce5a84dd580efc2c809b3ec403cbecd | |
parent | s/mail:/mailto:/g (diff) | |
download | mu4web-5a042eb3c64960b79ed1de3dfb735c79e51d8894.tar.gz mu4web-5a042eb3c64960b79ed1de3dfb735c79e51d8894.tar.xz |
General cleanup.
-rw-r--r-- | main.py | 115 | ||||
-rw-r--r-- | setup.cfg | 2 |
2 files changed, 63 insertions, 54 deletions
@@ -15,10 +15,11 @@ from http.server import HTTPServer, BaseHTTPRequestHandler parser = BytesParser(policy=email.policy.default) + def get_mail(id): cmd = subprocess.run(['mu', 'find', '-u', f'i:{id}', '--fields', 'l'], - stdout=PIPE) + stdout=PIPE) filename = cmd.stdout.decode('UTF-8').strip() with open(filename, "rb") as f: @@ -31,12 +32,8 @@ def header_format(key, value): lst = [] for addr in value.addresses: mail_addr = f'{addr.username}@{addr.domain}' - lst.append(('li', - str(addr.display_name), - ' <', - ('a', {'href': f'mailto:{mail_addr}'}, mail_addr), - '>', - )) + addr = ' <', ('a', {'href': f'mailto:{mail_addr}'}, mail_addr), '>' + lst.append(('li', str(addr.display_name)) + addr) return ('ul', lst) elif key == 'from': value = value.addresses[0] @@ -49,19 +46,21 @@ def header_format(key, value): elif key == 'in-reply-to': # type(value) == email.headerregistry._UnstructuredHeader id = str(value).strip("<>") - return ['<', ('a', {'href': '?'+urlencode({'id': id})}, id), '>'] + return ['<', ('a', {'href': '?' + urlencode({'id': id})}, id), '>'] else: return value + standalones = ['hr', 'br', 'meta'] + def _render_document(document): if type(document) == tuple: tag, *body = document if body and type(body[0]) == dict: print(body[0]) attributes = ' '.join(f'{a}="{html.escape(b)}"' - for (a, b) in body[0].items()) + for a, b in body[0].items()) body = body[1:] start = f'<{tag} {attributes}>' else: @@ -79,11 +78,12 @@ def _render_document(document): # strings, and everything else return html.escape(str(document)) + def render_document(document): return '<!doctype html>\n' + _render_document(document) -style = lambda: """ +style = lambda: """ nav { display: block; width: 100%; @@ -108,6 +108,8 @@ style = lambda: """ } """ + + def attachement_tree(mail): ct = mail.get_content_type() fn = mail.get_filename() @@ -131,108 +133,110 @@ def attachement_tree(mail): def login_page(returnto=None): - return ('form', { 'action': '/login', 'method': 'POST' }, + return ('form', {'action': '/login', 'method': 'POST'}, ('input', {'name': 'username', 'placeholder': 'Username'}), ('input', {'type': 'password', - 'placeholder': 'Password', - 'name': 'password'}), + 'placeholder': 'Password', + 'name': 'password'}), ('input', {'type': 'hidden', - 'name': 'returnto', - 'value': returnto}) + 'name': 'returnto', + 'value': returnto}) if returnto else [], ('input', {'type': 'submit'}), ) + def user_info(username): return [('span', username), ('form', {'action': '/logout', 'method': 'POST'}, ('input', {'type': 'submit', 'value': 'Logga ut'}))] + def login_prompt(): return ('a', {'href': '/login'}, 'Logga in') + def page_base(title=None, body=[], username=None): return ('html', ('head', - ('meta', { 'charset': 'utf-8' }), - ('title', title), - ('style', style), - ), - ('body', - ('nav', - user_info(username) if username else login_prompt() - ), - body)) + ('meta', {'charset': 'utf-8'}), + ('title', title), + ('style', style), + ), + ('body', + ('nav', + user_info(username) if username else login_prompt() + ), + body)) def response_for(id, username=None): mail = get_mail(id) - headers = {} for (key, value) in mail.items(): headers[key.lower()] = value head = [] for h in ['date', 'from', 'to', 'cc', 'bcc', 'subject', 'x-original-to', - 'in-reply-to']: + 'in-reply-to']: if x := headers.get(h.lower()): head += [('dt', h.title()), ('dd', header_format(h.lower(), x))] -# print() - body_part = mail.get_body(preferencelist=('html','plain')) + body_part = mail.get_body(preferencelist=('html', 'plain')) ct = body_part.get_content_type() if ct == 'text/html': body = lambda: body_part.get_content() else: body = ('pre', body_part.get_content()) - + if t := headers.get('subject'): title = f'Mail — {t}' else: title = 'Mail' - main_body = [ ('dl', *head), - ('hr',), - ('main', body), - ('hr',), - ('ul', attachement_tree(mail)),] - html_str = render_document( - page_base(title=title, body=main_body, username=username)) + main_body = [('dl', *head), + ('hr',), + ('main', body), + ('hr',), + ('ul', attachement_tree(mail)), + ] + html_str = render_document(page_base(title=title, + body=main_body, + username=username)) return html_str - def index_page(username): ids = [ - 'CAEzixGsw-4zJ8_ejK_vDgmcQ9s-MbBc-ho+HL4arV4a+ghOOPg@mail.gmail.com', - 'CA+pcBt-gLb0GtbFOjJ5_7Q_WXtqApVPQ9w-3O7GH=VqCEQat6g@mail.gmail.com', - ] + 'CAEzixGsw-4zJ8_ejK_vDgmcQ9s-MbBc-ho+HL4arV4a+ghOOPg@mail.gmail.com', + 'CA+pcBt-gLb0GtbFOjJ5_7Q_WXtqApVPQ9w-3O7GH=VqCEQat6g@mail.gmail.com', + ] - body = [ - ('h1', "Sample ID's"), + body = [('h1', "Sample ID's"), ('ul', - [('li', ('a', {'href': '?'+urlencode({'id': id})}, id)) - for id in ids] - ), + [('li', ('a', {'href': '?' + urlencode({'id': id})}, id)) + for id in ids] + ), ] - return render_document( - page_base( - title='Mail index', - body=body, - username=username - )) + return render_document(page_base(title='Mail index', + body=body, + username=username + )) + valid_session_cookies = {} + def validate_session_cookie(cookie): return valid_session_cookies.get(cookie.value) + def remove_session_cookie(cookie): if valid_session_cookies.get(cookie.value): del valid_session_cookies[cookie.value] @@ -240,8 +244,10 @@ def remove_session_cookie(cookie): cookie.expire = 0 return cookie + passwords = password.Passwords('passwords.json') + def new_session_cookie(username): global valid_session_cookies m = http.cookies.Morsel() @@ -250,6 +256,7 @@ def new_session_cookie(username): m.set('session', unique, unique) return m + class Handler(BaseHTTPRequestHandler): def do_GET(self): url = urlparse(self.path) @@ -295,10 +302,9 @@ class Handler(BaseHTTPRequestHandler): # TODO do something sensible here pass - def do_POST(self): url = urlparse(self.path) - query = parse_qs(url.query) + # query = parse_qs(url.query) cookies = BaseCookie(self.headers.get('Cookie')) logged_in = None if c := cookies.get('session'): @@ -307,7 +313,8 @@ class Handler(BaseHTTPRequestHandler): print(valid_session_cookies) if url.path == '/login': - cl = content_length = self.headers.get('content-length') + # cl = content_length = self.headers.get('content-length') + cl = self.headers.get('content-length') data = parse_qs(self.rfile.read(int(cl))) username = b''.join(data[b'username']).decode('UTF-8') password = b''.join(data[b'password']).decode('UTF-8') diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..1b76850 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[flake8] +ignore = E731 |