Automatically load Response and ModuleState in each nemubot modules

This commit is contained in:
Némunaire 2012-08-30 17:43:24 +02:00
parent 5e52c4dbbb
commit 99e91af2cb
2 changed files with 43 additions and 13 deletions

View File

@ -16,6 +16,8 @@
# 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/>.
from response import Response
class MessagesHook:
def __init__(self):
# Store specials hook
@ -92,73 +94,97 @@ class MessagesHook:
def treat_cmd(self, msg):
"""Treat a command message"""
treated = False
treated = list()
# First, treat simple hook
if msg.cmd[0] in self.cmd_hook:
for h in self.cmd_hook[msg.cmd[0]]:
treated |= h.run(msg)
res = h.run(msg)
if res is not None and res != False:
treated.append(res)
self.check_rest_times(self.cmd_hook, h)
# Then, treat regexp based hook
for hook in self.cmd_rgxp:
if hook.is_matching(msg.cmd[0], msg.channel):
treated |= hook.run(msg)
res = hook.run(msg)
if res is not None and res != False:
treated.append(res)
self.check_rest_times(self.cmd_rgxp, hook)
# Finally, treat default hooks if not catched before
for hook in self.cmd_default:
if treated:
break
treated |= hook.run(msg)
res = hook.run(msg)
if res is not None and res != False:
treated.append(res)
self.check_rest_times(self.cmd_default, hook)
return treated
def treat_ask(self, msg):
"""Treat an ask message"""
treated = False
treated = list()
# First, treat simple hook
if msg.content in self.ask_hook:
for h in self.ask_hook[msg.content]:
treated |= h.run(msg)
res = h.run(msg)
if res is not None and res != False:
treated.append(res)
self.check_rest_times(self.ask_hook, h)
# Then, treat regexp based hook
for hook in self.ask_rgxp:
if hook.is_matching(msg.content, msg.channel):
treated |= hook.run(msg)
res = hook.run(msg)
if res is not None and res != False:
treated.append(res)
self.check_rest_times(self.ask_rgxp, hook)
# Finally, treat default hooks if not catched before
for hook in self.ask_default:
if treated:
break
treated |= hook.run(msg)
res = hook.run(msg)
if res is not None and res != False:
treated.append(res)
self.check_rest_times(self.ask_default, hook)
return treated
def treat_answer(self, msg):
"""Treat a normal message"""
treated = False
treated = list()
# First, treat simple hook
if msg.content in self.msg_hook:
for h in self.msg_hook[msg.content]:
treated |= h.run(msg)
res = h.run(msg)
if res is not None and res != False:
treated.append(res)
self.check_rest_times(self.msg_hook, h)
# Then, treat regexp based hook
for hook in self.msg_rgxp:
if hook.is_matching(msg.content, msg.channel):
treated |= hook.run(msg)
res = hook.run(msg)
if res is not None and res != False:
treated.append(res)
self.check_rest_times(self.msg_rgxp, hook)
# Finally, treat default hooks if not catched before
for hook in self.msg_default:
if treated:
if len(treated) > 0:
break
treated |= hook.run(msg)
res = hook.run(msg)
if res is not None and res != False:
treated.append(res)
self.check_rest_times(self.msg_default, hook)
return treated
class Hook:
"""Class storing hook informations"""

View File

@ -23,6 +23,7 @@ import os
import sys
from hooks import Hook
import response
import xmlparser
class ModuleFinder(Finder):
@ -156,6 +157,9 @@ class ModuleLoader(SourceLoader):
module.has_access = lambda msg: mod_has_access(module,
module.CONF, msg)
module.ModuleState = xmlparser.module_state.ModuleState
module.Response = response.Response
# Load dependancies
if module.CONF is not None and module.CONF.hasNode("dependson"):
module.MODS = dict()