diff options
Diffstat (limited to 'Swiften/MUC')
-rw-r--r-- | Swiften/MUC/MUC.cpp | 67 | ||||
-rw-r--r-- | Swiften/MUC/MUC.h | 29 |
2 files changed, 53 insertions, 43 deletions
diff --git a/Swiften/MUC/MUC.cpp b/Swiften/MUC/MUC.cpp index 2b8054f..a6fbcbd 100644 --- a/Swiften/MUC/MUC.cpp +++ b/Swiften/MUC/MUC.cpp @@ -3,63 +3,64 @@ #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> +#include "Swiften/Presence/PresenceSender.h" #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() { +MUC::MUC(StanzaChannel* stanzaChannel, PresenceSender* presenceSender, const JID &muc) : ownMUCJID(muc), stanzaChannel(stanzaChannel), presenceSender(presenceSender) { + stanzaChannel->onPresenceReceived.connect(boost::bind(&MUC::handleIncomingPresence, this, _1)); } void MUC::joinAs(const String &nick) { + firstPresenceSeen = false; + + ownMUCJID = JID(ownMUCJID.getNode(), ownMUCJID.getDomain(), nick); + boost::shared_ptr<Presence> joinPresence(new Presence()); - joinPresence->setTo(JID(muc_.getNode(), muc_.getDomain(), nick)); + joinPresence->setTo(ownMUCJID); joinPresence->addPayload(boost::shared_ptr<Payload>(new MUCPayload())); - stanzaChannel_->sendPresence(joinPresence); - myNick_ = nick; + presenceSender->sendPresence(joinPresence); } 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); + presenceSender->removeDirectedPresenceReceiver(ownMUCJID); } void MUC::handleIncomingPresence(boost::shared_ptr<Presence> presence) { - if (presence->getFrom().toBare() != muc_ || presence->getFrom().getResource() == "") { + if (!isFromMUC(presence->getFrom())) { return; } + + if (!firstPresenceSeen) { + if (presence->getType() == Presence::Error) { + onJoinComplete(JoinFailed); + return; + } + firstPresenceSeen = true; + onJoinComplete(JoinSucceeded); + presenceSender->addDirectedPresenceReceiver(ownMUCJID); + } + String nick = presence->getFrom().getResource(); + if (nick.isEmpty()) { + return; + } 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; - } + std::map<String,MUCOccupant>::iterator i = occupants.find(nick); + if (i != occupants.end()) { + onOccupantLeft(i->second, Part, ""); + occupants.erase(i); } - if (!found) { - MUCOccupant occupant(nick); - occupants_.insert(occupants_.end(), std::pair<String, MUCOccupant>(nick, occupant)); - onOccupantJoined(occupant); + } + else if (presence->getType() == Presence::Available) { + std::pair<std::map<String,MUCOccupant>::iterator, bool> result = occupants.insert(std::make_pair(nick, MUCOccupant(nick))); + if (result.second) { + onOccupantJoined(result.first->second); } onOccupantPresenceChange(presence); } diff --git a/Swiften/MUC/MUC.h b/Swiften/MUC/MUC.h index 45bcbd3..2a327f4 100644 --- a/Swiften/MUC/MUC.h +++ b/Swiften/MUC/MUC.h @@ -1,5 +1,4 @@ -#ifndef SWIFTEN_MUC_H -#define SWIFTEN_MUC_H +#pragma once #include "Swiften/JID/JID.h" #include "Swiften/Base/String.h" @@ -14,6 +13,7 @@ namespace Swift { class StanzaChannel; + class PresenceSender; class MUC { public: @@ -21,8 +21,7 @@ namespace Swift { enum LeavingType { Part }; public: - MUC(StanzaChannel* stanzaChannel, const JID &muc); - ~MUC(); + MUC(StanzaChannel* stanzaChannel, PresenceSender* presenceSender, const JID &muc); void joinAs(const String &nick); String getCurrentNick(); @@ -38,12 +37,22 @@ namespace Swift { boost::signal<void (const MUCOccupant&, LeavingType, const String&)> onOccupantLeft; private: + bool isFromMUC(const JID& j) const { + return ownMUCJID.equals(j, JID::WithoutResource); + } + + const String& getOwnNick() const { + return ownMUCJID.getResource(); + } + + private: void handleIncomingPresence(boost::shared_ptr<Presence> presence); - JID muc_; - StanzaChannel *stanzaChannel_; - String myNick_; - std::map<String, MUCOccupant> occupants_; + + private: + JID ownMUCJID; + StanzaChannel* stanzaChannel; + PresenceSender* presenceSender; + std::map<String, MUCOccupant> occupants; + bool firstPresenceSeen; }; } - -#endif |