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