comparison sat/plugins/plugin_xep_0359.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 003b8b4b56a7
children f0f48b4deb35
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 Message Archive Management (XEP-0359) 4 # SAT plugin for Message Archive Management (XEP-0359)
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 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) 6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core import exceptions 22 from sat.core import exceptions
23 from sat.core.i18n import _ 23 from sat.core.i18n import _
24 from sat.core.log import getLogger 24 from sat.core.log import getLogger
25 from twisted.words.protocols.jabber import xmlstream 25 from twisted.words.protocols.jabber import xmlstream
26 from zope.interface import implements 26 from zope.interface import implementer
27 from wokkel import disco 27 from wokkel import disco
28 28
29 log = getLogger(__name__) 29 log = getLogger(__name__)
30 30
31 31
32 PLUGIN_INFO = { 32 PLUGIN_INFO = {
33 C.PI_NAME: u"Unique and Stable Stanza IDs", 33 C.PI_NAME: "Unique and Stable Stanza IDs",
34 C.PI_IMPORT_NAME: u"XEP-0359", 34 C.PI_IMPORT_NAME: "XEP-0359",
35 C.PI_TYPE: u"XEP", 35 C.PI_TYPE: "XEP",
36 C.PI_PROTOCOLS: [u"XEP-0359"], 36 C.PI_PROTOCOLS: ["XEP-0359"],
37 C.PI_MAIN: u"XEP_0359", 37 C.PI_MAIN: "XEP_0359",
38 C.PI_HANDLER: u"yes", 38 C.PI_HANDLER: "yes",
39 C.PI_DESCRIPTION: _(u"""Implementation of Unique and Stable Stanza IDs"""), 39 C.PI_DESCRIPTION: _("""Implementation of Unique and Stable Stanza IDs"""),
40 } 40 }
41 41
42 NS_SID = u"urn:xmpp:sid:0" 42 NS_SID = "urn:xmpp:sid:0"
43 43
44 44
45 class XEP_0359(object): 45 class XEP_0359(object):
46 46
47 def __init__(self, host): 47 def __init__(self, host):
48 log.info(_(u"Unique and Stable Stanza IDs plugin initialization")) 48 log.info(_("Unique and Stable Stanza IDs plugin initialization"))
49 self.host = host 49 self.host = host
50 host.registerNamespace(u"stanza_id", NS_SID) 50 host.registerNamespace("stanza_id", NS_SID)
51 host.trigger.add(u"message_parse", self._message_parseTrigger) 51 host.trigger.add("message_parse", self._message_parseTrigger)
52 52
53 def _message_parseTrigger(self, client, message_elt, mess_data): 53 def _message_parseTrigger(self, client, message_elt, mess_data):
54 """Check if message has a stanza-id""" 54 """Check if message has a stanza-id"""
55 stanza_id = self.getStanzaId(message_elt, client.jid.userhostJID()) 55 stanza_id = self.getStanzaId(message_elt, client.jid.userhostJID())
56 if stanza_id is not None: 56 if stanza_id is not None:
57 mess_data[u'extra'][u'stanza_id'] = stanza_id 57 mess_data['extra']['stanza_id'] = stanza_id
58 return True 58 return True
59 59
60 def getStanzaId(self, element, by): 60 def getStanzaId(self, element, by):
61 """Return stanza-id if found in element 61 """Return stanza-id if found in element
62 62
63 @param element(domish.Element): element to parse 63 @param element(domish.Element): element to parse
64 @param by(jid.JID): entity which should have set a stanza-id 64 @param by(jid.JID): entity which should have set a stanza-id
65 @return (unicode, None): stanza-id if found 65 @return (unicode, None): stanza-id if found
66 """ 66 """
67 stanza_id = None 67 stanza_id = None
68 for stanza_elt in element.elements(NS_SID, u"stanza-id"): 68 for stanza_elt in element.elements(NS_SID, "stanza-id"):
69 if stanza_elt.getAttribute(u"by") == by.full(): 69 if stanza_elt.getAttribute("by") == by.full():
70 if stanza_id is not None: 70 if stanza_id is not None:
71 # we must not have more than one element (§3 #4) 71 # we must not have more than one element (§3 #4)
72 raise exceptions.DataError( 72 raise exceptions.DataError(
73 u"More than one corresponding stanza-id found!") 73 "More than one corresponding stanza-id found!")
74 stanza_id = stanza_elt.getAttribute(u"id") 74 stanza_id = stanza_elt.getAttribute("id")
75 # we don't break to be sure that there is no more than one element 75 # we don't break to be sure that there is no more than one element
76 # with this "by" attribute 76 # with this "by" attribute
77 77
78 return stanza_id 78 return stanza_id
79 79
82 82
83 @param element(domish.Element): stanza where the <stanza-id/> must be added 83 @param element(domish.Element): stanza where the <stanza-id/> must be added
84 @param stanza_id(unicode): id to use 84 @param stanza_id(unicode): id to use
85 @param by(jid.JID, None): jid to use or None to use client.jid 85 @param by(jid.JID, None): jid to use or None to use client.jid
86 """ 86 """
87 sid_elt = element.addElement((NS_SID, u"stanza-id")) 87 sid_elt = element.addElement((NS_SID, "stanza-id"))
88 sid_elt[u"by"] = client.jid.userhost() if by is None else by.userhost() 88 sid_elt["by"] = client.jid.userhost() if by is None else by.userhost()
89 sid_elt[u"id"] = stanza_id 89 sid_elt["id"] = stanza_id
90 90
91 def getHandler(self, client): 91 def getHandler(self, client):
92 return XEP_0359_handler() 92 return XEP_0359_handler()
93 93
94 94
95 @implementer(disco.IDisco)
95 class XEP_0359_handler(xmlstream.XMPPHandler): 96 class XEP_0359_handler(xmlstream.XMPPHandler):
96 implements(disco.IDisco)
97 97
98 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): 98 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
99 return [disco.DiscoFeature(NS_SID)] 99 return [disco.DiscoFeature(NS_SID)]
100 100
101 def getDiscoItems(self, requestor, target, nodeIdentifier=""): 101 def getDiscoItems(self, requestor, target, nodeIdentifier=""):