1 /***************************************************************************
2 * Copyright (C) 2005-09 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 "ctcphandler.h"
27 CtcpHandler::CtcpHandler(CoreNetwork *parent)
28 : BasicHandler(parent),
32 QByteArray MQUOTE = QByteArray("\020");
33 ctcpMDequoteHash[MQUOTE + '0'] = QByteArray(1, '\000');
34 ctcpMDequoteHash[MQUOTE + 'n'] = QByteArray(1, '\n');
35 ctcpMDequoteHash[MQUOTE + 'r'] = QByteArray(1, '\r');
36 ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
38 QByteArray XQUOTE = QByteArray("\134");
39 ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
40 ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
43 QByteArray CtcpHandler::lowLevelQuote(const QByteArray &message) {
44 QByteArray quotedMessage = message;
46 QHash<QByteArray, QByteArray> quoteHash = ctcpMDequoteHash;
47 QByteArray MQUOTE = QByteArray("\020");
48 quoteHash.remove(MQUOTE + MQUOTE);
49 quotedMessage.replace(MQUOTE, MQUOTE + MQUOTE);
51 QHash<QByteArray, QByteArray>::const_iterator quoteIter = quoteHash.constBegin();
52 while(quoteIter != quoteHash.constEnd()) {
53 quotedMessage.replace(quoteIter.value(), quoteIter.key());
59 QByteArray CtcpHandler::lowLevelDequote(const QByteArray &message) {
60 QByteArray dequotedMessage;
61 QByteArray messagepart;
62 QHash<QByteArray, QByteArray>::iterator ctcpquote;
64 // copy dequote Message
65 for(int i = 0; i < message.size(); i++) {
66 messagepart = message.mid(i,1);
67 if(i+1 < message.size()) {
68 for(ctcpquote = ctcpMDequoteHash.begin(); ctcpquote != ctcpMDequoteHash.end(); ++ctcpquote) {
69 if(message.mid(i,2) == ctcpquote.key()) {
70 messagepart = ctcpquote.value();
76 dequotedMessage += messagepart;
78 return dequotedMessage;
81 QByteArray CtcpHandler::xdelimQuote(const QByteArray &message) {
82 QByteArray quotedMessage = message;
83 QHash<QByteArray, QByteArray>::const_iterator quoteIter = ctcpXDelimDequoteHash.constBegin();
84 while(quoteIter != ctcpXDelimDequoteHash.constEnd()) {
85 quotedMessage.replace(quoteIter.value(), quoteIter.key());
91 QByteArray CtcpHandler::xdelimDequote(const QByteArray &message) {
92 QByteArray dequotedMessage;
93 QByteArray messagepart;
94 QHash<QByteArray, QByteArray>::iterator xdelimquote;
96 for(int i = 0; i < message.size(); i++) {
97 messagepart = message.mid(i,1);
98 if(i+1 < message.size()) {
99 for(xdelimquote = ctcpXDelimDequoteHash.begin(); xdelimquote != ctcpXDelimDequoteHash.end(); ++xdelimquote) {
100 if(message.mid(i,2) == xdelimquote.key()) {
101 messagepart = xdelimquote.value();
107 dequotedMessage += messagepart;
109 return dequotedMessage;
112 void CtcpHandler::parse(Message::Type messageType, const QString &prefix, const QString &target, const QByteArray &message) {
115 //lowlevel message dequote
116 QByteArray dequotedMessage = lowLevelDequote(message);
118 CtcpType ctcptype = messageType == Message::Notice
122 Message::Flags flags = (messageType == Message::Notice && !network()->isChannelName(target))
123 ? Message::Redirected
126 // extract tagged / extended data
128 int xdelimEndPos = -1;
130 while((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
132 displayMsg(messageType, target, userDecode(target, dequotedMessage.left(xdelimPos)), prefix, flags);
134 xdelimEndPos = dequotedMessage.indexOf(XDELIM, xdelimPos + 1);
135 if(xdelimEndPos == -1) {
136 // no matching end delimiter found... treat rest of the message as ctcp
137 xdelimEndPos = dequotedMessage.count();
139 ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
140 dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
142 //dispatch the ctcp command
143 QString ctcpcmd = userDecode(target, ctcp.left(spacePos));
144 QString ctcpparam = userDecode(target, ctcp.mid(spacePos + 1));
146 spacePos = ctcp.indexOf(' ');
148 ctcpcmd = userDecode(target, ctcp.left(spacePos));
149 ctcpparam = userDecode(target, ctcp.mid(spacePos + 1));
151 ctcpcmd = userDecode(target, ctcp);
152 ctcpparam = QString();
155 handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
158 if(!dequotedMessage.isEmpty())
159 displayMsg(messageType, target, userDecode(target, dequotedMessage), prefix, flags);
162 QByteArray CtcpHandler::pack(const QByteArray &ctcpTag, const QByteArray &message) {
163 return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
166 void CtcpHandler::query(const QString &bufname, const QString &ctcpTag, const QString &message) {
167 QList<QByteArray> params;
168 params << serverEncode(bufname) << lowLevelQuote(pack(serverEncode(ctcpTag), userEncode(bufname, message)));
169 emit putCmd("PRIVMSG", params);
172 void CtcpHandler::reply(const QString &bufname, const QString &ctcpTag, const QString &message) {
173 QList<QByteArray> params;
174 params << serverEncode(bufname) << lowLevelQuote(pack(serverEncode(ctcpTag), userEncode(bufname, message)));
175 emit putCmd("NOTICE", params);
178 //******************************/
180 //******************************/
181 void CtcpHandler::handleAction(CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
183 emit displayMsg(Message::Action, typeByTarget(target), target, param, prefix);
186 void CtcpHandler::handlePing(CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
188 if(ctcptype == CtcpQuery) {
189 reply(nickFromMask(prefix), "PING", param);
190 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING request from %1").arg(prefix));
192 // display ping answer
193 uint now = QDateTime::currentDateTime().toTime_t();
194 uint then = QDateTime().fromTime_t(param.toInt()).toTime_t();
195 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING answer from %1 with %2 seconds round trip time").arg(prefix).arg(now-then));
199 void CtcpHandler::handleVersion(CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
201 if(ctcptype == CtcpQuery) {
202 reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC %1 (built on %2) -- http://www.quassel-irc.org")
203 .arg(Quassel::buildInfo().plainVersionString)
204 .arg(Quassel::buildInfo().buildDate));
205 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION request by %1").arg(prefix));
207 // display Version answer
208 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION answer from %1: %2").arg(prefix).arg(param));
212 void CtcpHandler::defaultHandler(const QString &cmd, CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
216 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));