summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/yahoo/libkyahoo/statusnotifiertask.cpp
blob: ddb249bdc9585fa4cffa877f4213245c60a589a7 (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
/*
    Kopete Yahoo Protocol
    Notifies about status changes of buddies

    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 "statusnotifiertask.h"
#include "transfer.h"
#include "ymsgtransfer.h"
#include "yahootypes.h"
#include "client.h"
#include <tqstring.h>
#include <tqstringlist.h>
#include <kdebug.h>
#include <klocale.h>

StatusNotifierTask::StatusNotifierTask(Task* parent) : Task(parent)
{
	kdDebug(YAHOO_RAW_DEBUG) ;
}

StatusNotifierTask::~StatusNotifierTask()
{

}

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

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

	if( t->service() == Yahoo::ServiceStealthOffline )
		parseStealthStatus( t );
	else if( t->service() == Yahoo::ServiceAuthorization )
		parseAuthorization( t );
	else
		parsetStatus( t );

	return true;
}

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


	if ( t->service() == Yahoo::ServiceLogon ||
		t->service() == Yahoo::ServiceLogoff ||
		t->service() == Yahoo::ServiceIsAway ||
		t->service() == Yahoo::ServiceIsBack ||
		t->service() == Yahoo::ServiceGameLogon ||
		t->service() == Yahoo::ServiceGameLogoff ||
		t->service() == Yahoo::ServiceIdAct ||
		t->service() == Yahoo::ServiceIddeAct ||
		t->service() == Yahoo::ServiceStatus ||
		t->service() == Yahoo::ServiceStealthOffline ||
		t->service() == Yahoo::ServiceAuthorization ||
		t->service() == Yahoo::ServiceBuddyStatus
	)
		return true;
	else
		return false;
}

void StatusNotifierTask::parsetStatus( YMSGTransfer* t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	if( t->status() == Yahoo::StatusDisconnected &&
		t->service() == Yahoo::ServiceLogoff )
	{
		emit loginResponse( Yahoo::LoginDupl, TQString() );
	}

	TQString	myNick;		/* key = 1 */
	TQString customError;	/* key = 16  */
	TQString nick;		/* key = 7  */
	int state;		/* key = 10  */
	TQString message;	/* key = 19  */
	int flags;		/* key = 13  */
	int away;		/* key = 47  */
	int idle;		/* key = 137 */
	bool utf;		/* key = 97 */
	int pictureChecksum;	/* key = 192 */

	customError = t->firstParam( 16 );
	if( !customError.isEmpty() )
		client()->notifyError( i18n("An unknown error has occurred."), customError, Client::Warning );

	myNick = t->firstParam( 1 );

	for( int i = 0; i < t->paramCount( 7 ); ++i)
	{
		nick = t->nthParam( 7, i );
		state = t->nthParamSeparated( 10, i, 7 ).toInt();
		flags = t->nthParamSeparated( 13, i, 7 ).toInt();
		away = t->nthParamSeparated( 47, i, 7 ).toInt();
		idle = t->nthParamSeparated( 137, i, 7 ).toInt();
		utf = t->nthParamSeparated( 97, i, 7 ).toInt() == 1;
		pictureChecksum = t->nthParamSeparated( 192, i, 7 ).toInt();
		if( utf )
			message = TQString::fromUtf8( t->nthParamSeparated( 19, i, 7 ) );
		else
			message = t->nthParamSeparated( 19, i, 7 );

		if( t->service() == Yahoo::ServiceLogoff || ( state != 0 && flags == 0 ) )
			emit statusChanged( nick, Yahoo::StatusOffline, TQString(), 0, 0, 0 );
		else
			emit statusChanged( nick, state, message, away, idle, pictureChecksum );
	}
}

void StatusNotifierTask::parseAuthorization( YMSGTransfer* t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	TQString nick;		/* key = 4  */
	TQString msg;		/* key = 14  */
	int state;		/* key = 13  */
	bool utf;		/* key = 97 */

	utf = t->firstParam( 97 ).toInt() == 1;
	nick = t->firstParam( 4 );
	if( utf )
		msg = TQString::fromUtf8( t->firstParam( 14 ) );
	else
		msg = t->firstParam( 14 );
	state = t->firstParam( 13 ).toInt();

	if( state == 1 )
	{
		emit( authorizationAccepted( nick ) );
	}
	else if( state == 2 )
	{
		emit( authorizationRejected( nick, msg ) );
	}
	else	// This is a request
	{
		TQString fname = t->firstParam( 216 );
		TQString lname = t->firstParam( 254 );
		TQString name;
		if( !fname.isEmpty() || !lname.isEmpty() )
			name = TQString("%1 %2").arg(fname).arg(lname);

		kdDebug(YAHOO_RAW_DEBUG) << "Emitting gotAuthorizationRequest( " << nick<< ", " << msg << ", " << name << " )" << endl;
		emit gotAuthorizationRequest( nick, msg, name );
	}
}

void StatusNotifierTask::parseStealthStatus( YMSGTransfer* t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	TQString nick;		/* key = 7  */
	int state;		/* key = 31  */

	nick = t->firstParam( 7 );
	state = t->firstParam( 31 ).toInt();

	emit stealthStatusChanged( nick, ( state == 1 ) ? Yahoo::StealthActive : Yahoo::StealthNotActive );
}

#include "statusnotifiertask.moc"