summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/yahoo/libkyahoo/conferencetask.cpp
blob: 9ab0d4c1fe980470b1d40a2b6f71389fffa1ca9b (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
/*
    Kopete Yahoo Protocol
    Handles conferences

    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 "conferencetask.h"
#include "transfer.h"
#include "ymsgtransfer.h"
#include "yahootypes.h"
#include "client.h"
#include <qstring.h>
#include <qstringlist.h>
#include <kdebug.h>

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

ConferenceTask::~ConferenceTask()
{
}

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

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

 	if( t->service() == Yahoo::ServiceConfInvite ||
		t->service() == Yahoo::ServiceConfAddInvite)
 		parseInvitation( t );
	else if( t->service() == Yahoo::ServiceConfMsg )
		parseMessage( t );
	else if( t->service() == Yahoo::ServiceConfLogon )
		parseUserJoined( t );
	else if( t->service() == Yahoo::ServiceConfLogoff )
		parseUserLeft( t );
	else if( t->service() == Yahoo::ServiceConfDecline )
		parseUserDeclined( t );

	return true;
}

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

	if ( t->service() == Yahoo::ServiceConfInvite ||
		t->service() == Yahoo::ServiceConfLogon ||
		t->service() == Yahoo::ServiceConfDecline ||
		t->service() == Yahoo::ServiceConfLogoff ||
		t->service() == Yahoo::ServiceConfAddInvite ||
		t->service() == Yahoo::ServiceConfMsg )
		return true;
	else
		return false;
}

void ConferenceTask::parseInvitation( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	int i = 0;
	QString who = t->firstParam( 50 );
	QString room = t->firstParam( 57 );
	bool utf = QString( t->firstParam( 97 ) ).toInt() == 1;
	QString msg;
	if( utf )
		msg = QString::fromUtf8( t->firstParam( 58 ) );
	else
		msg = t->firstParam( 58 );

	QStringList members;
	for( i = 0; i < t->paramCount( 52 ); ++i )
		members.append( t->nthParam( 52, i ) );
	for( i = 0; i < t->paramCount( 53 ); ++i )
		members.append( t->nthParam( 53, i ) );
	if( who == client()->userId() )
		return;

	if( !who.isEmpty() && !room.isEmpty() )
		emit gotInvite( who, room, msg, members );
}

void ConferenceTask::parseMessage( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	QString room = t->firstParam( 57 );
	QString from = t->firstParam( 3 );
	bool utf = QString( t->firstParam( 97 ) ).toInt() == 1;
	QString msg;
	if( utf )
		msg = QString::fromUtf8( t->firstParam( 14 ) );
	else
		msg = t->firstParam( 14 );

	if( !msg.isEmpty() )
		emit gotMessage( from, room, msg );
}

void ConferenceTask::parseUserJoined( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	QString room = t->firstParam( 57 );
	QString who = t->firstParam( 53 );

	if( !who.isEmpty() && !room.isEmpty() )
		emit userJoined( who, room );
}

void ConferenceTask::parseUserLeft( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	QString room = t->firstParam( 57 );
	QString who = t->firstParam( 56 );

	if( !who.isEmpty() && !room.isEmpty() )
		emit userLeft( who, room );
}

void ConferenceTask::parseUserDeclined( YMSGTransfer *t )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	QString room = t->firstParam( 57 );
	QString who = t->firstParam( 54 );
	QString msg = t->firstParam( 14 );

	if( !who.isEmpty() && !room.isEmpty() )
		emit userDeclined( who, room, msg );
}

void ConferenceTask::inviteConference( const QString &room, const QStringList &members, const QString &msg )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceConfInvite);
	t->setId( client()->sessionID() );
	t->setParam( 1, client()->userId().local8Bit() );
	t->setParam( 50, client()->userId().local8Bit() );
	t->setParam( 57, room.local8Bit() );
	t->setParam( 58, msg.local8Bit() );
	t->setParam( 97, 1 );
	for( QStringList::const_iterator it = members.begin(); it != members.end(); ++it )
		t->setParam( 52, (*it).local8Bit() );
	t->setParam( 13, "0" );

	send( t );
}

void ConferenceTask::addInvite( const QString &room, const QStringList &who, const QStringList &members, const QString &msg )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

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

	QString whoList = who.first();
	for( int i = 1; i < who.size(); i++ )
		whoList += QString(",%1").arg( who[i] );
	t->setParam( 51, whoList.local8Bit() );

	t->setParam( 57, room.local8Bit() );
	t->setParam( 58, msg.local8Bit() );
	t->setParam( 97, 1 );
	for( QStringList::const_iterator it = members.begin(); it != members.end(); ++it )
	{
		t->setParam( 52, (*it).local8Bit() );
		t->setParam( 53, (*it).local8Bit() );	// Note: this field should only be set if the buddy has already joined the conference, but no harm is done this way
	}
	t->setParam( 13, "0" );

	send( t );
}

void ConferenceTask::joinConference( const QString &room, const QStringList &members )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceConfLogon);
	t->setId( client()->sessionID() );
	t->setParam( 1, client()->userId().local8Bit() );
	for( QStringList::const_iterator it = members.begin(); it != members.end(); ++it )
		t->setParam( 3, (*it).local8Bit() );
	t->setParam( 57, room.local8Bit() );

	send( t );
}

void ConferenceTask::declineConference( const QString &room, const QStringList &members, const QString &msg )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceConfDecline);
	t->setId( client()->sessionID() );
	t->setParam( 1, client()->userId().local8Bit() );
	for( QStringList::const_iterator it = members.begin(); it != members.end(); ++it )
		t->setParam( 3, (*it).local8Bit() );
	t->setParam( 57, room.local8Bit() );
	t->setParam( 14, msg.utf8() );
	t->setParam( 97, 1 );

	send( t );
}
void ConferenceTask::leaveConference( const QString &room, const QStringList &members )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceConfLogoff);
	t->setId( client()->sessionID() );
	t->setParam( 1, client()->userId().local8Bit() );
	for( QStringList::const_iterator it = members.begin(); it != members.end(); ++it )
		t->setParam( 3, (*it).local8Bit() );
	t->setParam( 57, room.local8Bit() );

	send( t );
}

void ConferenceTask::sendMessage( const QString &room, const QStringList &members, const QString &msg )
{
	kdDebug(YAHOO_RAW_DEBUG) ;

	YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceConfMsg);
	t->setId( client()->sessionID() );
	t->setParam( 1, client()->userId().local8Bit() );
	for( QStringList::const_iterator it = members.begin(); it != members.end(); ++it )
		t->setParam( 53, (*it).local8Bit() );
	t->setParam( 57, room.local8Bit() );
	t->setParam( 14, msg.utf8() );
	t->setParam( 97, 1 );

	send( t );
}
#include "conferencetask.moc"