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/>.
|
|
|
|
|
2014-08-14 10:49:38 +00:00
|
|
|
import logging
|
2013-01-01 15:13:58 +00:00
|
|
|
import re
|
|
|
|
|
2012-08-30 15:43:24 +00:00
|
|
|
from response import Response
|
2014-04-30 20:19:32 +00:00
|
|
|
from exception import IRCException
|
2012-08-30 15:43:24 +00:00
|
|
|
|
2014-08-27 05:57:00 +00:00
|
|
|
logger = logging.getLogger("nemubot.hooks")
|
2014-08-14 10:49:38 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
class Hook:
|
|
|
|
"""Class storing hook informations"""
|
2014-08-12 18:08:55 +00:00
|
|
|
def __init__(self, call, name=None, data=None, regexp=None, channels=list(), server=None, end=None, call_end=None, help=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
|
2014-08-12 18:08:55 +00:00
|
|
|
self.help = help
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2014-09-01 17:21:54 +00:00
|
|
|
def match(self, message, channel=None, server=None):
|
|
|
|
if isinstance(message, Response):
|
|
|
|
return self.is_matching(None, channel, server)
|
|
|
|
elif message.qual == "cmd":
|
|
|
|
return self.is_matching(message.cmds[0], channel, server)
|
|
|
|
elif hasattr(message, "text"):
|
|
|
|
return self.is_matching(message.text, channel, server)
|
|
|
|
elif len(message.params) > 0:
|
|
|
|
return self.is_matching(message.params[0], channel, server)
|
|
|
|
else:
|
|
|
|
return self.is_matching(message.cmd, channel, server)
|
|
|
|
|
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 (
|
2013-01-01 15:13:58 +00:00
|
|
|
(self.name is None or strcmp == self.name) and (
|
|
|
|
self.end is None or strcmp == self.end) and (
|
2012-11-04 14:32:39 +00:00
|
|
|
self.regexp is None or 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
|
|
|
|
|
2014-04-30 20:19:32 +00:00
|
|
|
try:
|
|
|
|
if self.data is None:
|
|
|
|
if data2 is None:
|
|
|
|
return call(msg)
|
|
|
|
elif isinstance(data2, dict):
|
|
|
|
return call(msg, **data2)
|
|
|
|
else:
|
|
|
|
return call(msg, data2)
|
|
|
|
elif isinstance(self.data, dict):
|
|
|
|
if data2 is None:
|
|
|
|
return call(msg, **self.data)
|
|
|
|
else:
|
|
|
|
return call(msg, data2, **self.data)
|
2012-11-04 03:28:24 +00:00
|
|
|
else:
|
2014-04-30 20:19:32 +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)
|
|
|
|
except IRCException as e:
|
|
|
|
return e.fill_response(msg)
|
2014-08-12 18:08:55 +00:00
|
|
|
|
|
|
|
last_registered = []
|
|
|
|
|
|
|
|
def hook(store, *args, **kargs):
|
|
|
|
def sec(call):
|
|
|
|
last_registered.append((store, Hook(call, *args, **kargs)))
|
|
|
|
return call
|
|
|
|
return sec
|