annotate src/plugins/plugin_misc_text_commands.py @ 945:899dc9cd0f35

plugin XEP-0092: feature check before requesting version
author Goffi <goffi@goffi.org>
date Fri, 28 Mar 2014 18:19:29 +0100
parents 059b56cbd247
children 301b342c697a
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
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
22 from sat.core.sat_main import MessageSentAndStored
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
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
25 from twisted.python.failure 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
26 from logging import debug, info, warning, error
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
27
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
28 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
29 "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
30 "import_name": C.TEXT_CMDS,
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
31 "type": "Misc",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
32 "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
33 "dependencies": [],
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
34 "main": "TextCommands",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
35 "handler": "no",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
36 "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
37 }
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
38
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
39
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
40 class TextCommands(object):
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
41 #FIXME: doc strings for commands have to be translatable
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
42 # plugins need a dynamic translation system (translation
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
43 # 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
44
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
45 def __init__(self, host):
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
46 info(_("Text commands initialization"))
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
47 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
48 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
49 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
50 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
51 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
52
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 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
54 """ 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
55 @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
56
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 """
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 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
59 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
60 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
61 if not callable(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
62 warning(_("Skipping not callable [%s] attribute") % 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
63 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
64 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
65 if not cmd_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
66 warning(_("Skipping cmd_ method"))
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 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
68 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
69 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
70 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
71 new_name = cmd_name + str(suff)
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
72 warning(_("Conflict for command [%(old_name)s], renaming it to [%(new_name)s]") % {'old_name': cmd_name, 'new_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
73 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
74 self._commands[cmd_name] = 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
75 info(_("Registered text command [%s]") % cmd_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
76
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 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
78 """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
79 @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
80 - 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
81 - 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
82 - 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
83 @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
84
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 """
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 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
87 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
88
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
89 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
90 """ 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
91 pre_xml_treatments.addCallback(self._sendMessageCmdHook, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
92 return True
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
93
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
94 def _sendMessageCmdHook(self, mess_data, profile):
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
95 """ Check text commands in message, and react consequently
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
96 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
97 msg starting with // are escaped: they are sent with a single /
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
98 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
99 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
100 commands can be deferred or not
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
101
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
102 """
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
103 msg = mess_data["message"]
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
104 try:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
105 if msg[:2] == '//':
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
106 # we have a double '/', it's the escape sequence
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
107 mess_data["message"] = msg[1:]
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
108 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
109 if msg[0] != '/':
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
110 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
111 except IndexError:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
112 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
113
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
114 # we have a command
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
115 d = None
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
116 command = msg[1:].partition(' ')[0].lower()
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
117 if command.isalpha():
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
118 # 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
119 def retHandling(ret):
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
120 """ Handle command return value:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
121 if ret is True, normally send message (possibly modified by command)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
122 else, abord message sending
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
123
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
124 """
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
125 if ret:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
126 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
127 else:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
128 return Failure(MessageSentAndStored("text commands took over", mess_data))
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
129
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
130 try:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
131 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
132 d = defer.maybeDeferred(self._commands[command], mess_data, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
133 d.addCallback(retHandling)
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
134 except KeyError:
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
135 pass
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
136
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
137 return d or mess_data # if a command is detected, we should have a deferred, else be 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
138
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
139 def getRoomJID(self, arg, service_jid):
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
140 """Return a room jid with a shortcut
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
141 @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
142 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
143 @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
144 """
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
145 nb_arobas = arg.count('@')
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
146 if nb_arobas == 1:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
147 if arg[-1] != '@':
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
148 return jid.JID(arg)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
149 return jid.JID(arg + service_jid)
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
150 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
151
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 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
153 """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
154 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
155 _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
156 else:
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
157 _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
158
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
159 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
160
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
161 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
162 """show informations on entity"""
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
163 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
164
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
165 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
166
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
167 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
168 room = mess_data["to"].userhostJID()
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
169 try:
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
170 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
171 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
172 except KeyError:
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
173 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
174
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
175 if not entity:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
176 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
177 else:
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
178 try:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
179 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
180 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
181 raise jid.InvalidFormat
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
182 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
183 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
184 return False
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
185
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
186 if not target_jid.resource:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
187 target_jid.resource = self.host.memory.getLastResource(target_jid, profile)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
188
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
189 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
190
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
191 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
192 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
193 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
194
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
195 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
196 self.feedBack(u"\n".join(whois_msg), mess_data, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
197 return False
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
198
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
199 d.addCallback(feedBack)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
200 return d
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
201
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
202 def cmd_help(self, mess_data, profile):
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
203 """show help on available commands"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
204 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
205 longuest = max([len(command) for command in commands])
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
206 help_cmds = []
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
207
929
059b56cbd247 plugin text commands: commands are now sorted in /help
Goffi <goffi@goffi.org>
parents: 928
diff changeset
208 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
209 method = self._commands[command]
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
210 try:
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
211 help_str = method.__doc__.split('\n')[0]
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
212 except AttributeError:
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
213 help_str = ''
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
214 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
215 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
216
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
217 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
218 self.feedBack(help_mess, mess_data, profile)