summaryrefslogtreecommitdiffstats
blob: ce7467172cbf71fd58b439af475c37fb10c5b9d0 (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
/*
 * Copyright (c) 2011-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

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

#include <cassert>

#include <boost/lexical_cast.hpp>

#include <Swiften/Elements/MUCOccupant.h>

namespace Swift {

MUCItem MUCItemParser::itemFromTree(ParserElement::ref root) {
    MUCItem item;
    std::string affiliation = root->getAttributes().getAttribute("affiliation");
    std::string role = root->getAttributes().getAttribute("role");
    std::string nick = root->getAttributes().getAttribute("nick");
    std::string jid = root->getAttributes().getAttribute("jid");
    item.affiliation = parseAffiliation(affiliation);
    item.role = parseRole(role);
    if (!jid.empty()) {
        item.realJID = JID(jid);
    }
    if (!nick.empty()) {
        item.nick = nick;
    }
    std::string xmlns = root->getNamespace();
    std::string reason = root->getChild("reason", xmlns)->getText();
    std::string actor = root->getChild("actor", xmlns)->getAttributes().getAttribute("jid");
    if (!reason.empty()) {
        item.reason = reason;
    }
    if (!actor.empty()) {
        item.actor = JID(actor);
    }

    return item;
}

boost::optional<MUCOccupant::Role> MUCItemParser::parseRole(const std::string& roleString) {
    if (roleString == "moderator") {
        return MUCOccupant::Moderator;
    }
    if (roleString == "participant") {
        return MUCOccupant::Participant;
    }
    if (roleString == "visitor") {
        return MUCOccupant::Visitor;
    }
    if (roleString == "none") {
        return MUCOccupant::NoRole;
    }
    return boost::optional<MUCOccupant::Role>();
}

boost::optional<MUCOccupant::Affiliation> MUCItemParser::parseAffiliation(const std::string& affiliationString) {
    if (affiliationString == "owner") {
        return MUCOccupant::Owner;
    }
    if (affiliationString == "admin") {
        return MUCOccupant::Admin;
    }
    if (affiliationString == "member") {
        return MUCOccupant::Member;
    }
    if (affiliationString == "outcast") {
        return MUCOccupant::Outcast;
    }
    if (affiliationString == "none") {
        return MUCOccupant::NoAffiliation;
    }
    return boost::optional<MUCOccupant::Affiliation>();
}

}