changeset 506:2e43c74815ad

plugin text commands: Text commands is a new plugin that bring IRC-like commands - Text commands catch message sent, and check commands starting by / - /nick command is managed
author Goffi <goffi@goffi.org>
date Thu, 27 Sep 2012 00:54:42 +0200
parents 2402668b5d05
children f98bef71a918
files src/core/sat_main.py src/plugins/plugin_misc_text_commands.py
diffstat 2 files changed, 87 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/sat_main.py	Thu Sep 27 00:51:43 2012 +0200
+++ b/src/core/sat_main.py	Thu Sep 27 00:54:42 2012 +0200
@@ -465,11 +465,9 @@
                       "subject": subject,
                       "type": mess_type
                     }
-        if not self.trigger.point("sendMessage", mess_data, profile):
-            return
+        
         if mess_data["type"] == "auto":
-            #XXX: if the type has not been changed by a trigger method,
-            #     we try to guess it
+            # we try to guess the type 
             if mess_data["subject"]:
                 mess_data["type"] = 'normal'
             elif not mess_data["to"].resource: #if to JID has a resource, the type is not 'groupchat'
@@ -487,6 +485,10 @@
             else:
                 mess_data["type"] == 'chat'
             mess_data["type"] == "chat" if mess_data["subject"] else "normal"
+        
+        if not self.trigger.point("sendMessage", mess_data, profile):
+            return
+        
         debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to})
         message = domish.Element(('jabber:client','message'))
         message["to"] = mess_data["to"].full()
@@ -494,12 +496,12 @@
         message["type"] = mess_data["type"]
         if mess_data["subject"]:
             message.addElement("subject", "jabber:client", subject)
-        message.addElement("body", "jabber:client", msg)
+        message.addElement("body", "jabber:client", mess_data["message"])
         client.xmlstream.send(message)
         if mess_data["type"]!="groupchat":
-            self.memory.addToHistory(current_jid, jid.JID(to), unicode(msg), profile=profile) #we don't add groupchat message to history, as we get them back
+            self.memory.addToHistory(current_jid, jid.JID(to), unicode(mess_data["message"]), profile=profile) #we don't add groupchat message to history, as we get them back
                                                                                               #and they will be added then
-            self.bridge.newMessage(message['from'], unicode(msg), mess_type=mess_data["type"], to_jid=message['to'], profile=profile) #We send back the message, so all clients are aware of it
+            self.bridge.newMessage(message['from'], unicode(mess_data["message"]), mess_type=mess_data["type"], to_jid=message['to'], profile=profile) #We send back the message, so all clients are aware of it
 
 
     def setPresence(self, to="", show="", priority = 0, statuses={}, profile_key='@DEFAULT@'):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/plugins/plugin_misc_text_commands.py	Thu Sep 27 00:54:42 2012 +0200
@@ -0,0 +1,78 @@
+#!/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 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 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