comparison sat/plugins/plugin_xep_0095.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 sat.core import exceptions 25 from sat.core import exceptions
25 from twisted.words.protocols.jabber import xmlstream 26 from twisted.words.protocols.jabber import xmlstream
26 from twisted.words.protocols.jabber import error 27 from twisted.words.protocols.jabber import error
27 from zope.interface import implements 28 from zope.interface import implements
35 C.PI_IMPORT_NAME: "XEP-0095", 36 C.PI_IMPORT_NAME: "XEP-0095",
36 C.PI_TYPE: "XEP", 37 C.PI_TYPE: "XEP",
37 C.PI_PROTOCOLS: ["XEP-0095"], 38 C.PI_PROTOCOLS: ["XEP-0095"],
38 C.PI_MAIN: "XEP_0095", 39 C.PI_MAIN: "XEP_0095",
39 C.PI_HANDLER: "yes", 40 C.PI_HANDLER: "yes",
40 C.PI_DESCRIPTION: _("""Implementation of Stream Initiation""") 41 C.PI_DESCRIPTION: _("""Implementation of Stream Initiation"""),
41 } 42 }
42 43
43 44
44 IQ_SET = '/iq[@type="set"]' 45 IQ_SET = '/iq[@type="set"]'
45 NS_SI = 'http://jabber.org/protocol/si' 46 NS_SI = "http://jabber.org/protocol/si"
46 SI_REQUEST = IQ_SET + '/si[@xmlns="' + NS_SI + '"]' 47 SI_REQUEST = IQ_SET + '/si[@xmlns="' + NS_SI + '"]'
47 SI_PROFILE_HEADER = "http://jabber.org/protocol/si/profile/" 48 SI_PROFILE_HEADER = "http://jabber.org/protocol/si/profile/"
48 SI_ERROR_CONDITIONS = ('bad-profile', 'no-valid-streams') 49 SI_ERROR_CONDITIONS = ("bad-profile", "no-valid-streams")
49 50
50 51
51 class XEP_0095(object): 52 class XEP_0095(object):
52
53 def __init__(self, host): 53 def __init__(self, host):
54 log.info(_("Plugin XEP_0095 initialization")) 54 log.info(_("Plugin XEP_0095 initialization"))
55 self.host = host 55 self.host = host
56 self.si_profiles = {} # key: SI profile, value: callback 56 self.si_profiles = {} # key: SI profile, value: callback
57 57
68 68
69 def unregisterSIProfile(self, si_profile): 69 def unregisterSIProfile(self, si_profile):
70 try: 70 try:
71 del self.si_profiles[si_profile] 71 del self.si_profiles[si_profile]
72 except KeyError: 72 except KeyError:
73 log.error(u"Trying to unregister SI profile [{}] which was not registered".format(si_profile)) 73 log.error(
74 u"Trying to unregister SI profile [{}] which was not registered".format(
75 si_profile
76 )
77 )
74 78
75 def streamInit(self, iq_elt, client): 79 def streamInit(self, iq_elt, client):
76 """This method is called on stream initiation (XEP-0095 #3.2) 80 """This method is called on stream initiation (XEP-0095 #3.2)
77 81
78 @param iq_elt: IQ element 82 @param iq_elt: IQ element
79 """ 83 """
80 log.info(_("XEP-0095 Stream initiation")) 84 log.info(_("XEP-0095 Stream initiation"))
81 iq_elt.handled = True 85 iq_elt.handled = True
82 si_elt = iq_elt.elements(NS_SI, 'si').next() 86 si_elt = iq_elt.elements(NS_SI, "si").next()
83 si_id = si_elt['id'] 87 si_id = si_elt["id"]
84 si_mime_type = iq_elt.getAttribute('mime-type', 'application/octet-stream') 88 si_mime_type = iq_elt.getAttribute("mime-type", "application/octet-stream")
85 si_profile = si_elt['profile'] 89 si_profile = si_elt["profile"]
86 si_profile_key = si_profile[len(SI_PROFILE_HEADER):] if si_profile.startswith(SI_PROFILE_HEADER) else si_profile 90 si_profile_key = (
91 si_profile[len(SI_PROFILE_HEADER) :]
92 if si_profile.startswith(SI_PROFILE_HEADER)
93 else si_profile
94 )
87 if si_profile_key in self.si_profiles: 95 if si_profile_key in self.si_profiles:
88 #We know this SI profile, we call the callback 96 # We know this SI profile, we call the callback
89 self.si_profiles[si_profile_key](client, iq_elt, si_id, si_mime_type, si_elt) 97 self.si_profiles[si_profile_key](client, iq_elt, si_id, si_mime_type, si_elt)
90 else: 98 else:
91 #We don't know this profile, we send an error 99 # We don't know this profile, we send an error
92 self.sendError(client, iq_elt, 'bad-profile') 100 self.sendError(client, iq_elt, "bad-profile")
93 101
94 def sendError(self, client, request, condition): 102 def sendError(self, client, request, condition):
95 """Send IQ error as a result 103 """Send IQ error as a result
96 104
97 @param request(domish.Element): original IQ request 105 @param request(domish.Element): original IQ request
98 @param condition(str): error condition 106 @param condition(str): error condition
99 """ 107 """
100 if condition in SI_ERROR_CONDITIONS: 108 if condition in SI_ERROR_CONDITIONS:
101 si_condition = condition 109 si_condition = condition
102 condition = 'bad-request' 110 condition = "bad-request"
103 else: 111 else:
104 si_condition = None 112 si_condition = None
105 113
106 iq_error_elt = error.StanzaError(condition).toResponse(request) 114 iq_error_elt = error.StanzaError(condition).toResponse(request)
107 if si_condition is not None: 115 if si_condition is not None:
117 @param misc_elts(list[domish.Element]): list of elements to add 125 @param misc_elts(list[domish.Element]): list of elements to add
118 """ 126 """
119 log.info(_("sending stream initiation accept answer")) 127 log.info(_("sending stream initiation accept answer"))
120 if misc_elts is None: 128 if misc_elts is None:
121 misc_elts = [] 129 misc_elts = []
122 result_elt = xmlstream.toResponse(iq_elt, 'result') 130 result_elt = xmlstream.toResponse(iq_elt, "result")
123 si_elt = result_elt.addElement((NS_SI, 'si')) 131 si_elt = result_elt.addElement((NS_SI, "si"))
124 si_elt.addChild(feature_elt) 132 si_elt.addChild(feature_elt)
125 for elt in misc_elts: 133 for elt in misc_elts:
126 si_elt.addChild(elt) 134 si_elt.addChild(elt)
127 client.send(result_elt) 135 client.send(result_elt)
128 136
132 except StopIteration: 140 except StopIteration:
133 log.warning(u"No <si/> element found in result while expected") 141 log.warning(u"No <si/> element found in result while expected")
134 raise exceptions.DataError 142 raise exceptions.DataError
135 return (iq_elt, si_elt) 143 return (iq_elt, si_elt)
136 144
137 145 def proposeStream(
138 def proposeStream(self, client, to_jid, si_profile, feature_elt, misc_elts, mime_type='application/octet-stream'): 146 self,
147 client,
148 to_jid,
149 si_profile,
150 feature_elt,
151 misc_elts,
152 mime_type="application/octet-stream",
153 ):
139 """Propose a stream initiation 154 """Propose a stream initiation
140 155
141 @param to_jid(jid.JID): recipient 156 @param to_jid(jid.JID): recipient
142 @param si_profile(unicode): Stream initiation profile (XEP-0095) 157 @param si_profile(unicode): Stream initiation profile (XEP-0095)
143 @param feature_elt(domish.Element): feature element, according to XEP-0020 158 @param feature_elt(domish.Element): feature element, according to XEP-0020
152 sid = str(uuid.uuid4()) 167 sid = str(uuid.uuid4())
153 log.debug(_(u"Stream Session ID: %s") % offer["id"]) 168 log.debug(_(u"Stream Session ID: %s") % offer["id"])
154 169
155 offer["from"] = client.jid.full() 170 offer["from"] = client.jid.full()
156 offer["to"] = to_jid.full() 171 offer["to"] = to_jid.full()
157 si = offer.addElement('si', NS_SI) 172 si = offer.addElement("si", NS_SI)
158 si['id'] = sid 173 si["id"] = sid
159 si["mime-type"] = mime_type 174 si["mime-type"] = mime_type
160 si["profile"] = si_profile 175 si["profile"] = si_profile
161 for elt in misc_elts: 176 for elt in misc_elts:
162 si.addChild(elt) 177 si.addChild(elt)
163 si.addChild(feature_elt) 178 si.addChild(feature_elt)
173 def __init__(self, plugin_parent): 188 def __init__(self, plugin_parent):
174 self.plugin_parent = plugin_parent 189 self.plugin_parent = plugin_parent
175 self.host = plugin_parent.host 190 self.host = plugin_parent.host
176 191
177 def connectionInitialized(self): 192 def connectionInitialized(self):
178 self.xmlstream.addObserver(SI_REQUEST, self.plugin_parent.streamInit, client=self.parent) 193 self.xmlstream.addObserver(
179 194 SI_REQUEST, self.plugin_parent.streamInit, client=self.parent
180 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): 195 )
181 return [disco.DiscoFeature(NS_SI)] + [disco.DiscoFeature(u"http://jabber.org/protocol/si/profile/{}".format(profile_name)) for profile_name in self.plugin_parent.si_profiles] 196
182 197 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
183 def getDiscoItems(self, requestor, target, nodeIdentifier=''): 198 return [disco.DiscoFeature(NS_SI)] + [
199 disco.DiscoFeature(
200 u"http://jabber.org/protocol/si/profile/{}".format(profile_name)
201 )
202 for profile_name in self.plugin_parent.si_profiles
203 ]
204
205 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
184 return [] 206 return []