import time
import logging

import irc.client

from common.config import config

log = logging.getLogger('lrrbot')

TIMEOUT = 90

class JoinFilter:
	def __init__(self, lrrbot, loop):
		self.loop = loop
		self.lrrbot = lrrbot

		self.bot_start = None
		self.last_parts = {}

		self.lrrbot.reactor.add_global_handler('join', self.filter_joins, 20)
		self.lrrbot.reactor.add_global_handler('part', self.on_part, 20)
		self.lrrbot.reactor.execute_every(period=TIMEOUT, function=self.remove_stale_entries)

	def filter_joins(self, conn, event):
		source = irc.client.NickMask(event.source)
		if source.nick == config['username']:
			self.bot_start = time.time()
		else:
			now = time.time()
			last_part = self.last_parts.pop(source.nick, 0)
			if now - self.bot_start <= TIMEOUT:
				log.info("Filtered JOIN from %s because it was %f seconds from bot start", source.nick, now - self.bot_start)
				return "NO MORE"
			if now - last_part <= TIMEOUT:
				log.info("Filtered JOIN from %s because it was %f seconds from last PART", source.nick, now - last_part)
				return "NO MORE"

	def on_part(self, conn, event):
		source = irc.client.NickMask(event.source)
		self.last_parts[source.nick] = time.time()

	def remove_stale_entries(self):
		now = time.time()
		for nick, last_part in list(self.last_parts.items()):
			if now - last_part > TIMEOUT:
				del self.last_parts[nick]

@@ -8,23 +8,8 @@
 time
-%0Aimport logging
 %0A%0Aim
@@ -63,43 +63,8 @@
 ig%0A%0A
-log = logging.getLogger('lrrbot')%0A%0A
 TIME
@@ -727,281 +727,58 @@
 %09%09%09%09
-log.info(%22Filtered JOIN from %25s because it was %25f seconds from bot start%22, source.nick, now - self.bot_start)%0A%09%09%09%09return %22NO MORE%22%0A%09%09%09if now - last_part %3C= TIMEOUT:%0A%09%09%09%09log.info(%22Filtered JOIN from %25s because it was %25f seconds from last PART%22, source.nick, now - last_part)
+return %22NO MORE%22%0A%09%09%09if now - last_part %3C= TIMEOUT:
 %0A%09%09%09

disable logging