blob: fa70e4792ae6dbebefb40d6826fe8122bd4b1d44 [file] [log] [blame]
agsantos520da842020-12-01 16:39:06 -05001/**
2 * Copyright (C) 2020 Savoir-faire Linux Inc.
3 *
4 * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
agsantos82678f32020-12-09 15:03:24 -050021#include "BotChatHandler.h"
agsantos520da842020-12-01 16:39:06 -050022
23#include "pluglog.h"
24
25const char sep = separator();
agsantosb3c90842020-12-10 14:19:41 -050026const std::string TAG = "Bot";
agsantos520da842020-12-01 16:39:06 -050027
agsantosb3c90842020-12-10 14:19:41 -050028#define NAME "Bot"
agsantos520da842020-12-01 16:39:06 -050029
30namespace jami {
31
agsantos82678f32020-12-09 15:03:24 -050032BotChatHandler::BotChatHandler(const JAMI_PluginAPI* api,
33 std::map<std::string, std::string>&& preferences,
agsantos520da842020-12-01 16:39:06 -050034 std::string&& dataPath)
35 : api_ {api}
36 , datapath_ {dataPath}
37{
agsantos82678f32020-12-09 15:03:24 -050038 preferences_ = preferences;
agsantos520da842020-12-01 16:39:06 -050039 setId(datapath_);
agsantosb3c90842020-12-10 14:19:41 -050040 auto answerIt = preferences_.find("answer");
41 auto inTextIt = preferences_.find("inText");
42 if (answerIt != preferences_.end() && inTextIt != preferences_.end())
43 peerChatSubscriber_ = std::make_shared<BotPeerChatSubscriber>(api_,
44 answerIt->second,
45 inTextIt->second);
agsantos520da842020-12-01 16:39:06 -050046};
47
48void
agsantos82678f32020-12-09 15:03:24 -050049BotChatHandler::notifyChatSubject(std::pair<std::string, std::string>& subjectConnection,
agsantos520da842020-12-01 16:39:06 -050050 chatSubjectPtr subject)
51{
agsantosb3c90842020-12-10 14:19:41 -050052 if (peerChatSubscriber_ && subjects.find(subject) == subjects.end()) {
agsantos520da842020-12-01 16:39:06 -050053 std::ostringstream oss;
54 oss << "NEW SUBJECT: account = " << subjectConnection.first
55 << " peer = " << subjectConnection.second << std::endl;
56 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
57 subject->attach(peerChatSubscriber_.get());
58 subjects.insert(subject);
59 }
60}
61
62std::map<std::string, std::string>
agsantos82678f32020-12-09 15:03:24 -050063BotChatHandler::getChatHandlerDetails()
agsantos520da842020-12-01 16:39:06 -050064{
65 return {{"name", NAME}, {"iconPath", datapath_ + sep + "icon.png"}, {"pluginId", id()}};
66}
67
68void
agsantos82678f32020-12-09 15:03:24 -050069BotChatHandler::setPreferenceAttribute(const std::string& key, const std::string& value)
agsantos520da842020-12-01 16:39:06 -050070{
agsantosb3c90842020-12-10 14:19:41 -050071 if (peerChatSubscriber_) {
72 auto it = preferences_.find(key);
73 if (it != preferences_.end() && it->second != value) {
74 it->second = value;
75 if (key == "answer")
76 peerChatSubscriber_->setAnswer(value);
77 if (key == "inText")
78 peerChatSubscriber_->setInText(value);
agsantos520da842020-12-01 16:39:06 -050079 }
80 }
81}
82
83bool
agsantos82678f32020-12-09 15:03:24 -050084BotChatHandler::preferenceMapHasKey(const std::string& key)
agsantos520da842020-12-01 16:39:06 -050085{
agsantosb3c90842020-12-10 14:19:41 -050086 return key == "answer" || key == "inText";
agsantos520da842020-12-01 16:39:06 -050087}
88
89void
agsantos82678f32020-12-09 15:03:24 -050090BotChatHandler::detach(chatSubjectPtr subject)
agsantos520da842020-12-01 16:39:06 -050091{
agsantosb3c90842020-12-10 14:19:41 -050092 auto it = subjects.find(subject);
93 if (it != subjects.end()) {
agsantos520da842020-12-01 16:39:06 -050094 subject->detach(peerChatSubscriber_.get());
agsantosb3c90842020-12-10 14:19:41 -050095 subjects.erase(it);
agsantos520da842020-12-01 16:39:06 -050096 }
97}
98
agsantos82678f32020-12-09 15:03:24 -050099BotChatHandler::~BotChatHandler()
agsantos520da842020-12-01 16:39:06 -0500100{
agsantosb3c90842020-12-10 14:19:41 -0500101 for (const auto& subject : subjects) {
agsantos520da842020-12-01 16:39:06 -0500102 detach(subject);
103 }
agsantos520da842020-12-01 16:39:06 -0500104}
105} // namespace jami