annotate src/plugins/plugin_misc_text_commands.py @ 1108:6bdcaeafca11

core (constants): fixed bad XMLUI level constants
author Goffi <goffi@goffi.org>
date Wed, 20 Aug 2014 21:15:04 +0200
parents 246712d2e7bc
children 0abce7f17782
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
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
28
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
29 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
30 "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
31 "import_name": C.TEXT_CMDS,
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
32 "type": "Misc",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
33 "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
34 "dependencies": [],
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
35 "main": "TextCommands",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
36 "handler": "no",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
37 "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
38 }
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
39
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
40
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
41 class TextCommands(object):
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
42 #FIXME: doc strings for commands have to be translatable
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
43 # plugins need a dynamic translation system (translation
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
44 # 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
45
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
46 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
47
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
49 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
50 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
51 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
52 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
53 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
54 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
55
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
56 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
57 """ Add a text command
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
58 @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
59
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
60 """
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
61 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
62 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
63 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
64 if not callable(cmd):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
65 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
66 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
67 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
68 if not cmd_name:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
69 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
70 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
71 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
72 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
73 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
74 new_name = cmd_name + str(suff)
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
75 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
76 cmd_name = new_name
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
77 self._commands[cmd_name] = cmd
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
78 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
79
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
80 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
81 """Add a callback which give information to the /whois command
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
82 @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
83 - whois_msg: list of information strings to display, callback need to append its own strings to it
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
84 - target_jid: full jid from who we want informations
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
85 - 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
86 @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
87
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
88 """
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
89 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
90 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
91
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
92 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
93 """ 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
94 pre_xml_treatments.addCallback(self._sendMessageCmdHook, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
95 return True
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
96
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
97 def _sendMessageCmdHook(self, mess_data, profile):
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
98 """ Check text commands in message, and react consequently
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
99 msg starting with / are potential command. If a command is found, it is executed, else message is sent normally
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
100 msg starting with // are escaped: they are sent with a single /
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
101 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
102 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
103 commands can be deferred or not
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
104
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
105 """
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
106 msg = mess_data["message"]
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
107 try:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
108 if msg[:2] == '//':
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
109 # we have a double '/', it's the escape sequence
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
110 mess_data["message"] = msg[1:]
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
111 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
112 if msg[0] != '/':
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
113 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
114 except IndexError:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
115 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
116
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
117 # we have a command
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
118 d = None
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
119 command = msg[1:].partition(' ')[0].lower()
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
120 if command.isalpha():
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
121 # 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
122 def retHandling(ret):
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
123 """ Handle command return value:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
124 if ret is True, normally send message (possibly modified by command)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
125 else, abord message sending
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
126
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
127 """
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
128 if ret:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
129 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
130 else:
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 1025
diff changeset
131 log.debug("text commands took over")
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 1025
diff changeset
132 raise failure.Failure(exceptions.CancelError())
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
133
1082
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
134 def genericErrback(failure):
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
135 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
136 return False
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
137
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
138 try:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
139 mess_data["unparsed"] = msg[1 + len(command):] # part not yet parsed of the message
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
140 d = defer.maybeDeferred(self._commands[command], mess_data, profile)
1082
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
141 d.addCallbacks(lambda ret: ret, genericErrback) # XXX: dummy callback is needed
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
142 d.addCallback(retHandling)
1082
246712d2e7bc plugin XEP-0045, text_commands: add some commands:
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
143
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
144 except KeyError:
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
145 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
146 log.debug("text commands took over")
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 1025
diff changeset
147 raise failure.Failure(exceptions.CancelError())
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
148
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
149 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
150
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
151 def getRoomJID(self, arg, service_jid):
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
152 """Return a room jid with a shortcut
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
153 @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
154 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
155 @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
156 """
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
157 nb_arobas = arg.count('@')
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
158 if nb_arobas == 1:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
159 if arg[-1] != '@':
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
160 return jid.JID(arg)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
161 return jid.JID(arg + service_jid)
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
162 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
163
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
164 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
165 """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
166 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
167 _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
168 else:
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
169 _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
170
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
171 self.host.bridge.newMessage(unicode(mess_data["to"]), message, mess_data['type'], unicode(_from), {}, profile=profile)
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
172
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
173 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
174 """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
175
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
176 @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
177 @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
178 @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
179 @param profile: %(doc_profile)s
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
180 """
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
181 if not isinstance(types, str):
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
182 types = _(' or ').join(types)
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
183 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
184 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
185
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
186 def cmd_whois(self, mess_data, profile):
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
187 """show informations on entity"""
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
188 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
189
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
190 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
191
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
192 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
193 room = mess_data["to"].userhostJID()
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
194 try:
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
195 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
196 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
197 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
198 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
199
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
200 if not entity:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
201 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
202 else:
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
203 try:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
204 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
205 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
206 raise jid.InvalidFormat
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
207 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
208 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
209 return False
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
210
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
211 if not target_jid.resource:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
212 target_jid.resource = self.host.memory.getLastResource(target_jid, profile)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
213
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
214 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
215
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
216 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
217 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
218 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
219
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
220 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
221 self.feedBack(u"\n".join(whois_msg), mess_data, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
222 return False
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
223
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
224 d.addCallback(feedBack)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
225 return d
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
226
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
227 def cmd_help(self, mess_data, profile):
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
228 """show help on available commands"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
229 commands = filter(lambda method: method.startswith('cmd_'), dir(self))
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
230 longuest = max([len(command) for command in commands])
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
231 help_cmds = []
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
232
929
059b56cbd247 plugin text commands: commands are now sorted in /help
Goffi <goffi@goffi.org>
parents: 928
diff changeset
233 for command in sorted(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
234 method = self._commands[command]
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
235 try:
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
236 help_str = method.__doc__.split('\n')[0]
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
237 except AttributeError:
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
238 help_str = ''
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
239 spaces = (longuest - len(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
240 help_cmds.append(" /%s: %s %s" % (command, spaces, help_str))
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
241
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
242 help_mess = _(u"Text commands available:\n%s") % (u'\n'.join(help_cmds), )
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
243 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
244
24fe24cfb363 plugin text commands: added /me command to avoid the need to escape the /
Goffi <goffi@goffi.org>
parents: 1002
diff changeset
245 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
246 """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
247 # 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
248 return True