blob: 8f2475a7b59a9a1c893c5f7cfbbc6f7a8e6051ea [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;
Aline Gondim Santos607635b2022-08-22 15:40:59 -030063#ifdef __DEBUG__
64 Plog::log(Plog::LogPriority::INFO, TAG, "input " + message->data.at("body"));
65 Plog::log(Plog::LogPriority::INFO, TAG, "ouput " + answer);
66#endif
agsantos520da842020-12-01 16:39:06 -050067 }
68 if (!sendMsg.empty()) {
agsantos17c0bc62020-12-15 16:42:56 -050069 sendText(message->accountId, message->peerId, sendMsg, message->isSwarm);
agsantos520da842020-12-01 16:39:06 -050070 }
71 }
72 }
73}
74
75void
agsantos82678f32020-12-09 15:03:24 -050076BotPeerChatSubscriber::attached(Observable<pluginMessagePtr>* observable)
agsantos520da842020-12-01 16:39:06 -050077{
78 if (observables_.find(observable) == observables_.end()) {
79 std::ostringstream oss;
80 oss << "::Attached ! " << std::endl;
81 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
82 observables_.insert(observable);
83 isAttached = true;
84 }
85}
86
87void
agsantos82678f32020-12-09 15:03:24 -050088BotPeerChatSubscriber::detached(Observable<pluginMessagePtr>* observable)
agsantos520da842020-12-01 16:39:06 -050089{
agsantosb3c90842020-12-10 14:19:41 -050090 auto it = observables_.find(observable);
91 if (it != observables_.end()) {
92 observables_.erase(it);
agsantos520da842020-12-01 16:39:06 -050093 std::ostringstream oss;
94 oss << "::Detached()" << std::endl;
95 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
96 if (observables_.empty())
97 isAttached = false;
98 }
99}
100
101void
agsantos82678f32020-12-09 15:03:24 -0500102BotPeerChatSubscriber::sendText(std::string& accountId,
agsantos520da842020-12-01 16:39:06 -0500103 std::string& peerId,
agsantos17c0bc62020-12-15 16:42:56 -0500104 std::map<std::string, std::string>& sendMsg,
105 bool swarm)
agsantos520da842020-12-01 16:39:06 -0500106{
agsantos17c0bc62020-12-15 16:42:56 -0500107 pluginMessagePtr botAnswer = std::make_shared<JamiMessage>(accountId,
108 peerId,
109 false,
110 sendMsg,
111 true);
112 botAnswer->isSwarm = swarm;
Aline Gondim Santos607635b2022-08-22 15:40:59 -0300113#ifndef __DEBUG__
agsantos520da842020-12-01 16:39:06 -0500114 api_->invokeService(api_, "sendTextMessage", botAnswer.get());
Aline Gondim Santos607635b2022-08-22 15:40:59 -0300115#endif
agsantos520da842020-12-01 16:39:06 -0500116}
117} // namespace jami