diff options
| -rw-r--r-- | Sluift/SluiftClient.cpp | 35 | ||||
| -rw-r--r-- | Sluift/SluiftClient.h | 16 | ||||
| -rw-r--r-- | Sluift/client.cpp | 37 | 
3 files changed, 83 insertions, 5 deletions
| diff --git a/Sluift/SluiftClient.cpp b/Sluift/SluiftClient.cpp index f1c0191..4680d4b 100644 --- a/Sluift/SluiftClient.cpp +++ b/Sluift/SluiftClient.cpp @@ -1,5 +1,5 @@  /* - * Copyright (c) 2013-2016 Isode Limited. + * Copyright (c) 2013-2017 Isode Limited.   * All rights reserved.   * See the COPYING file for more information.   */ @@ -9,6 +9,7 @@  #include <boost/numeric/conversion/cast.hpp>  #include <Swiften/Client/Client.h> +#include <Swiften/Client/ClientBlockListManager.h>  #include <Swiften/Client/ClientXMLTracer.h>  #include <Swiften/Elements/Message.h>  #include <Swiften/Elements/Presence.h> @@ -37,6 +38,8 @@ SluiftClient::SluiftClient(      client->onPresenceReceived.connect(boost::bind(&SluiftClient::handleIncomingPresence, this, _1));      client->getPubSubManager()->onEvent.connect(boost::bind(&SluiftClient::handleIncomingPubSubEvent, this, _1, _2));      client->getRoster()->onInitialRosterPopulated.connect(boost::bind(&SluiftClient::handleInitialRosterPopulated, this)); +    client->getClientBlockListManager()->getBlockList()->onItemAdded.connect(boost::bind(&SluiftClient::handleIncomingBlockEvent, this, _1)); +    client->getClientBlockListManager()->getBlockList()->onItemRemoved.connect(boost::bind(&SluiftClient::handleIncomingUnblockEvent, this, _1));  }  SluiftClient::~SluiftClient() { @@ -46,12 +49,14 @@ SluiftClient::~SluiftClient() {  void SluiftClient::connect() {      rosterReceived = false; +    blockListReceived = false;      disconnectedError = boost::optional<ClientError>();      client->connect(options);  }  void SluiftClient::connect(const std::string& host, int port) {      rosterReceived = false; +    blockListReceived = false;      options.manualHostname = host;      options.manualPort = port;      disconnectedError = boost::optional<ClientError>(); @@ -126,6 +131,26 @@ boost::optional<SluiftClient::Event> SluiftClient::getNextEvent(      }  } +std::vector<JID> SluiftClient::getBlockList(int timeout) { +    Watchdog watchdog(timeout, networkFactories->getTimerFactory()); +    if (!blockListReceived) { +        // If we haven't requested it yet, request it for the first time +        client->getClientBlockListManager()->requestBlockList(); + +        // Wait for new events +        while (!watchdog.getTimedOut() && client->getClientBlockListManager()->getBlockList()->getState() != BlockList::Available) { +            eventLoop->runUntilEvents(); +        } + +        // Throw an error if we're timed out +        if (watchdog.getTimedOut()) { +            throw Lua::Exception("Timeout while requesting blocklist"); +        } +    } +    blockListReceived = true; +    return client->getClientBlockListManager()->getBlockList()->getItems(); +} +  std::vector<XMPPRosterItem> SluiftClient::getRoster(int timeout) {      Watchdog watchdog(timeout, networkFactories->getTimerFactory());      if (!rosterReceived) { @@ -161,6 +186,14 @@ void SluiftClient::handleIncomingPubSubEvent(const JID& from, std::shared_ptr<Pu      pendingEvents.push_back(Event(from, event));  } +void SluiftClient::handleIncomingBlockEvent(const JID& item) { +    pendingEvents.push_back(Event(item, Event::BlockEventType)); +} + +void SluiftClient::handleIncomingUnblockEvent(const JID& item) { +    pendingEvents.push_back(Event(item, Event::UnblockEventType)); +} +  void SluiftClient::handleInitialRosterPopulated() {      rosterReceived = true;  } diff --git a/Sluift/SluiftClient.h b/Sluift/SluiftClient.h index 2c221e6..a48c681 100644 --- a/Sluift/SluiftClient.h +++ b/Sluift/SluiftClient.h @@ -1,5 +1,5 @@  /* - * Copyright (c) 2013-2016 Isode Limited. + * Copyright (c) 2013-2017 Isode Limited.   * All rights reserved.   * See the COPYING file for more information.   */ @@ -13,6 +13,7 @@  #include <boost/optional.hpp>  #include <Swiften/Client/Client.h> +#include <Swiften/Client/ClientBlockListManager.h>  #include <Swiften/Client/ClientError.h>  #include <Swiften/Client/ClientOptions.h>  #include <Swiften/Elements/IQ.h> @@ -42,13 +43,15 @@ namespace Swift {                  enum Type {                      MessageType,                      PresenceType, -                    PubSubEventType +                    PubSubEventType, +                    BlockEventType, +                    UnblockEventType                  };                  Event(std::shared_ptr<Message> stanza) : type(MessageType), stanza(stanza) {}                  Event(std::shared_ptr<Presence> stanza) : type(PresenceType), stanza(stanza) {}                  Event(const JID& from, std::shared_ptr<PubSubEventPayload> payload) : type(PubSubEventType), from(from), pubsubEvent(payload) {} - +                Event(const JID& item, Type type) : type(type), item(item) {}                  Type type;                  // Message & Presence @@ -57,6 +60,9 @@ namespace Swift {                  // PubSubEvent                  JID from;                  std::shared_ptr<PubSubEventPayload> pubsubEvent; + +                // Blocklist +                JID item;              };              SluiftClient( @@ -106,6 +112,7 @@ namespace Swift {              boost::optional<SluiftClient::Event> getNextEvent(int timeout,                      boost::function<bool (const Event&)> condition = 0);              std::vector<XMPPRosterItem> getRoster(int timeout); +            std::vector<JID> getBlockList(int timeout);          private:              Sluift::Response doSendRequest(std::shared_ptr<Request> request, int timeout); @@ -113,6 +120,8 @@ namespace Swift {              void handleIncomingMessage(std::shared_ptr<Message> stanza);              void handleIncomingPresence(std::shared_ptr<Presence> stanza);              void handleIncomingPubSubEvent(const JID& from, std::shared_ptr<PubSubEventPayload> event); +            void handleIncomingBlockEvent(const JID& item); +            void handleIncomingUnblockEvent(const JID& item);              void handleInitialRosterPopulated();              void handleRequestResponse(std::shared_ptr<Payload> response, std::shared_ptr<ErrorPayload> error);              void handleDisconnected(const boost::optional<ClientError>& error); @@ -124,6 +133,7 @@ namespace Swift {              ClientOptions options;              ClientXMLTracer* tracer;              bool rosterReceived = false; +            bool blockListReceived = false;              std::deque<Event> pendingEvents;              boost::optional<ClientError> disconnectedError;              bool requestResponseReceived = false; diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 186effc..53e253f 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -1,5 +1,5 @@  /* - * Copyright (c) 2013-2016 Isode Limited. + * Copyright (c) 2013-2017 Isode Limited.   * All rights reserved.   * See the COPYING file for more information.   */ @@ -206,6 +206,24 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(  }  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" @@ -518,6 +536,23 @@ static void pushEvent(lua_State* L, const SluiftClient::Event& 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;          }      }  } | 
 Swift
 Swift