2019-12-15 16:17:37 +01:00
|
|
|
import re
|
2020-03-15 22:32:48 +01:00
|
|
|
from email.utils import parseaddr
|
2019-12-15 16:17:37 +01:00
|
|
|
|
|
|
|
from flask import render_template, request, redirect, url_for, flash
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import StringField, validators, ValidationError
|
|
|
|
|
|
|
|
from app.config import EMAIL_DOMAIN
|
|
|
|
from app.dashboard.base import dashboard_bp
|
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
2020-03-17 11:51:40 +01:00
|
|
|
from app.models import Alias, Contact
|
2019-12-15 16:17:37 +01:00
|
|
|
from app.utils import random_string
|
|
|
|
|
|
|
|
|
|
|
|
def email_validator():
|
|
|
|
"""validate email address. Handle both only email and email with name:
|
|
|
|
- ab@cd.com
|
|
|
|
- AB CD <ab@cd.com>
|
|
|
|
|
|
|
|
"""
|
|
|
|
message = "Invalid email format. Email must be either email@example.com or *First Last <email@example.com>*"
|
|
|
|
|
|
|
|
def _check(form, field):
|
|
|
|
email = field.data
|
2020-02-18 05:54:51 +01:00
|
|
|
email = email.strip()
|
2019-12-15 16:17:37 +01:00
|
|
|
email_part = email
|
|
|
|
|
|
|
|
if "<" in email and ">" in email:
|
|
|
|
if email.find("<") + 1 < email.find(">"):
|
|
|
|
email_part = email[email.find("<") + 1 : email.find(">")].strip()
|
|
|
|
|
|
|
|
if re.match(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$", email_part):
|
|
|
|
return
|
|
|
|
|
|
|
|
raise ValidationError(message)
|
|
|
|
|
|
|
|
return _check
|
|
|
|
|
|
|
|
|
|
|
|
class NewContactForm(FlaskForm):
|
|
|
|
email = StringField(
|
|
|
|
"Email", validators=[validators.DataRequired(), email_validator()]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-01-12 22:23:05 +01:00
|
|
|
@dashboard_bp.route("/alias_contact_manager/<alias_id>/", methods=["GET", "POST"])
|
2019-12-15 16:17:37 +01:00
|
|
|
@dashboard_bp.route(
|
2020-03-17 10:56:59 +01:00
|
|
|
"/alias_contact_manager/<alias_id>/<contact_id>", methods=["GET", "POST"]
|
2019-12-15 16:17:37 +01:00
|
|
|
)
|
|
|
|
@login_required
|
2020-03-17 10:56:59 +01:00
|
|
|
def alias_contact_manager(alias_id, contact_id=None):
|
2020-03-17 11:51:40 +01:00
|
|
|
alias = Alias.get(alias_id)
|
2019-12-15 16:17:37 +01:00
|
|
|
|
|
|
|
# sanity check
|
2020-03-17 11:51:40 +01:00
|
|
|
if not alias:
|
2019-12-15 16:17:37 +01:00
|
|
|
flash("You do not have access to this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
if alias.user_id != current_user.id:
|
2019-12-15 16:17:37 +01:00
|
|
|
flash("You do not have access to this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
new_contact_form = NewContactForm()
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
if request.form.get("form-name") == "create":
|
|
|
|
if new_contact_form.validate():
|
2020-02-18 05:54:51 +01:00
|
|
|
contact_email = new_contact_form.email.data.strip()
|
2019-12-15 16:17:37 +01:00
|
|
|
|
|
|
|
# generate a reply_email, make sure it is unique
|
|
|
|
# not use while to avoid infinite loop
|
2020-03-17 10:56:59 +01:00
|
|
|
reply_email = f"ra+{random_string(25)}@{EMAIL_DOMAIN}"
|
2019-12-15 16:17:37 +01:00
|
|
|
for _ in range(1000):
|
|
|
|
reply_email = f"ra+{random_string(25)}@{EMAIL_DOMAIN}"
|
2020-03-17 10:56:59 +01:00
|
|
|
if not Contact.get_by(reply_email=reply_email):
|
2019-12-15 16:17:37 +01:00
|
|
|
break
|
|
|
|
|
2020-03-15 22:32:48 +01:00
|
|
|
_, website_email = parseaddr(contact_email)
|
2019-12-15 16:17:37 +01:00
|
|
|
|
|
|
|
# already been added
|
2020-03-17 12:01:18 +01:00
|
|
|
if Contact.get_by(alias_id=alias.id, website_email=website_email):
|
2019-12-15 16:17:37 +01:00
|
|
|
flash(f"{website_email} is already added", "error")
|
|
|
|
return redirect(
|
2020-01-12 22:23:05 +01:00
|
|
|
url_for("dashboard.alias_contact_manager", alias_id=alias_id)
|
2019-12-15 16:17:37 +01:00
|
|
|
)
|
|
|
|
|
2020-03-17 10:56:59 +01:00
|
|
|
contact = Contact.create(
|
2020-03-17 12:01:18 +01:00
|
|
|
alias_id=alias.id,
|
2019-12-15 16:17:37 +01:00
|
|
|
website_email=website_email,
|
|
|
|
website_from=contact_email,
|
|
|
|
reply_email=reply_email,
|
|
|
|
)
|
|
|
|
|
|
|
|
LOG.d("create reverse-alias for %s", contact_email)
|
|
|
|
db.session.commit()
|
2019-12-30 17:43:38 +01:00
|
|
|
flash(f"Reverse alias for {contact_email} is created", "success")
|
2019-12-15 16:17:37 +01:00
|
|
|
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.alias_contact_manager",
|
2020-01-12 22:23:05 +01:00
|
|
|
alias_id=alias_id,
|
2020-03-17 10:56:59 +01:00
|
|
|
contact_id=contact.id,
|
2019-12-15 16:17:37 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
elif request.form.get("form-name") == "delete":
|
2020-03-17 10:56:59 +01:00
|
|
|
contact_id = request.form.get("contact-id")
|
|
|
|
contact = Contact.get(contact_id)
|
2019-12-15 16:17:37 +01:00
|
|
|
|
2020-03-17 10:56:59 +01:00
|
|
|
if not contact:
|
2019-12-15 16:17:37 +01:00
|
|
|
flash("Unknown error. Refresh the page", "warning")
|
2020-01-12 22:23:05 +01:00
|
|
|
return redirect(
|
|
|
|
url_for("dashboard.alias_contact_manager", alias_id=alias_id)
|
|
|
|
)
|
2020-03-17 12:01:18 +01:00
|
|
|
elif contact.alias_id != alias.id:
|
2019-12-15 16:17:37 +01:00
|
|
|
flash("You cannot delete reverse-alias", "warning")
|
2020-01-12 22:23:05 +01:00
|
|
|
return redirect(
|
|
|
|
url_for("dashboard.alias_contact_manager", alias_id=alias_id)
|
|
|
|
)
|
2019-12-15 16:17:37 +01:00
|
|
|
|
2020-03-17 10:56:59 +01:00
|
|
|
contact_name = contact.website_from
|
|
|
|
Contact.delete(contact_id)
|
2019-12-15 16:17:37 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
2019-12-30 17:43:38 +01:00
|
|
|
flash(f"Reverse-alias for {contact_name} has been deleted", "success")
|
2019-12-15 16:17:37 +01:00
|
|
|
|
2020-01-12 22:23:05 +01:00
|
|
|
return redirect(
|
|
|
|
url_for("dashboard.alias_contact_manager", alias_id=alias_id)
|
|
|
|
)
|
2019-12-15 16:17:37 +01:00
|
|
|
|
2020-03-17 10:56:59 +01:00
|
|
|
# make sure highlighted contact is at array start
|
2020-03-17 11:51:40 +01:00
|
|
|
contacts = alias.contacts
|
2019-12-15 16:17:37 +01:00
|
|
|
|
2020-03-17 10:56:59 +01:00
|
|
|
if contact_id:
|
|
|
|
contacts = sorted(contacts, key=lambda fe: fe.id == contact_id, reverse=True)
|
2019-12-15 16:17:37 +01:00
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"dashboard/alias_contact_manager.html",
|
2020-03-17 10:56:59 +01:00
|
|
|
contacts=contacts,
|
2020-03-17 11:51:40 +01:00
|
|
|
alias=alias,
|
2019-12-15 16:17:37 +01:00
|
|
|
new_contact_form=new_contact_form,
|
2020-03-17 10:56:59 +01:00
|
|
|
contact_id=contact_id,
|
2019-12-15 16:17:37 +01:00
|
|
|
)
|