commands containing multiple slashes are no longer treated like a separate command...
[quassel.git] / src / core / userinputhandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 #include "userinputhandler.h"
21
22 #include "util.h"
23
24 #include "networkconnection.h"
25 #include "network.h"
26 #include "ctcphandler.h"
27 #include "ircuser.h"
28
29 #include <QDebug>
30
31 UserInputHandler::UserInputHandler(NetworkConnection *parent) : BasicHandler(parent) {
32 }
33
34 void UserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg_) {
35   try {
36     if(msg_.isEmpty())
37       return;
38     QString cmd;
39     QString msg = msg_;
40     // leading slashes indicate there's a command to call unless there is anothere one in the first section (like a path /proc/cpuinfo)
41     int secondSlashPos = msg.indexOf('/', 1);
42     int firstSpacePos = msg.indexOf(' ');
43     if(!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
44       cmd = QString("SAY");
45     } else {
46       cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
47       msg = msg.section(' ', 1);
48     }
49     handle(cmd, Q_ARG(BufferInfo, bufferInfo), Q_ARG(QString, msg));
50   } catch(Exception e) {
51     emit displayMsg(Message::Error, bufferInfo.type(), "", e.msg());
52   }
53 }
54
55 // ====================
56 //  Public Slots
57 // ====================
58
59 void UserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg) {
60   Q_UNUSED(bufferInfo)
61
62   QString awayMsg = msg;
63   IrcUser *me = network()->me();
64
65   // if there is no message supplied we have to check if we are already away or not
66   if(msg.isEmpty()) {
67     if(me && !me->isAway())
68       awayMsg = networkConnection()->identity()->awayReason();
69   }
70   if(me)
71     me->setAwayMessage(awayMsg);
72   
73   putCmd("AWAY", serverEncode(awayMsg));
74 }
75
76 void UserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg) {
77   QString banChannel;
78   QString banUser;
79
80   QStringList params = msg.split(" ");
81
82   if(!params.isEmpty() && isChannelName(params[0])) {
83     banChannel = params.takeFirst();
84   } else if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
85     banChannel = bufferInfo.bufferName();
86   } else {
87     emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: channel unknown in command: /BAN %1").arg(msg));
88     return;
89   }
90
91   if(!params.isEmpty() && !params.contains("!") && network()->ircUser(params[0])) {
92     IrcUser *ircuser = network()->ircUser(params[0]);
93     // generalizedHost changes <nick> to  *!ident@*.sld.tld.
94     QString generalizedHost = ircuser->host();
95     if(generalizedHost.isEmpty()) {
96       emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: host unknown in command: /BAN %1").arg(msg));
97       return;
98     }
99
100     if(generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
101       int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1);
102       generalizedHost.replace(0, secondLastPeriodPosition, "*");
103     }
104     banUser = QString("*!%1@%2").arg(ircuser->user()).arg(generalizedHost);
105   } else {
106     banUser = params.join(" ");
107   }
108
109   QString banMsg = QString("MODE %1 +b %2").arg(banChannel).arg(banUser);
110   emit putRawLine(serverEncode(banMsg));
111 }
112
113 void UserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg) {
114   Q_UNUSED(bufferInfo)
115   QString nick = msg.section(' ', 0, 0);
116   QString ctcpTag = msg.section(' ', 1, 1).toUpper();
117   if (ctcpTag.isEmpty()) return;
118   QString message = "";
119   QString verboseMessage = tr("sending CTCP-%1 request").arg(ctcpTag);
120
121   if(ctcpTag == "PING") {
122     uint now = QDateTime::currentDateTime().toTime_t();
123     message = QString::number(now);
124   }
125
126   networkConnection()->ctcpHandler()->query(nick, ctcpTag, message);
127   emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
128 }
129
130 void UserInputHandler::handleDeop(const BufferInfo &bufferInfo, const QString &msg) {
131   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
132   QString m = "-"; for(int i = 0; i < nicks.count(); i++) m += 'o';
133   QStringList params;
134   params << bufferInfo.bufferName() << m << nicks;
135   emit putCmd("MODE", serverEncode(params));
136 }
137
138 void UserInputHandler::handleDevoice(const BufferInfo &bufferInfo, const QString &msg) {
139   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
140   QString m = "-"; for(int i = 0; i < nicks.count(); i++) m += 'v';
141   QStringList params;
142   params << bufferInfo.bufferName() << m << nicks;
143   emit putCmd("MODE", serverEncode(params));
144 }
145
146 void UserInputHandler::handleInvite(const BufferInfo &bufferInfo, const QString &msg) {
147   QStringList params;
148   params << msg << bufferInfo.bufferName();
149   emit putCmd("INVITE", serverEncode(params));
150 }
151
152 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
153   Q_UNUSED(bufferInfo)
154   QStringList params = msg.trimmed().split(" ");
155   QStringList chans = params[0].split(",");
156   QStringList keys;
157   if(params.count() > 1) keys = params[1].split(",");
158   emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
159   int i = 0;
160   for(; i < keys.count(); i++) {
161     if(i >= chans.count()) break;
162     networkConnection()->addChannelKey(chans[i], keys[i]);
163   }
164   for(; i < chans.count(); i++) {
165     networkConnection()->removeChannelKey(chans[i]);
166   }
167 }
168
169 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
170   QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
171   QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
172   if(reason.isEmpty())
173     reason = networkConnection()->identity()->kickReason();
174
175   QList<QByteArray> params;
176   params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
177   emit putCmd("KICK", params);
178 }
179
180 void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
181   Q_UNUSED(bufferInfo)
182   QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
183   QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
184   QList<QByteArray> params;
185   params << serverEncode(nick) << serverEncode(pass);
186   emit putCmd("KILL", params);
187 }
188
189
190 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
191   Q_UNUSED(bufferInfo)
192   emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
193 }
194
195 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
196   if(bufferInfo.bufferName().isEmpty()) return; // server buffer
197   networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
198   emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
199 }
200
201 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
202   Q_UNUSED(bufferInfo)
203
204   QStringList params = msg.split(' ', QString::SkipEmptyParts);
205   // if the first argument is neither a channel nor us (user modes are only to oneself) the current buffer is assumed to be the target
206   if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
207     params.prepend(bufferInfo.bufferName());
208   
209   // TODO handle correct encoding for buffer modes (channelEncode())
210   emit putCmd("MODE", serverEncode(params));
211 }
212
213 // TODO: show privmsgs
214 void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg) {
215   Q_UNUSED(bufferInfo);
216   if(!msg.contains(' '))
217     return;
218
219   QList<QByteArray> params;
220   params << serverEncode(msg.section(' ', 0, 0));
221   params << userEncode(params[0], msg.section(' ', 1));
222
223   emit putCmd("PRIVMSG", params);
224 }
225
226 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
227   Q_UNUSED(bufferInfo)
228   QString nick = msg.section(' ', 0, 0);
229   emit putCmd("NICK", serverEncode(nick));
230 }
231
232 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
233   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
234   QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
235   QStringList params;
236   params << bufferInfo.bufferName() << m << nicks;
237   emit putCmd("MODE", serverEncode(params));
238 }
239
240 void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
241   Q_UNUSED(bufferInfo)
242   emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
243 }
244
245 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
246   QList<QByteArray> params;
247   QString partReason;
248
249   // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
250   QString channelName = msg.section(' ', 0, 0);
251   if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
252     channelName = bufferInfo.bufferName();
253     partReason = msg;
254   } else {
255     partReason = msg.mid(channelName.length() + 1);
256   }
257   
258   if(partReason.isEmpty())
259     partReason = networkConnection()->identity()->partReason();
260
261   params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
262   emit putCmd("PART", params);
263 }
264
265 void UserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg) {
266   Q_UNUSED(bufferInfo)
267
268   QString param = msg;
269   if(param.isEmpty())
270     param = QTime::currentTime().toString("hh:mm:ss.zzz");
271
272   putCmd("PING", serverEncode(param));
273 }
274
275 // TODO: implement queries
276 void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
277   Q_UNUSED(bufferInfo)
278   QString target = msg.section(' ', 0, 0);
279   QString message = msg.section(' ', 1);
280   if(message.isEmpty())
281     emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, "Starting query with " + target, network()->myNick(), Message::Self);
282   else
283     emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
284   handleMsg(bufferInfo, msg);
285 }
286
287 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
288   Q_UNUSED(bufferInfo)
289
290   QString quitReason;
291   if(msg.isEmpty())
292     quitReason = networkConnection()->identity()->quitReason();
293   else
294     quitReason = msg;
295
296   emit putCmd("QUIT", serverEncode(quitReason));
297   networkConnection()->disconnectFromIrc();
298 }
299
300 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
301   Q_UNUSED(bufferInfo)
302   emit putRawLine(serverEncode(msg));
303 }
304
305 void UserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg) {
306   if(bufferInfo.bufferName().isEmpty()) return;  // server buffer
307   QList<QByteArray> params;
308   params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
309   emit putCmd("PRIVMSG", params);
310   emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
311 }
312
313 void UserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
314   if(bufferInfo.bufferName().isEmpty()) return;
315   if(!msg.isEmpty()) {
316     QList<QByteArray> params;
317     params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
318     emit putCmd("TOPIC", params);
319   } else {
320     emit networkConnection()->putRawLine("TOPIC " + serverEncode(bufferInfo.bufferName()) + " :");
321   }
322 }
323
324 void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg) {
325   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
326   QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'v';
327   QStringList params;
328   params << bufferInfo.bufferName() << m << nicks;
329   emit putCmd("MODE", serverEncode(params));
330 }
331
332 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
333   Q_UNUSED(bufferInfo)
334   emit putCmd("WHO", serverEncode(msg.split(' ')));
335 }
336
337 void UserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg) {
338   Q_UNUSED(bufferInfo)
339   emit putCmd("WHOIS", serverEncode(msg.split(' ')));
340 }
341
342 void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg) {
343   Q_UNUSED(bufferInfo)
344   emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
345 }
346
347 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
348   for(int i = 0; i < coreSession()->aliasManager().count(); i++) {
349     if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) {
350       expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg);
351       return;
352     }
353   }
354   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
355 }
356
357 void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
358   QStringList commands = alias.split(QRegExp("; ?"));
359   QStringList params = msg.split(' ');
360   for(int i = 0; i < commands.count(); i++) {
361     QString command = commands[i];
362     for(int j = params.count(); j > 0; j--) {
363       command = command.replace(QString("$%1").arg(j), params[j - 1]);
364     }
365     command = command.replace("$0", msg);
366     command = command.replace("$channelname", bufferInfo.bufferName());
367     command = command.replace("$currentnick", network()->myNick());
368     handleUserInput(bufferInfo, command);
369   }
370 }
371
372
373