summaryrefslogtreecommitdiff
path: root/indra/newview/rlvhandler.cpp
blob: 6c4b4391053bf1a32a5c85f7d6c46d301b12e1f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
 * @file rlvhandler.cpp
 * @author Kitty Barnett
 * @brief RLVa helper classes for internal use only
 *
 * $LicenseInfo:firstyear=2024&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2024, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"
#include "llagent.h"
#include "llstartup.h"
#include "llviewercontrol.h"
#include "llviewerobject.h"

#include "rlvcommon.h"
#include "rlvhandler.h"
#include "rlvhelper.h"

#include <boost/algorithm/string.hpp>

using namespace Rlv;

// ============================================================================
// Static variable initialization
//

bool RlvHandler::mIsEnabled = false;

// ============================================================================
// Command processing functions
//

bool RlvHandler::handleSimulatorChat(std::string& message, const LLChat& chat, const LLViewerObject* chatObj)
{
    // *TODO: There's an edge case for temporary attachments when going from enabled -> disabled with restrictions already in place
    static LLCachedControl<bool> enable_temp_attach(gSavedSettings, Settings::EnableTempAttach);
    static LLCachedControl<bool> show_debug_output(gSavedSettings, Settings::Debug);
    static LLCachedControl<bool> hide_unset_dupes(gSavedSettings, Settings::DebugHideUnsetDup);

    if ( message.length() <= 3 || Constants::CmdPrefix != message[0] || CHAT_TYPE_OWNER != chat.mChatType ||
         (chatObj && chatObj->isTempAttachment() && !enable_temp_attach()) )
    {
        return false;
    }

    message.erase(0, 1);
    LLStringUtil::toLower(message);
    CommandDbgOut cmdDbgOut(message, chatObj->getID() == gAgentID);

    boost_tokenizer tokens(message, boost::char_separator<char>(",", "", boost::drop_empty_tokens));
    for (const std::string& strCmd : tokens)
    {
        ECmdRet eRet = processCommand(chat.mFromID, strCmd, true);
        if ( show_debug_output() &&
             (!hide_unset_dupes() || (ECmdRet::SuccessUnset != eRet && ECmdRet::SuccessDuplicate != eRet)) )
        {
            cmdDbgOut.add(strCmd, eRet);
        }
    }

    message = cmdDbgOut.get();
    return true;
}

ECmdRet RlvHandler::processCommand(const LLUUID& idObj, const std::string& strCmd, bool fromObj)
{
    const RlvCommand rlvCmd(idObj, strCmd);
    return processCommand(std::ref(rlvCmd), fromObj);
}

ECmdRet RlvHandler::processCommand(std::reference_wrapper<const RlvCommand> rlvCmd, bool fromObj)
{
    {
        const RlvCommand& rlvCmdTmp = rlvCmd; // Reference to the temporary with limited variable scope since we don't want it to leak below

        RLV_DEBUGS << "[" << rlvCmdTmp.getObjectID() << "]: " << rlvCmdTmp.asString() << RLV_ENDL;
        if (!rlvCmdTmp.isValid())
        {
            RLV_DEBUGS << "\t-> invalid syntax" << RLV_ENDL;
            return ECmdRet::FailedSyntax;
        }
        if (rlvCmdTmp.isBlocked())
        {
            RLV_DEBUGS << "\t-> blocked command" << RLV_ENDL;
            return ECmdRet::FailedDisabled;
        }
    }

    ECmdRet eRet = ECmdRet::Unknown;
    switch (rlvCmd.get().getParamType())
    {
        case EParamType::Reply:
            eRet = rlvCmd.get().processCommand();
            break;
        case EParamType::Unknown:
        default:
            eRet = ECmdRet::FailedParam;
            break;
    }
    RLV_ASSERT(ECmdRet::Unknown != eRet);

    RLV_DEBUGS << "\t--> command " << (isReturnCodeSuccess(eRet) ? "succeeded" : "failed") << RLV_ENDL;

    return eRet;
}

// ============================================================================
// Initialization helper functions
//

bool RlvHandler::canEnable()
{
    return LLStartUp::getStartupState() <= STATE_LOGIN_CLEANUP;
}

bool RlvHandler::setEnabled(bool enable)
{
    if (mIsEnabled == enable)
        return enable;

    if (enable && canEnable())
    {
        RLV_INFOS << "Enabling Restrained Love API support - " << Strings::getVersionAbout() << RLV_ENDL;
        mIsEnabled = true;
    }

    return mIsEnabled;
}

// ============================================================================
// Command handlers (RLV_TYPE_REPLY)
//

ECmdRet CommandHandlerBaseImpl<EParamType::Reply>::processCommand(const RlvCommand& rlvCmd, ReplyHandlerFunc* pHandler)
{
    // Sanity check - <param> should specify a - valid - reply channel
    S32 nChannel;
    if (!LLStringUtil::convertToS32(rlvCmd.getParam(), nChannel) || !Util::isValidReplyChannel(nChannel, rlvCmd.getObjectID() == gAgentID))
        return ECmdRet::FailedParam;

    std::string strReply;
    ECmdRet eRet = (*pHandler)(rlvCmd, strReply);

    // If we made it this far then:
    //   - the command was handled successfully so we send off the response
    //   - the command failed but we still send off an - empty - response to keep the issuing script from blocking
    if (nChannel != 0)
    {
        Util::sendChatReply(nChannel, strReply);
    }
    RlvHandler::instance().mOnCommandOutput(rlvCmd, nChannel, strReply);

    return eRet;
}

// Handles: @getcommand[:<behaviour>[;<type>[;<separator>]]]=<channel>
template<> template<>
ECmdRet ReplyHandler<EBehaviour::GetCommand>::onCommand(const RlvCommand& rlvCmd, std::string& strReply)
{
    std::vector<std::string> optionList;
    Util::parseStringList(rlvCmd.getOption(), optionList);

    // If a second parameter is present it'll specify the command type
    EParamType eType = EParamType::Unknown;
    if (optionList.size() >= 2)
    {
        if (optionList[1] == "any" || optionList[1].empty())
            eType = EParamType::Unknown;
        else if (optionList[1] == "add")
            eType = EParamType::AddRem;
        else if (optionList[1] == "force")
            eType = EParamType::Force;
        else if (optionList[1] == "reply")
            eType = EParamType::Reply;
        else
            return ECmdRet::FailedOption;
    }

    std::list<std::string> cmdList;
    if (BehaviourDictionary::instance().getCommands(!optionList.empty() ? optionList[0] : LLStringUtil::null, eType, cmdList))
        strReply = boost::algorithm::join(cmdList, optionList.size() >= 3 ? optionList[2] : Constants::OptionSeparator);
    return ECmdRet::Succeeded;
}

// Handles: @version=<chnannel> and @versionnew=<channel>
template<> template<>
ECmdRet VersionReplyHandler::onCommand(const RlvCommand& rlvCmd, std::string& strReply)
{
    strReply = Strings::getVersion(EBehaviour::Version == rlvCmd.getBehaviourType());
    return ECmdRet::Succeeded;
}

// Handles: @versionnum[:impl]=<channel>
template<> template<>
ECmdRet ReplyHandler<EBehaviour::VersionNum>::onCommand(const RlvCommand& rlvCmd, std::string& strReply)
{
    if (!rlvCmd.hasOption())
        strReply = Strings::getVersionNum();
    else if ("impl" == rlvCmd.getOption())
        strReply = Strings::getVersionImplNum();
    else
        return ECmdRet::FailedOption;
    return ECmdRet::Succeeded;
}

// ============================================================================