diff options
author | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-12-08 14:06:17 +0100 |
---|---|---|
committer | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-12-08 14:06:17 +0100 |
commit | 410dc24e16b5a9aef0b8e60ec53594c368288a30 (patch) | |
tree | 1fe29ee836a594bd361a3bba4910f87458751626 | |
parent | a00292129478a2b38673fd39cd361c7142166d4d (diff) |
merge raw and read
add json format for read
-rw-r--r-- | src/jwebmail/__init__.py | 26 | ||||
-rw-r--r-- | src/jwebmail/render_mail.py | 10 | ||||
-rw-r--r-- | src/jwebmail/webmail.py | 81 |
3 files changed, 61 insertions, 56 deletions
diff --git a/src/jwebmail/__init__.py b/src/jwebmail/__init__.py index c8be663..0298467 100644 --- a/src/jwebmail/__init__.py +++ b/src/jwebmail/__init__.py @@ -4,7 +4,7 @@ import sys from os import environ from babel import parse_locale -from flask import Flask, abort, g +from flask import Flask, abort, g, redirect, url_for from flask_babel import Babel, get_locale from flask_login import LoginManager, login_required from jinja2 import ChainableUndefined @@ -20,7 +20,6 @@ from .webmail import ( login, logout, move, - rawmail, readmail, sendmail, writemail, @@ -35,7 +34,7 @@ else: toml_read_file = dict(load=toml_load, text=True) -__version__ = "2.0.0.dev5" +__version__ = "2.1.0.dev0" def validate_config(app): @@ -111,10 +110,14 @@ def create_app(): def route(app): - app.add_url_rule("/", view_func=login, methods=["GET", "POST"]) + app.add_url_rule( + "/", + endpoint="root", + view_func=lambda: redirect(url_for("login", lang_code=get_locale().language)), + ) + app.add_url_rule("/<lang_code>/", view_func=login, methods=["GET", "POST"]) - app.add_url_rule("/about", view_func=about) app.add_url_rule("/<lang_code>/about", view_func=about) app.add_url_rule("/<lang_code>/logout", view_func=logout) @@ -131,14 +134,15 @@ def route(app): "/<lang_code>/read/<folder>/<msgid>", endpoint="read", view_func=lr_readmail ) - lr_rawmail = login_required(rawmail) - app.add_url_rule("/raw/<msgid>", endpoint="raw", view_func=lr_rawmail) - app.add_url_rule("/raw/<folder>/<msgid>", endpoint="raw", view_func=lr_rawmail) - lr_writemail = login_required(writemail) app.add_url_rule("/<lang_code>/write", endpoint="write", view_func=lr_writemail) app.add_url_rule( - "/write", endpoint="send", view_func=login_required(sendmail), methods=["POST"] + "/<lang_code>/write", + endpoint="send", + view_func=login_required(sendmail), + methods=["POST"], ) - app.add_url_rule("/move/<folder>", view_func=login_required(move), methods=["POST"]) + app.add_url_rule( + "/<lang_code>/move/<folder>", view_func=login_required(move), methods=["POST"] + ) diff --git a/src/jwebmail/render_mail.py b/src/jwebmail/render_mail.py index 588959c..f597603 100644 --- a/src/jwebmail/render_mail.py +++ b/src/jwebmail/render_mail.py @@ -10,10 +10,13 @@ def render_text_plain(_subtype, content, _path): def render_text_html(_subtype, _content, path): if path: url = url_for( - "raw", msgid=request.view_args["msgid"], path=".".join(map(str, path)) + "read", + msgid=request.view_args["msgid"], + format="raw", + path=".".join(map(str, path)), ) else: - url = url_for("raw", msgid=request.view_args["msgid"]) + url = url_for("read", msgid=request.view_args["msgid"], format="raw") return f'<iframe src="{url}" class="jwm-mail-body-text-html" ></iframe>\n' @@ -69,9 +72,10 @@ def render_multipart(_subtype, content, path): ) ref_url = url_for( - "raw", + "read", msgid=request.view_args["msgid"], path=".".join(map(str, [*path, i])), + format="raw", ) R += "<p>" 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() |