Add events and event handlers, and IRC join/part events

This commit is contained in:
moneromooo 2015-03-20 18:07:53 +00:00
parent cea60feeae
commit 1efc378ce2
4 changed files with 60 additions and 2 deletions

View file

@ -385,6 +385,21 @@ def OnIdentified(link, identified):
return
RunNextCommand(link, identified)
def OnEventProxy(event,*args,**kwargs):
log_info('Got event %s, args %s' % (event, str(kwargs)))
if disabled:
log_info('Ignoring event while disabled')
return
link=kwargs['link'] if 'link' in kwargs else None
if link:
link.batch_send_start()
try:
OnEvent(event,*args,**kwargs)
except Exception,e:
log_error('Exception handling event %s: %s' % (str(event),str(e)))
if link:
link.batch_send_done()
def RegisterCommands():
RegisterCommand({'module': 'builtin', 'name': 'help', 'parms': '[module]', 'function': Help, 'help': "Displays help about %s" % config.tipbot_name})
RegisterCommand({'module': 'builtin', 'name': 'commands', 'parms': '[module]', 'function': Commands, 'help': "Displays list of commands"})
@ -453,7 +468,7 @@ for network_setup in start_networks:
name=network_name or network_type
try:
network=registered_networks[network_type](name=name)
network.set_callbacks(OnCommandProxy,OnIdentified)
network.set_callbacks(OnCommandProxy,OnIdentified,OnEventProxy)
if network.connect():
AddNetwork(network)
except Exception,e:

View file

@ -14,6 +14,7 @@ from tipbot.utils import *
modules = dict()
commands = dict()
event_handlers = dict()
calltable=dict()
def SendToProxy(link,msg):
@ -123,6 +124,11 @@ def RegisterCommand(command):
commands[command['name']] = []
commands[command['name']].append(command)
def RegisterEventHandler(eh):
if not eh['event'] in event_handlers:
event_handlers[eh['event']] = []
event_handlers[eh['event']].append(eh)
def OnCommand(link,cmd,check_admin,check_registered):
cmdparts = cmd[0].split(':')
log_log('cmdparts: %s' % str(cmdparts))
@ -178,6 +184,21 @@ def OnCommand(link,cmd,check_admin,check_registered):
if not silent:
link.send("Invalid command, try !help")
def OnEvent(event,*args,**kwargs):
log_log('modulename: event %s' % str(event))
if not event in event_handlers:
return
for eh in event_handlers[event]:
Lock()
try:
log_log('Calling %s handler from module %s' % (str(event),eh['module']))
eh['function'](event,*args,**kwargs)
except:
raise
finally:
Unlock()
def RunIdleFunctions(param=None):
for module in modules:
if 'idle' in modules[module]:
@ -201,6 +222,7 @@ def RunModuleHelpFunction(module,link):
def UnregisterModule(module):
global commands
global event_handlers
global idles
if not module in modules:
@ -220,4 +242,14 @@ def UnregisterModule(module):
new_commands[cmd] = newlist
commands = new_commands
new_event_handlers = dict()
for cmd in event_handlers:
newlist = []
for c in event_handlers[cmd]:
if c['module'] != module:
newlist.append(c)
if len(newlist) > 0:
new_event_handlers[cmd] = newlist
event_handlers = new_event_handlers
del modules[module]

View file

@ -337,6 +337,8 @@ class IRCNetwork(Network):
else:
self.userstable[chan][nick] = None
log_log("New list of users in %s: %s" % (chan, str(self.userstable[chan].keys())))
if self.on_event:
self.on_event('user-joined',link=Link(self,User(self,nick),Group(self,chan)))
elif action == 'PART':
nick = GetNick(who)
@ -346,6 +348,8 @@ class IRCNetwork(Network):
else:
del self.userstable[chan][nick]
log_log("New list of users in %s: %s" % (chan, str(self.userstable[chan].keys())))
if self.on_event:
self.on_event('user-left',link=Link(self,User(self,nick),Group(self,chan)))
elif action == 'QUIT':
nick = GetNick(who)
@ -357,6 +361,8 @@ class IRCNetwork(Network):
removed_list = removed_list + " " + chan
del self.userstable[chan][nick]
log_log("New list of users in %s: %s" % (chan, str(self.userstable[chan].keys())))
if self.on_event:
self.on_event('user-left',link=Link(self,User(self,nick)))
elif action == 'KICK':
nick = parts[3].lower()
@ -368,6 +374,8 @@ class IRCNetwork(Network):
removed_list = removed_list + " " + chan
del self.userstable[chan][nick]
log_log("New list of users in %s: %s" % (chan, str(self.userstable[chan].keys())))
if self.on_event:
self.on_event('user-left',link=Link(self,User(self,nick)))
elif action == 'NICK':
nick = GetNick(who)
@ -382,6 +390,8 @@ class IRCNetwork(Network):
else:
self.userstable[c][new_nick] = None
log_log("New list of users in %s: %s" % (c, str(self.userstable[c].keys())))
if self.on_event:
self.on_event('user-name',link=Link(self,User(self,new_nick),Group(self,chan)),old_name=nick)
except Exception,e:
log_error('Exception in top level action processing: %s' % str(e))

View file

@ -35,9 +35,10 @@ class Network:
def dump_users(self):
pass
def set_callbacks(self,on_command,on_identified):
def set_callbacks(self,on_command,on_identified,on_event):
self.on_command=on_command
self.on_identified=on_identified
self.on_event=on_event
def get_last_active_time(self,user_name,group_name=None):
return None