diff options
Diffstat (limited to 'py-bin/mail_auth.py')
-rw-r--r-- | py-bin/mail_auth.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/py-bin/mail_auth.py b/py-bin/mail_auth.py new file mode 100644 index 0000000..27607e8 --- /dev/null +++ b/py-bin/mail_auth.py @@ -0,0 +1,111 @@ +#mail authentication + +import smtplib, hmac, sha, re, time, logging +from utils import empy_render +import subprocess +import config + +class MailAuthMixIn: + def mail_form(self, req): + self.render_template(req, "mail_form.em") + mail_form.web_callable = True + + def mail_process(self, req): + email = req.params.get("email", "") + + success, status_or_token = self.jman.generate_token(email) + if not success: + self.render_template(req, "mail_error.em", dict(reason=status_or_token)) + return + token = status_or_token + + if not self.__send_mail(email, token): + msg = "Unerwarteter Fehler beim Versenden des Mails." + self.render_template(req, "mail_trylater.em", dict(reason=msg)) + return + + success, status = self.jman.prepare_user(email, token) + if not success: + self.render_template(req, "mail_error.em", dict(reason=status)) + return + + msg = "Mail erfolgreich versandt." + self.render_template(req, "mail_success.em", dict(status=msg, email=email)) + mail_process.web_callable = True + + def mail_pw_form(self, req): + user_id = req.params.get("uid", "") + token = req.params.get("tok", "") + last_error = req.params.get("error", "") + + token_ok, status_or_user = self.jman.validate_token(user_id, token) + if token_ok: + user_id = status_or_user.get_user_id() + self.render_template(req, "set_pw_form.em", + dict(user_id=user_id, error=last_error, command="mail_pw_process")) + else: + self.error_page(req, status_or_user) + mail_pw_form.web_callable = True + + def mail_pw_process(self, req): + password = req.params.get("password", "") + password2 = req.params.get("password2", "") + + ok, status = self.jman.is_acceptable_password(password, password2) + if not ok: + url = self.make_url([("cmd","mail_pw_form"), ("error", status)]) + self.redirect_to(req, url) + return + + ok, status_or_user = self.jman.activate_user(password) + if ok: + self.redirect_to(req, config.script_url + "?cmd=setup_main") + else: + self.error_page(req, status_or_user) + mail_pw_process.web_callable = True + + def __send_mail(self, email, token): + date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) + url = self.make_url([("cmd","mail_pw_form"), ("uid",email), ("tok",token)]) + ctx = dict(from_addr=config.mail_from_addr, to_addr=email, + date=date, reg_link=url) + message = empy_render("mail_message.em", context_raw = ctx) + + if hasattr(config,"use_sendmail") and config.use_sendmail: + return self.__sendmail_send_mail(email, token, message) + else: + return self.__smtp_send_mail(email, token, message) + + def __smtp_send_mail(self, email, token, message): + try: + server = smtplib.SMTP(config.smtp_server) + server.ehlo() + server.starttls() + server.ehlo() + if hasattr(config, "smtp_pass"): + server.login(config.smtp_user, config.smtp_pass) + server.sendmail(config.mail_from_addr, [email], message) + server.quit() + logging.info("Sent mail to %s." % email) + except Exception, e: + logging.error("Error sending mail to %s: %s" % (email, str(e))) + return False + + return True + + def __sendmail_send_mail(self, email, token, message): + try: + params = ["/usr/sbin/sendmail", "-f" + config.mail_from_addr, email] + p = subprocess.Popen(params, stdin=subprocess.PIPE) + p.communicate(message) + result = p.wait() + if result != 0: + logging.error("Error queuing mail for %s: return code %s" % + (email, str(result))) + return False + logging.info("Queued mail for %s." % email) + except Exception, e: + logging.error("Error queuing mail for %s: %s" % (email, str(e))) + return False + return True + |