diff options
Diffstat (limited to 'src/jwebmail/webmail.py')
-rw-r--r-- | src/jwebmail/webmail.py | 81 |
1 files changed, 39 insertions, 42 deletions
diff --git a/src/jwebmail/webmail.py b/src/jwebmail/webmail.py index 5ddc5d6..af60774 100644 --- a/src/jwebmail/webmail.py +++ b/src/jwebmail/webmail.py @@ -1,16 +1,7 @@ from urllib.parse import urlparse -from flask import ( - abort, - current_app, - flash, - g, - redirect, - render_template, - request, - url_for, -) -from flask_babel import get_locale, gettext, lazy_gettext +from flask import abort, current_app, flash, redirect, render_template, request, url_for +from flask_babel import gettext, lazy_gettext from flask_login import current_user, login_user, logout_user from flask_paginate import Pagination, get_page_parameter, get_per_page_parameter from flask_wtf import FlaskForm @@ -66,9 +57,7 @@ def login(): if urlparse(nxt).netloc: abort(401) - return redirect( - nxt or url_for("displayheaders", lang_code=get_locale().language), 303 - ) + return redirect(nxt or url_for("displayheaders"), 303) else: warn = gettext("login failed!") elif request.method == "POST": @@ -146,12 +135,43 @@ def displayheaders(folder=""): def readmail(msgid, folder=""): - try: - mail = get_read_mails_logged_in().show(folder, msgid) - except QMAuthError: - return render_template("not_found.html"), 404 + format = request.args.get("format", "html").lower() + + if format == "html": + try: + mail = get_read_mails_logged_in().show(folder, msgid) + except QMAuthError: + return render_template("not_found.html"), 404 + + return render_template("readmail.html", msg=mail, folder=folder) + + elif format == "raw": + path = request.args.get("path", "") - return render_template("readmail.html", msg=mail, folder=folder) + content = get_read_mails_logged_in().raw(folder, msgid, path) + + headers = [] + + cd = content["head"].get("content_disposition") + if cd and cd.lower() == "attachment": + headers.append( + ( + "Content-Disposition", + f"attachment; filename={content['head']['filename']}", + ) + ) + ct = to_mime_type(content["head"]) + if ct.startswith("text/"): + ct += "; charset=UTF-8" + headers.append(("Content-Type", ct)) + + return content["body"], headers + + elif format == "json": + mail = get_read_mails_logged_in().show(folder, msgid) + return mail + else: + abort(404) def writemail(): @@ -174,29 +194,6 @@ def move(folder): return redirect(url_for("displayheaders"), 303) -def rawmail(msgid, folder=""): - path = request.args.get("path", "") - - content = get_read_mails_logged_in().raw(folder, msgid, path) - - headers = [] - - cd = content["head"].get("content_disposition") - if cd and cd.lower() == "attachment": - headers.append( - ( - "Content-Disposition", - f"attachment; filename={content['head']['filename']}", - ) - ) - ct = to_mime_type(content["head"]) - if ct.startswith("text/"): - ct += "; charset=UTF-8" - headers.append(("Content-Type", ct)) - - return content["body"], headers - - def sendmail(): form = WriteForm() |