Split messages class into multiple files

This commit is contained in:
nemunaire 2015-04-06 09:50:13 +02:00 committed by nemunaire
parent 57bbca4e7a
commit c8d495d508
15 changed files with 280 additions and 198 deletions

View File

@ -10,7 +10,7 @@ import shlex
from nemubot import context from nemubot import context
from nemubot.exception import IRCException from nemubot.exception import IRCException
from nemubot.hooks import hook from nemubot.hooks import hook
from nemubot.message import TextMessage, Command from nemubot.message import Command
from nemubot.tools.xmlparser.node import ModuleState from nemubot.tools.xmlparser.node import ModuleState
nemubotversion = 3.4 nemubotversion = 3.4

View File

@ -142,8 +142,8 @@ def send(toks, **kwargs):
% (toks[1], srv.id)) % (toks[1], srv.id))
return 3 return 3
from nemubot.message import TextMessage from nemubot.message import Text
srv.send_response(TextMessage(" ".join(toks[2:]), server=None, srv.send_response(Text(" ".join(toks[2:]), server=None,
to=[toks[1]])) to=[toks[1]]))
return 0 return 0

View File

@ -20,7 +20,7 @@
import logging import logging
from nemubot.message import TextMessage, DirectAsk from nemubot.message import Text, DirectAsk
from nemubot.hooks import hook from nemubot.hooks import hook
nemubotversion = 3.4 nemubotversion = 3.4
@ -120,7 +120,7 @@ class Response:
self.get_message(maxlen - len(self.nick) - 2), self.get_message(maxlen - len(self.nick) - 2),
server=None, to=self.receivers) server=None, to=self.receivers)
else: else:
return TextMessage(self.get_message(maxlen), return Text(self.get_message(maxlen),
server=None, to=self.receivers) server=None, to=self.receivers)
def get_message(self, maxlen): def get_message(self, maxlen):

View File

