summaryrefslogtreecommitdiffstats
blob: a5282118955681a9d86169b11899551be1adb5e8 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (c) 2013-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Sluift/ElementConvertors/DiscoInfoConvertor.h>

#include <memory>

#include <boost/numeric/conversion/cast.hpp>

#include <lua.hpp>

#include <Swiften/Elements/Form.h>
#include <Sluift/LuaElementConvertors.h>
#include <Sluift/Lua/LuaUtils.h>

using namespace Swift;

DiscoInfoConvertor::DiscoInfoConvertor(LuaElementConvertors* convertors) :
        GenericLuaElementConvertor<DiscoInfo>("disco_info"),
        convertors(convertors) {
}

DiscoInfoConvertor::~DiscoInfoConvertor() {
}

std::shared_ptr<DiscoInfo> DiscoInfoConvertor::doConvertFromLua(lua_State* L) {
    std::shared_ptr<DiscoInfo> result = std::make_shared<DiscoInfo>();
    if (boost::optional<std::string> value = Lua::getStringField(L, -1, "node")) {
        result->setNode(*value);
    }

    lua_getfield(L, -1, "identities");
    if (lua_istable(L, -1)) {
        for (lua_pushnil(L); lua_next(L, -2); ) {
            result->addIdentity(DiscoInfo::Identity(
                    Lua::getStringField(L, -1, "name").get_value_or(""),
                    Lua::getStringField(L, -1, "category").get_value_or("client"),
                    Lua::getStringField(L, -1, "type").get_value_or("pc"),
                    Lua::getStringField(L, -1, "language").get_value_or("")));
            lua_pop(L, 1);
        }
    }
    lua_pop(L, 1);

    lua_getfield(L, -1, "features");
    if (lua_istable(L, -1)) {
        for (lua_pushnil(L); lua_next(L, -2); ) {
            if (lua_isstring(L, -1)) {
                result->addFeature(lua_tostring(L, -1));
            }
            lua_pop(L, 1);
        }
    }
    lua_pop(L, 1);

    lua_getfield(L, -1, "extensions");
    if (lua_istable(L, -1)) {
        for (lua_pushnil(L); lua_next(L, -2); ) {
            std::shared_ptr<Form> form = std::dynamic_pointer_cast<Form>(convertors->convertFromLuaUntyped(L, -1, "form"));
            if (!!form) {
                result->addExtension(form);
            }
            lua_pop(L, 1);
        }
    }
    lua_pop(L, 1);

    return result;
}

void DiscoInfoConvertor::doConvertToLua(lua_State* L, std::shared_ptr<DiscoInfo> payload) {
    lua_newtable(L);
    if (!payload->getNode().empty()) {
        lua_pushstring(L, payload->getNode().c_str());
        lua_setfield(L, -2, "node");
    }

    const std::vector<DiscoInfo::Identity>& identities = payload->getIdentities();
    if (!identities.empty()) {
        lua_createtable(L, boost::numeric_cast<int>(identities.size()), 0);
        for (size_t i = 0; i < identities.size(); ++i) {
            lua_createtable(L, 0, 0);
            if (!identities[i].getName().empty()) {
                lua_pushstring(L, identities[i].getName().c_str());
                lua_setfield(L, -2, "name");
            }
            if (!identities[i].getCategory().empty()) {
                lua_pushstring(L, identities[i].getCategory().c_str());
                lua_setfield(L, -2, "category");
            }
            if (!identities[i].getType().empty()) {
                lua_pushstring(L, identities[i].getType().c_str());
                lua_setfield(L, -2, "type");
            }
            if (!identities[i].getLanguage().empty()) {
                lua_pushstring(L, identities[i].getLanguage().c_str());
                lua_setfield(L, -2, "language");
            }
            lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
        }
        lua_setfield(L, -2, "identities");
    }

    const std::vector<std::string>& features = payload->getFeatures();
    if (!features.empty()) {
        lua_createtable(L, boost::numeric_cast<int>(features.size()), 0);
        for (size_t i = 0; i < features.size(); ++i) {
            lua_pushstring(L, features[i].c_str());
            lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
        }
        lua_setfield(L, -2, "features");
    }

    const std::vector<Form::ref>& extensions = payload->getExtensions();
    if (!extensions.empty()) {
        lua_createtable(L, boost::numeric_cast<int>(extensions.size()), 0);
        for (size_t i = 0; i < extensions.size(); ++i) {
            if (convertors->convertToLuaUntyped(L, extensions[i]) > 0) {
                lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
            }
        }
        lua_setfield(L, -2, "extensions");
    }
}

boost::optional<LuaElementConvertor::Documentation> DiscoInfoConvertor::getDocumentation() const {
    return Documentation(
            "DiscoInfo",
            "Represents `disco#info` service discovery data.\n\n"
            "This table has the following structure:\n\n"
            "- `node`: string\n"
            "- `identities`: array(table)\n"
            "  - `name`: string\n"
            "  - `category`: string\n"
            "  - `type`: string\n"
            "  - `language`: string\n"
            "- `features`: array(string)\n"
            "- `extensions`: array(table)\n"
            "-   `form`: string @{Form} (Optional)\n"
    );
}