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>
17
IGSConnection::IGSConnection() : IGSInterface()
21
qsocket = new QSocket(this);
29
connect(qsocket, SIGNAL(hostFound()), SLOT(OnHostFound()));
30
connect(qsocket, SIGNAL(connected()), SLOT(OnConnected()));
31
connect(qsocket, SIGNAL(readyRead()), SLOT(OnReadyRead()));
32
connect(qsocket, SIGNAL(connectionClosed()), SLOT(OnConnectionClosed()));
33
connect(qsocket, SIGNAL(delayedCloseFinished()), SLOT(OnDelayedCloseFinish()));
34
connect(qsocket, SIGNAL(bytesWritten(int)), SLOT(OnBytesWritten(int)));
35
connect(qsocket, SIGNAL(error(int)), SLOT(OnError(int)));
38
IGSConnection::~IGSConnection()
43
// maybe callback should be callback(void)...,
44
// any data can be transferred after signalling with normal functions
45
void IGSConnection::sendTextToApp(QString txt)
47
if (callbackFP != NULL)
51
// check for 'Login:' (and Password)
52
bool IGSConnection::checkPrompt()
54
// prompt can be login prompt or usermode prompt
56
if (bufferLineRest.length() < 4)
58
qDebug(QString("IGSConnection::checkPrompt called with string of size %1").arg(bufferLineRest.length()));
60
if (authState == PASSWORD)
62
int b = qsocket->bytesAvailable();
65
qsocket->waitForMore(500);
66
b = qsocket->bytesAvailable();
72
bufferLineRest += qsocket->getch();
81
if (bufferLineRest.find("Login:") != -1)
83
qDebug("Login: found");
85
// tell application what to send
86
sendTextToApp(bufferLineRest);
87
if (!username.isEmpty())
89
sendTextToApp("...sending: {" + QString(username) + "}");
91
sendTextToHost(username, true);// + '\n'); // CAREFUL : THIS SOMETIMES CHANGES on IGS
104
if ((bufferLineRest.find("Password:") != -1) || (bufferLineRest.find("1 1") != -1))
106
qDebug("Password or 1 1: found , strlen = %d", bufferLineRest.length());
107
sendTextToApp(tr("...send password"));
108
sendTextToHost(password, true);
114
else if (bufferLineRest.find("guest account") != -1)
133
void IGSConnection::OnHostFound()
135
qDebug("IGSConnection::OnHostFound()");
138
void IGSConnection::OnConnected()
140
qDebug("IGSConnection::OnConnected()");
142
sendTextToApp("Connected to " + qsocket->peerAddress().toString() + " " +
143
QString::number(qsocket->peerPort()));
145
// We got data to read
146
void IGSConnection::OnReadyRead()
149
// qDebug("OnReadyRead....");
150
while (qsocket->canReadLine())
152
int size = qsocket->bytesAvailable() + 1;
153
char *c = new char[size];
154
int bytes = qsocket->readLine(c, size);
155
QString x = textCodec->toUnicode(c);
164
// emit signal_setBytesIn(x.length());
166
x.truncate(x.length()-2);
170
if (authState == PASSWORD)
174
qDebug("PASSWORD***");
178
if (authState == LOGIN && qsocket->bytesAvailable() == 7)
181
qDebug("looking for 'Login:'");
182
while (qsocket->bytesAvailable())
183
y += qsocket->getch();
187
qDebug("Collected: " + y);
193
// convertBlockToLines();
196
// Connection was closed from host
197
void IGSConnection::OnConnectionClosed()
199
qDebug("CONNECTION CLOSED BY FOREIGN HOST");
201
// read last data that could be in the buffer
204
sendTextToApp("Connection closed by foreign host.\n");
207
// Connection was closed from application, but delayed
208
void IGSConnection::OnDelayedCloseFinish()
210
qDebug("DELAY CLOSED FINISHED");
213
sendTextToApp("Connection closed.\n");
216
void IGSConnection::OnBytesWritten(int /*nbytes*/)
218
// qDebug("%d BYTES WRITTEN", nbytes);
219
// emit signal_setBytesOut(nbytes);
222
void IGSConnection::OnError(int i)
226
case QSocket::ErrConnectionRefused: qDebug("ERROR: connection refused...");
228
case QSocket::ErrHostNotFound: qDebug("ERROR: host not found...");
230
case QSocket::ErrSocketRead: qDebug("ERROR: socket read...");
232
default: qDebug("ERROR: unknown Error...");
235
sendTextToApp("ERROR - Connection closed.\n");
240
* Functions called from the application
243
bool IGSConnection::isConnected()
245
// qDebug("IGSConnection::isConnected()");
246
return qsocket->state() == QSocket::Connection;
249
void IGSConnection::sendTextToHost(QString txt, bool ignoreCodec)
252
* This is intended because for weird reasons, if 'username' s given
253
* with a codec, IGS might reject it and ban the IP (!!!) for several hours.
254
* This seems to concern only windows users.
255
* Therefore, we pretend to ignore the codec when passing username or password
262
const char * txt2 = txt.latin1();
264
if ((len = qsocket->writeBlock(txt2, strlen(txt2) * sizeof(char))) != -1)
265
qsocket->writeBlock("\r\n", 2);
267
qWarning(QString("*** failed sending to host: %1").arg(txt2));
272
QCString raw = textCodec->fromUnicode(txt);
274
if (qsocket->writeBlock(raw.data(), raw.size() - 1) != -1)
275
qsocket->writeBlock("\r\n", 2);
277
qWarning(QString("*** failed sending to host: %1").arg(txt));
282
void IGSConnection::setTextCodec(QString codec)
284
textCodec = QTextCodec::codecForName(codec);
286
textCodec = QTextCodec::codecForLocale();
287
CHECK_PTR(textCodec);
292
bool IGSConnection::openConnection(const char *host, unsigned int port, const char *user, const char *pass)
294
if (qsocket->state() != QSocket::Idle ) {
295
qDebug("Called IGSConnection::openConnection while in state %d", qsocket->state());
299
//textCodec = QTextCodec::codecForName(codec);
301
// textCodec = QTextCodec::codecForLocale();
302
//CHECK_PTR(textCodec);
307
qDebug("Connecting to %s %d as [%s], [%s]...", host, port, username.latin1(), (password ? "***" : "NULL"));
308
sendTextToApp(tr("Trying to connect to %1 %2").arg(host,port));
312
int len = qstrlen(host);
313
if ((len > 0) && (len < 200)) // otherwise host points to junk
314
qsocket->connectToHost(host, (Q_UINT16) port);
315
return qsocket->state() != QSocket::Idle;
318
bool IGSConnection::closeConnection()
320
// We have no connection?
321
if (qsocket->state() == QSocket::Idle)
324
qDebug("Disconnecting...");
329
// Closing succeeded, return message
330
if (qsocket->state() == QSocket::Idle)
333
sendTextToApp("Connection closed.\n");
336
// Not yet closed? Data will be written and then slot OnDelayClosedFinish() called
340
const char* IGSConnection::getUsername ()
342
return (const char*) username;