blob: 371e51ce1179374198aec96bc9520b0ba2f74aaf [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();
26const std::string TAG = "bot";
27
28#define NAME "bot"
29
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_);
agsantos82678f32020-12-09 15:03:24 -050040 peerChatSubscriber_ = std::make_shared<BotPeerChatSubscriber>(api_, dataPath);
agsantos520da842020-12-01 16:39:06 -050041};
42
43void
agsantos82678f32020-12-09 15:03:24 -050044BotChatHandler::notifyChatSubject(std::pair<std::string, std::string>& subjectConnection,
agsantos520da842020-12-01 16:39:06 -050045 chatSubjectPtr subject)
46{
47 if (subjects.find(subject) == subjects.end()) {
48 std::ostringstream oss;
49 oss << "NEW SUBJECT: account = " << subjectConnection.first
50 << " peer = " << subjectConnection.second << std::endl;
51 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
52 subject->attach(peerChatSubscriber_.get());
53 subjects.insert(subject);
54 }
55}
56
57std::map<std::string, std::string>
agsantos82678f32020-12-09 15:03:24 -050058BotChatHandler::getChatHandlerDetails()
agsantos520da842020-12-01 16:39:06 -050059{
60 return {{"name", NAME}, {"iconPath", datapath_ + sep + "icon.png"}, {"pluginId", id()}};
61}
62
63void
agsantos82678f32020-12-09 15:03:24 -050064BotChatHandler::setPreferenceAttribute(const std::string& key, const std::string& value)
agsantos520da842020-12-01 16:39:06 -050065{
agsantos82678f32020-12-09 15:03:24 -050066 auto it = preferences_.find(key);
67 if (it != preferences_.end()) {
68 if (preferences_[key] != value) {
69 preferences_[key] = value;
agsantos520da842020-12-01 16:39:06 -050070 }
71 }
72}
73
74bool
agsantos82678f32020-12-09 15:03:24 -050075BotChatHandler::preferenceMapHasKey(const std::string& key)
agsantos520da842020-12-01 16:39:06 -050076{
77 return false;
78}
79
80void
agsantos82678f32020-12-09 15:03:24 -050081BotChatHandler::detach(chatSubjectPtr subject)
agsantos520da842020-12-01 16:39:06 -050082{
83 if (subjects.find(subject) != subjects.end()) {
84 subject->detach(peerChatSubscriber_.get());
85 subjects.erase(subject);
86 }
87}
88
agsantos82678f32020-12-09 15:03:24 -050089BotChatHandler::~BotChatHandler()
agsantos520da842020-12-01 16:39:06 -050090{
91 auto& copy(subjects);
92 for (const auto& subject : copy) {
93 detach(subject);
94 }
95 copy.clear();
96}
97} // namespace jami