bb6e3ce2d2ad0c27557b1076d0923e5068910ae5
[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 "client.h"
24 #include "clientuserinputhandler.h"
25 #include "clientsettings.h"
26 #include "ircuser.h"
27 #include "network.h"
28
29 ClientUserInputHandler::ClientUserInputHandler(QObject *parent)
30 : QObject(parent),
31   _initialized(false)
32 {
33   TabCompletionSettings s;
34   s.notify("CompletionSuffix", this, SLOT(completionSuffixChanged(QVariant)));
35   completionSuffixChanged(s.completionSuffix());
36
37   // we need this signal for future connects to reset the data;
38   connect(Client::instance(), SIGNAL(connected()), SLOT(clientConnected()));
39   connect(Client::instance(), SIGNAL(disconnected()), SLOT(clientDisconnected()));
40   if(Client::isConnected())
41     clientConnected();
42 }
43
44 void ClientUserInputHandler::clientConnected() {
45   _aliasManager = AliasManager();
46   Client::signalProxy()->synchronize(&_aliasManager);
47   connect(&_aliasManager, SIGNAL(initDone()), SLOT(initDone()));
48 }
49
50 void ClientUserInputHandler::clientDisconnected() {
51   // clear alias manager
52   _aliasManager = AliasManager();
53   _initialized = false;
54 }
55
56 void ClientUserInputHandler::initDone() {
57   _initialized = true;
58   for(int i = 0; i < _inputBuffer.count(); i++)
59     handleUserInput(_inputBuffer.at(i).first, _inputBuffer.at(i).second);
60   _inputBuffer.clear();
61 }
62
63 void ClientUserInputHandler::completionSuffixChanged(const QVariant &v) {
64   QString suffix = v.toString();
65   QString letter = "A-Za-z";
66   QString special = "\x5b-\x60\x7b-\x7d";
67   _nickRx = QRegExp(QString("^([%1%2][%1%2\\d-]*)%3").arg(letter, special, suffix).trimmed());
68 }
69
70 // this would be the place for a client-side hook
71 void ClientUserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg_) {
72   QString msg = msg_;
73
74   if(!_initialized) { // aliases not yet synced
75     _inputBuffer.append(qMakePair(bufferInfo, msg));
76     return;
77   }
78
79   // leading slashes indicate there's a command to call unless there is another one in the first section (like a path /proc/cpuinfo)
80   int secondSlashPos = msg.indexOf('/', 1);
81   int firstSpacePos = msg.indexOf(' ');
82   if(!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
83     if(msg.startsWith("//"))
84       msg.remove(0, 1); // //asdf is transformed to /asdf
85
86     // check if we addressed a user and update its timestamp in that case
87     if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
88       if(!msg.startsWith('/')) {
89         if(_nickRx.indexIn(msg) == 0) {
90           const Network *net = Client::network(bufferInfo.networkId());
91           IrcUser *user = net ? net->ircUser(_nickRx.cap(1)) : 0;
92           if(user)
93             user->setLastSpokenTo(bufferInfo.bufferId(), QDateTime::currentDateTime().toUTC());
94         }
95       }
96     }
97     msg.prepend("/SAY ");  // make sure we only send proper commands to the core
98
99   } else {
100     // check for aliases
101     QString cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
102     for(int i = 0; i < _aliasManager.count(); i++) {
103       if(_aliasManager[i].name.toLower() == cmd.toLower()) {
104         expand(_aliasManager[i].expansion, bufferInfo, msg.section(' ', 1));
105         return;
106       }
107     }
108   }
109
110   // all clear, send off to core.
111   emit sendInput(bufferInfo, msg);
112 }
113
114 void ClientUserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
115   const Network *network = Client::network(bufferInfo.networkId());
116   if(!network) {
117     // FIXME send error as soon as we have a method for that!
118     return;
119   }
120
121   QRegExp paramRangeR("\\$(\\d+)\\.\\.(\\d*)");
122   QStringList commands = alias.split(QRegExp("; ?"));
123   QStringList params = msg.split(' ');
124   QStringList expandedCommands;
125   for(int i = 0; i < commands.count(); i++) {
126     QString command = commands[i];
127
128     // replace ranges like $1..3
129     if(!params.isEmpty()) {
130       int pos;
131       while((pos = paramRangeR.indexIn(command)) != -1) {
132         int start = paramRangeR.cap(1).toInt();
133         bool ok;
134         int end = paramRangeR.cap(2).toInt(&ok);
135         if(!ok) {
136           end = params.count();
137         }
138         if(end < start)
139           command = command.replace(pos, paramRangeR.matchedLength(), QString());
140         else {
141           command = command.replace(pos, paramRangeR.matchedLength(), QStringList(params.mid(start - 1, end - start + 1)).join(" "));
142         }
143       }
144     }
145
146     for(int j = params.count(); j > 0; j--) {
147       IrcUser *ircUser = network->ircUser(params[j - 1]);
148       command = command.replace(QString("$%1:hostname").arg(j), ircUser ? ircUser->host() : QString("*"));
149       command = command.replace(QString("$%1").arg(j), params[j - 1]);
150     }
151     command = command.replace("$0", msg);
152     command = command.replace("$channelname", bufferInfo.bufferName());
153     command = command.replace("$currentnick", network->myNick());
154     expandedCommands << command;
155   }
156
157   while(!expandedCommands.isEmpty()) {
158     QString command;
159     if(expandedCommands[0].trimmed().toLower().startsWith("/wait")) {
160       command = expandedCommands.join("; ");
161       expandedCommands.clear();
162     } else {
163       command = expandedCommands.takeFirst();
164     }
165     handleUserInput(bufferInfo, command);
166   }
167 }