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
|
import os.path as os_path
import pwd
import sys
from os import environ
from babel import parse_locale
from flask import Flask, abort, g, redirect, 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 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 .view import add_view_funcs
from .webmail import (
about,
displayheaders,
login,
logout,
move,
readmail,
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.2.1.dev3"
csrf = CSRFProtect()
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.abspath(conf["JWEBMAIL"]["READ_MAILS"]["BACKEND"])
assert pwd.getpwnam(
conf["JWEBMAIL"]["READ_MAILS"].setdefault("MAILBOX_USER", environ["USER"])
)
assert os_path.abspath(
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=1, x_proto=1, x_host=1, x_prefix=1)
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)
add_view_funcs(app)
route(app)
@app.context_processor
def inject_version():
return {
"version": __version__,
"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", 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)
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"],
)
app.add_url_rule(
"/<lang_code>/move/<folder>", view_func=login_required(move), methods=["POST"]
)
|