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