/***************************************************************************
 *   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.             *
 ***************************************************************************/
#ifndef LIBEVAPACKET_H
#define LIBEVAPACKET_H

#include "evadefines.h"

// this is the base class for all QQ protocol classes
class Packet{
public:	
	Packet() {};
	// this is for outcoming packets
	Packet(const short version, const short command, const short sequence) ;
	// this is for incoming packets
	Packet( unsigned char *buf, int *len);
	Packet(const Packet &rhs);
	~Packet();
	
	bool operator==(const Packet &rhs)  const;
	Packet &operator=(const Packet &rhs);
	const int hashCode() ;
	
	const short getVersion() const {   return version;};
	const short getCommand() const {   return command;};
	const short getSequence()  const {   return sequence;};
	
	
	void setVersion(const short version) { this->version = version; };
	void setCommand(const short command) { this->command = command; };
	void setSequence(const short sequence) { this->sequence = sequence; };
	

	static const int getQQ() { return qqNum; }
	static void setQQ(const int id) { qqNum = id; }

	static void setUDP(bool isUDP) { mIsUDP = isUDP; };
	static bool isUDP() { return mIsUDP; };
	static void setPasswordKey(const unsigned char* pkey);
	
	static void clearAllKeys();      // called this after logged out to release memery
protected:
	short version;
	short command;
	short sequence; 
	
	static unsigned char iniKey[16];
	
	// any key is 16 bytes long defined in evadefines as QQ_KEY_LENGTH
	static unsigned char *getSessionKey()  { return sessionKey; }
	static unsigned char *getPasswordKey()  { return passwordKey; }
	static unsigned char *getFileSessionKey()  { return fileSessionKey; }
	
	static void setSessionKey(const unsigned char *skey); 
	static void setFileSessionKey(const unsigned char *fskey);

private: 
	static int qqNum;
	static bool mIsUDP;
	static unsigned char *sessionKey;
	static unsigned char *passwordKey;
	static unsigned char *fileSessionKey;  
};

// this is the parent class for all sent-out packets
class OutPacket : public Packet {
public:
	OutPacket() {}
	OutPacket(const short command, const bool needAck);  
	OutPacket(const OutPacket &rhs);
	virtual ~OutPacket() {}

	const int getResendCount() const { return resendCount; }
	
	bool fill(unsigned char *buf, int *len); 	
	bool needAck() const { return mNeedAck; };
	const bool needResend() { return (--resendCount) != 0; }
	OutPacket &operator=( const OutPacket &rhs);
	
protected:
	virtual int putBody(unsigned char *buf);

private:
	static short startSequenceNum;
	bool mNeedAck;
	int resendCount;
	int  putHead(unsigned char *buf);
	void encryptBody(unsigned char *b, int length, 
			unsigned char *decryptedBody, int *bodyLen);
};

// this class is inherited by all received-in packet
class InPacket : public Packet {
public:
	InPacket() {}
	InPacket( unsigned char *buf, int len);
	InPacket(const InPacket &rhs);
	virtual ~InPacket();
	
        void parse() { parseBody(); }
	const int getLength() const { return bodyLength; };
	unsigned char * getBody() const { return decryptedBuf; };
        void setInPacket(const InPacket *packet);
	InPacket &operator=( const InPacket &rhs);
protected:	
	int bodyLength;
	unsigned char *decryptedBuf;
	
	virtual void parseBody() {};  
private: 
	void  decryptBody(unsigned char * buf, int len,
			unsigned char *body, int *bodyLen);    
};

#endif
