Mercurial > libervia-backend
annotate sat/plugins/plugin_misc_text_commands.py @ 3012:2224fbbd45dd
memory (disco): fixed boolean to string conversion
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 18 Jul 2019 20:26:49 +0200 |
parents | dbfedde9bc61 |
children | ab2696e34d29 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1889
diff
changeset
|
1 #!/usr/bin/env python2 |
506
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
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 |
2771 | 5 # Copyright (C) 2009-2019 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 |
771 | 20 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
|
21 from sat.core.constants import Const as C |
1052
e88bff4c8b77
core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents:
1025
diff
changeset
|
22 from sat.core import exceptions |
508
7c6609dddb2c
plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents:
506
diff
changeset
|
23 from twisted.words.protocols.jabber import jid |
921 | 24 from twisted.internet import defer |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
929
diff
changeset
|
25 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
26 |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
929
diff
changeset
|
27 log = getLogger(__name__) |
1052
e88bff4c8b77
core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents:
1025
diff
changeset
|
28 from twisted.python import failure |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
29 from collections import OrderedDict |
506
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
32 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
|
33 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
|
34 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
|
35 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
|
36 C.PI_DEPENDENCIES: [], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
37 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
|
38 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
39 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
|
40 } |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
42 |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
43 class InvalidCommandSyntax(Exception): |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
44 """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
|
45 |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
46 pass |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
47 |
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 CMD_KEY = "@command" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
50 CMD_TYPES = ("group", "one2one", "all") |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
51 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
|
52 |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
53 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
54 class TextCommands(object): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 # 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
|
56 # plugins need a dynamic translation system (translation |
59b32c04e105
plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents:
515
diff
changeset
|
57 # 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
|
58 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 HELP_SUGGESTION = _( |
2689
d715d912afac
plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
60 u"Type '/help' to get a list of the available commands. If you didn't want to " |
d715d912afac
plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
61 u"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
|
62 ) |
1002
291eb8216f6e
plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
63 |
506
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 def __init__(self, host): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
929
diff
changeset
|
65 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
|
66 self.host = host |
2128 | 67 # this is internal command, so we set high priority |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2128
diff
changeset
|
68 host.trigger.add("sendMessage", self.sendMessageTrigger, 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
|
69 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
|
70 self._whois = [] |
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.registerTextCommands(self) |
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
|
72 |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
73 def _parseDocString(self, cmd, cmd_name): |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
74 """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
|
75 |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
76 @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
|
77 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
|
78 - 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
|
79 - @command keyword can be used, |
d715d912afac
plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
80 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
|
81 @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
|
82 - "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
|
83 - "type" (default "all"): the command type as specified in documentation |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
84 - "args" (default: ""): the arguments available, using syntax specified in documentation. |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
85 - "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
|
86 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
87 data = OrderedDict([("doc_short_help", ""), ("type", "all"), ("args", "")]) |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
88 docstring = cmd.__doc__ |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
89 if docstring is None: |
2842 | 90 log.warning(u"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
|
91 docstring = "" |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
92 |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
93 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
|
94 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
|
95 |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
96 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 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
|
98 |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
99 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
|
100 stripped = line.strip() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 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
|
102 colon_idx = line.find(":") |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
103 if colon_idx == -1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 raise InvalidCommandSyntax( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
105 "No colon found in argument description" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
106 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
107 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
|
108 if not arg_name: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
109 raise InvalidCommandSyntax( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
110 "No name found in argument description" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
111 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
112 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
|
113 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
|
114 elif cmd_indent: |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
115 # 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
|
116 break |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
117 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
|
118 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
|
119 |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
120 # type |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
121 colon_idx = stripped.find(":") |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
122 if colon_idx == -1: |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
123 raise InvalidCommandSyntax("missing colon") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
124 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
|
125 if len(type_data) == 0: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
126 type_data = "(all)" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
127 elif ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
128 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
|
129 ): |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 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
|
134 data["type"] = type_ |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
135 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
136 # args |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
137 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
|
138 except InvalidCommandSyntax as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 log.warning( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
140 u"Invalid command syntax for command {command}: {message}".format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 command=cmd_name, message=e.message |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
143 ) |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
144 |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
145 return data |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
146 |
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
|
147 def registerTextCommands(self, instance): |
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
|
148 """ 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
|
149 |
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
|
150 @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
|
151 """ |
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
|
152 for attr in dir(instance): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
153 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
|
154 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
|
155 if not callable(cmd): |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
156 log.warning(_(u"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
|
157 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
|
158 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
|
159 if not cmd_name: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
929
diff
changeset
|
160 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
|
161 if cmd_name in self._commands: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
162 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
|
163 while (cmd_name + str(suff)) in self._commands: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 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
|
165 new_name = cmd_name + str(suff) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
166 log.warning( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
167 _( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
168 u"Conflict for command [{old_name}], renaming it to [{new_name}]" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 ).format(old_name=cmd_name, new_name=new_name) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 ) |
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
|
171 cmd_name = new_name |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
172 self._commands[cmd_name] = cmd_data = OrderedDict( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
173 {"callback": cmd} |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
174 ) # We use an Ordered dict to keep documenation order |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
175 cmd_data.update(self._parseDocString(cmd, cmd_name)) |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
929
diff
changeset
|
176 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
|
177 |
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
|
178 def addWhoIsCb(self, callback, priority=0): |
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
|
179 """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
|
180 |
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 @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
|
182 - 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
|
183 its own strings to it |
1199 | 184 - 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
|
185 - profile: %(doc_profile)s |
2689
d715d912afac
plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
186 @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
|
187 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
|
188 """ |
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 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
|
190 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
|
191 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
192 def sendMessageTrigger( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
193 self, client, mess_data, pre_xml_treatments, post_xml_treatments |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
194 ): |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
195 """Install SendMessage command hook """ |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2128
diff
changeset
|
196 pre_xml_treatments.addCallback(self._sendMessageCmdHook, client) |
921 | 197 return True |
198 | |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2128
diff
changeset
|
199 def _sendMessageCmdHook(self, mess_data, client): |
921 | 200 """ 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
|
201 |
2689
d715d912afac
plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
202 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
|
203 else an help message is sent. |
921 | 204 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
|
205 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
|
206 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
|
207 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
|
208 parsed. |
d715d912afac
plugin XEP-0199: implementation of XMPP Ping
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
209 Commands can be deferred or not |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2128
diff
changeset
|
210 @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
|
211 @param profile: %(doc_profile)s |
921 | 212 """ |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
213 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
214 msg = mess_data["message"][""] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
215 msg_lang = "" |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
216 except KeyError: |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
217 try: |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
218 # we have not default message, we try to take the first found |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
219 msg_lang, msg = mess_data["message"].iteritems().next() |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
220 except StopIteration: |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
221 log.debug(u"No message found, skipping text commands") |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
222 return mess_data |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
223 |
921 | 224 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
225 if msg[:2] == "//": |
921 | 226 # 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
|
227 mess_data["message"][msg_lang] = msg[1:] |
921 | 228 return mess_data |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
229 if msg[0] != "/": |
921 | 230 return mess_data |
231 except IndexError: | |
232 return mess_data | |
233 | |
234 # we have a command | |
235 d = None | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
236 command = msg[1:].partition(" ")[0].lower() |
921 | 237 if command.isalpha(): |
238 # looks like an actual command, we try to call the corresponding method | |
239 def retHandling(ret): | |
240 """ Handle command return value: | |
241 if ret is True, normally send message (possibly modified by command) | |
242 else, abord message sending | |
243 """ | |
244 if ret: | |
245 return mess_data | |
246 else: | |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
247 log.debug(u"text command detected ({})".format(command)) |
1052
e88bff4c8b77
core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents:
1025
diff
changeset
|
248 raise failure.Failure(exceptions.CancelError()) |
921 | 249 |
1082
246712d2e7bc
plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents:
1052
diff
changeset
|
250 def genericErrback(failure): |
1889
be61c5c5e474
plugin text commands: fixed log in genericErrback which was raising an exception when something than StanzaError was received
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
251 try: |
be61c5c5e474
plugin text commands: fixed log in genericErrback which was raising an exception when something than StanzaError was received
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
252 msg = u"with condition {}".format(failure.value.condition) |
be61c5c5e474
plugin text commands: fixed log in genericErrback which was raising an exception when something than StanzaError was received
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
253 except AttributeError: |
be61c5c5e474
plugin text commands: fixed log in genericErrback which was raising an exception when something than StanzaError was received
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
254 msg = u"with error {}".format(failure.value) |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
255 self.feedBack(client, u"Command failed {}".format(msg), mess_data) |
1082
246712d2e7bc
plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents:
1052
diff
changeset
|
256 return False |
246712d2e7bc
plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents:
1052
diff
changeset
|
257 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
258 mess_data["unparsed"] = msg[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
259 1 + len(command) : |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
260 ] # part not yet parsed of the message |
921 | 261 try: |
1370
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
262 cmd_data = self._commands[command] |
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
|
263 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
264 self.feedBack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
265 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
266 _("Unknown command /%s. ") % command + self.HELP_SUGGESTION, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
267 mess_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
268 ) |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
269 log.debug("text command help message") |
1052
e88bff4c8b77
core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents:
1025
diff
changeset
|
270 raise failure.Failure(exceptions.CancelError()) |
1370
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
271 else: |
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
272 if not self._contextValid(mess_data, cmd_data): |
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
273 # The command is not launched in the right context, we throw a message with help instructions |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
274 context_txt = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
275 _("group discussions") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
276 if cmd_data["type"] == "group" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
277 else _("one to one discussions") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
278 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
279 feedback = _("/{command} command only applies in {context}.").format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
280 command=command, context=context_txt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
281 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
282 self.feedBack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
283 client, u"{} {}".format(feedback, self.HELP_SUGGESTION), mess_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
284 ) |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
285 log.debug("text command invalid message") |
1370
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
286 raise failure.Failure(exceptions.CancelError()) |
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
287 else: |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
288 d = defer.maybeDeferred(cmd_data["callback"], client, mess_data) |
1370
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
289 d.addErrback(genericErrback) |
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
290 d.addCallback(retHandling) |
921 | 291 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
292 return ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
293 d or mess_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
294 ) # if a command is detected, we should have a deferred, else we send the message normally |
506
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 |
1370
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
296 def _contextValid(self, mess_data, cmd_data): |
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
297 """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
|
298 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2128
diff
changeset
|
299 @param mess_data(dict): message data as given in sendMessage trigger |
1370
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
300 @param cmd_data(dict): command data as returned by self._parseDocString |
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
301 @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
|
302 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
303 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
|
304 cmd_data["type"] == "one2one" and mess_data["type"] == "groupchat" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
305 ): |
1370
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
306 return False |
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
307 return True |
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
308 |
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
|
309 def getRoomJID(self, arg, service_jid): |
508
7c6609dddb2c
plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents:
506
diff
changeset
|
310 """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
|
311 |
508
7c6609dddb2c
plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents:
506
diff
changeset
|
312 @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
|
313 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
|
314 @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
|
315 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
316 nb_arobas = arg.count("@") |
508
7c6609dddb2c
plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents:
506
diff
changeset
|
317 if nb_arobas == 1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
318 if arg[-1] != "@": |
508
7c6609dddb2c
plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents:
506
diff
changeset
|
319 return jid.JID(arg) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
320 return jid.JID(arg + service_jid) |
508
7c6609dddb2c
plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents:
506
diff
changeset
|
321 return jid.JID(u"%s@%s" % (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
|
322 |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
323 def feedBack(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
|
324 """Give a message back to the user""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
325 if mess_data["type"] == "groupchat": |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
326 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
|
327 else: |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
328 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
|
329 |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
330 # 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
|
331 mess_data["from"] = mess_data["to"] |
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
332 mess_data["to"] = to_ |
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
333 mess_data["type"] = C.MESS_TYPE_INFO |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
334 mess_data["message"] = {"": message} |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
335 mess_data["extra"]["info_type"] = info_type |
2152
6a004a22dd9e
plugins XEP-0033, XEP-0280, text commands: fixed method renaming after client refactoring
Goffi <goffi@goffi.org>
parents:
2145
diff
changeset
|
336 client.messageSendToBridge(mess_data) |
523
24c0d51449e7
plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents:
519
diff
changeset
|
337 |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
338 def cmd_whois(self, client, mess_data): |
1372
85caf0a3abb3
plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents:
1371
diff
changeset
|
339 """show informations on entity |
85caf0a3abb3
plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents:
1371
diff
changeset
|
340 |
85caf0a3abb3
plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents:
1371
diff
changeset
|
341 @command: [JID|ROOM_NICK] |
85caf0a3abb3
plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents:
1371
diff
changeset
|
342 - JID: entity to request |
85caf0a3abb3
plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents:
1371
diff
changeset
|
343 - ROOM_NICK: nick of the room to request |
85caf0a3abb3
plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents:
1371
diff
changeset
|
344 """ |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
929
diff
changeset
|
345 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
|
346 |
600
c5451501465b
plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
347 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
|
348 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
349 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
|
350 room = mess_data["to"].userhostJID() |
73873e9b56f7
plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
351 try: |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
352 if self.host.plugins["XEP-0045"].isNickInRoom(client, room, entity): |
928
73873e9b56f7
plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
353 entity = u"%s/%s" % (room, entity) |
73873e9b56f7
plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
354 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
929
diff
changeset
|
355 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
|
356 |
600
c5451501465b
plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
357 if not entity: |
c5451501465b
plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
358 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
|
359 else: |
600
c5451501465b
plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
360 try: |
c5451501465b
plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
361 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
|
362 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
|
363 raise jid.InvalidFormat |
1742
244a605623d6
complete the Exception's list when catching JID error:
souliane <souliane@mailoo.org>
parents:
1409
diff
changeset
|
364 except (RuntimeError, jid.InvalidFormat, AttributeError): |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
365 self.feedBack(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
|
366 return False |
c5451501465b
plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
367 |
921 | 368 if not target_jid.resource: |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
369 target_jid.resource = self.host.memory.getMainResource(client, target_jid) |
921 | 370 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
371 whois_msg = [_(u"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
|
372 |
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
|
373 d = defer.succeed(None) |
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
|
374 for ignore, callback in self._whois: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
375 d.addCallback( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
376 lambda ignore: callback(client, whois_msg, mess_data, target_jid) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
377 ) |
921 | 378 |
379 def feedBack(ignore): | |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
380 self.feedBack(client, u"\n".join(whois_msg), mess_data) |
921 | 381 return False |
382 | |
383 d.addCallback(feedBack) | |
384 return d | |
600
c5451501465b
plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
385 |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
386 def _getArgsHelp(self, cmd_data): |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
387 """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
|
388 |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
389 @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
|
390 @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
|
391 """ |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
392 strings = [] |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
393 for doc_name, doc_help in cmd_data.iteritems(): |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
394 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
|
395 arg_name = doc_name[8:] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
396 strings.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
397 u"- {name}: {doc_help}".format(name=arg_name, doc_help=_(doc_help)) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
398 ) |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
399 |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
400 return strings |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
401 |
2055
ed33cd382bf9
plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
402 def cmd_me(self, client, mess_data): |
ed33cd382bf9
plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
403 """display a message at third person |
ed33cd382bf9
plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
404 |
ed33cd382bf9
plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
405 @command (all): message |
ed33cd382bf9
plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
406 - message: message to show at third person |
ed33cd382bf9
plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
407 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
|
408 """ |
ed33cd382bf9
plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
409 # 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
|
410 return True |
ed33cd382bf9
plugin XEP-0245: removed plugin XEP-0245:
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
411 |
2111
98672e35d2f5
plugin text commands: added /whoami command which return current full jid
Goffi <goffi@goffi.org>
parents:
2055
diff
changeset
|
412 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
|
413 """give your own jid""" |
98672e35d2f5
plugin text commands: added /whoami command which return current full jid
Goffi <goffi@goffi.org>
parents:
2055
diff
changeset
|
414 self.feedBack(client, client.jid.full(), mess_data) |
98672e35d2f5
plugin text commands: added /whoami command which return current full jid
Goffi <goffi@goffi.org>
parents:
2055
diff
changeset
|
415 |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
416 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
|
417 """show help on available commands |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
418 |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
419 @command: [cmd_name] |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
420 - 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
|
421 """ |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
422 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
|
423 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
|
424 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
|
425 if cmd_name and cmd_name not in self._commands: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
426 self.feedBack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
427 client, _(u"Invalid command name [{}]\n".format(cmd_name)), mess_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
428 ) |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
429 cmd_name = "" |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
430 if not cmd_name: |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
431 # 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
|
432 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
|
433 help_cmds = [] |
517
59b32c04e105
plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents:
515
diff
changeset
|
434 |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
435 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
|
436 cmd_data = self._commands[command] |
1370
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
437 if not self._contextValid(mess_data, cmd_data): |
53c7678c27a6
plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents:
1369
diff
changeset
|
438 continue |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
439 spaces = (longuest - len(command)) * " " |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
440 help_cmds.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
441 " /{command}: {spaces} {short_help}".format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
442 command=command, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
443 spaces=spaces, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
444 short_help=cmd_data["doc_short_help"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
445 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
446 ) |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
447 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
448 help_mess = _(u"Text commands available:\n%s") % (u"\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
|
449 else: |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
450 # 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
|
451 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
|
452 syntax = cmd_data["args"] |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
453 help_mess = _(u"/{name}: {short_help}\n{syntax}{args_help}").format( |
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
454 name=cmd_name, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
455 short_help=cmd_data["doc_short_help"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
456 syntax=_(" " * 4 + "syntax: {}\n").format(syntax) if syntax else "", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
457 args_help=u"\n".join( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
458 [u" " * 8 + "{}".format(line) for line in self._getArgsHelp(cmd_data)] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
459 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
460 ) |
1369
dd1a148bd3d8
plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
461 |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
462 self.feedBack(client, help_mess, mess_data) |