@ -7,7 +7,7 @@ import subprocess
from threading import Thread from threading import Thread
from nemubot.hooks import hook from nemubot.hooks import hook
from nemubot.message import TextMessage from nemubot.message import Text
from nemubot.message.visitor import AbstractVisitor from nemubot.message.visitor import AbstractVisitor
nemubotversion = 3.4 nemubotversion = 3.4
@ -51,7 +51,7 @@ class SpeakerVisitor(AbstractVisitor):
self.last = last self.last = last
def visit_TextMessage(self, msg): def visit_Text(self, msg):
force = (self.last is None) force = (self.last is None)
if force or msg.date - self.last.date > timedelta(0, 500): if force or msg.date - self.last.date > timedelta(0, 500):
@ -86,14 +86,14 @@ class SpeakerVisitor(AbstractVisitor):
def visit_DirectAsk(self, msg): def visit_DirectAsk(self, msg):
res = TextMessage("%s: %s" % (msg.designated, msg.message), res = Text("%s: %s" % (msg.designated, msg.message),
server=msg.server, date=msg.date, server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm) to=msg.to, frm=msg.frm)
res.accept(self) res.accept(self)
def visit_Command(self, msg): def visit_Command(self, msg):
res = TextMessage("Bang %s%s%s" % (msg.cmd, res = Text("Bang %s%s%s" % (msg.cmd,
" " if len(msg.args) else "", " " if len(msg.args) else "",
" ".join(msg.args)), " ".join(msg.args)),
server=msg.server, date=msg.date, server=msg.server, date=msg.date,
@ -102,7 +102,7 @@ class SpeakerVisitor(AbstractVisitor):
def visit_OwnerCommand(self, msg): def visit_OwnerCommand(self, msg):
res = TextMessage("Owner Bang %s%s%s" % (msg.cmd, res = Text("Owner Bang %s%s%s" % (msg.cmd,
" " if len(msg.args) else "", " " if len(msg.args) else "",
" ".join(msg.args)), " ".join(msg.args)),
server=msg.server, date=msg.date, server=msg.server, date=msg.date,

View File

@ -8,7 +8,7 @@ from urllib.parse import quote
from nemubot.exception import IRCException from nemubot.exception import IRCException
from nemubot.hooks import hook from nemubot.hooks import hook
from nemubot.message import TextMessage from nemubot.message import Text
from nemubot.tools import web from nemubot.tools import web
nemubotversion = 3.4 nemubotversion = 3.4
@ -37,7 +37,7 @@ def gen_response(res, msg, srv):
if res is None: if res is None:
raise IRCException("mauvaise URL : %s" % srv) raise IRCException("mauvaise URL : %s" % srv)
else: else:
return TextMessage("URL pour %s : %s" % (srv, res), server=None, return Text("URL pour %s : %s" % (srv, res), server=None,
to=msg.to_response) to=msg.to_response)

View File

@ -30,6 +30,6 @@ class IRCException(Exception):
server=msg.server, to=msg.to_response) server=msg.server, to=msg.to_response)
else: else:
from nemubot.message import TextMessage from nemubot.message import Text
return TextMessage(self.message, return Text(self.message,
server=msg.server, to=msg.to_response) server=msg.server, to=msg.to_response)

View File

@ -39,12 +39,12 @@ class MessageHook(AbstractHook):
def match(self, msg, server=None): def match(self, msg, server=None):
if not isinstance(msg, nemubot.message.AbstractMessage): if not isinstance(msg, nemubot.message.abstract.Abstract):
return True return True
elif isinstance(msg, nemubot.message.Command): elif isinstance(msg, nemubot.message.Command):
return self.is_matching(msg.cmd, msg.to, server) return self.is_matching(msg.cmd, msg.to, server)
elif isinstance(msg, nemubot.message.TextMessage): elif isinstance(msg, nemubot.message.Text):
return self.is_matching(msg.message, msg.to, server) return self.is_matching(msg.message, msg.to, server)
else: else:
return False return False

View File

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
# Nemubot is a smart and modulable IM bot. # Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 Mercier Pierre-Olivier # Copyright (C) 2012-2015 Mercier Pierre-Olivier
# #
@ -16,157 +14,28 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime, timezone from nemubot.message.abstract import Abstract
from nemubot.message.text import Text
from nemubot.message.directask import DirectAsk
class AbstractMessage: from nemubot.message.command import Command
from nemubot.message.command import OwnerCommand
"""This class represents an abstract message"""
def __init__(self, server, date=None, to=None, to_response=None, frm=None):
"""Initialize an abstract message
Arguments:
server -- the servir identifier
date -- time of the message reception, default: now
to -- list of recipients
to_response -- if channel(s) where send the response differ
frm -- the sender
"""
self.server = server
self.date = datetime.now(timezone.utc) if date is None else date
self.to = to if to is not None else list()
self._to_response = (to_response if (to_response is None or
isinstance(to_response, list))
else [ to_response ])
self.frm = frm # None allowed when it designate this bot
self.frm_owner = False # Filled later, in consumer
@property
def to_response(self):
if self._to_response is not None:
return self._to_response
else:
return self.to
@property
def receivers(self):
# TODO: this is for legacy modules
return self.to_response
@property
def channel(self):
# TODO: this is for legacy modules
return self.to_response[0]
@property
def nick(self):
# TODO: this is for legacy modules
return self.frm
def accept(self, visitor):
visitor.visit(self)
def export_args(self, without=list()):
if not isinstance(without, list):
without = [ without ]
ret = {
"server": self.server,
"date": self.date,
"to": self.to,
"to_response": self._to_response,
"frm": self.frm
}
for w in without:
if w in ret:
del ret[w]
return ret
class TextMessage(AbstractMessage):
"""This class represent a simple message send to someone"""
def __init__(self, message, *args, **kargs):
"""Initialize a message with no particular specificity
Argument:
message -- the parsed message
"""
AbstractMessage.__init__(self, *args, **kargs)
self.message = message
def __str__(self):
return self.message
@property
def text(self):
# TODO: this is for legacy modules
return self.message
class DirectAsk(TextMessage):
"""This class represents a message to this bot"""
def __init__(self, designated, *args, **kargs):
"""Initialize a message to a specific person
Argument:
designated -- the user designated by the message
"""
TextMessage.__init__(self, *args, **kargs)
self.designated = designated
def respond(self, message):
return DirectAsk(self.frm,
message,
server=self.server,
to=self.to_response)
class Command(AbstractMessage):
"""This class represents a specialized TextMessage"""
def __init__(self, cmd, args=None, *nargs, **kargs):
AbstractMessage.__init__(self, *nargs, **kargs)
self.cmd = cmd
self.args = args if args is not None else list()
def __str__(self):
return self.cmd + " @" + ",@".join(self.args)
@property
def cmds(self):
# TODO: this is for legacy modules
return [self.cmd] + self.args
class OwnerCommand(Command):
"""This class represents a special command incomming from the owner"""
pass
def reload(): def reload():
global Abstract, Text, DirectAsk, Command, OwnerCommand
import imp import imp
import nemubot.message.abstract
imp.reload(nemubot.message.abstract)
Abstract = nemubot.message.abstract.Abstract
imp.reload(nemubot.message.text)
Text = nemubot.message.text.Text
imp.reload(nemubot.message.directask)
DirectAsk = nemubot.message.directask.DirectAsk
imp.reload(nemubot.message.command)
Command = nemubot.message.command.Command
OwnerCommand = nemubot.message.command.OwnerCommand
import nemubot.message.visitor import nemubot.message.visitor
imp.reload(nemubot.message.visitor) imp.reload(nemubot.message.visitor)

View File

@ -0,0 +1,90 @@
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 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/>.
from datetime import datetime, timezone
class Abstract:
"""This class represents an abstract message"""
def __init__(self, server, date=None, to=None, to_response=None, frm=None):
"""Initialize an abstract message
Arguments:
server -- the servir identifier
date -- time of the message reception, default: now
to -- list of recipients
to_response -- if channel(s) where send the response differ
frm -- the sender
"""
self.server = server
self.date = datetime.now(timezone.utc) if date is None else date
self.to = to if to is not None else list()
self._to_response = (to_response if (to_response is None or
isinstance(to_response, list))
else [ to_response ])
self.frm = frm # None allowed when it designate this bot
self.frm_owner = False # Filled later, in consumer
@property
def to_response(self):
if self._to_response is not None:
return self._to_response
else:
return self.to
@property
def receivers(self):
# TODO: this is for legacy modules
return self.to_response
@property
def channel(self):
# TODO: this is for legacy modules
return self.to_response[0]
@property
def nick(self):
# TODO: this is for legacy modules
return self.frm
def accept(self, visitor):
visitor.visit(self)
def export_args(self, without=list()):
if not isinstance(without, list):
without = [ without ]
ret = {
"server": self.server,
"date": self.date,
"to": self.to,
"to_response": self._to_response,
"frm": self.frm
}
for w in without:
if w in ret:
del ret[w]
return ret

View File

@ -0,0 +1,43 @@
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 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/>.
from nemubot.message.abstract import Abstract
class Command(Abstract):
"""This class represents a specialized TextMessage"""
def __init__(self, cmd, args=None, *nargs, **kargs):
Abstract.__init__(self, *nargs, **kargs)
self.cmd = cmd
self.args = args if args is not None else list()
def __str__(self):
return self.cmd + " @" + ",@".join(self.args)
@property
def cmds(self):
# TODO: this is for legacy modules
return [self.cmd] + self.args
class OwnerCommand(Command):
"""This class represents a special command incomming from the owner"""
pass

View File

@ -0,0 +1,39 @@
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 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/>.
from nemubot.message.text import Text
class DirectAsk(Text):
"""This class represents a message to this bot"""
def __init__(self, designated, *args, **kargs):
"""Initialize a message to a specific person
Argument:
designated -- the user designated by the message
"""
Text.__init__(self, *args, **kargs)
self.designated = designated
def respond(self, message):
return DirectAsk(self.frm,
message,
server=self.server,
to=self.to_response)

View File

@ -16,7 +16,7 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from nemubot.message import TextMessage from nemubot.message import Text
from nemubot.message.visitor import AbstractVisitor from nemubot.message.visitor import AbstractVisitor
@ -26,7 +26,7 @@ class IRC(AbstractVisitor):
self.pp = "" self.pp = ""
def visit_TextMessage(self, msg): def visit_Text(self, msg):
self.pp += "PRIVMSG %s :" % ",".join(msg.to) self.pp += "PRIVMSG %s :" % ",".join(msg.to)
if isinstance(msg.message, str): if isinstance(msg.message, str):
self.pp += msg.message self.pp += msg.message
@ -40,20 +40,20 @@ class IRC(AbstractVisitor):
# Avoid nick starting message when discussing on user channel # Avoid nick starting message when discussing on user channel
if len(others) != len(msg.to): if len(others) != len(msg.to):
res = TextMessage(msg.message, res = Text(msg.message,
server=msg.server, date=msg.date, server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm) to=msg.to, frm=msg.frm)
res.accept(self) res.accept(self)
if len(others): if len(others):
res = TextMessage("%s: %s" % (msg.designated, msg.message), res = Text("%s: %s" % (msg.designated, msg.message),
server=msg.server, date=msg.date, server=msg.server, date=msg.date,
to=others, frm=msg.frm) to=others, frm=msg.frm)
res.accept(self) res.accept(self)
def visit_Command(self, msg): def visit_Command(self, msg):
res = TextMessage("!%s%s%s" % (msg.cmd, res = Text("!%s%s%s" % (msg.cmd,
" " if len(msg.args) else "", " " if len(msg.args) else "",
" ".join(msg.args)), " ".join(msg.args)),
server=msg.server, date=msg.date, server=msg.server, date=msg.date,
@ -62,7 +62,7 @@ class IRC(AbstractVisitor):
def visit_OwnerCommand(self, msg): def visit_OwnerCommand(self, msg):
res = TextMessage("`%s%s%s" % (msg.cmd, res = Text("`%s%s%s" % (msg.cmd,
" " if len(msg.args) else "", " " if len(msg.args) else "",
" ".join(msg.args)), " ".join(msg.args)),
server=msg.server, date=msg.date, server=msg.server, date=msg.date,

41
nemubot/message/text.py Normal file
View File

@ -0,0 +1,41 @@
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 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/>.
from nemubot.message.abstract import Abstract
class Text(Abstract):
"""This class represent a simple message send to someone"""
def __init__(self, message, *args, **kargs):
"""Initialize a message with no particular specificity
Argument:
message -- the parsed message
"""
Abstract.__init__(self, *args, **kargs)
self.message = message
def __str__(self):
return self.message
@property
def text(self):
# TODO: this is for legacy modules
return self.message

View File

@ -20,7 +20,7 @@ def convert_legacy_store(old):
elif old == "ask_hook" or old == "ask_rgxp" or old == "ask_default": elif old == "ask_hook" or old == "ask_rgxp" or old == "ask_default":
return "in_DirectAsk" return "in_DirectAsk"
elif old == "msg_hook" or old == "msg_rgxp" or old == "msg_default": elif old == "msg_hook" or old == "msg_rgxp" or old == "msg_default":
return "in_TextMessage" return "in_Text"
elif old == "all_post": elif old == "all_post":
return "post" return "post"
elif old == "all_pre": elif old == "all_pre":

View File

@ -432,6 +432,6 @@ class IRCMessage:
# Normal message # Normal message
else: else:
return message.TextMessage(message=text, **common_args) return message.Text(message=text, **common_args)
return None return None