Add support for Elliptic Curve keys for CertFP
[quassel.git] / src / common / peer.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <QAbstractSocket>
24 #include <QDataStream>
25 #include <QPointer>
26
27 #include "authhandler.h"
28 #include "protocol.h"
29 #include "quassel.h"
30 #include "signalproxy.h"
31
32 class Peer : public QObject
33 {
34     Q_OBJECT
35
36 public:
37     explicit Peer(AuthHandler *authHandler, QObject *parent = 0);
38
39     virtual Protocol::Type protocol() const = 0;
40     virtual QString description() const = 0;
41
42     virtual SignalProxy *signalProxy() const = 0;
43     virtual void setSignalProxy(SignalProxy *proxy) = 0;
44
45     QDateTime connectedSince() const;
46     void setConnectedSince(const QDateTime &connectedSince);
47
48     QString buildDate() const;
49     void setBuildDate(const QString &buildDate);
50
51     QString clientVersion() const;
52     void setClientVersion(const QString &clientVersion);
53
54     bool hasFeature(Quassel::Feature feature) const;
55     Quassel::Features features() const;
56     void setFeatures(Quassel::Features features);
57
58     int id() const;
59     void setId(int id);
60
61     AuthHandler *authHandler() const;
62
63     virtual bool isOpen() const = 0;
64     virtual bool isSecure() const = 0;
65     virtual bool isLocal() const = 0;
66
67     virtual int lag() const = 0;
68
69     virtual QString address() const = 0;
70     virtual quint16 port() const = 0;
71
72 public slots:
73     /* Handshake messages */
74     virtual void dispatch(const Protocol::RegisterClient &) = 0;
75     virtual void dispatch(const Protocol::ClientDenied &) = 0;
76     virtual void dispatch(const Protocol::ClientRegistered &) = 0;
77     virtual void dispatch(const Protocol::SetupData &) = 0;
78     virtual void dispatch(const Protocol::SetupFailed &) = 0;
79     virtual void dispatch(const Protocol::SetupDone &) = 0;
80     virtual void dispatch(const Protocol::Login &) = 0;
81     virtual void dispatch(const Protocol::LoginFailed &) = 0;
82     virtual void dispatch(const Protocol::LoginSuccess &) = 0;
83     virtual void dispatch(const Protocol::SessionState &) = 0;
84
85     /* Sigproxy messages */
86     virtual void dispatch(const Protocol::SyncMessage &) = 0;
87     virtual void dispatch(const Protocol::RpcCall &) = 0;
88     virtual void dispatch(const Protocol::InitRequest &) = 0;
89     virtual void dispatch(const Protocol::InitData &) = 0;
90
91     virtual void close(const QString &reason = QString()) = 0;
92
93 signals:
94     void disconnected();
95     void secureStateChanged(bool secure = true);
96     void lagUpdated(int msecs);
97
98 protected:
99     template<typename T>
100     void handle(const T &protoMessage);
101
102 private:
103     QPointer<AuthHandler> _authHandler;
104
105     QDateTime _connectedSince;
106
107     QString _buildDate;
108     QString _clientVersion;
109     Quassel::Features _features;
110
111     int _id = -1;
112 };
113
114 // We need to special-case Peer* in attached signals/slots, so typedef it for the meta type system
115 typedef Peer * PeerPtr;
116 Q_DECLARE_METATYPE(PeerPtr)
117
118 QDataStream &operator<<(QDataStream &out, PeerPtr ptr);
119 QDataStream &operator>>(QDataStream &in, PeerPtr &ptr);
120
121
122 // Template method needed in the header
123 template<typename T> inline
124 void Peer::handle(const T &protoMessage)
125 {
126     switch(protoMessage.handler()) {
127         case Protocol::Handler::SignalProxy:
128             if (!signalProxy()) {
129                 qWarning() << Q_FUNC_INFO << "Cannot handle message without a SignalProxy!";
130                 return;
131             }
132             signalProxy()->handle(this, protoMessage);
133             break;
134
135         case Protocol::Handler::AuthHandler:
136             if (!authHandler()) {
137                 qWarning() << Q_FUNC_INFO << "Cannot handle auth messages without an active AuthHandler!";
138                 return;
139             }
140             authHandler()->handle(protoMessage);
141             break;
142
143         default:
144             qWarning() << Q_FUNC_INFO << "Unknown handler for protocol message!";
145             return;
146     }
147 }