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"
26 CtcpHandler::CtcpHandler(NetworkConnection *parent)
27 : BasicHandler(parent) {
29 QString MQUOTE = QString('\020');
30 ctcpMDequoteHash[MQUOTE + '0'] = QString('\000');
31 ctcpMDequoteHash[MQUOTE + 'n'] = QString('\n');
32 ctcpMDequoteHash[MQUOTE + 'r'] = QString('\r');
33 ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
35 XDELIM = QString('\001');
36 QString XQUOTE = QString('\134');
37 ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
38 ctcpXDelimDequoteHash[XQUOTE + QString('a')] = XDELIM;
41 QString CtcpHandler::dequote(QString message) {
42 QString dequotedMessage;
44 QHash<QString, QString>::iterator ctcpquote;
46 // copy dequote Message
47 for(int i = 0; i < message.size(); i++) {
48 messagepart = message[i];
49 if(i+1 < message.size()) {
50 for(ctcpquote = ctcpMDequoteHash.begin(); ctcpquote != ctcpMDequoteHash.end(); ++ctcpquote) {
51 if(message.mid(i,2) == ctcpquote.key()) {
52 messagepart = ctcpquote.value();
58 dequotedMessage += messagepart;
60 return dequotedMessage;
64 QString CtcpHandler::XdelimDequote(QString message) {
65 QString dequotedMessage;
67 QHash<QString, QString>::iterator xdelimquote;
69 for(int i = 0; i < message.size(); i++) {
70 messagepart = message[i];
71 if(i+1 < message.size()) {
72 for(xdelimquote = ctcpXDelimDequoteHash.begin(); xdelimquote != ctcpXDelimDequoteHash.end(); ++xdelimquote) {
73 if(message.mid(i,2) == xdelimquote.key()) {
74 messagepart = xdelimquote.value();
80 dequotedMessage += messagepart;
82 return dequotedMessage;
85 void CtcpHandler::parse(Message::Type messageType, QString prefix, QString target, QString message) {
88 //lowlevel message dequote
89 QString dequotedMessage = dequote(message);
91 CtcpType ctcptype = (messageType == Message::Notice)
95 // extract tagged / extended data
96 while(dequotedMessage.contains(XDELIM)) {
97 if(dequotedMessage.indexOf(XDELIM) > 0)
98 emit displayMsg(messageType, target, dequotedMessage.section(XDELIM,0,0), prefix);
99 // messages << dequotedMessage.section(XDELIM,0,0), prefix);
100 ctcp = XdelimDequote(dequotedMessage.section(XDELIM,1,1));
101 dequotedMessage = dequotedMessage.section(XDELIM,2,2);
103 //dispatch the ctcp command
104 QString ctcpcmd = ctcp.section(' ', 0, 0);
105 QString ctcpparam = ctcp.section(' ', 1);
107 handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
110 if(!dequotedMessage.isEmpty())
111 emit displayMsg(messageType, target, dequotedMessage, prefix);
115 QString CtcpHandler::pack(QString ctcpTag, QString message) {
116 return XDELIM + ctcpTag + ' ' + message + XDELIM;
119 void CtcpHandler::query(QString bufname, QString ctcpTag, QString message) {
121 params << bufname << pack(ctcpTag, message);
122 emit putCmd("PRIVMSG", params);
125 void CtcpHandler::reply(QString bufname, QString ctcpTag, QString message) {
127 params << bufname << pack(ctcpTag, message);
128 emit putCmd("NOTICE", params);
131 //******************************/
133 //******************************/
134 void CtcpHandler::handleAction(CtcpType ctcptype, QString prefix, QString target, QString param) {
136 emit displayMsg(Message::Action, target, param, prefix);
139 void CtcpHandler::handlePing(CtcpType ctcptype, QString prefix, QString target, QString param) {
141 if(ctcptype == CtcpQuery) {
142 reply(nickFromMask(prefix), "PING", param);
143 emit displayMsg(Message::Server, "", tr("Received CTCP PING request from %1").arg(prefix));
145 // display ping answer
146 uint now = QDateTime::currentDateTime().toTime_t();
147 uint then = QDateTime().fromTime_t(param.toInt()).toTime_t();
148 emit displayMsg(Message::Server, "", tr("Received CTCP PING answer from %1 with %2 seconds round trip time").arg(prefix).arg(now-then));
152 void CtcpHandler::handleVersion(CtcpType ctcptype, QString prefix, QString target, QString param) {
154 if(ctcptype == CtcpQuery) {
155 // FIXME use real Info about quassel :)
156 reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC (v%1 build >= %2) -- http://www.quassel-irc.org")
157 .arg(Global::quasselVersion).arg(Global::quasselBuild));
158 emit displayMsg(Message::Server, "", tr("Received CTCP VERSION request by %1").arg(prefix));
160 // display Version answer
161 emit displayMsg(Message::Server, "", tr("Received CTCP VERSION answer from %1: %2").arg(prefix).arg(param));
165 void CtcpHandler::defaultHandler(QString cmd, CtcpType ctcptype, QString prefix, QString target, QString param) {
169 emit displayMsg(Message::Error, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));