annotate src/plugins/plugin_xep_0245.py @ 1966:d727aab9a80e

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