plugin text commands: handles coroutines + better command parsing:

- async coroutine can now be used for text commands (in addition to Deferred and regular
  callbacks)
- isidentifier is now used to detect a command, allowing to use "_" in command names
- if message starts with a single `/`, it is never sent (previously it could be sent is
  some corner cases)
- don't use OrderedDict anymore as dict now keep insertion order by default
This commit is contained in:
Goffi 2020-03-13 17:50:08 +01:00
commit f7fb2dd238

View file

@ -17,16 +17,17 @@
# 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 twisted.words.protocols.jabber import jid
from twisted.internet import defer
from twisted.python import failure
from sat.core.i18n import _ from sat.core.i18n import _
from sat.core.constants import Const as C from sat.core.constants import Const as C
from sat.core import exceptions from sat.core import exceptions
from twisted.words.protocols.jabber import jid
from twisted.internet import defer
from sat.core.log import getLogger from sat.core.log import getLogger
from sat.tools import utils
log = getLogger(__name__) log = getLogger(__name__)
from twisted.python import failure
from collections import OrderedDict
PLUGIN_INFO = { PLUGIN_INFO = {
C.PI_NAME: "Text commands", C.PI_NAME: "Text commands",
@ -81,10 +82,15 @@ class TextCommands(object):
@return (dict): dictionary with parsed data where key can be: @return (dict): dictionary with parsed data where key can be:
- "doc_short_help" (default: ""): the untranslated short documentation - "doc_short_help" (default: ""): the untranslated short documentation
- "type" (default "all"): the command type as specified in documentation - "type" (default "all"): the command type as specified in documentation
- "args" (default: ""): the arguments available, using syntax specified in documentation. - "args" (default: ""): the arguments available, using syntax specified in
documentation.
- "doc_arg_[name]": the doc of [name] argument - "doc_arg_[name]": the doc of [name] argument
""" """
data = OrderedDict([("doc_short_help", ""), ("type", "all"), ("args", "")]) data = {
"doc_short_help": "",
"type": "all",
"args": "",
}
docstring = cmd.__doc__ docstring = cmd.__doc__
if docstring is None: if docstring is None:
log.warning("No docstring found for command {}".format(cmd_name)) log.warning("No docstring found for command {}".format(cmd_name))
@ -169,9 +175,7 @@ class TextCommands(object):
).format(old_name=cmd_name, new_name=new_name) ).format(old_name=cmd_name, new_name=new_name)
) )
cmd_name = new_name cmd_name = new_name
self._commands[cmd_name] = cmd_data = OrderedDict( self._commands[cmd_name] = cmd_data = {"callback": cmd}
{"callback": cmd}
) # We use an Ordered dict to keep documenation order
cmd_data.update(self._parseDocString(cmd, cmd_name)) cmd_data.update(self._parseDocString(cmd, cmd_name))
log.info(_("Registered text command [%s]") % cmd_name) log.info(_("Registered text command [%s]") % cmd_name)
@ -233,65 +237,70 @@ class TextCommands(object):
# we have a command # we have a command
d = None d = None
command = msg[1:].partition(" ")[0].lower() command = msg[1:].partition(" ")[0].lower().strip()
if command.isalpha(): if not command.isidentifier():
# looks like an actual command, we try to call the corresponding method self.feedBack(
def retHandling(ret): client,
""" Handle command return value: _("Invalid command /%s. ") % command + self.HELP_SUGGESTION,
if ret is True, normally send message (possibly modified by command) mess_data,
else, abord message sending )
""" raise failure.Failure(exceptions.CancelError())
if ret:
return mess_data
else:
log.debug("text command detected ({})".format(command))
raise failure.Failure(exceptions.CancelError())
def genericErrback(failure): # looks like an actual command, we try to call the corresponding method
try: def retHandling(ret):
msg = "with condition {}".format(failure.value.condition) """ Handle command return value:
except AttributeError: if ret is True, normally send message (possibly modified by command)
msg = "with error {}".format(failure.value) else, abord message sending
self.feedBack(client, "Command failed {}".format(msg), mess_data) """
return False if ret:
return mess_data
else:
log.debug("text command detected ({})".format(command))
raise failure.Failure(exceptions.CancelError())
mess_data["unparsed"] = msg[ def genericErrback(failure):
1 + len(command) :
] # part not yet parsed of the message
try: try:
cmd_data = self._commands[command] msg = "with condition {}".format(failure.value.condition)
except KeyError: except AttributeError:
self.feedBack( msg = "with error {}".format(failure.value)
client, self.feedBack(client, "Command failed {}".format(msg), mess_data)
_("Unknown command /%s. ") % command + self.HELP_SUGGESTION, return False
mess_data,
mess_data["unparsed"] = msg[
1 + len(command) :
] # part not yet parsed of the message
try:
cmd_data = self._commands[command]
except KeyError:
self.feedBack(
client,
_("Unknown command /%s. ") % command + self.HELP_SUGGESTION,
mess_data,
)
log.debug("text command help message")
raise failure.Failure(exceptions.CancelError())
else:
if not self._contextValid(mess_data, cmd_data):
# The command is not launched in the right context, we throw a message with help instructions
context_txt = (
_("group discussions")
if cmd_data["type"] == "group"
else _("one to one discussions")
) )
log.debug("text command help message") feedback = _("/{command} command only applies in {context}.").format(
command=command, context=context_txt
)
self.feedBack(
client, "{} {}".format(feedback, self.HELP_SUGGESTION), mess_data
)
log.debug("text command invalid message")
raise failure.Failure(exceptions.CancelError()) raise failure.Failure(exceptions.CancelError())
else: else:
if not self._contextValid(mess_data, cmd_data): d = utils.asDeferred(cmd_data["callback"], client, mess_data)
# The command is not launched in the right context, we throw a message with help instructions d.addErrback(genericErrback)
context_txt = ( d.addCallback(retHandling)
_("group discussions")
if cmd_data["type"] == "group"
else _("one to one discussions")
)
feedback = _("/{command} command only applies in {context}.").format(
command=command, context=context_txt
)
self.feedBack(
client, "{} {}".format(feedback, self.HELP_SUGGESTION), mess_data
)
log.debug("text command invalid message")
raise failure.Failure(exceptions.CancelError())
else:
d = defer.maybeDeferred(cmd_data["callback"], client, mess_data)
d.addErrback(genericErrback)
d.addCallback(retHandling)
return ( return d
d or mess_data
) # if a command is detected, we should have a deferred, else we send the message normally
def _contextValid(self, mess_data, cmd_data): def _contextValid(self, mess_data, cmd_data):
"""Tell if a command can be used in the given context """Tell if a command can be used in the given context