Encodings are now honored for both sending and receiving. Cleaned up encode/decode
[quassel.git] / src / core / ctcphandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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"
21
22 #include "global.h"
23 #include "util.h"
24 #include "message.h"
25 #include "network.h"
26
27 CtcpHandler::CtcpHandler(NetworkConnection *parent)
28   : BasicHandler(parent) {
29
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;
35
36   XDELIM = QString('\001');
37   QString XQUOTE = QString('\134');
38   ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
39   ctcpXDelimDequoteHash[XQUOTE + QString('a')] = XDELIM;
40 }
41
42 QString CtcpHandler::dequote(QString message) {
43   QString dequotedMessage;
44   QString messagepart;
45   QHash<QString, QString>::iterator ctcpquote;
46   
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();
54           i++;
55           break;
56         }
57       }
58     }
59     dequotedMessage += messagepart;
60   }
61   return dequotedMessage;
62 }
63
64
65 QString CtcpHandler::XdelimDequote(QString message) {
66   QString dequotedMessage;
67   QString messagepart;
68   QHash<QString, QString>::iterator xdelimquote;
69
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();
76           i++;
77           break;
78         }
79       }
80     }
81     dequotedMessage += messagepart;
82   }
83   return dequotedMessage;
84 }
85
86 void CtcpHandler::parse(Message::Type messageType, QString prefix, QString target, QString message) {
87   QString ctcp;
88   
89   //lowlevel message dequote
90   QString dequotedMessage = dequote(message);
91
92   CtcpType ctcptype = (messageType == Message::Notice)
93     ? CtcpReply
94     : CtcpQuery;
95
96   BufferInfo::Type bufferType = typeByTarget(target);
97      
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);
105     
106     //dispatch the ctcp command
107     QString ctcpcmd = ctcp.section(' ', 0, 0);
108     QString ctcpparam = ctcp.section(' ', 1);
109
110     handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
111   }
112   
113   if(!dequotedMessage.isEmpty())
114     emit displayMsg(messageType, bufferType, target, dequotedMessage, prefix);
115
116 }
117
118 QString CtcpHandler::pack(QString ctcpTag, QString message) {
119   return XDELIM + ctcpTag + ' ' + message + XDELIM;
120 }
121
122 // TODO handle encodings correctly!
123 void CtcpHandler::query(QString bufname, QString ctcpTag, QString message) {
124   QStringList params;
125   params << bufname << pack(ctcpTag, message);
126   emit putCmd("PRIVMSG", serverEncode(params));
127 }
128
129 void CtcpHandler::reply(QString bufname, QString ctcpTag, QString message) {
130   QStringList params;
131   params << bufname << pack(ctcpTag, message);
132   emit putCmd("NOTICE", serverEncode(params));
133 }
134
135 //******************************/
136 // CTCP HANDLER
137 //******************************/
138 void CtcpHandler::handleAction(CtcpType ctcptype, QString prefix, QString target, QString param) {
139   Q_UNUSED(ctcptype)
140   emit displayMsg(Message::Action, typeByTarget(target), target, param, prefix);
141 }
142
143 void CtcpHandler::handlePing(CtcpType ctcptype, QString prefix, QString target, QString param) {
144   Q_UNUSED(target)
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));
148   } else {
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));
153   }
154 }
155
156 void CtcpHandler::handleVersion(CtcpType ctcptype, QString prefix, QString target, QString param) {
157   Q_UNUSED(target)
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));
163   } else {
164     // display Version answer
165     emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION answer from %1: %2").arg(prefix).arg(param));
166   }
167 }
168
169 void CtcpHandler::defaultHandler(QString cmd, CtcpType ctcptype, QString prefix, QString target, QString param) {
170   Q_UNUSED(ctcptype);
171   Q_UNUSED(target);
172   Q_UNUSED(param);
173   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));
174 }
175
176