cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / client / clientuserinputhandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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 <QDateTime>
24
25 #include "bufferinfo.h"
26 #include "buffermodel.h"
27 #include "client.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"
35 #include "ircuser.h"
36 #include "messagemodel.h"
37 #include "network.h"
38 #include "types.h"
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 void ClientUserInputHandler::completionSuffixChanged(const QVariant& v)
49 {
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());
54 }
55
56 // this would be the place for a client-side hook
57 void ClientUserInputHandler::handleUserInput(const BufferInfo& bufferInfo, const QString& msg)
58 {
59     if (msg.isEmpty())
60         return;
61
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;
66             if (user)
67                 user->setLastSpokenTo(bufferInfo.bufferId(), QDateTime::currentDateTime().toUTC());
68         }
69     }
70
71     AliasManager::CommandList clist = Client::aliasManager()->processInput(bufferInfo, msg);
72
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));
77     }
78 }
79
80 void ClientUserInputHandler::defaultHandler(const QString& cmd, const BufferInfo& bufferInfo, const QString& text)
81 {
82     QString command = QString("/%1 %2").arg(cmd, text);
83     emit sendInput(bufferInfo, command);
84 }
85
86 void ClientUserInputHandler::handleExec(const BufferInfo& bufferInfo, const QString& execString)
87 {
88     auto* exec = new ExecWrapper(this);  // gets suicidal when it's done
89     exec->start(bufferInfo, execString);
90 }
91
92 void ClientUserInputHandler::handleJoin(const BufferInfo& bufferInfo, const QString& text)
93 {
94     auto channelName = text;
95     if (channelName.isEmpty()) {
96         if (bufferInfo.type() == BufferInfo::ChannelBuffer) {
97             channelName = bufferInfo.bufferName();
98         } else {
99             Client::messageModel()->insertErrorMessage(bufferInfo, tr("/JOIN expects a channel"));
100             return;
101         }
102     }
103     switchBuffer(bufferInfo.networkId(), channelName.section(' ', 0, 0));
104     // send to core
105     defaultHandler("JOIN", bufferInfo, channelName);
106 }
107
108 void ClientUserInputHandler::handleQuery(const BufferInfo& bufferInfo, const QString& text)
109 {
110     if (text.isEmpty()) {
111         Client::messageModel()->insertErrorMessage(bufferInfo, tr("/QUERY expects at least a nick"));
112         return;
113     }
114     switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
115     // send to core
116     defaultHandler("QUERY", bufferInfo, text);
117 }
118
119 void ClientUserInputHandler::handleIgnore(const BufferInfo& bufferInfo, const QString& text)
120 {
121     if (text.isEmpty()) {
122         emit Client::instance()->displayIgnoreList("");
123         return;
124     }
125     // If rule contains no ! or @, we assume it is just a nickname, and turn it into an ignore rule for that nick
126     QString rule = (text.contains('!') || text.contains('@')) ? text : text + "!*@*";
127
128     Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::IgnoreType::SenderIgnore,
129                                                           rule,
130                                                           false,
131                                                           // Use a dynamic ignore rule, for reversibility
132                                                           IgnoreListManager::StrictnessType::SoftStrictness,
133                                                           // Use current network as scope
134                                                           IgnoreListManager::ScopeType::NetworkScope,
135                                                           Client::network(bufferInfo.networkId())->networkName(),
136                                                           true);
137 }
138
139 void ClientUserInputHandler::handleList(const BufferInfo& bufferInfo, const QString& text)
140 {
141     // Pass along any potential search parameters, list channels immediately
142     Client::instance()->displayChannelList(bufferInfo.networkId(), text, true);
143 }
144
145 void ClientUserInputHandler::switchBuffer(const NetworkId& networkId, const QString& bufferName)
146 {
147     BufferId newBufId = Client::networkModel()->bufferId(networkId, bufferName);
148     if (!newBufId.isValid()) {
149         Client::bufferModel()->switchToBufferAfterCreation(networkId, bufferName);
150     }
151     else {
152         Client::bufferModel()->switchToBuffer(newBufId);
153         // unhide the buffer
154         ClientBufferViewManager* clientBufferViewManager = Client::bufferViewManager();
155         QList<ClientBufferViewConfig*> bufferViewConfigList = clientBufferViewManager->clientBufferViewConfigs();
156         foreach (ClientBufferViewConfig* bufferViewConfig, bufferViewConfigList) {
157             if (bufferViewConfig->temporarilyRemovedBuffers().contains(newBufId)) {
158                 bufferViewConfig->requestAddBuffer(newBufId, bufferViewConfig->bufferList().length());
159                 // if (bufferViewConfig->sortAlphabetically()) {
160                 // TODO we need to trigger a sort here, but can't reach the model required
161                 // to get a bufferviewfilter, as the bufferviewmanager only managers configs
162                 // BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
163                 //}
164             }
165         }
166     }
167 }