Happy New Year!
[quassel.git] / src / common / authhandler.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 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 #ifndef AUTHHANDLER_H
22 #define AUTHHANDLER_H
23
24 #include <QTcpSocket>
25
26 #include "protocol.h"
27
28 class Peer;
29
30 class AuthHandler : public QObject
31 {
32     Q_OBJECT
33
34 public:
35     enum State {
36         UnconnectedState,
37         HostLookupState,
38         ConnectingState,
39         ConnectedState,
40         RetryWithLegacyState,
41         AuthenticatingState,
42         AuthenticatedState,
43         ClosingState
44     };
45
46     AuthHandler(QObject *parent = 0);
47
48     State state() const;
49     QTcpSocket *socket() const;
50
51     virtual void handle(const Protocol::RegisterClient &) { invalidMessage(); }
52     virtual void handle(const Protocol::ClientDenied &) { invalidMessage(); }
53     virtual void handle(const Protocol::ClientRegistered &) { invalidMessage(); }
54     virtual void handle(const Protocol::SetupData &) { invalidMessage(); }
55     virtual void handle(const Protocol::SetupFailed &) { invalidMessage(); }
56     virtual void handle(const Protocol::SetupDone &) { invalidMessage(); }
57     virtual void handle(const Protocol::Login &) { invalidMessage(); }
58     virtual void handle(const Protocol::LoginFailed &) { invalidMessage(); }
59     virtual void handle(const Protocol::LoginSuccess &) { invalidMessage(); }
60     virtual void handle(const Protocol::SessionState &) { invalidMessage(); }
61
62     // fallback for unknown types, will trigger an error
63     template<class T>
64     void handle(const T &) { invalidMessage(); }
65
66 public slots:
67     void close();
68
69 signals:
70     void stateChanged(State state);
71     void disconnected();
72
73     void socketStateChanged(QAbstractSocket::SocketState state);
74     void socketError(QAbstractSocket::SocketError error, const QString &errorString);
75
76 protected:
77     void setSocket(QTcpSocket *socket);
78     void setState(State state);
79
80 private slots:
81     void onSocketError(QAbstractSocket::SocketError error);
82     void onSocketDisconnected();
83
84 private:
85     void invalidMessage();
86
87     State _state;
88     QTcpSocket *_socket; // FIXME: should be a QSharedPointer? -> premature disconnect before the peer has taken over
89     bool _disconnectedSent;
90 };
91
92 #endif