diff --git a/backends/libcommuni/ircnetworkplugin.cpp b/backends/libcommuni/ircnetworkplugin.cpp index 74bdec24229287ab76ebdc641187a073deac5044..85e55d25dc53e8fe0ae806b7b09a285d23374cb5 100644 --- a/backends/libcommuni/ircnetworkplugin.cpp +++ b/backends/libcommuni/ircnetworkplugin.cpp @@ -1,6 +1,9 @@ #include "ircnetworkplugin.h" #include #include +#include "transport/logging.h" + +DEFINE_LOGGER(logger, "IRCNetworkPlugin"); #define FROM_UTF8(WHAT) QString::fromUtf8((WHAT).c_str(), (WHAT).size()) #define TO_UTF8(WHAT) std::string((WHAT).toUtf8().data(), (WHAT).toUtf8().size()) @@ -17,7 +20,6 @@ void IRCNetworkPlugin::readData() { if (availableBytes == 0) return; - std::cout << "READ\n"; std::string d = std::string(m_socket->readAll().data(), availableBytes); handleDataRead(d); } @@ -27,7 +29,8 @@ void IRCNetworkPlugin::sendData(const std::string &string) { } void IRCNetworkPlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) { - // Server is in server-mode, so user is JID of server when we want to connect + // In server mode, hostname of the server we want to connect to is stored in "user" JID. + // In component mode we will connect user to the IRC network once he joins the room. if (CONFIG_BOOL(config, "service.server_mode")) { MyIrcSession *session = new MyIrcSession(user, this); std::string h = user.substr(0, user.find("@")); @@ -35,10 +38,11 @@ void IRCNetworkPlugin::handleLoginRequest(const std::string &user, const std::st session->setHost(FROM_UTF8(h.substr(h.find("%") + 1))); session->setPort(6667); session->open(); - std::cout << "CONNECTING IRC NETWORK " << h.substr(h.find("%") + 1) << "\n"; + LOG4CXX_INFO(logger, user << ": Connecting IRC network " << h.substr(h.find("%") + 1)); m_sessions[user] = session; } else { + LOG4CXX_INFO(logger, user << ": Ready for connections"); handleConnected(user); } } @@ -53,7 +57,6 @@ void IRCNetworkPlugin::handleLogoutRequest(const std::string &user, const std::s void IRCNetworkPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &/*xhtml*/) { std::string u = user; - std::cout << "AAAAA " << legacyName << "\n"; if (!CONFIG_BOOL(config, "service.server_mode")) { u = user + legacyName.substr(legacyName.find("@") + 1); if (u.find("/") != std::string::npos) { @@ -61,7 +64,7 @@ void IRCNetworkPlugin::handleMessageSendRequest(const std::string &user, const s } } if (m_sessions[u] == NULL) { - std::cout << "No session for " << u << "\n"; + LOG4CXX_WARN(logger, user << ": Session name: " << u << ", No session for user"); return; } @@ -74,19 +77,19 @@ void IRCNetworkPlugin::handleMessageSendRequest(const std::string &user, const s r = legacyName.substr(legacyName.find("/") + 1); } } - std::cout << "MESSAGE " << u << " " << r << "\n"; + LOG4CXX_INFO(logger, user << ": Session name: " << u << ", message to " << r); m_sessions[u]->sendCommand(IrcCommand::createMessage(FROM_UTF8(r), FROM_UTF8(message))); - std::cout << "SENT\n"; } void IRCNetworkPlugin::handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password) { - std::cout << "JOIN\n"; std::string r = room; std::string u = user; if (!CONFIG_BOOL(config, "service.server_mode")) { u = user + room.substr(room.find("@") + 1); r = room.substr(0, room.find("@")); } + + LOG4CXX_INFO(logger, user << ": Session name: " << u << ", Joining room " << r); if (m_sessions[u] == NULL) { // in gateway mode we want to login this user to network according to legacyName if (room.find("@") != std::string::npos) { @@ -96,16 +99,16 @@ void IRCNetworkPlugin::handleJoinRoomRequest(const std::string &user, const std: session->setHost(FROM_UTF8(room.substr(room.find("@") + 1))); session->setPort(6667); session->open(); - std::cout << "CONNECTING IRC NETWORK " << room.substr(room.find("@") + 1) << "\n"; - std::cout << "SUFFIX " << room.substr(room.find("@")) << "\n"; + LOG4CXX_INFO(logger, user << ": Connecting IRC network " << room.substr(room.find("@") + 1)); m_sessions[u] = session; } else { + LOG4CXX_WARN(logger, user << ": There's no proper server defined in room to which this user wants to join: " << room); return; } } - std::cout << "JOINING " << r << "\n"; - m_sessions[u]->addAutoJoinChannel(r); + + m_sessions[u]->addAutoJoinChannel(r, password); m_sessions[u]->sendCommand(IrcCommand::createJoin(FROM_UTF8(r), FROM_UTF8(password))); m_sessions[u]->rooms += 1; // update nickname, because we have nickname per session, no nickname per room. @@ -123,11 +126,14 @@ void IRCNetworkPlugin::handleLeaveRoomRequest(const std::string &user, const std if (m_sessions[u] == NULL) return; + LOG4CXX_INFO(logger, user << ": Session name: " << u << ", Leaving room " << r); + m_sessions[u]->sendCommand(IrcCommand::createPart(FROM_UTF8(r))); m_sessions[u]->removeAutoJoinChannel(r); m_sessions[u]->rooms -= 1; if (m_sessions[u]->rooms <= 0) { + LOG4CXX_INFO(logger, user << ": Session name: " << u << ", User is not in room, disconnecting from network"); m_sessions[u]->close(); m_sessions[u]->deleteLater(); m_sessions.erase(u); diff --git a/backends/libcommuni/ircnetworkplugin.h b/backends/libcommuni/ircnetworkplugin.h index c15dc9a151157090986f3cb04c315652fd87031d..ff77d2bccb34f2c1587253c8ef65b4dd01b1b1c7 100644 --- a/backends/libcommuni/ircnetworkplugin.h +++ b/backends/libcommuni/ircnetworkplugin.h @@ -26,8 +26,6 @@ class IRCNetworkPlugin : public QObject, public NetworkPlugin { void handleLeaveRoomRequest(const std::string &user, const std::string &room); - std::map m_sessions; - public slots: void readData(); void sendData(const std::string &string); @@ -35,4 +33,5 @@ class IRCNetworkPlugin : public QObject, public NetworkPlugin { private: Config *config; QTcpSocket *m_socket; + std::map m_sessions; }; \ No newline at end of file diff --git a/backends/libcommuni/session.cpp b/backends/libcommuni/session.cpp index 43709a202e6b262b6977378ae21fdc3cb98e8081..51505fb48c0e53eed52f0cb3e8f4e6f04a1d5b80 100644 --- a/backends/libcommuni/session.cpp +++ b/backends/libcommuni/session.cpp @@ -11,7 +11,6 @@ #include "session.h" #include #include -#include "Swiften/Elements/StatusShow.h" #include #include @@ -40,8 +39,8 @@ void MyIrcSession::on_connected() { np->handleConnected(user); } - for(std::list::const_iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { - sendCommand(IrcCommand::createJoin(FROM_UTF8(*it))); + for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { + sendCommand(IrcCommand::createJoin(FROM_UTF8(it->second->getChannel()), FROM_UTF8(it->second->getPassword()))); } if (getIdentify().find(" ") != std::string::npos) { @@ -88,23 +87,23 @@ void MyIrcSession::on_parted(IrcMessage *message) { void MyIrcSession::on_quit(IrcMessage *message) { IrcQuitMessage *m = (IrcQuitMessage *) message; - for(std::list::const_iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { + for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { bool flags = 0; std::string nickname = TO_UTF8(m->sender().name()); flags = correctNickname(nickname); - LOG4CXX_INFO(logger, user << ": " << nickname << " quit " << (*it) + suffix); - np->handleParticipantChanged(user, nickname, (*it) + suffix,(int) flags, pbnetwork::STATUS_NONE, TO_UTF8(m->reason())); + LOG4CXX_INFO(logger, user << ": " << nickname << " quit " << it->second->getChannel() + suffix); + np->handleParticipantChanged(user, nickname, it->second->getChannel() + suffix,(int) flags, pbnetwork::STATUS_NONE, TO_UTF8(m->reason())); } } void MyIrcSession::on_nickChanged(IrcMessage *message) { IrcNickMessage *m = (IrcNickMessage *) message; - for(std::list::const_iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { + for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { std::string nickname = TO_UTF8(m->sender().name()); - bool flags = m_modes[(*it) + nickname]; + bool flags = m_modes[it->second->getChannel() + nickname]; LOG4CXX_INFO(logger, user << ": " << nickname << " changed nickname to " << TO_UTF8(m->nick())); - np->handleParticipantChanged(user, nickname, (*it) + suffix,(int) flags, pbnetwork::STATUS_ONLINE, "", TO_UTF8(m->nick())); + np->handleParticipantChanged(user, nickname, it->second->getChannel() + suffix,(int) flags, pbnetwork::STATUS_ONLINE, "", TO_UTF8(m->nick())); } } @@ -117,15 +116,15 @@ void MyIrcSession::on_modeChanged(IrcMessage *message) { if (nickname.empty()) return; LOG4CXX_INFO(logger, user << ": " << nickname << " changed mode to " << mode); - for(std::list::const_iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { + for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { if (mode == "+o") { - m_modes[(*it) + nickname] = 1; + m_modes[it->second->getChannel() + nickname] = 1; } else { - m_modes[(*it) + nickname] = 0; + m_modes[it->second->getChannel() + nickname] = 0; } - bool flags = m_modes[(*it) + nickname]; - np->handleParticipantChanged(user, nickname, (*it) + suffix,(int) flags, pbnetwork::STATUS_ONLINE, ""); + bool flags = m_modes[it->second->getChannel() + nickname]; + np->handleParticipantChanged(user, nickname, it->second->getChannel() + suffix,(int) flags, pbnetwork::STATUS_ONLINE, ""); } } diff --git a/backends/libcommuni/session.h b/backends/libcommuni/session.h index 8b31da583794a078829fc3175203323a64eede98..bff2aee4c2559489b173fc08efc917cf42418013 100644 --- a/backends/libcommuni/session.h +++ b/backends/libcommuni/session.h @@ -13,6 +13,8 @@ #include #include +#include "Swiften/Swiften.h" +#include using namespace Transport; @@ -21,17 +23,31 @@ class MyIrcSession : public IrcSession Q_OBJECT public: + class AutoJoinChannel { + public: + AutoJoinChannel(const std::string &channel = "", const std::string &password = "") : m_channel(channel), m_password(password) {} + virtual ~AutoJoinChannel() {} + + const std::string &getChannel() { return m_channel; } + const std::string &getPassword() { return m_password; } + private: + std::string m_channel; + std::string m_password; + }; + + typedef std::map > AutoJoinMap; + MyIrcSession(const std::string &user, NetworkPlugin *np, const std::string &suffix = "", QObject* parent = 0); std::map m_modes; std::string suffix; int rooms; - void addAutoJoinChannel(const std::string &channel) { - m_autoJoin.push_back(channel); + void addAutoJoinChannel(const std::string &channel, const std::string &password) { + m_autoJoin[channel] = boost::make_shared(channel, password); } void removeAutoJoinChannel(const std::string &channel) { - m_autoJoin.remove(channel); + m_autoJoin.erase(channel); } void setIdentify(const std::string &identify) { @@ -63,7 +79,7 @@ protected: NetworkPlugin *np; std::string user; std::string m_identify; - std::list m_autoJoin; + AutoJoinMap m_autoJoin; std::string m_topicData; bool m_connected; }; diff --git a/backends/libcommuni/singleircnetworkplugin.cpp b/backends/libcommuni/singleircnetworkplugin.cpp index 276ac5ec831a7a9b5386b2938da82ee30efeecb3..439d765b189b6484e059fa326d0c6905dbba8ab8 100644 --- a/backends/libcommuni/singleircnetworkplugin.cpp +++ b/backends/libcommuni/singleircnetworkplugin.cpp @@ -114,7 +114,7 @@ void SingleIRCNetworkPlugin::handleJoinRoomRequest(const std::string &user, cons } LOG4CXX_INFO(logger, user << ": Joining " << room); - m_sessions[user]->addAutoJoinChannel(room); + m_sessions[user]->addAutoJoinChannel(room, password); m_sessions[user]->sendCommand(IrcCommand::createJoin(FROM_UTF8(room), FROM_UTF8(password))); m_sessions[user]->rooms += 1; diff --git a/include/transport/conversation.h b/include/transport/conversation.h index d3999d284ec11e7afa2ff76ccbe66bc92e4b117b..d228099f6ea803dc195a613331e69fd7038775f8 100644 --- a/include/transport/conversation.h +++ b/include/transport/conversation.h @@ -114,6 +114,8 @@ class Conversation { return m_room; } + void destroyRoom(); + private: ConversationManager *m_conversationManager; std::string m_legacyName; diff --git a/src/conversation.cpp b/src/conversation.cpp index 7aca1f4c8a208e4700219d2eee66401199898f50..22799776a093520225ab9e433267111754adc7b9 100644 --- a/src/conversation.cpp +++ b/src/conversation.cpp @@ -38,6 +38,28 @@ Conversation::Conversation(ConversationManager *conversationManager, const std:: Conversation::~Conversation() { } +void Conversation::destroyRoom() { + if (m_muc) { + Swift::Presence::ref presence = Swift::Presence::create(); + std::string legacyName = m_legacyName; + if (legacyName.find_last_of("@") != std::string::npos) { + legacyName.replace(legacyName.find_last_of("@"), 1, "%"); // OK + } + presence->setFrom(Swift::JID(legacyName, m_conversationManager->getComponent()->getJID().toBare(), m_nickname)); + presence->setTo(m_jid); + presence->setType(Swift::Presence::Unavailable); + + Swift::MUCItem item; + item.affiliation = Swift::MUCOccupant::NoAffiliation; + item.role = Swift::MUCOccupant::NoRole; + Swift::MUCUserPayload *p = new Swift::MUCUserPayload (); + p->addItem(item); + + presence->addPayload(boost::shared_ptr(p)); + m_conversationManager->getComponent()->getStanzaChannel()->sendPresence(presence); + } +} + void Conversation::setRoom(const std::string &room) { m_room = room; m_legacyName = m_room + "/" + m_legacyName; diff --git a/src/conversationmanager.cpp b/src/conversationmanager.cpp index 58d88c2415b18522c82ac279506c50b71546ce09..2089238ee2d888305d1ef640d3023ab059b4a59a 100644 --- a/src/conversationmanager.cpp +++ b/src/conversationmanager.cpp @@ -41,6 +41,7 @@ ConversationManager::ConversationManager(User *user, Component *component){ ConversationManager::~ConversationManager() { while(!m_convs.empty()) { LOG4CXX_INFO(logger, m_user->getJID().toString() << ": Removing conversation " << (*m_convs.begin()).first); + (*m_convs.begin()).second->destroyRoom(); delete (*m_convs.begin()).second; m_convs.erase(m_convs.begin()); } diff --git a/src/tests/conversationmanager.cpp b/src/tests/conversationmanager.cpp index 4319177b76757c2d17f1043eb26f8de06e268bb6..5d517f7cf2832912d4a75229cfaa7ce43839df55 100644 --- a/src/tests/conversationmanager.cpp +++ b/src/tests/conversationmanager.cpp @@ -28,6 +28,7 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe CPPUNIT_TEST(handleChatstateMessages); CPPUNIT_TEST(handleParticipantChanged); CPPUNIT_TEST(handlePMFromXMPP); + CPPUNIT_TEST(handleGroupchatRemoved); CPPUNIT_TEST_SUITE_END(); public: @@ -278,6 +279,22 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe pmconv->handleMessage(msg2); } + void handleGroupchatRemoved() { + User *user = userManager->getUser("user@localhost"); + TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true); + conv->setNickname("nickname"); + conv->setJID("user@localhost/resource"); + received.clear(); + conv->destroyRoom(); + delete conv; + + CPPUNIT_ASSERT_EQUAL(1, (int) received.size()); + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[0]))); + CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::None, dynamic_cast(getStanza(received[0]))->getShow()); + CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast(getStanza(received[0]))->getTo().toString()); + CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/nickname"), dynamic_cast(getStanza(received[0]))->getFrom().toString()); + } + }; CPPUNIT_TEST_SUITE_REGISTRATION (ConversationManagerTest); diff --git a/src/transport.cpp b/src/transport.cpp index 5c25150909b2c2c4843b6b12fe2c4cdfc6c1da01..99ef21d93bad98fddd108002f4978eb3f6063288 100644 --- a/src/transport.cpp +++ b/src/transport.cpp @@ -28,7 +28,7 @@ #include "transport/logging.h" #include "discoinforesponder.h" #include "storageparser.h" -#ifdef _MSC_VER +#ifdef _WIN32 #include #include "Swiften/TLS/Schannel/SchannelServerContext.h" #include "Swiften/TLS/Schannel/SchannelServerContextFactory.h" diff --git a/src/userregistration.cpp b/src/userregistration.cpp index 1fbdd82dcdfc4331a8fbdf289eaf81069bce5b40..a13006ca8ad43e865dd2aa83e3b49f5cd06f1016 100644 --- a/src/userregistration.cpp +++ b/src/userregistration.cpp @@ -316,6 +316,12 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID else if (textSingle->getName() == "local_password") { local_password = textSingle->getValue(); } + // Pidgin sends it as textSingle, not sure why... + else if (textSingle->getName() == "unregister") { + if (textSingle->getValue() == "1" || textSingle->getValue() == "true") { + payload->setRemove(true); + } + } continue; }