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
|
/*
Kopete Yahoo Protocol
yahoochattask.cpp - Handle Yahoo Chat
Copyright (c) 2006 André Duffeck <duffeck@kde.org>
Kopete (c) 2002-2006 by the Kopete developers <kopete-devel@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 "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 <QDomDocument>
using namespace KYahoo;
YahooChatTask::YahooChatTask(Task* parent) : Task(parent)
{
kDebug(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()
{
kDebug(YAHOO_RAW_DEBUG) ;
KIO::TransferJob *transfer;
transfer = KIO::get( KUrl("http://insider.msg.yahoo.com/ycontent/?chatcat=0"), KIO::NoReload, KIO::HideProgressInfo );
transfer->addMetaData( "UserAgent", "Mozilla/4.0 (compatible; MSIE 5.5)");
transfer->addMetaData( "no-cache", "true" );
transfer->addMetaData( "cookies", "manual" );
transfer->addMetaData("setcookies", QString("Cookie: %1; %2; %3").arg(client()->tCookie(), client()->yCookie()) );
connect( transfer, SIGNAL(result(KJob*)), this, SLOT(slotCategoriesComplete(KJob*)) );
connect( transfer, SIGNAL(data(KIO::Job*,QByteArray)), this, SLOT(slotData(KIO::Job*,QByteArray)) );
}
void YahooChatTask::getYahooChatRooms( const Yahoo::ChatCategory &category )
{
kDebug(YAHOO_RAW_DEBUG) << "Category Id: " << category.id;
KIO::TransferJob *transfer;
transfer = KIO::get( KUrl(QString("http://insider.msg.yahoo.com/ycontent/?chatroom_%1=0").arg( category.id )), KIO::NoReload, KIO::HideProgressInfo );
transfer->addMetaData( "UserAgent", "Mozilla/4.0 (compatible; MSIE 5.5)");
transfer->addMetaData( "no-cache", "true" );
transfer->addMetaData( "cookies", "manual" );
transfer->addMetaData("setcookies", QString("Cookie: %1; %2; %3").arg(client()->tCookie(), client()->yCookie()) );
connect( transfer, SIGNAL(result(KJob*)), this, SLOT(slotChatRoomsComplete(KJob*)) );
connect( transfer, SIGNAL(data(KIO::Job*,QByteArray)), this, SLOT(slotData(KIO::Job*,QByteArray)) );
m_jobs[ transfer ].category = category;
}
void YahooChatTask::slotData( KIO::Job *job, const QByteArray& data)
{
kDebug(YAHOO_RAW_DEBUG) ;
m_jobs[job].data.append( data );
}
void YahooChatTask::slotCategoriesComplete( KJob *job )
{
kDebug(YAHOO_RAW_DEBUG) ;
KIO::TransferJob *transfer = static_cast< KIO::TransferJob * >(job);
if ( job->error () || transfer->isErrorPage () )
{
kDebug(YAHOO_RAW_DEBUG) << "An error occurred while downloading the chat categories list.";
}
else
{
QDomDocument doc;
doc.setContent( m_jobs[ transfer ].data );
emit gotYahooChatCategories( doc );
}
m_jobs.remove( transfer );
}
void YahooChatTask::slotChatRoomsComplete( KJob *job )
{
kDebug(YAHOO_RAW_DEBUG) ;
KIO::TransferJob *transfer = static_cast< KIO::TransferJob * >(job);
if ( job->error () || transfer->isErrorPage () )
{
kDebug(YAHOO_RAW_DEBUG) << "An error occurred while downloading the chat categories list.";
}
else
{
QDomDocument doc;
doc.setContent( m_jobs[ transfer ].data );
// kDebug(YAHOO_RAW_DEBUG) << doc.toString();
emit gotYahooChatRooms( m_jobs[ transfer ].category, doc );
}
m_jobs.remove( transfer );
}
void YahooChatTask::joinRoom( const Yahoo::ChatRoom &room )
{
kDebug(YAHOO_RAW_DEBUG) << "Joining room " << room.name << " (" << room.id << ")...";
if( !m_loggedIn )
{
m_pendingJoins.append( room );
login();
return;
}
YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceChatJoin);
t->setId( client()->sessionID() );
t->setParam( 1, client()->userId().toLocal8Bit() );
t->setParam( 104, room.name.toLocal8Bit() );
t->setParam( 129, room.id );
t->setParam( 62, 2 );
send( t );
}
void YahooChatTask::sendYahooChatMessage( const QString &msg, const QString &handle )
{
kDebug(YAHOO_RAW_DEBUG) ;
if( !m_loggedIn )
{
kDebug(YAHOO_RAW_DEBUG) << "Error: trying to send, but not logged in.";
return;
}
YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceComment);
t->setId( client()->sessionID() );
t->setParam( 1, client()->userId().toLocal8Bit() );
t->setParam( 104, handle.toLocal8Bit() );
t->setParam( 117, msg.toLocal8Bit() );
t->setParam( 124, 1 );
send( t );
}
void YahooChatTask::login()
{
kDebug(YAHOO_RAW_DEBUG) ;
YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceChatOnline);
t->setId( client()->sessionID() );
t->setParam( 1, client()->userId().toLocal8Bit() );
t->setParam( 135, QString("ym%1").arg(YMSG_PROGRAM_VERSION_STRING).toLocal8Bit() );
send( t );
}
void YahooChatTask::logout()
{
kDebug(YAHOO_RAW_DEBUG) ;
YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceChatLogout);
t->setId( client()->sessionID() );
t->setParam( 1, client()->userId().toLocal8Bit() );
send( t );
}
void YahooChatTask::parseLoginResponse( YMSGTransfer *t )
{
if( !t->firstParam( 1 ).startsWith( client()->userId().toLocal8Bit() ) )
return;
m_loggedIn = true;
for( int i = 0; i < m_pendingJoins.size(); ++i )
{
Yahoo::ChatRoom entry = m_pendingJoins.at( i );
joinRoom( entry );
m_pendingJoins.removeAt( i );
}
}
void YahooChatTask::parseJoin( YMSGTransfer *t )
{
int room;
int category;
QString handle;
QString comment;
bool suppressJoinNotification = false;
QString 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() )
{
kDebug(YAHOO_RAW_DEBUG) << "Showing captcha request";
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 );
}
QString 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 )
{
kDebug(YAHOO_RAW_DEBUG) ;
QString handle;
QString msg;
QString 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 )
{
kDebug(YAHOO_RAW_DEBUG) ;
QString handle;
QString 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 )
{
kDebug(YAHOO_RAW_DEBUG) ;
QString nick = t->firstParam( 1 );
if( nick == client()->userId() )
m_loggedIn = false;
}
#include "yahoochattask.moc"
|