|
@@ -1,18 +1,20 @@
|
|
|
-import os
|
|
|
import hashlib
|
|
|
-import sys
|
|
|
import json
|
|
|
-import re
|
|
|
import logging
|
|
|
-from uuid import uuid4
|
|
|
+import os
|
|
|
+import re
|
|
|
+import sys
|
|
|
+from datetime import datetime
|
|
|
from email.mime.text import MIMEText
|
|
|
-from subprocess import Popen, PIPE
|
|
|
+from subprocess import PIPE, Popen
|
|
|
+from uuid import uuid4
|
|
|
|
|
|
-from flask import Flask, request, render_template, abort, url_for, make_response
|
|
|
+from flask import (Flask, abort, make_response, render_template, request,
|
|
|
+ url_for)
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
-if 'MESSAGGERIA_SETTING' in os.environ:
|
|
|
- app.config.from_envvar('MESSAGGERIA_SETTING')
|
|
|
+if "MESSAGGERIA_SETTING" in os.environ:
|
|
|
+ app.config.from_envvar("MESSAGGERIA_SETTING")
|
|
|
|
|
|
|
|
|
UPLOAD_DIR = os.getenv("UPLOAD_DIR", "./uploads/")
|
|
@@ -21,15 +23,15 @@ logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
|
|
|
|
def sendmail(sender, to, subject, body):
|
|
|
- #msg = MIMEText(body)
|
|
|
- #msg["From"] = sender
|
|
|
- #msg["To"] = to
|
|
|
- #msg["Subject"] = subject
|
|
|
- #p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
|
|
|
- args = ["/usr/bin/mail", "-s", subject, '--'] + to
|
|
|
+ # msg = MIMEText(body)
|
|
|
+ # msg["From"] = sender
|
|
|
+ # msg["To"] = to
|
|
|
+ # msg["Subject"] = subject
|
|
|
+ # p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
|
|
|
+ args = ["/usr/bin/mail", "-s", subject, "--"] + to
|
|
|
p = Popen(args, stdin=PIPE)
|
|
|
# p.communicate(msg.as_bytes())
|
|
|
- p.communicate(body.encode('utf8'))
|
|
|
+ p.communicate(body.encode("utf8"))
|
|
|
|
|
|
|
|
|
def read_config():
|
|
@@ -46,7 +48,7 @@ def read_config():
|
|
|
|
|
|
@app.route("/")
|
|
|
def home():
|
|
|
- return 'casella di destinazione non specificata'
|
|
|
+ return "casella di destinazione non specificata"
|
|
|
|
|
|
|
|
|
@app.route("/<site>")
|
|
@@ -60,10 +62,10 @@ def site(site):
|
|
|
|
|
|
@app.route("/upload/<site>", methods=["POST"])
|
|
|
def upload(site):
|
|
|
- temp_fname = '_%s.ogg' % uuid4().hex
|
|
|
+ temp_fname = "_%s.ogg" % uuid4().hex
|
|
|
temp_fpath = os.path.join(UPLOAD_DIR, temp_fname)
|
|
|
# prima scrivi su un file temporaneo, poi fai rename
|
|
|
- h = hashlib.new('sha1')
|
|
|
+ h = hashlib.new("sha1")
|
|
|
with open(temp_fpath, "wb") as buf:
|
|
|
while True:
|
|
|
some_data = request.stream.read(1024)
|
|
@@ -72,22 +74,30 @@ def upload(site):
|
|
|
buf.write(some_data)
|
|
|
h.update(some_data)
|
|
|
# rinomina con l'hash
|
|
|
- app.logger.info('hash = %s', h.hexdigest())
|
|
|
- fname = '%s.ogg' % h.hexdigest()
|
|
|
+ app.logger.info("hash = %s", h.hexdigest())
|
|
|
+ fname = "%s.ogg" % h.hexdigest()
|
|
|
os.rename(temp_fpath, os.path.join(UPLOAD_DIR, fname))
|
|
|
if site in read_config()["sites"]:
|
|
|
to = read_config()["sites"][site].get("email", [])
|
|
|
if to:
|
|
|
- sender = os.getenv('MAIL_FROM', '')
|
|
|
+ sender = os.getenv("MAIL_FROM", "")
|
|
|
if not sender:
|
|
|
- app.logger.info('Not sending email (unconfigured FROM)')
|
|
|
+ app.logger.info("Not sending email (unconfigured FROM)")
|
|
|
else:
|
|
|
- app.logger.debug('Sending email for `%s` to `%s`',
|
|
|
- site, ';'.join(to))
|
|
|
- url = url_for("dl", fname=fname, _external=True, _scheme='https')
|
|
|
- sendmail(sender, to,
|
|
|
- subject='Nuovo messaggio (%s)' % site,
|
|
|
- body="Ascoltalo su\n%s" % url)
|
|
|
+ app.logger.debug("Sending email for `%s` to `%s`", site, ";".join(to))
|
|
|
+ url = url_for("dl", fname=fname, _external=True, _scheme="https")
|
|
|
+ sendmail(
|
|
|
+ sender,
|
|
|
+ to,
|
|
|
+ subject="Nuovo messaggio (%s)" % site,
|
|
|
+ body="Alle {now:%H:%M} hai ricevuto un messaggio nella segreteria di {site}\n"
|
|
|
+ "Puoi ascoltarlo cliccando su\n{url}\n"
|
|
|
+ "Per scaricare l'audio, troverai un link dentro la pagina"
|
|
|
+ "---\n"
|
|
|
+ "Il servizio è gentilmente offerto da degenerazione.xyz".format(
|
|
|
+ now=datetime.now(), site=site, url=url
|
|
|
+ ),
|
|
|
+ )
|
|
|
return fname
|
|
|
|
|
|
|
|
@@ -99,7 +109,8 @@ def play(fname):
|
|
|
fpath = os.path.join(UPLOAD_DIR, fname)
|
|
|
if not os.path.exists(fpath):
|
|
|
abort(404)
|
|
|
- return render_template('player.html', fname=fname)
|
|
|
+ return render_template("player.html", fname=fname)
|
|
|
+
|
|
|
|
|
|
@app.route("/download/<fname>")
|
|
|
def dl(fname):
|
|
@@ -112,9 +123,10 @@ def dl(fname):
|
|
|
with open(fpath, "rb") as buf:
|
|
|
content = buf.read()
|
|
|
r = make_response(content)
|
|
|
- r.headers['Content-Type'] = 'audio/ogg' # TODO: better detect
|
|
|
+ r.headers["Content-Type"] = "audio/ogg" # TODO: better detect
|
|
|
return r
|
|
|
|
|
|
-@app.route('/info/license')
|
|
|
+
|
|
|
+@app.route("/info/license")
|
|
|
def license():
|
|
|
- return render_template('license.html')
|
|
|
+ return render_template("license.html")
|