Mercurial > libervia-backend
comparison sat/plugins/plugin_exp_parrot.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 94708a7d3ecf |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 # SAT plugin for parrot mode (experimental) | 4 # SAT plugin for parrot mode (experimental) |
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
6 | 6 |
36 C.PI_DEPENDENCIES: ["XEP-0045"], | 36 C.PI_DEPENDENCIES: ["XEP-0045"], |
37 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS], | 37 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS], |
38 C.PI_MAIN: "Exp_Parrot", | 38 C.PI_MAIN: "Exp_Parrot", |
39 C.PI_HANDLER: "no", | 39 C.PI_HANDLER: "no", |
40 C.PI_DESCRIPTION: _( | 40 C.PI_DESCRIPTION: _( |
41 u"""Implementation of parrot mode (repeat messages between 2 entities)""" | 41 """Implementation of parrot mode (repeat messages between 2 entities)""" |
42 ), | 42 ), |
43 } | 43 } |
44 | 44 |
45 | 45 |
46 class Exp_Parrot(object): | 46 class Exp_Parrot(object): |
58 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100) | 58 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100) |
59 # host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) | 59 # host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) |
60 try: | 60 try: |
61 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) | 61 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) |
62 except KeyError: | 62 except KeyError: |
63 log.info(_(u"Text commands not available")) | 63 log.info(_("Text commands not available")) |
64 | 64 |
65 # def sendMessageTrigger(self, client, mess_data, treatments): | 65 # def sendMessageTrigger(self, client, mess_data, treatments): |
66 # """ Deactivate other triggers if recipient is in parrot links """ | 66 # """ Deactivate other triggers if recipient is in parrot links """ |
67 # try: | 67 # try: |
68 # _links = client.parrot_links | 68 # _links = client.parrot_links |
88 if not from_jid.userhostJID() in _links: | 88 if not from_jid.userhostJID() in _links: |
89 return True | 89 return True |
90 | 90 |
91 message = {} | 91 message = {} |
92 for e in message_elt.elements(C.NS_CLIENT, "body"): | 92 for e in message_elt.elements(C.NS_CLIENT, "body"): |
93 body = unicode(e) | 93 body = str(e) |
94 lang = e.getAttribute("lang") or "" | 94 lang = e.getAttribute("lang") or "" |
95 | 95 |
96 try: | 96 try: |
97 entity_type = self.host.memory.getEntityData( | 97 entity_type = self.host.memory.getEntityData( |
98 from_jid, [C.ENTITY_TYPE], profile)[C.ENTITY_TYPE] | 98 from_jid, [C.ENTITY_TYPE], profile)[C.ENTITY_TYPE] |
105 ): | 105 ): |
106 # we won't repeat our own messages | 106 # we won't repeat our own messages |
107 return True | 107 return True |
108 else: | 108 else: |
109 src_txt = from_jid.user | 109 src_txt = from_jid.user |
110 message[lang] = u"[{}] {}".format(src_txt, body) | 110 message[lang] = "[{}] {}".format(src_txt, body) |
111 | 111 |
112 linked = _links[from_jid.userhostJID()] | 112 linked = _links[from_jid.userhostJID()] |
113 | 113 |
114 client.sendMessage( | 114 client.sendMessage( |
115 jid.JID(unicode(linked)), message, None, "auto", no_trigger=True | 115 jid.JID(str(linked)), message, None, "auto", no_trigger=True |
116 ) | 116 ) |
117 | 117 |
118 return True | 118 return True |
119 | 119 |
120 def addParrot(self, client, source_jid, dest_jid): | 120 def addParrot(self, client, source_jid, dest_jid): |
128 except AttributeError: | 128 except AttributeError: |
129 _links = client.parrot_links = {} | 129 _links = client.parrot_links = {} |
130 | 130 |
131 _links[source_jid.userhostJID()] = dest_jid | 131 _links[source_jid.userhostJID()] = dest_jid |
132 log.info( | 132 log.info( |
133 u"Parrot mode: %s will be repeated to %s" | 133 "Parrot mode: %s will be repeated to %s" |
134 % (source_jid.userhost(), unicode(dest_jid)) | 134 % (source_jid.userhost(), str(dest_jid)) |
135 ) | 135 ) |
136 | 136 |
137 def removeParrot(self, client, source_jid): | 137 def removeParrot(self, client, source_jid): |
138 """Remove parrot link | 138 """Remove parrot link |
139 | 139 |
164 self.addParrot(client, link_left_jid, link_right_jid) | 164 self.addParrot(client, link_left_jid, link_right_jid) |
165 self.addParrot(client, link_right_jid, link_left_jid) | 165 self.addParrot(client, link_right_jid, link_left_jid) |
166 | 166 |
167 txt_cmd.feedBack( | 167 txt_cmd.feedBack( |
168 client, | 168 client, |
169 "Parrot mode activated for {}".format(unicode(link_left_jid)), | 169 "Parrot mode activated for {}".format(str(link_left_jid)), |
170 mess_data, | 170 mess_data, |
171 ) | 171 ) |
172 | 172 |
173 return False | 173 return False |
174 | 174 |
181 link_left_jid = jid.JID(mess_data["unparsed"].strip()) | 181 link_left_jid = jid.JID(mess_data["unparsed"].strip()) |
182 if not link_left_jid.user or not link_left_jid.host: | 182 if not link_left_jid.user or not link_left_jid.host: |
183 raise jid.InvalidFormat | 183 raise jid.InvalidFormat |
184 except jid.InvalidFormat: | 184 except jid.InvalidFormat: |
185 txt_cmd.feedBack( | 185 txt_cmd.feedBack( |
186 client, u"Can't deactivate Parrot mode for invalid jid", mess_data | 186 client, "Can't deactivate Parrot mode for invalid jid", mess_data |
187 ) | 187 ) |
188 return False | 188 return False |
189 | 189 |
190 link_right_jid = mess_data["to"] | 190 link_right_jid = mess_data["to"] |
191 | 191 |
192 self.removeParrot(client, link_left_jid) | 192 self.removeParrot(client, link_left_jid) |
193 self.removeParrot(client, link_right_jid) | 193 self.removeParrot(client, link_right_jid) |
194 | 194 |
195 txt_cmd.feedBack( | 195 txt_cmd.feedBack( |
196 client, | 196 client, |
197 u"Parrot mode deactivated for {} and {}".format( | 197 "Parrot mode deactivated for {} and {}".format( |
198 unicode(link_left_jid), unicode(link_right_jid) | 198 str(link_left_jid), str(link_right_jid) |
199 ), | 199 ), |
200 mess_data, | 200 mess_data, |
201 ) | 201 ) |
202 | 202 |
203 return False | 203 return False |