utils.py 7.26 KB
Newer Older
Robin Sonnabend's avatar
Robin Sonnabend committed
1
from flask import render_template, request
2
3
4
5

import random
import string
import regex
6
import math
7
8
9
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
10
from email.mime.application import MIMEApplication
11
from datetime import datetime, date, timedelta
Robin Sonnabend's avatar
Robin Sonnabend committed
12
13
import requests
from io import BytesIO
14
import ipaddress
15
16
from socket import getfqdn
from uuid import uuid4
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

import config

def random_string(length):
    return "".join((random.choice(string.ascii_letters) for i in range(length)))

def is_past(some_date):
    return (datetime.now() - some_date).total_seconds() > 0

def encode_kwargs(kwargs):
    encoded_kwargs = {}
    for key in kwargs:
        value = kwargs[key]
        if hasattr(value, "id"):
            encoded_kwargs[key] = (type(value), value.id, True)
        else:
            encoded_kwargs[key] = (type(value), value, False)
    return encoded_kwargs

def decode_kwargs(encoded_kwargs):
    kwargs = {}
    for name in encoded_kwargs:
        kind, id, from_db = encoded_kwargs[name]
        if from_db:
            kwargs[name] = kind.query.filter_by(id=id).first()
        else:
            kwargs[name] = id
    return kwargs
        

class UrlManager:
    def __init__(self, config):
        self.pattern = regex.compile(r"(?:(?<proto>https?):\/\/)?(?<hostname>[[:alnum:]_.]+(?:\:[[:digit:]]+)?)?(?<path>(?:\/[[:alnum:]_#]*)+)?(?:\?(?<params>.*))?")
        self.base = "{}://{}{}{}"
        self.proto = getattr(config, "URL_PROTO", "https")
        self.root = getattr(config, "URL_ROOT", "example.com")
        self.path = getattr(config, "URL_PATH", "/")
        self.params = getattr(config, "URL_PARAMS", "")

    def complete(self, url):
        match = self.pattern.match(url)
        if match is None:
            return None
        proto = match.group("proto") or self.proto
        root = match.group("hostname") or self.root
        path = match.group("path") or self.path
        params = match.group("params") or self.params
        return self.base.format(proto, root, path, "?" + params if len(params) > 0 else "")

Robin Sonnabend's avatar
Robin Sonnabend committed
66
#url_manager = UrlManager(config)
67
68
69
70
71
72
73
74

class MailManager:
    def __init__(self, config):
        self.active = getattr(config, "MAIL_ACTIVE", False)
        self.from_addr = getattr(config, "MAIL_FROM", "")
        self.hostname = getattr(config, "MAIL_HOST", "")
        self.username = getattr(config, "MAIL_USER", "")
        self.password = getattr(config, "MAIL_PASSWORD", "")
75
        self.use_tls = getattr(config, "MAIL_USE_TLS", True)
76
        self.use_starttls = getattr(config, "MAIL_USE_STARTTLS", False)
77

78
    def send(self, to_addr, subject, content, appendix=None, reply_to=None):
79
80
81
        if (not self.active
            or not self.hostname
            or not self.from_addr):
Robin Sonnabend's avatar
Robin Sonnabend committed
82
            return
83
        msg = MIMEMultipart("mixed") # todo: test if clients accept attachment-free mails set to multipart/mixed
Robin Sonnabend's avatar
Robin Sonnabend committed
84
85
86
        msg["From"] = self.from_addr
        msg["To"] = to_addr
        msg["Subject"] = subject
87
        msg["Message-ID"] = "<{}@{}>".format(uuid4(), getfqdn())
88
89
        if reply_to is not None:
            msg["Reply-To"] = reply_to
Robin Sonnabend's avatar
Robin Sonnabend committed
90
        msg.attach(MIMEText(content, _charset="utf-8"))
91
92
93
94
95
        if appendix is not None:
            for name, file_like in appendix:
                part = MIMEApplication(file_like.read(), "octet-stream")
                part["Content-Disposition"] = 'attachment; filename="{}"'.format(name)
                msg.attach(part)
96
        server = (smtplib.SMTP_SSL if self.use_tls else smtplib.SMTP)(self.hostname)
97
98
        if self.use_starttls:
            server.starttls()
99
100
        if self.username not in [None, ""] and self.password not in [None, ""]:
            server.login(self.username, self.password)
101
        server.sendmail(self.from_addr, to_addr.split(","), msg.as_string())
Robin Sonnabend's avatar
Robin Sonnabend committed
102
        server.quit()
103
104

mail_manager = MailManager(config)
Robin Sonnabend's avatar
Robin Sonnabend committed
105
106
107
108
109
110
111
112
113
114

