annotate src/plugins/plugin_misc_text_commands.py @ 993:301b342c697a

core: use of the new core.log module: /!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author Goffi <goffi@goffi.org>
date Sat, 19 Apr 2014 19:19:19 +0200
parents 059b56cbd247
children 291eb8216f6e
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
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
26 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
27 log = getLogger(__name__)
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
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
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
46 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
47 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
48 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
49 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
50 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
51 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
52 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
53
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 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
55 """ 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
56 @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
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 """
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 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
60 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
61 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
62 if not callable(cmd):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
63 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
64 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
65 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
66 if not cmd_name:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
67 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
68 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
69 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
70 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
71 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
72 new_name = cmd_name + str(suff)
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
73 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
74 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
75 self._commands[cmd_name] = cmd
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
76 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
77
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 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
79 """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
80 @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
81 - 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
82 - 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
83 - 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
84 @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
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 """
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 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
88 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
89
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
90 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
91 """ 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
92 pre_xml_treatments.addCallback(self._sendMessageCmdHook, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
93 return True
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
94
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
95 def _sendMessageCmdHook(self, mess_data, profile):
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
96 """ Check text commands in message, and react consequently
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
97 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
98 msg starting with // are escaped: they are sent with a single /
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
99 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
100 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
101 commands can be deferred or not
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
102
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
103 """
506
2e43c74815ad plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff changeset
104 msg = mess_data["message"]
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
105 try:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
106 if msg[:2] == '//':
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
107 # we have a double '/', it's the escape sequence
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
108 mess_data["message"] = msg[1:]
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
109 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
110 if msg[0] != '/':
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 except IndexError:
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
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
115 # we have a command
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
116 d = None
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
117 command = msg[1:].partition(' ')[0].lower()
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
118 if command.isalpha():
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
119 # 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
120 def retHandling(ret):
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
121 """ Handle command return value:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
122 if ret is True, normally send message (possibly modified by command)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
123 else, abord message sending
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 """
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
126 if ret:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
127 return mess_data
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
128 else:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
129 return Failure(MessageSentAndStored("text commands took over", mess_data))
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
130
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
131 try:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
132 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
133 d = defer.maybeDeferred(self._commands[command], mess_data, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
134 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
135 except KeyError:
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
136 pass
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
137
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
138 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
139
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 def getRoomJID(self, arg, service_jid):
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
141 """Return a room jid with a shortcut
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
142 @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
143 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
144 @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
145 """
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
146 nb_arobas = arg.count('@')
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
147 if nb_arobas == 1:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
148 if arg[-1] != '@':
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
149 return jid.JID(arg)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
150 return jid.JID(arg + service_jid)
508
7c6609dddb2c plugin text commands: /leave management:
Goffi <goffi@goffi.org>
parents: 506
diff changeset
151 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
152
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
153 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
154 """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
155 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
156 _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
157 else:
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
158 _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
159
24c0d51449e7 plugin text commands: added _feedback method to send an answer to user
Goffi <goffi@goffi.org>
parents: 519
diff changeset
160 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
161
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
162 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
163 """show informations on entity"""
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
164 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
165
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
166 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
167
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
168 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
169 room = mess_data["to"].userhostJID()
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
170 try:
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 927
diff changeset
171 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
172 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
173 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 929
diff changeset
174 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
175
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
176 if not entity:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
177 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
178 else:
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
179 try:
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
180 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
181 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
182 raise jid.InvalidFormat
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
183 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
184 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
185 return False
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
186
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
187 if not target_jid.resource:
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
188 target_jid.resource = self.host.memory.getLastResource(target_jid, profile)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
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 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
191
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
192 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
193 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
194 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
195
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
196 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
197 self.feedBack(u"\n".join(whois_msg), mess_data, profile)
921
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
198 return False
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
199
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
200 d.addCallback(feedBack)
8dd168c7741c plugin text commands: refactoring:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
201 return d
600
c5451501465b plugin text commands: basic /whois command (just show jid so far)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
202
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
203 def cmd_help(self, mess_data, profile):
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
204 """show help on available commands"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
205 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
206 longuest = max([len(command) for command in commands])
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
207 help_cmds = []
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
208
929
059b56cbd247 plugin text commands: commands are now sorted in /help
Goffi <goffi@goffi.org>
parents: 928
diff changeset
209 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
210 method = self._commands[command]
517
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
211 try:
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
212 help_str = method.__doc__.split('\n')[0]
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
213 except AttributeError:
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
214 help_str = ''
59b32c04e105 plugin text commands: added /help command
Goffi <goffi@goffi.org>
parents: 515
diff changeset
215 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
216 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
217
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
218 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
219 self.feedBack(help_mess, mess_data, profile)