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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
import sys
from datetime import datetime
from os import environ
from shutil import which
from babel import parse_locale
from flask import Flask, abort, g, redirect, request_finished, url_for
from flask_babel import Babel, get_locale
from flask_login import LoginManager, login_required
from flask_wtf.csrf import CSRFProtect
from jinja2 import ChainableUndefined
from markupsafe import Markup
from werkzeug.middleware.proxy_fix import ProxyFix
from .css import compile_css_command
from .read_mails import load_user
from .render_mail import format_mail
from .webmail import (
about,
displayheaders,
login,
logout,
move,
readmail,
remove,
sendmail,
writemail,
)
if sys.version_info >= (3, 11):
from tomllib import load as toml_load
toml_read_file = dict(load=toml_load, text=False)
else:
from toml import load as toml_load
toml_read_file = dict(load=toml_load, text=True)
__version__ = "2.8.1.dev2"
csrf = CSRFProtect()
def validate_config(app):
conf = app.config
assert "@" in conf["JWEBMAIL"]["ADMIN_MAIL"]
assert which(conf["JWEBMAIL"]["READ_MAILS"]["BACKEND"])
assert which(
conf["JWEBMAIL"]["READ_MAILS"].setdefault("AUTHENTICATOR", "qmail-authuser")
)
def create_app():
app = Flask(__name__)
app.jinja_options = dict(undefined=ChainableUndefined)
if app.config["DEBUG"]:
app.config.from_file("../../jwebmail.toml", **toml_read_file)
else:
app.config.from_file(environ["JWEBMAIL_CONFIG"], **toml_read_file)
if app.config["JWEBMAIL"].get("PROXY"):
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=True, x_proto=True, x_host=True, x_prefix=True
)
validate_config(app)
csrf.init_app(app)
babel = Babel(app, locale_selector=lambda: g.get("lang_code"))
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)
route(app)
tr_dict = {
"en": str.maketrans({" ": " "}),
"de": str.maketrans({".": ",", " ": " "}),
}
@app.template_filter("num_localize")
def num_localize(txt):
if str(get_locale()).startswith("de"):
return Markup(txt.translate(tr_dict["de"]))
else:
return Markup(txt.translate(tr_dict["en"]))
@app.context_processor
def inject_version():
return {
"version": __version__,
"get_locale": get_locale,
"format_mail": format_mail,
"parse_iso_date": datetime.fromisoformat,
}
@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", babel.default_locale.language)
@app.url_value_preprocessor
def pull_lang_code(endpoint, values):
if values and values.get("lang_code"):
try:
pl = parse_locale(values["lang_code"])
if pl[0] in ["en", "de"]:
g.lang_code = values.pop("lang_code")
else:
abort(404)
except ValueError:
abort(404)
@request_finished.connect_via(app)
def close_qma(_app, **_):
if "read_mails" in g:
g.read_mails.close()
g.read_mails = None
return app
def route(app):
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("/<lang_code>/about", view_func=about)
app.add_url_rule("/<lang_code>/logout", view_func=logout)
dh = login_required(displayheaders)
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(
"/<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_writemail = login_required(writemail)
app.add_url_rule("/<lang_code>/write", endpoint="write", view_func=lr_writemail)
app.add_url_rule(
"/<lang_code>/write",
endpoint="send",
view_func=login_required(sendmail),
methods=["POST"],
)
lr_move = login_required(move)
app.add_url_rule("/<lang_code>/move/", view_func=lr_move, methods=["POST"])
app.add_url_rule("/<lang_code>/move/<folder>", view_func=lr_move, methods=["POST"])
lr_remove = login_required(remove)
app.add_url_rule(
"/<lang_code>/remove/",
view_func=lr_remove,
methods=["POST"],
)
app.add_url_rule(
"/<lang_code>/remove/<folder>",
view_func=lr_remove,
methods=["POST"],
)
|