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"
32 UserInputHandler::UserInputHandler(NetworkConnection *parent) : BasicHandler(parent) {
35 void UserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg_) {
41 // leading slashes indicate there's a command to call unless there is anothere one in the first section (like a path /proc/cpuinfo)
42 int secondSlashPos = msg.indexOf('/', 1);
43 int firstSpacePos = msg.indexOf(' ');
44 if(!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
45 if(msg.startsWith("//"))
46 msg.remove(0, 1); // //asdf is transformed to /asdf
49 cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
50 msg = msg.section(' ', 1);
52 handle(cmd, Q_ARG(BufferInfo, bufferInfo), Q_ARG(QString, msg));
53 } catch(Exception e) {
54 emit displayMsg(Message::Error, bufferInfo.type(), "", e.msg());
58 // ====================
60 // ====================
62 void UserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg) {
65 QString awayMsg = msg;
66 IrcUser *me = network()->me();
68 // if there is no message supplied we have to check if we are already away or not
70 if(me && !me->isAway())
71 awayMsg = networkConnection()->identity()->awayReason();
74 me->setAwayMessage(awayMsg);
76 putCmd("AWAY", serverEncode(awayMsg));
79 void UserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg) {
83 QStringList params = msg.split(" ");
85 if(!params.isEmpty() && isChannelName(params[0])) {
86 banChannel = params.takeFirst();
87 } else if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
88 banChannel = bufferInfo.bufferName();
90 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: channel unknown in command: /BAN %1").arg(msg));
94 if(!params.isEmpty() && !params.contains("!") && network()->ircUser(params[0])) {
95 IrcUser *ircuser = network()->ircUser(params[0]);
96 // generalizedHost changes <nick> to *!ident@*.sld.tld.
97 QString generalizedHost = ircuser->host();
98 if(generalizedHost.isEmpty()) {
99 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: host unknown in command: /BAN %1").arg(msg));
103 if(generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
104 int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1);
105 generalizedHost.replace(0, secondLastPeriodPosition, "*");
107 banUser = QString("*!%1@%2").arg(ircuser->user()).arg(generalizedHost);
109 banUser = params.join(" ");
112 QString banMsg = QString("MODE %1 +b %2").arg(banChannel).arg(banUser);
113 emit putRawLine(serverEncode(banMsg));
116 void UserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg) {
118 QString nick = msg.section(' ', 0, 0);
119 QString ctcpTag = msg.section(' ', 1, 1).toUpper();
120 if (ctcpTag.isEmpty()) return;
121 QString message = "";
122 QString verboseMessage = tr("sending CTCP-%1 request").arg(ctcpTag);
124 if(ctcpTag == "PING") {
125 uint now = QDateTime::currentDateTime().toTime_t();
126 message = QString::number(now);
129 networkConnection()->ctcpHandler()->query(nick, ctcpTag, message);
130 emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
133 void UserInputHandler::handleDeop(const BufferInfo &bufferInfo, const QString &msg) {
134 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
135 QString m = "-"; for(int i = 0; i < nicks.count(); i++) m += 'o';
137 params << bufferInfo.bufferName() << m << nicks;
138 emit putCmd("MODE", serverEncode(params));
141 void UserInputHandler::handleDevoice(const BufferInfo &bufferInfo, const QString &msg) {
142 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
143 QString m = "-"; for(int i = 0; i < nicks.count(); i++) m += 'v';
145 params << bufferInfo.bufferName() << m << nicks;
146 emit putCmd("MODE", serverEncode(params));
149 void UserInputHandler::handleInvite(const BufferInfo &bufferInfo, const QString &msg) {
151 params << msg << bufferInfo.bufferName();
152 emit putCmd("INVITE", serverEncode(params));
155 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
156 Q_UNUSED(bufferInfo);
158 // trim spaces before chans or keys
159 QString sane_msg = msg;
160 sane_msg.replace(QRegExp(", +"), ",");
161 QStringList params = sane_msg.trimmed().split(" ");
162 QStringList chans = params[0].split(",");
165 for(i = 0; i < chans.count(); i++) {
166 if(!network()->isChannelName(chans[i]))
167 chans[i].prepend('#');
169 params[0] = chans.join(",");
170 if(params.count() > 1) keys = params[1].split(",");
171 emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
173 for(; i < keys.count(); i++) {
174 if(i >= chans.count()) break;
175 networkConnection()->addChannelKey(chans[i], keys[i]);
177 for(; i < chans.count(); i++) {
178 networkConnection()->removeChannelKey(chans[i]);
182 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
183 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
184 QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
186 reason = networkConnection()->identity()->kickReason();
188 QList<QByteArray> params;
189 params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
190 emit putCmd("KICK", params);
193 void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
195 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
196 QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
197 QList<QByteArray> params;
198 params << serverEncode(nick) << serverEncode(pass);
199 emit putCmd("KILL", params);
203 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
205 emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
208 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
209 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
210 networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
211 emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
214 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
217 QStringList params = msg.split(' ', QString::SkipEmptyParts);
218 // 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
219 if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
220 params.prepend(bufferInfo.bufferName());
222 // TODO handle correct encoding for buffer modes (channelEncode())
223 emit putCmd("MODE", serverEncode(params));
226 // TODO: show privmsgs
227 void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg) {
228 Q_UNUSED(bufferInfo);
229 if(!msg.contains(' '))
232 QList<QByteArray> params;
233 params << serverEncode(msg.section(' ', 0, 0));
234 params << userEncode(params[0], msg.section(' ', 1));
236 emit putCmd("PRIVMSG", params);
239 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
241 QString nick = msg.section(' ', 0, 0);
242 emit putCmd("NICK", serverEncode(nick));
245 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
246 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
247 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
249 params << bufferInfo.bufferName() << m << nicks;
250 emit putCmd("MODE", serverEncode(params));
253 void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
255 emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
258 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
259 QList<QByteArray> params;
262 // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
263 QString channelName = msg.section(' ', 0, 0);
264 if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
265 channelName = bufferInfo.bufferName();
268 partReason = msg.mid(channelName.length() + 1);
271 if(partReason.isEmpty())
272 partReason = networkConnection()->identity()->partReason();
274 params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
275 emit putCmd("PART", params);
278 void UserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg) {
283 param = QTime::currentTime().toString("hh:mm:ss.zzz");
285 putCmd("PING", serverEncode(param));
288 // TODO: implement queries
289 void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
291 QString target = msg.section(' ', 0, 0);
292 QString message = msg.section(' ', 1);
293 if(message.isEmpty())
294 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, "Starting query with " + target, network()->myNick(), Message::Self);
296 emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
297 handleMsg(bufferInfo, msg);
300 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
305 quitReason = networkConnection()->identity()->quitReason();
309 emit putCmd("QUIT", serverEncode(quitReason));
310 networkConnection()->disconnectFromIrc();
313 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
315 emit putRawLine(serverEncode(msg));
318 void UserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg) {
319 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
320 QList<QByteArray> params;
321 params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
322 emit putCmd("PRIVMSG", params);
323 emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
326 void UserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
327 if(bufferInfo.bufferName().isEmpty()) return;
329 QList<QByteArray> params;
330 params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
331 emit putCmd("TOPIC", params);
333 emit networkConnection()->putRawLine("TOPIC " + serverEncode(bufferInfo.bufferName()) + " :");
337 void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg) {
338 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
339 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'v';
341 params << bufferInfo.bufferName() << m << nicks;
342 emit putCmd("MODE", serverEncode(params));
345 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
347 emit putCmd("WHO", serverEncode(msg.split(' ')));
350 void UserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg) {
352 emit putCmd("WHOIS", serverEncode(msg.split(' ')));
355 void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg) {
357 emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
360 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
361 for(int i = 0; i < coreSession()->aliasManager().count(); i++) {
362 if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) {
363 expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg);
367 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
370 void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
371 QStringList commands = alias.split(QRegExp("; ?"));
372 QStringList params = msg.split(' ');
373 for(int i = 0; i < commands.count(); i++) {
374 QString command = commands[i];
375 for(int j = params.count(); j > 0; j--) {
376 IrcUser *ircUser = network()->ircUser(params[j - 1]);
377 command = command.replace(QString("$%1:hostname").arg(j), ircUser ? ircUser->host() : QString("*"));
378 command = command.replace(QString("$%1").arg(j), params[j - 1]);
380 command = command.replace("$0", msg);
381 command = command.replace("$channelname", bufferInfo.bufferName());
382 command = command.replace("$currentnick", network()->myNick());
383 handleUserInput(bufferInfo, command);