~ubuntu-branches/ubuntu/raring/qgo/raring

« back to all changes in this revision

Viewing changes to src/igsconnection.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin A. Godisch
  • Date: 2005-01-01 23:07:10 UTC
  • Revision ID: james.westby@ubuntu.com-20050101230710-fhng6yidm47xlb2i
Tags: upstream-1.0.0-r2
ImportĀ upstreamĀ versionĀ 1.0.0-r2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /* 
 
2
 * igsconnection.cpp
 
3
 *      
 
4
 * insprired by qigs
 
5
 *
 
6
 * authors: Peter Strempel & Tom Le Duc & Johannes Mesa
 
7
 *
 
8
 * this code is still in experimentational phase
 
9
 */
 
10
 
 
11
 
 
12
#include "igsconnection.h"
 
13
#include <qgbkcodec.h> 
 
14
#include <qtextcodec.h>
 
15
#include <qtimer.h>
 
16
 
 
17
 
 
18
IGSConnection::IGSConnection() : IGSInterface()
 
19
 
20
        authState = LOGIN;
 
21
 
 
22
        qsocket = new QSocket(this);
 
23
        CHECK_PTR(qsocket);
 
24
        
 
25
        username = "";
 
26
        password = "";
 
27
        
 
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)));
 
35
}
 
36
 
 
37
IGSConnection::~IGSConnection()
 
38
{
 
39
        delete qsocket;
 
40
}
 
41
 
 
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)
 
45
{
 
46
        if (callbackFP != NULL)
 
47
                (*callbackFP)(txt, size);
 
48
}
 
49
 
 
50
void IGSConnection::sendTextToApp(const char *s)
 
51
{
 
52
        if (callbackFP != NULL)
 
53
                (*callbackFP)(s, (unsigned) strlen(s)); 
 
54
}
 
55
 
 
56
void IGSConnection::sendTextToApp(QString *txt)
 
57
{
 
58
        if (callbackFP != NULL)
 
59
                (*callbackFP)((const char*) *txt, (unsigned) txt->length()); 
 
60
}
 
61
 
 
62
void IGSConnection::sendTextToApp(QCString *txt)
 
63
{
 
64
        if (callbackFP != NULL)
 
65
                (*callbackFP)((const char*) *txt, (unsigned) txt->length());
 
66
}
 
67
 
 
68
// check for 'Login:' (and Password)
 
69
bool IGSConnection::checkPrompt()
 
70
{
 
71
        // prompt can be login prompt or usermode prompt
 
72
                
 
73
        if (bufferLineRest.length() < 4)
 
74
        {
 
75
                qDebug(QString("IGSConnection::checkPrompt called with string of size %1").arg(bufferLineRest.length()));
 
76
 
 
77
                if (authState == PASSWORD)
 
78
                {
 
79
                        int b = qsocket->bytesAvailable();
 
80
                        if (!b)
 
81
                        {
 
82
                                qsocket->waitForMore(500);
 
83
                                b = qsocket->bytesAvailable();
 
84
                                if (!b)
 
85
                                        return false;
 
86
                        }
 
87
 
 
88
                        while (b-- > 0)
 
89
                                bufferLineRest += qsocket->getch();
 
90
                }
 
91
                else
 
92
                        return false; 
 
93
        }
 
94
 
 
95
        switch (authState)
 
96
        {
 
97
                case LOGIN:
 
98
                        if (bufferLineRest.find("Login:") != -1)
 
99
                        {
 
100
                                qDebug("Login: found");
 
101
                                
 
102
                                // tell application what to send
 
103
                                sendTextToApp(bufferLineRest);
 
104
                                if (!username.isEmpty())
 
105
                                {
 
106
                                        sendTextToApp("...sending: {" + QString(username) + "}");
 
107
                                
 
108
                                        sendTextToHost(username);// + '\n');       // CAREFUL : THIS SOMETIMES CHANGES on IGS
 
109
                                }
 
110
 
 
111
                                // next state
 
112
                                if (password)
 
113
                                        authState = PASSWORD;
 
114
                                else
 
115
                                        authState = SESSION;
 
116
                                return true;
 
117
                        }
 
118
                        break;
 
119
 
 
120
                case PASSWORD:
 
121
                        if ((bufferLineRest.find("Password:") != -1) || (bufferLineRest.find("1 1") != -1))
 
122
                        {
 
123
                                qDebug("Password or 1 1: found , strlen = %d", bufferLineRest.length());
 
124
                                sendTextToApp(tr("...send password"));
 
125
                                sendTextToHost(password);
 
126
 
 
127
                                // next state
 
128
                                authState = SESSION;
 
129
                                return true;
 
130
                        }
 
131
                        else if (bufferLineRest.find("guest account") != -1)
 
132
                        {
 
133
                                authState = SESSION;
 
134
                                return true;
 
135
                        }
 
136
                        break;
 
137
 
 
138
                default:
 
139
                        break;
 
140
        }
 
141
        
 
142
        return false;
 
143
}
 
144
 
 
145
 
 
146
/*
 
147
 * Slots
 
148
 */
 
149
 
 
150
void IGSConnection::OnHostFound()
 
151
{
 
152
        qDebug("IGSConnection::OnHostFound()");
 
153
}
 
154
 
 
155
void IGSConnection::OnConnected()
 
156
{
 
157
        qDebug("IGSConnection::OnConnected()");
 
158
 
 
159
        sendTextToApp("Connected to " + qsocket->peerAddress().toString() + " " +
 
160
                  QString::number(qsocket->peerPort()));
 
161
}
 
