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