summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2014-12-18 15:24:07 (GMT)
committerTobias Markmann <tm@ayena.de>2014-12-18 15:24:59 (GMT)
commit60b15814caa60b555d75848e921df2525c51d766 (patch)
treec6aef1eaa6aef5061655eaa888afbab1082ca218 /Swiften/Examples
parentb26bad1500ffc22e6fc36f316d3eb0d0ed1e869f (diff)
downloadswift-60b15814caa60b555d75848e921df2525c51d766.zip
swift-60b15814caa60b555d75848e921df2525c51d766.tar.bz2
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
Diffstat (limited to 'Swiften/Examples')
-rw-r--r--Swiften/Examples/MUCListAndJoin/.gitignore1
-rw-r--r--Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp120
-rw-r--r--Swiften/Examples/MUCListAndJoin/SConscript7
-rw-r--r--Swiften/Examples/SConscript1
4 files changed, 129 insertions, 0 deletions
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 <iostream>
+
+#include <boost/optional.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include <Swiften/Base/foreach.h>
+#include <Swiften/Client/Client.h>
+#include <Swiften/Client/ClientXMLTracer.h>
+#include <Swiften/Disco/GetDiscoItemsRequest.h>
+#include <Swiften/EventLoop/SimpleEventLoop.h>
+#include <Swiften/JID/JID.h>
+#include <Swiften/MUC/MUC.h>
+#include <Swiften/MUC/MUCManager.h>
+#include <Swiften/Network/BoostNetworkFactories.h>
+#include <Swiften/Network/Timer.h>
+#include <Swiften/Network/TimerFactory.h>
+
+using namespace Swift;
+using namespace std;
+
+static SimpleEventLoop eventLoop;
+static BoostNetworkFactories networkFactories(&eventLoop);
+
+static boost::shared_ptr<Client> 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<DiscoItems> 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<ClientError>&) {
+ cout << "Disconnected." << endl;
+}
+
+static void handleIncomingMessage(boost::shared_ptr<Message> message) {
+ if (message->getFrom().toBare() == roomJID) {
+ cout << "[ " << roomJID << " ] " << message->getFrom().getResource() << ": " << message->getBody() << endl;
+ }
+}
+
+/*
+ * Usage: ./MUCListAndJoin <jid> <password> <muc_domain>
+ */
+int main(int argc, char* argv[]) {
+ int ret = 0;
+
+ if (argc != 4) {
+ cout << "Usage: ./" << argv[0] << " <jid> <password> <muc_domain>" << endl;
+ ret = -1;
+ }
+ else {
+ mucJID = JID(argv[3]);
+ client = boost::make_shared<Client>(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",
])