diff options
Diffstat (limited to 'Sluift/client.cpp')
-rw-r--r-- | Sluift/client.cpp | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 186effc..53e253f 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -1,32 +1,32 @@ /* - * Copyright (c) 2013-2016 Isode Limited. + * Copyright (c) 2013-2017 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <boost/assign/list_of.hpp> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #include <Swiften/Base/IDGenerator.h> #include <Swiften/Disco/ClientDiscoManager.h> #include <Swiften/Elements/DiscoInfo.h> #include <Swiften/Elements/Message.h> #include <Swiften/Elements/Presence.h> #include <Swiften/Elements/RawXMLPayload.h> #include <Swiften/Elements/RosterItemPayload.h> #include <Swiften/Elements/RosterPayload.h> #include <Swiften/Elements/SoftwareVersion.h> #include <Swiften/JID/JID.h> #include <Swiften/Presence/PresenceSender.h> #include <Swiften/Presence/SubscriptionManager.h> #include <Swiften/Queries/GenericRequest.h> #include <Swiften/Queries/IQRouter.h> #include <Swiften/Queries/Requests/GetSoftwareVersionRequest.h> #include <Swiften/Roster/SetRosterRequest.h> #include <Swiften/Roster/XMPPRoster.h> #include <Swiften/Roster/XMPPRosterItem.h> #include <Swiften/TLS/PKCS12Certificate.h> #include <Sluift/ElementConvertors/IQConvertor.h> #include <Sluift/ElementConvertors/MessageConvertor.h> @@ -179,60 +179,78 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( "Returns a table of all the contacts in the contact list.", "self\n", "" ) { Sluift::globals.eventLoop.runOnce(); SluiftClient* client = getClient(L); Lua::Table contactsTable; for (const auto& item : client->getRoster(getGlobalTimeout(L))) { std::string subscription; switch(item.getSubscription()) { case RosterItemPayload::None: subscription = "none"; break; case RosterItemPayload::To: subscription = "to"; break; case RosterItemPayload::From: subscription = "from"; break; case RosterItemPayload::Both: subscription = "both"; break; case RosterItemPayload::Remove: subscription = "remove"; break; } Lua::Table itemTable = boost::assign::map_list_of ("jid", std::make_shared<Lua::Value>(item.getJID().toString())) ("name", std::make_shared<Lua::Value>(item.getName())) ("subscription", std::make_shared<Lua::Value>(subscription)) ("groups", std::make_shared<Lua::Value>(std::vector<Lua::Value>(item.getGroups().begin(), item.getGroups().end()))); contactsTable[item.getJID().toString()] = std::make_shared<Lua::Value>(itemTable); } pushValue(L, contactsTable); Lua::registerTableToString(L, -1); return 1; } SLUIFT_LUA_FUNCTION_WITH_HELP( + Client, get_block_list, + "Returns a table of all the items in the blocking list.", + "self\n", + "" +) { + Sluift::globals.eventLoop.runOnce(); + SluiftClient* client = getClient(L); + lua_newtable(L); + int i = 0; + for (const auto& item : client->getBlockList(getGlobalTimeout(L))) { + lua_pushstring(L, item.toString().c_str()); + lua_rawseti(L, -2, boost::numeric_cast<int>(++i)); + } + Lua::registerTableToString(L, -1); + return 1; +} + +SLUIFT_LUA_FUNCTION_WITH_HELP( Client, send_message, "Send a message.", "self\n" "to the JID to send the message to\n" "body the body of the message. Can alternatively be specified using the `body` option\n", "to the JID to send the message to\n" "body the body of the message\n" "subject the subject of the MUC room to set\n" "type the type of message to send (`normal`, `chat`, `error`, `groupchat`, `headline`)\n" "payloads payloads to add to the message\n" ) { Sluift::globals.eventLoop.runOnce(); JID to; boost::optional<std::string> body; boost::optional<std::string> subject; std::vector<std::shared_ptr<Payload> > payloads; int index = 2; Message::Type type = Message::Chat; if (lua_isstring(L, index)) { to = std::string(lua_tostring(L, index)); ++index; if (lua_isstring(L, index)) { body = lua_tostring(L, index); ++index; } } if (lua_istable(L, index)) { if (boost::optional<std::string> value = Lua::getStringField(L, index, "to")) { to = *value; @@ -491,60 +509,77 @@ static void pushEvent(lua_State* L, const SluiftClient::Event& event) { Lua::pushValue(L, result); addPayloadsToTable(L, message->getPayloads()); Lua::registerTableToString(L, -1); break; } case SluiftClient::Event::PresenceType: { Presence::ref presence = std::dynamic_pointer_cast<Presence>(event.stanza); Lua::Table result = boost::assign::map_list_of ("type", std::make_shared<Lua::Value>(std::string("presence"))) ("from", std::make_shared<Lua::Value>(presence->getFrom().toString())) ("to", std::make_shared<Lua::Value>(presence->getTo().toString())) ("status", std::make_shared<Lua::Value>(presence->getStatus())) ("presence_type", std::make_shared<Lua::Value>(PresenceConvertor::convertPresenceTypeToString(presence->getType()))); Lua::pushValue(L, result); addPayloadsToTable(L, presence->getPayloads()); Lua::registerTableToString(L, -1); break; } case SluiftClient::Event::PubSubEventType: { Sluift::globals.elementConvertor.convertToLua(L, event.pubsubEvent); lua_pushstring(L, "pubsub"); lua_setfield(L, -2, "type"); lua_pushstring(L, event.from.toString().c_str()); lua_setfield(L, -2, "from"); lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); lua_getfield(L, -1, "process_pubsub_event"); lua_pushvalue(L, -3); lua_call(L, 1, 0); lua_pop(L, 1); + break; + } + case SluiftClient::Event::BlockEventType: { + Lua::Table result = boost::assign::map_list_of + ("type", std::make_shared<Lua::Value>(std::string("block"))) + ("jid", std::make_shared<Lua::Value>(event.item.toString())); + Lua::pushValue(L, result); + Lua::registerTableToString(L, -1); + break; + } + case SluiftClient::Event::UnblockEventType: { + Lua::Table result = boost::assign::map_list_of + ("type", std::make_shared<Lua::Value>(std::string("unblock"))) + ("jid", std::make_shared<Lua::Value>(event.item.toString())); + Lua::pushValue(L, result); + Lua::registerTableToString(L, -1); + break; } } } struct CallUnaryLuaPredicateOnEvent { CallUnaryLuaPredicateOnEvent(lua_State* L, int index) : L(L), index(index) { } bool operator()(const SluiftClient::Event& event) { lua_pushvalue(L, index); pushEvent(L, event); if (lua_pcall(L, 1, 1, 0) != 0) { throw Lua::Exception(lua_tostring(L, -1)); } bool result = lua_toboolean(L, -1); lua_pop(L, 1); return result; } lua_State* L; int index; }; SLUIFT_LUA_FUNCTION(Client, get_next_event) { Sluift::globals.eventLoop.runOnce(); SluiftClient* client = getClient(L); int timeout = getGlobalTimeout(L); boost::optional<SluiftClient::Event::Type> type; |