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(); |
| 26 | const std::string TAG = "bot"; |
| 27 | |
| 28 | #define NAME "bot" |
| 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 | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame^] | 40 | peerChatSubscriber_ = std::make_shared<BotPeerChatSubscriber>(api_, dataPath); |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame^] | 44 | BotChatHandler::notifyChatSubject(std::pair<std::string, std::string>& subjectConnection, |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 45 | 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 | |
| 57 | std::map<std::string, std::string> |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame^] | 58 | BotChatHandler::getChatHandlerDetails() |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 59 | { |
| 60 | return {{"name", NAME}, {"iconPath", datapath_ + sep + "icon.png"}, {"pluginId", id()}}; |
| 61 | } |
| 62 | |
| 63 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame^] | 64 | BotChatHandler::setPreferenceAttribute(const std::string& key, const std::string& value) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 65 | { |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame^] | 66 | auto it = preferences_.find(key); |
| 67 | if (it != preferences_.end()) { |
| 68 | if (preferences_[key] != value) { |
| 69 | preferences_[key] = value; |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | bool |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame^] | 75 | BotChatHandler::preferenceMapHasKey(const std::string& key) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 76 | { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame^] | 81 | BotChatHandler::detach(chatSubjectPtr subject) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 82 | { |
| 83 | if (subjects.find(subject) != subjects.end()) { |
| 84 | subject->detach(peerChatSubscriber_.get()); |
| 85 | subjects.erase(subject); |
| 86 | } |
| 87 | } |
| 88 | |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame^] | 89 | BotChatHandler::~BotChatHandler() |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 90 | { |
| 91 | auto& copy(subjects); |
| 92 | for (const auto& subject : copy) { |
| 93 | detach(subject); |
| 94 | } |
| 95 | copy.clear(); |
| 96 | } |
| 97 | } // namespace jami |