]> git.ipfire.org Git - ipfire.org.git/blob - src/web/newsletter.py
Deploy rate-limiting
[ipfire.org.git] / src / web / newsletter.py
1 #!/usr/bin/python3
2
3 import email.mime.text
4 import logging
5 import tornado.gen
6 import tornado.process
7
8 from . import base
9
10 class SubscribeHandler(base.BaseHandler):
11 @base.blacklisted
12 def prepare(self):
13 # Makes sure that we call blacklist for everything
14 pass
15
16 @tornado.gen.coroutine
17 @base.ratelimit(minutes=15, requests=5)
18 def post(self):
19 address = self.get_argument("email")
20
21 # Create an email with us as sender
22 m = email.mime.text.MIMEText("")
23 m.add_header("To", "ipfire-announce-join@lists.ipfire.org")
24 m.add_header("From", address)
25 m.add_header("Sender", "no-reply@ipfire.org")
26 m.add_header("Subject", "Subscribe")
27
28 logging.debug("Sending email:\n%s" % m.as_string())
29
30 # Call sendmail
31 p = tornado.process.Subprocess(["sendmail", "-t"],
32 stdin=tornado.process.Subprocess.STREAM)
33
34 # Pipe the email into sendmail
35 yield tornado.gen.Task(p.stdin.write, m.as_bytes())
36
37 # Close standard input
38 p.stdin.close()
39
40 # Wait until sendmail is done
41 ret = yield p.wait_for_exit()
42
43 logging.info("%s has subscribed to the newsletter" % address)
44
45 self.render("newsletter/subscribed.html", address=address)