-class StopForumSpam(Object):
- def init(self, email, address):
- self.email, self.address = email, address
-
- async def send_request(self, **kwargs):
- arguments = {
- "json" : "1",
- }
- arguments.update(kwargs)
-
- # Create request
- request = tornado.httpclient.HTTPRequest(
- "https://api.stopforumspam.org/api", method="POST",
- connect_timeout=2, request_timeout=5)
- request.body = urllib.parse.urlencode(arguments)
-
- # Send the request
- response = await self.backend.http_client.fetch(request)
-
- # Decode the JSON response
- return json.loads(response.body.decode())
-
- async def check_address(self):
- response = await self.send_request(ip=self.address)
-
- try:
- confidence = response["ip"]["confidence"]
- except KeyError:
- confidence = 100
-
- logging.debug("Confidence for %s: %s" % (self.address, confidence))
-
- return confidence
-
- async def check_email(self):
- response = await self.send_request(email=self.email)
-
- try:
- confidence = response["email"]["confidence"]
- except KeyError:
- confidence = 100
-
- logging.debug("Confidence for %s: %s" % (self.email, confidence))
-
- return confidence
-
- async def check(self, threshold=95):
- """
- This function tries to detect if we have a spammer.
-
- To honour the privacy of our users, we only send the IP
- address and username and if those are on the database, we
- will send the email address as well.
- """
- confidences = [await self.check_address(), await self.check_email()]
-
- # Build a score based on the lowest confidence
- return 100 - min(confidences)
-
-