ec21f4ccd2c9a357e0cea1029d49bf9cb5caced6
[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 "message.h"
23 #include "network.h"
24 #include "quassel.h"
25 #include "util.h"
26
27 CtcpHandler::CtcpHandler(NetworkConnection *parent)
28   : BasicHandler(parent),
29     XDELIM("\001")
30 {
31
32   QByteArray MQUOTE = QByteArray("\020");
33   ctcpMDequoteHash[MQUOTE + '0'] = QByteArray("\000");
34   ctcpMDequoteHash[MQUOTE + 'n'] = QByteArray("\n");
35   ctcpMDequoteHash[MQUOTE + 'r'] = QByteArray("\r");
36   ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
37
38   QByteArray XQUOTE = QByteArray("\134");
39   ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
40   ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
41 }
42
43 QByteArray CtcpHandler::lowLevelQuote(const QByteArray &message) {
44   QByteArray quotedMessage = message;
45   QHash<QByteArray, QByteArray>::const_iterator quoteIter = ctcpMDequoteHash.constBegin();
46   while(quoteIter != ctcpMDequoteHash.constEnd()) {
47     quotedMessage.replace(quoteIter.value(), quoteIter.key());
48     quoteIter++;
49   }
50   return quotedMessage;
51 }
52
53 QByteArray CtcpHandler::lowLevelDequote(const QByteArray &message) {
54   QByteArray dequotedMessage;
55   QByteArray messagepart;
56   QHash<QByteArray, QByteArray>::iterator ctcpquote;
57
58   // copy dequote Message
59   for(int i = 0; i < message.size(); i++) {
60     messagepart = message.mid(i,1);
61     if(i+1 < message.size()) {
62       for(ctcpquote = ctcpMDequoteHash.begin(); ctcpquote != ctcpMDequoteHash.end(); ++ctcpquote) {
63         if(message.mid(i,2) == ctcpquote.key()) {
64           messagepart = ctcpquote.value();
65           i++;
66           break;
67         }
68       }
69     }
70     dequotedMessage += messagepart;
71   }
72   return dequotedMessage;
73 }
74
75 QByteArray CtcpHandler::xdelimQuote(const QByteArray &message) {
76   QByteArray quotedMessage = message;
77   QHash<QByteArray, QByteArray>::const_iterator quoteIter = ctcpXDelimDequoteHash.constBegin();
78   while(quoteIter != ctcpXDelimDequoteHash.constEnd()) {
79     quotedMessage.replace(quoteIter.value(), quoteIter.key());
80     quoteIter++;
81   }
82   return quotedMessage;
83 }
84
85 QByteArray CtcpHandler::xdelimDequote(const QByteArray &message) {
86   QByteArray dequotedMessage;
87   QByteArray messagepart;
88   QHash<QByteArray, QByteArray>::iterator xdelimquote;
89
90   for(int i = 0; i < message.size(); i++) {
91     messagepart = message.mid(i,1);
92     if(i+1 < message.size()) {
93       for(xdelimquote = ctcpXDelimDequoteHash.begin(); xdelimquote != ctcpXDelimDequoteHash.end(); ++xdelimquote) {
94         if(message.mid(i,2) == xdelimquote.key()) {
95           messagepart = xdelimquote.value();
96           i++;
97           break;
98         }
99       }
100     }
101     dequotedMessage += messagepart;
102   }
103   return dequotedMessage;
104 }
105
106 void CtcpHandler::parse(Message::Type messageType, const QString &prefix, const QString &target, const QByteArray &message) {
107   QByteArray ctcp;
108
109   //lowlevel message dequote
110   QByteArray dequotedMessage = lowLevelDequote(message);
111
112   CtcpType ctcptype = messageType == Message::Notice
113     ? CtcpReply
114     : CtcpQuery;
115
116   Message::Flags flags = (messageType == Message::Notice && !network()->isChannelName(target))
117     ? Message::Redirected
118     : Message::None;
119
120   // extract tagged / extended data
121   int xdelimPos = -1;
122   int xdelimEndPos = -1;
123   int spacePos = -1;
124   while((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
125     if(xdelimPos > 0)
126       displayMsg(messageType, target, userDecode(target, dequotedMessage.left(xdelimPos)), prefix, flags);
127
128     xdelimEndPos = dequotedMessage.indexOf(XDELIM, xdelimPos + 1);
129     if(xdelimEndPos == -1) {
130       // no matching end delimiter found... treat rest of the message as ctcp
131       xdelimEndPos = dequotedMessage.count();
132     }
133     ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
134     dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
135
136     //dispatch the ctcp command
137     QString ctcpcmd = userDecode(target, ctcp.left(spacePos));
138     QString ctcpparam = userDecode(target, ctcp.mid(spacePos + 1));
139
140     spacePos = ctcp.indexOf(' ');
141     if(spacePos != -1) {
142       ctcpcmd = userDecode(target, ctcp.left(spacePos));
143       ctcpparam = userDecode(target, ctcp.mid(spacePos + 1));
144     } else {
145       ctcpcmd = userDecode(target, ctcp);
146       ctcpparam = QString();
147     }
148
149     handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
150   }
151
152   if(!dequotedMessage.isEmpty())
153     displayMsg(messageType, target, userDecode(target, dequotedMessage), prefix, flags);
154 }
155
156 QByteArray CtcpHandler::pack(const QByteArray &ctcpTag, const QByteArray &message) {
157   return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
158 }
159
160 void CtcpHandler::query(const QString &bufname, const QString &ctcpTag, const QString &message) {
161   QList<QByteArray> params;
162   params << serverEncode(bufname) << lowLevelQuote(pack(serverEncode(ctcpTag), userEncode(bufname, message)));
163   emit putCmd("PRIVMSG", params);
164 }
165
166 void CtcpHandler::reply(const QString &bufname, const QString &ctcpTag, const QString &message) {
167   QList<QByteArray> params;
168   params << serverEncode(bufname) << lowLevelQuote(pack(serverEncode(ctcpTag), userEncode(bufname, message)));
169   emit putCmd("NOTICE", params);
170 }
171
172 //******************************/
173 // CTCP HANDLER
174 //******************************/
175 void CtcpHandler::handleAction(CtcpType ctcptype, const QString &prefix, const QString &target, const QString &param) {
176   Q_UNUSED(ctcptype)
177   emit displayMsg(Message::Action, typeByTarget(target), target, param, prefix);
178 }
179
180 void CtcpHandler::handlePing(CtcpType ctcptype, const QString &prefix, const QString &target, const QString &param) {
181   Q_UNUSED(target)
182   if(ctcptype == CtcpQuery) {
183     reply(nickFromMask(prefix), "PING", param);
184     emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING request from %1").arg(prefix));
185   } else {
186     // display ping answer
187     uint now = QDateTime::currentDateTime().toTime_t();
188     uint then = QDateTime().fromTime_t(param.toInt()).toTime_t();
189     emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING answer from %1 with %2 seconds round trip time").arg(prefix).arg(now-then));
190   }
191 }
192
193 void CtcpHandler::handleVersion(CtcpType ctcptype, const QString &prefix, const QString &target, const QString &param) {
194   Q_UNUSED(target)
195   if(ctcptype == CtcpQuery) {
196     reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC %1 (built on %2) -- http://www.quassel-irc.org")
197         .arg(Quassel::buildInfo().plainVersionString)
198         .arg(Quassel::buildInfo().buildDate));
199     emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION request by %1").arg(prefix));
200   } else {
201     // display Version answer
202     emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION answer from %1: %2").arg(prefix).arg(param));
203   }
204 }
205
206 void CtcpHandler::defaultHandler(const QString &cmd, CtcpType ctcptype, const QString &prefix, const QString &target, const QString &param) {
207   Q_UNUSED(ctcptype);
208   Q_UNUSED(target);
209   Q_UNUSED(param);
210   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));
211 }
212
213