annotate sat/plugins/plugin_xep_0047.py @ 2886:b06cb71079fa

core (xmpp): new networkEnabled() and networkDisabled() methods: those methods can be called by platform specific plugins when network is known to be (un)available. This way, connection attempts can be cancelled when no network is available, saving resources (notably battery on mobile devices), or attempts can be restarted immediately when network is known to be available again.
author Goffi <goffi@goffi.org>
date Sat, 06 Apr 2019 19:05:57 +0200
parents 003b8b4b56a7
children 69e4716d6268
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SAT plugin for managing gateways (xep-0047)
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
10 # (at your option) any later version.
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
15 # GNU Affero General Public License for more details.
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 638
diff changeset
20 from sat.core.i18n import _
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
21 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
22
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
23 log = getLogger(__name__)
1558
6a8dd91476f0 plugin XEP-0047: minor improvements
Goffi <goffi@goffi.org>
parents: 1524
diff changeset
24 from sat.core.constants import Const as C
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
25 from sat.core import exceptions
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
26 from twisted.words.protocols.jabber import jid
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
27 from twisted.words.protocols.jabber import xmlstream
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
28 from twisted.words.protocols.jabber import error
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.internet import reactor
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
30 from twisted.internet import defer
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
31 from twisted.python import failure
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
32
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from wokkel import disco, iwokkel
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
34
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from zope.interface import implements
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
36
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
37 import base64
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
38
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
39 try:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from twisted.words.protocols.xmlstream import XMPPHandler
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
41 except ImportError:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
42 from wokkel.subprotocols import XMPPHandler
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
43
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44 MESSAGE = "/message"
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
45 IQ_SET = '/iq[@type="set"]'
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46 NS_IBB = "http://jabber.org/protocol/ibb"
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47 IBB_OPEN = IQ_SET + '/open[@xmlns="' + NS_IBB + '"]'
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
48 IBB_CLOSE = IQ_SET + '/close[@xmlns="' + NS_IBB + '" and @sid="{}"]'
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
49 IBB_IQ_DATA = IQ_SET + '/data[@xmlns="' + NS_IBB + '" and @sid="{}"]'
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
50 IBB_MESSAGE_DATA = MESSAGE + '/data[@xmlns="' + NS_IBB + '" and @sid="{}"]'
1628
e9936fcfaf91 plugin 0047: increased timeout to 120s
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
51 TIMEOUT = 120 # timeout for workflow
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 DEFER_KEY = "finished" # key of the deferred used to track session end
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
53
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
54 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
55 C.PI_NAME: "In-Band Bytestream Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
56 C.PI_IMPORT_NAME: "XEP-0047",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
57 C.PI_TYPE: "XEP",
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
58 C.PI_MODES: C.PLUG_MODE_BOTH,
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
59 C.PI_PROTOCOLS: ["XEP-0047"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
60 C.PI_MAIN: "XEP_0047",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
61 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 C.PI_DESCRIPTION: _("""Implementation of In-Band Bytestreams"""),
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 }
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
65
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
66 class XEP_0047(object):
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67 NAMESPACE = NS_IBB
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
68 BLOCK_SIZE = 4096
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
71 log.info(_("In-Band Bytestreams plugin initialization"))
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.host = host
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
73
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
74 def getHandler(self, client):
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75 return XEP_0047_handler(self)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
77 def profileConnected(self, client):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
78 client.xep_0047_current_stream = {} # key: stream_id, value: data(dict)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
79
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
80 def _timeOut(self, sid, client):
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
81 """Delete current_stream id, called after timeout
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
82
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
83 @param sid(unicode): session id of client.xep_0047_current_stream
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
84 @param client: %(doc_client)s
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
85 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 log.info(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 u"In-Band Bytestream: TimeOut reached for id {sid} [{profile}]".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 sid=sid, profile=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 )
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
91 self._killSession(sid, client, "TIMEOUT")
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
92
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
93 def _killSession(self, sid, client, failure_reason=None):
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
94 """Delete a current_stream id, clean up associated observers
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
95
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
96 @param sid(unicode): session id
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
97 @param client: %(doc_client)s
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
98 @param failure_reason(None, unicode): if None the session is successful
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
99 else, will be used to call failure_cb
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
100 """
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
101 try:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
102 session = client.xep_0047_current_stream[sid]
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
103 except KeyError:
1583
d46aae87c03a plugins XEP-0047, XEP-0065, XEP-0096: fixed use of failure.condition insteand of failure.value + removed gettext use for local logs
Goffi <goffi@goffi.org>
parents: 1578
diff changeset
104 log.warning(u"kill id called on a non existant id")
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
105 return
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
106
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
107 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
108 observer_cb = session["observer_cb"]
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
109 except KeyError:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
110 pass
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
111 else:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
112 client.xmlstream.removeObserver(session["event_data"], observer_cb)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
113
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
114 if session["timer"].active():
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
115 session["timer"].cancel()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
116
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
117 del client.xep_0047_current_stream[sid]
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
118
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
119 success = failure_reason is None
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
120 stream_d = session[DEFER_KEY]
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
121
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
122 if success:
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
123 stream_d.callback(None)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
124 else:
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
125 stream_d.errback(failure.Failure(exceptions.DataError(failure_reason)))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
126
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
127 def createSession(self, *args, **kwargs):
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
128 """like [_createSession] but return the session deferred instead of the whole session
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
129
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
130 session deferred is fired when transfer is finished
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
131 """
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
132 return self._createSession(*args, **kwargs)[DEFER_KEY]
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
133
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
134 def _createSession(self, client, stream_object, to_jid, sid):
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
135 """Called when a bytestream is imminent
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
136
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
137 @param stream_object(IConsumer): stream object where data will be written
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
138 @param to_jid(jid.JId): jid of the other peer
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
139 @param sid(unicode): session id
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
140 @return (dict): session data
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
141 """
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
142 if sid in client.xep_0047_current_stream:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 raise exceptions.ConflictError(u"A session with this id already exists !")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 session_data = client.xep_0047_current_stream[sid] = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 "id": sid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 DEFER_KEY: defer.Deferred(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
147 "to": to_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
148 "stream_object": stream_object,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
149 "seq": -1,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
150 "timer": reactor.callLater(TIMEOUT, self._timeOut, sid, client),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
151 }
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
152
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
153 return session_data
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
154
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
155 def _onIBBOpen(self, iq_elt, client):
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
156 """"Called when an IBB <open> element is received
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
157
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
158 @param iq_elt(domish.Element): the whole <iq> stanza
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
159 """
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
160 log.debug(_(u"IBB stream opening"))
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
161 iq_elt.handled = True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 open_elt = iq_elt.elements(NS_IBB, "open").next()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
163 block_size = open_elt.getAttribute("block-size")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
164 sid = open_elt.getAttribute("sid")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
165 stanza = open_elt.getAttribute("stanza", "iq")
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
166 if not sid or not block_size or int(block_size) > 65535:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
167 return self._sendError("not-acceptable", sid or None, iq_elt, client)
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
168 if not sid in client.xep_0047_current_stream:
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
169 log.warning(_(u"Ignoring unexpected IBB transfer: %s" % sid))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
170 return self._sendError("not-acceptable", sid or None, iq_elt, client)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
171 session_data = client.xep_0047_current_stream[sid]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
172 if session_data["to"] != jid.JID(iq_elt["from"]):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
174 _("sended jid inconsistency (man in the middle attack attempt ?)")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
176 return self._sendError("not-acceptable", sid, iq_elt, client)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
177
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
178 # at this stage, the session looks ok and will be accepted
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
179
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
180 # we reset the timeout:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
181 session_data["timer"].reset(TIMEOUT)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
182
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
183 # we save the xmlstream, events and observer data to allow observer removal
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
184 session_data["event_data"] = event_data = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
185 IBB_MESSAGE_DATA if stanza == "message" else IBB_IQ_DATA
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
186 ).format(sid)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
187 session_data["observer_cb"] = observer_cb = self._onIBBData
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
188 event_close = IBB_CLOSE.format(sid)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
189 # we now set the stream observer to look after data packet
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
190 # FIXME: if we never get the events, the observers stay.
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
191 # would be better to have generic observer and check id once triggered
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
192 client.xmlstream.addObserver(event_data, observer_cb, client=client)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
193 client.xmlstream.addOnetimeObserver(event_close, self._onIBBClose, client=client)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
194 # finally, we send the accept stanza
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
195 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
196 client.send(iq_result_elt)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
197
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
198 def _onIBBClose(self, iq_elt, client):
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
199 """"Called when an IBB <close> element is received
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
200
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
201 @param iq_elt(domish.Element): the whole <iq> stanza
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
202 """
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
203 iq_elt.handled = True
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
204 log.debug(_("IBB stream closing"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
205 close_elt = iq_elt.elements(NS_IBB, "close").next()
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
206 # XXX: this observer is only triggered on valid sid, so we don't need to check it
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
207 sid = close_elt["sid"]
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
208
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
209 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
210 client.send(iq_result_elt)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
211 self._killSession(sid, client)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
212
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
213 def _onIBBData(self, element, client):
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
214 """Observer called on <iq> or <message> stanzas with data element
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
215
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
216 Manage the data elelement (check validity and write to the stream_object)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
217 @param element(domish.Element): <iq> or <message> stanza
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
218 """
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
219 element.handled = True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
220 data_elt = element.elements(NS_IBB, "data").next()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
221 sid = data_elt["sid"]
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
222
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
223 try:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
224 session_data = client.xep_0047_current_stream[sid]
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
225 except KeyError:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
226 log.warning(_(u"Received data for an unknown session id"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
227 return self._sendError("item-not-found", None, element, client)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
228
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
229 from_jid = session_data["to"]
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
230 stream_object = session_data["stream_object"]
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
231
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
232 if from_jid.full() != element["from"]:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
233 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
234 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
235 u"sended jid inconsistency (man in the middle attack attempt ?)\ninitial={initial}\ngiven={given}"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
236 ).format(initial=from_jid, given=element["from"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
237 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
238 if element.name == "iq":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
239 self._sendError("not-acceptable", sid, element, client)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
240 return
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
241
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
242 session_data["seq"] = (session_data["seq"] + 1) % 65535
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
243 if int(data_elt.getAttribute("seq", -1)) != session_data["seq"]:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
244 log.warning(_(u"Sequence error"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
245 if element.name == "iq":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
246 reason = "not-acceptable"
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
247 self._sendError(reason, sid, element, client)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
248 self.terminateStream(session_data, client, reason)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
249 return
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
250
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
251 # we reset the timeout:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
252 session_data["timer"].reset(TIMEOUT)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
253
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
254 # we can now decode the data
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
255 try:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
256 stream_object.write(base64.b64decode(str(data_elt)))
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
257 except TypeError:
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
258 # The base64 data is invalid
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
259 log.warning(_(u"Invalid base64 data"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
260 if element.name == "iq":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
261 self._sendError("not-acceptable", sid, element, client)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
262 self.terminateStream(session_data, client, reason)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
263 return
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
264
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
265 # we can now ack success
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
266 if element.name == "iq":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
267 iq_result_elt = xmlstream.toResponse(element, "result")
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
268 client.send(iq_result_elt)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
269
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
270 def _sendError(self, error_condition, sid, iq_elt, client):
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
271 """Send error stanza
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
272
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
273 @param error_condition: one of twisted.words.protocols.jabber.error.STANZA_CONDITIONS keys
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
274 @param sid(unicode,None): jingle session id, or None, if session must not be destroyed
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
275 @param iq_elt(domish.Element): full <iq> stanza
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
276 @param client: %(doc_client)s
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
277 """
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
278 iq_elt = error.StanzaError(error_condition).toResponse(iq_elt)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
279 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
280 u"Error while managing in-band bytestream session, cancelling: {}".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
281 error_condition
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
282 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
283 )
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
284 if sid is not None:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
285 self._killSession(sid, client, error_condition)
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
286 client.send(iq_elt)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
287
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
288 def startStream(self, client, stream_object, to_jid, sid, block_size=None):
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
289 """Launch the stream workflow
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
290
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
291 @param stream_object(ifaces.IStreamProducer): stream object to send
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
292 @param to_jid(jid.JID): JID of the recipient
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
293 @param sid(unicode): Stream session id
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
294 @param block_size(int, None): size of the block (or None for default)
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
295 """
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
296 session_data = self._createSession(client, stream_object, to_jid, sid)
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
297
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
298 if block_size is None:
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
299 block_size = XEP_0047.BLOCK_SIZE
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
300 assert block_size <= 65535
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
301 session_data["block_size"] = block_size
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
302
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
303 iq_elt = client.IQ()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
304 iq_elt["to"] = to_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
305 open_elt = iq_elt.addElement((NS_IBB, "open"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
306 open_elt["block-size"] = str(block_size)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
307 open_elt["sid"] = sid
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
308 open_elt["stanza"] = "iq" # TODO: manage <message> stanza ?
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
309 args = [session_data, client]
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
310 d = iq_elt.send()
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
311 d.addCallbacks(self._IQDataStreamCb, self._IQDataStreamEb, args, None, args)
1558
6a8dd91476f0 plugin XEP-0047: minor improvements
Goffi <goffi@goffi.org>
parents: 1524
diff changeset
312 return session_data[DEFER_KEY]
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
313
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
314 def _IQDataStreamCb(self, iq_elt, session_data, client):
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
315 """Called during the whole data streaming
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
316
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
317 @param iq_elt(domish.Element): iq result
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
318 @param session_data(dict): data of this streaming session
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
319 @param client: %(doc_client)s
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
320 """
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
321 session_data["timer"].reset(TIMEOUT)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
322
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
323 buffer_ = session_data["stream_object"].read(session_data["block_size"])
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
324 if buffer_:
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
325 next_iq_elt = client.IQ()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
326 next_iq_elt["to"] = session_data["to"].full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
327 data_elt = next_iq_elt.addElement((NS_IBB, "data"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
328 seq = session_data["seq"] = (session_data["seq"] + 1) % 65535
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
329 data_elt["seq"] = unicode(seq)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
330 data_elt["sid"] = session_data["id"]
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
331 data_elt.addContent(base64.b64encode(buffer_))
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
332 args = [session_data, client]
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
333 d = next_iq_elt.send()
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
334 d.addCallbacks(self._IQDataStreamCb, self._IQDataStreamEb, args, None, args)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
335 else:
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
336 self.terminateStream(session_data, client)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
337
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
338 def _IQDataStreamEb(self, failure, session_data, client):
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
339 if failure.check(error.StanzaError):
1583
d46aae87c03a plugins XEP-0047, XEP-0065, XEP-0096: fixed use of failure.condition insteand of failure.value + removed gettext use for local logs
Goffi <goffi@goffi.org>
parents: 1578
diff changeset
340 log.warning(u"IBB transfer failed: {}".format(failure.value))
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
341 else:
1583
d46aae87c03a plugins XEP-0047, XEP-0065, XEP-0096: fixed use of failure.condition insteand of failure.value + removed gettext use for local logs
Goffi <goffi@goffi.org>
parents: 1578
diff changeset
342 log.error(u"IBB transfer failed: {}".format(failure.value))
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
343 self.terminateStream(session_data, client, "IQ_ERROR")
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
344
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
345 def terminateStream(self, session_data, client, failure_reason=None):
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
346 """Terminate the stream session
1524
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
347
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
348 @param session_data(dict): data of this streaming session
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
349 @param client: %(doc_client)s
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
350 @param failure_reason(unicode, None): reason of the failure, or None if steam was successful
7b0fcefd52d4 plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
351 """
1578
7fef6cdf5953 plugin XEP-0047: some cleaning
Goffi <goffi@goffi.org>
parents: 1558
diff changeset
352 iq_elt = client.IQ()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
353 iq_elt["to"] = session_data["to"].full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
354 close_elt = iq_elt.addElement((NS_IBB, "close"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
355 close_elt["sid"] = session_data["id"]
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
356 iq_elt.send()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
357 self._killSession(session_data["id"], client, failure_reason)
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
358
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
359
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
360 class XEP_0047_handler(XMPPHandler):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
361 implements(iwokkel.IDisco)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
362
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
363 def __init__(self, parent):
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
364 self.plugin_parent = parent
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
365
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
366 def connectionInitialized(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
367 self.xmlstream.addObserver(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
368 IBB_OPEN, self.plugin_parent._onIBBOpen, client=self.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
369 )
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
370
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
371 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
372 return [disco.DiscoFeature(NS_IBB)]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
373
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
374 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
375 return []