1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "clientuserinputhandler.h"
25 #include "bufferinfo.h"
26 #include "buffermodel.h"
28 #include "clientaliasmanager.h"
29 #include "clientbufferviewconfig.h"
30 #include "clientbufferviewmanager.h"
31 #include "clientignorelistmanager.h"
32 #include "clientsettings.h"
33 #include "execwrapper.h"
34 #include "ignorelistmanager.h"
36 #include "messagemodel.h"
40 ClientUserInputHandler::ClientUserInputHandler(QObject* parent)
41 : BasicHandler(parent)
43 TabCompletionSettings s;
44 s.notify("CompletionSuffix", this, &ClientUserInputHandler::completionSuffixChanged);
45 completionSuffixChanged(s.completionSuffix());
48 void ClientUserInputHandler::completionSuffixChanged(const QVariant& v)
50 QString suffix = v.toString();
51 QString letter = "A-Za-z";
52 QString special = "\x5b-\x60\x7b-\x7d"; // NOLINT(modernize-raw-string-literal)
53 _nickRx = QRegExp(QString("^([%1%2][%1%2\\d-]*)%3").arg(letter, special, suffix).trimmed());
56 // this would be the place for a client-side hook
57 void ClientUserInputHandler::handleUserInput(const BufferInfo& bufferInfo, const QString& msg)
62 if (!msg.startsWith('/')) {
63 if (_nickRx.indexIn(msg) == 0) {
64 const Network* net = Client::network(bufferInfo.networkId());
65 IrcUser* user = net ? net->ircUser(_nickRx.cap(1)) : nullptr;
67 user->setLastSpokenTo(bufferInfo.bufferId(), QDateTime::currentDateTime().toUTC());
71 AliasManager::CommandList clist = Client::aliasManager()->processInput(bufferInfo, msg);
73 for (int i = 0; i < clist.count(); i++) {
74 QString cmd = clist.at(i).second.section(' ', 0, 0).remove(0, 1).toUpper();
75 QString payload = clist.at(i).second.section(' ', 1);
76 handle(cmd, Q_ARG(BufferInfo, clist.at(i).first), Q_ARG(QString, payload));
80 void ClientUserInputHandler::defaultHandler(const QString& cmd, const BufferInfo& bufferInfo, const QString& text)
82 QString command = QString("/%1 %2").arg(cmd, text);
83 emit sendInput(bufferInfo, command);
86 void ClientUserInputHandler::handleExec(const BufferInfo& bufferInfo, const QString& execString)
88 auto* exec = new ExecWrapper(this); // gets suicidal when it's done
89 exec->start(bufferInfo, execString);
92 void ClientUserInputHandler::handleJoin(const BufferInfo& bufferInfo, const QString& text)
95 Client::messageModel()->insertErrorMessage(bufferInfo, tr("/JOIN expects a channel"));
98 switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
100 defaultHandler("JOIN", bufferInfo, text);
103 void ClientUserInputHandler::handleQuery(const BufferInfo& bufferInfo, const QString& text)
105 if (text.isEmpty()) {
106 Client::messageModel()->insertErrorMessage(bufferInfo, tr("/QUERY expects at least a nick"));
109 switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
111 defaultHandler("QUERY", bufferInfo, text);
114 void ClientUserInputHandler::handleIgnore(const BufferInfo& bufferInfo, const QString& text)
116 if (text.isEmpty()) {
117 emit Client::instance()->displayIgnoreList("");
120 // If rule contains no ! or @, we assume it is just a nickname, and turn it into an ignore rule for that nick
121 QString rule = (text.contains('!') || text.contains('@')) ? text : text + "!*@*";
123 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::IgnoreType::SenderIgnore,
126 // Use a dynamic ignore rule, for reversibility
127 IgnoreListManager::StrictnessType::SoftStrictness,
128 // Use current network as scope
129 IgnoreListManager::ScopeType::NetworkScope,
130 Client::network(bufferInfo.networkId())->networkName(),
134 void ClientUserInputHandler::handleList(const BufferInfo& bufferInfo, const QString& text)
136 // Pass along any potential search parameters, list channels immediately
137 Client::instance()->displayChannelList(bufferInfo.networkId(), text, true);
140 void ClientUserInputHandler::switchBuffer(const NetworkId& networkId, const QString& bufferName)
142 BufferId newBufId = Client::networkModel()->bufferId(networkId, bufferName);
143 if (!newBufId.isValid()) {
144 Client::bufferModel()->switchToBufferAfterCreation(networkId, bufferName);
147 Client::bufferModel()->switchToBuffer(newBufId);
149 ClientBufferViewManager* clientBufferViewManager = Client::bufferViewManager();
150 QList<ClientBufferViewConfig*> bufferViewConfigList = clientBufferViewManager->clientBufferViewConfigs();
151 foreach (ClientBufferViewConfig* bufferViewConfig, bufferViewConfigList) {
152 if (bufferViewConfig->temporarilyRemovedBuffers().contains(newBufId)) {
153 bufferViewConfig->requestAddBuffer(newBufId, bufferViewConfig->bufferList().length());
154 // if (bufferViewConfig->sortAlphabetically()) {
155 // TODO we need to trigger a sort here, but can't reach the model required
156 // to get a bufferviewfilter, as the bufferviewmanager only managers configs
157 // BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());