Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add joining a MIX channel by invitation #86

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add MIXInvite, MIXInvitation and MIXInvitationAck Element
Add elements with their parser and serializers.

License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.

Test-Information:
Added tests for elements parsers and Serializers based on examples in XEP 0369, which passes.
Tested on Ubuntu 16.04 LTS.

Change-Id: Ib5bcdee5f02681cecc0c3fd30d19627be483a7c1
  • Loading branch information
tarun018 committed Jul 22, 2017
commit 716063bcf090918640a3edab7b1ba9ca1c6ff99c
63 changes: 63 additions & 0 deletions Swiften/Elements/MIXInvitation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#pragma once

#include <memory>

#include <Swiften/Base/API.h>
#include <Swiften/Elements/Payload.h>
#include <Swiften/JID/JID.h>

namespace Swift {
class SWIFTEN_API MIXInvitation : public Payload {

public:
using ref = std::shared_ptr<MIXInvitation>;

public:

MIXInvitation() {}

const JID& getInviter() const {
return inviter_;
}

void setInviter(JID inviter) {
inviter_ = inviter;
}

const JID& getInvitee() const {
return invitee_;
}

void setInvitee(JID invitee) {
invitee_ = invitee;
}

const JID& getChannel() const {
return channel_;
}

void setChannel(JID channel) {
channel_ = channel;
}

const std::string& getToken() const {
return token_;
}

void setToken(std::string token) {
token_ = token;
}

private:
JID inviter_;
JID invitee_;
JID channel_;
std::string token_;
};
}
47 changes: 47 additions & 0 deletions Swiften/Elements/MIXInvitationAck.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#pragma once

#include <memory>

#include <Swiften/Base/API.h>
#include <Swiften/Elements/Payload.h>
#include <Swiften/Elements/MIXInvitation.h>

namespace Swift {
class SWIFTEN_API MIXInvitationAck : public Payload {

public:
using ref = std::shared_ptr<MIXInvitationAck>;

enum class InvitationStatus {Joined, Declined, Acknowledged};

public:

MIXInvitationAck() {}

const MIXInvitation::ref getInvitation() const {
return invitation_;
}

void setInvitation(MIXInvitation::ref invitation) {
invitation_ = invitation;
}

InvitationStatus getInvitationStatus() const {
return status_;
}

void setInvitationStatus(InvitationStatus status) {
status_ = status;
}

private:
InvitationStatus status_;
MIXInvitation::ref invitation_;
};
}
47 changes: 47 additions & 0 deletions Swiften/Elements/MIXInvite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#pragma once

#include <memory>

#include <boost/optional.hpp>

#include <Swiften/Base/API.h>
#include <Swiften/Elements/Payload.h>
#include <Swiften/Elements/MIXInvitation.h>

namespace Swift {
class SWIFTEN_API MIXInvite : public Payload {

public:
using ref = std::shared_ptr<MIXInvite>;

public:

MIXInvite() {}

const boost::optional<MIXInvitation::ref>& getInvitation() const {
return invitation_;
}

void setInvitation(MIXInvitation::ref invitation) {
invitation_ = invitation;
}

const boost::optional<JID>& getInvitee() const {
return invitee_;
}

void setInvitee(JID invitee) {
invitee_ = invitee;
}

private:
boost::optional<JID> invitee_;
boost::optional<MIXInvitation::ref> invitation_;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
#include <Swiften/Parser/PayloadParsers/MAMQueryParser.h>
#include <Swiften/Parser/PayloadParsers/MAMResultParser.h>
#include <Swiften/Parser/PayloadParsers/MIXParticipantParserFactory.h>
#include <Swiften/Parser/PayloadParsers/MIXInvitationParserFactory.h>
#include <Swiften/Parser/PayloadParsers/MIXInvitationAckParser.h>
#include <Swiften/Parser/PayloadParsers/MIXInviteParser.h>
#include <Swiften/Parser/PayloadParsers/MIXCreateParser.h>
#include <Swiften/Parser/PayloadParsers/MIXRegisterNickParserFactory.h>
#include <Swiften/Parser/PayloadParsers/MIXSetNickParserFactory.h>
Expand Down Expand Up @@ -140,9 +143,12 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() {
factories_.push_back(std::make_shared<ChatStateParserFactory>());
factories_.push_back(std::make_shared<ClientStateParserFactory>());
factories_.push_back(std::make_shared<MIXParticipantParserFactory>());
factories_.push_back(std::make_shared<MIXInvitationParserFactory>());
factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXDestroyParser> >("destroy", "urn:xmpp:mix:0"));
factories_.push_back(std::make_shared<MIXRegisterNickParserFactory>());
factories_.push_back(std::make_shared<MIXSetNickParserFactory>());
factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXInvitationAckParser> >("invitation-ack", "urn:xmpp:mix:0"));
factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXInviteParser> >("invite", "urn:xmpp:mix:0"));
factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXCreateParser> >("create", "urn:xmpp:mix:0"));
factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXUpdateSubscriptionParser> >("update-subscription", "urn:xmpp:mix:0"));
factories_.push_back(std::make_shared<GenericPayloadParserFactory<MIXUserPreferenceParser> >("user-preference", "urn:xmpp:mix:0"));
Expand Down
68 changes: 68 additions & 0 deletions Swiften/Parser/PayloadParsers/MIXInvitationAckParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#include <Swiften/Parser/PayloadParsers/MIXInvitationAckParser.h>

