Mercurial > libervia-backend
comparison sat/plugins/plugin_exp_parrot.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 003b8b4b56a7 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from sat.core.log import getLogger | 22 from sat.core.log import getLogger |
23 | |
23 log = getLogger(__name__) | 24 log = getLogger(__name__) |
24 from twisted.words.protocols.jabber import jid | 25 from twisted.words.protocols.jabber import jid |
25 | 26 |
26 from sat.core.exceptions import UnknownEntityError | 27 from sat.core.exceptions import UnknownEntityError |
27 #from sat.tools import trigger | 28 |
29 # from sat.tools import trigger | |
28 | 30 |
29 PLUGIN_INFO = { | 31 PLUGIN_INFO = { |
30 C.PI_NAME: "Parrot Plugin", | 32 C.PI_NAME: "Parrot Plugin", |
31 C.PI_IMPORT_NAME: "EXP-PARROT", | 33 C.PI_IMPORT_NAME: "EXP-PARROT", |
32 C.PI_TYPE: "EXP", | 34 C.PI_TYPE: "EXP", |
33 C.PI_PROTOCOLS: [], | 35 C.PI_PROTOCOLS: [], |
34 C.PI_DEPENDENCIES: ["XEP-0045"], | 36 C.PI_DEPENDENCIES: ["XEP-0045"], |
35 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS], | 37 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS], |
36 C.PI_MAIN: "Exp_Parrot", | 38 C.PI_MAIN: "Exp_Parrot", |
37 C.PI_HANDLER: "no", | 39 C.PI_HANDLER: "no", |
38 C.PI_DESCRIPTION: _(u"""Implementation of parrot mode (repeat messages between 2 entities)""") | 40 C.PI_DESCRIPTION: _( |
41 u"""Implementation of parrot mode (repeat messages between 2 entities)""" | |
42 ), | |
39 } | 43 } |
40 | 44 |
41 | 45 |
42 class Exp_Parrot(object): | 46 class Exp_Parrot(object): |
43 """Parrot mode plugin: repeat messages from one entity or MUC room to another one""" | 47 """Parrot mode plugin: repeat messages from one entity or MUC room to another one""" |
48 | |
44 # XXX: This plugin can be potentially dangerous if we don't trust entities linked | 49 # XXX: This plugin can be potentially dangerous if we don't trust entities linked |
45 # this is specially true if we have other triggers. | 50 # this is specially true if we have other triggers. |
46 # sendMessageTrigger avoid other triggers execution, it's deactivated to allow | 51 # sendMessageTrigger avoid other triggers execution, it's deactivated to allow |
47 # /unparrot command in text commands plugin. | 52 # /unparrot command in text commands plugin. |
48 # FIXME: potentially unsecure, specially with e2e encryption | 53 # FIXME: potentially unsecure, specially with e2e encryption |
49 | 54 |
50 def __init__(self, host): | 55 def __init__(self, host): |
51 log.info(_("Plugin Parrot initialization")) | 56 log.info(_("Plugin Parrot initialization")) |
52 self.host = host | 57 self.host = host |
53 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100) | 58 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100) |
54 #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) | 59 # host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) |
55 try: | 60 try: |
56 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) | 61 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) |
57 except KeyError: | 62 except KeyError: |
58 log.info(_(u"Text commands not available")) | 63 log.info(_(u"Text commands not available")) |
59 | 64 |
60 #def sendMessageTrigger(self, client, mess_data, treatments): | 65 # def sendMessageTrigger(self, client, mess_data, treatments): |
61 # """ Deactivate other triggers if recipient is in parrot links """ | 66 # """ Deactivate other triggers if recipient is in parrot links """ |
62 # try: | 67 # try: |
63 # _links = client.parrot_links | 68 # _links = client.parrot_links |
64 # except AttributeError: | 69 # except AttributeError: |
65 # return True | 70 # return True |
82 | 87 |
83 if not from_jid.userhostJID() in _links: | 88 if not from_jid.userhostJID() in _links: |
84 return True | 89 return True |
85 | 90 |
86 message = {} | 91 message = {} |
87 for e in message_elt.elements(C.NS_CLIENT, 'body'): | 92 for e in message_elt.elements(C.NS_CLIENT, "body"): |
88 body = unicode(e) | 93 body = unicode(e) |
89 lang = e.getAttribute('lang') or '' | 94 lang = e.getAttribute("lang") or "" |
90 | 95 |
91 try: | 96 try: |
92 entity_type = self.host.memory.getEntityData(from_jid, ['type'], profile)["type"] | 97 entity_type = self.host.memory.getEntityData(from_jid, ["type"], profile)[ |
98 "type" | |
99 ] | |
93 except (UnknownEntityError, KeyError): | 100 except (UnknownEntityError, KeyError): |
94 entity_type = "contact" | 101 entity_type = "contact" |
95 if entity_type == 'chatroom': | 102 if entity_type == "chatroom": |
96 src_txt = from_jid.resource | 103 src_txt = from_jid.resource |
97 if src_txt == self.host.plugins["XEP-0045"].getRoomNick(client, from_jid.userhostJID()): | 104 if src_txt == self.host.plugins["XEP-0045"].getRoomNick( |
98 #we won't repeat our own messages | 105 client, from_jid.userhostJID() |
106 ): | |
107 # we won't repeat our own messages | |
99 return True | 108 return True |
100 else: | 109 else: |
101 src_txt = from_jid.user | 110 src_txt = from_jid.user |
102 message[lang] = u"[{}] {}".format(src_txt, body) | 111 message[lang] = u"[{}] {}".format(src_txt, body) |
103 | 112 |
104 linked = _links[from_jid.userhostJID()] | 113 linked = _links[from_jid.userhostJID()] |
105 | 114 |
106 client.sendMessage(jid.JID(unicode(linked)), message, None, "auto", no_trigger=True) | 115 client.sendMessage( |
116 jid.JID(unicode(linked)), message, None, "auto", no_trigger=True | |
117 ) | |
107 | 118 |
108 return True | 119 return True |
109 | 120 |
110 def addParrot(self, client, source_jid, dest_jid): | 121 def addParrot(self, client, source_jid, dest_jid): |
111 """Add a parrot link from one entity to another one | 122 """Add a parrot link from one entity to another one |
117 _links = client.parrot_links | 128 _links = client.parrot_links |
118 except AttributeError: | 129 except AttributeError: |
119 _links = client.parrot_links = {} | 130 _links = client.parrot_links = {} |
120 | 131 |
121 _links[source_jid.userhostJID()] = dest_jid | 132 _links[source_jid.userhostJID()] = dest_jid |
122 log.info(u"Parrot mode: %s will be repeated to %s" % (source_jid.userhost(), unicode(dest_jid))) | 133 log.info( |
134 u"Parrot mode: %s will be repeated to %s" | |
135 % (source_jid.userhost(), unicode(dest_jid)) | |
136 ) | |
123 | 137 |
124 def removeParrot(self, client, source_jid): | 138 def removeParrot(self, client, source_jid): |
125 """Remove parrot link | 139 """Remove parrot link |
126 | 140 |
127 @param source_jid: this entity will no more be repeated | 141 @param source_jid: this entity will no more be repeated |
139 try: | 153 try: |
140 link_left_jid = jid.JID(mess_data["unparsed"].strip()) | 154 link_left_jid = jid.JID(mess_data["unparsed"].strip()) |
141 if not link_left_jid.user or not link_left_jid.host: | 155 if not link_left_jid.user or not link_left_jid.host: |
142 raise jid.InvalidFormat | 156 raise jid.InvalidFormat |
143 except (RuntimeError, jid.InvalidFormat, AttributeError): | 157 except (RuntimeError, jid.InvalidFormat, AttributeError): |
144 txt_cmd.feedBack(client, "Can't activate Parrot mode for invalid jid", mess_data) | 158 txt_cmd.feedBack( |
159 client, "Can't activate Parrot mode for invalid jid", mess_data | |
160 ) | |
145 return False | 161 return False |
146 | 162 |
147 link_right_jid = mess_data['to'] | 163 link_right_jid = mess_data["to"] |
148 | 164 |
149 self.addParrot(client, link_left_jid, link_right_jid) | 165 self.addParrot(client, link_left_jid, link_right_jid) |
150 self.addParrot(client, link_right_jid, link_left_jid) | 166 self.addParrot(client, link_right_jid, link_left_jid) |
151 | 167 |
152 txt_cmd.feedBack(client, "Parrot mode activated for {}".format(unicode(link_left_jid)), mess_data) | 168 txt_cmd.feedBack( |
169 client, | |
170 "Parrot mode activated for {}".format(unicode(link_left_jid)), | |
171 mess_data, | |
172 ) | |
153 | 173 |
154 return False | 174 return False |
155 | 175 |
156 def cmd_unparrot(self, client, mess_data): | 176 def cmd_unparrot(self, client, mess_data): |
157 """remove Parrot mode between 2 entities, in both directions.""" | 177 """remove Parrot mode between 2 entities, in both directions.""" |
161 try: | 181 try: |
162 link_left_jid = jid.JID(mess_data["unparsed"].strip()) | 182 link_left_jid = jid.JID(mess_data["unparsed"].strip()) |
163 if not link_left_jid.user or not link_left_jid.host: | 183 if not link_left_jid.user or not link_left_jid.host: |
164 raise jid.InvalidFormat | 184 raise jid.InvalidFormat |
165 except jid.InvalidFormat: | 185 except jid.InvalidFormat: |
166 txt_cmd.feedBack(client, u"Can't deactivate Parrot mode for invalid jid", mess_data) | 186 txt_cmd.feedBack( |
187 client, u"Can't deactivate Parrot mode for invalid jid", mess_data | |
188 ) | |
167 return False | 189 return False |
168 | 190 |
169 link_right_jid = mess_data['to'] | 191 link_right_jid = mess_data["to"] |
170 | 192 |
171 self.removeParrot(client, link_left_jid) | 193 self.removeParrot(client, link_left_jid) |
172 self.removeParrot(client, link_right_jid) | 194 self.removeParrot(client, link_right_jid) |
173 | 195 |
174 txt_cmd.feedBack(client, u"Parrot mode deactivated for {} and {}".format(unicode(link_left_jid), unicode(link_right_jid)), mess_data) | 196 txt_cmd.feedBack( |
197 client, | |
198 u"Parrot mode deactivated for {} and {}".format( | |
199 unicode(link_left_jid), unicode(link_right_jid) | |
200 ), | |
201 mess_data, | |
202 ) | |
175 | 203 |
176 return False | 204 return False |