summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/yahoo/libkyahoo/yahoochattask.cpp
blob: 1c9528ffd95454d88883596879fa51cb2b6fd19f (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
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
/*
    Kopete Yahoo Protocol
    yahoochattask.cpp - Handle Yahoo Chat

    Copyright (c) 2006 AndrĂ© Duffeck <[email protected]>
    Kopete (c) 2002-2006 by the Kopete developers <[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 "yahoochattask.h"
#include "transfer.h"
#include "ymsgtransfer.h"
#include "yahootypes.h"
#include "client.h"
#include <kdebug.h>
#include <klocale.h>
#include <kio/global.h>
#include <kio/job.h>
#include <kio/jobclasses.h>
#include <tqdom.h>

YahooChatTask::YahooChatTask(Task* parent) : Task(parent)
{
	kdDebug(YAHOO_RAW_DEBUG) ;
	m_loggedIn = false;
}

YahooChatTask::~YahooChatTask()
{
}

bool YahooChatTask::take( Transfer* transfer )
{
	if ( !forMe( transfer ) )
		return false;

	YMSGTransfer *t = 0L;
	t = dynamic_cast<YMSGTransfer*>(transfer);
	if (!t)
		return false;

	if( t->service() == Yahoo::ServiceChatOnline )
		parseLoginResponse( t );
	else if( t->service() == Yahoo::ServiceComment )
		parseChatMessage( t );
	else if( t->service() == Yahoo::ServiceChatJoin )
		parseJoin( t );
	else if( t->service() == Yahoo::ServiceChatExit )
		parseChatExit( t );
	else if( t->service() == Yahoo::ServiceChatLogout )
		parseLogout( t );
	return true;
}

bool YahooChatTask::forMe( const Transfer* transfer ) const
{
	const YMSGTransfer *t = 0L;
	t = dynamic_cast<const YMSGTransfer*>(transfer);
	if (!t)
		return false;

	if ( t->service() == Yahoo::ServiceChatOnline ||
		t->service() == Yahoo::ServiceChatGoto ||
		t->service() == Yahoo::ServiceChatJoin ||
		t->service() == Yahoo::ServiceChatleave ||
		t->service() == Yahoo::ServiceChatExit ||
		t->service() == Yahoo::ServiceChatLogout ||
		t->service() == Yahoo::ServiceChatPing ||
		t->service() == Yahoo::ServiceChatLogon ||
		t->service() == Yahoo::ServiceChatLogoff ||
		t->service() == Yahoo::ServiceComment )
		return true;
	else
		return false;
}

void YahooChatTask::onGo()
{
}

void YahooChatTask::getYahooChatCategories()
{
	kdDebug(YAHOO_RAW_DEBUG) ;
	KIO::TransferJob *transfer;

	transfer = KIO::get( KURL("http://insider.msg.yahoo.com/ycontent/?chatcat=0"), false, false );
	transfer->addMetaData( "UserAgent", "Mozilla/4.0 (compatible; MSIE 5.5)");
	transfer->addMetaData( "no-cache", "true" );
	transfer->addMetaData( "cookies", "manual" );
	transfer->addMetaData("setcookies", TQString("Cookie: %1; %2; %3").arg(client()->tCookie(), client()->yCookie()) );


	connect( transfer, TQT_SIGNAL( result( KIO::Job* ) ), this, TQT_SLOT( slotCategoriesComplete( KIO::Job* ) ) );
	connect( transfer, TQT_SIGNAL( data( KIO::Job*, const TQByteArray& ) ), this, TQT_SLOT( slotData( KIO::Job*, const TQByteArray& ) ) );
}

void YahooChatTask::getYahooChatRooms( const Yahoo::ChatCategory &category )
{
	kdDebug(YAHOO_RAW_DEBUG) << "Category Id: " << category.id << endl;
	KIO::TransferJob *transfer;

	transfer = KIO::get( KURL(TQString("http://insider.msg.yahoo.com/ycontent/?chatroom_%1=0").arg( category.id )), false, false );
	transfer->addMetaData( "UserAgent", "Mozilla/4.0 (compatible; MSIE 5.5)");
	transfer->addMetaData( "no-cache", "true" );
	transfer->addMetaData( "cookies", "manual" );
	transfer->addMetaData("setcookies", TQString("Cookie: %1; %2; %3").arg(client()->tCookie(), client()->yCookie()) );


	connect( transfer, TQT_SIGNAL( result( KIO::Job* ) ), this, TQT_SLOT( slotChatRoomsComplete( KIO::Job* ) ) );
	connect( transfer, TQT_SIGNAL( data( KIO::Job*, const TQByteArray& ) ), this, TQT_SLOT( slotData( KIO::Job*, const TQByteArray& ) ) );

	m_jobs[ transfer ].category = category;
}

void YahooChatTask::slotData( KIO::Job *job, const TQByteArray& data)
{
	kdDebug(YAHOO_RAW_DEBUG) ;
	// FIXME (same)
	//m_jobs[job].data.append( data );
}

void YahooChatTask::slotCategoriesComplete( KIO::Job *job )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

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

	if ( job->error () || transfer->isErrorPage () )
	{
		kdDebug(YAHOO_RAW_DEBUG) << "An error occurred while downloading the chat categories list." << endl;
	}
	else
	{
		TQDomDocument doc;
		doc.setContent( m_jobs[ transfer ].data );
		emit gotYahooChatCategories( doc );
	}

	m_jobs.remove( transfer );
}

void YahooChatTask::slotChatRoomsComplete( KIO::Job *job )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

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

	if ( job->error () || transfer->isErrorPage () )
	{
		kdDebug(YAHOO_RAW_DEBUG) << "An error occurred while downloading the chat categories list." << endl;
	}
	else
	{
		TQDomDocument doc;
		doc.setContent( m_jobs[ transfer ].data );
// 		kdDebug(YAHOO_RAW_DEBUG) << doc.toString();
		emit gotYahooChatRooms( m_jobs[ transfer ].category, doc );
	}

	m_jobs.remove( transfer );
}

void YahooChatTask::joinRoom( const Yahoo::ChatRoom &room )
{
	kdDebug(YAHOO_RAW_DEBUG) << "Joining room " << room.name << " (" << room.id << ")..." << endl;
	if( !m_loggedIn )
	{
		m_pendingJoins.append( room );
		login();
		return;
	}

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceChatJoin);
	t->setId( client()->sessionID() );
	t->setParam( 1, client()->userId().local8Bit() );
	t->setParam( 104, room.name.local8Bit() );
	t->setParam( 129, room.id );
	t->setParam( 62, 2 );

	send( t );
}

void YahooChatTask::sendYahooChatMessage( const TQString &msg, const TQString &handle )
{
	kdDebug(YAHOO_RAW_DEBUG) ;
	if( !m_loggedIn )
	{
		kdDebug(YAHOO_RAW_DEBUG) << "Error: trying to send, but not logged in." << endl;
		return;
	}

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceComment);
	t->setId( client()->sessionID() );
	t->setParam( 1, client()->userId().local8Bit() );
	t->setParam( 104, handle.local8Bit() );
	t->setParam( 117, msg.local8Bit() );
	t->setParam( 124, 1 );

	send( t );
}

void YahooChatTask::login()
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceChatOnline);
	t->setId( client()->sessionID() );
	t->setParam( 1, client()->userId().local8Bit() );
	t->setParam( 135, TQString("ym%1").arg(YMSG_PROGRAM_VERSION_STRING).local8Bit() );

	send( t );
}

void YahooChatTask::logout()
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceChatLogout);
	t->setId( client()->sessionID() );
	t->setParam( 1, client()->userId().local8Bit() );

	send( t );
}

void YahooChatTask::parseLoginResponse( YMSGTransfer *t )
{
	if( !TQString(t->firstParam( 1 )).startsWith( client()->userId().local8Bit() ) )
		return;
	m_loggedIn = true;

	TQValueList< Yahoo::ChatRoom >::iterator it;
	for ( it = m_pendingJoins.begin(); it != m_pendingJoins.end(); ++it )
	//for( int i = 0; i < m_pendingJoins.size(); ++i )
	{
		// FIXME (same)
		// Yahoo::ChatRoom entry = m_pendingJoins.at( i );
		Yahoo::ChatRoom entry = *it;
		joinRoom( entry );

		// FIXME (same)
		//m_pendingJoins.removeAt( i );
		m_pendingJoins.erase(it);
	}
}

void YahooChatTask::parseJoin( YMSGTransfer *t )
{
	int room;
	int category;
	TQString handle;
	TQString comment;
	bool suppressJoinNotification = false;
	TQString error;

	room = t->firstParam( 129 ).toInt();
	category = t->firstParam( 128 ).toInt();
	handle = t->firstParam( 104 );
	comment = t->firstParam( 105 );
	error = t->firstParam( 114 );

	if( error.startsWith( "-35" ) ) {
		client()->notifyError( i18n("Could not join chat"),
				i18n("The room is full. Please choose another one."), Client::Error );
		return;
	} else if( error.startsWith( "-15" ) ) {
		client()->notifyError( i18n("Could not join chat"),
				i18n("Invalid user."), Client::Error );
		return;
	} else if( !error.isEmpty() ) {
		client()->notifyError( i18n("Could not join chat"),
				i18n("An unknown error occurred while joining the chat room."), Client::Error );
		return;
	}

        // Yahoo sends a captcha requests before we can join the room
        if( room == 0 && category == 0 && !comment.isEmpty() )
        {
        	kdDebug(YAHOO_RAW_DEBUG) << "Showing captcha request" << endl;
		emit chatRoomJoined( room, category, "", handle );
		emit chatMessageReceived( "Yahoo", comment, handle );
        }

	if( room > 0 && category > 0 )
	{
		// We have just joined this room. Suppress join notifications for the people
		// that are already in here.
		suppressJoinNotification = true;
		emit chatRoomJoined( room, category, comment, handle );
	}

	TQString nick;
	for( int i = 0; i < t->paramCount( 109 ); ++i )
	{
		nick = t->nthParam( 109 , i );
		emit chatBuddyHasJoined( nick, handle, suppressJoinNotification );
	}
}

void YahooChatTask::parseChatMessage( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	TQString handle;
	TQString msg;
	TQString nick;

	handle = t->firstParam( 104 );
	for( int i = 0; i < t->paramCount( 109 ); ++i )
	{
		nick = t->nthParam( 109, i );
		msg = t->nthParamSeparated( 117, i, 109 );
		emit chatMessageReceived( nick, msg, handle );
	}
}

void YahooChatTask::parseChatExit( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	TQString handle;
	TQString nick;

	handle = t->firstParam( 104 );
	for( int i = 0; i < t->paramCount( 109 ); ++i )
	{
		nick = t->nthParam( 109, i );
		emit chatBuddyHasLeft( nick, handle );
	}
}

void YahooChatTask::parseLogout( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	TQString nick = t->firstParam( 1 );
	if( nick == client()->userId() )
		m_loggedIn = false;
}

#include "yahoochattask.moc"