annotate sat/plugins/plugin_misc_text_commands.py @ 4067:3f62c2445df1

plugin XEP-0339: "Source-Specific Media Attributes in Jingle" implementation: rel 439
author Goffi <goffi@goffi.org>
date Thu, 01 Jun 2023 20:53:33 +0200
parents 524856bd7b19
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
4 # SàT plugin for managing text commands
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3216
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
10 # (at your option) any later version.
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
15 # GNU Affero General Public License for more details.
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 600
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
20 from twisted.words.protocols.jabber import jid
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
21 from twisted.internet import defer
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
22 from twisted.python import failure
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 698
diff changeset
23 from sat.core.i18n import _
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
24 from sat.core.constants import Const as C
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 1025
diff changeset
25 from sat.core import exceptions
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
26 from sat.core.log import getLogger
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
27 from sat.tools import utils
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
28
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
29
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
30 log = getLogger(__name__)
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
31
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
32 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
33 C.PI_NAME: "Text commands",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
34 C.PI_IMPORT_NAME: C.TEXT_CMDS,
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
35 C.PI_TYPE: "Misc",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
36 C.PI_PROTOCOLS: ["XEP-0245"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
37 C.PI_DEPENDENCIES: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
38 C.PI_MAIN: "TextCommands",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
39 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 C.PI_DESCRIPTION: _("""IRC like text commands"""),
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
41 }
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
42
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
43
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
44 class InvalidCommandSyntax(Exception):
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
45 """Throwed while parsing @command in docstring if syntax is invalid"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
47 pass
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
48
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
49
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
50 CMD_KEY = "@command"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 CMD_TYPES = ("group", "one2one", "all")
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
52 FEEDBACK_INFO_TYPE = "TEXT_CMD"
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
53
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
54
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
55 class TextCommands(object):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 # FIXME: doc strings for commands have to be translatable
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
57 # plugins need a dynamic translation system (translation
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
58 # should be downloadable independently)
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
59
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 HELP_SUGGESTION = _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
61 "Type '/help' to get a list of the available commands. If you didn't want to "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
62 "use a command, please start your message with '//' to escape the slash."
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 )
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
64
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
66 log.info(_("Text commands initialization"))
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.host = host
2128
aa94f33fd2ad plugin otr: various improvments:
Goffi <goffi@goffi.org>
parents: 2111
diff changeset
68 # this is internal command, so we set high priority
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
69 host.trigger.add("sendMessage", self.send_message_trigger, priority=1000000)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
70 self._commands = {}
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
71 self._whois = []
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
72 self.register_text_commands(self)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
73
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
74 def _parse_doc_string(self, cmd, cmd_name):
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
75 """Parse a docstring to get text command data
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
76
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
77 @param cmd: function or method callback for the command,
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
78 its docstring will be used for self documentation in the following way:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
79 - first line is the command short documentation, shown with /help
2689
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
80 - @command keyword can be used,
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
81 see http://wiki.goffi.org/wiki/Coding_style/en for documentation
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
82 @return (dict): dictionary with parsed data where key can be:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
83 - "doc_short_help" (default: ""): the untranslated short documentation
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
84 - "type" (default "all"): the command type as specified in documentation
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
85 - "args" (default: ""): the arguments available, using syntax specified in
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
86 documentation.
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
87 - "doc_arg_[name]": the doc of [name] argument
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
88 """
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
89 data = {
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
90 "doc_short_help": "",
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
91 "type": "all",
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
92 "args": "",
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
93 }
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
94 docstring = cmd.__doc__
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
95 if docstring is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
96 log.warning("No docstring found for command {}".format(cmd_name))
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
97 docstring = ""
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
98
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
99 doc_data = docstring.split("\n")
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
100 data["doc_short_help"] = doc_data[0]
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
101
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
102 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 cmd_indent = 0 # >0 when @command is found are we are parsing it
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
104
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
105 for line in doc_data:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
106 stripped = line.strip()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 if cmd_indent and line[cmd_indent : cmd_indent + 5] == " -":
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
108 colon_idx = line.find(":")
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
109 if colon_idx == -1:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 raise InvalidCommandSyntax(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 "No colon found in argument description"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
112 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113 arg_name = line[cmd_indent + 6 : colon_idx].strip()
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
114 if not arg_name:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
115 raise InvalidCommandSyntax(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
116 "No name found in argument description"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 arg_help = line[colon_idx + 1 :].strip()
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
119 data["doc_arg_{}".format(arg_name)] = arg_help
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
120 elif cmd_indent:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
121 # we are parsing command and indent level is not good, it's finished
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
122 break
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
123 elif stripped.startswith(CMD_KEY):
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
124 cmd_indent = line.find(CMD_KEY)
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
125
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
126 # type
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
127 colon_idx = stripped.find(":")
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
128 if colon_idx == -1:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
129 raise InvalidCommandSyntax("missing colon")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 type_data = stripped[len(CMD_KEY) : colon_idx].strip()
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
131 if len(type_data) == 0:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 type_data = "(all)"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 elif (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 len(type_data) <= 2 or type_data[0] != "(" or type_data[-1] != ")"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135 ):
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
136 raise InvalidCommandSyntax("Bad type data syntax")
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
137 type_ = type_data[1:-1]
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
138 if type_ not in CMD_TYPES:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
139 raise InvalidCommandSyntax("Unknown type {}".format(type_))
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
140 data["type"] = type_
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
141
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 # args
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 data["args"] = stripped[colon_idx + 1 :].strip()
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
144 except InvalidCommandSyntax as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
146 "Invalid command syntax for command {command}: {message}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
147 command=cmd_name, message=e.message
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
148 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
149 )
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
150
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
151 return data
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
152
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
153 def register_text_commands(self, instance):
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
154 """ Add a text command
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
155
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
156 @param instance: instance of a class containing text commands
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
157 """
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
158 for attr in dir(instance):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
159 if attr.startswith("cmd_"):
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
160 cmd = getattr(instance, attr)
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
161 if not callable(cmd):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
162 log.warning(_("Skipping not callable [%s] attribute") % attr)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
163 continue
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
164 cmd_name = attr[4:]
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
165 if not cmd_name:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
166 log.warning(_("Skipping cmd_ method"))
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
167 if cmd_name in self._commands:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
168 suff = 2
927
cd150dd947e3 plugin text-commands: fixed name conflicts management + plugin parrot: removed now done TODO
Goffi <goffi@goffi.org>
parents: 926
diff changeset
169 while (cmd_name + str(suff)) in self._commands:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
170 suff += 1
927
cd150dd947e3 plugin text-commands: fixed name conflicts management + plugin parrot: removed now done TODO
Goffi <goffi@goffi.org>
parents: 926
diff changeset
171 new_name = cmd_name + str(suff)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
172 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
174 "Conflict for command [{old_name}], renaming it to [{new_name}]"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 ).format(old_name=cmd_name, new_name=new_name)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
176 )
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
177 cmd_name = new_name
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
178 self._commands[cmd_name] = cmd_data = {"callback": cmd}
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
179 cmd_data.update(self._parse_doc_string(cmd, cmd_name))
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
180 log.info(_("Registered text command [%s]") % cmd_name)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
181
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
182 def add_who_is_cb(self, callback, priority=0):
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
183 """Add a callback which give information to the /whois command
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
184
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
185 @param callback: a callback which will be called with the following arguments
2689
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
186 - whois_msg: list of information strings to display, callback need to append
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
187 its own strings to it
1199
96fb74a4714d misc: fixed some typos
Matteo Cypriani <mcy@lm7.fr>
parents: 1171
diff changeset
188 - target_jid: full jid from whom we want information
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
189 - profile: %(doc_profile)s
2689
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
190 @param priority: priority of the information to show (the highest priority will
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
191 be displayed first)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
192 """
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
193 self._whois.append((priority, callback))
928
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
194 self._whois.sort(key=lambda item: item[0], reverse=True)
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
195
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
196 def send_message_trigger(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
197 self, client, mess_data, pre_xml_treatments, post_xml_treatments
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
198 ):
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
199 """Install SendMessage command hook """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
200 pre_xml_treatments.addCallback(self._send_message_cmd_hook, client)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
201 return True
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
202
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
203 def _send_message_cmd_hook(self, mess_data, client):
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
204 """ Check text commands in message, and react consequently
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
205
2689
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
206 msg starting with / are potential command. If a command is found, it is executed,
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
207 else an help message is sent.
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
208 msg starting with // are escaped: they are sent with a single /
2689
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
209 commands can abord message sending (if they return anything evaluating to False),
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
210 or continue it (if they return True), eventually after modifying the message
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
211 an "unparsed" key is added to message, containing part of the message not yet
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
212 parsed.
d715d912afac plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
213 Commands can be deferred or not
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2128
diff changeset
214 @param mess_data(dict): data comming from sendMessage trigger
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
215 @param profile: %(doc_profile)s
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
216 """
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
217 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
218 msg = mess_data["message"][""]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
219 msg_lang = ""
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
220 except KeyError:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
221 try:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
222 # we have not default message, we try to take the first found
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
223 msg_lang, msg = next(iter(mess_data["message"].items()))
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
224 except StopIteration:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
225 log.debug("No message found, skipping text commands")
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
226 return mess_data
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
227
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
228 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
229 if msg[:2] == "//":
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
230 # we have a double '/', it's the escape sequence
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
231 mess_data["message"][msg_lang] = msg[1:]
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
232 return mess_data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
233 if msg[0] != "/":
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
234 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
235 except IndexError:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
236 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
237
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
238 # we have a command
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
239 d = None
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
240 command = msg[1:].partition(" ")[0].lower().strip()
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
241 if not command.isidentifier():
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
242 self.feed_back(
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
243 client,
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
244 _("Invalid command /%s. ") % command + self.HELP_SUGGESTION,
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
245 mess_data,
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
246 )
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
247 raise failure.Failure(exceptions.CancelError())
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
248
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
249 # looks like an actual command, we try to call the corresponding method
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
250 def ret_handling(ret):
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
251 """ Handle command return value:
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
252 if ret is True, normally send message (possibly modified by command)
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
253 else, abord message sending
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
254 """
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
255 if ret:
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
256 return mess_data
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
257 else:
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
258 log.debug("text command detected ({})".format(command))
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
259 raise failure.Failure(exceptions.CancelError())
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
260
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
261 def generic_errback(failure):
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
262 try:
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
263 msg = "with condition {}".format(failure.value.condition)
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
264 except AttributeError:
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
265 msg = "with error {}".format(failure.value)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
266 self.feed_back(client, "Command failed {}".format(msg), mess_data)
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
267 return False
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
268
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
269 mess_data["unparsed"] = msg[
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
270 1 + len(command) :
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
271 ] # part not yet parsed of the message
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
272 try:
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
273 cmd_data = self._commands[command]
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
274 except KeyError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
275 self.feed_back(
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
276 client,
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
277 _("Unknown command /%s. ") % command + self.HELP_SUGGESTION,
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
278 mess_data,
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
279 )
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
280 log.debug("text command help message")
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
281 raise failure.Failure(exceptions.CancelError())
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
282 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
283 if not self._context_valid(mess_data, cmd_data):
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
284 # The command is not launched in the right context, we throw a message with help instructions
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
285 context_txt = (
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
286 _("group discussions")
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
287 if cmd_data["type"] == "group"
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
288 else _("one to one discussions")
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
289 )
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
290 feedback = _("/{command} command only applies in {context}.").format(
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
291 command=command, context=context_txt
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
292 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
293 self.feed_back(
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
294 client, "{} {}".format(feedback, self.HELP_SUGGESTION), mess_data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
295 )
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
296 log.debug("text command invalid message")
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 1025
diff changeset
297 raise failure.Failure(exceptions.CancelError())
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
298 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
299 d = utils.as_deferred(cmd_data["callback"], client, mess_data)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
300 d.addErrback(generic_errback)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
301 d.addCallback(ret_handling)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
302
3216
8418e0c83ed7 plugin text commands: handles coroutines + better command parsing:
Goffi <goffi@goffi.org>
parents: 3156
diff changeset
303 return d
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
304
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
305 def _context_valid(self, mess_data, cmd_data):
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
306 """Tell if a command can be used in the given context
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
307
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2128
diff changeset
308 @param mess_data(dict): message data as given in sendMessage trigger
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
309 @param cmd_data(dict): command data as returned by self._parse_doc_string
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
310 @return (bool): True if command can be used in this context
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
311 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
312 if (cmd_data["type"] == "group" and mess_data["type"] != "groupchat") or (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
313 cmd_data["type"] == "one2one" and mess_data["type"] == "groupchat"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
314 ):
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
315 return False
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
316 return True
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
317
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
318 def get_room_jid(self, arg, service_jid):
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
319 """Return a room jid with a shortcut
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
320
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
321 @param arg: argument: can be a full room jid (e.g.: sat@chat.jabberfr.org)
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
322 or a shortcut (e.g.: sat or sat@ for sat on current service)
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
323 @param service_jid: jid of the current service (e.g.: chat.jabberfr.org)
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
324 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
325 nb_arobas = arg.count("@")
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
326 if nb_arobas == 1:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
327 if arg[-1] != "@":
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
328 return jid.JID(arg)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
329 return jid.JID(arg + service_jid)
3544
ae5f63e5ed2c plugin XEP-0045: fix `/join` text command
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
330 return jid.JID(f"{arg}@{service_jid}")
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
331
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
332 def feed_back(self, client, message, mess_data, info_type=FEEDBACK_INFO_TYPE):
523
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
333 """Give a message back to the user"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
334 if mess_data["type"] == "groupchat":
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
335 to_ = mess_data["to"].userhostJID()
523
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
336 else:
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
337 to_ = client.jid
523
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
338
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
339 # we need to invert send message back, so sender need to original destinee
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
340 mess_data["from"] = mess_data["to"]
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
341 mess_data["to"] = to_
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
342 mess_data["type"] = C.MESS_TYPE_INFO
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
343 mess_data["message"] = {"": message}
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
344 mess_data["extra"]["info_type"] = info_type
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
345 client.message_send_to_bridge(mess_data)
523
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
346
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
347 def cmd_whois(self, client, mess_data):
1372
85caf0a3abb3 plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents: 1371
diff changeset
348 """show informations on entity
85caf0a3abb3 plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents: 1371
diff changeset
349
85caf0a3abb3 plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents: 1371
diff changeset
350 @command: [JID|ROOM_NICK]
85caf0a3abb3 plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents: 1371
diff changeset
351 - JID: entity to request
85caf0a3abb3 plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents: 1371
diff changeset
352 - ROOM_NICK: nick of the room to request
85caf0a3abb3 plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents: 1371
diff changeset
353 """
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
354 log.debug("Catched whois command")
698
d731ae066158 core: sendMessage's options parameter has been renamed to extra to be consistent with newMessage
Goffi <goffi@goffi.org>
parents: 697
diff changeset
355
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
356 entity = mess_data["unparsed"].strip()
698
d731ae066158 core: sendMessage's options parameter has been renamed to extra to be consistent with newMessage
Goffi <goffi@goffi.org>
parents: 697
diff changeset
357
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
358 if mess_data["type"] == "groupchat":
928
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
359 room = mess_data["to"].userhostJID()
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
360 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
361 if self.host.plugins["XEP-0045"].is_nick_in_room(client, room, entity):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
362 entity = "%s/%s" % (room, entity)
928
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
363 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
364 log.warning("plugin XEP-0045 is not present")
698
d731ae066158 core: sendMessage's options parameter has been renamed to extra to be consistent with newMessage
Goffi <goffi@goffi.org>
parents: 697
diff changeset
365
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
366 if not entity:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
367 target_jid = mess_data["to"]
698
d731ae066158 core: sendMessage's options parameter has been renamed to extra to be consistent with newMessage
Goffi <goffi@goffi.org>
parents: 697
diff changeset
368 else:
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
369 try:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
370 target_jid = jid.JID(entity)
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
371 if not target_jid.user or not target_jid.host:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
372 raise jid.InvalidFormat
1742
244a605623d6 complete the Exception's list when catching JID error:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
373 except (RuntimeError, jid.InvalidFormat, AttributeError):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
374 self.feed_back(client, _("Invalid jid, can't whois"), mess_data)
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
375 return False
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
376
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
377 if not target_jid.resource:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
378 target_jid.resource = self.host.memory.main_resource_get(client, target_jid)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
379
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
380 whois_msg = [_("whois for %(jid)s") % {"jid": target_jid}]
698
d731ae066158 core: sendMessage's options parameter has been renamed to extra to be consistent with newMessage
Goffi <goffi@goffi.org>
parents: 697
diff changeset
381
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
382 d = defer.succeed(None)
3156
0318802dfe28 core (memory/disco): fixed checkFeature return Failure:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
383 for __, callback in self._whois:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
384 d.addCallback(
3156
0318802dfe28 core (memory/disco): fixed checkFeature return Failure:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
385 lambda __: callback(client, whois_msg, mess_data, target_jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
386 )
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
387
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
388 def feed_back(__):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
389 self.feed_back(client, "\n".join(whois_msg), mess_data)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
390 return False
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
391
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
392 d.addCallback(feed_back)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
393 return d
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
394
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
395 def _get_args_help(self, cmd_data):
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
396 """Return help string for args of cmd_name, according to docstring data
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
397
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
398 @param cmd_data: command data
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
399 @return (list[unicode]): help strings
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
400 """
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
401 strings = []
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
402 for doc_name, doc_help in cmd_data.items():
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
403 if doc_name.startswith("doc_arg_"):
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
404 arg_name = doc_name[8:]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
405 strings.append(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
406 "- {name}: {doc_help}".format(name=arg_name, doc_help=_(doc_help))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
407 )
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
408
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
409 return strings
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
410
2055
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
411 def cmd_me(self, client, mess_data):
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
412 """display a message at third person
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
413
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
414 @command (all): message
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
415 - message: message to show at third person
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
416 e.g.: "/me clenches his fist" will give "[YOUR_NICK] clenches his fist"
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
417 """
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
418 # We just ignore the command as the match is done on receiption by clients
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
419 return True
ed33cd382bf9 plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents: 1970
diff changeset
420
2111
98672e35d2f5 plugin text commands: added /whoami command which return current full jid
Goffi <goffi@goffi.org>
parents: 2055
diff changeset
421 def cmd_whoami(self, client, mess_data):
98672e35d2f5 plugin text commands: added /whoami command which return current full jid
Goffi <goffi@goffi.org>
parents: 2055
diff changeset
422 """give your own jid"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
423 self.feed_back(client, client.jid.full(), mess_data)
2111
98672e35d2f5 plugin text commands: added /whoami command which return current full jid
Goffi <goffi@goffi.org>
parents: 2055
diff changeset
424
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
425 def cmd_help(self, client, mess_data):
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
426 """show help on available commands
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
427
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
428 @command: [cmd_name]
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
429 - cmd_name: name of the command for detailed help
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
430 """
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
431 cmd_name = mess_data["unparsed"].strip()
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
432 if cmd_name and cmd_name[0] == "/":
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
433 cmd_name = cmd_name[1:]
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
434 if cmd_name and cmd_name not in self._commands:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
435 self.feed_back(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
436 client, _("Invalid command name [{}]\n".format(cmd_name)), mess_data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
437 )
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
438 cmd_name = ""
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
439 if not cmd_name:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
440 # we show the global help
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
441 longuest = max([len(command) for command in self._commands])
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
442 help_cmds = []
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
443
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
444 for command in sorted(self._commands):
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
445 cmd_data = self._commands[command]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
446 if not self._context_valid(mess_data, cmd_data):
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
447 continue
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
448 spaces = (longuest - len(command)) * " "
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
449 help_cmds.append(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
450 " /{command}: {spaces} {short_help}".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
451 command=command,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
452 spaces=spaces,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
453 short_help=cmd_data["doc_short_help"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
454 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
455 )
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
456
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
457 help_mess = _("Text commands available:\n%s") % ("\n".join(help_cmds),)
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
458 else:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
459 # we show detailled help for a command
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
460 cmd_data = self._commands[cmd_name]
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
461 syntax = cmd_data["args"]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
462 help_mess = _("/{name}: {short_help}\n{syntax}{args_help}").format(
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
463 name=cmd_name,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
464 short_help=cmd_data["doc_short_help"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
465 syntax=_(" " * 4 + "syntax: {}\n").format(syntax) if syntax else "",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2842
diff changeset
466 args_help="\n".join(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
467 [" " * 8 + "{}".format(line) for line in self._get_args_help(cmd_data)]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
468 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
469 )
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
470
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3544
diff changeset
471 self.feed_back(client, help_mess, mess_data)