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

#include <Sluift/ElementConvertors/PresenceConvertor.h>

#include <boost/smart_ptr/make_shared.hpp>

#include <lua.hpp>

#include <Sluift/LuaElementConvertors.h>

using namespace Swift;

PresenceConvertor::PresenceConvertor(LuaElementConvertors* convertors) :
        StanzaConvertor<Presence>("presence"),
        convertors(convertors) {
}

PresenceConvertor::~PresenceConvertor() {
}

boost::shared_ptr<Presence> PresenceConvertor::doConvertFromLua(lua_State* L) {
    boost::shared_ptr<Presence> result = getStanza(L, convertors);
    lua_getfield(L, -1, "type");
    if (lua_isstring(L, -1)) {
        result->setType(convertPresenceTypeFromString(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    return result;
}

void PresenceConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<Presence> stanza) {
    pushStanza(L, stanza, convertors);
    const std::string type = convertPresenceTypeToString(stanza->getType());
    lua_pushstring(L, type.c_str());
    lua_setfield(L, -2, "type");
}

boost::optional<LuaElementConvertor::Documentation> PresenceConvertor::getDocumentation() const {
    return Documentation(
        "Presence",
        "This table has the following fields:\n\n"
        "- `type`: string\n"
        "- `id`: string\n"
        "- `from`: string\n"
        "- `to`: string\n"
        "- `payloads`: array<@{Payload}>\n"
    );
}

std::string PresenceConvertor::convertPresenceTypeToString(Presence::Type type) {
    switch (type) {
        case Presence::Available: return "available";
        case Presence::Error: return "error";
        case Presence::Probe: return "probe";
        case Presence::Subscribe: return "subscribe";
        case Presence::Subscribed: return "subscribed";
        case Presence::Unavailable: return "unavailable";
        case Presence::Unsubscribe: return "unsubscribe";
        case Presence::Unsubscribed: return "unsubscribed";
    }
    assert(false);
    return "";
}

Presence::Type PresenceConvertor::convertPresenceTypeFromString(const std::string& type) {
    if (type == "available") {
        return Presence::Available;
    }
    else if (type == "error") {
        return Presence::Error;
    }
    else if (type == "probe") {
        return Presence::Probe;
    }
    else if (type == "subscribe") {
        return Presence::Subscribe;
    }
    else if (type == "subscribed") {
        return Presence::Subscribed;
    }
    else if (type == "unavailable") {
        return Presence::Unavailable;
    }
    else if (type == "unsubscribe") {
        return Presence::Unsubscribe;
    }
    else if (type == "unsubscribed") {
        return Presence::Unsubscribed;
    }
    else {
        throw Lua::Exception("Illegal presence type: '" + type + "'");
    }
}