/***************************************************************************
 *   Copyright (C) 2005 by yunfan                                          *
 *   yunfan_zg@163.com                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "evalogintoken.h"
#include "evadefines.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

RequestLoginTokenPacket::RequestLoginTokenPacket()
	: OutPacket(QQ_CMD_REQUEST_LOGIN_TOKEN, true)
{
}

RequestLoginTokenPacket::RequestLoginTokenPacket( const RequestLoginTokenPacket & rhs )
	: OutPacket(rhs)
{
}

RequestLoginTokenPacket & RequestLoginTokenPacket::operator =( const RequestLoginTokenPacket &rhs)
{
	*((OutPacket *)this) = (OutPacket)rhs;
	return *this;
}

int RequestLoginTokenPacket::putBody( unsigned char * buf )
{
	buf[0]=0x00;
	return 1;
}





/**  *************************************************************************************************************/

RequestLoginTokenReplyPacket::RequestLoginTokenReplyPacket()
	: InPacket(), replyCode(0x01), length(0), 
	token(NULL)
{
}

RequestLoginTokenReplyPacket::RequestLoginTokenReplyPacket( unsigned char * buf, int len )
	: InPacket(buf, len), replyCode(0x01), length(0), 
	token(NULL)
{
}

RequestLoginTokenReplyPacket::RequestLoginTokenReplyPacket( const RequestLoginTokenReplyPacket & rhs )
	: InPacket(rhs), replyCode(0x01), length(0), 
	token(NULL)
{
	replyCode = rhs.getReplyCode();
	length = rhs.getTokenLength();
	if(length){
		if(!token)
			token = (unsigned char *)malloc(length * sizeof(unsigned char));
		if(!token){
			length = 0;
			fprintf(stderr, "RequestLoginTokenReplyPacket( const RequestLoginTokenReplyPacket & rhs ): malloc() failed\n");
			return;
		}
		memcpy(token, rhs.getToken(), length);
	}
}

RequestLoginTokenReplyPacket::~RequestLoginTokenReplyPacket()
{
	if(token)
		free(token);
}

RequestLoginTokenReplyPacket & RequestLoginTokenReplyPacket::operator =( const RequestLoginTokenReplyPacket & rhs )
{
	*((InPacket *)this) = (InPacket)rhs;
	replyCode = rhs.getReplyCode();
	length = rhs.getTokenLength();
	if(length){
		if(!token)
			token = (unsigned char *)malloc(length * sizeof(unsigned char));
		if(!token){
			length = 0;
			fprintf(stderr, "RequestLoginTokenReplyPacket( const RequestLoginTokenReplyPacket & rhs ): malloc() failed\n");
			return *this;
		}
		memcpy(token, rhs.getToken(), length);
	}
	return *this;
}

const bool RequestLoginTokenReplyPacket::isReplyOk( )  const
{
	return replyCode == QQ_REQUEST_LOGIN_TOKEN_REPLY_OK;
}

void RequestLoginTokenReplyPacket::parseBody( )
{
	replyCode = decryptedBuf[0];
	if(replyCode == QQ_REQUEST_LOGIN_TOKEN_REPLY_OK){
		length = decryptedBuf[1];
		if(token)
			free(token);
		token = (unsigned char *)malloc(length * sizeof(unsigned char));		
		if(!token){
			length = 0;
			fprintf(stderr, "RequestLoginTokenReplyPacket( const RequestLoginTokenReplyPacket & rhs ): malloc() failed\n");
			return;
		}
		memcpy(token, decryptedBuf + 2, length);
		setLoginToken(token, length); // we set the loginToken now
	}
}

