comparison src/plugins/plugin_exp_parrot.py @ 926:d609581bf74a

plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve: - registerTextCommands can be called by a plugin in its __init__ method, it looks for methods starting with "cmd_" and register them as new commands - addWhoIsCb: add a callback to /whois command, the callback can complete whois informations - plugins parrot, XEP-0045 and XEP-0092 now manage their own commands
author Goffi <goffi@goffi.org>
date Mon, 24 Mar 2014 10:57:15 +0100
parents 1fe00f0c9a91
children cd150dd947e3
comparison
equal deleted inserted replaced
925:5c78cefd233f 926:d609581bf74a
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C
21 from logging import debug, info, warning, error 22 from logging import debug, info, warning, error
22 from twisted.words.protocols.jabber import jid 23 from twisted.words.protocols.jabber import jid
23 24
24 from sat.core.exceptions import UnknownEntityError 25 from sat.core.exceptions import UnknownEntityError
25 #from sat.tools.misc import SkipOtherTriggers 26 #from sat.tools.misc import SkipOtherTriggers
28 "name": "Parrot Plugin", 29 "name": "Parrot Plugin",
29 "import_name": "EXP-PARROT", 30 "import_name": "EXP-PARROT",
30 "type": "EXP", 31 "type": "EXP",
31 "protocols": [], 32 "protocols": [],
32 "dependencies": ["XEP-0045"], 33 "dependencies": ["XEP-0045"],
34 "recommendations": [C.TEXT_CMDS],
33 "main": "Exp_Parrot", 35 "main": "Exp_Parrot",
34 "handler": "no", 36 "handler": "no",
35 "description": _("""Implementation of parrot mode (repeat messages between 2 entities)""") 37 "description": _("""Implementation of parrot mode (repeat messages between 2 entities)""")
36 } 38 }
37 39
46 def __init__(self, host): 48 def __init__(self, host):
47 info(_("Plugin Parrot initialization")) 49 info(_("Plugin Parrot initialization"))
48 self.host = host 50 self.host = host
49 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100) 51 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100)
50 #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) 52 #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100)
53 try:
54 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
55 except KeyError:
56 info(_("Text commands not available"))
51 57
52 #def sendMessageTrigger(self, mess_data, treatments, profile): 58 #def sendMessageTrigger(self, mess_data, treatments, profile):
53 # """ Deactivate other triggers if recipient is in parrot links """ 59 # """ Deactivate other triggers if recipient is in parrot links """
54 # client = self.host.getClient(profile) 60 # client = self.host.getClient(profile)
55 # try: 61 # try:
120 client = self.host.getClient(profile) 126 client = self.host.getClient(profile)
121 try: 127 try:
122 del client.parrot_links[source_jid.userhostJID()] 128 del client.parrot_links[source_jid.userhostJID()]
123 except (AttributeError, KeyError): 129 except (AttributeError, KeyError):
124 pass 130 pass
131
132 def cmd_parrot(self, mess_data, profile):
133 """activate Parrot mode between 2 entities, in both directions."""
134 #TODO: these commands must not be hardcoded, an interface should be made
135 # to allow plugins to register simple commands like this.
136
137 debug("Catched parrot command")
138 txt_cmd = self.host.plugins[C.TEXT_CMDS]
139
140 try:
141 link_left_jid = jid.JID(mess_data["unparsed"].strip())
142 if not link_left_jid.user or not link_left_jid.host:
143 raise jid.InvalidFormat
144 except jid.InvalidFormat:
145 txt_cmd.feedBack("Can't activate Parrot mode for invalid jid", mess_data, profile)
146 return False
147
148 link_right_jid = mess_data['to']
149
150 self.addParrot(link_left_jid, link_right_jid, profile)
151 self.addParrot(link_right_jid, link_left_jid, profile)
152
153 txt_cmd.feedBack("Parrot mode activated for %s" % (unicode(link_left_jid), ), mess_data, profile)
154
155 return False
156
157 def cmd_unparrot(self, mess_data, profile):
158 """remove Parrot mode between 2 entities, in both directions."""
159 debug("Catched unparrot command")
160 txt_cmd = self.host.plugins[C.TEXT_CMDS]
161
162 try:
163 link_left_jid = jid.JID(mess_data["unparsed"].strip())
164 if not link_left_jid.user or not link_left_jid.host:
165 raise jid.InvalidFormat
166 except jid.InvalidFormat:
167 txt_cmd.feedBack("Can't deactivate Parrot mode for invalid jid", mess_data, profile)
168 return False
169
170 link_right_jid = mess_data['to']
171
172 self.removeParrot(link_left_jid, profile)
173 self.removeParrot(link_right_jid, profile)
174
175 txt_cmd.feedBack("Parrot mode deactivated for %s and %s" % (unicode(link_left_jid), unicode(link_right_jid)), mess_data, profile)
176
177 return False