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 "ctcphandler.h"
27 CtcpHandler::CtcpHandler(NetworkConnection *parent)
28 : BasicHandler(parent) {
30 QString MQUOTE = QString('\020');
31 ctcpMDequoteHash[MQUOTE + '0'] = QString('\000');
32 ctcpMDequoteHash[MQUOTE + 'n'] = QString('\n');
33 ctcpMDequoteHash[MQUOTE + 'r'] = QString('\r');
34 ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
36 XDELIM = QString('\001');
37 QString XQUOTE = QString('\134');
38 ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
39 ctcpXDelimDequoteHash[XQUOTE + QString('a')] = XDELIM;
42 QString CtcpHandler::dequote(QString message) {
43 QString dequotedMessage;
45 QHash<QString, QString>::iterator ctcpquote;
47 // copy dequote Message
48 for(int i = 0; i < message.size(); i++) {
49 messagepart = message[i];
50 if(i+1 < message.size()) {
51 for(ctcpquote = ctcpMDequoteHash.begin(); ctcpquote != ctcpMDequoteHash.end(); ++ctcpquote) {
52 if(message.mid(i,2) == ctcpquote.key()) {
53 messagepart = ctcpquote.value();
59 dequotedMessage += messagepart;
61 return dequotedMessage;
65 QString CtcpHandler::XdelimDequote(QString message) {
66 QString dequotedMessage;
68 QHash<QString, QString>::iterator xdelimquote;
70 for(int i = 0; i < message.size(); i++) {
71 messagepart = message[i];
72 if(i+1 < message.size()) {
73 for(xdelimquote = ctcpXDelimDequoteHash.begin(); xdelimquote != ctcpXDelimDequoteHash.end(); ++xdelimquote) {
74 if(message.mid(i,2) == xdelimquote.key()) {
75 messagepart = xdelimquote.value();
81 dequotedMessage += messagepart;
83 return dequotedMessage;
86 void CtcpHandler::parse(Message::Type messageType, QString prefix, QString target, QString message) {
89 //lowlevel message dequote
90 QString dequotedMessage = dequote(message);
92 CtcpType ctcptype = (messageType == Message::Notice)
96 BufferInfo::Type bufferType = typeByTarget(target);
98 // extract tagged / extended data
99 while(dequotedMessage.contains(XDELIM)) {
100 if(dequotedMessage.indexOf(XDELIM) > 0)
101 emit displayMsg(messageType, bufferType, target, dequotedMessage.section(XDELIM,0,0), prefix);
102 // messages << dequotedMessage.section(XDELIM,0,0), prefix);
103 ctcp = XdelimDequote(dequotedMessage.section(XDELIM,1,1));
104 dequotedMessage = dequotedMessage.section(XDELIM,2,2);
106 //dispatch the ctcp command
107 QString ctcpcmd = ctcp.section(' ', 0, 0);
108 QString ctcpparam = ctcp.section(' ', 1);
110 handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
113 if(!dequotedMessage.isEmpty())
114 emit displayMsg(messageType, bufferType, target, dequotedMessage, prefix);
118 QString CtcpHandler::pack(QString ctcpTag, QString message) {
119 return XDELIM + ctcpTag + ' ' + message + XDELIM;
122 // TODO handle encodings correctly!
123 void CtcpHandler::query(QString bufname, QString ctcpTag, QString message) {
125 params << bufname << pack(ctcpTag, message);
126 emit putCmd("PRIVMSG", serverEncode(params));
129 void CtcpHandler::reply(QString bufname, QString ctcpTag, QString message) {
131 params << bufname << pack(ctcpTag, message);
132 emit putCmd("NOTICE", serverEncode(params));
135 //******************************/
137 //******************************/
138 void CtcpHandler::handleAction(CtcpType ctcptype, QString prefix, QString target, QString param) {
140 emit displayMsg(Message::Action, typeByTarget(target), target, param, prefix);
143 void CtcpHandler::handlePing(CtcpType ctcptype, QString prefix, QString target, QString param) {
145 if(ctcptype == CtcpQuery) {
146 reply(nickFromMask(prefix), "PING", param);
147 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING request from %1").arg(prefix));
149 // display ping answer
150 uint now = QDateTime::currentDateTime().toTime_t();
151 uint then = QDateTime().fromTime_t(param.toInt()).toTime_t();
152 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING answer from %1 with %2 seconds round trip time").arg(prefix).arg(now-then));
156 void CtcpHandler::handleVersion(CtcpType ctcptype, QString prefix, QString target, QString param) {
158 if(ctcptype == CtcpQuery) {
159 // FIXME use real Info about quassel :)
160 reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC (v%1 build >= %2) -- http://www.quassel-irc.org")
161 .arg(Global::quasselVersion).arg(Global::quasselBuild));
162 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION request by %1").arg(prefix));
164 // display Version answer
165 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION answer from %1: %2").arg(prefix).arg(param));
169 void CtcpHandler::defaultHandler(QString cmd, CtcpType ctcptype, QString prefix, QString target, QString param) {
173 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));