included qca2 into build system
[quassel.git] / src / client / clientuserinputhandler.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 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 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 "messagemodel.h"
33
34 #include <QDateTime>
35
36 ClientUserInputHandler::ClientUserInputHandler(QObject *parent)
37 : BasicHandler(parent)
38 {
39   TabCompletionSettings s;
40   s.notify("CompletionSuffix", this, SLOT(completionSuffixChanged(QVariant)));
41   completionSuffixChanged(s.completionSuffix());
42 }
43
44 void ClientUserInputHandler::completionSuffixChanged(const QVariant &v) {
45   QString suffix = v.toString();
46   QString letter = "A-Za-z";
47   QString special = "\x5b-\x60\x7b-\x7d";
48   _nickRx = QRegExp(QString("^([%1%2][%1%2\\d-]*)%3").arg(letter, special, suffix).trimmed());
49 }
50
51 // this would be the place for a client-side hook
52 void ClientUserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg) {
53   if(msg.isEmpty())
54     return;
55
56   if(!msg.startsWith('/')) {
57     if(_nickRx.indexIn(msg) == 0) {
58       const Network *net = Client::network(bufferInfo.networkId());
59       IrcUser *user = net ? net->ircUser(_nickRx.cap(1)) : 0;
60       if(user)
61         user->setLastSpokenTo(bufferInfo.bufferId(), QDateTime::currentDateTime().toUTC());
62     }
63   }
64
65   AliasManager::CommandList clist = Client::aliasManager()->processInput(bufferInfo, msg);
66
67   for(int i = 0; i < clist.count(); i++) {
68     QString cmd = clist.at(i).second.section(' ', 0, 0).remove(0, 1).toUpper();
69     QString payload = clist.at(i).second.section(' ', 1);
70     handle(cmd, Q_ARG(BufferInfo, clist.at(i).first), Q_ARG(QString, payload));
71   }
72 }
73
74 void ClientUserInputHandler::defaultHandler(const QString &cmd, const BufferInfo &bufferInfo, const QString &text) {
75   QString command = QString("/%1 %2").arg(cmd, text);
76   emit sendInput(bufferInfo, command);
77 }
78
79 void ClientUserInputHandler::handleExec(const BufferInfo &bufferInfo, const QString &execString) {
80   ExecWrapper *exec = new ExecWrapper(this); // gets suicidal when it's done
81   exec->start(bufferInfo, execString);
82 }
83
84 void ClientUserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &text) {
85   if(text.isEmpty()) {
86     Client::messageModel()->insertErrorMessage(bufferInfo, tr("/JOIN expects a channel"));
87     return;
88   }
89   switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
90   // send to core
91   defaultHandler("JOIN", bufferInfo, text);
92 }
93
94 void ClientUserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &text) {
95   if(text.isEmpty()) {
96     Client::messageModel()->insertErrorMessage(bufferInfo, tr("/QUERY expects at least a nick"));
97     return;
98   }
99   switchBuffer(bufferInfo.networkId(), text.section(' ', 0, 0));
100   // send to core
101   defaultHandler("QUERY", bufferInfo, text);
102 }
103
104 void ClientUserInputHandler::switchBuffer(const NetworkId &networkId, const QString &bufferName) {
105   BufferId newBufId = Client::networkModel()->bufferId(networkId, bufferName);
106   if(!newBufId.isValid()) {
107     Client::bufferModel()->switchToBufferAfterCreation(networkId, bufferName);
108   }
109   else {
110     Client::bufferModel()->switchToBuffer(newBufId);
111   }
112 }