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::handleJ(const BufferInfo &bufferInfo, const QString &msg) {
153   QString trimmed = msg.trimmed();
154   if(trimmed.length() == 0) return;
155   if(trimmed[0].isLetter()) trimmed.prepend("#");
156   handleJoin(bufferInfo, trimmed);
157 }
158
159 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
160   Q_UNUSED(bufferInfo)
161   QStringList params = msg.trimmed().split(" ");
162   QStringList chans = params[0].split(",");
163   QStringList keys;
164   if(params.count() > 1) keys = params[1].split(",");
165   emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
166   int i = 0;
167   for(; i < keys.count(); i++) {
168     if(i >= chans.count()) break;
169     networkConnection()->addChannelKey(chans[i], keys[i]);
170   }
171   for(; i < chans.count(); i++) {
172     networkConnection()->removeChannelKey(chans[i]);
173   }
174 }
175
176 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
177   QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
178   QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
179   if(reason.isEmpty())
180     reason = networkConnection()->identity()->kickReason();
181
182   QList<QByteArray> params;
183   params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
184   emit putCmd("KICK", params);
185 }
186
187 void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
188   Q_UNUSED(bufferInfo)
189   QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
190   QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
191   QList<QByteArray> params;
192   params << serverEncode(nick) << serverEncode(pass);
193   emit putCmd("KILL", params);
194 }
195
196
197 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
198   Q_UNUSED(bufferInfo)
199   emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
200 }
201
202 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
203   if(bufferInfo.bufferName().isEmpty()) return; // server buffer
204   networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
205   emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
206 }
207
208 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
209   Q_UNUSED(bufferInfo)
210
211   QStringList params = msg.split(' ', QString::SkipEmptyParts);
212   // 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
213   if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
214     params.prepend(bufferInfo.bufferName());
215   
216   // TODO handle correct encoding for buffer modes (channelEncode())
217   emit putCmd("MODE", serverEncode(params));
218 }
219
220 // TODO: show privmsgs
221 void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg) {
222   Q_UNUSED(bufferInfo);
223   if(!msg.contains(' '))
224     return;
225
226   QList<QByteArray> params;
227   params << serverEncode(msg.section(' ', 0, 0));
228   params << userEncode(params[0], msg.section(' ', 1));
229
230   emit putCmd("PRIVMSG", params);
231 }
232
233 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
234   Q_UNUSED(bufferInfo)
235   QString nick = msg.section(' ', 0, 0);
236   emit putCmd("NICK", serverEncode(nick));
237 }
238
239 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
240   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
241   QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
242   QStringList params;
243   params << bufferInfo.bufferName() << m << nicks;
244   emit putCmd("MODE", serverEncode(params));
245 }
246
247 void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
248   Q_UNUSED(bufferInfo)
249   emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
250 }
251
252 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
253   QList<QByteArray> params;
254   QString partReason;
255
256   // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
257   QString channelName = msg.section(' ', 0, 0);
258   if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
259     channelName = bufferInfo.bufferName();
260     partReason = msg;
261   } else {
262     partReason = msg.mid(channelName.length() + 1);
263   }
264   
265   if(partReason.isEmpty())
266     partReason = networkConnection()->identity()->partReason();
267
268   params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
269   emit putCmd("PART", params);
270 }
271
272 // TODO: implement queries
273 void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
274   Q_UNUSED(bufferInfo)
275   QString target = msg.section(' ', 0, 0);
276   QString message = msg.section(' ', 1);
277   if(message.isEmpty())
278     emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, "Starting query with " + target, network()->myNick(), Message::Self);
279   else
280     emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
281   handleMsg(bufferInfo, msg);
282 }
283
284 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
285   Q_UNUSED(bufferInfo)
286
287   QString quitReason;
288   if(msg.isEmpty())
289     quitReason = networkConnection()->identity()->quitReason();
290   else
291     quitReason = msg;
292
293   emit putCmd("QUIT", serverEncode(quitReason));
294   networkConnection()->disconnectFromIrc();
295 }
296
297 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
298   Q_UNUSED(bufferInfo)
299   emit putRawLine(serverEncode(msg));
300 }
301
302 void UserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg) {
303   if(bufferInfo.bufferName().isEmpty()) return;  // server buffer
304   QList<QByteArray> params;
305   params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
306   emit putCmd("PRIVMSG", params);
307   emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
308 }
309
310 void UserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
311   if(bufferInfo.bufferName().isEmpty()) return;
312   if(!msg.isEmpty()) {
313     QList<QByteArray> params;
314     params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
315     emit putCmd("TOPIC", params);
316   } else {
317     emit networkConnection()->putRawLine("TOPIC " + serverEncode(bufferInfo.bufferName()) + " :");
318   }
319 }
320
321 void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg) {
322   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
323   QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'v';
324   QStringList params;
325   params << bufferInfo.bufferName() << m << nicks;
326   emit putCmd("MODE", serverEncode(params));
327 }
328
329 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
330   Q_UNUSED(bufferInfo)
331   emit putCmd("WHO", serverEncode(msg.split(' ')));
332 }
333
334 void UserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg) {
335   Q_UNUSED(bufferInfo)
336   emit putCmd("WHOIS", serverEncode(msg.split(' ')));
337 }
338
339 void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg) {
340   Q_UNUSED(bufferInfo)
341   emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
342 }
343
344 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
345   Q_UNUSED(bufferInfo)
346   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
347 }
348
349