blob: 19760f5a7dfc0ee2a1c6fd6767251b4c1a135d50 [file] [log] [blame]
agsantos520da842020-12-01 16:39:06 -05001/**
Sébastien Blincb783e32021-02-12 11:34:10 -05002 * Copyright (C) 2020-2021 Savoir-faire Linux Inc.
agsantos520da842020-12-01 16:39:06 -05003 *
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 "BotPeerChatSubscriber.h"
agsantos520da842020-12-01 16:39:06 -050022#include "pluglog.h"
23
24const std::string TAG = "bot";
25
26namespace jami {
27
agsantosb3c90842020-12-10 14:19:41 -050028BotPeerChatSubscriber::BotPeerChatSubscriber(const JAMI_PluginAPI* api,
agsantos3fcc6212021-08-18 17:11:29 -030029 PluginPreferenceHandler* prefHandler)
agsantos520da842020-12-01 16:39:06 -050030 : api_ {api}
agsantosb3c90842020-12-10 14:19:41 -050031{
agsantos3fcc6212021-08-18 17:11:29 -030032 aph_ = prefHandler;
agsantosb3c90842020-12-10 14:19:41 -050033}
agsantos520da842020-12-01 16:39:06 -050034
agsantos82678f32020-12-09 15:03:24 -050035BotPeerChatSubscriber::~BotPeerChatSubscriber()
agsantos520da842020-12-01 16:39:06 -050036{
37 std::ostringstream oss;
38 oss << "~botChatProcessor" << std::endl;
39 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
40}
41
42void
agsantos82678f32020-12-09 15:03:24 -050043BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessagePtr& message)
agsantos520da842020-12-01 16:39:06 -050044{
agsantos3fcc6212021-08-18 17:11:29 -030045 if (!aph_)
46 return;
47 std::string input = aph_->getPreferences(message->accountId, "inText");
48 std::string answer = aph_->getPreferences(message->accountId, "answer");
agsantos520da842020-12-01 16:39:06 -050049 if (isAttached) {
agsantos82678f32020-12-09 15:03:24 -050050 if (message->direction) {
agsantos520da842020-12-01 16:39:06 -050051 std::map<std::string, std::string> sendMsg;
agsantos17c0bc62020-12-15 16:42:56 -050052 if (message->fromHistory)
53 return;
54 if (!message->isSwarm)
55 for (auto& pair : message->data) {
agsantos3fcc6212021-08-18 17:11:29 -030056 if (pair.first == "text/plain" && pair.second == input) {
57 sendMsg[pair.first] = answer;
agsantos17c0bc62020-12-15 16:42:56 -050058 }
agsantos520da842020-12-01 16:39:06 -050059 }
agsantos3fcc6212021-08-18 17:11:29 -030060 else if (message->data.at("type") == "text/plain" && message->data.at("body") == input) {
agsantos17c0bc62020-12-15 16:42:56 -050061 sendMsg["type"] = "text/plain";
agsantos3fcc6212021-08-18 17:11:29 -030062 sendMsg["body"] = answer;
agsantos520da842020-12-01 16:39:06 -050063 }
64 if (!sendMsg.empty()) {
agsantos17c0bc62020-12-15 16:42:56 -050065 sendText(message->accountId, message->peerId, sendMsg, message->isSwarm);
agsantos520da842020-12-01 16:39:06 -050066 }
67 }
68 }
69}
70
71void
agsantos82678f32020-12-09 15:03:24 -050072BotPeerChatSubscriber::attached(Observable<pluginMessagePtr>* observable)
agsantos520da842020-12-01 16:39:06 -050073{
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
83void
agsantos82678f32020-12-09 15:03:24 -050084BotPeerChatSubscriber::detached(Observable<pluginMessagePtr>* observable)
agsantos520da842020-12-01 16:39:06 -050085{
agsantosb3c90842020-12-10 14:19:41 -050086 auto it = observables_.find(observable);
87 if (it != observables_.end()) {
88 observables_.erase(it);
agsantos520da842020-12-01 16:39:06 -050089 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
97void
agsantos82678f32020-12-09 15:03:24 -050098BotPeerChatSubscriber::sendText(std::string& accountId,
agsantos520da842020-12-01 16:39:06 -050099 std::string& peerId,
agsantos17c0bc62020-12-15 16:42:56 -0500100 std::map<std::string, std::string>& sendMsg,
101 bool swarm)
agsantos520da842020-12-01 16:39:06 -0500102{
agsantos17c0bc62020-12-15 16:42:56 -0500103 pluginMessagePtr botAnswer = std::make_shared<JamiMessage>(accountId,
104 peerId,
105 false,
106 sendMsg,
107 true);
108 botAnswer->isSwarm = swarm;
agsantos520da842020-12-01 16:39:06 -0500109 api_->invokeService(api_, "sendTextMessage", botAnswer.get());
110}
111} // namespace jami