summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/closeconnectiontask.cpp
blob: 5492694966f73be392ddf5cd6ea039d7ecd34184 (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
/*
    Kopete Oscar Protocol
    closeconnectiontask.h - Handles the closing of the connection to the server

    Copyright (c) 2004 Matt Rogers <[email protected]>

    Kopete (c) 2002-2004 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 "closeconnectiontask.h"

#include <qstring.h>
#include <qvaluelist.h>
#include <kdebug.h>
#include <klocale.h>
#include "connection.h"
#include "transfer.h"
#include "oscarutils.h"

using namespace Oscar;

CloseConnectionTask::CloseConnectionTask( Task* parent )
	: Task(parent)
{
}


CloseConnectionTask::~CloseConnectionTask()
{
}

const QByteArray& CloseConnectionTask::cookie() const
{
	return m_cookie;
}

const QString& CloseConnectionTask::bosHost() const
{
	return m_bosHost;
}

const QString& CloseConnectionTask::bosPort() const
{
	return m_bosPort;
}

bool CloseConnectionTask::take( Transfer* transfer )
{
	QString errorReason;
	WORD errorNum = 0;
	if ( forMe( transfer ) )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "RECV (DISCONNECT)" << endl;

		FlapTransfer* ft = dynamic_cast<FlapTransfer*> ( transfer );
		
		if ( !ft )
		{
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo 
				<< "Could not convert transfer object to type FlapTransfer!!"  << endl;
			return false;
		}
		
		QValueList<TLV> tlvList = ft->buffer()->getTLVList();
			
		TLV uin = findTLV( tlvList, 0x0001 );
		if ( uin )
		{
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "found TLV(1) [UIN], uin=" << QString( uin.data ) << endl;
		}
	
		TLV err = findTLV( tlvList, 0x0008 );
		if ( !err )
			err = findTLV( tlvList, 0x0009 );

		if ( err.type == 0x0008 || err.type == 0x0009 )
		{
			errorNum = ( ( err.data[0] << 8 ) | err.data[1] );
	
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "found TLV(8) [ERROR] error= " << errorNum << endl;
	
			Oscar::SNAC s = { 0, 0, 0, 0 };
			client()->fatalTaskError( s, errorNum );
			return true; //if there's an error, we'll need to disconnect anyways
		}

		TLV server = findTLV( tlvList, 0x0005 );
		if ( server )
		{
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "found TLV(5) [SERVER] " << QString( server.data ) << endl;
			QString ip = server.data;
			int index = ip.find( ':' );
			m_bosHost = ip.left( index );
			ip.remove( 0 , index+1 ); //get rid of the colon and everything before it
			m_bosPort = ip;
		}

		TLV cookie = findTLV( tlvList, 0x0006 );
		if ( cookie )
		{
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "found TLV(6) [COOKIE]" << endl;
			m_cookie.duplicate( cookie.data );
		}
		
		tlvList.clear();
		
		if ( m_bosHost.isEmpty() )
		{
			kdWarning(OSCAR_RAW_DEBUG) << k_funcinfo << "Empty host address!" << endl;
			
			Oscar::SNAC s = { 0, 0, 0, 0 };
			client()->fatalTaskError( s, 0 );
			return true;
		}
		
		kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "We should reconnect to server '" 
			<< m_bosHost << "' on port " << m_bosPort << endl;
		setSuccess( errorNum, errorReason );
		return true;
	}
	return false;
}

bool CloseConnectionTask::forMe( const Transfer* transfer ) const
{
	const FlapTransfer* ft = dynamic_cast<const FlapTransfer*> ( transfer );

	if (!ft)
		return false;

	if ( ft && ft->flapChannel() == 4 )
		return true;
	else
		return false;
}

//kate: tab-width 4; indent-mode csands;