2012-08-14 03:51:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Nemubot is a modulable IRC bot, built around XML configuration files.
|
|
|
|
# Copyright (C) 2012 Mercier Pierre-Olivier
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-08-30 15:43:24 +00:00
|
|
|
from response import Response
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
class MessagesHook:
|
2012-08-31 03:21:19 +00:00
|
|
|
def __init__(self, context):
|
|
|
|
self.context = context
|
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
# Store specials hooks
|
2012-08-22 19:05:33 +00:00
|
|
|
self.all_pre = list() # Treated before any parse
|
2012-11-02 11:10:37 +00:00
|
|
|
self.all_post = list() # Treated before send message to user
|
2012-08-22 19:05:33 +00:00
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
# Store IRC commands hooks
|
|
|
|
self.irc_hook = dict()
|
|
|
|
|
|
|
|
# Store direct hooks
|
2012-08-14 03:51:55 +00:00
|
|
|
self.cmd_hook = dict()
|
|
|
|
self.ask_hook = dict()
|
|
|
|
self.msg_hook = dict()
|
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
# Store regexp hooks
|
2012-08-14 03:51:55 +00:00
|
|
|
self.cmd_rgxp = list()
|
|
|
|
self.ask_rgxp = list()
|
|
|
|
self.msg_rgxp = list()
|
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
# Store default hooks (after other hooks if no match)
|
|
|
|
self.cmd_default = list()
|
|
|
|
self.ask_default = list()
|
|
|
|
self.msg_default = list()
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-09-01 09:23:41 +00:00
|
|
|
def add_hook(self, store, hook, module_src=None):
|
2012-08-14 03:51:55 +00:00
|
|
|
"""Insert in the right place a hook into the given store"""
|
2012-10-04 11:50:48 +00:00
|
|
|
if module_src is None:
|
|
|
|
print ("\033[1;35mWarning:\033[0m No source module was passed to "
|
|
|
|
"add_hook function, please fix it in order to be "
|
|
|
|
"compatible with unload feature")
|
|
|
|
|
2012-08-31 03:21:19 +00:00
|
|
|
if store in self.context.hooks_cache:
|
|
|
|
del self.context.hooks_cache[store]
|
|
|
|
|
2012-09-01 09:23:41 +00:00
|
|
|
if not hasattr(self, store):
|
2012-10-14 23:15:07 +00:00
|
|
|
print ("\033[1;35mWarning:\033[0m unrecognized hook store")
|
2012-08-31 03:21:19 +00:00
|
|
|
return
|
2012-09-01 09:23:41 +00:00
|
|
|
attr = getattr(self, store)
|
2012-08-31 03:21:19 +00:00
|
|
|
|
|
|
|
if isinstance(attr, dict) and hook.name is not None:
|
|
|
|
if hook.name not in attr:
|
|
|
|
attr[hook.name] = list()
|
|
|
|
attr[hook.name].append(hook)
|
2012-11-04 03:28:24 +00:00
|
|
|
if hook.end is not None:
|
|
|
|
if hook.end not in attr:
|
|
|
|
attr[hook.end] = list()
|
|
|
|
attr[hook.end].append(hook)
|
2012-08-31 03:21:19 +00:00
|
|
|
elif isinstance(attr, list):
|
|
|
|
attr.append(hook)
|
2012-08-14 03:51:55 +00:00
|
|
|
else:
|
2012-10-04 11:50:48 +00:00
|
|
|
print ("\033[1;32mWarning:\033[0m unrecognized hook store type")
|
2012-09-01 09:23:41 +00:00
|
|
|
return
|
2012-11-04 03:28:24 +00:00
|
|
|
if module_src is not None and hasattr(module_src, "REGISTERED_HOOKS"):
|
2012-09-01 09:23:41 +00:00
|
|
|
module_src.REGISTERED_HOOKS.append((store, hook))
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
def register_hook_attributes(self, store, module, node):
|
|
|
|
if node.hasAttribute("name"):
|
2012-08-31 03:21:19 +00:00
|
|
|
self.add_hook(store + "_hook", Hook(getattr(module, node["call"]),
|
2012-09-01 09:23:41 +00:00
|
|
|
node["name"]),
|
|
|
|
module)
|
2012-08-22 19:05:33 +00:00
|
|
|
elif node.hasAttribute("regexp"):
|
2012-08-31 03:21:19 +00:00
|
|
|
self.add_hook(store + "_rgxp", Hook(getattr(module, node["call"]),
|
2012-09-01 09:23:41 +00:00
|
|
|
None, None, node["regexp"]),
|
|
|
|
module)
|
2012-08-22 19:05:33 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
def register_hook(self, module, node):
|
|
|
|
"""Create a hook from configuration node"""
|
|
|
|
if node.name == "message" and node.hasAttribute("type"):
|
|
|
|
if node["type"] == "cmd" or node["type"] == "all":
|
2012-08-22 19:05:33 +00:00
|
|
|
self.register_hook_attributes("cmd", module, node)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
|
|
|
if node["type"] == "ask" or node["type"] == "all":
|
2012-08-22 19:05:33 +00:00
|
|
|
self.register_hook_attributes("ask", module, node)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
if (node["type"] == "msg" or node["type"] == "answer" or
|
|
|
|
node["type"] == "all"):
|
|
|
|
self.register_hook_attributes("answer", module, node)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
def del_hook(self, store, hook, module_src=None):
|
2012-09-01 09:23:41 +00:00
|
|
|
"""Remove a registered hook from a given store"""
|
|
|
|
if store in self.context.hooks_cache:
|
|
|
|
del self.context.hooks_cache[store]
|
|
|
|
|
|
|
|
if not hasattr(self, store):
|
|
|
|
print ("Warning: unrecognized hook store type")
|
|
|
|
return
|
|
|
|
attr = getattr(self, store)
|
|
|
|
|
|
|
|
if isinstance(attr, dict) and hook.name is not None:
|
|
|
|
if hook.name in attr:
|
|
|
|
attr[hook.name].remove(hook)
|
2012-11-04 03:28:24 +00:00
|
|
|
if hook.end is not None and hook.end in attr:
|
|
|
|
attr[hook.end].remove(hook)
|
2012-09-01 09:23:41 +00:00
|
|
|
else:
|
|
|
|
attr.remove(hook)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
if module_src is not None:
|
|
|
|
module_src.REGISTERED_HOOKS.remove((store, hook))
|
|
|
|
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
class Hook:
|
|
|
|
"""Class storing hook informations"""
|
2012-11-04 03:28:24 +00:00
|
|
|
def __init__(self, call, name=None, data=None, regexp=None, channels=list(), server=None, end=None, call_end=None):
|
2012-08-14 03:51:55 +00:00
|
|
|
self.name = name
|
2012-11-04 03:28:24 +00:00
|
|
|
self.end = end
|
2012-08-14 03:51:55 +00:00
|
|
|
self.call = call
|
2012-11-04 03:28:24 +00:00
|
|
|
if call_end is None:
|
|
|
|
self.call_end = self.call
|
|
|
|
else:
|
|
|
|
self.call_end = call_end
|
2012-08-14 03:51:55 +00:00
|
|
|
self.regexp = regexp
|
|
|
|
self.data = data
|
|
|
|
self.times = -1
|
2012-11-04 03:28:24 +00:00
|
|
|
self.server = server
|
2012-08-22 19:05:33 +00:00
|
|
|
self.channels = channels
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
def is_matching(self, strcmp, channel=None, server=None):
|
2012-08-14 03:51:55 +00:00
|
|
|
"""Test if the current hook correspond to the message"""
|
2012-11-04 03:28:24 +00:00
|
|
|
return (channel is None or len(self.channels) <= 0 or
|
|
|
|
channel in self.channels) and (server is None or
|
|
|
|
self.server is None or self.server == server) and (
|
2012-08-22 19:05:33 +00:00
|
|
|
(self.name is not None and strcmp == self.name) or (
|
2012-11-04 03:28:24 +00:00
|
|
|
self.end is not None and strcmp == self.end) or (
|
2012-08-22 19:05:33 +00:00
|
|
|
self.regexp is not None and re.match(self.regexp, strcmp)))
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
def run(self, msg, data2=None, strcmp=None):
|
2012-08-14 03:51:55 +00:00
|
|
|
"""Run the hook"""
|
2012-08-22 19:05:33 +00:00
|
|
|
if self.times != 0:
|
2012-08-14 03:51:55 +00:00
|
|
|
self.times -= 1
|
2012-08-22 19:05:33 +00:00
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
if (self.end is not None and strcmp is not None and
|
|
|
|
self.call_end is not None and strcmp == self.end):
|
|
|
|
call = self.call_end
|
|
|
|
self.times = 0
|
|
|
|
else:
|
|
|
|
call = self.call
|
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
if self.data is None:
|
2012-11-04 03:28:24 +00:00
|
|
|
if data2 is None:
|
|
|
|
return call(msg)
|
|
|
|
elif isinstance(data2, dict):
|
|
|
|
return call(msg, **data2)
|
|
|
|
else:
|
|
|
|
return call(msg, data2)
|
2012-08-22 19:05:33 +00:00
|
|
|
elif isinstance(self.data, dict):
|
2012-11-04 03:28:24 +00:00
|
|
|
if data2 is None:
|
|
|
|
return call(msg, **self.data)
|
|
|
|
else:
|
|
|
|
return call(msg, data2, **self.data)
|
2012-08-22 19:05:33 +00:00
|
|
|
else:
|
2012-11-04 03:28:24 +00:00
|
|
|
if data2 is None:
|
|
|
|
return call(msg, self.data)
|
|
|
|
elif isinstance(data2, dict):
|
|
|
|
return call(msg, self.data, **data2)
|
|
|
|
else:
|
|
|
|
return call(msg, self.data, data2)
|