diff --git a/spectrum/src/frontends/slack/SlackSession.cpp b/spectrum/src/frontends/slack/SlackSession.cpp index b8465cb5440dd7ae31cb314ac47c3c60da7a3e0b..ab3e8acc8704a41fafd83ea57fcb66a79b759fbd 100644 --- a/spectrum/src/frontends/slack/SlackSession.cpp +++ b/spectrum/src/frontends/slack/SlackSession.cpp @@ -29,6 +29,8 @@ #include "transport/Util.h" #include "transport/Buddy.h" #include "transport/Config.h" +#include "transport/ConversationManager.h" +#include "transport/Conversation.h" #include #include @@ -52,16 +54,57 @@ SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, m_rtm->onRTMStarted.connect(boost::bind(&SlackSession::handleRTMStarted, this)); m_rtm->onMessageReceived.connect(boost::bind(&SlackSession::handleMessageReceived, this, _1, _2, _3, _4, false)); + m_onlineBuddiesTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(20000); + m_onlineBuddiesTimer->onTick.connect(boost::bind(&SlackSession::sendOnlineBuddies, this)); } SlackSession::~SlackSession() { delete m_rtm; + m_onlineBuddiesTimer->stop(); } -void SlackSession::sendMessage(boost::shared_ptr message) { - if (message->getFrom().getResource() == m_uinfo.uin) { +void SlackSession::sendOnlineBuddies() { + if (!m_user) { return; } + std::map convs = m_user->getConversationManager()->getConversations(); + for (std::map ::const_iterator it = convs.begin(); it != convs.end(); it++) { + Conversation *conv = it->second; + if (!conv) { + continue; + } + + std::string onlineBuddies = "Online users: " + conv->getParticipants(); + + if (m_onlineBuddies[it->first] != onlineBuddies) { + m_onlineBuddies[it->first] = onlineBuddies; + std::string legacyName = it->first; + if (legacyName.find_last_of("@") != std::string::npos) { + legacyName.replace(legacyName.find_last_of("@"), 1, "%"); // OK + } + + + std::string to = legacyName + "@" + m_component->getJID().toBare().toString(); + setPurpose(onlineBuddies, m_jid2channel[to]); + } + } + m_onlineBuddiesTimer->start(); +} + +void SlackSession::sendMessage(boost::shared_ptr message) { + if (m_user) { + std::map convs = m_user->getConversationManager()->getConversations(); + for (std::map ::const_iterator it = convs.begin(); it != convs.end(); it++) { + Conversation *conv = it->second; + if (!conv) { + continue; + } + + if (conv->getNickname() == message->getFrom().getResource()) { + return; + } + } + } std::string from = message->getFrom().getResource(); std::string channel = m_jid2channel[message->getFrom().toBare().toString()]; @@ -84,13 +127,17 @@ void SlackSession::sendMessage(boost::shared_ptr message) { m_rtm->getAPI()->sendMessage(from, channel, message->getBody()); } -void SlackSession::setPurpose(const std::string &purpose) { - if (m_slackChannel.empty()) { +void SlackSession::setPurpose(const std::string &purpose, const std::string &channel) { + std::string ch = channel; + if (ch.empty()) { + ch = m_slackChannel; + } + if (ch.empty()) { return; } - LOG4CXX_INFO(logger, "Setting channel purppose: " << m_slackChannel << " " << purpose); - m_rtm->getAPI()->setPurpose(m_slackChannel, purpose); + LOG4CXX_INFO(logger, "Setting channel purppose: " << ch << " " << purpose); + m_rtm->getAPI()->setPurpose(ch, purpose); } void SlackSession::handleJoinMessage(const std::string &message, std::vector &args, bool quiet) { @@ -114,11 +161,13 @@ void SlackSession::handleJoinMessage(const std::string &message, std::vectorgetUserSetting(m_uinfo.id, "rooms", type, rooms); - rooms += message + "\n"; - m_storageBackend->updateUserSetting(m_uinfo.id, "rooms", rooms); + if (!quiet) { + std::string rooms = ""; + int type = (int) TYPE_STRING; + m_storageBackend->getUserSetting(m_uinfo.id, "rooms", type, rooms); + rooms += message + "\n"; + m_storageBackend->updateUserSetting(m_uinfo.id, "rooms", rooms); + } Swift::Presence::ref presence = Swift::Presence::create(); presence->setFrom(Swift::JID("", m_uinfo.jid, "default")); @@ -127,6 +176,8 @@ void SlackSession::handleJoinMessage(const std::string &message, std::vectoraddPayload(boost::shared_ptr(new Swift::MUCPayload())); m_component->getFrontend()->onPresenceReceived(presence); + m_onlineBuddiesTimer->start(); + if (!quiet) { std::string msg; msg += "Spectrum 2 is now joining the room. To leave the room later to disable transporting, you can use `.spectrum2 leave.room #" + SlackAPI::SlackObjectToPlainText(args[5], true, true) + "`."; @@ -200,12 +251,16 @@ void SlackSession::handleRegisterMessage(const std::string &message, std::vector void SlackSession::handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message, const std::string &ts, bool quiet) { if (m_ownerChannel != channel) { std::string to = m_channel2jid[channel]; + if (m_rtm->getUserName(user) == m_rtm->getSelfName()) { + return; + } + if (!to.empty()) { boost::shared_ptr msg(new Swift::Message()); msg->setType(Swift::Message::Groupchat); msg->setTo(to); msg->setFrom(Swift::JID("", m_uinfo.jid, "default")); - msg->setBody("<" + user + "> " + message); + msg->setBody("<" + m_rtm->getUserName(user) + "> " + message); m_component->getFrontend()->onMessageReceived(msg); } else { @@ -241,7 +296,7 @@ void SlackSession::handleMessageReceived(const std::string &channel, const std:: boost::shared_ptr msg(new Swift::Message()); msg->setTo(b->getJID()); msg->setFrom(Swift::JID("", m_uinfo.jid, "default")); - msg->setBody("<" + user + "> " + message); + msg->setBody("<" + m_rtm->getUserName(user) + "> " + message); m_component->getFrontend()->onMessageReceived(msg); } }