summaryrefslogtreecommitdiffstats
blob: 502f40091d405d4ae352d80fd51d8da524d9b2ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2015-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h>

#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>

namespace Swift {

S5BProxyRequestParser::S5BProxyRequestParser() : parseActivate(false) {
}

S5BProxyRequestParser::~S5BProxyRequestParser() {
}

void S5BProxyRequestParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) {
    if (element == "streamhost") {
        if (attributes.getAttributeValue("host") && attributes.getAttributeValue("jid") && attributes.getAttributeValue("port")) {
            std::string host = attributes.getAttributeValue("host").get_value_or("");
            int port = -1;
            JID jid = attributes.getAttributeValue("jid").get_value_or("");

            try {
                port = boost::lexical_cast<int>(attributes.getAttributeValue("port").get());
            } catch (boost::bad_lexical_cast &) {
                port = -1;
            }
            if (!host.empty() && port != -1 && jid.isValid()) {
                S5BProxyRequest::StreamHost streamHost;
                streamHost.host = host;
                streamHost.port = port;
                streamHost.jid = jid;
                getPayloadInternal()->setStreamHost(streamHost);
            }
        }
    } else if (element == "activate") {
        parseActivate = true;
    } else if (element == "query") {
        if (attributes.getAttributeValue("sid")) {
            getPayloadInternal()->setSID(attributes.getAttributeValue("sid").get());
        }
    }
}

void S5BProxyRequestParser::handleEndElement(const std::string& element, const std::string&) {
    if (element == "activate") {
        JID activate = JID(activateJID);
        if (activate.isValid()) {
            getPayloadInternal()->setActivate(activate);
        }
        parseActivate = false;
    }
}

void S5BProxyRequestParser::handleCharacterData(const std::string& data) {
    if (parseActivate) {
        activateJID = activateJID + data;
    }
}

}