blob: f02b12103d740021359795ecdee20da21ccd839a [file] [log] [blame]
agsantos520da842020-12-01 16:39:06 -05001/**
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
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,
29 const std::string& textAnswer,
30 const std::string& inText)
agsantos520da842020-12-01 16:39:06 -050031 : api_ {api}
agsantosb3c90842020-12-10 14:19:41 -050032{
33 setAnswer(textAnswer);
34 setInText(inText);
35}
agsantos520da842020-12-01 16:39:06 -050036
agsantos82678f32020-12-09 15:03:24 -050037BotPeerChatSubscriber::~BotPeerChatSubscriber()
agsantos520da842020-12-01 16:39:06 -050038{
39 std::ostringstream oss;
40 oss << "~botChatProcessor" << std::endl;
41 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
42}
43
44void
agsantosb3c90842020-12-10 14:19:41 -050045BotPeerChatSubscriber::setAnswer(const std::string& textAnswer)
46{
47 textAnswer_ = textAnswer;
48}
49
50void
51BotPeerChatSubscriber::setInText(const std::string& inText)
52{
53 inText_ = inText;
54}
55
56void
agsantos82678f32020-12-09 15:03:24 -050057BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessagePtr& message)
agsantos520da842020-12-01 16:39:06 -050058{
59 if (isAttached) {
agsantos82678f32020-12-09 15:03:24 -050060 if (message->direction) {
agsantos520da842020-12-01 16:39:06 -050061 std::map<std::string, std::string> sendMsg;
62 for (auto& pair : message->data) {
agsantosb3c90842020-12-10 14:19:41 -050063 if (pair.first == "text/plain" && pair.second == inText_) {
64 sendMsg[pair.first] = textAnswer_;
agsantos520da842020-12-01 16:39:06 -050065 }
66 }
67 if (!sendMsg.empty()) {
68 sendText(message->accountId, message->peerId, sendMsg);
69 }
70 }
71 }
72}
73
74void
agsantos82678f32020-12-09 15:03:24 -050075BotPeerChatSubscriber::attached(Observable<pluginMessagePtr>* observable)
agsantos520da842020-12-01 16:39:06 -050076{
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
86void
agsantos82678f32020-12-09 15:03:24 -050087BotPeerChatSubscriber::detached(Observable<pluginMessagePtr>* observable)
agsantos520da842020-12-01 16:39:06 -050088{
agsantosb3c90842020-12-10 14:19:41 -050089 auto it = observables_.find(observable);
90 if (it != observables_.end()) {
91 observables_.erase(it);
agsantos520da842020-12-01 16:39:06 -050092 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
100void
agsantos82678f32020-12-09 15:03:24 -0500101BotPeerChatSubscriber::sendText(std::string& accountId,
agsantos520da842020-12-01 16:39:06 -0500102 std::string& peerId,
103 std::map<std::string, std::string>& sendMsg)
104{
agsantos82678f32020-12-09 15:03:24 -0500105 pluginMessagePtr botAnswer = std::make_shared<JamiMessage>(accountId, peerId, false, sendMsg, true);
agsantos520da842020-12-01 16:39:06 -0500106 api_->invokeService(api_, "sendTextMessage", botAnswer.get());
107}
108} // namespace jami