6
* authors: Peter Strempel & Tom Le Duc & Johannes Mesa
8
* this code is still in experimentational phase
12
#include "igsconnection.h"
13
#include <qgbkcodec.h>
14
#include <qtextcodec.h>
18
IGSConnection::IGSConnection() : IGSInterface()
22
qsocket = new QSocket(this);
28
connect(qsocket, SIGNAL(hostFound()), SLOT(OnHostFound()));
29
connect(qsocket, SIGNAL(connected()), SLOT(OnConnected()));
30
connect(qsocket, SIGNAL(readyRead()), SLOT(OnReadyRead()));
31
connect(qsocket, SIGNAL(connectionClosed()), SLOT(OnConnectionClosed()));
32
connect(qsocket, SIGNAL(delayedCloseFinished()), SLOT(OnDelayedCloseFinish()));
33
connect(qsocket, SIGNAL(bytesWritten(int)), SLOT(OnBytesWritten(int)));
34
connect(qsocket, SIGNAL(error(int)), SLOT(OnError(int)));
37
IGSConnection::~IGSConnection()
42
// maybe callback should be callback(void)...,
43
// any data can be transferred after signalling with normal functions
44
void IGSConnection::sendTextToApp(const char *txt, unsigned size)
46
if (callbackFP != NULL)
47
(*callbackFP)(txt, size);
50
void IGSConnection::sendTextToApp(const char *s)
52
if (callbackFP != NULL)
53
(*callbackFP)(s, (unsigned) strlen(s));
56
void IGSConnection::sendTextToApp(QString *txt)
58
if (callbackFP != NULL)
59
(*callbackFP)((const char*) *txt, (unsigned) txt->length());
62
void IGSConnection::sendTextToApp(QCString *txt)
64
if (callbackFP != NULL)
65
(*callbackFP)((const char*) *txt, (unsigned) txt->length());
68
// check for 'Login:' (and Password)
69
bool IGSConnection::checkPrompt()
71
// prompt can be login prompt or usermode prompt
73
if (bufferLineRest.length() < 4)
75
qDebug(QString("IGSConnection::checkPrompt called with string of size %1").arg(bufferLineRest.length()));
77
if (authState == PASSWORD)
79
int b = qsocket->bytesAvailable();
82
qsocket->waitForMore(500);
83
b = qsocket->bytesAvailable();
89
bufferLineRest += qsocket->getch();
98
if (bufferLineRest.find("Login:") != -1)
100
qDebug("Login: found");
102
// tell application what to send
103
sendTextToApp(bufferLineRest);
104
if (!username.isEmpty())
106
sendTextToApp("...sending: {" + QString(username) + "}");
108
sendTextToHost(username);// + '\n'); // CAREFUL : THIS SOMETIMES CHANGES on IGS
113
authState = PASSWORD;
121
if ((bufferLineRest.find("Password:") != -1) || (bufferLineRest.find("1 1") != -1))
123
qDebug("Password or 1 1: found , strlen = %d", bufferLineRest.length());
124
sendTextToApp(tr("...send password"));
125
sendTextToHost(password);
131
else if (bufferLineRest.find("guest account") != -1)
150
void IGSConnection::OnHostFound()
152
qDebug("IGSConnection::OnHostFound()");
155
void IGSConnection::OnConnected()
157
qDebug("IGSConnection::OnConnected()");
159
sendTextToApp("Connected to " + qsocket->peerAddress().toString() + " " +
160
QString::number(qsocket->peerPort()));
163
// We got data to read
164
void IGSConnection::OnReadyRead()
167
// qDebug("OnReadyRead....");
168
QString y = QString();
169
while (qsocket->canReadLine())
171
QString x = qsocket->readLine();
179
// emit signal_setBytesIn(x.length());
181
x.truncate(x.length()-2);
185
if (authState == PASSWORD)
189
qDebug("PASSWORD***");
193
if (authState == LOGIN && qsocket->bytesAvailable() == 7)
195
qDebug("looking for 'Login:'");
196
while (qsocket->bytesAvailable())
197
y += qsocket->getch();
201
qDebug("Collected: " + y);
207
// convertBlockToLines();
210
// Connection was closed from host
211
void IGSConnection::OnConnectionClosed()
213
qDebug("CONNECTION CLOSED BY FOREIGN HOST");
215
// read last data that could be in the buffer
218
sendTextToApp("Connection closed by foreign host.\n");
221
// Connection was closed from application, but delayed
222
void IGSConnection::OnDelayedCloseFinish()
224
qDebug("DELAY CLOSED FINISHED");
227
sendTextToApp("Connection closed.\n");
230
void IGSConnection::OnBytesWritten(int /*nbytes*/)
232
// qDebug("%d BYTES WRITTEN", nbytes);
233
// emit signal_setBytesOut(nbytes);
236
void IGSConnection::OnError(int i)
240
case QSocket::ErrConnectionRefused: qDebug("ERROR: connection refused...");
242
case QSocket::ErrHostNotFound: qDebug("ERROR: host not found...");
244
case QSocket::ErrSocketRead: qDebug("ERROR: socket read...");
246
default: qDebug("ERROR: unknown Error...");
249
sendTextToApp("ERROR - Connection closed.\n");
254
* Functions called from the application
257
bool IGSConnection::isConnected()
259
// qDebug("IGSConnection::isConnected()");
260
return qsocket->state() == QSocket::Connection;
263
void IGSConnection::sendTextToHost(const char *txt)
265
// maybe some checking here...
269
if ((len = qsocket->writeBlock(txt, strlen(txt) * sizeof(char))) != -1)
270
qsocket->writeBlock("\r\n", 2);
272
qWarning(QString("*** failed sending to host: %1").arg(txt));
275
void IGSConnection::sendTextToHost(QString *txt)
277
sendTextToHost((const char*) txt->latin1());
280
bool IGSConnection::openConnection(const char *host, unsigned int port,
281
const char *user, const char *pass)
283
if (qsocket->state() != QSocket::Idle ) {
284
qDebug("Called IGSConnection::openConnection while in state %d", qsocket->state());
291
qDebug("Connecting to %s %d as [%s], [%s]...", host, port, username.latin1(), (password ? "***" : "NULL"));
292
sendTextToApp(tr("Trying to connect to") + " " + QString(host) + " " + QString::number(port));
296
int len = qstrlen(host);
297
if ((len > 0) && (len < 200)) // otherwise host points to junk
298
qsocket->connectToHost(host, (Q_UINT16) port);
299
return qsocket->state() != QSocket::Idle;
302
bool IGSConnection::closeConnection()
304
// We have no connection?
305
if (qsocket->state() == QSocket::Idle)
308
qDebug("Disconnecting...");
313
// Closing succeeded, return message
314
if (qsocket->state() == QSocket::Idle)
317
sendTextToApp("Connection closed.\n");
320
// Not yet closed? Data will be written and then slot OnDelayClosedFinish() called
324
const char* IGSConnection::getUsername ()
326
return (const char*) username;