diff options
Diffstat (limited to 'Sluift/client.cpp')
-rw-r--r-- | Sluift/client.cpp | 53 |
1 files changed, 50 insertions, 3 deletions
diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 6d8bee8..df43075 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -5,4 +5,9 @@ */ +#include <boost/lambda/lambda.hpp> +#include <boost/lambda/bind.hpp> +#include <boost/assign/list_of.hpp> +#include <iostream> + #include <Sluift/SluiftClient.h> #include <Swiften/JID/JID.h> @@ -21,5 +26,4 @@ #include <Swiften/Presence/SubscriptionManager.h> #include <Swiften/Roster/XMPPRosterItem.h> -#include <boost/assign/list_of.hpp> #include <Sluift/Watchdog.h> #include <Swiften/Queries/Requests/GetSoftwareVersionRequest.h> @@ -31,7 +35,7 @@ #include <Sluift/Lua/LuaUtils.h> #include <Sluift/globals.h> -#include <iostream> using namespace Swift; +namespace lambda = boost::lambda; static const std::string SLUIFT_CLIENT = Lua::FunctionRegistry::getMetaTableNameForType("Client"); @@ -377,4 +381,24 @@ static void pushEvent(lua_State* L, const SluiftClient::Event& event) { } +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(); @@ -383,4 +407,5 @@ SLUIFT_LUA_FUNCTION(Client, get_next_event) { int timeout = Sluift::globals.timeout; boost::optional<SluiftClient::Event::Type> type; + int condition = 0; if (lua_istable(L, 2)) { if (boost::optional<std::string> typeString = Lua::getStringField(L, 2, "type")) { @@ -398,7 +423,23 @@ SLUIFT_LUA_FUNCTION(Client, get_next_event) { timeout = *timeoutInt; } + lua_getfield(L, 2, "if"); + if (lua_isfunction(L, -1)) { + condition = Lua::absoluteOffset(L, -1); + } } - if (boost::optional<SluiftClient::Event> event = client->getNextEvent(type, timeout)) { + boost::optional<SluiftClient::Event> event; + if (condition) { + event = client->getNextEvent(timeout, CallUnaryLuaPredicateOnEvent(L, condition)); + } + else if (type) { + event = client->getNextEvent( + timeout, lambda::bind(&SluiftClient::Event::type, lambda::_1) == *type); + } + else { + event = client->getNextEvent(timeout); + } + + if (event) { pushEvent(L, *event); } @@ -511,4 +552,10 @@ SLUIFT_LUA_FUNCTION(Client, set_caps_node) { } +SLUIFT_LUA_FUNCTION(Client, jid) { + SluiftClient* client = getClient(L); + lua_pushstring(L, client->getClient()->getJID().toString().c_str()); + return 1; +} + SLUIFT_LUA_FUNCTION(Client, __gc) { SluiftClient* client = getClient(L); |