2014-09-11 19:20:56 +00:00
|
|
|
# -*- 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
|
2014-10-05 16:19:20 +00:00
|
|
|
import message
|
2014-09-11 19:20:56 +00:00
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
|
2014-09-11 19:20:56 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-10-05 16:19:20 +00:00
|
|
|
def match(self, msg, server=None):
|
|
|
|
if not isinstance(msg, message.AbstractMessage):
|
2014-09-26 16:00:22 +00:00
|
|
|
return True
|
2014-09-11 19:20:56 +00:00
|
|
|
|
2014-10-05 16:19:20 +00:00
|
|
|
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)
|
2014-09-11 19:20:56 +00:00
|
|
|
else:
|
2014-10-05 16:19:20 +00:00
|
|
|
return False
|
2014-09-11 19:20:56 +00:00
|
|
|
|
|
|
|
|
2014-10-05 16:19:20 +00:00
|
|
|
def is_matching(self, strcmp, receivers=list(), server=None):
|
2014-09-11 19:20:56 +00:00
|
|
|
"""Test if the current hook correspond to the message"""
|
2014-11-09 13:11:54 +00:00
|
|
|
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)))):
|
2014-10-05 16:19:20 +00:00
|
|
|
|
|
|
|
if receivers and self.channels:
|
|
|
|
for receiver in receivers:
|
|
|
|
if receiver in self.channels:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
return False
|