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"
25 CtcpHandler::CtcpHandler(NetworkConnection *parent)
26 : BasicHandler(parent) {
28 QString MQUOTE = QString('\020');
29 ctcpMDequoteHash[MQUOTE + '0'] = QString('\000');
30 ctcpMDequoteHash[MQUOTE + 'n'] = QString('\n');
31 ctcpMDequoteHash[MQUOTE + 'r'] = QString('\r');
32 ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
34 XDELIM = QString('\001');
35 QString XQUOTE = QString('\134');
36 ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
37 ctcpXDelimDequoteHash[XQUOTE + QString('a')] = XDELIM;
40 QString CtcpHandler::dequote(QString message) {
41 QString dequotedMessage;
43 QHash<QString, QString>::iterator ctcpquote;
45 // copy dequote Message
46 for(int i = 0; i < message.size(); i++) {
47 messagepart = message[i];
48 if(i+1 < message.size()) {
49 for(ctcpquote = ctcpMDequoteHash.begin(); ctcpquote != ctcpMDequoteHash.end(); ++ctcpquote) {
50 if(message.mid(i,2) == ctcpquote.key()) {
51 messagepart = ctcpquote.value();
57 dequotedMessage += messagepart;
59 return dequotedMessage;
63 QString CtcpHandler::XdelimDequote(QString message) {
64 QString dequotedMessage;
66 QHash<QString, QString>::iterator xdelimquote;
68 for(int i = 0; i < message.size(); i++) {
69 messagepart = message[i];
70 if(i+1 < message.size()) {
71 for(xdelimquote = ctcpXDelimDequoteHash.begin(); xdelimquote != ctcpXDelimDequoteHash.end(); ++xdelimquote) {
72 if(message.mid(i,2) == xdelimquote.key()) {
73 messagepart = xdelimquote.value();
79 dequotedMessage += messagepart;
81 return dequotedMessage;
84 void CtcpHandler::parse(Message::Type messageType, QString prefix, QString target, QString message) {
87 //lowlevel message dequote
88 QString dequotedMessage = dequote(message);
90 CtcpType ctcptype = (messageType == Message::Notice)
94 // extract tagged / extended data
95 while(dequotedMessage.contains(XDELIM)) {
96 if(dequotedMessage.indexOf(XDELIM) > 0)
97 emit displayMsg(messageType, target, dequotedMessage.section(XDELIM,0,0), prefix);
98 // messages << dequotedMessage.section(XDELIM,0,0), prefix);
99 ctcp = XdelimDequote(dequotedMessage.section(XDELIM,1,1));
100 dequotedMessage = dequotedMessage.section(XDELIM,2,2);
102 //dispatch the ctcp command
103 QString ctcpcmd = ctcp.section(' ', 0, 0);
104 QString ctcpparam = ctcp.section(' ', 1);
106 handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
109 if(!dequotedMessage.isEmpty())
110 emit displayMsg(messageType, target, dequotedMessage, prefix);
114 QString CtcpHandler::pack(QString ctcpTag, QString message) {
115 return XDELIM + ctcpTag + ' ' + message + XDELIM;
118 void CtcpHandler::query(QString bufname, QString ctcpTag, QString message) {
120 params << bufname << pack(ctcpTag, message);
121 emit putCmd("PRIVMSG", params);
124 void CtcpHandler::reply(QString bufname, QString ctcpTag, QString message) {
126 params << bufname << pack(ctcpTag, message);
127 emit putCmd("NOTICE", params);
130 //******************************/
132 //******************************/
133 void CtcpHandler::handleAction(CtcpType ctcptype, QString prefix, QString target, QString param) {
135 emit displayMsg(Message::Action, target, param, prefix);
138 void CtcpHandler::handlePing(CtcpType ctcptype, QString prefix, QString target, QString param) {
140 if(ctcptype == CtcpQuery) {
141 reply(nickFromMask(prefix), "PING", param);
142 emit displayMsg(Message::Server, "", tr("Received CTCP PING request from %1").arg(prefix));
144 // display ping answer
145 uint now = QDateTime::currentDateTime().toTime_t();
146 uint then = QDateTime().fromTime_t(param.toInt()).toTime_t();
147 emit displayMsg(Message::Server, "", tr("Received CTCP PING answer from %1 with %2 seconds round trip time").arg(prefix).arg(now-then));
151 void CtcpHandler::handleVersion(CtcpType ctcptype, QString prefix, QString target, QString param) {
153 if(ctcptype == CtcpQuery) {
154 // FIXME use real Info about quassel :)
155 reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC (Pre-Release) - http://www.quassel-irc.org"));
156 emit displayMsg(Message::Server, "", tr("Received CTCP VERSION request by %1").arg(prefix));
158 // display Version answer
159 emit displayMsg(Message::Server, "", tr("Received CTCP VERSION answer from %1: %2").arg(prefix).arg(param));
163 void CtcpHandler::defaultHandler(QString cmd, CtcpType ctcptype, QString prefix, QString target, QString param) {
167 emit displayMsg(Message::Error, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));