1# Copyright (C) 2020 Philipp Hörist <philipp AT hoerist.com>
2#
3# This file is part of nbxmpp.
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License
7# as published by the Free Software Foundation; either version 3
8# of the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; If not, see <http://www.gnu.org/licenses/>.
17
18from urllib.parse import urlparse
19from urllib.parse import unquote
20
21from nbxmpp.structs import CommonResult
22from nbxmpp.errors import StanzaError
23from nbxmpp.errors import is_error
24from nbxmpp.simplexml import Node
25
26
27def process_response(response):
28    if response.isError():
29        raise StanzaError(response)
30
31    return CommonResult(jid=response.getFrom())
32
33
34def raise_if_error(result):
35    if is_error(result):
36        raise result
37
38
39def finalize(task, result):
40    if is_error(result):
41        raise result
42    if isinstance(result, Node):
43        return task.set_result(result)
44    return result
45
46
47def parse_xmpp_uri(uri):
48    url = urlparse(uri)
49    if url.scheme != 'xmpp':
50        raise ValueError('not a xmpp uri')
51
52    if not ';' in url.query:
53        return (url.path, url.query, {})
54
55    action, query = url.query.split(';', 1)
56    key_value_pairs = query.split(';')
57
58    dict_ = {}
59    for key_value in key_value_pairs:
60        key, value = key_value.split('=')
61        dict_[key] = unquote(value)
62
63    return (url.path, action, dict_)
64