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 "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, |
| 29 | const std::string& textAnswer, |
| 30 | const std::string& inText) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 31 | : api_ {api} |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame^] | 32 | { |
| 33 | setAnswer(textAnswer); |
| 34 | setInText(inText); |
| 35 | } |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 36 | |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 37 | BotPeerChatSubscriber::~BotPeerChatSubscriber() |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 38 | { |
| 39 | std::ostringstream oss; |
| 40 | oss << "~botChatProcessor" << std::endl; |
| 41 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 42 | } |
| 43 | |
| 44 | void |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame^] | 45 | BotPeerChatSubscriber::setAnswer(const std::string& textAnswer) |
| 46 | { |
| 47 | textAnswer_ = textAnswer; |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | BotPeerChatSubscriber::setInText(const std::string& inText) |
| 52 | { |
| 53 | inText_ = inText; |
| 54 | } |
| 55 | |
| 56 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 57 | BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessagePtr& message) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 58 | { |
| 59 | if (isAttached) { |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 60 | if (message->direction) { |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 61 | std::map<std::string, std::string> sendMsg; |
| 62 | for (auto& pair : message->data) { |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame^] | 63 | if (pair.first == "text/plain" && pair.second == inText_) { |
| 64 | sendMsg[pair.first] = textAnswer_; |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | if (!sendMsg.empty()) { |
| 68 | sendText(message->accountId, message->peerId, sendMsg); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 75 | BotPeerChatSubscriber::attached(Observable<pluginMessagePtr>* observable) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 76 | { |
| 77 | if (observables_.find(observable) == observables_.end()) { |
| 78 | std::ostringstream oss; |
| 79 | oss << "::Attached ! " << std::endl; |
| 80 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 81 | observables_.insert(observable); |
| 82 | isAttached = true; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 87 | BotPeerChatSubscriber::detached(Observable<pluginMessagePtr>* observable) |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 88 | { |
agsantos | b3c9084 | 2020-12-10 14:19:41 -0500 | [diff] [blame^] | 89 | auto it = observables_.find(observable); |
| 90 | if (it != observables_.end()) { |
| 91 | observables_.erase(it); |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 92 | std::ostringstream oss; |
| 93 | oss << "::Detached()" << std::endl; |
| 94 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 95 | if (observables_.empty()) |
| 96 | isAttached = false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 101 | BotPeerChatSubscriber::sendText(std::string& accountId, |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 102 | std::string& peerId, |
| 103 | std::map<std::string, std::string>& sendMsg) |
| 104 | { |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 105 | pluginMessagePtr botAnswer = std::make_shared<JamiMessage>(accountId, peerId, false, sendMsg, true); |
agsantos | 520da84 | 2020-12-01 16:39:06 -0500 | [diff] [blame] | 106 | api_->invokeService(api_, "sendTextMessage", botAnswer.get()); |
| 107 | } |
| 108 | } // namespace jami |