summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/yahoo/libkyahoo/receivefiletask.cpp
blob: 532e21bf2e97b083ba925fc887604ce3a3935222 (plain)
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
/*
    Kopete Yahoo Protocol
    Receive a file

    Copyright (c) 2006 André Duffeck <duffeck@kde.org>

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include "receivefiletask.h"
#include "transfer.h"
#include "ymsgtransfer.h"
#include "yahootypes.h"
#include "client.h"

#include <tqtimer.h>
#include <tqfile.h>
#include <kdebug.h>
#include <klocale.h>
#include <tdeio/global.h>
#include <tdeio/job.h>
#include <tdeio/jobclasses.h>

ReceiveFileTask::ReceiveFileTask(Task* parent) : Task(parent)
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
	m_transmitted = 0;
	m_file = 0;
	m_transferJob = 0;
}

ReceiveFileTask::~ReceiveFileTask()
{
	delete m_file;
	m_file = 0;
}

void ReceiveFileTask::onGo()
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceFileTransfer7);
	switch( m_type )
	{
	case FileTransferAccept:
		m_file = new TQFile( m_localUrl.path() );
		if( !m_file->open( IO_WriteOnly ) )
		{
			emit error( m_transferId, TDEIO::ERR_CANNOT_OPEN_FOR_WRITING, i18n("Could not open file for writing.") );
			setError();
			delete t;
			return;
		}
		m_transferJob = TDEIO::get( m_remoteUrl, false, false );
		TQObject::connect( m_transferJob, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( slotComplete( TDEIO::Job* ) ) );
		TQObject::connect( m_transferJob, TQT_SIGNAL( data( TDEIO::Job*, const TQByteArray & ) ), this, TQT_SLOT( slotData( TDEIO::Job*, const TQByteArray & ) ) );
		delete t;
		break;
	case FileTransfer7Accept:
		t->setId( client()->sessionID() );
		t->setParam( 1, client()->userId().local8Bit() );
		t->setParam( 5, m_userId.local8Bit() );
		t->setParam( 265, m_remoteUrl.url().local8Bit() );
		t->setParam( 222, 3 );

		send( t );
		break;
	case FileTransfer7Reject:
		t->setId( client()->sessionID() );
		t->setParam( 1, client()->userId().local8Bit() );
		t->setParam( 5, m_userId.local8Bit() );
		t->setParam( 265, m_remoteUrl.url().local8Bit() );
		t->setParam( 222, 4 );

		send( t );
		break;
	default:
		delete t;
	}
}

bool ReceiveFileTask::take( Transfer* transfer )
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	if ( !forMe( transfer ) )
		return false;

	YMSGTransfer *t = static_cast<YMSGTransfer*>(transfer);

	parseFileTransfer7Info( t );

	return true;
}

bool ReceiveFileTask::forMe( const Transfer *transfer ) const
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
	const YMSGTransfer *t = 0L;
	t = dynamic_cast<const YMSGTransfer*>(transfer);
	if (!t)
		return false;


	if( t->service() == Yahoo::ServiceFileTransfer7Info )
	{
		// Only take this transfer if we are the corresponding task (in case of simultaneous file transfers)
		if( t->firstParam( 265 ) == m_remoteUrl.url().local8Bit() )
			return true;
		else
			return false;
	}
	else
		return false;
}

void ReceiveFileTask::slotData( TDEIO::Job *job, const TQByteArray& data )
{
	Q_UNUSED( job );
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	m_transmitted += data.size();
	emit bytesProcessed( m_transferId, m_transmitted );
	m_file->writeBlock( data.data() , data.size() );
}

void ReceiveFileTask::slotComplete( TDEIO::Job *job )
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	TDEIO::TransferJob *transfer = static_cast< TDEIO::TransferJob * >(job);

	if( m_file )
		m_file->close();
	if ( job->error () || transfer->isErrorPage () )
	{
		emit error( m_transferId, TDEIO::ERR_ABORTED, i18n("An error occurred while downloading the file.") );
		setError();
	}
	else
	{
		emit complete( m_transferId );
		setSuccess();
	}
}

void ReceiveFileTask::parseFileTransfer7Info( YMSGTransfer *transfer )
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	if( transfer->firstParam( 249 ).toInt() == 1 )
	{
		// Reject P2P Transfer offer
		YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceFileTransfer7Accept);
		t->setId( client()->sessionID() );
		t->setParam( 1, client()->userId().local8Bit() );
		t->setParam( 5, transfer->firstParam( 4 ) );
		t->setParam( 265, transfer->firstParam( 265 ) );
		t->setParam( 66, -3 );

		send( t );
	}
	else if( transfer->firstParam( 249 ).toInt() == 3 )
	{
		m_file = new TQFile( m_localUrl.path() );
		if( !m_file->open( IO_WriteOnly ) )
		{
			emit error( m_transferId, TDEIO::ERR_CANNOT_OPEN_FOR_WRITING, i18n("Could not open file for writing.") );
			setError();
			return;
		}

		YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceFileTransfer7Accept);
		t->setId( client()->sessionID() );
		t->setParam( 1, client()->userId().local8Bit() );
		t->setParam( 5, transfer->firstParam( 4 ) );
		t->setParam( 265, transfer->firstParam( 265 ) );
		t->setParam( 27, transfer->firstParam( 27 ) );
		t->setParam( 249, 3 );			// Use Reflection server
		t->setParam( 251, transfer->firstParam( 251 ) );

		send( t );
		// The server expects a HTTP HEAD command prior to the GET
		m_mimetypeJob = TDEIO::mimetype(TQString::fromLatin1("http://%1/relay?token=%2&sender=%3&recver=%4")
				.arg( TQString(transfer->firstParam( 250 )) ).arg( TQString(transfer->firstParam( 251 )) ).arg(m_userId).arg(client()->userId()), false);
		m_mimetypeJob->addMetaData("cookies", "manual");
		m_mimetypeJob->addMetaData("setcookies", TQString::fromLatin1("Cookie: T=%1; path=/; domain=.yahoo.com; Y=%2; C=%3;")
				.arg(client()->tCookie()).arg(client()->yCookie()).arg(client()->cCookie()) );


		m_transferJob = TDEIO::get( TQString::fromLatin1("http://%1/relay?token=%2&sender=%3&recver=%4")
				.arg( TQString(transfer->firstParam( 250 )) ).arg( TQString(transfer->firstParam( 251 )) ).arg(m_userId).arg(client()->userId()), false, false );
		TQObject::connect( m_transferJob, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( slotComplete( TDEIO::Job* ) ) );
		TQObject::connect( m_transferJob, TQT_SIGNAL( data( TDEIO::Job*, const TQByteArray & ) ), this, TQT_SLOT( slotData( TDEIO::Job*, const TQByteArray & ) ) );
		m_transferJob->addMetaData("cookies", "manual");
 		m_transferJob->addMetaData("setcookies", TQString::fromLatin1("Cookie: T=%1; path=/; domain=.yahoo.com; Y=%2;  path=/; domain=.yahoo.com;")
 				.arg(client()->tCookie()).arg(client()->yCookie()) );
	}
}

void ReceiveFileTask::setRemoteUrl( KURL url )
{
	m_remoteUrl = url;
}

void ReceiveFileTask::setLocalUrl( KURL url )
{
	m_localUrl = url;
}

void ReceiveFileTask::setTransferId( unsigned int transferId )
{
	m_transferId = transferId;
}

void ReceiveFileTask::setType( Type type )
{
	m_type = type;
}

void ReceiveFileTask::setUserId( const TQString &userId )
{
	m_userId = userId;
}

void ReceiveFileTask::canceled( unsigned int id )
{
	if( m_transferId != id )
		return;

	if( m_transferJob )
		m_transferJob->kill();

	setError();
}

#include "receivefiletask.moc"