summaryrefslogtreecommitdiffstats
blob: 0c09c44b392224b96cdd262220b4709df1de8b5a (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
/*
 * Copyright (c) 2017 Tarun Gupta
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

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

#include <boost/optional.hpp>

#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Parser/PayloadParsers/MIXSubscribeParser.h>
#include <Swiften/Parser/PayloadParsers/FormParser.h>

using namespace Swift;

MIXJoinParser::MIXJoinParser() : level_(0) {
}

MIXJoinParser::~MIXJoinParser() {
}

void MIXJoinParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
    if (level_ == 0) {
        if (boost::optional<std::string> attributeValue = attributes.getAttributeValue("channel")) {
            if (boost::optional<JID> jid = JID::parse(*attributeValue)) {
                getPayloadInternal()->setChannel(*jid);
            }
        }
        if (boost::optional<std::string> attributeValue = attributes.getAttributeValue("jid")) {
            if (boost::optional<JID> jid = JID::parse(*attributeValue)) {
                getPayloadInternal()->setJID(*jid);
            }
        }
    }

    if (level_ == 1) {
        if (element == "subscribe" && ns == "urn:xmpp:mix:0") {
            currentPayloadParser_ = std::make_shared<MIXSubscribeParser>();
        }
        if (element == "x" && ns == "jabber:x:data") {
            currentPayloadParser_ = std::make_shared<FormParser>();
        }
    }

    if (level_ >= 1 && currentPayloadParser_) {
        currentPayloadParser_->handleStartElement(element, ns, attributes);
    }
    ++level_;
}

void MIXJoinParser::handleEndElement(const std::string& element, const std::string& ns) {
    --level_;
    if (currentPayloadParser_) {
        if (level_ >= 1) {
            currentPayloadParser_->handleEndElement(element, ns);
        }

        if (level_ == 1) {
            if (element == "subscribe" && ns == "urn:xmpp:mix:0") {
                getPayloadInternal()->addSubscription(std::dynamic_pointer_cast<MIXSubscribe>(currentPayloadParser_->getPayload()));
            }
            if (element == "x" && ns == "jabber:x:data") {
                getPayloadInternal()->setForm(std::dynamic_pointer_cast<Form>(currentPayloadParser_->getPayload()));
            }
            currentPayloadParser_.reset();
        }
    }
}

void MIXJoinParser::handleCharacterData(const std::string& data) {
    if (level_ > 1 && currentPayloadParser_) {
        currentPayloadParser_->handleCharacterData(data);
    }
}