summaryrefslogtreecommitdiffstats
blob: a88a5b2d97cb200d14936fce024138790c91bc74 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * 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/JingleParser.h>

#include <boost/intrusive_ptr.hpp>

#include <Swiften/Base/Log.h>
#include <Swiften/Elements/JingleContentPayload.h>
#include <Swiften/Elements/JingleFileTransferHash.h>
#include <Swiften/Parser/PayloadParserFactory.h>

namespace Swift {

    JingleParser::JingleParser(PayloadParserFactoryCollection* factories) : factories(factories), level(0) {

    }

    void JingleParser::handleStartElement(const std::string& element, const std::string &ns, const AttributeMap& attributes) {
        if (level == 0) {
            // <jingle > tag
            JinglePayload::ref payload = getPayloadInternal();
            payload->setAction(stringToAction(attributes.getAttributeValue("action").get_value_or("")));
            payload->setInitiator(JID(attributes.getAttributeValue("initiator").get_value_or("")));
            payload->setResponder(JID(attributes.getAttributeValue("responder").get_value_or("")));
            payload->setSessionID(attributes.getAttributeValue("sid").get_value_or(""));
        }

        if (level == 1) {
            PayloadParserFactory* payloadParserFactory = factories->getPayloadParserFactory(element, ns, attributes);
            if (payloadParserFactory) {
                currentPayloadParser.reset(payloadParserFactory->createPayloadParser());
            }
        }

        if (level >= 1 && currentPayloadParser) {
            currentPayloadParser->handleStartElement(element, ns, attributes);
        }

        ++level;
    }

    void JingleParser::handleEndElement(const std::string& element, const std::string &ns) {
        --level;
        if (currentPayloadParser) {
            if (level >= 1) {
                currentPayloadParser->handleEndElement(element, ns);
            }

            if (level == 1) {
                std::shared_ptr<JinglePayload::Reason> reason = std::dynamic_pointer_cast<JinglePayload::Reason>(currentPayloadParser->getPayload());
                if (reason) {
                    getPayloadInternal()->setReason(*reason);
                }

                std::shared_ptr<JingleContentPayload> payload = std::dynamic_pointer_cast<JingleContentPayload>(currentPayloadParser->getPayload());
                if (payload) {
                    getPayloadInternal()->addContent(payload);
                }

                std::shared_ptr<JingleFileTransferHash> hash = std::dynamic_pointer_cast<JingleFileTransferHash>(currentPayloadParser->getPayload());
                if (hash) {
                    getPayloadInternal()->addPayload(hash);
                }
            }
        }
    }

    void JingleParser::handleCharacterData(const std::string& data) {
        if (level > 1 && currentPayloadParser) {
            currentPayloadParser->handleCharacterData(data);
        }
    }

    JinglePayload::Action JingleParser::stringToAction(const std::string &str) const {
        if (str == "content-accept") {
            return JinglePayload::ContentAccept;
        } else if (str == "content-add") {
            return JinglePayload::ContentAdd;
        } else if (str == "content-modify") {
            return JinglePayload::ContentModify;
        } else if (str == "content-reject") {
            return JinglePayload::ContentReject;
        } else if (str == "content-remove") {
            return JinglePayload::ContentRemove;
        } else if (str == "description-info") {
            return JinglePayload::DescriptionInfo;
        } else if (str == "security-info") {
            return JinglePayload::SecurityInfo;
        } else if (str == "session-accept") {
            return JinglePayload::SessionAccept;
        } else if (str == "session-info") {
            return JinglePayload::SessionInfo;
        } else if (str == "session-initiate") {
            return JinglePayload::SessionInitiate;
        } else if (str == "session-terminate") {
            return JinglePayload::SessionTerminate;
        } else if (str == "transport-accept") {
            return JinglePayload::TransportAccept;
        } else if (str == "transport-info") {
            return JinglePayload::TransportInfo;
        } else if (str == "transport-reject") {
            return JinglePayload::TransportReject;
        } else if (str == "transport-replace") {
            return JinglePayload::TransportReplace;
        } else {
            return JinglePayload::UnknownAction;
        }

    }
}