From 60b15814caa60b555d75848e921df2525c51d766 Mon Sep 17 00:00:00 2001 From: Tobias Markmann Date: Thu, 18 Dec 2014 16:24:07 +0100 Subject: Add example showing how to login, list rooms at a MUC service, join the first result and show incoming messages. Test-Information: Tested on OS X 10.9.5 with a Prosody MUC service. Change-Id: If64e80bf4fc70366fabb3fd86d6da67a6f307280 diff --git a/Swiften/Examples/MUCListAndJoin/.gitignore b/Swiften/Examples/MUCListAndJoin/.gitignore new file mode 100644 index 0000000..85cf8db --- /dev/null +++ b/Swiften/Examples/MUCListAndJoin/.gitignore @@ -0,0 +1 @@ +MUCListAndJoin diff --git a/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp b/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp new file mode 100644 index 0000000..e7c7f1f --- /dev/null +++ b/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace Swift; +using namespace std; + +static SimpleEventLoop eventLoop; +static BoostNetworkFactories networkFactories(&eventLoop); + +static boost::shared_ptr client; +static MUC::ref muc; +static JID mucJID; +static JID roomJID; + +static void joinMUC() { + cout << "Joining " << roomJID.toString() << endl; + + muc = client->getMUCManager()->createMUC(roomJID); + muc->joinAs("SwiftExample"); +} + +static void handleRoomsItemsResponse(boost::shared_ptr items, ErrorPayload::ref error) { + if (error) { + cout << "Error fetching list of rooms." << endl; + return; + } + + int roomNr = 0; + cout << "List of rooms at " << mucJID.toString() << endl; + foreach (DiscoItems::Item item, items->getItems()) { + roomNr++; + cout << "\t" << roomNr << ". " << item.getJID().getNode() << " - " << item.getName() << std::endl; + if (roomNr == 1) { + roomJID = item.getJID(); + } + } + cout << endl; + joinMUC(); +} + +static void handleConnected() { + cout << "Connected." << endl; + // search for MUC rooms + + GetDiscoItemsRequest::ref discoItemsRequest = GetDiscoItemsRequest::create(mucJID, client->getIQRouter()); + discoItemsRequest->onResponse.connect(&handleRoomsItemsResponse); + + cout << "Request list of rooms." << endl; + discoItemsRequest->send(); +} + +static void handleDisconnected(const boost::optional&) { + cout << "Disconnected." << endl; +} + +static void handleIncomingMessage(boost::shared_ptr message) { + if (message->getFrom().toBare() == roomJID) { + cout << "[ " << roomJID << " ] " << message->getFrom().getResource() << ": " << message->getBody() << endl; + } +} + +/* + * Usage: ./MUCListAndJoin + */ +int main(int argc, char* argv[]) { + int ret = 0; + + if (argc != 4) { + cout << "Usage: ./" << argv[0] << " " << endl; + ret = -1; + } + else { + mucJID = JID(argv[3]); + client = boost::make_shared(JID(argv[1]), string(argv[2]), &networkFactories); + client->setAlwaysTrustCertificates(); + + // Enable the following line for detailed XML logging + // ClientXMLTracer* tracer = new ClientXMLTracer(client.get()); + + client->onConnected.connect(&handleConnected); + client->onDisconnected.connect(&handleDisconnected); + client->onMessageReceived.connect(&handleIncomingMessage); + + cout << "Connecting..." << flush; + client->connect(); + { + Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); + timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); + + Timer::ref disconnectTimer = networkFactories.getTimerFactory()->createTimer(25000); + disconnectTimer->onTick.connect(boost::bind(&Client::disconnect, client.get())); + + timer->start(); + disconnectTimer->start(); + + eventLoop.run(); + } + } + return ret; +} diff --git a/Swiften/Examples/MUCListAndJoin/SConscript b/Swiften/Examples/MUCListAndJoin/SConscript new file mode 100644 index 0000000..73cc372 --- /dev/null +++ b/Swiften/Examples/MUCListAndJoin/SConscript @@ -0,0 +1,7 @@ +Import("env") + +myenv = env.Clone() +myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) +myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) + +tester = myenv.Program("MUCListAndJoin", ["MUCListAndJoin.cpp"]) diff --git a/Swiften/Examples/SConscript b/Swiften/Examples/SConscript index fb568fc..8a0a48f 100644 --- a/Swiften/Examples/SConscript +++ b/Swiften/Examples/SConscript @@ -10,4 +10,5 @@ SConscript(dirs = [ "NetworkTool", "ParserTester", "BenchTool", + "MUCListAndJoin", ]) -- cgit v0.10.2-6-g49f6