view src/plugins/plugin_misc_text_commands.py @ 508:7c6609dddb2c

plugin text commands: /leave management: - _getRoomJid manage room jid argument: the argument can be full jid or a shortcut. in case of shortcut, room or room@ can be used for shortcut room@service.domain.tld where service.domain.tld is the same as the one of the room where the command is entered - /leave alone leave the current room, /leave [room] leave the given room - /part is a synonym of /leave
author Goffi <goffi@goffi.org>
date Fri, 28 Sep 2012 00:31:10 +0200
parents 2e43c74815ad
children 64ff046dc201
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
SàT plugin for managing text commands
Copyright (C) 2009, 2010, 2011, 2012  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 twisted.words.protocols.jabber import jid
from logging import debug, info, warning, error

PLUGIN_INFO = {
"name": "Text commands",
"import_name": "text-commands",
"type": "Misc",
"protocols": [],
"dependencies": ["XEP-0045"],
"main": "TextCommands",
"handler": "no",
"description": _("""IRC like text commands""")
}

class TextCommands():

    def __init__(self, host):
        info(_("Text commands initialization"))
        self.host = host
        host.trigger.add("sendMessage", self.sendMessageTrigger)

    def sendMessageTrigger(self, mess_data, profile):
        """ Check text commands in message, and react consequently """
        msg = mess_data["message"]
        if msg:
            if msg[0] == '/':
                command = msg[1:].partition(' ')[0].lower()
                if command.isalpha():
                    # looks like an actual command, we try to call the corresponding method
                    try:
                        mess_data["unparsed"] = msg[1+len(command):] #part not yet parsed of the message
                        return getattr(self,"cmd_%s" % command)(mess_data,profile)
                    except AttributeError:
                        pass
            elif msg[0] == '\\': #we have escape char
                try:
                    if msg[1] in ('/','\\'): # we have '\/' or '\\', we escape to '/' or '\'
                        mess_data["message"] = msg[1:]
                except IndexError:
                    pass
        return True

    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 cmd_nick(self, mess_data, profile):
        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_leave(self, mess_data, profile):
        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)