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::handleJ(const BufferInfo &bufferInfo, const QString &msg) {
150 QString trimmed = msg.trimmed();
151 if(trimmed.length() == 0) return;
152 if(trimmed[0].isLetter()) trimmed.prepend("#");
153 handleJoin(bufferInfo, trimmed);
156 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
158 QStringList params = msg.trimmed().split(" ");
159 QStringList chans = params[0].split(",");
161 if(params.count() > 1) keys = params[1].split(",");
162 emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
164 for(; i < keys.count(); i++) {
165 if(i >= chans.count()) break;
166 networkConnection()->addChannelKey(chans[i], keys[i]);
168 for(; i < chans.count(); i++) {
169 networkConnection()->removeChannelKey(chans[i]);
173 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
174 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
175 QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
177 reason = networkConnection()->identity()->kickReason();
179 QList<QByteArray> params;
180 params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
181 emit putCmd("KICK", params);
184 void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
186 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
187 QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
188 QList<QByteArray> params;
189 params << serverEncode(nick) << serverEncode(pass);
190 emit putCmd("KILL", params);
194 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
196 emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
199 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
200 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
201 networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
202 emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
205 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
208 QStringList params = msg.split(' ', QString::SkipEmptyParts);
209 // 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
210 if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
211 params.prepend(bufferInfo.bufferName());
213 // TODO handle correct encoding for buffer modes (channelEncode())
214 emit putCmd("MODE", serverEncode(params));
217 // TODO: show privmsgs
218 void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg) {
219 Q_UNUSED(bufferInfo);
220 if(!msg.contains(' '))
223 QList<QByteArray> params;
224 params << serverEncode(msg.section(' ', 0, 0));
225 params << userEncode(params[0], msg.section(' ', 1));
227 emit putCmd("PRIVMSG", params);
230 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
232 QString nick = msg.section(' ', 0, 0);
233 emit putCmd("NICK", serverEncode(nick));
236 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
237 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
238 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
240 params << bufferInfo.bufferName() << m << nicks;
241 emit putCmd("MODE", serverEncode(params));
244 void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
246 emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
249 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
250 QList<QByteArray> params;
253 // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
254 QString channelName = msg.section(' ', 0, 0);
255 if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
256 channelName = bufferInfo.bufferName();
259 partReason = msg.mid(channelName.length() + 1);
262 if(partReason.isEmpty())
263 partReason = networkConnection()->identity()->partReason();
265 params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
266 emit putCmd("PART", params);
269 // TODO: implement queries
270 void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
272 QString target = msg.section(' ', 0, 0);
273 QString message = msg.section(' ', 1);
274 if(message.isEmpty())
275 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, "Starting query with " + target, network()->myNick(), Message::Self);
277 emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
278 handleMsg(bufferInfo, msg);
281 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
286 quitReason = networkConnection()->identity()->quitReason();
290 emit putCmd("QUIT", serverEncode(quitReason));
291 networkConnection()->disconnectFromIrc();
294 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
296 emit putRawLine(serverEncode(msg));
299 void UserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg) {
300 if(bufferInfo.bufferName().isEmpty()) return; // server buffer
301 QList<QByteArray> params;
302 params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
303 emit putCmd("PRIVMSG", params);
304 emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
307 void UserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
308 if(bufferInfo.bufferName().isEmpty()) return;
310 QList<QByteArray> params;
311 params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
312 emit putCmd("TOPIC", params);
314 emit networkConnection()->putRawLine("TOPIC " + serverEncode(bufferInfo.bufferName()) + " :");
318 void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg) {
319 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
320 QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'v';
322 params << bufferInfo.bufferName() << m << nicks;
323 emit putCmd("MODE", serverEncode(params));
326 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
328 emit putCmd("WHO", serverEncode(msg.split(' ')));
331 void UserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg) {
333 emit putCmd("WHOIS", serverEncode(msg.split(' ')));
336 void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg) {
338 emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
341 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
343 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));