blob: 371e51ce1179374198aec96bc9520b0ba2f74aaf [file] [log] [blame]
/**
* Copyright (C) 2020 Savoir-faire Linux Inc.
*
* Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "BotChatHandler.h"
#include "pluglog.h"
const char sep = separator();
const std::string TAG = "bot";
#define NAME "bot"
namespace jami {
BotChatHandler::BotChatHandler(const JAMI_PluginAPI* api,
std::map<std::string, std::string>&& preferences,
std::string&& dataPath)
: api_ {api}
, datapath_ {dataPath}
{
preferences_ = preferences;
setId(datapath_);
peerChatSubscriber_ = std::make_shared<BotPeerChatSubscriber>(api_, dataPath);
};
void
BotChatHandler::notifyChatSubject(std::pair<std::string, std::string>& subjectConnection,
chatSubjectPtr subject)
{
if (subjects.find(subject) == subjects.end()) {
std::ostringstream oss;
oss << "NEW SUBJECT: account = " << subjectConnection.first
<< " peer = " << subjectConnection.second << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
subject->attach(peerChatSubscriber_.get());
subjects.insert(subject);
}
}
std::map<std::string, std::string>
BotChatHandler::getChatHandlerDetails()
{
return {{"name", NAME}, {"iconPath", datapath_ + sep + "icon.png"}, {"pluginId", id()}};
}
void
BotChatHandler::setPreferenceAttribute(const std::string& key, const std::string& value)
{
auto it = preferences_.find(key);
if (it != preferences_.end()) {
if (preferences_[key] != value) {
preferences_[key] = value;
}
}
}
bool
BotChatHandler::preferenceMapHasKey(const std::string& key)
{
return false;
}
void
BotChatHandler::detach(chatSubjectPtr subject)
{
if (subjects.find(subject) != subjects.end()) {
subject->detach(peerChatSubscriber_.get());
subjects.erase(subject);
}
}
BotChatHandler::~BotChatHandler()
{
auto& copy(subjects);
for (const auto& subject : copy) {
detach(subject);
}
copy.clear();
}
} // namespace jami