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 QStringList CtcpHandler::parse(CtcpType ctcptype, QString prefix, QString target, QString message) {
88 //lowlevel message dequote
89 QString dequotedMessage = dequote(message);
91 // extract tagged / extended data
92 while(dequotedMessage.contains(XDELIM)) {
93 if(dequotedMessage.indexOf(XDELIM) > 0)
94 messages << dequotedMessage.section(XDELIM,0,0);
95 ctcp = XdelimDequote(dequotedMessage.section(XDELIM,1,1));
96 dequotedMessage = dequotedMessage.section(XDELIM,2,2);
98 //dispatch the ctcp command
99 QString ctcpcmd = ctcp.section(' ', 0, 0);
100 QString ctcpparam = ctcp.section(' ', 1);
102 handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
104 if(!dequotedMessage.isEmpty()) {
105 messages << dequotedMessage;
110 QString CtcpHandler::pack(QString ctcpTag, QString message) {
111 return XDELIM + ctcpTag + ' ' + message + XDELIM;
114 void CtcpHandler::query(QString bufname, QString ctcpTag, QString message) {
116 params << bufname << pack(ctcpTag, message);
117 emit putCmd("PRIVMSG", params);
120 void CtcpHandler::reply(QString bufname, QString ctcpTag, QString message) {
122 params << bufname << pack(ctcpTag, message);
123 emit putCmd("NOTICE", params);
126 //******************************/
128 //******************************/
129 void CtcpHandler::handleAction(CtcpType ctcptype, QString prefix, QString target, QString param) {
131 emit displayMsg(Message::Action, target, param, prefix);
134 void CtcpHandler::handlePing(CtcpType ctcptype, QString prefix, QString target, QString param) {
136 if(ctcptype == CtcpQuery) {
137 reply(nickFromMask(prefix), "PING", param);
138 emit displayMsg(Message::Server, "", tr("Received CTCP PING request from %1").arg(prefix));
140 // display ping answer
144 void CtcpHandler::handleVersion(CtcpType ctcptype, QString prefix, QString target, QString param) {
146 if(ctcptype == CtcpQuery) {
147 // FIXME use real Info about quassel :)
148 reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC (Pre-Release) - http://www.quassel-irc.org"));
149 emit displayMsg(Message::Server, "", tr("Received CTCP VERSION request by %1").arg(prefix));
151 // TODO display Version answer
155 void CtcpHandler::defaultHandler(QString cmd, CtcpType ctcptype, QString prefix, QString target, QString param) {
159 emit displayMsg(Message::Error, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));