1 /***************************************************************************
2 * Copyright (C) 2005-2016 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"
23 #include "bufferinfo.h"
24 #include "buffermodel.h"
26 #include "clientaliasmanager.h"
27 #include "clientbufferviewconfig.h"
28 #include "clientbufferviewmanager.h"
29 #include "clientignorelistmanager.h"
30 #include "clientsettings.h"
31 #include "execwrapper.h"
32 #include "ignorelistmanager.h"
34 #include "messagemodel.h"
40 ClientUserInputHandler::ClientUserInputHandler(QObject *parent)
41 : BasicHandler(parent)
43 TabCompletionSettings s;
44 s.notify("CompletionSuffix", this, SLOT(completionSuffixChanged(QVariant)));
45 completionSuffixChanged(s.completionSuffix());
49 void ClientUserInputHandler::completionSuffixChanged(const QVariant &v)
51 QString suffix = v.toString();
52 QString letter = "A-Za-z";
53 QString special = "\x5b-\x60\x7b-\x7d";
54 _nickRx = QRegExp(QString("^([%1%2][%1%2\\d-]*)%3").arg(letter, special, suffix).trimmed());
58 // this would be the place for a client-side hook
59 void ClientUserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg)
64 if (!msg.startsWith('/')) {
65 if (_nickRx.indexIn(msg) == 0) {
66 const Network *net = Client::network(bufferInfo.networkId());
67 IrcUser *user = net ? net->ircUser(_nickRx.cap(1)) : 0;
69 user->setLastSpokenTo(bufferInfo.bufferId(), QDateTime::currentDateTime().toUTC());
73 AliasManager::CommandList clist = Client::aliasManager()->processInput(bufferInfo, msg);
75 for (int i = 0; i < clist.count(); i++) {
76 QString cmd = clist.at(i).second.section(' ', 0, 0).remove(0, 1).toUpper();
77 QString payload = clist.at(i).second.section(' ', 1);
78 handle(cmd, Q_ARG(BufferInfo, clist.at(i).first), Q_ARG(QString, payload));
83 void ClientUserInputHandler::defaultHandler(const QString &cmd, const BufferInfo &bufferInfo, const QString &text)
85 QString command = QString("/%1 %2").arg(cmd, text);
86 emit sendInput(bufferInfo, command);
90 void ClientUserInputHandler::handleExec(const BufferInfo &bufferInfo, const QString &execString)
92 ExecWrapper *exec = new ExecWrapper(this); // gets suicidal when it's done
93 exec->start(bufferInfo, execString);
97 void ClientUserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &text)
100 Client::messageModel()->insertErrorMessage(bufferInfo, tr("/JOIN expects a channel"));
103 switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
105 defaultHandler("JOIN", bufferInfo, text);
109 void ClientUserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &text)
111 if (text.isEmpty()) {
112 Client::messageModel()->insertErrorMessage(bufferInfo, tr("/QUERY expects at least a nick"));
115 switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
117 defaultHandler("QUERY", bufferInfo, text);
121 void ClientUserInputHandler::handleIgnore(const BufferInfo &bufferInfo, const QString &text)
123 if (text.isEmpty()) {
124 emit Client::instance()->displayIgnoreList("");
127 // If rule contains no ! or @, we assume it is just a nickname, and turn it into an ignore rule for that nick
128 QString rule = (text.contains('!') || text.contains('@')) ? text : text + "!*@*";
130 Client::ignoreListManager()->requestAddIgnoreListItem(
131 IgnoreListManager::IgnoreType::SenderIgnore,
134 // Use a dynamic ignore rule, for reversibility
135 IgnoreListManager::StrictnessType::SoftStrictness,
136 // Use current network as scope
137 IgnoreListManager::ScopeType::NetworkScope,
138 Client::network(bufferInfo.networkId())->networkName(),
143 void ClientUserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &text)
145 Client::instance()->displayChannelList(bufferInfo.networkId());
149 void ClientUserInputHandler::switchBuffer(const NetworkId &networkId, const QString &bufferName)
151 BufferId newBufId = Client::networkModel()->bufferId(networkId, bufferName);
152 if (!newBufId.isValid()) {
153 Client::bufferModel()->switchToBufferAfterCreation(networkId, bufferName);
156 Client::bufferModel()->switchToBuffer(newBufId);
158 ClientBufferViewManager *clientBufferViewManager = Client::bufferViewManager();
159 QList<ClientBufferViewConfig *> bufferViewConfigList = clientBufferViewManager->clientBufferViewConfigs();
160 foreach(ClientBufferViewConfig *bufferViewConfig, bufferViewConfigList) {
161 if (bufferViewConfig->temporarilyRemovedBuffers().contains(newBufId)) {
162 bufferViewConfig->requestAddBuffer(newBufId, bufferViewConfig->bufferList().length());
163 //if (bufferViewConfig->sortAlphabetically()) {
164 // TODO we need to trigger a sort here, but can't reach the model required
165 // to get a bufferviewfilter, as the bufferviewmanager only managers configs
166 //BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());