comparison 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
comparison
equal deleted inserted replaced
1963:a2bc5089c2eb 1964:a86e41d9245d
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for managing xep-245
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
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/>.
19
20 from sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger
23 log = getLogger(__name__)
24
25
26 PLUGIN_INFO = {
27 "name": "XEP-0245 Plugin",
28 "import_name": "XEP-0245",
29 "type": "XEP",
30 "protocols": ["XEP-0245"],
31 "recommendations": [C.TEXT_CMDS],
32 "main": "XEP_0245",
33 "handler": "no",
34 "description": _("""/me syntax handling""")
35 }
36
37
38 class XEP_0245(object):
39
40 def __init__(self, host):
41 log.info(_("Plugin XEP_245 initialization"))
42 self.host = host
43 try:
44 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
45 except KeyError:
46 pass
47 host.trigger.add("messageSend", self.MessageSendTrigger)
48 host.trigger.add("MessageReceived", self.MessageReceivedTrigger)
49
50 def handleMe(self, mess_data, client):
51 """Check if messages starts with "/me " and change them if it is the case
52
53 if several messages (different languages) are presents, they all need to start with "/me "
54 if it is for a group chat, resource is used as nick, else roster.getNick is used
55 """
56 # TODO: XHTML-IM /me are not handled
57 for lang, mess in mess_data['message'].iteritems():
58 if not mess.startswith('/me '):
59 # if not all messages start with "/me", no need to continue
60 return mess_data
61 try:
62 nick = mess_data['nick']
63 except KeyError:
64 if mess_data['type'] == C.MESS_TYPE_GROUPCHAT:
65 nick = mess_data['nick'] = mess_data['from'].resource
66 else:
67 from_jid = mess_data['from']
68 try:
69 ent_type = self.host.memory.getEntityDatum(from_jid.userhostJID(), C.ENTITY_TYPE, client.profile)
70 except KeyError:
71 ent_type = None
72 if ent_type == 'MUC':
73 nick = mess_data['nick'] = from_jid.resource
74 else:
75 nick = mess_data['nick'] = client.roster.getNick(from_jid)
76 mess_data.setdefault('me_update', {})[lang] = u"* {}{}".format(nick, mess[3:])
77
78 if 'me_update' in mess_data:
79 mess_data['message'].update(mess_data.pop('me_update'))
80 mess_data["type"] = C.MESS_TYPE_INFO
81 mess_data["extra"][C.MESS_EXTRA_INFO] = "me"
82 return mess_data
83
84 def MessageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments):
85 post_xml_treatments.addCallback(self.handleMe, client)
86 return True
87
88 def MessageReceivedTrigger(self, client, message_elt, post_treat):
89 """ Check if source is linked and repeat message, else do nothing """
90 post_treat.addCallback(self.handleMe, client)
91 return True
92
93 def cmd_me(self, client, mess_data):
94 """display a message at third person
95
96 @command (all): message
97 - message: message to show at third person
98 e.g.: "/me clenches his fist" will give "[YOUR_NICK] clenches his fist"
99 """
100 # We just ignore the command as the match is done on receiption by clients
101 return True