Mercurial > libervia-backend
view src/plugins/plugin_misc_text_commands.py @ 921:8dd168c7741c
plugin text commands: refactoring:
- commands can now return a deferred
- if commands return True, message is sent, else it is cancelled
- the escape sequence is now '//' instead of '\/'
- added software version to /whois command
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 22 Mar 2014 13:47:33 +0100 |
parents | 1fe00f0c9a91 |
children | c897c8d321b3 |
line wrap: on
line source
#!/usr/bin/python # -*- coding: utf-8 -*- # SàT plugin for managing text commands # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from sat.core.i18n import _ from sat.core.sat_main import MessageSentAndStored from twisted.words.protocols.jabber import jid from twisted.internet import defer from twisted.python.failure import Failure from logging import debug, info, warning, error PLUGIN_INFO = { "name": "Text commands", "import_name": "TEXT-COMMANDS", "type": "Misc", "protocols": [], "dependencies": ["XEP-0045", "EXP-PARROT", "XEP-0092"], "main": "TextCommands", "handler": "no", "description": _("""IRC like text commands""") } class TextCommands(object): #FIXME: doc strings for commands have to be translatable # plugins need a dynamic translation system (translation # should be downloadable independently) def __init__(self, host): info(_("Text commands initialization")) self.host = host host.trigger.add("sendMessage", self.sendMessageTrigger) def sendMessageTrigger(self, mess_data, treatments, profile): """ Install SendMessage command hook """ treatments.addCallback(self._sendMessageCmdHook, profile) return True def _sendMessageCmdHook(self, mess_data, profile): """ Check text commands in message, and react consequently msg starting with / are potential command. If a command is found, it is executed, else message is sent normally msg starting with // are escaped: they are sent with a single / commands can abord message sending (if they return anything evaluating to False), or continue it (if they return True), eventually after modifying the message an "unparsed" key is added to message, containing part of the message not yet parsed commands can be deferred or not """ msg = mess_data["message"] try: if msg[:2] == '//': # we have a double '/', it's the escape sequence mess_data["message"] = msg[1:] return mess_data if msg[0] != '/': return mess_data except IndexError: return mess_data # we have a command d = None command = msg[1:].partition(' ')[0].lower() if command.isalpha(): # looks like an actual command, we try to call the corresponding method def retHandling(ret): """ Handle command return value: if ret is True, normally send message (possibly modified by command) else, abord message sending """ if ret: return mess_data else: return Failure(MessageSentAndStored("text commands took over", mess_data)) try: mess_data["unparsed"] = msg[1 + len(command):] # part not yet parsed of the message d = defer.maybeDeferred(getattr(self, "cmd_%s" % command), mess_data, profile) d.addCallback(retHandling) except AttributeError: pass return d or mess_data # if a command is detected, we should have a deferred, else be send the message normally def _getRoomJID(self, arg, service_jid): """Return a room jid with a shortcut @param arg: argument: can be a full room jid (e.g.: sat@chat.jabberfr.org) or a shortcut (e.g.: sat or sat@ for sat on current service) @param service_jid: jid of the current service (e.g.: chat.jabberfr.org) """ nb_arobas = arg.count('@') if nb_arobas == 1: if arg[-1] != '@': return jid.JID(arg) return jid.JID(arg + service_jid) return jid.JID(u"%s@%s" % (arg, service_jid)) def _feedBack(self, message, mess_data, profile): """Give a message back to the user""" if mess_data["type"] == 'groupchat': _from = mess_data["to"].userhostJID() else: _from = self.host.getJidNStream(profile)[0] self.host.bridge.newMessage(unicode(mess_data["to"]), message, mess_data['type'], unicode(_from), {}, profile=profile) def cmd_nick(self, mess_data, profile): """change nickname""" debug("Catched nick command") if mess_data['type'] != "groupchat": #/nick command does nothing if we are not on a group chat info("Ignoring /nick command on a non groupchat message") return True nick = mess_data["unparsed"].strip() room = mess_data["to"] self.host.plugins["XEP-0045"].nick(room, nick, profile) return False def cmd_join(self, mess_data, profile): """join a new room (on the same service if full jid is not specified)""" debug("Catched join command") if mess_data['type'] != "groupchat": #/leave command does nothing if we are not on a group chat info("Ignoring /join command on a non groupchat message") return True if mess_data["unparsed"].strip(): room = self._getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host) nick = (self.host.plugins["XEP-0045"].getRoomNick(mess_data["to"].userhost(), profile) or self.host.getClient(profile).jid.user) self.host.plugins["XEP-0045"].join(room, nick, {}, profile) return False def cmd_leave(self, mess_data, profile): """quit a room""" debug("Catched leave command") if mess_data['type'] != "groupchat": #/leave command does nothing if we are not on a group chat info("Ignoring /leave command on a non groupchat message") return True if mess_data["unparsed"].strip(): room = self._getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host) else: room = mess_data["to"] self.host.plugins["XEP-0045"].leave(room, profile) return False def cmd_part(self, mess_data, profile): """just a synonym of /leave""" return self.cmd_leave(mess_data, profile) def cmd_title(self, mess_data, profile): """change room's subject""" debug("Catched title command") if mess_data['type'] != "groupchat": #/leave command does nothing if we are not on a group chat info("Ignoring /title command on a non groupchat message") return True subject = mess_data["unparsed"].strip() if subject: room = mess_data["to"] self.host.plugins["XEP-0045"].subject(room, subject, profile) return False def cmd_topic(self, mess_data, profile): """just a synonym of /title""" return self.cmd_title(mess_data, profile) def cmd_parrot(self, mess_data, profile): """activate Parrot mode between 2 entities, in both directions.""" #TODO: these commands must not be hardcoded, an interface should be made # to allow plugins to register simple commands like this. debug("Catched parrot command") try: link_left_jid = jid.JID(mess_data["unparsed"].strip()) if not link_left_jid.user or not link_left_jid.host: raise jid.InvalidFormat except jid.InvalidFormat: self._feedBack("Can't activate Parrot mode for invalid jid", mess_data, profile) return False link_right_jid = mess_data['to'] self.host.plugins["EXP-PARROT"].addParrot(link_left_jid, link_right_jid, profile) self.host.plugins["EXP-PARROT"].addParrot(link_right_jid, link_left_jid, profile) self._feedBack("Parrot mode activated for %s" % (unicode(link_left_jid), ), mess_data, profile) return False def cmd_unparrot(self, mess_data, profile): """remove Parrot mode between 2 entities, in both directions.""" debug("Catched unparrot command") try: link_left_jid = jid.JID(mess_data["unparsed"].strip()) if not link_left_jid.user or not link_left_jid.host: raise jid.InvalidFormat except jid.InvalidFormat: self._feedBack("Can't deactivate Parrot mode for invalid jid", mess_data, profile) return False link_right_jid = mess_data['to'] self.host.plugins["EXP-PARROT"].removeParrot(link_left_jid, profile) self.host.plugins["EXP-PARROT"].removeParrot(link_right_jid, profile) self._feedBack("Parrot mode deactivated for %s and %s" % (unicode(link_left_jid), unicode(link_right_jid)), mess_data, profile) return False def cmd_whois(self, mess_data, profile): """show informations on entity""" debug("Catched whois command") entity = mess_data["unparsed"].strip() if mess_data['type'] == "groupchat": room = mess_data["to"] if self.host.plugins["XEP-0045"].isNickInRoom(room, entity, profile): entity = u"%s/%s" % (room, entity) if not entity: target_jid = mess_data["to"] else: try: target_jid = jid.JID(entity) if not target_jid.user or not target_jid.host: raise jid.InvalidFormat except (jid.InvalidFormat, RuntimeError): self._feedBack(_("Invalid jid, can't whois"), mess_data, profile) return False if not target_jid.resource: target_jid.resource = self.host.memory.getLastResource(target_jid, profile) whois_msg = [_(u"whois for %(jid)s") % {'jid': target_jid}] # version def versionCb(version_data): name, version, os = version_data if name: whois_msg.append(_("Client name: %s") % name) if version: whois_msg.append(_("Client version: %s") % version) if os: whois_msg.append(_("Operating system: %s") % os) d = self.host.plugins['XEP-0092'].getVersion(target_jid, profile) d.addCallback(versionCb) #TODO: add informations here (vcard, etc) def feedBack(ignore): self._feedBack(u"\n".join(whois_msg), mess_data, profile) return False d.addCallback(feedBack) return d def cmd_help(self, mess_data, profile): """show help on available commands""" commands = filter(lambda method: method.startswith('cmd_'), dir(self)) longuest = max([len(command) for command in commands]) help_cmds = [] for command in commands: method = getattr(self, command) try: help_str = method.__doc__.split('\n')[0] except AttributeError: help_str = '' spaces = (longuest - len(command)) * ' ' help_cmds.append(" /%s: %s %s" % (command[4:], spaces, help_str)) help_mess = _(u"Text commands available:\n%s") % (u'\n'.join(help_cmds), ) self._feedBack(help_mess, mess_data, profile)