agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 1 | /** |
Sébastien Blin | cb783e3 | 2021-02-12 11:34:10 -0500 | [diff] [blame] | 2 | * Copyright (C) 2020-2021 Savoir-faire Linux Inc. |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 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 "BotPeerChatSubscriber.h" |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 22 | #include "pluglog.h" |
| 23 | |
| 24 | const std::string TAG = "bot"; |
| 25 | |
| 26 | namespace jami { |
| 27 | |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 28 | BotPeerChatSubscriber::BotPeerChatSubscriber(const JAMI_PluginAPI* api, |
agsantos | 3fcc621 | 2021-08-18 17:11:29 -0300 | [diff] [blame^] | 29 | PluginPreferenceHandler* prefHandler) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 30 | : api_ {api} |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 31 | { |
agsantos | 3fcc621 | 2021-08-18 17:11:29 -0300 | [diff] [blame^] | 32 | aph_ = prefHandler; |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 33 | } |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 34 | |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 35 | BotPeerChatSubscriber::~BotPeerChatSubscriber() |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 36 | { |
| 37 | std::ostringstream oss; |
| 38 | oss << "~botChatProcessor" << std::endl; |
| 39 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 40 | } |
| 41 | |
| 42 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 43 | BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessagePtr& message) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 44 | { |
agsantos | 3fcc621 | 2021-08-18 17:11:29 -0300 | [diff] [blame^] | 45 | if (!aph_) |
| 46 | return; |
| 47 | std::string input = aph_->getPreferences(message->accountId, "inText"); |
| 48 | std::string answer = aph_->getPreferences(message->accountId, "answer"); |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 49 | if (isAttached) { |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 50 | if (message->direction) { |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 51 | std::map<std::string, std::string> sendMsg; |
agsantos | 17c0bc6 | 2020-12-15 16:42:56 -0500 | [diff] [blame] | 52 | if (message->fromHistory) |
| 53 | return; |
| 54 | if (!message->isSwarm) |
| 55 | for (auto& pair : message->data) { |
agsantos | 3fcc621 | 2021-08-18 17:11:29 -0300 | [diff] [blame^] | 56 | if (pair.first == "text/plain" && pair.second == input) { |
| 57 | sendMsg[pair.first] = answer; |
agsantos | 17c0bc6 | 2020-12-15 16:42:56 -0500 | [diff] [blame] | 58 | } |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 59 | } |
agsantos | 3fcc621 | 2021-08-18 17:11:29 -0300 | [diff] [blame^] | 60 | else if (message->data.at("type") == "text/plain" && message->data.at("body") == input) { |
agsantos | 17c0bc6 | 2020-12-15 16:42:56 -0500 | [diff] [blame] | 61 | sendMsg["type"] = "text/plain"; |
agsantos | 3fcc621 | 2021-08-18 17:11:29 -0300 | [diff] [blame^] | 62 | sendMsg["body"] = answer; |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 63 | } |
| 64 | if (!sendMsg.empty()) { |
agsantos | 17c0bc6 | 2020-12-15 16:42:56 -0500 | [diff] [blame] | 65 | sendText(message->accountId, message->peerId, sendMsg, message->isSwarm); |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 72 | BotPeerChatSubscriber::attached(Observable<pluginMessagePtr>* observable) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 73 | { |
| 74 | if (observables_.find(observable) == observables_.end()) { |
| 75 | std::ostringstream oss; |
| 76 | oss << "::Attached ! " << std::endl; |
| 77 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 78 | observables_.insert(observable); |
| 79 | isAttached = true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 84 | BotPeerChatSubscriber::detached(Observable<pluginMessagePtr>* observable) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 85 | { |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame] | 86 | auto it = observables_.find(observable); |
| 87 | if (it != observables_.end()) { |
| 88 | observables_.erase(it); |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 89 | std::ostringstream oss; |
| 90 | oss << "::Detached()" << std::endl; |
| 91 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 92 | if (observables_.empty()) |
| 93 | isAttached = false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 98 | BotPeerChatSubscriber::sendText(std::string& accountId, |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 99 | std::string& peerId, |
agsantos | 17c0bc6 | 2020-12-15 16:42:56 -0500 | [diff] [blame] | 100 | std::map<std::string, std::string>& sendMsg, |
| 101 | bool swarm) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 102 | { |
agsantos | 17c0bc6 | 2020-12-15 16:42:56 -0500 | [diff] [blame] | 103 | pluginMessagePtr botAnswer = std::make_shared<JamiMessage>(accountId, |
| 104 | peerId, |
| 105 | false, |
| 106 | sendMsg, |
| 107 | true); |
| 108 | botAnswer->isSwarm = swarm; |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 109 | api_->invokeService(api_, "sendTextMessage", botAnswer.get()); |
| 110 | } |
| 111 | } // namespace jami |