agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 1 | /** |
| 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 | |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 21 | #include "BotChatHandler.h" |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 22 | |
| 23 | #include "pluglog.h" |
| 24 | |
| 25 | const char sep = separator(); |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 26 | const std::string TAG = "Bot"; |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 27 | |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 28 | #define NAME "Bot" |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 29 | |
| 30 | namespace jami { |
| 31 | |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 32 | BotChatHandler::BotChatHandler(const JAMI_PluginAPI* api, |
| 33 | std::map<std::string, std::string>&& preferences, |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 34 | std::string&& dataPath) |
| 35 | : api_ {api} |
| 36 | , datapath_ {dataPath} |
| 37 | { |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 38 | preferences_ = preferences; |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 39 | setId(datapath_); |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 40 | 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); |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 49 | BotChatHandler::notifyChatSubject(std::pair<std::string, std::string>& subjectConnection, |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 50 | chatSubjectPtr subject) |
| 51 | { |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 52 | if (peerChatSubscriber_ && subjects.find(subject) == subjects.end()) { |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 53 | 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 | |
| 62 | std::map<std::string, std::string> |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 63 | BotChatHandler::getChatHandlerDetails() |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 64 | { |
| 65 | return {{"name", NAME}, {"iconPath", datapath_ + sep + "icon.png"}, {"pluginId", id()}}; |
| 66 | } |
| 67 | |
| 68 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 69 | BotChatHandler::setPreferenceAttribute(const std::string& key, const std::string& value) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 70 | { |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 71 | 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); |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | bool |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 84 | BotChatHandler::preferenceMapHasKey(const std::string& key) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 85 | { |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 86 | return key == "answer" || key == "inText"; |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 90 | BotChatHandler::detach(chatSubjectPtr subject) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 91 | { |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 92 | auto it = subjects.find(subject); |
| 93 | if (it != subjects.end()) { |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 94 | subject->detach(peerChatSubscriber_.get()); |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 95 | subjects.erase(it); |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 99 | BotChatHandler::~BotChatHandler() |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 100 | { |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 101 | for (const auto& subject : subjects) { |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 102 | detach(subject); |
| 103 | } |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 104 | } |
| 105 | } // namespace jami |