summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/yahoo/libkyahoo/sendpicturetask.cpp
blob: 84edecaa00668249de59d3c6663bbe8bcc802caa (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
    Kopete Yahoo Protocol
    sendpicturetask.cpp - Send our picture or information about it

    Copyright (c) 2005 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 "sendpicturetask.h"
#include "transfer.h"
#include "ymsgtransfer.h"
#include "yahootypes.h"
#include "client.h"
#include <qstring.h>
#include <qfile.h>
#include <qdatastream.h>
#include <kio/global.h>
#include <kio/job.h>
#include <kio/jobclasses.h>
#include <kbufferedsocket.h>
#include <kdebug.h>
#include <klocale.h>

using namespace KNetwork;

SendPictureTask::SendPictureTask(Task* parent) : Task(parent)
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
	m_socket = 0;
}

SendPictureTask::~SendPictureTask()
{
	delete m_socket;
}

void SendPictureTask::onGo()
{
	switch( m_type )
	{
		case UploadPicture:
			initiateUpload();
		break;
		case SendChecksum:
			sendChecksum();
		break;
		case SendInformation:
			sendInformation();
		break;
		case SendStatus:
			sendStatus();
		break;
	}
}

void SendPictureTask::initiateUpload()
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
	m_socket = new KBufferedSocket( "filetransfer.msg.yahoo.com", QString::number(80) );
	connect( m_socket, SIGNAL( connected( const KResolverEntry& ) ), this, SLOT( connectSucceeded() ) );
	connect( m_socket, SIGNAL( gotError(int) ), this, SLOT( connectFailed(int) ) );
	connect( m_socket, SIGNAL( readyRead() ), this, SLOT( readResult() ) );

	m_socket->connect();
}

void SendPictureTask::connectFailed( int i)
{
	kdDebug(YAHOO_RAW_DEBUG) << i << ": " << static_cast<const KBufferedSocket*>( sender() )->errorString() << endl;

	client()->notifyError(i18n("The picture was not successfully uploaded"), QString("%1 - %2").arg(i).arg(static_cast<const KBufferedSocket*>( sender() )->errorString()), Client::Error );
	setError();
}

void SendPictureTask::connectSucceeded()
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;
	YMSGTransfer t(Yahoo::ServicePictureUpload);

	QFile file( m_path );

	t.setId( client()->sessionID() );
	t.setParam( 1, client()->userId().local8Bit());
	t.setParam( 38, 604800);
	t.setParam( 0, client()->userId().local8Bit());
	t.setParam( 28, file.size() );
	t.setParam( 27, m_fileName.local8Bit() );
	t.setParam( 14, "" );
	QByteArray buffer;
	QByteArray paket;
	QDataStream stream( buffer, IO_WriteOnly );

	if ( file.open( IO_ReadOnly ) )
	{
		kdDebug(YAHOO_RAW_DEBUG) << "File successfully opened. Reading..." << endl;
	}
	else
	{
		kdDebug(YAHOO_RAW_DEBUG) << "Error opening file: " << file.errorString() << endl;
		client()->notifyError(i18n("Error opening file: %1").arg(m_path), file.errorString(), Client::Error );
		return;
	}

	paket = t.serialize();
	kdDebug(YAHOO_RAW_DEBUG) << "Sizes: File (" << m_path << "): " << file.size() << " - paket: " << paket.size() << endl;
	QString header = QString::fromLatin1("POST /notifyft HTTP/1.1\r\n"
			"Cookie: Y=%1; T=%2; C=%3 ;\r\n"
			"User-Agent: Mozilla/4.0 (compatible; MSIE 5.5)\r\n"
			"Host: filetransfer.msg.yahoo.com\r\n"
			"Content-length: %4\r\n"
			"Cache-Control: no-cache\r\n\r\n").arg(client()->yCookie()).arg(client()->tCookie()).arg(client()->cCookie()).arg(file.size()+4+paket.size());
	stream.writeRawBytes( header.local8Bit(), header.length() );
	stream.writeRawBytes( paket.data(), paket.size() );
	stream << (Q_INT8)0x32 << (Q_INT8)0x39 << (Q_INT8)0xc0 << (Q_INT8)0x80;
	stream.writeRawBytes( file.readAll(), file.size() );

	kdDebug(YAHOO_RAW_DEBUG) << "Buffersize: " << buffer.size() << endl;
	if( m_socket->writeBlock( buffer, buffer.size() ) )
	{
		kdDebug(YAHOO_RAW_DEBUG) << "Upload Successful!" << endl;
		m_socket->enableRead( true );
// 		setSuccess();
	}
	else
	{
		kdDebug(YAHOO_RAW_DEBUG) << "Upload Failed!" << endl;
		setError();
	}
}

void SendPictureTask::readResult()
{
	kdDebug(YAHOO_RAW_DEBUG) << m_socket->bytesAvailable() << endl;

	// FIXME: to check
// 	m_socket->enableRead( false );
// 	QByteArray buf;
// 	buf.resize( m_socket->bytesAvailable() );
// 	m_socket->read( buf.data(), m_socket->bytesAvailable() );
//
// 	if( buf.indexOf( "error", 0 ) >= 0 )
// 	{
// 		kdDebug(YAHOO_RAW_DEBUG) << "Picture upload failed" << endl;
// 		setError();
// 	}
// 	else
// 	{
// 		kdDebug(YAHOO_RAW_DEBUG) << "Picture upload acknowledged." << endl;
// 		setSuccess();
// 	}

	// from original file
	QByteArray ar( m_socket->bytesAvailable() );
        m_socket->readBlock ( ar.data (), ar.size () );
        QString buf( ar );

        m_socket->close();
        if( buf.find( "error", 0, false ) >= 0 )
        {
                kdDebug(YAHOO_RAW_DEBUG) << k_funcinfo << "Picture upload failed" << endl;
                setSuccess( false );
        }
        else
        {
                kdDebug(YAHOO_RAW_DEBUG) << k_funcinfo << "Picture upload acknowledged." << endl;
                setSuccess( true );
        }
}

void SendPictureTask::sendChecksum()
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServicePictureChecksum);
	t->setId( client()->sessionID() );
	t->setParam(1, client()->userId().local8Bit());
	if( !m_target.isEmpty() )
		t->setParam( 5, m_target.local8Bit() );
	t->setParam(192, m_checksum);
	t->setParam(212, 1);
	send( t );

	setSuccess();
}

void SendPictureTask::sendInformation()
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServicePicture);
	t->setId( client()->sessionID() );
	t->setParam(1, client()->userId().local8Bit());
	t->setParam(13, 2 );
	t->setParam(5, m_target.local8Bit() );
	t->setParam(20, m_url.local8Bit() );
	t->setParam(192, m_checksum);

	send( t );

	setSuccess();
}

void SendPictureTask::sendStatus()
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServicePictureStatus);
	t->setId( client()->sessionID() );
	t->setParam(3, client()->userId().local8Bit());
	t->setParam(213, m_status );

	send( t );

	setSuccess();
}

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

void SendPictureTask::setTarget( const QString &to )
{
	m_target = to;
}

void SendPictureTask::setFilename( const QString &filename )
{
	m_fileName = filename;
}

void SendPictureTask::setFilesize( int filesize )
{
	m_fileSize = filesize;
}

void SendPictureTask::setPath( const QString &path )
{
	m_path = path;
}

void SendPictureTask::setChecksum( int checksum )
{
	m_checksum = checksum;
}

void SendPictureTask::setStatus( int status )
{
	m_status = status;
}

void SendPictureTask::setUrl( const QString &url )
{
	m_url = url;
}

#include "sendpicturetask.moc"