162
 
 
163
// We got data to read
 
164
void IGSConnection::OnReadyRead()
 
165
{
 
166
//*
 
167
//      qDebug("OnReadyRead....");
 
168
        QString y = QString();
 
169
        while (qsocket->canReadLine())
 
170
        {
 
171
                QString x = qsocket->readLine();
 
172
                if (x.isEmpty())
 
173
                {
 
174
                        qDebug("READ:NULL");
 
175
                        return;
 
176
                }
 
177
 
 
178
                // some statistics
 
179
//              emit signal_setBytesIn(x.length());
 
180
 
 
181
                x.truncate(x.length()-2);
 
182
 
 
183
                sendTextToApp(x);
 
184
 
 
185
                if (authState == PASSWORD)
 
186
                {
 
187
                        bufferLineRest = x;
 
188
                        checkPrompt();
 
189
                        qDebug("PASSWORD***");
 
190
                }
 
191
        }
 
192
        
 
193
        if (authState == LOGIN && qsocket->bytesAvailable() == 7)
 
194
        {
 
195
                qDebug("looking for 'Login:'");
 
196
                while (qsocket->bytesAvailable())
 
197
                        y += qsocket->getch();
 
198
                
 
199
                if (y)
 
200
                {
 
201
                        qDebug("Collected: " + y);
 
202
                        bufferLineRest = y;
 
203
                        checkPrompt();
 
204
                }
 
205
        }
 
206
//*/
 
207
//      convertBlockToLines();
 
208
}
 
209
 
 
210
// Connection was closed from host
 
211
void IGSConnection::OnConnectionClosed() 
 
212
{
 
213
        qDebug("CONNECTION CLOSED BY FOREIGN HOST");
 
214
 
 
215
        // read last data that could be in the buffer
 
216
        OnReadyRead();
 
217
        authState = LOGIN;
 
218
        sendTextToApp("Connection closed by foreign host.\n");
 
219
}
 
220
 
 
221
// Connection was closed from application, but delayed
 
222
void IGSConnection::OnDelayedCloseFinish()
 
223
{
 
224
        qDebug("DELAY CLOSED FINISHED");
 
225
        
 
226
        authState = LOGIN;
 
227
        sendTextToApp("Connection closed.\n");
 
228
}
 
229
 
 
230
void IGSConnection::OnBytesWritten(int /*nbytes*/)
 
231
{
 
232
//      qDebug("%d BYTES WRITTEN", nbytes);
 
233
//      emit signal_setBytesOut(nbytes);
 
234
}
 
235
 
 
236
void IGSConnection::OnError(int i)
 
237
{
 
238
        switch (i)
 
239
        {
 
240
                case QSocket::ErrConnectionRefused: qDebug("ERROR: connection refused...");
 
241
                        break;
 
242
                case QSocket::ErrHostNotFound: qDebug("ERROR: host not found...");
 
243
                        break;
 
244
                case QSocket::ErrSocketRead: qDebug("ERROR: socket read...");
 
245
                        break;
 
246
                default: qDebug("ERROR: unknown Error...");
 
247
                        break;
 
248
        }
 
249
        sendTextToApp("ERROR - Connection closed.\n");
 
250
}
 
251
 
 
252
 
 
253
/*
 
254
 * Functions called from the application
 
255
 */
 
256
 
 
257
bool IGSConnection::isConnected()
 
258
{
 
259
//      qDebug("IGSConnection::isConnected()");
 
260
        return qsocket->state() == QSocket::Connection;
 
261
}
 
262
 
 
263
void IGSConnection::sendTextToHost(const char *txt)
 
264
{
 
265
        // maybe some checking here...
 
266
 
 
267
        int len;
 
268
 
 
269
        if ((len = qsocket->writeBlock(txt, strlen(txt) * sizeof(char))) != -1)
 
270
                qsocket->writeBlock("\r\n", 2);
 
271
        else
 
272
                qWarning(QString("*** failed sending to host: %1").arg(txt));
 
273
}
 
274
 
 
275
void IGSConnection::sendTextToHost(QString *txt)
 
276
{
 
277
        sendTextToHost((const char*) txt->latin1());
 
278
}
 
279
 
 
280
bool IGSConnection::openConnection(const char *host, unsigned int port,
 
281
                                                                        const char *user, const char *pass)
 
282
{
 
283
        if (qsocket->state() != QSocket::Idle ) {
 
284
                qDebug("Called IGSConnection::openConnection while in state %d", qsocket->state());
 
285
                return false;
 
286
        }
 
287
 
 
288
        username = user;
 
289
        password = pass;
 
290
        
 
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));
 
293
 
 
294
        ASSERT(host != 0);
 
295
        ASSERT(port != 0);
 
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;
 
300
}
 
301
 
 
302
bool IGSConnection::closeConnection()
 
303
{
 
304
        // We have no connection?
 
305
        if (qsocket->state() == QSocket::Idle)
 
306
                return false;
 
307
 
 
308
        qDebug("Disconnecting...");
 
309
 
 
310
        // Close it.
 
311
        qsocket->close();
 
312
 
 
313
        // Closing succeeded, return message
 
314
        if (qsocket->state() == QSocket::Idle)
 
315
        {
 
316
                authState = LOGIN;
 
317
                sendTextToApp("Connection closed.\n");
 
318
        }
 
319
 
 
320
        // Not yet closed? Data will be written and then slot OnDelayClosedFinish() called
 
321
        return true;
 
322
}
 
323
 
 
324
const char* IGSConnection::getUsername ()
 
325
{
 
326
        return (const char*) username;
 
327
}