1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
// -*- Mode: c++-mode; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 2; -*-
//
// Copyright (C) 2004 Grzegorz Jaskiewicz <gj at pointblue.com.pl>
//
// gadudcctransaction.cpp
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
#include "kopetetransfermanager.h"
#include "kopetemetacontact.h"
#include "kopeteuiglobal.h"
#include <tqsocketnotifier.h>
#include <tqfile.h>
#include "gadudcctransaction.h"
#include "gaducontact.h"
#include "gaduaccount.h"
#include "gadudcc.h"
#include "libgadu.h"
GaduDCCTransaction::GaduDCCTransaction( GaduDCC* parent, const char* name )
:TQObject( parent, name ), gaduDCC_( parent )
{
read_ = NULL;
write_ = NULL;
contact = NULL;
transfer_ = NULL;
dccSock_ = NULL;
peer = 0;
}
GaduDCCTransaction::~GaduDCCTransaction()
{
closeDCC();
}
unsigned int
GaduDCCTransaction::recvUIN()
{
if ( dccSock_ ) {
return dccSock_->uin;
}
return 0;
}
unsigned int
GaduDCCTransaction::peerUIN()
{
if ( dccSock_ ) {
return dccSock_->peer_uin;
}
return 0;
}
bool
GaduDCCTransaction::setupOutgoing( GaduContact* peerContact, TQString& filePath )
{
GaduContact* me;
GaduAccount* metoo;
if ( !peerContact ) {
return false;
}
me = static_cast<GaduContact*>( peerContact->account()->myself() );
TQString aaa = peerContact->contactIp().toString();
kdDebug( 14100 ) << "slotOutgoin for UIN: " << peerContact->uin() << " port " << peerContact->contactPort() << " ip " <<aaa<< endl;
kdDebug( 14100 ) << "File path is " << filePath << endl;
if ( peerContact->contactPort() >= 10 ) {
dccSock_ = gg_dcc_send_file( htonl( peerContact->contactIp().ip4Addr() ), peerContact->contactPort(), me->uin(), peerContact->uin() );
gg_dcc_fill_file_info(dccSock_,filePath.ascii());
transfer_ = Kopete::TransferManager::transferManager()->addTransfer ( peerContact,
filePath, dccSock_->file_info.size, peerContact->metaContact()->displayName(), Kopete::FileTransferInfo::Outgoing );
createNotifiers( true );
enableNotifiers( dccSock_->check );
}
else {
kdDebug( 14100 ) << "Peer " << peerContact->uin() << " is passive, requesting reverse connection" << endl;
metoo = static_cast<GaduAccount*>( me->account() );
gaduDCC_->requests[peerContact->uin()]=filePath;
metoo->dccRequest( peerContact );
}
return false;
}
bool
GaduDCCTransaction::setupIncoming( const unsigned int uin, GaduContact* peerContact )
{
if ( !peerContact ) {
kdDebug( 14100 ) << "setupIncoming called with peerContact == NULL " << endl;
return false;
}
TQString aaa = peerContact->contactIp().toString();
kdDebug( 14100 ) << "setupIncoming for UIN: " << uin << " port " << peerContact->contactPort() << " ip " <<aaa<< endl;
peer = peerContact->uin();
dccSock_ = gg_dcc_get_file( htonl( peerContact->contactIp().ip4Addr() ), peerContact->contactPort(), uin, peer );
contact = peerContact;
return setupIncoming( dccSock_ );
}
bool
GaduDCCTransaction::setupIncoming( gg_dcc* dccS )
{
if ( !dccS ) {
kdDebug(14100) << "gg_dcc_get_file failed in GaduDCCTransaction::setupIncoming" << endl;
return false;
}
dccSock_ = dccS;
peer = dccS->uin;
connect ( Kopete::TransferManager::transferManager(), TQT_SIGNAL( accepted( Kopete::Transfer *, const TQString & ) ),
this, TQT_SLOT( slotIncomingTransferAccepted ( Kopete::Transfer *, const TQString & ) ) );
connect ( Kopete::TransferManager::transferManager(), TQT_SIGNAL( refused( const Kopete::FileTransferInfo & ) ),
this, TQT_SLOT( slotTransferRefused( const Kopete::FileTransferInfo & ) ) );
incoming = true;
createNotifiers( true );
enableNotifiers( dccSock_->check );
return true;
}
void
GaduDCCTransaction::closeDCC()
{
kdDebug(14100) << "closeDCC()" << endl;
disableNotifiers();
destroyNotifiers();
gg_dcc_free( dccSock_ );
dccSock_ = NULL;
}
void
GaduDCCTransaction::destroyNotifiers()
{
disableNotifiers();
if ( read_ ) {
delete read_;
read_ = NULL;
}
if ( write_ ) {
delete write_;
write_ = NULL;
}
}
void
GaduDCCTransaction::createNotifiers( bool connect )
{
if ( !dccSock_ ){
return;
}
read_ = new TQSocketNotifier( dccSock_->fd, TQSocketNotifier::Read, this );
read_->setEnabled( false );
write_ = new TQSocketNotifier( dccSock_->fd, TQSocketNotifier::Write, this );
write_->setEnabled( false );
if ( connect ) {
TQObject::connect( read_, TQT_SIGNAL( activated( int ) ), TQT_SLOT( watcher() ) );
TQObject::connect( write_, TQT_SIGNAL( activated( int ) ), TQT_SLOT( watcher() ) );
}
}
void
GaduDCCTransaction::enableNotifiers( int checkWhat )
{
if( (checkWhat & GG_CHECK_READ) && read_ ) {
read_->setEnabled( true );
}
if( (checkWhat & GG_CHECK_WRITE) && write_ ) {
write_->setEnabled( true );
}
}
void
GaduDCCTransaction::disableNotifiers()
{
if ( read_ ) {
read_->setEnabled( false );
}
if ( write_ ) {
write_->setEnabled( false );
}
}
void
GaduDCCTransaction::slotIncomingTransferAccepted ( Kopete::Transfer* transfer, const TQString& fileName )
{
if ( (long)transfer->info().transferId () != transferId_ ) {
return;
}
transfer_ = transfer;
localFile_.setName( fileName );
if ( localFile_.exists() ) {
KGuiItem resumeButton( i18n ( "&Resume" ) );
KGuiItem overwriteButton( i18n ( "Over&write" ) );
switch ( KMessageBox::questionYesNoCancel( Kopete::UI::Global::mainWidget (),
i18n( "The file %1 already exists, do you want to resume or overwrite it?" ).arg( fileName ),
i18n( "File Exists: %1" ).arg( fileName ), resumeButton, overwriteButton ) )
{
// resume
case KMessageBox::Yes:
if ( localFile_.open( IO_WriteOnly | IO_Append ) ) {
dccSock_->offset = localFile_.size();
dccSock_->file_fd = localFile_.handle();
}
break;
// overwrite
case KMessageBox::No:
if ( localFile_.open( IO_ReadWrite ) ) {
dccSock_->offset = 0;
dccSock_->file_fd = localFile_.handle();
}
break;
// cancel
default:
closeDCC();
deleteLater();
return;
break;
}
if ( localFile_.handle() < 1 ) {
closeDCC();
deleteLater();
return;
}
}
else {
// overwrite by default
if ( localFile_.open( IO_ReadWrite ) == FALSE ) {
transfer->slotError ( KIO::ERR_COULD_NOT_WRITE, fileName );
closeDCC();
deleteLater ();
return;
}
dccSock_->offset = 0;
dccSock_->file_fd = localFile_.handle();
}
connect ( transfer, TQT_SIGNAL( result( KIO::Job * ) ), this, TQT_SLOT( slotTransferResult() ) );
// reenable notifiers
enableNotifiers( dccSock_->check );
}
void
GaduDCCTransaction::slotTransferResult()
{
if ( transfer_->error() == KIO::ERR_USER_CANCELED ) {
closeDCC();
deleteLater();
}
}
void
GaduDCCTransaction::slotTransferRefused ( const Kopete::FileTransferInfo& transfer )
{
if ( (long)transfer.transferId () != transferId_ )
return;
closeDCC();
deleteLater();
}
void
GaduDCCTransaction::askIncommingTransfer()
{
transferId_ = Kopete::TransferManager::transferManager()->askIncomingTransfer ( contact,
TQString( (const char*)dccSock_->file_info.filename ), dccSock_->file_info.size );
}
void
GaduDCCTransaction::watcher() {
gg_event* dccEvent;
GaduAccount* account;
disableNotifiers();
dccEvent = gg_dcc_watch_fd( dccSock_ );
if ( ! dccEvent ) {
// connection is bad
closeDCC();
return;
}
switch ( dccEvent->type ) {
case GG_EVENT_DCC_CLIENT_ACCEPT:
kdDebug(14100) << " GG_EVENT_DCC_CLIENT_ACCEPT " << endl;
// check dccsock->peer_uin, if unknown, oh well;
// is it for us ?
account = gaduDCC_->account( dccSock_->uin );
if ( !account ) {
kdDebug( 14100 ) << " this dcc transaction is for uin " << dccSock_->uin << ", which is not quite for me... closing" << endl;
// unknown 'to' ?, we're off
gg_free_event( dccEvent );
closeDCC();
deleteLater();
return;
}
if ( !peer ) {
contact = static_cast<GaduContact*> (account->contacts()[ TQString::number( dccSock_->peer_uin ) ]);
}
else {
contact = static_cast<GaduContact*> (account->contacts()[ TQString::number( peer ) ]);
}
if ( contact == NULL ) {
// refusing, contact on the list
kdDebug(14100) << " dcc connection from " << dccSock_->peer_uin << " refused, UIN not on the list " <<endl;
gg_free_event( dccEvent );
closeDCC();
// emit error
deleteLater();
return;
}
else {
// ask user to accept that transfer
kdDebug(14100) << " dcc accepted from " << dccSock_->peer_uin << endl;
}
break;
case GG_EVENT_DCC_CALLBACK:
kdDebug(14100) << "GG_DCC_EVENT_CALLBACK" << endl;
break;
case GG_EVENT_NONE:
kdDebug(14100) << " GG_EVENT_NONE" << endl;
// update gui with progress
if ( transfer_ ) {
transfer_->slotProcessed( dccSock_->offset );
}
break;
case GG_EVENT_DCC_NEED_FILE_ACK:
kdDebug(14100) << " GG_EVENT_DCC_NEED_FILE_ACK " << endl;
gg_free_event( dccEvent );
askIncommingTransfer();
return;
break;
case GG_EVENT_DCC_NEED_FILE_INFO:
if (gaduDCC_->requests.contains(dccSock_->peer_uin)) {
TQString filePath = gaduDCC_->requests[dccSock_->peer_uin];
kdDebug() << "Callback request found. Sending " << filePath << endl;
gaduDCC_->requests.remove(dccSock_->peer_uin);
gg_dcc_fill_file_info(dccSock_,filePath.ascii());
transfer_ = Kopete::TransferManager::transferManager()->addTransfer ( contact,
filePath, dccSock_->file_info.size, contact->metaContact()->displayName(), Kopete::FileTransferInfo::Outgoing );
} else {
gg_free_event( dccEvent );
closeDCC();
deleteLater();
return;
}
break;
case GG_EVENT_DCC_ERROR:
kdDebug(14100) << " GG_EVENT_DCC_ERROR :" << dccEvent->event.dcc_error << endl;
if ( transfer_ ) {
switch( dccEvent->event.dcc_error ) {
case GG_ERROR_DCC_REFUSED:
transfer_->slotError( KIO::ERR_SLAVE_DEFINED, i18n( "Connection to peer was refused; it possibly does not listen for incoming connections." ) );
break;
case GG_ERROR_DCC_EOF:
transfer_->slotError( KIO::ERR_SLAVE_DEFINED, i18n( "File transfer transaction was not agreed by peer." ) );
break;
case GG_ERROR_DCC_HANDSHAKE:
transfer_->slotError( KIO::ERR_SLAVE_DEFINED, i18n( "File-transfer handshake failure." ) );
break;
case GG_ERROR_DCC_FILE:
transfer_->slotError( KIO::ERR_SLAVE_DEFINED, i18n( "File transfer had problems with the file." ) );
break;
case GG_ERROR_DCC_NET:
transfer_->slotError( KIO::ERR_SLAVE_DEFINED, i18n( "There was network error during file transfer." ) );
break;
default:
transfer_->slotError( KIO::ERR_SLAVE_DEFINED, i18n( "Unknown File-Transfer error." ) );
break;
}
}
gg_free_event( dccEvent );
closeDCC();
deleteLater();
return;
case GG_EVENT_DCC_DONE:
if ( transfer_ ) {
transfer_->slotComplete();
}
closeDCC();
deleteLater();
return;
default:
kdDebug(14100) << "unknown/unhandled DCC EVENT: " << dccEvent->type << endl;
break;
}
if ( dccEvent ) {
gg_free_event( dccEvent );
}
enableNotifiers( dccSock_->check );
}
#include "gadudcctransaction.moc"
|