Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0048.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 | 8990ed9aad31 |
children | fee60f17ebac |
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 Bookmarks (xep-0048) | 4 # SAT plugin for Bookmarks (xep-0048) |
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 |
72 "bookmarksList", | 72 "bookmarksList", |
73 ".plugin", | 73 ".plugin", |
74 in_sign="sss", | 74 in_sign="sss", |
75 out_sign="a{sa{sa{ss}}}", | 75 out_sign="a{sa{sa{ss}}}", |
76 method=self._bookmarksList, | 76 method=self._bookmarksList, |
77 async=True, | 77 async_=True, |
78 ) | 78 ) |
79 host.bridge.addMethod( | 79 host.bridge.addMethod( |
80 "bookmarksRemove", | 80 "bookmarksRemove", |
81 ".plugin", | 81 ".plugin", |
82 in_sign="ssss", | 82 in_sign="ssss", |
83 out_sign="", | 83 out_sign="", |
84 method=self._bookmarksRemove, | 84 method=self._bookmarksRemove, |
85 async=True, | 85 async_=True, |
86 ) | 86 ) |
87 host.bridge.addMethod( | 87 host.bridge.addMethod( |
88 "bookmarksAdd", | 88 "bookmarksAdd", |
89 ".plugin", | 89 ".plugin", |
90 in_sign="ssa{ss}ss", | 90 in_sign="ssa{ss}ss", |
91 out_sign="", | 91 out_sign="", |
92 method=self._bookmarksAdd, | 92 method=self._bookmarksAdd, |
93 async=True, | 93 async_=True, |
94 ) | 94 ) |
95 try: | 95 try: |
96 self.private_plg = self.host.plugins["XEP-0049"] | 96 self.private_plg = self.host.plugins["XEP-0049"] |
97 except KeyError: | 97 except KeyError: |
98 self.private_plg = None | 98 self.private_plg = None |
113 private = yield self._getServerBookmarks("private", client.profile) | 113 private = yield self._getServerBookmarks("private", client.profile) |
114 pubsub = client.bookmarks_pubsub = None | 114 pubsub = client.bookmarks_pubsub = None |
115 | 115 |
116 for bookmarks in (local, private, pubsub): | 116 for bookmarks in (local, private, pubsub): |
117 if bookmarks is not None: | 117 if bookmarks is not None: |
118 for (room_jid, data) in bookmarks[XEP_0048.MUC_TYPE].items(): | 118 for (room_jid, data) in list(bookmarks[XEP_0048.MUC_TYPE].items()): |
119 if data.get("autojoin", "false") == "true": | 119 if data.get("autojoin", "false") == "true": |
120 nick = data.get("nick", client.jid.user) | 120 nick = data.get("nick", client.jid.user) |
121 self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) | 121 self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) |
122 | 122 |
123 # we don't use a DeferredList to gather result here, as waiting for all room would | 123 # we don't use a DeferredList to gather result here, as waiting for all room would |
194 | 194 |
195 for attr in XEP_0048.MUC_ATTRS: | 195 for attr in XEP_0048.MUC_ATTRS: |
196 if conference_elt.hasAttribute(attr): | 196 if conference_elt.hasAttribute(attr): |
197 data[attr] = conference_elt[attr] | 197 data[attr] = conference_elt[attr] |
198 try: | 198 try: |
199 data["nick"] = unicode( | 199 data["nick"] = str( |
200 conference_elt.elements(NS_BOOKMARKS, "nick").next() | 200 next(conference_elt.elements(NS_BOOKMARKS, "nick")) |
201 ) | 201 ) |
202 except StopIteration: | 202 except StopIteration: |
203 pass | 203 pass |
204 # TODO: manage password (need to be secured, see XEP-0049 §4) | 204 # TODO: manage password (need to be secured, see XEP-0049 §4) |
205 | 205 |
262 | 262 |
263 client = self.host.getClient(profile) | 263 client = self.host.getClient(profile) |
264 d = self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) | 264 d = self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) |
265 | 265 |
266 def join_eb(failure): | 266 def join_eb(failure): |
267 log.warning(u"Error while trying to join room: {}".format(failure)) | 267 log.warning("Error while trying to join room: {}".format(failure)) |
268 # FIXME: failure are badly managed in plugin XEP-0045. Plugin XEP-0045 need to be fixed before managing errors correctly here | 268 # FIXME: failure are badly managed in plugin XEP-0045. Plugin XEP-0045 need to be fixed before managing errors correctly here |
269 return {} | 269 return {} |
270 | 270 |
271 d.addCallbacks(lambda __: {}, join_eb) | 271 d.addCallbacks(lambda __: {}, join_eb) |
272 return d | 272 return d |
290 client.bookmarks_pubsub, | 290 client.bookmarks_pubsub, |
291 ): | 291 ): |
292 if bookmarks is None: | 292 if bookmarks is None: |
293 continue | 293 continue |
294 for (room_jid, data) in sorted( | 294 for (room_jid, data) in sorted( |
295 bookmarks[XEP_0048.MUC_TYPE].items(), | 295 list(bookmarks[XEP_0048.MUC_TYPE].items()), |
296 key=lambda item: item[1].get("name", item[0].user), | 296 key=lambda item: item[1].get("name", item[0].user), |
297 ): | 297 ): |
298 room_jid_s = room_jid.full() | 298 room_jid_s = room_jid.full() |
299 adv_list.setRowIndex( | 299 adv_list.setRowIndex( |
300 u"%s %s" % (room_jid_s, data.get("nick") or client.jid.user) | 300 "%s %s" % (room_jid_s, data.get("nick") or client.jid.user) |
301 ) | 301 ) |
302 xmlui.addText(data.get("name", "")) | 302 xmlui.addText(data.get("name", "")) |
303 xmlui.addJid(room_jid) | 303 xmlui.addJid(room_jid) |
304 if C.bool(data.get("autojoin", C.BOOL_FALSE)): | 304 if C.bool(data.get("autojoin", C.BOOL_FALSE)): |
305 xmlui.addText("autojoin") | 305 xmlui.addText("autojoin") |
352 - "private": Private XML storage (XEP-0049) | 352 - "private": Private XML storage (XEP-0049) |
353 - "local": Store in SàT database | 353 - "local": Store in SàT database |
354 @param profile_key: %(doc_profile_key)s | 354 @param profile_key: %(doc_profile_key)s |
355 """ | 355 """ |
356 assert storage_type in ("auto", "pubsub", "private", "local") | 356 assert storage_type in ("auto", "pubsub", "private", "local") |
357 if type_ == XEP_0048.URL_TYPE and {"autojoin", "nick"}.intersection(data.keys()): | 357 if type_ == XEP_0048.URL_TYPE and {"autojoin", "nick"}.intersection(list(data.keys())): |
358 raise ValueError("autojoin or nick can't be used with URLs") | 358 raise ValueError("autojoin or nick can't be used with URLs") |
359 client = self.host.getClient(profile_key) | 359 client = self.host.getClient(profile_key) |
360 if storage_type == "auto": | 360 if storage_type == "auto": |
361 if client.bookmarks_pubsub is not None: | 361 if client.bookmarks_pubsub is not None: |
362 storage_type = "pubsub" | 362 storage_type = "pubsub" |