1/***************************************************************************
2 * Copyright (C) 2005-2014 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 "buffermodel.h"
24#include "client.h"
25#include "clientaliasmanager.h"
26#include "clientsettings.h"
27#include "execwrapper.h"
28#include "ircuser.h"
29#include "network.h"
30#include "types.h"
31#include "bufferinfo.h"
32#include "clientbufferviewconfig.h"
33#include "clientbufferviewmanager.h"
34#include "messagemodel.h"
35
36#include <QDateTime>
37
38ClientUserInputHandler::ClientUserInputHandler(QObject *parent)
39 : BasicHandler(parent)
40{
41 TabCompletionSettings s;
42 s.notify("CompletionSuffix", this, SLOT(completionSuffixChanged(QVariant)));
43 completionSuffixChanged(s.completionSuffix());
44}
45
46
47void ClientUserInputHandler::completionSuffixChanged(const QVariant &v)
48{
49 QString suffix = v.toString();
50 QString letter = "A-Za-z";
51 QString special = "\x5b-\x60\x7b-\x7d";
52 _nickRx = QRegExp(QString("^([%1%2][%1%2\\d-]*)%3").arg(letter, special, suffix).trimmed());
53}
54
55
56// this would be the place for a client-side hook
57void 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)) : 0;
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
81void ClientUserInputHandler::defaultHandler(const QString &cmd, const BufferInfo &bufferInfo, const QString &text)
82{
83 QString command = QString("/%1 %2").arg(cmd, text);
84 emit sendInput(bufferInfo, command);
85}
86
87
88void ClientUserInputHandler::handleExec(const BufferInfo &bufferInfo, const QString &execString)
89{
90 ExecWrapper *exec = new ExecWrapper(this); // gets suicidal when it's done
91 exec->start(bufferInfo, execString);
92}
93
94
95void ClientUserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &text)
96{
97 if (text.isEmpty()) {
98 Client::messageModel()->insertErrorMessage(bufferInfo, tr("/JOIN expects a channel"));
99 return;
100 }
101 switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
102 // send to core
103 defaultHandler("JOIN", bufferInfo, text);
104}
105
106
107void ClientUserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &text)
108{
109 if (text.isEmpty()) {
110 Client::messageModel()->insertErrorMessage(bufferInfo, tr("/QUERY expects at least a nick"));
111 return;
112 }
113 switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
114 // send to core
115 defaultHandler("QUERY", bufferInfo, text);
116}
117
118
119void ClientUserInputHandler::switchBuffer(const NetworkId &networkId, const QString &bufferName)
120{
121 BufferId newBufId = Client::networkModel()->bufferId(networkId, bufferName);
122 if (!newBufId.isValid()) {
123 Client::bufferModel()->switchToBufferAfterCreation(networkId, bufferName);
124 }
125 else {
126 Client::bufferModel()->switchToBuffer(newBufId);
127 // unhide the buffer
128 ClientBufferViewManager *clientBufferViewManager = Client::bufferViewManager();
129 QList<ClientBufferViewConfig *> bufferViewConfigList = clientBufferViewManager->clientBufferViewConfigs();
130 foreach(ClientBufferViewConfig *bufferViewConfig, bufferViewConfigList) {
131 if (bufferViewConfig->temporarilyRemovedBuffers().contains(newBufId)) {
132 bufferViewConfig->addBuffer(newBufId, bufferViewConfig->bufferList().length());
133 //if (bufferViewConfig->sortAlphabetically()) {
134 // TODO we need to trigger a sort here, but can't reach the model required
135 // to get a bufferviewfilter, as the bufferviewmanager only managers configs
136 //BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
137 //}
138 }
139 }
140 }
141}
142