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