summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-06-01 08:48:42 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-06-01 09:24:28 (GMT)
commit2812bddd81f8a1b804c7460f4e14cd0aa393d129 (patch)
treed46294f35150c4f0f43deaf2d31fceaf945ae715 /Swiften/MUC
downloadswift-contrib-2812bddd81f8a1b804c7460f4e14cd0aa393d129.zip
swift-contrib-2812bddd81f8a1b804c7460f4e14cd0aa393d129.tar.bz2
Import.
Diffstat (limited to 'Swiften/MUC')
-rw-r--r--Swiften/MUC/MUC.cpp69
-rw-r--r--Swiften/MUC/MUC.h49
-rw-r--r--Swiften/MUC/MUCOccupant.cpp15
-rw-r--r--Swiften/MUC/MUCOccupant.h21
-rw-r--r--Swiften/MUC/Makefile.inc3
5 files changed, 157 insertions, 0 deletions
diff --git a/Swiften/MUC/MUC.cpp b/Swiften/MUC/MUC.cpp
new file mode 100644
index 0000000..2b8054f
--- /dev/null
+++ b/Swiften/MUC/MUC.cpp
@@ -0,0 +1,69 @@
+#include "Swiften/MUC/MUC.h"
+
+#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Client/StanzaChannel.h"
+#include "Swiften/Elements/IQ.h"
+#include "Swiften/Elements/Message.h"
+#include "Swiften/Elements/MUCPayload.h"
+#include "Swiften/Elements/Presence.h"
+
+namespace Swift {
+
+typedef std::pair<String, MUCOccupant> StringMUCOccupantPair;
+
+MUC::MUC(StanzaChannel* stanzaChannel, const JID &muc) : muc_(muc), stanzaChannel_(stanzaChannel) {
+ stanzaChannel_->onPresenceReceived.connect(boost::bind(&MUC::handleIncomingPresence, this, _1));
+}
+
+MUC::~MUC() {
+}
+
+void MUC::joinAs(const String &nick) {
+ boost::shared_ptr<Presence> joinPresence(new Presence());
+ joinPresence->setTo(JID(muc_.getNode(), muc_.getDomain(), nick));
+ joinPresence->addPayload(boost::shared_ptr<Payload>(new MUCPayload()));
+ stanzaChannel_->sendPresence(joinPresence);
+ myNick_ = nick;
+}
+
+void MUC::part() {
+ boost::shared_ptr<Presence> partPresence(new Presence());
+ partPresence->setType(Presence::Unavailable);
+ partPresence->setTo(JID(muc_.getNode(), muc_.getDomain(), myNick_));
+ stanzaChannel_->sendPresence(partPresence);
+}
+
+void MUC::handleIncomingPresence(boost::shared_ptr<Presence> presence) {
+ if (presence->getFrom().toBare() != muc_ || presence->getFrom().getResource() == "") {
+ return;
+ }
+ String nick = presence->getFrom().getResource();
+ if (presence->getType() == Presence::Unavailable) {
+ foreach (StringMUCOccupantPair occupantPair, occupants_) {
+ if (occupantPair.first == nick) {
+ occupants_.erase(nick);
+ onOccupantLeft(occupantPair.second, Part, "");
+ break;
+ }
+ }
+ } else if (presence->getType() == Presence::Available) {
+ bool found = false;
+ foreach (StringMUCOccupantPair occupantPair, occupants_) {
+ if (occupantPair.first == nick) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ MUCOccupant occupant(nick);
+ occupants_.insert(occupants_.end(), std::pair<String, MUCOccupant>(nick, occupant));
+ onOccupantJoined(occupant);
+ }
+ onOccupantPresenceChange(presence);
+ }
+}
+
+
+}
diff --git a/Swiften/MUC/MUC.h b/Swiften/MUC/MUC.h
new file mode 100644
index 0000000..45bcbd3
--- /dev/null
+++ b/Swiften/MUC/MUC.h
@@ -0,0 +1,49 @@
+#ifndef SWIFTEN_MUC_H
+#define SWIFTEN_MUC_H
+
+#include "Swiften/JID/JID.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/Elements/Message.h"
+#include "Swiften/Elements/Presence.h"
+#include "Swiften/MUC/MUCOccupant.h"
+
+#include <boost/signals.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include <map>
+
+namespace Swift {
+ class StanzaChannel;
+
+ class MUC {
+ public:
+ enum JoinResult { JoinSucceeded, JoinFailed };
+ enum LeavingType { Part };
+
+ public:
+ MUC(StanzaChannel* stanzaChannel, const JID &muc);
+ ~MUC();
+
+ void joinAs(const String &nick);
+ String getCurrentNick();
+ void part();
+ void handleIncomingMessage(boost::shared_ptr<Message> message);
+
+ public:
+ boost::signal<void (JoinResult)> onJoinComplete;
+ boost::signal<void (boost::shared_ptr<Message>)> onMessageReceived;
+ boost::signal<void (boost::shared_ptr<Presence>)> onOccupantPresenceChange;
+ boost::signal<void (const MUCOccupant&)> onOccupantJoined;
+ /**Occupant, type, and reason. */
+ boost::signal<void (const MUCOccupant&, LeavingType, const String&)> onOccupantLeft;
+
+ private:
+ void handleIncomingPresence(boost::shared_ptr<Presence> presence);
+ JID muc_;
+ StanzaChannel *stanzaChannel_;
+ String myNick_;
+ std::map<String, MUCOccupant> occupants_;
+ };
+}
+
+#endif
diff --git a/Swiften/MUC/MUCOccupant.cpp b/Swiften/MUC/MUCOccupant.cpp
new file mode 100644
index 0000000..6ed8591
--- /dev/null
+++ b/Swiften/MUC/MUCOccupant.cpp
@@ -0,0 +1,15 @@
+#include "Swiften/MUC/MUCOccupant.h"
+
+namespace Swift {
+
+MUCOccupant::MUCOccupant(const String &nick) : nick_(nick) {
+}
+
+MUCOccupant::~MUCOccupant() {
+}
+
+String MUCOccupant::getNick() const {
+ return nick_;
+}
+
+}
diff --git a/Swiften/MUC/MUCOccupant.h b/Swiften/MUC/MUCOccupant.h
new file mode 100644
index 0000000..22e58ac
--- /dev/null
+++ b/Swiften/MUC/MUCOccupant.h
@@ -0,0 +1,21 @@
+#ifndef SWIFTEN_MUCOccupant_H
+#define SWIFTEN_MUCOccupant_H
+
+#include "Swiften/Base/String.h"
+
+namespace Swift {
+ class Client;
+
+ class MUCOccupant {
+ public:
+ MUCOccupant(const String &nick);
+ ~MUCOccupant();
+
+ String getNick() const;
+
+ private:
+ String nick_;
+ };
+}
+
+#endif
diff --git a/Swiften/MUC/Makefile.inc b/Swiften/MUC/Makefile.inc
new file mode 100644
index 0000000..d97b9fa
--- /dev/null
+++ b/Swiften/MUC/Makefile.inc
@@ -0,0 +1,3 @@
+SWIFTEN_SOURCES += \
+ Swiften/MUC/MUC.cpp \
+ Swiften/MUC/MUCOccupant.cpp