summaryrefslogtreecommitdiffstats
blob: d2bb1002ca34b5ca50b9e96598d888ae88649be6 (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
#include <boost/bind.hpp>
#include <boost/thread.hpp>

#include "Swiften/Client/Client.h"
#include "Swiften/EventLoop/SimpleEventLoop.h"
#include "Swiften/EventLoop/MainEventLoop.h"
#include "Swiften/Queries/Requests/GetRosterRequest.h"
#include "Swiften/Queries/IQRouter.h"
#include "Swiften/Elements/DiscoInfo.h"
#include "Swiften/Elements/CapsInfo.h"
#include "Swiften/Queries/Responders/DiscoInfoResponder.h"
#include "Swiften/Disco/CapsInfoGenerator.h"

using namespace Swift;
using namespace boost;

class TuneBot {
	public:
		TuneBot(const JID& jid, const String& password) {
			client_ = new Client(jid, password);
			router_ = new IQRouter(client_);
			
			DiscoInfo discoInfo;
			discoInfo.addIdentity(DiscoInfo::Identity("TuneBot", "client", "bot"));
			discoInfo.addFeature("http://jabber.org/protocol/tune+notify");
			capsInfo_ = boost::shared_ptr<CapsInfo>(new CapsInfo(CapsInfoGenerator("http://el-tramo.be/tunebot").generateCapsInfo(discoInfo)));
			discoResponder_ = new DiscoInfoResponder(router_);
			discoResponder_->setDiscoInfo(discoInfo);
			discoResponder_->setDiscoInfo(capsInfo_->getNode() + "#" + capsInfo_->getVersion(), discoInfo);

			client_->onSessionStarted.connect(bind(&TuneBot::handleSessionStarted, this));
			client_->onMessageReceived.connect(bind(&TuneBot::handleMessage, this, _1));
			client_->connect();
		}

		void handleSessionStarted() {
			GetRosterRequest* rosterRequest = new GetRosterRequest(router_, Request::AutoDeleteAfterResponse);
			rosterRequest->onResponse.connect(bind(&TuneBot::handleRosterReceived, this, _1));
			rosterRequest->send();
		}

		void handleRosterReceived(shared_ptr<Payload>) {
			boost::shared_ptr<Presence> presence(new Presence());
			presence->addPayload(capsInfo_);
			presence->setPriority(-1);
			client_->send(presence);
		}

		void handleMessage(shared_ptr<Message> message) {
			// TODO
		}
	
	private:
		Client* client_;
		IQRouter* router_;
		DiscoInfoResponder* discoResponder_;
		boost::shared_ptr<CapsInfo> capsInfo_;
};


int main(int argc, char* argv[])
{
	if (argc != 3) {
		std::cerr << "Usage: " << argv[0] << " <jid> <password>" << std::endl;
		return -1;
	}

	SimpleEventLoop eventLoop;

	TuneBot bot(argv[1], argv[2]);
	eventLoop.run();
}