We have a working message parser now, and a framework for handler functions!
[quassel.git] / network / server.cpp
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 #include "quassel.h"
22 #include "server.h"
23 #include "cmdcodes.h"
24
25 Server::Server() {
26   socket = new QTcpSocket();
27
28 }
29
30 Server::~Server() {
31   delete socket;
32 }
33
34 void Server::init() {
35   Message::init(&dispatchServerMsg, &dispatchUserMsg);
36 }
37
38 void Server::run() {
39   connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
40   connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
41   connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
42   connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
43   connect(socket, SIGNAL(readyRead()), this, SLOT(socketHasData()));
44
45   stream.setDevice(socket);
46   //connectToIrc("irc.quakenet.org", 6667);
47   exec();
48 }
49
50 /*
51 QAbstractSocket::SocketState TcpConnection::state( ) const {
52   return socket.state();
53 }
54 */
55
56 void Server::connectToIrc( const QString & host, quint16 port ) {
57   qDebug() << "Connecting...";
58   socket->connectToHost(host, port);
59 }
60
61 void Server::disconnectFromIrc( ) {
62   socket->disconnectFromHost();
63 }
64
65 void Server::putRawLine( const QString &s ) {
66   qDebug() << "Sent: " << s;
67   stream << s << "\r\n" << flush;
68   //Message::createFromServerString(this, s);
69 }
70
71 void Server::socketHasData( ) {
72   while(socket->canReadLine()) {
73     QString s = stream.readLine();
74     qDebug() << "Read: " << s;
75     emit recvRawServerMsg(s);
76     Message *msg = Message::createFromServerString(this, s);
77     if(msg) {
78       try { handleServerMsg(msg); } catch(Exception e) {
79         emit recvLine(e.msg() + "\n");
80       }
81     }
82     delete msg;
83   }
84 }
85
86 void Server::socketError( QAbstractSocket::SocketError err ) {
87   qDebug() << "Socket Error!";
88   //emit error(err);
89 }
90
91 void Server::socketConnected( ) {
92   qDebug() << "Socket connected!";
93   //emit connected();
94 }
95
96 void Server::socketDisconnected( ) {
97   qDebug() << "Socket disconnected!";
98   //emit disconnected();
99 }
100
101 void Server::socketStateChanged(QAbstractSocket::SocketState state) {
102   qDebug() << "Socket state changed: " << state;
103 }
104
105 /** Handle a message sent by the IRC server that does not have a custom handler. */
106 void Server::handleServerMsg(Message *msg) {
107   int cmdCode = msg->getCmdCode();
108   QString prefix = msg->getPrefix();
109   QStringList params = msg->getParams();
110   if(cmdCode < 0) {
111     switch(-cmdCode) {
112       case CMD_PING:
113         // PING <server1> [<server2>]
114         if(params.size() == 1) {
115           putRawLine(QString("PONG :") + params[0]);
116         } else if(params.size() == 2) {
117           putRawLine(QString("PONG ") + params[0] + " :" + params[1]);
118         } else throw ParseError(msg);
119         break;
120
121       default:
122         throw Exception(QString("No handler installed for command: ") + msg->getCmd() + " " + msg->getParams().join(" "));
123     }
124   } else if(msg->getCmdCode() > 0) {
125     switch(msg->getCmdCode()) {
126
127       default:
128         //
129         throw Exception(msg->getCmd() + " " + msg->getParams().join(" "));
130     }
131
132   } else {
133     throw UnknownCmdError(msg);
134   }
135 }
136
137 QString Server::handleUserMsg(Message *msg) {
138
139   return "";
140 }
141
142 /* Exception classes for message handling */
143 Server::ParseError::ParseError(Message *msg) {
144   _msg = QString("Command Parse Error: ") + msg->getCmd() + msg->getParams().join(" ");
145
146 }
147
148 Server::UnknownCmdError::UnknownCmdError(Message *msg) {
149   _msg = QString("Unknown Command: ") + msg->getCmd();
150
151 }