BotPeerChatSubscriber.cpp 4.04 KB
Newer Older
Aline Gondim Santos's avatar
Aline Gondim Santos committed
1
/**
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2020-2021 Savoir-faire Linux Inc.
Aline Gondim Santos's avatar
Aline Gondim Santos committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 *  Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

21
#include "BotPeerChatSubscriber.h"
Aline Gondim Santos's avatar
Aline Gondim Santos committed
22
23
24
25
26
27
#include "pluglog.h"

const std::string TAG = "bot";

namespace jami {

28
BotPeerChatSubscriber::BotPeerChatSubscriber(const JAMI_PluginAPI* api,
29
                                             PluginPreferenceHandler* prefHandler)
Aline Gondim Santos's avatar
Aline Gondim Santos committed
30
    : api_ {api}
31
{
32
    aph_ = prefHandler;
33
}
Aline Gondim Santos's avatar
Aline Gondim Santos committed
34

35
BotPeerChatSubscriber::~BotPeerChatSubscriber()
Aline Gondim Santos's avatar
Aline Gondim Santos committed
36
37
38
39
40
41
42
{
    std::ostringstream oss;
    oss << "~botChatProcessor" << std::endl;
    Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
}

void
43
BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessagePtr& message)
Aline Gondim Santos's avatar
Aline Gondim Santos committed
44
{
45
46
47
48
    if (!aph_)
        return;
    std::string input = aph_->getPreferences(message->accountId, "inText");
    std::string answer = aph_->getPreferences(message->accountId, "answer");
Aline Gondim Santos's avatar
Aline Gondim Santos committed
49
    if (isAttached) {
50
        if (message->direction) {
Aline Gondim Santos's avatar
Aline Gondim Santos committed
51
            std::map<std::string, std::string> sendMsg;
Aline Gondim Santos's avatar
Aline Gondim Santos committed
52
53
54
55
            if (message->fromHistory)
                return;
            if (!message->isSwarm)
                for (auto& pair : message->data) {
56
57
                    if (pair.first == "text/plain" && pair.second == input) {
                        sendMsg[pair.first] = answer;
Aline Gondim Santos's avatar
Aline Gondim Santos committed
58
                    }
Aline Gondim Santos's avatar
Aline Gondim Santos committed
59
                }
60
            else if (message->data.at("type") == "text/plain" && message->data.at("body") == input) {
Aline Gondim Santos's avatar
Aline Gondim Santos committed
61
                sendMsg["type"] = "text/plain";
62
                sendMsg["body"] = answer;
63
64
65
66
#ifdef __DEBUG__
                Plog::log(Plog::LogPriority::INFO, TAG, "input " + message->data.at("body"));
                Plog::log(Plog::LogPriority::INFO, TAG, "ouput " + answer);
#endif
Aline Gondim Santos's avatar
Aline Gondim Santos committed
67
68
            }
            if (!sendMsg.empty()) {
Aline Gondim Santos's avatar
Aline Gondim Santos committed
69
                sendText(message->accountId, message->peerId, sendMsg, message->isSwarm);
Aline Gondim Santos's avatar
Aline Gondim Santos committed
70
71
72
73
74
75
            }
        }
    }
}

void
76
BotPeerChatSubscriber::attached(Observable<pluginMessagePtr>* observable)
Aline Gondim Santos's avatar
Aline Gondim Santos committed
77
78
79
80
81
82
83
84
85
86
87
{
    if (observables_.find(observable) == observables_.end()) {
        std::ostringstream oss;
        oss << "::Attached ! " << std::endl;
        Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
        observables_.insert(observable);
        isAttached = true;
    }
}

void
88
BotPeerChatSubscriber::detached(Observable<pluginMessagePtr>* observable)
Aline Gondim Santos's avatar
Aline Gondim Santos committed
89
{
90
91
92
    auto it = observables_.find(observable);
    if (it != observables_.end()) {
        observables_.erase(it);
Aline Gondim Santos's avatar
Aline Gondim Santos committed
93
94
95
96
97
98
99
100
101
        std::ostringstream oss;
        oss << "::Detached()" << std::endl;
        Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
        if (observables_.empty())
            isAttached = false;
    }
}

void
102
BotPeerChatSubscriber::sendText(std::string& accountId,
Aline Gondim Santos's avatar
Aline Gondim Santos committed
103
                                std::string& peerId,
Aline Gondim Santos's avatar
Aline Gondim Santos committed
104
105
                                std::map<std::string, std::string>& sendMsg,
                                bool swarm)
Aline Gondim Santos's avatar
Aline Gondim Santos committed
106
{
Aline Gondim Santos's avatar
Aline Gondim Santos committed
107
108
109
110
111
112
    pluginMessagePtr botAnswer = std::make_shared<JamiMessage>(accountId,
                                                               peerId,
                                                               false,
                                                               sendMsg,
                                                               true);
    botAnswer->isSwarm = swarm;
113
#ifndef __DEBUG__
Aline Gondim Santos's avatar
Aline Gondim Santos committed
114
    api_->invokeService(api_, "sendTextMessage", botAnswer.get());
115
#endif
Aline Gondim Santos's avatar
Aline Gondim Santos committed
116
117
}
} // namespace jami