def get_first_unused_int(numbers):
    positive_numbers = [number for number in numbers if number >= 0]
    if len(positive_numbers) == 0:
        return 0
    highest = max(positive_numbers)
    for given, linear in zip(positive_numbers, range(highest+1)):
        if linear < given:
            return linear
    return highest + 1
Robin Sonnabend's avatar
Robin Sonnabend committed
115

116
117
def normalize_pad(pad):
    return pad.replace(" ", "_")
Robin Sonnabend's avatar
Robin Sonnabend committed
118
def get_etherpad_url(pad):
119
    return "{}/p/{}".format(config.ETHERPAD_URL, normalize_pad(pad))
Robin Sonnabend's avatar
Robin Sonnabend committed
120
def get_etherpad_export_url(pad):
121
    return "{}/p/{}/export/txt".format(config.ETHERPAD_URL, normalize_pad(pad))
Robin Sonnabend's avatar
Robin Sonnabend committed
122
def get_etherpad_import_url(pad):
123
    return "{}/p/{}/import".format(config.ETHERPAD_URL, normalize_pad(pad))
Robin Sonnabend's avatar
Robin Sonnabend committed
124
125

def get_etherpad_text(pad):
126
    req = requests.get(get_etherpad_export_url(pad))
Robin Sonnabend's avatar
Robin Sonnabend committed
127
128
129
    return req.text

def set_etherpad_text(pad, text, only_if_default=True):
Administrator's avatar
Administrator committed
130
    print(pad)
Robin Sonnabend's avatar
Robin Sonnabend committed
131
132
133
134
135
136
    if only_if_default:
        current_text = get_etherpad_text(pad)
        if current_text != config.EMPTY_ETHERPAD and len(current_text.strip()) > 0:
            return False
    file_like = BytesIO(text.encode("utf-8"))
    files = {"file": file_like}
Administrator's avatar
Administrator committed
137
138
139
    url = get_etherpad_import_url(pad)
    print(url)
    req = requests.post(url, files=files)
Robin Sonnabend's avatar
Robin Sonnabend committed
140
141
    return req.status_code == 200
    
Robin Sonnabend's avatar
Robin Sonnabend committed
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
def split_terms(text, quote_chars="\"'", separators=" \t\n"):
    terms = []
    in_quote = False
    last_quote_char = ""
    current_term = ""
    for char in text:
        if in_quote:
            if char != last_quote_char:
                current_term += char
            else:
                in_quote = False
                last_quote_char = ""
                terms.append(current_term)
                current_term = ""
        else:
            if char in separators:
                if len(current_term) > 0:
                    terms.append(current_term)
                    current_term = ""
            else:
                if char in quote_chars and len(current_term) == 0:
                    in_quote = True
                    last_quote_char = char
                else:
                    current_term += char
    if len(current_term) > 0:
        terms.append(current_term)
    return terms
Robin Sonnabend's avatar
Robin Sonnabend committed
170
171
172
173
174
175

def optional_int_arg(name):
    try:
        return int(request.args.get(name))
    except (ValueError, TypeError):
        return None
176
177
178
179
180
181
182
183
184
185
186

def add_line_numbers(text):
    raw_lines = text.splitlines()
    linenumber_length = math.ceil(math.log10(len(raw_lines)) + 1)
    lines = []
    for linenumber, line in enumerate(raw_lines):
        lines.append("{} {}".format(
            str(linenumber+1).rjust(linenumber_length),
            line
        ))
    return "\n".join(lines)
187
188
189
190
191
192
193
194
195
196
197
198
199

def check_ip_in_networks(networks_string):
    address = ipaddress.ip_address(request.remote_addr)
    if address == ipaddress.ip_address("127.0.0.1") and "X-Real-Ip" in request.headers:
        address = ipaddress.ip_address(request.headers["X-Real-Ip"])
    try:
        for network_string in networks_string.split(","):
            network = ipaddress.ip_network(network_string.strip())
            if address in network:
                return True
        return False
    except ValueError:
        return False
Robin Sonnabend's avatar
Robin Sonnabend committed
200
201
202
203
204
205
206
207

def fancy_join(values, sep1=" und ", sep2=", "):
    values = list(values)
    if len(values) <= 1:
        return "".join(values)
    last = values[-1]
    start = values[:-1]
    return "{}{}{}".format(sep2.join(start), sep1, last)
Robin Sonnabend's avatar
Robin Sonnabend committed
208
209
210

def footnote_hash(text, length=5):
    return str(sum(ord(c) * i for i, c in enumerate(text)) % 10**length)