summaryrefslogtreecommitdiffstats
blob: 1aaebac95dced2b9259481c9985ed03da4c3bb01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2014-2016 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 roomCount = 0;
    cout << "List of rooms at " << mucJID.toString() << endl;
    foreach (DiscoItems::Item item, items->getItems()) {
        roomCount++;
        cout << "\t" << roomCount << ". " << item.getJID().getNode() << " - " << item.getName() << std::endl;
        if (roomCount == 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().get_value_or("") << 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;
}