Finishing the switch to types.h and the resulting cleanup.
[quassel.git] / src / core / ctcphandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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(Server *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           dequotedMessage += 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 QStringList CtcpHandler::parse(CtcpType ctcptype, QString prefix, QString target, QString message) {
85   QStringList messages;
86   QString ctcp;
87   
88   //lowlevel message dequote
89   QString dequotedMessage = dequote(message);
90   
91   // extract tagged / extended data
92   while(dequotedMessage.contains(XDELIM)) {
93     messages << dequotedMessage.section(XDELIM,0,0);
94     ctcp = XdelimDequote(dequotedMessage.section(XDELIM,1,1));
95     dequotedMessage = dequotedMessage.section(XDELIM,2,2);
96     
97     //dispatch the ctcp command
98     QString ctcpcmd = ctcp.section(' ', 0, 0);
99     QString ctcpparam = ctcp.section(' ', 1);
100
101     handle(ctcpcmd, Q_ARG(CtcpType, ctcptype), Q_ARG(QString, prefix), Q_ARG(QString, target), Q_ARG(QString, ctcpparam));
102   }
103   if(!dequotedMessage.isEmpty()) {
104     messages << dequotedMessage;
105   }
106   return messages;
107 }
108
109 QString CtcpHandler::pack(QString ctcpTag, QString message) {
110   return XDELIM + ctcpTag + ' ' + message + XDELIM;
111 }
112
113 void CtcpHandler::query(QString bufname, QString ctcpTag, QString message) {
114   QStringList params;
115   params << bufname << pack(ctcpTag, message);
116   emit putCmd("PRIVMSG", params); 
117 }
118
119 void CtcpHandler::reply(QString bufname, QString ctcpTag, QString message) {
120   QStringList params;
121   params << bufname << pack(ctcpTag, message);
122   emit putCmd("NOTICE", params);
123 }
124
125 //******************************/
126 // CTCP HANDLER
127 //******************************/
128 void CtcpHandler::handleAction(CtcpType ctcptype, QString prefix, QString target, QString param) {
129   emit displayMsg(Message::Action, target, param, prefix);
130 }
131
132 void CtcpHandler::handlePing(CtcpType ctcptype, QString prefix, QString target, QString param) {
133   if(ctcptype == CtcpQuery) {
134     reply(nickFromMask(prefix), "PING", param);
135     emit displayMsg(Message::Server, "", tr("Received CTCP PING request from %1").arg(prefix));
136   } else {
137     // display ping answer
138   }
139 }
140
141 void CtcpHandler::handleVersion(CtcpType ctcptype, QString prefix, QString target, QString param) {
142   if(ctcptype == CtcpQuery) {
143     // FIXME use real Info about quassel :)
144     reply(nickFromMask(prefix), "VERSION", QString("Quassel IRC (Pre-Release) - http://www.quassel-irc.org"));
145     emit displayMsg(Message::Server, "", tr("Received CTCP VERSION request by %1").arg(prefix));
146   } else {
147     // TODO display Version answer
148   }
149 }
150
151 void CtcpHandler::defaultHandler(QString cmd, CtcpType ctcptype, QString prefix, QString target, QString param) {
152   emit displayMsg(Message::Error, "", tr("Received unknown CTCP %1 by %2").arg(cmd).arg(prefix));
153 }
154
155