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
|
/*
Kopete Yahoo Protocol
Send a file
Copyright (c) 2006 André Duffeck <[email protected]>
*************************************************************************
* *
* 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 "sendfiletask.h"
#include "transfer.h"
#include "ymsgtransfer.h"
#include "yahootypes.h"
#include "client.h"
#include <tqstring.h>
#include <tqtimer.h>
#include <tdeapplication.h> // for random()
#include <kdebug.h>
#include <tdelocale.h>
#include <kstreamsocket.h>
#include <tdeio/global.h>
using namespace KNetwork;
SendFileTask::SendFileTask(Task* parent) : Task(parent)
{
kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
m_transmitted = 0;
m_socket = 0;
TQTime epoch(0, 0, 0);
}
SendFileTask::~SendFileTask()
{
m_socket->deleteLater();
m_socket = 0;
}
bool SendFileTask::forMe( const Transfer *transfer ) const
{
const YMSGTransfer *t = static_cast<const YMSGTransfer*>(transfer);
if(!t)
return false;
if((t->service() == Yahoo::ServiceFileTransfer7 ||
t->service() == Yahoo::ServiceFileTransfer7Accept) &&
t->firstParam(265).data() == m_yahooTransferId)
{
return true;
}
return false;
}
bool SendFileTask::take(Transfer* transfer)
{
if( !forMe( transfer ) )
return false;
YMSGTransfer *t = static_cast<YMSGTransfer*>(transfer);
kdDebug(YAHOO_RAW_DEBUG) << t->service() << endl;
if(t->service() == Yahoo::ServiceFileTransfer7)
parseFileTransfer(t);
else if(t->service() == Yahoo::ServiceFileTransfer7Accept)
parseTransferAccept(t);
return true;
}
void SendFileTask::parseFileTransfer( const Transfer *transfer )
{
kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
const YMSGTransfer *t = static_cast<const YMSGTransfer*>(transfer);
if(!t)
return;
if(t->firstParam(222).toInt() == 4)
{
emit declined();
}
else if(t->firstParam(222).toInt() == 3)
{
sendFileTransferInfo();
}
else
{
setError();
emit error(m_transferId, 0, i18n("Unknown error"));
}
}
void SendFileTask::onGo()
{
kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
m_file.setName( m_url.path() );
m_yahooTransferId = newYahooTransferId();
YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceFileTransfer7);
t->setId( client()->sessionID() );
t->setParam( 1, client()->userId().local8Bit() );
t->setParam( 5, m_target.local8Bit() );
t->setParam( 265, m_yahooTransferId.local8Bit() );
t->setParam( 222, 1 );
t->setParam( 266, 1 );
t->setParam( 302, 268 );
t->setParam( 300, 268 );
t->setParam( 27, m_url.fileName().local8Bit() );
t->setParam( 28, m_file.size());
t->setParam( 301, 268 );
t->setParam( 303, 268 );
send( t );
}
void SendFileTask::sendFileTransferInfo()
{
kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
KResolverResults results = KResolver::resolve("filetransfer.msg.yahoo.com", TQString::number(80));
if(results.count() > 0)
{
m_relayHost = results.first().address().toString();
m_relayHost = m_relayHost.left( m_relayHost.length() - 3 ); // Remove the :80 from the end
kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << m_relayHost << endl;
}
else
{
emit error(m_transferId, 0, i18n("Unable to connect to file transfer server"));
setError();
return;
}
YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceFileTransfer7Info);
t->setId( client()->sessionID() );
t->setParam( 1, client()->userId().local8Bit() );
t->setParam( 5, m_target.local8Bit() );
t->setParam( 265, m_yahooTransferId.local8Bit() );
t->setParam( 27, m_url.fileName().local8Bit() );
t->setParam( 249, 3 );
t->setParam( 250, m_relayHost.local8Bit() );
send( t );
}
void SendFileTask::parseTransferAccept(const Transfer *transfer)
{
kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
const YMSGTransfer *t = static_cast<const YMSGTransfer*>(transfer);
// Disconnected
if(t->status() == Yahoo::StatusDisconnected)
{
setError();
return;
}
m_token = KURL::encode_string(t->firstParam(251));
kdDebug(YAHOO_RAW_DEBUG) << "Token: " << m_token << endl;
m_socket = new KStreamSocket( m_relayHost, TQString::number(80) );
m_socket->setBlocking( true );
connect( m_socket, TQT_SIGNAL( connected( const KResolverEntry& ) ), this, TQT_SLOT( connectSucceeded() ) );
connect( m_socket, TQT_SIGNAL( gotError(int) ), this, TQT_SLOT( connectFailed(int) ) );
m_socket->connect();
}
void SendFileTask::connectFailed( int i )
{
TQString err = TDESocketBase::errorString(m_socket->error());
kdDebug(YAHOO_RAW_DEBUG) << i << ": " << err << endl;
emit error( m_transferId, i, err );
setError();
}
void SendFileTask::connectSucceeded()
{
kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
TQByteArray buffer;
TQDataStream stream( buffer, IO_WriteOnly );
if ( m_file.open(IO_ReadOnly ) )
{
kdDebug(YAHOO_RAW_DEBUG) << k_funcinfo << "File successfully opened. Reading..." << endl;
}
else
{
kdDebug(YAHOO_RAW_DEBUG) << k_funcinfo << "Error opening file: " << TQString(m_file.errorString()).ascii() << endl;
client()->notifyError( i18n( "An error occurred while sending the file." ), m_file.errorString(), Client::Error );
setError();
return;
}
kdDebug(YAHOO_RAW_DEBUG) << k_funcinfo << "Sizes: File (" << m_url << "): " << m_file.size() << endl;
TQString header =
"POST /relay?token=" + m_token +
"&sender=" + client()->userId() +
"&recver=" + m_target + " HTTP/1.1\r\n"
"User-Agent: Mozilla/5.0\r\n"
"Cache-Control: no-cache\r\n"
"Cookie: T=" + client()->tCookie() + "; Y=" + client()->yCookie() + "\r\n"
"Host: " + m_relayHost + "\r\n"
"Content-Length: " + TQString::number(m_file.size()) + "\r\n"
"Connection: Close\r\n\r\n";
kdDebug() << header << endl;
stream.writeRawBytes( header.local8Bit(), header.length() );
if( !m_socket->writeBlock( buffer, buffer.size() ) )
{
emit error( m_transferId, m_socket->error(), m_socket->TDESocketBase::errorString() );
m_socket->close();
}
else
{
connect( m_socket, TQT_SIGNAL(readyWrite()), this, TQT_SLOT(transmitData()) );
m_socket->enableWrite( true );
}
}
void SendFileTask::transmitData()
{
kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
int read = 0;
int written = 0;
char buf[1024];
m_socket->enableWrite( false );
read = m_file.readBlock( buf, 1024 );
written = m_socket->writeBlock( buf, read );
kdDebug(YAHOO_RAW_DEBUG) << k_funcinfo << "read:" << read << " written: " << written << endl;
m_transmitted += read;
emit bytesProcessed( m_transferId, m_transmitted );
if( written != read )
{
kdDebug(YAHOO_RAW_DEBUG) << k_funcinfo << "Upload Failed!" << endl;
emit error( m_transferId, m_socket->error(), m_socket->TDESocketBase::errorString() );
setError();
return;
}
if( m_transmitted == m_file.size() )
{
kdDebug(YAHOO_RAW_DEBUG) << k_funcinfo << "Upload Successful: " << m_transmitted << endl;
emit complete( m_transferId );
setSuccess();
m_socket->close();
}
else
{
m_socket->enableWrite( true );
}
}
void SendFileTask::setTarget( const TQString &to )
{
m_target = to;
}
void SendFileTask::setMessage( const TQString &msg )
{
m_msg = msg;
}
void SendFileTask::setFileUrl( KURL url )
{
m_url = url;
}
void SendFileTask::setTransferId( unsigned int transferId )
{
m_transferId = transferId;
}
void SendFileTask::canceled( unsigned int id )
{
if( m_transferId != id )
return;
if( m_socket )
m_socket->close();
setError();
}
TQString SendFileTask::newYahooTransferId()
{
// Adapted from libpurple/protocols/yahoo/yahoo_filexfer.c yahoo_xfer_new_xfer_id()
TQString newId;
for(int i = 0; i < 22; i++)
{
char j = TDEApplication::random() % 61;
if(j < 26)
newId += j + 'a';
else if(j < 52)
newId += j - 26 + 'A';
else
newId += j - 52 + '0';
}
newId += "$$";
kdDebug() << "New Yahoo Transfer Id: " << newId << endl;
return newId;
}
#include "sendfiletask.moc"
|