From 3827787d38d08b166489fbcec58a83ecaf2a571f Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Tue, 8 Jun 2021 19:59:22 -0500 Subject: [PATCH] Jabber support for receiving invitations --- application/Server.cpp | 8 ++- application/views/InviteDialogue.cpp | 1 - protocols/xmpp/JabberHandler.cpp | 85 ++++++++++++++++++++++++---- protocols/xmpp/JabberHandler.h | 22 ++++++- 4 files changed, 98 insertions(+), 18 deletions(-) diff --git a/application/Server.cpp b/application/Server.cpp index 4bdbf96..4072e8e 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -340,7 +340,6 @@ Server::ImMessage(BMessage* msg) } case IM_ROOM_INVITE_RECEIVED: { - msg->PrintToStream(); BString chat_id; User* user = _EnsureUser(msg); BString user_id = msg->FindString("user_id"); @@ -350,6 +349,7 @@ Server::ImMessage(BMessage* msg) ProtocolLooper* looper = _LooperFromMessage(msg); if (msg->FindString("chat_id", &chat_id) != B_OK || looper == NULL) + { result = B_SKIP_MESSAGE; break; } @@ -370,9 +370,11 @@ Server::ImMessage(BMessage* msg) alertBody.ReplaceAll("%room%", chat_name); alertBody.ReplaceAll("%body%", body); - BMessage* accept = new BMessage(IM_ROOM_INVITE_ACCEPT); + BMessage* accept = new BMessage(IM_MESSAGE); + accept->AddInt32("im_what", IM_ROOM_INVITE_ACCEPT); accept->AddString("chat_id", chat_id); - BMessage* reject = new BMessage(IM_ROOM_INVITE_REFUSE); + BMessage* reject = new BMessage(IM_MESSAGE); + accept->AddInt32("im_what", IM_ROOM_INVITE_REFUSE); reject->AddString("chat_id", chat_id); InviteDialogue* invite = new InviteDialogue(BMessenger(looper), diff --git a/application/views/InviteDialogue.cpp b/application/views/InviteDialogue.cpp index a05ee13..cc9f066 100644 --- a/application/views/InviteDialogue.cpp +++ b/application/views/InviteDialogue.cpp @@ -30,7 +30,6 @@ InviteDialogue::MessageReceived(BMessage* msg) BAlert::MessageReceived(msg); return; } - msg->PrintToStream(); switch (which) { diff --git a/protocols/xmpp/JabberHandler.cpp b/protocols/xmpp/JabberHandler.cpp index ecda478..415074f 100644 --- a/protocols/xmpp/JabberHandler.cpp +++ b/protocols/xmpp/JabberHandler.cpp @@ -145,17 +145,10 @@ JabberHandler::Process(BMessage* msg) } case IM_JOIN_ROOM: { - BString chat_id = msg->FindString("chat_id"); - BString join_id = chat_id; - join_id << "/" << fNick; + BString chat_id; + if (msg->FindString("chat_id", &chat_id) == B_OK) + _JoinRoom(chat_id.String()); - gloox::MUCRoom* room = - new gloox::MUCRoom(fClient, gloox::JID(join_id.String()), - this, this); - room->join(); - room->getRoomItems(); - - fRooms.AddItem(chat_id, room); break; } @@ -175,6 +168,19 @@ JabberHandler::Process(BMessage* msg) break; } + case IM_ROOM_INVITE_ACCEPT: { + BString chat_id; + if (msg->FindString("chat_id", &chat_id) != B_OK) + break; + + BStringList splitAtPassword; + chat_id.Split("#", false, splitAtPassword); + chat_id = splitAtPassword.StringAt(0); + + _JoinRoom(chat_id.String()); + break; + } + case IM_ROOM_KICK_PARTICIPANT: case IM_ROOM_BAN_PARTICIPANT: case IM_ROOM_UNBAN_PARTICIPANT: @@ -268,6 +274,7 @@ JabberHandler::UpdateSettings(BMessage* msg) fClient->setPort(fPort); fClient->registerConnectionListener(this); fClient->registerMessageSessionHandler(this); + fClient->registerMUCInvitationHandler(new InviteHandler(fClient, this)); fClient->rosterManager()->registerRosterListener(this); fClient->disco()->setVersion("Caya", VERSION); fClient->disco()->setIdentity("client", "caya"); @@ -773,8 +780,6 @@ JabberHandler::_StatusSetMsg(const char* user_id, gloox::Presence::PresenceType if (BString(message).IsEmpty() == false) msg.AddString("message", message); - msg.PrintToStream(); - _SendMessage(&msg); } @@ -967,6 +972,23 @@ JabberHandler::_MUCUserId(BString chat_id, const char* nick, BString* id) } +void +JabberHandler::_JoinRoom(const char* chat_id) +{ + BString join_id(chat_id); + join_id << "/" << fNick; + + gloox::MUCRoom* room = fRooms.ValueFor(chat_id); + if (room == NULL) + room = new gloox::MUCRoom(fClient, gloox::JID(join_id.String()), this, this); + + room->join(); + room->getRoomItems(); + + fRooms.AddItem(BString(chat_id), room); +} + + void JabberHandler::_MUCModeration(BMessage* msg) { @@ -1486,6 +1508,8 @@ JabberHandler::handleMUCRequest(gloox::MUCRoom* room, const gloox::DataForm &for } + + void JabberHandler::handleItemAdded(const gloox::JID&) { @@ -1634,3 +1658,40 @@ JabberHandler::handleVCardResult(gloox::VCardHandler::VCardContext context, //if (context == gloox::VCardHandler::FetchVCard) //else } + + +InviteHandler::InviteHandler(gloox::ClientBase* client, JabberHandler* handler) + : + gloox::MUCInvitationHandler(client), + fHandler(handler) +{ +} + + +void +InviteHandler::handleMUCInvitation(const gloox::JID& room, const gloox::JID& from, + const std::string& reason, const std::string& body, + const std::string& password, bool cont, + const std::string& thread) +{ + std::string chat_name = room.resource().c_str(); + BString chat_id = room.full().c_str(); + + if (chat_name.empty() == true) + chat_name = chat_id.String(); + if (password.empty() == false) + chat_id << "#" << password.c_str(); + + BMessage invite(IM_MESSAGE); + invite.AddInt32("im_what", IM_ROOM_INVITE_RECEIVED); + invite.AddString("chat_id", chat_id); + invite.AddString("chat_name", chat_name.c_str()); + invite.AddString("user_id", from.bare().c_str()); + if (reason.empty() == false) + invite.AddString("body", reason.c_str()); + invite.AddString("protocol", fHandler->Signature()); + + fHandler->MessengerInterface()->SendMessage(new BMessage(invite)); +} + + diff --git a/protocols/xmpp/JabberHandler.h b/protocols/xmpp/JabberHandler.h index 3365f0e..91b939d 100644 --- a/protocols/xmpp/JabberHandler.h +++ b/protocols/xmpp/JabberHandler.h @@ -19,12 +19,13 @@ #include #include #include +#include #include #include #include -#include -#include +#include #include +#include #include #include #include @@ -34,6 +35,7 @@ #include class BList; +class InviteHandler; typedef KeyMap RoomMap; @@ -103,6 +105,7 @@ private: fConnection; gloox::VCardManager* fVCardManager; gloox::MessageSession* fSession; + InviteHandler* fInviteHandler; gloox::JID fJid; thread_id fRecvThread; @@ -116,6 +119,7 @@ private: void _SendMessage(BMessage* msg); void _MessageSent(const char* id, const char* subject, const char* body); + void _JoinRoom(const char* chat_id); void _ChatCreatedMsg(const char* id); void _RoleChangedMsg(BString chat_id, BString user_id, @@ -194,4 +198,18 @@ private: const gloox::JID&, gloox::StanzaError); }; + +class InviteHandler : public gloox::MUCInvitationHandler { +public: + InviteHandler(gloox::ClientBase* parent, JabberHandler* handler); + void handleMUCInvitation(const gloox::JID& room, const gloox::JID& from, + const std::string& reason, const std::string& body, + const std::string& password, bool cont, + const std::string& thread); +private: + JabberHandler* fHandler; +}; + + #endif // _JABBER_HANDLER_H +