[wip] move files in order to have a clean directory structure

This commit is contained in:
nemunaire 2015-01-03 18:49:01 +01:00
commit 41f7dc2456
31 changed files with 0 additions and 0 deletions

88
nemubot/hooks/__init__.py Normal file
View file

@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2014 nemunaire
#
# 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/>.
import imp
from exception import IRCException
def call_game(call, *args, **kargs):
"""TODO"""
l = list()
d = kargs
for a in args:
if a is not None:
if isinstance(a, dict):
d.update(a)
else:
l.append(a)
return call(*l, **d)
class AbstractHook:
"""Abstract class for Hook implementation"""
def __init__(self, call, data=None, mtimes=-1, end_call=None):
self.call = call
self.data = data
self.times = mtimes
self.end_call = end_call
def match(self, data1, server):
return NotImplemented
def run(self, data1, *args):
"""Run the hook"""
self.times -= 1
try:
ret = call_game(self.call, data1, self.data, *args)
except IRCException as e:
ret = e.fill_response(data1)
finally:
if self.times == 0:
self.call_end(ret)
return ret
from hooks.messagehook import MessageHook
last_registered = []
def hook(store, *args, **kargs):
"""Function used as a decorator for module loading"""
def sec(call):
last_registered.append((store, MessageHook(call, *args, **kargs)))
return call
return sec
def reload():
import hooks.manager
imp.reload(hooks.manager)
import hooks.messagehook
imp.reload(hooks.messagehook)

108
nemubot/hooks/manager.py Normal file
View file

@ -0,0 +1,108 @@
# -*- coding: utf-8 -*-
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2014 nemunaire
#
# 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/>.
class HooksManager:
"""Class to manage hooks"""
def __init__(self):
"""Initialize the manager"""
self.hooks = dict()
def add_hook(self, hook, *triggers):
"""Add a hook to the manager
Argument:
hook -- a Hook instance
triggers -- string that trigger the hook
"""
trigger = "_".join(triggers)
if trigger not in self.hooks:
self.hooks[trigger] = list()
#print("ADD hook: %s => %s" % (trigger, hook))
self.hooks[trigger].append(hook)
def del_hook(self, hook=None, *triggers):
"""Remove the given hook from the manager
Return:
Boolean value reporting the deletion success
Argument:
triggers -- trigger string to remove
Keyword argument:
hook -- a Hook instance to remove from the trigger string
"""
trigger = "_".join(triggers)
if trigger in self.hooks:
if hook is None:
del self.hooks[trigger]
else:
self.hooks[trigger].remove(hook)
return True
return False
def get_hooks(self, *triggers):
"""Returns list of trigger hooks that match the given trigger string
Argument:
triggers -- the trigger string
Keyword argument:
data -- Data to pass to the hook as argument
"""
trigger = "_".join(triggers)
res = list()
for key in self.hooks:
if trigger.find(key) == 0:
res += self.hooks[key]
#print("GET hooks: %s => %d" % (trigger, len(res)))
return res
def exec_hook(self, *triggers, **data):
"""Trigger hooks that match the given trigger string
Argument:
trigger -- the trigger string
Keyword argument:
data -- Data to pass to the hook as argument
"""
trigger = "_".join(triggers)
for key in self.hooks:
if trigger.find(key) == 0:
for hook in self.hooks[key]:
hook.run(**data)

View file

@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2014 nemunaire
#
# 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/>.
import re
from exception import IRCException
import hooks
import message
class MessageHook(hooks.AbstractHook):
"""Class storing hook information, specialized for a generic Message"""
def __init__(self, call, name=None, data=None, regexp=None,
channels=list(), server=None, mtimes=-1, end_call=None):
hooks.AbstractHook.__init__(self, call=call, data=data,
end_call=end_call, mtimes=mtimes)
self.name = name
self.regexp = regexp
self.server = server
self.channels = channels
def match(self, msg, server=None):
if not isinstance(msg, message.AbstractMessage):
return True
elif isinstance(msg, message.Command):
return self.is_matching(msg.cmd, msg.to, server)
elif isinstance(msg, message.TextMessage):
return self.is_matching(msg.message, msg.to, server)
else:
return False
def is_matching(self, strcmp, receivers=list(), server=None):
"""Test if the current hook correspond to the message"""
if ((server is None or self.server is None or self.server == server)
and ((self.name is None or strcmp == self.name) and (
self.regexp is None or re.match(self.regexp, strcmp)))):
if receivers and self.channels:
for receiver in receivers:
if receiver in self.channels:
return True
else:
return True
return False