1 /***************************************************************************
2 * Copyright (C) 2005-07 by the Quassel IRC Team *
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(Server *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 dequotedMessage += 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 messages << dequotedMessage.section(XDELIM,0,0);
94 ctcp = XdelimDequote(dequotedMessage.section(XDELIM,1,1));
95 dequotedMessage = dequotedMessage.section(XDELIM,2,2);
97 //dispatch the ctcp command
98 QString ctcpcmd = ctcp.section(' ', 0, 0);
99 QString ctcpparam = ctcp.section(' ', 1);
101 handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
103 if(!dequotedMessage.isEmpty()) {
104 messages << dequotedMessage;
109 QString CtcpHandler::pack(QString ctcpTag, QString message) {
110 return XDELIM + ctcpTag + ' ' + message + XDELIM;
113 void CtcpHandler::query(QString bufname, QString ctcpTag, QString message) {
115 params << bufname << pack(ctcpTag, message);
116 emit putCmd("PRIVMSG", params);
119 void CtcpHandler::reply(QString bufname, QString ctcpTag, QString message) {
121 params << bufname << pack(ctcpTag, message);
122 emit putCmd("NOTICE", params);
125 //******************************/
127 //******************************/
128 void CtcpHandler::handleAction(CtcpType ctcptype, QString prefix, QString target, QString param) {
129 emit displayMsg(Message::Action, target, param, prefix);
132 void CtcpHandler::handlePing(CtcpType ctcptype, QString prefix, QString target, QString param) {
133 if(ctcptype == CtcpQuery) {
134 reply(nickFromMask(prefix), "PING", param);
135 emit displayMsg(Message::Server, "", tr("Received CTCP PING request from %1").arg(prefix));
137 // display ping answer
141 void CtcpHandler::handleVersion(CtcpType ctcptype, QString prefix, QString target, QString param) {
142 if(ctcptype == CtcpQuery) {
143 // FIXME use real Info about quassel :)
144 reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC (Pre-Release) - http://www.quassel-irc.org"));
145 emit displayMsg(Message::Server, "", tr("Received CTCP VERSION request by %1").arg(prefix));
147 // TODO display Version answer
151 void CtcpHandler::defaultHandler(QString cmd, CtcpType ctcptype, QString prefix, QString target, QString param) {
152 emit displayMsg(Message::Error, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));