b0077c19072d3c4499bfcf1de64cd9079784d99d
[quassel.git] / network / server.h
1 /***************************************************************************
2  *   Copyright (C) 2005/06 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
21 #ifndef _SERVER_H_
22 #define _SERVER_H_
23
24 #include <QtCore>
25 #include <QTimer>
26 #include <QtNetwork>
27
28 #include "quassel.h"
29
30 #define DEFAULT_PORT 6667
31
32
33 /**
34  * This is a server object, managing a single connection to an IRC server, handling the associated channels and so on.
35  * We have this run in its own thread mainly to not block other server objects or the core if something goes wrong,
36  * e.g. if some scripts starts running wild...
37  */
38
39 class Server : public QThread {
40   Q_OBJECT
41
42   public:
43     Server();
44     ~Server();
45     static void init();
46
47     void run();
48
49     // serverState state();
50
51   public slots:
52     // void setServerOptions();
53     void connectToIrc(const QString &host, quint16 port = DEFAULT_PORT);
54     void disconnectFromIrc();
55
56     void putRawLine(QString input);
57     void putCmd(QString cmd, QStringList params, QString prefix = 0);
58
59   signals:
60     //void outputLine(const QString & /*, Buffer *target = 0 */);
61
62     void recvRawServerMsg(QString);
63     void recvLine(QString); // temp, should send a message to the GUI
64
65   private slots:
66     void socketHasData();
67     void socketError(QAbstractSocket::SocketError);
68     void socketConnected();
69     void socketDisconnected();
70     void socketStateChanged(QAbstractSocket::SocketState);
71
72     /* Message Handlers */
73     /* handleXxxxFromServer(QString prefix, QStringList params); */
74     void handleNoticeFromServer(QString, QStringList);
75     void handlePingFromServer(QString, QStringList);
76
77     void defaultHandlerForServer(QString cmd, QString prefix, QStringList params);
78
79   private:
80     QTcpSocket socket;
81     QTextStream stream;
82
83     void handleServerMsg(QString rawMsg);
84     void handleUserMsg(QString usrMsg);
85     //static inline void dispatchServerMsg(Message *msg) { msg->getServer()->handleServerMsg(msg); }
86     //static inline void dispatchUserMsg(Message *msg)   { msg->getServer()->handleUserMsg(msg); }
87
88     class ParseError : public Exception {
89       public:
90         ParseError(QString cmd, QString prefix, QStringList params);
91     };
92
93     class UnknownCmdError : public Exception {
94       public:
95         UnknownCmdError(QString cmd, QString prefix, QStringList params);
96     };
97 };
98
99 class Buffer {};
100
101
102 #endif