test: Add build system support and a main function for unit tests
[quassel.git] / src / client / clientuserinputhandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "clientuserinputhandler.h"
22
23 #include "bufferinfo.h"
24 #include "buffermodel.h"
25 #include "client.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"
33 #include "ircuser.h"
34 #include "messagemodel.h"
35 #include "network.h"
36 #include "types.h"
37
38 #include <QDateTime>
39
40 ClientUserInputHandler::ClientUserInputHandler(QObject *parent)
41     : BasicHandler(parent)
42 {
43     TabCompletionSettings s;
44     s.notify("CompletionSuffix", this, &ClientUserInputHandler::completionSuffixChanged);
45     completionSuffixChanged(s.completionSuffix());
46 }
47
48
49 void ClientUserInputHandler::completionSuffixChanged(const QVariant &v)
50 {
51     QString suffix = v.toString();
52     QString letter = "A-Za-z";
53     QString special = "\x5b-\x60\x7b-\x7d";  // NOLINT(modernize-raw-string-literal)
54     _nickRx = QRegExp(QString("^([%1%2][%1%2\\d-]*)%3").arg(letter, special, suffix).trimmed());
55 }
56
57
58 // this would be the place for a client-side hook
59 void ClientUserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg)
60 {
61     if (msg.isEmpty())
62         return;
63
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)) : nullptr;
68             if (user)
69                 user->setLastSpokenTo(bufferInfo.bufferId(), QDateTime::currentDateTime().toUTC());
70         }
71     }
72
73     AliasManager::CommandList clist = Client::aliasManager()->processInput(bufferInfo, msg);
74
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));
79     }
80 }
81
82
83 void ClientUserInputHandler::defaultHandler(const QString &cmd, const BufferInfo &bufferInfo, const QString &text)
84 {
85     QString command = QString("/%1 %2").arg(cmd, text);
86     emit sendInput(bufferInfo, command);
87 }
88
89
90 void ClientUserInputHandler::handleExec(const BufferInfo &bufferInfo, const QString &execString)
91 {
92     auto *exec = new ExecWrapper(this); // gets suicidal when it's done
93     exec->start(bufferInfo, execString);
94 }
95
96
97 void ClientUserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &text)
98 {
99     if (text.isEmpty()) {
100         Client::messageModel()->insertErrorMessage(bufferInfo, tr("/JOIN expects a channel"));
101         return;
102     }
103     switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
104     // send to core
105     defaultHandler("JOIN", bufferInfo, text);
106 }
107
108
109 void ClientUserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &text)
110 {
111     if (text.isEmpty()) {
112         Client::messageModel()->insertErrorMessage(bufferInfo, tr("/QUERY expects at least a nick"));
113         return;
114     }
115     switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
116     // send to core
117     defaultHandler("QUERY", bufferInfo, text);
118 }
119
120
121 void ClientUserInputHandler::handleIgnore(const BufferInfo &bufferInfo, const QString &text)
122 {
123     if (text.isEmpty()) {
124         emit Client::instance()->displayIgnoreList("");
125         return;
126     }
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 + "!*@*";
129
130     Client::ignoreListManager()->requestAddIgnoreListItem(
131             IgnoreListManager::IgnoreType::SenderIgnore,
132             rule,
133             false,
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(),
139             true
140     );
141 }
142
143 void ClientUserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &text)
144 {
145     // Pass along any potential search parameters, list channels immediately
146     Client::instance()->displayChannelList(bufferInfo.networkId(), text, true);
147 }
148
149
150 void ClientUserInputHandler::switchBuffer(const NetworkId &networkId, const QString &bufferName)
151 {
152     BufferId newBufId = Client::networkModel()->bufferId(networkId, bufferName);
153     if (!newBufId.isValid()) {
154         Client::bufferModel()->switchToBufferAfterCreation(networkId, bufferName);
155     }
156     else {
157         Client::bufferModel()->switchToBuffer(newBufId);
158         // unhide the buffer
159         ClientBufferViewManager *clientBufferViewManager = Client::bufferViewManager();
160         QList<ClientBufferViewConfig *> bufferViewConfigList = clientBufferViewManager->clientBufferViewConfigs();
161         foreach(ClientBufferViewConfig *bufferViewConfig, bufferViewConfigList) {
162             if (bufferViewConfig->temporarilyRemovedBuffers().contains(newBufId)) {
163                 bufferViewConfig->requestAddBuffer(newBufId, bufferViewConfig->bufferList().length());
164                 //if (bufferViewConfig->sortAlphabetically()) {
165                 // TODO we need to trigger a sort here, but can't reach the model required
166                 // to get a bufferviewfilter, as the bufferviewmanager only managers configs
167                 //BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
168                 //}
169             }
170         }
171     }
172 }