/***************************************************************************
 *   Copyright (C) 2004 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 "evafriend.h"
#include "evadefines.h"
#include <arpa/inet.h>
#include <string.h>

FriendItem::FriendItem(const unsigned char *buf, int *len)
{
    parseData(buf, len);
}

FriendItem::FriendItem(const FriendItem &rhs)
{
	*this = rhs;
}

FriendItem &FriendItem::operator=(const FriendItem &rhs)
{
	qqNum = rhs.getQQ();
	face = rhs.getFace() ;
	age = rhs.getAge();
	gender = rhs.getGender();
	nick = rhs.getNick();
	extFlag = rhs.getExtFlag();
	commonFlag = rhs.getCommonFlag();
	loginTime = rhs.getLoginTime();
	idleTime = rhs.getIdleTime();
	lastRefreshTime = rhs.getLastRefreshTime();
        return *this;
}

void FriendItem::parseData(const unsigned char *buf, int *len)
{
	int qqtemp;
	memcpy(&qqtemp, buf, 4);
	qqNum = ntohl(qqtemp);
	face = buf[5];
	age = buf[6];
	gender = buf[7];
	int nickLen = (int)(buf[8]);
	char *b = (char *)malloc( (nickLen+1)*sizeof(char)); 
	memcpy(b, buf+9, nickLen);
	b[nickLen]=0x00;
	nick.assign(b);
	free(b);
	// 2 bits unknown, ignore
	extFlag = buf[9+nickLen+2];
	commonFlag = buf[9+nickLen+3];
	*len = 9+nickLen+4;
}

/*  ======================================================= */

GetFriendListPacket::GetFriendListPacket() 
	: OutPacket(QQ_CMD_GET_FRIEND_LIST, true),
	  startPosition(QQ_FRIEND_LIST_POSITION_START)
{ 
}

GetFriendListPacket::GetFriendListPacket(const short startPosition)
	: OutPacket(QQ_CMD_GET_FRIEND_LIST, true),
	  startPosition(startPosition)
{
}

GetFriendListPacket::GetFriendListPacket(const GetFriendListPacket &rhs)
	: OutPacket(rhs)
{
	startPosition = rhs.getStartPosition();
}

GetFriendListPacket &GetFriendListPacket::operator=( const GetFriendListPacket &rhs)
{
	*((OutPacket*)this) = (OutPacket)rhs;
	startPosition = rhs.getStartPosition();
        return *this;
}

int GetFriendListPacket::putBody(unsigned char *buf)
{
	//buf[0] = (Q_UINT8)((startPosition>>8) & 0x00ff);
	//buf[1] = (Q_UINT8)(startPosition & 0x00ff);	
	short pos = htons(startPosition);
	memcpy(buf, &pos, 2);
	
	buf[2] = QQ_FRIEND_LIST_SORTED;
	buf[3] = 0x00;
	buf[4] = 0x01;
	return 5;
}

/*  ======================================================= */

GetFriendListReplyPacket::GetFriendListReplyPacket(unsigned char *buf, int len)
	: InPacket(buf, len)
{    
}

GetFriendListReplyPacket::GetFriendListReplyPacket(const GetFriendListReplyPacket &rhs)
	:InPacket(rhs)
{
	position = rhs.getPosition();
	friends = rhs.getFriendList();
}

GetFriendListReplyPacket &GetFriendListReplyPacket::operator=( const GetFriendListReplyPacket &rhs)
{
	*((InPacket *)this) = (InPacket)rhs;
	position = rhs.getPosition();
	friends = rhs.getFriendList();
        return *this;
}

void GetFriendListReplyPacket::parseBody()
{
	short pos;
	memcpy(&pos, decryptedBuf, 2);
	position = ntohs(pos);
	//position = (Q_UINT16)(((char)decryptedBuf[0])<<8) + (Q_UINT8)decryptedBuf[1];
	//printf("bodyLength:%d\n", bodyLength);
	int offset = 2;
	while(bodyLength > offset) {
		int tmp=0;
		friends.push_back(FriendItem(decryptedBuf+offset, &tmp));       
		offset+=tmp;
	}
}


 
