import aiomas
import asyncio
import re
import logging

import common.url
from common import utils
from lrrbot import storage
import irc.client

log = logging.getLogger('linkspam')

class LinkSpam:
	router = aiomas.rpc.Service()

	def __init__(self, lrrbot, loop):
		self.loop = loop
		self.lrrbot = lrrbot
		#self.re_url = loop.run_until_complete(common.url.url_regex())
		self.rules = [
			{
				"re": re.compile(rule["re"], re.IGNORECASE),
				"message": rule["message"],
				"type": rule.get('type', 'spam'),
			}
			for rule in storage.data.get("link_spam_rules", [])
		]
		self.lrrbot.rpc_server.link_spam = self
		#self.lrrbot.reactor.add_global_handler("pubmsg", self.check_link_spam, 21)

	@aiomas.expose
	def modify_link_spam_rules(self, data):
		storage.data['link_spam_rules'] = data
		storage.save()
		self.rules = [
			{
				"re": re.compile(rule['re'], re.IGNORECASE),
				"message": rule['message'],
				"type": rule.get('type', 'spam'),
			}
			for rule in storage.data['link_spam_rules']
		]

	def check_link_spam(self, conn, event):
		asyncio.async(self.check_urls(conn, event, event.arguments[0])).add_done_callback(utils.check_exception)

	@asyncio.coroutine
	def check_urls(self, conn, event, message):
		urls = []
		for match in self.re_url.finditer(message):
			for url in match.groups():
				if url is not None:
					urls.append(url)
					break
		canonical_urls = yield from asyncio.gather(*map(common.url.canonical_url, urls), loop=self.loop)
		for original_url, url_chain in zip(urls, canonical_urls):
			for url in url_chain:
				for rule in self.rules:
					match = rule["re"].search(url)
					if match is not None:
						source = irc.client.NickMask(event.source)
						log.info("Detected link spam from %s - %r contains the URL %r which redirects to %r which matches %r",
							source.nick, message, original_url, url, rule["re"].pattern)
						yield from self.lrrbot.ban(conn, event, rule["message"] % {str(i+1): v for i, v in enumerate(match.groups())}, rule['type'])
						return

@@ -301,17 +301,16 @@
 rrbot%0A%09%09
-#
 self.re_
@@ -616,9 +616,8 @@
 f%0A%09%09
-#
 self

Revert "Disable link spam filter"