1 /***************************************************************************
2 * Copyright (C) 2005-10 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"
26 #include "coreignorelistmanager.h"
28 CtcpHandler::CtcpHandler(CoreNetwork *parent)
29 : CoreBasicHandler(parent),
31 _ignoreListManager(parent->ignoreListManager())
34 QByteArray MQUOTE = QByteArray("\020");
35 ctcpMDequoteHash[MQUOTE + '0'] = QByteArray(1, '\000');
36 ctcpMDequoteHash[MQUOTE + 'n'] = QByteArray(1, '\n');
37 ctcpMDequoteHash[MQUOTE + 'r'] = QByteArray(1, '\r');
38 ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
40 QByteArray XQUOTE = QByteArray("\134");
41 ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
42 ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
45 QByteArray CtcpHandler::lowLevelQuote(const QByteArray &message) {
46 QByteArray quotedMessage = message;
48 QHash<QByteArray, QByteArray> quoteHash = ctcpMDequoteHash;
49 QByteArray MQUOTE = QByteArray("\020");
50 quoteHash.remove(MQUOTE + MQUOTE);
51 quotedMessage.replace(MQUOTE, MQUOTE + MQUOTE);
53 QHash<QByteArray, QByteArray>::const_iterator quoteIter = quoteHash.constBegin();
54 while(quoteIter != quoteHash.constEnd()) {
55 quotedMessage.replace(quoteIter.value(), quoteIter.key());
61 QByteArray CtcpHandler::lowLevelDequote(const QByteArray &message) {
62 QByteArray dequotedMessage;
63 QByteArray messagepart;
64 QHash<QByteArray, QByteArray>::iterator ctcpquote;
66 // copy dequote Message
67 for(int i = 0; i < message.size(); i++) {
68 messagepart = message.mid(i,1);
69 if(i+1 < message.size()) {
70 for(ctcpquote = ctcpMDequoteHash.begin(); ctcpquote != ctcpMDequoteHash.end(); ++ctcpquote) {
71 if(message.mid(i,2) == ctcpquote.key()) {
72 messagepart = ctcpquote.value();
78 dequotedMessage += messagepart;
80 return dequotedMessage;
83 QByteArray CtcpHandler::xdelimQuote(const QByteArray &message) {
84 QByteArray quotedMessage = message;
85 QHash<QByteArray, QByteArray>::const_iterator quoteIter = ctcpXDelimDequoteHash.constBegin();
86 while(quoteIter != ctcpXDelimDequoteHash.constEnd()) {
87 quotedMessage.replace(quoteIter.value(), quoteIter.key());
93 QByteArray CtcpHandler::xdelimDequote(const QByteArray &message) {
94 QByteArray dequotedMessage;
95 QByteArray messagepart;
96 QHash<QByteArray, QByteArray>::iterator xdelimquote;
98 for(int i = 0; i < message.size(); i++) {
99 messagepart = message.mid(i,1);
100 if(i+1 < message.size()) {
101 for(xdelimquote = ctcpXDelimDequoteHash.begin(); xdelimquote != ctcpXDelimDequoteHash.end(); ++xdelimquote) {
102 if(message.mid(i,2) == xdelimquote.key()) {
103 messagepart = xdelimquote.value();
109 dequotedMessage += messagepart;
111 return dequotedMessage;
114 void CtcpHandler::parse(Message::Type messageType, const QString &prefix, const QString &target, const QByteArray &message) {
117 //lowlevel message dequote
118 QByteArray dequotedMessage = lowLevelDequote(message);
120 CtcpType ctcptype = messageType == Message::Notice
124 Message::Flags flags = (messageType == Message::Notice && !network()->isChannelName(target))
125 ? Message::Redirected
128 // extract tagged / extended data
130 int xdelimEndPos = -1;
132 while((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
134 displayMsg(messageType, target, userDecode(target, dequotedMessage.left(xdelimPos)), prefix, flags);
136 xdelimEndPos = dequotedMessage.indexOf(XDELIM, xdelimPos + 1);
137 if(xdelimEndPos == -1) {
138 // no matching end delimiter found... treat rest of the message as ctcp
139 xdelimEndPos = dequotedMessage.count();
141 ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
142 dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
144 //dispatch the ctcp command
145 QString ctcpcmd = userDecode(target, ctcp.left(spacePos));
146 QString ctcpparam = userDecode(target, ctcp.mid(spacePos + 1));
148 spacePos = ctcp.indexOf(' ');
150 ctcpcmd = userDecode(target, ctcp.left(spacePos));
151 ctcpparam = userDecode(target, ctcp.mid(spacePos + 1));
153 ctcpcmd = userDecode(target, ctcp);
154 ctcpparam = QString();
157 handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
160 if(!dequotedMessage.isEmpty())
161 displayMsg(messageType, target, userDecode(target, dequotedMessage), prefix, flags);
164 QByteArray CtcpHandler::pack(const QByteArray &ctcpTag, const QByteArray &message) {
165 if(message.isEmpty())
166 return XDELIM + ctcpTag + XDELIM;
168 return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
171 void CtcpHandler::query(const QString &bufname, const QString &ctcpTag, const QString &message) {
172 QList<QByteArray> params;
173 params << serverEncode(bufname) << lowLevelQuote(pack(serverEncode(ctcpTag), userEncode(bufname, message)));
174 emit putCmd("PRIVMSG", params);
177 void CtcpHandler::reply(const QString &bufname, const QString &ctcpTag, const QString &message) {
178 QList<QByteArray> params;
179 params << serverEncode(bufname) << lowLevelQuote(pack(serverEncode(ctcpTag), userEncode(bufname, message)));
180 emit putCmd("NOTICE", params);
183 //******************************/
185 //******************************/
186 void CtcpHandler::handleAction(CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
188 emit displayMsg(Message::Action, typeByTarget(target), target, param, prefix);
191 void CtcpHandler::handleClientinfo(CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
193 if(ctcptype == CtcpQuery) {
194 if(_ignoreListManager->ctcpMatch(prefix, network()->networkName(), "CLIENTINFO"))
196 reply(nickFromMask(prefix), "CLIENTINFO", QString("ACTION CLIENTINFO PING TIME VERSION"));
197 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP CLIENTINFO request from %1").arg(prefix));
199 // display clientinfo answer
200 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP CLIENTINFO answer from %1: %2")
201 .arg(nickFromMask(prefix)).arg(param));
205 void CtcpHandler::handlePing(CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
207 if(ctcptype == CtcpQuery) {
208 if(_ignoreListManager->ctcpMatch(prefix, network()->networkName(), "PING"))
210 reply(nickFromMask(prefix), "PING", param);
211 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING request from %1").arg(prefix));
213 // display ping answer
214 uint now = QDateTime::currentDateTime().toTime_t();
215 uint then = QDateTime().fromTime_t(param.toInt()).toTime_t();
216 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING answer from %1 with %2 seconds round trip time")
217 .arg(nickFromMask(prefix)).arg(now-then));
221 void CtcpHandler::handleVersion(CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
223 if(ctcptype == CtcpQuery) {
224 if(_ignoreListManager->ctcpMatch(prefix, network()->networkName(), "VERSION"))
226 reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC %1 (built on %2) -- http://www.quassel-irc.org")
227 .arg(Quassel::buildInfo().plainVersionString)
228 .arg(Quassel::buildInfo().buildDate));
229 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION request by %1").arg(prefix));
231 // display Version answer
232 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION answer from %1: %2")
233 .arg(nickFromMask(prefix)).arg(param));
237 void CtcpHandler::handleTime(CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
239 if(ctcptype == CtcpQuery) {
240 if(_ignoreListManager->ctcpMatch(prefix, network()->networkName(), "TIME"))
242 reply(nickFromMask(prefix), "TIME", QDateTime::currentDateTime().toString());
243 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP TIME request by %1").arg(prefix));
246 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP TIME answer from %1: %2")
247 .arg(nickFromMask(prefix)).arg(param));
251 void CtcpHandler::defaultHandler(const QString &cmd, CtcpType ctcptype, const QString &prefix, const QString &target, const QString ¶m) {
254 if(!_ignoreListManager->ctcpMatch(prefix, network()->networkName())) {
255 QString str = tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix);
257 str.append(tr(" with arguments: %1").arg(param));
258 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", str);