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