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))) {
46 cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
47 msg = msg.section(' ', 1);
49 handle(cmd, Q_ARG(BufferInfo, bufferInfo), Q_ARG(QString, msg));
50 } catch(Exception e) {
51 emit displayMsg(Message::Error, bufferInfo.type(), "", e.msg());
55 // ====================
57 // ====================
59 void UserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg) {
62 QString awayMsg = msg;
63 IrcUser *me = network()->me();
65 // if there is no message supplied we have to check if we are already away or not
67 if(me && !me->isAway())
68 awayMsg = networkConnection()->identity()->awayReason();
71 me->setAwayMessage(awayMsg);
73 putCmd("AWAY", serverEncode(awayMsg));
76 void UserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg) {
80 QStringList params = msg.split(" ");
82 if(!params.isEmpty() && isChannelName(params[0])) {
83 banChannel = params.takeFirst();
84 } else if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
85 banChannel = bufferInfo.bufferName();
87 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: channel unknown in command: /BAN %1").arg(msg));
91 if(!params.isEmpty() && !params.contains("!") && network()->ircUser(params[0])) {
92 IrcUser *ircuser = network()->ircUser(params[0]);
93 // generalizedHost changes <nick> to *!ident@*.sld.tld.
94 QString generalizedHost = ircuser->host();
95 if(generalizedHost.isEmpty()) {
96 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: host unknown in command: /BAN %1").arg(msg));
100 if(generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
101 int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1);
102 generalizedHost.replace(0, secondLastPeriodPosition, "*");
104 banUser = QString("*!%1@%2").arg(ircuser->user()).arg(generalizedHost);
106 banUser = params.join(" ");
109 QString banMsg = QString("MODE %1 +b %2").arg(banChannel).arg(banUser);
110 emit putRawLine(serverEncode(banMsg));
113 void UserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg) {
115 QString nick = msg.section(' ', 0, 0);
116 QString ctcpTag = msg.section(' ', 1, 1).toUpper();
117 if (ctcpTag.isEmpty()) return;
118 QString message = "";
119 QString verboseMessage = tr("sending CTCP-%1 request").arg(ctcpTag);
121 if(ctcpTag == "PING") {
122 uint now = QDateTime::currentDateTime().toTime_t();
123 message = QString::number(now);
126 networkConnection()->ctcpHandler()->query(nick, ctcpTag, message);
127 emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
130 void UserInputHandler::handleDeop(const BufferInfo &bufferInfo, const QString &msg) {
131 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
132 QString m = "-"; for(int i = 0; i < nicks.count(); i++) m += 'o';
134 params << bufferInfo.bufferName() << m << nicks;
135 emit putCmd("MODE", serverEncode(params));
138 void UserInputHandler::handleDevoice(const BufferInfo &bufferInfo, const QString &msg) {
139 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
140 QString m = "-"; for(int i = 0; i < nicks.count(); i++) m += 'v';
142 params << bufferInfo.bufferName() << m << nicks;
143 emit putCmd("MODE", serverEncode(params));
146 void UserInputHandler::handleInvite(const BufferInfo &bufferInfo, const QString &msg) {
148 params << msg << bufferInfo.bufferName();
149 emit putCmd("INVITE", serverEncode(params));
152 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
154 QStringList params = msg.trimmed().split(" ");
155 QStringList chans = params[0].split(",");
157 if(params.count() > 1) keys = params[1].split(",");
158 emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
160 for(; i < keys.count(); i++) {
161 if(i >= chans.count()) break;
162 networkConnection()->addChannelKey(chans[i], keys[i]);
164 for(; i < chans.count(); i++) {
165 networkConnection()->removeChannelKey(chans[i]);
169 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
170 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
171 QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
173 reason = networkConnection()->identity()->kickReason();
175 QList<QByteArray> params;
176 params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
177 emit putCmd("KICK", params);
180 void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
182 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
183 QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
184 QList<QByteArray> params;
185 params << serverEncode(nick) << serverEncode(pass);
186 emit putCmd("KILL", params);
190 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
192 emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
195 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
196 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
197 networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
198 emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
201 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
204 QStringList params = msg.split(' ', QString::SkipEmptyParts);
205 // 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
206 if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
207 params.prepend(bufferInfo.bufferName());
209 // TODO handle correct encoding for buffer modes (channelEncode())
210 emit putCmd("MODE", serverEncode(params));
213 // TODO: show privmsgs
214 void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg) {
215 Q_UNUSED(bufferInfo);
216 if(!msg.contains(' '))
219 QList<QByteArray> params;
220 params << serverEncode(msg.section(' ', 0, 0));
221 params << userEncode(params[0], msg.section(' ', 1));
223 emit putCmd("PRIVMSG", params);
226 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
228 QString nick = msg.section(' ', 0, 0);
229 emit putCmd("NICK", serverEncode(nick));
232 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
233 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
234 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
236 params << bufferInfo.bufferName() << m << nicks;
237 emit putCmd("MODE", serverEncode(params));
240 void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
242 emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
245 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
246 QList<QByteArray> params;
249 // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
250 QString channelName = msg.section(' ', 0, 0);
251 if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
252 channelName = bufferInfo.bufferName();
255 partReason = msg.mid(channelName.length() + 1);
258 if(partReason.isEmpty())
259 partReason = networkConnection()->identity()->partReason();
261 params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
262 emit putCmd("PART", params);
265 void UserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg) {
270 param = QTime::currentTime().toString("hh:mm:ss.zzz");
272 putCmd("PING", serverEncode(param));
275 // TODO: implement queries
276 void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
278 QString target = msg.section(' ', 0, 0);
279 QString message = msg.section(' ', 1);
280 if(message.isEmpty())
281 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, "Starting query with " + target, network()->myNick(), Message::Self);
283 emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
284 handleMsg(bufferInfo, msg);
287 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
292 quitReason = networkConnection()->identity()->quitReason();
296 emit putCmd("QUIT", serverEncode(quitReason));
297 networkConnection()->disconnectFromIrc();
300 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
302 emit putRawLine(serverEncode(msg));
305 void UserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg) {
306 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
307 QList<QByteArray> params;
308 params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
309 emit putCmd("PRIVMSG", params);
310 emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
313 void UserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
314 if(bufferInfo.bufferName().isEmpty()) return;
316 QList<QByteArray> params;
317 params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
318 emit putCmd("TOPIC", params);
320 emit networkConnection()->putRawLine("TOPIC " + serverEncode(bufferInfo.bufferName()) + " :");
324 void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg) {
325 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
326 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'v';
328 params << bufferInfo.bufferName() << m << nicks;
329 emit putCmd("MODE", serverEncode(params));
332 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
334 emit putCmd("WHO", serverEncode(msg.split(' ')));
337 void UserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg) {
339 emit putCmd("WHOIS", serverEncode(msg.split(' ')));
342 void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg) {
344 emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
347 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
348 for(int i = 0; i < coreSession()->aliasManager().count(); i++) {
349 if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) {
350 expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg);
354 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
357 void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
358 QStringList commands = alias.split(QRegExp("; ?"));
359 QStringList params = msg.split(' ');
360 for(int i = 0; i < commands.count(); i++) {
361 QString command = commands[i];
362 for(int j = params.count(); j > 0; j--) {
363 command = command.replace(QString("$%1").arg(j), params[j - 1]);
365 command = command.replace("$0", msg);
366 command = command.replace("$channelname", bufferInfo.bufferName());
367 command = command.replace("$currentnick", network()->myNick());
368 handleUserInput(bufferInfo, command);