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::handleJ(const BufferInfo &bufferInfo, const QString &msg) {
153 QString trimmed = msg.trimmed();
154 if(trimmed.length() == 0) return;
155 if(trimmed[0].isLetter()) trimmed.prepend("#");
156 handleJoin(bufferInfo, trimmed);
159 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
161 QStringList params = msg.trimmed().split(" ");
162 QStringList chans = params[0].split(",");
164 if(params.count() > 1) keys = params[1].split(",");
165 emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
167 for(; i < keys.count(); i++) {
168 if(i >= chans.count()) break;
169 networkConnection()->addChannelKey(chans[i], keys[i]);
171 for(; i < chans.count(); i++) {
172 networkConnection()->removeChannelKey(chans[i]);
176 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
177 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
178 QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
180 reason = networkConnection()->identity()->kickReason();
182 QList<QByteArray> params;
183 params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
184 emit putCmd("KICK", params);
187 void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
189 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
190 QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
191 QList<QByteArray> params;
192 params << serverEncode(nick) << serverEncode(pass);
193 emit putCmd("KILL", params);
197 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
199 emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
202 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
203 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
204 networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
205 emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
208 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
211 QStringList params = msg.split(' ', QString::SkipEmptyParts);
212 // 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
213 if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
214 params.prepend(bufferInfo.bufferName());
216 // TODO handle correct encoding for buffer modes (channelEncode())
217 emit putCmd("MODE", serverEncode(params));
220 // TODO: show privmsgs
221 void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg) {
222 Q_UNUSED(bufferInfo);
223 if(!msg.contains(' '))
226 QList<QByteArray> params;
227 params << serverEncode(msg.section(' ', 0, 0));
228 params << userEncode(params[0], msg.section(' ', 1));
230 emit putCmd("PRIVMSG", params);
233 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
235 QString nick = msg.section(' ', 0, 0);
236 emit putCmd("NICK", serverEncode(nick));
239 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
240 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
241 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
243 params << bufferInfo.bufferName() << m << nicks;
244 emit putCmd("MODE", serverEncode(params));
247 void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
249 emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
252 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
253 QList<QByteArray> params;
256 // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
257 QString channelName = msg.section(' ', 0, 0);
258 if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
259 channelName = bufferInfo.bufferName();
262 partReason = msg.mid(channelName.length() + 1);
265 if(partReason.isEmpty())
266 partReason = networkConnection()->identity()->partReason();
268 params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
269 emit putCmd("PART", params);
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) {
346 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));