comparison sat/plugins/plugin_exp_parrot.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_exp_parrot.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for parrot mode (experimental)
5 # Copyright (C) 2009-2018 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 from twisted.words.protocols.jabber import jid
25
26 from sat.core.exceptions import UnknownEntityError
27 #from sat.tools import trigger
28
29 PLUGIN_INFO = {
30 C.PI_NAME: "Parrot Plugin",
31 C.PI_IMPORT_NAME: "EXP-PARROT",
32 C.PI_TYPE: "EXP",
33 C.PI_PROTOCOLS: [],
34 C.PI_DEPENDENCIES: ["XEP-0045"],
35 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS],
36 C.PI_MAIN: "Exp_Parrot",
37 C.PI_HANDLER: "no",
38 C.PI_DESCRIPTION: _(u"""Implementation of parrot mode (repeat messages between 2 entities)""")
39 }
40
41
42 class Exp_Parrot(object):
43 """Parrot mode plugin: repeat messages from one entity or MUC room to another one"""
44 # XXX: This plugin can be potentially dangerous if we don't trust entities linked
45 # this is specially true if we have other triggers.
46 # sendMessageTrigger avoid other triggers execution, it's deactivated to allow
47 # /unparrot command in text commands plugin.
48 # FIXME: potentially unsecure, specially with e2e encryption
49
50 def __init__(self, host):
51 log.info(_("Plugin Parrot initialization"))
52 self.host = host
53 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100)
54 #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100)
55 try:
56 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
57 except KeyError:
58 log.info(_(u"Text commands not available"))
59
60 #def sendMessageTrigger(self, client, mess_data, treatments):
61 # """ Deactivate other triggers if recipient is in parrot links """
62 # try:
63 # _links = client.parrot_links
64 # except AttributeError:
65 # return True
66 #
67 # if mess_data['to'].userhostJID() in _links.values():
68 # log.debug("Parrot link detected, skipping other triggers")
69 # raise trigger.SkipOtherTriggers
70
71 def MessageReceivedTrigger(self, client, message_elt, post_treat):
72 """ Check if source is linked and repeat message, else do nothing """
73 # TODO: many things are not repeated (subject, thread, etc)
74 profile = client.profile
75 client = self.host.getClient(profile)
76 from_jid = message_elt["from"]
77
78 try:
79 _links = client.parrot_links
80 except AttributeError:
81 return True
82
83 if not from_jid.userhostJID() in _links:
84 return True
85
86 message = {}
87 for e in message_elt.elements(C.NS_CLIENT, 'body'):
88 body = unicode(e)
89 lang = e.getAttribute('lang') or ''
90
91 try:
92 entity_type = self.host.memory.getEntityData(from_jid, ['type'], profile)["type"]
93 except (UnknownEntityError, KeyError):
94 entity_type = "contact"
95 if entity_type == 'chatroom':
96 src_txt = from_jid.resource
97 if src_txt == self.host.plugins["XEP-0045"].getRoomNick(client, from_jid.userhostJID()):
98 #we won't repeat our own messages
99 return True
100 else:
101 src_txt = from_jid.user
102 message[lang] = u"[{}] {}".format(src_txt, body)
103
104 linked = _links[from_jid.userhostJID()]
105
106 client.sendMessage(jid.JID(unicode(linked)), message, None, "auto", no_trigger=True)
107
108 return True
109
110 def addParrot(self, client, source_jid, dest_jid):
111 """Add a parrot link from one entity to another one
112
113 @param source_jid: entity from who messages will be repeated
114 @param dest_jid: entity where the messages will be repeated
115 """
116 try:
117 _links = client.parrot_links
118 except AttributeError:
119 _links = client.parrot_links = {}
120
121 _links[source_jid.userhostJID()] = dest_jid
122 log.info(u"Parrot mode: %s will be repeated to %s" % (source_jid.userhost(), unicode(dest_jid)))
123
124 def removeParrot(self, client, source_jid):
125 """Remove parrot link
126
127 @param source_jid: this entity will no more be repeated
128 """
129 try:
130 del client.parrot_links[source_jid.userhostJID()]
131 except (AttributeError, KeyError):
132 pass
133
134 def cmd_parrot(self, client, mess_data):
135 """activate Parrot mode between 2 entities, in both directions."""
136 log.debug("Catched parrot command")
137 txt_cmd = self.host.plugins[C.TEXT_CMDS]
138
139 try:
140 link_left_jid = jid.JID(mess_data["unparsed"].strip())
141 if not link_left_jid.user or not link_left_jid.host:
142 raise jid.InvalidFormat
143 except (RuntimeError, jid.InvalidFormat, AttributeError):
144 txt_cmd.feedBack(client, "Can't activate Parrot mode for invalid jid", mess_data)
145 return False
146
147 link_right_jid = mess_data['to']
148
149 self.addParrot(client, link_left_jid, link_right_jid)
150 self.addParrot(client, link_right_jid, link_left_jid)
151
152 txt_cmd.feedBack(client, "Parrot mode activated for {}".format(unicode(link_left_jid)), mess_data)
153
154 return False
155
156 def cmd_unparrot(self, client, mess_data):
157 """remove Parrot mode between 2 entities, in both directions."""
158 log.debug("Catched unparrot command")
159 txt_cmd = self.host.plugins[C.TEXT_CMDS]
160
161 try:
162 link_left_jid = jid.JID(mess_data["unparsed"].strip())
163 if not link_left_jid.user or not link_left_jid.host:
164 raise jid.InvalidFormat
165 except jid.InvalidFormat:
166 txt_cmd.feedBack(client, u"Can't deactivate Parrot mode for invalid jid", mess_data)
167 return False
168
169 link_right_jid = mess_data['to']
170
171 self.removeParrot(client, link_left_jid)
172 self.removeParrot(client, link_right_jid)
173
174 txt_cmd.feedBack(client, u"Parrot mode deactivated for {} and {}".format(unicode(link_left_jid), unicode(link_right_jid)), mess_data)
175
176 return False