Commits

Jason R. Coombs committed b694ee4

Add leave logging support

Comments (0)

Files changed (3)

+1105.6
+======
+
+* Added support for logging leave events as well as join events.
+* Added a new ``@on_leave`` decorator, suitable for implementing custom
+  handlers for leave events.
+
 1105.5
 ======
 
 		for line in msg.splitlines():
 			connection.notice(nick, line)
 
+	def on_leave(self, connection, event):
+		nick = event.source.nick
+		channel = event.target
+		for func in _leave_registry:
+			try:
+				func(client=connection, event=event, nick=nick,
+					channel=channel)
+			except Exception:
+				print(datetime.datetime.now(),
+					"Error in on_leave handler %s" % func,
+					file=sys.stderr)
+				traceback.print_exc()
+
 	def on_pubmsg(self, connection, event):
 		msg = u''.join(event.arguments)
 		if not msg.strip():
 _delay_registry = []
 _at_registry = []
 _join_registry = []
+_leave_registry = []
 
 class Handler(object):
 	_registry = []
 		return func
 	return deco
 
+def on_leave(doc=None):
+	def deco(func):
+		_leave_registry.append(func)
+		return func
+	return deco
+
 def get_args():
 	parser = argparse.ArgumentParser()
 	parser.add_argument('config', type=pmxbot.dictlib.ConfigDict.from_yaml,
+"""
+Support for rolls, leave and join events (i.e. Roll Call).
+"""
 
 from __future__ import absolute_import
 
 import pmxbot
 from . import storage
 from . import logging
-from pmxbot.core import on_join
+from pmxbot.core import on_join, on_leave
 
 class ParticipantLogger(storage.SelectableStorage):
 	"Base class for logging participants"
 		return
 	ParticipantLogger.store.log_join(nick, channel)
 
+@on_leave()
+def log_leave(nick, channel, **kwargs):
+	if channel not in pmxbot.config.log_channels:
+		return
+	ParticipantLogger.store.log_leave(nick, channel)
+
 class SQLiteLogger(ParticipantLogger, storage.SQLiteStorage):
 
 	def init_tables(self):
 		self.db.ensure_index([
 			('datetime.d', storage.pymongo.DESCENDING),
 			('channel', storage.pymongo.ASCENDING),
-			])
+		])
 		now = datetime.datetime.utcnow()
 		self.db.insert(dict(channel=channel, nick=nick, change=change,
 			datetime=logging.MongoDBLogger._fmt_date(now)))