1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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"
24 #include "networkconnection.h"
26 #include "ctcphandler.h"
31 UserInputHandler::UserInputHandler(NetworkConnection *parent) : BasicHandler(parent) {
34 void UserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg_) {
40 if(!msg.startsWith('/')) {
43 cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
44 msg = msg.section(' ', 1);
46 handle(cmd, Q_ARG(BufferInfo, bufferInfo), Q_ARG(QString, msg));
47 } catch(Exception e) {
48 emit displayMsg(Message::Error, bufferInfo.type(), "", e.msg());
52 // ====================
54 // ====================
56 void UserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg) {
59 QString awayMsg = msg;
60 IrcUser *me = network()->me();
62 // if there is no message supplied we have to check if we are already away or not
64 if(me && !me->isAway())
65 awayMsg = networkConnection()->identity()->awayReason();
68 me->setAwayMessage(awayMsg);
70 putCmd("AWAY", serverEncode(awayMsg));
73 void UserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg) {
77 QStringList params = msg.split(" ");
79 if(!params.isEmpty() && isChannelName(params[0])) {
80 banChannel = params.takeFirst();
81 } else if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
82 banChannel = bufferInfo.bufferName();
84 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: channel unknown in command: /BAN %1").arg(msg));
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));
97 if(generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
98 int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1);
99 generalizedHost.replace(0, secondLastPeriodPosition, "*");
101 banUser = QString("*!%1@%2").arg(ircuser->user()).arg(generalizedHost);
103 banUser = params.join(" ");
106 QString banMsg = QString("MODE %1 +b %2").arg(banChannel).arg(banUser);
107 emit putRawLine(serverEncode(banMsg));
110 void UserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg) {
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);
118 if(ctcpTag == "PING") {
119 uint now = QDateTime::currentDateTime().toTime_t();
120 message = QString::number(now);
123 networkConnection()->ctcpHandler()->query(nick, ctcpTag, message);
124 emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
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';
131 params << bufferInfo.bufferName() << m << nicks;
132 emit putCmd("MODE", serverEncode(params));
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';
139 params << bufferInfo.bufferName() << m << nicks;
140 emit putCmd("MODE", serverEncode(params));
143 void UserInputHandler::handleInvite(const BufferInfo &bufferInfo, const QString &msg) {
145 params << msg << bufferInfo.bufferName();
146 emit putCmd("INVITE", serverEncode(params));
149 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
151 QStringList params = msg.trimmed().split(" ");
152 QStringList chans = params[0].split(",");
154 if(params.count() > 1) keys = params[1].split(",");
155 emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
157 for(; i < keys.count(); i++) {
158 if(i >= chans.count()) break;
159 networkConnection()->addChannelKey(chans[i], keys[i]);
161 for(; i < chans.count(); i++) {
162 networkConnection()->removeChannelKey(chans[i]);
166 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
167 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
168 QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
170 reason = networkConnection()->identity()->kickReason();
172 QList<QByteArray> params;
173 params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
174 emit putCmd("KICK", params);
177 void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
179 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
180 QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
181 QList<QByteArray> params;
182 params << serverEncode(nick) << serverEncode(pass);
183 emit putCmd("KILL", params);
187 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
189 emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
192 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
193 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
194 networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
195 emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
198 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
201 QStringList params = msg.split(' ', QString::SkipEmptyParts);
202 // 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
203 if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
204 params.prepend(bufferInfo.bufferName());
206 // TODO handle correct encoding for buffer modes (channelEncode())
207 emit putCmd("MODE", serverEncode(params));
210 // TODO: show privmsgs
211 void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg) {
212 Q_UNUSED(bufferInfo);
213 if(!msg.contains(' '))
216 QList<QByteArray> params;
217 params << serverEncode(msg.section(' ', 0, 0));
218 params << userEncode(params[0], msg.section(' ', 1));
220 emit putCmd("PRIVMSG", params);
223 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
225 QString nick = msg.section(' ', 0, 0);
226 emit putCmd("NICK", serverEncode(nick));
229 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
230 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
231 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
233 params << bufferInfo.bufferName() << m << nicks;
234 emit putCmd("MODE", serverEncode(params));
237 void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
239 emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
242 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
243 QList<QByteArray> params;
246 // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
247 QString channelName = msg.section(' ', 0, 0);
248 if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
249 channelName = bufferInfo.bufferName();
252 partReason = msg.mid(channelName.length() + 1);
255 if(partReason.isEmpty())
256 partReason = networkConnection()->identity()->partReason();
258 params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
259 emit putCmd("PART", params);
262 void UserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg) {
267 param = QTime::currentTime().toString("hh:mm:ss.zzz");
269 putCmd("PING", serverEncode(param));
272 // TODO: implement queries
273 void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
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);
280 emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
281 handleMsg(bufferInfo, msg);
284 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
289 quitReason = networkConnection()->identity()->quitReason();
293 emit putCmd("QUIT", serverEncode(quitReason));
294 networkConnection()->disconnectFromIrc();
297 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
299 emit putRawLine(serverEncode(msg));
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);
310 void UserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
311 if(bufferInfo.bufferName().isEmpty()) return;
313 QList<QByteArray> params;
314 params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
315 emit putCmd("TOPIC", params);
317 emit networkConnection()->putRawLine("TOPIC " + serverEncode(bufferInfo.bufferName()) + " :");
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';
325 params << bufferInfo.bufferName() << m << nicks;
326 emit putCmd("MODE", serverEncode(params));
329 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
331 emit putCmd("WHO", serverEncode(msg.split(' ')));
334 void UserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg) {
336 emit putCmd("WHOIS", serverEncode(msg.split(' ')));
339 void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg) {
341 emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
344 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
345 for(int i = 0; i < coreSession()->aliasManager().count(); i++) {
346 if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) {
347 expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg);
351 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
354 void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
355 QStringList commands = alias.split(QRegExp("; ?"));
356 QStringList params = msg.split(' ');
357 for(int i = 0; i < commands.count(); i++) {
358 QString command = commands[i];
359 for(int j = params.count(); j > 0; j--) {
360 command = command.replace(QString("$%1").arg(j), params[j - 1]);
362 command = command.replace("$0", msg);
363 command = command.replace("$channelname", bufferInfo.bufferName());
364 command = command.replace("$currentnick", network()->myNick());
365 handleUserInput(bufferInfo, command);