summaryrefslogtreecommitdiff
path: root/src/jwebmail/__init__.py
blob: 405f35eda270cdf3ac86ba1435c63c7c81d126d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os.path as os_path
import pwd
import sys
from os import environ

from flask import Flask, g
from flask_babel import Babel, get_locale
from flask_login import LoginManager, login_required
from jinja2 import ChainableUndefined

from .css import compile_css_command
from .read_mails import load_user
from .render_mail import format_mail
from .view import add_view_funcs
from .webmail import (
    about,
    displayheaders,
    login,
    logout,
    move,
    rawmail,
    readmail,
    sendmail,
    writemail,
)

if sys.version_info >= (3, 11):
    from tomllib import load as toml_load
else:
    from toml import load as toml_load

__version__ = "2.0.0"


def validate_config(app):
    conf = app.config

    assert "@" in conf["JWEBMAIL"]["ADMIN_MAIL"]

    assert os_path.isdir(conf["JWEBMAIL"]["READ_MAILS"]["MAILBOX"])
    assert os_path.isfile(conf["JWEBMAIL"]["READ_MAILS"]["BACKEND"])

    assert pwd.getpwnam(
        conf["JWEBMAIL"]["READ_MAILS"].setdefault("MAILBOX_USER", environ["USER"])
    )
    assert os_path.isfile(
        conf["JWEBMAIL"]["READ_MAILS"].setdefault("AUTHENTICATOR", "qmail-authuser")
    )

    conf["JWEBMAIL"].setdefault("DEFAULT_LANGUAGE", "de")


def create_app():
    app = Flask(__name__)
    app.jinja_options = dict(undefined=ChainableUndefined)

    app.config.from_file("../../jwebmail.toml", load=toml_load, text=False)
    validate_config(app)

    DEFAULT_LANGUAGE = app.config["JWEBMAIL"]["DEFAULT_LANGUAGE"]

    Babel(app, locale_selector=lambda: g.get("lang_code", DEFAULT_LANGUAGE))

    app.cli.add_command(compile_css_command)

    login_manager = LoginManager()
    login_manager.login_view = "login"
    login_manager.user_loader(load_user)
    login_manager.init_app(app)

    add_view_funcs(app)
    route(app)

    @app.context_processor
    def inject_version():
        return {"version": "4.0", "get_locale": get_locale, "format_mail": format_mail}

    @app.url_defaults
    def add_language_code(endpoint, values):
        if "lang_code" in values:
            return
        if app.url_map.is_endpoint_expecting(endpoint, "lang_code"):
            values["lang_code"] = g.get("lang_code", DEFAULT_LANGUAGE)

    @app.url_value_preprocessor
    def pull_lang_code(endpoint, values):
        g.lang_code = (
            values.pop("lang_code", DEFAULT_LANGUAGE) if values else DEFAULT_LANGUAGE
        )

    return app


def route(app):
    app.add_url_rule("/", view_func=login, methods=["GET", "POST"])
    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("/logout", view_func=logout)
    app.add_url_rule("/<lang_code>/logout", view_func=logout)

    dh = login_required(displayheaders)
    app.add_url_rule("/home/", view_func=dh)
    app.add_url_rule("/home/<folder>", view_func=dh)
    app.add_url_rule("/<lang_code>/home/", view_func=dh)
    app.add_url_rule("/<lang_code>/home/<folder>", view_func=dh)

    lr_readmail = login_required(readmail)
    app.add_url_rule("/read/<msgid>", endpoint="read", view_func=lr_readmail)
    app.add_url_rule("/read/<folder>/<msgid>", endpoint="read", view_func=lr_readmail)
    app.add_url_rule(
        "/<lang_code>/read/<msgid>", endpoint="read", view_func=lr_readmail
    )
    app.add_url_rule(
        "/<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("/write", endpoint="write", view_func=lr_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"]
    )

    app.add_url_rule("/move/<folder>", view_func=login_required(move), methods=["POST"])