3ecf3c420d43e52577b05618036366d722771d18
[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 <QDateTime>
22
23 #include "buffermodel.h"
24 #include "client.h"
25 #include "clientaliasmanager.h"
26 #include "clientuserinputhandler.h"
27 #include "clientsettings.h"
28 #include "execwrapper.h"
29 #include "ircuser.h"
30 #include "network.h"
31
32 ClientUserInputHandler::ClientUserInputHandler(QObject *parent)
33 : QObject(parent)
34 {
35   TabCompletionSettings s;
36   s.notify("CompletionSuffix", this, SLOT(completionSuffixChanged(QVariant)));
37   completionSuffixChanged(s.completionSuffix());
38 }
39
40 void ClientUserInputHandler::completionSuffixChanged(const QVariant &v) {
41   QString suffix = v.toString();
42   QString letter = "A-Za-z";
43   QString special = "\x5b-\x60\x7b-\x7d";
44   _nickRx = QRegExp(QString("^([%1%2][%1%2\\d-]*)%3").arg(letter, special, suffix).trimmed());
45 }
46
47 // this would be the place for a client-side hook
48 void ClientUserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg) {
49
50   if(!msg.startsWith('/')) {
51     if(_nickRx.indexIn(msg) == 0) {
52       const Network *net = Client::network(bufferInfo.networkId());
53       IrcUser *user = net ? net->ircUser(_nickRx.cap(1)) : 0;
54       if(user)
55         user->setLastSpokenTo(bufferInfo.bufferId(), QDateTime::currentDateTime().toUTC());
56     }
57   }
58
59   AliasManager::CommandList clist = Client::aliasManager()->processInput(bufferInfo, msg);
60
61   for(int i = 0; i < clist.count(); i++) {
62     QString cmd = clist.at(i).second.section(' ', 0, 0).remove(0, 1).toUpper();
63     QString args = clist.at(i).second.section(' ', 1);
64     if(cmd == "EXEC")
65       handleExec(clist.at(i).first, args);
66     else {
67       if(cmd == "JOIN" || cmd == "QUERY") {
68         BufferId newBufId = Client::networkModel()->bufferId(bufferInfo.networkId(), args.section(' ', 0, 0));
69         if(!newBufId.isValid()) {
70           Client::bufferModel()->switchToBufferAfterCreation(bufferInfo.networkId(), args.section(' ', 0, 0));
71         }
72         else {
73           Client::bufferModel()->switchToBuffer(newBufId);
74         }
75       }
76       emit sendInput(clist.at(i).first, clist.at(i).second);
77     }
78   }
79 }
80
81 void ClientUserInputHandler::handleExec(const BufferInfo &bufferInfo, const QString &execString) {
82   ExecWrapper *exec = new ExecWrapper(this); // gets suicidal when it's done
83   exec->start(bufferInfo, execString);
84 }