annotate src/plugins/plugin_misc_text_commands.py @ 1370:53c7678c27a6

plugin text commands: added _contextValid method: - _contextValid check if a method can be called in this context, according to docstring declaration - use it to parse results in /help command - use it avoid use of command in wrong context, and help message is sent in this case
author Goffi <goffi@goffi.org>
date Thu, 19 Mar 2015 14:29:03 +0100
parents dd1a148bd3d8
children 876c9fbd0b3d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
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
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 771
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 698
diff changeset
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
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
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
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
26 log = getLogger(__name__)
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 1025
diff changeset
27 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
28 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
29
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
30 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
31 "name": "Text commands",
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
32 "import_name": C.TEXT_CMDS,
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
33 "type": "Misc",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
34 "protocols": [],
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
35 "dependencies": [],
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
36 "main": "TextCommands",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
37 "handler": "no",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
38 "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
39 }
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
40
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
41
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
42 class InvalidCommandSyntax(Exception):
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
43 """Throwed while parsing @command in docstring if syntax is invalid"""
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
44 pass
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
45
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
46
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
47 CMD_KEY = "@command"
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
48 CMD_TYPES = ('group', 'one2one', 'all')
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
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
51 class TextCommands(object):
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
52 #FIXME: doc strings for commands have to be translatable
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
53 # plugins need a dynamic translation system (translation
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
54 # 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
55
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
56 HELP_SUGGESTION = _("Type '/help' to get a list of the available commands. If you didn't want to use a command, please start your message with '//' to escape the slash.")
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
57
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
59 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
60 self.host = host
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
61 host.trigger.add("sendMessage", self.sendMessageTrigger)
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
62 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
63 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
64 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
65
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
66 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
67 """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
68
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
69 @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
70 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
71 - first line is the command short documentation, shown with /help
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
72 - @command keyword can be used, see http://wiki.goffi.org/wiki/Coding_style/en for documentation
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
73 @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
74 - "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
75 - "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
76 - "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
77 - "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
78 """
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
79 data = OrderedDict([('doc_short_help', ""),
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
80 ('type', 'all'),
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
81 ('args', '')])
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
82 docstring = cmd.__doc__
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
83 if docstring is None:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
84 log.warning(u"Not docstring found for command {}".format(cmd_name))
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
85 docstring = ""
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
86
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
87 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
88 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
89
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
90 try:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
91 cmd_indent = 0 # >0 when @command is found are we are parsing it
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 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
94 stripped = line.strip()
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
95 if cmd_indent and line[cmd_indent:cmd_indent+5] == " -":
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
96 colon_idx = line.find(":")
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
97 if colon_idx == -1:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
98 raise InvalidCommandSyntax("No colon found in argument description")
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
99 arg_name = line[cmd_indent+6:colon_idx].strip()
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
100 if not arg_name:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
101 raise InvalidCommandSyntax("No name found in argument description")
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
102 arg_help = line[colon_idx+1:].strip()
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
103 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
104 elif cmd_indent:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
105 # 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
106 break
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
107 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
108 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
109
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
110 # type
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
111 colon_idx = stripped.find(":")
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
112 if colon_idx == -1:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
113 raise InvalidCommandSyntax("missing colon")
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
114 type_data = stripped[len(CMD_KEY):colon_idx].strip()
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
115 if len(type_data) == 0:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
116 type_data="(all)"
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
117 elif len(type_data) <= 2 or type_data[0] != '(' or type_data[-1] != ')':
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
118 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
119 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
120 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
121 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
122 data["type"] = type_
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
123
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
124 #args
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
125 data["args"] = stripped[colon_idx+1:].strip()
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
126 except InvalidCommandSyntax as e:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
127 log.warning(u"Invalid command syntax for command {command}: {message}".format(command=cmd_name, message=e.message))
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
128
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
129 return data
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
130
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
131
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
132 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
133 """ 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
134
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
135 @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
136 """
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
137 for attr in dir(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
138 if attr.startswith('cmd_'):
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
139 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
140 if not callable(cmd):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
141 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
142 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
143 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
144 if not cmd_name:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
145 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
146 if cmd_name in 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
147 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
148 while (cmd_name + str(suff)) in self._commands:
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
149 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
150 new_name = cmd_name + str(suff)
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
151 log.warning(_("Conflict for command [%(old_name)s], renaming it to [%(new_name)s]") % {'old_name': cmd_name, 'new_name': new_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
152 cmd_name = new_name
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
153 self._commands[cmd_name] = cmd_data = OrderedDict({'callback':cmd}) # We use an Ordered dict to keep documenation order
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
154 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
155 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
156
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 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
158 """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
159
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 @param callback: a callback which will be called with the following arguments
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 - whois_msg: list of information strings to display, callback need to append its own strings to it
1199
96fb74a4714d misc: fixed some typos
Matteo Cypriani <mcy@lm7.fr>
parents: 1171
diff changeset
162 - 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
163 - profile: %(doc_profile)s
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 @param priority: priority of the information to show (the highest priority will be displayed first)
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 """
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
166 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
167 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
168
922
c897c8d321b3 core: sendMessageTrigger now manage pre and post treatments, which happen before or after XML generation
Goffi <goffi@goffi.org>
parents: 921
diff changeset
169 def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile):
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
170 """ Install SendMessage command hook """
922
c897c8d321b3 core: sendMessageTrigger now manage pre and post treatments, which happen before or after XML generation
Goffi <goffi@goffi.org>
parents: 921
diff changeset
171 pre_xml_treatments.addCallback(self._sendMessageCmdHook, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
172 return True
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
173
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
174 def _sendMessageCmdHook(self, mess_data, profile):
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
175 """ 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
176
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
177 msg starting with / are potential command. If a command is found, it is executed, else and help message is sent
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
178 msg starting with // are escaped: they are sent with a single /
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
179 commands can abord message sending (if they return anything evaluating to False), or continue it (if they return True), eventually after modifying the message
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
180 an "unparsed" key is added to message, containing part of the message not yet parsed
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
181 commands can be deferred or not
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
182 @param mess_data(dict): data comming from sendMessage trigger
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
183 @param profile: %(doc_profile)s
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
184 """
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
185 msg = mess_data["message"]
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
186 try:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
187 if msg[:2] == '//':
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
188 # we have a double '/', it's the escape sequence
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
189 mess_data["message"] = msg[1:]
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
190 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
191 if msg[0] != '/':
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
192 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
193 except IndexError:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
194 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
195
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
196 # we have a command
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
197 d = None
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
198 command = msg[1:].partition(' ')[0].lower()
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
199 if command.isalpha():
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
200 # looks like an actual command, we try to call the corresponding method
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
201 def retHandling(ret):
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
202 """ Handle command return value:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
203 if ret is True, normally send message (possibly modified by command)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
204 else, abord message sending
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
205 """
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
206 if ret:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
207 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
208 else:
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 1025
diff changeset
209 log.debug("text commands took over")
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 1025
diff changeset
210 raise failure.Failure(exceptions.CancelError())
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
211
1082
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
212 def genericErrback(failure):
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
213 self.feedBack("Command failed with condition '%s'" % failure.value.condition, mess_data, profile)
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
214 return False
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
215
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
216 mess_data["unparsed"] = msg[1 + len(command):] # part not yet parsed of the message
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
217 try:
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
218 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
219 except KeyError:
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
220 self.feedBack(_("Unknown command /%s. ") % command + self.HELP_SUGGESTION, mess_data, profile)
1082
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
221 log.debug("text commands took over")
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 1025
diff changeset
222 raise failure.Failure(exceptions.CancelError())
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
223 else:
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
224 if not self._contextValid(mess_data, cmd_data):
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
225 # The command is not launched in the right context, we throw a message with help instructions
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
226 context_txt = _("group discussions") if cmd_data["type"] == "group" else _("one to one discussions")
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
227 feedback = _("/{command} command only applies in {context}.").format(command=command, context=context_txt)
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
228 self.feedBack(u"{} {}".format(feedback, self.HELP_SUGGESTION), mess_data, profile)
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
229 log.debug("text commands took over")
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
230 raise failure.Failure(exceptions.CancelError())
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
231 else:
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
232 d = defer.maybeDeferred(cmd_data["callback"], mess_data, profile)
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
233 d.addErrback(genericErrback)
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
234 d.addCallback(retHandling)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
235
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
236 return d or mess_data # 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
237
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
238 def _contextValid(self, mess_data, cmd_data):
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
239 """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
240
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
241 @param mess_data(dict): message data as given in sendMessage trigger
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
242 @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
243 @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
244 """
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
245 if ((cmd_data['type'] == "group" and mess_data["type"] != "groupchat") or
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
246 (cmd_data['type'] == 'one2one' and mess_data["type"] == "groupchat")):
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
247 return False
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
248 return True
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
249
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
250 def getRoomJID(self, arg, service_jid):
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
251 """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
252
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
253 @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
254 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
255 @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
256 """
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
257 nb_arobas = arg.count('@')
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
258 if nb_arobas == 1:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
259 if arg[-1] != '@':
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
260 return jid.JID(arg)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
261 return jid.JID(arg + service_jid)
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
262 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
263
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
264 def feedBack(self, message, mess_data, profile):
523
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
265 """Give a message back to the user"""
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
266 if mess_data["type"] == 'groupchat':
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
267 _from = mess_data["to"].userhostJID()
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
268 else:
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
269 _from = self.host.getJidNStream(profile)[0]
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
270
1171
0abce7f17782 core: a new "info" type is used in newMessage for system messages (not comming from outside)
Goffi <goffi@goffi.org>
parents: 1082
diff changeset
271 self.host.bridge.newMessage(unicode(mess_data["to"]), message, C.MESS_TYPE_INFO, unicode(_from), {}, profile=profile)
523
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
272
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
273 def feedBackWrongContext(self, command, types, mess_data, profile):
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
274 """Give a generic message to the user when a command has been used in a wrong context.
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
275
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
276 @param command (string): the command name (without the slash)
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
277 @param types (string, list): the message types to which the command applies.
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
278 @param mess_data (dict): original message data
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
279 @param profile: %(doc_profile)s
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
280 """
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
281 if not isinstance(types, str):
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
282 types = _(' or ').join(types)
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
283 feedback = _("/%(command)s command only applies on %(type)s messages. ") % {'command': command, 'type': types}
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
284 self.host.plugins[C.TEXT_CMDS].feedBack(feedback + self.HELP_SUGGESTION, mess_data, profile)
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
285
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
286 def cmd_whois(self, mess_data, profile):
1199
96fb74a4714d misc: fixed some typos
Matteo Cypriani <mcy@lm7.fr>
parents: 1171
diff changeset
287 """show information on entity"""
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
288 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
289
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
290 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
291
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
292 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
293 room = mess_data["to"].userhostJID()
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
294 try:
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
295 if self.host.plugins["XEP-0045"].isNickInRoom(room, entity, profile):
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
296 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
297 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
298 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
299
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
300 if not entity:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
301 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
302 else:
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
303 try:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
304 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
305 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
306 raise jid.InvalidFormat
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
307 except (jid.InvalidFormat, RuntimeError):
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
308 self.feedBack(_("Invalid jid, can't whois"), mess_data, profile)
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
309 return False
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
310
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
311 if not target_jid.resource:
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
312 target_jid.resource = self.host.memory.getMainResource(target_jid, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
313
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
314 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
315
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
316 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
317 for ignore, callback in self._whois:
928
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
318 d.addCallback(lambda ignore: callback(whois_msg, mess_data, target_jid, profile))
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
319
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
320 def feedBack(ignore):
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
321 self.feedBack(u"\n".join(whois_msg), mess_data, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
322 return False
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
323
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
324 d.addCallback(feedBack)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
325 return d
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
326
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
327 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
328 """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
329
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
330 @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
331 @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
332 """
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
333 strings = []
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
334 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
335 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
336 arg_name = doc_name[8:]
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
337 strings.append(u"- {name}: {doc_help}".format(name=arg_name, doc_help=_(doc_help)))
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
338
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
339 return strings
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
340
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
341 def cmd_help(self, mess_data, profile):
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
342 """show help on available commands
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
343
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
344 @command: [cmd_name]
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
345 - 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
346 """
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
347 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
348 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
349 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
350 if cmd_name and cmd_name not in self._commands:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
351 self.feedBack(_(u"Invalid command name [{}]\n".format(cmd_name)), mess_data, profile)
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
352 cmd_name = ""
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
353 if not cmd_name:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
354 # 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
355 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
356 help_cmds = []
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
357
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
358 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
359 cmd_data = self._commands[command]
1370
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
360 if not self._contextValid(mess_data, cmd_data):
53c7678c27a6 plugin text commands: added _contextValid method:
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
361 continue
1369
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
362 spaces = (longuest - len(command)) * ' '
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
363 help_cmds.append(" /{command}: {spaces} {short_help}".format(
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
364 command=command,
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
365 spaces=spaces,
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
366 short_help=cmd_data["doc_short_help"]
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
367 ))
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
368
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
369 help_mess = _(u"Text commands available:\n%s") % (u'\n'.join(help_cmds), )
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
370 else:
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
371 # 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
372 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
373 syntax = cmd_data["args"]
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
374 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
375 name=cmd_name,
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
376 short_help=cmd_data['doc_short_help'],
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
377 syntax=_(" "*4+"syntax: {}\n").format(syntax) if syntax else "",
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
378 args_help=u'\n'.join([u" "*8+"{}".format(line) for line in self._getArgsHelp(cmd_data)]))
dd1a148bd3d8 plugin text commands: docstring parsing for commands, and better /help command:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
379
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
380 self.feedBack(help_mess, mess_data, profile)
1025
24fe24cfb363 plugin text commands: added /me command to avoid the need to escape the /
Goffi <goffi@goffi.org>
parents: 1002
diff changeset
381
24fe24cfb363 plugin text commands: added /me command to avoid the need to escape the /
Goffi <goffi@goffi.org>
parents: 1002
diff changeset
382 def cmd_me(self, mess_data, profile):
24fe24cfb363 plugin text commands: added /me command to avoid the need to escape the /
Goffi <goffi@goffi.org>
parents: 1002
diff changeset
383 """Display a message at third person"""
24fe24cfb363 plugin text commands: added /me command to avoid the need to escape the /
Goffi <goffi@goffi.org>
parents: 1002
diff changeset
384 # We just catch the method and continue it as the frontends should manage /me display
24fe24cfb363 plugin text commands: added /me command to avoid the need to escape the /
Goffi <goffi@goffi.org>
parents: 1002
diff changeset
385 return True