Fix core crash
[quassel.git] / src / core / ctcpparser.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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
21 #ifndef CTCPPARSER_H
22 #define CTCPPARSER_H
23
24 #include <QUuid>
25
26 #include "corenetwork.h"
27 #include "eventmanager.h"
28 #include "ircevent.h"
29
30 class CoreSession;
31 class CtcpEvent;
32
33 class CtcpParser : public QObject {
34   Q_OBJECT
35
36 public:
37   CtcpParser(CoreSession *coreSession, QObject *parent = 0);
38
39   inline CoreSession *coreSession() const { return _coreSession; }
40
41   void query(CoreNetwork *network, const QString &bufname, const QString &ctcpTag, const QString &message);
42   void reply(CoreNetwork *network, const QString &bufname, const QString &ctcpTag, const QString &message);
43
44   Q_INVOKABLE void processIrcEventRawNotice(IrcEventRawMessage *event);
45   Q_INVOKABLE void processIrcEventRawPrivmsg(IrcEventRawMessage *event);
46
47   Q_INVOKABLE void sendCtcpEvent(CtcpEvent *event);
48
49 signals:
50   void newEvent(Event *event);
51
52 protected:
53   inline CoreNetwork *coreNetwork(NetworkEvent *e) const { return qobject_cast<CoreNetwork *>(e->network()); }
54
55   // FIXME duplicates functionality in EventStringifier, maybe want to put that in something common
56   //! Creates and sends a MessageEvent
57   void displayMsg(NetworkEvent *event,
58                   Message::Type msgType,
59                   const QString &msg,
60                   const QString &sender = QString(),
61                   const QString &target = QString(),
62                   Message::Flags msgFlags = Message::None);
63
64   void parse(IrcEventRawMessage *event, Message::Type msgType);
65
66   QByteArray lowLevelQuote(const QByteArray &);
67   QByteArray lowLevelDequote(const QByteArray &);
68   QByteArray xdelimQuote(const QByteArray &);
69   QByteArray xdelimDequote(const QByteArray &);
70
71   QByteArray pack(const QByteArray &ctcpTag, const QByteArray &message);
72   void packedReply(CoreNetwork *network, const QString &bufname, const QList<QByteArray> &replies);
73
74 private:
75   inline QString targetDecode(IrcEventRawMessage *e, const QByteArray &msg) { return coreNetwork(e)->userDecode(e->target(), msg); }
76
77   CoreSession *_coreSession;
78
79   struct CtcpReply {
80     CoreNetwork *network;
81     QString bufferName;
82     QList<QByteArray> replies;
83
84     CtcpReply() : network(0) {}
85     CtcpReply(CoreNetwork *net, const QString &buf) : network(net), bufferName(buf) {}
86   };
87
88   QHash<QUuid, CtcpReply> _replies;
89
90   QHash<QByteArray, QByteArray> _ctcpMDequoteHash;
91   QHash<QByteArray, QByteArray> _ctcpXDelimDequoteHash;
92 };
93
94 #endif