- MessageTypes are now binary exclusive which allows easy checks with multimple condi...
[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 void CtcpHandler::query(QString bufname, QString ctcpTag, QString message) {
123   QStringList params;
124   params << bufname << pack(ctcpTag, message);
125   emit putCmd("PRIVMSG", params); 
126 }
127
128 void CtcpHandler::reply(QString bufname, QString ctcpTag, QString message) {
129   QStringList params;
130   params << bufname << pack(ctcpTag, message);
131   emit putCmd("NOTICE", params);
132 }
133
134 //******************************/
135 // CTCP HANDLER
136 //******************************/
137 void CtcpHandler::handleAction(CtcpType ctcptype, QString prefix, QString target, QString param) {
138   Q_UNUSED(ctcptype)
139   emit displayMsg(Message::Action, typeByTarget(target), target, param, prefix);
140 }
141
142 void CtcpHandler::handlePing(CtcpType ctcptype, QString prefix, QString target, QString param) {
143   Q_UNUSED(target)
144   if(ctcptype == CtcpQuery) {
145     reply(nickFromMask(prefix), "PING", param);
146     emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING request from %1").arg(prefix));
147   } else {
148     // display ping answer
149     uint now = QDateTime::currentDateTime().toTime_t();
150     uint then = QDateTime().fromTime_t(param.toInt()).toTime_t();
151     emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP PING answer from %1 with %2 seconds round trip time").arg(prefix).arg(now-then));
152   }
153 }
154
155 void CtcpHandler::handleVersion(CtcpType ctcptype, QString prefix, QString target, QString param) {
156   Q_UNUSED(target)
157   if(ctcptype == CtcpQuery) {
158     // FIXME use real Info about quassel :)
159     reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC (v%1 build >= %2) -- http://www.quassel-irc.org")
160         .arg(Global::quasselVersion).arg(Global::quasselBuild));
161     emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION request by %1").arg(prefix));
162   } else {
163     // display Version answer
164     emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Received CTCP VERSION answer from %1: %2").arg(prefix).arg(param));
165   }
166 }
167
168 void CtcpHandler::defaultHandler(QString cmd, CtcpType ctcptype, QString prefix, QString target, QString param) {
169   Q_UNUSED(ctcptype);
170   Q_UNUSED(target);
171   Q_UNUSED(param);
172   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));
173 }
174
175