summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/groupwise/libgroupwise/inputprotocolbase.cpp
blob: 0a7d7d8d7e3ec54391ec4f43116ddd1da3e6c580 (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
/*
    Kopete Groupwise Protocol
    inputprotocolbase.cpp - Ancestor of all protocols used for reading GroupWise input

    Copyright (c) 2004      SUSE Linux AG	 	 http://www.suse.com
    
    Kopete (c) 2002-2004 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 <kdebug.h>

#include "gwerror.h"

#include "gwfield.h"
#include "inputprotocolbase.h"

InputProtocolBase::InputProtocolBase(TQObject *tqparent, const char *name)
 : TQObject(tqparent, name)
{
}


InputProtocolBase::~InputProtocolBase()
{
}

void InputProtocolBase::debug( const TQString &str )
{
#ifdef LIBGW_USE_KDEBUG
	kdDebug( 14191 ) << "debug: " << str << endl;
#else
	qDebug( "GW RAW PROTO: %s\n", str.ascii() );
#endif
}

uint InputProtocolBase::state() const
{
	return m_state;
}

bool InputProtocolBase::readString( TQString &message )
{
	uint len;
	TQCString rawData;
	if ( !safeReadBytes( rawData, len ) )
		return false;
	message = TQString::fromUtf8( rawData.data(), len - 1 );
	return true;
}


bool InputProtocolBase::okToProceed()
{
	if ( m_din.device() )
	{
		if ( m_din.atEnd() )
		{
			m_state = NeedMore;
			debug( "InputProtocol::okToProceed() - Server message ended prematurely!" );
		}
		else
			return true;
	}
	return false;
}

bool InputProtocolBase::safeReadBytes( TQCString & data, uint & len )
{
	// read the length of the bytes
	TQ_UINT32 val;
	if ( !okToProceed() )
		return false;
	m_din >> val;
	m_bytes += sizeof( TQ_UINT32 );
	if ( val > NMFIELD_MAX_STR_LENGTH )
		return false;
	//qDebug( "EventProtocol::safeReadBytes() - expecting %i bytes", val );
	TQCString temp( val );
	if ( val != 0 )
	{
		if ( !okToProceed() )
			return false;
		// if the server splits packets here we are in trouble,
		// as there is no way to see how much data was actually read
		m_din.readRawBytes( temp.data(), val );
		// the rest of the string will be filled with FF,
		// so look for that in the last position instead of \0
		// this caused a crash - guessing that temp.length() is set to the number of bytes actually read...
		// if ( (TQ_UINT8)( * ( temp.data() + ( temp.length() - 1 ) ) ) == 0xFF )
		if ( temp.length() < ( val - 1 ) )
		{
			debug( TQString( "InputProtocol::safeReadBytes() - string broke, giving up, only got: %1 bytes out of %2" ).tqarg( temp.length() ).tqarg( val ) );
			m_state = NeedMore;
			return false;
		}
	}
	data = temp;
	len = val;
	m_bytes += val;
	return true;
}

#include "inputprotocolbase.moc"