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:
|
|
|
|
def __init__(self):
|
2012-08-22 19:05:33 +00:00
|
|
|
# Store specials hook
|
|
|
|
self.all_pre = list() # Treated before any parse
|
|
|
|
#self.all_post = list() # Treated before send message to user
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
# Store direct hook
|
|
|
|
self.cmd_hook = dict()
|
|
|
|
self.ask_hook = dict()
|
|
|
|
self.msg_hook = dict()
|
|
|
|
|
|
|
|
# Store regexp hook
|
|
|
|
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
|
|
|
|
|
|
|
def add_hook(self, store, hook):
|
|
|
|
"""Insert in the right place a hook into the given store"""
|
|
|
|
if isinstance(store, dict) and hook.name is not None:
|
|
|
|
if hook.name not in store:
|
|
|
|
store[hook.name] = list()
|
|
|
|
store[hook.name].append(hook)
|
|
|
|
elif isinstance(store, list):
|
|
|
|
store.append(hook)
|
|
|
|
else:
|
|
|
|
print ("Warning: unrecognized hook store type")
|
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
def register_hook_attributes(self, store, module, node):
|
|
|
|
if node.hasAttribute("name"):
|
|
|
|
self.add_hook(getattr(self, store + "_hook"), Hook(getattr(module,
|
|
|
|
node["call"]),
|
|
|
|
node["name"]))
|
|
|
|
elif node.hasAttribute("regexp"):
|
|
|
|
self.add_hook(getattr(self, store + "_rgxp"), Hook(getattr(module,
|
|
|
|
node["call"]),
|
|
|
|
None, None,
|
|
|
|
node["regexp"]))
|
|
|
|
|
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
|
|
|
|
|
|
|
def check_rest_times(self, store, hook):
|
|
|
|
"""Remove from store the hook if it has been executed given time"""
|
|
|
|
if hook.times == 0:
|
|
|
|
if isinstance(store, dict):
|
|
|
|
store[hook.name].remove(hook)
|
|
|
|
if len(store) == 0:
|
|
|
|
del store[hook.name]
|
|
|
|
elif isinstance(store, list):
|
|
|
|
store.remove(hook)
|
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
def treat_pre(self, msg):
|
|
|
|
"""Treat a message before all other treatment"""
|
|
|
|
for h in self.all_pre:
|
|
|
|
h.run(msg)
|
|
|
|
self.check_rest_times(self.all_pre, h)
|
|
|
|
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
def treat_cmd(self, msg):
|
|
|
|
"""Treat a command message"""
|
2012-08-30 15:43:24 +00:00
|
|
|
treated = list()
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
# First, treat simple hook
|
|
|
|
if msg.cmd[0] in self.cmd_hook:
|
|
|
|
for h in self.cmd_hook[msg.cmd[0]]:
|
2012-08-30 15:43:24 +00:00
|
|
|
res = h.run(msg)
|
|
|
|
if res is not None and res != False:
|
|
|
|
treated.append(res)
|
2012-08-14 03:51:55 +00:00
|
|
|
self.check_rest_times(self.cmd_hook, h)
|
|
|
|
|
|
|
|
# Then, treat regexp based hook
|
|
|
|
for hook in self.cmd_rgxp:
|
2012-08-22 19:05:33 +00:00
|
|
|
if hook.is_matching(msg.cmd[0], msg.channel):
|
2012-08-30 15:43:24 +00:00
|
|
|
res = hook.run(msg)
|
|
|
|
if res is not None and res != False:
|
|
|
|
treated.append(res)
|
2012-08-14 03:51:55 +00:00
|
|
|
self.check_rest_times(self.cmd_rgxp, hook)
|
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
# Finally, treat default hooks if not catched before
|
|
|
|
for hook in self.cmd_default:
|
|
|
|
if treated:
|
|
|
|
break
|
2012-08-30 15:43:24 +00:00
|
|
|
res = hook.run(msg)
|
|
|
|
if res is not None and res != False:
|
|
|
|
treated.append(res)
|
2012-08-22 19:05:33 +00:00
|
|
|
self.check_rest_times(self.cmd_default, hook)
|
|
|
|
|
2012-08-30 15:43:24 +00:00
|
|
|
return treated
|
2012-08-22 19:05:33 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
def treat_ask(self, msg):
|
|
|
|
"""Treat an ask message"""
|
2012-08-30 15:43:24 +00:00
|
|
|
treated = list()
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
# First, treat simple hook
|
|
|
|
if msg.content in self.ask_hook:
|
|
|
|
for h in self.ask_hook[msg.content]:
|
2012-08-30 15:43:24 +00:00
|
|
|
res = h.run(msg)
|
|
|
|
if res is not None and res != False:
|
|
|
|
treated.append(res)
|
2012-08-14 03:51:55 +00:00
|
|
|
self.check_rest_times(self.ask_hook, h)
|
|
|
|
|
|
|
|
# Then, treat regexp based hook
|
|
|
|
for hook in self.ask_rgxp:
|
2012-08-22 19:05:33 +00:00
|
|
|
if hook.is_matching(msg.content, msg.channel):
|
2012-08-30 15:43:24 +00:00
|
|
|
res = hook.run(msg)
|
|
|
|
if res is not None and res != False:
|
|
|
|
treated.append(res)
|
2012-08-14 03:51:55 +00:00
|
|
|
self.check_rest_times(self.ask_rgxp, hook)
|
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
# Finally, treat default hooks if not catched before
|
|
|
|
for hook in self.ask_default:
|
|
|
|
if treated:
|
|
|
|
break
|
2012-08-30 15:43:24 +00:00
|
|
|
res = hook.run(msg)
|
|
|
|
if res is not None and res != False:
|
|
|
|
treated.append(res)
|
2012-08-22 19:05:33 +00:00
|
|
|
self.check_rest_times(self.ask_default, hook)
|
|
|
|
|
2012-08-30 15:43:24 +00:00
|
|
|
return treated
|
2012-08-22 19:05:33 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
def treat_answer(self, msg):
|
|
|
|
"""Treat a normal message"""
|
2012-08-30 15:43:24 +00:00
|
|
|
treated = list()
|
2012-08-22 19:52:49 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
# First, treat simple hook
|
|
|
|
if msg.content in self.msg_hook:
|
2012-08-22 19:05:33 +00:00
|
|
|
for h in self.msg_hook[msg.content]:
|
2012-08-30 15:43:24 +00:00
|
|
|
res = h.run(msg)
|
|
|
|
if res is not None and res != False:
|
|
|
|
treated.append(res)
|
2012-08-14 03:51:55 +00:00
|
|
|
self.check_rest_times(self.msg_hook, h)
|
|
|
|
|
|
|
|
# Then, treat regexp based hook
|
|
|
|
for hook in self.msg_rgxp:
|
2012-08-22 19:05:33 +00:00
|
|
|
if hook.is_matching(msg.content, msg.channel):
|
2012-08-30 15:43:24 +00:00
|
|
|
res = hook.run(msg)
|
|
|
|
if res is not None and res != False:
|
|
|
|
treated.append(res)
|
2012-08-14 03:51:55 +00:00
|
|
|
self.check_rest_times(self.msg_rgxp, hook)
|
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
# Finally, treat default hooks if not catched before
|
|
|
|
for hook in self.msg_default:
|
2012-08-30 15:43:24 +00:00
|
|
|
if len(treated) > 0:
|
2012-08-22 19:05:33 +00:00
|
|
|
break
|
2012-08-30 15:43:24 +00:00
|
|
|
res = hook.run(msg)
|
|
|
|
if res is not None and res != False:
|
|
|
|
treated.append(res)
|
2012-08-22 19:05:33 +00:00
|
|
|
self.check_rest_times(self.msg_default, hook)
|
|
|
|
|
2012-08-30 15:43:24 +00:00
|
|
|
return treated
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
|
|
|
|
class Hook:
|
|
|
|
"""Class storing hook informations"""
|
2012-08-22 19:05:33 +00:00
|
|
|
def __init__(self, call, name=None, data=None, regexp=None, channels=list()):
|
2012-08-14 03:51:55 +00:00
|
|
|
self.name = name
|
|
|
|
self.call = call
|
|
|
|
self.regexp = regexp
|
|
|
|
self.data = data
|
|
|
|
self.times = -1
|
2012-08-22 19:05:33 +00:00
|
|
|
self.channels = channels
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
def is_matching(self, strcmp, channel):
|
2012-08-14 03:51:55 +00:00
|
|
|
"""Test if the current hook correspond to the message"""
|
2012-08-22 19:05:33 +00:00
|
|
|
return (len(self.channel) <= 0 or channel in self.channels) and (
|
|
|
|
(self.name is not None and strcmp == self.name) or (
|
|
|
|
self.regexp is not None and re.match(self.regexp, strcmp)))
|
2012-08-14 03:51:55 +00:00
|
|
|
|
|
|
|
def run(self, msg):
|
|
|
|
"""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
|
|
|
|
|
|
|
if self.data is None:
|
|
|
|
return self.call(msg)
|
|
|
|
elif isinstance(self.data, dict):
|
|
|
|
return self.call(msg, **self.data)
|
|
|
|
else:
|
|
|
|
return self.call(msg, self.data)
|