#include <boost/optional.hpp>

#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Parser/PayloadParsers/MIXInvitationParser.h>

namespace Swift {

MIXInvitationAckParser::MIXInvitationAckParser() : level_(0) {
}

MIXInvitationAckParser::~MIXInvitationAckParser() {
}

void MIXInvitationAckParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {

if (level_ == 1) {
if (element == "invitation" && ns == "urn:xmpp:mix:0") {
currentPayloadParser_ = std::make_shared<MIXInvitationParser>();
} else {
currentText_.clear();
}
}

if (level_ >= 1 && currentPayloadParser_) {
currentPayloadParser_->handleStartElement(element, ns, attributes);
}
++level_;
}

void MIXInvitationAckParser::handleEndElement(const std::string& element, const std::string& ns) {
--level_;
if (level_ >= 1 && currentPayloadParser_) {
currentPayloadParser_->handleEndElement(element, ns);
}

if (level_ == 1) {
if (currentPayloadParser_) {
getPayloadInternal()->setInvitation(std::dynamic_pointer_cast<MIXInvitation>(currentPayloadParser_->getPayload()));
currentPayloadParser_ = nullptr;
} else if (element == "value" && ns == "urn:xmpp:mix:0") {
if (currentText_ == "Declined") {
getPayloadInternal()->setInvitationStatus(MIXInvitationAck::InvitationStatus::Declined);
} else if (currentText_ == "Joined") {
getPayloadInternal()->setInvitationStatus(MIXInvitationAck::InvitationStatus::Joined);
} else if (currentText_ == "Acknowledged") {
getPayloadInternal()->setInvitationStatus(MIXInvitationAck::InvitationStatus::Acknowledged);
}
}
}
}

void MIXInvitationAckParser::handleCharacterData(const std::string& data) {
if (currentPayloadParser_) {
currentPayloadParser_->handleCharacterData(data);
} else {
currentText_ += data;
}
}

}
32 changes: 32 additions & 0 deletions Swiften/Parser/PayloadParsers/MIXInvitationAckParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#pragma once

#include <memory>

#include <Swiften/Base/API.h>
#include <Swiften/Elements/MIXInvitationAck.h>
#include <Swiften/Parser/GenericPayloadParser.h>

namespace Swift {
class PayloadParser;

class SWIFTEN_API MIXInvitationAckParser : public GenericPayloadParser<MIXInvitationAck> {
public:
MIXInvitationAckParser();
virtual ~MIXInvitationAckParser() override;

virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) override;
virtual void handleEndElement(const std::string& element, const std::string&) override;
virtual void handleCharacterData(const std::string& data) override;

private:
int level_;
std::shared_ptr<PayloadParser> currentPayloadParser_;
std::string currentText_;
};
}
30 changes: 30 additions & 0 deletions Swiften/Parser/PayloadParsers/MIXInvitationParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#include <Swiften/Parser/PayloadParsers/MIXInvitationParser.h>

namespace Swift {

void MIXInvitationParser::handleTree(ParserElement::ref root) {
for (const auto& child : root->getAllChildren()) {
if (child->getName() == "inviter" && child->getNamespace() == root->getNamespace()) {
if (boost::optional<JID> jid = JID::parse(child->getText())) {
getPayloadInternal()->setInviter(*jid);
}
} else if (child->getName() == "invitee" && child->getNamespace() == root->getNamespace()) {
if (boost::optional<JID> jid = JID::parse(child->getText())) {
getPayloadInternal()->setInvitee(*jid);
}
} else if (child->getName() == "channel" && child->getNamespace() == root->getNamespace()) {
if (boost::optional<JID> jid = JID::parse(child->getText())) {
getPayloadInternal()->setChannel(*jid);
}
} else if (child->getName() == "token" && child->getNamespace() == root->getNamespace()) {
getPayloadInternal()->setToken(child->getText());
}
}
}
}
19 changes: 19 additions & 0 deletions Swiften/Parser/PayloadParsers/MIXInvitationParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#pragma once

#include <Swiften/Base/API.h>
#include <Swiften/Elements/MIXInvitation.h>
#include <Swiften/Parser/GenericPayloadTreeParser.h>

namespace Swift {
class SWIFTEN_API MIXInvitationParser : public GenericPayloadTreeParser<MIXInvitation> {
public:
MIXInvitationParser() {}
virtual void handleTree(ParserElement::ref root) override;
};
}
27 changes: 27 additions & 0 deletions Swiften/Parser/PayloadParsers/MIXInvitationParserFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#pragma once

#include <Swiften/Base/API.h>
#include <Swiften/Parser/GenericPayloadParserFactory.h>
#include <Swiften/Parser/PayloadParsers/MIXInvitationParser.h>

namespace Swift {
class SWIFTEN_API MIXInvitationParserFactory : public PayloadParserFactory {
public:
MIXInvitationParserFactory() {
}

virtual bool canParse(const std::string& element, const std::string& ns, const AttributeMap&) const override {
return element == "invitation" && ns == "urn:xmpp:mix:0";
}

virtual PayloadParser* createPayloadParser() override {
return new MIXInvitationParser();
}
};
}
Loading