diff src/plugins/plugin_xep_0245.py @ 1964:a86e41d9245d

plugin XEP-0245: implementation of XEP-0245 /me syntax
author Goffi <goffi@goffi.org>
date Mon, 20 Jun 2016 18:42:00 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/plugins/plugin_xep_0245.py	Mon Jun 20 18:42:00 2016 +0200
@@ -0,0 +1,101 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+
+# SAT plugin for managing xep-245
+# Copyright (C) 2009-2016 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.constants import Const as C
+from sat.core.log import getLogger
+log = getLogger(__name__)
+
+
+PLUGIN_INFO = {
+    "name": "XEP-0245 Plugin",
+    "import_name": "XEP-0245",
+    "type": "XEP",
+    "protocols": ["XEP-0245"],
+    "recommendations": [C.TEXT_CMDS],
+    "main": "XEP_0245",
+    "handler": "no",
+    "description": _("""/me syntax handling""")
+}
+
+
+class XEP_0245(object):
+
+    def __init__(self, host):
+        log.info(_("Plugin XEP_245 initialization"))
+        self.host = host
+        try:
+            self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
+        except KeyError:
+            pass
+        host.trigger.add("messageSend", self.MessageSendTrigger)
+        host.trigger.add("MessageReceived", self.MessageReceivedTrigger)
+
+    def handleMe(self, mess_data, client):
+        """Check if messages starts with "/me " and change them if it is the case
+
+        if several messages (different languages) are presents, they all need to start with "/me "
+        if it is for a group chat, resource is used as nick, else roster.getNick is used
+        """
+        # TODO: XHTML-IM /me are not handled
+        for lang, mess in mess_data['message'].iteritems():
+            if not mess.startswith('/me '):
+                # if not all messages start with "/me", no need to continue
+                return mess_data
+            try:
+                nick = mess_data['nick']
+            except KeyError:
+                if mess_data['type'] == C.MESS_TYPE_GROUPCHAT:
+                    nick = mess_data['nick'] = mess_data['from'].resource
+                else:
+                    from_jid = mess_data['from']
+                    try:
+                        ent_type = self.host.memory.getEntityDatum(from_jid.userhostJID(), C.ENTITY_TYPE, client.profile)
+                    except KeyError:
+                        ent_type = None
+                    if ent_type == 'MUC':
+                        nick = mess_data['nick'] = from_jid.resource
+                    else:
+                        nick = mess_data['nick'] = client.roster.getNick(from_jid)
+            mess_data.setdefault('me_update', {})[lang] = u"* {}{}".format(nick, mess[3:])
+
+        if 'me_update' in mess_data:
+            mess_data['message'].update(mess_data.pop('me_update'))
+            mess_data["type"] = C.MESS_TYPE_INFO
+            mess_data["extra"][C.MESS_EXTRA_INFO] = "me"
+        return mess_data
+
+    def MessageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments):
+        post_xml_treatments.addCallback(self.handleMe, client)
+        return True
+
+    def MessageReceivedTrigger(self, client, message_elt, post_treat):
+        """ Check if source is linked and repeat message, else do nothing  """
+        post_treat.addCallback(self.handleMe, client)
+        return True
+
+    def cmd_me(self, client, mess_data):
+        """display a message at third person
+
+        @command (all): message
+            - message: message to show at third person
+                e.g.: "/me clenches his fist" will give "[YOUR_NICK] clenches his fist"
+        """
+        # We just ignore the command as the match is done on receiption by clients
+        return True