1 /*
2     Kopete Oscar Protocol
3     logintask.cpp - Handles logging into to the AIM or ICQ service
4 
5     Copyright (c) 2004 Matt Rogers <mattr@kde.org>
6     Copyright (c) 2007 Roman Jarosz <kedgedev@centrum.cz>
7 
8     Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
9 
10     *************************************************************************
11     *                                                                       *
12     * This library is free software; you can redistribute it and/or         *
13     * modify it under the terms of the GNU Lesser General Public            *
14     * License as published by the Free Software Foundation; either          *
15     * version 2 of the License, or (at your option) any later version.      *
16     *                                                                       *
17     *************************************************************************
18 */
19 
20 #include "logintask.h"
21 
22 #include <qtimer.h>
23 #include <kdebug.h>
24 #include <KLocalizedString>
25 
26 #include "connection.h"
27 #include "closeconnectiontask.h"
28 #include "oscarlogintask.h"
29 #include "oscarutils.h"
30 #include "rateinfotask.h"
31 #include "serverversionstask.h"
32 #include "transfer.h"
33 
34 /**
35  * Stage One Task Implementation
36  */
37 
StageOneLoginTask(Task * parent)38 StageOneLoginTask::StageOneLoginTask( Task* parent )
39 	: Task ( parent )
40 {
41 	m_loginTask = nullptr;
42 	m_closeTask = nullptr;
43 }
44 
~StageOneLoginTask()45 StageOneLoginTask::~StageOneLoginTask()
46 {
47 }
48 
take(Transfer * transfer)49 bool StageOneLoginTask::take( Transfer* transfer )
50 {
51 	if ( forMe( transfer ) )
52 	{
53 		kDebug(OSCAR_RAW_DEBUG) << "Starting login";
54 		kDebug(OSCAR_RAW_DEBUG) << "Sending the FLAP version back";
55 
56 		//send the flap version response
57 		FLAP f = { 0x01, 0 , 0 };
58 		Buffer *outbuf = new Buffer;
59 		outbuf->addDWord(0x00000001); //flap version
60 		f.length = outbuf->length();
61 		Transfer* ft = createTransfer( f, outbuf );
62 		send( ft );
63 
64 		m_loginTask = new OscarLoginTask( client()->rootTask() );
65 		m_closeTask = new CloseConnectionTask( client()->rootTask() );
66 		connect( m_loginTask, SIGNAL(finished()), this, SLOT(loginTaskFinished()) );
67 		connect( m_closeTask, SIGNAL(finished()), this, SLOT(closeTaskFinished()) );
68 		m_loginTask->go( Task::AutoDelete );
69 		return true;
70 	}
71 	return false;
72 }
73 
closeTaskFinished()74 void StageOneLoginTask::closeTaskFinished()
75 {
76 	kDebug(OSCAR_RAW_DEBUG) ;
77 	if ( m_closeTask->success() )
78 		setSuccess( m_closeTask->statusCode(), m_closeTask->statusString() );
79 	else
80 		setError( m_closeTask->statusCode(), m_closeTask->statusString() );
81 }
82 
loginTaskFinished()83 void StageOneLoginTask::loginTaskFinished()
84 {
85 	kDebug(OSCAR_RAW_DEBUG) ;
86 	m_cookie = m_loginTask->cookie();
87 	m_bosPort = m_loginTask->bosPort();
88 	m_bosServer = m_loginTask->bosHost();
89 	m_bosEncrypted = m_loginTask->bosEncrypted();
90 	m_bosSSLName = m_loginTask->bosSSLName();
91 
92 	if ( !m_loginTask->success() )
93 	{
94 		disconnect( m_closeTask, SIGNAL(finished()), this, SLOT(closeTaskFinished()) );
95 		setError( m_loginTask->statusCode(), m_loginTask->statusString() );
96 	}
97 }
98 
forMe(const Transfer * transfer) const99 bool StageOneLoginTask::forMe( const Transfer* transfer ) const
100 {
101 	const FlapTransfer* ft = dynamic_cast<const FlapTransfer*> ( transfer );
102 
103 	if (!ft)
104 		return false;
105 
106 	return ( ft && ft->flapChannel() == 1 );
107 }
108 
loginCookie() const109 const QByteArray& StageOneLoginTask::loginCookie() const
110 {
111 	return m_cookie;
112 }
113 
bosServer() const114 const QString& StageOneLoginTask::bosServer() const
115 {
116 	return m_bosServer;
117 }
118 
bosPort() const119 const QString& StageOneLoginTask::bosPort() const
120 {
121 	return m_bosPort;
122 }
123 
bosEncrypted() const124 bool StageOneLoginTask::bosEncrypted() const
125 {
126 	return m_bosEncrypted;
127 }
128 
bosSSLName() const129 const QString& StageOneLoginTask::bosSSLName() const
130 {
131 	return m_bosSSLName;
132 }
133 
134 /**
135  * Stage Two Task Implementation
136  */
StageTwoLoginTask(Task * parent)137 StageTwoLoginTask::StageTwoLoginTask( Task* parent )
138 	: Task( parent )
139 {
140 	//Create our tasks
141 	Task* rootTask = client()->rootTask();
142 	m_versionTask = new ServerVersionsTask( rootTask );
143 	m_rateTask = new RateInfoTask( rootTask );
144 
145 	QObject::connect( m_versionTask, SIGNAL(finished()), this, SLOT(versionTaskFinished()) );
146 	QObject::connect( m_rateTask, SIGNAL(finished()), this, SLOT(rateTaskFinished()) );
147 }
148 
~StageTwoLoginTask()149 StageTwoLoginTask::~StageTwoLoginTask()
150 {
151 	delete m_versionTask;
152 }
153 
take(Transfer * transfer)154 bool StageTwoLoginTask::take( Transfer* transfer )
155 {
156 	bool yes = forMe( transfer );
157 	return yes;
158 }
159 
forMe(const Transfer * transfer) const160 bool StageTwoLoginTask::forMe( const Transfer* transfer ) const
161 {
162 	const FlapTransfer* ft = dynamic_cast<const FlapTransfer*>( transfer );
163 
164 	if (!ft)
165 		return false;
166 
167 	int channel = ft->flapChannel();
168 	if ( channel == 1 )
169 		return true;
170 	else
171 		return false;
172 }
173 
onGo()174 void StageTwoLoginTask::onGo()
175 {
176 	if ( !m_cookie.isEmpty() )
177 	{
178 		//send the flap back
179 		FLAP f = { 0x01, 0, 0 };
180 		Buffer* outbuf = new Buffer();
181 		outbuf->addDWord( 0x00000001 );
182 		outbuf->addTLV( 0x06, m_cookie );
183 		Transfer* ft = createTransfer( f, outbuf );
184 		kDebug(OSCAR_RAW_DEBUG) << "Sending the login cookie back";
185 		send( ft );
186 	}
187 	else
188 		setError( -1, QString() );
189 	return;
190 }
191 
setCookie(const QByteArray & newCookie)192 void StageTwoLoginTask::setCookie( const QByteArray& newCookie )
193 {
194 	m_cookie = newCookie;
195 }
196 
cookie()197 const QByteArray& StageTwoLoginTask::cookie()
198 {
199 	return m_cookie;
200 }
201 
versionTaskFinished()202 void StageTwoLoginTask::versionTaskFinished()
203 {
204 	//start the rate info task
205 	m_rateTask->go( Task::AutoDelete );
206 }
207 
rateTaskFinished()208 void StageTwoLoginTask::rateTaskFinished()
209 {
210 	setSuccess( 0, QString() );
211 }
212 
213