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