a3bd3087d519bca62276c666995f907d82bc21ee
[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
26 CtcpHandler::CtcpHandler(NetworkConnection *parent)
27   : BasicHandler(parent) {
28
29   QString MQUOTE = QString('\020');
30   ctcpMDequoteHash[MQUOTE + '0'] = QString('\000');
31   ctcpMDequoteHash[MQUOTE + 'n'] = QString('\n');
32   ctcpMDequoteHash[MQUOTE + 'r'] = QString('\r');
33   ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
34
35   XDELIM = QString('\001');
36   QString XQUOTE = QString('\134');
37   ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
38   ctcpXDelimDequoteHash[XQUOTE + QString('a')] = XDELIM;
39 }
40
41 QString CtcpHandler::dequote(QString message) {
42   QString dequotedMessage;
43   QString messagepart;
44   QHash<QString, QString>::iterator ctcpquote;
45   
46   // copy dequote Message
47   for(int i = 0; i < message.size(); i++) {
48     messagepart = message[i];
49     if(i+1 < message.size()) {
50       for(ctcpquote = ctcpMDequoteHash.begin(); ctcpquote != ctcpMDequoteHash.end(); ++ctcpquote) {
51         if(message.mid(i,2) == ctcpquote.key()) {
52           messagepart = ctcpquote.value();
53           i++;
54           break;
55         }
56       }
57     }
58     dequotedMessage += messagepart;
59   }
60   return dequotedMessage;
61 }
62
63
64 QString CtcpHandler::XdelimDequote(QString message) {
65   QString dequotedMessage;
66   QString messagepart;
67   QHash<QString, QString>::iterator xdelimquote;
68
69   for(int i = 0; i < message.size(); i++) {
70     messagepart = message[i];
71     if(i+1 < message.size()) {
72       for(xdelimquote = ctcpXDelimDequoteHash.begin(); xdelimquote != ctcpXDelimDequoteHash.end(); ++xdelimquote) {
73         if(message.mid(i,2) == xdelimquote.key()) {
74           messagepart = xdelimquote.value();
75           i++;
76           break;
77         }
78       }
79     }
80     dequotedMessage += messagepart;
81   }
82   return dequotedMessage;
83 }
84
85 void CtcpHandler::parse(Message::Type messageType, QString prefix, QString target, QString message) {
86   QString ctcp;
87   
88   //lowlevel message dequote
89   QString dequotedMessage = dequote(message);
90
91   CtcpType ctcptype = (messageType == Message::Notice)
92     ? CtcpReply
93     : CtcpQuery;
94
95   // extract tagged / extended data
96   while(dequotedMessage.contains(XDELIM)) {
97     if(dequotedMessage.indexOf(XDELIM) > 0)
98       emit displayMsg(messageType, target, dequotedMessage.section(XDELIM,0,0), prefix);
99     // messages << dequotedMessage.section(XDELIM,0,0), prefix);
100     ctcp = XdelimDequote(dequotedMessage.section(XDELIM,1,1));
101     dequotedMessage = dequotedMessage.section(XDELIM,2,2);
102     
103     //dispatch the ctcp command
104     QString ctcpcmd = ctcp.section(' ', 0, 0);
105     QString ctcpparam = ctcp.section(' ', 1);
106
107     handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
108   }
109   
110   if(!dequotedMessage.isEmpty())
111     emit displayMsg(messageType, target, dequotedMessage, prefix);
112
113 }
114
115 QString CtcpHandler::pack(QString ctcpTag, QString message) {
116   return XDELIM + ctcpTag + ' ' + message + XDELIM;
117 }
118
119 void CtcpHandler::query(QString bufname, QString ctcpTag, QString message) {
120   QStringList params;
121   params << bufname << pack(ctcpTag, message);
122   emit putCmd("PRIVMSG", params); 
123 }
124
125 void CtcpHandler::reply(QString bufname, QString ctcpTag, QString message) {
126   QStringList params;
127   params << bufname << pack(ctcpTag, message);
128   emit putCmd("NOTICE", params);
129 }
130
131 //******************************/
132 // CTCP HANDLER
133 //******************************/
134 void CtcpHandler::handleAction(CtcpType ctcptype, QString prefix, QString target, QString param) {
135   Q_UNUSED(ctcptype)
136   emit displayMsg(Message::Action, target, param, prefix);
137 }
138
139 void CtcpHandler::handlePing(CtcpType ctcptype, QString prefix, QString target, QString param) {
140   Q_UNUSED(target)
141   if(ctcptype == CtcpQuery) {
142     reply(nickFromMask(prefix), "PING", param);
143     emit displayMsg(Message::Server, "", tr("Received CTCP PING request from %1").arg(prefix));
144   } else {
145     // display ping answer
146     uint now = QDateTime::currentDateTime().toTime_t();
147     uint then = QDateTime().fromTime_t(param.toInt()).toTime_t();
148     emit displayMsg(Message::Server, "", tr("Received CTCP PING answer from %1 with %2 seconds round trip time").arg(prefix).arg(now-then));
149   }
150 }
151
152 void CtcpHandler::handleVersion(CtcpType ctcptype, QString prefix, QString target, QString param) {
153   Q_UNUSED(target)
154   if(ctcptype == CtcpQuery) {
155     // FIXME use real Info about quassel :)
156     reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC (v%1 build >= %2) -- http://www.quassel-irc.org")
157         .arg(Global::quasselVersion).arg(Global::quasselBuild));
158     emit displayMsg(Message::Server, "", tr("Received CTCP VERSION request by %1").arg(prefix));
159   } else {
160     // display Version answer
161     emit displayMsg(Message::Server, "", tr("Received CTCP VERSION answer from %1: %2").arg(prefix).arg(param));
162   }
163 }
164
165 void CtcpHandler::defaultHandler(QString cmd, CtcpType ctcptype, QString prefix, QString target, QString param) {
166   Q_UNUSED(ctcptype);
167   Q_UNUSED(target);
168   Q_UNUSED(param);
169   emit displayMsg(Message::Error, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));
170 }
171
172