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 // 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(msg.startsWith("//"))
45 msg.remove(0, 1); // //asdf is transformed to /asdf
48 cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
49 msg = msg.section(' ', 1);
51 handle(cmd, Q_ARG(BufferInfo, bufferInfo), Q_ARG(QString, msg));
52 } catch(Exception e) {
53 emit displayMsg(Message::Error, bufferInfo.type(), "", e.msg());
57 // ====================
59 // ====================
61 void UserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg) {
64 QString awayMsg = msg;
65 IrcUser *me = network()->me();
67 // if there is no message supplied we have to check if we are already away or not
69 if(me && !me->isAway())
70 awayMsg = networkConnection()->identity()->awayReason();
73 me->setAwayMessage(awayMsg);
75 putCmd("AWAY", serverEncode(awayMsg));
78 void UserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg) {
82 QStringList params = msg.split(" ");
84 if(!params.isEmpty() && isChannelName(params[0])) {
85 banChannel = params.takeFirst();
86 } else if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
87 banChannel = bufferInfo.bufferName();
89 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: channel unknown in command: /BAN %1").arg(msg));
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));
102 if(generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
103 int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1);
104 generalizedHost.replace(0, secondLastPeriodPosition, "*");
106 banUser = QString("*!%1@%2").arg(ircuser->user()).arg(generalizedHost);
108 banUser = params.join(" ");
111 QString banMsg = QString("MODE %1 +b %2").arg(banChannel).arg(banUser);
112 emit putRawLine(serverEncode(banMsg));
115 void UserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg) {
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);
123 if(ctcpTag == "PING") {
124 uint now = QDateTime::currentDateTime().toTime_t();
125 message = QString::number(now);
128 networkConnection()->ctcpHandler()->query(nick, ctcpTag, message);
129 emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
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';
136 params << bufferInfo.bufferName() << m << nicks;
137 emit putCmd("MODE", serverEncode(params));
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';
144 params << bufferInfo.bufferName() << m << nicks;
145 emit putCmd("MODE", serverEncode(params));
148 void UserInputHandler::handleInvite(const BufferInfo &bufferInfo, const QString &msg) {
150 params << msg << bufferInfo.bufferName();
151 emit putCmd("INVITE", serverEncode(params));
154 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
156 QStringList params = msg.trimmed().split(" ");
157 QStringList chans = params[0].split(",");
160 for(i = 0; i < chans.count(); i++) {
161 if (chans.at(i)[0].isLetterOrNumber())
162 chans[i].prepend(QChar('#'));
164 params[0] = chans.join(",");
165 if(params.count() > 1) keys = params[1].split(",");
166 emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
168 for(; i < keys.count(); i++) {
169 if(i >= chans.count()) break;
170 networkConnection()->addChannelKey(chans[i], keys[i]);
172 for(; i < chans.count(); i++) {
173 networkConnection()->removeChannelKey(chans[i]);
177 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
178 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
179 QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
181 reason = networkConnection()->identity()->kickReason();
183 QList<QByteArray> params;
184 params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
185 emit putCmd("KICK", params);
188 void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
190 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
191 QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
192 QList<QByteArray> params;
193 params << serverEncode(nick) << serverEncode(pass);
194 emit putCmd("KILL", params);
198 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
200 emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
203 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
204 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
205 networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
206 emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
209 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
212 QStringList params = msg.split(' ', QString::SkipEmptyParts);
213 // 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
214 if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
215 params.prepend(bufferInfo.bufferName());
217 // TODO handle correct encoding for buffer modes (channelEncode())
218 emit putCmd("MODE", serverEncode(params));
221 // TODO: show privmsgs
222 void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg) {
223 Q_UNUSED(bufferInfo);
224 if(!msg.contains(' '))
227 QList<QByteArray> params;
228 params << serverEncode(msg.section(' ', 0, 0));
229 params << userEncode(params[0], msg.section(' ', 1));
231 emit putCmd("PRIVMSG", params);
234 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
236 QString nick = msg.section(' ', 0, 0);
237 emit putCmd("NICK", serverEncode(nick));
240 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
241 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
242 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
244 params << bufferInfo.bufferName() << m << nicks;
245 emit putCmd("MODE", serverEncode(params));
248 void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
250 emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
253 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
254 QList<QByteArray> params;
257 // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
258 QString channelName = msg.section(' ', 0, 0);
259 if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
260 channelName = bufferInfo.bufferName();
263 partReason = msg.mid(channelName.length() + 1);
266 if(partReason.isEmpty())
267 partReason = networkConnection()->identity()->partReason();
269 params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
270 emit putCmd("PART", params);
273 void UserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg) {
278 param = QTime::currentTime().toString("hh:mm:ss.zzz");
280 putCmd("PING", serverEncode(param));
283 // TODO: implement queries
284 void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
286 QString target = msg.section(' ', 0, 0);
287 QString message = msg.section(' ', 1);
288 if(message.isEmpty())
289 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, "Starting query with " + target, network()->myNick(), Message::Self);
291 emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
292 handleMsg(bufferInfo, msg);
295 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
300 quitReason = networkConnection()->identity()->quitReason();
304 emit putCmd("QUIT", serverEncode(quitReason));
305 networkConnection()->disconnectFromIrc();
308 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
310 emit putRawLine(serverEncode(msg));
313 void UserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg) {
314 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
315 QList<QByteArray> params;
316 params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
317 emit putCmd("PRIVMSG", params);
318 emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
321 void UserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
322 if(bufferInfo.bufferName().isEmpty()) return;
324 QList<QByteArray> params;
325 params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
326 emit putCmd("TOPIC", params);
328 emit networkConnection()->putRawLine("TOPIC " + serverEncode(bufferInfo.bufferName()) + " :");
332 void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg) {
333 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
334 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'v';
336 params << bufferInfo.bufferName() << m << nicks;
337 emit putCmd("MODE", serverEncode(params));
340 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
342 emit putCmd("WHO", serverEncode(msg.split(' ')));
345 void UserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg) {
347 emit putCmd("WHOIS", serverEncode(msg.split(' ')));
350 void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg) {
352 emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
355 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
356 for(int i = 0; i < coreSession()->aliasManager().count(); i++) {
357 if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) {
358 expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg);
362 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
365 void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
366 QStringList commands = alias.split(QRegExp("; ?"));
367 QStringList params = msg.split(' ');
368 for(int i = 0; i < commands.count(); i++) {
369 QString command = commands[i];
370 for(int j = params.count(); j > 0; j--) {
371 IrcUser *ircUser = network()->ircUser(params[j - 1]);
372 command = command.replace(QString("$%1:hostname").arg(j), ircUser ? ircUser->host() : QString("*"));
373 command = command.replace(QString("$%1").arg(j), params[j - 1]);
375 command = command.replace("$0", msg);
376 command = command.replace("$channelname", bufferInfo.bufferName());
377 command = command.replace("$currentnick", network()->myNick());
378 handleUserInput(bufferInfo, command);